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

Logging in a static function causes segfault #53

Closed
godbyk opened this issue Aug 27, 2015 · 4 comments
Closed

Logging in a static function causes segfault #53

godbyk opened this issue Aug 27, 2015 · 4 comments

Comments

@godbyk
Copy link

godbyk commented Aug 27, 2015

Previously, logging from a static function used to work fine. It would just log to the console because InitGoogleLogging() hadn't been called yet.

Now, logging from a static function causes the program to segfault.

Here's a short test program that demonstrates the problem. The static bool b = f("..."); line triggers the crash. If that line is commented out, the program runs fine.

#include <glog/logging.h>
#include <string>

bool f(const std::string& message)
{
    LOG(INFO) << message;
    return true;
}

// Uncommenting the following line will trigger the crash
//static bool b = f("Logging before main() crashes.");

int main(int argc, char* argv[])
{
    f("Logging after main() but before InitGoogleLogging() is fine.");
    google::InitGoogleLogging(argv[0]);
    f("Logging after InitGoogleLogging() is obviously fine.");

    return 0;
}

Example stack trace from gdb:

#0  0x00007ffff796eae0 in std::string::empty() const () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x0000000000408366 in google::LogMessage::Init (this=0x7fffffffd458, file=0x42d484 "static_log.cpp", line=6, severity=0, 
    send_method=(void (google::LogMessage::*)(google::LogMessage * const)) 0x4087f0 <google::LogMessage::SendToLog()>)
    at /home/godbyk/git/glog/src/logging.cc:1251
#2  0x0000000000407b79 in google::LogMessage::LogMessage (this=0x7fffffffd458, file=0x42d484 "static_log.cpp", line=6)
    at /home/godbyk/git/glog/src/logging.cc:1160
#3  0x0000000000405925 in f (message="Logging before main() crashes.") at static_log.cpp:6
#4  0x000000000040516f in __cxx_global_var_init () at static_log.cpp:11
#5  0x00000000004051fe in _GLOBAL__sub_I_static_log.cpp ()
#6  0x000000000042d3ed in __libc_csu_init ()
#7  0x00007ffff6fe49cf in __libc_start_main (main=0x4059a0 <main(int, char**)>, argc=1, argv=0x7fffffffd5e8, 
    init=0x42d3a0 <__libc_csu_init>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffd5d8) at libc-start.c:245
#8  0x00000000004057d9 in _start ()
@godbyk
Copy link
Author

godbyk commented Aug 31, 2015

I got this sorted it. My build script was compiling against the static glog library and the dynamic gflags library.

Here are how the combinations work out:

glog  gflags  status
----  ------  ------
 so    so     OK
 so    a      error (see below)
 a     so     crash
 a     a      crash

When using the dynamic glog library and the static gflags library, the program prints the following error at runtime:

ERROR: something wrong with flag 'flagfile' in file '/home/username/git/gflags/src/gflags.cc'.  One possibility: file '/home/username/git/gflags/src/gflags.cc' is being linked both statically and dynamically into this executable.

@Shixiaowei02
Copy link

I also encountered this problem, but it seems that sometimes the results of static links are normal.

@echoface
Copy link

echoface commented Apr 4, 2020

same problem here. libgflags.a(2.2.2) + libglog.a (0.4.0) cause crash when init LOGOBJECT, is there any root cause found about this issue?

centos and debian both, other platform not testing

log:
0x00007ffff6715c90 in std::string::c_str() const () from /lib64/libstdc++.so.6
Missing separate debuginfos, use: debuginfo-install keyutils-libs-1.5.8-3.el7.x86_64 krb5-libs-1.15.1-37.el7_7.2.x86_64 libcom_err-1.42.9-16.el7.x86_64 libgcc-4.8.5-39.el7.x86_64 libselinux-2.5-14.1.el7.x86_64 libstdc++-4.8.5-39.el7.x86_64 mariadb-libs-5.5.64-1.el7.x86_64 openssl-libs-1.0.2k-19.el7.x86_64 zlib-1.2.7-18.el7.x86_64
(gdb) bt
#0 0x00007ffff6715c90 in std::string::c_str() const () from /lib64/libstdc++.so.6
#1 0x0000000000482ac4 in google::VLOG2Initializer() ()
#2 0x0000000000482ece in google::InitVLOG3__(int**, int*, char const*, int) ()
#3 0x000000000045cfef in base::MessageLoop::ThreadMain() ()
#4 0x000000000049d2b0 in execute_native_thread_routine ()
#5 0x00007ffff7bc6e65 in start_thread () from /lib64/libpthread.so.0
#6 0x00007ffff5e7188d in clone () from /lib64/libc.so.6

@mawenge
Copy link

mawenge commented Jun 20, 2024

I declared a global variable, and in the constructor of that variable, I called glog to print logs. Then a segmentation fault occurred. In other words, the error occurred before the main method, in the constructor of the global variable.

Thread 1 "metric_test" received signal SIGSEGV, Segmentation fault.
0x00005555558e35d8 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const ()
(gdb) bt
#0  0x00005555558e35d8 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const ()
#1  0x00005555558e936e in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::empty() const ()
#2  0x0000555555aca04e in google::LogMessage::Init(char const*, int, int, void (google::LogMessage::*)()) ()
#3  0x0000555555ac985c in google::LogMessage::LogMessage(char const*, int) ()
#4  0x00007ffff72404df in data_prism::CivilToBsonDate(absl::lts_20240116::time_internal::cctz::detail::civil_time<absl::lts_20240116::time_internal::second_tag> const&, absl::lts_20240116::TimeZone const&) ()

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

No branches or pull requests

4 participants