; --------------------------------------------------------------------------------
; @Title: Script to switch to run mode debugging over Ethernet
; @Description:
;  This script configures the TRACE32 to switch to run mode debugging. The debugger
;  communicates over Ethernet with the gdbserver.
; @Author: KJM
; @Copyright: (C) 1989-2014 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Id: runmode_ethernet.cmm 7695 2014-09-30 09:17:52Z kjmal $

  LOCAL &spaceid

  IF !SYSTEM.UP()
  (
      ; We are not connected to the target so try to connect. 
      SYStem.CPU *
      SYStem.Mode Attach
  )
  
  IF !STATE.RUN()
    Go
  
  WinCLEAR
  
  ; Open a terminal window
  WinPOS 0% 50% 50% 50%
  DO ~~/demo/etc/terminal/serial/term.cmm COM3 115200.
  ; Start the t32server
  TERM.Out "gdbserver --multi :2345" 10.
  
  WAIT 1.s
  
  ; Enable the GDB monitor mode in TRACE32
  SYStem.MemAccess GdbMON
  ; Set target IP and port number
  SYStem.PORT 192.168.188.50:2345
  
 ; Enable GDB extended mode
  SYStem.Option gdbEXTENDED ON
  
  ; Enable GDB NonStop Mode
  SYStem.Option gdbNONSTOP ON
  
  ; Enable address extension in TRACE32
  SYStem.Option MMUSPACES ON
  
  ; Switch to run mode
  Go.MONitor
  

  ; Open a TASK.LIST
  WinPOS 50% 0% 50% 100%
  TASK.List
  
  ; Open a List window
  WinPOS 0% 0% 49% 48%
  List.auto
  
; --------------------------------------------------------------------------------
  ; Start a first process for debugging
  PRINT "Starting process hello"
  TASK.RUN /bin/hello
  WAIT 1.s
  &spaceid=TASK.SPACEID("hello")
  ; Load the debug symbols of process "hello" if not already done by the autoloader
  IF !sYmbol.EXIST(main)||ADDRESS.SEGMENT(main)!=&spaceid
  (
    Data.LOAD.Elf hello &spaceid:0 /NoCODE /NoClear
  )
  ; Go to the main function of process hello
  Go main

; -------------------------------------------------------------------------------- 
  ; Attach to the running process "sieve"
  PRINT "Attaching to process sieve"
  &spaceid=TASK.SPACEID("sieve")
  TASK.Attach &spaceid
  ; Load the debug symbols of process "sieve" if not already done by the autoloader
  IF !sYmbol.EXIST(main)||ADDRESS.SEGMENT(main)!=&spaceid
  (
    Data.LOAD.Elf sieve &spaceid:0 /NoCODE /NoClear
  )
  Go main
  
  PRINT ""
  ENDDO