Print function name of calling function?

Discussion in 'C Programming' started by dspfun, Feb 20, 2007.

  1. dspfun

    dspfun Guest

    Hi,

    Is it possible to print the function name of the calling function?

    For example, f1() and f2() both calls f3(), in f3() I would like to
    print the name of the function calling f3() which could either be f1()
    or f2().

    BRs!
     
    dspfun, Feb 20, 2007
    #1
    1. Advertising

  2. In article <>,
    dspfun <> wrote:

    >Is it possible to print the function name of the calling function?


    There is no facility for that in standard C.


    >For example, f1() and f2() both calls f3(), in f3() I would like to
    >print the name of the function calling f3() which could either be f1()
    >or f2().


    Some systems provide extensions to allow the calling stack to
    be examined, but C doesn't require it, and it isn't necessarily
    easy even on systems where it can be done.
    --
    Programming is what happens while you're busy making other plans.
     
    Walter Roberson, Feb 20, 2007
    #2
    1. Advertising

  3. dspfun wrote:
    > Hi,
    >
    > Is it possible to print the function name of the calling function?


    Not portably.

    > For example, f1() and f2() both calls f3(), in f3() I would like to
    > print the name of the function calling f3() which could either be f1()
    > or f2().


    So the caller can't rely on f3() behaving consistently?

    If you need more info than you pass in the function arguments, the
    obvious suggestion is to simply add some more function arguments.
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Feb 20, 2007
    #3
  4. dspfun

    dspfun Guest

    On 20 Feb, 08:32, "Harald van Dijk" <> wrote:
    > dspfun wrote:
    > > Hi,

    >
    > > Is it possible to print the function name of the calling function?

    >
    > Not portably.
    >
    > > For example, f1() and f2() both calls f3(), in f3() I would like to
    > > print the name of the function calling f3() which could either be f1()
    > > or f2().

    >
    > So the caller can't rely on f3() behaving consistently?
    >
    > If you need more info than you pass in the function arguments, the
    > obvious suggestion is to simply add some more function arguments.


    In gdb it is possible to get a call trace, how can gdb get the call
    trace? Does gdb use some OS-specific functions for this?
     
    dspfun, Feb 20, 2007
    #4
  5. dspfun

    Klarth Guest

    Yes it is possible in gdb. Use the command "bt" to get a stack trace.

    On Feb 20, 4:42 pm, "dspfun" <> wrote:
    > On 20 Feb, 08:32, "Harald van Dijk" <> wrote:
    >
    > > dspfun wrote:
    > > > Hi,

    >
    > > > Is it possible to print the function name of the calling function?

    >
    > > Not portably.

    >
    > > > For example, f1() and f2() both calls f3(), in f3() I would like to
    > > > print the name of the function calling f3() which could either be f1()
    > > > or f2().

    >
    > > So the caller can't rely on f3() behaving consistently?

    >
    > > If you need more info than you pass in the function arguments, the
    > > obvious suggestion is to simply add some more function arguments.

    >
    > In gdb it is possible to get a call trace, how can gdb get the call
    > trace? Does gdb use some OS-specific functions for this?
     
    Klarth, Feb 20, 2007
    #5
  6. dspfun wrote:
    > On 20 Feb, 08:32, "Harald van Dijk" <> wrote:
    > > dspfun wrote:
    > > > Hi,

    > >
    > > > Is it possible to print the function name of the calling function?

    > >
    > > Not portably.
    > >
    > > > For example, f1() and f2() both calls f3(), in f3() I would like to
    > > > print the name of the function calling f3() which could either be f1()
    > > > or f2().

    > >
    > > So the caller can't rely on f3() behaving consistently?
    > >
    > > If you need more info than you pass in the function arguments, the
    > > obvious suggestion is to simply add some more function arguments.

    >
    > In gdb it is possible to get a call trace, how can gdb get the call
    > trace? Does gdb use some OS-specific functions for this?


    gdb uses system-specific ways to get the function's addresses, but
    loads the compiler-generated debug info (including the calling
    function's name) from the executable. In your own code, you cannot
    even assume that your own program can read itself, let alone rely on a
    specific debugging format.
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Feb 20, 2007
    #6
  7. dspfun

    dspfun Guest

    On 20 Feb, 08:51, "Harald van Dijk" <> wrote:
    > dspfun wrote:
    > > On 20 Feb, 08:32, "Harald van Dijk" <> wrote:
    > > > dspfun wrote:
    > > > > Hi,

    >
    > > > > Is it possible to print the function name of the calling function?

    >
    > > > Not portably.

    >
    > > > > For example, f1() and f2() both calls f3(), in f3() I would like to
    > > > > print the name of the function calling f3() which could either be f1()
    > > > > or f2().

    >
    > > > So the caller can't rely on f3() behaving consistently?

    >
    > > > If you need more info than you pass in the function arguments, the
    > > > obvious suggestion is to simply add some more function arguments.

    >
    > > In gdb it is possible to get a call trace, how can gdb get the call
    > > trace? Does gdb use some OS-specific functions for this?

    >
    > gdb uses system-specific ways to get the function's addresses, but
    > loads the compiler-generated debug info (including the calling
    > function's name) from the executable. In your own code, you cannot
    > even assume that your own program can read itself, let alone rely on a
    > specific debugging format.- Dölj citerad text -
    >
    > - Visa citerad text -


    What is the reason that a program cannot read itself? Why can't the
    program rely on the debugging format (except for portability reasons)?
     
    dspfun, Feb 20, 2007
    #7
  8. dspfun wrote:
    > On 20 Feb, 08:51, "Harald van Dijk" <> wrote:
    > > dspfun wrote:
    > > > On 20 Feb, 08:32, "Harald van Dijk" <> wrote:
    > > > > dspfun wrote:
    > > > > > Hi,

    > >
    > > > > > Is it possible to print the function name of the calling function?

    > >
    > > > > Not portably.

    > >
    > > > > > For example, f1() and f2() both calls f3(), in f3() I would like to
    > > > > > print the name of the function calling f3() which could either be f1()
    > > > > > or f2().

    > >
    > > > > So the caller can't rely on f3() behaving consistently?

    > >
    > > > > If you need more info than you pass in the function arguments, the
    > > > > obvious suggestion is to simply add some more function arguments.

    > >
    > > > In gdb it is possible to get a call trace, how can gdb get the call
    > > > trace? Does gdb use some OS-specific functions for this?

    > >
    > > gdb uses system-specific ways to get the function's addresses, but
    > > loads the compiler-generated debug info (including the calling
    > > function's name) from the executable. In your own code, you cannot
    > > even assume that your own program can read itself, let alone rely on a
    > > specific debugging format.

    >
    > What is the reason that a program cannot read itself?


    Many reasons, but for a simple one, on a Unix-like system, try chmod a-
    r executable. You (and thus the program) won't be able to read it, but
    you can still run it.

    > Why can't the
    > program rely on the debugging format (except for portability reasons)?


    Well, if you don't count the reasons that a program cannot rely on a
    specific debug format, then there are no reasons. :) Seriously, the
    debugging format is not even sure to remain the same with different
    versions of the same compiler for the same system.
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Feb 20, 2007
    #8
  9. dspfun wrote:
    > Hi,
    >
    > Is it possible to print the function name of the calling function?
    >
    > For example, f1() and f2() both calls f3(), in f3() I would like to
    > print the name of the function calling f3() which could either be f1()
    > or f2().


    If you want functions to know the names of their callers, pass it as an
    argument.
     
    Martin Ambuhl, Feb 20, 2007
    #9
  10. dspfun

    matevzb Guest

    On Feb 20, 8:26 am, "dspfun" <> wrote:
    > Hi,
    >
    > Is it possible to print the function name of the calling function?
    >
    > For example, f1() and f2() both calls f3(), in f3() I would like to
    > print the name of the function calling f3() which could either be f1()
    > or f2().

    If I remember correctly, a C99 implementation should implement the
    __func__ identifier. It's implicitly declared and equals to
    static const char __func__[] = "function-name";
    --
    WYCIWYG - what you C is what you get
     
    matevzb, Feb 20, 2007
    #10
  11. matevzb wrote:
    > On Feb 20, 8:26 am, "dspfun" <> wrote:
    > > Hi,
    > >
    > > Is it possible to print the function name of the calling function?
    > >
    > > For example, f1() and f2() both calls f3(), in f3() I would like to
    > > print the name of the function calling f3() which could either be f1()
    > > or f2().

    > If I remember correctly, a C99 implementation should implement the
    > __func__ identifier. It's implicitly declared and equals to
    > static const char __func__[] = "function-name";


    You are correct. However, __func__ contains the name of the current
    function, not of the calling function.
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Feb 20, 2007
    #11
  12. dspfun

    matevzb Guest

    On Feb 20, 10:09 am, "Harald van Dijk" <> wrote:
    > matevzb wrote:
    > > On Feb 20, 8:26 am, "dspfun" <> wrote:
    > > > Hi,

    >
    > > > Is it possible to print the function name of the calling function?

    >
    > > > For example, f1() and f2() both calls f3(), in f3() I would like to
    > > > print the name of the function calling f3() which could either be f1()
    > > > or f2().

    > > If I remember correctly, a C99 implementation should implement the
    > > __func__ identifier. It's implicitly declared and equals to
    > > static const char __func__[] = "function-name";

    >
    > You are correct. However, __func__ contains the name of the current
    > function, not of the calling function.

    Sorry, I misread the OP's question. In that case it's probably best if
    the __func__ is passed to the called function.
    --
    WYCIWYG - what you C is what you get
     
    matevzb, Feb 20, 2007
    #12
  13. dspfun

    CBFalconer Guest

    Harald van D?k wrote:
    > matevzb wrote:
    >> On Feb 20, 8:26 am, "dspfun" <> wrote:
    >>>
    >>> Is it possible to print the function name of the calling function?
    >>>
    >>> For example, f1() and f2() both calls f3(), in f3() I would like
    >>> to print the name of the function calling f3() which could either
    >>> be f1() or f2().

    >>
    >> If I remember correctly, a C99 implementation should implement the
    >> __func__ identifier. It's implicitly declared and equals to
    >> static const char __func__[] = "function-name";

    >
    > You are correct. However, __func__ contains the name of the current
    > function, not of the calling function.


    Exactly. So where do you think the '__func__' should be used? The
    result is a char* pointer, which you can pass wherever you think it
    might be of interest.

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>

    "A man who is right every time is not likely to do very much."
    -- Francis Crick, co-discover of DNA
    "There is nothing more amazing than stupidity in action."
    -- Thomas Matthews
     
    CBFalconer, Feb 20, 2007
    #13
  14. >> > > Is it possible to print the function name of the calling function?
    >> >
    >> > Not portably.
    >> >
    >> > > For example, f1() and f2() both calls f3(), in f3() I would like to
    >> > > print the name of the function calling f3() which could either be f1()
    >> > > or f2().
    >> >
    >> > So the caller can't rely on f3() behaving consistently?
    >> >
    >> > If you need more info than you pass in the function arguments, the
    >> > obvious suggestion is to simply add some more function arguments.

    >>
    >> In gdb it is possible to get a call trace, how can gdb get the call
    >> trace? Does gdb use some OS-specific functions for this?

    >
    >gdb uses system-specific ways to get the function's addresses, but
    >loads the compiler-generated debug info (including the calling
    >function's name) from the executable. In your own code, you cannot
    >even assume that your own program can read itself, let alone rely on a
    >specific debugging format.


    It gets worse than that. Also, gdb has been known to get this
    WRONG, especially if something managed to clobber the stack (if
    there is one) frame.

    - You cannot assume that your own program can even FIND itself. argv[0]
    need not be a *file* name. Also, it's easy for malicious invokers to
    fake this information. Same goes for getenv("PATH").
    - Not all systems have things like /proc/curproc/file (FreeBSD) or
    /proc/self/exe (Linux) which provide a symlink to the executable.
    You still might not have permission to access it in any case.
    - You cannot assume that you have read permission on the executable.
    Execute-only permission is possible in a number of operating systems.
    Plus, the higher-level directory permissions may not permit access.
    - You cannot assume that you can open the executable for read while it's
    running. Some operating systems give an ETXTBSY error. (More give
    ETXTBSY if you try opening for *write*).
    - You cannot assume that the executable has any debugging symbols in *ANY*
    format, much less one that you can interpret. They may not be generated,
    or they may be stripped off later for production use. Among other things,
    in a large software distribution, those symbols can take up a lot of
    disk space. And closed-source software distributions will probably remove
    symbols to avoid giving away too much information.
    - Even if the executable has debugging symbols, you may not have all the
    debugging symbols you need (e.g. for libraries, static-linked or dynamic-
    linked). Sometimes a library function can call YOUR function (e.g.
    qsort calling a compare function).
    - Finding the symbols for a dynamic-linked library can be very
    system-dependent. No, you can't depend on having dynamic-linked libraries,
    but neither can you depend on NOT having them.
    - Even if you have all the function addresses, stack (if there is one) frame
    formats and calling sequences are very system-dependent.
    - The symbol associated with the function name may bear minimal resemblance
    to the actual function name (C++ name-mangling, for example).
     
    Gordon Burditt, Feb 20, 2007
    #14
  15. >> > Is it possible to print the function name of the calling function?
    >> >
    >> > For example, f1() and f2() both calls f3(), in f3() I would like to
    >> > print the name of the function calling f3() which could either be f1()
    >> > or f2().

    >> If I remember correctly, a C99 implementation should implement the
    >> __func__ identifier. It's implicitly declared and equals to
    >> static const char __func__[] = "function-name";

    >
    >You are correct. However, __func__ contains the name of the current
    >function, not of the calling function.


    True, but you could still use it to advantage. Define an N-argument
    macro f3(), which calls f3_real() with the N arguments plus an added
    one, passed as __func__. Now, the name of the calling function is
    passed automatically.
     
    Gordon Burditt, Feb 20, 2007
    #15
  16. dspfun

    Guest

    On Feb 20, 5:51 pm, (Gordon Burditt) wrote:
    > >gdb uses system-specific ways to get the function's addresses, but
    > >loads the compiler-generated debug info (including the calling
    > >function's name) from the executable. In your own code, you cannot
    > >even assume that your own program can read itself, let alone rely on a
    > >specific debugging format.

    >
    > It gets worse than that. Also, gdb has been known to get this
    > WRONG, especially if something managed to clobber the stack (if
    > there is one) frame.



    While I can't speak to GDB, I've seen plenty of situations where
    somewhat odd (but still valid) stack frames exist due to various sorts
    of optimization, and which confuse things trying to trace "standard"
    stack frames.

    Inlined functions will often have that attribute.
     
    , Feb 21, 2007
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    861
    Roedy Green
    Jan 2, 2006
  2. Andreas Lagemann
    Replies:
    8
    Views:
    503
    Mike Wahler
    Jan 10, 2005
  3. tiwy
    Replies:
    0
    Views:
    453
  4. keto
    Replies:
    0
    Views:
    1,001
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    374
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page