Online Book Reader

Home Category

Running Linux, 5th Edition - Matthias Kalle Dalheimer [397]

By Root 1091 0

The No such file or directory error is given because gdb can't locate the source file for _ _select. This is often the case with system calls and library functions, and it's nothing to worry about.

You can also start gdb with the command

papaya$ gdb pgmseq 254

Once gdb attaches to the running process, it temporarily suspends the program and lets you take over, issuing gdb commands. Or you can set a breakpoint or watchpoint (with the break and watch commands) and use continue to cause the program to continue execution until the breakpoint is triggered.

The detach command detaches gdb from the running process. You can then use attach again, on another process, if necessary. If you find a bug, you can detach the current process, make changes to the source, recompile, and use the file command to load the new executable into gdb. You can then start the new version of the program and use the attach command to debug it. All without leaving gdb!

In fact, gdb allows you to debug three programs concurrently: one running directly under gdb, one tracing with a core file, and one running as an independent process. The target command allows you to select which one you wish to debug.

Changing and Examining Data

To examine the values of variables in your program, you can use the print, x, and ptype commands. The print command is the most commonly used data inspection command; it takes as an argument an expression in the source language (usually C or C++) and returns its value. For example:

(gdb) print mydisplay

$10 = (struct _XDisplay *) 0x9c800

(gdb)

This displays the value of the variable mydisplay, as well as an indication of its type. Because this variable is a pointer, you can examine its contents by dereferencing the pointer, as you would in C:

(gdb) print *mydisplay

$11 = {ext_data = 0x0, free_funcs = 0x99c20, fd = 5, lock = 0,

proto_major_version = 11, proto_minor_version = 0,

vendor = 0x9dff0 "XFree86", resource_base = 41943040,

...

error_vec = 0x0, cms = {defaultCCCs = 0xa3d80 "",\

clientCmaps = 0x991a0 "'",

perVisualIntensityMaps = 0x0}, conn_checker = 0, im_filters = 0x0}

(gdb)

mydisplay is an extensive structure used by X programs; we have abbreviated the output for your reading enjoyment.

print can print the value of just about any expression, including C function calls (which it executes on the fly, within the context of the running program):

(gdb) print getpid()

$11 = 138

(gdb)

Of course, not all functions may be called in this manner. Only those functions that have been linked to the running program may be called. If a function has not been linked to the program and you attempt to call it, gdb will complain that there is no such symbol in the current context.

More complicated expressions may be used as arguments to print as well, including assignments to variables. For example:

(gdb) print mydisplay->vendor = "Linux"

$19 = 0x9de70 "Linux"

(gdb)

assigns to the vendor member of the mydisplay structure the value "Linux" instead of "XFree86" (a useless modification, but interesting nonetheless). In this way, you can interactively change data in a running program to correct errant behavior or test uncommon situations.

Note that after each print command, the value displayed is assigned to one of the gdb convenience registers, which are gdb internal variables that may be handy for you to use. For example, to recall the value of mydisplay in the previous example, we need to merely print the value of $10:

(gdb) print $10

$21 = (struct _XDisplay *) 0x9c800

(gdb)

You may also use expressions, such as typecasts, with the print command. Almost anything goes.

The ptype command gives you detailed (and often long-winded) information about a variable's type or the definition of a struct or typedef. To get a full definition for the struct_XDisplay used by the mydisplay variable, we use:

(gdb) ptype mydisplay

type = struct _XDisplay {

struct _XExtData *ext_data;

struct _XFreeFuncs *free_funcs;

int fd;

int lock;

int proto_major_version;

....

struct _XIMFilter *im_filters;

Return Main Page Previous Page Next Page

®Online Book Reader