GDB Monitor commands in CLion

旧时模样 提交于 2019-12-04 06:47:21
Eldar Abusalimov

CLion doesn't block any particular command from .gdbinit on purpose. The thing is, these commands are executed on the debugger startup, before attaching to the target. That means that the monitor reset command gets executed without a remote session being run yet, hence it fails.

Just to clarify:

  • here's what happens when you execute GDB manually:

    # commands from .gdbinit
    target remote localhost:2331
    set verbose on
    file "/path_to_output_file/blinky.elf"
    monitor reset
    break main
    
  • here's what happens when you execute GDB from CLion with the same .gdbinit file:

    # 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)
    
  • and here's what's going on when you execute GDB from CLion with the attach command removed:

    # 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
    

The issues you linked are totally the right ones, please feel free to vote (disclaimer: I'm one of CLion developers). I couldn't come up with a reasonable workaround to suggest you for now, I'm afraid.

Update:

There actually is a workaround for your use case that works for both CLion and terminal debug sessions. You can use GDB hooks to achieve that.

In your .gdbinit file replace the commands in question with the following lines:

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

This way, every time the remote target is connected, the GDB will execute the commands specified in the defined hook, regardless the way you start the debugger, either from CLion or from a terminal.

Looking around for the exact same issue, I came across this GitHub project that has a great step-by-step guide on setting up a JLink debugger on CLion. What really helped me out is the script that generates the .gdbinit in the user home directory.

There is no need to add a file /firmware.elf command as this is taken care of by CLion when a debug session is launched. On the other hand, a load command is necessary to flash your target.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!