gdb.sgml
来自「cygwin, 著名的在win32下模拟unix操作系统的东东」· SGML 代码 · 共 89 行
SGML
89 行
<sect1 id="gdb"><title>Debugging Cygwin Programs</title><para>When your program doesn't work right, it usually has a "bug" init, meaning there's something wrong with the program itself that iscausing unexpected results or crashes. Diagnosing these bugs andfixing them is made easy by special tools called<emphasis>debuggers</emphasis>. In the case of Cygwin, the debuggeris GDB, which stands for "GNU DeBugger". This tool lets you run yourprogram in a controlled environment where you can investigate thestate of your program while it is running or after it crashes.Crashing programs sometimes create "core" files. In Cygwin these are regular text files that cannot be used directly by GDB.</para><para>Before you can debug your program, you need to prepare yourprogram for debugging. What you need to do is add<literal>-g</literal> to all the other flags you use when compilingyour sources to objects.</para><example><title>Compiling with -g</title><screen><prompt>$</prompt> gcc -g -O2 -c myapp.c<prompt>$</prompt> gcc -g myapp.c -o myapp</screen></example><para>What this does is add extra information to the objects (they getmuch bigger too) that tell the debugger about line numbers, variablenames, and other useful things. These extra symbols and debugginginformation give your program enough information about the originalsources so that the debugger can make debugging much easier foryou.</para><para>In Windows versions of GNUPro, GDB comes with a full-featuredgraphical interface. In Cygwin Net distributions, GDB is onlyavailable as a command-line tool. To invoke GDB, simply type<command>gdb myapp.exe</command> at the command prompt. It willdisplay some text telling you about itself, then<literal>(gdb)</literal> will appear to prompt you to enter commands.Whenever you see this prompt, it means that gdb is waiting for you totype in a command, like <command>run</command> or<command>help</command>. Oh <literal>:-)</literal> type<command>help</command> to get help on the commands you can type in,or read the <citation>GDB User's Manual</citation> for a completedescription of GDB and how to use it.</para><para>If your program crashes and you're trying to figure out why itcrashed, the best thing to do is type <command>run</command> and letyour program run. After it crashes, you can type<command>where</command> to find out where it crashed, or<command>info locals</command> to see the values of all the localvariables. There's also a <command>print</command> that lets you lookat individual variables or what pointers point to.</para><para>If your program is doing something unexpected, you can use the<command>break</command> command to tell gdb to stop your program when itgets to a specific function or line number:</para><example><title>"break" in gdb</title><screen><prompt>(gdb)</prompt> break my_function<prompt>(gdb)</prompt> break 47</screen></example><para>Now, when you type <command>run</command> your program will stopat that "breakpoint" and you can use the other gdb commands to look atthe state of your program at that point, modify variables, and<command>step</command> through your program's statements one at atime.</para><para>Note that you may specify additional arguments to the<command>run</command> command to provide command-line arguments toyour program. These two cases are the same as far as your program isconcerned:</para><example><title>Debugging with command line arguments</title><screen><prompt>$</prompt> myprog -t foo --queue 47<prompt>$</prompt> gdb myprog<prompt>(gdb)</prompt> run -t foo --queue 47</screen></example></sect1>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?