# \[RCR\] Accessor for trace\_func

**URL:** <https://rubytalk.org/t/rcr-accessor-for-trace-func/3411>\
**Category:** ruby-talk\
**Created:** [7 December 2002 06:12 UTC](https://rubytalk.org/t/rcr-accessor-for-trace-func/3411 "2002-12-07T06:12:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathaniel\_Talbott](https://avatars.discourse-cdn.com/v4/letter/n/ec9cab/32.png) [@Nathaniel\_Talbott](https://rubytalk.org/u/Nathaniel_Talbott)\
**Post date:** [7 December 2002 06:12 UTC](https://rubytalk.org/t/rcr-accessor-for-trace-func/3411/1 "2002-12-07T06:12:24Z")

</div>

Robert Feldt actually requested this back in ruby-talk:11158, but  
nothing every come of it then (and I’m hurting for it now), so I thought  
I’d bring it back up again. I’ll add a RubyGarden RCR after there’s a  
bit of discussion.

Why do I need it? Well, currently you’ll find the following at the top  
of the at\_exit block in test/unit.rb:

```
    set_trace_func DEBUGGER__.context.method(:trace_func).to_proc if

```

$DEBUG

I need this because the trace\_func is no longer registered when at\_exit  
gets called, so if it’s missing, I can’t debug tests run with unit.rb.  
OK, no problem, I can handle that UNTIL… somebody wants to profile  
their tests. Uh oh. Now I have two ugly lines like that. And it only  
works for those two… not Joe Schmoe’s Nifty Debugger or some other  
cool thing that gets done with a trace\_func.

If I had Kernel.trace\_func (which I think is an OK name… but the name  
is not what’s important to me at this point), I could just add a BEGIN  
block to unit.rb:

```
    BEGIN { $saved_trace_func = trace_func }

```

and change the line in at\_exit like so:

```
    set_trace_func $saved_trace_func

```

And it oughta work for any old trace\_func that’s thrown at it.

If there’s a better way to handle this, by all means, point me at it.  
But if not, what of the RCR?

Comments? Questions? Snide remarks?

Nathaniel

\<:((\>\<

- 
  - 
    - 

> **···**
>
> > RoleModel Software, Inc.  
> > EQUIP VI

---

<div class="post-metadata">

**Author:** ![Paul\_Brannan](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@Paul\_Brannan](https://rubytalk.org/u/Paul_Brannan)\
**Post date:** [7 December 2002 16:46 UTC](https://rubytalk.org/t/rcr-accessor-for-trace-func/3411/2 "2002-12-07T16:46:19Z")

</div>

I think this proposed RCR is a good idea. It allows trace funcs to be  
chained together, which is a good thing if you want to trace and profile  
at the same time.

It might be nice if the RCR requested that $trace\_func be a hooked  
global variable.

One problem with chaining trace funcs is that it makes it difficult to  
remove a trace func from the chain. Another solution, if you don’t want  
to wait for Ruby to change, is to redefine set\_trace\_func:

$trace\_funcs = []

class Object  
alias\_method :old\_set\_trace\_func, :set\_trace\_func

```
def set_trace_func(func)
  $trace_funcs.push(func)
end

old_set_trace_func proc { |*args|
  $trace_funcs.each { |f| f.call(*args) }
}

```

end

Any call to set\_trace\_func will now add the func to the list of trace  
funcs instead of replacing the existing one. This makes it easy to  
remove a trace func (just write a remove\_trace\_func method), but will  
have the side effect of breaking code that relies on “set\_trace\_func  
nil” removing all existing trace funcs.

Paul

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [9 December 2002 23:13 UTC](https://rubytalk.org/t/rcr-accessor-for-trace-func/3411/3 "2002-12-09T23:13:57Z")

</div>

Hi,

> **···**
>
> In message “[RCR] Accessor for trace\_func” on 02/12/07, nathaniel@NOSPAMtalbott.ws [nathaniel@NOSPAMtalbott.ws](mailto:nathaniel@NOSPAMtalbott.ws) writes:
> 
> > Robert Feldt actually requested this back in ruby-talk:11158, but  
> > nothing every come of it then (and I’m hurting for it now), so I thought  
> > I’d bring it back up again. I’ll add a RubyGarden RCR after there’s a  
> > bit of discussion.
> 
> I saw IRC log that Nathaniel worried about this request being  
> ignored. So I give you ACK. I’m thinking about this, without getting  
> conclusion yet.
> 
> ```
> matz.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Talbott](https://avatars.discourse-cdn.com/v4/letter/n/ec9cab/32.png) [@Nathaniel\_Talbott](https://rubytalk.org/u/Nathaniel_Talbott)\
**Post date:** [7 December 2002 18:06 UTC](https://rubytalk.org/t/rcr-accessor-for-trace-func/3411/4 "2002-12-07T18:06:57Z")

</div>

> I think this proposed RCR is a good idea. It allows trace  
> funcs to be chained together, which is a good thing if you  
> want to trace and profile at the same time.

Before looking at eval.c I didn’t even realize that trace\_funcs were  
exclusive, i.e., “there can only be one.” But that is, indeed, the case.  
I’d personally be satisfied with just being able to grab “the one” that  
is set, but I can see benefit in having built-in ability to add multiple  
ones. Probably a separate RCR, though.

> It might be nice if the RCR requested that $trace\_func be a  
> hooked global variable.

What do you mean by “hooked”?

> One problem with chaining trace funcs is that it makes it  
> difficult to remove a trace func from the chain. Another  
> solution, if you don’t want to wait for Ruby to change, is to  
> redefine set\_trace\_func:
> 
> $trace\_funcs =
> 
> class Object  
> alias\_method :old\_set\_trace\_func, :set\_trace\_func
> 
> ```
> def set_trace_func(func)
> $trace_funcs.push(func)
> end
> 
> old_set_trace_func proc { |*args|
> $trace_funcs.each { |f| f.call(*args) }
> }
> 
> ```
> 
> end
> 
> Any call to set\_trace\_func will now add the func to the list  
> of trace funcs instead of replacing the existing one. This  
> makes it easy to remove a trace func (just write a  
> remove\_trace\_func method), but will have the side effect of  
> breaking code that relies on “set\_trace\_func nil” removing  
> all existing trace funcs.

I thought about this solution, and even played with it, but it doesn’t  
work at all for my case: the debugger sets its trace\_func long before I  
get a change to redefine the method.

Nathaniel

\<:((\>\<

- 
  - 
    - 

> **···**
>
> Paul Brannan [[mailto:pbrannan@atdesk.com](mailto:pbrannan@atdesk.com)] wrote:
> 
> > RoleModel Software, Inc.  
> > EQUIP VI

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Talbott](https://avatars.discourse-cdn.com/v4/letter/n/ec9cab/32.png) [@Nathaniel\_Talbott](https://rubytalk.org/u/Nathaniel_Talbott)\
**Post date:** [10 December 2002 01:37 UTC](https://rubytalk.org/t/rcr-accessor-for-trace-func/3411/5 "2002-12-10T01:37:43Z")

</div>

> > Robert Feldt actually requested this back in ruby-talk:11158, but  
> > nothing every come of it then (and I’m hurting for it now), so I  
> > thought I’d bring it back up again. I’ll add a RubyGarden RCR after  
> > there’s a bit of discussion.
> 
> I saw IRC log that Nathaniel worried about this request being  
> ignored. So I give you ACK. I’m thinking about this,  
> without getting conclusion yet.

Thanks, matz, it’s great to know you’re thinking about it. Would it help  
if I went ahead and added an RCR on RubyGarden, or would it be better to  
wait?

Nathaniel

\<:((\>\<

- 
  - 
    - 

> **···**
>
> Yukihiro Matsumoto [[mailto:matz@ruby-lang.org](mailto:matz@ruby-lang.org)] wrote:
> 
> > RoleModel Software, Inc.  
> > EQUIP VI

---

<div class="post-metadata">

**Author:** ![Paul\_Brannan](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@Paul\_Brannan](https://rubytalk.org/u/Paul_Brannan)\
**Post date:** [7 December 2002 18:22 UTC](https://rubytalk.org/t/rcr-accessor-for-trace-func/3411/6 "2002-12-07T18:22:24Z")

</div>

> > It might be nice if the RCR requested that $trace\_func be a  
> > hooked global variable.
> 
> What do you mean by “hooked”?

rb\_define\_hooked\_variable creates a hooked variable. This is how  
$stdin, $stdout, $!, and other variables are implemented. See  
README.EXT for an explanation.

> I thought about this solution, and even played with it, but it doesn’t  
> work at all for my case: the debugger sets its trace\_func long before I  
> get a change to redefine the method.

With:  
[http://rm-f.net/~cout/code/ruby/treasures/RubyTreasures-0.4/lib/hacks/fix\_trace\_func.rb.html](http://rm-f.net/~cout/code/ruby/treasures/RubyTreasures-0.4/lib/hacks/fix_trace_func.rb.html)

I can write:  
ruby -rfix\_trace\_func -rtracer -rcrimson\_profiler test.rb

and I get both tracing and profiling. I had to use a slightly modified  
profiler to get this to work, but I can’t remember why at the moment.

Paul

> **···**
>
> On Sun, Dec 08, 2002 at 03:06:57AM +0900, nathaniel@NOSPAMtalbott.ws wrote:

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [10 December 2002 04:49 UTC](https://rubytalk.org/t/rcr-accessor-for-trace-func/3411/7 "2002-12-10T04:49:46Z")

</div>

Hi,

> **···**
>
> In message “Re: [RCR] Accessor for trace\_func” on 02/12/10, nathaniel@NOSPAMtalbott.ws [nathaniel@NOSPAMtalbott.ws](mailto:nathaniel@NOSPAMtalbott.ws) writes:
> 
> > Thanks, matz, it’s great to know you’re thinking about it. Would it help  
> > if I went ahead and added an RCR on RubyGarden, or would it be better to  
> > wait?
> 
> Just wait. And discuss when you have related ideas.  
> It will not be added before 1.8.0 though.
> 
> ```
> matz.
> 
> ```
> 
> p.s.  
> You will have 1.8.0 preview within this year. Merry Christmas.

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Talbott](https://avatars.discourse-cdn.com/v4/letter/n/ec9cab/32.png) [@Nathaniel\_Talbott](https://rubytalk.org/u/Nathaniel_Talbott)\
**Post date:** [7 December 2002 19:06 UTC](https://rubytalk.org/t/rcr-accessor-for-trace-func/3411/8 "2002-12-07T19:06:51Z")

</div>

> > I thought about this solution, and even played with it, but it  
> > doesn’t  
> > work at all for my case: the debugger sets its trace\_func long  
> > before  
> > I get a change to redefine the method.
> 
> With:

[http://rm-f.net/~cout/code/ruby/treasures/RubyTreasures-0.4/lib/hacks/fi](http://rm-f.net/~cout/code/ruby/treasures/RubyTreasures-0.4/lib/hacks/fi)  
x\_trace\_func.rb.html

> I can write:  
> ruby -rfix\_trace\_func -rtracer -rcrimson\_profiler test.rb
> 
> and I get both tracing and profiling. I had to use a slightly  
> modified  
> profiler to get this to work, but I can’t remember why at the moment.

I know it would work if I made the user require a fix before requiring  
the debugger, but that just doesn’t seem right. As a user myself, I want  
this to work:

ruby -rdebug my\_tests.rb

Nathaniel

\<:((\>\<

- 
  - 
    - 

> **···**
>
> Paul Brannan [[mailto:pbrannan@atdesk.com](mailto:pbrannan@atdesk.com)] wrote:
> 
> > RoleModel Software, Inc.  
> > EQUIP VI

---

<div class="post-metadata">

**Author:** ![Shashank\_Date3](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Shashank\_Date3](https://rubytalk.org/u/Shashank_Date3)\
**Post date:** [10 December 2002 05:27 UTC](https://rubytalk.org/t/rcr-accessor-for-trace-func/3411/9 "2002-12-10T05:27:06Z")

</div>

“Yukihiro Matsumoto” [matz@ruby-lang.org](mailto:matz@ruby-lang.org) wrote in message

> matz.  
> p.s.  
> You will have 1.8.0 preview within this year. Merry Christmas.

Dear Santa,

Can we send you our Christmas wish list then? 😉

– shanko

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [10 December 2002 05:34 UTC](https://rubytalk.org/t/rcr-accessor-for-trace-func/3411/10 "2002-12-10T05:34:41Z")

</div>

Hi,

> **···**
>
> In message “Re: [RCR] Accessor for trace\_func” on 02/12/10, “Shashank Date” [sdate@kc.rr.com](mailto:sdate@kc.rr.com) writes:
> 
> > Can we send you our Christmas wish list then? 😉
> 
> Yes, you can. But you have to be a good boy to get your best gift.
> 
> ```
> matz.
> 
> ```
