0

我正在使用 SDL2 用 C++ 编写 CHIP-8 解释器。源代码位于https://github.com/robbie0630/Chip8Emu。存在此 ROM出现分段错误的问题。我尝试用 GDB 调试问题,但是当我输入 时bt,它显示了一个不完整的堆栈跟踪,只显示了前两个函数,使我无法有效地诊断问题。如何获得完整且有用的堆栈跟踪?

编辑:当我运行时bt,GDB 显示:

#0  0x0000000101411a14 in ?? ()
#1  0x0000000000406956 in Chip8_CPU::doCycle (this=0x7fffffffc7b0) at /my/home/code/Chip8Emu/src/cpu.cpp:223
#2  0x0000000000402080 in main (argc=2, argv=0x7fffffffe108) at /my/home/code/Chip8Emu/src/main.cpp:152

这是没用的,因为??没有说明什么,第 223 行cpu.cpp是函数调用。

编辑 2:我在程序上运行了 valgrind,这是输出:

==11791== Conditional jump or move depends on uninitialised value(s)
==11791==    at 0x406BA0: Chip8_CPU::doCycle() (cpu.cpp:215)
==11791==    by 0x4020EF: main (main.cpp:152)
==11791== 
==11791== Jump to the invalid address stated on the next line
==11791==    at 0x101411A74: ???
==11791==    by 0x4020EF: main (main.cpp:152)
==11791==  Address 0x101411a74 is not stack'd, malloc'd or (recently) free'd
==11791== 
==11791== 
==11791== Process terminating with default action of signal 11 (SIGSEGV)
==11791==  Access not within mapped region at address 0x101411A74
==11791==    at 0x101411A74: ???
==11791==    by 0x4020EF: main (main.cpp:152)
==11791==  If you believe this happened as a result of a stack
==11791==  overflow in your program's main thread (unlikely but
==11791==  possible), you can try to increase the size of the
==11791==  main thread stack using the --main-stacksize= flag.
==11791==  The main thread stack size used in this run was 8388608.
==11791== 
==11791== HEAP SUMMARY:
==11791==     in use at exit: 7,827,602 bytes in 41,498 blocks
==11791==   total heap usage: 169,848 allocs, 128,350 frees, 94,139,303 bytes allocated
==11791== 
==11791== LEAK SUMMARY:
==11791==    definitely lost: 0 bytes in 0 blocks
==11791==    indirectly lost: 0 bytes in 0 blocks
==11791==      possibly lost: 4,056,685 bytes in 36,878 blocks
==11791==    still reachable: 3,770,917 bytes in 4,620 blocks
==11791==         suppressed: 0 bytes in 0 blocks
==11791== Rerun with --leak-check=full to see details of leaked memory
==11791== 
==11791== For counts of detected and suppressed errors, rerun with: -v
==11791== Use --track-origins=yes to see where uninitialised values come from
==11791== ERROR SUMMARY: 12 errors from 3 contexts (suppressed: 0 from 0)
Killed

编辑 3:我再次运行 GDB,这次是在看GfxDraw,我注意到发生了这种情况:

Old value = (void (*)(array2d)) 0x1411bc4
New value = (void (*)(array2d)) 0x101411bc4
Chip8_CPU::doCycle (this=0x7fffffffc7a0) at /home/robbie/code/Chip8Emu/src/cpu.cpp:213
(gdb) cont
Continuing.

Thread 1 "Chip8Emu" received signal SIGSEGV, Segmentation fault.
0x0000000101411bc4 in ?? ()

所以不知何故GfxDraw被修改为无效的函数指针。但是,我无法弄清楚它在哪里被修改。

4

1 回答 1

0

几个月后,我终于发现了问题所在。一些讨厌的 CHIP-8 程序对超出数组范围的图形内存进行非法内存访问,并破坏 CPU 的属性(例如GfxDraw)。我通过访问图形内存at并忽略std::out_of_range错误来解决此问题。它现在似乎有效,所以我宣布它是解决方案。

于 2017-05-22T12:39:10.803 回答