📄 gdb.html
字号:
</P><P>An explicit redirection in <CODE>run</CODE> overrides the <CODE>tty</CODE> command'seffect on the input/output device, but not its effect on the controllingterminal.</P><P>When you use the <CODE>tty</CODE> command or redirect input in the <CODE>run</CODE>command, only the input <EM>for your program</EM> is affected. The inputfor GDB still comes from your terminal.</P><H2><A NAME="SEC23" HREF="gdb_toc.html#TOC23">Debugging an already-running process</A></H2><P><A NAME="IDX56"></A><A NAME="IDX57"></A></P><DL COMPACT><DT><CODE>attach <VAR>process-id</VAR></CODE><DD>This command attaches to a running process--one that was startedoutside GDB. (<CODE>info files</CODE> shows your activetargets.) The command takes as argument a process ID. The usual way tofind out the process-id of a Unix process is with the <CODE>ps</CODE> utility,or with the <SAMP>`jobs -l'</SAMP> shell command.<CODE>attach</CODE> does not repeat if you press <KBD>RET</KBD> a second time afterexecuting the command.</DL><P>To use <CODE>attach</CODE>, your program must be running in an environmentwhich supports processes; for example, <CODE>attach</CODE> does not work forprograms on bare-board targets that lack an operating system. You mustalso have permission to send the process a signal.</P><P>When using <CODE>attach</CODE>, you should first use the <CODE>file</CODE> commandto specify the program running in the process and load its symbol table.See section <A HREF="gdb.html#SEC86">Commands to specify files</A>.</P><P>The first thing GDB does after arranging to debug the specifiedprocess is to stop it. You can examine and modify an attached processwith all the GDB commands that are ordinarily available when you startprocesses with <CODE>run</CODE>. You can insert breakpoints; you can step andcontinue; you can modify storage. If you would rather the processcontinue running, you may use the <CODE>continue</CODE> command afterattaching GDB to the process.</P><DL COMPACT><DT><CODE>detach</CODE><DD><A NAME="IDX58"></A> When you have finished debugging the attached process, you can use the<CODE>detach</CODE> command to release it from GDB control. Detachingthe process continues its execution. After the <CODE>detach</CODE> command,that process and GDB become completely independent once more, and youare ready to <CODE>attach</CODE> another process or start one with <CODE>run</CODE>.<CODE>detach</CODE> does not repeat if you press <KBD>RET</KBD> again afterexecuting the command.</DL><P>If you exit GDB or use the <CODE>run</CODE> command while you have anattached process, you kill that process. By default, GDB asksfor confirmation if you try to do either of these things; you cancontrol whether or not you need to confirm by using the <CODE>setconfirm</CODE> command (see section <A HREF="gdb.html#SEC99">Optional warnings and messages</A>).</P><H2><A NAME="SEC24" HREF="gdb_toc.html#TOC24">Killing the child process</A></H2><DL COMPACT><DT><CODE>kill</CODE><DD><A NAME="IDX59"></A> Kill the child process in which your program is running under GDB.</DL><P>This command is useful if you wish to debug a core dump instead of arunning process. GDB ignores any core dump file while your programis running.</P><P>On some operating systems, a program cannot be executed outside GDBwhile you have breakpoints set on it inside GDB. You can use the<CODE>kill</CODE> command in this situation to permit running your programoutside the debugger.</P><P>The <CODE>kill</CODE> command is also useful if you wish to recompile andrelink your program, since on many systems it is impossible to modify anexecutable file while it is running in a process. In this case, when younext type <CODE>run</CODE>, GDB notices that the file has changed, andreads the symbol table again (while trying to preserve your currentbreakpoint settings).</P><H2><A NAME="SEC25" HREF="gdb_toc.html#TOC25">Additional process information</A></H2><P><A NAME="IDX60"></A><A NAME="IDX61"></A>Some operating systems provide a facility called <SAMP>`/proc'</SAMP> that canbe used to examine the image of a running process using file-systemsubroutines. If GDB is configured for an operating system with thisfacility, the command <CODE>info proc</CODE> is available to report on severalkinds of information about the process running your program. <CODE>info proc</CODE> works only on SVR4 systems that support <CODE>procfs</CODE>.</P><DL COMPACT><DT><CODE>info proc</CODE><DD><A NAME="IDX62"></A> Summarize available information about the process.<A NAME="IDX63"></A><DT><CODE>info proc mappings</CODE><DD>Report on the address ranges accessible in the program, with informationon whether your program may read, write, or execute each range.<A NAME="IDX64"></A><DT><CODE>info proc times</CODE><DD>Starting time, user CPU time, and system CPU time for your program andits children.<A NAME="IDX65"></A><DT><CODE>info proc id</CODE><DD>Report on the process IDs related to your program: its own process ID,the ID of its parent, the process group ID, and the session ID.<A NAME="IDX66"></A><DT><CODE>info proc status</CODE><DD>General information on the state of the process. If the process isstopped, this report includes the reason for stopping, and any signalreceived.<DT><CODE>info proc all</CODE><DD>Show all the above information about the process.</DL><H2><A NAME="SEC26" HREF="gdb_toc.html#TOC26">Debugging programs with multiple threads</A></H2><P><A NAME="IDX67"></A><A NAME="IDX68"></A><A NAME="IDX69"></A>In some operating systems, a single program may have more than one<EM>thread</EM> of execution. The precise semantics of threads differ fromone operating system to another, but in general the threads of a singleprogram are akin to multiple processes--except that they share oneaddress space (that is, they can all examine and modify the samevariables). On the other hand, each thread has its own registers andexecution stack, and perhaps private memory.</P><P>GDB provides these facilities for debugging multi-threadprograms:</P><UL><LI>automatic notification of new threads<LI><SAMP>`thread <VAR>threadno</VAR>'</SAMP>, a command to switch among threads<LI><SAMP>`info threads'</SAMP>, a command to inquire about existing threads<LI><SAMP>`thread apply [<VAR>threadno</VAR>] [<VAR>all</VAR>] <VAR>args</VAR>'</SAMP>,a command to apply a command to a list of threads<LI>thread-specific breakpoints</UL><BLOCKQUOTE><P><EM>Warning:</EM> These facilities are not yet available on everyGDB configuration where the operating system supports threads.If your GDB does not support threads, these commands have noeffect. For example, a system without thread support shows no outputfrom <SAMP>`info threads'</SAMP>, and always rejects the <CODE>thread</CODE> command,like this:</P><PRE>(gdb) info threads(gdb) thread 1Thread ID 1 not known. Use the "info threads" command tosee the IDs of currently known threads.</PRE></BLOCKQUOTE><P><A NAME="IDX70"></A><A NAME="IDX71"></A>The GDB thread debugging facility allows you to observe allthreads while your program runs--but whenever GDB takescontrol, one thread in particular is always the focus of debugging.This thread is called the <EM>current thread</EM>. Debugging commands showprogram information from the perspective of the current thread.</P><P><A NAME="IDX72"></A><A NAME="IDX73"></A>Whenever GDB detects a new thread in your program, it displaysthe target system's identification for the thread with a message in theform <SAMP>`[New <VAR>systag</VAR>]'</SAMP>. <VAR>systag</VAR> is a thread identifierwhose form varies depending on the particular system. For example, onLynxOS, you might see</P><PRE>[New process 35 thread 27]</PRE><P>when GDB notices a new thread. In contrast, on an SGI system,the <VAR>systag</VAR> is simply something like <SAMP>`process 368'</SAMP>, with nofurther qualifier.</P><P><A NAME="IDX74"></A><A NAME="IDX75"></A>For debugging purposes, GDB associates its own threadnumber--always a single integer--with each thread in your program.</P><DL COMPACT><DT><CODE>info threads</CODE><DD><A NAME="IDX76"></A> Display a summary of all threads currently in yourprogram. GDB displays for each thread (in this order):<OL><LI>the thread number assigned by GDB<LI>the target system's thread identifier (<VAR>systag</VAR>)<LI>the current stack frame summary for that thread</OL>An asterisk <SAMP>`*'</SAMP> to the left of the GDB thread numberindicates the current thread.For example, </DL><PRE>(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</PRE><DL COMPACT><DT><CODE>thread <VAR>threadno</VAR></CODE><DD><A NAME="IDX77"></A> Make thread number <VAR>threadno</VAR> the current thread. The commandargument <VAR>threadno</VAR> is the internal GDB thread number, asshown in the first field of the <SAMP>`info threads'</SAMP> display.GDB responds by displaying the system identifier of the threadyou selected, and its current stack frame summary:<PRE>(gdb) thread 2[Switching to process 35 thread 23]0x34e5 in sigpause ()</PRE>As with the <SAMP>`[New ...]'</SAMP> message, the form of the text after<SAMP>`Switching to'</SAMP> depends on your system's conventions for identifyingthreads. <A NAME="IDX78"></A><DT><CODE>thread apply [<VAR>threadno</VAR>] [<VAR>all</VAR>] <VAR>args</VAR></CODE><DD>The <CODE>thread apply</CODE> command allows you to apply a command to one ormore threads. Specify the numbers of the threads that you want affectedwith the command argument <VAR>threadno</VAR>. <VAR>threadno</VAR> is the internalGDB thread number, as shown in the first field of the <SAMP>`infothreads'</SAMP> display. To apply a command to all threads, use <CODE>thread apply all</CODE> <VAR>args</VAR>. </DL><P><A NAME="IDX79"></A><A NAME="IDX80"></A><A NAME="IDX81"></A>Whenever GDB stops your program, due to a breakpoint or asignal, it automatically selects the thread where that breakpoint orsignal happened. GDB alerts you to the context switch with amessage of the form <SAMP>`[Switching to <VAR>systag</VAR>]'</SAMP> to identify thethread.</P><P>See section <A HREF="gdb.html#SEC40">Stopping and starting multi-thread programs</A>, formore information about how GDB behaves when you stop and startprograms with multiple threads.</P><P>See section <A HREF="gdb.html#SEC31">Setting watchpoints</A>, for information aboutwatchpoints in programs with multiple threads.</P><H2><A NAME="SEC27" HREF="gdb_toc.html#TOC27">Debugging programs with multiple processes</A></H2><P><A NAME="IDX82"></A><A NAME="IDX83"></A><A NAME="IDX84"></A>GDB has no special support for debugging programs which createadditional processes using the <CODE>fork</CODE> function. When a programforks, GDB will continue to debug the parent process and thechild process will run unimpeded. If you have set a breakpoint in anycode which the child then executes, the child will get a <CODE>SIGTRAP</CODE>signal which (unless it catches the signal) will cause it to terminate.</P><P>However, if you want to debug the child process there is a workaroundwhich isn't too painful. Put a call to <CODE>sleep</CODE> in the code whichthe child process executes after the fork. It may be useful to sleeponly if a certain environment variable is set, or a certain file exists,so that the delay need not occur when you don't want to run GDBon the child. While the child is sleeping, use the <CODE>ps</CODE> program toget its process ID. Then tell GDB (a new invocation ofGDB if you are also debugging the parent process) to attach tothe child process (see section <A HREF="gdb.html#SEC23">Debugging an already-running process</A>). From that point on you can debugthe child process just like any other process which you attached to.</P><H1><A NAME="SEC28" HREF="gdb_toc.html#TOC28">Stopping and Continuing</A></H1><P>The principal purposes of using a debugger are so that you can stop yourprogram before it terminates; or so that, if your program runs intotrouble, you can investigate and find out why.</P><P>Inside GDB, your program may stop for any of several reasons, suchasa signal,a breakpoint, or reaching a new line after a GDBcommand such as <CODE>step</CODE>. You may then examine and changevariables, set new breakpoints or remove old ones, and then continueexecution. Usually, the messages shown by GDB provide ampleexplanation of the status of your program--but you can also explicitlyrequest this information at any time.</P><DL COMPACT><DT><CODE>info program</CODE><DD><A NAME="IDX85"></A> Display information about the status of your program: whether it isrunning or not,what process it is,and why it stopped.</DL><H2><A NAME="SEC29" HREF="gdb_toc.html#TOC29">Breakpoints, watchpoints, and exceptions</A></H2><P><A NAME="IDX86"></A>A <EM>breakpoint</EM> makes your program stop whenever a certain point inthe program is reached. For each breakpoint, you can addconditions to control in finer detail whether your program stops.You can set breakpoints with the <CODE>break</CODE> command and its variants(see section <A HREF="gdb.html#SEC30">Setting breakpoints</A>), to specify the place whereyour program should stop by line number, func
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -