all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How can I debug a macro?
@ 2015-12-16  8:21 Alan Schmitt
  2015-12-16  9:43 ` Phillip Lord
  2015-12-17 11:39 ` William Xu
  0 siblings, 2 replies; 7+ messages in thread
From: Alan Schmitt @ 2015-12-16  8:21 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 591 bytes --]

Hello,

I'm trying to track down a bug in `org-babel-comint-with-output' which
is a macro in org's ob-comint.el. I know how to use edebug, but it does
not seem to work with macros. I tried to read the info manual to figure
out how to use it (for instance info:elisp#Instrumenting Macro Calls),
but I don't understand what I actually need to do to be able to step
through this code.

Could someone please give me a hand in debugging this macro?

Thanks,

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Athmospheric CO₂ (Updated December 13, 2015, Mauna Loa Obs.): 401.31 ppm

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How can I debug a macro?
  2015-12-16  8:21 How can I debug a macro? Alan Schmitt
@ 2015-12-16  9:43 ` Phillip Lord
  2015-12-16 13:21   ` Alan Schmitt
  2015-12-17 11:39 ` William Xu
  1 sibling, 1 reply; 7+ messages in thread
From: Phillip Lord @ 2015-12-16  9:43 UTC (permalink / raw)
  To: Alan Schmitt; +Cc: help-gnu-emacs


Alan Schmitt <alan.schmitt@polytechnique.org> writes:

> Hello,
>
> I'm trying to track down a bug in `org-babel-comint-with-output' which
> is a macro in org's ob-comint.el. I know how to use edebug, but it does
> not seem to work with macros. I tried to read the info manual to figure
> out how to use it (for instance info:elisp#Instrumenting Macro Calls),
> but I don't understand what I actually need to do to be able to step
> through this code.
>
> Could someone please give me a hand in debugging this macro?


It depends what you want to do. If you use edebug on the macro itself,
you will get step-through debugging on the macro expansion. This can be
useful, although in most cases, I find "M-x pp-macroexpand-last-sexp"
works better (the macrostep package is worth looking as well, cause it's
very cool).

If you want to debug a *usage* of the macro, then the macro itself needs
to declare to edebug how it should be evaled. In most cases this is
quite easy. For `org-babel-comint-with-output' it's slightly tricker
because AFAICT the `meta' argument is not evaluated.

Consider these:

(defmacro temp-m (one &rest body)
  (declare (debug (form body)))
  (let ((a one))
    `(progn ,@body)))

(defun h ()
  (temp-m
   '(1 2 3)
   (message "hello")))

with the "declare" form in temp-m, calling "h" will step through the
code in "h" as you expect. The debug sexp means "first argument is a
form, the rest are body elements which need stepping through".

So, I think evalling:

(def-edebug-spec org-babel-comint-with-output
  (form body))

should do the trick, although probably you want to send a patch in for
org-comint.el (using the declare version) so that it works for everyone.

Phil






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How can I debug a macro?
  2015-12-16  9:43 ` Phillip Lord
@ 2015-12-16 13:21   ` Alan Schmitt
  2015-12-16 21:21     ` Phillip Lord
  0 siblings, 1 reply; 7+ messages in thread
From: Alan Schmitt @ 2015-12-16 13:21 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 2356 bytes --]

On 2015-12-16 10:43, phillip.lord@russet.org.uk (Phillip Lord) writes:

> If you want to debug a *usage* of the macro, then the macro itself needs
> to declare to edebug how it should be evaled. In most cases this is
> quite easy. For `org-babel-comint-with-output' it's slightly tricker
> because AFAICT the `meta' argument is not evaluated.

I want to debug the usage of the macro. It returns the wrong value and
I want to understand why.

> Consider these:
>
> (defmacro temp-m (one &rest body)
>   (declare (debug (form body)))
>   (let ((a one))
>     `(progn ,@body)))
>
> (defun h ()
>   (temp-m
>    '(1 2 3)
>    (message "hello")))
>
> with the "declare" form in temp-m, calling "h" will step through the
> code in "h" as you expect. The debug sexp means "first argument is a
> form, the rest are body elements which need stepping through".
>
> So, I think evalling:
>
> (def-edebug-spec org-babel-comint-with-output
>   (form body))
>
> should do the trick, although probably you want to send a patch in for
> org-comint.el (using the declare version) so that it works for everyone.

I'm trying this, but I'm confused at to what I need to do after I add
the declare form.

This is what I tried:
- add the declare form to org-babel-comint-with-output
- C-u C-M-x on org-babel-comint-with-output
- evaluate a block in org-mode

I then see a message:
edebug: Symbol's value as variable is void: edebug-def-mark

I then tried to debug the function calling the macro:
- C-u C-M-x on org-babel-execute:ocaml
- strangely, a debugging start even though I have not called the
function yet (I have not evaluated the source block)
- I try stepping through the macro, and it makes no sense (I see a lot
of things like "Result: (edebug-after (edebug-before 17) 20 (session
(edebug-after 0 18 org-babel-ocaml-eoe-output) t (edebug-after 0 19
full-body)))")
- if I then try to run a code block, I can start to step through
org-babel-execute:ocaml as expected, but as soon as I reach the point
where the macro is to be evaluated, I get an error: "Symbol's value as
variable is void: edebug-after"

I feel like I'm missing something here. Am I using this correctly?

Thanks,

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Athmospheric CO₂ (Updated December 13, 2015, Mauna Loa Obs.): 401.31 ppm

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How can I debug a macro?
  2015-12-16 13:21   ` Alan Schmitt
@ 2015-12-16 21:21     ` Phillip Lord
  2015-12-17  7:27       ` Alan Schmitt
  0 siblings, 1 reply; 7+ messages in thread
From: Phillip Lord @ 2015-12-16 21:21 UTC (permalink / raw)
  To: Alan Schmitt; +Cc: help-gnu-emacs

Alan Schmitt <alan.schmitt@polytechnique.org> writes:

> On 2015-12-16 10:43, phillip.lord@russet.org.uk (Phillip Lord) writes:
>
>> If you want to debug a *usage* of the macro, then the macro itself needs
>> to declare to edebug how it should be evaled. In most cases this is
>> quite easy. For `org-babel-comint-with-output' it's slightly tricker
>> because AFAICT the `meta' argument is not evaluated.
>
> I want to debug the usage of the macro. It returns the wrong value and
> I want to understand why.
>
>>
>> (def-edebug-spec org-babel-comint-with-output
>>   (form body))
>>
>> should do the trick, although probably you want to send a patch in for
>> org-comint.el (using the declare version) so that it works for everyone.
>
> I'm trying this, but I'm confused at to what I need to do after I add
> the declare form.

Sorry, I should have given a complete example. But I didn't know how to
call org-babel-comint-with-output, so I was guessing.


>
> This is what I tried:
> - add the declare form to org-babel-comint-with-output

So, on closer insepction I realise that there is an edebug spec
immediately after the macro.

(def-edebug-spec org-babel-comint-with-output (sexp body))


> - C-u C-M-x on org-babel-comint-with-output

Yeah, don't do that. Otherwise, you will be debugging the macro
expansion.

> - evaluate a block in org-mode
>
> I then see a message:
> edebug: Symbol's value as variable is void: edebug-def-mark

Yep, sorry by debug form was a bit wrong.

> I then tried to debug the function calling the macro:
> - C-u C-M-x on org-babel-execute:ocaml
> - strangely, a debugging start even though I have not called the
> function yet (I have not evaluated the source block)

That's correct. You've instrumented org-babel-comint-with-output, so it
gets evaluated at the time you eval org-babel-execute:ocaml. The macro
returns some form, of course, and this will be evaluated when the
calling function is evaled.


> I feel like I'm missing something here. Am I using this correctly?

Try the debug statement given here. You should now get step through
debugging of the arguments to org-babel-comint-with-output (or the
second one anyway -- the first one is not evaluated).

If this still doesn't help, and you need to debug the *macro* not the
usage, then put point after this after do M-x pp-macroexpand-last-sexp

(org-babel-comint-with-output
    		  (session org-babel-ocaml-eoe-output t full-body)
                (insert
		 (concat
		  (org-babel-chomp
    		  full-body)";;\n"org-babel-ocaml-eoe-indicator))

which will give you this.


(org-babel-comint-in-buffer session
  (let
      ((string-buffer "")
       dangling-text raw)
    (setq comint-output-filter-functions
          (cons
           (lambda
             (text)
             (setq string-buffer
                   (concat string-buffer text)))
           comint-output-filter-functions))
    (unwind-protect
        (progn
          (goto-char
           (process-mark
            (get-buffer-process
             (current-buffer))))
          (let
              ((start
                (point))
               (end
                (point-max)))
            (setq dangling-text
                  (buffer-substring start end))
            (delete-region start end))
          (insert
           (concat
            (org-babel-chomp full-body)
            ";;\n" org-babel-ocaml-eoe-indicator))
          (tuareg-interactive-send-input)
          (while
              (progn
                (goto-char comint-last-input-end)
                (not
                 (save-excursion
                   (and
                    (re-search-forward
                     (regexp-quote org-babel-ocaml-eoe-output)
                     nil t)
                    (re-search-forward comint-prompt-regexp nil t)))))
            (accept-process-output
             (get-buffer-process
              (current-buffer))))
          (goto-char
           (process-mark
            (get-buffer-process
             (current-buffer))))
          (insert dangling-text))
      (setq comint-output-filter-functions
            (cdr comint-output-filter-functions)))
    (if
        (and t full-body
             (string-match
              (replace-regexp-in-string "\n" "[\n]+"
                                        (regexp-quote
                                         (or full-body "")))
              string-buffer))
        (setq raw
              (substring string-buffer
                         (match-end 0))))
    (split-string string-buffer comint-prompt-regexp)))


Unfortunately, it's got another macro inside. If you *really* need to
debug this, then, the best you can do is put org-babel-execute:ocaml
into a file, and replace the org-babel-comint-with-output form with this
expansion. Then you can run edebug directly on that.

I'd rather not have a macro with such a large expansion myself, because
it is hard to debug, but sometimes it's necessary.

Phil



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How can I debug a macro?
  2015-12-16 21:21     ` Phillip Lord
@ 2015-12-17  7:27       ` Alan Schmitt
  0 siblings, 0 replies; 7+ messages in thread
From: Alan Schmitt @ 2015-12-17  7:27 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 847 bytes --]

On 2015-12-16 22:21, phillip.lord@russet.org.uk (Phillip Lord) writes:

> If this still doesn't help, and you need to debug the *macro* not the
> usage, then put point after this after do M-x pp-macroexpand-last-sexp
>
> (org-babel-comint-with-output
>     		  (session org-babel-ocaml-eoe-output t full-body)
>                 (insert
> 		 (concat
> 		  (org-babel-chomp
>     		  full-body)";;\n"org-babel-ocaml-eoe-indicator))

Thank you, this was most helpful and I learned a lot. I ended up using
debug-on-entry and stepping through the function before I read your
message, I found out the problem was the t argument to the macro. I can
see the problem better in the expanded macro.

Thanks again,

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Athmospheric CO₂ (Updated December 13, 2015, Mauna Loa Obs.): 401.31 ppm

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How can I debug a macro?
  2015-12-16  8:21 How can I debug a macro? Alan Schmitt
  2015-12-16  9:43 ` Phillip Lord
@ 2015-12-17 11:39 ` William Xu
  2015-12-17 12:03   ` Alan Schmitt
  1 sibling, 1 reply; 7+ messages in thread
From: William Xu @ 2015-12-17 11:39 UTC (permalink / raw)
  To: help-gnu-emacs

Alan Schmitt <alan.schmitt@polytechnique.org> writes:

> Could someone please give me a hand in debugging this macro?

This package in melpa seems useful for debugging macro.  It will expand
the macro in place, so you can debug step by step.  

  macrostep          20151213.145  available  melpa      interactive macro expander


-- 
William




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How can I debug a macro?
  2015-12-17 11:39 ` William Xu
@ 2015-12-17 12:03   ` Alan Schmitt
  0 siblings, 0 replies; 7+ messages in thread
From: Alan Schmitt @ 2015-12-17 12:03 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 584 bytes --]

On 2015-12-17 12:39, William Xu <william.xwl@gmail.com> writes:

> Alan Schmitt <alan.schmitt@polytechnique.org> writes:
>
>> Could someone please give me a hand in debugging this macro?
>
> This package in melpa seems useful for debugging macro.  It will expand
> the macro in place, so you can debug step by step.  
>
>   macrostep          20151213.145  available  melpa      interactive macro expander

Ah, thank you, this looks very useful.

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Athmospheric CO₂ (Updated December 13, 2015, Mauna Loa Obs.): 401.31 ppm

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-12-17 12:03 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-12-16  8:21 How can I debug a macro? Alan Schmitt
2015-12-16  9:43 ` Phillip Lord
2015-12-16 13:21   ` Alan Schmitt
2015-12-16 21:21     ` Phillip Lord
2015-12-17  7:27       ` Alan Schmitt
2015-12-17 11:39 ` William Xu
2015-12-17 12:03   ` Alan Schmitt

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.