gdb alias for quick saving/loading of breakpoints - c

So I'm pretty new to gdb, and have just learned that you can save breakpoints with:
save breakpoints filename
and load them with
source filename
which is great, but because it's something I more or less plan on doing every time I enter/exit gdb, I'd like to get it down to a quick alias.
So, in my ~/.gdbinit I have the lines
alias savebps = save breakpoints .gdb_bps
alias loadbps = source .gdb_bps
loadbps
Unfortunately, every time I open gdb I get the error:
Invalid command to alias to: save breakpoints .gdb_bps
I know(/ strongly think) I have the syntax correct, as I've tested
alias savebps = help
and that alias works. So I think it's an issue with having a non-gdb command word (the filename) as part of an alias.
So, my questions are this:
Am I being totally stupid and there's already a great way to auto-save and maintain my list of breakpoints?
Can GDB have filenames in aliases? Or am I looking for something other than an 'alias'?
If they CAN have filenames in them, what am I doing wrong?
Oh and as a note the '.gdb_bps' is an arbitrary file name I just kinda came up with as it'd be a nice unobtrusive thing to easily .gitignore and stuff.
Thanks!

Can GDB have filenames in aliases?
Looks like no. It looks like aliases can't have any arguments to commands, not only filenames. This alias fails also:
(gdb) alias spe = set print elements 0
Invalid command to alias to: set print elements 0
Or am I looking for something other than an 'alias'?
Yes, you can use user-defined command instead:
(gdb) define savebps
Type commands for definition of "savebps".
End with a line saying just "end".
>save breakpoints .gdb_bps
>end
(gdb)
(gdb) define loadbps
Type commands for definition of "loadbps".
End with a line saying just "end".
>source .gdb_bps
>end
(gdb)

Related

using GDB with arguments

For a class assignment we needed to write a compiler. This includes an optimizer portion. In other words, we take in a file with some "code". An output file is generated. In the second step we take in the outputted code and remove any "dead" code and re-output to a second file. I have some problems with the optimizer portion and would like to use gdb. But I can't get gdb to operate properly with the input and output files arguments. The way we would normally run the optimizer is:
./optimize <tinyL.out> optimized.out
where tinyL.out is the file outputted in the first step and optimized.out is the file I want to output with the new optimized and compiled code.
I have searched Google for the solution and the tips I have found do not seem to work for my situation. Most people seem to want to only accept an input file and not output a separate file as I need to do.
Any help is appreciated (of course)
I'm not exactly sure what you're asking. But since I'm not yet able to comment everywhere, I write this answer with a guess and edit/delete if necessary.
When GDB is started and before you start the program you wish to debug, set the arguments you want to use with set args.
A reference to the documentation.
You just need to do the file redirection within gdb.
gdb ./optimize
(gdb) run < tinyL.out > optimized.out
https://stackoverflow.com/a/2388594/5657035

Cannot break on symbol -[UIView _viewHierarchyUnpreparedForConstraint:]

I am debugging a problem with programmatically adding constraints. When my app crashes LLDB tells me to break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug the problem further. However, when I add a breakpoint with LLDB :
b s -n -[UIView _viewHierarchyUnpreparedForConstraint:]
I get the following warning from LLDB:
WARNING: Unable to resolve breakpoint to any actual locations.
I also tried adding a symbolic breakpoint using the Breakpoint navigator + option.
So it looks to me as if this symbol does not exist.
How can i see a list of all the symbols generated in order to make sure that this symbol exists or not?
thanks for your help
-Malena
The lldb command line is space-delimited, so if you want to pass arguments or option values to it that have spaces in them, you need to use quotes to protect the spaces. See if this works:
(lldb) b s -n "-[UIView _viewHierarchyUnpreparedForConstraint:]"
The careful reader would have noted that the command as you typed it had "-[UIView" as the option value for -n and then a dangling argument "_viewHierarchyUnpreparedForConstraint:]" and by rights (since break set takes no arguments) you should have gotten an error about break set taking no arguments. THAT is a bug...
To answer your other question, the lldb command:
(lldb) image dump symtab
will dump all the symbols in the program. You can scope this to a particular library by adding it to the command line, so for instance this one is probably in UIKit, so:
(lldb) image dump symtab UIKit
will show you only the symbols in UIKit. There's also a command to lookup particular symbols by name, image lookup -n though in general if the breakpoint setter can't find them, image lookup isn't going to find them either.

Checking when a variable is modified

Using Valgrind or any other debugger in Linux, how can one see places where a variable is modified. I am using gcc. Note that I don't want to step into the code using gdb. I just want to run the program and have the debugger report me in the end, places where the variable is modified in the code.
Hm, thinking about it it's not exact duplicate of Can I set a breakpoint on 'memory access' in GDB?, because it asks a little bit more. So:
Use gdb
Find the address you want to watch (hardware watchpoints only work for watching address, so you have to run it to the point where the variable or object are instantiated, take their address and use the watch command on that address.
Attach command to the address to give you a backtrace (or any other info you need to collect) and continue.
So you'll have something like:
p &variable
watch *$$
cmd
bt
c
end
(I am not completely sure with the $$, I normally use the $n as printed by the p command).
Use Breakpoint Command Lists to do this in gdb. You will have to know the address of variable to watch. Set watchpoint with a series of commands like this:
watch *0xfeedface
commands
silent
bt
cont
end
You can also optionally save all this output to log file. Look gdb doc for more details.

help with gdb traces (or similar)

I have an application. I have the source code (in C). I can compile it anyway I want. Add whatever tool I want to it. Etc. However, I do not want to pepper the source code with a bunch of printf's. I want to be able to produce a log of some sort that shows when a particular value (some member of a global structure for example) is written to (its value changes). I'd like to be able to show source file and line number, and the old and new value.
I'd also like to be able to specify the value by name, not address. But address is OK. Bonus points if I can specify a value that is local to a function.
I'm still scratching my head trying to figure out gdb's trace commands. Any help is greatly appreciated. Thanks.
First, you'll need to make sure to compile your program with debug symbols, and probably w/o optimization to make gdb most useful. For gcc, that'd be -g -O0.
Second, the feature you're looking for isn't tracing, its watchpoints.
(gdb) help watch
Set a watchpoint for an expression.
A watchpoint stops execution of your program whenever the value of
an expression changes.
So, given some example code:
int main() {
int a;
a = 1;
a = 2;
return 0;
}
you can then run gdb on it, and:
(gdb) b main
Breakpoint 1 at 0x80483a5: file test.c, line 4.
(gdb) run
Starting program: /tmp/test
Breakpoint 1, main () at test.c:4
4 a = 1;
(gdb) watch a
Hardware watchpoint 2: a
(gdb) c
Continuing.
Hardware watchpoint 2: a
Old value = -1207552288
New value = 2
main () at test.c:8
8 return 0;
it's working slightly funny due to a being on the stack, not memory. And if optimization were on, it'd work even less: a would be optimized out.
As stated already, you need to set a watchpoint on your variable.
The you use the "commands" command
(gdb) help commands
Set commands to be executed when a breakpoint is hit.
Give breakpoint number as argument after "commands".
With no argument, the targeted breakpoint is the last one set.
The commands themselves follow starting on the next line.
Type a line containing "end" to indicate the end of them.
Give "silent" as the first line to make the breakpoint silent;
then no output is printed when it is hit, except what the commands print.
So, find the watchpoint number from the watch command, and do this (assuming your watch is the second break)
(gdp) commands 2
> print a
> cont
> end
Assuming a is the variable you want. You can leave out the print line if you're happy with the output gdb gives you anyway.
You can also use the commands in your original breakpoint to set the watchpoint and continue.
Thank you both #derobert and #peter! I finally went back to this, and this:
break main
commands
watch somevar
commands
cont
end
cont
end
run
does the trick. This works when "somevar" is global, or local to "main". If "somevar" is local to anther function, just replace "main" with that function name above.
Put these commands in a file (e.g. "gdbscript") and run gdb like:
gdb -x gdbscript a.out

Make GDB print control flow of functions as they are called

How do I make gdb print functions of interest as they are called, indented according to how deep in the stack they are?
I want to be able to say something like (made up):
(gdb) trace Foo* Bar* printf
And have gdb print all functions which begin with Foo or Bar, as they are called. Kind of like gnu cflow, except using the debugging symbols and only printing functions which actually get called, not all possible call flows.
Tools which won't help include cachegrind, callgrind and oprofile, which order the results by which functions were called most often. I need the order of calling preserved.
The wildcarding (or equivalent) is essential, as there are a lot of Foo and Bar funcs. Although I would settle for recording absolutely every function. Or, perhaps telling gdb to record all functions in a particular library.
Some GDB wizard must have a script for this common job!
In your case I would turn to the define command in gdb, which allows you to define a function, which can take up to 10 arguments.
You can pass in the names of functions to "trace" as arguments to the function you define, or record them all in the function itself. I'd do something like the following
define functiontrace
if $arg0
break $arg0
commands
where
continue
end
end
if $arg1
...
Arguments to a user-defined function in gdb are referenced as $arg0-$arg9. Alternatively, you could just record every function you wanted to trace in the function, instead of using $arg0-9.
Note: this will not indent as to depth in the stack trace, but will print the stack trace every time the function is called. I find this approach more useful than strace etc... because it will log any function you want, system, library, local, or otherwise.
There's rbreak cmd accepting regular expression for setting breakpoints. You can use:
(gdb) rbreak Foo.*
(gdb) rbreak Bar.*
(gdb) break printf
See this for details on breakpoints.
Then use commands to print every function as it's called. E.g. let α = the number of the last breakpoint (you can check it with i br if you missed), then do:
(gdb) commands 1-α
Type commands for breakpoint(s) 1-α, one per line.
End with a line saying just "end".
>silent
>bt 1
>c
>end
(gdb)
Some elaboration: silent suppresses unnecessary informational messages, bt 1 prints the last frame of backtrace (i.e. it's the current function), c is a shortcut for continue, to continue execution, and end is just the delimiter of command list.
NB: if you trace library functions, you may want to wait for lib to get loaded. E.g. set a break to main or whatever function, run app until that point, and only then set breakpoints you wanted.
Use the right tool for the job ;)
How to print the next N executed lines automatically in GDB?
Did you see litb's excellent anwser to a similar post here ?
He uses readelf to get interesting symbols, gdb commands to get the trace, and awk to glue all that.
Basically what you have to change is to modify his gdb command script to remove the 1 depth from backtrace to see the stack and filter specific functions, and reformat the output with an awk/python/(...) script to present it as a tree. (I admit I'm too lazy to do it now...)
You may call gdb in batch mode (using -x option), break where you need and ask for backtrace (bt), then you filter the result using grep or egrep.
Indents are more difficult, however bt output is ordered so you have current function at the top of the trace and main at very bottom.
So you create file with commands:
br <function name where to break>
run
bt
kill
quit
then run gdb <program> -x<command file>
Filter strings that starts with #<digit> - you get stack trace.

Resources