1

I use emacs/gdb for debugging. Generally, I

  1. emacs
  2. M-x then type gdb
  3. type exe name.

Every debug, I've gone through these 3 steps.

I want to know: is it possible to run gdb from the command line, e.g.

emacs -xxx "gdb my_exe" ....
  • just in case, if you want to use gdb from command line (without emacs) you can try gdb -tui my_exe – Picaud Vincent Dec 17 '18 at 13:34
  • Thanks for all you replies, guys. I am new to Emacs. But after more looking, I decide to give up Emacs. The major reason is that Emacs does not support lldb (officially). I think nowadays, it's still hard to say which one is better between GCC/GDB and Clang/LLVM/LLDB. But I do think that Clang/LLVM looks more promising. I really don't understand what the guy (, who made the decision that Emacs cannot support LLDB) is thinking (You know who I am talking about ^_^). To learn Emacs, I need spend lots of time. But I really don't want to spend time on the software which does not look promising. I – kk9527 Dec 18 '18 at 6:23
  • Here is the first reddit thread I found when Googling emacs lldb in case anyone is interested: reddit.com/r/emacs/comments/6qbwjl/… I would suggest that someone post a question in this forum specifically on point and then anyone with enough reputation can throw a bounty to encourage someone to write up a draft repository (although not officially supported, yet that is ...). – lawlist Dec 18 '18 at 8:09
1

man emacs gives the following - perhaps one of these invocations would help:

   The following options are Lisp-oriented (these options are processed in the order encountered):

          -f function, --funcall=function
                  Execute the lisp function function.

          -l file, --load=file
                  Load the lisp code in the file file.

          --eval=expr, --execute=expr
                  Evaluate the Lisp expression expr.

Specifically, this seems to work for me:

emacs --eval '(gdb "gdb -i=mi dcalc")'
1

You can pass options to Emacs to make it run code when it starts: -l /path/to/file.el, -f function-called-without-arguments, --eval "(some-lisp-code 'taking-care-of \"quoting for the shell\")".

The function gdb is autoloaded, so you don't need to load a package explicitly. It takes an argument. You'll have to write some Lisp code to grab it from the command line.

I have a Gdb startup file which does a bit more work than just grabbing the name of the executable from the command line: pass arguments to the debugged program, show the program name in the frame title, make exiting this session easier than my normal session, turn on auto-revert, set up a window configuration. Some of it is a matter of taste, pick what you like and leave out the rest.

;;; Emacs session dedicated to gdb

;; I'm often editing source files in my main session, so revert them
;; here automatically.
(when (fboundp 'global-auto-revert-mode)
  (global-auto-revert-mode))

;; Make this session easier to exit.
(if (boundp 'confirm-kill-emacs)
    (setq confirm-kill-emacs nil))
(defun my-confirm-kill-emacs ()
  (y-or-n-p "Really exit Emacs gdb session? "))
(or (memq 'my-confirm-kill-emacs kill-emacs-query-functions)
    (setq kill-emacs-query-functions
          (append kill-emacs-query-functions '(my-confirm-kill-emacs))))
(defun clear-buffer-process-query-on-exit-flag ()
  (let ((proc (get-buffer-process (current-buffer))))
    (if proc
        (set-process-query-on-exit-flag proc nil))))
(defun clear-gdb-inferior-process-query-on-exit-flag ()
  (if (equal (process-name (get-buffer-process (current-buffer))) "gdb-inferior")
      (clear-buffer-process-query-on-exit-flag)))
(add-hook 'comint-exec-hook 'clear-gdb-inferior-process-query-on-exit-flag)
(defadvice gud-common-init
    (after gud-common-init-set-process-query-on-exit-flag activate)
  "Don't ask for confirmation if exiting Emacs during the debug session."
  (clear-buffer-process-query-on-exit-flag))

;; I shouldn't need this, but `gdb-many-windows' often bugs out, and
;; this is useful to manually fix the windows.
(defun make-window-dedicated (arg)
  (interactive "p")
  "Make this window dedicated to the buffer that it's currently displaying.
With a zero or negative prefix argument, make this window non-dedicated."
  (set-window-dedicated-p (selected-window) (> arg 0)))

;; Run gdb.
(let ((command-string (combine-and-quote-strings command-line-args-left)))
  (set-frame-name (concat "gdb: " command-string))
  (gdb (concat "gdb -i=mi --args " command-string)))
(setq command-line-args-left nil)

;; Set up a nice window configuration.
(gdb-many-windows)

With this file saved as /path/to/my-gdb-session.el, I have a wrapper script called ~/bin/emacs-gdb that contains

#!/bin/sh
exec emacs -l /path/to/my-gdb-session.el "$@"

I can then run emacs-gdb my_exe argument1 argument2.

You can make it a shell alias (alias emacs-gdb='emacs -l /path/to/my-gdb-session.el') or a Windows shortcut if you prefer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.