-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsource_location.c
69 lines (48 loc) · 1.71 KB
/
source_location.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
#include <stdio.h>
#include <assert.h>
#include <execinfo.h>
#if __USE_STACK_TRACE_TO_IDENTIFY_TASKS__
#include "source_location.h"
/**
Mangle together the return pointers from all stack frames to get a 32-bit integer
likely to be a unique for each point in the user's code.
I am not sure how likely collisions are using this method.
Note, some mpicxx compilers will destroy this function, so it is in this file by itself to be compiled with g++
*/
unsigned long source_location_ulong(){
unsigned long long s = 0;
void* callstack[128];
int i, frames = backtrace(callstack, 128);
if(frames > 0){
s = (unsigned long long)callstack[0];
for (i = 1; i < frames; ++i) {
s = s ^ (unsigned long long)callstack[i];
}
}
// Drop this down to an unsigned long
unsigned long s_ul = (unsigned long)(s & 0xFFFFFFFF) ^ (unsigned long)((s>>32) & 0xFFFFFFFF);
return s_ul;
}
int source_location_int(){
long long s = 0;
void* callstack[128];
int i, frames = backtrace(callstack, 128);
if(frames > 0){
s = (long long)callstack[0];
for (i = 1; i < frames; ++i) {
s = s ^ (long long)callstack[i];
}
}
// Drop this down to an unsigned long
int s_int = (int)(s & 0xFFFFFFFF) ^ (int)((s>>32) & 0xFFFFFFFF);
// printf("%d\n", s_int);
return s_int;
}
#else
int source_location_int(){
return 0;
}
unsigned long source_location_ulong(){
return 0;
}
#endif