Get Function Names from Call Stack

Get function names from call stack

You need to generate a map file. The map file contains the function address and memory locations in your executable. Have your build system modified to generate a map file.

From the map file, you can use a text editor and search for addresses. I once wrote a program to find the two symbols bounding a given address. Worked great for environments like yours.

How to retrive the function names from callstack addresses of another process

First they need to know where the modules are loaded from, EnumProcessModules

Then they can use the symbol helper functions in dbghelp to create a "virtual" copy of the process state SymLoadModuleEx

Check around the library which delivers SymLoadModuleEx to see how the symbolic information can be decoded.

What I precisely want to know, is the way I can implement the feature in c++ for my debug tool, which can take a call stack frame address from another program, and get information about that call stack frame.

Yes this is completely possible, from the results of a loaded symbol file, you can resolve the address from the original machine, to a function in a DLL. However, you need to know where the DLL has been loaded in the target process, for this to work.

This could be an extra piece of information from the target process when it initializes, or by using EnumProcessModules.

How do you find out the caller function in JavaScript?

Note that this solution is deprecated and should no longer be used according to MDN documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller



function Hello()
{
alert("caller is " + Hello.caller);
}

Note that this feature is non-standard, from Function.caller:

Non-standard

This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.


The following is the old answer from 2008, which is no longer supported in modern Javascript:

function Hello()
{
alert("caller is " + arguments.callee.caller.toString());
}

How can we know the caller function's name?

There's nothing you can do only in a.

However, with a simple standard macro trick, you can achieve what you want, IIUC showing the name of the caller.

void a()
{
/* Your code */
}

void a_special( char const * caller_name )
{
printf( "a was called from %s", caller_name );
a();
}

#define a() a_special(__func__)

void b()
{
a();
}

Getting the caller function name inside another function in Python?

You can use the inspect module to get the info you want. Its stack method returns a list of frame records.

  • For Python 2 each frame record is a list. The third element in each record is the caller name. What you want is this:

    >>> import inspect
    >>> def f():
    ... print inspect.stack()[1][3]
    ...
    >>> def g():
    ... f()
    ...
    >>> g()
    g

  • For Python 3.5+, each frame record is a named tuple so you need to replace

    print inspect.stack()[1][3]

    with

    print(inspect.stack()[1].function)

    on the above code.

How to get name of calling function/method in PHP?

The debug_backtrace() function is the only way to know this, if you're lazy it's one more reason you should code the GetCallingMethodName() yourself. Fight the laziness! :D

How can I find the method that called the current method?

Try this:

using System.Diagnostics;
// Get call stack
StackTrace stackTrace = new StackTrace();
// Get calling method name
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);

one-liner:

(new System.Diagnostics.StackTrace()).GetFrame(1).GetMethod().Name

It is from Get Calling Method using Reflection [C#].

How do I find the name of the calling function?

Here are two options:

  1. You can get a full stacktrace (including the name, module, and offset of the calling function) with recent versions of glibc with the GNU backtrace functions. See my answer here for the details. This is probably the easiest thing.

  2. If that isn't exactly what you're looking for, then you might try libunwind, but it's going to involve more work.

Keep in mind that this isn't something you can know statically (as with PRETTY_FUNCTION); you actually have to walk the stack to figure out what function called you. So this isn't something that's really worth doing in ordinary debug printfs. If you want to do more serious debugging or analysis, though, then this might be useful for you.



Related Topics



Leave a reply



Submit