-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.h
140 lines (103 loc) · 5.22 KB
/
util.h
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
/*
Copyright (c) 2015 ThreadScan authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef _UTIL_H_
#define _UTIL_H_
#include <pthread.h>
#include "queue.h"
#include <signal.h>
/****************************************************************************/
/* Defines, typedefs, etc. */
/****************************************************************************/
#define FOREACH_IN_THREAD_LIST(td, tl) do { \
pthread_mutex_lock(&(tl)->lock); \
(td) = (tl)->head; \
while (NULL != (td)) {
#define ENDFOREACH_IN_THREAD_LIST(td, tl) (td) = (td)->next; } \
pthread_mutex_unlock(&(tl)->lock); \
} while (0)
#define FOREACH_BREAK_THREAD_LIST(tl) do { \
pthread_mutex_unlock(&(tl)->lock); \
} while (0)
#define PAGESIZE ((size_t)0x1000)
#define PAGEALIGN(addr) ((addr) & ~(PAGESIZE - 1))
#define MIN_OF(a, b) ((a) < (b) ? (a) : (b))
#define BCAS(ptr, compare, swap) \
__sync_bool_compare_and_swap(ptr, compare, swap)
#define _TIMESTAMP_MASK 0x7FFFFFFFFFFFFFFF
#define _TIMESTAMP_FLAG 0x8000000000000000
#define TIMESTAMP(field) ((field) & _TIMESTAMP_MASK)
#define TIMESTAMP_RAISE_FLAG(field) ((field) | _TIMESTAMP_FLAG)
#define TIMESTAMP_IS_ACTIVE(field) ((field) & _TIMESTAMP_FLAG)
#define TIMESTAMP_SET_ACTIVE(field) TIMESTAMP_RAISE_FLAG(field)
typedef struct mem_range_t mem_range_t;
typedef struct thread_data_t thread_data_t;
typedef struct thread_list_t thread_list_t;
/****************************************************************************/
/* Memory range data for write protection. */
/****************************************************************************/
struct mem_range_t {
size_t low;
size_t high;
};
/****************************************************************************/
/* Storage for per-thread data. */
/****************************************************************************/
struct thread_data_t {
// User parameters for creating a new thread.
void *(*user_routine) (void *);
void *user_arg;
// Thread metadata fields.
thread_data_t *next; // Linked list of thread metadata.
pthread_t self; // That's me!
char *user_stack_low; // Low address on the user stack.
char *user_stack_high; // Actually, just the high address to lock.
int stack_is_ours; // Whether threadscan allocated the stack.
int is_active; // The thread is running user code.
queue_t ptr_list; // Local list of pointers to be collected.
size_t local_timestamp;
int times_without_update;
mem_range_t local_block; // Non-stack memory local to this thread.
// Reference count prevents premature free'ing of the structure while
// other threads are looking at it.
int ref_count;
};
struct thread_list_t {
thread_data_t *head;
pthread_mutex_t lock;
};
thread_data_t *threadscan_util_thread_data_new ();
void threadscan_util_thread_data_decr_ref (thread_data_t *td);
void threadscan_util_thread_data_free (thread_data_t *td);
void threadscan_util_thread_data_cleanup (pthread_t tid);
void threadscan_util_thread_list_init (thread_list_t *tl);
void threadscan_util_thread_list_add (thread_list_t *tl, thread_data_t *td);
void threadscan_util_thread_list_remove (thread_list_t *tl, thread_data_t *td);
thread_data_t *threadscan_util_thread_list_find (thread_list_t *tl,
size_t addr);
/****************************************************************************/
/* I/O functions. */
/****************************************************************************/
int threadscan_diagnostic (const char *format, ...);
void threadscan_fatal (const char *format, ...);
/****************************************************************************/
/* Sort utility. */
/****************************************************************************/
void threadscan_util_randomize (size_t *addrs, int n);
void threadscan_util_sort (size_t *a, int length);
#endif // !defined _UTIL_H_