[Flash/Flex] 使用gdb调试多线程FlasCC应用程序
2013-04-01 14:07
375 查看
多线程C/C++程序使用pthread库可以被FlasCC编译,在Flash Player11.5或以上版本中运行。 我们开始吧! 我们将使用一个非常简单的多线程C程序,文件名sample.c,它来自这个优秀的pthreads多线程教程: #include <pthread.h> #include <stdio.h> #define NUM_THREADS 5 void *PrintHello(void *threadid) { long tid; tid = (long)threadid; printf("Hello World! It's me, thread #%ld!\n", tid); pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc; long t; for(t=0; t<NUM_THREADS; t++){ printf("In main: creating thread %ld\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } /* Last thing that main() should do */ pthread_exit(NULL); } 复制代码 这段代码创建一些线程将字符输出到屏幕上。首先使用gcc将代码编译到SWF。 .../flascc/sdk/usr/bin/gcc -g -O0 -pthread sample.c -emit-swf -o sample.swf 复制代码 现在使用gdb运行这个SWF。 .../flascc/sdk/usr/bin/gdb sample.swf 复制代码 在PrintHello函数前增加一个breakpoint,并运行程序: (gdb) break PrintHello No symbol table is loaded. Use the "file" command. Make breakpoint pending on future shared library load? (y or ) y Breakpoint 1 (PrintHello) pending. (gdb) run Starting program: sample.swf 0xdddddddd in ?? () 复制代码 注意PrintHello函数仍被FlashPlayer执行了,这是因为在默认情况下,gdb只在主线程执行,为了对其他线程进行操作,我们需要打开gdb的多线程支持。为此,我们需要启用gdb的"non-stop"模式。"non-stop"模式是指当程序中一个线程停止时,其他线程不受影响。当插入或在后台执行新的命令行,gdb会提示你输入下一个命令而原来的代码执行不受影响。当后台代码执行结束,gdb会通知你。在此模式下,FlasCC允许你单独控制所有线程。 "non-stop"模式与gdb默认的"all-stop"模式略有不同。在"all-stop"模式中,gdb同时执行所有命令,这意味着只有在所有命令执行结束,才可以输入新命令。在此模式下,gdb会忽略主线程以外的其他线程。注意这是gdb的特定行为,转而言之,"all-stop"模式是指程序中的一个线程停止,其他所有线程也将停止。 我们按CTRL+C退出gdb会话,选择Y。 ^C Program received signal SIGTRAP, Trace/breakpoint trap. 0x00000000 in ?? () (gdb) q A debugging session is active. Inferior 1 [Remote target] will be killed. Quit anyway? (y or n) y 复制代码 重新启动gdb,我们尝试使用"non-stop"模式。 .../flascc/sdk/usr/bin/gdb sample.swf 复制代码 "non-stop"模式调试程序 "non-stop"模式必须在开运行程序前启用,为此你需要调用3个命令: (gdb) set pagination off (gdb) set target-async on (gdb) set non-stop on 复制代码 技巧:如果你讨厌每次输入这些命令,你可以在a.gdbinit文件中自定义一个gdb命令。 define myCustomCommand set pagination off set target-async on set non-stop on end 复制代码 .gdbinit文件会在gdb每次启动时自动加载,它可以放在根目录或当前目录,这样下次启动gdb时,你只需要输入一行代码就可以启用"non-stop"模式。 (gdb) myCustomCommand 复制代码 现在我们已经准备好调试程序,首先设置一个breakpoint在main: (gdb) break main 复制代码 运行SWF: (gdb) run 复制代码 程序需要几秒加载,然后在main这中断了。你可以忽略[Worker 1]#1的输出,直到确认程序在main这中断了。 Breakpoint 1, 0xf0000083 in main (argc=0, argv=0x200ff0) at sample.c:18 18 for(t=0; t<NUM_THREADS; t++){ (gdb) 复制代码 目前为止这与默认模式的gdb很相似,但这也是事情发生变化的地方。也许你想让代码继续执行,当你这样做的时候你会看到错误提示: (gdb) step Cannot execute this command while the selected thread is running. 复制代码 gdb报告说虽然线程被中断,可是该线程仍然在运行。你可能不理解为什么会有2个线程?毕竟在这段代码中,我们没有使用多线程。原因在于起用多线程支持后,FlasCC会创建后台线程执行程序,这允许你对C代码进行多种操作而不影响UI。你可以使用info threads命令查看运行程序的线程: (gdb) info threads Id Target Id Frame 2 Worker 2 0xf0000083 in main (argc=0, argv=0x200ff0) at sample.c:18 * 1 Worker 1 (running) 复制代码 你可以看到线程2,代码在18行处中断了,而线程1,UI线程仍然在运行。为了管理UI事件,UI线程负责管理其他较低级别的线程。由于这个原因,如果你想其他线程能够顺畅运行,最好不要中断UI线程。 *表示当前gdb中选择的程序。可以使用线程命令改变gdb当前选择的线程。我们来选择线程2。 (gdb) thread 2 [Switching to thread 2 (Worker 2)] #0 0xf0000083 in main (argc=0, argv=0x200ff0) at sample.c:18 18 for(t=0; t<NUM_THREADS; t++){ 复制代码 现在再试一下step,输入2次: (gdb) step 19 printf("In main: creating thread %ld\n", t); (gdb) step 20 rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); 复制代码 在这里我们使用pthread_create函数创建一个线程。该线程将调用PrintHello函数。如果你注意观察sample.c的源代码,会发现我们实际是在一个for循环中创建大量的线程。我们中断了PrintHello函数,所以我们可以完全控制每一个线程,创建或调用PrintHello函数。 (gdb) break PrintHello Breakpoint 2 at 0xf000004e: file sample.c, line 8. 复制代码 现在所处的线程是运行步进的,我们来看看会发生什么: (gdb) step 21 if (rc){ (gdb) [New Worker 4] Breakpoint 2, 0xf000004f in PrintHello (threadid=0x0) at sample.c:8 8 tid = (long)threadid; 复制代码 如果你仔细看会觉得有点怪。当你跳过执行这一行代码,gdb提示一切正常,同时另一个线程开始调用PrintHello ,很快它执行到中断处,输出到屏幕上。别担心一切正常。你会发现你仍然可以输入gdb命令,即使你没有看见任何提示符。 如果你想找回提示符,使用CTRL+C即可。CTRL+C,然后使用info threads命令查看线程: Quit (gdb) info threads Id Target Id Frame 3 Worker 4 0xf000004f in PrintHello (threadid=0x0) at sample.c:8 * 2 Worker 2 0xf00000a2 in main (argc=0, argv=0x200ff0) at sample.c:21 1 Worker 1 (running) (gdb) 复制代码 使用step命令 切换到Id3线程,使用2次step命令: (gdb) thread 3 [Switching to thread 3 (Worker 4)] #0 0xf000004f in PrintHello (threadid=0x0) at sample.c:8 8 tid = (long)threadid; (gdb) step 9 printf("Hello World! It's me, thread #%ld!\n", tid); (gdb) step 10 pthread_exit(NULL); (gdb) 复制代码 我们看到PrintHello函数的最后一行pthread_exit()被执行。如果我们多执行一次step命令,意味着这个线程执行结束,我们将看不到任何输出: (gdb) step 复制代码 不用担心这也是正常的。step命令会gdb跳过当前行,直接运行下一行代码。然而在这种情况下,pthread_exit()会导致调用进程终止,没有返回值也不会继续运行下一行(线程已运行结束)。gdb会等待是否有新命令输入。你可以按下CTRL+C中断gdb,再按下CTRL+C获得提示符,然后使用info threads命令查看线程: (gdb) info threads Id Target Id Frame 2 Worker 2 0xf00000a2 in main (argc=0, argv=0x200ff0) at sample.c:21 1 Worker 1 (running) The current thread has terminated. See `help thread'. (gdb) 复制代码 注意Id3线程已终止,没有显示在线程列表里,也没有*当前选择的线程。现在我们回到被挂起的线程2: (gdb) thread 2 [Switching to thread 2 (Worker 2)] #0 0xf00000a2 in main (argc=0, argv=0x200ff0) at sample.c:21 21 if (rc){ 复制代码 如果我们执行4次step命令,将创建1个新线程调用PrintHello函数。 (gdb) step 18 for(t=0; t<NUM_THREADS; t++){ (gdb) step 19 printf("In main: creating thread %ld\n", t); (gdb) step 20 rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); (gdb) step 21 if (rc){ (gdb) [New Worker 5] Breakpoint 2, 0xf000004f in PrintHello (threadid=0x1) at sample.c:8 8 tid = (long)threadid; 复制代码 CTRL+C,然后选择新线程: Quit (gdb) info threads Id Target Id Frame 4 Worker 5 0xf000004f in PrintHello (threadid=0x1) at sample.c:8 * 2 Worker 2 0xf00000a2 in main (argc=0, argv=0x200ff0) at sample.c:21 1 Worker 1 (running) (gdb) thread 4 [Switching to thread 4 (Worker 5)] #0 0xf000004f in PrintHello (threadid=0x1) at sample.c:8 8 tid = (long)threadid; (gdb) 复制代码 使用continue命令 这次我们试试continue命令: (gdb) continue Continuing. 复制代码 当gdb continue线程,会在中断处一直等待,直到你输入新的命令。在这里线程2被挂起,线程4已结束,除非我们恢复线程2,否则程序不会再运行到中断处。为了继续调试程序,我们需要中断gdb并恢复线程2。CTRL+C中断gdb,再按下CTRL+C获得提示符,然后使用info threads命令查看线程: (gdb) info threads Id Target Id Frame 2 Worker 2 0xf00000a2 in main (argc=0, argv=0x200ff0) at sample.c: 21 1 Worker 1 (running) The current thread has terminated. See `help thread'. (gdb) 复制代码 返回主线程,然后执行4次step命令,创建新线程。 (gdb) thread 2 [Switching to thread 2 (Worker 2)] #0 0xf00000a2 in main (argc=0, argv=0x200ff0) at sample.c:21 21 if (rc){ (gdb) step 18 for(t=0; t<NUM_THREADS; t++){ (gdb) step 19 printf("In main: creating thread %ld\n", t); (gdb) step 20 rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); (gdb) step 21 if (rc){ (gdb) [New Worker 6] Breakpoint 2, 0xf000004f in PrintHello (threadid=0x2) at sample.c:8 8 tid = (long)threadid; 复制代码 按下CTRL+C获得提示符,然后切换到Id5线程: Quit (gdb) info threads Id Target Id Frame 5 Worker 6 0xf000004f in PrintHello (threadid=0x2) at sample.c:8 * 2 Worker 2 0xf00000a2 in main (argc=0, argv=0x200ff0) at sample.c: 21 1 Worker 1 (running) (gdb) thread 5 [Switching to thread 5 (Worker 6)] #0 0xf000004f in PrintHello (threadid=0x2) at sample.c:8 8 tid = (long)threadid; (gdb) 复制代码 使用continue&命令 再次执行continue命令,与上次稍有不同,使用的命令是continue&。&符号表示这是一个异步命令,gdb会提示你输入下一个命令。 (gdb) continue& Continuing. (gdb) 复制代码 这个命令允许我们继续长时间运行线程,同时允许我们继续输入新命令及调试其他线程。本教程最后附有一个表格,显示所有&支持的命令。 如果现在执行info threads,我们会看到这个线程运行完毕。 (gdb) info threads Id Target Id Frame 2 Worker 2 0xf00000a2 in main (argc=0, argv=0x200ff0) at sample.c: 21 1 Worker 1 (running) The current thread has terminated. See `help thread'. 复制代码 这种情况下使用continue&很方便,因我们不需要在线程结束后按CTRL+C。 我们回到 主线程,执行continue&: (gdb) thread 2 [Switching to thread 2 (Worker 2)] #0 0xf00000a2 in main (argc=0, argv=0x200ff0) at sample.c:21 21 if (rc){ (gdb) continue& Continuing. (gdb) Breakpoint 2, 0xf000004f in PrintHello (threadid=0x3) at sample.c:8 8 tid = (long)threadid; Breakpoint 2, 0xf000004f in PrintHello (threadid=0x4) at sample.c:8 8 tid = (long)threadid; 复制代码 仔细观察你会发现当其他线程运行到中断处,提示符就会出现。按下CTRL+C获得提示符。 使用thread apply命令 在调试多线程应用时,可能有很多线程,你需要经常在线程之间切换。thread apply是一个很方便的命令。它允许你为一系列线程调用相同的命令。举例,我们来backtrace2个线程到PrintHello中断处。 (gdb) info threads Id Target Id Frame 7 Worker 8 0xf000004f in PrintHello (threadid=0x4) at sample.c:8 6 Worker 7 0xf000004f in PrintHello (threadid=0x3) at sample.c:8 1 Worker 1 (running) The current thread has terminated. See `help thread'. (gdb) thread apply 6 7 backtrace Thread 6 (Worker 7): #0 0xf000004f in PrintHello (threadid=0x3) at sample.c:8 #1 0xf000ce95 in _thread_start () from remote:6.elf #2 0xf000152e in _thread_run () from remote:2.elf #3 0x00000000 in ?? () Thread 7 (Worker 8): #0 0xf000004f in PrintHello (threadid=0x4) at sample.c:8 #1 0xf000ce95 in _thread_start () from remote:6.elf #2 0xf000152e in _thread_run () from remote:2.elf #3 0x00000000 in ?? () (gdb) 复制代码 你也可以通过thread apply all将命令适用于所有线程。 (gdb) thread apply all backtrace Thread 7 (Worker 8): #0 0xf000004f in PrintHello (threadid=0x4) at sample.c:8 #1 0xf000ce95 in _thread_start () from remote:6.elf #2 0xf000152e in _thread_run () from remote:2.elf #3 0x00000000 in ?? () Thread 6 (Worker 7): #0 0xf000004f in PrintHello (threadid=0x3) at sample.c:8 #1 0xf000ce95 in _thread_start () from remote:6.elf #2 0xf000152e in _thread_run () from remote:2.elf #3 0x00000000 in ?? () Thread 1 (Worker 1): Target is executing. (gdb) 复制代码 结论 现在你知道怎样在gdb中调试多线程的FlasCC程序。如果你有任何问题请在下面留言。但最好还是FlasCC论坛提交你的问题。如果你还没有注册,请点击这里。 命令摘要 以下是常用的调试命令 info threads 显示线程列表 thread X 设置选定线程的Id thread apply X Y 对指定线程X执行指定命令Y 例子:thread apply 3 4 step thread apply all Y 对所有线程X执行指定命令Y 例子:thread apply all step 以下命令支持& 异步 step& 跳过本行代码(进行子程序调用) next& 将程序移动到下一行代码(进行子程序调用) continue& 继续执行当前选定线程 更多参考资料 使用gdb发现多线程bug 使用多线程Debug程序 gdb"non-stop"模式 gdb"all-stop"模式 POSIX线程编程 原文链接:http://blogs.adobe.com/flascc/2012/11/09/debugging-multi-threaded-flascc-applications-with-gdb/ |
相关文章推荐
- [Flash/Flex] 使用gdb查找多线程bug
- 使用gdb调试多进程多线程程序
- 使用gdb调试多线程程序总结
- TI-Davinci开发系列之五CCS5.2使用gdbserver远程调试应用程序
- 使用 Java Debug Interface 调试多线程应用程序(不太懂)
- 使用gdb调试多线程程序总结
- 使用GDB调试多线程程序
- 使用gdb调试多进程和多线程程序
- 如何使用GDB调试多线程
- 【Linux】使用gdb调试多进程多线程程序
- 使用gdb调试多进程和多线程
- 使用gdb调试多线程程序总结
- 使用gdb调试多进程与多线程程序
- 使用gdb调试多进程多线程程序
- 【Linux】使用gdb调试多进程&多线程
- 如何使用GDB调试多线程
- Linux下使用gdb调试多进程与多线程程序
- [Flash/Flex] FlasCC配置脚本的使用
- 使用gdb调试多线程程序总结
- 使用gdb调试多进程多线程调试