dap-mode icon indicating copy to clipboard operation
dap-mode copied to clipboard

How to run an executable with command line options?

Open cody-addison opened this issue 6 years ago • 12 comments
trafficstars

I apologize if this has already been discussed somewhere, but I couldn't find any mention of it. I'm trying to debug a C++ application using lldb and need to pass command line options to the application for debugging. How do I do this?

My config is (dap-mode 1) (dap-ui-mode 1) (require 'dap-gdb-lldb)

When I run dap-debug, it asks for the executable to debug and starts a session, but I'm not able to run it with options.

cody-addison avatar May 24 '19 18:05 cody-addison

Take a look at https://github.com/WebFreak001/code-debug/blob/master/package.json#L72 . It lists all arguments that you can pass to the debug adapter. Then in dap-mode you could do M-x dap-debug-edit-template and specify all params listed in code-debug execute the register call and the template will be registered and accessible via dap-debug. We need better support/documentation at dap-mode side for each adapter, but it is not possible to keep up with each one so we rely on the debug adapter users to help.

yyoncho avatar May 24 '19 18:05 yyoncho

Thanks for the tip. I was able to edit the configuration and have the executable run. However now it just runs to completion without giving me a chance to set breakpoints. Is there a way to load the executable without immediately running it?

cody-addison avatar May 24 '19 20:05 cody-addison

You should be able to place the breakpoints before starting the program or you are asking something else? AFAIK native debug have some limitations, you may read about them in its readme.

yyoncho avatar May 24 '19 20:05 yyoncho

I am trying to place breakpoints before starting the program. I run dap-debug and select my template and then the program executes and runs to completion. When in the process would I set the breakpoints?

Do you recommend I follow the instructions for LLDB here: https://github.com/emacs-lsp/dap-mode#lldb instead of the native debug solution?

cody-addison avatar May 24 '19 21:05 cody-addison

I am trying to place breakpoints before starting the program. I run dap-debug and select my template and then the program executes and runs to completion. When in the process would I set the breakpoints?

You could do dap-breakpoint-toggle/add/remove or click in the fringe before starting the application. AFAIK Native debug has this limitation:

"Adding breakpoints while the program runs will not interrupt it immediately. For that you need to pause & resume the program once first. However adding breakpoints while its paused works as expected."

And also it does work with multiple threads.

Do you recommend I follow the instructions for LLDB here: https://github.com/emacs-lsp/dap-mode#lldb instead of the native debug solution?

IMO in the long term, it will be the prefered solution since that debug adapter is implemented by lldb team. There is one more debug adapter for lldb which I think will work much better ATM but we do not have integration with it: https://github.com/vadimcn/vscode-lldb . But like I mentioned I am not using this debug adapter and I cannot tell for sure.

yyoncho avatar May 25 '19 04:05 yyoncho

Then in dap-mode you could do M-x dap-debug-edit-template and specify all params listed in code-debug execute the register call and the template will be registered and accessible via dap-debug.

I can't understand these instructions. Does this mean that in the template I'm editing, if I wanted to change the gdb executable used, I can add :gdbpath "/usr/bin/gdb-multiarch"?

To be extremely specific: I need to launch /usr/bin/gdb-multiarch rather than the default gdb as specified in the the vscode extension.

I've tried

(dap-debug (list :type "gdb"
                 :request "launch"
                 :name "gdb-multiarch"
                 :dap-server-path dap-gdb-lldb-debug-program
                 :gdbpath "/usr/bin/gdb-multiarch"
                 :target "/home/bjc/src/MyStuff/bleusb/target/thumbv6m-none-eabi/release/usb"
                 :cwd "/home/bjc/src/MyStuff/bleusb/usb"))

and it seems to still launch the default gdb.

bjc avatar Sep 26 '19 20:09 bjc

After trying again, it turns out that setting :gdbpath does, indeed, work. I'm not sure what trouble I was having before.

bjc avatar Sep 26 '19 20:09 bjc

Put a breakpoint in main and debug as usual. To find main, you can use helm-lsp-workspace-symbol or lsp-ivy-worskspace-symbol.

nbfalcon avatar Oct 24 '20 08:10 nbfalcon

Take a look at https://github.com/WebFreak001/code-debug/blob/master/package.json#L72 . It lists all arguments that you can pass to the debug adapter. Then in dap-mode you could do M-x dap-debug-edit-template and specify all params listed in code-debug execute the register call and the template will be registered and accessible via dap-debug. We need better support/documentation at dap-mode side for each adapter, but it is not possible to keep up with each one so we rely on the debug adapter users to help.

@yyoncho Is there any better way to do so? Imagine a use case when you are debugging binary with all kind of different command line parameters, for example some unit test binary when the test name is passed as command line argument. Native emacs gud interface allows you to provide all the arguments dynamically (also keeping the history), without updating a configuration file. Is there any chance this will be implemented for dap? like some dap-debug-custom command, when you pass the arguments interactively?

EgorDuplensky avatar Apr 01 '22 16:04 EgorDuplensky

@EgorDuplensky you can build whatever flow you want creating a wrapper over dap-debug.

Here is sample one:

(defun my/debug ()
  (interactive)
  (dap-debug
   (list :type "cppdbg"
         :request "launch"
         :name "cpptools::Run Configuration"
         :MIMode "gdb"
         :program (read-file-name "Enter the binary to debug...")
         :cwd "${workspaceFolder}"
         :environment [])))

(note how :program is populated interactively).

yyoncho avatar Apr 02 '22 10:04 yyoncho

@EgorDuplensky in general what I do is to create the configurations I need and then I use dap-debug-recent/dap-debug-last to pick the right one.

yyoncho avatar Apr 02 '22 10:04 yyoncho

@EgorDuplensky you can build whatever flow you want creating a wrapper over dap-debug.

Here is sample one:

(defun my/debug ()
  (interactive)
  (dap-debug
   (list :type "cppdbg"
         :request "launch"
         :name "cpptools::Run Configuration"
         :MIMode "gdb"
         :program (read-file-name "Enter the binary to debug...")
         :cwd "${workspaceFolder}"
         :environment [])))

(note how :program is populated interactively).

Thank you for the code example. This is actually almost exactly what I have in my emacs config (except your version is much cleaner than mine). I was trying to say that this is pretty common use case and this is a bit strange dap mode doesn't have it as part of the general feautes / workflow. What do you think? As for me I am not confident enough in my elisp skills to create a PR for this feature.

EgorDuplensky avatar Apr 02 '22 11:04 EgorDuplensky