-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathdebuggercommon.h
More file actions
282 lines (241 loc) · 10.1 KB
/
debuggercommon.h
File metadata and controls
282 lines (241 loc) · 10.1 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
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
/*
Copyright 2020-2026 Vector 35 Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <string.h>
#include <optional>
#ifndef WIN32
#include "libgen.h"
#endif
namespace BinaryNinjaDebugger {
struct ModuleNameAndOffset
{
// TODO: maybe we should use DebugModule instead of its name
// Update: We are not using a DebugModule here because the base address information of it can be outdated;
// instead, we only keep a name and an offset.
std::string module;
uint64_t offset;
ModuleNameAndOffset() : module(""), offset(0) {}
ModuleNameAndOffset(std::string mod, uint64_t off) : module(mod), offset(off) {}
bool operator==(const ModuleNameAndOffset& other) const
{
return IsSameBaseModule(other) && (offset == other.offset);
}
bool operator<(const ModuleNameAndOffset& other) const
{
if (module < other.module)
return true;
if (module > other.module)
return false;
return offset < other.offset;
}
bool operator>(const ModuleNameAndOffset& other) const
{
if (module > other.module)
return true;
if (module < other.module)
return false;
return offset > other.offset;
}
static std::string GetPathBaseName(const std::string& path)
{
#ifdef WIN32
// TODO: someone please write it on Windows!
char baseName[MAX_PATH];
char ext[MAX_PATH];
_splitpath_s(path.c_str(), NULL, 0, NULL, 0, baseName, MAX_PATH, ext, MAX_PATH);
return std::string(baseName) + std::string(ext);
#else
return basename(strdup(path.c_str()));
#endif
}
bool IsSameBaseModule(const ModuleNameAndOffset& other) const
{
return ((module == other.module) || (GetPathBaseName(module) == GetPathBaseName(other.module)));
}
bool IsSameBaseModule(const std::string& other) const
{
return ((module == other) || (GetPathBaseName(module) == GetPathBaseName(other)));
}
static bool IsSameBaseModule(const std::string& module1, const std::string& module2)
{
return ((module1 == module2) || (GetPathBaseName(module1) == GetPathBaseName(module2)));
}
};
// TTD Memory Access Types - bitfield flags that can be combined
enum TTDMemoryAccessType
{
TTDMemoryRead = 1,
TTDMemoryWrite = 2,
TTDMemoryExecute = 4,
TTDMemoryAll = TTDMemoryRead | TTDMemoryWrite | TTDMemoryExecute
};
// TTD Position - represents a position in the TTD trace
struct TTDPosition
{
uint64_t sequence; // Sequence number in trace
uint64_t step; // Step within sequence
TTDPosition() : sequence(0), step(0) {}
TTDPosition(uint64_t seq, uint64_t st) : sequence(seq), step(st) {}
bool operator==(const TTDPosition& other) const
{
return sequence == other.sequence && step == other.step;
}
bool operator<(const TTDPosition& other) const
{
if (sequence < other.sequence)
return true;
if (sequence > other.sequence)
return false;
return step < other.step;
}
};
// TTD Bookmark - a saved position in the trace with optional metadata
struct TTDBookmark
{
TTDPosition position;
uint64_t viewAddress;
std::string note;
TTDBookmark() : viewAddress(0) {}
TTDBookmark(const TTDPosition& pos, const std::string& n = "", uint64_t addr = 0)
: position(pos), viewAddress(addr), note(n) {}
};
// TTD Memory Access Event - complete set of fields from Microsoft documentation
struct TTDMemoryEvent
{
std::string eventType; // Event type (e.g., "MemoryAccess")
uint32_t threadId; // Thread ID that performed the access
uint32_t uniqueThreadId; // Unique thread identifier
TTDPosition timeStart; // Position when event started
TTDPosition timeEnd; // Position when event ended
uint64_t address; // Memory address accessed
uint64_t size; // Size of memory access
uint64_t memoryAddress; // Memory address (may be same as address)
uint64_t instructionAddress; // IP - Address of instruction that caused the access
uint64_t value; // Value that was read/written/executed
TTDMemoryAccessType accessType; // Type of memory access (parsed from object)
TTDMemoryEvent() : threadId(0), uniqueThreadId(0), address(0), size(0), memoryAddress(0), instructionAddress(0), value(0), accessType(TTDMemoryRead) {}
};
struct TTDPositionRangeIndexedMemoryEvent{
TTDPosition position; // Position of the memory event
uint32_t threadId; // Thread ID that performed the access
uint32_t uniqueThreadId; // Unique thread ID that performed the access
uint64_t address; // Memory address accessed
uint64_t instructionAddress; // Instruction pointer at time of access
uint64_t size; // Size of memory access
TTDMemoryAccessType accessType; // Type of memory access (parsed from object)
uint64_t value; // Value that was read/written/executed
uint8_t data[8]; // The next 8 bytes of data at the memory address
TTDPositionRangeIndexedMemoryEvent() : threadId(0), uniqueThreadId(0), address(0), instructionAddress(0), size(0), accessType(TTDMemoryRead), value(0)
{
memset(data, 0, sizeof(data));
}
};
// TTD Call Event - complete set of fields from Microsoft documentation for TTD.Calls
struct TTDCallEvent
{
std::string eventType; // Event type (always "Call" for TTD.Calls objects)
uint32_t threadId; // OS thread ID of thread that made the call
uint32_t uniqueThreadId; // Unique ID for the thread across the trace
std::string function; // Symbolic name of the function
uint64_t functionAddress; // Function's address in memory
uint64_t returnAddress; // Instruction to return to after the call
uint64_t returnValue; // Return value of the function (if not void)
bool hasReturnValue; // Whether the function has a return value
std::vector<std::string> parameters; // Array containing parameters passed to the function
TTDPosition timeStart; // Position when call started
TTDPosition timeEnd; // Position when call ended
TTDCallEvent() : threadId(0), uniqueThreadId(0), functionAddress(0), returnAddress(0), returnValue(0), hasReturnValue(false) {}
};
// TTD Event Types - bitfield flags for filtering events
enum TTDEventType
{
TTDEventNone = 0,
TTDEventThreadCreated = 1,
TTDEventThreadTerminated = 2,
TTDEventModuleLoaded = 4,
TTDEventModuleUnloaded = 8,
TTDEventException = 16,
TTDEventAll = TTDEventThreadCreated | TTDEventThreadTerminated | TTDEventModuleLoaded | TTDEventModuleUnloaded | TTDEventException
};
// TTD Module - information about modules that were loaded/unloaded during trace
struct TTDModule
{
std::string name; // Name and path of the module
uint64_t address; // Address where the module was loaded
uint64_t size; // Size of the module in bytes
uint32_t checksum; // Checksum of the module
uint32_t timestamp; // Timestamp of the module
TTDModule() : address(0), size(0), checksum(0), timestamp(0) {}
};
// TTD Thread - information about threads and their lifetime during trace
struct TTDThread
{
uint32_t uniqueId; // Unique ID for the thread across the trace
uint32_t id; // TID of the thread
TTDPosition lifetimeStart; // Lifetime start position
TTDPosition lifetimeEnd; // Lifetime end position
TTDPosition activeTimeStart; // Active time start position
TTDPosition activeTimeEnd; // Active time end position
TTDThread() : uniqueId(0), id(0) {}
};
// TTD Exception Types
enum TTDExceptionType
{
TTDExceptionSoftware,
TTDExceptionHardware
};
// TTD Exception - information about exceptions that occurred during trace
struct TTDException
{
TTDExceptionType type; // Type of exception (Software/Hardware)
uint64_t programCounter; // Instruction where exception was thrown
uint32_t code; // Exception code
uint32_t flags; // Exception flags
uint64_t recordAddress; // Where in memory the exception record is found
TTDPosition position; // Position where exception occurred
TTDException() : type(TTDExceptionSoftware), programCounter(0), code(0), flags(0), recordAddress(0) {}
};
// TTD Event - represents important events that happened during trace
struct TTDEvent
{
TTDEventType type; // Type of event
TTDPosition position; // Position where event occurred
// Optional child objects - existence depends on event type
std::optional<TTDModule> module; // For ModuleLoaded/ModuleUnloaded events
std::optional<TTDThread> thread; // For ThreadCreated/ThreadTerminated events
std::optional<TTDException> exception; // For Exception events
TTDEvent() : type(TTDEventThreadCreated) {}
TTDEvent(TTDEventType eventType) : type(eventType) {}
};
// TTD String Entry - represents a string found in the trace
struct TTDStringEntry
{
uint64_t id;
std::string data; // The string content
uint64_t address; // Linear address where string begins
uint64_t size; // Size in bytes
TTDPosition firstAccess; // Position of first access in the trace
TTDPosition lastAccess; // Position of last access in the trace
std::string encoding; // "utf8" or "utf16"
TTDStringEntry() : id(0), address(0), size(0) {}
};
// Breakpoint types - used to specify the type of breakpoint to set
enum DebugBreakpointType
{
SoftwareBreakpoint = 0, // Default software breakpoint
HardwareExecuteBreakpoint = 1, // Hardware execution breakpoint
HardwareReadBreakpoint = 2, // Hardware read watchpoint
HardwareWriteBreakpoint = 3, // Hardware write watchpoint
HardwareAccessBreakpoint = 4 // Hardware read/write watchpoint
};
}; // namespace BinaryNinjaDebugger