banner



How To Set Breakpoint In Gdb

Kernighan famously said:

Everyone knows that debugging is twice as hard as writing a program in the first identify. And so if you lot're as clever as yous tin be when y'all write it, how will you always debug it?

On the surface, this is a call to continue your code simple, just there is a corollary: debuggability is the limiting gene in software development. Whatsoever your metric for the "goodness" of your software - functioning, features, size, velocity - if you double your effectiveness at debugging you'll double the "goodness" of your software.

Over the last twelvemonth or and so, I shared on the gdbWatchPoint resource centre numerous ways to assist you increase the stride of your debugging.

In this article, I selection 5 of my favorite GDB topics that I believe tin help y'all spend fewer hours debugging and write amend software faster.

Let's dive in.

one. GDB TUI Mode

GDB'due south command-line isn't intuitive, but if you spent some fourth dimension discovering and learning the commands, and so you'll find it very powerful.

I guess, i of the least known, but perhaps the most useful things in GDB is TUI mode, i.eastward. Text User Interface manner.

The trouble with the GDB command-line is that it doesn't give you the context of where you are in the program; for example, if the program hits a breakpoint, so GDB returns you simply one line.

If you type Ctrl-x-a (or tui enable), so another text window opens; the source window shows the source file of the program, also as the current line and the breakpoints. Yous're now in GDB's TUI mode.

TUI Mode

It'due south not all sunshine though. Sometimes the screen messes up a bit but Ctrl-50 refreshes the screen.

It'due south unlikely that the source window alone provides you with all the data you need. Typing Ctrl-x-2 helps you cycle through dissimilar text windows and layouts:

  • source window only,
  • assembly window but,
  • separate-view with source and associates,
  • split-view with source and registers, or
  • carve up-view associates and registers.

These windows are non all visible at the same time. The command window with the GDB prompt and GDB output is always visible.

Ctrl-10-1 returns the layout to the command window with the source or associates pane.

Finally, y'all'll notice that the arrow-keys curlicue the active window in TUI mode, which means that you can't use the up and down arrows to get to your previous commands every bit yous normally would do in the GDB control-line. To move through your command history, use Ctrl-p (previous control) and Ctrl-n (next command) instead; Ctrl-b and Crtl-f respectively to movement the cursor back (left) and forwards (right) to edit the line.

UDB Time Travel Debugger
Find and fix test failures in minutes - including C/C++ race conditions and memory corruptions
Learn more than »

2. GDB Breakpoint Types

A breakpoint lets y'all finish the execution of the program at a time of your choice. Information technology's an essential (and I reckon probably the most used) command in your debugging flow.

There are different breakpoint types.

Basic breakpoints

You set a bones breakpoint with the post-obit command:

suspension [LOCATION] or but b [LOCATION]

Where LOCATION can exist a line number, function name, or "*" and an address.

(gdb) break hello.c:7

Once your program hits a breakpoint, it waits for education. You lot can navigate or footstep through the plan using next, continue, step i, and other commands. To speed upwards your typing, yous can abbreviate all of these commands; n for next, c for go on, si for step i - you become information technology.

Temporary breakpoints

A temporary breakpoint only stops the execution of the plan once. When the plan hits a temporary breakpoint, it deletes automatically, which saves you the trouble of managing and cleaning upward obsolete breakpoints.

You set a temporary breakpoint with the following command:

tbreak [LOCATION]

(gdb) tbreak main

Tip: Using the start command instead of the usual run command sets a temporary breakpoint atmaster().

Conditional breakpoints

Instead of stepping through the execution of the program until it hits the breakpoint again, y'all can specify a condition for a breakpoint that stops the execution if met. You tin can write pretty much whatsoever status you want in the programming linguistic communication of the program that y'all're debugging, which makes conditional breakpoints very powerful and efficient.

Y'all set a provisional breakpoint with the post-obit command:

break [LOCATION] if [Condition]

Here [CONDITION] is a boolean expression, which, in GDB, is TRUE if the result is nonzero; otherwise, information technology is FALSE. The status tin can include a part phone call, the value of a variable, or the event of whatever GDB expression.

(gdb) break my_func if i==5

You tin can make all breakpoint types conditional by adding the suffix, if [CONDITION].

At that place is more yous tin can do.  You tin can prepare a condition on an existing breakpoint by using the breakpoint number every bit a reference.

condition [BREAKPOINT #] [CONDITION]

(gdb) condition 2 i==5

Typing condition [BREAKPOINT #] removes the condition from a breakpoint.

REGEX breakpoints

The regex breakpoint command sets an unconditional breakpoint on all functions that match a regular expression (regex) and prints a list of all breakpoints it set. Once set, these breakpoints are treated just like the breakpoints y'all set with the normal break command. Y'all can delete them, disable them, or make them provisional the same mode every bit whatever other breakpoint.

You set a regex breakpoint with the following command:

rbreak REGEXP

(gdb) rbreak howdy.c::my_func*

When debugging C++ programs, rbreak is particularly useful for setting breakpoints on overloaded functions that are not members of whatsoever special classes.

rbreak saves you lot a lot of time typing and trying to recall exact function names.

In this recording, I encompass the various breakpoint types, and how to utilize each of them in your debugging.

iii. GDB's tight integration with Python

GDB has tight integration with Python. You tin practise all kinds of smart things to help make detecting (and resolving) thorny bugs a breeze. Plus, there are lots of other tricks you can do to customize GDB to your particular project and debugging needs - for instance, scripting routine actions, writing your ain commands, and creating pretty-printers.

Not taking advantage of Python is something you may regret subsequently, a missed opportunity to increase your debugging speed. It's a small investment in time that pays dorsum chop-chop and, over fourth dimension, significantly.

You lot can first small-scale and expand your Python resource over time.

A simple way to become started is by invoking the interpreter with typing python. You tin now use whatsoever Python code, for example, typing print('Hi World'), followed past typing cease calls the python impress() function.

The Python code isn't passed to another process but runs inside the GDB process. Python truly integrates with GDB.

But to employ the Python in GDB properly, you must import the GDB module, which gives you all you need.

(gdb) python import gdb

Switching to TUI mode, you tin employ the .execute command to create a breakpoint, or even better, create an object example of gdb.breakpoint. We at present can manipulate and interrogate this object, enable or disable it, etc.

Breakpoint object

Pretty much anything y'all can practise on the GDB command-line you tin do with a breakpoint from Python. You can suspend commands to a breakpoint, make information technology conditional, or make the breakpoint specific to a thread.

The same is truthful for virtually other commands in GDB; if you tin do it from the GDB command-line, and so you tin can practise it from Python.

Tip: Consider making a gdbinit per project, and committing it into your project's source control and then that everyone working on that project tin do good.

In this video, I dip into the Python integration with GDB to get you lot started. If you've struggled with imagining what Python can do, this one helps you out.

iv. GDB Pretty-Printers

We all employ structures and classes in the code we write, and GDB endeavors to display what these are but misses the contextual data; when yous use a handful of members, and in item unions, then interpreting these more than complicated structures can get overwhelming.

When GDB prints a value, it checks whether at that place is a pretty-printer registered for that value outset. If at that place is, then GDB uses that pretty-printer to display the value. Otherwise, the value prints in the usual way.

In the case below, I didn't have a pretty-printer for the siginfo_t construction, which caused the impress info command to return all the data in the structure, including expanding the unions it uses.

Output - Print Info

What do you think?

Messy and not like shooting fish in a barrel to read.

I hope, creating a uncomplicated pretty-printer saves you much time staring at your reckoner screen.

Nearly pretty-printers comprise of two master elements:

  • The lookup office to identify the value blazon, and
  • the printer role itself.

To run across how this works, permit's write a bones pretty-printer in Python that returns the si_signo value from the siginfo_t structure for an interrupt point that the program receives.

# Start off with defining the printer as a Python object.

class SiginfoPrinter:

# The constructor takes the value and stores information technology for after.

def _init_(cocky, val):
self.val = val

  # The to_string method returns the value of the
# si_signo attribute of the directory.

def to_string(self):
  signo = self.val['si_signo']
return str(signo)

# Next, define the lookup function that returns the
# printer object when it receives a siginfo_t.

# The function takes the GDB value-type, which, in
# our instance is used to await for the siginfo_t.

def my_pp_func(val):
  if str(val.type)=='siginfo_t': return SiginfoPrinter(val)

# Finally, append the pretty-printer equally object/ role to
# the list of registered GDB printers.

gdb.pretty_printers.append(my_pp_func)

# Our pretty-printer is now bachelor when nosotros debug
# the inferior program in GDB.

Now, run whatever plan and hit Ctrl-c to quit. The pretty-printer returns the value 2; the value for a SIGINT.

(gdb) source prettyprint.py
(gdb) print info
$4 = 2
(gdb)

Much easier to read.

Of course, this is merely a unproblematic pretty-printer plan, but the possibilities are endless. You tin can extract and impress any value of a member in a structure with your pretty-printer.

In my video, I show you a quick way to pretty-impress structures in GDB and build-out this basic pretty-printer some more. You'll learn that information technology isn't difficult to extrapolate this handy printer to display whatever structure you want.

5. Reversible Debugging

To quote Kernighan in one case more than:

Debugging involves astern reasoning, like solving murder mysteries. Something incommunicable occurred, and the merely solid information is that it really did occur. And so we must think backward from the issue to find the reasons.

Or in other words, we really want the debugger to exist able to tell u.s. what happened in the past - reality has diverged from your expectations, and debugging ways figuring out at what point your expectations and reality diverged.

You need to know what your plan actually did as opposed to what y'all expected it was going to exercise. This is why debugging typically involves reproducing the bug many times, slowly teasing out more than and more than information until you lot pivot it downwards.

Reversible debugging takes away all that guesswork and trial and mistake; the debugger can tell you directly what simply happened.

Not everyone knows nigh it, simply GDB has built-in reversible debugging since release 7.0 (2009). Information technology works pretty well, but you've to be prepare to sacrifice operation (a lot of it); it'southward dramatically slower than whatsoever of the purpose-built fourth dimension-travel debuggers out there like UDB.

But information technology's congenital correct in, so why non use it?

Type record to start reversible-debugging.

(gdb) record

You can utilise reverse commands to go astern, for case:

reverse-next (rn) - footstep the program backward, run through subroutine calls

reverse-nexti (rni) - footstep astern one education, but run through called subroutines

reverse-step (rs) - step the program backward until information technology reaches the beginning of a previous source line

opposite-pace (rsi) - stride backward ane instruction

reverse-go on (rc) - continue the programme but run information technology in opposite

reverse-terminate(rf) - execute the program backward until merely earlier the point the memory changes

The claiming is that in most cases, you lot can't predict when the program faults, which means that y'all may have to run (and record) the program repeatedly until information technology somewhen stalls.

Some GDB commands tin can help you out here.

Breakpoints and watchpoints work in contrary, which tin can help you, for example, to go along directly to a previous indicate in the program at which specific variable changes. Reverse watchpoints tin be incredibly powerful. I know of several instances of bugs that alluded a developer for months or even years that were solved in a few hours with the ability of reverse watchpoints.

Another cool GDB thing is that you can trigger a command (or series of commands) when the plan hits a breakpoint.

Type command [breakpoint #], where [breakpoint #] is the identifier for your breakpoint.

command 1
Blazon commands for breakpoint(southward) one, one per line.
End with a line proverb only "end".
tape
continue
cease

In this video, I demonstrate live on phase reversible-debugging in GDB. Check it out, it shows you pace-by-step (in opposite) how to trace a error in my program.

Try UDB for free
Step backwards in time in your program to view registers and memory with a Time Travel Debugger
Larn more »

That'due south information technology.

I shared five like shooting fish in a barrel means to reduce your debugging hours. Perhaps a trivial overwhelming at present, but the practiced news is that you don't accept to adopt all at once. That probably would take the reverse effect. Sometimes piffling tweaks to your usual debugging habits and routines can already make a big deviation. Just offset with small steps.

Take your time to read the unlike tutorials and watch the videos that I shared in this article and adopt the things that y'all feel tin can amend your debugging. And, make sure to share your takeaways with your projection members and organization so everyone benefits.

To make sure y'all practise non miss my side by side GDB tutorial, sign up for the gdbWatchPoint mailing now.

Source: https://undo.io/resources/gdb-watchpoint/5-ways-reduce-debugging-hours/

0 Response to "How To Set Breakpoint In Gdb"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel