📄 c-debugger7.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><link rel="STYLESHEET" type="text/css" href="wrs.css"><title> Debugger </title></head><body bgcolor="FFFFFF"><p class="navbar" align="right"><a href="index.html"><img border="0" alt="[Contents]" src="icons/contents.gif"></a><a href="GuideIX.html"><img border="0" alt="[Index]" src="icons/index.gif"></a><a href="c-debugger.html"><img border="0" alt="[Top]" src="icons/top.gif"></a><a href="c-debugger6.html"><img border="0" alt="[Prev]" src="icons/prev.gif"></a><a href="c-debugger8.html"><img border="0" alt="[Next]" src="icons/next.gif"></a></p><font face="Helvetica, sans-serif" class="sans"><h3 class="H2"><i><a name="85425">8.7 Tcl: Debugger Automation</a></i></h3></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="85427"> </a>The debugger exploits Tcl at two levels: like other Tornado tools, it uses Tcl to build the graphical interface, but it also includes a Tcl interpreter at the GDB command level. This section discusses using the Tcl interpreter inside the Tornado enhanced GDB, at the command level. </p></dl></dl><dl class="margin"><dd><p class="table" callout><table border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td valign="top" width="40"><br><img border="0" alt="*" src="icons/note.gif"></td><td><hr><div class="CalloutCell"><a name="92149"><b class="symbol_UC"><font face="Helvetica, sans-serif" size="-1" class="sans">NOTE: </font></b></a>For information about using Tcl to customize the Tornado GUI, see <a href="c-debugger8.html#85600"><i class="title">8.8 Tcl: Debugger Customization</i></a>. This section is mainly of interest when you need complex debugger macros; thus, you might want to skip it on first reading.</div></td></tr><tr valign="top"><td></td><td><hr></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p callout><dl class="margin"><dd><p class="Body"><a name="85438"> </a>Tcl has two major advantages over the other GDB macro facility (the <b class="command">define</b> command). First, Tcl provides control and looping (such as <b class="tclProc">for</b>, <b class="tclProc">foreach</b>, <b class="tclProc">while</b>, and <b class="tclProc">case</b>). Second, Tcl procedures can take parameters. Tcl, building on the command interface, extends the scripting facility of GDB to allow you to create new commands.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="85439">8.7.1 Tcl: A Simple Debugger Example </a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="85443"> </a>To submit commands to the Tcl interpreter within GDB from the Tornado <b class="guiLabel"><font face="Helvetica, sans-serif" size="-1" class="sans">Debugger</font></b> window, use the <b class="command">tcl</b> command. For example:</p><dl class="margin"><dd><pre class="Code2"><b><a name="85445"></b><tt class="output">(gdb) </tt><b>tcl info tclversion</a></b></pre></dl><dd><p class="Body"><a name="85446"> </a>This command reports which version of Tcl is integrated with GDB. All the text passed as arguments to the <b class="command">tcl</b> command (in this example, <b class="keyword">info tclversion</b>) is provided to the Tcl interpreter exactly as typed. Convenience variables (described in <i class="title">Debugging with GDB: Convenience Variables</i>) are not expanded by GDB. However, Tcl scripts can force GDB to evaluate their arguments; see <a href="c-debugger7.html#85499"><i class="title">8.7.3 Tcl: Invoking GDB Facilities</i></a>.</p><dd><p class="Body"><a name="85451"> </a>You can also define Tcl procedures from the GDB command line. The following example procedure, <b class="tclProc">mload</b>, calls the <b class="command">load</b> command for each file in a list:</p><dl class="margin"><dd><pre class="Code2"><b><a name="85452"></b><tt class="output">(gdb) </tt><b>tcl proc mload args {foreach obj $args {gdb load $obj}}</a></b></pre></dl><dd><p class="Body"><a name="85453"> </a>You can run the new procedure from the GDB command line; for example:</p><dl class="margin"><dd><pre class="Code2"><b><a name="85454"></b><tt class="output">(gdb)</tt><b> tcl mload vxColor.o priTst.o</a></b></pre></dl><dd><p class="Body"><a name="85456"> </a>To avoid typing <b class="command">tcl</b> every time, use the <b class="command">tclproc</b> command to assign a new GDB command name to the Tcl procedure. For example:</p><dl class="margin"><dd><pre class="Code2"><b><a name="85457"></b><tt class="output">(gdb)</tt><b> tclproc mld mload</a></b></pre></dl><dd><p class="Body"><a name="85458"> </a>This command creates a new GDB command, <b class="command">mld</b>. Now, instead of typing <b class="command">tcl mload</b>, you can run <b class="command">mld</b> as follows:</p><dl class="margin"><dd><pre class="Code2"><b><a name="85459"></b><tt class="output">(gdb) </tt><b>mld vxColor.o priTst.o</a></b></pre></dl><dd><p class="Body"><a name="85460"> </a>You can collect Tcl procedures in a file, and load them into the GDB Tcl interpreter with this command:</p><dl class="margin"><dd><pre class="Code2"><b><a name="85461"></b><tt class="output">(gdb) </tt><b>tcl source <i class="textVariable">tclFile</i></a></b></pre></dl><dd><p class="Body"><a name="85462"> </a>If you develop a collection of Tcl procedures that you want to make available automatically in all your debugging sessions, write them in the file <b class="file">gdb.tcl</b>. The GDB Tcl interpreter reads this file when it begins executing. (See <a href="c-debugger8.html#85604"><i class="title">8.8.1 Tcl: Debugger Initialization Files</i></a> for a discussion of where you can put this file, and of how all the Tornado debugger and GDB initialization files interact.)</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="85467">8.7.2 Tcl: Specialized GDB Commands</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="85471"> </a>The Tornado debugger (CrossWind) includes four commands to help you use Tcl. The first two were discussed in the previous section. The commands are:</p></dl><dl class="margin"><dd><div class="Item"><a name="85473"> </a><b class="command">tcl</b> <i class="textVariable">command</i> </div><dl class="margin"><dd><div class="Indent"><a name="86139"> </a>Passes the remainder of the command line to the Tcl interpreter, without attempting to evaluate any of the text as a GDB command.</div><br></dl><dd><div class="Item"><a name="85475"> </a><b class="command">tclproc</b> <i class="textVariable">gdbName</i> <i class="textVariable">TclName</i> </div><dl class="margin"><dd><div class="Indent"><a name="85476"> </a>Creates a GDB command <i class="textVariable">gdbName</i> for a Tcl procedure named <i class="textVariable">TclName</i>. GDB does not evaluate the arguments when <i class="textVariable">gdbName </i>is invoked; it passes them to the Tcl procedure just as they were entered.</div><br></dl></dl></dl><dl class="margin"><dd><p class="table" callout><table border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td valign="top" width="40"><br><img border="0" alt="*" src="icons/note.gif"></td><td><hr><div class="CalloutCell"><a name="92173"><b class="symbol_UC"><font face="Helvetica, sans-serif" size="-1" class="sans">NOTE: </font></b></a>To execute <b class="command">tclproc</b> commands automatically when GDB begins executing, you can place them in <b class="file">.gdbinit</b> directly (see <a href="c-debugger5.html#85162"><i class="title">8.5.1 GDB Initialization Files</i></a>), because <b class="command">tclproc</b> is a GDB command rather than a Tcl command. However, if you want to keep the <b class="command">tclproc</b> definition together with supporting Tcl code, you can exploit the <b class="tclProc">gdb</b> Tcl extension described in <a href="c-debugger7.html#85499"><i class="title">8.7.3 Tcl: Invoking GDB Facilities</i></a> to call <b class="tclProc">gdb</b> <b class="command">tclproc</b> in <b class="file">gdb.tcl</b>.</div></td></tr><tr valign="top"><td></td><td><hr></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p callout><dl class="margin"><dd><div class="Item"><a name="85485"> </a><b class="command">tcldebug</b> </div><dl class="margin"><dd><div class="Indent"><a name="86144"> </a>Toggles Tcl debugging mode. Helps debug Tcl scripts that use GDB facilities. When Tcl debugging is ON, all GDB commands or other GDB queries made by the Tcl interpreter are printed.</div><br></dl><dd><div class="Item"><a name="85487"> </a><b class="command">tclerror</b> </div><dl class="margin"><dd><div class="Indent"><a name="86146"> </a>Toggles Tcl verbose error printing, to help debug Tcl scripts. When verbose error mode is ON, the entire stack of error information maintained by the Tcl interpreter appears when a Tcl error occurs that is not caught. Otherwise, when verbose error mode is OFF, only the innermost error message is printed. For example:</div><br><dl class="margin"><dd><pre class="Code3"><b><a name="85488"></b><tt class="output">(gdb) </tt><b>tcl puts stdout [expr $x+2] </b><tt class="output">can't read "x": no such variable</tt><b></a></b><dd> <b><a name="85489"></b><tt class="output">(gdb) </tt><b>tclerror </b><tt class="output">TCL verbose error reporting is ON.</tt><b></a></b><dd> <b><a name="85490"></b><tt class="output">(gdb)</tt><b> tcl puts stdout [expr $x+2] </b><tt class="output">can't read "x": no such variable while executing "expr $x..." invoked from within "puts stdout [expr $x..."</tt><b></a></b></pre></dl></dl></dl><dl class="margin"><dd><p class="Body"><a name="85492"> </a>Tcl also stores the error stack in a global variable, <b class="symbol_lc">errorInfo</b>. To see the error stack when Tcl verbose error mode is OFF, examine this variable as follows:</p><dl class="margin"><dd><pre class="Code2"><b><a name="85493"></b><tt class="output">(gdb)</tt><b> tcl $errorInfo</a></b></pre></dl><dd><p class="Body"><a name="85497"> </a>For more information about error handling in Tcl, see <a href="x-tclsumm2.html#84341"><i class="title">B.2.9 Tcl Error Handling</i></a>.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="85499">8.7.3 Tcl: Invoking GDB Facilities</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="85501"> </a>You can access GDB facilities from Tcl scripts with the following Tcl extensions:</p></dl><dl class="margin"><dd><div class="Item"><a name="85503"> </a><b class="tclProc">gdb</b> <i class="textVariable">arguments</i> </div><dl class="margin"><dd><div class="Indent"><a name="86148"> </a>Executes a GDB command (the converse of the GDB <b class="command">tcl</b> command). Tcl evaluates the arguments, performing all applicable substitutions, then combines them (separated by spaces) into one string, which is passed to GDB's internal command interpreter for execution. </div><br><dd><div class="Indent"><a name="85504"> </a>If the GDB command produces output, it is shown in the <b class="guiLabel"><font face="Helvetica, sans-serif" size="-1" class="sans">Debugger</font></b> window.</div><br><dd><div class="Indent"><a name="85505"> </a>If Tcl debugging is enabled (with <b class="command">tcldebug</b>), the following message is printed:</div><br><dl class="margin"><dd><pre class="Code3"><b><a name="85506"></b><tt class="output"> execute:</tt><b> <i class="textVariable">command</i> </a></b></pre></dl><dd><div class="Indent"><a name="85507"> </a>If the GDB command causes an error, the Tcl procedure <b class="tclProc">gdb</b> signals a Tcl error, which causes unwinding if not caught (for information about unwinding, see <a href="x-tclsumm2.html#84341"><i class="title">B.2.9 Tcl Error Handling</i></a>).</div><br></dl><dd><div class="Item"><a name="85512"> </a><b class="tclProc">gdbEvalScalar </b><i class="textVariable">exprlist</i> </div><dl class="margin"><dd><div class="Indent"><a name="85513"> </a>Evaluates a list of expressions <i class="textVariable">exprlist</i> and returns a list of single integer values (in hexadecimal), one for each element of <i class="textVariable">exprlist</i>.<sup><a href="#foot"><b class="FootnoteMarker">1</b></a></sup> If an expression represents a scalar value (such as <b class="keyword">int</b>, <b class="keyword">long</b>, or <b class="keyword">char</b>), that value is returned. If an expression represents a <b class="keyword">float</b> or <b class="keyword">double</b>, the fractional part is truncated. If an expression represents an aggregate type, such as a structure or array, the address of the indicated object is returned. Standard rules for Tcl argument evaluation apply. </div><br><dd><div class="Indent"><a name="85517"> </a>If Tcl debugging is enabled, the following message is printed for each expression:</div><br><dl class="margin">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -