GDB Debug Program (ii)

Source: Internet
Author: User
Tags aliases thread stop

The following article goes from Chenhao csdn blog: http://blog.csdn.net/haoel/article/details/2879

Overview of GDB commands
———————

Once GDB is started, you can start debugging with GDB's commands, as soon as you are brought into GDB's debug environment, and GDB commands can be viewed using the Help command, as shown below:

   /home/hchen> gdb
    GNU gdb 5.1.1
    Copyright 2002 free Software Foundation, Inc.
    GDB is free software, cov Ered by the GNU general public License, and you is
    Welcome to change it and/or distribute copies of It under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for gdb.  Type ' show warranty ' for details.
    This GDB is configured as "I386-suse-linux".
    (gdb) help
    List of classes of commands:

    aliases--aliases of other commands
     Breakpoints-Making program stop at certain points
    data--examining data
  &N Bsp Files--Specifying and examining files
    Internals--Maintenance commands
    Obscure--obscure features
    running--running the program
    Stack--examining The stack
    status-Status inquiries
    Support-Support facilities
    tracepoints-tracing of program execution without stopping the program
    user-de Fined--user-defined commands

Type ' help ' followed by a class name for a list of commands in that class.
Type ' help ' followed by command name for full documentation.
Command name abbreviations is allowed if unambiguous.
(GDB)

GDB has a lot of commands, and gdb divides it into many different categories. The help command is just an example of the type of GDB command, and if you want to see the commands in the category, you can see all the commands that set a breakpoint by using the helps <class> command, such as breakpoints. You can also go directly to help <command> to see the commands.


gdb, when you enter a command, you can use the first few characters of the command only, and of course, the first few characters of the command should mark a single command, Under Linux, you can hit tab two times to get the full name of the command, and if there are duplicates, then GDB will take it out.
   
    Example one: When entering function func, set a breakpoint. can be typed as break func, or directly b func
    (gdb) b func
    breakpoint 1 at 0x8048458:file Hell O.C, line 10.
 
    Example Two: Enter B by pressing two times tab, and you will see all the commands that begin with B:
    (gdb) b
    backtrace  break      BT
    (GDB)

    example three: Just remember the prefix of the function, so you can:
    (GDB) b Make_ < press TAB;
    (press TAB again, you'll see:)
    make_a_section_from_file      Make_environ
    make_abs_section              Make_function_type
    make_blockvector              Make_pointer_type
    make_cleanup                  Make_reference_type
    make_command                  make_symbol_completion_list
    (gdb) b make_
    GDB gives you an example of all the functions that start with make.

Example four: When you debug a C + + program, you can have the same function name. Such as:
(gdb) b ' bubble (M-?
Bubble (double,double) Bubble (int,int)
(gdb) b ' Bubble (
You can see all the overloaded functions and parameters in C + +. (Note: M-and "Press two tab" is a meaning)

To exit GDB, simply send a quit or a command called Q.

A shell program that runs UNIX in GDB
————————————

In the GDB environment, you can execute the UNIX shell command, using GDB's shell command to complete:

Shell <command string>
Call the Unix shell to execute <command string> the shell of Unix defined in the environment variable shell will be used to execute <command string> If the shell is not defined, Then use Unix's standard shell:/bin/sh. (Use Command.com or cmd.exe in Windows)

There is also a GDB command that is make:
Make <make-args>
You can build your own program by executing the Make command in GDB. This command is equivalent to "shell make <make-args>".

For example, if you modify the TST.C program and try make TST, build the TST directly. Without having to do the GCC tst.c-o yourself again tst!! No-G Oh!


Run the program in GDB
————————

When GDB is started with gdb <program>, GDB searches the path path and the current directory for <program> source files. To confirm that GDB is reading the source file, you can use the L or List command to see if GDB can list the source code.

In GDB, run the program using the R or Run command. program runs, you may need to set up the following four things.

1, program operation parameters.
Set args can specify runtime parameters. (Example: Set args 10 20 30 40 50)
The show args command can view the set run parameters.

2, the operating environment.
Path <dir> can set the running paths of the program.
Show paths view the running path of the program.
Set environment VarName [=value] Sets the environment variable. such as: Set env User=hchen
Show environment [varname] View environment variables.

3, working directory.
CD <dir> equivalent to the Shell's CD command.
The PWD displays the current directory.

4, the input and output of the program.
Info terminal shows you the terminal mode used by your program.
Use redirection control program output. such as: Run > outfile
A TTY command can refer to an end device that writes input and output. such as: Tty/dev/ttyb


To debug a program that is already running
————————

Two methods:
1. Under UNIX, use PS to see the PID (Process ID) of the running program, and then use the GDB <program> PID format to hook up the running program.
2, first use GDB <program> associated with the source code, and GDB, in gdb with the attach command to hook up the process PID. Use the detach to cancel the hook process.

Pause/Resume Program run
—————————

In the debugger, it is necessary to pause the program, and GDB can conveniently pause the program's operation. You can set up which line of the program stops, under what conditions, stop when you receive a signal, and so on. To make it easier for you to see the variables at run time, as well as the process at run time.

When the process is stopped by GDB, you can use the Info program to see if it is running, the process number, and why it was paused.

In gdb, we can pause in the following ways: Breakpoints (breakpoint), Observation points (watchpoint), Snap points (catchpoint), signal (signals), thread stop (threads Stops). If you want to restore the program to run, you can use the C or Continue command.


First, set breakpoints (breakpoint)

We use the break command to set breakpoints. There are several ways to set breakpoints on the front:

Break <function>
Stops when the specified function is entered. The function name can be specified in C + + using the Class::function or functions (type,type) format.

Break <linenum>
Stops at the specified line number.

Break +offset
Break-offset
Stops at the offset line before or after the current line number. Offiset is the natural number.

Break Filename:linenum
Stop at the LineNum line of the source file filename.

Break Filename:function
Stop at the entrance of the function function of the source file filename.

Break *address
Stop at the memory address where the program is running.

Break
The break command stops at the next instruction when there are no arguments.

Break ... if <condition>
... Can be the above parameters, condition indicates the condition, when the condition is established, stop. For example, in an environment-based body, you can set the break if i=100, which means that when I is 100, the program is stopped.

When viewing breakpoints, you can use the info command as follows: (Note: n indicates a breakpoint number)
info breakpoints [n]
Info break [n]

Second, set the observation point (watchpoint)

The observer generally observes whether the value of an expression (the variable is also an expression) has changed, and if so, stops the program immediately. We have the following methods to set the observation point:

Watch <expr>
Set an observer point for the expression (variable) expr. When the value of a quantity expression changes, stop the program immediately.

Rwatch <expr>
When the expression (variable) expr is read, the program is stopped.

Awatch <expr>
When the value of an expression (variable) is read or written, the program is stopped.

Info watchpoints
Lists all the observed points that are currently set.


Third, set the snap point (catchpoint)

    you can set a snap point to catch some events while the program is running. such as: Loading shared libraries (dynamic link libraries) or C + + exceptions. The format of the snap point is:
   
    catch <event>
         when the event occurs, stop the program. The event can be the following:
        1, throw an exception thrown by C + +. (throw keyword)
        2, Catch a C + + caught exception. (Catch is keyword)
        3, when Exec calls the system to call exec. (exec is a keyword, currently this feature is only useful on HP-UX)
        4, when fork calls the system call fork. (Fork is the keyword, currently this feature is only useful under HP-UX)
        5, Vfork calls the system when calling Vfork. (Vfork is a keyword that currently works only on HP-UX)
        6, load, or load <libname> When you load a shared library (dynamic link library). (load is a keyword that currently works only on HP-UX)
        7, unload, or unload <libname> When you uninstall a shared library (dynamic link library). (Unload is the keyword, currently this feature is only useful on HP-UX)

Tcatch <event>
Only one snap point is set once, and when the program stops, the point is automatically deleted.

GDB Debug Program (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.