Monday, November 22, 2010

Gdb tips

Hi, as I'm trying to get a deeper understandment about the features included in Gdb, I started to read its manual and taking some key concecpts do post here.

Today we will start with the concept of Stopping and Continuing the program under Gdb.


Stopping and Continuing

The principal purposes of using a debugger are so that you can stop your program before it terminates, and if it runs into trouble, you can investigate and find out why.



Breakpoints

A breakpoint makes your program stop whenever a certain point in the program is reached, and you can add conditions to control in finer detail you program.

You can use the break command to create a breakpoint, a breakpoint needs a place to know when stops, this address can be the line number, a function name or the exact address in the program.



Watchpoint

A watchpoint is a special breakpoint that stops your program when the value of and expression changes, the commands to manage the wachpoints are the same as for breakpoints.



Gdb assigns an integer number to each breakpoint and watchpoint you create.

In many commands to manage them, is only necessary this integer number to choose the specified break or watchpoint.

Some Gdb commands accept a range of breakpoints on which to operate, like “5-7”, will only operate in breakpoint 5 through 7.

The variable $bpnum records the number of the breakpoint you've set most recently.



break location

Set a breakpoint at the given location



break

When called without arguments, break sets a breakpoint at the next instruction to be executed in the selected stack frame.



break … if cond

Set a breakpoint with condition cond. Evaluate the expression cond each time theh breakpoint is reached, and stop if the condition evaluates to true.



tbreak args

Set a breakpoint that will be deleted once reached.



info breakpoint [n] or i b [n]

The info if all breakpoints and watchpoints the [n] argument is optional and prints the info only only this breakpoint.

The columns displayed are:

Num : Integer number representing the breakpoint

Type : Breakpoint or watchpoint

Disp : Disposition, whether the breakpoint is marked to be disabled or enabled when hit

Enb : Enabled breakpoints are marked with “y”. And the not enabled are with “n”

Address : The address of the breakpoint in your program.

What : Where the breakpoint is in the source of your program



If the breakpoint is conditional, info break shows the condition on the line following the affected breakpoint; breakpoint commands, if any, are listed after that.

Also info break shows how many times the breakpoints has been hit.

Gdb allows you to set any number of breakpoints at the same place in your program. There is nothing silly or meaningless about this, when the breakpoints are conditional, this is even useful.





Setting Watchpoints

You can use watchpoint to stop execution whenever the value of an expression changes without having to predict a particular place where this may happen.

You can set a watchpoint on an expression even if the expression can not be evaluated yet.



watch expr

Set a watchpoint for an expression.

The simplest use of this command is to watch the value of a single variable.



rwatch expr

Set a watchpoint that will break when the value of expr is read by the program.



awatch expr

Set a watchpoint that will break when expr is either read from or written into by the program.



info watchpoints

Prints a list of watchpoints, using the same format as the info break.



To set a watchpoint on an fixed address, you must dereference it, as the addres itself is just a constant number which will never change.

(gdb) watch 0x600850

Cannot watch constant value 0x600850

(gdb) watch *(int *) 0x600850

Watchpoint 1: *(int *) 6293584



Deleting Breakpoints


clear

Delete any breakpoint at the next instruction to be executed

clear [ location | function | linenum ]

Delete any breakpoint set at the specified location or function name or line number.

If you have multiple files, you can specify the filename before the locations above.


delete [breakpoints] [range]

Delete the breakpoints and watchpoints of the breakpoint number, or the range specified.


Disabling Breakpoints


Sometimes we might prefer to disable some break or watchpoints rather do delete it.

The commands used to enable or disable the breakpoints and watchpoints must be used with their respective integer number, that identifies them, you can see it using info breakpoints.


A break or watchpoint can have any of four different states of enablement:

enabled : The breakpoint stops your program, is the default set with the break command

disabled: The breakpoint has no effect.

enabled once: The breakpoint stops your program, but then becomes disabled

enabled for deletion: The breakpoint stops your program but is deleted after it. As tbreak does

The commands to enable or disable breakpoints and watchpoints.

disable [breakpoints] [range] or dis

Disable the specified breakpoints, or all if none are listed.

enable [breakpoints] [range]

Enable the specified breakpoints.

enable [breakpoints] once range...

Enable the specified breakpoints temporarily for one time, then Gdb disables it.


enable [breakpoints] delete range...

Enable the specified breakpoints to work once, then Gdb deletes it.



Break Conditions

The simplest sort of breakpoint break every time your program reaches a specified place.

You can also specify a condition for a breakpoint.

A condition is just a Boolean expression in your programming language that Gdb evaluates each time your program reaches it and stop only if the conditions is true.


Break conditions can be specified when a brekpoint is set, by using 'if' in the arguments to the break command and they can also be changed at any time with the condition command.


condition bnum expression

Specify expression as the break condition for breakpoint, watchpoint identified by bnum.

After you set a condition, breakpoint bnum stops your program only if the value of expression is true.


condition bnum

Remove the condition from breakpoint number bnum.


A special case of a breakpoint condition is to stop only when the breakpoint has been reached a certain number of times. For this we use:


ignore bnum count


Breakpoint Command Lists

You can give any breakpoint a series of commands to execute when your program stopes due to that breakpoint. You might want to print the values of certain expressions, or enable other breakpoints.


commands [range...]

command-list …

end

Specify a list of commands for the given breakpoint.

Type a line containing just end to terminate the commands.

To remove all commands from a breakpoint, type commands and follow it immediately with end.

With no argument, commands refers to the last breakpoint or watchpoint.

If the first command you specify in a command list is silent, the usual message about stopping at a breakpoint is not printed.

If none of the remaining commands print anythiing, you see no sign that the breakpoint was reached, silent is meaningful only at the beginning of a breakpoint command list.

The commands echo, output, and printf allow you to print precisely controlled output, and are often useful in silent breakpoints, an example:

break foo if x>0

commands

silent

printf “x is %d\n”, x

cont

end


One application for breakpoint commands is to compensate for one bug so you can test for another.

Put a breakpoint just after the erroneous line of code, give it a condition to detect the case in which something erroneous has been done, and give it commands to assign correct values to any variables that need them. End with the continue commands so that your program does not stop, and start with the silent commands so that no output is produced. Here is an example:

break 403

commands

silent

set x = y + 4

cont

end

1 Comments:

Anonymous Anonymous said...

الأشياء الجيدة جدا.

5:59 AM  

Post a Comment

<< Home