<HTML> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <!-- Created on March, 27 2008 by texi2html 1.64 --> <!-- Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author) Karl Berry <karl@freefriends.org> Olaf Bachmann <obachman@mathematik.uni-kl.de> and many others. Maintained by: Olaf Bachmann <obachman@mathematik.uni-kl.de> Send bugs and suggestions to <texi2html@mathematik.uni-kl.de> --> <HEAD> <TITLE>Debugging with GDB: Sequences</TITLE> <META NAME="description" CONTENT="Debugging with GDB: Sequences"> <META NAME="keywords" CONTENT="Debugging with GDB: Sequences"> <META NAME="resource-type" CONTENT="document"> <META NAME="distribution" CONTENT="global"> <META NAME="Generator" CONTENT="texi2html 1.64"> </HEAD> <BODY LANG="" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000"> <A NAME="SEC229"></A> <TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0> <TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_20.html#SEC228"> < </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC230"> > </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_4.html#SEC14"> << </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb.html#SEC_Top"> Up </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_22.html#SEC234"> >> </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb.html#SEC_Top">Top</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_toc.html#SEC_Contents">Contents</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_38.html#SEC764">Index</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_abt.html#SEC_About"> ? </A>]</TD> </TR></TABLE> <H1> 20. Canned Sequences of Commands </H1> <!--docid::SEC229::--> <P> Aside from breakpoint commands (see section <A HREF="gdb_6.html#SEC39">Breakpoint Command Lists</A>), GDB provides two ways to store sequences of commands for execution as a unit: user-defined commands and command files. </P><P> <BLOCKQUOTE><TABLE BORDER=0 CELLSPACING=0> <TR><TD ALIGN="left" VALIGN="TOP"><A HREF="gdb_21.html#SEC230">20.1 User-defined Commands</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">How to define your own commands</TD></TR> <TR><TD ALIGN="left" VALIGN="TOP"><A HREF="gdb_21.html#SEC231">20.2 User-defined Command Hooks</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Hooks for user-defined commands</TD></TR> <TR><TD ALIGN="left" VALIGN="TOP"><A HREF="gdb_21.html#SEC232">20.3 Command Files</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">How to write scripts of commands to be stored in a file</TD></TR> <TR><TD ALIGN="left" VALIGN="TOP"><A HREF="gdb_21.html#SEC233">20.4 Commands for Controlled Output</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Commands for controlled output</TD></TR> </TABLE></BLOCKQUOTE> <P> <A NAME="Define"></A> <HR SIZE="6"> <A NAME="SEC230"></A> <TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0> <TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC229"> < </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC231"> > </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC229"> << </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC229"> Up </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_22.html#SEC234"> >> </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb.html#SEC_Top">Top</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_toc.html#SEC_Contents">Contents</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_38.html#SEC764">Index</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_abt.html#SEC_About"> ? </A>]</TD> </TR></TABLE> <H2> 20.1 User-defined Commands </H2> <!--docid::SEC230::--> <P> <A NAME="IDX1183"></A> <A NAME="IDX1184"></A> A <EM>user-defined command</EM> is a sequence of GDB commands to which you assign a new name as a command. This is done with the <CODE>define</CODE> command. User commands may accept up to 10 arguments separated by whitespace. Arguments are accessed within the user command via <CODE>$arg0<small>...</small>$arg9</CODE>. A trivial example: </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>define adder print $arg0 + $arg1 + $arg2 end </FONT></pre></td></tr></table></P><P> To execute the command use: </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>adder 1 2 3 </FONT></pre></td></tr></table></P><P> This defines the command <CODE>adder</CODE>, which prints the sum of its three arguments. Note the arguments are text substitutions, so they may reference variables, use complex expressions, or even perform inferior functions calls. </P><P> <A NAME="IDX1185"></A> <A NAME="IDX1186"></A> In addition, <CODE>$argc</CODE> may be used to find out how many arguments have been passed. This expands to a number in the range 0<small>...</small>10. </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>define adder if $argc == 2 print $arg0 + $arg1 end if $argc == 3 print $arg0 + $arg1 + $arg2 end end </FONT></pre></td></tr></table></P><P> <DL COMPACT> <A NAME="IDX1187"></A> <DT><CODE>define <VAR>commandname</VAR></CODE> <DD>Define a command named <VAR>commandname</VAR>. If there is already a command by that name, you are asked to confirm that you want to redefine it. <P> The definition of the command is made up of other GDB command lines, which are given following the <CODE>define</CODE> command. The end of these commands is marked by a line containing <CODE>end</CODE>. </P><P> <A NAME="IDX1188"></A> <A NAME="IDX1189"></A> <DT><CODE>document <VAR>commandname</VAR></CODE> <DD>Document the user-defined command <VAR>commandname</VAR>, so that it can be accessed by <CODE>help</CODE>. The command <VAR>commandname</VAR> must already be defined. This command reads lines of documentation just as <CODE>define</CODE> reads the lines of the command definition, ending with <CODE>end</CODE>. After the <CODE>document</CODE> command is finished, <CODE>help</CODE> on command <VAR>commandname</VAR> displays the documentation you have written. <P> You may use the <CODE>document</CODE> command again to change the documentation of a command. Redefining the command with <CODE>define</CODE> does not change the documentation. </P><P> <A NAME="IDX1190"></A> <A NAME="IDX1191"></A> <DT><CODE>dont-repeat</CODE> <DD>Used inside a user-defined command, this tells GDB that this command should not be repeated when the user hits <KBD>RET</KBD> (see section <A HREF="gdb_4.html#SEC15">repeat last command</A>). <P> <A NAME="IDX1192"></A> <DT><CODE>help user-defined</CODE> <DD>List all user-defined commands, with the first line of the documentation (if any) for each. <P> <A NAME="IDX1193"></A> <DT><CODE>show user</CODE> <DD><DT><CODE>show user <VAR>commandname</VAR></CODE> <DD>Display the GDB commands used to define <VAR>commandname</VAR> (but not its documentation). If no <VAR>commandname</VAR> is given, display the definitions for all user-defined commands. <P> <A NAME="IDX1194"></A> <A NAME="IDX1195"></A> <A NAME="IDX1196"></A> <DT><CODE>show max-user-call-depth</CODE> <DD><DT><CODE>set max-user-call-depth</CODE> <DD>The value of <CODE>max-user-call-depth</CODE> controls how many recursion levels are allowed in user-defined commands before GDB suspects an infinite recursion and aborts the command. </DL> <P> In addition to the above commands, user-defined commands frequently use control flow commands, described in <A HREF="gdb_21.html#SEC232">20.3 Command Files</A>. </P><P> When user-defined commands are executed, the commands of the definition are not printed. An error in any command stops execution of the user-defined command. </P><P> If used interactively, commands that would ask for confirmation proceed without asking when used inside a user-defined command. Many GDB commands that normally print messages to say what they are doing omit the messages when used in a user-defined command. </P><P> <A NAME="Hooks"></A> <HR SIZE="6"> <A NAME="SEC231"></A> <TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0> <TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC230"> < </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC232"> > </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC232"> << </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC229"> Up </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_22.html#SEC234"> >> </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb.html#SEC_Top">Top</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_toc.html#SEC_Contents">Contents</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_38.html#SEC764">Index</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_abt.html#SEC_About"> ? </A>]</TD> </TR></TABLE> <H2> 20.2 User-defined Command Hooks </H2> <!--docid::SEC231::--> <P> <A NAME="IDX1197"></A> You may define <EM>hooks</EM>, which are a special kind of user-defined command. Whenever you run the command <SAMP>`foo'</SAMP>, if the user-defined command <SAMP>`hook-foo'</SAMP> exists, it is executed (with no arguments) before that command. </P><P> <A NAME="IDX1198"></A> <A NAME="IDX1199"></A> A hook may also be defined which is run after the command you executed. Whenever you run the command <SAMP>`foo'</SAMP>, if the user-defined command <SAMP>`hookpost-foo'</SAMP> exists, it is executed (with no arguments) after that command. Post-execution hooks may exist simultaneously with pre-execution hooks, for the same command. </P><P> It is valid for a hook to call the command which it hooks. If this occurs, the hook is not re-executed, thereby avoiding infinite recursion. </P><P> <A NAME="IDX1200"></A> In addition, a pseudo-command, <SAMP>`stop'</SAMP> exists. Defining (<SAMP>`hook-stop'</SAMP>) makes the associated commands execute every time execution stops in your program: before breakpoint commands are run, displays are printed, or the stack frame is printed. </P><P> For example, to ignore <CODE>SIGALRM</CODE> signals while single-stepping, but treat them normally during normal execution, you could define: </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>define hook-stop handle SIGALRM nopass end define hook-run handle SIGALRM pass end define hook-continue handle SIGALRM pass end </FONT></pre></td></tr></table></P><P> As a further example, to hook at the beginning and end of the <CODE>echo</CODE> command, and to add extra text to the beginning and end of the message, you could define: </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>define hook-echo echo <<<--- end define hookpost-echo echo --->>>\n end (gdb) echo Hello World <<<---Hello World--->>> (gdb) </FONT></pre></td></tr></table></P><P> You can define a hook for any single-word command in GDB, but not for command aliases; you should define a hook for the basic command name, e.g. <CODE>backtrace</CODE> rather than <CODE>bt</CODE>. If an error occurs during the execution of your hook, execution of GDB commands stops and GDB issues a prompt (before the command that you actually typed had a chance to run). </P><P> If you try to define a hook which does not match any known command, you get a warning from the <CODE>define</CODE> command. </P><P> <A NAME="Command Files"></A> <HR SIZE="6"> <A NAME="SEC232"></A> <TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0> <TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC231"> < </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC233"> > </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC233"> << </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC229"> Up </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_22.html#SEC234"> >> </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb.html#SEC_Top">Top</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_toc.html#SEC_Contents">Contents</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_38.html#SEC764">Index</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_abt.html#SEC_About"> ? </A>]</TD> </TR></TABLE> <H2> 20.3 Command Files </H2> <!--docid::SEC232::--> <P> <A NAME="IDX1201"></A> <A NAME="IDX1202"></A> A command file for GDB is a text file made of lines that are GDB commands. Comments (lines starting with <KBD>#</KBD>) may also be included. An empty line in a command file does nothing; it does not mean to repeat the last command, as it would from the terminal. </P><P> You can request the execution of a command file with the <CODE>source</CODE> command: </P><P> <DL COMPACT> <A NAME="IDX1203"></A> <A NAME="IDX1204"></A> <DT><CODE>source [<CODE>-v</CODE>] <VAR>filename</VAR></CODE> <DD>Execute the command file <VAR>filename</VAR>. </DL> <P> The lines in a command file are generally executed sequentially, unless the order of execution is changed by one of the <EM>flow-control commands</EM> described below. The commands are not printed as they are executed. An error in any command terminates execution of the command file and control is returned to the console. </P><P> GDB searches for <VAR>filename</VAR> in the current directory and then on the search path (specified with the <SAMP>`directory'</SAMP> command). </P><P> If <CODE>-v</CODE>, for verbose mode, is given then GDB displays each command as it is executed. The option must be given before <VAR>filename</VAR>, and is interpreted as part of the filename anywhere else. </P><P> Commands that would ask for confirmation if used interactively proceed without asking when used in a command file. Many GDB commands that normally print messages to say what they are doing omit the messages when called from command files. </P><P> GDB also accepts command input from standard input. In this mode, normal output goes to standard output and error output goes to standard error. Errors in a command file supplied on standard input do not terminate execution of the command file--execution continues with the next command. </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>gdb < cmds > log 2>&1 </FONT></pre></td></tr></table></P><P> (The syntax above will vary depending on the shell used.) This example will execute commands from the file <TT>`cmds'</TT>. All output and errors would be directed to <TT>`log'</TT>. </P><P> Since commands stored on command files tend to be more general than commands typed interactively, they frequently need to deal with complicated situations, such as different or unexpected values of variables and symbols, changes in how the program being debugged is built, etc. GDB provides a set of flow-control commands to deal with these complexities. Using these commands, you can write complex scripts that loop over data structures, execute commands conditionally, etc. </P><P> <DL COMPACT> <A NAME="IDX1205"></A> <A NAME="IDX1206"></A> <DT><CODE>if</CODE> <DD><DT><CODE>else</CODE> <DD>This command allows to include in your script conditionally executed commands. The <CODE>if</CODE> command takes a single argument, which is an expression to evaluate. It is followed by a series of commands that are executed only if the expression is true (its value is nonzero). There can then optionally be an <CODE>else</CODE> line, followed by a series of commands that are only executed if the expression was false. The end of the list is marked by a line containing <CODE>end</CODE>. <P> <A NAME="IDX1207"></A> <DT><CODE>while</CODE> <DD>This command allows to write loops. Its syntax is similar to <CODE>if</CODE>: the command takes a single argument, which is an expression to evaluate, and must be followed by the commands to execute, one per line, terminated by an <CODE>end</CODE>. These commands are called the <EM>body</EM> of the loop. The commands in the body of <CODE>while</CODE> are executed repeatedly as long as the expression evaluates to true. <P> <A NAME="IDX1208"></A> <DT><CODE>loop_break</CODE> <DD>This command exits the <CODE>while</CODE> loop in whose body it is included. Execution of the script continues after that <CODE>while</CODE>s <CODE>end</CODE> line. <P> <A NAME="IDX1209"></A> <DT><CODE>loop_continue</CODE> <DD>This command skips the execution of the rest of the body of commands in the <CODE>while</CODE> loop in whose body it is included. Execution branches to the beginning of the <CODE>while</CODE> loop, where it evaluates the controlling expression. <P> <A NAME="IDX1210"></A> <DT><CODE>end</CODE> <DD>Terminate the block of commands that are the body of <CODE>if</CODE>, <CODE>else</CODE>, or <CODE>while</CODE> flow-control commands. </DL> <P> <A NAME="Output"></A> <HR SIZE="6"> <A NAME="SEC233"></A> <TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0> <TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC232"> < </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_22.html#SEC234"> > </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC229"> << </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC229"> Up </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_22.html#SEC234"> >> </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb.html#SEC_Top">Top</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_toc.html#SEC_Contents">Contents</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_38.html#SEC764">Index</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_abt.html#SEC_About"> ? </A>]</TD> </TR></TABLE> <H2> 20.4 Commands for Controlled Output </H2> <!--docid::SEC233::--> <P> During the execution of a command file or a user-defined command, normal GDB output is suppressed; the only output that appears is what is explicitly printed by the commands in the definition. This section describes three commands useful for generating exactly the output you want. </P><P> <DL COMPACT> <A NAME="IDX1211"></A> <DT><CODE>echo <VAR>text</VAR></CODE> <DD>Print <VAR>text</VAR>. Nonprinting characters can be included in <VAR>text</VAR> using C escape sequences, such as <SAMP>`\n'</SAMP> to print a newline. <STRONG>No newline is printed unless you specify one.</STRONG> In addition to the standard C escape sequences, a backslash followed by a space stands for a space. This is useful for displaying a string with spaces at the beginning or the end, since leading and trailing spaces are otherwise trimmed from all arguments. To print <SAMP>` and foo = '</SAMP>, use the command <SAMP>`echo \ and foo = \ '</SAMP>. <P> A backslash at the end of <VAR>text</VAR> can be used, as in C, to continue the command onto subsequent lines. For example, </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>echo This is some text\n\ which is continued\n\ onto several lines.\n </FONT></pre></td></tr></table></P><P> produces the same output as </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>echo This is some text\n echo which is continued\n echo onto several lines.\n </FONT></pre></td></tr></table></P><P> <A NAME="IDX1212"></A> <DT><CODE>output <VAR>expression</VAR></CODE> <DD>Print the value of <VAR>expression</VAR> and nothing but that value: no newlines, no <SAMP>`$<VAR>nn</VAR> = '</SAMP>. The value is not entered in the value history either. See section <A HREF="gdb_9.html#SEC60">Expressions</A>, for more information on expressions. <P> <DT><CODE>output/<VAR>fmt</VAR> <VAR>expression</VAR></CODE> <DD>Print the value of <VAR>expression</VAR> in format <VAR>fmt</VAR>. You can use the same formats as for <CODE>print</CODE>. See section <A HREF="gdb_9.html#SEC63">Output Formats</A>, for more information. <P> <A NAME="IDX1213"></A> <DT><CODE>printf <VAR>template</VAR>, <VAR>expressions</VAR><small>...</small></CODE> <DD>Print the values of one or more <VAR>expressions</VAR> under the control of the string <VAR>template</VAR>. To print several values, make <VAR>expressions</VAR> be a comma-separated list of individual expressions, which may be either numbers or pointers. Their values are printed as specified by <VAR>template</VAR>, exactly as a C program would do by executing the code below: <P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>printf (<VAR>template</VAR>, <VAR>expressions</VAR><small>...</small>); </FONT></pre></td></tr></table></P><P> As in <CODE>C</CODE> <CODE>printf</CODE>, ordinary characters in <VAR>template</VAR> are printed verbatim, while <EM>conversion specification</EM> introduced by the <SAMP>`%'</SAMP> character cause subsequent <VAR>expressions</VAR> to be evaluated, their values converted and formatted according to type and style information encoded in the conversion specifications, and then printed. </P><P> For example, you can print two values in hex like this: </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>printf "foo, bar-foo = 0x%x, 0x%x\n", foo, bar-foo </FONT></pre></td></tr></table></P><P> <CODE>printf</CODE> supports all the standard <CODE>C</CODE> conversion specifications, including the flags and modifiers between the <SAMP>`%'</SAMP> character and the conversion letter, with the following exceptions: </P><P> <UL> <LI> The argument-ordering modifiers, such as <SAMP>`2$'</SAMP>, are not supported. <P> <LI> The modifier <SAMP>`*'</SAMP> is not supported for specifying precision or width. <P> <LI> The <SAMP>`''</SAMP> flag (for separation of digits into groups according to <CODE>LC_NUMERIC'</CODE>) is not supported. <P> <LI> The type modifiers <SAMP>`hh'</SAMP>, <SAMP>`j'</SAMP>, <SAMP>`t'</SAMP>, and <SAMP>`z'</SAMP> are not supported. <P> <LI> The conversion letter <SAMP>`n'</SAMP> (as in <SAMP>`%n'</SAMP>) is not supported. <P> <LI> The conversion letters <SAMP>`a'</SAMP> and <SAMP>`A'</SAMP> are not supported. </UL> <P> Note that the <SAMP>`ll'</SAMP> type modifier is supported only if the underlying <CODE>C</CODE> implementation used to build GDB supports the <CODE>long long int</CODE> type, and the <SAMP>`L'</SAMP> type modifier is supported only if <CODE>long double</CODE> type is available. </P><P> As in <CODE>C</CODE>, <CODE>printf</CODE> supports simple backslash-escape sequences, such as <CODE>\n</CODE>, <SAMP>`\t'</SAMP>, <SAMP>`\\'</SAMP>, <SAMP>`\"'</SAMP>, <SAMP>`\a'</SAMP>, and <SAMP>`\f'</SAMP>, that consist of backslash followed by a single character. Octal and hexadecimal escape sequences are not supported. </P><P> Additionally, <CODE>printf</CODE> supports conversion specifications for DFP (<EM>Decimal Floating Point</EM>) types using the following length modifiers together with a floating point specifier. letters: </P><P> <UL> <LI> <SAMP>`H'</SAMP> for printing <CODE>Decimal32</CODE> types. <P> <LI> <SAMP>`D'</SAMP> for printing <CODE>Decimal64</CODE> types. <P> <LI> <SAMP>`DD'</SAMP> for printing <CODE>Decimal128</CODE> types. </UL> <P> If the underlying <CODE>C</CODE> implementation used to build GDB has support for the three length modifiers for DFP types, other modifiers such as width and precision will also be available for GDB to use. </P><P> In case there is no such <CODE>C</CODE> support, no additional modifiers will be available and the value will be printed in the standard way. </P><P> Here's an example of printing DFP types using the above conversion letters: <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>printf "D32: %Hf - D64: %Df - D128: %DDf\n",1.2345df,1.2E10dd,1.2E1dl </FONT></pre></td></tr></table></P><P> </DL> <P> <A NAME="Interpreters"></A> <HR SIZE="6"> <TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0> <TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_21.html#SEC229"> << </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_22.html#SEC234"> >> </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb.html#SEC_Top">Top</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_toc.html#SEC_Contents">Contents</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_38.html#SEC764">Index</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_abt.html#SEC_About"> ? </A>]</TD> </TR></TABLE> <BR> <FONT SIZE="-1"> <address> <p>Please send FSF & GNU inquiries & questions to <a href="mailto:gnu@gnu.org">gnu@gnu.org</a>. There are also <a href="http://www.gnu.org/home.html#ContactInfo">other ways to contact</a> the FSF.</p> <p>These pages are maintained by <a href="http://www.gnu.org/software/gdb/">the GDB developers</a>.</p> <p>Copyright Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.</p> <p>Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.</p> </address> This document was generated by <I>GDB Administrator</I> on <I>March, 27 2008</I> using <A HREF="http://www.mathematik.uni-kl.de/~obachman/Texi2html "><I>texi2html</I></A> </BODY> </HTML>