我正在尝试使用远程GDB调试嵌入式项目。我的系统:
我的.gdbinit文件中有以下内容:
target remote localhost:2331 #(I remove this line when debugging with CLion)
set verbose on
file "/path_to_output_file/blinky.elf"
monitor reset
break main
几天困扰我的事情是,如果我直接从终端调试gdb,这可以正常工作,但是当我在CLion中使用调试器时却没有。在CLion我收到错误:
此目标不支持“monitor”命令。
我的理论是终端接受“监视器重置”命令(至少它没有抱怨)。另一方面,CLion会打印错误,但之后会显示继续执行而不进行重置。结果似乎是当我在CLion中启动一个新的调试会话时,我不会在main()的开头开始。
CLion是否阻止了监视器命令?如果是这样,那么为什么并且有解决方法?
CLion没有故意阻止来自.gdbinit
的任何特定命令。问题是,这些命令在附加到目标之前在调试器启动时执行。这意味着monitor reset
命令在没有远程会话运行的情况下执行,因此失败。
只是为了澄清:
# commands from .gdbinit
target remote localhost:2331
set verbose on
file "/path_to_output_file/blinky.elf"
monitor reset
break main
.gdbinit
文件从CLion执行GDB时发生的情况:
# commands from .gdbinit
target remote localhost:2331
set verbose on
file "/path_to_output_file/blinky.elf"
monitor reset
break main
# commands executed by CLion to attach
target remote localhost:2331 # <- ERROR (A program is being debugged already)
# commands from .gdbinit
set verbose on
file "/path_to_output_file/blinky.elf"
monitor reset # <- ERROR not attached to remote gdbserver => unknown command
# ... not executed due to the error above
break main
# commands executed by CLion to attach
target remote localhost:2331
您链接的问题完全是正确的,请随时投票(免责声明:我是CLION开发人员之一)。 我害怕,我现在无法想出一个合理的解决方法来建议你。
实际上,您的用例的解决方法适用于CLion和终端调试会话。您可以使用GDB hooks来实现这一目标。
在您的.gdbinit
文件中,使用以下行替换相关命令:
define target hookpost-remote
file "/path_to_output_file/blinky.elf"
monitor reset
break main
end
这样,每次连接远程目标时,GDB都将执行定义的钩子中指定的命令,无论您是从CLion还是从终端启动调试器。
为了找到完全相同的问题,我遇到了这个GitHub project,它有关于在CLion上设置JLink调试器的详细分步指南。真正帮助我的是在用户主目录中生成.gdbinit
的脚本。
无需添加file /firmware.elf
命令,因为在启动调试会话时,CLion会对此进行处理。另一方面,需要使用load
命令来闪现目标。