-
Notifications
You must be signed in to change notification settings - Fork 1
/
gdb.c
288 lines (220 loc) · 7.29 KB
/
gdb.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
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
288
/* Interaction with GDB or GDBserver.
Copyright (C) 2012 Mentor Graphics.
This file is part of dagent, the Debugging Agent.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <sys/mman.h>
#include <string.h>
#include <malloc.h>
#include "log.h"
#include "agent.h"
#include "tracepoint.h"
#include "backend.h"
extern unsigned char *GDB_AGENT_SYM(trace_buffer_hi);
extern unsigned char *GDB_AGENT_SYM(trace_buffer_lo);
/* Place a breakpoints on these three functions. */
void
GDB_AGENT_SYM(stop_tracing) (void)
{
}
void
GDB_AGENT_SYM(flush_trace_buffer)(void)
{
}
void
GDB_AGENT_SYM(about_to_request_buffer_space) ()
{}
/* Two gdbserver-read-only variables. They are used by gdbserver to track
the jump pad buffers. */
char *GDB_AGENT_SYM(gdb_jump_pad_buffer);
char *GDB_AGENT_SYM(gdb_jump_pad_buffer_end);
/********************** trace buffer control ***************************/
#define GDB_FLUSH_COUNT_MASK 0xfffffff0
#define GDB_FLUSH_COUNT_MASK_PREV 0x7ff00000
#define GDB_FLUSH_COUNT_MASK_CURR 0x0007ff00
struct trace_buffer_control
{
unsigned char *start;
unsigned char *free;
unsigned char *end_free;
unsigned char *wrap;
};
unsigned int GDB_AGENT_SYM(trace_buffer_ctrl_curr);
struct trace_buffer_control GDB_AGENT_SYM(trace_buffer_ctrl)[3];
unsigned int GDB_AGENT_SYM(traceframe_write_count);
unsigned int GDB_AGENT_SYM(traceframe_read_count);
#if defined(__GNUC__)
# define memory_barrier() asm volatile ("" : : : "memory")
#else
# define memory_barrier() do {} while (0)
#endif
#define cmpxchg(mem, oldval, newval) \
__sync_val_compare_and_swap (mem, oldval, newval)
/* Carve out a piece of the trace buffer. AMT is the ammount to allocate.
Return NULL in case of failure. */
unsigned char *
gdb_trace_buffer_alloc (size_t amt)
{
unsigned char *rslt;
struct trace_buffer_control *tbctrl;
unsigned int curr;
unsigned int prev, prev_filtered;
unsigned int commit_count;
unsigned int commit;
unsigned int readout;
gdb_verbose ("Want to allocate %d+%u bytes in trace buffer",
(int) amt, (unsigned) sizeof (traceframe_t));
/* Account for the EOB marker. */
amt += sizeof (traceframe_t);
again:
memory_barrier ();
/* Read the current token and extract the index to try to write to,
storing it in CURR. */
prev = GDB_AGENT_SYM(trace_buffer_ctrl_curr);
prev_filtered = prev & ~GDB_FLUSH_COUNT_MASK;
curr = prev_filtered + 1;
if (curr > 2)
curr = 0;
GDB_AGENT_SYM(about_to_request_buffer_space) ();
GDB_AGENT_SYM(trace_buffer_ctrl)[curr]
= GDB_AGENT_SYM(trace_buffer_ctrl)[prev_filtered];
gdb_verbose ("trying curr=%u", curr);
tbctrl = &GDB_AGENT_SYM(trace_buffer_ctrl)[curr];
while (1)
{
if (tbctrl->end_free < tbctrl->free)
{
if (tbctrl->free + amt <= GDB_AGENT_SYM(trace_buffer_hi))
break;
else
{
gdb_verbose ("Upper part too small, setting wraparound");
/* tbctrl->wrap = tbctrl->free; */
tbctrl->free = GDB_AGENT_SYM(trace_buffer_lo);
}
}
/* The normal case. */
if (tbctrl->free + amt <= tbctrl->end_free)
break;
GDB_AGENT_SYM(flush_trace_buffer)();
memory_barrier ();
if (GDB_AGENT_SYM(tracing))
{
gdb_verbose ("gdbserver flushed buffer, retrying");
goto again;
}
/* GDBserver cancelled the tracing. Bail out as well. */
return NULL;
}
/* If we get here, we know we can provide the asked-for space. */
rslt = tbctrl->free;
tbctrl->free += (amt - sizeof (traceframe_t));
/* Or not. If GDBserver changed the trace buffer behind our back,
we get to restart a new allocation attempt. */
/* Build the tentative token. */
commit_count = (((prev & GDB_FLUSH_COUNT_MASK_CURR) + 0x100)
& GDB_FLUSH_COUNT_MASK_CURR);
commit = (((prev & GDB_FLUSH_COUNT_MASK_CURR) << 12)
| commit_count
| curr);
/* Try to commit it. */
readout = cmpxchg (&GDB_AGENT_SYM(trace_buffer_ctrl_curr), prev, commit);
if (readout != prev)
{
gdb_verbose ("GDBserver has touched the trace buffer, restarting."
" (prev=%08x, commit=%08x, readout=%08x)",
prev, commit, readout);
goto again;
}
GDB_AGENT_SYM(about_to_request_buffer_space) ();
/* Check if the change has been effective, even if GDBserver stopped
us at the breakpoint. */
{
unsigned int refetch;
memory_barrier ();
refetch = GDB_AGENT_SYM(trace_buffer_ctrl_curr);
if (refetch == commit
|| ((refetch & GDB_FLUSH_COUNT_MASK_PREV) >> 12) == commit_count)
gdb_verbose ("Change is effective: (prev=%08x, commit=%08x, "
"readout=%08x, refetch=%08x)",
prev, commit, readout, refetch);
else
{
gdb_verbose ("GDBserver has touched the trace buffer, not effective."
" (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
prev, commit, readout, refetch);
goto again;
}
}
((traceframe_t *) tbctrl->free)->tpnum = 0;
((traceframe_t *) tbctrl->free)->data_size = 0;
return rslt;
}
void
traceframe_finish (traceframe_t *tframe)
{
GDB_AGENT_SYM(traceframe_write_count)++;
GDB_AGENT_SYM(traceframes_created)++;
}
uint64_t
GDB_AGENT_SYM(get_raw_reg) (unsigned char *raw_regs, int regnum)
{
return agent_backend->reg.get_raw_reg (raw_regs, regnum);
}
uint64_t
GDB_AGENT_SYM(get_trace_state_variable_value) (int num)
{
return 0;
}
void
GDB_AGENT_SYM(set_trace_state_variable_value) (int num, uint64_t val)
{
/* FIXME: Not implemented. */
}
typedef struct collecting_t
{
uintptr_t tpoint;
uintptr_t thread_area;
} collecting_t;
static collecting_t *GDB_AGENT_SYM(collecting) __attribute__((used));
char *GDB_AGENT_SYM(gdb_trampoline_buffer);
char *GDB_AGENT_SYM(gdb_trampoline_buffer_end);
char *GDB_AGENT_SYM(gdb_trampoline_buffer_error);
char *GDB_AGENT_SYM(gdb_tp_heap_buffer) __attribute__((used));
void
initialize_gdb_config (void)
{
int pagesize = agent_get_pagesize ();
uintptr_t addr;
for (addr = pagesize; addr != 0; addr += pagesize)
{
GDB_AGENT_SYM(gdb_jump_pad_buffer) = mmap ((void *) addr,
pagesize * 20,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
-1, 0);
if (GDB_AGENT_SYM(gdb_jump_pad_buffer) != MAP_FAILED)
break;
}
if (addr == 0)
gdb_inform ("Unable to set up jump pad buffer!");
GDB_AGENT_SYM(gdb_jump_pad_buffer_end)
= GDB_AGENT_SYM(gdb_jump_pad_buffer) + pagesize * 20;
GDB_AGENT_SYM(gdb_trampoline_buffer) = 0;
GDB_AGENT_SYM(gdb_trampoline_buffer_end) = 0;
GDB_AGENT_SYM(gdb_trampoline_buffer_error) = malloc (100);
strcpy (GDB_AGENT_SYM(gdb_trampoline_buffer_error), "No errors reported");
GDB_AGENT_SYM(gdb_tp_heap_buffer) = malloc (5 * 1024 * 1024);
}
struct agent_config agent_config_gdb = {
{1, 1}, initialize_gdb_config,
};