; --------------------------------------------------------------------------------
; @Title: Lynx Application Debugging Script
; @Description: 
;   This script waits for an application to be started,
;   loads the symbols and halts the application at main()
;   NOTE: LynxOS and the LynxOS awareness must be up.
;   Start this script with the process name as argument
;   Example:
;     do app_debug hello      ; waits for "hello" to be started
;   Prerequisites:
;   - LynxOS must be booted
;   - LynxOS awareness must be configured
;   - Autoloader must be configured
; @Keywords: awareness, lynx
; @Author: DIE
; @Copyright: (C) 1989-2014 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Id: app_debug.cmm 6750 2014-04-07 21:27:07Z servera $


; define local macros
 LOCAL &process &breakaddr

; get the process name as parameter to the script
 ENTRY &process


; Activate autoloader for processes:
 TASK.sYmbol.Option AutoLoad Process
 

; Delete possibly existing breakpoints of previous process runs
 
 IF sYmbol.EXIST("\\&process")
   sYmbol.Delete \\&process
 GROUP.Delete "&process"


; Wait for process to be loaded.
; The process must be started by executing it in the LynxOS console.

 ; We cannot load process symbols as long as we don't
 ; know the address translation. The MMU for the new
 ; process is set up, right before it is started at execuser()
 
 IF run()
    Break.direct    ; halt target for breakpoint setting

 ; execuser() is called for every process to start -
 ; the conditional breakpoint halts only, if the desired
 ; process is found in the process table.
 
 IF sYmbol.EXIST(`.execuser`)            ; LynxOS 3.1/4.0
   &breakaddr=ADDRESS.OFFSET(`.execuser`)
 ELSE IF sYmbol.EXIST(`.ctx_execuser`)   ; LynxOS-178 2.0
   &breakaddr=ADDRESS.OFFSET(`.ctx_execuser`)
 ELSE IF sYmbol.EXIST(`ctx_execuser`)    ; LynxOS-178 2.1, 2.2
   &breakaddr=ADDRESS.OFFSET(`ctx_execuser`)
 ELSE
 (
   PRINT "process execution label not found."
   ENDDO
 )

 Break.Delete &breakaddr        ; delete previous set breakpoints
 Break.Set &breakaddr /CONDition task.proc.space("&process")!=0xffffffff

 ON PBREAKAT &breakaddr GOTO continue1  ; if breakpoint reached: continue
 Go.direct         ; let the target run and load the process
 PRINT "waiting for &process to be started..."

 STOP       ; halt script until breakpoint reached
 
 ; breakpoint hit, continue script
continue1:

 Break.Delete &breakaddr    // delete breakpoint
 ON PBREAKAT &breakaddr     // delete script action


; Yep! The process is loaded and we found it.

 ; Now load the process symbols to the space id of the process

 PRINT "process &process started, loading symbols..."

 sYmbol.AutoLOAD.CLEAR "&process"   ; clear possibly previous set
 sYmbol.AutoLOAD.CHECK              ; force new autoloader list
 sYmbol.AutoLOAD.TOUCH "&process"   ; force loading of process symbols
 

; Now set a breakpoint at it's main entry point.
 
 ; There may be more "main" symbols in the system,
 ; we're searching for the right one.
 
 IF sYmbol.COUNT(\\&process\*\main)==0
 (
   PRINT "symbol 'main' of process &process not found"
   ENDDO
 )
   
 Eval 0
 sYmbol.ForEach "Eval address.offset(*)" \\&process\*\main
 &breakaddr=EVAL()

 Break.Set &breakaddr   ; set breakpoint on main

 ON PBREAKAT &breakaddr GOTO continue2  ; if breakpoint reached: continue
 Go.direct         ; let the target run and start the process
 PRINT "waiting for reaching main..."

 STOP       ; halt script until breakpoint reached

 ; breakpoint hit, continue script
continue2: 
         
 Break.Delete &breakaddr    // delete breakpoint
 ON PBREAKAT &breakaddr     // delete script action


 PRINT "done."

 ; NOTE: if finished debugging with a process, or if restarting
 ; the process, you have to delete the symbols and restart the
 ; application debugging. Delete the symbols with
 ; sYmbol.AutoLoad.CLEAR "&process"


 ENDDO
 
