pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post 0ae238f15 by Anonymous on Wed 6th Jul 2022 11:58
original post e21dfbc03a by Anonymous on Wed 6th Jul 2022 10:20
Download diff
Show old version | new version | both versions

    
11
--- /dev/null	2022-04-19 22:28:32.062891741 +0200
2-
+++ pthread_mutex_init_log.c	2022-07-06 11:16:54.162382585 +0200
2+
+++ pthread_mutex_init_log.c	2022-07-06 12:57:16.111555681 +0200
3-
@@ -0,0 +1,22 @@
3+
@@ -0,0 +1,64 @@
44
+#define _GNU_SOURCE 1 /* needed for |RTLD_NEXT| */
55
+
66
+#include <pthread.h>
77
+#include <unistd.h>
8+
+#include <errno.h>
810
+#include <string.h>
11+
+#include <stdbool.h>
912
+#include <dlfcn.h>
1013
+
1114
+static int once = 0;
1215
+
16+
+#define STDERR_MSG(msg) \
1319
+int pthread_mutex_init(pthread_mutex_t *restrict mutex,
1420
+		const pthread_mutexattr_t *restrict attr) {
15-
+	const char *mymessage = "Interceptor: In our own pthread_mutex_init()\n";
21+
+	pthread_mutexattr_t *xattr = (pthread_mutexattr_t *)attr;
22+
+	int pmi_res;
1624
+
25+
+	const char *mymessage = "pthread_mutex_init_log.so: In our own pthread_mutex_init()\n";
1726
+	/* just print message ONCE */
1827
+	if (once++ == 0)
19-
+		(void)write(2, mymessage, strlen(mymessage));
28+
+		STDERR_MSG(mymessage);
2029
+
2130
+	int (*original_pthread_mutex_init)(pthread_mutex_t *restrict mutex,
2231
+		const pthread_mutexattr_t *restrict attr);
2332
+	original_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
29-
+#include <pthread.h>
30-
+#include <errno.h>
31-
+#include <stdio.h>
3233
+
33-
+int main(void) {
34+
+	pthread_mutexattr_t myattr;
34-
+	(void)puts("# start.");
35+
+	bool myattr_used = false;
3536
+
36-
+	pthread_mutex_t mutex;
37+
+	if (!xattr) {
38+
+		pthread_mutexattr_init(&myattr);
3742
+
38-
+	pthread_mutex_init(&mutex, NULL);
43+
+	int mutex_protocol = -666;
39-
+	perror("pthread_mutex_init() result");
44+
+	(void)pthread_mutexattr_getprotocol(xattr, &mutex_protocol);
45+
+	if (mutex_protocol != PTHREAD_PRIO_INHERIT) {
4047
+
41-
+	(void)puts("# end.");
48+
+		pthread_mutexattr_setprotocol(xattr, PTHREAD_PRIO_INHERIT);
49+
+		snprintf(msgbuff,
4257
+
43-
+	return 0;
58+
+	pmi_res = (*original_pthread_mutex_init)(mutex, xattr);
59+
+	pmi_errno = errno;
4860
+
49-
+pthread_mutex_init_log.o: pthread_mutex_init_log.c
61+
+	if (myattr_used) {
50-
+	gcc -std=gnu11 -g pthread_mutex_init_log.c -fPIC -c -o pthread_mutex_init_log.o
62+
+		pthread_mutexattr_destroy(&myattr);
51-
+pthread_mutex_init_log.so: pthread_mutex_init_log.o
63+
+	}
5364
+
54-
+test1: test1.c
65+
+	errno = pmi_errno;
55-
+	gcc -std=gnu11 -g test1.c -o test1
66+
+	return pmi_res;
67+
+}
5674
+
57-
+run_test: test1 pthread_mutex_init_log.so
75+
+int main(void) {
58-
+	ksh93 -c 'LD_PRELOAD=$$PWD/pthread_mutex_init_log.so ./test1'
76+
+	(void)puts("# start.");
5977
+
60-
+clean:
78+
+	pthread_mutex_t mutex;
61-
+	rm -f *.o *.so test1
79+
+
62-
80+
+	pthread_mutex_init(&mutex, NULL);
63-
81+
+	perror("pthread_mutex_init() result");
64-
82+
+
65-
83+
+	(void)puts("# end.");
66-
84+
+
67-
85+
+	return 0;
68-
86+
+}
69-
87+
--- /dev/null	2022-04-19 22:28:32.062891741 +0200
70-
88+
+++ Makefile	2022-07-06 12:55:59.122798683 +0200
71-
89+
@@ -0,0 +1,14 @@
72-
90+
+
73-
91+
+pthread_mutex_init_log.o: pthread_mutex_init_log.c
74-
92+
+	gcc -std=gnu11 -g pthread_mutex_init_log.c -fPIC -c -o pthread_mutex_init_log.o
75-
93+
+pthread_mutex_init_log.so: pthread_mutex_init_log.o
76-
94+
+	gcc -std=gnu11 -g pthread_mutex_init_log.o -fPIC -shared -ldl -lpthread -o pthread_mutex_init_log.so
77-
95+
+
78-
96+
+test1: test1.c
79-
97+
+	gcc -std=gnu11 -g test1.c -o test1
80-
98+
+
81-
99+
+run_test: test1 pthread_mutex_init_log.so
82-
100+
+	ksh93 -c 'LD_PRELOAD=$$PWD/pthread_mutex_init_log.so ./test1'
83-
101+
+
84-
102+
+clean:
85-
103+
+	rm -f *.o *.so test1
86104

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