-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht-stacktrace.c
More file actions
74 lines (61 loc) · 1.27 KB
/
t-stacktrace.c
File metadata and controls
74 lines (61 loc) · 1.27 KB
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
/*
*
* t-stacktrace.c
*
* Author: Markku Rossi <[email protected]>
*
* Copyright (c) 2016 Markku Rossi.
*
* See the LICENSE file for the details on licensing.
*
* Unit tests for stacktrace.
*
*/
#include "piincludes.h"
#include "pistacktrace.h"
struct Count
{
void *ptr;
unsigned int count;
};
typedef struct Count Count;
static unsigned int
recursive(int level, void **stack_trace, unsigned int stack_trace_depth)
{
if (level > 0)
return recursive(level - 1, stack_trace, stack_trace_depth);
return pi_stack_trace_set(stack_trace, stack_trace_depth);
}
static void
count_ptr(void *ptr, Count *counts)
{
int i;
for (i = 0; counts[i].ptr; i++)
if (counts[i].ptr == ptr)
break;
counts[i].ptr = ptr;
counts[i].count++;
}
int
main(int argc, char *argv[])
{
void *stack_trace[20];
Count counts[20] = {0};
int level = 10;
int depth;
int i;
depth = recursive(level, stack_trace, 20);
/* Count program counter occurrences. */
for (i = 0; i < depth; i++)
count_ptr(stack_trace[i], counts);
/* At least one PC must occur `level' times. */
for (i = 0; counts[i].ptr; i++)
if (counts[i].count >= level)
break;
if (!counts[i].ptr)
{
fprintf(stderr, "Could not find recursion PC!\n");
exit(1);
}
return 0;
}