determining function name in runtime

S

shyam

Hi all

is there any way the determine which function currently is executing in
a process ?

For example we can find the file name using __FILE__
and line number using __LINE__ macros

simillarly is there any macro which gives the function name

thanks shyam
 
V

Vladimir S. Oka

shyam said:
Hi all

is there any way the determine which function currently is executing in
a process ?

I assume you mean program...
For example we can find the file name using __FILE__
and line number using __LINE__ macros

simillarly is there any macro which gives the function name

Yes, and it's __func__ (note double underscores before and after).
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I assume you mean program...


Yes, and it's __func__ (note double underscores before and after).

Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Thus,
char *where_I_am = "I am at line " __LINE__ "\n";
would work, but
char *where_I_am = "I am in function " __func__ "\n";
would not.

OTOH,
printf("I am at line %d in function %s\n",__LINE__,__func__);
will work.


- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEBwFDagVFX4UWr64RAlQdAJ9XT4qQS65GzFFRPO0RDFqxskDA+wCdGWGd
evG1lEWtpMvGxVdvP/Y97RM=
=vy6r
-----END PGP SIGNATURE-----
 
M

MrG{DRGN}

Lew Pitcher said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Vladimir S. Oka wrote:
-snip

, and it's __func__ (note double underscores before and after).
Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Thus,
char *where_I_am = "I am at line " __LINE__ "\n";
would work, but
char *where_I_am = "I am in function " __func__ "\n";
would not.

OTOH,
printf("I am at line %d in function %s\n",__LINE__,__func__);
will work.

Let me guess, __func__ is a C99 feature?
<OT> (which means I can't use it with my compiler msvc 6.0).<OT>
 
V

Vladimir S. Oka

MrG{DRGN} said:
-snip

, and it's __func__ (note double underscores before and after).

Let me guess, __func__ is a C99 feature?
<OT> (which means I can't use it with my compiler msvc 6.0).<OT>

Yes it is.
<OT>I know nothing about M$VC, but it may provide it as an
extension?<OT>
 
I

Ico

Lew Pitcher said:
Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.


Is there a reason why __func__ is not a macro ?
 
T

tmp123

Ico said:
Is there a reason why __func__ is not a macro ?

It is easy for the preprocessor to know the current file and line.
However, it can not known the function name (even it can not known what
is a function) if it doesn't knowns C syntax (and the preprocessor
doesn't).
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Is there a reason why __func__ is not a macro ?

My copy of the draft C99 standard doesn't give a reason.

My /guess/ is that __FILE__ and __LINE__ can be implemented as macros
because they do not require any interpretation of the C code (they are
strictly derived from the mechanics of reading the source files).

OTOH, __func__ requires the compiler's interpretation of the code in
order to work, so it cannot be implemented in the same manner as __FILE_
and __LINE__.


- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEByGZagVFX4UWr64RAjQJAJ9unyR7P+JPi14YwCi+eKaYF+VeMgCgs0N/
yRzUKfTTmLVMySmRWJUv6us=
=KubY
-----END PGP SIGNATURE-----
 
M

Micah Cowan

tmp123 said:
It is easy for the preprocessor to know the current file and line.
However, it can not known the function name (even it can not known what
is a function) if it doesn't knowns C syntax (and the preprocessor
doesn't).

s/doesn't/might not/

Several implementations have provided such a macro. While they /may/
have been handled differently from other macros (I don't actually
know), they were still handled as string literals, and thus you could
make use of the concatenation feature, etc.

But I think the reason you give above is at least why the standard
didn't want to /require/ __func__ to be a macro (and so they required
it to be something else).

-Micah
 
J

Jordan Abel

-snip

, and it's __func__ (note double underscores before and after).

Let me guess, __func__ is a C99 feature?
<OT> (which means I can't use it with my compiler msvc 6.0).<OT>

No, but i have a suggestion:

#if __STDC_VERSION__ < 199901l
#define WHERE_FMT __FILE__ ":%d"
#define WHERE_ARGS __LINE__
#else
#define WHERE_FMT __FILE__ ":%s:%d"
#define WHERE_ARGS __LINE__,__func__
#endif

fprintf(stderr,"Line printed to stderr at "WHERE_FMT"\n",WHERE_ARGS);

will print file.c:function:42 on c99 systems, file.c:42 on others.
 
J

Jordan Abel

My copy of the draft C99 standard doesn't give a reason.

That's because that's not the right place to look
http://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf

lines 29-31 on page 50 [PDF page 57] read as follows:

A new feature of C99: C99 introduces predefined identifiers, which have
block scope (as distinct 30 from predefined macros which have file
scope), and one such predefined identifier, __func__, which allows the
function name to be used at execution time.

That seems to be saying that the reason is because a macro has file
scope, and thus couldn't have a different value on different lines
unless undefined and redefined by the user.

Note, however, that string literal concatenation takes place in phase 6,
whereas semantic analysis [which is when { } becomes a block scope, etc]
is phase 7.
 
B

Ben Bacarisse

No, but i have a suggestion:

#if __STDC_VERSION__ < 199901l
#define WHERE_FMT __FILE__ ":%d"
#define WHERE_ARGS __LINE__
#else
#define WHERE_FMT __FILE__ ":%s:%d"

Micro correction: swap %s and %d (or __LINE__ and __func__ below)
 
K

Keith Thompson

Jordan Abel said:
My copy of the draft C99 standard doesn't give a reason.

That's because that's not the right place to look
http://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf

lines 29-31 on page 50 [PDF page 57] read as follows:

A new feature of C99: C99 introduces predefined identifiers, which have
block scope (as distinct 30 from predefined macros which have file
scope), and one such predefined identifier, __func__, which allows the
function name to be used at execution time.

That seems to be saying that the reason is because a macro has file
scope, and thus couldn't have a different value on different lines
unless undefined and redefined by the user.

__LINE__ has a different value on different lines, and the
preprocessor has no problem handling that.

__func__ isn't a macro because making it one would have required the
preprocessor (more precisely, translation phase 4) to understand more
of the language grammar than is currently required.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top