devdoer bird | 15 Aug 18:59
Gravatar

How to get the line number of current executable code?

HI:
 
I want to implement a function like "get_current_lineno()/0" to get the current line number of the calling point?
Eg.
.....
....
io:format("current line is ~w\n",[get_current_lineno()])
.....
 
the above code will print the line number of the calling point in the source file.
 
How can I do this  in erlang?
<div>
<div>HI:</div>
<div>&nbsp;</div>
<div>I want to implement a function like "get_current_lineno()/0" to get the current line number of the calling point? </div>
<div>Eg.</div>
<div>.....</div>
<div>....</div>
<div>io:format("current line is ~w\n",[get_current_lineno()])</div>
<div>.....</div>
<div>&nbsp;</div>
<div>the above code will print the line number of the calling point in the source file.</div>
<div>&nbsp;</div>
<div>How can I do this&nbsp; in erlang?</div>
</div>
Anders Nygren | 15 Aug 20:22

Re: How to get the line number of current executable code?

2008/8/15 devdoer bird <devdoer2 <at> gmail.com>:
> HI:
>
> I want to implement a function like "get_current_lineno()/0" to get the
> current line number of the calling point?
> Eg.
> .....
> ....
> io:format("current line is ~w\n",[get_current_lineno()])
> .....
>
> the above code will print the line number of the calling point in the source
> file.
>
> How can I do this  in erlang?

There is a predefined macro ?LINE that does that
so
io:format("current line is ~w\n",[?LINE])

/Anders

> _______________________________________________
> erlang-questions mailing list
> erlang-questions <at> erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
devdoer bird | 15 Aug 20:31
Gravatar

Re: How to get the line number of current executable code?

Thanks.
 
How can I do this without macro? I know python supply some tools to determine the line number in run time,like inspect module.

 
2008/8/16, Anders Nygren <anders.nygren <at> gmail.com>:
2008/8/15 devdoer bird <devdoer2 <at> gmail.com>:
> HI:
>
> I want to implement a function like "get_current_lineno()/0" to get the
> current line number of the calling point?
> Eg.
> .....
> ....
> io:format("current line is ~w\n",[get_current_lineno()])
> .....
>
> the above code will print the line number of the calling point in the source
> file.
>
> How can I do this  in erlang?

There is a predefined macro ?LINE that does that
so
io:format("current line is ~w\n",[?LINE])

/Anders

> _______________________________________________
> erlang-questions mailing list
> erlang-questions <at> erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
>

<div>
<div>Thanks.</div>
<div>&nbsp;</div>
<div>How can I do this without macro? I know python supply some tools to determine the line number in run time,like inspect module. <br><br>&nbsp;</div>
<div>
<span class="gmail_quote">2008/8/16, Anders Nygren &lt;<a href="mailto:anders.nygren <at> gmail.com">anders.nygren <at> gmail.com</a>&gt;:</span>
<blockquote class="gmail_quote">2008/8/15 devdoer bird &lt;<a href="mailto:devdoer2 <at> gmail.com">devdoer2 <at> gmail.com</a>&gt;:<br>&gt; HI:<br>
&gt;<br>&gt; I want to implement a function like "get_current_lineno()/0" to get the<br>&gt; current line number of the calling point?<br>&gt; Eg.<br>&gt; .....<br>&gt; ....<br>&gt; io:format("current line is ~w\n",[get_current_lineno()])<br>
&gt; .....<br>&gt;<br>&gt; the above code will print the line number of the calling point in the source<br>&gt; file.<br>&gt;<br>&gt; How can I do this&nbsp;&nbsp;in erlang?<br><br>There is a predefined macro ?LINE that does that<br>
so<br>io:format("current line is ~w\n",[?LINE])<br><br>/Anders<br><br>&gt; _______________________________________________<br>&gt; erlang-questions mailing list<br>&gt; <a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> erlang.org</a><br>
&gt; <a href="http://www.erlang.org/mailman/listinfo/erlang-questions">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>&gt;<br>
</blockquote>
</div>
<br>
</div>
Richard Carlsson | 15 Aug 21:05
Favicon

Re: How to get the line number of current executable code?

devdoer bird wrote:
> How can I do this without macro? I know python supply some tools to
> determine the line number in run time,like inspect module.

There is currently no support for that in Erlang.

    /Richard
Ahmed Ali | 16 Aug 18:40

Re: How to get the line number of current executable code?

Hi Richard,

How about if I compiled the source with debugging enabled? Isn't this
information supposed to be there (along with function name and other
info)?

/Ahmed Al-Issaei

On Fri, Aug 15, 2008 at 11:05 PM, Richard Carlsson <richardc <at> it.uu.se> wrote:
> devdoer bird wrote:
>> How can I do this without macro? I know python supply some tools to
>> determine the line number in run time,like inspect module.
>
> There is currently no support for that in Erlang.
>
>    /Richard
> _______________________________________________
> erlang-questions mailing list
> erlang-questions <at> erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
Richard Carlsson | 16 Aug 20:19
Favicon

Re: How to get the line number of current executable code?

Ahmed Ali wrote:
> Hi Richard,
> 
> How about if I compiled the source with debugging enabled? Isn't this
> information supposed to be there (along with function name and other
> info)?

The problem is that there is no direct line-for-line mapping between the
abstract code in the debugging information, and the executing beam code.
The beam code does contain the info about the current function, but no
line numbers. Using a combination of the two, you could find the line
number for the current function, but figuring out the actual line
within that function (given the current point in the beam code) could
only be approximated using heuristics.

If you're actually running in interpreted mode (debugging mode), you
do get the current line, but it's a couple of orders of magmitude
slower, so you don't want to do that for all your code.

I hope some day someone will find the time to make the compiler
propagate the line number information into a mapping table that could
be included in the beam file at all times, even if the file was not
compiled with debugging enabled. That would make it possible to give
much nicer stacktraces.

    /Richard
Thomas Lindgren | 17 Aug 14:14
Favicon

Re: How to get the line number of current executable code?


--- On Sat, 8/16/08, Richard Carlsson <richardc <at> it.uu.se> wrote:
> I hope some day someone will find the time to make the
> compiler
> propagate the line number information into a mapping table
> that could
> be included in the beam file at all times, even if the file
> was not
> compiled with debugging enabled. That would make it
> possible to give
> much nicer stacktraces.

Indeed. Doing this might be a bit finicky but it's very helpful. The usual scripting language suspects
already provide such stack traces, by the way.

Best,
Thomas

      
Matt Williamson | 15 Aug 21:07
Gravatar

Re: How to get the line number of current executable code?

You should trust the macro. It must use a similar method to Python's because they are both compiled to bytecode and thus there wouldn't *really* be line numbers in either one.

2008/8/15 devdoer bird <devdoer2 <at> gmail.com>
Thanks.
 
How can I do this without macro? I know python supply some tools to determine the line number in run time,like inspect module.

 
2008/8/16, Anders Nygren <anders.nygren <at> gmail.com>:
2008/8/15 devdoer bird <devdoer2 <at> gmail.com>:
> HI:
>
> I want to implement a function like "get_current_lineno()/0" to get the
> current line number of the calling point?
> Eg.
> .....
> ....
> io:format("current line is ~w\n",[get_current_lineno()])
> .....
>
> the above code will print the line number of the calling point in the source
> file.
>
> How can I do this  in erlang?

There is a predefined macro ?LINE that does that
so
io:format("current line is ~w\n",[?LINE])

/Anders

> _______________________________________________
> erlang-questions mailing list
> erlang-questions <at> erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
>


_______________________________________________
erlang-questions mailing list
erlang-questions <at> erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions

<div><div dir="ltr">You should trust the macro. It must use a similar method to Python's because they are both compiled to bytecode and thus there wouldn't *really* be line numbers in either one. <br><br><div class="gmail_quote">
2008/8/15 devdoer bird <span dir="ltr">&lt;<a href="mailto:devdoer2 <at> gmail.com">devdoer2 <at> gmail.com</a>&gt;</span><br><blockquote class="gmail_quote">
<div>Thanks.</div>
<div>&nbsp;</div>
<div>How can I do this without macro? I know python supply some tools to determine the line number in run time,like inspect module. <br><br>&nbsp;</div>
<div>
<span class="gmail_quote">2008/8/16, Anders Nygren &lt;<a href="mailto:anders.nygren <at> gmail.com" target="_blank">anders.nygren <at> gmail.com</a>&gt;:</span><div class="Ih2E3d">
<blockquote class="gmail_quote">2008/8/15 devdoer bird &lt;<a href="mailto:devdoer2 <at> gmail.com" target="_blank">devdoer2 <at> gmail.com</a>&gt;:<br>
&gt; HI:<br>
&gt;<br>&gt; I want to implement a function like "get_current_lineno()/0" to get the<br>&gt; current line number of the calling point?<br>&gt; Eg.<br>&gt; .....<br>&gt; ....<br>&gt; io:format("current line is ~w\n",[get_current_lineno()])<br>

&gt; .....<br>&gt;<br>&gt; the above code will print the line number of the calling point in the source<br>&gt; file.<br>&gt;<br>&gt; How can I do this&nbsp;&nbsp;in erlang?<br><br>There is a predefined macro ?LINE that does that<br>

so<br>io:format("current line is ~w\n",[?LINE])<br><br>/Anders<br><br>&gt; _______________________________________________<br>&gt; erlang-questions mailing list<br>&gt; <a href="mailto:erlang-questions <at> erlang.org" target="_blank">erlang-questions <at> erlang.org</a><br>

&gt; <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>&gt;<br>
</blockquote>
</div>
</div>
<br><br>_______________________________________________<br>
erlang-questions mailing list<br><a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> erlang.org</a><br><a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote>
</div>
<br>
</div></div>
Edwin Fine | 15 Aug 22:29

Re: How to get the line number of current executable code?

Something I've wished for in numerous languages over the years is a macro that expands to the current function, something like ?MODULE. I don't suppose there is one lurking somewhere in Erlang...?

2008/8/15 Matt Williamson <dawsdesign <at> gmail.com>
You should trust the macro. It must use a similar method to Python's because they are both compiled to bytecode and thus there wouldn't *really* be line numbers in either one.

2008/8/15 devdoer bird <devdoer2 <at> gmail.com>
Thanks.
 
How can I do this without macro? I know python supply some tools to determine the line number in run time,like inspect module.

 
2008/8/16, Anders Nygren <anders.nygren <at> gmail.com>:
2008/8/15 devdoer bird <devdoer2 <at> gmail.com>:
> HI:
>
> I want to implement a function like "get_current_lineno()/0" to get the
> current line number of the calling point?
> Eg.
> .....
> ....
> io:format("current line is ~w\n",[get_current_lineno()])
> .....
>
> the above code will print the line number of the calling point in the source
> file.
>
> How can I do this  in erlang?

There is a predefined macro ?LINE that does that
so
io:format("current line is ~w\n",[?LINE])

/Anders

> _______________________________________________
> erlang-questions mailing list
> erlang-questions <at> erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
>


_______________________________________________
erlang-questions mailing list
erlang-questions <at> erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
erlang-questions <at> erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions



--
For every expert there is an equal and opposite expert - Arthur C. Clarke
<div><div dir="ltr">Something I've wished for in numerous languages over the years is a macro that expands to the current function, something like ?MODULE. I don't suppose there is one lurking somewhere in Erlang...?<br><br><div class="gmail_quote">2008/8/15 Matt Williamson <span dir="ltr">&lt;<a href="mailto:dawsdesign <at> gmail.com">dawsdesign <at> gmail.com</a>&gt;</span><br><blockquote class="gmail_quote">
<div dir="ltr">You should trust the macro. It must use a similar method to Python's because they are both compiled to bytecode and thus there wouldn't *really* be line numbers in either one. <br><div class="Ih2E3d">
<br><div class="gmail_quote">
2008/8/15 devdoer bird <span dir="ltr">&lt;<a href="mailto:devdoer2 <at> gmail.com" target="_blank">devdoer2 <at> gmail.com</a>&gt;</span><br><blockquote class="gmail_quote">

<div>Thanks.</div>
<div>&nbsp;</div>
<div>How can I do this without macro? I know python supply some tools to determine the line number in run time,like inspect module. <br><br>&nbsp;</div>
<div>
<span class="gmail_quote">2008/8/16, Anders Nygren &lt;<a href="mailto:anders.nygren <at> gmail.com" target="_blank">anders.nygren <at> gmail.com</a>&gt;:</span><div>
<blockquote class="gmail_quote">2008/8/15 devdoer bird &lt;<a href="mailto:devdoer2 <at> gmail.com" target="_blank">devdoer2 <at> gmail.com</a>&gt;:<br>

&gt; HI:<br>
&gt;<br>&gt; I want to implement a function like "get_current_lineno()/0" to get the<br>&gt; current line number of the calling point?<br>&gt; Eg.<br>&gt; .....<br>&gt; ....<br>&gt; io:format("current line is ~w\n",[get_current_lineno()])<br>

&gt; .....<br>&gt;<br>&gt; the above code will print the line number of the calling point in the source<br>&gt; file.<br>&gt;<br>&gt; How can I do this&nbsp;&nbsp;in erlang?<br><br>There is a predefined macro ?LINE that does that<br>

so<br>io:format("current line is ~w\n",[?LINE])<br><br>/Anders<br><br>&gt; _______________________________________________<br>&gt; erlang-questions mailing list<br>&gt; <a href="mailto:erlang-questions <at> erlang.org" target="_blank">erlang-questions <at> erlang.org</a><br>

&gt; <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>&gt;<br>
</blockquote>
</div>
</div>
<br><br>_______________________________________________<br>
erlang-questions mailing list<br><a href="mailto:erlang-questions <at> erlang.org" target="_blank">erlang-questions <at> erlang.org</a><br><a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote>
</div>
<br>
</div>
</div>
<br>_______________________________________________<br>
erlang-questions mailing list<br><a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> erlang.org</a><br><a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote>
</div>
<br><br clear="all"><br>-- <br>For every expert there is an equal and opposite expert - Arthur C. Clarke<br>
</div></div>
Ulf Wiger | 15 Aug 23:06

Re: How to get the line number of current executable code?

Why sure, you just do this:

-module(m).
-export([f/0]).

-define(FUNCTION, hd(element(2,element(2,catch erlang:error([]))))).

f() ->
  {current_function, ?FUNCTION}.

Eshell V5.5.4  (abort with ^G)
1> c(m).
{ok,m}
2> m:f().
{current_function,{m,f,0}}
3>

BR,
Ulf W  ;-)

2008/8/15 Edwin Fine <erlang-questions_efine <at> usa.net>:
> Something I've wished for in numerous languages over the years is a macro
> that expands to the current function, something like ?MODULE. I don't
> suppose there is one lurking somewhere in Erlang...?
>
> 2008/8/15 Matt Williamson <dawsdesign <at> gmail.com>
>>
>> You should trust the macro. It must use a similar method to Python's
>> because they are both compiled to bytecode and thus there wouldn't *really*
>> be line numbers in either one.
>>
>> 2008/8/15 devdoer bird <devdoer2 <at> gmail.com>
>>>
>>> Thanks.
>>>
>>> How can I do this without macro? I know python supply some tools to
>>> determine the line number in run time,like inspect module.
>>>
>>>
>>> 2008/8/16, Anders Nygren <anders.nygren <at> gmail.com>:
>>>>
>>>> 2008/8/15 devdoer bird <devdoer2 <at> gmail.com>:
>>>> > HI:
>>>> >
>>>> > I want to implement a function like "get_current_lineno()/0" to get
>>>> > the
>>>> > current line number of the calling point?
>>>> > Eg.
>>>> > .....
>>>> > ....
>>>> > io:format("current line is ~w\n",[get_current_lineno()])
>>>> > .....
>>>> >
>>>> > the above code will print the line number of the calling point in the
>>>> > source
>>>> > file.
>>>> >
>>>> > How can I do this  in erlang?
>>>>
>>>> There is a predefined macro ?LINE that does that
>>>> so
>>>> io:format("current line is ~w\n",[?LINE])
>>>>
>>>> /Anders
>>>>
>>>> > _______________________________________________
>>>> > erlang-questions mailing list
>>>> > erlang-questions <at> erlang.org
>>>> > http://www.erlang.org/mailman/listinfo/erlang-questions
>>>> >
>>>
>>>
>>> _______________________________________________
>>> erlang-questions mailing list
>>> erlang-questions <at> erlang.org
>>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions <at> erlang.org
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>
>
>
> --
> For every expert there is an equal and opposite expert - Arthur C. Clarke
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions <at> erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
Edwin Fine | 15 Aug 23:48

Re: How to get the line number of current executable code?

How cool is that?! Thanks!

On Fri, Aug 15, 2008 at 5:06 PM, Ulf Wiger <ulf <at> wiger.net> wrote:
Why sure, you just do this:

-module(m).
-export([f/0]).

-define(FUNCTION, hd(element(2,element(2,catch erlang:error([]))))).

f() ->
 {current_function, ?FUNCTION}.



Eshell V5.5.4  (abort with ^G)
1> c(m).
{ok,m}
2> m:f().
{current_function,{m,f,0}}
3>

BR,
Ulf W  ;-)

2008/8/15 Edwin Fine <erlang-questions_efine <at> usa.net>:
> Something I've wished for in numerous languages over the years is a macro
> that expands to the current function, something like ?MODULE. I don't
> suppose there is one lurking somewhere in Erlang...?
>
> 2008/8/15 Matt Williamson <dawsdesign <at> gmail.com>
>>
>> You should trust the macro. It must use a similar method to Python's
>> because they are both compiled to bytecode and thus there wouldn't *really*
>> be line numbers in either one.
>>
>> 2008/8/15 devdoer bird <devdoer2 <at> gmail.com>
>>>
>>> Thanks.
>>>
>>> How can I do this without macro? I know python supply some tools to
>>> determine the line number in run time,like inspect module.
>>>
>>>
>>> 2008/8/16, Anders Nygren <anders.nygren <at> gmail.com>:
>>>>
>>>> 2008/8/15 devdoer bird <devdoer2 <at> gmail.com>:
>>>> > HI:
>>>> >
>>>> > I want to implement a function like "get_current_lineno()/0" to get
>>>> > the
>>>> > current line number of the calling point?
>>>> > Eg.
>>>> > .....
>>>> > ....
>>>> > io:format("current line is ~w\n",[get_current_lineno()])
>>>> > .....
>>>> >
>>>> > the above code will print the line number of the calling point in the
>>>> > source
>>>> > file.
>>>> >
>>>> > How can I do this  in erlang?
>>>>
>>>> There is a predefined macro ?LINE that does that
>>>> so
>>>> io:format("current line is ~w\n",[?LINE])
>>>>
>>>> /Anders
>>>>
>>>> > _______________________________________________
>>>> > erlang-questions mailing list
>>>> > erlang-questions <at> erlang.org
>>>> > http://www.erlang.org/mailman/listinfo/erlang-questions
>>>> >
>>>
>>>
>>> _______________________________________________
>>> erlang-questions mailing list
>>> erlang-questions <at> erlang.org
>>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions <at> erlang.org
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>
>
>
> --
> For every expert there is an equal and opposite expert - Arthur C. Clarke
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions <at> erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
>




--
For every expert there is an equal and opposite expert - Arthur C. Clarke
<div><div dir="ltr">How cool is that?! Thanks!<br><br><div class="gmail_quote">On Fri, Aug 15, 2008 at 5:06 PM, Ulf Wiger <span dir="ltr">&lt;<a href="mailto:ulf <at> wiger.net">ulf <at> wiger.net</a>&gt;</span> wrote:<br><blockquote class="gmail_quote">
Why sure, you just do this:<br><br>
-module(m).<br>
-export([f/0]).<br><br>
-define(FUNCTION, hd(element(2,element(2,catch erlang:error([]))))).<br><br>
f() -&gt;<br>
 &nbsp;{current_function, ?FUNCTION}.<br><br><br><br>
Eshell V5.5.4 &nbsp;(abort with ^G)<br>
1&gt; c(m).<br>
{ok,m}<br>
2&gt; m:f().<br>
{current_function,{m,f,0}}<br>
3&gt;<br><br>
BR,<br>
Ulf W &nbsp;;-)<br><br>
2008/8/15 Edwin Fine &lt;<a href="mailto:erlang-questions_efine <at> usa.net">erlang-questions_efine <at> usa.net</a>&gt;:<br><div>
<div></div>
<div class="Wj3C7c">&gt; Something I've wished for in numerous languages over the years is a macro<br>
&gt; that expands to the current function, something like ?MODULE. I don't<br>
&gt; suppose there is one lurking somewhere in Erlang...?<br>
&gt;<br>
&gt; 2008/8/15 Matt Williamson &lt;<a href="mailto:dawsdesign <at> gmail.com">dawsdesign <at> gmail.com</a>&gt;<br>
&gt;&gt;<br>
&gt;&gt; You should trust the macro. It must use a similar method to Python's<br>
&gt;&gt; because they are both compiled to bytecode and thus there wouldn't *really*≤br>
&gt;&gt; be line numbers in either one.<br>
&gt;&gt;<br>
&gt;&gt; 2008/8/15 devdoer bird &lt;<a href="mailto:devdoer2 <at> gmail.com">devdoer2 <at> gmail.com</a>&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Thanks.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; How can I do this without macro? I know python supply some tools to<br>
&gt;&gt;&gt; determine the line number in run time,like inspect module.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; 2008/8/16, Anders Nygren &lt;<a href="mailto:anders.nygren <at> gmail.com">anders.nygren <at> gmail.com</a>&gt;:<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; 2008/8/15 devdoer bird &lt;<a href="mailto:devdoer2 <at> gmail.com">devdoer2 <at> gmail.com</a>&gt;:<br>
&gt;&gt;&gt;&gt; &gt; HI:<br>
&gt;&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt;&gt; &gt; I want to implement a function like "get_current_lineno()/0" to get<br>
&gt;&gt;&gt;&gt; &gt; the<br>
&gt;&gt;&gt;&gt; &gt; current line number of the calling point?<br>
&gt;&gt;&gt;&gt; &gt; Eg.<br>
&gt;&gt;&gt;&gt; &gt; .....<br>
&gt;&gt;&gt;&gt; &gt; ....<br>
&gt;&gt;&gt;&gt; &gt; io:format("current line is ~w\n",[get_current_lineno()])<br>
&gt;&gt;&gt;&gt; &gt; .....<br>
&gt;&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt;&gt; &gt; the above code will print the line number of the calling point in the<br>
&gt;&gt;&gt;&gt; &gt; source<br>
&gt;&gt;&gt;&gt; &gt; file.<br>
&gt;&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt;&gt; &gt; How can I do this &nbsp;in erlang?<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; There is a predefined macro ?LINE that does that<br>
&gt;&gt;&gt;&gt; so<br>
&gt;&gt;&gt;&gt; io:format("current line is ~w\n",[?LINE])<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; /Anders<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; &gt; _______________________________________________<br>
&gt;&gt;&gt;&gt; &gt; erlang-questions mailing list<br>
&gt;&gt;&gt;&gt; &gt; <a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> erlang.org</a><br>
&gt;&gt;&gt;&gt; &gt; <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
&gt;&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt; erlang-questions mailing list<br>
&gt;&gt;&gt; <a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> erlang.org</a><br>
&gt;&gt;&gt; <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; erlang-questions mailing list<br>
&gt;&gt; <a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> erlang.org</a><br>
&gt;&gt; <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; For every expert there is an equal and opposite expert - Arthur C. Clarke<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; erlang-questions mailing list<br>
&gt; <a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> erlang.org</a><br>
&gt; <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
&gt;<br><br>
</div>
</div>
</blockquote>
</div>
<br><br clear="all"><br>-- <br>For every expert there is an equal and opposite expert - Arthur C. Clarke<br>
</div></div>
Edwin Fine | 16 Aug 19:59

Re: How to get the line number of current executable code?

Ulf,

On second thoughts, although that IS very cool, it would likely be very expensive because it generates and catches an exception. This would matter if it is used to do a lot of logging (which is what I would use it for). What I really would like is a compile-time constant (pre-processor macro). I suppose one could hack epp...?

Ed

On Fri, Aug 15, 2008 at 5:06 PM, Ulf Wiger <ulf <at> wiger.net> wrote:
Why sure, you just do this:

-module(m).
-export([f/0]).

-define(FUNCTION, hd(element(2,element(2,catch erlang:error([]))))).

f() ->
 {current_function, ?FUNCTION}.



Eshell V5.5.4  (abort with ^G)
1> c(m).
{ok,m}
2> m:f().
{current_function,{m,f,0}}
3>

BR,
Ulf W  ;-)

2008/8/15 Edwin Fine <erlang-questions_efine <at> usa.net>:
> Something I've wished for in numerous languages over the years is a macro
> that expands to the current function, something like ?MODULE. I don't
> suppose there is one lurking somewhere in Erlang...?
>
> 2008/8/15 Matt Williamson <dawsdesign <at> gmail.com>
>>
>> You should trust the macro. It must use a similar method to Python's
>> because they are both compiled to bytecode and thus there wouldn't *really*
>> be line numbers in either one.
>>
>> 2008/8/15 devdoer bird <devdoer2 <at> gmail.com>
>>>
>>> Thanks.
>>>
>>> How can I do this without macro? I know python supply some tools to
>>> determine the line number in run time,like inspect module.
>>>
>>>
>>> 2008/8/16, Anders Nygren <anders.nygren <at> gmail.com>:
>>>>
>>>> 2008/8/15 devdoer bird <devdoer2 <at> gmail.com>:
>>>> > HI:
>>>> >
>>>> > I want to implement a function like "get_current_lineno()/0" to get
>>>> > the
>>>> > current line number of the calling point?
>>>> > Eg.
>>>> > .....
>>>> > ....
>>>> > io:format("current line is ~w\n",[get_current_lineno()])
>>>> > .....
>>>> >
>>>> > the above code will print the line number of the calling point in the
>>>> > source
>>>> > file.
>>>> >
>>>> > How can I do this  in erlang?
>>>>
>>>> There is a predefined macro ?LINE that does that
>>>> so
>>>> io:format("current line is ~w\n",[?LINE])
>>>>
>>>> /Anders
>>>>
>>>> > _______________________________________________
>>>> > erlang-questions mailing list
>>>> > erlang-questions <at> erlang.org
>>>> > http://www.erlang.org/mailman/listinfo/erlang-questions
>>>> >
>>>
>>>
>>> _______________________________________________
>>> erlang-questions mailing list
>>> erlang-questions <at> erlang.org
>>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions <at> erlang.org
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>
>
>
> --
> For every expert there is an equal and opposite expert - Arthur C. Clarke
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions <at> erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
>




--
For every expert there is an equal and opposite expert - Arthur C. Clarke
<div><div dir="ltr">Ulf,<br><br>On second thoughts, although that IS very cool, it would likely be very expensive because it generates and catches an exception. This would matter if it is used to do a lot of logging (which is what I would use it for). What I really would like is a compile-time constant (pre-processor macro). I suppose one could hack epp...?<br><br>Ed<br><br><div class="gmail_quote">On Fri, Aug 15, 2008 at 5:06 PM, Ulf Wiger <span dir="ltr">&lt;<a href="mailto:ulf <at> wiger.net">ulf <at> wiger.net</a>&gt;</span> wrote:<br><blockquote class="gmail_quote">
Why sure, you just do this:<br><br>
-module(m).<br>
-export([f/0]).<br><br>
-define(FUNCTION, hd(element(2,element(2,catch erlang:error([]))))).<br><br>
f() -&gt;<br>
 &nbsp;{current_function, ?FUNCTION}.<br><br><br><br>
Eshell V5.5.4 &nbsp;(abort with ^G)<br>
1&gt; c(m).<br>
{ok,m}<br>
2&gt; m:f().<br>
{current_function,{m,f,0}}<br>
3&gt;<br><br>
BR,<br>
Ulf W &nbsp;;-)<br><br>
2008/8/15 Edwin Fine &lt;<a href="mailto:erlang-questions_efine <at> usa.net">erlang-questions_efine <at> usa.net</a>&gt;:<br><div>
<div></div>
<div class="Wj3C7c">&gt; Something I've wished for in numerous languages over the years is a macro<br>
&gt; that expands to the current function, something like ?MODULE. I don't<br>
&gt; suppose there is one lurking somewhere in Erlang...?<br>
&gt;<br>
&gt; 2008/8/15 Matt Williamson &lt;<a href="mailto:dawsdesign <at> gmail.com">dawsdesign <at> gmail.com</a>&gt;<br>
&gt;&gt;<br>
&gt;&gt; You should trust the macro. It must use a similar method to Python's<br>
&gt;&gt; because they are both compiled to bytecode and thus there wouldn't *really*≤br>
&gt;&gt; be line numbers in either one.<br>
&gt;&gt;<br>
&gt;&gt; 2008/8/15 devdoer bird &lt;<a href="mailto:devdoer2 <at> gmail.com">devdoer2 <at> gmail.com</a>&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Thanks.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; How can I do this without macro? I know python supply some tools to<br>
&gt;&gt;&gt; determine the line number in run time,like inspect module.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; 2008/8/16, Anders Nygren &lt;<a href="mailto:anders.nygren <at> gmail.com">anders.nygren <at> gmail.com</a>&gt;:<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; 2008/8/15 devdoer bird &lt;<a href="mailto:devdoer2 <at> gmail.com">devdoer2 <at> gmail.com</a>&gt;:<br>
&gt;&gt;&gt;&gt; &gt; HI:<br>
&gt;&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt;&gt; &gt; I want to implement a function like "get_current_lineno()/0" to get<br>
&gt;&gt;&gt;&gt; &gt; the<br>
&gt;&gt;&gt;&gt; &gt; current line number of the calling point?<br>
&gt;&gt;&gt;&gt; &gt; Eg.<br>
&gt;&gt;&gt;&gt; &gt; .....<br>
&gt;&gt;&gt;&gt; &gt; ....<br>
&gt;&gt;&gt;&gt; &gt; io:format("current line is ~w\n",[get_current_lineno()])<br>
&gt;&gt;&gt;&gt; &gt; .....<br>
&gt;&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt;&gt; &gt; the above code will print the line number of the calling point in the<br>
&gt;&gt;&gt;&gt; &gt; source<br>
&gt;&gt;&gt;&gt; &gt; file.<br>
&gt;&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt;&gt; &gt; How can I do this &nbsp;in erlang?<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; There is a predefined macro ?LINE that does that<br>
&gt;&gt;&gt;&gt; so<br>
&gt;&gt;&gt;&gt; io:format("current line is ~w\n",[?LINE])<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; /Anders<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; &gt; _______________________________________________<br>
&gt;&gt;&gt;&gt; &gt; erlang-questions mailing list<br>
&gt;&gt;&gt;&gt; &gt; <a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> erlang.org</a><br>
&gt;&gt;&gt;&gt; &gt; <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
&gt;&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt; erlang-questions mailing list<br>
&gt;&gt;&gt; <a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> erlang.org</a><br>
&gt;&gt;&gt; <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; erlang-questions mailing list<br>
&gt;&gt; <a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> erlang.org</a><br>
&gt;&gt; <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; For every expert there is an equal and opposite expert - Arthur C. Clarke<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; erlang-questions mailing list<br>
&gt; <a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> erlang.org</a><br>
&gt; <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
&gt;<br><br>
</div>
</div>
</blockquote>
</div>
<br><br clear="all"><br>-- <br>For every expert there is an equal and opposite expert - Arthur C. Clarke<br>
</div></div>
Richard Carlsson | 16 Aug 20:22
Favicon

Re: How to get the line number of current executable code?

Edwin Fine wrote:
> On second thoughts, although that IS very cool, it would likely be very
> expensive because it generates and catches an exception. This would
> matter if it is used to do a lot of logging (which is what I would use
> it for). What I really would like is a compile-time constant
> (pre-processor macro). I suppose one could hack epp...?

Don't worry about that. Exception handling is not particularly
expensive in Erlang. (Certainly not compared to the work of logging.)

    /Richard
Ulf Wiger | 17 Aug 11:19

Re: How to get the line number of current executable code?

I should perhaps bring special notation to the smiley at the
end of my message. (:

Whether or not the solution is cool is surely a matter of
taste - but I believe that the one who first came up with it
was Mats Cronqvist, the champion of obfuscated Erlang
code. (:

I'm not sure about *very* expensive. The exception mechanism
isn't that heavyweight. At least in the past, there were issues
with HiPE, in that exceptions in native code would generate an
empty stack trace. I vaguely recall that being fixed now.

I agree that a compile-time constant would be much preferred.

BR,
Ulf W

2008/8/16 Edwin Fine <erlang-questions_efine <at> usa.net>:
> Ulf,
>
> On second thoughts, although that IS very cool, it would likely be very
> expensive because it generates and catches an exception. This would matter
> if it is used to do a lot of logging (which is what I would use it for).
> What I really would like is a compile-time constant (pre-processor macro). I
> suppose one could hack epp...?
>
> Ed
>
> On Fri, Aug 15, 2008 at 5:06 PM, Ulf Wiger <ulf <at> wiger.net> wrote:
>>
>> Why sure, you just do this:
>>
>> -module(m).
>> -export([f/0]).
>>
>> -define(FUNCTION, hd(element(2,element(2,catch erlang:error([]))))).
>>
>> f() ->
>>  {current_function, ?FUNCTION}.
>>
>>
>>
>> Eshell V5.5.4  (abort with ^G)
>> 1> c(m).
>> {ok,m}
>> 2> m:f().
>> {current_function,{m,f,0}}
>> 3>
>>
>> BR,
>> Ulf W  ;-)
>>
>> 2008/8/15 Edwin Fine <erlang-questions_efine <at> usa.net>:
>> > Something I've wished for in numerous languages over the years is a
>> > macro
>> > that expands to the current function, something like ?MODULE. I don't
>> > suppose there is one lurking somewhere in Erlang...?
>> >
>> > 2008/8/15 Matt Williamson <dawsdesign <at> gmail.com>
>> >>
>> >> You should trust the macro. It must use a similar method to Python's
>> >> because they are both compiled to bytecode and thus there wouldn't
>> >> *really*
>> >> be line numbers in either one.
>> >>
>> >> 2008/8/15 devdoer bird <devdoer2 <at> gmail.com>
>> >>>
>> >>> Thanks.
>> >>>
>> >>> How can I do this without macro? I know python supply some tools to
>> >>> determine the line number in run time,like inspect module.
>> >>>
>> >>>
>> >>> 2008/8/16, Anders Nygren <anders.nygren <at> gmail.com>:
>> >>>>
>> >>>> 2008/8/15 devdoer bird <devdoer2 <at> gmail.com>:
>> >>>> > HI:
>> >>>> >
>> >>>> > I want to implement a function like "get_current_lineno()/0" to get
>> >>>> > the
>> >>>> > current line number of the calling point?
>> >>>> > Eg.
>> >>>> > .....
>> >>>> > ....
>> >>>> > io:format("current line is ~w\n",[get_current_lineno()])
>> >>>> > .....
>> >>>> >
>> >>>> > the above code will print the line number of the calling point in
>> >>>> > the
>> >>>> > source
>> >>>> > file.
>> >>>> >
>> >>>> > How can I do this  in erlang?
>> >>>>
>> >>>> There is a predefined macro ?LINE that does that
>> >>>> so
>> >>>> io:format("current line is ~w\n",[?LINE])
>> >>>>
>> >>>> /Anders
>> >>>>
>> >>>> > _______________________________________________
>> >>>> > erlang-questions mailing list
>> >>>> > erlang-questions <at> erlang.org
>> >>>> > http://www.erlang.org/mailman/listinfo/erlang-questions
>> >>>> >
>> >>>
>> >>>
>> >>> _______________________________________________
>> >>> erlang-questions mailing list
>> >>> erlang-questions <at> erlang.org
>> >>> http://www.erlang.org/mailman/listinfo/erlang-questions
>> >>
>> >>
>> >> _______________________________________________
>> >> erlang-questions mailing list
>> >> erlang-questions <at> erlang.org
>> >> http://www.erlang.org/mailman/listinfo/erlang-questions
>> >
>> >
>> >
>> > --
>> > For every expert there is an equal and opposite expert - Arthur C.
>> > Clarke
>> >
>> > _______________________________________________
>> > erlang-questions mailing list
>> > erlang-questions <at> erlang.org
>> > http://www.erlang.org/mailman/listinfo/erlang-questions
>> >
>>
>
>
>
> --
> For every expert there is an equal and opposite expert - Arthur C. Clarke
>
Andras Georgy Bekes | 18 Aug 16:00

Re: How to get the line number of current executable code?

> -define(FUNCTION, hd(element(2,element(2,catch erlang:error([]))))).
A safer solution (it has no side effects, unlike the above):

-define(FUNCTION,element(2,process_info(self(),current_function))).

I don't know if it's more expensive or not, but if it does matter, 
you're doing something wrong.

	Georgy
Mats Cronqvist | 25 Aug 08:57

Re: How to get the line number of current executable code?

Ulf Wiger wrote:
> I should perhaps bring special notation to the smiley at the
> end of my message. (:
>
> Whether or not the solution is cool is surely a matter of
> taste - but I believe that the one who first came up with it
> was Mats Cronqvist, the champion of obfuscated Erlang
> code. (:
>   
  thanks for the endorsement...

  for the record, i have since moved on to using this:
-define(position,[process_info(self(),current_function),{line,?LINE}]).

  sometimes adding this:
proplists:lookup(source,?MODULE:module_info(compile)).

  mats
Ahmed Ali | 26 Aug 01:34

Re: How to get the line number of current executable code?

Hi All,

This discussion is interesting. Just wanted to know if it is possible
to get the function that called current function. Is this possible?

Best regards,

Ahmed Al-Issaei

On Mon, Aug 25, 2008 at 10:57 AM, Mats Cronqvist
<mats.cronqvist <at> gmail.com> wrote:
> Ulf Wiger wrote:
>> I should perhaps bring special notation to the smiley at the
>> end of my message. (:
>>
>> Whether or not the solution is cool is surely a matter of
>> taste - but I believe that the one who first came up with it
>> was Mats Cronqvist, the champion of obfuscated Erlang
>> code. (:
>>
>  thanks for the endorsement...
>
>  for the record, i have since moved on to using this:
> -define(position,[process_info(self(),current_function),{line,?LINE}]).
>
>  sometimes adding this:
> proplists:lookup(source,?MODULE:module_info(compile)).
>
>  mats
> _______________________________________________
> erlang-questions mailing list
> erlang-questions <at> erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
Richard Carlsson | 26 Aug 08:58
Favicon

Re: How to get the line number of current executable code?

Ahmed Ali wrote:
> Hi All,
> 
> This discussion is interesting. Just wanted to know if it is possible
> to get the function that called current function. Is this possible?

The only way of doing that is to force an exception, and then look
at the stack trace. Something like this:

  Trace = try throw(oops) catch oops -> erlang:get_stacktrace() end

However, due to tail call optimization in Erlang, you will not see
any functions in the list that were visited on the way but were exited
through a tail call.

    /Richard
Mats Cronqvist | 26 Aug 10:54

Re: How to get the line number of current executable code?

Richard Carlsson wrote:
> Ahmed Ali wrote:
>   
>> Hi All,
>>
>> This discussion is interesting. Just wanted to know if it is possible
>> to get the function that called current function. Is this possible?
>>     
>
> The only way of doing that is to force an exception, and then look
> at the stack trace. 

  there is no way to reliably get the name of the calling function.
  it is true that the stack sometimes contain the name of the calling 
function.
  it is not true that the only way to inspect the stack is by forcing an 
exception.

  i'm too lazy to write actual code, but try this in the shell;

(r12 <at> sterlett)41> {_,BT}=erlang:process_info(self(),backtrace).
(r12 <at> sterlett)42> S=fun(B,{_,[_,{F,L}|_]})-> 
<<_:F/binary,R:L/binary,_/binary>> =B, R end.
(r12 <at> sterlett)43> S(BT,re:run(BT,"Return addr\\s[0-9a-z]+\\s\\(([^\\s]*)")).
<<"erl_eval:expr/5">>

  clear as the day!

  mats
Richard Carlsson | 26 Aug 11:15
Favicon

Re: How to get the line number of current executable code?

Mats Cronqvist wrote:
>  it is not true that the only way to inspect the stack is by forcing an
> exception.
> 
>  i'm too lazy to write actual code, but try this in the shell;
> 
> (r12 <at> sterlett)41> {_,BT}=erlang:process_info(self(),backtrace).
> (r12 <at> sterlett)42> S=fun(B,{_,[_,{F,L}|_]})->
> <<_:F/binary,R:L/binary,_/binary>> =B, R end.
> (r12 <at> sterlett)43> S(BT,re:run(BT,"Return
> addr\\s[0-9a-z]+\\s\\(([^\\s]*)")).
> <<"erl_eval:expr/5">>

Ugh! I didn't know that flag to process_info existed, but I'm happy to
see that Mats' talent for sniffing out the dark corners of Erlang and
using them for unspeakable deeds is as strong as ever.

    /Richard

Ahmed Ali | 26 Aug 21:05

Re: How to get the line number of current executable code?

Hi Mats,

I've seen process_display(Pid, backtrace) but never thought to use it
this way. Thanks for the insight.

Best regards,

Ahmed Al-Issaei

On Tue, Aug 26, 2008 at 12:54 PM, Mats Cronqvist
<mats.cronqvist <at> gmail.com> wrote:
> Richard Carlsson wrote:
>>
>> Ahmed Ali wrote:
>>
>>>
>>> Hi All,
>>>
>>> This discussion is interesting. Just wanted to know if it is possible
>>> to get the function that called current function. Is this possible?
>>>
>>
>> The only way of doing that is to force an exception, and then look
>> at the stack trace.
>
>  there is no way to reliably get the name of the calling function.
>  it is true that the stack sometimes contain the name of the calling
> function.
>  it is not true that the only way to inspect the stack is by forcing an
> exception.
>
>  i'm too lazy to write actual code, but try this in the shell;
>
> (r12 <at> sterlett)41> {_,BT}=erlang:process_info(self(),backtrace).
> (r12 <at> sterlett)42> S=fun(B,{_,[_,{F,L}|_]})->
> <<_:F/binary,R:L/binary,_/binary>> =B, R end.
> (r12 <at> sterlett)43> S(BT,re:run(BT,"Return addr\\s[0-9a-z]+\\s\\(([^\\s]*)")).
> <<"erl_eval:expr/5">>
>
>  clear as the day!
>
>  mats
>
Bengt Kleberg | 26 Aug 08:58
Favicon

Re: How to get the line number of current executable code?

Greetings,

Building upon the suggestion made by Ulf Wiger this would probably work
(possible reasons for not working includes, but are not limited to, tail
calls):
-define(FUNCTION_THAT_CALLED, lists:nth(2, element(2,element(2,catch
erlang:error([]))))).

bengt

On Tue, 2008-08-26 at 03:34 +0400, Ahmed Ali wrote:
> Hi All,
> 
> This discussion is interesting. Just wanted to know if it is possible
> to get the function that called current function. Is this possible?
> 
> Best regards,
> 
> Ahmed Al-Issaei
> 
> On Mon, Aug 25, 2008 at 10:57 AM, Mats Cronqvist
> <mats.cronqvist <at> gmail.com> wrote:
> > Ulf Wiger wrote:
> >> I should perhaps bring special notation to the smiley at the
> >> end of my message. (:
> >>
> >> Whether or not the solution is cool is surely a matter of
> >> taste - but I believe that the one who first came up with it
> >> was Mats Cronqvist, the champion of obfuscated Erlang
> >> code. (:
> >>
> >  thanks for the endorsement...
> >
> >  for the record, i have since moved on to using this:
> > -define(position,[process_info(self(),current_function),{line,?LINE}]).
> >
> >  sometimes adding this:
> > proplists:lookup(source,?MODULE:module_info(compile)).
> >
> >  mats
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions <at> erlang.org
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
> _______________________________________________
> erlang-questions mailing list
> erlang-questions <at> erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions

Thomas Lindgren | 17 Aug 12:01
Favicon

Re: How to get the line number of current executable code?


--- On Sat, 8/16/08, Edwin Fine <erlang-questions_efine <at> usa.net> wrote:

> On second thoughts, although that IS very cool, it would
> likely be very
> expensive because it generates and catches an exception.
> This would matter
> if it is used to do a lot of logging (which is what I would
> use it for).
> What I really would like is a compile-time constant
> (pre-processor macro). I
> suppose one could hack epp...?

The basic problem is that epp works on the token level, not the syntax tree level, so when you find ?FUNCTION
there is no information about what function you're in. (And figuring it out seems like a thankless job.) 

One possible hack: expand ?FUNCTION into something that can be recognized and replaced by a later pass. For instance:

1. In epp, replace ?FUNCTION by the syntax tree or tokens of a call to a made-up function,
"erlang:current_function()", or some other unique well-known marker.

2. In sys_pre_expand, walk the syntax tree for each function M:F/N, and replace all occurrences of
"erlang:current_function()" with {M, F, N}. (As part of expanding away records, etc.)

Occurrences of ?FUNCTION outside of functions will have to be handled too, e.g. by an error.

Best,
Thomas

      
Richard A. O'Keefe | 18 Aug 01:58
Favicon

Re: How to get the line number of current executable code?

On 17 Aug 2008, at 10:01 pm, Thomas Lindgren wrote:
> The basic problem is that epp works on the token level, not the  
> syntax tree level, so when you find ?FUNCTION there is no  
> information about what function you're in. (And figuring it out  
> seems like a thankless job.)
>
I note that while __FILE__, __LINE__, __DATE__, __TIME__ and so on are
macros in C, __func__ is not.  It is a keyword.  So the solution that
Thomas Lindgren proposes seems good.  I would prefer one mechanism that
solves several issues, so I suggest that instead of ?FUNCTION the thing
to have is ?LOCATION
  => {File, Line, Module, Function, Arity, Clause_Number}

Kevin Scaldeferri | 15 Aug 22:35

Re: How to get the line number of current executable code?

Many languages are compiled to byte code, but able to recover line  
numbers at run time.  I assume they annotate the byte codes with file  
and line information.  Note that this is more powerful than the Erlang  
macros.  For example, Perl's caller() function allows you to inspect  
the full call stack in this way.

-kevin

On Aug 15, 2008, at 12:07 PM, Matt Williamson wrote:

> You should trust the macro. It must use a similar method to Python's  
> because they are both compiled to bytecode and thus there wouldn't  
> *really* be line numbers in either one.
>
> 2008/8/15 devdoer bird <devdoer2 <at> gmail.com>
> Thanks.
>
> How can I do this without macro? I know python supply some tools to  
> determine the line number in run time,like inspect module.
>
>
> 2008/8/16, Anders Nygren <anders.nygren <at> gmail.com>:
> 2008/8/15 devdoer bird <devdoer2 <at> gmail.com>:
> > HI:
> >
> > I want to implement a function like "get_current_lineno()/0" to  
> get the
> > current line number of the calling point?
> > Eg.
> > .....
> > ....
> > io:format("current line is ~w\n",[get_current_lineno()])
> > .....
> >
> > the above code will print the line number of the calling point in  
> the source
> > file.
> >
> > How can I do this  in erlang?
>
> There is a predefined macro ?LINE that does that
> so
> io:format("current line is ~w\n",[?LINE])
>
> /Anders
>
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions <at> erlang.org
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions <at> erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions <at> erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions

Richard A. O'Keefe | 18 Aug 02:46
Favicon

Re: How to get the line number of current executable code?


On 16 Aug 2008, at 8:35 am, Kevin Scaldeferri wrote:

> Many languages are compiled to byte code, but able to recover line
> numbers at run time.  I assume they annotate the byte codes with file
> and line information.  Note that this is more powerful than the Erlang
> macros.  For example, Perl's caller() function allows you to inspect
> the full call stack in this way.

There is a tradeoff.
When your compiler does good stuff like constant propagation,
inlining, loop unrolling, loop fusion, &c, it gets hard to make
'the line number' mean anything.  For example, suppose we had

	Ns = [sum(L) || L <- Ls],
	Ds = [length(L) || L <- Ls],
	Aves = [N/D || {N,D} <- lists:zip(Ns, Ds)]

This is the kind of thing the GHC compiler eats for lunch;
assuming Ns and Ds are not used anywhere else it turns into
the equivalent of

	Aves = [sum(L)/length(L) || L <- Ls]

Which line does this correspond to?  ALL of them.
(Actually, I believe GHC will take this a stage further, and inline
and fuse the sum and length loops as well.  In which case we have
code within a single "line" coming from several files.)

The Erlang compiler isn't that smart.  (Yet.  A man can dream.)

One of the reasons that Perl can give you line numbers is that
Perl is to speed what a spent match is to illumination.

Debugging information can be bulky.
For a non-Erlang example, I tried a small Java file.
It has 172 lines, 116 of which count as SLOC.
javac -g:none Calls.java  => 2583 bytes
javac         Calls.java  => 3035 bytes; 452 bytes of line#
javac -g      Calls.java  => 3283 bytes; 248 bytes of other stuff
The Java compiler counted 96 lines it thought worth recording,
so that's 4.71 bytes per line.  (Clearly they are not bothering
to compress this much.)  There are about 1380 bytes of actual code,
so the line number table is about 1/3 as big as the code.

Even assuming that virtual memory is huge and close to free,
line number information should be used rarely enough that you
don't want to mix it in with the instructions; you want to
segregate it somewhere else.

The really interesting thing is that javap basically lists the
line number mappings backwards.  Instead of saying "block [a..z)
of byte code corresponds to line L", it says "line L corresponds
to byte code starting at x".  This suggests a fairly, um,
direct mapping from source code to byte code: inlining,
loop fusion, loop unrolling, &c are left to the JIT.

Now the Erlang compiler isn't as smart as GHC, but it _is_
smarter than that.  With inlining and tail recursion optimisation,
even the stack trace isn't what it seems.

A rather long-winded way of saying TANSTAAFL, I suppose.

Robert Virding | 18 Aug 14:59

Re: How to get the line number of current executable code?

2008/8/18 Richard A. O'Keefe <ok <at> cs.otago.ac.nz>

On 16 Aug 2008, at 8:35 am, Kevin Scaldeferri wrote:

There is a tradeoff.
When your compiler does good stuff like constant propagation,
inlining, loop unrolling, loop fusion, &c, it gets hard to make
'the line number' mean anything.  For example, suppose we had

       Ns = [sum(L) || L <- Ls],
       Ds = [length(L) || L <- Ls],
       Aves = [N/D || {N,D} <- lists:zip(Ns, Ds)]

This is the kind of thing the GHC compiler eats for lunch;
assuming Ns and Ds are not used anywhere else it turns into
the equivalent of

       Aves = [sum(L)/length(L) || L <- Ls]

Which line does this correspond to?  ALL of them.
(Actually, I believe GHC will take this a stage further, and inline
and fuse the sum and length loops as well.  In which case we have
code within a single "line" coming from several files.)

The Erlang compiler isn't that smart.  (Yet.  A man can dream.)

One problem Erlang would have doing this type of optimisation, which would be wonderful to have, is the handling of side effects and errors. That type of aggressive inlining and fusing would change the order in which side effects occur which means that the code would not be equivalent. Similar for errors. Haskell, being side-effect free, does not have this problem.

Robert

<div><div dir="ltr">2008/8/18 Richard A. O'Keefe <span dir="ltr">&lt;<a href="mailto:ok <at> cs.otago.ac.nz" target="_blank">ok <at> cs.otago.ac.nz</a>&gt;</span><br><div class="gmail_quote">
<blockquote class="gmail_quote">

<div>
<br>
On 16 Aug 2008, at 8:35 am, Kevin Scaldeferri wrote:<br><br>
</div>There is a tradeoff.<br>
When your compiler does good stuff like constant propagation,<br>
inlining, loop unrolling, loop fusion, &amp;c, it gets hard to make<br>
'the line number' mean anything. &nbsp;For example, suppose we had<br><br>
 &nbsp; &nbsp; &nbsp; &nbsp;Ns = [sum(L) || L &lt;- Ls],<br>
 &nbsp; &nbsp; &nbsp; &nbsp;Ds = [length(L) || L &lt;- Ls],<br>
 &nbsp; &nbsp; &nbsp; &nbsp;Aves = [N/D || {N,D} &lt;- lists:zip(Ns, Ds)]<br><br>
This is the kind of thing the GHC compiler eats for lunch;<br>
assuming Ns and Ds are not used anywhere else it turns into<br>
the equivalent of<br><br>
 &nbsp; &nbsp; &nbsp; &nbsp;Aves = [sum(L)/length(L) || L &lt;- Ls]<br><br>
Which line does this correspond to? &nbsp;ALL of them.<br>
(Actually, I believe GHC will take this a stage further, and inline<br>
and fuse the sum and length loops as well. &nbsp;In which case we have<br>
code within a single "line" coming from several files.)<br><br>
The Erlang compiler isn't that smart. &nbsp;(Yet. &nbsp;A man can dream.)</blockquote>
<div>
<br>One problem Erlang would have doing this type of optimisation, which would be wonderful to have, is the handling of side effects and errors. That type of aggressive inlining and fusing would change the order in which side effects occur which means that the code would not be equivalent. Similar for errors. Haskell, being side-effect free, does not have this problem.<br><br>Robert<br><br>
</div>
</div>
</div></div>
Richard A. O'Keefe | 19 Aug 02:53
Favicon

Re: How to get the line number of current executable code?


On 19 Aug 2008, at 12:59 am, Robert Virding wrote:

> The Erlang compiler isn't that smart.  (Yet.  A man can dream.)
>
> One problem Erlang would have doing this type of optimisation, which  
> would be wonderful to have, is the handling of side effects and  
> errors.

I have also dreamed of a
  -pure([f/n,...]).
directive, such as NU Prolog had.
This would make a verifiable claim that the function(s) named would
only call functions that are themselves pure, and a promise that if
any exception would be raised in a call to such a function it would
not matter which exception.

However, that seemed off topic.

--
If stupidity were a crime, who'd 'scape hanging?

Andras Georgy Bekes | 19 Aug 17:39

Re: How to get the line number of current executable code?

>   -pure([f/n,...]).
> directive, such as NU Prolog had.
> This would make a verifiable claim that the function(s) named would
> only call functions that are themselves pure
What happens if a pure function calls a function that is not pure?

I mean, the compiler just can't check it. Of course it can check local 
functions, but a function in another module can not be trusted. It 
might be pure at the time of compilation, but it might be impure in its 
next version.

It could only be checked in runtime, throwing an exception whenever an 
impure function is called from a pure one.

	Georgy
Ulf Wiger (TN/EAB | 19 Aug 18:59
Favicon

Re: How to get the line number of current executable code?

Andras Georgy Bekes skrev:
>>   -pure([f/n,...]).
>> directive, such as NU Prolog had.
>> This would make a verifiable claim that the function(s) named would
>> only call functions that are themselves pure
> What happens if a pure function calls a function that is not pure?
> 
> I mean, the compiler just can't check it. Of course it can check local 
> functions, but a function in another module can not be trusted. It 
> might be pure at the time of compilation, but it might be impure in its 
> next version.

Yes, so cross-module calls cannot be allowed in a pure function,
at least unless we find a way to statically group modules together.

BR,
Ulf W
Richard A. O'Keefe | 20 Aug 07:44
Favicon

Re: How to get the line number of current executable code?


On 20 Aug 2008, at 3:39 am, Andras Georgy Bekes wrote:

>>  -pure([f/n,...]).
>> directive, such as NU Prolog had.
>> This would make a verifiable claim that the function(s) named would
>> only call functions that are themselves pure
> What happens if a pure function calls a function that is not pure?
>
> I mean, the compiler just can't check it.

Yes it can.  That's the whole POINT of -pure declarations.
The NU Prolog system *could* and *did* check this.

> Of course it can check local
> functions, but a function in another module can not be trusted.

Cannot be trusted?  Sure it can.  You haven't seen a complete
proposal, because I have other maddened grizzly bears to stun,
but the idea is that
  - the meta-data for a module records which exported functions are
    pure and which are not
  - when the compiler notes a pure function calling a function
    from another module, it records a dependency on that function
    being exported as pure
  - when a module is loaded, the run time system checks that
    every function it tries to import as pure from some other
    module IS pure if that module is already loaded,
    every module that depends on this one exporting some
    function(s) as pure is satisfied.
    If either check fails, the module is not loaded.

This takes care of the "another module" issue.
The tricky one is higher order functions.
I actually came up with this idea back shortly before
'funs' were added to the language, and dropped it when they
came in.  There are basically two ways out: one is to say
that a pure function can't be or call a higher order function,
and the other is to use some sort of type system.

Andras Georgy Bekes | 21 Aug 12:16

Re: How to get the line number of current executable code?

>   - the meta-data for a module records which exported functions are
>     pure and which are not
>   - when the compiler notes a pure function calling a function
>     from another module, it records a dependency on that function
>     being exported as pure
>   - when a module is loaded, the run time system checks that
>     every function it tries to import as pure from some other
>     module IS pure if that module is already loaded,
>     every module that depends on this one exporting some
>     function(s) as pure is satisfied.
>     If either check fails, the module is not loaded.
This is completely against current Erlang practice.

Currently if a module A uses module B, noone checks if B contains the 
used functions or not. Neither at compile time, nor at module load 
time. I think because the check would make module upgrades way more 
difficult.

Think about a module B using a function AF from module A. In the next 
version of the modules, the function A:AF is removed and B is not using 
it any more. Currently you just load the two modules. In the presence 
of a load-time check, you'd have to load module B first, then A. No 
problem here.

Now think about a module A using B's BF and B using A's AF. When both 
functions are removed, there is no legal upgrade order.

The same problem exists with checked pure functions. Module A using B's 
pure BF (and expects it to be pure), and B using A's pure AF (and 
expects it to be pure). In the next version both the functions switch 
to impure, together with their consumers. There is no legal upgrade 
order :-(

There probably are solutions (simultaneous atomic loading of several 
modules? using extra transition module versions to make the upgrade 
possible?), but current module upgrading technology/practice must 
change radically.

	Georgy