语法高亮,滚动条美化,设置页面调整

This commit is contained in:
fofolee
2019-04-19 02:41:09 +08:00
parent 1e8f76c000
commit 359d29ee0b
1590 changed files with 12328 additions and 11441 deletions

View File

@@ -139,9 +139,9 @@ GDB帮助命令提供对GDB名种命令的解释说明。 如果指定了“
</tbody>
</table>
<h3 id="语法">语法</h3>
<pre><code>gdb(选项)(参数)</code></pre>
<pre><code class="language-bash">gdb(选项)(参数)</code></pre>
<h3 id="选项">选项</h3>
<pre><code>-cd设置工作目录
<pre><code class="language-bash">-cd设置工作目录
-q安静模式不打印介绍信息和版本信息
-d添加文件查找路径
-x从指定文件中执行GDB指令
@@ -150,7 +150,7 @@ GDB帮助命令提供对GDB名种命令的解释说明。 如果指定了“
<p>文件:二进制可执行程序。</p>
<h3 id="实例">实例</h3>
<p>以下是linux下dgb调试的一个实例先给出一个示例用的小程序C语言代码</p>
<pre><code>#include &lt;stdio.h&gt;
<pre><code class="language-bash">#include &lt;stdio.h&gt;
int nGlobalVar = 0;
int tempFunction(int a, int b)
@@ -177,10 +177,10 @@ int main()
return 0;
}</code></pre>
<p>请将此代码复制出来并保存到文件 gdb-sample.c 中然后切换到此文件所在目录用GCC编译之</p>
<pre><code>gcc gdb-sample.c -o gdb-sample -g</code></pre>
<pre><code class="language-bash">gcc gdb-sample.c -o gdb-sample -g</code></pre>
<p>在上面的命令行中,使用 -o 参数指定了编译生成的可执行文件名为 gdb-sample使用参数 -g 表示将源代码信息编译到可执行文件中。如果不使用参数 -g会给后面的GDB调试造成不便。当然如果我们没有程序的源代码自然也无从使用 -g 参数,调试/跟踪时也只能是汇编代码级别的调试/跟踪。</p>
<p>下面“gdb”命令启动GDB将首先显示GDB说明不管它</p>
<pre><code>GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
<pre><code class="language-bash">GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
@@ -190,22 +190,22 @@ This GDB was configured as &quot;i386-redhat-linux-gnu&quot;.
(gdb)</code></pre>
<p>上面最后一行“(gdb)”为GDB内部命令引导符等待用户输入GDB命令。</p>
<p>下面使用“file”命令载入被调试程序 gdb-sample这里的 gdb-sample 即前面 GCC 编译输出的可执行文件):</p>
<pre><code>(gdb) file gdb-sample
<pre><code class="language-bash">(gdb) file gdb-sample
Reading symbols from gdb-sample...done.</code></pre>
<p>上面最后一行提示已经加载成功。</p>
<p>下面使用“r”命令执行Run被调试文件因为尚未设置任何断点将直接执行到程序结束</p>
<pre><code>(gdb) r
<pre><code class="language-bash">(gdb) r
Starting program: /home/liigo/temp/test_jmp/test_jmp/gdb-sample
n = 1, nGlobalVar = 88
tempFunction is called, a = 1, b = 2
n = 3
Program exited normally.</code></pre>
<p>下面使用“b”命令在 main 函数开头设置一个断点Breakpoint</p>
<pre><code>(gdb) b main
<pre><code class="language-bash">(gdb) b main
Breakpoint 1 at 0x804835c: file gdb-sample.c, line 19.</code></pre>
<p>上面最后一行提示已经成功设置断点,并给出了该断点信息:在源文件 gdb-sample.c 第19行处设置断点这是本程序的第一个断点序号为1断点处的代码地址为 0x804835c此值可能仅在本次调试过程中有效。回过头去看源代码第19行中的代码为“n = 1”恰好是 main 函数中的第一个可执行语句前面的“int n;”为变量定义语句,并非可执行语句)。</p>
<p>再次使用“r”命令执行Run被调试程序</p>
<pre><code>(gdb) r
<pre><code class="language-bash">(gdb) r
Starting program: /home/liigo/temp/gdb-sample
Breakpoint 1, main () at gdb-sample.c:19
@@ -213,20 +213,20 @@ Breakpoint 1, main () at gdb-sample.c:19
<p>程序中断在gdb-sample.c第19行处即main函数是第一个可执行语句处。</p>
<p>上面最后一行信息为下一条将要执行的源代码为“n = 1;”它是源代码文件gdb-sample.c中的第19行。</p>
<p>下面使用“s”命令Step执行下一行代码即第19行“n = 1;”):</p>
<pre><code>(gdb) s
<pre><code class="language-bash">(gdb) s
20 n++;</code></pre>
<p>上面的信息表示已经执行完“n = 1;”并显示下一条要执行的代码为第20行的“n++;”。</p>
<p>既然已经执行了“n = 1;”,即给变量 n 赋值为 1那我们用“p”命令Print看一下变量 n 的值是不是 1 </p>
<pre><code>(gdb) p n
<pre><code class="language-bash">(gdb) p n
$1 = 1</code></pre>
<p>果然是 1。$1大致是表示这是第一次使用“p”命令——再次执行“p n”将显示“$2 = 1”——此信息应该没有什么用处。</p>
<p>下面我们分别在第26行、tempFunction 函数开头各设置一个断点分别使用命令“b 26”“b tempFunction”</p>
<pre><code>(gdb) b 26
<pre><code class="language-bash">(gdb) b 26
Breakpoint 2 at 0x804837b: file gdb-sample.c, line 26.
(gdb) b tempFunction
Breakpoint 3 at 0x804832e: file gdb-sample.c, line 12.</code></pre>
<p>使用“c”命令继续Continue执行被调试程序程序将中断在第二 个断点26行此时全局变量 nGlobalVar 的值应该是 88再一次执行“c”命令程序将中断于第三个断点12行tempFunction 函数开头处此时tempFunction 函数的两个参数 a、b 的值应分别是 1 和 2</p>
<pre><code>(gdb) c
<pre><code class="language-bash">(gdb) c
Continuing.
Breakpoint 2, main () at gdb-sample.c:26
@@ -245,17 +245,17 @@ $3 = 1
$4 = 2</code></pre>
<p>上面反馈的信息一切都在我们预料之中~~</p>
<p>再一次执行“c”命令Continue因为后面再也没有其它断点程序将一直执行到结束</p>
<pre><code>(gdb) c
<pre><code class="language-bash">(gdb) c
Continuing.
tempFunction is called, a = 1, b = 2
n = 3
Program exited normally.</code></pre>
<p>有时候需要看到编译器生成的汇编代码,以进行汇编级的调试或跟踪,又该如何操作呢?</p>
<p>这就要用到display命令“display /i $pc”了此命令前面已有详细解释</p>
<pre><code>(gdb) display /i $pc
<pre><code class="language-bash">(gdb) display /i $pc
(gdb)</code></pre>
<p>此后程序再中断时,就可以显示出汇编代码了:</p>
<pre><code>(gdb) r
<pre><code class="language-bash">(gdb) r
Starting program: /home/liigo/temp/test_jmp/test_jmp/gdb-sample
Breakpoint 1, main () at gdb-sample.c:19
@@ -263,7 +263,7 @@ Breakpoint 1, main () at gdb-sample.c:19
1: x/i $pc 0x804835c &lt;main+16&gt;: movl $0x1,0xfffffffc(%ebp)</code></pre>
<p>看到了汇编代码“n = 1;”对应的汇编代码是“movl $0x1,0xfffffffc(%ebp)”。</p>
<p>并且以后程序每次中断都将显示下一条汇编指定“si”命令用于执行一条汇编代码——区别于“s”执行一行C代码</p>
<pre><code>(gdb) si
<pre><code class="language-bash">(gdb) si
20 n++;
1: x/i $pc 0x8048363 &lt;main+23&gt;: lea 0xfffffffc(%ebp),%eax
(gdb) si
@@ -280,12 +280,12 @@ Breakpoint 1, main () at gdb-sample.c:19
1: x/i $pc 0x804836d &lt;main+33&gt;: addl $0x64,0x80494fc</code></pre>
<p>接下来我们试一下命令“b *<函数名称>”。</p>
<p>为了更简明有必要先删除目前所有断点使用“d”命令——Delete breakpoint</p>
<pre><code>(gdb) d
<pre><code class="language-bash">(gdb) d
Delete all breakpoints? (y or n) y
(gdb)</code></pre>
<p>当被询问是否删除所有断点时输入“y”并按回车键即可。</p>
<p>下面使用命令“b *main”在 main 函数的 prolog 代码处设置断点prolog、epilog分别表示编译器在每个函数的开头和结尾自行插入的代码</p>
<pre><code>(gdb) b *main
<pre><code class="language-bash">(gdb) b *main
Breakpoint 4 at 0x804834c: file gdb-sample.c, line 17.
(gdb) r
The program being debugged has been started already.
@@ -315,7 +315,7 @@ Breakpoint 4, main () at gdb-sample.c:17
19 n = 1;
1: x/i $pc 0x804835c &lt;main+16&gt;: movl $0x1,0xfffffffc(%ebp)</code></pre>
<p>此时可以使用“i r”命令显示寄存器中的当前值———“i r”即“Infomation Register”</p>
<pre><code>(gdb) i r
<pre><code class="language-bash">(gdb) i r
eax 0xbffff6a4 -1073744220
ecx 0x42015554 1107383636
edx 0x40016bc8 1073834952
@@ -333,17 +333,17 @@ es 0x2b 43
fs 0x0 0
gs 0x33 51</code></pre>
<p>当然也可以显示任意一个指定的寄存器值:</p>
<pre><code>(gdb) i r eax
<pre><code class="language-bash">(gdb) i r eax
eax 0xbffff6a4 -1073744220</code></pre>
<p>最后一个要介绍的命令是“q”退出QuitGDB调试环境</p>
<pre><code>(gdb) q
<pre><code class="language-bash">(gdb) q
The program is running. exit anyway? (y or n)</code></pre>
<h2 id="补充内容">补充内容</h2>
<blockquote>
<p>gdb 教程:<a href="http://www.imooc.com/learn/394">慕课网-Linux C语言指针与内存-第三章</a></p>
</blockquote>
<p>如果删除源代码, 就无法显示行号等辅助信息了</p>
<pre><code>gcc -g gdb.c -o gdb.out # -g 支持gdb调试; -o 输出, 默认为 a.out
<pre><code class="language-bash">gcc -g gdb.c -o gdb.out # -g 支持gdb调试; -o 输出, 默认为 a.out
gdb gdb.out # 进入 gdb 调试环境
enter # 继续执行上条命令