I use emacs/gdb for debugging. Generally, I
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" ....
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
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")'
You can pass options to Emacs to make it run code when it starts:
--eval "(some-lisp-code 'taking-care-of \"quoting for the shell\")".
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.