9

Is there a way in C++11 (using the latest GCC) to get the name, or the file and line number, of the method calling the currently executed method (the caller)?

I want to use this information in an error message when, for example, the following code fails:

void SomewhereInMyProgram()
{
    DoSomething(nullptr);
}

void DoSomething(const char* str)
{
    Contract::Requires(str != nullptr);
    // ...
}

Currently I have code in place that reports the error as occurring in DoSomething. While this is technically true, I'd like it to report the error as occurring in SomewhereInMyProgram wherever that may be. That would make my life a whole lot easier!

The solution can use any C++11 features, macro's or GCC specific stuff, but rather not something I have to add at each and every call site.

I think a stacktrace will not help me, because I cannot use exception handling. Actually, I'm very limited: it's a freestanding environment where the standard C++ headers are not available. I was hoping for a macro solution of some sort.


class Contract
{
public:
    static void RequiresImpl(bool condition, const char* expression,
        const char* file, int line);

    #define Requires(condition) RequiresImpl(condition, #condition , \
        __FILE__, __LINE__ )
};
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
  • 3
    Sounds like you are looking for a runtime stacktrace, ie have a look at this SO question (how they get the stacktrace) http://stackoverflow.com/questions/77005/how-to-generate-a-stacktrace-when-my-gcc-c-app-crashes – epatel Apr 08 '13 at 17:02
  • @epatel I am **not** looking for a stacktrace, because I cannot use exception handling. By the way, I am surely not asking the same question as the one you marked duplicate? – Daniel A.A. Pelsmaeker Apr 08 '13 at 17:12
  • I meant that you should look into the `handler()` function to see how they get the stack trace from there, ie the functions `backtrace()`, `backtrace_symbols()` and `backtrace_symbols_fd()`. Try using them at the same place you want to get the "caller" – epatel Apr 08 '13 at 17:15
  • Does this answer your question? [How to get the file name and a line number of a function call?](https://stackoverflow.com/questions/21022436/how-to-get-the-file-name-and-a-line-number-of-a-function-call) – user202729 Dec 30 '19 at 03:53

3 Answers3

6

Wrap DoSomething in a macro:

void DoSomethingImp(char const *, char const *file, char const *line)
{
    // do whatever needed, use file and line to report problems
}

#define DoSomething(x) DoSomethingImp(x, __FILE__, __LINE__)

DISCLAIMER:

This is not the best thing to do, people are screaming on WIN API macros defined this way for either ANSI or UNICODE. But I believe this is the only way if you don't want to change every call to DoSomething.

Tomek
  • 4,554
  • 1
  • 19
  • 19
1

To the best of my knowledge, the only way to AUTOMATICALLY get information about previous calls is to use a backtrace. This post has a ton of information about doing that:

How to generate a stacktrace when my gcc C++ app crashes

Community
  • 1
  • 1
krowe
  • 2,129
  • 17
  • 19
  • After reading your additional requirements, I think you're going to need to use something like this: http://stackoverflow.com/questions/5081123/how-to-add-code-at-the-entry-of-every-function – krowe Apr 08 '13 at 17:50
-4

In gcc you can use one of the following macros: __PRETTY_FUNCTION__ or __FUNCTION__ or __func__.

Amartel
  • 4,248
  • 2
  • 15
  • 21