Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i hook commands sent to pdb through gud?

i've started using pdb through gud in emacs 23.3, how can i hook command messages sent to the debugger from the buffer? i wrote the advice below for use with gdb, in order to persist comint's ring, but can't find an equivalent function to hook for pdb. i'm using python-mode.el as my major mode.

thanks.

(defadvice gdb-send-item (before gdb-save-history first nil activate)
  "write input ring on quit"
  (if (equal (type-of item) 'string) ; avoid problems with 'unprintable' structures sent to this function..
    (if (string-match "^q\\(u\\|ui\\|uit\\)?$" item)
      (progn (comint-write-input-ring)
             (message "history file '%s' written" comint-input-ring-file-name)))))
like image 553
elbeardmorez Avatar asked Jun 06 '11 20:06

elbeardmorez


People also ask

How do you continue in pdb?

Finally, we end with a c to continue execution, which ends the command block. Typing c again at the (Pdb) prompt starts execution and we see our new print statement running. If you'd rather stop execution instead of continuing, you can use end instead of c in the command block.

How do I watch a variable in pdb?

For watching a variable when you are hitting a breakpoint, you can use the commands command. E.g. printing some_variable when hitting breakpoint #1 (canonical example from pdb doc). Additionally, you can use the condition command to ensure the breakpoint is only hit whenever the variable takes a certain value.

What command in pdb resumes execution?

You can use breakpoint commands to start your program up again. Simply use the continue command, or step , or any other command that resumes execution.

How do I use pdb debugger in Python?

Starting Python Debugger To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().


1 Answers

i think i probably could have answered my own question at the time with just a little more digging, but the first gdb solution rather took it out of me on the old learning front. i recovered, so..

C-h b C-s Major

after a bit of scrolling we can identify 'comint-send-input' as the function bound to key 'enter'. looking at this function's source, comint.el:1765 is a call to 'run-hook-with-args' ..this is where we realise that there's nowhere specifically 'pdb' to do what we want.

gud is a generic wrapper to call external debugging processes and return the results ..hence control isn't there in elisp. it was the same with gdb, but there was a nice (pre-existing) wrapper around the external call which made advising that function feel 'clean'.

so the hack.. just above 'comint-send-input' lies 'comint-add-to-input-history'.. dead easy.

;;save command history
(defadvice comint-add-to-input-history (before pdb-save-history activate compile)
  "write input ring on exit"
  (message "%s" cmd)
  (if (string-match "^e\\(x\\|xi\\|xit\\)?$" cmd)
  (progn (comint-write-input-ring)
     (message "history file '%s' written" comint-input-ring-file-name)))
)

fyi, i have these to initiate the input ring for the debugging sessions

;#debugger history
(defun debug-history-ring (file)
  (comint-read-input-ring t)
  (setq comint-input-ring-file-name file)
  (setq comint-input-ring-size 1000)
  (setq comint-input-ignoredups t))
(let ((hooks '((gdb-mode-hook . (lambda () (debug-history-ring "~/.gdbhist")))
       (pdb-mode-hook . (lambda () (debug-history-ring  "~/.pythonhist"))))))
  (dolist (hook hooks) (print (cdr hook)) (add-hook (car hook) (cdr hook))))

..and to write to the history file if the debugging buffer is killed

  (add-hook 'kill-buffer-hook 'comint-write-input-ring)

cheers.

like image 71
elbeardmorez Avatar answered Oct 20 '22 09:10

elbeardmorez