7

我正在尝试使用远程 GDB 调试嵌入式项目。我的系统:

  • 目标:ARM Cortex M0。
  • SEGGER J-Link GDB Server V6.10 命令行版
  • arm-none-eabi-gdb 7.10.1.20160616-cvs
  • CLion 2016.2.2,内部版本 #CL-162.1967.7
  • Ubuntu 16.04

我的 .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 中,我收到错误消息:

此目标不支持“监视”命令。

我的理论是终端接受“监视器重置”命令(至少它不会抱怨)。另一方面,CLion 会打印一个错误,但之后似乎会继续前进而不进行重置。结果似乎是,当我在 CLion 中启动新的调试会话时,我不会从 main() 的开头开始。

CLion 是否阻止了监控命令?如果是这样,那么为什么以及是否有解决方法?

我觉得我的问题可能与CPP-7322CPP-7256有关。

4

2 回答 2

10

CLion 不会故意阻止任何特定命令.gdbinit。问题是,这些命令在调试器启动时执行,然后附加到目标。这意味着该monitor reset命令在没有运行远程会话的情况下被执行,因此它失败了。

只是为了澄清:

  • 这是手动执行 GDB 时发生的情况:

    # 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)
    
  • 以下是当您从 CLion 执行 GDB 并删除附加命令时发生的情况:

    # 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 挂钩来实现这一点。

在您的.gdbinit文件中,将相关命令替换为以下几行:

define target hookpost-remote
file "/path_to_output_file/blinky.elf"
monitor reset
break main
end

这样,每次连接远程目标时,GDB 都会执行定义的钩子中指定的命令,无论您以何种方式启动调试器,无论是从 CLion 还是从终端。

于 2016-10-03T09:53:15.903 回答
1

在寻找完全相同的问题时,我遇到了这个GitHub 项目,该项目有一个很好的分步指南,用于在 CLion 上设置 JLink 调试器。.gdbinit真正帮助我的是在用户主目录中生成的脚本。

无需添加file /firmware.elf命令,因为在启动调试会话时由 CLion 负责。另一方面,load需要一个命令来刷新您的目标。

于 2018-08-31T18:43:10.743 回答