[racket] how to trace stack by command line tools?

291 views
Skip to first unread message

mikeyao

unread,
Feb 2, 2012, 12:59:41 AM2/2/12
to us...@racket-lang.org
I'm learning scheme and code in emacs and geiser. I want to watch the whole
recursive process to understand well. I know drracket can do it. Is it other
tools(command line) that can use in emacs?

____________________
Racket Users list:
http://lists.racket-lang.org/users

Danny Yoo

unread,
Feb 2, 2012, 12:17:13 PM2/2/12
to mikeyao, us...@racket-lang.org
On Thu, Feb 2, 2012 at 12:59 AM, mikeyao <mike...@gmail.com> wrote:
> I'm learning scheme and code in emacs and geiser. I want to watch the whole
> recursive process to understand well. I know drracket can do it. Is it other
> tools(command line) that can use in emacs?


A simple tool you can use is the 'racket/trace' library:

http://docs.racket-lang.org/reference/debugging.html

It'll make the execution of a function visible to you. You have to
explicitly trace the function you want to watch.

---

There's also an "errortrace" module that can help produce stack traces:

http://docs.racket-lang.org/errortrace/using-errortrace.html

You need to do a little work to hook it into your program. For an
example, see the very top startup script of Eric Hanchrow's rudybot:

https://github.com/offby1/rudybot/blob/master/freenode-main.rkt

where you'll see that it has the line:

exec racket -l errortrace --require "$0" --main -- ${1+"$@"}

at the top: that's one way to hook errortrace in.

zermelo

unread,
Feb 2, 2012, 2:19:18 PM2/2/12
to us...@racket-lang.org

I have tried trace library but I have a problem.
If I put my definitions in the interactive window, everything is fine.
But if I put the definitions in the definition window and then I try to
trace them, I get error "set!: cannot modify a constant: ".
Why?

Danny Yoo

unread,
Feb 2, 2012, 2:28:49 PM2/2/12
to zermelo, us...@racket-lang.org
> I have tried trace library but I have a problem.
> If I put my definitions in the interactive window, everything is fine.
> But if I put the definitions in the definition window and then I try to
> trace them, I get error "set!: cannot modify a constant: ".
> Why?

Let me assume you're doing something like this, that in Definitions,
you have something like this:

#lang racket
(require racket/trace)
(define (f x) (* x x))

and in Interactions, you're trying to use trace:

> (trace f)

and you see an error at this point:

set!: cannot modify a constant: ..

What's happening here is that the program in Definitions tries to
determine whether each definition is constant or not. It doesn't see
that anything is changing 'f' inside Definitions, so it freezes the
definition. Unfortunately, that means that you can't trace it.

Here's how to work around this: you can add the following right after
a definition, such as f:

#lang racket
(require racket/trace)
(define (f x) (* x x))
(set! f f)

The set! there is a no-op, but it effectively tells Racket not to
enforce the constantness of f. Then trace can be run on it from
Interactions.

Eli Barzilay

unread,
Feb 2, 2012, 2:32:28 PM2/2/12
to Danny Yoo, us...@racket-lang.org, zermelo
A few minutes ago, Danny Yoo wrote:
> Here's how to work around this: you can add the following right after
> a definition, such as f:
>
> #lang racket
> (require racket/trace)
> (define (f x) (* x x))
> (set! f f)
>
> The set! there is a no-op, but it effectively tells Racket not to
> enforce the constantness of f. Then trace can be run on it from
> Interactions.

Perhaps an easier approach is to just trace it inside the definitions:

#lang racket
(require racket/trace)
(define (f x) (* x x))

(trace f)

--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://barzilay.org/ Maze is Life!

Jose A. Ortega Ruiz

unread,
Feb 2, 2012, 3:07:20 PM2/2/12
to us...@racket-lang.org
On Thu, Feb 02 2012, Danny Yoo wrote:

[...]

> There's also an "errortrace" module that can help produce stack traces:
>
> http://docs.racket-lang.org/errortrace/using-errortrace.html
>
> You need to do a little work to hook it into your program. For an
> example, see the very top startup script of Eric Hanchrow's rudybot:
>
> https://github.com/offby1/rudybot/blob/master/freenode-main.rkt
>
> where you'll see that it has the line:
>
> exec racket -l errortrace --require "$0" --main -- ${1+"$@"}
>
> at the top: that's one way to hook errortrace in.

Just a note (since the OP mentioned he's using it): geiser already loads
errortrace in its REPLs, so using geiser is another way.

Cheers,
jao
--
Men can live without air a few minutes, without water for about two
weeks, without food for about two months - and without a new thought
for years on end.
-Kent Ruth

zermelo

unread,
Feb 3, 2012, 9:09:44 AM2/3/12
to us...@racket-lang.org
On Thu, 02 Feb 2012 14:32:28 -0500, Eli Barzilay wrote:

> A few minutes ago, Danny Yoo wrote:
>> Here's how to work around this: you can add the following right after
>> a definition, such as f:
>>
>> #lang racket
>> (require racket/trace)
>> (define (f x) (* x x))
>> (set! f f)
>>
>> The set! there is a no-op, but it effectively tells Racket not to
>> enforce the constantness of f. Then trace can be run on it from
>> Interactions.
>
> Perhaps an easier approach is to just trace it inside the definitions:
>
> #lang racket
> (require racket/trace)
> (define (f x) (* x x))
> (trace f)

Thanks.

Reply all
Reply to author
Forward
0 new messages