开发者

A question on shared library and thread specific data

开发者 https://www.devze.com 2023-04-05 10:44 出处:网络
This question is in reference to gdb and valgrind within a makefile. I found the reason of segmentation fault as pointed out in the previous quetion and I now don\'t know how to solve the issue. I am

This question is in reference to gdb and valgrind within a makefile. I found the reason of segmentation fault as pointed out in the previous quetion and I now don't know how to solve the issue. I am sorry i shall not be able to post the code as it spreads across multiple fil开发者_开发技巧es and hence tends to be quite huge.

looking at the Makefile

all: clients.so simulator backup     
    LD_PRELOAD=/home/Juggler/client/clients.so ./simulator 
backup: backup.c libclient.a  
    gcc backup.c -o backup -L /home/Juggler/client -L. -lclient -ldl 
simulator: simulator.c libclient.a
    gcc -g simulator.c -o simulator -L /home/Juggler/client -L. -lclient -ldl -pthread 

libclient.a: libclient.o client.o  
    ar rcs libclient.a libclient.o client.o
libclient.o:libclient.c
   gcc -c libclient.c -o libclient.o -pthread
clients.so: client.o client_invoke.o
   ld -shared -o clients.so client_invoke.o client.o -ldl
client_invoke.o: client_invoke.c
   gcc -Wall -fPIC -DPIC -c -g client_invoke.c
client.o: client.c
   gcc -Wall -fPIC -DPIC -c -g client.c -ldl -pthread

Simulator starts a number of threads and each thread first uses functions in libclient.a to reach client.c where connection is made to a server thru sockets. Each thread is connected to the server via a different socket and have used the pthread_keycreate and its family of functions to keep the socket value thread specific. Other calls from each thread(open,read etc) goes through the shared library client.so that was preloaded and then to client.c to send message to server through the thread specific socket.

Now, the problem is any message to server from a thread that takes the route through libclient.a uses the thread-specific socket but any message that takes the route through clients.so shared library encounters a segmentation fault on send(). I tried printing out the socket descriptor and it prints in the first route but not the second...segfault occurs in the print statement.

Does thread-specific data and shared library work?

Thanks

Edit: Code in and around segfault

int call_execute(char *argcalls[])
{
  int *saved_socket;
  saved_socket = (int*)pthread_getspecific(key_to_sockfd);
   .....
  n = send(*saved_sockfd,argcalls[i],strlen(argcalls[i]),0);//segfault here.argcalls[i] not giving segfault

  ...

}

Would be happy to clarify. call_execute is called from client_invoke when the segfault occurs. But when it is called from libclient.c it works fine.


Yes, TSS and shared libraries work in principle, with one caveat: From my experience with similar issues in mingw-cross-compiling, I assume that your pthread library must also be linked dynamically to provide a common storage location for the thread-specific data. I don't think this is the case normally for gcc, but am not sure about that. Two suggestions:

  1. Try linking with -shared-libgcc.
  2. Relocate the code that allocates the socket into a third library and/or keep it purely in client.so, propagating only the pointer to the struct.
0

精彩评论

暂无评论...
验证码 换一张
取 消