GDB User Guide 2 ~

Source: Internet
Author: User

Program environment
======================================
"Environment" includes a series of environment variables and their values. Environment variables generally record some common information,
For example, your user name, Home Directory, your terminal model, and the search path of your running program. Generally, you can
Environment variables are then shared by all programs you run. During debugging, you can set the appropriate
Instead of exiting GDB.

'Path directory'
Add new content before the 'path' environment variable ('path' provides the path for searching the execution file ). For GDB and
For your program, you may need to set some special paths. Use ':' or a space to separate them. If the directory already exists
In the path, this operation will move it to the front.
You can use the '$ cmd' string to represent the current path. If you use'. ', it indicates that you use the 'path' command.
GDB will replace the current path with '.' before adding directory to the search path.

'Show paths'
Displays the setting of the current path variable.

'Show Environment [varname]'
Displays the value of an environment variable. If you do not specify the variable name, GDB will display all the variable names and their
Content. Environment can be abbreviated to 'env'

'Set environment varname [=] value'
Set the value of an environment variable. But it is only valid for the program you are debugging. It does not work for GDB itself.
The value can be any string. If no value is specified, the variable value is set to null.
Let's look at an example:
Set env user = foo
Tells a Linux program that the user name will be 'foo' when it runs next time'

'Unset environment varname'
Deletes an environment variable.

Note: GDB uses the shell specified by the 'shell' environment variable to run your program.

Working path
======================================
Every time you run your program using the 'run' command, your program inherits
Current working directory. While the gdb working directory is inherited from its parent process (generally
Shell ). However, you can use the 'cd' command to specify the working directory.
The working directory of GDB is the way for it to find some files or information.
'CD directory'
Set the working directory of GDB to directory
'Pwd'
Print the current directory.
Input/Output of your program
====================================
Lack of time, your program input/output and GDB input/output use the same terminal.
GDB switches between itself and your program to interact with you, but this can cause confusion.
'Info Terminal'
Displays the type of the terminal you are currently using.
You can redirect the input/output of your program.
For example:
Run> OUTFILE
Run your program and write the standard output of your program into the file OUTFILE.
Another way to specify input/output for your program is to use the 'tty 'command.
Take a file name as the parameter and use this file as the default life for using the 'run' command in the future.
Command file. It also re-sets the control terminal for the sub-process.
For example:
Tty/dev/ttyb
Specify the process to be started using the 'run' command later. Use the terminal '/dev/ttyb' as the program input.
/Output, and set this terminal as the control terminal of your process.
A clear redirection using the 'run' command will reset the content set by 'tty'
But does not affect the control terminal. When you use the 'tty 'command or in the 'run' command
/When the output is redirected, only the input/output of the program currently debugged is changed,
It does not affect other programs.
Debug a running program:
==========================================

'Attach process-id'
This command connects a running process (started outside GDB) to GDB, so that
Debugging. Process-ID is the process number. ('Ps' or 'jobs-L' is used in UNIX to view processes)
'Attach 'is generally not repeated. (When you press enter more than one)
Of course, to use the 'Attach 'command, your operating system environment must support processes.
In addition, you must have the power to send signals to this process.
When using the 'Attach 'command, you should first use the 'file' command to specify
Contact program source code and symbol table. When GDB receives the 'Attach 'command
This is to stop the process. You can use all gdb commands to debug a "connection"
The process, just as you start it in GDB using the 'run' command. If you want the process to continue running
Line. Use the 'contine' or 'C' command.
'Detach'
After debugging, you can use this command to disconnect the process from GDB. (Remove GDB
Control) after the command is executed, the process continues to be executed.
If you exit GDB after using 'Attach 'to connect to a process, or run the 'run' command
If another process is executed, the 'Attach' process will be killed. However, GDB will
Ask you to confirm whether you want to exit or execute a new process.

Terminate a sub-process
======================================
'Kill'
Kill command to end the sub-process of your program under GDB
This command is more useful when you want to debug (check) a core dump file. GDB debugging process
All core dump is ignored.
In some operating systems, a program cannot run independently from GDB after a breakpoint is added to it.
You can use the kill command to solve this problem.
The 'Kill 'command is also useful when you want to recompile and connect your program. Because some systems cannot be modified.
The executable program being executed. In this way, when you use the 'run' command again, GDB will know that your program has been changed.
If it changes, GDB will load the new symbol again. (And keep your current breakpoint settings as much as possible.
Additional process information
====================================
Some operating systems provide a Device directory named '/proc' for checking the process image. If GDB is
Run the following command in the operating system: 'info proc. ('Info proc' life
It is only useful in svr4 systems that support 'procfs.
'Info proc'
Displays the process summary.
'Info proc ings'
Report the IP address range that your process can access.
'Info proc Times'
The start time, user CPU time, and system CPU time of your process and sub-process.
'Info proc id'
Report process ID information.
'Info proc status'
Report the general status of your process. If the process is stopped. This report also includes the reason for stopping and
Signal.
'Info proc all'
Displays all the information returned by the preceding commands.
Multi-threaded program debugging
==========================================================
In some operating systems, a single program can have more than one thread running. Precise Determination of threads and processes?
?

?
?
With its own registers, the runtime Stack may also have private memory.
GDB provides the following functions for debugging multi-threaded processes:
* New threads are automatically advertised.
* 'Thread threadno' is a command used to switch between threads.
* 'Info Threads' is a command used to query existing threads.
* 'Thread apply [threadno] [all] ARGs ', a command used to provide commands to the thread.
* Thread-related breakpoint settings.
Note: These features are not available in all GDB versions. The final analysis depends on whether the operating system supports these features.
If your GDB does not support these commands, an error message is displayed:
(GDB) info threads
(GDB) thread 1
Thread Id 1 not known. Use the "info Threads" command
See the IDs of currently known threads.
GDB line-level debugging allows you to observe all the threads in your program
GDB control, there is always a "current" thread. The debugging command takes effect on the "current" process.
Once GDB discovers a new thread in your program, it will automatically display the system information about this thread
. For example:
[New process 35 thread 27]
However, the format is related to the operating system.
For debugging purposes, GDB sets its own thread number.
'Info Threads'
Displays the summary of all threads in the process. GDB is displayed in sequence:
1. Thread number (set by GDB)
2. The thread ID of the target system.
3. The current stack of this thread.
The thread that is preceded by '*' indicates the current thread.
For example:
(GDB) info threads
3 process 35 thread 27 0x34e5 in sigpause ()
2 process 35 thread 23 0x34e5 in sigpause ()
* 1 process 35 thread 13 main (argc = 1, argv = 0x7ffffff8)
At threadtest. C: 68

'Thread threadno'
Set the thread whose thread number is threadno to the current thread. The command line parameter threadno is set in GDB.
Thread number. You can run the 'info Threads' command to view the thread number set in GDB. GDB displays this thread
And the stack corresponding to the thread. For example:

(GDB) thread 2
[Switching to process 35 thread 23]
0x34e5 in sigpause ()
"The content after switching depends on the definition of the thread ID in your operating system.

'Thread apply [threadno] [all] args'
This command allows you to issue the same command "ARGs" to more than one thread. The meaning of [threadno] is the same as above.
If you want to issue a command to all the threads in your process, use the [all] Option.
It automatically selects a signal or
The thread where the breakpoint occurs is the current thread. GDB uses a message in the format of '[Switching to register Ag ]'.
To report to you.
* See run and STOP multi-threaded programs.
* See: Set observation points.

Debug multi-process programs
========================================================== =
GDB does not support many programs that use the 'fork' system call to generate new processes. When a program starts
When a new process is created, GDB will continue debugging the parent process, and the child process will not be affected. If you are
If a breakpoint is set at the place where the process may be executed, the sub-process will receive the 'sigtrap 'signal. If the sub-process does not
If this signal is processed, the default processing is to terminate the sub-process.
However, if you need to debug sub-processes, there is a solution that is not very troublesome. In
Add the 'sleep 'command before the first few statements when the sub-process is running. This is not introduced during the debugging process.
It is very troublesome to start the program (but you need to pay attention to exceptions :-)). Then use the 'ps' command to list
Run the 'Attach' command. So there is no problem.
I think this is not the case in actual use. I tried it in the debugging process, as though not necessarily
It depends on the version of GDB and the operating system you are using.

Stop and continue
***********************
The basic function of the debugger is to stop the program under certain conditions before it is terminated.
Then, you can check what your program did when an error occurs.
In GDB, your program will be temporarily stopped for various reasons, such as a signal, a breakpoint, or
Because you use the 'step' command. When the program stops, you can check and change the value of the variable, set or remove it.
Breakpoint, and then continue running your program. Generally, when the program stops, GDB will display some information about the program status.
. For example, the reason for the program to stop, the stack, and so on. For more details, you can use 'info
Program 'command. In addition, if you enter this command at any time, GDB will display the status information of the current program running.
.

'Info program'
Display information about your program status: whether your program is running or stopped, what process is it, and why is it stopped?
.

Breakpoint, observation point, and exception
==========================================================
A breakpoint stops a program when it reaches a breakpoint. For each breakpoint
You can set more advanced information to determine when a breakpoint works. You can use the 'Break' command
To set breakpoints in your program. In the previous example, we have mentioned how to use this command.
You can set a breakpoint on the line, function, or even the exact address. In a language containing Exception Handling (such
In C ++), you can set breakpoints where exceptions occur.
In settings of SunOS 4.X, svr4, and Alpha OSF/1, you can also set breakpoints in the shared library.
An observation point is a special breakpoint. They work when the value of an expression changes in your program. You must
You must use other commands to set observation points. In addition to this feature, you can observe it like a normal breakpoint.
Click to perform operations-use the same command as a normal breakpoint operation to enable, disable, and delete an observation point.
You can arrange the program variables displayed when your program is interrupted.
When you set a breakpoint or observation point in a program, GDB assigns a value to each breakpoint or observation point.
This value must be used in the operation commands.

Set breakpoints
==================
Use 'Break' or 'B' to set breakpoints. GDB uses the environment variable $ bpnum to record your latest settings
Breakpoint.
You have many methods to set breakpoints.
 

'Break function'
This command is used to set breakpoints on a function. When you use languages that allow function overloading, such as C ++
You can set breakpoints on several overloaded functions at the same time.

'Break + offset'
'Break-offset'
Set breakpoints in the first or last lines of the current program. Offset is the row number.

'Break linenum'
Set a breakpoint on the line with linenum. The program stops running before this row.

'Break filename: linenum'
Set a breakpoint in line linenum of the original file named filename.

'Break filename: function'
Set a breakpoint on the function of the original file named filename.
When multiple files contain the same function name, the file name must be provided.

'Break * address'
Set a breakpoint on the address. This command allows you
Set breakpoints in sequence.
'Break'
When the 'Break' command does not contain any parameters
Set a breakpoint on the next command in the running stack in sequence. In addition to the stack bottom, this command causes
Once the program returns from the current function, it stops. Similar commands are 'shares', but 'shares'
Do not set breakpoints. This is useful in loop statements.
GDB executes at least one command when resuming execution.

'Break... If cond'
This command sets a condition breakpoint, which is specified by cond.
When a breakpoint occurs, Cond is calculated. When the value of Cond is non-zero, the program stops at the breakpoint. This means
When the value of Cond is true, the program stops .... Some parameters can be described below.

'Tbreak ARGs'
Set the breakpoint to only valid once. The use of argS is the same as that of the parameter in 'Break.

'Hbreak args'
Set a hardware-supported breakpoint. ARGs is the same as the 'Break' command.
'Break' is the same. However, this breakpoint needs to be supported by hardware.
All commands are valid. The main purpose of this command is to debug the EPROM/ROM program. Because
This command can be used to set breakpoints without changing the code. This can be the same as sparclite DSU
. When the program accesses certain variables and code, DSU sets a "trap ". Note:
You can only use one breakpoint at a time. When a breakpoint is set, the original breakpoint is deleted first.

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.