-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathdebug.c
72 lines (59 loc) · 1.33 KB
/
debug.c
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
/*
* Copyright (c) 2014 Wu, Xingbo <wuxb45@gmail.com>
*
* All rights reserved. No warranty, explicit or implicit, provided.
*/
#define _GNU_SOURCE
#define _LARGEFILE64_SOURCE
#include <sys/time.h>
#include <stdio.h>
#include <execinfo.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
uint64_t
debug_time_usec(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000lu + tv.tv_usec;
}
double
debug_time_sec(void)
{
const uint64_t usec = debug_time_usec();
return ((double)usec) / 1000000.0;
}
uint64_t
debug_diff_usec(const uint64_t last)
{
return debug_time_usec() - last;
}
double
debug_diff_sec(const double last)
{
return debug_time_sec() - last;
}
uint64_t
debug_tv_diff(const struct timeval * const t0, const struct timeval * const t1)
{
return UINT64_C(1000000) * (t1->tv_sec - t0->tv_sec) + (t1->tv_usec - t0->tv_usec);
}
void
debug_print_tv_diff(char * tag, const struct timeval t0, const struct timeval t1)
{
printf("%s: %lu us\n", tag, debug_tv_diff(&t0, &t1));
}
void
debug_trace (void)
{
void *array[100];
char **strings;
const int size = backtrace(array, 100);
strings = backtrace_symbols(array, size);
printf("Obtained %d stack frames.\n", size);
int i;
for (i = 0; i < size; i++)
printf ("%d -> %s\n", i, strings[i]);
free (strings);
}