summaryrefslogtreecommitdiffstats
path: root/tests/btparser/thread.at
blob: 5a3976e37c7e53d05a67f24393231e702c899dfa (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# Checking the btparser. -*- Autotest -*-

AT_BANNER([Threads])

## ----------------------- ##
## btp_thread_remove_frame ##
## ----------------------- ##
AT_TESTFUN([btp_thread_remove_frame],
[[
#include <thread.h>
#include <frame.h>
#include <location.h>
#include <utils.h>
#include <assert.h>
#include <stdlib.h>

int main(void)
{
  struct btp_frame *frame2 = btp_frame_new();
  frame2->function_name = btp_strdup("write");
  frame2->number = 2;
  frame2->source_file = btp_strdup("gfileutils.c");
  frame2->source_line = 120;
  frame2->address = 0x322160e7fdULL;

  struct btp_frame *frame1 = btp_frame_new();
  frame1->function_name = btp_strdup("write_to_temp_file");
  frame1->number = 1;
  frame1->source_file = btp_strdup("gfileutils.c");
  frame1->source_line = 980;
  frame1->address = 0x322160e7fdULL;
  frame1->next = frame2;

  struct btp_frame *frame0 = btp_frame_new();
  frame0->function_name = btp_strdup("fsync");
  frame0->number = 0;
  frame0->source_file = btp_strdup("../sysdeps/unix/syscall-template.S");
  frame0->source_line = 82;
  frame0->address = 0x322160e7fdULL;
  frame0->next = frame1;

  struct btp_thread thread;
  btp_thread_init(&thread);
  thread.number = 3;
  thread.frames = frame0;

  /* Remove the frame from the top of the stack. */
  assert(btp_thread_remove_frame(&thread, frame0));
  assert(frame1 == thread.frames);
  assert(frame2 == thread.frames->next);

  /* Remove the frame from the bottom of the stack. */
  assert(btp_thread_remove_frame(&thread, frame2));
  assert(frame1 == thread.frames);
  assert(NULL == thread.frames->next);

  /* Remove the last remaining frame. */
  assert(btp_thread_remove_frame(&thread, frame1));
  assert(NULL == thread.frames);

  /* Remove nonexistant frame -> should return false. */
  assert(!btp_thread_remove_frame(&thread, frame0));
  assert(NULL == thread.frames);

  return 0;
}
]])

## ------------------------------ ##
## btp_thread_remove_frames_above ##
## ------------------------------ ##
AT_TESTFUN([btp_thread_remove_frames_above],
[[
#include <thread.h>
#include <frame.h>
#include <location.h>
#include <utils.h>
#include <assert.h>
#include <stdlib.h>

int main(void)
{
  struct btp_frame *frame2 = btp_frame_new();
  frame2->function_name = btp_strdup("write");
  frame2->number = 2;
  frame2->source_file = btp_strdup("gfileutils.c");
  frame2->source_line = 120;
  frame2->address = 0x322160e7fdULL;

  struct btp_frame *frame1 = btp_frame_new();
  frame1->function_name = btp_strdup("write_to_temp_file");
  frame1->number = 1;
  frame1->source_file = btp_strdup("gfileutils.c");
  frame1->source_line = 980;
  frame1->address = 0x322160e7fdULL;
  frame1->next = frame2;

  struct btp_frame *frame0 = btp_frame_new();
  frame0->function_name = btp_strdup("fsync");
  frame0->number = 0;
  frame0->source_file = btp_strdup("../sysdeps/unix/syscall-template.S");
  frame0->source_line = 82;
  frame0->address = 0x322160e7fdULL;
  frame0->next = frame1;

  struct btp_thread thread;
  btp_thread_init(&thread);
  thread.number = 3;
  thread.frames = frame0;

  /* Remove the frames above the top of the stack. */
  assert(btp_thread_remove_frames_above(&thread, frame0));
  assert(frame0 == thread.frames);
  assert(frame1 == thread.frames->next);

  /* Remove the frames above the second frame from the top of the stack. */
  assert(btp_thread_remove_frames_above(&thread, frame1));
  assert(frame1 == thread.frames);
  assert(frame2 == thread.frames->next);

  /* Remove the frames above the bottom of the stack. */
  assert(btp_thread_remove_frames_above(&thread, frame2));
  assert(frame2 == thread.frames);
  assert(NULL == thread.frames->next);

  /* Remove frames above a nonexistant frame -> should return false. */
  assert(!btp_thread_remove_frames_above(&thread, frame0));
  assert(frame2 == thread.frames);

  return 0;
}
]])

## -------------------------------- ##
## btp_thread_remove_frames_below_n ##
## -------------------------------- ##
AT_TESTFUN([btp_thread_remove_frames_below_n],
[[
#include <thread.h>
#include <frame.h>
#include <location.h>
#include <utils.h>
#include <assert.h>
#include <stdlib.h>

int main(void)
{
  struct btp_frame *frame2 = btp_frame_new();
  frame2->function_name = btp_strdup("write");
  frame2->number = 2;
  frame2->source_file = btp_strdup("gfileutils.c");
  frame2->source_line = 120;
  frame2->address = 0x322160e7fdULL;

  struct btp_frame *frame1 = btp_frame_new();
  frame1->function_name = btp_strdup("write_to_temp_file");
  frame1->number = 1;
  frame1->source_file = btp_strdup("gfileutils.c");
  frame1->source_line = 980;
  frame1->address = 0x322160e7fdULL;
  frame1->next = frame2;

  struct btp_frame *frame0 = btp_frame_new();
  frame0->function_name = btp_strdup("fsync");
  frame0->number = 0;
  frame0->source_file = btp_strdup("../sysdeps/unix/syscall-template.S");
  frame0->source_line = 82;
  frame0->address = 0x322160e7fdULL;
  frame0->next = frame1;

  struct btp_thread thread;
  btp_thread_init(&thread);
  thread.number = 3;
  thread.frames = frame0;

  /* Remove no frame as n is too large. */
  btp_thread_remove_frames_below_n(&thread, 5);
  assert(frame0 == thread.frames);
  assert(frame1 == thread.frames->next);
  assert(frame2 == thread.frames->next->next);
  assert(NULL == thread.frames->next->next->next);

  /* Remove the frames below 1. */
  btp_thread_remove_frames_below_n(&thread, 1);
  assert(frame0 == thread.frames);
  assert(NULL == thread.frames->next);

  /* Remove the frames below 0. */
  btp_thread_remove_frames_below_n(&thread, 0);
  assert(NULL == thread.frames);

  /* Try to remove frames when no frame is present. */
  btp_thread_remove_frames_below_n(&thread, 4);
  assert(NULL == thread.frames);

  return 0;
}
]])

## ---------------- ##
## btp_thread_parse ##
## ---------------- ##

AT_TESTFUN([btp_thread_parse],
[[
#include <thread.h>
#include <frame.h>
#include <location.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

void check(char *input,
           struct btp_thread *expected_thread)
{
  printf("===============================================\n"
         "Testing input: %s",
	 input);

  char *old_input = input;
  struct btp_location location;
  btp_location_init(&location);
  struct btp_thread *thread = btp_thread_parse(&input, &location);
  assert(!expected_thread || thread);
  if (thread)
  {
    assert(*input == '\0');
    assert(btp_thread_cmp(thread, expected_thread) == 0);
    btp_thread_free(thread);
  }
  else
  {
    /* Check that the pointer is not moved. */
    assert(old_input == input);
    assert(!expected_thread);
  }
}

int main(void)
{
  /* Basic test. */
  struct btp_frame frame0, frame1;
  btp_frame_init(&frame0);
  frame0.function_name = "fsync";
  frame0.number = 0;
  frame0.source_file = "../sysdeps/unix/syscall-template.S";
  frame0.source_line = 82;
  frame0.address = 0x322160e7fdULL;
  frame0.next = &frame1;

  btp_frame_init(&frame1);
  frame1.function_name = "write_to_temp_file";
  frame1.number = 1;
  frame1.source_file = "gfileutils.c";
  frame1.source_line = 980;
  frame1.address = 0x322160e7fdULL;

  struct btp_thread thread;
  btp_thread_init(&thread);
  thread.number = 3;
  thread.frames = &frame0;

  check("Thread 3 (Thread 11917):\n"
	"#0  0x000000322160e7fd in fsync () at ../sysdeps/unix/syscall-template.S:82\n"
	"No locals.\n"
	"#1  0x000000322222987a in write_to_temp_file (\n"
	"    filename=0x18971b0 \"/home/jfclere/.recently-used.xbel\", \n"
	"    contents=<value optimized out>, length=29917, error=0x7fff3cbe4110)\n"
	"    at gfileutils.c:980\n"
	"        statbuf = {st_dev = 64768, st_ino = 13709, st_nlink = 1, \n"
	"          st_mode = 33152, st_uid = 500, st_gid = 500, __pad0 = 0, \n"
	"          st_rdev = 0, st_size = 29917, st_blksize = 4096, st_blocks = 64, \n"
	"          st_atim = {tv_sec = 1273231242, tv_nsec = 73521863}, st_mtim = {\n"
	"            tv_sec = 1273231242, tv_nsec = 82521015}, st_ctim = {\n"
	"            tv_sec = 1273231242, tv_nsec = 190522021}, __unused = {0, 0, 0}}\n",
	&thread);

  /* Another format of the header. */
  check("Thread 3 (LWP 635):\n"
	"#0  0x000000322160e7fd in fsync () at ../sysdeps/unix/syscall-template.S:82\n"
	"No locals.\n"
	"#1  0x000000322222987a in write_to_temp_file () at gfileutils.c:980\n"
	"No locals.\n",
	&thread);

  return 0;
}
]])

## -------------------------- ##
## btp_thread_parse-locations ##
## -------------------------- ##

AT_TESTFUN([btp_thread_parse-locations],
[[
#include <thread.h>
#include <frame.h>
#include <location.h>
#include <assert.h>
#include <stdlib.h>

/**
 * Checks that the thread parser fails on ceratin location
 * (line and column), with an error message.
 */
void check(char *input,
           int expected_line,
	   int expected_column)
{
  char *old_input = input;
  struct btp_location location;
  btp_location_init(&location);
  assert(NULL == btp_thread_parse(&input, &location));

  /* Check that the pointer is not moved. */
  assert(old_input == input);

  /* Check the location. */
  printf("location %d:%d '%s', expected %d:%d\n",
         location.line, location.column, location.message,
	 expected_line, expected_column);

  assert(location.line == expected_line);
  assert(location.column == expected_column);
  assert(location.message);
  assert(location.message[0] != '\0');
}

int main(void)
{
  /* Thread keyword */
  check("Thraed 3 (Thread 11917):\n", 1, 0);

  /* Spaces after the thread keyword. */
  check("Thread3 (Thread 11917):\n", 1, 6);

  /* Thread number. */
  check("Thread  a (Thread 11917):\n", 1, 8);

  /* Spaces after the thread number. */
  check("Thread 8(Thread 11917):\n", 1, 8);

  /* A parenthesis. */
  check("Thread 8 (11917):\n", 1, 9);

  /* Second number. */
  check("Thread 8 (Thread ffff):\n", 1, 17);

  /* Semicolon missing. */
  check("Thread 8 (Thread 1)\n", 1, 18);

  /* Not a single frame. */
  check("Thread 3 (Thread 11917):\n\n", 2, 0);

  return 0;
}
]])

## ------------------- ##
## btp_thread_skip_lwp ##
## ------------------- ##

AT_TESTFUN([btp_thread_skip_lwp],
[[
#include <thread.h>
#include <assert.h>
#include <stdlib.h>

void check(char *input,
           int expected_count)
{
  char *old_input = input;
  assert(expected_count == btp_thread_skip_lwp(&input));
  assert(input - old_input == expected_count);
}

int main(void)
{
  check("(LWP 20)", 8);
  check("(LWP 20)10", 8);
  check("(LWP 245443452355454343450)", 27);
  check("(LWP 245443452355454343450)  ", 27);

  check("", 0);
  check(" ", 0);
  check(" (LWP 20)", 0);
  check("(LWP", 0);
  check("(LWP 20", 0);
  check("(LWP )", 0);
  check("(LWP 20()", 0);
  check("(LWP 0x0)", 0);
  check("(LWP 20(", 0);

  return 0;
}
]])