赞
踩
1 Introduction
In some applications, it is not feasible for the debugger to interrupt the program's execution long enough for the developer to learn anything helpful about its behavior. If the program's correctness depends on its real-time behavior, delays introduced by a debugger might cause the program to change its behavior drastically, or perhaps fail, even when the code itself is correct. It is useful to be able to observe the program's behavior without interrupting it.
Using gdb 's trace
and collect
commands, you can specify locations in the program, called tracepoints , and arbitrary expressions to evaluate when those tracepoints are reached. Later, using the tfind
command, you can examine the values those expressions had when the program hit the tracepoints. The expressions may also denote objects in memory—structures or arrays, for example—whose values gdb should record; while visiting a particular tracepoint, you may inspect those objects as if they were in memory at that moment. However, because gdb records these values without interacting with you, it can do so quickly and unobtrusively, hopefully not disturbing the program's behavior.
Before running such a trace experiment , an arbitrary number of tracepoints can be set. A tracepoint is actually a special type of breakpoint (see Set Breaks ), so you can manipulate it using standard breakpoint commands . For instance, as with breakpoints, tracepoint numbers are successive integers starting from one, and many of the commands associated with tracepoints take the tracepoint number as their argument, to identify which tracepoint to work on.
For each tracepoint, you can specify, in advance, some arbitrary set of data that you want the target to collect in the trace buffer when it hits that tracepoint . The collected data can include registers, local variables, or global data. Later, you can use gdb commands to examine the values these data had at the time the tracepoint was hit .
Regular and fast tracepoints are dynamic tracing facilities , meaning that they can be used to insert tracepoints at (almost) any location in the target. Some targets may also support controlling static tracepoints from gdb . With static tracing, a set of instrumentation points, also known as markers , are embedded in the target program, and can be activated or deactivated by name or address. These are usually placed at locations which facilitate investigating what the target is actually doing. gdb 's support for static tracing includes being able to list instrumentation points, and attach them with gdb defined high level tracepoints that expose the whole range of convenience of gdb 's tracepoints support. Namelly, support for collecting registers values and values of global or local (to the instrumentation point) variables; tracepoint conditions and trace state variables. The act of installing a gdb static tracepoint on an instrumentation point, or marker, is referred to as probing a static tracepoint marker.
2 Set Tracepoints
create and delete tracepoints
http://sourceware.org/gdb/current/onlinedocs/gdb/Create-and-Delete-Tracepoints.html#Create-and-Delete-Tracepoints
trace location
The trace
command is very similar to the break
command. Its argument location can be a source line, a function name, or an address in the target program. See Specify Location . The trace
command defines a tracepoint, which is a point in the target program where the debugger will briefly stop, collect some data, and then allow the program to continue. Setting a tracepoint or changing its actions doesn't take effect until the next tstart
command, and once a trace experiment is running, further changes will not have any effect until the next trace experiment starts.
Here are some examples of using the trace
command:
(gdb) trace foo.c:121 // a source file and line number
(gdb) trace +2 // 2 lines forward
(gdb) trace my_function // first source line of function
(gdb) trace *my_function // EXACT start address of function
(gdb) trace *0x2117c4 // an address
You can abbreviate trace
as tr
.
trace
location
if
cond
ftrace
location
[ if
cond
]
strace
location
[ if
cond
]
delete tracepoint
[
num
]( abbreviate this command as
del tr
)
disable tracepoint
[
num
]
enable tracepoint
[
num
]
passcount
[
n
[
num
]]
passcount
command sets the passcount of the most recently defined tracepoint. If no passcount is given, the trace experiment will run until stopped explicitly by the user.
http://sourceware.org/gdb/current/onlinedocs/gdb/Tracepoint-Actions.html#Tracepoint-Actions
actions
[
num
]
actions
without bothering about its number). You specify the actions themselves on the following lines, one action at a time, and terminate the actions list with a line containing just
end
. So far, the only defined actions are
collect
,
teval
, and
while-stepping
.
collect
expr1
,
expr2
, ...
teval
expr1
,
expr2
, ...
while-stepping
n
set default-collect
expr1
,
expr2
, ...
show default-collect
info tracepoints
[
num
]
info tp
.
info static-tracepoint-markers
http://sourceware.org/gdb/current/onlinedocs/gdb/Starting-and-Stopping-Trace-Experiments.html#Starting-and-Stopping-Trace-Experiments
tstart
tstop
tstatus
Here is an example of the commands we described so far:
(gdb) trace gdb_c_test
(gdb) actions
Enter actions for tracepoint #1, one per line.
> collect $regs,$locals,$args
> while-stepping 11
> collect $regs
> end
> end
(gdb) tstart
[time passes ...]
(gdb) tstop
You can choose to continue running the trace experiment even if gdb disconnects from the target, voluntarily or involuntarily. For commands such as detach
, the debugger will ask what you want to do with the trace. But for unexpected terminations (gdb crash, network outage), it would be unfortunate to lose hard-won trace data, so the variable disconnected-tracing
lets you decide whether the trace should continue running without gdb .
set disconnected-tracing on
set disconnected-tracing off
detach
or
quit
will ask you directly what to do about a running trace no matter what this variable's setting, so the variable is mainly useful for handling unexpected situations, such as loss of the network.
show disconnected-tracing
When you reconnect to the target, the trace experiment may or may not still be running; it might have filled the trace buffer in the meantime, or stopped for one of the other reasons. If it is running, it will continue after reconnection.
set circular-trace-buffer on
set circular-trace-buffer off
show circular-trace-buffer
3 Analyze Collected Data
After the tracepoint experiment ends, you use gdb commands for examining the trace data. The basic idea is that each tracepoint collects a trace snapshot every time it is hit and another snapshot every time it single-steps. All these snapshots are consecutively numbered from zero and go into a buffer, and you can examine them later. The way you examine them is to focus on a specific trace snapshot. When the remote stub is focused on a trace snapshot, it will respond to all gdb requests for memory and registers by reading from the buffer which belongs to that snapshot, rather than from real memory or registers of the program being debugged. This means that all gdb commands (print
, info registers
, backtrace
, etc.) will behave as if we were currently debugging the program state as it was when the tracepoint occurred. Any requests for data that are not in the buffer will fail.
[1] http://sourceware.org/gdb/current/onlinedocs/gdb/Tracepoints.html#Tracepoints
4 Convenience Variables for Tracepoints
(int) $trace_frame
(int) $tracepoint
(int) $trace_line
(char []) $trace_file
(char []) $trace_func
$tracepoint
.
Note: $trace_file
is not suitable for use in printf
, use output
instead.
Here's a simple example of using these convenience variables for stepping through all the trace snapshots and printing some of their data. Note that these are not the same as trace state variables, which are managed by the target.
(gdb) tfind start
(gdb) while $trace_frame != -1
> output $trace_file
> printf ", line %d (tracepoint #%d)/n", $trace_line, $tracepoint
> tfind
> end
5 Using Trace Files
In some situations, the target running a trace experiment may no longer be available; perhaps it crashed, or the hardware was needed for a different activity. To handle these cases, you can arrange to dump the trace data into a file, and later use that file as a source of trace data, via the target tfile
command.
tsave [ -r ]
filename
-r
(“remote”) to direct the target to save the data directly into
filename in its own filesystem, which may be more efficient if the trace buffer is very large. (Note, however, that
target tfile
can only read from files accessible to the host.)
target tfile
filename
tstatus
will report the state of the trace run at the moment the data was saved, as well as the current trace frame you are examining.
filename must be on a filesystem accessible to the host.
Refer:
[1] http://sourceware.org/gdb/current/onlinedocs/gdb/Tracepoints.html#Tracepoints
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。