pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post 9fe by Anonymous on Mon 1st Aug 2022 16:32
original post f76c47 by Anonymous on Wed 6th Jul 2022 18:29
Download diff
Show old version | new version | both versions

    
1-
--- /dev/null	2022-04-19 22:28:32.062891741 +0200
1+
--- /dev/null	2022-07-25 11:42:10.933510077 +0200
2-
+++ pthread_interceptor.c	2022-07-06 19:28:52.893103035 +0200
2+
+++ pthread_interceptor/Makefile	2022-08-01 15:09:58.790413283 +0200
3-
@@ -0,0 +1,90 @@
3+
@@ -0,0 +1,53 @@
4-
+/*
4+
+#
5-
+ * pthread_interceptor.c
5+
+# Makefile for LD_PRELOAD pthreadinterceptor
6-
+ *
6+
+#
157
+
16-
+#define _GNU_SOURCE 1 /* needed for |RTLD_NEXT| */
8+
+#
9+
+# ToDo:
1713
+
18-
+#include <pthread.h>
14+
+CSTDFLAGS=-std=c17
19-
+#include <unistd.h>
15+
+CARCHFLAGS=-m32 # -m32 or -m64
20-
+#include <errno.h>
16+
+CC=gcc # gcc or clang
2517
+
26-
+/* config */
18+
+#
27-
+#define PICFG_PRINT_ONCE_MSG 1
19+
+# misc
28-
+#define PICFG_ENFORCE_PRIO_INHERIT 1
20+
+#
21+
+all:	run_test pthr_inter_binaries.tar.bz2
2922
+
30-
+/* misc */
23+
+clean:
31-
+#define STDERR_MSG(msg) \
24+
+	rm -f *.o *.so test1 pthr_inter_binaries.tar.bz2
3325
+
26+
+#
3433
+
35-
+/*
34+
+#
36-
+ * interceptor for |pthread_mutex_init()|
35+
+# tests
37-
+ */
36+
+#
4337
+
44-
+#if PICFG_PRINT_ONCE_MSG
38+
+# -rdynamic is required so that we get accurate function names
45-
+	static int once = 0;
39+
+# from |backtrace()|&co
46-
+	const char *mymessage = "pthread_interceptor.so: "
40+
+test1: test1.c
47-
+		"Once: In our own pthread_mutex_init()\n";
41+
+	$(CC) $(CSTDFLAGS) $(CARCHFLAGS) -Wall -rdynamic -g test1.c -o test1
5242
+
53-
+	int (*original_pthread_mutex_init)(pthread_mutex_t *restrict mutex,
43+
+run_test: test1 pthread_interceptor.so pthread_interceptor_wrapper.ksh
54-
+		const pthread_mutexattr_t *restrict attr);
44+
+	@printf "# Begin tests.\n"
55-
+	original_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
45+
+	ksh93 pthread_interceptor_wrapper.ksh ./test1
46+
+	@printf "# End tests.\n"
5647
+
57-
+	pthread_mutexattr_t myattr;
48+
+#
58-
+	bool myattr_used = false;
49+
+# package binaries
50+
+#
5954
+
6555
+
66-
+#if PICFG_ENFORCE_PRIO_INHERIT
56+
+# EOF.
67-
+	int mutex_protocol = -666;
57+
--- /dev/null	2022-07-25 11:42:10.933510077 +0200
68-
+	(void)pthread_mutexattr_getprotocol(xattr, &mutex_protocol);
58+
+++ pthread_interceptor/pthread_interceptor.c	2022-08-01 15:29:50.137635263 +0200
69-
+	if (mutex_protocol != PTHREAD_PRIO_INHERIT) {
59+
@@ -0,0 +1,118 @@
70-
+		char msgbuff[256]; /* fixed size, but no |malloc()| allowed here */
60+
+/*
71-
+
61+
+ * pthread_interceptor.c
72-
+		(void)pthread_mutexattr_setprotocol(xattr, PTHREAD_PRIO_INHERIT);
62+
+ *
73-
+		(void)snprintf(msgbuff,
63+
+ * intercept pthread calls
74-
+			sizeof(msgbuff),
64+
+ * (like |pthread_mutex_init()|) via LD_PRELOAD
75-
+			"pthread_interceptor.so: "
65+
+ *
76-
+			"PTHREAD_PRIO_INHERIT enforced "
66+
+ * Example usage:
77-
+			"for mutex=0x%lx, was protocol=%d\n",
67+
+ * $ ksh93 -c 'LD_PRELOAD="$PWD/pthread_interceptor.so" ./test_prog'
78-
+			(unsigned long)(void *)mutex,
68+
+ *
79-
+			mutex_protocol);
69+
+ * Written by Roland Mainz <roland.mainz@nrubsig.org>
80-
+		STDERR_MSG(msgbuff);
70+
+ */
8371
+
84-
+	pmi_res = (*original_pthread_mutex_init)(mutex, xattr);
72+
+#define _GNU_SOURCE 1 /* needed for |RTLD_NEXT| */
8673
+
87-
+	if (myattr_used) {
74+
+#include <pthread.h>
88-
+		(void)pthread_mutexattr_destroy(&myattr);
75+
+#include <unistd.h>
89-
+	}
76+
+#include <errno.h>
77+
+#include <stdio.h>
9082
+
91-
+	errno = pmi_errno;
83+
+/* config */
92-
+	return pmi_res;
84+
+#define PICFG_PRINT_ONCE_MSG		1
93-
+}
85+
+#define PICFG_ENFORCE_PRIO_INHERIT	1
94-
--- /dev/null	2022-04-19 22:28:32.062891741 +0200
86+
+#define PICFG_PRINT_BACKTRACE		1
10187
+
102-
+#include <stdlib.h>
88+
+/* misc */
103-
+#include <stdio.h>
89+
+#define STDERR_MSG(msg) \
104-
+#include <pthread.h>
90+
+	(void)write(STDERR_FILENO, (msg), strlen(msg))
10691
+
11092
+
111-
+	pthread_mutex_t mutex1;
93+
+#if PICFG_PRINT_ONCE_MSG
112-
+	pthread_mutex_t mutex2;
94+
+static pthread_once_t picfg_init_once = PTHREAD_ONCE_INIT;
11395
+
114-
+	(void)pthread_mutex_init(&mutex1, NULL);
96+
+static
115-
+	perror("pthread_mutex_init(1) result");
97+
+void picfg_init(void)
98+
+{
116104
+
119105
+
121106
+
122-
+	return EXIT_SUCCESS;
107+
+/*
123-
+}
108+
+ * interceptor for |pthread_mutex_init()|
124-
--- /dev/null	2022-04-19 22:28:32.062891741 +0200
109+
+ */
125-
+++ Makefile	2022-07-06 19:26:43.063939487 +0200
110+
+int pthread_mutex_init(pthread_mutex_t *restrict mutex,
126-
@@ -0,0 +1,34 @@
111+
+		const pthread_mutexattr_t *restrict attr) {
127-
+#
112+
+	pthread_mutexattr_t *xattr = (pthread_mutexattr_t *)attr;
128-
+# Makefile for LD_PRELOAD pthreadinterceptor
113+
+	int pmi_res;
129-
+#
114+
+	int pmi_errno;
130115
+
131-
+CSTDFLAGS=-std=c11
116+
+#if PICFG_PRINT_ONCE_MSG
132-
+CARCHFLAGS=-m64 # -m32 or -m64
117+
+	(void)pthread_once(&picfg_init_once, picfg_init);
133-
+CC=gcc # gcc or clang
118+
+#endif /* PICFG_PRINT_ONCE_MSG */
134119
+
135-
+#
120+
+	int (*original_pthread_mutex_init)(pthread_mutex_t *restrict mutex,
136-
+# build LD_PRELOAD function interceptor
121+
+		const pthread_mutexattr_t *restrict attr);
137-
+#
122+
+	original_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
142123
+
143-
+#
124+
+	pthread_mutexattr_t myattr;
144-
+# tests
125+
+	bool myattr_used = false;
148126
+
149-
+run_test: test1 pthread_interceptor.so
127+
+	if (!xattr) {
150-
+	ksh93 -c 'LD_PRELOAD="$$PWD/pthread_interceptor.so" ./test1'
128+
+		(void)pthread_mutexattr_init(&myattr);
129+
+		myattr_used = true;
151132
+
152-
+#
133+
+#if PICFG_ENFORCE_PRIO_INHERIT
153-
+# misc
134+
+	int mutex_protocol = -666;
154-
+#
135+
+	(void)pthread_mutexattr_getprotocol(xattr, &mutex_protocol);
155-
+all:	run_test
136+
+	if (mutex_protocol != PTHREAD_PRIO_INHERIT) {
137+
+		char msgbuff[256]; /* fixed size, but no |malloc()| allowed here */
156138
+
157-
+clean:
139+
+		(void)pthread_mutexattr_setprotocol(xattr, PTHREAD_PRIO_INHERIT);
158-
+	rm -f *.o *.so test1
140+
+		(void)snprintf(msgbuff,
159-
+
141+
+			sizeof(msgbuff),
160-
+# EOF.
142+
+			"# pthread_interceptor.so: "
161-
143+
+			"PTHREAD_PRIO_INHERIT enforced "
162-
144+
+			"for mutex=0x%lx, was protocol=%d\n",
163-
145+
+			(unsigned long)(void *)mutex,
164-
146+
+			mutex_protocol);
165-
147+
+		STDERR_MSG(msgbuff);
166-
148+
+
167-
149+
+#if PICFG_PRINT_BACKTRACE
168-
150+
+#define BT_BUFF_ELEMENTS 256
169-
151+
+#define MYMIN(a,b) (((a) < (b)) ? (a) : (b))
170-
152+
+		/*
171-
153+
+		 * print backtrace
172-
154+
+		 * Might cause a gross mix of marmelade if multiple
173-
155+
+		 * threads write at the same time, and syslog is used
174-
156+
+		 * to log stderr
175-
157+
+		 * To limit the madness we restrict the backtrace to
176-
158+
+		 * four lines
177-
159+
+		 */
178-
160+
+		void *bt_buffer[BT_BUFF_ELEMENTS];
179-
161+
+		int nptrs;
180-
162+
+		nptrs = backtrace(bt_buffer, BT_BUFF_ELEMENTS);
181-
163+
+		backtrace_symbols_fd(bt_buffer, MYMIN(nptrs,4), STDERR_FILENO);
182-
164+
+	}
183-
165+
+#endif /* PICFG_PRINT_BACKTRACE */
184-
166+
+#endif /* PICFG_ENFORCE_PRIO_INHERIT */
185-
167+
+
186-
168+
+	pmi_res = (*original_pthread_mutex_init)(mutex, xattr);
187-
169+
+	pmi_errno = errno;
188-
170+
+
189-
171+
+	if (myattr_used) {
190-
172+
+		(void)pthread_mutexattr_destroy(&myattr);
191-
173+
+	}
192-
174+
+
193-
175+
+	errno = pmi_errno;
194-
176+
+	return pmi_res;
195-
177+
+}
196-
178+
--- /dev/null	2022-07-25 11:42:10.933510077 +0200
197-
179+
+++ pthread_interceptor/pthread_interceptor_wrapper.ksh	2022-08-01 11:48:14.603478675 +0200
198-
180+
@@ -0,0 +1,12 @@
199-
181+
+#!/bin/ksh93
200-
182+
+
201-
183+
+#
202-
184+
+# pthread_interceptor wrapper script
203-
185+
+#
204-
186+
+
205-
187+
+export LD_PRELOAD="$PWD/pthread_interceptor.so:$LD_PRELOAD"
206-
188+
+
207-
189+
+"$@"
208-
190+
+
209-
191+
+# EOF.
210-
192+
+
211-
193+
--- /dev/null	2022-07-25 11:42:10.933510077 +0200
212-
194+
+++ pthread_interceptor/test1.c	2022-08-01 14:54:55.634801754 +0200
213-
195+
@@ -0,0 +1,30 @@
214-
196+
+/*
215-
197+
+ * test1 for pthread_interceptor.c
216-
198+
+ *
217-
199+
+ */
218-
200+
+
219-
201+
+#include <stdlib.h>
220-
202+
+#include <stdio.h>
221-
203+
+#include <pthread.h>
222-
204+
+#include <errno.h>
223-
205+
+
224-
206+
+void run_tests1(void)
225-
207+
+{
226-
208+
+	pthread_mutex_t mutex1;
227-
209+
+	pthread_mutex_t mutex2;
228-
210+
+
229-
211+
+	(void)pthread_mutex_init(&mutex1, NULL);
230-
212+
+	perror("pthread_mutex_init(mutex1) result");
231-
213+
+
232-
214+
+	(void)pthread_mutex_init(&mutex2, NULL);
233-
215+
+	perror("pthread_mutex_init(mutex2) result");
234-
216+
+}
235-
217+
+
236-
218+
+int main(int ac, char *av[])
237-
219+
+{
238-
220+
+	(void)puts("# start.");
239-
221+
+	run_tests1();
240-
222+
+	(void)puts("# end.");
241-
223+
+
242-
224+
+	return EXIT_SUCCESS;
243-
225+
+}
244226

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