gdb调试(二)断点设置(英文板)

Breakpoints are set with the break command (abbreviated b). The debugger convenience variable ` $bpnum' records thenumber of the breakpoint you've set most recently; see Convenience Variables, for a discussion of what you can do withconvenience variables.
break location
Set a breakpoint at the given location, which can specify afunction name, a line number, or an address of an instruction. (See Specify Location, for a list of all the possible ways tospecify a location.) The breakpoint will stop your program justbefore it executes any of the code in the specified location.

When using source languages that permit overloading of symbols, such asC++, a function name may refer to more than one possible place to break. See Ambiguous Expressions, for a discussion ofthat situation.

It is also possible to insert a breakpoint that will stop the programonly if a specific thread (see Thread-Specific Breakpoints)or a specific task (see Ada Tasks) hits that breakpoint.

break
When called without any arguments, break sets a breakpoint atthe next instruction to be executed in the selected stack frame(see Examining the Stack). In any selected frame but theinnermost, this makes your program stop as soon as controlreturns to that frame. This is similar to the effect of a finish command in the frame inside the selected frame—exceptthat finish does not leave an active breakpoint. If you use break without an argument in the innermost frame, gdb stopsthe next time it reaches the current location; this may be usefulinside loops.

gdb normally ignores breakpoints when it resumes execution, until atleast one instruction has been executed. If it did not do this, youwould be unable to proceed past a breakpoint without first disabling thebreakpoint. This rule applies whether or not the breakpoint alreadyexisted when your program stopped.

break ... if cond
Set a breakpoint with condition cond; evaluate the expression cond each time the breakpoint is reached, and stop only if thevalue is nonzero—that is, if cond evaluates as true. ` ...' stands for one of the possible arguments describedabove (or no argument) specifying where to break. See Break Conditions, for more information on breakpoint conditions.


tbreak args
Set a breakpoint enabled only for one stop. args are thesame as for the break command, and the breakpoint is set in the sameway, but the breakpoint is automatically deleted after the first time yourprogram stops there. See Disabling Breakpoints.


hbreak args
Set a hardware-assisted breakpoint. args are the same as for the break command and the breakpoint is set in the same way, but thebreakpoint requires hardware support and some target hardware may nothave this support. The main purpose of this is EPROM/ROM codedebugging, so you can set a breakpoint at an instruction withoutchanging the instruction. This can be used with the new trap-generationprovided by SPARClite DSU and most x86-based targets. These targetswill generate traps when a program accesses some data or instructionaddress that is assigned to the debug registers. However the hardwarebreakpoint registers can take a limited number of breakpoints. Forexample, on the DSU, only two data breakpoints can be set at a time, and gdb will reject this command if more than two are used. Deleteor disable unused hardware breakpoints before setting new ones(see Disabling Breakpoints). See Break Conditions. For remote targets, you can restrict the number of hardwarebreakpoints gdb will use, see set remote hardware-breakpoint-limit.


thbreak args
Set a hardware-assisted breakpoint enabled only for one stop. argsare the same as for the hbreak command and the breakpoint is set inthe same way. However, like the tbreak command,the breakpoint is automatically deleted after thefirst time your program stops there. Also, like the hbreakcommand, the breakpoint requires hardware support and some target hardwaremay not have this support. See Disabling Breakpoints. See also Break Conditions.


rbreak regex
Set breakpoints on all functions matching the regular expression regex. This command sets an unconditional breakpoint on allmatches, printing a list of all breakpoints it set. Once thesebreakpoints are set, they are treated just like the breakpoints set withthe break command. You can delete them, disable them, or makethem conditional the same way as any other breakpoint.

The syntax of the regular expression is the standard one used with toolslike grep. Note that this is different from the syntax used byshells, so for instance foo* matches all functions that includean fo followed by zero or more os. There is an implicit.* leading and trailing the regular expression you supply, so tomatch only functions that begin with foo, use ^foo.

When debugging C++ programs, rbreak is useful for settingbreakpoints on overloaded functions that are not members of any specialclasses.

The rbreak command can be used to set breakpoints inall the functions in a program, like this:

          (gdb) rbreak .
     

rbreak file : regex
If rbreak is called with a filename qualification, it limitsthe search for functions matching the given regular expression to thespecified file. This can be used, for example, to set breakpoints onevery function in a given file:
          (gdb) rbreak file.c:.
     

The colon separating the filename qualifier from the regex mayoptionally be surrounded by spaces.


info breakpoints [ n ... ] info break [ n ... ]
Print a table of all breakpoints, watchpoints, and catchpoints set andnot deleted. Optional argument n means print information onlyabout the specified breakpoint(s) (or watchpoint(s) or catchpoint(s)). For each breakpoint, following columns are printed:
Breakpoint Numbers
Type
Breakpoint, watchpoint, or catchpoint.
Disposition
Whether the breakpoint is marked to be disabled or deleted when hit.
Enabled or Disabled
Enabled breakpoints are marked with ` y'. ` n' marks breakpointsthat are not enabled.
Address
Where the breakpoint is in your program, as a memory address. For apending breakpoint whose address is not yet known, this field willcontain ` <PENDING>'. Such breakpoint won't fire until a sharedlibrary that has the symbol or line referred by breakpoint is loaded. See below for details. A breakpoint with several locations willhave ` <MULTIPLE>' in this field—see below for details.
What
Where the breakpoint is in the source for your program, as a file andline number. For a pending breakpoint, the original string passed tothe breakpoint command will be listed as it cannot be resolved untilthe appropriate shared library is loaded in the future.

If a breakpoint is conditional, info break shows the condition onthe line following the affected breakpoint; breakpoint commands, if any,are listed after that. A pending breakpoint is allowed to have a conditionspecified for it. The condition is not parsed for validity until a sharedlibrary is loaded that allows the pending breakpoint to resolve to avalid location.

info break with a breakpointnumber n as argument lists only that breakpoint. Theconvenience variable $_ and the default examining-address forthe x command are set to the address of the last breakpointlisted (see Examining Memory).

info break displays a count of the number of times the breakpointhas been hit. This is especially useful in conjunction with theignore command. You can ignore a large number of breakpointhits, look at the breakpoint info to see how many times the breakpointwas hit, and then run again, ignoring one less than that number. Thiswill get you quickly to the last hit of that breakpoint.

gdb allows you to set any number of breakpoints at the same place inyour program. There is nothing silly or meaningless about this. Whenthe breakpoints are conditional, this is even useful(see Break Conditions).

It is possible that a breakpoint corresponds to several locationsin your program. Examples of this situation are:

  • For a C++ constructor, the gcc compiler generates severalinstances of the function body, used in different cases.
  • For a C++ template function, a given line in the function cancorrespond to any number of instantiations.
  • For an inlined function, a given source line can correspond toseveral places where that function is inlined.

In all those cases, gdb will insert a breakpoint at allthe relevant locations1.

A breakpoint with multiple locations is displayed in the breakpointtable using several rows—one header row, followed by one row foreach breakpoint location. The header row has `<MULTIPLE>' in theaddress column. The rows for individual locations contain the actualaddresses for locations, and show the functions to which thoselocations belong. The number column for a location is of the formbreakpoint-number.location-number.

For example:

     Num     Type           Disp Enb  Address    What
     1       breakpoint     keep y    <MULTIPLE>
             stop only if i==1
             breakpoint already hit 1 time
     1.1                         y    0x080486a2 in void foo<int>() at t.cc:8
     1.2                         y    0x080486ca in void foo<double>() at t.cc:8

Each location can be individually enabled or disabled by passingbreakpoint-number.location-number as argument to theenable and disable commands. Note that you cannotdelete the individual locations from the list, you can only delete theentire list of locations that belong to their parent breakpoint (withthe delete num command, where num is the number ofthe parent breakpoint, 1 in the above example). Disabling or enablingthe parent breakpoint (see Disabling) affects all of the locationsthat belong to that breakpoint.

It's quite common to have a breakpoint inside a shared library. Shared libraries can be loaded and unloaded explicitly,and possibly repeatedly, as the program is executed. To supportthis use case, gdb updates breakpoint locations wheneverany shared library is loaded or unloaded. Typically, you wouldset a breakpoint in a shared library at the beginning of yourdebugging session, when the library is not loaded, and when thesymbols from the library are not available. When you try to setbreakpoint, gdb will ask you if you want to seta so called pending breakpoint—breakpoint whose addressis not yet resolved.

After the program is run, whenever a new shared library is loaded,gdb reevaluates all the breakpoints. When a newly loadedshared library contains the symbol or line referred to by somepending breakpoint, that breakpoint is resolved and becomes anordinary breakpoint. When a library is unloaded, all breakpointsthat refer to its symbols or source lines become pending again.

This logic works for breakpoints with multiple locations, too. Forexample, if you have a breakpoint in a C++ template function, anda newly loaded shared library has an instantiation of that template,a new location is added to the list of locations for the breakpoint.

Except for having unresolved address, pending breakpoints do notdiffer from regular breakpoints. You can set conditions or commands,enable and disable them and perform other breakpoint operations.

gdb provides some additional commands for controlling whathappens when the `break' command cannot resolve breakpointaddress specification to an address:

set breakpoint pending auto
This is the default behavior. When gdb cannot find the breakpointlocation, it queries you whether a pending breakpoint should be created.
set breakpoint pending on
This indicates that an unrecognized breakpoint location should automaticallyresult in a pending breakpoint being created.
set breakpoint pending off
This indicates that pending breakpoints are not to be created. Anyunrecognized breakpoint location results in an error. This setting doesnot affect any pending breakpoints previously created.
show breakpoint pending
Show the current behavior setting for creating pending breakpoints.

The settings above only affect the break command and itsvariants. Once breakpoint is set, it will be automatically updatedas shared libraries are loaded and unloaded.

For some targets, gdb can automatically decide if hardware orsoftware breakpoints should be used, depending on whether thebreakpoint address is read-only or read-write. This applies tobreakpoints set with the break command as well as to internalbreakpoints set by commands like next and finish. Forbreakpoints set with hbreak, gdb will always use hardwarebreakpoints.

You can control this automatic behaviour with the following commands::

set breakpoint auto-hw on
This is the default behavior. When gdb sets a breakpoint, itwill try to use the target memory map to decide if software or hardwarebreakpoint must be used.
set breakpoint auto-hw off
This indicates gdb should not automatically select breakpointtype. If the target provides a memory map, gdb will warn whentrying to set software breakpoint at a read-only address.

gdb normally implements breakpoints by replacing the program codeat the breakpoint address with a special instruction, which, whenexecuted, given control to the debugger. By default, the programcode is so modified only when the program is resumed. As soon asthe program stops, gdb restores the original instructions. Thisbehaviour guards against leaving breakpoints inserted in thetarget should gdb abrubptly disconnect. However, with slow remotetargets, inserting and removing breakpoint can reduce the performance. This behavior can be controlled with the following commands::

set breakpoint always-inserted off
All breakpoints, including newly added by the user, are inserted inthe target only when the target is resumed. All breakpoints areremoved from the target when it stops.
set breakpoint always-inserted on
Causes all breakpoints to be inserted in the target at all times. Ifthe user adds a new breakpoint, or changes an existing breakpoint, thebreakpoints in the target are updated immediately. A breakpoint isremoved from the target only when breakpoint itself is removed.


set breakpoint always-inserted auto
This is the default mode. If gdb is controlling the inferiorin non-stop mode (see Non-Stop Mode), gdb behaves as if breakpoint always-inserted mode is on. If gdb iscontrolling the inferior in all-stop mode, gdb behaves as if breakpoint always-inserted mode is off.

gdb itself sometimes sets breakpoints in your program forspecial purposes, such as proper handling of longjmp (in Cprograms). These internal breakpoints are assigned negative numbers,starting with -1; `info breakpoints' does not display them. You can see these breakpoints with the gdb maintenance command`maint info breakpoints' (see maint info breakpoints).


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值