Calling external script on breakpoint with Register Values as parameters - trace32

I would like to script Trace32 so I can dump register state and pass to my script on a breakpoint trigger.
I am currently looking at the /CMD flag.
Is there any way I can set a breakpoint in the format of this:
Break.set main /CMD "OS.Command MyScript.sh $R0 $R1 $R2 ..."
where I am dumping the registers and passing it to MyScript as parameters.
My backup plan is to use wp.Register and have my script monitor file system instead.
Thanks ahead for help!

I have this solution.
When setting the breakpoint, instead use /CMD "DO bkpt_trigger.cmm"
Then in bkpt_trigger.cmm
&r0=Register(R0)
&r1=Register(R1)
...
Os.Command echo &r0 &r1

Related

Is it possible to register commands to a breakpoint from within an external file in GDB?

GDB allows registering a set of commands to a specific breakpoint via commands NUM syntax. I need to register the set of commands for a specific breakpoint via an external file, by using a syntax something like the following:
commands ./main.c:18
silent
print buffer[0]
cont
end
commands ./io.c:29
silent
printf "Hello world %i\n", myvar1
cont
end
The commands path/to/file:XX syntax is made up by me. Because the NUM in commands NUM syntax requires exactly the breakpoint's runtime ID number (assigned by GDB), I can not use a deterministic syntax for that purpose.
I'm currently registering breakpoints via a text file with such a content:
break ./main.c:18
break ./io.c:29
and then issuing source breakpoints.txt command inside GDB. It seems that there is no way to register commands at the same time while registering a breakpoint:
(gdb) help break
Set breakpoint at specified line or function.
break [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]
PROBE_MODIFIER shall be present if the command is to be placed in a
probe point. Accepted values are -probe' (for a generic, automatically guessed probe type), -probe-stap' (for a SystemTap probe) or
`-probe-dtrace' (for a DTrace probe).
LOCATION may be a line number, function name, or "*" and an address.
If a line number is specified, break at start of code for that line.
If a function is specified, break at start of code for that function.
If an address is specified, break at that exact address.
With no LOCATION, uses current execution address of the selected
stack frame. This is useful for breaking on return to a stack frame.
THREADNUM is the number from "info threads".
CONDITION is a boolean expression.
Multiple breakpoints at one place are permitted, and useful if their
conditions are different.
Question
Is there any easy way to set some predetermined commands for a predetermined breakpoint from within a file?
If not, is there any equivalent way to pass the (gdb) info breakpoints output to a file or a program while pipe is not available in GDB (version 5.3)? Currently I'm trying a workaround by using logging feature for that purpose:
set logging file /tmp/breakpoints
set logging on
info breakpoints
set logging off
Is there any easy way to set some predetermined commands for a predetermined breakpoint from within a file?
Yes: if you use commands without NUM, the commands will apply to the last breakpoint set. So you want something like:
break main.c:18
commands
silent
print buffer[0]
cont
end

Check in Trace 32 on which CPU breakpoint stopped

Does anyone knows if that is possible to check ID of CPU on which I reached breakpoint?
I want to print it out and resume execution immediately, so likely need a t32 cmd or global variable.
You get the currently active core number with PRACTICE function CORE() e.g. like this
PRINT CORE()
while you can always execute a command when you hit a breakpoint with the /CMD option of the Break.Set command:
Break.Set <addr> /CMD "<TRACE32 command>"
Putting both together you get
Break.Set 0x10000 /CMD "PRINT ""Core "" CORE() "" stopped at "" PP()"
Note: In TRACE32 double-quotes are escaped with double-quotes. Function PP() returns the current program counter. If you want to restart you core immediately add option /RESUME to Break.Set.

Lauterbach execute script when breakpoint is hit

I am using Lauterbach to debug a PowerPC embedded C software. I want to execute the below ALGO from a .cmm(PRACTICE) script. Pleas let me know if it is possible:
Set Breakpoint
When Breakpoint is hit, execute a .cmm file. This .cmm file will rewrite the values of an array.
Continue execution of program
I don't want to stub the whole function. The code has to be untouched.
Set the breakpoint with
Break.Set <addr> /Program /CMD "DO myScript.cmm"
To continue the execution of the target program, add the command Go to the end of the called PRACTICE script.
If you can't add the command Go to the end of the called PRACTICE script, you'll need a veneer-script like this:
// Content of myScript.cmm
DO myAlgorithm.cmm
Go
ENDDO
The Break.Set command knows also an option /RESUME, but this is not suitable for your case, since it won't wait until the called PRACTICE script has finished.
As you have mentioned !
I don't want to stub the whole function. The code has to be untouched.
You can try this;
;set a breakpoint on function
BREAK.SET <function_name/addr>\<LINE NUMBER>
;store address of current program counter(PC)
&pc=r(pc)
&call=address.offset(<function_name/addr>\<LINE NUMBER>) ;This will give the address of a function where breakpoint is set.
;Compare the address if it hit on correct function
IF (&pc==&call)
Do call_meonceHIT.cmm ;your desired .cmm script.
Break.Delete /ALL ; to delete all the set breakpoint.
This will make sure that breakpoint is hitting correct function or runnable.

How can I use a variable name instead of addresses when debugging valgrind runs with gdb?

Let's say I'm debugging with valgrind and gdb by doing:
$ valgrind --vgdb-error=0 ./magic
...and then in a second terminal:
$ gdb ./magic
...
(gdb) target remote | /usr/lib/valgrind/../../bin/vgdb
If I want to examine the defined-ness of some memory, I can use:
(gdb) p &batman
$1 = (float *) 0xffeffe20c
(gdb) p sizeof(batman)
$2 = 4
(gdb) monitor get_vbits 0xffeffe20c 4
ffffffff
Using three commands to do one thing is kind of annoying, especially since I usually want to do this a few times for many different variables in the same stack frame. But if I try the obvious thing, I get:
(gdb) monitor get_vbits &batman sizeof(batman)
missing or malformed address
Is it possible to get gdb to evaluate &batman and sizeof(batman) on the same line as my monitor command?
But if I try the obvious thing, I get: missing or malformed address
This is from GDB doc (http://sourceware.org/gdb/onlinedocs/gdb/Connecting.html#index-monitor-1210) for the monitor cmd:
monitor cmd
This command allows you to send arbitrary commands
directly to the remote monitor. Since gdb doesn't care about the
commands it sends like this, this command is the way to extend gdb—you
can add new commands that only the external monitor will understand
and implement.
As you can see "gdb doesn't care about the commands it sends like this". It probably means that the command after monitor is not processed in any way and sent AS IS.
What you can do to evaluate your variable on the same line is to use user defined commands in gdb (http://sourceware.org/gdb/onlinedocs/gdb/Define.html). Define your own comand and use the eval gdb command to prepare your command with necessary values (http://sourceware.org/gdb/current/onlinedocs/gdb/Output.html#index-eval-1744):
define monitor_var
eval "monitor get_vbits %p %d", &$arg0, sizeof($arg0)
end
And then use it like this:
(gdb) monitor_var batman

load breakpoint file error

I have previously saved list of breakpoints using
save breakpoints blist
now after compiling the program when I try to reload the same break points with the load command
load blist
I get this error
You can't do that when your target is `exec'
How to resolve this ?
load blist
Try source blist instead.
From "help save breakpoints":
Save current breakpoint definitions as a script.
The way to read a script is the source command. The load command means something different entirely.
I have breakpoints saved to file, say gdb.br, file content looks like:
br /project/src/file.c : 100
commands
silent
printf "\nbacktrace:\n"
bt
cont
end
This break just output backtrace and continue execution. You may also use simple breaks, like:
br /project/src/file.c : 100
br className::methodName
I have a lot of breaks there - gdb fails to add them via copy-past. Also I can't use load command on my multi-threading system.
To attach with gdb and load breakpoints I use this sequence:
gdb -p 1523 -x gdb.br
Where 1523 is process pid you want to attach to. -x is primarily
intedent to be used to load commands, set environment, but also could be used to load your breaks.
Hope this will help.