多线程应用调试

来源:互联网 发布:matlab 矩阵相似程度 编辑:程序博客网 时间:2024/06/18 15:56
 

一. Getting In and Out of gdb

  1. invoking gdb:
    • gdb 
    • gdb program 
    • gdb program core
    • gdb program pid 
    • gdb -q
  2. quit gdb: quitCtrl-D
  3. Shell Commands:
    • shell command-string
    • make make-args

    4. Logging Output :

    • set logging on(Enable logging)
    • set logging off (Disable logging)
    • set logging file file(Change the name of the current logfile. The default logfile is ‘gdb.txt’)



二. gdb Commands

  • repeat coommands: <Enter>
  • Command Completion: <Tab>
  • Getting Help: help/h 可查看命令帮助。
    • help class   
    • help command
    • apropos args(searches through all of the gdb commands)
    • show version 
    • set prompt $(set set the gdb prompt to a $-sign)




三. Running Programs Under gdb

  • Compiling for Debugging: 在编译的时候添加 -g 选项。
  • Starting your Program:
    • run/r(运行程序)
    • start ( start does the equivalent of setting a temporary breakpoint at the beginning of the main procedure and then invoking the ‘run’ command)
  • Your Program’s Arguments:
    • The arguments to your program can be specified by the arguments of the run command, run with no arguments uses the same arguments used by the previous run, or those set
      by the set args command.
    • set args (Specify the arguments to be used the next time your program is run.)
    • show args (Show the arguments to give your program when it is started.)
  • Your Program’s Environment:
    • path directory (Add directory to the front of the PATH environment variable)
    • show paths (Display the list of search paths for executables)
    • show environment [varname]<4>set environment varname [=value]
  • Your Program’s Working Directory:
    • cd directory(Set the gdb working directory to directory.)
    • pwd (Print the gdb working directory)
  • Your Program’s Input and Output:
    • info terminal(Displays information recorded by gdb about the terminal modes your program is using)
    • run > outfile(redirect your program’s input and/or output using shell redirection)
  • Debugging an Already-running Process:
    • attach process-id (attaches to a running process)
    • detach (detach the running process)
  • Killing the Child Process:  kill (Kill the child process in which your program is running under gdb.)
  • Setting a Bookmark to Return to Later: On linux, gdb is able to save a snapshot of a program’s state, called a checkpoint, and come back to it later, Returning to a checkpoint effectively undoes everything that has happened in the program since the checkpoint was saved. To use the checkpoint/restart method of debugging:
    • checkpoint (Save a snapshot of the debugged program’s current execution state. The checkpoint command takes no arguments, but each checkpoint is assigned a small integer id, similar to a breakpoint id)
    • info checkpoints (List the checkpoints that have been saved in the current debugging session)
    • restart checkpoint-id (Restore the program state that was saved as checkpoint number checkpoint-id)
    • delete checkpoint checkpoint-id (Delete the previously-saved checkpoint identified by checkpoint-id.)


三. Debugging Programs with Multiple Threads

  • thread threadno: switch among threads(threadno为info threads中前面的标号)
  • info threads: Display a summary of all threads currently in your program, An asterisk ‘*’ to the left of the gdb thread number indicates the current thread
  • thread apply [threadno ] [all ] args: apply a command to a list of threads
  • break linespec thread threadno/ break linespec thread threadno if ...(set breakpoints on all threads, or on a particular thread. linespec specifies source lines; there are several ways of writing them, but the effect is always to specify some source line, Use the qualifier ‘thread threadno’ with a breakpoint command to specify that you only want gdb to stop the program when a particular thread reaches this breakpoint. threadno is one of the numeric thread identifiers assigned by gdb, shown in the first column of the ‘info threads’ display. If you do not specify ‘thread threadno’ when you set a breakpoint, the breakpoint applies to all threads of your program.)
  • On some OSes, you can lock the OS scheduler and thus allow only a single thread to run.
    • set scheduler-locking mode (Set the scheduler locking mode. If it is off, then there is no locking and any thread may run at any time. If on, then only the current thread may run when the inferior is resumed.)
    •  show scheduler-locking (Display the current scheduler locking mode)




四. Debugging Programs with Multiple Processes

  • When a program forks, gdb will continue to debug the parent process and the child process will run unimpeded. If you have set a breakpoint in any code which the child then executes, the child will get a SIGTRAP signal which (unless it catches the signal) will cause it to terminate. 通用方法是: 在child process 的代码中加入sleep, 然后attach child-pid。
  • 在Linux平台上(GDB 6.6或以上版本):
    • set follow-fork-mode mode(Set the debugger response to a program call of fork or vfork, The mode argument can be: a> parent(The original process is debugged after a fork. The child process runs unimpeded. This is the default) b> child(The new process is debugged after a fork))
    • show follow-fork-mode (Display the current debugger response to a fork or vfork call.)
    • set detach-on-fork mode(Tells gdb whether to detach one of the processes after a fork, or retain debugger control over them both) the mode argument can be: a> on : The child process (or parent process, depending on the value of follow-fork-mode) will be detached and allowed to run independently. This is the default.   b> off:   Both processes will be held under the control of gdb. One process (child or parent, depending on the value of follow-fork-mode) is debugged as usual, while the other is held suspended.
    • show detach-on-follow (Show whether detach-on-follow mode is on/off.)
    • info forks(Print a list of all forked processes under the control of gdb)
    • fork fork-id(Make fork number fork-id the current process. The argument fork-id is the internal fork number assigned by gdb, as shown in the first field of the ‘info forks’ display.)



五. Stopping and Continuing

  • info program: display information about the status of your program: whether it is running or not, what process it is, and why it stopped.
  • Breakpoint: A breakpoint makes your program stop whenever a certain point in the program is reached. Breakpoints are set with thebreak/b command
    • break function (Set a breakpoint at entry to function function.)
    • break +offset break -offset (Set a breakpoint some number of lines forward or back from the position at which execution stopped in the currently selected stack frame)
    • break linenum(Set a breakpoint at line linenum in source file filename.)
    • break filename :function (Set a breakpoint at entry to function function found in file filename, very useful when multiple files contain similarly named functions)
    • break *address (Set a breakpoint at address address.)  
    • break(When called without any arguments, break sets a breakpoint at the next instruction to be executed in the selected stack frame)
    • break ... if cond (Set a breakpoint with condition cond, evaluate the expression cond each time the breakpoint is reached, and stop only if the value is nonzero) 
    • info breakpoints/break/watchpoints (Print a table of all breakpoints, watchpoints, and catchpoints set and not deleted.)
  • Watchpoints:   A watchpoint is a special breakpoint that stops your program when the value of an expression changes. The expression may be a value of a variable, or it could involve values of one or more variables combined by operators, such as ‘a + b’.
    • watch expr (Set a watchpoint for an expression, The simplest (and the most popular) use of this command is to watch the value of a single variable: (gdb) watch foo )
    • rwatch expr (Set a watchpoint that will break when the value of expr is read by the program)
    • awatch expr (Set a watchpoint that will break when expr is either read from or written into by the program.)
    • info watchpoints
  • Catchpoint: 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.
    • catch event (Stop when event occurs)
    • tcatch event (Set a catchpoint that is enabled only for one stop. The catchpoint is automatically deleted after the first time the event is caught.)
  • Deleting Breakpoints:  
    • clear (Delete any breakpoints at the next instruction to be executed in the selected stack frame ,When the innermost frame is selected, this is a good way to delete a breakpoint where your program just stopped.)
    • clear function / clear filename :function (Delete any breakpoints set at entry to the named function.)
    • clear linenum / clear filename :linenum (Delete any breakpoints set at or within the code of the specified linenum of the specified filename.)
    • delete [breakpoints] [range ...](Delete the breakpoints, watchpoints, or catchpoints of the breakpoint ranges pecified as arguments.)
  • Disabling Breakpoints:
    • disable [breakpoints] [range ...](Disable the specified breakpoints—or all breakpoints, if none are listed.)
    • enable [breakpoints] [range ...](Enable the specified breakpoints (or all defined breakpoints). They become effective once again in stopping your program.)
    • enable [breakpoints] once range ...(Enable the specified breakpoints temporarily. gdb disables any of these breakpoints immediately after stopping your program.)
    • enable [breakpoints] delete range ...(Enable the specified breakpoints to work once, then die.)
  • Continuing and Stepping:
    • continue / c / fg(Resume program execution, at the address where your program last stopped)  
    • step / s(Continue running your program until control reaches a different source line, then stop it and return control to gdb,step steps inside any functions called within the line.)
    • step count(Continue running as in step, but do so count times.)
    • next [count ](Continue to the next source line in the current (innermost) stack frame.)  
    • finish(Continue running until just after function in the selected stack frame returns. Print the returned value (if any).)  
    • until / u(Continue running until a source line past the current line, in the current stack frame, is reached. This command is used to avoid single stepping through a loop more than once.)
    • until/u location (Continue running your program until either the specified location is reached,
      or the current stack frame returns.)
    • advance location (Continue running the program up to the given location. )
    • stepi / si(Execute one machine instruction, then stop and return to the debugger.)
    • nexti / ni(Execute one machine instruction, but if it is a function call, proceed until the function returns.)
  • Signals:
    • info signals (Print a table of all the kinds of signals and how gdb has been told to handle each one) 
    • info signals sig (Similar, but print information only about the specified signal number) 
    • handle signal [keywords...] (Change the way gdb handles signal signal. signal can be the number of a signal or its name (with or without the ‘SIG’ at the beginning); a list of signal numbers of the form ‘low-high’; or the word ‘all’, meaning all the known signals.) The keywords allowed by the handle command can be abbreviated. Their full names are:
      • nostop (gdb should not stop your program when this signal happens. It may still print a message telling you that the signal has come in.)
      • stop (gdb should stop your program when this signal happens. This implies theprint keyword as well.) 
      • print (gdb should print a message when this signal happens)
      • noprint (gdb should not mention the occurrence of the signal at all. This implies the nostop keyword as well.) 
      • pass/noignore (gdb should allow your program to see this signal; your program can handle the signal, or else it may terminate if the signal is fatal and not handled.)
      • nopass/ignore (gdb should not allow your program to see this signal.)



六. Examining the Stack

  • Stack Frames: When your program is started, the stack has only one frame, that of the function main. This is called the initial frame or the outermost frame. Each time a function is called, anew frame is made. Each time a function returns, the frame for that function invocation is eliminated. If a function is recursive, there can be many frames for the same function. Gdb assigns numbers to all existing stack frames, starting with zero for the innermost frame, one for the frame that called it, and so on upward.
    • frame args (The frame command allows you to move from one stack frame to another, and to print the stack frame you select. args may be either the address of the frame or the stack frame number. Without an argument, frame prints the current stack frame.) 
    • select-frame (The select-frame command allows you to move from one stack frame to another without printing the frame. This is the silent version of frame.)
  • Backtraces: A backtrace is a summary of how your program got where it is. It shows one line per frame, for many frames, starting with the currently executing frame (frame zero), followed by its caller (frame one), and on up the stack.
    • backtrace/ bt(Print a backtrace of the entire stack: one line per frame for all frames in the stack. You can stop the backtrace at any time by typing the system interrupt character, normally Ctrl-c.)
    • backtrace n/bt n (Similar, but print only the innermost n frames.)  
    • backtrace -n/ bt -n
    • backtrace full/ bt full/bt full n / bt full -n (Print the values of the local variables also. n specifies the number of frames to print, as described above.)
    • The nameswhereinfo stack (abbreviated info s) are additional aliases for backtrace.
    • In a multi-threaded program, gdb by default shows the backtrace only for the current thread. To display the backtrace for several or all of the threads, use the command thread: For example, if you type thread apply all backtrace, gdb will display the backtrace for all the threads;
      (Similar, but print only the outermost n frames.)   and
  • Selecting a Frame:
    • frame n / f n (Select frame number n. Recall that frame zero is the innermost (currently executing) frame, frame one is the frame that called the innermost one, and so on. The highest-numbered frame is the one for main.)
    • frame addr/ f addr (Select the frame at address addr.)  
    • up n (Move n frames up the stack.)
    • down n (Move n frames down the stack.)   All of these commands end by printing two lines of output describing the frame. The first line shows the frame number, the function name, the arguments, and the source file and line number of execution in that frame. The second line shows the text of that source line.
  • Information About a Frame: 
    • frame/ f (When used without any argument, this command does not change which frame is selected, but prints a brief description of the currently selected stack frame.)
    • info frame /info f(This command prints a verbose description of the selected stack frame)
    • info frame addr/ info f addr (Print a verbose description of the frame at address addr, without selecting that frame.)
    • info args (Print the arguments of the selected frame, each on a separate line.)
    • info localsinfo catch (Print a list of all the exception handlers that are active in the current stack frame at the current point of execution.) (Print the local variables of the selected frame, each on a separate line.)




七. Examining Source Files

  • Printing Source Lines:
    • list linenum (Print lines centered around line number linenum in the current source file.)  
    • list function (Print lines centered around the beginning of function function.)
    • list (Print more lines.)
    • list -(Print lines just before the lines last printed.)
    • list + (Print lines just after the lines last printed. )
    • set listsize count(Make the list command display count source lines)
    • show listsize
  • Editing Source Files:
    • edit (Edit the current source file at the active line number in the program)
    • edit number (Edit the current source file with number as the active line number.)
    • edit function (Edit the file containing function at the beginning of its definition.)
  • Choosing your Editor: You can customize gdb to use any editor you want1. By default, it is ‘/bin/ex’, but you can change this by setting the environment variable EDITOR before using gdb. For example, to configure gdb to use the vi editor, you could use these commands with the sh shell: EDITOR=/usr/bin/vi ;
    ;export EDITOR; gdb ...
  • Searching Source Files:
    • forward-search regexp/ search regexp
    • reverse-search regexp
  • Specifying Source Directories: gdb has a list of directories to search
    for source files; this is called the source path. Each time gdb wants a source file, it tries all the directories in the list, in the order they are present in the list, until it finds a file with the desired name.
    • directory dirname .../dir dirname ... (Add directory dirname to the front of the source path. Several directory namesmay be given to this command, separated by ‘:’ or whitespace)
    • directory (Reset the source path to its default value)
    • show directories(Print the source path)
  • Source and Machine Code: You can use the command info line to map source lines to program addresses (and vice versa), and the command disassemble to display a range of addresses as machine instructions.


八. Examining Data

  • The usual way to examine data in your program is with the print command (abbreviated p), or its synonym inspect. It evaluates and prints the value of an expression of the language your program is written in.
    • print expr /print /f expr(expr is an expression (in the source language). By default the value of expr is printed in a format appropriate to its data type; you can choose a different format by specifying ‘/f’, where f is a letter specifying the format;)
    • print /print /f(If you omit expr, gdb displays the last value again) 。 A more low-level way of examining data is with the x command. It examines data in memory at a specified address and prints it in a specified format.
  • Expressions: print and many other gdb commands accept an expression and compute its value.
    • Any kind of constant, variable or operator defined by the programming language you are using is valid in an expression in gdb.
    • @’ is a binary operator for treating parts of memory as arrays.
    • ::’ allows you to specify a variable in terms of the file or function where it is defined. (file::variable or function::variable)
  • Artificial Arrays:
    • by referring to a contiguous span of memory as an artificial array, using the binary operator ‘@’. The left operand of ‘@’ should be the first element of the desired array and be an individual object. The right operand should be the desired length of the array. The result is an array value whose elements are all of the type of the left argument. For example: int *array = (int *) malloc (len * sizeof (int)); you can print the contents of array with: p *array@len
  • Output Formats: The simplest use of output formats is to say how to print a value already computed. This is done by starting the arguments of the print command with a slash and a format letter.The format letters supported are:
    • x (print the integer in hexadecimal)
    • d (Print as integer in signed decimal.)
    • u(Print as integer in unsigned decimal.)
    • o (Print as integer in octal.)
    • (Print as integer in binary.The letter ‘t’ stands for “two”.)
    • a(Print as an address)
    • c(print as a character constant.)
    • f (print using typical floating point syntax)
  • Examining Memory: You can use the command x (for “examine”) to examine memory in any of several formats, independently of your program’s data types.
    • x/nfu addrx addr(Use the x command to examine memory.n, f, and u are all optional parameters that specify how much memory to display and how to format it; addr is an expression giving the address where you want to start displaying memory.)
    • 参数:
      • n (the repeat count)
      • f (the display format)
      • u (the unit size) 。其中The unit size is any of:
        • b (byte);
        • h(halfwords, two bytes);
        • w (words, four bytes);
        • g(Giant words, eight bytes)
    • The addresses and contents printed by the x command are not saved in the value history because there is often too much of them and they would get in the way. Instead, gdb makes these values available for subsequent use in expressions as values of the convenience variables $_ and $__.
  • Automatic Display: Each expression added to the list is given a number to
    identify it; to remove an expression from the list, you specify that number.
    • display expr(Add the expression expr to the list of expressions to display each time your program stops.)
    • display/fmt expr (add the expression expr to the auto-display list but arrange to display it each time in the specified format fmt.)
    • display/fmt addr (For fmt ‘i’ or ‘s’, or including a unit-size or a number of units, add the expression addr as a memory address to be examined each time your program stops.) eg: display/i $pcundisplay dnums... (Remove item numbers dnums from the list of expressions to display)
    • disable display dnums...(Disable the display of item numbers dnums.)
    • enable display dnums...
    • display (Display the current values of the expressions on the list, just as is done when your program stops.)
    • info display (Print the list of expressions previously set up to display automatically, each one with its item number, but without showing the values.) 
    • 变量在作用域内自动display, 而在作用域外自动disable display (see the machine instruction about to be executed each time execution stops) 
  • Value History: Values printed by the print command are saved in the gdb value history. This allows you to refer to them in other expressions. The values printed are given history numbers by which you can refer to them.print shows you the history number assigned to a value by printing ‘$num = ’ before the value; here num is the history number. To refer to any previous value, use ‘$’ followed by the value’s history number. Just $ refers to the most recent value in the history, and $$ refers to the value before that. $$n refers to the nth value from the end; eg:p *$ 
    • show values(Print the last ten values in the value history, with their item numbers.)
    • show values n (Print ten history values centered on history item number n.)
    • show values + (Print ten history values just after the values last printed.)
  • Registers: You can refer to machine register contents, in expressions, as variables with names starting with ‘$’.
    • info registers (Print the names and values of all registers except floating-point and vector registers (in the selected stack frame))
    • info all-registers (Print the names and values of all registers, including floating-point and vector registers (in the selected stack frame))
    • info registers regname ...(Print the relativized value of each specified register regname.)
  • Operating System Auxiliary Information...
  • Produce a Core File from Your Program: generate-core-file [file] / gcore [file] (Produce a core dump of the inferior process. The optional argument file specifies the file name where to put the core dump. If not specified, the file name defaults to ‘core.pid’, where pid is the inferior process ID.)
  • Character Sets:
    • set target-charset charset (Set the current target character set to charset. if you type set target-charset followed by <TAB><TAB>, gdb will list the target character sets it supports.)
    • set host-charset charset (Set the current host character set to charset.)
    • set charset charset (Set the current host and target character sets to charset. )
    • show charset/show host-charset /show target-charset
原创粉丝点击