gdb使用
gdb是非常强大的调试工具,在文本模式下使用。使用方法可以参考陈皓的两篇文章
用GDB调试程序(一)
用GDB调试程序(二)
gdb常用命令在下表列出:
命令 | 描述 |
---|---|
backtrace(或bt) | 查看各级函数调用及参数 |
finish | 连续运行到当前函数返回为止,然后停下来等待命令 |
frame(或f) | 帧编号 选择栈帧 |
info(或i) | locals 查看当前栈帧局部变量的值 |
list(或l) | 列出源代码,接着上次的位置往下列,每次列10行 |
list 行号 | 列出从第几行开始的源代码 |
list 函数名 | 列出某个函数的源代码 |
next(或n) | 执行下一行语句 |
print(或p) | 打印表达式的值,通过表达式可以修改变量的值或者调用函数 |
quit(或q) | 退出gdb调试环境 |
set var | 修改变量的值 |
start | 开始执行程序,停在main函数第一行语句前面等待命令 |
step(或s) | 执行下一行语句,如果有函数调用则进入到函数中 |
gdb与多线程
在多线程编程时,当我们需要调试时,有时需要控制某些线程停在断点,有些线程继续执行。有时需要控制线程的运行顺序。有时需要中断某个线程,切换到其他线程。这些都可以通过gdb实现。
先来看一下gdb调试多线程常用命令:
-
info threads:显示可以调试的所有线程。gdb会为每个线程分配一个ID(和tid不同),编号一般从1开始。后面的ID是指这个ID。
-
thread ID:切换当前调试的线程为指定ID的线程。
-
break FileName.cpp:LinuNum thread all:所有线程都在文件FileName.cpp的第LineNum行有断点。
-
thread apply ID1 ID2 IDN command:多个线程执行gdb命令command。
-
thread apply all command:所有线程都执行command命令。
-
set scheduler-locking off|on|step:在调式某一个线程时,其他线程是否执行。off,不锁定任何线程,默认值。on,锁定其他线程,只有当前线程执行。step,在step(单步)时,只有被调试线程运行。
-
set non-stop on/off:当调式一个线程时,其他线程是否运行。
-
set pagination on/off:在使用backtrace时,在分页时是否停止。
-
set target-async on/ff:同步和异步。同步,gdb在输出提示符之前等待程序报告一些线程已经终止的信息。而异步的则是直接返回。
来看一个例子:
gdbTest.cpp。程序很简单,只是让两个线程执行函数ThreadFun,在函数中打印传入的参数。
<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <iostream></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <pthread.h></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>* ThreadFun(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>* arg) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> *value=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static_cast</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>*> (arg); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"This is thread"</span><<*value<<<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::endl; pthread_exit(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main() { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> ret=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; pthread_t thread_id1,thread_id2; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>* v1=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>* v2=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>); ret = pthread_create(&thread_id1, NULL, ThreadFun, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static_cast</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>*>(v1)); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (ret) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Create pthread error!"</span><<<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::endl; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; } ret = pthread_create(&thread_id2, NULL, ThreadFun, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static_cast</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>*>(v2)); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (ret) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Create pthread error!"</span><<<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::endl; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; } pthread_join(thread_id1, NULL); pthread_join(thread_id2, NULL); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li></ul>
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ gdb gdbThreadTest<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//调试gdbThreadTest</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
加断点
<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">(gdb) break <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> Breakpoint <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">at</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x400a19</span>: <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> kangThread.cpp, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">line</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7.</span> (gdb) break <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span> Breakpoint <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">at</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x400b35</span>: <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">file</span> kangThread.cpp, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">line</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35.</span> (gdb) info break Num Type Disp Enb Address What <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">breakpoint</span> keep y <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0000000000400a19</span> <span class="hljs-operator" style="box-sizing: border-box;">in</span> ThreadFun(void*) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">at</span> kangThread.cpp:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">breakpoint</span> keep y <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0000000000400b35</span> <span class="hljs-operator" style="box-sizing: border-box;">in</span> main() <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">at</span> kangThread.cpp:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>
开始运行
<code class="hljs sql has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">(gdb) r Starting program: /home/kang/src/mulThread/kangThread [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Traceback (most recent <span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">call</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">last</span>): File <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py"</span>, line <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">63</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">module</span>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">from</span> libstdcxx.v6.printers import register_libstdcxx_printers ImportError: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">No</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">module</span> named <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'libstdcxx'</span> [New Thread <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x7ffff6fd5700</span> (LWP <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2773</span>)] [Switching <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> Thread <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x7ffff6fd5700</span> (LWP <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2773</span>)] Breakpoint <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, ThreadFun (arg=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x602010</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">at</span> kangThread.cpp:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> warning: Source file <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> more recent than executable. <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> *<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">value</span>=static_cast<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>*> (arg);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>
查看线程信息
<code class="hljs mathematica has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">(gdb) info thread [New <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Thread</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x7ffff67d4700</span> (LWP <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2774</span>)] Id Target Id <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Frame</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Thread</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x7ffff67d4700</span> (LWP <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2774</span>) <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"kangThread"</span> clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">81</span> * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Thread</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x7ffff6fd5700</span> (LWP <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2773</span>) <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"kangThread"</span> ThreadFun (arg=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x602010</span>) at kangThread.cpp:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Thread</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x7ffff7fda780</span> (LWP <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2769</span>) <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"kangThread"</span> clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">81</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
可以看到ID为2的线程执行到了断点Breakpoint 1。可以看一下value的值
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">(gdb) n <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">std:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:cout<<<span class="hljs-string" style="box-sizing: border-box;">"This is thread"</span><<*value<<std</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:endl</span>; (gdb) p *value <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$2</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
切换到线程3,看一下线程3执行到了哪里
<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">(gdb) c Continuing. <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> [Switching to Thread <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x7ffff67d4700</span> (LWP <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2774</span>)] Breakpoint <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, ThreadFun (arg=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x602030</span>) at kangThread.cpp:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> *value=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static_cast</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>*> (arg); (gdb) info thread Id Target Id Frame * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> Thread <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x7ffff67d4700</span> (LWP <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2774</span>) <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"kangThread"</span> ThreadFun (arg=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x602030</span>) at kangThread.cpp:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> Thread <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x7ffff6fd5700</span> (LWP <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2773</span>) <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"kangThread"</span> __GI__dl_debug_state () at dl-debug.c:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">74</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> Thread <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x7ffff7fda780</span> (LWP <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2769</span>) <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"kangThread"</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00007ffff7bc566b</span> in pthread_join (threadid=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">140737337186048</span>, thread_return=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0</span>) at pthread_join.c:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">92</span> (gdb) thread <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> [Switching to thread <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> (Thread <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x7ffff67d4700</span> (LWP <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2774</span>))] <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#0 ThreadFun (arg=0x602030) at kangThread.cpp:7</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> *value=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static_cast</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>*> (arg); (gdb) n <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"This is thread"</span><<*value<<<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::endl; (gdb) p *value $<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li></ul>
可以看出线程3的value为2。
还有其他许多命令和方法,要在实践中慢慢熟悉。
-
顶
- 0