Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose DumpStackTraceToString to application/user #144

Closed
sudhagarc opened this issue Dec 7, 2016 · 2 comments · Fixed by #1075
Closed

Expose DumpStackTraceToString to application/user #144

sudhagarc opened this issue Dec 7, 2016 · 2 comments · Fixed by #1075
Milestone

Comments

@sudhagarc
Copy link

Currently, it seems like only way to get call stack (backtrace or stack trace) is to set FLAGS_log_backtrace_at variable with the desired filename and line number. However, this is a global variable and when used in multithreaded environment, it leads to data corruption issue, hence process crash. One way to prevent it would be to use locks and allow only one thread to dump the call stack.

I'm wondering whether it's possible to expose DumpStackTraceToString utility to application or user. Would that make it lock-free to get the stack trace and make it performant? or, is there any other alternative to achieve the same i.e., get call stack in multithreaded application where more than one thread can simultaneously invoke logging with backtrace set.

@ijesonchen
Copy link

Since it's a function and you can declear it any where, e.g. in your own header and just use it. Don't forget to link glog library.
In my code, I declare it in my header file like this:

namespace google {
namespace glog_internal_namespace_ {
void DumpStackTraceToString(std::string *stacktrace);
}
} // namespace google

And use it like this:

if (condition){
std::string stack;
google::glog_internal_namespace_::DumpStackTraceToString(&stack);
LOG(ERROR) << "ERROR " << err_string << ". stack\n" << stack;
}

And it seems working well.

@sergiud sergiud closed this as completed Mar 30, 2021
@sergiud sergiud mentioned this issue May 6, 2021
@amosbird
Copy link

It generates a hidden symbol by default:

❯ readelf --demangle -s --wide contrib/glog/libglogd.so.0.5.0 | rg DumpStackTraceToString
   980: 00000000000317e0    39 FUNC    LOCAL  HIDDEN    13 google::glog_internal_namespace_::DumpStackTraceToString(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)

static build works fine though.

Am I miss anything?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants