pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post f76c47 by Anonymous on Wed 6th Jul 2022 18:29
original post ce2 by Anonymous on Wed 6th Jul 2022 12:53
Download diff
Show old version | new version | both versions

    
11
--- /dev/null	2022-04-19 22:28:32.062891741 +0200
2-
+++ pthread_interceptor.c	2022-07-06 13:47:43.337141261 +0200
2+
+++ pthread_interceptor.c	2022-07-06 19:28:52.893103035 +0200
3-
@@ -0,0 +1,86 @@
3+
@@ -0,0 +1,90 @@
44
+/*
55
+ * pthread_interceptor.c
66
+ *
7-
+ * intercept pthread calls liek |pthread_mutex_init()| via LD_PRELOAD
7+
+ * intercept pthread calls
8+
+ * (like |pthread_mutex_init()|) via LD_PRELOAD
89
+ *
10+
+ * Example usage:
913
+ * Written by Roland Mainz <roland.mainz@nrubsig.org>
1014
+ */
1115
+
1216
+#define _GNU_SOURCE 1 /* needed for |RTLD_NEXT| */
1317
+
1418
+#include <pthread.h>
1519
+#include <unistd.h>
1620
+#include <errno.h>
1721
+#include <stdio.h>
1822
+#include <string.h>
1923
+#include <stdbool.h>
2024
+#include <dlfcn.h>
2125
+
2226
+/* config */
2327
+#define PICFG_PRINT_ONCE_MSG 1
2428
+#define PICFG_ENFORCE_PRIO_INHERIT 1
2529
+
2630
+/* misc */
2731
+#define STDERR_MSG(msg) \
2832
+	(void)write(STDERR_FILENO, (msg), strlen(msg))
2933
+
3034
+
3135
+/*
3236
+ * interceptor for |pthread_mutex_init()|
3337
+ */
3438
+int pthread_mutex_init(pthread_mutex_t *restrict mutex,
3539
+		const pthread_mutexattr_t *restrict attr) {
3640
+	pthread_mutexattr_t *xattr = (pthread_mutexattr_t *)attr;
3741
+	int pmi_res;
3842
+	int pmi_errno;
3943
+
4044
+#if PICFG_PRINT_ONCE_MSG
4145
+	static int once = 0;
4246
+	const char *mymessage = "pthread_interceptor.so: "
4347
+		"Once: In our own pthread_mutex_init()\n";
44-
+	/* just print message ONCE */
48+
+	/* just print message ONCE (BUG: every INT_MAX times) */
4549
+	if (once++ == 0)
4650
+		STDERR_MSG(mymessage);
4751
+#endif /* PICFG_PRINT_ONCE_MSG */
4852
+
4953
+	int (*original_pthread_mutex_init)(pthread_mutex_t *restrict mutex,
5054
+		const pthread_mutexattr_t *restrict attr);
5155
+	original_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
5256
+
5357
+	pthread_mutexattr_t myattr;
5458
+	bool myattr_used = false;
5559
+
5660
+	if (!xattr) {
5761
+		(void)pthread_mutexattr_init(&myattr);
5862
+		myattr_used = true;
5963
+		xattr = &myattr;
6064
+	}
6165
+
6266
+#if PICFG_ENFORCE_PRIO_INHERIT
6367
+	int mutex_protocol = -666;
6468
+	(void)pthread_mutexattr_getprotocol(xattr, &mutex_protocol);
6569
+	if (mutex_protocol != PTHREAD_PRIO_INHERIT) {
6670
+		char msgbuff[256]; /* fixed size, but no |malloc()| allowed here */
6771
+
6872
+		(void)pthread_mutexattr_setprotocol(xattr, PTHREAD_PRIO_INHERIT);
6973
+		(void)snprintf(msgbuff,
7074
+			sizeof(msgbuff),
7175
+			"pthread_interceptor.so: "
7276
+			"PTHREAD_PRIO_INHERIT enforced "
7377
+			"for mutex=0x%lx, was protocol=%d\n",
7478
+			(unsigned long)(void *)mutex,
7579
+			mutex_protocol);
7680
+		STDERR_MSG(msgbuff);
7781
+	}
7882
+#endif /* PICFG_ENFORCE_PRIO_INHERIT */
7983
+
8084
+	pmi_res = (*original_pthread_mutex_init)(mutex, xattr);
8185
+	pmi_errno = errno;
8286
+
8387
+	if (myattr_used) {
8488
+		(void)pthread_mutexattr_destroy(&myattr);
8589
+	}
8690
+
8791
+	errno = pmi_errno;
8892
+	return pmi_res;
8993
+}
9094
--- /dev/null	2022-04-19 22:28:32.062891741 +0200
9195
+++ test1.c	2022-07-06 13:52:16.151772792 +0200
9296
@@ -0,0 +1,27 @@
9397
+/*
9498
+ * test1 for pthread_interceptor.c
9599
+ *
96100
+ */
97101
+
98102
+#include <stdlib.h>
99103
+#include <stdio.h>
100104
+#include <pthread.h>
101105
+#include <errno.h>
102106
+
103107
+int main(int ac, char *av[])
104108
+{
105109
+	(void)puts("# start.");
106110
+
107111
+	pthread_mutex_t mutex1;
108112
+	pthread_mutex_t mutex2;
109113
+
110114
+	(void)pthread_mutex_init(&mutex1, NULL);
111115
+	perror("pthread_mutex_init(1) result");
112116
+
113117
+	(void)pthread_mutex_init(&mutex2, NULL);
114118
+	perror("pthread_mutex_init(2) result");
115119
+
116120
+	(void)puts("# end.");
117121
+
118122
+	return EXIT_SUCCESS;
119123
+}
120124
--- /dev/null	2022-04-19 22:28:32.062891741 +0200
121-
+++ Makefile	2022-07-06 13:50:52.426969179 +0200
125+
+++ Makefile	2022-07-06 19:26:43.063939487 +0200
122-
@@ -0,0 +1,24 @@
126+
@@ -0,0 +1,34 @@
123127
+#
124128
+# Makefile for LD_PRELOAD pthreadinterceptor
125129
+#
126130
+
127131
+CSTDFLAGS=-std=c11
132+
+CARCHFLAGS=-m64 # -m32 or -m64
128133
+CC=gcc # gcc or clang
129134
+
135+
+#
130138
+pthread_interceptor.o: pthread_interceptor.c
131-
+	$(CC) $(CSTDFLAGS) -Wall -g pthread_interceptor.c -fPIC -c -o pthread_interceptor.o
139+
+	$(CC) $(CSTDFLAGS) $(CARCHFLAGS) -Wall -g pthread_interceptor.c -fPIC -c -o pthread_interceptor.o
132140
+pthread_interceptor.so: pthread_interceptor.o
133-
+	$(CC) $(CSTDFLAGS) -Wall -g pthread_interceptor.o -fPIC -shared -ldl -lpthread -o pthread_interceptor.so
141+
+	$(CC) $(CSTDFLAGS) $(CARCHFLAGS) -Wall -g pthread_interceptor.o -fPIC -shared -ldl -lpthread -o pthread_interceptor.so
134142
+
143+
+#
135146
+test1: test1.c
136-
+	$(CC) $(CSTDFLAGS) -Wall -g test1.c -o test1
147+
+	$(CC) $(CSTDFLAGS) $(CARCHFLAGS) -Wall -g test1.c -o test1
137148
+
138149
+run_test: test1 pthread_interceptor.so
139-
+	ksh93 -c 'LD_PRELOAD=$$PWD/pthread_interceptor.so ./test1'
150+
+	ksh93 -c 'LD_PRELOAD="$$PWD/pthread_interceptor.so" ./test1'
140151
+
152+
+#
141155
+all:	run_test
142156
+
143157
+clean:
144158
+	rm -f *.o *.so test1
145159
+
146160
+# EOF.
147161

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