Search code examples
debuggingclioncortex-mgdbserversegger-jlink

GDB Monitor commands in CLion


I'm trying to debug an embedded project using remote GDB. My system:

  • Target: ARM Cortex M0.
  • SEGGER J-Link GDB Server V6.10 Command Line Version
  • arm-none-eabi-gdb 7.10.1.20160616-cvs
  • CLion 2016.2.2, Build #CL-162.1967.7
  • Ubuntu 16.04

I have the following in my .gdbinit file:

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

The thing that has troubled me for days now, is that this works fine if I debug with gdb directly from a terminal, but not when I use the debugger in CLion. In CLion I get the error:

"monitor" command not supported by this target.

My theory is that the terminal accepts the "monitor reset" command (at least it doesn't complain). CLion on the other hand, prints an error, but appears to move on afterwards without doing the reset. The consequence seems to be that when I start a new debugging session in CLion I don't start at the beginning of main().

Is CLion blocking the monitor commands? If so, then why and is there a workaround?

I have the feeling that my questions might be related to CPP-7322 and CPP-7256.


Solution

  • 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.