1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
#include <cstdlib>
#include <cstring>
#include <errno.h>
#include <inttypes.h>
#include <pthread.h>
#include <setjmp.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <vector>
#if defined(__APPLE__)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2)
int pthread_threadid_np(pthread_t,__uint64_t*);
#elif defined(__linux__)
#include <sys/syscall.h>
#endif
static const char *const RETVAL_PREFIX = "retval:";
static const char *const SLEEP_PREFIX = "sleep:";
static const char *const STDERR_PREFIX = "stderr:";
static const char *const THREAD_PREFIX = "thread:";
static const char *const THREAD_COMMAND_NEW = "new";
static const char *const THREAD_COMMAND_PRINT_IDS = "print-ids";
static const char *const THREAD_COMMAND_SEGFAULT = "segfault";
static bool g_print_thread_ids = false;
static pthread_mutex_t g_print_mutex = PTHREAD_MUTEX_INITIALIZER;
static bool g_threads_do_segfault = false;
static pthread_mutex_t g_jump_buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
static jmp_buf g_jump_buffer;
static bool g_is_segfaulting = false;
static void
print_thread_id ()
{
// Put in the right magic here for your platform to spit out the thread id (tid) that debugserver/lldb-gdbserver would see as a TID.
// Otherwise, let the else clause print out the unsupported text so that the unit test knows to skip verifying thread ids.
#if defined(__APPLE__)
__uint64_t tid = 0;
pthread_threadid_np(pthread_self(), &tid);
printf ("%" PRIx64, tid);
#elif defined (__linux__)
// This is a call to gettid() via syscall.
printf ("%" PRIx64, static_cast<uint64_t> (syscall (__NR_gettid)));
#else
printf("{no-tid-support}");
#endif
}
static void
signal_handler (int signo)
{
const char *signal_name = NULL;
switch (signo)
{
case SIGUSR1: signal_name = "SIGUSR1"; break;
case SIGSEGV: signal_name = "SIGSEGV"; break;
default: signal_name = NULL;
}
// Print notice that we received the signal on a given thread.
pthread_mutex_lock (&g_print_mutex);
if (signal_name)
printf ("received %s on thread id: ", signal_name);
else
printf ("received signo %d (%s) on thread id: ", signo, strsignal (signo));
print_thread_id ();
printf ("\n");
pthread_mutex_unlock (&g_print_mutex);
// Reset the signal handler if we're one of the expected signal handlers.
switch (signo)
{
case SIGSEGV:
// Fix up the pointer we're writing to. This needs to happen if nothing intercepts the SIGSEGV
// (i.e. if somebody runs this from the command line).
longjmp(g_jump_buffer, 1);
break;
case SIGUSR1:
if (g_is_segfaulting)
{
// Fix up the pointer we're writing to. This is used to test gdb remote signal delivery.
// A SIGSEGV will be raised when the thread is created, switched out for a SIGUSR1, and
// then this code still needs to fix the seg fault.
// (i.e. if somebody runs this from the command line).
longjmp(g_jump_buffer, 1);
}
break;
}
// Reset the signal handler.
sig_t sig_result = signal (signo, signal_handler);
if (sig_result == SIG_ERR)
{
fprintf(stderr, "failed to set signal handler: errno=%d\n", errno);
exit (1);
}
}
static void*
thread_func (void *arg)
{
static pthread_mutex_t s_thread_index_mutex = PTHREAD_MUTEX_INITIALIZER;
static int s_thread_index = 1;
pthread_mutex_lock (&s_thread_index_mutex);
const int this_thread_index = s_thread_index++;
pthread_mutex_unlock (&s_thread_index_mutex);
if (g_print_thread_ids)
{
pthread_mutex_lock (&g_print_mutex);
printf ("thread %d id: ", this_thread_index);
print_thread_id ();
printf ("\n");
pthread_mutex_unlock (&g_print_mutex);
}
if (g_threads_do_segfault)
{
// Sleep for a number of seconds based on the thread index.
// TODO add ability to send commands to test exe so we can
// handle timing more precisely. This is clunky. All we're
// trying to do is add predictability as to the timing of
// signal generation by created threads.
int sleep_seconds = 2 * (this_thread_index - 1);
while (sleep_seconds > 0)
sleep_seconds = sleep(sleep_seconds);
// Test creating a SEGV.
pthread_mutex_lock (&g_jump_buffer_mutex);
g_is_segfaulting = true;
int *bad_p = NULL;
if (setjmp(g_jump_buffer) == 0)
{
// Force a seg fault signal on this thread.
*bad_p = 0;
}
else
{
// Tell the system we're no longer seg faulting.
// Used by the SIGUSR1 signal handler that we inject
// in place of the SIGSEGV so it only tries to
// recover from the SIGSEGV if this seg fault code
// was in play.
g_is_segfaulting = false;
}
pthread_mutex_unlock (&g_jump_buffer_mutex);
pthread_mutex_lock (&g_print_mutex);
printf ("thread ");
print_thread_id ();
printf (": past SIGSEGV\n");
pthread_mutex_unlock (&g_print_mutex);
}
int sleep_seconds_remaining = 5;
while (sleep_seconds_remaining > 0)
{
sleep_seconds_remaining = sleep (sleep_seconds_remaining);
}
return NULL;
}
int main (int argc, char **argv)
{
std::vector<pthread_t> threads;
int return_value = 0;
// Set the signal handler.
sig_t sig_result = signal (SIGALRM, signal_handler);
if (sig_result == SIG_ERR)
{
fprintf(stderr, "failed to set SIGALRM signal handler: errno=%d\n", errno);
exit (1);
}
sig_result = signal (SIGUSR1, signal_handler);
if (sig_result == SIG_ERR)
{
fprintf(stderr, "failed to set SIGUSR1 handler: errno=%d\n", errno);
exit (1);
}
sig_result = signal (SIGSEGV, signal_handler);
if (sig_result == SIG_ERR)
{
fprintf(stderr, "failed to set SIGUSR1 handler: errno=%d\n", errno);
exit (1);
}
// Process command line args.
for (int i = 1; i < argc; ++i)
{
if (std::strstr (argv[i], STDERR_PREFIX))
{
// Treat remainder as text to go to stderr.
fprintf (stderr, "%s\n", (argv[i] + strlen (STDERR_PREFIX)));
}
else if (std::strstr (argv[i], RETVAL_PREFIX))
{
// Treat as the return value for the program.
return_value = std::atoi (argv[i] + strlen (RETVAL_PREFIX));
}
else if (std::strstr (argv[i], SLEEP_PREFIX))
{
// Treat as the amount of time to have this process sleep (in seconds).
int sleep_seconds_remaining = std::atoi (argv[i] + strlen (SLEEP_PREFIX));
// Loop around, sleeping until all sleep time is used up. Note that
// signals will cause sleep to end early with the number of seconds remaining.
for (int i = 0; sleep_seconds_remaining > 0; ++i)
{
sleep_seconds_remaining = sleep (sleep_seconds_remaining);
// std::cout << "sleep result (call " << i << "): " << sleep_seconds_remaining << std::endl;
}
}
else if (std::strstr (argv[i], THREAD_PREFIX))
{
// Check if we're creating a new thread.
if (std::strstr (argv[i] + strlen(THREAD_PREFIX), THREAD_COMMAND_NEW))
{
// Create a new thread.
pthread_t new_thread;
const int err = ::pthread_create (&new_thread, NULL, thread_func, NULL);
if (err)
{
fprintf (stderr, "pthread_create() failed with error code %d\n", err);
exit (err);
}
threads.push_back (new_thread);
}
else if (std::strstr (argv[i] + strlen(THREAD_PREFIX), THREAD_COMMAND_PRINT_IDS))
{
// Turn on thread id announcing.
g_print_thread_ids = true;
// And announce us.
pthread_mutex_lock (&g_print_mutex);
printf ("thread 0 id: ");
print_thread_id ();
printf ("\n");
pthread_mutex_unlock (&g_print_mutex);
}
else if (std::strstr (argv[i] + strlen(THREAD_PREFIX), THREAD_COMMAND_SEGFAULT))
{
g_threads_do_segfault = true;
}
else
{
// At this point we don't do anything else with threads.
// Later use thread index and send command to thread.
}
}
else
{
// Treat the argument as text for stdout.
printf("%s\n", argv[i]);
}
}
// If we launched any threads, join them
for (std::vector<pthread_t>::iterator it = threads.begin (); it != threads.end (); ++it)
{
void *thread_retval = NULL;
const int err = ::pthread_join (*it, &thread_retval);
if (err != 0)
fprintf (stderr, "pthread_join() failed with error code %d\n", err);
}
return return_value;
}
|