Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Function To Behave Differently If Called From Main Thread

29 views
Skip to first unread message

Frederick Gotham

unread,
Sep 20, 2019, 10:24:35 AM9/20/19
to

Some of you may vomit at my idea. . .

But I don't want to go editing files all over the place... I only want to make an edit to one small little source file.

So here's my idea. . .

There's a function that must behave a little differently if it's called from a **particular** function in a **particular** thread. I can't change the file that the function is called from. In fact, I can only edit the small source file containing the called function. So here's what I'm thinking, add a little function:

#include <cstdlib> /* free */
#include <cstring> /* strcmp */
#include <execinfo.h> /* backtrace, backtrace_symbols */

static bool Is_Called_From_Certain_Func(void)
{
void *bt[1024];

int const bt_size = backtrace(bt, 1024);

char **const bt_syms = backtrace_symbols(bt, bt_size);

bool retval = false;

for (int i = 0; i < bt_size; ++i)
{
if ( 0 == std::strcmp("Some_Func", str_entry_point) )
{
retval = true;
break;
}
}

std::free(bt_syms);

return retval;
}

This will allow me to see if this function was called from a ***particular** function... and then I think I can use pthread functions to get the thread ID, and combine the two to see if the function should behave differently.

thoughts?

Frederick Gotham

unread,
Sep 20, 2019, 10:26:30 AM9/20/19
to

> if ( 0 == std::strcmp("Some_Func", str_entry_point) )


Typo: "str_entry_point" should be "bt_syms[i]"

Robert Wessel

unread,
Sep 20, 2019, 12:13:49 PM9/20/19
to
Obviously any hope of that working is very implementation specific,
and further, even GCC states that this may not work at optimization
levels higher than -O0. Most notably, you may see omitted stack
frames and inlined functions with any level of optimization, and thus
the "calling" function may leave no trace on the stack at all.

And, of course, backtrace_symbols won't work unless you've compiled
with debug info.

Perhaps some thread local storage to hold a flag might be a better
option, although that has many of the same issues as any sort of
global storage.

Louis Krupp

unread,
Sep 21, 2019, 3:13:20 AM9/21/19
to
On Fri, 20 Sep 2019 07:24:21 -0700 (PDT), Frederick Gotham
<cauldwel...@gmail.com> wrote:

>
My thoughts:

As Robert indicated, this might work ... sometimes.

If you were in the middle of a debugging session, then using backtrace
info to expose information about the call chain and to test hypotheses
could be a clever way to save yourself hours of work.

Depending on backtrace info in production code sounds like a good way
to land someone in an hours-long debugging session.

Don't do it.

Louis

Queequeg

unread,
Sep 21, 2019, 9:57:41 AM9/21/19
to
Frederick Gotham <cauldwel...@gmail.com> wrote:

> There's a function that must behave a little differently if it's called
> from a **particular** function in a **particular** thread.

With particular thread it's simple, just store thread ID and compare it
later. With function it gets tricky.

Can you edit the header included from the file that you cannot modify? If
yes, maybe instead of a function prototype, write a macro that will check
__FUNCTION__ variable, or just call your function (that you can modify)
with __FUNCTION__ as an argument.

Something like this:

#v+ test.cpp
#include <string>
#include <cstdio>

// prototype which you can modify (in header)
#define myFunction() myFunction2(__FUNCTION__)

// function that you can modify
void myFunction2(const std::string& functionName)
{
if (functionName == "main")
printf("Called from main\n");
else
printf("Not called from main, called from %s instead\n",
functionName.c_str());
}

// function that you cannot modify
void fn()
{
myFunction();
}

// function that you cannot modify
int main()
{
myFunction();
fn();
return 0;
}
#v-

--
https://www.youtube.com/watch?v=9lSzL1DqQn0

Frederick Gotham

unread,
Sep 23, 2019, 3:44:29 AM9/23/19
to
On Saturday, September 21, 2019 at 2:57:41 PM UTC+1, Queequeg wrote:

<snip>
> // prototype which you can modify (in header)
> #define myFunction() myFunction2(__FUNCTION__)
<snip>
> void fn()
> {
> myFunction();
> }

This would be a really nice way of doing it, but when I said I only want to change one source file, I really should have said one translation unit.

Everything has already been compiled to object files on the target platform, and so now I only want to alter one object file.

Frederick Gotham

unread,
Sep 23, 2019, 4:40:35 AM9/23/19
to


I see just now that Boost has a new library for doing stack traces
0 new messages