-
Notifications
You must be signed in to change notification settings - Fork 8
/
idadbg_local.h
287 lines (252 loc) · 7.63 KB
/
idadbg_local.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
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
#ifndef IDADBG_LOCAL_H
#define IDADBG_LOCAL_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <deque>
#include <map>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>
#ifndef _WIN32
#include <unistd.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <netdb.h>
#else
namespace WINDOWS
{
#undef timerisset
#undef timercmp
#undef timerclear
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <WinUser.h>
#include <Ws2tcpip.h>
};
#endif
#ifndef qnotused
# define qnotused(x) (void)x
#endif
//--------------------------------------------------------------------------
// How many miliseconds to wait for incoming packets
#define RCV_TIMEOUT 10
//--------------------------------------------------------------------------
// Wrappers for OS-depended types/functions
//--------------------------------------------------------------------------
#ifdef _WIN32
# if _MSC_VER
# if defined(PIN_NUMERIC_BUILD) && PIN_NUMERIC_BUILD < 76991 || defined(__LINT__)
# define snprintf _snprintf
# define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
# define bcopy(b1,b2,len) (memmove((b2), (b1), (len)), (void) 0)
# endif
# endif
# if defined(_WIN64) && defined(PIN_NUMERIC_BUILD) && PIN_NUMERIC_BUILD < 76991
typedef signed __int64 ssize_t;
# else
typedef signed int ssize_t;
# endif
typedef WINDOWS::SOCKET PIN_SOCKET;
#define pin_socket p_socket
#define pin_htons p_htons
#define pin_bind p_bind
#define pin_listen p_listen
#define pin_accept(s, addr, addrlen) p_accept(s, (WINDOWS::sockaddr *)addr, addrlen)
#define pin_select(s, rds, wds, eds, tv) p_select(s, rds, wds, eds, tv)
#define pin_setsockopt(s, level, optname, optval, optlen) p_setsockopt(s, level, optname, (const char*)optval, optlen)
#define pin_closesocket(s) p_closesocket(s)
typedef WINDOWS::timeval pin_timeval;
typedef WINDOWS::fd_set pin_fd_set;
// the following typedefs are necessary for FD_... macros
#define fd_set WINDOWS::fd_set
typedef WINDOWS::u_int u_int;
typedef WINDOWS::u_short u_short;
typedef WINDOWS::SOCKET SOCKET;
#define pin_sockaddr WINDOWS::sockaddr
#define pin_socklen_t WINDOWS::socklen_t
#define pin_sockaddr_in WINDOWS::sockaddr_in
#else
#define PIN_SOCKET int
#define INVALID_SOCKET -1
#define pin_socket socket
#define pin_accept accept
#define pin_select select
#define pin_timeval struct timeval
#define pin_fd_set fd_set
#define pin_setsockopt setsockopt
#define pin_sockaddr_in sockaddr_in
#define pin_htons htons
#define pin_bind ::bind
#define pin_listen listen
#define pin_sockaddr sockaddr
#define pin_socklen_t socklen_t
#define pin_closesocket close
#endif
//--------------------------------------------------------------------------
// Internal macros/types
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// in versions prior to 2.13 locking functions had names without 'PIN_' prefix
#if PIN_PRODUCT_VERSION_MAJOR < 2 || (PIN_PRODUCT_VERSION_MAJOR == 2 && PIN_PRODUCT_VERSION_MINOR <= 12)
# define PIN_InitLock InitLock
# define PIN_GetLock GetLock
# define PIN_ReleaseLock ReleaseLock
#endif
//--------------------------------------------------------------------------
// OS we are running on
#ifdef TARGET_WINDOWS
#define TARGET_OS PIN_TARGET_OS_WINDOWS
#else
# ifdef TARGET_LINUX
# define TARGET_OS PIN_TARGET_OS_LINUX
# else
# ifdef TARGET_MAC
# define TARGET_OS PIN_TARGET_OS_MAC
# else
# error "Unsupported OS"
# endif
# endif
#endif
//--------------------------------------------------------------------------
// Logging & debug
#define MSG(fmt, ...) \
do \
{ \
if ( debug_level > 0 ) \
{ \
char _buf[MAXSTR*4]; \
pin_snprintf(_buf, sizeof(_buf), fmt, ##__VA_ARGS__); \
fprintf(stderr, "%s", _buf); \
LOG(_buf); \
} \
} \
while ( 0 )
#define DEBUG(level, fmt, ...) \
do \
{ \
if ( debug_level >= level ) \
MSG(fmt, ##__VA_ARGS__); \
} \
while ( 0 )
//lint -esym(750, SEMAFORE_WAIT) local macro 'SEMAFORE_WAIT' not referenced
//lint -esym(750, SEMAFORE_CLEAR) local macro 'SEMAFORE_CLEAR' not referenced
//lint -esym(750, SEMAFORE_SET) local macro 'SEMAFORE_SET' not referenced
//lint -esym(750, WAIT_AFTER_CALLBACK) local macro 'WAIT_AFTER_CALLBACK' not referenced
#define SEMAFORE_WAIT(x) \
do \
{\
if ( !PIN_SemaphoreIsSet(x) ) \
{\
DEBUG(3, "(%d / %d) WAIT for %s...\n", int(PIN_GetTid()), __LINE__, #x);\
PIN_SemaphoreWait(x);\
DEBUG(3, "(%d / %d) WAIT Ok\n", int(PIN_GetTid()), __LINE__);\
} \
else\
PIN_SemaphoreWait(x);\
}\
while ( false )
#define APP_WAIT(x) \
do \
{\
DEBUG(3, "(%d / %d) APP_WAIT %s...\n", int(PIN_GetTid()), __LINE__, #x);\
wait_app_resume(x);\
}\
while ( false )
#define SEMAFORE_CLEAR(x) \
do \
{\
DEBUG(2, "(%d / %d) CLEAR %s...\n", int(PIN_GetTid()), __LINE__, #x);\
PIN_SemaphoreClear(x);\
}\
while ( false )
#define SEMAFORE_SET(x) \
do \
{\
DEBUG(2, "(%d / %d) SET %s...\n", int(PIN_GetTid()), __LINE__, #x);\
PIN_SemaphoreSet(x);\
}\
while ( false )
#define WAIT_AFTER_CALLBACK() \
do \
{\
janitor_for_pinlock_t process_state_guard(&process_state_lock);\
DEBUG(3, "(%d / %d) callback wait...\n", int(PIN_GetTid()), __LINE__);\
breakpoints.prepare_suspend();\
}\
while ( false )
#define app_wait(sem) APP_WAIT(sem)
#define sema_wait(sem) SEMAFORE_WAIT(sem)
#define sema_clear(sem) SEMAFORE_CLEAR(sem)
#define sema_set(sem) SEMAFORE_SET(sem)
#define wait_after_callback() WAIT_AFTER_CALLBACK()
//-V:DEBUG:547
//--------------------------------------------------------------------------
// tracebuf entry
struct trc_element_t
{
idapin_registers_t regs;
ADDRINT ea;
THREADID tid;
pin_tev_type_t type;
trc_element_t(THREADID _tid, ADDRINT _ea, pin_tev_type_t _type)
: ea(_ea), tid(_tid), type(_type)
{
regs.eip = ea;
regs.eax =
regs.ebx =
regs.ecx =
regs.edx =
regs.esi =
regs.edi =
regs.ebp =
regs.esp =
#if defined(PIN_64)
regs.r8 =
regs.r9 =
regs.r10 =
regs.r11 =
regs.r12 =
regs.r13 =
regs.r14 =
regs.r15 =
#endif
regs.eflags =
regs.cs =
regs.ds =
regs.es =
regs.fs =
regs.gs =
regs.ss = BADADDR;
};
};
//--------------------------------------------------------------------------
class janitor_for_pinlock_t
{
protected:
PIN_LOCK *resource;
public:
janitor_for_pinlock_t(PIN_LOCK *lock) : resource(lock)
{
PIN_GetLock(resource, PIN_ThreadId() + 1);
}
~janitor_for_pinlock_t()
{
if ( resource != nullptr )
release();
}
//lint -sem(janitor_for_pinlock_t::release,cleanup)
void release()
{
PIN_ReleaseLock(resource);
resource = nullptr;
}
};
#endif