pastebin - collaborative debugging tool
rovema.kpaste.net RSS


Intercepting pthread_mutext_init() via LD_PRELOAD
Posted by Anonymous on Wed 6th Jul 2022 10:20
raw | new post
view followups (newest first): Intercepting pthread_mutext_init() via LD_PRELOAD by Anonymous

  1. --- /dev/null   2022-04-19 22:28:32.062891741 +0200
  2. +++ pthread_mutex_init_log.c    2022-07-06 11:16:54.162382585 +0200
  3. @@ -0,0 +1,22 @@
  4. +#define _GNU_SOURCE 1 /* needed for |RTLD_NEXT| */
  5. +
  6. +#include <pthread.h>
  7. +#include <unistd.h>
  8. +#include <string.h>
  9. +#include <dlfcn.h>
  10. +
  11. +static int once = 0;
  12. +
  13. +int pthread_mutex_init(pthread_mutex_t *restrict mutex,
  14. +               const pthread_mutexattr_t *restrict attr) {
  15. +       const char *mymessage = "Interceptor: In our own pthread_mutex_init()\n";
  16. +      
  17. +       /* just print message ONCE */
  18. +       if (once++ == 0)
  19. +               (void)write(2, mymessage, strlen(mymessage));
  20. +
  21. +       int (*original_pthread_mutex_init)(pthread_mutex_t *restrict mutex,
  22. +               const pthread_mutexattr_t *restrict attr);
  23. +       original_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
  24. +       return (*original_pthread_mutex_init)(mutex, attr);
  25. +}
  26. --- /dev/null   2022-04-19 22:28:32.062891741 +0200
  27. +++ test1.c     2022-07-06 11:16:22.874096153 +0200
  28. @@ -0,0 +1,16 @@
  29. +#include <pthread.h>
  30. +#include <errno.h>
  31. +#include <stdio.h>
  32. +
  33. +int main(void) {
  34. +       (void)puts("# start.");
  35. +
  36. +       pthread_mutex_t mutex;
  37. +      
  38. +       pthread_mutex_init(&mutex, NULL);
  39. +       perror("pthread_mutex_init() result");
  40. +
  41. +       (void)puts("# end.");
  42. +
  43. +       return 0;
  44. +}
  45. --- /dev/null   2022-04-19 22:28:32.062891741 +0200
  46. +++ Makefile    2022-07-06 11:17:24.038655927 +0200
  47. @@ -0,0 +1,14 @@
  48. +
  49. +pthread_mutex_init_log.o: pthread_mutex_init_log.c
  50. +       gcc -std=gnu11 -g pthread_mutex_init_log.c -fPIC -c -o pthread_mutex_init_log.o
  51. +pthread_mutex_init_log.so: pthread_mutex_init_log.o
  52. +       gcc -std=gnu11 -g pthread_mutex_init_log.o -fPIC -shared -ldl -o pthread_mutex_init_log.so
  53. +
  54. +test1: test1.c
  55. +       gcc -std=gnu11 -g test1.c -o test1
  56. +      
  57. +run_test: test1 pthread_mutex_init_log.so
  58. +       ksh93 -c 'LD_PRELOAD=$$PWD/pthread_mutex_init_log.so ./test1'
  59. +
  60. +clean:
  61. +       rm -f *.o *.so test1

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at