Starting up gdb from Emacs with the right pid automatically

Last week I was developing this patch:

It was full of corner cases, so I wrote many tests, and I debugged each test using gdb often.

That let me to do a lot of: make a test, debug it using gdb, and repeat.

Each time I would need to debug gdb I would look for it’s pid using ps, but that was quite repetitive, way too repetitive for an emacs user.

So there’s now a better way with this little bit of elisp:

(defun gdb-test ()
  "Run gdb with the pid of the gdb currectly executing a test"
  (gdb (concat gud-gdb-command-name (format " -p %s" (system-process-pid "gdb.*-nw.*-nx")))))

(defun system-process-pid (regex-str)
  "Return the pid for a process with args matching regex-str"
  (dolist (pid (list-system-processes))
    (let ((attrs (process-attributes pid)))
      (if (string-match-p regex-str
			  (cdr (assoc 'args attrs)))
	  (return pid)))))

(provide 'custom-lib)

This will find the pid of the gdb under test, assuming that it always has -nw.*-nw it it’s arguments.

So calling M-x gdb-test will start gdb with the pid of the gdb under test et voila 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *