Stop GDB from stepping through glibc functions - c

Sorry for the nondescript title, couldn't think of anything better.
When I run my program through GDB to find a Segmentation Fault, I get thousands of lines of this garbage:
(gdb) step
_IO_new_file_xsputn (f=0x76fa0b40 <_IO_2_1_stdout_>, data=0x7efff284, n=7) at fileops.c
1279 fileops.c: No such file or directory.
(gdb) step
1273 in fileops.c
(gdb) step
1279 in fileops.c
(gdb) step
1286 in fileops.c
(gdb) step
1288 in fileops.c
(gdb) step
1289 in fileops.c
(gdb) step
1292 in fileops.c
(gdb) step
1294 in fileops.c
(gdb) step
1292 in fileops.c
...and on and on and on. It makes debugging very difficult and tedious because:
I must type step (or press Enter) repeatedly.
I can hardly ever even see the output of my program, as it's hidden in pointless information.
If I'm pressing Enter repeatedly, I often miss the Seg Fault I'm looking for.
How can I tell gdb to quit printing the lines like this?
1273 in fileops.c
Is there some config file or command-line option I can use?

Are you sure you need to step through glibc code in fileops.c ?
If you want only to debug your own code you should finish current frame to go out of _IO_new_file_xsputn function after you have stepped into it and continue debugging your code.

How can I tell gdb to quit printing the lines like this?
By using the step command, you asked GDB to print exactly this info.
If you don't want that info, you should stop asking for it.
I often miss the Seg Fault I'm looking for.
If you simply run, GDB will automatically stop when you get a SIGSEGV, you can't miss that.
What you probably want is to stop in your code some time before the SIGSEGV, and step from there. There are several techniques to achieve this, reverse debugging is one of them. Using replay debugging is another.
It's hard to offer more advice, because you didn't show what you are actually trying to do.

That's how step works - it steps through the code.
What about a breakpoint? If you set one the debugger will stop there and let you step through the code manually.
For something like a segmentation fault or similar signals, which abort execution, you can run and backtrace when the program has stopped to gain information about the location execution has stopped at. Then set a breakpoint there and the program will stop at the offending instruction.

Related

Memory read failed for 0x0 in a game loop, need a proper debugging method

My program crashes somewhere during a game loop at variable times independent of input events. I am looking for a suitable debugging method that I can use to find the problem in my code.
When I fire up my lldb debugger against the executable, run it without set breakpoints, I get the following output after some time, when the program crashes:
Process 86823 stopped
* thread #2, queue = 'com.apple.libdispatch-manager', stop reason = EXC_BAD_ACCESS (code=1, address=0x1)
frame #0: 0x0000000000000001
error: memory read failed for 0x0
Target 0: (smoke) stopped.
This tells me, that I am trying to read something from a bad memory address. The issue is, I am not sure how to pinpoint that in my code, as this may or may not occur in a given cycle of a game loop. So just setting a breakpoint is problematic. What is the proper debugging method in this case?

Is there an lldb equivalent of gdb's 'bt full'?

I'm trying to show the variables in all stack frames, but haven't found a good way to do this in one shot like I can with bt full. The 'frame variable' only shows the variables in the current frame and there didn't seem to be any equivalent in the LLDB mapping guide.
bt all is the short alias, thread backtrace all is the full command.
In the olden days, in gdb you needed to do thread apply all backtrace iirc, or t a a bt for short. They must have added bt full since then. I'm not sure if I'm a fan of bt full, honestly. Is it asking for a complete backtrace of a single thread? Or a complete backtrace of all threads? My first impression is that this would only give you a complete backtrace of one thread.
Anyway, not a big fan of gdb's bt full naming, not sure I'd want to add it as a recognized command in lldb, I think lldb's bt all shortcut is a good choice.

GDB - show last n lines executed

Sometimes in GDB I want to see the control flow that got the program to where it is now. Simply put, how do I make GDB print the last x lines executed?
This is yet another use case for Reverse Debugging.
You should start process record and replay at some point:
(gdb) record
When you want to see last executed lines you can go backwards like
this:
(gdb) reverse-step 3
or
(gdb) reverse-next 3
Use this answer https://stackoverflow.com/a/1545732/72178 to actually
print the next N executed lines.
You simply cannot do that (easily) in gdb, because the execution trace of any program is not kept (and keeping it would be very expensive : it would slow down a lot the execution, and it would use a lot of resources - memory & disk space).
You can however use the backtrace or bt command of gdb to show the call stack, that is the current instruction pointer in the current function, the calling function, the calling function of the calling function, and so forth
BTW, if you really wanted it, you might script recent gdb using Python or Guile to give you such information. You certainly could keep the entire trace (by scripting around the step & backtrace functionalities).

how to return to main function in gdb

I'm using gdb for debugging
I get a segmentation fault, and then I want to set another break point in the main function and run the program from the beginning
however, although I have finished the current run
and it shows "THe program is not being run"
when I input 'list'
it shows a code snippet of a libarary file
it means currently I'm not in the main function
If I re-run the program, even if I set the break point at the beginning of the main()
it still get segmentation fault, it means the program is running within the library file
so how to return to the main() function?
thanks!
tips: I'm using libpcap.h and I have a '-lpcap' option when compiling
BTW, when I use break 9
to set a breakpoint at 9, gdb runs the program to the 11-th line? what is wrong with this inaccuracy? thanks!
Simply re-issue the run command. You will lose program state, but not breakpoints which seems to match what you need.
"BTW, when I use break 9 to set a breakpoint at 9, gdb runs the program to the 11-th line" - from this, and other information you've provided, it sounds like perhaps the source code is out of sync with gdb's mapping of addresses to source lines. Have you by any chance been editing the program? Have you recompiled it and restarted gdb? Have you seen any warnings similar to "executable is more recent than source"?
If I re-run the program, even if I set the break point at the
beginning of the main() it still get segmentation fault, it means the
program is running within the library file
Actually it means that you either failed to set breakpoint on main function or program execution not reaches main and gets segmentation fault. Try the following steps:
Rebuild program from scratch with debug info (-g gcc option). Reset breakpoint and watch for any warnings from gdb.
If program still crashes with breakpoint set on main look at stack trace (bt command in gdb). It is probably happening before main and you will not see main in stack trace.

GDB structure output

I haven't worked with gdb for a long time and this feels like a basic question.
I am trying to observe a structure as it changes but rather than break at a specific point and print it out I'd rather let the application run as normal and give me a snapshot of the structure at a specific point. Think a breakpoint that performs an action (print struct) rather than pausing execution.
I am interested in looking at the changes to the structure all at once rather than incrementally. I can get what I want through printf, but gdb is much more elegant.
Update: Thank you for all the responses. I want to watch one struct at a particular point and the commands solution is just what I needed. This has been very helpful.
A nice approach is to set a breakpoint with associated commands, e.g:
break main.c:100
commands 1
print data_structure
continue
end
This runs the two commands print data_structure and continue whenever breakpoint 1 is reached.
If the information held by your data structure might be changed by several code lines, you can also use gdb's watch. Note that it is horribly slow so it should be used carefully. The commands part is just the same.
(gdb) break main
Breakpoint 1 at 0x80483b5:
(gdb) run
Breakpoint 1, main ()
(gdb) watch data_structure
Hardware watchpoint 2: data_structure
(gdb) commands 2
Type commands for when breakpoint 2 is hit, one per line.
End with a line saying just "end".
> print data_structure
> continue
> end
(gdb) continue

Resources