I have coded a program that works fine on windows, then I had to use it on linux so I coded the needed things for it to work on linux. What I mainly changed was the thread creation function ;
void thread_create(void *(*threadfunc)(void *), void *u)//linux
{
#ifdef linux
pthread_t id;
pthread_create(&id, NULL, threadfunc, u);
#endif
}
void thread_create(void (*threadfunc)(void *), void *u)//windows
{
#ifndef linux
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadfunc, u, 0, NULL);
#endif
}
The program has a lot of threads. I used gdb to see where it crashes, and the crash always occures at the curl_easy_perform function. Note that I am using this function a lot of times before where I know it will crash, the program never crashes before that point. When I am using them before that point, I'm not sending any data with POST/GET methods, I'm only getting informations from pages without sending any data.
So gdb always tells me :
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffdf65c700 (LWP 26488)]
0x00007ffff6fb0360 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) bt
#0 0x00007ffff6fb0360 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00000000004a1e70 in readmoredata ()
#2 0x00000000004b33d7 in Curl_fillreadbuffer ()
#3 0x00000000004b40fc in Curl_readwrite ()
#4 0x000000000049b3bc in multi_runsingle ()
#5 0x000000000049c275 in curl_multi_perform ()
#6 0x00000000004972c3 in curl_easy_perform ()
I would like to give you something you can make tests on, but I really can't. The program has thousands of lines.
Related
I have a main program that uses dlopen to load a library that is written in C++ and call a routine.
handle = dlopen(LIBNAME,RTLD_LAZY);
if(handle == NULL){
fprintf(stderr,"Could not open library %s: %s\n",LIBNAME,dlerror());
exit(1);
}
...
startptr = (int (*)(char*,char*,char*))dlsym(handle,"PluginStart");
if (startptr == NULL){
fprintf(stderr,"Could not find start\n");
exit(1);
}
...
int res = (*startptr)(name,sig,descr);
The start code in the plugin creates an instance of a class,
and spins of a thread to do the startup so that the start code can return
to the main program. The thread runs a loop so that the plugin is running independently from the main program.
Manager(){
std::thread mgrThread(&Manager::threadFunc,this);
mgrThread.detach();
};
void Manager::threadFunc(void)
{
//setup stuff that ends in a loop
}
This worked fine when the main program was written in C. I decided to add a map
to the main program so I changed to C++, and the creation of the thread in the plugin now segfaults.
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) where
#0 0x0000000000000000 in ?? ()
#1 0x00007ffff7e740a9 in std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) () from /lib/x86_64-linux-gnu/libstdc++.so.6
#2 0x00007ffff7fac0c7 in std::thread::thread<void (Manager::*)(), Manager*, void> (this=0x7fffffffddc0, __f=
#0x7fffffffddd0: (void (Manager::*)(class Manager * const)) 0x7ffff7face36 <Manager::threadFunc()>)
at /usr/include/c++/9/thread:130
#3 0x00007ffff7fabf76 in Manager::Manager (this=0x555555580500) at Manager.h:13
#4 0x00007ffff7fabc21 in PluginStart (outName=0x555555561080 <name> "", outSig=0x555555561480 <sig> "",
outDesc=0x555555561880 <descr> "") at testPlugin.cpp:35
#5 0x000055555555b678 in main (argc=1, argv=0x7fffffffdf68) at main.cpp:122
Creating a thread in the main program that does a single printf and exits fixes the failure.
void tstFunc(void){
printf("xyzzy\n");
}
...
std::thread tstThread(tstFunc);
tstThread.detach();
...
So apparently the pthread library is can't dynamically load correctly from the plugin if the C++ library is loaded without pthread in the main program?
I was using -Wl,--dynamic-list=./mexports.txt in the link of the main program to make the callbacks the plugin uses visible to the plugin. I changed it to -Wl,--export-dynamic to see if a more generous symbol export worked, but it only works if I create and use a thread in the main program (for both link options).
Any help would be appreciated.
Ubuntu 20.04 LTS, g++ 9.4.0, no c++ std specified in the makefile.
An Qt application is crashing and even debugger mode it's all I get:
ASSERT: "!isEmpty()" in file
C:\Qt\Qt5.5.0\5.5\mingw492_32\include\QtCore/qlist.h, line 321
That line in the file points to:
inline void removeLast() { Q_ASSERT(!isEmpty()); erase(--end()); }
But I'd like more information. Like what line exactly in the source code is using it (from search, no direct call to removeLast() done).
Is this possible?
If you run your program in a debugger, it will stop on the assertion and you'll be able to examine the stack trace. For example with this program in GDB :
#include <QList>
int main(int argc,char* argv[])
{
QList<int> my_list;
my_list.append(1);
my_list.pop_back(); // 1
my_list.pop_back(); // 2
return 0;
}
When you run it :
(gdb) r
Starting program: /home/leiaz/tmp/qttest/build/proj
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
ASSERT: "!isEmpty()" in file /usr/include/qt/QtCore/qlist.h, line 321
Program received signal SIGABRT, Aborted.
0x00007ffff61275f8 in raise () from /usr/lib/libc.so.6
The assertion stop the debugger, and you can ask for the stack trace :
(gdb) backtrace
#0 0x00007ffff61275f8 in raise () from /usr/lib/libc.so.6
#1 0x00007ffff6128a7a in abort () from /usr/lib/libc.so.6
#2 0x00007ffff6dc11e1 in QMessageLogger::fatal(char const*, ...) const () from /usr/lib/libQt5Core.so.5
#3 0x00007ffff6dbc34e in qt_assert(char const*, char const*, int) () from /usr/lib/libQt5Core.so.5
#4 0x00000000004060aa in QList<int>::removeLast (this=0x7fffffffe4d0)
at /usr/include/qt/QtCore/qlist.h:321
#5 0x0000000000405de0 in QList<int>::pop_back (this=0x7fffffffe4d0)
at /usr/include/qt/QtCore/qlist.h:337
#6 0x0000000000405ad4 in main (argc=1, argv=0x7fffffffe5d8) at /home/leiaz/tmp/qttest/main.cc:9
You can see removeLast was called by pop_back and my code start at frame 6 :
(gdb) frame 6
#6 0x0000000000405ad4 in main (argc=1, argv=0x7fffffffe5d8) at /home/leiaz/tmp/qttest/main.cc:9
9 my_list.pop_back(); // 2
Here you can examine the values of other variables in that frame.
If you are using Qt Creator see Viewing Call Stack Trace.
Like what line exactly in the source code is using it (from search, no direct call to removeLast() done). Is this possible?
Unfortunately assert() or Q_ASSERT() macros just show that the conditions were wrong, not which code was causing these conditions.
Especially if called many times and/or from many places, assertions aren't very helpful to detect what code actually was causing it.
You can set a conditional breakpoint for the isEmpty() condition, if that's well supported with your debugger.
You can also set a breakpoint in the standard abort() function, if you have access to the debug symbols.
If there's not, and if you have full access to the source code (which you have for a function inlined in a header) you can work around that deficiency. The way I'm usually going, is to change such code temporarily to
void removeLast()
{
if(isEmpty()) { // <<<<<<<<<<< Put an encapsulating if clause here
return; // <<<<<<<<<<<< set breakpoint
}
Q_ASSERT(!isEmpty()); erase(--end());
}
and set a debugger breakpoint. When the breakpoint is hit while running the code from the debugger, I'll examine the call stack to see where this came from.
The following piece of code works with g++ 4.6 compiler but crashes with segmentation fault when compiled with g++ 5.1 compiler. The variable access gString is causing the segmentation fault.
#define _GLIBCXX_DEBUG 1
#define _GLIBCXX_USE_CXX11_ABI 0
#include<string>
#include<iostream>
#include<vector>
static std::string gString("hello");
static void
__attribute__((constructor))
initialize()
{
gString.assign("hello world");
return;
}
static void
__attribute__((destructor))
finalize()
{
return;
}
int main(int ac, char **av)
{
//std::cerr<<gString;
return 0;
}
GDB output:
Reading symbols from /home/rk/str...done.
(gdb) b initialize
Breakpoint 1 at 0x401419: file str.cc, line 15.
(gdb) r
Starting program: /home/rk/str
Breakpoint 1, initialize() () at str.cc:15
15 gString.assign("hello world");
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
0x00000000004018d6 in std::string::size() const () at /usr/include/c++/5/bits/basic_string.h:3118
3118 { return _M_rep()->_M_length; }
(gdb) bt
#0 0x00000000004018d6 in std::string::size() const () at /usr/include/c++/5/bits/basic_string.h:3118
#1 0x00000000004016ff in std::string::assign(char const*, unsigned long) () at /usr/include/c++/5/bits/basic_string.tcc:706
#2 0x000000000040166e in std::string::assign(char const*) () at /usr/include/c++/5/bits/basic_string.h:3542
#3 0x0000000000401428 in initialize() () at str.cc:15
#4 0x00000000004023dd in __libc_csu_init ()
#5 0x00007ffff71ad700 in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
#6 0x0000000000401289 in _start ()
Why are you using __attribute__((constructor)) in C++ instead of simply a global object with a constructor? Those attributes are useful in C code, but redundant in C++.
The problem is that your constructor runs before the standard iostreams have been initialized, which would not be a problem if you used a global object with a constructor.
You could try adding a priority to your constructor, but I don't think it will help in this case:
__attribute__((constructor(999)))
The runtime error also happens with gcc 4.9.2 (see ideone example).
The problem is related to the iostreams which are not yet initialized. Commenting out the cerr line, and everything works fine
Apparently, it's a known issue.
Edit: Additional remarks
This small workaround seems to work, at least with 4.9: use c stdio instead of iostreams:
fprintf(stderr, "_initialize"); // this works
But I fully agree with Jonathan's suggestion of using a global (singleton ?) object relying solely on well defined standard C++ behaviour, unless you really need the constructor being run exactly at the moment of a dynamic library load.
Consider the following function defined in a C library:
void f(void (*callback)(int)) { callback(0); }
which will be called from a C++11 program defining callback(), possibly throwing an exception, as below:
void callback(int) { /* can I throw ? */}
try {
f(callback);
} catch (...) {
/* can the exception be caught ? */
}
My questions are:
does the standard allows the callback to throw when called from f()? If not, must/should I declare callback() with the noexcept specifier?
If yes, does the standard allows to catch the exception object being thrown?
Below source code can be compliled.
exception.cpp:
extern "C"
void throwInCPP() {
throw 5;
}
main.c
#include <stdio.h>
void throwInCPP();
void throwExp(void (*callback)()) {
callback();
}
int main() {
throwExp(throwInCPP);
return 0;
}
When when it gets ran, exception will be thrown and the program unwind the stack try to find a catch block but it cannot. Then abort happens:
#0 0x00007ffff7238d67 in raise () from /usr/lib/libc.so.6
#1 0x00007ffff723a118 in abort () from /usr/lib/libc.so.6
#2 0x00007ffff7b2e1f5 in __gnu_cxx::__verbose_terminate_handler() ()
from /usr/lib/libstdc++.so.6
#3 0x00007ffff7b2c076 in ?? () from /usr/lib/libstdc++.so.6
#4 0x00007ffff7b2c0c1 in std::terminate() () from /usr/lib/libstdc++.so.6
#5 0x00007ffff7b2c2d8 in __cxa_throw () from /usr/lib/libstdc++.so.6
#6 0x00000000004007fa in throwInCPP ()
#7 0x00000000004007bd in throwExp (callback=0x4007d4 <throwInCPP>) at main.c:6
#8 0x00000000004007cd in main () at main.c:11
I think you can always call a cpp function which throws an expection in c, but you can never put a catch block in c since it doesn't compile.
How is the C++ exception handling runtime implemented? This explains how does the exception work in c++.
I don't really think throw exception in c will be something good since you cannot do anything with it in the "normal" way.
I'm having a segmentation fault with my program.
In fact I write a library in C++ and convert it for tcl using SWIG.
The segfault occurs here:
return Tcl_NewIntObj(static_cast< int >(value));
where value=0
the gdb back trace shows:
(gdb) bt
#0 0x000054b6 in ?? ()
#1 0xb6650d5d in SWIG_From_long (value=0) at mntdisplay_wrap.cc:1712
#2 SWIG_From_int (value=0) at mntdisplay_wrap.cc:1722
#3 Testguimnt_Init (interp=0x9714e28) at mntdisplay_wrap.cc:3774
#4 0xb76748fe in Tcl_LoadObjCmd () from /opt/ActiveTcl-8.6/lib/libtcl8.6.so
#5 0xb75d02af in TclNREvalObjv () from /opt/ActiveTcl-8.6/lib/libtcl8.6.so
#6 0xb75d0859 in Tcl_EvalObjv () from /opt/ActiveTcl-8.6/lib/libtcl8.6.so
#7 0xb75d0d99 in TclEvalEx () from /opt/ActiveTcl-8.6/lib/libtcl8.6.so
#8 0xb7670045 in Tcl_FSEvalFileEx () from /opt/ActiveTcl-8.6/lib/libtcl8.6.so
#9 0xb767645f in Tcl_MainEx () from /opt/ActiveTcl-8.6/lib/libtcl8.6.so
#10 0x0804885c in main ()
In the wrapper:
line 1712:
SWIGINTERNINLINE Tcl_Obj*
SWIG_From_long (long value)
{
if (((long) INT_MIN <= value) && (value <= (long) INT_MAX)) {
return Tcl_NewIntObj(static_cast< int >(value)); //1712
} else {
return Tcl_NewLongObj(value);
}
}
1722:
SWIGINTERNINLINE Tcl_Obj *
SWIG_From_int (int value)
{
return SWIG_From_long (value); //1722
}
3774:
SWIG_Tcl_SetConstantObj(interp, "MESSAGE_NEW", SWIG_From_int(static_cast< int >(MESSAGE_NEW)));
where MESSAGE_NEW is defined in a enum and is 0.
Please, if you have any idea, please help me. Thank you!
EDIT:
I found the cause of the problem: it's an linking error.
I created a new thread for this issue:
C++: linked library disappears and gives segfault during execution
I found the problem.
Please see my other post:
C++: linked library disappears and gives segfault during execution
There was an undefined symbol of my library. I defined it and problem solved!
The confusion was, my program crashed in the middle of tcl wrapper functions (where my undefined symbol was not involved at all). I don't really know why but that's it..
Hope it will help others!