What is the Continue in Gdb Does
| ||
Chapter 7. Stopping and ContinuingThe principal purposes of using a debugger are so that you can stop your program before it terminates; or so that, if your program runs into trouble, you can investigate and find out why. Inside gdb, your program may stop for any of several reasons, such as a signal, a breakpoint, or reaching a new line after a gdb command such as step. You may then examine and change variables, set new breakpoints or remove old ones, and then continue execution. Usually, the messages shown by gdb provide ample explanation of the status of your program--but you can also explicitly request this information at any time.
7.1. Breakpoints, watchpoints, and catchpointsA breakpoint makes your program stop whenever a certain point in the program is reached. For each breakpoint, you can add conditions to control in finer detail whether your program stops. You can set breakpoints with the break command and its variants (refer to Section 7.1.1 Setting breakpoints), to specify the place where your program should stop by line number, function name or exact address in the program. In HP-UX, SunOS 4.x, SVR4, and Alpha OSF/1 configurations, you can set breakpoints in shared libraries before the executable is run. There is a minor limitation on HP-UX systems: you must wait until the executable is run in order to set breakpoints in shared library routines that are not called directly by the program (for example, routines that are arguments in a pthread_create call). A watchpoint is a special breakpoint that stops your program when the value of an expression changes. You must use a different command to set watchpoints (refer to Section 7.1.2 Setting watchpoints), but aside from that, you can manage a watchpoint like any other breakpoint: you enable, disable, and delete both breakpoints and watchpoints using the same commands. You can arrange to have values from your program displayed automatically whenever gdb stops at a breakpoint. Refer to Section 10.6 Automatic display. A catchpoint is another special breakpoint that stops your program when a certain kind of event occurs, such as the throwing of a C++ exception or the loading of a library. As with watchpoints, you use a different command to set a catchpoint (refer to Section 7.1.3 Setting catchpoints), but aside from that, you can manage a catchpoint like any other breakpoint. (To stop when your program receives a signal, use the handle command. Refer to Section 7.3 Signals. gdb assigns a number to each breakpoint, watchpoint, or catchpoint when you create it; these numbers are successive integers starting with one. In many of the commands for controlling various features of breakpoints you use the breakpoint number to say which breakpoint you want to change. Each breakpoint may be enabled or disabled; if disabled, it has no effect on your program until you enable it again. Some gdb commands accept a range of breakpoints on which to operate. A breakpoint range is either a single breakpoint number, like 5, or two such numbers, in increasing order, separated by a hyphen, like 5-7. When a breakpoint range is given to a command, all breakpoint in that range are operated on. 7.1.1. Setting breakpointsBreakpoints are set with the break command (abbreviated b). The debugger convenience variable $bpnum records the number of the breakpoint you've set most recently; refer to Section 10.9 Convenience variables, for a discussion of what you can do with convenience variables. You have several ways to say where the breakpoint should go.
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 (refer to Section 7.1.6 Break conditions). gdb itself sometimes sets breakpoints in your program for special purposes, such as proper handling of longjmp (in C programs). 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 (refer to Appendix C Maintenance Commands). 7.1.2. Setting watchpointsYou can use a watchpoint to stop execution whenever the value of an expression changes, without having to predict a particular place where this may happen. Depending on your system, watchpoints may be implemented in software or hardware. gdb does software watchpointing by single-stepping your program and testing the variable's value each time, which is hundreds of times slower than normal execution. (But this may still be worth it, to catch errors where you have no clue what part of your program is the culprit.) On some systems, such as HP-UX, gnu/Linux and some other x86-based targets, gdb includes support for hardware watchpoints, which do not slow down the running of your program.
gdb sets a hardware watchpoint if possible. Hardware watchpoints execute very quickly, and the debugger reports a change in value at the exact instruction where the change occurs. If gdb cannot set a hardware watchpoint, it sets a software watchpoint, which executes more slowly and reports the change in value at the next statement, not the instruction, after the change occurs. When you issue the watch command, gdb reports
Hardware watchpoint num: expr if it was able to set a hardware watchpoint. Currently, the awatch and rwatch commands can only set hardware watchpoints, because accesses to data that don't change the value of the watched expression cannot be detected without examining every instruction as it is being executed, and gdb does not do that currently. If gdb finds that it is unable to set a hardware breakpoint with the awatch or rwatch command, it will print a message like this:
Expression cannot be implemented with read/access watchpoint. Sometimes, gdb cannot set a hardware watchpoint because the data type of the watched expression is wider than what a hardware watchpoint on the target machine can handle. For example, some systems can only watch regions that are up to 4 bytes wide; on such systems you cannot set hardware watchpoints for an expression that yields a double-precision floating-point number (which is typically 8 bytes wide). As a work-around, it might be possible to break the large region into a series of smaller ones and watch them with separate watchpoints. If you set too many hardware watchpoints, gdb might be unable to insert all of them when you resume the execution of your program. Since the precise number of active watchpoints is unknown until such time as the program is about to be resumed, gdb might not be able to warn you about this when you set the watchpoints, and the warning will be printed only when the program is resumed:
Hardware watchpoint num: Could not insert watchpoint If this happens, delete or disable some of the watchpoints. The SPARClite DSU will generate traps when a program accesses some data or instruction address that is assigned to the debug registers. For the data addresses, DSU facilitates the watch command. However the hardware breakpoint registers can only take two data watchpoints, and both watchpoints must be the same kind. For example, you can set two watchpoints with watch commands, two with rwatch commands, or two with awatch commands, but you cannot set one watchpoint with one command and the other with a different command. gdb will reject the command if you try to mix watchpoints. Delete or disable unused watchpoint commands before setting new ones. If you call a function interactively using print or call, any watchpoints you have set will be inactive until gdb reaches another kind of breakpoint or the call completes. gdb automatically deletes watchpoints that watch local (automatic) variables, or expressions that involve such variables, when they go out of scope, that is, when the execution leaves the block in which these variables were defined. In particular, when the program being debugged terminates, all local variables go out of scope, and so only watchpoints that watch global variables remain set. If you rerun the program, you will need to set all such watchpoints again. One way of doing that would be to set a code breakpoint at the entry to the main function and when it breaks, set all the watchpoints.
7.1.3. Setting catchpointsYou can use catchpoints to cause the debugger to stop for certain kinds of program events, such as C++ exceptions or the loading of a shared library. Use the catch command to set a catchpoint.
Use the info break command to list the current catchpoints. There are currently some limitations to C++ exception handling (catch throw and catch catch) in gdb:
Sometimes catch is not the best way to debug exception handling: if you need to know exactly where an exception is raised, it is better to stop before the exception handler is called, since that way you can see the stack before any unwinding takes place. If you set a breakpoint in an exception handler instead, it may not be easy to find out where the exception was raised. To stop just before an exception handler is called, you need some knowledge of the implementation. In the case of gnu C++, exceptions are raised by calling a library function named __raise_exception which has the following ANSI C interface:
/* addr is where the exception identifier is stored. id is the exception identifier. */ void __raise_exception (void **addr, void *id); To make the debugger catch all exceptions before any stack unwinding takes place, set a breakpoint on __raise_exception (refer to Section 7.1 Breakpoints, watchpoints, and catchpoints). With a conditional breakpoint (refer to Section 7.1.6 Break conditions) that depends on the value of id, you can stop your program when a specific exception is raised. You can use multiple conditional breakpoints to stop your program when any of a number of exceptions are raised. 7.1.4. Deleting breakpointsIt is often necessary to eliminate a breakpoint, watchpoint, or catchpoint once it has done its job and you no longer want your program to stop there. This is called deleting the breakpoint. A breakpoint that has been deleted no longer exists; it is forgotten. With the clear command you can delete breakpoints according to where they are in your program. With the delete command you can delete individual breakpoints, watchpoints, or catchpoints by specifying their breakpoint numbers. It is not necessary to delete a breakpoint to proceed past it. gdb automatically ignores breakpoints on the first instruction to be executed when you continue execution without changing the execution address.
7.1.5. Disabling breakpointsRather than deleting a breakpoint, watchpoint, or catchpoint, you might prefer to disable it. This makes the breakpoint inoperative as if it had been deleted, but remembers the information on the breakpoint so that you can enable it again later. You disable and enable breakpoints, watchpoints, and catchpoints with the enable and disable commands, optionally specifying one or more breakpoint numbers as arguments. Use info break or info watch to print a list of breakpoints, watchpoints, and catchpoints if you do not know which numbers to use. A breakpoint, watchpoint, or catchpoint can have any of four different states of enablement:
You can use the following commands to enable or disable breakpoints, watchpoints, and catchpoints:
Except for a breakpoint set with tbreak (refer to Section 7.1.1 Setting breakpoints), breakpoints that you set are initially enabled; subsequently, they become disabled or enabled only when you use one of the commands above. (The command until can set and delete a breakpoint of its own, but it does not change the state of your other breakpoints; (refer to Section 7.2 Continuing and stepping.) 7.1.6. Break conditionsThe simplest sort of breakpoint breaks 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 (refer to Section 10.1 Expressions). A breakpoint with a condition evaluates the expression each time your program reaches it, and your program stops only if the condition is true. This is the converse of using assertions for program validation; in that situation, you want to stop when the assertion is violated--that is, when the condition is false. In C, if you want to test an assertion expressed by the condition assert, you should set the condition ! assert on the appropriate breakpoint. Conditions are also accepted for watchpoints; you may not need them, since a watchpoint is inspecting the value of an expression anyhow--but it might be simpler, say, to just set a watchpoint on a variable name, and specify a condition that tests whether the new value is an interesting one. Break conditions can have side effects, and may even call functions in your program. This can be useful, for example, to activate functions that log program progress, or to use your own print functions to format special data structures. The effects are completely predictable unless there is another enabled breakpoint at the same address. (In that case, gdb might see the other breakpoint first and stop your program without checking the condition of this one.) Note that breakpoint commands are usually more convenient and flexible than break conditions for the purpose of performing side effects when a breakpoint is reached (refer to Section 7.1.7 Breakpoint command lists). Break conditions can be specified when a breakpoint is set, by using if in the arguments to the break command. Refer to Section 7.1.1 Setting breakpoints. They can also be changed at any time with the condition command. You can also use the if keyword with the watch command. The catch command does not recognize the if keyword; condition is the only way to impose a further condition on a catchpoint.
A special case of a breakpoint condition is to stop only when the breakpoint has been reached a certain number of times. This is so useful that there is a special way to do it, using the ignore count of the breakpoint. Every breakpoint has an ignore count, which is an integer. Most of the time, the ignore count is zero, and therefore has no effect. But if your program reaches a breakpoint whose ignore count is positive, then instead of stopping, it just decrements the ignore count by one and continues. As a result, if the ignore count value is n, the breakpoint does not stop the next n times your program reaches it.
Ignore counts apply to breakpoints, watchpoints, and catchpoints. 7.1.7. Breakpoint command listsYou can give any breakpoint (or watchpoint or catchpoint) a series of commands to execute when your program stops due to that breakpoint. For example, you might want to print the values of certain expressions, or enable other breakpoints.
Pressing You can use breakpoint commands to start your program up again. Simply use the continue command, or step, or any other command that resumes execution. Any other commands in the command list, after a command that resumes execution, are ignored. This is because any time you resume execution (even with a simple next or step), you may encounter another breakpoint--which could have its own command list, leading to ambiguities about which list to execute. If the first command you specify in a command list is silent, the usual message about stopping at a breakpoint is not printed. This may be desirable for breakpoints that are to print a specific message and then continue. If none of the remaining commands print anything, 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. Refer to Section 22.4 Commands for controlled output. For example, here is how you could use breakpoint commands to print the value of x at entry to foo whenever x is positive.
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 command so that your program does not stop, and start with the silent command so that no output is produced. Here is an example:
break 403 commands silent set x = y + 4 cont end 7.1.8. Breakpoint menusSome programming languages (notably C++ and Objective-C) permit a single function name to be defined several times, for application in different contexts. This is called overloading. When a function name is overloaded, break function is not enough to tell gdb where you want a breakpoint. If you realize this is a problem, you can use something like break function(types) to specify which particular version of the function you want. Otherwise, gdb offers you a menu of numbered choices for different possible breakpoints, and waits for your selection with the prompt >. The first two options are always [0] cancel and [1] all. Typing 1 sets a breakpoint at each definition of function, and typing 0 aborts the break command without setting any new breakpoints. For example, the following session excerpt shows an attempt to set a breakpoint at the overloaded symbol String::after. We choose three particular definitions of that function name:
(gdb) b String::after [0] cancel [1] all [2] file:String.cc; line number:867 [3] file:String.cc; line number:860 [4] file:String.cc; line number:875 [5] file:String.cc; line number:853 [6] file:String.cc; line number:846 [7] file:String.cc; line number:735 > 2 4 6 Breakpoint 1 at 0xb26c: file String.cc, line 867. Breakpoint 2 at 0xb344: file String.cc, line 875. Breakpoint 3 at 0xafcc: file String.cc, line 846. Multiple breakpoints were set. Use the "delete" command to delete unwanted breakpoints. (gdb) 7.1.9. "Cannot insert breakpoints"Under some operating systems, breakpoints cannot be used in a program if any other process is running that program. In this situation, attempting to run or continue a program with a breakpoint causes gdb to print an error message:
Cannot insert breakpoints. The same program may be running in another process. When this happens, you have three ways to proceed:
A similar message can be printed if you request too many active hardware-assisted breakpoints and watchpoints:
Stopped; cannot insert breakpoints. You may have requested too many hardware breakpoints and watchpoints. This message is printed when you attempt to resume the program, since only then gdb knows exactly how many hardware breakpoints and watchpoints it needs to insert. When this message is printed, you need to disable or remove some of the hardware-assisted breakpoints and watchpoints, and then continue.
|
thompsonevand1971.blogspot.com
Source: https://www.linuxtopia.org/online_books/redhat_linux_debugging_with_gdb/stopping.html
0 Response to "What is the Continue in Gdb Does"
Post a Comment