tracepoint.cc revision 1.1.1.2 1 1.1 christos /* Tracepoint code for remote server for GDB.
2 1.1.1.2 christos Copyright (C) 2009-2023 Free Software Foundation, Inc.
3 1.1 christos
4 1.1 christos This file is part of GDB.
5 1.1 christos
6 1.1 christos This program is free software; you can redistribute it and/or modify
7 1.1 christos it under the terms of the GNU General Public License as published by
8 1.1 christos the Free Software Foundation; either version 3 of the License, or
9 1.1 christos (at your option) any later version.
10 1.1 christos
11 1.1 christos This program is distributed in the hope that it will be useful,
12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 1.1 christos GNU General Public License for more details.
15 1.1 christos
16 1.1 christos You should have received a copy of the GNU General Public License
17 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 1.1 christos
19 1.1 christos #include "server.h"
20 1.1 christos #include "tracepoint.h"
21 1.1 christos #include "gdbthread.h"
22 1.1 christos #include "gdbsupport/rsp-low.h"
23 1.1 christos
24 1.1 christos #include <ctype.h>
25 1.1 christos #include <fcntl.h>
26 1.1 christos #include <unistd.h>
27 1.1 christos #include <chrono>
28 1.1 christos #include <inttypes.h>
29 1.1 christos #include "ax.h"
30 1.1 christos #include "tdesc.h"
31 1.1 christos
32 1.1 christos #define IPA_SYM_STRUCT_NAME ipa_sym_addresses
33 1.1 christos #include "gdbsupport/agent.h"
34 1.1 christos
35 1.1 christos #define DEFAULT_TRACE_BUFFER_SIZE 5242880 /* 5*1024*1024 */
36 1.1 christos
37 1.1 christos /* This file is built for both GDBserver, and the in-process
38 1.1 christos agent (IPA), a shared library that includes a tracing agent that is
39 1.1 christos loaded by the inferior to support fast tracepoints. Fast
40 1.1 christos tracepoints (or more accurately, jump based tracepoints) are
41 1.1 christos implemented by patching the tracepoint location with a jump into a
42 1.1 christos small trampoline function whose job is to save the register state,
43 1.1 christos call the in-process tracing agent, and then execute the original
44 1.1 christos instruction that was under the tracepoint jump (possibly adjusted,
45 1.1 christos if PC-relative, or some such).
46 1.1 christos
47 1.1 christos The current synchronization design is pull based. That means,
48 1.1 christos GDBserver does most of the work, by peeking/poking at the inferior
49 1.1 christos agent's memory directly for downloading tracepoint and associated
50 1.1 christos objects, and for uploading trace frames. Whenever the IPA needs
51 1.1 christos something from GDBserver (trace buffer is full, tracing stopped for
52 1.1 christos some reason, etc.) the IPA calls a corresponding hook function
53 1.1 christos where GDBserver has placed a breakpoint.
54 1.1 christos
55 1.1 christos Each of the agents has its own trace buffer. When browsing the
56 1.1 christos trace frames built from slow and fast tracepoints from GDB (tfind
57 1.1 christos mode), there's no guarantee the user is seeing the trace frames in
58 1.1 christos strict chronological creation order, although, GDBserver tries to
59 1.1 christos keep the order relatively reasonable, by syncing the trace buffers
60 1.1 christos at appropriate times.
61 1.1 christos
62 1.1 christos */
63 1.1 christos
64 1.1 christos #ifdef IN_PROCESS_AGENT
65 1.1 christos
66 1.1 christos static void trace_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
67 1.1 christos
68 1.1 christos static void
69 1.1 christos trace_vdebug (const char *fmt, ...)
70 1.1 christos {
71 1.1 christos char buf[1024];
72 1.1 christos va_list ap;
73 1.1 christos
74 1.1 christos va_start (ap, fmt);
75 1.1 christos vsprintf (buf, fmt, ap);
76 1.1 christos fprintf (stderr, PROG "/tracepoint: %s\n", buf);
77 1.1 christos va_end (ap);
78 1.1 christos }
79 1.1 christos
80 1.1.1.2 christos #define trace_debug(fmt, args...) \
81 1.1 christos do { \
82 1.1.1.2 christos if (debug_threads) \
83 1.1 christos trace_vdebug ((fmt), ##args); \
84 1.1 christos } while (0)
85 1.1 christos
86 1.1 christos #else
87 1.1 christos
88 1.1.1.2 christos #define trace_debug(fmt, args...) \
89 1.1 christos do { \
90 1.1.1.2 christos threads_debug_printf ((fmt), ##args); \
91 1.1 christos } while (0)
92 1.1 christos
93 1.1 christos #endif
94 1.1 christos
95 1.1 christos /* Prefix exported symbols, for good citizenship. All the symbols
96 1.1 christos that need exporting are defined in this module. Note that all
97 1.1 christos these symbols must be tagged with IP_AGENT_EXPORT_*. */
98 1.1 christos #ifdef IN_PROCESS_AGENT
99 1.1 christos # define gdb_tp_heap_buffer IPA_SYM_EXPORTED_NAME (gdb_tp_heap_buffer)
100 1.1 christos # define gdb_jump_pad_buffer IPA_SYM_EXPORTED_NAME (gdb_jump_pad_buffer)
101 1.1 christos # define gdb_jump_pad_buffer_end IPA_SYM_EXPORTED_NAME (gdb_jump_pad_buffer_end)
102 1.1 christos # define gdb_trampoline_buffer IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer)
103 1.1 christos # define gdb_trampoline_buffer_end IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer_end)
104 1.1 christos # define gdb_trampoline_buffer_error IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer_error)
105 1.1 christos # define collecting IPA_SYM_EXPORTED_NAME (collecting)
106 1.1 christos # define gdb_collect_ptr IPA_SYM_EXPORTED_NAME (gdb_collect_ptr)
107 1.1 christos # define stop_tracing IPA_SYM_EXPORTED_NAME (stop_tracing)
108 1.1 christos # define flush_trace_buffer IPA_SYM_EXPORTED_NAME (flush_trace_buffer)
109 1.1 christos # define about_to_request_buffer_space IPA_SYM_EXPORTED_NAME (about_to_request_buffer_space)
110 1.1 christos # define trace_buffer_is_full IPA_SYM_EXPORTED_NAME (trace_buffer_is_full)
111 1.1 christos # define stopping_tracepoint IPA_SYM_EXPORTED_NAME (stopping_tracepoint)
112 1.1 christos # define expr_eval_result IPA_SYM_EXPORTED_NAME (expr_eval_result)
113 1.1 christos # define error_tracepoint IPA_SYM_EXPORTED_NAME (error_tracepoint)
114 1.1 christos # define tracepoints IPA_SYM_EXPORTED_NAME (tracepoints)
115 1.1 christos # define tracing IPA_SYM_EXPORTED_NAME (tracing)
116 1.1 christos # define trace_buffer_ctrl IPA_SYM_EXPORTED_NAME (trace_buffer_ctrl)
117 1.1 christos # define trace_buffer_ctrl_curr IPA_SYM_EXPORTED_NAME (trace_buffer_ctrl_curr)
118 1.1 christos # define trace_buffer_lo IPA_SYM_EXPORTED_NAME (trace_buffer_lo)
119 1.1 christos # define trace_buffer_hi IPA_SYM_EXPORTED_NAME (trace_buffer_hi)
120 1.1 christos # define traceframe_read_count IPA_SYM_EXPORTED_NAME (traceframe_read_count)
121 1.1 christos # define traceframe_write_count IPA_SYM_EXPORTED_NAME (traceframe_write_count)
122 1.1 christos # define traceframes_created IPA_SYM_EXPORTED_NAME (traceframes_created)
123 1.1 christos # define trace_state_variables IPA_SYM_EXPORTED_NAME (trace_state_variables)
124 1.1 christos # define get_raw_reg_ptr IPA_SYM_EXPORTED_NAME (get_raw_reg_ptr)
125 1.1 christos # define get_trace_state_variable_value_ptr \
126 1.1 christos IPA_SYM_EXPORTED_NAME (get_trace_state_variable_value_ptr)
127 1.1 christos # define set_trace_state_variable_value_ptr \
128 1.1 christos IPA_SYM_EXPORTED_NAME (set_trace_state_variable_value_ptr)
129 1.1 christos # define ust_loaded IPA_SYM_EXPORTED_NAME (ust_loaded)
130 1.1 christos # define helper_thread_id IPA_SYM_EXPORTED_NAME (helper_thread_id)
131 1.1 christos # define cmd_buf IPA_SYM_EXPORTED_NAME (cmd_buf)
132 1.1 christos # define ipa_tdesc_idx IPA_SYM_EXPORTED_NAME (ipa_tdesc_idx)
133 1.1 christos #endif
134 1.1 christos
135 1.1 christos #ifndef IN_PROCESS_AGENT
136 1.1 christos
137 1.1 christos /* Addresses of in-process agent's symbols GDBserver cares about. */
138 1.1 christos
139 1.1 christos struct ipa_sym_addresses
140 1.1 christos {
141 1.1 christos CORE_ADDR addr_gdb_tp_heap_buffer;
142 1.1 christos CORE_ADDR addr_gdb_jump_pad_buffer;
143 1.1 christos CORE_ADDR addr_gdb_jump_pad_buffer_end;
144 1.1 christos CORE_ADDR addr_gdb_trampoline_buffer;
145 1.1 christos CORE_ADDR addr_gdb_trampoline_buffer_end;
146 1.1 christos CORE_ADDR addr_gdb_trampoline_buffer_error;
147 1.1 christos CORE_ADDR addr_collecting;
148 1.1 christos CORE_ADDR addr_gdb_collect_ptr;
149 1.1 christos CORE_ADDR addr_stop_tracing;
150 1.1 christos CORE_ADDR addr_flush_trace_buffer;
151 1.1 christos CORE_ADDR addr_about_to_request_buffer_space;
152 1.1 christos CORE_ADDR addr_trace_buffer_is_full;
153 1.1 christos CORE_ADDR addr_stopping_tracepoint;
154 1.1 christos CORE_ADDR addr_expr_eval_result;
155 1.1 christos CORE_ADDR addr_error_tracepoint;
156 1.1 christos CORE_ADDR addr_tracepoints;
157 1.1 christos CORE_ADDR addr_tracing;
158 1.1 christos CORE_ADDR addr_trace_buffer_ctrl;
159 1.1 christos CORE_ADDR addr_trace_buffer_ctrl_curr;
160 1.1 christos CORE_ADDR addr_trace_buffer_lo;
161 1.1 christos CORE_ADDR addr_trace_buffer_hi;
162 1.1 christos CORE_ADDR addr_traceframe_read_count;
163 1.1 christos CORE_ADDR addr_traceframe_write_count;
164 1.1 christos CORE_ADDR addr_traceframes_created;
165 1.1 christos CORE_ADDR addr_trace_state_variables;
166 1.1 christos CORE_ADDR addr_get_raw_reg_ptr;
167 1.1 christos CORE_ADDR addr_get_trace_state_variable_value_ptr;
168 1.1 christos CORE_ADDR addr_set_trace_state_variable_value_ptr;
169 1.1 christos CORE_ADDR addr_ust_loaded;
170 1.1 christos CORE_ADDR addr_ipa_tdesc_idx;
171 1.1 christos };
172 1.1 christos
173 1.1 christos static struct
174 1.1 christos {
175 1.1 christos const char *name;
176 1.1 christos int offset;
177 1.1 christos } symbol_list[] = {
178 1.1 christos IPA_SYM(gdb_tp_heap_buffer),
179 1.1 christos IPA_SYM(gdb_jump_pad_buffer),
180 1.1 christos IPA_SYM(gdb_jump_pad_buffer_end),
181 1.1 christos IPA_SYM(gdb_trampoline_buffer),
182 1.1 christos IPA_SYM(gdb_trampoline_buffer_end),
183 1.1 christos IPA_SYM(gdb_trampoline_buffer_error),
184 1.1 christos IPA_SYM(collecting),
185 1.1 christos IPA_SYM(gdb_collect_ptr),
186 1.1 christos IPA_SYM(stop_tracing),
187 1.1 christos IPA_SYM(flush_trace_buffer),
188 1.1 christos IPA_SYM(about_to_request_buffer_space),
189 1.1 christos IPA_SYM(trace_buffer_is_full),
190 1.1 christos IPA_SYM(stopping_tracepoint),
191 1.1 christos IPA_SYM(expr_eval_result),
192 1.1 christos IPA_SYM(error_tracepoint),
193 1.1 christos IPA_SYM(tracepoints),
194 1.1 christos IPA_SYM(tracing),
195 1.1 christos IPA_SYM(trace_buffer_ctrl),
196 1.1 christos IPA_SYM(trace_buffer_ctrl_curr),
197 1.1 christos IPA_SYM(trace_buffer_lo),
198 1.1 christos IPA_SYM(trace_buffer_hi),
199 1.1 christos IPA_SYM(traceframe_read_count),
200 1.1 christos IPA_SYM(traceframe_write_count),
201 1.1 christos IPA_SYM(traceframes_created),
202 1.1 christos IPA_SYM(trace_state_variables),
203 1.1 christos IPA_SYM(get_raw_reg_ptr),
204 1.1 christos IPA_SYM(get_trace_state_variable_value_ptr),
205 1.1 christos IPA_SYM(set_trace_state_variable_value_ptr),
206 1.1 christos IPA_SYM(ust_loaded),
207 1.1 christos IPA_SYM(ipa_tdesc_idx),
208 1.1 christos };
209 1.1 christos
210 1.1 christos static struct ipa_sym_addresses ipa_sym_addrs;
211 1.1 christos
212 1.1 christos static int read_inferior_integer (CORE_ADDR symaddr, int *val);
213 1.1 christos
214 1.1 christos /* Returns true if both the in-process agent library and the static
215 1.1 christos tracepoints libraries are loaded in the inferior, and agent has
216 1.1 christos capability on static tracepoints. */
217 1.1 christos
218 1.1 christos static int
219 1.1 christos in_process_agent_supports_ust (void)
220 1.1 christos {
221 1.1 christos int loaded = 0;
222 1.1 christos
223 1.1 christos if (!agent_loaded_p ())
224 1.1 christos {
225 1.1 christos warning ("In-process agent not loaded");
226 1.1 christos return 0;
227 1.1 christos }
228 1.1 christos
229 1.1 christos if (agent_capability_check (AGENT_CAPA_STATIC_TRACE))
230 1.1 christos {
231 1.1 christos /* Agent understands static tracepoint, then check whether UST is in
232 1.1 christos fact loaded in the inferior. */
233 1.1 christos if (read_inferior_integer (ipa_sym_addrs.addr_ust_loaded, &loaded))
234 1.1 christos {
235 1.1 christos warning ("Error reading ust_loaded in lib");
236 1.1 christos return 0;
237 1.1 christos }
238 1.1 christos
239 1.1 christos return loaded;
240 1.1 christos }
241 1.1 christos else
242 1.1 christos return 0;
243 1.1 christos }
244 1.1 christos
245 1.1 christos static void
246 1.1 christos write_e_ipa_not_loaded (char *buffer)
247 1.1 christos {
248 1.1 christos sprintf (buffer,
249 1.1 christos "E.In-process agent library not loaded in process. "
250 1.1 christos "Fast and static tracepoints unavailable.");
251 1.1 christos }
252 1.1 christos
253 1.1 christos /* Write an error to BUFFER indicating that UST isn't loaded in the
254 1.1 christos inferior. */
255 1.1 christos
256 1.1 christos static void
257 1.1 christos write_e_ust_not_loaded (char *buffer)
258 1.1 christos {
259 1.1 christos #ifdef HAVE_UST
260 1.1 christos sprintf (buffer,
261 1.1 christos "E.UST library not loaded in process. "
262 1.1 christos "Static tracepoints unavailable.");
263 1.1 christos #else
264 1.1 christos sprintf (buffer, "E.GDBserver was built without static tracepoints support");
265 1.1 christos #endif
266 1.1 christos }
267 1.1 christos
268 1.1 christos /* If the in-process agent library isn't loaded in the inferior, write
269 1.1 christos an error to BUFFER, and return 1. Otherwise, return 0. */
270 1.1 christos
271 1.1 christos static int
272 1.1 christos maybe_write_ipa_not_loaded (char *buffer)
273 1.1 christos {
274 1.1 christos if (!agent_loaded_p ())
275 1.1 christos {
276 1.1 christos write_e_ipa_not_loaded (buffer);
277 1.1 christos return 1;
278 1.1 christos }
279 1.1 christos return 0;
280 1.1 christos }
281 1.1 christos
282 1.1 christos /* If the in-process agent library and the ust (static tracepoints)
283 1.1 christos library aren't loaded in the inferior, write an error to BUFFER,
284 1.1 christos and return 1. Otherwise, return 0. */
285 1.1 christos
286 1.1 christos static int
287 1.1 christos maybe_write_ipa_ust_not_loaded (char *buffer)
288 1.1 christos {
289 1.1 christos if (!agent_loaded_p ())
290 1.1 christos {
291 1.1 christos write_e_ipa_not_loaded (buffer);
292 1.1 christos return 1;
293 1.1 christos }
294 1.1 christos else if (!in_process_agent_supports_ust ())
295 1.1 christos {
296 1.1 christos write_e_ust_not_loaded (buffer);
297 1.1 christos return 1;
298 1.1 christos }
299 1.1 christos return 0;
300 1.1 christos }
301 1.1 christos
302 1.1 christos /* Cache all future symbols that the tracepoints module might request.
303 1.1 christos We can not request symbols at arbitrary states in the remote
304 1.1 christos protocol, only when the client tells us that new symbols are
305 1.1 christos available. So when we load the in-process library, make sure to
306 1.1 christos check the entire list. */
307 1.1 christos
308 1.1 christos void
309 1.1 christos tracepoint_look_up_symbols (void)
310 1.1 christos {
311 1.1 christos int i;
312 1.1 christos
313 1.1 christos if (agent_loaded_p ())
314 1.1 christos return;
315 1.1 christos
316 1.1 christos for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
317 1.1 christos {
318 1.1 christos CORE_ADDR *addrp =
319 1.1 christos (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
320 1.1 christos
321 1.1 christos if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
322 1.1 christos {
323 1.1.1.2 christos threads_debug_printf ("symbol `%s' not found", symbol_list[i].name);
324 1.1 christos return;
325 1.1 christos }
326 1.1 christos }
327 1.1 christos
328 1.1 christos agent_look_up_symbols (NULL);
329 1.1 christos }
330 1.1 christos
331 1.1 christos #endif
332 1.1 christos
333 1.1 christos /* GDBserver places a breakpoint on the IPA's version (which is a nop)
334 1.1 christos of the "stop_tracing" function. When this breakpoint is hit,
335 1.1 christos tracing stopped in the IPA for some reason. E.g., due to
336 1.1 christos tracepoint reaching the pass count, hitting conditional expression
337 1.1 christos evaluation error, etc.
338 1.1 christos
339 1.1 christos The IPA's trace buffer is never in circular tracing mode: instead,
340 1.1 christos GDBserver's is, and whenever the in-process buffer fills, it calls
341 1.1 christos "flush_trace_buffer", which triggers an internal breakpoint.
342 1.1 christos GDBserver reacts to this breakpoint by pulling the meanwhile
343 1.1 christos collected data. Old frames discarding is always handled on the
344 1.1 christos GDBserver side. */
345 1.1 christos
346 1.1 christos #ifdef IN_PROCESS_AGENT
347 1.1 christos int
348 1.1 christos read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
349 1.1 christos {
350 1.1 christos memcpy (myaddr, (void *) (uintptr_t) memaddr, len);
351 1.1 christos return 0;
352 1.1 christos }
353 1.1 christos
354 1.1 christos /* Call this in the functions where GDBserver places a breakpoint, so
355 1.1 christos that the compiler doesn't try to be clever and skip calling the
356 1.1 christos function at all. This is necessary, even if we tell the compiler
357 1.1 christos to not inline said functions. */
358 1.1 christos
359 1.1 christos #if defined(__GNUC__)
360 1.1 christos # define UNKNOWN_SIDE_EFFECTS() asm ("")
361 1.1 christos #else
362 1.1 christos # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
363 1.1 christos #endif
364 1.1 christos
365 1.1 christos /* This is needed for -Wmissing-declarations. */
366 1.1 christos IP_AGENT_EXPORT_FUNC void stop_tracing (void);
367 1.1 christos
368 1.1 christos IP_AGENT_EXPORT_FUNC void
369 1.1 christos stop_tracing (void)
370 1.1 christos {
371 1.1 christos /* GDBserver places breakpoint here. */
372 1.1 christos UNKNOWN_SIDE_EFFECTS();
373 1.1 christos }
374 1.1 christos
375 1.1 christos /* This is needed for -Wmissing-declarations. */
376 1.1 christos IP_AGENT_EXPORT_FUNC void flush_trace_buffer (void);
377 1.1 christos
378 1.1 christos IP_AGENT_EXPORT_FUNC void
379 1.1 christos flush_trace_buffer (void)
380 1.1 christos {
381 1.1 christos /* GDBserver places breakpoint here. */
382 1.1 christos UNKNOWN_SIDE_EFFECTS();
383 1.1 christos }
384 1.1 christos
385 1.1 christos #endif
386 1.1 christos
387 1.1 christos #ifndef IN_PROCESS_AGENT
388 1.1 christos static int
389 1.1 christos tracepoint_handler (CORE_ADDR address)
390 1.1 christos {
391 1.1 christos trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
392 1.1 christos paddress (address));
393 1.1 christos return 0;
394 1.1 christos }
395 1.1 christos
396 1.1 christos /* Breakpoint at "stop_tracing" in the inferior lib. */
397 1.1.1.2 christos static struct breakpoint *stop_tracing_bkpt;
398 1.1 christos static int stop_tracing_handler (CORE_ADDR);
399 1.1 christos
400 1.1 christos /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
401 1.1.1.2 christos static struct breakpoint *flush_trace_buffer_bkpt;
402 1.1 christos static int flush_trace_buffer_handler (CORE_ADDR);
403 1.1 christos
404 1.1 christos static void download_trace_state_variables (void);
405 1.1 christos static void upload_fast_traceframes (void);
406 1.1 christos
407 1.1 christos static int run_inferior_command (char *cmd, int len);
408 1.1 christos
409 1.1 christos static int
410 1.1 christos read_inferior_integer (CORE_ADDR symaddr, int *val)
411 1.1 christos {
412 1.1 christos return read_inferior_memory (symaddr, (unsigned char *) val,
413 1.1 christos sizeof (*val));
414 1.1 christos }
415 1.1 christos
416 1.1 christos struct tracepoint;
417 1.1 christos static int tracepoint_send_agent (struct tracepoint *tpoint);
418 1.1 christos
419 1.1 christos static int
420 1.1 christos read_inferior_uinteger (CORE_ADDR symaddr, unsigned int *val)
421 1.1 christos {
422 1.1 christos return read_inferior_memory (symaddr, (unsigned char *) val,
423 1.1 christos sizeof (*val));
424 1.1 christos }
425 1.1 christos
426 1.1 christos static int
427 1.1 christos read_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR *val)
428 1.1 christos {
429 1.1 christos void *pval = (void *) (uintptr_t) val;
430 1.1 christos int ret;
431 1.1 christos
432 1.1 christos ret = read_inferior_memory (symaddr, (unsigned char *) &pval, sizeof (pval));
433 1.1 christos *val = (uintptr_t) pval;
434 1.1 christos return ret;
435 1.1 christos }
436 1.1 christos
437 1.1 christos static int
438 1.1 christos write_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR val)
439 1.1 christos {
440 1.1 christos void *pval = (void *) (uintptr_t) val;
441 1.1 christos return target_write_memory (symaddr,
442 1.1 christos (unsigned char *) &pval, sizeof (pval));
443 1.1 christos }
444 1.1 christos
445 1.1 christos static int
446 1.1 christos write_inferior_integer (CORE_ADDR symaddr, int val)
447 1.1 christos {
448 1.1 christos return target_write_memory (symaddr, (unsigned char *) &val, sizeof (val));
449 1.1 christos }
450 1.1 christos
451 1.1 christos static int
452 1.1 christos write_inferior_int8 (CORE_ADDR symaddr, int8_t val)
453 1.1 christos {
454 1.1 christos return target_write_memory (symaddr, (unsigned char *) &val, sizeof (val));
455 1.1 christos }
456 1.1 christos
457 1.1 christos static int
458 1.1 christos write_inferior_uinteger (CORE_ADDR symaddr, unsigned int val)
459 1.1 christos {
460 1.1 christos return target_write_memory (symaddr, (unsigned char *) &val, sizeof (val));
461 1.1 christos }
462 1.1 christos
463 1.1 christos static CORE_ADDR target_malloc (ULONGEST size);
464 1.1 christos
465 1.1 christos #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD) \
466 1.1 christos do { \
467 1.1 christos memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \
468 1.1 christos BUF += sizeof ((OBJ)->FIELD); \
469 1.1 christos } while (0)
470 1.1 christos
471 1.1 christos #endif
472 1.1 christos
473 1.1 christos /* Base action. Concrete actions inherit this. */
474 1.1 christos
475 1.1 christos struct tracepoint_action
476 1.1 christos {
477 1.1 christos char type;
478 1.1 christos };
479 1.1 christos
480 1.1 christos /* An 'M' (collect memory) action. */
481 1.1 christos struct collect_memory_action
482 1.1 christos {
483 1.1 christos struct tracepoint_action base;
484 1.1 christos
485 1.1 christos ULONGEST addr;
486 1.1 christos ULONGEST len;
487 1.1 christos int32_t basereg;
488 1.1 christos };
489 1.1 christos
490 1.1 christos /* An 'R' (collect registers) action. */
491 1.1 christos
492 1.1 christos struct collect_registers_action
493 1.1 christos {
494 1.1 christos struct tracepoint_action base;
495 1.1 christos };
496 1.1 christos
497 1.1 christos /* An 'X' (evaluate expression) action. */
498 1.1 christos
499 1.1 christos struct eval_expr_action
500 1.1 christos {
501 1.1 christos struct tracepoint_action base;
502 1.1 christos
503 1.1 christos struct agent_expr *expr;
504 1.1 christos };
505 1.1 christos
506 1.1 christos /* An 'L' (collect static trace data) action. */
507 1.1 christos struct collect_static_trace_data_action
508 1.1 christos {
509 1.1 christos struct tracepoint_action base;
510 1.1 christos };
511 1.1 christos
512 1.1 christos #ifndef IN_PROCESS_AGENT
513 1.1 christos static CORE_ADDR
514 1.1 christos m_tracepoint_action_download (const struct tracepoint_action *action)
515 1.1 christos {
516 1.1 christos CORE_ADDR ipa_action = target_malloc (sizeof (struct collect_memory_action));
517 1.1 christos
518 1.1 christos target_write_memory (ipa_action, (unsigned char *) action,
519 1.1 christos sizeof (struct collect_memory_action));
520 1.1 christos
521 1.1 christos return ipa_action;
522 1.1 christos }
523 1.1 christos static char *
524 1.1 christos m_tracepoint_action_send (char *buffer, const struct tracepoint_action *action)
525 1.1 christos {
526 1.1 christos struct collect_memory_action *maction
527 1.1 christos = (struct collect_memory_action *) action;
528 1.1 christos
529 1.1 christos COPY_FIELD_TO_BUF (buffer, maction, addr);
530 1.1 christos COPY_FIELD_TO_BUF (buffer, maction, len);
531 1.1 christos COPY_FIELD_TO_BUF (buffer, maction, basereg);
532 1.1 christos
533 1.1 christos return buffer;
534 1.1 christos }
535 1.1 christos
536 1.1 christos static CORE_ADDR
537 1.1 christos r_tracepoint_action_download (const struct tracepoint_action *action)
538 1.1 christos {
539 1.1 christos CORE_ADDR ipa_action = target_malloc (sizeof (struct collect_registers_action));
540 1.1 christos
541 1.1 christos target_write_memory (ipa_action, (unsigned char *) action,
542 1.1 christos sizeof (struct collect_registers_action));
543 1.1 christos
544 1.1 christos return ipa_action;
545 1.1 christos }
546 1.1 christos
547 1.1 christos static char *
548 1.1 christos r_tracepoint_action_send (char *buffer, const struct tracepoint_action *action)
549 1.1 christos {
550 1.1 christos return buffer;
551 1.1 christos }
552 1.1 christos
553 1.1 christos static CORE_ADDR download_agent_expr (struct agent_expr *expr);
554 1.1 christos
555 1.1 christos static CORE_ADDR
556 1.1 christos x_tracepoint_action_download (const struct tracepoint_action *action)
557 1.1 christos {
558 1.1 christos CORE_ADDR ipa_action = target_malloc (sizeof (struct eval_expr_action));
559 1.1 christos CORE_ADDR expr;
560 1.1 christos
561 1.1 christos target_write_memory (ipa_action, (unsigned char *) action,
562 1.1 christos sizeof (struct eval_expr_action));
563 1.1 christos expr = download_agent_expr (((struct eval_expr_action *) action)->expr);
564 1.1 christos write_inferior_data_pointer (ipa_action
565 1.1 christos + offsetof (struct eval_expr_action, expr),
566 1.1 christos expr);
567 1.1 christos
568 1.1 christos return ipa_action;
569 1.1 christos }
570 1.1 christos
571 1.1 christos /* Copy agent expression AEXPR to buffer pointed by P. If AEXPR is NULL,
572 1.1 christos copy 0 to P. Return updated header of buffer. */
573 1.1 christos
574 1.1 christos static char *
575 1.1 christos agent_expr_send (char *p, const struct agent_expr *aexpr)
576 1.1 christos {
577 1.1 christos /* Copy the length of condition first, and then copy its
578 1.1 christos content. */
579 1.1 christos if (aexpr == NULL)
580 1.1 christos {
581 1.1 christos memset (p, 0, 4);
582 1.1 christos p += 4;
583 1.1 christos }
584 1.1 christos else
585 1.1 christos {
586 1.1 christos memcpy (p, &aexpr->length, 4);
587 1.1 christos p +=4;
588 1.1 christos
589 1.1 christos memcpy (p, aexpr->bytes, aexpr->length);
590 1.1 christos p += aexpr->length;
591 1.1 christos }
592 1.1 christos return p;
593 1.1 christos }
594 1.1 christos
595 1.1 christos static char *
596 1.1 christos x_tracepoint_action_send ( char *buffer, const struct tracepoint_action *action)
597 1.1 christos {
598 1.1 christos struct eval_expr_action *eaction = (struct eval_expr_action *) action;
599 1.1 christos
600 1.1 christos return agent_expr_send (buffer, eaction->expr);
601 1.1 christos }
602 1.1 christos
603 1.1 christos static CORE_ADDR
604 1.1 christos l_tracepoint_action_download (const struct tracepoint_action *action)
605 1.1 christos {
606 1.1 christos CORE_ADDR ipa_action
607 1.1 christos = target_malloc (sizeof (struct collect_static_trace_data_action));
608 1.1 christos
609 1.1 christos target_write_memory (ipa_action, (unsigned char *) action,
610 1.1 christos sizeof (struct collect_static_trace_data_action));
611 1.1 christos
612 1.1 christos return ipa_action;
613 1.1 christos }
614 1.1 christos
615 1.1 christos static char *
616 1.1 christos l_tracepoint_action_send (char *buffer, const struct tracepoint_action *action)
617 1.1 christos {
618 1.1 christos return buffer;
619 1.1 christos }
620 1.1 christos
621 1.1 christos static char *
622 1.1 christos tracepoint_action_send (char *buffer, const struct tracepoint_action *action)
623 1.1 christos {
624 1.1 christos switch (action->type)
625 1.1 christos {
626 1.1 christos case 'M':
627 1.1 christos return m_tracepoint_action_send (buffer, action);
628 1.1 christos case 'R':
629 1.1 christos return r_tracepoint_action_send (buffer, action);
630 1.1 christos case 'X':
631 1.1 christos return x_tracepoint_action_send (buffer, action);
632 1.1 christos case 'L':
633 1.1 christos return l_tracepoint_action_send (buffer, action);
634 1.1 christos }
635 1.1 christos error ("Unknown trace action '%c'.", action->type);
636 1.1 christos }
637 1.1 christos
638 1.1 christos static CORE_ADDR
639 1.1 christos tracepoint_action_download (const struct tracepoint_action *action)
640 1.1 christos {
641 1.1 christos switch (action->type)
642 1.1 christos {
643 1.1 christos case 'M':
644 1.1 christos return m_tracepoint_action_download (action);
645 1.1 christos case 'R':
646 1.1 christos return r_tracepoint_action_download (action);
647 1.1 christos case 'X':
648 1.1 christos return x_tracepoint_action_download (action);
649 1.1 christos case 'L':
650 1.1 christos return l_tracepoint_action_download (action);
651 1.1 christos }
652 1.1 christos error ("Unknown trace action '%c'.", action->type);
653 1.1 christos }
654 1.1 christos #endif
655 1.1 christos
656 1.1 christos /* This structure describes a piece of the source-level definition of
657 1.1 christos the tracepoint. The contents are not interpreted by the target,
658 1.1 christos but preserved verbatim for uploading upon reconnection. */
659 1.1 christos
660 1.1 christos struct source_string
661 1.1 christos {
662 1.1 christos /* The type of string, such as "cond" for a conditional. */
663 1.1 christos char *type;
664 1.1 christos
665 1.1 christos /* The source-level string itself. For the sake of target
666 1.1 christos debugging, we store it in plaintext, even though it is always
667 1.1 christos transmitted in hex. */
668 1.1 christos char *str;
669 1.1 christos
670 1.1 christos /* Link to the next one in the list. We link them in the order
671 1.1 christos received, in case some make up an ordered list of commands or
672 1.1 christos some such. */
673 1.1 christos struct source_string *next;
674 1.1 christos };
675 1.1 christos
676 1.1 christos enum tracepoint_type
677 1.1 christos {
678 1.1 christos /* Trap based tracepoint. */
679 1.1 christos trap_tracepoint,
680 1.1 christos
681 1.1 christos /* A fast tracepoint implemented with a jump instead of a trap. */
682 1.1 christos fast_tracepoint,
683 1.1 christos
684 1.1 christos /* A static tracepoint, implemented by a program call into a tracing
685 1.1 christos library. */
686 1.1 christos static_tracepoint
687 1.1 christos };
688 1.1 christos
689 1.1 christos struct tracepoint_hit_ctx;
690 1.1 christos
691 1.1 christos typedef enum eval_result_type (*condfn) (unsigned char *,
692 1.1 christos ULONGEST *);
693 1.1 christos
694 1.1 christos /* The definition of a tracepoint. */
695 1.1 christos
696 1.1 christos /* Tracepoints may have multiple locations, each at a different
697 1.1 christos address. This can occur with optimizations, template
698 1.1 christos instantiation, etc. Since the locations may be in different
699 1.1 christos scopes, the conditions and actions may be different for each
700 1.1 christos location. Our target version of tracepoints is more like GDB's
701 1.1 christos notion of "breakpoint locations", but we have almost nothing that
702 1.1 christos is not per-location, so we bother having two kinds of objects. The
703 1.1 christos key consequence is that numbers are not unique, and that it takes
704 1.1 christos both number and address to identify a tracepoint uniquely. */
705 1.1 christos
706 1.1 christos struct tracepoint
707 1.1 christos {
708 1.1 christos /* The number of the tracepoint, as specified by GDB. Several
709 1.1 christos tracepoint objects here may share a number. */
710 1.1 christos uint32_t number;
711 1.1 christos
712 1.1 christos /* Address at which the tracepoint is supposed to trigger. Several
713 1.1 christos tracepoints may share an address. */
714 1.1 christos CORE_ADDR address;
715 1.1 christos
716 1.1 christos /* Tracepoint type. */
717 1.1 christos enum tracepoint_type type;
718 1.1 christos
719 1.1 christos /* True if the tracepoint is currently enabled. */
720 1.1 christos int8_t enabled;
721 1.1 christos
722 1.1 christos /* The number of single steps that will be performed after each
723 1.1 christos tracepoint hit. */
724 1.1 christos uint64_t step_count;
725 1.1 christos
726 1.1 christos /* The number of times the tracepoint may be hit before it will
727 1.1 christos terminate the entire tracing run. */
728 1.1 christos uint64_t pass_count;
729 1.1 christos
730 1.1 christos /* Pointer to the agent expression that is the tracepoint's
731 1.1 christos conditional, or NULL if the tracepoint is unconditional. */
732 1.1 christos struct agent_expr *cond;
733 1.1 christos
734 1.1 christos /* The list of actions to take when the tracepoint triggers. */
735 1.1 christos uint32_t numactions;
736 1.1 christos struct tracepoint_action **actions;
737 1.1 christos
738 1.1 christos /* Count of the times we've hit this tracepoint during the run.
739 1.1 christos Note that while-stepping steps are not counted as "hits". */
740 1.1 christos uint64_t hit_count;
741 1.1 christos
742 1.1 christos /* Cached sum of the sizes of traceframes created by this point. */
743 1.1 christos uint64_t traceframe_usage;
744 1.1 christos
745 1.1 christos CORE_ADDR compiled_cond;
746 1.1 christos
747 1.1 christos /* Link to the next tracepoint in the list. */
748 1.1 christos struct tracepoint *next;
749 1.1 christos
750 1.1 christos #ifndef IN_PROCESS_AGENT
751 1.1 christos /* The list of actions to take when the tracepoint triggers, in
752 1.1 christos string/packet form. */
753 1.1 christos char **actions_str;
754 1.1 christos
755 1.1 christos /* The collection of strings that describe the tracepoint as it was
756 1.1 christos entered into GDB. These are not used by the target, but are
757 1.1 christos reported back to GDB upon reconnection. */
758 1.1 christos struct source_string *source_strings;
759 1.1 christos
760 1.1 christos /* The number of bytes displaced by fast tracepoints. It may subsume
761 1.1 christos multiple instructions, for multi-byte fast tracepoints. This
762 1.1 christos field is only valid for fast tracepoints. */
763 1.1 christos uint32_t orig_size;
764 1.1 christos
765 1.1 christos /* Only for fast tracepoints. */
766 1.1 christos CORE_ADDR obj_addr_on_target;
767 1.1 christos
768 1.1 christos /* Address range where the original instruction under a fast
769 1.1 christos tracepoint was relocated to. (_end is actually one byte past
770 1.1 christos the end). */
771 1.1 christos CORE_ADDR adjusted_insn_addr;
772 1.1 christos CORE_ADDR adjusted_insn_addr_end;
773 1.1 christos
774 1.1 christos /* The address range of the piece of the jump pad buffer that was
775 1.1 christos assigned to this fast tracepoint. (_end is actually one byte
776 1.1 christos past the end).*/
777 1.1 christos CORE_ADDR jump_pad;
778 1.1 christos CORE_ADDR jump_pad_end;
779 1.1 christos
780 1.1 christos /* The address range of the piece of the trampoline buffer that was
781 1.1 christos assigned to this fast tracepoint. (_end is actually one byte
782 1.1 christos past the end). */
783 1.1 christos CORE_ADDR trampoline;
784 1.1 christos CORE_ADDR trampoline_end;
785 1.1 christos
786 1.1 christos /* The list of actions to take while in a stepping loop. These
787 1.1 christos fields are only valid for patch-based tracepoints. */
788 1.1 christos int num_step_actions;
789 1.1 christos struct tracepoint_action **step_actions;
790 1.1 christos /* Same, but in string/packet form. */
791 1.1 christos char **step_actions_str;
792 1.1 christos
793 1.1 christos /* Handle returned by the breakpoint or tracepoint module when we
794 1.1 christos inserted the trap or jump, or hooked into a static tracepoint.
795 1.1 christos NULL if we haven't inserted it yet. */
796 1.1 christos void *handle;
797 1.1 christos #endif
798 1.1 christos
799 1.1 christos };
800 1.1 christos
801 1.1 christos #ifndef IN_PROCESS_AGENT
802 1.1 christos
803 1.1 christos /* Given `while-stepping', a thread may be collecting data for more
804 1.1 christos than one tracepoint simultaneously. On the other hand, the same
805 1.1 christos tracepoint with a while-stepping action may be hit by more than one
806 1.1 christos thread simultaneously (but not quite, each thread could be handling
807 1.1 christos a different step). Each thread holds a list of these objects,
808 1.1 christos representing the current step of each while-stepping action being
809 1.1 christos collected. */
810 1.1 christos
811 1.1 christos struct wstep_state
812 1.1 christos {
813 1.1 christos struct wstep_state *next;
814 1.1 christos
815 1.1 christos /* The tracepoint number. */
816 1.1 christos int tp_number;
817 1.1 christos /* The tracepoint's address. */
818 1.1 christos CORE_ADDR tp_address;
819 1.1 christos
820 1.1 christos /* The number of the current step in this 'while-stepping'
821 1.1 christos action. */
822 1.1 christos long current_step;
823 1.1 christos };
824 1.1 christos
825 1.1 christos #endif
826 1.1 christos
827 1.1 christos EXTERN_C_PUSH
828 1.1 christos
829 1.1 christos /* The linked list of all tracepoints. Marked explicitly as used as
830 1.1 christos the in-process library doesn't use it for the fast tracepoints
831 1.1 christos support. */
832 1.1 christos IP_AGENT_EXPORT_VAR struct tracepoint *tracepoints;
833 1.1 christos
834 1.1 christos /* The first tracepoint to exceed its pass count. */
835 1.1 christos
836 1.1 christos IP_AGENT_EXPORT_VAR struct tracepoint *stopping_tracepoint;
837 1.1 christos
838 1.1 christos /* True if the trace buffer is full or otherwise no longer usable. */
839 1.1 christos
840 1.1 christos IP_AGENT_EXPORT_VAR int trace_buffer_is_full;
841 1.1 christos
842 1.1 christos /* The first error that occurred during expression evaluation. */
843 1.1 christos
844 1.1 christos /* Stored as an int to avoid the IPA ABI being dependent on whatever
845 1.1 christos the compiler decides to use for the enum's underlying type. Holds
846 1.1 christos enum eval_result_type values. */
847 1.1 christos IP_AGENT_EXPORT_VAR int expr_eval_result = expr_eval_no_error;
848 1.1 christos
849 1.1 christos EXTERN_C_POP
850 1.1 christos
851 1.1 christos #ifndef IN_PROCESS_AGENT
852 1.1 christos
853 1.1 christos /* Pointer to the last tracepoint in the list, new tracepoints are
854 1.1 christos linked in at the end. */
855 1.1 christos
856 1.1 christos static struct tracepoint *last_tracepoint;
857 1.1 christos
858 1.1.1.2 christos static const char * const eval_result_names[] =
859 1.1 christos {
860 1.1 christos "terror:in the attic", /* this should never be reported */
861 1.1 christos "terror:empty expression",
862 1.1 christos "terror:empty stack",
863 1.1 christos "terror:stack overflow",
864 1.1 christos "terror:stack underflow",
865 1.1 christos "terror:unhandled opcode",
866 1.1 christos "terror:unrecognized opcode",
867 1.1 christos "terror:divide by zero"
868 1.1 christos };
869 1.1 christos
870 1.1 christos #endif
871 1.1 christos
872 1.1 christos /* The tracepoint in which the error occurred. */
873 1.1 christos
874 1.1 christos EXTERN_C_PUSH
875 1.1 christos IP_AGENT_EXPORT_VAR struct tracepoint *error_tracepoint;
876 1.1 christos EXTERN_C_POP
877 1.1 christos
878 1.1 christos struct trace_state_variable
879 1.1 christos {
880 1.1 christos /* This is the name of the variable as used in GDB. The target
881 1.1 christos doesn't use the name, but needs to have it for saving and
882 1.1 christos reconnection purposes. */
883 1.1 christos char *name;
884 1.1 christos
885 1.1 christos /* This number identifies the variable uniquely. Numbers may be
886 1.1 christos assigned either by the target (in the case of builtin variables),
887 1.1 christos or by GDB, and are presumed unique during the course of a trace
888 1.1 christos experiment. */
889 1.1 christos int number;
890 1.1 christos
891 1.1 christos /* The variable's initial value, a 64-bit signed integer always. */
892 1.1 christos LONGEST initial_value;
893 1.1 christos
894 1.1 christos /* The variable's value, a 64-bit signed integer always. */
895 1.1 christos LONGEST value;
896 1.1 christos
897 1.1 christos /* Pointer to a getter function, used to supply computed values. */
898 1.1 christos LONGEST (*getter) (void);
899 1.1 christos
900 1.1 christos /* Link to the next variable. */
901 1.1 christos struct trace_state_variable *next;
902 1.1 christos };
903 1.1 christos
904 1.1 christos /* Linked list of all trace state variables. */
905 1.1 christos
906 1.1 christos #ifdef IN_PROCESS_AGENT
907 1.1.1.2 christos static struct trace_state_variable *alloced_trace_state_variables;
908 1.1 christos #endif
909 1.1 christos
910 1.1 christos IP_AGENT_EXPORT_VAR struct trace_state_variable *trace_state_variables;
911 1.1 christos
912 1.1 christos /* The results of tracing go into a fixed-size space known as the
913 1.1 christos "trace buffer". Because usage follows a limited number of
914 1.1 christos patterns, we manage it ourselves rather than with malloc. Basic
915 1.1 christos rules are that we create only one trace frame at a time, each is
916 1.1 christos variable in size, they are never moved once created, and we only
917 1.1 christos discard if we are doing a circular buffer, and then only the oldest
918 1.1 christos ones. Each trace frame includes its own size, so we don't need to
919 1.1 christos link them together, and the trace frame number is relative to the
920 1.1 christos first one, so we don't need to record numbers. A trace frame also
921 1.1 christos records the number of the tracepoint that created it. The data
922 1.1 christos itself is a series of blocks, each introduced by a single character
923 1.1 christos and with a defined format. Each type of block has enough
924 1.1 christos type/length info to allow scanners to jump quickly from one block
925 1.1 christos to the next without reading each byte in the block. */
926 1.1 christos
927 1.1 christos /* Trace buffer management would be simple - advance a free pointer
928 1.1 christos from beginning to end, then stop - were it not for the circular
929 1.1 christos buffer option, which is a useful way to prevent a trace run from
930 1.1 christos stopping prematurely because the buffer filled up. In the circular
931 1.1 christos case, the location of the first trace frame (trace_buffer_start)
932 1.1 christos moves as old trace frames are discarded. Also, since we grow trace
933 1.1 christos frames incrementally as actions are performed, we wrap around to
934 1.1 christos the beginning of the trace buffer. This is per-block, so each
935 1.1 christos block within a trace frame remains contiguous. Things get messy
936 1.1 christos when the wrapped-around trace frame is the one being discarded; the
937 1.1 christos free space ends up in two parts at opposite ends of the buffer. */
938 1.1 christos
939 1.1 christos #ifndef ATTR_PACKED
940 1.1 christos # if defined(__GNUC__)
941 1.1 christos # define ATTR_PACKED __attribute__ ((packed))
942 1.1 christos # else
943 1.1 christos # define ATTR_PACKED /* nothing */
944 1.1 christos # endif
945 1.1 christos #endif
946 1.1 christos
947 1.1 christos /* The data collected at a tracepoint hit. This object should be as
948 1.1 christos small as possible, since there may be a great many of them. We do
949 1.1 christos not need to keep a frame number, because they are all sequential
950 1.1 christos and there are no deletions; so the Nth frame in the buffer is
951 1.1 christos always frame number N. */
952 1.1 christos
953 1.1 christos struct traceframe
954 1.1 christos {
955 1.1 christos /* Number of the tracepoint that collected this traceframe. A value
956 1.1 christos of 0 indicates the current end of the trace buffer. We make this
957 1.1 christos a 16-bit field because it's never going to happen that GDB's
958 1.1 christos numbering of tracepoints reaches 32,000. */
959 1.1 christos int tpnum : 16;
960 1.1 christos
961 1.1 christos /* The size of the data in this trace frame. We limit this to 32
962 1.1 christos bits, even on a 64-bit target, because it's just implausible that
963 1.1 christos one is validly going to collect 4 gigabytes of data at a single
964 1.1 christos tracepoint hit. */
965 1.1 christos unsigned int data_size : 32;
966 1.1 christos
967 1.1 christos /* The base of the trace data, which is contiguous from this point. */
968 1.1 christos unsigned char data[0];
969 1.1 christos
970 1.1 christos } ATTR_PACKED;
971 1.1 christos
972 1.1 christos /* The size of the EOB marker, in bytes. A traceframe with zeroed
973 1.1 christos fields (and no data) marks the end of trace data. */
974 1.1 christos #define TRACEFRAME_EOB_MARKER_SIZE offsetof (struct traceframe, data)
975 1.1 christos
976 1.1 christos /* This flag is true if the trace buffer is circular, meaning that
977 1.1 christos when it fills, the oldest trace frames are discarded in order to
978 1.1 christos make room. */
979 1.1 christos
980 1.1 christos #ifndef IN_PROCESS_AGENT
981 1.1 christos static int circular_trace_buffer;
982 1.1 christos #endif
983 1.1 christos
984 1.1 christos /* Size of the trace buffer. */
985 1.1 christos
986 1.1 christos static LONGEST trace_buffer_size;
987 1.1 christos
988 1.1 christos EXTERN_C_PUSH
989 1.1 christos
990 1.1 christos /* Pointer to the block of memory that traceframes all go into. */
991 1.1 christos
992 1.1 christos IP_AGENT_EXPORT_VAR unsigned char *trace_buffer_lo;
993 1.1 christos
994 1.1 christos /* Pointer to the end of the trace buffer, more precisely to the byte
995 1.1 christos after the end of the buffer. */
996 1.1 christos
997 1.1 christos IP_AGENT_EXPORT_VAR unsigned char *trace_buffer_hi;
998 1.1 christos
999 1.1 christos EXTERN_C_POP
1000 1.1 christos
1001 1.1 christos /* Control structure holding the read/write/etc. pointers into the
1002 1.1 christos trace buffer. We need more than one of these to implement a
1003 1.1 christos transaction-like mechanism to guarantees that both GDBserver and the
1004 1.1 christos in-process agent can try to change the trace buffer
1005 1.1 christos simultaneously. */
1006 1.1 christos
1007 1.1 christos struct trace_buffer_control
1008 1.1 christos {
1009 1.1 christos /* Pointer to the first trace frame in the buffer. In the
1010 1.1 christos non-circular case, this is equal to trace_buffer_lo, otherwise it
1011 1.1 christos moves around in the buffer. */
1012 1.1 christos unsigned char *start;
1013 1.1 christos
1014 1.1 christos /* Pointer to the free part of the trace buffer. Note that we clear
1015 1.1 christos several bytes at and after this pointer, so that traceframe
1016 1.1 christos scans/searches terminate properly. */
1017 1.1 christos unsigned char *free;
1018 1.1 christos
1019 1.1 christos /* Pointer to the byte after the end of the free part. Note that
1020 1.1 christos this may be smaller than trace_buffer_free in the circular case,
1021 1.1 christos and means that the free part is in two pieces. Initially it is
1022 1.1 christos equal to trace_buffer_hi, then is generally equivalent to
1023 1.1 christos trace_buffer_start. */
1024 1.1 christos unsigned char *end_free;
1025 1.1 christos
1026 1.1 christos /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
1027 1.1 christos this is the point at which the trace data breaks, and resumes at
1028 1.1 christos trace_buffer_lo. */
1029 1.1 christos unsigned char *wrap;
1030 1.1 christos };
1031 1.1 christos
1032 1.1 christos /* Same as above, to be used by GDBserver when updating the in-process
1033 1.1 christos agent. */
1034 1.1 christos struct ipa_trace_buffer_control
1035 1.1 christos {
1036 1.1 christos uintptr_t start;
1037 1.1 christos uintptr_t free;
1038 1.1 christos uintptr_t end_free;
1039 1.1 christos uintptr_t wrap;
1040 1.1 christos };
1041 1.1 christos
1042 1.1 christos
1043 1.1 christos /* We have possibly both GDBserver and an inferior thread accessing
1044 1.1 christos the same IPA trace buffer memory. The IPA is the producer (tries
1045 1.1 christos to put new frames in the buffer), while GDBserver occasionally
1046 1.1 christos consumes them, that is, flushes the IPA's buffer into its own
1047 1.1 christos buffer. Both sides need to update the trace buffer control
1048 1.1 christos pointers (current head, tail, etc.). We can't use a global lock to
1049 1.1 christos synchronize the accesses, as otherwise we could deadlock GDBserver
1050 1.1 christos (if the thread holding the lock stops for a signal, say). So
1051 1.1 christos instead of that, we use a transaction scheme where GDBserver writes
1052 1.1 christos always prevail over the IPAs writes, and, we have the IPA detect
1053 1.1 christos the commit failure/overwrite, and retry the whole attempt. This is
1054 1.1 christos mainly implemented by having a global token object that represents
1055 1.1 christos who wrote last to the buffer control structure. We need to freeze
1056 1.1 christos any inferior writing to the buffer while GDBserver touches memory,
1057 1.1 christos so that the inferior can correctly detect that GDBserver had been
1058 1.1 christos there, otherwise, it could mistakingly think its commit was
1059 1.1 christos successful; that's implemented by simply having GDBserver set a
1060 1.1 christos breakpoint the inferior hits if it is the critical region.
1061 1.1 christos
1062 1.1 christos There are three cycling trace buffer control structure copies
1063 1.1 christos (buffer head, tail, etc.), with the token object including an index
1064 1.1 christos indicating which is current live copy. The IPA tentatively builds
1065 1.1 christos an updated copy in a non-current control structure, while GDBserver
1066 1.1 christos always clobbers the current version directly. The IPA then tries
1067 1.1 christos to atomically "commit" its version; if GDBserver clobbered the
1068 1.1 christos structure meanwhile, that will fail, and the IPA restarts the
1069 1.1 christos allocation process.
1070 1.1 christos
1071 1.1 christos Listing the step in further detail, we have:
1072 1.1 christos
1073 1.1 christos In-process agent (producer):
1074 1.1 christos
1075 1.1 christos - passes by `about_to_request_buffer_space' breakpoint/lock
1076 1.1 christos
1077 1.1 christos - reads current token, extracts current trace buffer control index,
1078 1.1 christos and starts tentatively updating the rightmost one (0->1, 1->2,
1079 1.1 christos 2->0). Note that only one inferior thread is executing this code
1080 1.1 christos at any given time, due to an outer lock in the jump pads.
1081 1.1 christos
1082 1.1 christos - updates counters, and tries to commit the token.
1083 1.1 christos
1084 1.1 christos - passes by second `about_to_request_buffer_space' breakpoint/lock,
1085 1.1 christos leaving the sync region.
1086 1.1 christos
1087 1.1 christos - checks if the update was effective.
1088 1.1 christos
1089 1.1 christos - if trace buffer was found full, hits flush_trace_buffer
1090 1.1 christos breakpoint, and restarts later afterwards.
1091 1.1 christos
1092 1.1 christos GDBserver (consumer):
1093 1.1 christos
1094 1.1 christos - sets `about_to_request_buffer_space' breakpoint/lock.
1095 1.1 christos
1096 1.1 christos - updates the token unconditionally, using the current buffer
1097 1.1 christos control index, since it knows that the IP agent always writes to
1098 1.1 christos the rightmost, and due to the breakpoint, at most one IP thread
1099 1.1 christos can try to update the trace buffer concurrently to GDBserver, so
1100 1.1 christos there will be no danger of trace buffer control index wrap making
1101 1.1 christos the IPA write to the same index as GDBserver.
1102 1.1 christos
1103 1.1 christos - flushes the IP agent's trace buffer completely, and updates the
1104 1.1 christos current trace buffer control structure. GDBserver *always* wins.
1105 1.1 christos
1106 1.1 christos - removes the `about_to_request_buffer_space' breakpoint.
1107 1.1 christos
1108 1.1 christos The token is stored in the `trace_buffer_ctrl_curr' variable.
1109 1.1 christos Internally, it's bits are defined as:
1110 1.1 christos
1111 1.1 christos |-------------+-----+-------------+--------+-------------+--------------|
1112 1.1 christos | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1113 1.1 christos |-------------+-----+-------------+--------+-------------+--------------|
1114 1.1 christos | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1115 1.1 christos |-------------+-----+-------------+--------+-------------+--------------|
1116 1.1 christos
1117 1.1 christos GSB - GDBserver Stamp Bit
1118 1.1 christos PC - Previous Counter
1119 1.1 christos CC - Current Counter
1120 1.1 christos TBCI - Trace Buffer Control Index
1121 1.1 christos
1122 1.1 christos
1123 1.1 christos An IPA update of `trace_buffer_ctrl_curr' does:
1124 1.1 christos
1125 1.1 christos - read CC from the current token, save as PC.
1126 1.1 christos - updates pointers
1127 1.1 christos - atomically tries to write PC+1,CC
1128 1.1 christos
1129 1.1 christos A GDBserver update of `trace_buffer_ctrl_curr' does:
1130 1.1 christos
1131 1.1 christos - reads PC and CC from the current token.
1132 1.1 christos - updates pointers
1133 1.1 christos - writes GSB,PC,CC
1134 1.1 christos */
1135 1.1 christos
1136 1.1 christos /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1137 1.1 christos for the counters described below. The cleared bits are used to
1138 1.1 christos hold the index of the items of the `trace_buffer_ctrl' array that
1139 1.1 christos is "current". */
1140 1.1 christos #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1141 1.1 christos
1142 1.1 christos /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1143 1.1 christos counter, and the `current' counter. */
1144 1.1 christos
1145 1.1 christos #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1146 1.1 christos #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1147 1.1 christos
1148 1.1 christos /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1149 1.1 christos always stamps this bit as set. */
1150 1.1 christos #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1151 1.1 christos
1152 1.1 christos #ifdef IN_PROCESS_AGENT
1153 1.1 christos IP_AGENT_EXPORT_VAR struct trace_buffer_control trace_buffer_ctrl[3];
1154 1.1 christos IP_AGENT_EXPORT_VAR unsigned int trace_buffer_ctrl_curr;
1155 1.1 christos
1156 1.1 christos # define TRACE_BUFFER_CTRL_CURR \
1157 1.1 christos (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1158 1.1 christos
1159 1.1 christos #else
1160 1.1 christos
1161 1.1 christos /* The GDBserver side agent only needs one instance of this object, as
1162 1.1 christos it doesn't need to sync with itself. Define it as array anyway so
1163 1.1 christos that the rest of the code base doesn't need to care for the
1164 1.1 christos difference. */
1165 1.1.1.2 christos static trace_buffer_control trace_buffer_ctrl[1];
1166 1.1 christos # define TRACE_BUFFER_CTRL_CURR 0
1167 1.1 christos #endif
1168 1.1 christos
1169 1.1 christos /* These are convenience macros used to access the current trace
1170 1.1 christos buffer control in effect. */
1171 1.1 christos #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1172 1.1 christos #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1173 1.1 christos #define trace_buffer_end_free \
1174 1.1 christos (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1175 1.1 christos #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1176 1.1 christos
1177 1.1 christos
1178 1.1 christos /* Macro that returns a pointer to the first traceframe in the buffer. */
1179 1.1 christos
1180 1.1 christos #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1181 1.1 christos
1182 1.1 christos /* Macro that returns a pointer to the next traceframe in the buffer.
1183 1.1 christos If the computed location is beyond the wraparound point, subtract
1184 1.1 christos the offset of the wraparound. */
1185 1.1 christos
1186 1.1 christos #define NEXT_TRACEFRAME_1(TF) \
1187 1.1 christos (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1188 1.1 christos
1189 1.1 christos #define NEXT_TRACEFRAME(TF) \
1190 1.1 christos ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1191 1.1 christos - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1192 1.1 christos ? (trace_buffer_wrap - trace_buffer_lo) \
1193 1.1 christos : 0)))
1194 1.1 christos
1195 1.1 christos /* The difference between these counters represents the total number
1196 1.1 christos of complete traceframes present in the trace buffer. The IP agent
1197 1.1 christos writes to the write count, GDBserver writes to read count. */
1198 1.1 christos
1199 1.1 christos IP_AGENT_EXPORT_VAR unsigned int traceframe_write_count;
1200 1.1 christos IP_AGENT_EXPORT_VAR unsigned int traceframe_read_count;
1201 1.1 christos
1202 1.1 christos /* Convenience macro. */
1203 1.1 christos
1204 1.1 christos #define traceframe_count \
1205 1.1 christos ((unsigned int) (traceframe_write_count - traceframe_read_count))
1206 1.1 christos
1207 1.1 christos /* The count of all traceframes created in the current run, including
1208 1.1 christos ones that were discarded to make room. */
1209 1.1 christos
1210 1.1 christos IP_AGENT_EXPORT_VAR int traceframes_created;
1211 1.1 christos
1212 1.1 christos #ifndef IN_PROCESS_AGENT
1213 1.1 christos
1214 1.1 christos /* Read-only regions are address ranges whose contents don't change,
1215 1.1 christos and so can be read from target memory even while looking at a trace
1216 1.1 christos frame. Without these, disassembly for instance will likely fail,
1217 1.1 christos because the program code is not usually collected into a trace
1218 1.1 christos frame. This data structure does not need to be very complicated or
1219 1.1 christos particularly efficient, it's only going to be used occasionally,
1220 1.1 christos and only by some commands. */
1221 1.1 christos
1222 1.1 christos struct readonly_region
1223 1.1 christos {
1224 1.1 christos /* The bounds of the region. */
1225 1.1 christos CORE_ADDR start, end;
1226 1.1 christos
1227 1.1 christos /* Link to the next one. */
1228 1.1 christos struct readonly_region *next;
1229 1.1 christos };
1230 1.1 christos
1231 1.1 christos /* Linked list of readonly regions. This list stays in effect from
1232 1.1 christos one tstart to the next. */
1233 1.1 christos
1234 1.1 christos static struct readonly_region *readonly_regions;
1235 1.1 christos
1236 1.1 christos #endif
1237 1.1 christos
1238 1.1 christos /* The global that controls tracing overall. */
1239 1.1 christos
1240 1.1 christos IP_AGENT_EXPORT_VAR int tracing;
1241 1.1 christos
1242 1.1 christos #ifndef IN_PROCESS_AGENT
1243 1.1 christos
1244 1.1 christos /* Controls whether tracing should continue after GDB disconnects. */
1245 1.1 christos
1246 1.1 christos int disconnected_tracing;
1247 1.1 christos
1248 1.1 christos /* The reason for the last tracing run to have stopped. We initialize
1249 1.1 christos to a distinct string so that GDB can distinguish between "stopped
1250 1.1 christos after running" and "stopped because never run" cases. */
1251 1.1 christos
1252 1.1 christos static const char *tracing_stop_reason = "tnotrun";
1253 1.1 christos
1254 1.1 christos static int tracing_stop_tpnum;
1255 1.1 christos
1256 1.1 christos /* 64-bit timestamps for the trace run's start and finish, expressed
1257 1.1 christos in microseconds from the Unix epoch. */
1258 1.1 christos
1259 1.1.1.2 christos static LONGEST tracing_start_time;
1260 1.1.1.2 christos static LONGEST tracing_stop_time;
1261 1.1 christos
1262 1.1 christos /* The (optional) user-supplied name of the user that started the run.
1263 1.1 christos This is an arbitrary string, and may be NULL. */
1264 1.1 christos
1265 1.1.1.2 christos static char *tracing_user_name;
1266 1.1 christos
1267 1.1 christos /* Optional user-supplied text describing the run. This is
1268 1.1 christos an arbitrary string, and may be NULL. */
1269 1.1 christos
1270 1.1.1.2 christos static char *tracing_notes;
1271 1.1 christos
1272 1.1 christos /* Optional user-supplied text explaining a tstop command. This is an
1273 1.1 christos arbitrary string, and may be NULL. */
1274 1.1 christos
1275 1.1.1.2 christos static char *tracing_stop_note;
1276 1.1 christos
1277 1.1 christos #endif
1278 1.1 christos
1279 1.1 christos /* Functions local to this file. */
1280 1.1 christos
1281 1.1 christos /* Base "class" for tracepoint type specific data to be passed down to
1282 1.1 christos collect_data_at_tracepoint. */
1283 1.1 christos struct tracepoint_hit_ctx
1284 1.1 christos {
1285 1.1 christos enum tracepoint_type type;
1286 1.1 christos };
1287 1.1 christos
1288 1.1 christos #ifdef IN_PROCESS_AGENT
1289 1.1 christos
1290 1.1 christos /* Fast/jump tracepoint specific data to be passed down to
1291 1.1 christos collect_data_at_tracepoint. */
1292 1.1 christos struct fast_tracepoint_ctx
1293 1.1 christos {
1294 1.1 christos struct tracepoint_hit_ctx base;
1295 1.1 christos
1296 1.1 christos struct regcache regcache;
1297 1.1 christos int regcache_initted;
1298 1.1 christos unsigned char *regspace;
1299 1.1 christos
1300 1.1 christos unsigned char *regs;
1301 1.1 christos struct tracepoint *tpoint;
1302 1.1 christos };
1303 1.1 christos
1304 1.1 christos /* Static tracepoint specific data to be passed down to
1305 1.1 christos collect_data_at_tracepoint. */
1306 1.1 christos struct static_tracepoint_ctx
1307 1.1 christos {
1308 1.1 christos struct tracepoint_hit_ctx base;
1309 1.1 christos
1310 1.1 christos /* The regcache corresponding to the registers state at the time of
1311 1.1 christos the tracepoint hit. Initialized lazily, from REGS. */
1312 1.1 christos struct regcache regcache;
1313 1.1 christos int regcache_initted;
1314 1.1 christos
1315 1.1 christos /* The buffer space REGCACHE above uses. We use a separate buffer
1316 1.1 christos instead of letting the regcache malloc for both signal safety and
1317 1.1 christos performance reasons; this is allocated on the stack instead. */
1318 1.1 christos unsigned char *regspace;
1319 1.1 christos
1320 1.1 christos /* The register buffer as passed on by lttng/ust. */
1321 1.1 christos struct registers *regs;
1322 1.1 christos
1323 1.1 christos /* The "printf" formatter and the args the user passed to the marker
1324 1.1 christos call. We use this to be able to collect "static trace data"
1325 1.1 christos ($_sdata). */
1326 1.1 christos const char *fmt;
1327 1.1 christos va_list *args;
1328 1.1 christos
1329 1.1 christos /* The GDB tracepoint matching the probed marker that was "hit". */
1330 1.1 christos struct tracepoint *tpoint;
1331 1.1 christos };
1332 1.1 christos
1333 1.1 christos #else
1334 1.1 christos
1335 1.1 christos /* Static tracepoint specific data to be passed down to
1336 1.1 christos collect_data_at_tracepoint. */
1337 1.1 christos struct trap_tracepoint_ctx
1338 1.1 christos {
1339 1.1 christos struct tracepoint_hit_ctx base;
1340 1.1 christos
1341 1.1 christos struct regcache *regcache;
1342 1.1 christos };
1343 1.1 christos
1344 1.1 christos #endif
1345 1.1 christos
1346 1.1 christos #ifndef IN_PROCESS_AGENT
1347 1.1 christos static CORE_ADDR traceframe_get_pc (struct traceframe *tframe);
1348 1.1 christos static int traceframe_read_tsv (int num, LONGEST *val);
1349 1.1 christos #endif
1350 1.1 christos
1351 1.1 christos static int condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1352 1.1 christos struct tracepoint *tpoint);
1353 1.1 christos
1354 1.1 christos #ifndef IN_PROCESS_AGENT
1355 1.1 christos static void clear_readonly_regions (void);
1356 1.1 christos static void clear_installed_tracepoints (void);
1357 1.1 christos #endif
1358 1.1 christos
1359 1.1 christos static void collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1360 1.1 christos CORE_ADDR stop_pc,
1361 1.1 christos struct tracepoint *tpoint);
1362 1.1 christos #ifndef IN_PROCESS_AGENT
1363 1.1 christos static void collect_data_at_step (struct tracepoint_hit_ctx *ctx,
1364 1.1 christos CORE_ADDR stop_pc,
1365 1.1 christos struct tracepoint *tpoint, int current_step);
1366 1.1 christos static void compile_tracepoint_condition (struct tracepoint *tpoint,
1367 1.1 christos CORE_ADDR *jump_entry);
1368 1.1 christos #endif
1369 1.1 christos static void do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1370 1.1 christos CORE_ADDR stop_pc,
1371 1.1 christos struct tracepoint *tpoint,
1372 1.1 christos struct traceframe *tframe,
1373 1.1 christos struct tracepoint_action *taction);
1374 1.1 christos
1375 1.1 christos #ifndef IN_PROCESS_AGENT
1376 1.1 christos static struct tracepoint *fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR);
1377 1.1 christos
1378 1.1 christos static void install_tracepoint (struct tracepoint *, char *own_buf);
1379 1.1 christos static void download_tracepoint (struct tracepoint *);
1380 1.1 christos static int install_fast_tracepoint (struct tracepoint *, char *errbuf);
1381 1.1 christos static void clone_fast_tracepoint (struct tracepoint *to,
1382 1.1 christos const struct tracepoint *from);
1383 1.1 christos #endif
1384 1.1 christos
1385 1.1 christos static LONGEST get_timestamp (void);
1386 1.1 christos
1387 1.1 christos #if defined(__GNUC__)
1388 1.1 christos # define memory_barrier() asm volatile ("" : : : "memory")
1389 1.1 christos #else
1390 1.1 christos # define memory_barrier() do {} while (0)
1391 1.1 christos #endif
1392 1.1 christos
1393 1.1 christos /* We only build the IPA if this builtin is supported, and there are
1394 1.1 christos no uses of this in GDBserver itself, so we're safe in defining this
1395 1.1 christos unconditionally. */
1396 1.1 christos #define cmpxchg(mem, oldval, newval) \
1397 1.1 christos __sync_val_compare_and_swap (mem, oldval, newval)
1398 1.1 christos
1399 1.1 christos /* Record that an error occurred during expression evaluation. */
1400 1.1 christos
1401 1.1 christos static void
1402 1.1 christos record_tracepoint_error (struct tracepoint *tpoint, const char *which,
1403 1.1 christos enum eval_result_type rtype)
1404 1.1 christos {
1405 1.1 christos trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1406 1.1 christos tpoint->number, paddress (tpoint->address), which, rtype);
1407 1.1 christos
1408 1.1 christos #ifdef IN_PROCESS_AGENT
1409 1.1 christos /* Only record the first error we get. */
1410 1.1 christos if (cmpxchg (&expr_eval_result,
1411 1.1 christos expr_eval_no_error,
1412 1.1 christos rtype) != expr_eval_no_error)
1413 1.1 christos return;
1414 1.1 christos #else
1415 1.1 christos if (expr_eval_result != expr_eval_no_error)
1416 1.1 christos return;
1417 1.1 christos #endif
1418 1.1 christos
1419 1.1 christos error_tracepoint = tpoint;
1420 1.1 christos }
1421 1.1 christos
1422 1.1 christos /* Trace buffer management. */
1423 1.1 christos
1424 1.1 christos static void
1425 1.1 christos clear_trace_buffer (void)
1426 1.1 christos {
1427 1.1 christos trace_buffer_start = trace_buffer_lo;
1428 1.1 christos trace_buffer_free = trace_buffer_lo;
1429 1.1 christos trace_buffer_end_free = trace_buffer_hi;
1430 1.1 christos trace_buffer_wrap = trace_buffer_hi;
1431 1.1 christos /* A traceframe with zeroed fields marks the end of trace data. */
1432 1.1 christos ((struct traceframe *) trace_buffer_free)->tpnum = 0;
1433 1.1 christos ((struct traceframe *) trace_buffer_free)->data_size = 0;
1434 1.1 christos traceframe_read_count = traceframe_write_count = 0;
1435 1.1 christos traceframes_created = 0;
1436 1.1 christos }
1437 1.1 christos
1438 1.1 christos #ifndef IN_PROCESS_AGENT
1439 1.1 christos
1440 1.1 christos static void
1441 1.1 christos clear_inferior_trace_buffer (void)
1442 1.1 christos {
1443 1.1 christos CORE_ADDR ipa_trace_buffer_lo;
1444 1.1 christos CORE_ADDR ipa_trace_buffer_hi;
1445 1.1 christos struct traceframe ipa_traceframe = { 0 };
1446 1.1 christos struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
1447 1.1 christos
1448 1.1 christos read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
1449 1.1 christos &ipa_trace_buffer_lo);
1450 1.1 christos read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
1451 1.1 christos &ipa_trace_buffer_hi);
1452 1.1 christos
1453 1.1 christos ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
1454 1.1 christos ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
1455 1.1 christos ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
1456 1.1 christos ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
1457 1.1 christos
1458 1.1 christos /* A traceframe with zeroed fields marks the end of trace data. */
1459 1.1 christos target_write_memory (ipa_sym_addrs.addr_trace_buffer_ctrl,
1460 1.1 christos (unsigned char *) &ipa_trace_buffer_ctrl,
1461 1.1 christos sizeof (ipa_trace_buffer_ctrl));
1462 1.1 christos
1463 1.1 christos write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr, 0);
1464 1.1 christos
1465 1.1 christos /* A traceframe with zeroed fields marks the end of trace data. */
1466 1.1 christos target_write_memory (ipa_trace_buffer_lo,
1467 1.1 christos (unsigned char *) &ipa_traceframe,
1468 1.1 christos sizeof (ipa_traceframe));
1469 1.1 christos
1470 1.1 christos write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count, 0);
1471 1.1 christos write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count, 0);
1472 1.1 christos write_inferior_integer (ipa_sym_addrs.addr_traceframes_created, 0);
1473 1.1 christos }
1474 1.1 christos
1475 1.1 christos #endif
1476 1.1 christos
1477 1.1 christos static void
1478 1.1 christos init_trace_buffer (LONGEST bufsize)
1479 1.1 christos {
1480 1.1 christos size_t alloc_size;
1481 1.1 christos
1482 1.1 christos trace_buffer_size = bufsize;
1483 1.1 christos
1484 1.1 christos /* Make sure to internally allocate at least space for the EOB
1485 1.1 christos marker. */
1486 1.1 christos alloc_size = (bufsize < TRACEFRAME_EOB_MARKER_SIZE
1487 1.1 christos ? TRACEFRAME_EOB_MARKER_SIZE : bufsize);
1488 1.1 christos trace_buffer_lo = (unsigned char *) xrealloc (trace_buffer_lo, alloc_size);
1489 1.1 christos
1490 1.1 christos trace_buffer_hi = trace_buffer_lo + trace_buffer_size;
1491 1.1 christos
1492 1.1 christos clear_trace_buffer ();
1493 1.1 christos }
1494 1.1 christos
1495 1.1 christos #ifdef IN_PROCESS_AGENT
1496 1.1 christos
1497 1.1 christos /* This is needed for -Wmissing-declarations. */
1498 1.1 christos IP_AGENT_EXPORT_FUNC void about_to_request_buffer_space (void);
1499 1.1 christos
1500 1.1 christos IP_AGENT_EXPORT_FUNC void
1501 1.1 christos about_to_request_buffer_space (void)
1502 1.1 christos {
1503 1.1 christos /* GDBserver places breakpoint here while it goes about to flush
1504 1.1 christos data at random times. */
1505 1.1 christos UNKNOWN_SIDE_EFFECTS();
1506 1.1 christos }
1507 1.1 christos
1508 1.1 christos #endif
1509 1.1 christos
1510 1.1 christos /* Carve out a piece of the trace buffer, returning NULL in case of
1511 1.1 christos failure. */
1512 1.1 christos
1513 1.1 christos static void *
1514 1.1 christos trace_buffer_alloc (size_t amt)
1515 1.1 christos {
1516 1.1 christos unsigned char *rslt;
1517 1.1 christos struct trace_buffer_control *tbctrl;
1518 1.1 christos unsigned int curr;
1519 1.1 christos #ifdef IN_PROCESS_AGENT
1520 1.1 christos unsigned int prev, prev_filtered;
1521 1.1 christos unsigned int commit_count;
1522 1.1 christos unsigned int commit;
1523 1.1 christos unsigned int readout;
1524 1.1 christos #else
1525 1.1 christos struct traceframe *oldest;
1526 1.1 christos unsigned char *new_start;
1527 1.1 christos #endif
1528 1.1 christos
1529 1.1 christos trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1530 1.1 christos (long) amt, (long) sizeof (struct traceframe));
1531 1.1 christos
1532 1.1 christos /* Account for the EOB marker. */
1533 1.1 christos amt += TRACEFRAME_EOB_MARKER_SIZE;
1534 1.1 christos
1535 1.1 christos #ifdef IN_PROCESS_AGENT
1536 1.1 christos again:
1537 1.1 christos memory_barrier ();
1538 1.1 christos
1539 1.1 christos /* Read the current token and extract the index to try to write to,
1540 1.1 christos storing it in CURR. */
1541 1.1 christos prev = trace_buffer_ctrl_curr;
1542 1.1 christos prev_filtered = prev & ~GDBSERVER_FLUSH_COUNT_MASK;
1543 1.1 christos curr = prev_filtered + 1;
1544 1.1 christos if (curr > 2)
1545 1.1 christos curr = 0;
1546 1.1 christos
1547 1.1 christos about_to_request_buffer_space ();
1548 1.1 christos
1549 1.1 christos /* Start out with a copy of the current state. GDBserver may be
1550 1.1 christos midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1551 1.1 christos be able to commit anyway if that happens. */
1552 1.1 christos trace_buffer_ctrl[curr]
1553 1.1 christos = trace_buffer_ctrl[prev_filtered];
1554 1.1 christos trace_debug ("trying curr=%u", curr);
1555 1.1 christos #else
1556 1.1 christos /* The GDBserver's agent doesn't need all that syncing, and always
1557 1.1 christos updates TCB 0 (there's only one, mind you). */
1558 1.1 christos curr = 0;
1559 1.1 christos #endif
1560 1.1 christos tbctrl = &trace_buffer_ctrl[curr];
1561 1.1 christos
1562 1.1 christos /* Offsets are easier to grok for debugging than raw addresses,
1563 1.1 christos especially for the small trace buffer sizes that are useful for
1564 1.1 christos testing. */
1565 1.1 christos trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1566 1.1 christos curr,
1567 1.1 christos (int) (tbctrl->start - trace_buffer_lo),
1568 1.1 christos (int) (tbctrl->free - trace_buffer_lo),
1569 1.1 christos (int) (tbctrl->end_free - trace_buffer_lo),
1570 1.1 christos (int) (tbctrl->wrap - trace_buffer_lo),
1571 1.1 christos (int) (trace_buffer_hi - trace_buffer_lo));
1572 1.1 christos
1573 1.1 christos /* The algorithm here is to keep trying to get a contiguous block of
1574 1.1 christos the requested size, possibly discarding older traceframes to free
1575 1.1 christos up space. Since free space might come in one or two pieces,
1576 1.1 christos depending on whether discarded traceframes wrapped around at the
1577 1.1 christos high end of the buffer, we test both pieces after each
1578 1.1 christos discard. */
1579 1.1 christos while (1)
1580 1.1 christos {
1581 1.1 christos /* First, if we have two free parts, try the upper one first. */
1582 1.1 christos if (tbctrl->end_free < tbctrl->free)
1583 1.1 christos {
1584 1.1 christos if (tbctrl->free + amt <= trace_buffer_hi)
1585 1.1 christos /* We have enough in the upper part. */
1586 1.1 christos break;
1587 1.1 christos else
1588 1.1 christos {
1589 1.1 christos /* Our high part of free space wasn't enough. Give up
1590 1.1 christos on it for now, set wraparound. We will recover the
1591 1.1 christos space later, if/when the wrapped-around traceframe is
1592 1.1 christos discarded. */
1593 1.1 christos trace_debug ("Upper part too small, setting wraparound");
1594 1.1 christos tbctrl->wrap = tbctrl->free;
1595 1.1 christos tbctrl->free = trace_buffer_lo;
1596 1.1 christos }
1597 1.1 christos }
1598 1.1 christos
1599 1.1 christos /* The normal case. */
1600 1.1 christos if (tbctrl->free + amt <= tbctrl->end_free)
1601 1.1 christos break;
1602 1.1 christos
1603 1.1 christos #ifdef IN_PROCESS_AGENT
1604 1.1 christos /* The IP Agent's buffer is always circular. It isn't used
1605 1.1 christos currently, but `circular_trace_buffer' could represent
1606 1.1 christos GDBserver's mode. If we didn't find space, ask GDBserver to
1607 1.1 christos flush. */
1608 1.1 christos
1609 1.1 christos flush_trace_buffer ();
1610 1.1 christos memory_barrier ();
1611 1.1 christos if (tracing)
1612 1.1 christos {
1613 1.1 christos trace_debug ("gdbserver flushed buffer, retrying");
1614 1.1 christos goto again;
1615 1.1 christos }
1616 1.1 christos
1617 1.1 christos /* GDBserver cancelled the tracing. Bail out as well. */
1618 1.1 christos return NULL;
1619 1.1 christos #else
1620 1.1 christos /* If we're here, then neither part is big enough, and
1621 1.1 christos non-circular trace buffers are now full. */
1622 1.1 christos if (!circular_trace_buffer)
1623 1.1 christos {
1624 1.1 christos trace_debug ("Not enough space in the trace buffer");
1625 1.1 christos return NULL;
1626 1.1 christos }
1627 1.1 christos
1628 1.1 christos trace_debug ("Need more space in the trace buffer");
1629 1.1 christos
1630 1.1 christos /* If we have a circular buffer, we can try discarding the
1631 1.1 christos oldest traceframe and see if that helps. */
1632 1.1 christos oldest = FIRST_TRACEFRAME ();
1633 1.1 christos if (oldest->tpnum == 0)
1634 1.1 christos {
1635 1.1 christos /* Not good; we have no traceframes to free. Perhaps we're
1636 1.1 christos asking for a block that is larger than the buffer? In
1637 1.1 christos any case, give up. */
1638 1.1 christos trace_debug ("No traceframes to discard");
1639 1.1 christos return NULL;
1640 1.1 christos }
1641 1.1 christos
1642 1.1 christos /* We don't run this code in the in-process agent currently.
1643 1.1 christos E.g., we could leave the in-process agent in autonomous
1644 1.1 christos circular mode if we only have fast tracepoints. If we do
1645 1.1 christos that, then this bit becomes racy with GDBserver, which also
1646 1.1 christos writes to this counter. */
1647 1.1 christos --traceframe_write_count;
1648 1.1 christos
1649 1.1 christos new_start = (unsigned char *) NEXT_TRACEFRAME (oldest);
1650 1.1 christos /* If we freed the traceframe that wrapped around, go back
1651 1.1 christos to the non-wrap case. */
1652 1.1 christos if (new_start < tbctrl->start)
1653 1.1 christos {
1654 1.1 christos trace_debug ("Discarding past the wraparound");
1655 1.1 christos tbctrl->wrap = trace_buffer_hi;
1656 1.1 christos }
1657 1.1 christos tbctrl->start = new_start;
1658 1.1 christos tbctrl->end_free = tbctrl->start;
1659 1.1 christos
1660 1.1 christos trace_debug ("Discarded a traceframe\n"
1661 1.1 christos "Trace buffer [%d], start=%d free=%d "
1662 1.1 christos "endfree=%d wrap=%d hi=%d",
1663 1.1 christos curr,
1664 1.1 christos (int) (tbctrl->start - trace_buffer_lo),
1665 1.1 christos (int) (tbctrl->free - trace_buffer_lo),
1666 1.1 christos (int) (tbctrl->end_free - trace_buffer_lo),
1667 1.1 christos (int) (tbctrl->wrap - trace_buffer_lo),
1668 1.1 christos (int) (trace_buffer_hi - trace_buffer_lo));
1669 1.1 christos
1670 1.1 christos /* Now go back around the loop. The discard might have resulted
1671 1.1 christos in either one or two pieces of free space, so we want to try
1672 1.1 christos both before freeing any more traceframes. */
1673 1.1 christos #endif
1674 1.1 christos }
1675 1.1 christos
1676 1.1 christos /* If we get here, we know we can provide the asked-for space. */
1677 1.1 christos
1678 1.1 christos rslt = tbctrl->free;
1679 1.1 christos
1680 1.1 christos /* Adjust the request back down, now that we know we have space for
1681 1.1 christos the marker, but don't commit to AMT yet, we may still need to
1682 1.1 christos restart the operation if GDBserver touches the trace buffer
1683 1.1 christos (obviously only important in the in-process agent's version). */
1684 1.1 christos tbctrl->free += (amt - sizeof (struct traceframe));
1685 1.1 christos
1686 1.1 christos /* Or not. If GDBserver changed the trace buffer behind our back,
1687 1.1 christos we get to restart a new allocation attempt. */
1688 1.1 christos
1689 1.1 christos #ifdef IN_PROCESS_AGENT
1690 1.1 christos /* Build the tentative token. */
1691 1.1 christos commit_count = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) + 0x100)
1692 1.1 christos & GDBSERVER_FLUSH_COUNT_MASK_CURR);
1693 1.1 christos commit = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) << 12)
1694 1.1 christos | commit_count
1695 1.1 christos | curr);
1696 1.1 christos
1697 1.1 christos /* Try to commit it. */
1698 1.1 christos readout = cmpxchg (&trace_buffer_ctrl_curr, prev, commit);
1699 1.1 christos if (readout != prev)
1700 1.1 christos {
1701 1.1 christos trace_debug ("GDBserver has touched the trace buffer, restarting."
1702 1.1 christos " (prev=%08x, commit=%08x, readout=%08x)",
1703 1.1 christos prev, commit, readout);
1704 1.1 christos goto again;
1705 1.1 christos }
1706 1.1 christos
1707 1.1 christos /* Hold your horses here. Even if that change was committed,
1708 1.1 christos GDBserver could come in, and clobber it. We need to hold to be
1709 1.1 christos able to tell if GDBserver clobbers before or after we committed
1710 1.1 christos the change. Whenever GDBserver goes about touching the IPA
1711 1.1 christos buffer, it sets a breakpoint in this routine, so we have a sync
1712 1.1 christos point here. */
1713 1.1 christos about_to_request_buffer_space ();
1714 1.1 christos
1715 1.1 christos /* Check if the change has been effective, even if GDBserver stopped
1716 1.1 christos us at the breakpoint. */
1717 1.1 christos
1718 1.1 christos {
1719 1.1 christos unsigned int refetch;
1720 1.1 christos
1721 1.1 christos memory_barrier ();
1722 1.1 christos
1723 1.1 christos refetch = trace_buffer_ctrl_curr;
1724 1.1 christos
1725 1.1 christos if (refetch == commit
1726 1.1 christos || ((refetch & GDBSERVER_FLUSH_COUNT_MASK_PREV) >> 12) == commit_count)
1727 1.1 christos {
1728 1.1 christos /* effective */
1729 1.1 christos trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1730 1.1 christos "readout=%08x, refetch=%08x)",
1731 1.1 christos prev, commit, readout, refetch);
1732 1.1 christos }
1733 1.1 christos else
1734 1.1 christos {
1735 1.1 christos trace_debug ("GDBserver has touched the trace buffer, not effective."
1736 1.1 christos " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1737 1.1 christos prev, commit, readout, refetch);
1738 1.1 christos goto again;
1739 1.1 christos }
1740 1.1 christos }
1741 1.1 christos #endif
1742 1.1 christos
1743 1.1 christos /* We have a new piece of the trace buffer. Hurray! */
1744 1.1 christos
1745 1.1 christos /* Add an EOB marker just past this allocation. */
1746 1.1 christos ((struct traceframe *) tbctrl->free)->tpnum = 0;
1747 1.1 christos ((struct traceframe *) tbctrl->free)->data_size = 0;
1748 1.1 christos
1749 1.1 christos /* Adjust the request back down, now that we know we have space for
1750 1.1 christos the marker. */
1751 1.1 christos amt -= sizeof (struct traceframe);
1752 1.1 christos
1753 1.1 christos if (debug_threads)
1754 1.1 christos {
1755 1.1 christos trace_debug ("Allocated %d bytes", (int) amt);
1756 1.1 christos trace_debug ("Trace buffer [%d] start=%d free=%d "
1757 1.1 christos "endfree=%d wrap=%d hi=%d",
1758 1.1 christos curr,
1759 1.1 christos (int) (tbctrl->start - trace_buffer_lo),
1760 1.1 christos (int) (tbctrl->free - trace_buffer_lo),
1761 1.1 christos (int) (tbctrl->end_free - trace_buffer_lo),
1762 1.1 christos (int) (tbctrl->wrap - trace_buffer_lo),
1763 1.1 christos (int) (trace_buffer_hi - trace_buffer_lo));
1764 1.1 christos }
1765 1.1 christos
1766 1.1 christos return rslt;
1767 1.1 christos }
1768 1.1 christos
1769 1.1 christos #ifndef IN_PROCESS_AGENT
1770 1.1 christos
1771 1.1 christos /* Return the total free space. This is not necessarily the largest
1772 1.1 christos block we can allocate, because of the two-part case. */
1773 1.1 christos
1774 1.1 christos static int
1775 1.1 christos free_space (void)
1776 1.1 christos {
1777 1.1 christos if (trace_buffer_free <= trace_buffer_end_free)
1778 1.1 christos return trace_buffer_end_free - trace_buffer_free;
1779 1.1 christos else
1780 1.1 christos return ((trace_buffer_end_free - trace_buffer_lo)
1781 1.1 christos + (trace_buffer_hi - trace_buffer_free));
1782 1.1 christos }
1783 1.1 christos
1784 1.1 christos /* An 'S' in continuation packets indicates remainder are for
1785 1.1 christos while-stepping. */
1786 1.1 christos
1787 1.1 christos static int seen_step_action_flag;
1788 1.1 christos
1789 1.1 christos /* Create a tracepoint (location) with given number and address. Add this
1790 1.1 christos new tracepoint to list and sort this list. */
1791 1.1 christos
1792 1.1 christos static struct tracepoint *
1793 1.1 christos add_tracepoint (int num, CORE_ADDR addr)
1794 1.1 christos {
1795 1.1 christos struct tracepoint *tpoint, **tp_next;
1796 1.1 christos
1797 1.1 christos tpoint = XNEW (struct tracepoint);
1798 1.1 christos tpoint->number = num;
1799 1.1 christos tpoint->address = addr;
1800 1.1 christos tpoint->numactions = 0;
1801 1.1 christos tpoint->actions = NULL;
1802 1.1 christos tpoint->actions_str = NULL;
1803 1.1 christos tpoint->cond = NULL;
1804 1.1 christos tpoint->num_step_actions = 0;
1805 1.1 christos tpoint->step_actions = NULL;
1806 1.1 christos tpoint->step_actions_str = NULL;
1807 1.1 christos /* Start all off as regular (slow) tracepoints. */
1808 1.1 christos tpoint->type = trap_tracepoint;
1809 1.1 christos tpoint->orig_size = -1;
1810 1.1 christos tpoint->source_strings = NULL;
1811 1.1 christos tpoint->compiled_cond = 0;
1812 1.1 christos tpoint->handle = NULL;
1813 1.1 christos tpoint->next = NULL;
1814 1.1 christos
1815 1.1 christos /* Find a place to insert this tracepoint into list in order to keep
1816 1.1 christos the tracepoint list still in the ascending order. There may be
1817 1.1 christos multiple tracepoints at the same address as TPOINT's, and this
1818 1.1 christos guarantees TPOINT is inserted after all the tracepoints which are
1819 1.1 christos set at the same address. For example, fast tracepoints A, B, C are
1820 1.1 christos set at the same address, and D is to be insert at the same place as
1821 1.1 christos well,
1822 1.1 christos
1823 1.1 christos -->| A |--> | B |-->| C |->...
1824 1.1 christos
1825 1.1 christos One jump pad was created for tracepoint A, B, and C, and the target
1826 1.1 christos address of A is referenced/used in jump pad. So jump pad will let
1827 1.1 christos inferior jump to A. If D is inserted in front of A, like this,
1828 1.1 christos
1829 1.1 christos -->| D |-->| A |--> | B |-->| C |->...
1830 1.1 christos
1831 1.1 christos without updating jump pad, D is not reachable during collect, which
1832 1.1 christos is wrong. As we can see, the order of B, C and D doesn't matter, but
1833 1.1 christos A should always be the `first' one. */
1834 1.1 christos for (tp_next = &tracepoints;
1835 1.1 christos (*tp_next) != NULL && (*tp_next)->address <= tpoint->address;
1836 1.1 christos tp_next = &(*tp_next)->next)
1837 1.1 christos ;
1838 1.1 christos tpoint->next = *tp_next;
1839 1.1 christos *tp_next = tpoint;
1840 1.1 christos last_tracepoint = tpoint;
1841 1.1 christos
1842 1.1 christos seen_step_action_flag = 0;
1843 1.1 christos
1844 1.1 christos return tpoint;
1845 1.1 christos }
1846 1.1 christos
1847 1.1 christos #ifndef IN_PROCESS_AGENT
1848 1.1 christos
1849 1.1 christos /* Return the tracepoint with the given number and address, or NULL. */
1850 1.1 christos
1851 1.1 christos static struct tracepoint *
1852 1.1 christos find_tracepoint (int id, CORE_ADDR addr)
1853 1.1 christos {
1854 1.1 christos struct tracepoint *tpoint;
1855 1.1 christos
1856 1.1 christos for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
1857 1.1 christos if (tpoint->number == id && tpoint->address == addr)
1858 1.1 christos return tpoint;
1859 1.1 christos
1860 1.1 christos return NULL;
1861 1.1 christos }
1862 1.1 christos
1863 1.1 christos /* Remove TPOINT from global list. */
1864 1.1 christos
1865 1.1 christos static void
1866 1.1 christos remove_tracepoint (struct tracepoint *tpoint)
1867 1.1 christos {
1868 1.1 christos struct tracepoint *tp, *tp_prev;
1869 1.1 christos
1870 1.1 christos for (tp = tracepoints, tp_prev = NULL; tp && tp != tpoint;
1871 1.1 christos tp_prev = tp, tp = tp->next)
1872 1.1 christos ;
1873 1.1 christos
1874 1.1 christos if (tp)
1875 1.1 christos {
1876 1.1 christos if (tp_prev)
1877 1.1 christos tp_prev->next = tp->next;
1878 1.1 christos else
1879 1.1 christos tracepoints = tp->next;
1880 1.1 christos
1881 1.1 christos xfree (tp);
1882 1.1 christos }
1883 1.1 christos }
1884 1.1 christos
1885 1.1 christos /* There may be several tracepoints with the same number (because they
1886 1.1 christos are "locations", in GDB parlance); return the next one after the
1887 1.1 christos given tracepoint, or search from the beginning of the list if the
1888 1.1 christos first argument is NULL. */
1889 1.1 christos
1890 1.1 christos static struct tracepoint *
1891 1.1 christos find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num)
1892 1.1 christos {
1893 1.1 christos struct tracepoint *tpoint;
1894 1.1 christos
1895 1.1 christos if (prev_tp)
1896 1.1 christos tpoint = prev_tp->next;
1897 1.1 christos else
1898 1.1 christos tpoint = tracepoints;
1899 1.1 christos for (; tpoint; tpoint = tpoint->next)
1900 1.1 christos if (tpoint->number == num)
1901 1.1 christos return tpoint;
1902 1.1 christos
1903 1.1 christos return NULL;
1904 1.1 christos }
1905 1.1 christos
1906 1.1 christos #endif
1907 1.1 christos
1908 1.1 christos /* Append another action to perform when the tracepoint triggers. */
1909 1.1 christos
1910 1.1 christos static void
1911 1.1 christos add_tracepoint_action (struct tracepoint *tpoint, const char *packet)
1912 1.1 christos {
1913 1.1 christos const char *act;
1914 1.1 christos
1915 1.1 christos if (*packet == 'S')
1916 1.1 christos {
1917 1.1 christos seen_step_action_flag = 1;
1918 1.1 christos ++packet;
1919 1.1 christos }
1920 1.1 christos
1921 1.1 christos act = packet;
1922 1.1 christos
1923 1.1 christos while (*act)
1924 1.1 christos {
1925 1.1 christos const char *act_start = act;
1926 1.1 christos struct tracepoint_action *action = NULL;
1927 1.1 christos
1928 1.1 christos switch (*act)
1929 1.1 christos {
1930 1.1 christos case 'M':
1931 1.1 christos {
1932 1.1 christos struct collect_memory_action *maction =
1933 1.1 christos XNEW (struct collect_memory_action);
1934 1.1 christos ULONGEST basereg;
1935 1.1 christos int is_neg;
1936 1.1 christos
1937 1.1 christos maction->base.type = *act;
1938 1.1 christos action = &maction->base;
1939 1.1 christos
1940 1.1 christos ++act;
1941 1.1 christos is_neg = (*act == '-');
1942 1.1 christos if (*act == '-')
1943 1.1 christos ++act;
1944 1.1 christos act = unpack_varlen_hex (act, &basereg);
1945 1.1 christos ++act;
1946 1.1 christos act = unpack_varlen_hex (act, &maction->addr);
1947 1.1 christos ++act;
1948 1.1 christos act = unpack_varlen_hex (act, &maction->len);
1949 1.1 christos maction->basereg = (is_neg
1950 1.1 christos ? - (int) basereg
1951 1.1 christos : (int) basereg);
1952 1.1 christos trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1953 1.1 christos pulongest (maction->len),
1954 1.1 christos paddress (maction->addr), maction->basereg);
1955 1.1 christos break;
1956 1.1 christos }
1957 1.1 christos case 'R':
1958 1.1 christos {
1959 1.1 christos struct collect_registers_action *raction =
1960 1.1 christos XNEW (struct collect_registers_action);
1961 1.1 christos
1962 1.1 christos raction->base.type = *act;
1963 1.1 christos action = &raction->base;
1964 1.1 christos
1965 1.1 christos trace_debug ("Want to collect registers");
1966 1.1 christos ++act;
1967 1.1 christos /* skip past hex digits of mask for now */
1968 1.1 christos while (isxdigit(*act))
1969 1.1 christos ++act;
1970 1.1 christos break;
1971 1.1 christos }
1972 1.1 christos case 'L':
1973 1.1 christos {
1974 1.1 christos struct collect_static_trace_data_action *raction =
1975 1.1 christos XNEW (struct collect_static_trace_data_action);
1976 1.1 christos
1977 1.1 christos raction->base.type = *act;
1978 1.1 christos action = &raction->base;
1979 1.1 christos
1980 1.1 christos trace_debug ("Want to collect static trace data");
1981 1.1 christos ++act;
1982 1.1 christos break;
1983 1.1 christos }
1984 1.1 christos case 'S':
1985 1.1 christos trace_debug ("Unexpected step action, ignoring");
1986 1.1 christos ++act;
1987 1.1 christos break;
1988 1.1 christos case 'X':
1989 1.1 christos {
1990 1.1 christos struct eval_expr_action *xaction = XNEW (struct eval_expr_action);
1991 1.1 christos
1992 1.1 christos xaction->base.type = *act;
1993 1.1 christos action = &xaction->base;
1994 1.1 christos
1995 1.1 christos trace_debug ("Want to evaluate expression");
1996 1.1 christos xaction->expr = gdb_parse_agent_expr (&act);
1997 1.1 christos break;
1998 1.1 christos }
1999 1.1 christos default:
2000 1.1 christos trace_debug ("unknown trace action '%c', ignoring...", *act);
2001 1.1 christos break;
2002 1.1 christos case '-':
2003 1.1 christos break;
2004 1.1 christos }
2005 1.1 christos
2006 1.1 christos if (action == NULL)
2007 1.1 christos break;
2008 1.1 christos
2009 1.1 christos if (seen_step_action_flag)
2010 1.1 christos {
2011 1.1 christos tpoint->num_step_actions++;
2012 1.1 christos
2013 1.1 christos tpoint->step_actions
2014 1.1 christos = XRESIZEVEC (struct tracepoint_action *, tpoint->step_actions,
2015 1.1 christos tpoint->num_step_actions);
2016 1.1 christos tpoint->step_actions_str
2017 1.1 christos = XRESIZEVEC (char *, tpoint->step_actions_str,
2018 1.1 christos tpoint->num_step_actions);
2019 1.1 christos tpoint->step_actions[tpoint->num_step_actions - 1] = action;
2020 1.1 christos tpoint->step_actions_str[tpoint->num_step_actions - 1]
2021 1.1 christos = savestring (act_start, act - act_start);
2022 1.1 christos }
2023 1.1 christos else
2024 1.1 christos {
2025 1.1 christos tpoint->numactions++;
2026 1.1 christos tpoint->actions
2027 1.1 christos = XRESIZEVEC (struct tracepoint_action *, tpoint->actions,
2028 1.1 christos tpoint->numactions);
2029 1.1 christos tpoint->actions_str
2030 1.1 christos = XRESIZEVEC (char *, tpoint->actions_str, tpoint->numactions);
2031 1.1 christos tpoint->actions[tpoint->numactions - 1] = action;
2032 1.1 christos tpoint->actions_str[tpoint->numactions - 1]
2033 1.1 christos = savestring (act_start, act - act_start);
2034 1.1 christos }
2035 1.1 christos }
2036 1.1 christos }
2037 1.1 christos
2038 1.1 christos #endif
2039 1.1 christos
2040 1.1 christos /* Find or create a trace state variable with the given number. */
2041 1.1 christos
2042 1.1 christos static struct trace_state_variable *
2043 1.1 christos get_trace_state_variable (int num)
2044 1.1 christos {
2045 1.1 christos struct trace_state_variable *tsv;
2046 1.1 christos
2047 1.1 christos #ifdef IN_PROCESS_AGENT
2048 1.1 christos /* Search for an existing variable. */
2049 1.1 christos for (tsv = alloced_trace_state_variables; tsv; tsv = tsv->next)
2050 1.1 christos if (tsv->number == num)
2051 1.1 christos return tsv;
2052 1.1 christos #endif
2053 1.1 christos
2054 1.1 christos /* Search for an existing variable. */
2055 1.1 christos for (tsv = trace_state_variables; tsv; tsv = tsv->next)
2056 1.1 christos if (tsv->number == num)
2057 1.1 christos return tsv;
2058 1.1 christos
2059 1.1 christos return NULL;
2060 1.1 christos }
2061 1.1 christos
2062 1.1 christos /* Find or create a trace state variable with the given number. */
2063 1.1 christos
2064 1.1 christos static struct trace_state_variable *
2065 1.1 christos create_trace_state_variable (int num, int gdb)
2066 1.1 christos {
2067 1.1 christos struct trace_state_variable *tsv;
2068 1.1 christos
2069 1.1 christos tsv = get_trace_state_variable (num);
2070 1.1 christos if (tsv != NULL)
2071 1.1 christos return tsv;
2072 1.1 christos
2073 1.1 christos /* Create a new variable. */
2074 1.1 christos tsv = XNEW (struct trace_state_variable);
2075 1.1 christos tsv->number = num;
2076 1.1 christos tsv->initial_value = 0;
2077 1.1 christos tsv->value = 0;
2078 1.1 christos tsv->getter = NULL;
2079 1.1 christos tsv->name = NULL;
2080 1.1 christos #ifdef IN_PROCESS_AGENT
2081 1.1 christos if (!gdb)
2082 1.1 christos {
2083 1.1 christos tsv->next = alloced_trace_state_variables;
2084 1.1 christos alloced_trace_state_variables = tsv;
2085 1.1 christos }
2086 1.1 christos else
2087 1.1 christos #endif
2088 1.1 christos {
2089 1.1 christos tsv->next = trace_state_variables;
2090 1.1 christos trace_state_variables = tsv;
2091 1.1 christos }
2092 1.1 christos return tsv;
2093 1.1 christos }
2094 1.1 christos
2095 1.1 christos /* This is needed for -Wmissing-declarations. */
2096 1.1 christos IP_AGENT_EXPORT_FUNC LONGEST get_trace_state_variable_value (int num);
2097 1.1 christos
2098 1.1 christos IP_AGENT_EXPORT_FUNC LONGEST
2099 1.1 christos get_trace_state_variable_value (int num)
2100 1.1 christos {
2101 1.1 christos struct trace_state_variable *tsv;
2102 1.1 christos
2103 1.1 christos tsv = get_trace_state_variable (num);
2104 1.1 christos
2105 1.1 christos if (!tsv)
2106 1.1 christos {
2107 1.1 christos trace_debug ("No trace state variable %d, skipping value get", num);
2108 1.1 christos return 0;
2109 1.1 christos }
2110 1.1 christos
2111 1.1 christos /* Call a getter function if we have one. While it's tempting to
2112 1.1 christos set up something to only call the getter once per tracepoint hit,
2113 1.1 christos it could run afoul of thread races. Better to let the getter
2114 1.1 christos handle it directly, if necessary to worry about it. */
2115 1.1 christos if (tsv->getter)
2116 1.1 christos tsv->value = (tsv->getter) ();
2117 1.1 christos
2118 1.1 christos trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2119 1.1 christos num, plongest (tsv->value));
2120 1.1 christos
2121 1.1 christos return tsv->value;
2122 1.1 christos }
2123 1.1 christos
2124 1.1 christos /* This is needed for -Wmissing-declarations. */
2125 1.1 christos IP_AGENT_EXPORT_FUNC void set_trace_state_variable_value (int num,
2126 1.1 christos LONGEST val);
2127 1.1 christos
2128 1.1 christos IP_AGENT_EXPORT_FUNC void
2129 1.1 christos set_trace_state_variable_value (int num, LONGEST val)
2130 1.1 christos {
2131 1.1 christos struct trace_state_variable *tsv;
2132 1.1 christos
2133 1.1 christos tsv = get_trace_state_variable (num);
2134 1.1 christos
2135 1.1 christos if (!tsv)
2136 1.1 christos {
2137 1.1 christos trace_debug ("No trace state variable %d, skipping value set", num);
2138 1.1 christos return;
2139 1.1 christos }
2140 1.1 christos
2141 1.1 christos tsv->value = val;
2142 1.1 christos }
2143 1.1 christos
2144 1.1 christos LONGEST
2145 1.1 christos agent_get_trace_state_variable_value (int num)
2146 1.1 christos {
2147 1.1 christos return get_trace_state_variable_value (num);
2148 1.1 christos }
2149 1.1 christos
2150 1.1 christos void
2151 1.1 christos agent_set_trace_state_variable_value (int num, LONGEST val)
2152 1.1 christos {
2153 1.1 christos set_trace_state_variable_value (num, val);
2154 1.1 christos }
2155 1.1 christos
2156 1.1 christos static void
2157 1.1 christos set_trace_state_variable_name (int num, const char *name)
2158 1.1 christos {
2159 1.1 christos struct trace_state_variable *tsv;
2160 1.1 christos
2161 1.1 christos tsv = get_trace_state_variable (num);
2162 1.1 christos
2163 1.1 christos if (!tsv)
2164 1.1 christos {
2165 1.1 christos trace_debug ("No trace state variable %d, skipping name set", num);
2166 1.1 christos return;
2167 1.1 christos }
2168 1.1 christos
2169 1.1 christos tsv->name = (char *) name;
2170 1.1 christos }
2171 1.1 christos
2172 1.1 christos static void
2173 1.1 christos set_trace_state_variable_getter (int num, LONGEST (*getter) (void))
2174 1.1 christos {
2175 1.1 christos struct trace_state_variable *tsv;
2176 1.1 christos
2177 1.1 christos tsv = get_trace_state_variable (num);
2178 1.1 christos
2179 1.1 christos if (!tsv)
2180 1.1 christos {
2181 1.1 christos trace_debug ("No trace state variable %d, skipping getter set", num);
2182 1.1 christos return;
2183 1.1 christos }
2184 1.1 christos
2185 1.1 christos tsv->getter = getter;
2186 1.1 christos }
2187 1.1 christos
2188 1.1 christos /* Add a raw traceframe for the given tracepoint. */
2189 1.1 christos
2190 1.1 christos static struct traceframe *
2191 1.1 christos add_traceframe (struct tracepoint *tpoint)
2192 1.1 christos {
2193 1.1 christos struct traceframe *tframe;
2194 1.1 christos
2195 1.1 christos tframe
2196 1.1 christos = (struct traceframe *) trace_buffer_alloc (sizeof (struct traceframe));
2197 1.1 christos
2198 1.1 christos if (tframe == NULL)
2199 1.1 christos return NULL;
2200 1.1 christos
2201 1.1 christos tframe->tpnum = tpoint->number;
2202 1.1 christos tframe->data_size = 0;
2203 1.1 christos
2204 1.1 christos return tframe;
2205 1.1 christos }
2206 1.1 christos
2207 1.1 christos /* Add a block to the traceframe currently being worked on. */
2208 1.1 christos
2209 1.1 christos static unsigned char *
2210 1.1 christos add_traceframe_block (struct traceframe *tframe,
2211 1.1 christos struct tracepoint *tpoint, int amt)
2212 1.1 christos {
2213 1.1 christos unsigned char *block;
2214 1.1 christos
2215 1.1 christos if (!tframe)
2216 1.1 christos return NULL;
2217 1.1 christos
2218 1.1 christos block = (unsigned char *) trace_buffer_alloc (amt);
2219 1.1 christos
2220 1.1 christos if (!block)
2221 1.1 christos return NULL;
2222 1.1 christos
2223 1.1 christos gdb_assert (tframe->tpnum == tpoint->number);
2224 1.1 christos
2225 1.1 christos tframe->data_size += amt;
2226 1.1 christos tpoint->traceframe_usage += amt;
2227 1.1 christos
2228 1.1 christos return block;
2229 1.1 christos }
2230 1.1 christos
2231 1.1 christos /* Flag that the current traceframe is finished. */
2232 1.1 christos
2233 1.1 christos static void
2234 1.1 christos finish_traceframe (struct traceframe *tframe)
2235 1.1 christos {
2236 1.1 christos ++traceframe_write_count;
2237 1.1 christos ++traceframes_created;
2238 1.1 christos }
2239 1.1 christos
2240 1.1 christos #ifndef IN_PROCESS_AGENT
2241 1.1 christos
2242 1.1 christos /* Given a traceframe number NUM, find the NUMth traceframe in the
2243 1.1 christos buffer. */
2244 1.1 christos
2245 1.1 christos static struct traceframe *
2246 1.1 christos find_traceframe (int num)
2247 1.1 christos {
2248 1.1 christos struct traceframe *tframe;
2249 1.1 christos int tfnum = 0;
2250 1.1 christos
2251 1.1 christos for (tframe = FIRST_TRACEFRAME ();
2252 1.1 christos tframe->tpnum != 0;
2253 1.1 christos tframe = NEXT_TRACEFRAME (tframe))
2254 1.1 christos {
2255 1.1 christos if (tfnum == num)
2256 1.1 christos return tframe;
2257 1.1 christos ++tfnum;
2258 1.1 christos }
2259 1.1 christos
2260 1.1 christos return NULL;
2261 1.1 christos }
2262 1.1 christos
2263 1.1 christos static CORE_ADDR
2264 1.1 christos get_traceframe_address (struct traceframe *tframe)
2265 1.1 christos {
2266 1.1 christos CORE_ADDR addr;
2267 1.1 christos struct tracepoint *tpoint;
2268 1.1 christos
2269 1.1 christos addr = traceframe_get_pc (tframe);
2270 1.1 christos
2271 1.1 christos if (addr)
2272 1.1 christos return addr;
2273 1.1 christos
2274 1.1 christos /* Fallback strategy, will be incorrect for while-stepping frames
2275 1.1 christos and multi-location tracepoints. */
2276 1.1 christos tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
2277 1.1 christos return tpoint->address;
2278 1.1 christos }
2279 1.1 christos
2280 1.1 christos /* Search for the next traceframe whose address is inside or outside
2281 1.1 christos the given range. */
2282 1.1 christos
2283 1.1 christos static struct traceframe *
2284 1.1 christos find_next_traceframe_in_range (CORE_ADDR lo, CORE_ADDR hi, int inside_p,
2285 1.1 christos int *tfnump)
2286 1.1 christos {
2287 1.1 christos client_state &cs = get_client_state ();
2288 1.1 christos struct traceframe *tframe;
2289 1.1 christos CORE_ADDR tfaddr;
2290 1.1 christos
2291 1.1 christos *tfnump = cs.current_traceframe + 1;
2292 1.1 christos tframe = find_traceframe (*tfnump);
2293 1.1 christos /* The search is not supposed to wrap around. */
2294 1.1 christos if (!tframe)
2295 1.1 christos {
2296 1.1 christos *tfnump = -1;
2297 1.1 christos return NULL;
2298 1.1 christos }
2299 1.1 christos
2300 1.1 christos for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2301 1.1 christos {
2302 1.1 christos tfaddr = get_traceframe_address (tframe);
2303 1.1 christos if (inside_p
2304 1.1 christos ? (lo <= tfaddr && tfaddr <= hi)
2305 1.1 christos : (lo > tfaddr || tfaddr > hi))
2306 1.1 christos return tframe;
2307 1.1 christos ++*tfnump;
2308 1.1 christos }
2309 1.1 christos
2310 1.1 christos *tfnump = -1;
2311 1.1 christos return NULL;
2312 1.1 christos }
2313 1.1 christos
2314 1.1 christos /* Search for the next traceframe recorded by the given tracepoint.
2315 1.1 christos Note that for multi-location tracepoints, this will find whatever
2316 1.1 christos location appears first. */
2317 1.1 christos
2318 1.1 christos static struct traceframe *
2319 1.1 christos find_next_traceframe_by_tracepoint (int num, int *tfnump)
2320 1.1 christos {
2321 1.1 christos client_state &cs = get_client_state ();
2322 1.1 christos struct traceframe *tframe;
2323 1.1 christos
2324 1.1 christos *tfnump = cs.current_traceframe + 1;
2325 1.1 christos tframe = find_traceframe (*tfnump);
2326 1.1 christos /* The search is not supposed to wrap around. */
2327 1.1 christos if (!tframe)
2328 1.1 christos {
2329 1.1 christos *tfnump = -1;
2330 1.1 christos return NULL;
2331 1.1 christos }
2332 1.1 christos
2333 1.1 christos for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2334 1.1 christos {
2335 1.1 christos if (tframe->tpnum == num)
2336 1.1 christos return tframe;
2337 1.1 christos ++*tfnump;
2338 1.1 christos }
2339 1.1 christos
2340 1.1 christos *tfnump = -1;
2341 1.1 christos return NULL;
2342 1.1 christos }
2343 1.1 christos
2344 1.1 christos #endif
2345 1.1 christos
2346 1.1 christos #ifndef IN_PROCESS_AGENT
2347 1.1 christos
2348 1.1 christos /* Clear all past trace state. */
2349 1.1 christos
2350 1.1 christos static void
2351 1.1 christos cmd_qtinit (char *packet)
2352 1.1 christos {
2353 1.1 christos client_state &cs = get_client_state ();
2354 1.1 christos struct trace_state_variable *tsv, *prev, *next;
2355 1.1 christos
2356 1.1 christos /* Can't do this command without a pid attached. */
2357 1.1 christos if (current_thread == NULL)
2358 1.1 christos {
2359 1.1 christos write_enn (packet);
2360 1.1 christos return;
2361 1.1 christos }
2362 1.1 christos
2363 1.1 christos /* Make sure we don't try to read from a trace frame. */
2364 1.1 christos cs.current_traceframe = -1;
2365 1.1 christos
2366 1.1 christos stop_tracing ();
2367 1.1 christos
2368 1.1 christos trace_debug ("Initializing the trace");
2369 1.1 christos
2370 1.1 christos clear_installed_tracepoints ();
2371 1.1 christos clear_readonly_regions ();
2372 1.1 christos
2373 1.1 christos tracepoints = NULL;
2374 1.1 christos last_tracepoint = NULL;
2375 1.1 christos
2376 1.1 christos /* Clear out any leftover trace state variables. Ones with target
2377 1.1 christos defined getters should be kept however. */
2378 1.1 christos prev = NULL;
2379 1.1 christos tsv = trace_state_variables;
2380 1.1 christos while (tsv)
2381 1.1 christos {
2382 1.1 christos trace_debug ("Looking at var %d", tsv->number);
2383 1.1 christos if (tsv->getter == NULL)
2384 1.1 christos {
2385 1.1 christos next = tsv->next;
2386 1.1 christos if (prev)
2387 1.1 christos prev->next = next;
2388 1.1 christos else
2389 1.1 christos trace_state_variables = next;
2390 1.1 christos trace_debug ("Deleting var %d", tsv->number);
2391 1.1 christos free (tsv);
2392 1.1 christos tsv = next;
2393 1.1 christos }
2394 1.1 christos else
2395 1.1 christos {
2396 1.1 christos prev = tsv;
2397 1.1 christos tsv = tsv->next;
2398 1.1 christos }
2399 1.1 christos }
2400 1.1 christos
2401 1.1 christos clear_trace_buffer ();
2402 1.1 christos clear_inferior_trace_buffer ();
2403 1.1 christos
2404 1.1 christos write_ok (packet);
2405 1.1 christos }
2406 1.1 christos
2407 1.1 christos /* Unprobe the UST marker at ADDRESS. */
2408 1.1 christos
2409 1.1 christos static void
2410 1.1 christos unprobe_marker_at (CORE_ADDR address)
2411 1.1 christos {
2412 1.1 christos char cmd[IPA_CMD_BUF_SIZE];
2413 1.1 christos
2414 1.1 christos sprintf (cmd, "unprobe_marker_at:%s", paddress (address));
2415 1.1 christos run_inferior_command (cmd, strlen (cmd) + 1);
2416 1.1 christos }
2417 1.1 christos
2418 1.1 christos /* Restore the program to its pre-tracing state. This routine may be called
2419 1.1 christos in error situations, so it needs to be careful about only restoring
2420 1.1 christos from known-valid bits. */
2421 1.1 christos
2422 1.1 christos static void
2423 1.1 christos clear_installed_tracepoints (void)
2424 1.1 christos {
2425 1.1 christos struct tracepoint *tpoint;
2426 1.1 christos struct tracepoint *prev_stpoint;
2427 1.1 christos
2428 1.1 christos target_pause_all (true);
2429 1.1 christos
2430 1.1 christos prev_stpoint = NULL;
2431 1.1 christos
2432 1.1 christos /* Restore any bytes overwritten by tracepoints. */
2433 1.1 christos for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2434 1.1 christos {
2435 1.1 christos /* Catch the case where we might try to remove a tracepoint that
2436 1.1 christos was never actually installed. */
2437 1.1 christos if (tpoint->handle == NULL)
2438 1.1 christos {
2439 1.1 christos trace_debug ("Tracepoint %d at 0x%s was "
2440 1.1 christos "never installed, nothing to clear",
2441 1.1 christos tpoint->number, paddress (tpoint->address));
2442 1.1 christos continue;
2443 1.1 christos }
2444 1.1 christos
2445 1.1 christos switch (tpoint->type)
2446 1.1 christos {
2447 1.1 christos case trap_tracepoint:
2448 1.1 christos {
2449 1.1 christos struct breakpoint *bp
2450 1.1 christos = (struct breakpoint *) tpoint->handle;
2451 1.1 christos
2452 1.1 christos delete_breakpoint (bp);
2453 1.1 christos }
2454 1.1 christos break;
2455 1.1 christos case fast_tracepoint:
2456 1.1 christos {
2457 1.1 christos struct fast_tracepoint_jump *jump
2458 1.1 christos = (struct fast_tracepoint_jump *) tpoint->handle;
2459 1.1 christos
2460 1.1 christos delete_fast_tracepoint_jump (jump);
2461 1.1 christos }
2462 1.1 christos break;
2463 1.1 christos case static_tracepoint:
2464 1.1 christos if (prev_stpoint != NULL
2465 1.1 christos && prev_stpoint->address == tpoint->address)
2466 1.1 christos /* Nothing to do. We already unprobed a tracepoint set at
2467 1.1 christos this marker address (and there can only be one probe
2468 1.1 christos per marker). */
2469 1.1 christos ;
2470 1.1 christos else
2471 1.1 christos {
2472 1.1 christos unprobe_marker_at (tpoint->address);
2473 1.1 christos prev_stpoint = tpoint;
2474 1.1 christos }
2475 1.1 christos break;
2476 1.1 christos }
2477 1.1 christos
2478 1.1 christos tpoint->handle = NULL;
2479 1.1 christos }
2480 1.1 christos
2481 1.1 christos target_unpause_all (true);
2482 1.1 christos }
2483 1.1 christos
2484 1.1 christos /* Parse a packet that defines a tracepoint. */
2485 1.1 christos
2486 1.1 christos static void
2487 1.1 christos cmd_qtdp (char *own_buf)
2488 1.1 christos {
2489 1.1 christos int tppacket;
2490 1.1 christos /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2491 1.1 christos int trail_hyphen = 0;
2492 1.1 christos ULONGEST num;
2493 1.1 christos ULONGEST addr;
2494 1.1 christos ULONGEST count;
2495 1.1 christos struct tracepoint *tpoint;
2496 1.1 christos const char *packet = own_buf;
2497 1.1 christos
2498 1.1 christos packet += strlen ("QTDP:");
2499 1.1 christos
2500 1.1 christos /* A hyphen at the beginning marks a packet specifying actions for a
2501 1.1 christos tracepoint already supplied. */
2502 1.1 christos tppacket = 1;
2503 1.1 christos if (*packet == '-')
2504 1.1 christos {
2505 1.1 christos tppacket = 0;
2506 1.1 christos ++packet;
2507 1.1 christos }
2508 1.1 christos packet = unpack_varlen_hex (packet, &num);
2509 1.1 christos ++packet; /* skip a colon */
2510 1.1 christos packet = unpack_varlen_hex (packet, &addr);
2511 1.1 christos ++packet; /* skip a colon */
2512 1.1 christos
2513 1.1 christos /* See if we already have this tracepoint. */
2514 1.1 christos tpoint = find_tracepoint (num, addr);
2515 1.1 christos
2516 1.1 christos if (tppacket)
2517 1.1 christos {
2518 1.1 christos /* Duplicate tracepoints are never allowed. */
2519 1.1 christos if (tpoint)
2520 1.1 christos {
2521 1.1 christos trace_debug ("Tracepoint error: tracepoint %d"
2522 1.1 christos " at 0x%s already exists",
2523 1.1 christos (int) num, paddress (addr));
2524 1.1 christos write_enn (own_buf);
2525 1.1 christos return;
2526 1.1 christos }
2527 1.1 christos
2528 1.1 christos tpoint = add_tracepoint (num, addr);
2529 1.1 christos
2530 1.1 christos tpoint->enabled = (*packet == 'E');
2531 1.1 christos ++packet; /* skip 'E' */
2532 1.1 christos ++packet; /* skip a colon */
2533 1.1 christos packet = unpack_varlen_hex (packet, &count);
2534 1.1 christos tpoint->step_count = count;
2535 1.1 christos ++packet; /* skip a colon */
2536 1.1 christos packet = unpack_varlen_hex (packet, &count);
2537 1.1 christos tpoint->pass_count = count;
2538 1.1 christos /* See if we have any of the additional optional fields. */
2539 1.1 christos while (*packet == ':')
2540 1.1 christos {
2541 1.1 christos ++packet;
2542 1.1 christos if (*packet == 'F')
2543 1.1 christos {
2544 1.1 christos tpoint->type = fast_tracepoint;
2545 1.1 christos ++packet;
2546 1.1 christos packet = unpack_varlen_hex (packet, &count);
2547 1.1 christos tpoint->orig_size = count;
2548 1.1 christos }
2549 1.1 christos else if (*packet == 'S')
2550 1.1 christos {
2551 1.1 christos tpoint->type = static_tracepoint;
2552 1.1 christos ++packet;
2553 1.1 christos }
2554 1.1 christos else if (*packet == 'X')
2555 1.1 christos {
2556 1.1 christos tpoint->cond = gdb_parse_agent_expr (&packet);
2557 1.1 christos }
2558 1.1 christos else if (*packet == '-')
2559 1.1 christos break;
2560 1.1 christos else if (*packet == '\0')
2561 1.1 christos break;
2562 1.1 christos else
2563 1.1 christos trace_debug ("Unknown optional tracepoint field");
2564 1.1 christos }
2565 1.1 christos if (*packet == '-')
2566 1.1 christos {
2567 1.1 christos trail_hyphen = 1;
2568 1.1 christos trace_debug ("Also has actions\n");
2569 1.1 christos }
2570 1.1 christos
2571 1.1 christos trace_debug ("Defined %stracepoint %d at 0x%s, "
2572 1.1 christos "enabled %d step %" PRIu64 " pass %" PRIu64,
2573 1.1 christos tpoint->type == fast_tracepoint ? "fast "
2574 1.1 christos : tpoint->type == static_tracepoint ? "static " : "",
2575 1.1 christos tpoint->number, paddress (tpoint->address), tpoint->enabled,
2576 1.1 christos tpoint->step_count, tpoint->pass_count);
2577 1.1 christos }
2578 1.1 christos else if (tpoint)
2579 1.1 christos add_tracepoint_action (tpoint, packet);
2580 1.1 christos else
2581 1.1 christos {
2582 1.1 christos trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2583 1.1 christos (int) num, paddress (addr));
2584 1.1 christos write_enn (own_buf);
2585 1.1 christos return;
2586 1.1 christos }
2587 1.1 christos
2588 1.1 christos /* Install tracepoint during tracing only once for each tracepoint location.
2589 1.1 christos For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2590 1.1 christos determine the last QTDP packet for one tracepoint location by checking
2591 1.1 christos trailing hyphen in QTDP packet. */
2592 1.1 christos if (tracing && !trail_hyphen)
2593 1.1 christos {
2594 1.1 christos struct tracepoint *tp = NULL;
2595 1.1 christos
2596 1.1 christos /* Pause all threads temporarily while we patch tracepoints. */
2597 1.1 christos target_pause_all (false);
2598 1.1 christos
2599 1.1 christos /* download_tracepoint will update global `tracepoints'
2600 1.1 christos list, so it is unsafe to leave threads in jump pad. */
2601 1.1 christos target_stabilize_threads ();
2602 1.1 christos
2603 1.1 christos /* Freeze threads. */
2604 1.1 christos target_pause_all (true);
2605 1.1 christos
2606 1.1 christos
2607 1.1 christos if (tpoint->type != trap_tracepoint)
2608 1.1 christos {
2609 1.1 christos /* Find another fast or static tracepoint at the same address. */
2610 1.1 christos for (tp = tracepoints; tp; tp = tp->next)
2611 1.1 christos {
2612 1.1 christos if (tp->address == tpoint->address && tp->type == tpoint->type
2613 1.1 christos && tp->number != tpoint->number)
2614 1.1 christos break;
2615 1.1 christos }
2616 1.1 christos
2617 1.1 christos /* TPOINT is installed at the same address as TP. */
2618 1.1 christos if (tp)
2619 1.1 christos {
2620 1.1 christos if (tpoint->type == fast_tracepoint)
2621 1.1 christos clone_fast_tracepoint (tpoint, tp);
2622 1.1 christos else if (tpoint->type == static_tracepoint)
2623 1.1 christos tpoint->handle = (void *) -1;
2624 1.1 christos }
2625 1.1 christos }
2626 1.1 christos
2627 1.1 christos if (use_agent && tpoint->type == fast_tracepoint
2628 1.1 christos && agent_capability_check (AGENT_CAPA_FAST_TRACE))
2629 1.1 christos {
2630 1.1 christos /* Download and install fast tracepoint by agent. */
2631 1.1 christos if (tracepoint_send_agent (tpoint) == 0)
2632 1.1 christos write_ok (own_buf);
2633 1.1 christos else
2634 1.1 christos {
2635 1.1 christos write_enn (own_buf);
2636 1.1 christos remove_tracepoint (tpoint);
2637 1.1 christos }
2638 1.1 christos }
2639 1.1 christos else
2640 1.1 christos {
2641 1.1 christos download_tracepoint (tpoint);
2642 1.1 christos
2643 1.1 christos if (tpoint->type == trap_tracepoint || tp == NULL)
2644 1.1 christos {
2645 1.1 christos install_tracepoint (tpoint, own_buf);
2646 1.1 christos if (strcmp (own_buf, "OK") != 0)
2647 1.1 christos remove_tracepoint (tpoint);
2648 1.1 christos }
2649 1.1 christos else
2650 1.1 christos write_ok (own_buf);
2651 1.1 christos }
2652 1.1 christos
2653 1.1 christos target_unpause_all (true);
2654 1.1 christos return;
2655 1.1 christos }
2656 1.1 christos
2657 1.1 christos write_ok (own_buf);
2658 1.1 christos }
2659 1.1 christos
2660 1.1 christos static void
2661 1.1 christos cmd_qtdpsrc (char *own_buf)
2662 1.1 christos {
2663 1.1 christos ULONGEST num, addr, start, slen;
2664 1.1 christos struct tracepoint *tpoint;
2665 1.1 christos const char *packet = own_buf;
2666 1.1 christos const char *saved;
2667 1.1 christos char *srctype, *src;
2668 1.1 christos size_t nbytes;
2669 1.1 christos struct source_string *last, *newlast;
2670 1.1 christos
2671 1.1 christos packet += strlen ("QTDPsrc:");
2672 1.1 christos
2673 1.1 christos packet = unpack_varlen_hex (packet, &num);
2674 1.1 christos ++packet; /* skip a colon */
2675 1.1 christos packet = unpack_varlen_hex (packet, &addr);
2676 1.1 christos ++packet; /* skip a colon */
2677 1.1 christos
2678 1.1 christos /* See if we already have this tracepoint. */
2679 1.1 christos tpoint = find_tracepoint (num, addr);
2680 1.1 christos
2681 1.1 christos if (!tpoint)
2682 1.1 christos {
2683 1.1 christos trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2684 1.1 christos (int) num, paddress (addr));
2685 1.1 christos write_enn (own_buf);
2686 1.1 christos return;
2687 1.1 christos }
2688 1.1 christos
2689 1.1 christos saved = packet;
2690 1.1 christos packet = strchr (packet, ':');
2691 1.1 christos srctype = (char *) xmalloc (packet - saved + 1);
2692 1.1 christos memcpy (srctype, saved, packet - saved);
2693 1.1 christos srctype[packet - saved] = '\0';
2694 1.1 christos ++packet;
2695 1.1 christos packet = unpack_varlen_hex (packet, &start);
2696 1.1 christos ++packet; /* skip a colon */
2697 1.1 christos packet = unpack_varlen_hex (packet, &slen);
2698 1.1 christos ++packet; /* skip a colon */
2699 1.1 christos src = (char *) xmalloc (slen + 1);
2700 1.1 christos nbytes = hex2bin (packet, (gdb_byte *) src, strlen (packet) / 2);
2701 1.1 christos src[nbytes] = '\0';
2702 1.1 christos
2703 1.1 christos newlast = XNEW (struct source_string);
2704 1.1 christos newlast->type = srctype;
2705 1.1 christos newlast->str = src;
2706 1.1 christos newlast->next = NULL;
2707 1.1 christos /* Always add a source string to the end of the list;
2708 1.1 christos this keeps sequences of actions/commands in the right
2709 1.1 christos order. */
2710 1.1 christos if (tpoint->source_strings)
2711 1.1 christos {
2712 1.1 christos for (last = tpoint->source_strings; last->next; last = last->next)
2713 1.1 christos ;
2714 1.1 christos last->next = newlast;
2715 1.1 christos }
2716 1.1 christos else
2717 1.1 christos tpoint->source_strings = newlast;
2718 1.1 christos
2719 1.1 christos write_ok (own_buf);
2720 1.1 christos }
2721 1.1 christos
2722 1.1 christos static void
2723 1.1 christos cmd_qtdv (char *own_buf)
2724 1.1 christos {
2725 1.1 christos ULONGEST num, val, builtin;
2726 1.1 christos char *varname;
2727 1.1 christos size_t nbytes;
2728 1.1 christos struct trace_state_variable *tsv;
2729 1.1 christos const char *packet = own_buf;
2730 1.1 christos
2731 1.1 christos packet += strlen ("QTDV:");
2732 1.1 christos
2733 1.1 christos packet = unpack_varlen_hex (packet, &num);
2734 1.1 christos ++packet; /* skip a colon */
2735 1.1 christos packet = unpack_varlen_hex (packet, &val);
2736 1.1 christos ++packet; /* skip a colon */
2737 1.1 christos packet = unpack_varlen_hex (packet, &builtin);
2738 1.1 christos ++packet; /* skip a colon */
2739 1.1 christos
2740 1.1 christos nbytes = strlen (packet) / 2;
2741 1.1 christos varname = (char *) xmalloc (nbytes + 1);
2742 1.1 christos nbytes = hex2bin (packet, (gdb_byte *) varname, nbytes);
2743 1.1 christos varname[nbytes] = '\0';
2744 1.1 christos
2745 1.1 christos tsv = create_trace_state_variable (num, 1);
2746 1.1 christos tsv->initial_value = (LONGEST) val;
2747 1.1 christos tsv->name = varname;
2748 1.1 christos
2749 1.1 christos set_trace_state_variable_value (num, (LONGEST) val);
2750 1.1 christos
2751 1.1 christos write_ok (own_buf);
2752 1.1 christos }
2753 1.1 christos
2754 1.1 christos static void
2755 1.1 christos cmd_qtenable_disable (char *own_buf, int enable)
2756 1.1 christos {
2757 1.1 christos const char *packet = own_buf;
2758 1.1 christos ULONGEST num, addr;
2759 1.1 christos struct tracepoint *tp;
2760 1.1 christos
2761 1.1 christos packet += strlen (enable ? "QTEnable:" : "QTDisable:");
2762 1.1 christos packet = unpack_varlen_hex (packet, &num);
2763 1.1 christos ++packet; /* skip a colon */
2764 1.1 christos packet = unpack_varlen_hex (packet, &addr);
2765 1.1 christos
2766 1.1 christos tp = find_tracepoint (num, addr);
2767 1.1 christos
2768 1.1 christos if (tp)
2769 1.1 christos {
2770 1.1 christos if ((enable && tp->enabled) || (!enable && !tp->enabled))
2771 1.1 christos {
2772 1.1 christos trace_debug ("Tracepoint %d at 0x%s is already %s",
2773 1.1 christos (int) num, paddress (addr),
2774 1.1 christos enable ? "enabled" : "disabled");
2775 1.1 christos write_ok (own_buf);
2776 1.1 christos return;
2777 1.1 christos }
2778 1.1 christos
2779 1.1 christos trace_debug ("%s tracepoint %d at 0x%s",
2780 1.1 christos enable ? "Enabling" : "Disabling",
2781 1.1 christos (int) num, paddress (addr));
2782 1.1 christos
2783 1.1 christos tp->enabled = enable;
2784 1.1 christos
2785 1.1 christos if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
2786 1.1 christos {
2787 1.1 christos int offset = offsetof (struct tracepoint, enabled);
2788 1.1 christos CORE_ADDR obj_addr = tp->obj_addr_on_target + offset;
2789 1.1 christos
2790 1.1.1.2 christos int ret = write_inferior_int8 (obj_addr, enable);
2791 1.1 christos if (ret)
2792 1.1 christos {
2793 1.1 christos trace_debug ("Cannot write enabled flag into "
2794 1.1 christos "inferior process memory");
2795 1.1 christos write_enn (own_buf);
2796 1.1 christos return;
2797 1.1 christos }
2798 1.1 christos }
2799 1.1 christos
2800 1.1 christos write_ok (own_buf);
2801 1.1 christos }
2802 1.1 christos else
2803 1.1 christos {
2804 1.1 christos trace_debug ("Tracepoint %d at 0x%s not found",
2805 1.1 christos (int) num, paddress (addr));
2806 1.1 christos write_enn (own_buf);
2807 1.1 christos }
2808 1.1 christos }
2809 1.1 christos
2810 1.1 christos static void
2811 1.1 christos cmd_qtv (char *own_buf)
2812 1.1 christos {
2813 1.1 christos client_state &cs = get_client_state ();
2814 1.1 christos ULONGEST num;
2815 1.1 christos LONGEST val = 0;
2816 1.1 christos int err;
2817 1.1 christos char *packet = own_buf;
2818 1.1 christos
2819 1.1 christos packet += strlen ("qTV:");
2820 1.1 christos unpack_varlen_hex (packet, &num);
2821 1.1 christos
2822 1.1 christos if (cs.current_traceframe >= 0)
2823 1.1 christos {
2824 1.1 christos err = traceframe_read_tsv ((int) num, &val);
2825 1.1 christos if (err)
2826 1.1 christos {
2827 1.1 christos strcpy (own_buf, "U");
2828 1.1 christos return;
2829 1.1 christos }
2830 1.1 christos }
2831 1.1 christos /* Only make tsv's be undefined before the first trace run. After a
2832 1.1 christos trace run is over, the user might want to see the last value of
2833 1.1 christos the tsv, and it might not be available in a traceframe. */
2834 1.1 christos else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0)
2835 1.1 christos {
2836 1.1 christos strcpy (own_buf, "U");
2837 1.1 christos return;
2838 1.1 christos }
2839 1.1 christos else
2840 1.1 christos val = get_trace_state_variable_value (num);
2841 1.1 christos
2842 1.1 christos sprintf (own_buf, "V%s", phex_nz (val, 0));
2843 1.1 christos }
2844 1.1 christos
2845 1.1 christos /* Clear out the list of readonly regions. */
2846 1.1 christos
2847 1.1 christos static void
2848 1.1 christos clear_readonly_regions (void)
2849 1.1 christos {
2850 1.1 christos struct readonly_region *roreg;
2851 1.1 christos
2852 1.1 christos while (readonly_regions)
2853 1.1 christos {
2854 1.1 christos roreg = readonly_regions;
2855 1.1 christos readonly_regions = readonly_regions->next;
2856 1.1 christos free (roreg);
2857 1.1 christos }
2858 1.1 christos }
2859 1.1 christos
2860 1.1 christos /* Parse the collection of address ranges whose contents GDB believes
2861 1.1 christos to be unchanging and so can be read directly from target memory
2862 1.1 christos even while looking at a traceframe. */
2863 1.1 christos
2864 1.1 christos static void
2865 1.1 christos cmd_qtro (char *own_buf)
2866 1.1 christos {
2867 1.1 christos ULONGEST start, end;
2868 1.1 christos struct readonly_region *roreg;
2869 1.1 christos const char *packet = own_buf;
2870 1.1 christos
2871 1.1 christos trace_debug ("Want to mark readonly regions");
2872 1.1 christos
2873 1.1 christos clear_readonly_regions ();
2874 1.1 christos
2875 1.1 christos packet += strlen ("QTro");
2876 1.1 christos
2877 1.1 christos while (*packet == ':')
2878 1.1 christos {
2879 1.1 christos ++packet; /* skip a colon */
2880 1.1 christos packet = unpack_varlen_hex (packet, &start);
2881 1.1 christos ++packet; /* skip a comma */
2882 1.1 christos packet = unpack_varlen_hex (packet, &end);
2883 1.1 christos
2884 1.1 christos roreg = XNEW (struct readonly_region);
2885 1.1 christos roreg->start = start;
2886 1.1 christos roreg->end = end;
2887 1.1 christos roreg->next = readonly_regions;
2888 1.1 christos readonly_regions = roreg;
2889 1.1 christos trace_debug ("Added readonly region from 0x%s to 0x%s",
2890 1.1 christos paddress (roreg->start), paddress (roreg->end));
2891 1.1 christos }
2892 1.1 christos
2893 1.1 christos write_ok (own_buf);
2894 1.1 christos }
2895 1.1 christos
2896 1.1 christos /* Test to see if the given range is in our list of readonly ranges.
2897 1.1 christos We only test for being entirely within a range, GDB is not going to
2898 1.1 christos send a single memory packet that spans multiple regions. */
2899 1.1 christos
2900 1.1 christos int
2901 1.1 christos in_readonly_region (CORE_ADDR addr, ULONGEST length)
2902 1.1 christos {
2903 1.1 christos struct readonly_region *roreg;
2904 1.1 christos
2905 1.1 christos for (roreg = readonly_regions; roreg; roreg = roreg->next)
2906 1.1 christos if (roreg->start <= addr && (addr + length - 1) <= roreg->end)
2907 1.1 christos return 1;
2908 1.1 christos
2909 1.1 christos return 0;
2910 1.1 christos }
2911 1.1 christos
2912 1.1 christos static CORE_ADDR gdb_jump_pad_head;
2913 1.1 christos
2914 1.1 christos /* Return the address of the next free jump space. */
2915 1.1 christos
2916 1.1 christos static CORE_ADDR
2917 1.1 christos get_jump_space_head (void)
2918 1.1 christos {
2919 1.1 christos if (gdb_jump_pad_head == 0)
2920 1.1 christos {
2921 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
2922 1.1 christos &gdb_jump_pad_head))
2923 1.1 christos {
2924 1.1.1.2 christos internal_error ("error extracting jump_pad_buffer");
2925 1.1 christos }
2926 1.1 christos }
2927 1.1 christos
2928 1.1 christos return gdb_jump_pad_head;
2929 1.1 christos }
2930 1.1 christos
2931 1.1 christos /* Reserve USED bytes from the jump space. */
2932 1.1 christos
2933 1.1 christos static void
2934 1.1 christos claim_jump_space (ULONGEST used)
2935 1.1 christos {
2936 1.1 christos trace_debug ("claim_jump_space reserves %s bytes at %s",
2937 1.1 christos pulongest (used), paddress (gdb_jump_pad_head));
2938 1.1 christos gdb_jump_pad_head += used;
2939 1.1 christos }
2940 1.1 christos
2941 1.1 christos static CORE_ADDR trampoline_buffer_head = 0;
2942 1.1 christos static CORE_ADDR trampoline_buffer_tail;
2943 1.1 christos
2944 1.1 christos /* Reserve USED bytes from the trampoline buffer and return the
2945 1.1 christos address of the start of the reserved space in TRAMPOLINE. Returns
2946 1.1 christos non-zero if the space is successfully claimed. */
2947 1.1 christos
2948 1.1 christos int
2949 1.1 christos claim_trampoline_space (ULONGEST used, CORE_ADDR *trampoline)
2950 1.1 christos {
2951 1.1 christos if (!trampoline_buffer_head)
2952 1.1 christos {
2953 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
2954 1.1 christos &trampoline_buffer_tail))
2955 1.1 christos {
2956 1.1.1.2 christos internal_error ("error extracting trampoline_buffer");
2957 1.1 christos }
2958 1.1 christos
2959 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2960 1.1 christos &trampoline_buffer_head))
2961 1.1 christos {
2962 1.1.1.2 christos internal_error ("error extracting trampoline_buffer_end");
2963 1.1 christos }
2964 1.1 christos }
2965 1.1 christos
2966 1.1 christos /* Start claiming space from the top of the trampoline space. If
2967 1.1 christos the space is located at the bottom of the virtual address space,
2968 1.1 christos this reduces the possibility that corruption will occur if a null
2969 1.1 christos pointer is used to write to memory. */
2970 1.1 christos if (trampoline_buffer_head - trampoline_buffer_tail < used)
2971 1.1 christos {
2972 1.1 christos trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2973 1.1 christos pulongest (used));
2974 1.1 christos return 0;
2975 1.1 christos }
2976 1.1 christos
2977 1.1 christos trampoline_buffer_head -= used;
2978 1.1 christos
2979 1.1 christos trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2980 1.1 christos pulongest (used), paddress (trampoline_buffer_head));
2981 1.1 christos
2982 1.1 christos *trampoline = trampoline_buffer_head;
2983 1.1 christos return 1;
2984 1.1 christos }
2985 1.1 christos
2986 1.1 christos /* Returns non-zero if there is space allocated for use in trampolines
2987 1.1 christos for fast tracepoints. */
2988 1.1 christos
2989 1.1 christos int
2990 1.1 christos have_fast_tracepoint_trampoline_buffer (char *buf)
2991 1.1 christos {
2992 1.1 christos CORE_ADDR trampoline_end, errbuf;
2993 1.1 christos
2994 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2995 1.1 christos &trampoline_end))
2996 1.1 christos {
2997 1.1.1.2 christos internal_error ("error extracting trampoline_buffer_end");
2998 1.1 christos }
2999 1.1 christos
3000 1.1 christos if (buf)
3001 1.1 christos {
3002 1.1 christos buf[0] = '\0';
3003 1.1 christos strcpy (buf, "was claiming");
3004 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_error,
3005 1.1 christos &errbuf))
3006 1.1 christos {
3007 1.1.1.2 christos internal_error ("error extracting errbuf");
3008 1.1 christos }
3009 1.1 christos
3010 1.1 christos read_inferior_memory (errbuf, (unsigned char *) buf, 100);
3011 1.1 christos }
3012 1.1 christos
3013 1.1 christos return trampoline_end != 0;
3014 1.1 christos }
3015 1.1 christos
3016 1.1 christos /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3017 1.1 christos the command fails, or 0 otherwise. If the command ran
3018 1.1 christos successfully, but probing the marker failed, ERROUT will be filled
3019 1.1 christos with the error to reply to GDB, and -1 is also returned. This
3020 1.1 christos allows directly passing IPA errors to GDB. */
3021 1.1 christos
3022 1.1 christos static int
3023 1.1 christos probe_marker_at (CORE_ADDR address, char *errout)
3024 1.1 christos {
3025 1.1 christos char cmd[IPA_CMD_BUF_SIZE];
3026 1.1 christos int err;
3027 1.1 christos
3028 1.1 christos sprintf (cmd, "probe_marker_at:%s", paddress (address));
3029 1.1 christos err = run_inferior_command (cmd, strlen (cmd) + 1);
3030 1.1 christos
3031 1.1 christos if (err == 0)
3032 1.1 christos {
3033 1.1 christos if (*cmd == 'E')
3034 1.1 christos {
3035 1.1 christos strcpy (errout, cmd);
3036 1.1 christos return -1;
3037 1.1 christos }
3038 1.1 christos }
3039 1.1 christos
3040 1.1 christos return err;
3041 1.1 christos }
3042 1.1 christos
3043 1.1 christos static void
3044 1.1 christos clone_fast_tracepoint (struct tracepoint *to, const struct tracepoint *from)
3045 1.1 christos {
3046 1.1 christos to->jump_pad = from->jump_pad;
3047 1.1 christos to->jump_pad_end = from->jump_pad_end;
3048 1.1 christos to->trampoline = from->trampoline;
3049 1.1 christos to->trampoline_end = from->trampoline_end;
3050 1.1 christos to->adjusted_insn_addr = from->adjusted_insn_addr;
3051 1.1 christos to->adjusted_insn_addr_end = from->adjusted_insn_addr_end;
3052 1.1 christos to->handle = from->handle;
3053 1.1 christos
3054 1.1 christos gdb_assert (from->handle);
3055 1.1 christos inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump *) from->handle);
3056 1.1 christos }
3057 1.1 christos
3058 1.1 christos #define MAX_JUMP_SIZE 20
3059 1.1 christos
3060 1.1 christos /* Install fast tracepoint. Return 0 if successful, otherwise return
3061 1.1 christos non-zero. */
3062 1.1 christos
3063 1.1 christos static int
3064 1.1 christos install_fast_tracepoint (struct tracepoint *tpoint, char *errbuf)
3065 1.1 christos {
3066 1.1 christos CORE_ADDR jentry, jump_entry;
3067 1.1 christos CORE_ADDR trampoline;
3068 1.1 christos CORE_ADDR collect;
3069 1.1 christos ULONGEST trampoline_size;
3070 1.1 christos int err = 0;
3071 1.1 christos /* The jump to the jump pad of the last fast tracepoint
3072 1.1 christos installed. */
3073 1.1 christos unsigned char fjump[MAX_JUMP_SIZE];
3074 1.1 christos ULONGEST fjump_size;
3075 1.1 christos
3076 1.1 christos if (tpoint->orig_size < target_get_min_fast_tracepoint_insn_len ())
3077 1.1 christos {
3078 1.1 christos trace_debug ("Requested a fast tracepoint on an instruction "
3079 1.1 christos "that is of less than the minimum length.");
3080 1.1 christos return 0;
3081 1.1 christos }
3082 1.1 christos
3083 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_collect_ptr,
3084 1.1 christos &collect))
3085 1.1 christos {
3086 1.1 christos error ("error extracting gdb_collect_ptr");
3087 1.1 christos return 1;
3088 1.1 christos }
3089 1.1 christos
3090 1.1 christos jentry = jump_entry = get_jump_space_head ();
3091 1.1 christos
3092 1.1 christos trampoline = 0;
3093 1.1 christos trampoline_size = 0;
3094 1.1 christos
3095 1.1 christos /* Install the jump pad. */
3096 1.1 christos err = target_install_fast_tracepoint_jump_pad
3097 1.1 christos (tpoint->obj_addr_on_target, tpoint->address, collect,
3098 1.1 christos ipa_sym_addrs.addr_collecting, tpoint->orig_size, &jentry,
3099 1.1 christos &trampoline, &trampoline_size, fjump, &fjump_size,
3100 1.1 christos &tpoint->adjusted_insn_addr, &tpoint->adjusted_insn_addr_end, errbuf);
3101 1.1 christos
3102 1.1 christos if (err)
3103 1.1 christos return 1;
3104 1.1 christos
3105 1.1 christos /* Wire it in. */
3106 1.1 christos tpoint->handle = set_fast_tracepoint_jump (tpoint->address, fjump,
3107 1.1 christos fjump_size);
3108 1.1 christos
3109 1.1 christos if (tpoint->handle != NULL)
3110 1.1 christos {
3111 1.1 christos tpoint->jump_pad = jump_entry;
3112 1.1 christos tpoint->jump_pad_end = jentry;
3113 1.1 christos tpoint->trampoline = trampoline;
3114 1.1 christos tpoint->trampoline_end = trampoline + trampoline_size;
3115 1.1 christos
3116 1.1 christos /* Pad to 8-byte alignment. */
3117 1.1 christos jentry = ((jentry + 7) & ~0x7);
3118 1.1 christos claim_jump_space (jentry - jump_entry);
3119 1.1 christos }
3120 1.1 christos
3121 1.1 christos return 0;
3122 1.1 christos }
3123 1.1 christos
3124 1.1 christos
3125 1.1 christos /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3126 1.1 christos
3127 1.1 christos static void
3128 1.1 christos install_tracepoint (struct tracepoint *tpoint, char *own_buf)
3129 1.1 christos {
3130 1.1 christos tpoint->handle = NULL;
3131 1.1 christos *own_buf = '\0';
3132 1.1 christos
3133 1.1 christos if (tpoint->type == trap_tracepoint)
3134 1.1 christos {
3135 1.1 christos /* Tracepoints are installed as memory breakpoints. Just go
3136 1.1 christos ahead and install the trap. The breakpoints module
3137 1.1 christos handles duplicated breakpoints, and the memory read
3138 1.1 christos routine handles un-patching traps from memory reads. */
3139 1.1 christos tpoint->handle = set_breakpoint_at (tpoint->address,
3140 1.1 christos tracepoint_handler);
3141 1.1 christos }
3142 1.1 christos else if (tpoint->type == fast_tracepoint || tpoint->type == static_tracepoint)
3143 1.1 christos {
3144 1.1 christos if (!agent_loaded_p ())
3145 1.1 christos {
3146 1.1 christos trace_debug ("Requested a %s tracepoint, but fast "
3147 1.1 christos "tracepoints aren't supported.",
3148 1.1 christos tpoint->type == static_tracepoint ? "static" : "fast");
3149 1.1 christos write_e_ipa_not_loaded (own_buf);
3150 1.1 christos return;
3151 1.1 christos }
3152 1.1 christos if (tpoint->type == static_tracepoint
3153 1.1 christos && !in_process_agent_supports_ust ())
3154 1.1 christos {
3155 1.1 christos trace_debug ("Requested a static tracepoint, but static "
3156 1.1 christos "tracepoints are not supported.");
3157 1.1 christos write_e_ust_not_loaded (own_buf);
3158 1.1 christos return;
3159 1.1 christos }
3160 1.1 christos
3161 1.1 christos if (tpoint->type == fast_tracepoint)
3162 1.1 christos install_fast_tracepoint (tpoint, own_buf);
3163 1.1 christos else
3164 1.1 christos {
3165 1.1 christos if (probe_marker_at (tpoint->address, own_buf) == 0)
3166 1.1 christos tpoint->handle = (void *) -1;
3167 1.1 christos }
3168 1.1 christos
3169 1.1 christos }
3170 1.1 christos else
3171 1.1.1.2 christos internal_error ("Unknown tracepoint type");
3172 1.1 christos
3173 1.1 christos if (tpoint->handle == NULL)
3174 1.1 christos {
3175 1.1 christos if (*own_buf == '\0')
3176 1.1 christos write_enn (own_buf);
3177 1.1 christos }
3178 1.1 christos else
3179 1.1 christos write_ok (own_buf);
3180 1.1 christos }
3181 1.1 christos
3182 1.1 christos static void download_tracepoint_1 (struct tracepoint *tpoint);
3183 1.1 christos
3184 1.1 christos static void
3185 1.1 christos cmd_qtstart (char *packet)
3186 1.1 christos {
3187 1.1 christos struct tracepoint *tpoint, *prev_ftpoint, *prev_stpoint;
3188 1.1 christos CORE_ADDR tpptr = 0, prev_tpptr = 0;
3189 1.1 christos
3190 1.1 christos trace_debug ("Starting the trace");
3191 1.1 christos
3192 1.1 christos /* Pause all threads temporarily while we patch tracepoints. */
3193 1.1 christos target_pause_all (false);
3194 1.1 christos
3195 1.1 christos /* Get threads out of jump pads. Safe to do here, since this is a
3196 1.1 christos top level command. And, required to do here, since we're
3197 1.1 christos deleting/rewriting jump pads. */
3198 1.1 christos
3199 1.1 christos target_stabilize_threads ();
3200 1.1 christos
3201 1.1 christos /* Freeze threads. */
3202 1.1 christos target_pause_all (true);
3203 1.1 christos
3204 1.1 christos /* Sync the fast tracepoints list in the inferior ftlib. */
3205 1.1 christos if (agent_loaded_p ())
3206 1.1 christos download_trace_state_variables ();
3207 1.1 christos
3208 1.1 christos /* No previous fast tpoint yet. */
3209 1.1 christos prev_ftpoint = NULL;
3210 1.1 christos
3211 1.1 christos /* No previous static tpoint yet. */
3212 1.1 christos prev_stpoint = NULL;
3213 1.1 christos
3214 1.1 christos *packet = '\0';
3215 1.1 christos
3216 1.1 christos if (agent_loaded_p ())
3217 1.1 christos {
3218 1.1 christos /* Tell IPA about the correct tdesc. */
3219 1.1 christos if (write_inferior_integer (ipa_sym_addrs.addr_ipa_tdesc_idx,
3220 1.1 christos target_get_ipa_tdesc_idx ()))
3221 1.1.1.2 christos error ("Error setting ipa_tdesc_idx variable in lib");
3222 1.1 christos }
3223 1.1 christos
3224 1.1 christos /* Start out empty. */
3225 1.1 christos if (agent_loaded_p ())
3226 1.1 christos write_inferior_data_pointer (ipa_sym_addrs.addr_tracepoints, 0);
3227 1.1 christos
3228 1.1 christos /* Download and install tracepoints. */
3229 1.1 christos for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
3230 1.1 christos {
3231 1.1 christos /* Ensure all the hit counts start at zero. */
3232 1.1 christos tpoint->hit_count = 0;
3233 1.1 christos tpoint->traceframe_usage = 0;
3234 1.1 christos
3235 1.1 christos if (tpoint->type == trap_tracepoint)
3236 1.1 christos {
3237 1.1 christos /* Tracepoints are installed as memory breakpoints. Just go
3238 1.1 christos ahead and install the trap. The breakpoints module
3239 1.1 christos handles duplicated breakpoints, and the memory read
3240 1.1 christos routine handles un-patching traps from memory reads. */
3241 1.1 christos tpoint->handle = set_breakpoint_at (tpoint->address,
3242 1.1 christos tracepoint_handler);
3243 1.1 christos }
3244 1.1 christos else if (tpoint->type == fast_tracepoint
3245 1.1 christos || tpoint->type == static_tracepoint)
3246 1.1 christos {
3247 1.1 christos if (maybe_write_ipa_not_loaded (packet))
3248 1.1 christos {
3249 1.1 christos trace_debug ("Requested a %s tracepoint, but fast "
3250 1.1 christos "tracepoints aren't supported.",
3251 1.1 christos tpoint->type == static_tracepoint
3252 1.1 christos ? "static" : "fast");
3253 1.1 christos break;
3254 1.1 christos }
3255 1.1 christos
3256 1.1 christos if (tpoint->type == fast_tracepoint)
3257 1.1 christos {
3258 1.1 christos int use_agent_p
3259 1.1 christos = use_agent && agent_capability_check (AGENT_CAPA_FAST_TRACE);
3260 1.1 christos
3261 1.1 christos if (prev_ftpoint != NULL
3262 1.1 christos && prev_ftpoint->address == tpoint->address)
3263 1.1 christos {
3264 1.1 christos if (use_agent_p)
3265 1.1 christos tracepoint_send_agent (tpoint);
3266 1.1 christos else
3267 1.1 christos download_tracepoint_1 (tpoint);
3268 1.1 christos
3269 1.1 christos clone_fast_tracepoint (tpoint, prev_ftpoint);
3270 1.1 christos }
3271 1.1 christos else
3272 1.1 christos {
3273 1.1 christos /* Tracepoint is installed successfully? */
3274 1.1 christos int installed = 0;
3275 1.1 christos
3276 1.1 christos /* Download and install fast tracepoint by agent. */
3277 1.1 christos if (use_agent_p)
3278 1.1 christos installed = !tracepoint_send_agent (tpoint);
3279 1.1 christos else
3280 1.1 christos {
3281 1.1 christos download_tracepoint_1 (tpoint);
3282 1.1 christos installed = !install_fast_tracepoint (tpoint, packet);
3283 1.1 christos }
3284 1.1 christos
3285 1.1 christos if (installed)
3286 1.1 christos prev_ftpoint = tpoint;
3287 1.1 christos }
3288 1.1 christos }
3289 1.1 christos else
3290 1.1 christos {
3291 1.1 christos if (!in_process_agent_supports_ust ())
3292 1.1 christos {
3293 1.1 christos trace_debug ("Requested a static tracepoint, but static "
3294 1.1 christos "tracepoints are not supported.");
3295 1.1 christos break;
3296 1.1 christos }
3297 1.1 christos
3298 1.1 christos download_tracepoint_1 (tpoint);
3299 1.1 christos /* Can only probe a given marker once. */
3300 1.1 christos if (prev_stpoint != NULL
3301 1.1 christos && prev_stpoint->address == tpoint->address)
3302 1.1 christos tpoint->handle = (void *) -1;
3303 1.1 christos else
3304 1.1 christos {
3305 1.1 christos if (probe_marker_at (tpoint->address, packet) == 0)
3306 1.1 christos {
3307 1.1 christos tpoint->handle = (void *) -1;
3308 1.1 christos
3309 1.1 christos /* So that we can handle multiple static tracepoints
3310 1.1 christos at the same address easily. */
3311 1.1 christos prev_stpoint = tpoint;
3312 1.1 christos }
3313 1.1 christos }
3314 1.1 christos }
3315 1.1 christos
3316 1.1 christos prev_tpptr = tpptr;
3317 1.1 christos tpptr = tpoint->obj_addr_on_target;
3318 1.1 christos
3319 1.1 christos if (tpoint == tracepoints)
3320 1.1 christos /* First object in list, set the head pointer in the
3321 1.1 christos inferior. */
3322 1.1 christos write_inferior_data_pointer (ipa_sym_addrs.addr_tracepoints, tpptr);
3323 1.1 christos else
3324 1.1 christos write_inferior_data_pointer (prev_tpptr
3325 1.1 christos + offsetof (struct tracepoint, next),
3326 1.1 christos tpptr);
3327 1.1 christos }
3328 1.1 christos
3329 1.1 christos /* Any failure in the inner loop is sufficient cause to give
3330 1.1 christos up. */
3331 1.1 christos if (tpoint->handle == NULL)
3332 1.1 christos break;
3333 1.1 christos }
3334 1.1 christos
3335 1.1 christos /* Any error in tracepoint insertion is unacceptable; better to
3336 1.1 christos address the problem now, than end up with a useless or misleading
3337 1.1 christos trace run. */
3338 1.1 christos if (tpoint != NULL)
3339 1.1 christos {
3340 1.1 christos clear_installed_tracepoints ();
3341 1.1 christos if (*packet == '\0')
3342 1.1 christos write_enn (packet);
3343 1.1 christos target_unpause_all (true);
3344 1.1 christos return;
3345 1.1 christos }
3346 1.1 christos
3347 1.1 christos stopping_tracepoint = NULL;
3348 1.1 christos trace_buffer_is_full = 0;
3349 1.1 christos expr_eval_result = expr_eval_no_error;
3350 1.1 christos error_tracepoint = NULL;
3351 1.1 christos tracing_start_time = get_timestamp ();
3352 1.1 christos
3353 1.1 christos /* Tracing is now active, hits will now start being logged. */
3354 1.1 christos tracing = 1;
3355 1.1 christos
3356 1.1 christos if (agent_loaded_p ())
3357 1.1 christos {
3358 1.1 christos if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 1))
3359 1.1 christos {
3360 1.1.1.2 christos internal_error ("Error setting tracing variable in lib");
3361 1.1 christos }
3362 1.1 christos
3363 1.1 christos if (write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
3364 1.1 christos 0))
3365 1.1 christos {
3366 1.1.1.2 christos internal_error ("Error clearing stopping_tracepoint variable"
3367 1.1 christos " in lib");
3368 1.1 christos }
3369 1.1 christos
3370 1.1 christos if (write_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 0))
3371 1.1 christos {
3372 1.1.1.2 christos internal_error ("Error clearing trace_buffer_is_full variable"
3373 1.1 christos " in lib");
3374 1.1 christos }
3375 1.1 christos
3376 1.1 christos stop_tracing_bkpt = set_breakpoint_at (ipa_sym_addrs.addr_stop_tracing,
3377 1.1 christos stop_tracing_handler);
3378 1.1 christos if (stop_tracing_bkpt == NULL)
3379 1.1 christos error ("Error setting stop_tracing breakpoint");
3380 1.1 christos
3381 1.1 christos flush_trace_buffer_bkpt
3382 1.1 christos = set_breakpoint_at (ipa_sym_addrs.addr_flush_trace_buffer,
3383 1.1 christos flush_trace_buffer_handler);
3384 1.1 christos if (flush_trace_buffer_bkpt == NULL)
3385 1.1 christos error ("Error setting flush_trace_buffer breakpoint");
3386 1.1 christos }
3387 1.1 christos
3388 1.1 christos target_unpause_all (true);
3389 1.1 christos
3390 1.1 christos write_ok (packet);
3391 1.1 christos }
3392 1.1 christos
3393 1.1 christos /* End a tracing run, filling in a stop reason to report back to GDB,
3394 1.1 christos and removing the tracepoints from the code. */
3395 1.1 christos
3396 1.1 christos void
3397 1.1 christos stop_tracing (void)
3398 1.1 christos {
3399 1.1 christos if (!tracing)
3400 1.1 christos {
3401 1.1 christos trace_debug ("Tracing is already off, ignoring");
3402 1.1 christos return;
3403 1.1 christos }
3404 1.1 christos
3405 1.1 christos trace_debug ("Stopping the trace");
3406 1.1 christos
3407 1.1 christos /* Pause all threads before removing fast jumps from memory,
3408 1.1 christos breakpoints, and touching IPA state variables (inferior memory).
3409 1.1 christos Some thread may hit the internal tracing breakpoints, or be
3410 1.1 christos collecting this moment, but that's ok, we don't release the
3411 1.1 christos tpoint object's memory or the jump pads here (we only do that
3412 1.1 christos when we're sure we can move all threads out of the jump pads).
3413 1.1 christos We can't now, since we may be getting here due to the inferior
3414 1.1 christos agent calling us. */
3415 1.1 christos target_pause_all (true);
3416 1.1 christos
3417 1.1 christos /* Stop logging. Tracepoints can still be hit, but they will not be
3418 1.1 christos recorded. */
3419 1.1 christos tracing = 0;
3420 1.1 christos if (agent_loaded_p ())
3421 1.1 christos {
3422 1.1 christos if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 0))
3423 1.1 christos {
3424 1.1.1.2 christos internal_error ("Error clearing tracing variable in lib");
3425 1.1 christos }
3426 1.1 christos }
3427 1.1 christos
3428 1.1 christos tracing_stop_time = get_timestamp ();
3429 1.1 christos tracing_stop_reason = "t???";
3430 1.1 christos tracing_stop_tpnum = 0;
3431 1.1 christos if (stopping_tracepoint)
3432 1.1 christos {
3433 1.1 christos trace_debug ("Stopping the trace because "
3434 1.1 christos "tracepoint %d was hit %" PRIu64 " times",
3435 1.1 christos stopping_tracepoint->number,
3436 1.1 christos stopping_tracepoint->pass_count);
3437 1.1 christos tracing_stop_reason = "tpasscount";
3438 1.1 christos tracing_stop_tpnum = stopping_tracepoint->number;
3439 1.1 christos }
3440 1.1 christos else if (trace_buffer_is_full)
3441 1.1 christos {
3442 1.1 christos trace_debug ("Stopping the trace because the trace buffer is full");
3443 1.1 christos tracing_stop_reason = "tfull";
3444 1.1 christos }
3445 1.1 christos else if (expr_eval_result != expr_eval_no_error)
3446 1.1 christos {
3447 1.1 christos trace_debug ("Stopping the trace because of an expression eval error");
3448 1.1 christos tracing_stop_reason = eval_result_names[expr_eval_result];
3449 1.1 christos tracing_stop_tpnum = error_tracepoint->number;
3450 1.1 christos }
3451 1.1 christos #ifndef IN_PROCESS_AGENT
3452 1.1 christos else if (!gdb_connected ())
3453 1.1 christos {
3454 1.1 christos trace_debug ("Stopping the trace because GDB disconnected");
3455 1.1 christos tracing_stop_reason = "tdisconnected";
3456 1.1 christos }
3457 1.1 christos #endif
3458 1.1 christos else
3459 1.1 christos {
3460 1.1 christos trace_debug ("Stopping the trace because of a tstop command");
3461 1.1 christos tracing_stop_reason = "tstop";
3462 1.1 christos }
3463 1.1 christos
3464 1.1 christos stopping_tracepoint = NULL;
3465 1.1 christos error_tracepoint = NULL;
3466 1.1 christos
3467 1.1 christos /* Clear out the tracepoints. */
3468 1.1 christos clear_installed_tracepoints ();
3469 1.1 christos
3470 1.1 christos if (agent_loaded_p ())
3471 1.1 christos {
3472 1.1 christos /* Pull in fast tracepoint trace frames from the inferior lib
3473 1.1 christos buffer into our buffer, even if our buffer is already full,
3474 1.1 christos because we want to present the full number of created frames
3475 1.1 christos in addition to what fit in the trace buffer. */
3476 1.1 christos upload_fast_traceframes ();
3477 1.1 christos }
3478 1.1 christos
3479 1.1 christos if (stop_tracing_bkpt != NULL)
3480 1.1 christos {
3481 1.1 christos delete_breakpoint (stop_tracing_bkpt);
3482 1.1 christos stop_tracing_bkpt = NULL;
3483 1.1 christos }
3484 1.1 christos
3485 1.1 christos if (flush_trace_buffer_bkpt != NULL)
3486 1.1 christos {
3487 1.1 christos delete_breakpoint (flush_trace_buffer_bkpt);
3488 1.1 christos flush_trace_buffer_bkpt = NULL;
3489 1.1 christos }
3490 1.1 christos
3491 1.1 christos target_unpause_all (true);
3492 1.1 christos }
3493 1.1 christos
3494 1.1 christos static int
3495 1.1 christos stop_tracing_handler (CORE_ADDR addr)
3496 1.1 christos {
3497 1.1 christos trace_debug ("lib hit stop_tracing");
3498 1.1 christos
3499 1.1 christos /* Don't actually handle it here. When we stop tracing we remove
3500 1.1 christos breakpoints from the inferior, and that is not allowed in a
3501 1.1 christos breakpoint handler (as the caller is walking the breakpoint
3502 1.1 christos list). */
3503 1.1 christos return 0;
3504 1.1 christos }
3505 1.1 christos
3506 1.1 christos static int
3507 1.1 christos flush_trace_buffer_handler (CORE_ADDR addr)
3508 1.1 christos {
3509 1.1 christos trace_debug ("lib hit flush_trace_buffer");
3510 1.1 christos return 0;
3511 1.1 christos }
3512 1.1 christos
3513 1.1 christos static void
3514 1.1 christos cmd_qtstop (char *packet)
3515 1.1 christos {
3516 1.1 christos stop_tracing ();
3517 1.1 christos write_ok (packet);
3518 1.1 christos }
3519 1.1 christos
3520 1.1 christos static void
3521 1.1 christos cmd_qtdisconnected (char *own_buf)
3522 1.1 christos {
3523 1.1 christos ULONGEST setting;
3524 1.1 christos char *packet = own_buf;
3525 1.1 christos
3526 1.1 christos packet += strlen ("QTDisconnected:");
3527 1.1 christos
3528 1.1 christos unpack_varlen_hex (packet, &setting);
3529 1.1 christos
3530 1.1 christos write_ok (own_buf);
3531 1.1 christos
3532 1.1 christos disconnected_tracing = setting;
3533 1.1 christos }
3534 1.1 christos
3535 1.1 christos static void
3536 1.1 christos cmd_qtframe (char *own_buf)
3537 1.1 christos {
3538 1.1 christos client_state &cs = get_client_state ();
3539 1.1 christos ULONGEST frame, pc, lo, hi, num;
3540 1.1 christos int tfnum, tpnum;
3541 1.1 christos struct traceframe *tframe;
3542 1.1 christos const char *packet = own_buf;
3543 1.1 christos
3544 1.1 christos packet += strlen ("QTFrame:");
3545 1.1 christos
3546 1.1 christos if (startswith (packet, "pc:"))
3547 1.1 christos {
3548 1.1 christos packet += strlen ("pc:");
3549 1.1 christos unpack_varlen_hex (packet, &pc);
3550 1.1 christos trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc));
3551 1.1 christos tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum);
3552 1.1 christos }
3553 1.1 christos else if (startswith (packet, "range:"))
3554 1.1 christos {
3555 1.1 christos packet += strlen ("range:");
3556 1.1 christos packet = unpack_varlen_hex (packet, &lo);
3557 1.1 christos ++packet;
3558 1.1 christos unpack_varlen_hex (packet, &hi);
3559 1.1 christos trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3560 1.1 christos paddress (lo), paddress (hi));
3561 1.1 christos tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum);
3562 1.1 christos }
3563 1.1 christos else if (startswith (packet, "outside:"))
3564 1.1 christos {
3565 1.1 christos packet += strlen ("outside:");
3566 1.1 christos packet = unpack_varlen_hex (packet, &lo);
3567 1.1 christos ++packet;
3568 1.1 christos unpack_varlen_hex (packet, &hi);
3569 1.1 christos trace_debug ("Want to find next traceframe "
3570 1.1 christos "outside the range 0x%s to 0x%s",
3571 1.1 christos paddress (lo), paddress (hi));
3572 1.1 christos tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum);
3573 1.1 christos }
3574 1.1 christos else if (startswith (packet, "tdp:"))
3575 1.1 christos {
3576 1.1 christos packet += strlen ("tdp:");
3577 1.1 christos unpack_varlen_hex (packet, &num);
3578 1.1 christos tpnum = (int) num;
3579 1.1 christos trace_debug ("Want to find next traceframe for tracepoint %d", tpnum);
3580 1.1 christos tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum);
3581 1.1 christos }
3582 1.1 christos else
3583 1.1 christos {
3584 1.1 christos unpack_varlen_hex (packet, &frame);
3585 1.1 christos tfnum = (int) frame;
3586 1.1 christos if (tfnum == -1)
3587 1.1 christos {
3588 1.1 christos trace_debug ("Want to stop looking at traceframes");
3589 1.1 christos cs.current_traceframe = -1;
3590 1.1 christos write_ok (own_buf);
3591 1.1 christos return;
3592 1.1 christos }
3593 1.1 christos trace_debug ("Want to look at traceframe %d", tfnum);
3594 1.1 christos tframe = find_traceframe (tfnum);
3595 1.1 christos }
3596 1.1 christos
3597 1.1 christos if (tframe)
3598 1.1 christos {
3599 1.1 christos cs.current_traceframe = tfnum;
3600 1.1 christos sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum);
3601 1.1 christos }
3602 1.1 christos else
3603 1.1 christos sprintf (own_buf, "F-1");
3604 1.1 christos }
3605 1.1 christos
3606 1.1 christos static void
3607 1.1 christos cmd_qtstatus (char *packet)
3608 1.1 christos {
3609 1.1 christos char *stop_reason_rsp = NULL;
3610 1.1 christos char *buf1, *buf2, *buf3;
3611 1.1 christos const char *str;
3612 1.1 christos int slen;
3613 1.1 christos
3614 1.1 christos /* Translate the plain text of the notes back into hex for
3615 1.1 christos transmission. */
3616 1.1 christos
3617 1.1 christos str = (tracing_user_name ? tracing_user_name : "");
3618 1.1 christos slen = strlen (str);
3619 1.1 christos buf1 = (char *) alloca (slen * 2 + 1);
3620 1.1 christos bin2hex ((gdb_byte *) str, buf1, slen);
3621 1.1 christos
3622 1.1 christos str = (tracing_notes ? tracing_notes : "");
3623 1.1 christos slen = strlen (str);
3624 1.1 christos buf2 = (char *) alloca (slen * 2 + 1);
3625 1.1 christos bin2hex ((gdb_byte *) str, buf2, slen);
3626 1.1 christos
3627 1.1 christos str = (tracing_stop_note ? tracing_stop_note : "");
3628 1.1 christos slen = strlen (str);
3629 1.1 christos buf3 = (char *) alloca (slen * 2 + 1);
3630 1.1 christos bin2hex ((gdb_byte *) str, buf3, slen);
3631 1.1 christos
3632 1.1 christos trace_debug ("Returning trace status as %d, stop reason %s",
3633 1.1 christos tracing, tracing_stop_reason);
3634 1.1 christos
3635 1.1 christos if (agent_loaded_p ())
3636 1.1 christos {
3637 1.1 christos target_pause_all (true);
3638 1.1 christos
3639 1.1 christos upload_fast_traceframes ();
3640 1.1 christos
3641 1.1 christos target_unpause_all (true);
3642 1.1 christos }
3643 1.1 christos
3644 1.1 christos stop_reason_rsp = (char *) tracing_stop_reason;
3645 1.1 christos
3646 1.1 christos /* The user visible error string in terror needs to be hex encoded.
3647 1.1 christos We leave it as plain string in `tracing_stop_reason' to ease
3648 1.1 christos debugging. */
3649 1.1 christos if (startswith (stop_reason_rsp, "terror:"))
3650 1.1 christos {
3651 1.1 christos const char *result_name;
3652 1.1 christos int hexstr_len;
3653 1.1 christos char *p;
3654 1.1 christos
3655 1.1 christos result_name = stop_reason_rsp + strlen ("terror:");
3656 1.1 christos hexstr_len = strlen (result_name) * 2;
3657 1.1 christos p = stop_reason_rsp
3658 1.1 christos = (char *) alloca (strlen ("terror:") + hexstr_len + 1);
3659 1.1 christos strcpy (p, "terror:");
3660 1.1 christos p += strlen (p);
3661 1.1 christos bin2hex ((gdb_byte *) result_name, p, strlen (result_name));
3662 1.1 christos }
3663 1.1 christos
3664 1.1 christos /* If this was a forced stop, include any stop note that was supplied. */
3665 1.1 christos if (strcmp (stop_reason_rsp, "tstop") == 0)
3666 1.1 christos {
3667 1.1 christos stop_reason_rsp = (char *) alloca (strlen ("tstop:") + strlen (buf3) + 1);
3668 1.1 christos strcpy (stop_reason_rsp, "tstop:");
3669 1.1 christos strcat (stop_reason_rsp, buf3);
3670 1.1 christos }
3671 1.1 christos
3672 1.1 christos sprintf (packet,
3673 1.1 christos "T%d;"
3674 1.1 christos "%s:%x;"
3675 1.1 christos "tframes:%x;tcreated:%x;"
3676 1.1 christos "tfree:%x;tsize:%s;"
3677 1.1 christos "circular:%d;"
3678 1.1 christos "disconn:%d;"
3679 1.1 christos "starttime:%s;stoptime:%s;"
3680 1.1 christos "username:%s;notes:%s:",
3681 1.1 christos tracing ? 1 : 0,
3682 1.1 christos stop_reason_rsp, tracing_stop_tpnum,
3683 1.1 christos traceframe_count, traceframes_created,
3684 1.1 christos free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
3685 1.1 christos circular_trace_buffer,
3686 1.1 christos disconnected_tracing,
3687 1.1 christos phex_nz (tracing_start_time, sizeof (tracing_start_time)),
3688 1.1 christos phex_nz (tracing_stop_time, sizeof (tracing_stop_time)),
3689 1.1 christos buf1, buf2);
3690 1.1 christos }
3691 1.1 christos
3692 1.1 christos static void
3693 1.1 christos cmd_qtp (char *own_buf)
3694 1.1 christos {
3695 1.1 christos ULONGEST num, addr;
3696 1.1 christos struct tracepoint *tpoint;
3697 1.1 christos const char *packet = own_buf;
3698 1.1 christos
3699 1.1 christos packet += strlen ("qTP:");
3700 1.1 christos
3701 1.1 christos packet = unpack_varlen_hex (packet, &num);
3702 1.1 christos ++packet; /* skip a colon */
3703 1.1 christos packet = unpack_varlen_hex (packet, &addr);
3704 1.1 christos
3705 1.1 christos /* See if we already have this tracepoint. */
3706 1.1 christos tpoint = find_tracepoint (num, addr);
3707 1.1 christos
3708 1.1 christos if (!tpoint)
3709 1.1 christos {
3710 1.1 christos trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3711 1.1 christos (int) num, paddress (addr));
3712 1.1 christos write_enn (own_buf);
3713 1.1 christos return;
3714 1.1 christos }
3715 1.1 christos
3716 1.1 christos sprintf (own_buf, "V%" PRIu64 ":%" PRIu64 "", tpoint->hit_count,
3717 1.1 christos tpoint->traceframe_usage);
3718 1.1 christos }
3719 1.1 christos
3720 1.1 christos /* State variables to help return all the tracepoint bits. */
3721 1.1 christos static struct tracepoint *cur_tpoint;
3722 1.1 christos static unsigned int cur_action;
3723 1.1 christos static unsigned int cur_step_action;
3724 1.1 christos static struct source_string *cur_source_string;
3725 1.1 christos static struct trace_state_variable *cur_tsv;
3726 1.1 christos
3727 1.1 christos /* Compose a response that is an imitation of the syntax by which the
3728 1.1 christos tracepoint was originally downloaded. */
3729 1.1 christos
3730 1.1 christos static void
3731 1.1 christos response_tracepoint (char *packet, struct tracepoint *tpoint)
3732 1.1 christos {
3733 1.1 christos char *buf;
3734 1.1 christos
3735 1.1 christos sprintf (packet, "T%x:%s:%c:%" PRIx64 ":%" PRIx64, tpoint->number,
3736 1.1 christos paddress (tpoint->address),
3737 1.1 christos (tpoint->enabled ? 'E' : 'D'), tpoint->step_count,
3738 1.1 christos tpoint->pass_count);
3739 1.1 christos if (tpoint->type == fast_tracepoint)
3740 1.1 christos sprintf (packet + strlen (packet), ":F%x", tpoint->orig_size);
3741 1.1 christos else if (tpoint->type == static_tracepoint)
3742 1.1 christos sprintf (packet + strlen (packet), ":S");
3743 1.1 christos
3744 1.1 christos if (tpoint->cond)
3745 1.1 christos {
3746 1.1 christos buf = gdb_unparse_agent_expr (tpoint->cond);
3747 1.1 christos sprintf (packet + strlen (packet), ":X%x,%s",
3748 1.1 christos tpoint->cond->length, buf);
3749 1.1 christos free (buf);
3750 1.1 christos }
3751 1.1 christos }
3752 1.1 christos
3753 1.1 christos /* Compose a response that is an imitation of the syntax by which the
3754 1.1 christos tracepoint action was originally downloaded (with the difference
3755 1.1 christos that due to the way we store the actions, this will output a packet
3756 1.1 christos per action, while GDB could have combined more than one action
3757 1.1 christos per-packet. */
3758 1.1 christos
3759 1.1 christos static void
3760 1.1 christos response_action (char *packet, struct tracepoint *tpoint,
3761 1.1 christos char *taction, int step)
3762 1.1 christos {
3763 1.1 christos sprintf (packet, "%c%x:%s:%s",
3764 1.1 christos (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address),
3765 1.1 christos taction);
3766 1.1 christos }
3767 1.1 christos
3768 1.1 christos /* Compose a response that is an imitation of the syntax by which the
3769 1.1 christos tracepoint source piece was originally downloaded. */
3770 1.1 christos
3771 1.1 christos static void
3772 1.1 christos response_source (char *packet,
3773 1.1 christos struct tracepoint *tpoint, struct source_string *src)
3774 1.1 christos {
3775 1.1 christos char *buf;
3776 1.1 christos int len;
3777 1.1 christos
3778 1.1 christos len = strlen (src->str);
3779 1.1 christos buf = (char *) alloca (len * 2 + 1);
3780 1.1 christos bin2hex ((gdb_byte *) src->str, buf, len);
3781 1.1 christos
3782 1.1 christos sprintf (packet, "Z%x:%s:%s:%x:%x:%s",
3783 1.1 christos tpoint->number, paddress (tpoint->address),
3784 1.1 christos src->type, 0, len, buf);
3785 1.1 christos }
3786 1.1 christos
3787 1.1 christos /* Return the first piece of tracepoint definition, and initialize the
3788 1.1 christos state machine that will iterate through all the tracepoint
3789 1.1 christos bits. */
3790 1.1 christos
3791 1.1 christos static void
3792 1.1 christos cmd_qtfp (char *packet)
3793 1.1 christos {
3794 1.1 christos trace_debug ("Returning first tracepoint definition piece");
3795 1.1 christos
3796 1.1 christos cur_tpoint = tracepoints;
3797 1.1 christos cur_action = cur_step_action = 0;
3798 1.1 christos cur_source_string = NULL;
3799 1.1 christos
3800 1.1 christos if (cur_tpoint)
3801 1.1 christos response_tracepoint (packet, cur_tpoint);
3802 1.1 christos else
3803 1.1 christos strcpy (packet, "l");
3804 1.1 christos }
3805 1.1 christos
3806 1.1 christos /* Return additional pieces of tracepoint definition. Each action and
3807 1.1 christos stepping action must go into its own packet, because of packet size
3808 1.1 christos limits, and so we use state variables to deliver one piece at a
3809 1.1 christos time. */
3810 1.1 christos
3811 1.1 christos static void
3812 1.1 christos cmd_qtsp (char *packet)
3813 1.1 christos {
3814 1.1 christos trace_debug ("Returning subsequent tracepoint definition piece");
3815 1.1 christos
3816 1.1 christos if (!cur_tpoint)
3817 1.1 christos {
3818 1.1 christos /* This case would normally never occur, but be prepared for
3819 1.1 christos GDB misbehavior. */
3820 1.1 christos strcpy (packet, "l");
3821 1.1 christos }
3822 1.1 christos else if (cur_action < cur_tpoint->numactions)
3823 1.1 christos {
3824 1.1 christos response_action (packet, cur_tpoint,
3825 1.1 christos cur_tpoint->actions_str[cur_action], 0);
3826 1.1 christos ++cur_action;
3827 1.1 christos }
3828 1.1 christos else if (cur_step_action < cur_tpoint->num_step_actions)
3829 1.1 christos {
3830 1.1 christos response_action (packet, cur_tpoint,
3831 1.1 christos cur_tpoint->step_actions_str[cur_step_action], 1);
3832 1.1 christos ++cur_step_action;
3833 1.1 christos }
3834 1.1 christos else if ((cur_source_string
3835 1.1 christos ? cur_source_string->next
3836 1.1 christos : cur_tpoint->source_strings))
3837 1.1 christos {
3838 1.1 christos if (cur_source_string)
3839 1.1 christos cur_source_string = cur_source_string->next;
3840 1.1 christos else
3841 1.1 christos cur_source_string = cur_tpoint->source_strings;
3842 1.1 christos response_source (packet, cur_tpoint, cur_source_string);
3843 1.1 christos }
3844 1.1 christos else
3845 1.1 christos {
3846 1.1 christos cur_tpoint = cur_tpoint->next;
3847 1.1 christos cur_action = cur_step_action = 0;
3848 1.1 christos cur_source_string = NULL;
3849 1.1 christos if (cur_tpoint)
3850 1.1 christos response_tracepoint (packet, cur_tpoint);
3851 1.1 christos else
3852 1.1 christos strcpy (packet, "l");
3853 1.1 christos }
3854 1.1 christos }
3855 1.1 christos
3856 1.1 christos /* Compose a response that is an imitation of the syntax by which the
3857 1.1 christos trace state variable was originally downloaded. */
3858 1.1 christos
3859 1.1 christos static void
3860 1.1 christos response_tsv (char *packet, struct trace_state_variable *tsv)
3861 1.1 christos {
3862 1.1 christos char *buf = (char *) "";
3863 1.1 christos int namelen;
3864 1.1 christos
3865 1.1 christos if (tsv->name)
3866 1.1 christos {
3867 1.1 christos namelen = strlen (tsv->name);
3868 1.1 christos buf = (char *) alloca (namelen * 2 + 1);
3869 1.1 christos bin2hex ((gdb_byte *) tsv->name, buf, namelen);
3870 1.1 christos }
3871 1.1 christos
3872 1.1 christos sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0),
3873 1.1 christos tsv->getter ? 1 : 0, buf);
3874 1.1 christos }
3875 1.1 christos
3876 1.1 christos /* Return the first trace state variable definition, and initialize
3877 1.1 christos the state machine that will iterate through all the tsv bits. */
3878 1.1 christos
3879 1.1 christos static void
3880 1.1 christos cmd_qtfv (char *packet)
3881 1.1 christos {
3882 1.1 christos trace_debug ("Returning first trace state variable definition");
3883 1.1 christos
3884 1.1 christos cur_tsv = trace_state_variables;
3885 1.1 christos
3886 1.1 christos if (cur_tsv)
3887 1.1 christos response_tsv (packet, cur_tsv);
3888 1.1 christos else
3889 1.1 christos strcpy (packet, "l");
3890 1.1 christos }
3891 1.1 christos
3892 1.1 christos /* Return additional trace state variable definitions. */
3893 1.1 christos
3894 1.1 christos static void
3895 1.1 christos cmd_qtsv (char *packet)
3896 1.1 christos {
3897 1.1 christos trace_debug ("Returning additional trace state variable definition");
3898 1.1 christos
3899 1.1 christos if (cur_tsv)
3900 1.1 christos {
3901 1.1 christos cur_tsv = cur_tsv->next;
3902 1.1 christos if (cur_tsv)
3903 1.1 christos response_tsv (packet, cur_tsv);
3904 1.1 christos else
3905 1.1 christos strcpy (packet, "l");
3906 1.1 christos }
3907 1.1 christos else
3908 1.1 christos strcpy (packet, "l");
3909 1.1 christos }
3910 1.1 christos
3911 1.1 christos /* Return the first static tracepoint marker, and initialize the state
3912 1.1 christos machine that will iterate through all the static tracepoints
3913 1.1 christos markers. */
3914 1.1 christos
3915 1.1 christos static void
3916 1.1 christos cmd_qtfstm (char *packet)
3917 1.1 christos {
3918 1.1 christos if (!maybe_write_ipa_ust_not_loaded (packet))
3919 1.1 christos run_inferior_command (packet, strlen (packet) + 1);
3920 1.1 christos }
3921 1.1 christos
3922 1.1 christos /* Return additional static tracepoints markers. */
3923 1.1 christos
3924 1.1 christos static void
3925 1.1 christos cmd_qtsstm (char *packet)
3926 1.1 christos {
3927 1.1 christos if (!maybe_write_ipa_ust_not_loaded (packet))
3928 1.1 christos run_inferior_command (packet, strlen (packet) + 1);
3929 1.1 christos }
3930 1.1 christos
3931 1.1 christos /* Return the definition of the static tracepoint at a given address.
3932 1.1 christos Result packet is the same as qTsST's. */
3933 1.1 christos
3934 1.1 christos static void
3935 1.1 christos cmd_qtstmat (char *packet)
3936 1.1 christos {
3937 1.1 christos if (!maybe_write_ipa_ust_not_loaded (packet))
3938 1.1 christos run_inferior_command (packet, strlen (packet) + 1);
3939 1.1 christos }
3940 1.1 christos
3941 1.1 christos /* Sent the agent a command to close it. */
3942 1.1 christos
3943 1.1 christos void
3944 1.1 christos gdb_agent_about_to_close (int pid)
3945 1.1 christos {
3946 1.1 christos char buf[IPA_CMD_BUF_SIZE];
3947 1.1 christos
3948 1.1 christos if (!maybe_write_ipa_not_loaded (buf))
3949 1.1 christos {
3950 1.1.1.2 christos scoped_restore_current_thread restore_thread;
3951 1.1 christos
3952 1.1 christos /* Find any thread which belongs to process PID. */
3953 1.1.1.2 christos switch_to_thread (find_any_thread_of_pid (pid));
3954 1.1 christos
3955 1.1 christos strcpy (buf, "close");
3956 1.1 christos
3957 1.1 christos run_inferior_command (buf, strlen (buf) + 1);
3958 1.1 christos }
3959 1.1 christos }
3960 1.1 christos
3961 1.1 christos /* Return the minimum instruction size needed for fast tracepoints as a
3962 1.1 christos hexadecimal number. */
3963 1.1 christos
3964 1.1 christos static void
3965 1.1 christos cmd_qtminftpilen (char *packet)
3966 1.1 christos {
3967 1.1 christos if (current_thread == NULL)
3968 1.1 christos {
3969 1.1 christos /* Indicate that the minimum length is currently unknown. */
3970 1.1 christos strcpy (packet, "0");
3971 1.1 christos return;
3972 1.1 christos }
3973 1.1 christos
3974 1.1 christos sprintf (packet, "%x", target_get_min_fast_tracepoint_insn_len ());
3975 1.1 christos }
3976 1.1 christos
3977 1.1 christos /* Respond to qTBuffer packet with a block of raw data from the trace
3978 1.1 christos buffer. GDB may ask for a lot, but we are allowed to reply with
3979 1.1 christos only as much as will fit within packet limits or whatever. */
3980 1.1 christos
3981 1.1 christos static void
3982 1.1 christos cmd_qtbuffer (char *own_buf)
3983 1.1 christos {
3984 1.1 christos ULONGEST offset, num, tot;
3985 1.1 christos unsigned char *tbp;
3986 1.1 christos const char *packet = own_buf;
3987 1.1 christos
3988 1.1 christos packet += strlen ("qTBuffer:");
3989 1.1 christos
3990 1.1 christos packet = unpack_varlen_hex (packet, &offset);
3991 1.1 christos ++packet; /* skip a comma */
3992 1.1 christos unpack_varlen_hex (packet, &num);
3993 1.1 christos
3994 1.1 christos trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3995 1.1 christos (int) num, phex_nz (offset, 0));
3996 1.1 christos
3997 1.1 christos tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
3998 1.1 christos
3999 1.1 christos /* If we're right at the end, reply specially that we're done. */
4000 1.1 christos if (offset == tot)
4001 1.1 christos {
4002 1.1 christos strcpy (own_buf, "l");
4003 1.1 christos return;
4004 1.1 christos }
4005 1.1 christos
4006 1.1 christos /* Object to any other out-of-bounds request. */
4007 1.1 christos if (offset > tot)
4008 1.1 christos {
4009 1.1 christos write_enn (own_buf);
4010 1.1 christos return;
4011 1.1 christos }
4012 1.1 christos
4013 1.1 christos /* Compute the pointer corresponding to the given offset, accounting
4014 1.1 christos for wraparound. */
4015 1.1 christos tbp = trace_buffer_start + offset;
4016 1.1 christos if (tbp >= trace_buffer_wrap)
4017 1.1 christos tbp -= (trace_buffer_wrap - trace_buffer_lo);
4018 1.1 christos
4019 1.1 christos /* Trim to the remaining bytes if we're close to the end. */
4020 1.1 christos if (num > tot - offset)
4021 1.1 christos num = tot - offset;
4022 1.1 christos
4023 1.1 christos /* Trim to available packet size. */
4024 1.1 christos if (num >= (PBUFSIZ - 16) / 2 )
4025 1.1 christos num = (PBUFSIZ - 16) / 2;
4026 1.1 christos
4027 1.1 christos bin2hex (tbp, own_buf, num);
4028 1.1 christos }
4029 1.1 christos
4030 1.1 christos static void
4031 1.1 christos cmd_bigqtbuffer_circular (char *own_buf)
4032 1.1 christos {
4033 1.1 christos ULONGEST val;
4034 1.1 christos char *packet = own_buf;
4035 1.1 christos
4036 1.1 christos packet += strlen ("QTBuffer:circular:");
4037 1.1 christos
4038 1.1 christos unpack_varlen_hex (packet, &val);
4039 1.1 christos circular_trace_buffer = val;
4040 1.1 christos trace_debug ("Trace buffer is now %s",
4041 1.1 christos circular_trace_buffer ? "circular" : "linear");
4042 1.1 christos write_ok (own_buf);
4043 1.1 christos }
4044 1.1 christos
4045 1.1 christos static void
4046 1.1 christos cmd_bigqtbuffer_size (char *own_buf)
4047 1.1 christos {
4048 1.1 christos ULONGEST val;
4049 1.1 christos LONGEST sval;
4050 1.1 christos char *packet = own_buf;
4051 1.1 christos
4052 1.1 christos /* Can't change the size during a tracing run. */
4053 1.1 christos if (tracing)
4054 1.1 christos {
4055 1.1 christos write_enn (own_buf);
4056 1.1 christos return;
4057 1.1 christos }
4058 1.1 christos
4059 1.1 christos packet += strlen ("QTBuffer:size:");
4060 1.1 christos
4061 1.1 christos /* -1 is sent as literal "-1". */
4062 1.1 christos if (strcmp (packet, "-1") == 0)
4063 1.1 christos sval = DEFAULT_TRACE_BUFFER_SIZE;
4064 1.1 christos else
4065 1.1 christos {
4066 1.1 christos unpack_varlen_hex (packet, &val);
4067 1.1 christos sval = (LONGEST) val;
4068 1.1 christos }
4069 1.1 christos
4070 1.1 christos init_trace_buffer (sval);
4071 1.1 christos trace_debug ("Trace buffer is now %s bytes",
4072 1.1 christos plongest (trace_buffer_size));
4073 1.1 christos write_ok (own_buf);
4074 1.1 christos }
4075 1.1 christos
4076 1.1 christos static void
4077 1.1 christos cmd_qtnotes (char *own_buf)
4078 1.1 christos {
4079 1.1 christos size_t nbytes;
4080 1.1 christos char *saved, *user, *notes, *stopnote;
4081 1.1 christos char *packet = own_buf;
4082 1.1 christos
4083 1.1 christos packet += strlen ("QTNotes:");
4084 1.1 christos
4085 1.1 christos while (*packet)
4086 1.1 christos {
4087 1.1 christos if (startswith (packet, "user:"))
4088 1.1 christos {
4089 1.1 christos packet += strlen ("user:");
4090 1.1 christos saved = packet;
4091 1.1 christos packet = strchr (packet, ';');
4092 1.1 christos nbytes = (packet - saved) / 2;
4093 1.1 christos user = (char *) xmalloc (nbytes + 1);
4094 1.1 christos nbytes = hex2bin (saved, (gdb_byte *) user, nbytes);
4095 1.1 christos user[nbytes] = '\0';
4096 1.1 christos ++packet; /* skip the semicolon */
4097 1.1 christos trace_debug ("User is '%s'", user);
4098 1.1 christos xfree (tracing_user_name);
4099 1.1 christos tracing_user_name = user;
4100 1.1 christos }
4101 1.1 christos else if (startswith (packet, "notes:"))
4102 1.1 christos {
4103 1.1 christos packet += strlen ("notes:");
4104 1.1 christos saved = packet;
4105 1.1 christos packet = strchr (packet, ';');
4106 1.1 christos nbytes = (packet - saved) / 2;
4107 1.1 christos notes = (char *) xmalloc (nbytes + 1);
4108 1.1 christos nbytes = hex2bin (saved, (gdb_byte *) notes, nbytes);
4109 1.1 christos notes[nbytes] = '\0';
4110 1.1 christos ++packet; /* skip the semicolon */
4111 1.1 christos trace_debug ("Notes is '%s'", notes);
4112 1.1 christos xfree (tracing_notes);
4113 1.1 christos tracing_notes = notes;
4114 1.1 christos }
4115 1.1 christos else if (startswith (packet, "tstop:"))
4116 1.1 christos {
4117 1.1 christos packet += strlen ("tstop:");
4118 1.1 christos saved = packet;
4119 1.1 christos packet = strchr (packet, ';');
4120 1.1 christos nbytes = (packet - saved) / 2;
4121 1.1 christos stopnote = (char *) xmalloc (nbytes + 1);
4122 1.1 christos nbytes = hex2bin (saved, (gdb_byte *) stopnote, nbytes);
4123 1.1 christos stopnote[nbytes] = '\0';
4124 1.1 christos ++packet; /* skip the semicolon */
4125 1.1 christos trace_debug ("tstop note is '%s'", stopnote);
4126 1.1 christos xfree (tracing_stop_note);
4127 1.1 christos tracing_stop_note = stopnote;
4128 1.1 christos }
4129 1.1 christos else
4130 1.1 christos break;
4131 1.1 christos }
4132 1.1 christos
4133 1.1 christos write_ok (own_buf);
4134 1.1 christos }
4135 1.1 christos
4136 1.1 christos int
4137 1.1 christos handle_tracepoint_general_set (char *packet)
4138 1.1 christos {
4139 1.1 christos if (strcmp ("QTinit", packet) == 0)
4140 1.1 christos {
4141 1.1 christos cmd_qtinit (packet);
4142 1.1 christos return 1;
4143 1.1 christos }
4144 1.1 christos else if (startswith (packet, "QTDP:"))
4145 1.1 christos {
4146 1.1 christos cmd_qtdp (packet);
4147 1.1 christos return 1;
4148 1.1 christos }
4149 1.1 christos else if (startswith (packet, "QTDPsrc:"))
4150 1.1 christos {
4151 1.1 christos cmd_qtdpsrc (packet);
4152 1.1 christos return 1;
4153 1.1 christos }
4154 1.1 christos else if (startswith (packet, "QTEnable:"))
4155 1.1 christos {
4156 1.1 christos cmd_qtenable_disable (packet, 1);
4157 1.1 christos return 1;
4158 1.1 christos }
4159 1.1 christos else if (startswith (packet, "QTDisable:"))
4160 1.1 christos {
4161 1.1 christos cmd_qtenable_disable (packet, 0);
4162 1.1 christos return 1;
4163 1.1 christos }
4164 1.1 christos else if (startswith (packet, "QTDV:"))
4165 1.1 christos {
4166 1.1 christos cmd_qtdv (packet);
4167 1.1 christos return 1;
4168 1.1 christos }
4169 1.1 christos else if (startswith (packet, "QTro:"))
4170 1.1 christos {
4171 1.1 christos cmd_qtro (packet);
4172 1.1 christos return 1;
4173 1.1 christos }
4174 1.1 christos else if (strcmp ("QTStart", packet) == 0)
4175 1.1 christos {
4176 1.1 christos cmd_qtstart (packet);
4177 1.1 christos return 1;
4178 1.1 christos }
4179 1.1 christos else if (strcmp ("QTStop", packet) == 0)
4180 1.1 christos {
4181 1.1 christos cmd_qtstop (packet);
4182 1.1 christos return 1;
4183 1.1 christos }
4184 1.1 christos else if (startswith (packet, "QTDisconnected:"))
4185 1.1 christos {
4186 1.1 christos cmd_qtdisconnected (packet);
4187 1.1 christos return 1;
4188 1.1 christos }
4189 1.1 christos else if (startswith (packet, "QTFrame:"))
4190 1.1 christos {
4191 1.1 christos cmd_qtframe (packet);
4192 1.1 christos return 1;
4193 1.1 christos }
4194 1.1 christos else if (startswith (packet, "QTBuffer:circular:"))
4195 1.1 christos {
4196 1.1 christos cmd_bigqtbuffer_circular (packet);
4197 1.1 christos return 1;
4198 1.1 christos }
4199 1.1 christos else if (startswith (packet, "QTBuffer:size:"))
4200 1.1 christos {
4201 1.1 christos cmd_bigqtbuffer_size (packet);
4202 1.1 christos return 1;
4203 1.1 christos }
4204 1.1 christos else if (startswith (packet, "QTNotes:"))
4205 1.1 christos {
4206 1.1 christos cmd_qtnotes (packet);
4207 1.1 christos return 1;
4208 1.1 christos }
4209 1.1 christos
4210 1.1 christos return 0;
4211 1.1 christos }
4212 1.1 christos
4213 1.1 christos int
4214 1.1 christos handle_tracepoint_query (char *packet)
4215 1.1 christos {
4216 1.1 christos if (strcmp ("qTStatus", packet) == 0)
4217 1.1 christos {
4218 1.1 christos cmd_qtstatus (packet);
4219 1.1 christos return 1;
4220 1.1 christos }
4221 1.1 christos else if (startswith (packet, "qTP:"))
4222 1.1 christos {
4223 1.1 christos cmd_qtp (packet);
4224 1.1 christos return 1;
4225 1.1 christos }
4226 1.1 christos else if (strcmp ("qTfP", packet) == 0)
4227 1.1 christos {
4228 1.1 christos cmd_qtfp (packet);
4229 1.1 christos return 1;
4230 1.1 christos }
4231 1.1 christos else if (strcmp ("qTsP", packet) == 0)
4232 1.1 christos {
4233 1.1 christos cmd_qtsp (packet);
4234 1.1 christos return 1;
4235 1.1 christos }
4236 1.1 christos else if (strcmp ("qTfV", packet) == 0)
4237 1.1 christos {
4238 1.1 christos cmd_qtfv (packet);
4239 1.1 christos return 1;
4240 1.1 christos }
4241 1.1 christos else if (strcmp ("qTsV", packet) == 0)
4242 1.1 christos {
4243 1.1 christos cmd_qtsv (packet);
4244 1.1 christos return 1;
4245 1.1 christos }
4246 1.1 christos else if (startswith (packet, "qTV:"))
4247 1.1 christos {
4248 1.1 christos cmd_qtv (packet);
4249 1.1 christos return 1;
4250 1.1 christos }
4251 1.1 christos else if (startswith (packet, "qTBuffer:"))
4252 1.1 christos {
4253 1.1 christos cmd_qtbuffer (packet);
4254 1.1 christos return 1;
4255 1.1 christos }
4256 1.1 christos else if (strcmp ("qTfSTM", packet) == 0)
4257 1.1 christos {
4258 1.1 christos cmd_qtfstm (packet);
4259 1.1 christos return 1;
4260 1.1 christos }
4261 1.1 christos else if (strcmp ("qTsSTM", packet) == 0)
4262 1.1 christos {
4263 1.1 christos cmd_qtsstm (packet);
4264 1.1 christos return 1;
4265 1.1 christos }
4266 1.1 christos else if (startswith (packet, "qTSTMat:"))
4267 1.1 christos {
4268 1.1 christos cmd_qtstmat (packet);
4269 1.1 christos return 1;
4270 1.1 christos }
4271 1.1 christos else if (strcmp ("qTMinFTPILen", packet) == 0)
4272 1.1 christos {
4273 1.1 christos cmd_qtminftpilen (packet);
4274 1.1 christos return 1;
4275 1.1 christos }
4276 1.1 christos
4277 1.1 christos return 0;
4278 1.1 christos }
4279 1.1 christos
4280 1.1 christos #endif
4281 1.1 christos #ifndef IN_PROCESS_AGENT
4282 1.1 christos
4283 1.1 christos /* Call this when thread TINFO has hit the tracepoint defined by
4284 1.1 christos TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4285 1.1 christos action. This adds a while-stepping collecting state item to the
4286 1.1 christos threads' collecting state list, so that we can keep track of
4287 1.1 christos multiple simultaneous while-stepping actions being collected by the
4288 1.1 christos same thread. This can happen in cases like:
4289 1.1 christos
4290 1.1 christos ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4291 1.1 christos ff0002 INSN2
4292 1.1 christos ff0003 INSN3 <-- TP2, collect $regs
4293 1.1 christos ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4294 1.1 christos ff0005 INSN5
4295 1.1 christos
4296 1.1 christos Notice that when instruction INSN5 is reached, the while-stepping
4297 1.1 christos actions of both TP1 and TP3 are still being collected, and that TP2
4298 1.1 christos had been collected meanwhile. The whole range of ff0001-ff0005
4299 1.1 christos should be single-stepped, due to at least TP1's while-stepping
4300 1.1 christos action covering the whole range. */
4301 1.1 christos
4302 1.1 christos static void
4303 1.1 christos add_while_stepping_state (struct thread_info *tinfo,
4304 1.1 christos int tp_number, CORE_ADDR tp_address)
4305 1.1 christos {
4306 1.1 christos struct wstep_state *wstep = XNEW (struct wstep_state);
4307 1.1 christos
4308 1.1 christos wstep->next = tinfo->while_stepping;
4309 1.1 christos
4310 1.1 christos wstep->tp_number = tp_number;
4311 1.1 christos wstep->tp_address = tp_address;
4312 1.1 christos wstep->current_step = 0;
4313 1.1 christos
4314 1.1 christos tinfo->while_stepping = wstep;
4315 1.1 christos }
4316 1.1 christos
4317 1.1 christos /* Release the while-stepping collecting state WSTEP. */
4318 1.1 christos
4319 1.1 christos static void
4320 1.1 christos release_while_stepping_state (struct wstep_state *wstep)
4321 1.1 christos {
4322 1.1 christos free (wstep);
4323 1.1 christos }
4324 1.1 christos
4325 1.1 christos /* Release all while-stepping collecting states currently associated
4326 1.1 christos with thread TINFO. */
4327 1.1 christos
4328 1.1 christos void
4329 1.1 christos release_while_stepping_state_list (struct thread_info *tinfo)
4330 1.1 christos {
4331 1.1 christos struct wstep_state *head;
4332 1.1 christos
4333 1.1 christos while (tinfo->while_stepping)
4334 1.1 christos {
4335 1.1 christos head = tinfo->while_stepping;
4336 1.1 christos tinfo->while_stepping = head->next;
4337 1.1 christos release_while_stepping_state (head);
4338 1.1 christos }
4339 1.1 christos }
4340 1.1 christos
4341 1.1 christos /* If TINFO was handling a 'while-stepping' action, the step has
4342 1.1 christos finished, so collect any step data needed, and check if any more
4343 1.1 christos steps are required. Return true if the thread was indeed
4344 1.1 christos collecting tracepoint data, false otherwise. */
4345 1.1 christos
4346 1.1 christos int
4347 1.1 christos tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
4348 1.1 christos {
4349 1.1 christos struct tracepoint *tpoint;
4350 1.1 christos struct wstep_state *wstep;
4351 1.1 christos struct wstep_state **wstep_link;
4352 1.1 christos struct trap_tracepoint_ctx ctx;
4353 1.1 christos
4354 1.1 christos /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4355 1.1 christos our buffer. */
4356 1.1 christos if (agent_loaded_p ())
4357 1.1 christos upload_fast_traceframes ();
4358 1.1 christos
4359 1.1 christos /* Check if we were indeed collecting data for one of more
4360 1.1 christos tracepoints with a 'while-stepping' count. */
4361 1.1 christos if (tinfo->while_stepping == NULL)
4362 1.1 christos return 0;
4363 1.1 christos
4364 1.1 christos if (!tracing)
4365 1.1 christos {
4366 1.1 christos /* We're not even tracing anymore. Stop this thread from
4367 1.1 christos collecting. */
4368 1.1 christos release_while_stepping_state_list (tinfo);
4369 1.1 christos
4370 1.1 christos /* The thread had stopped due to a single-step request indeed
4371 1.1 christos explained by a tracepoint. */
4372 1.1 christos return 1;
4373 1.1 christos }
4374 1.1 christos
4375 1.1 christos wstep = tinfo->while_stepping;
4376 1.1 christos wstep_link = &tinfo->while_stepping;
4377 1.1 christos
4378 1.1 christos trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4379 1.1.1.2 christos target_pid_to_str (tinfo->id).c_str (),
4380 1.1 christos wstep->tp_number, paddress (wstep->tp_address));
4381 1.1 christos
4382 1.1 christos ctx.base.type = trap_tracepoint;
4383 1.1 christos ctx.regcache = get_thread_regcache (tinfo, 1);
4384 1.1 christos
4385 1.1 christos while (wstep != NULL)
4386 1.1 christos {
4387 1.1 christos tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
4388 1.1 christos if (tpoint == NULL)
4389 1.1 christos {
4390 1.1 christos trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4391 1.1 christos wstep->tp_number, paddress (wstep->tp_address),
4392 1.1.1.2 christos target_pid_to_str (tinfo->id).c_str ());
4393 1.1 christos
4394 1.1 christos /* Unlink. */
4395 1.1 christos *wstep_link = wstep->next;
4396 1.1 christos release_while_stepping_state (wstep);
4397 1.1 christos wstep = *wstep_link;
4398 1.1 christos continue;
4399 1.1 christos }
4400 1.1 christos
4401 1.1 christos /* We've just finished one step. */
4402 1.1 christos ++wstep->current_step;
4403 1.1 christos
4404 1.1 christos /* Collect data. */
4405 1.1 christos collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
4406 1.1 christos stop_pc, tpoint, wstep->current_step);
4407 1.1 christos
4408 1.1 christos if (wstep->current_step >= tpoint->step_count)
4409 1.1 christos {
4410 1.1 christos /* The requested numbers of steps have occurred. */
4411 1.1 christos trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4412 1.1.1.2 christos target_pid_to_str (tinfo->id).c_str (),
4413 1.1 christos wstep->tp_number, paddress (wstep->tp_address));
4414 1.1 christos
4415 1.1 christos /* Unlink the wstep. */
4416 1.1 christos *wstep_link = wstep->next;
4417 1.1 christos release_while_stepping_state (wstep);
4418 1.1 christos wstep = *wstep_link;
4419 1.1 christos
4420 1.1 christos /* Only check the hit count now, which ensure that we do all
4421 1.1 christos our stepping before stopping the run. */
4422 1.1 christos if (tpoint->pass_count > 0
4423 1.1 christos && tpoint->hit_count >= tpoint->pass_count
4424 1.1 christos && stopping_tracepoint == NULL)
4425 1.1 christos stopping_tracepoint = tpoint;
4426 1.1 christos }
4427 1.1 christos else
4428 1.1 christos {
4429 1.1 christos /* Keep single-stepping until the requested numbers of steps
4430 1.1 christos have occurred. */
4431 1.1 christos wstep_link = &wstep->next;
4432 1.1 christos wstep = *wstep_link;
4433 1.1 christos }
4434 1.1 christos
4435 1.1 christos if (stopping_tracepoint
4436 1.1 christos || trace_buffer_is_full
4437 1.1 christos || expr_eval_result != expr_eval_no_error)
4438 1.1 christos {
4439 1.1 christos stop_tracing ();
4440 1.1 christos break;
4441 1.1 christos }
4442 1.1 christos }
4443 1.1 christos
4444 1.1 christos return 1;
4445 1.1 christos }
4446 1.1 christos
4447 1.1 christos /* Handle any internal tracing control breakpoint hits. That means,
4448 1.1 christos pull traceframes from the IPA to our buffer, and syncing both
4449 1.1 christos tracing agents when the IPA's tracing stops for some reason. */
4450 1.1 christos
4451 1.1 christos int
4452 1.1 christos handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
4453 1.1 christos {
4454 1.1 christos /* Pull in fast tracepoint trace frames from the inferior in-process
4455 1.1 christos agent's buffer into our buffer. */
4456 1.1 christos
4457 1.1 christos if (!agent_loaded_p ())
4458 1.1 christos return 0;
4459 1.1 christos
4460 1.1 christos upload_fast_traceframes ();
4461 1.1 christos
4462 1.1 christos /* Check if the in-process agent had decided we should stop
4463 1.1 christos tracing. */
4464 1.1 christos if (stop_pc == ipa_sym_addrs.addr_stop_tracing)
4465 1.1 christos {
4466 1.1 christos int ipa_trace_buffer_is_full;
4467 1.1 christos CORE_ADDR ipa_stopping_tracepoint;
4468 1.1 christos int ipa_expr_eval_result;
4469 1.1 christos CORE_ADDR ipa_error_tracepoint;
4470 1.1 christos
4471 1.1 christos trace_debug ("lib stopped at stop_tracing");
4472 1.1 christos
4473 1.1 christos read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full,
4474 1.1 christos &ipa_trace_buffer_is_full);
4475 1.1 christos
4476 1.1 christos read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
4477 1.1 christos &ipa_stopping_tracepoint);
4478 1.1 christos write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0);
4479 1.1 christos
4480 1.1 christos read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint,
4481 1.1 christos &ipa_error_tracepoint);
4482 1.1 christos write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0);
4483 1.1 christos
4484 1.1 christos read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result,
4485 1.1 christos &ipa_expr_eval_result);
4486 1.1 christos write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0);
4487 1.1 christos
4488 1.1 christos trace_debug ("lib: trace_buffer_is_full: %d, "
4489 1.1 christos "stopping_tracepoint: %s, "
4490 1.1 christos "ipa_expr_eval_result: %d, "
4491 1.1 christos "error_tracepoint: %s, ",
4492 1.1 christos ipa_trace_buffer_is_full,
4493 1.1 christos paddress (ipa_stopping_tracepoint),
4494 1.1 christos ipa_expr_eval_result,
4495 1.1 christos paddress (ipa_error_tracepoint));
4496 1.1 christos
4497 1.1.1.2 christos if (ipa_trace_buffer_is_full)
4498 1.1.1.2 christos trace_debug ("lib stopped due to full buffer.");
4499 1.1.1.2 christos
4500 1.1.1.2 christos if (ipa_stopping_tracepoint)
4501 1.1.1.2 christos trace_debug ("lib stopped due to tpoint");
4502 1.1.1.2 christos
4503 1.1.1.2 christos if (ipa_error_tracepoint)
4504 1.1.1.2 christos trace_debug ("lib stopped due to error");
4505 1.1 christos
4506 1.1 christos if (ipa_stopping_tracepoint != 0)
4507 1.1 christos {
4508 1.1 christos stopping_tracepoint
4509 1.1 christos = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint);
4510 1.1 christos }
4511 1.1 christos else if (ipa_expr_eval_result != expr_eval_no_error)
4512 1.1 christos {
4513 1.1 christos expr_eval_result = ipa_expr_eval_result;
4514 1.1 christos error_tracepoint
4515 1.1 christos = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint);
4516 1.1 christos }
4517 1.1 christos stop_tracing ();
4518 1.1 christos return 1;
4519 1.1 christos }
4520 1.1 christos else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer)
4521 1.1 christos {
4522 1.1 christos trace_debug ("lib stopped at flush_trace_buffer");
4523 1.1 christos return 1;
4524 1.1 christos }
4525 1.1 christos
4526 1.1 christos return 0;
4527 1.1 christos }
4528 1.1 christos
4529 1.1 christos /* Return true if TINFO just hit a tracepoint. Collect data if
4530 1.1 christos so. */
4531 1.1 christos
4532 1.1 christos int
4533 1.1 christos tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
4534 1.1 christos {
4535 1.1 christos struct tracepoint *tpoint;
4536 1.1 christos int ret = 0;
4537 1.1 christos struct trap_tracepoint_ctx ctx;
4538 1.1 christos
4539 1.1 christos /* Not tracing, don't handle. */
4540 1.1 christos if (!tracing)
4541 1.1 christos return 0;
4542 1.1 christos
4543 1.1 christos ctx.base.type = trap_tracepoint;
4544 1.1 christos ctx.regcache = get_thread_regcache (tinfo, 1);
4545 1.1 christos
4546 1.1 christos for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
4547 1.1 christos {
4548 1.1 christos /* Note that we collect fast tracepoints here as well. We'll
4549 1.1 christos step over the fast tracepoint jump later, which avoids the
4550 1.1 christos double collect. However, we don't collect for static
4551 1.1 christos tracepoints here, because UST markers are compiled in program,
4552 1.1 christos and probes will be executed in program. So static tracepoints
4553 1.1 christos are collected there. */
4554 1.1 christos if (tpoint->enabled && stop_pc == tpoint->address
4555 1.1 christos && tpoint->type != static_tracepoint)
4556 1.1 christos {
4557 1.1 christos trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4558 1.1.1.2 christos target_pid_to_str (tinfo->id).c_str (),
4559 1.1 christos tpoint->number, paddress (tpoint->address));
4560 1.1 christos
4561 1.1 christos /* Test the condition if present, and collect if true. */
4562 1.1 christos if (!tpoint->cond
4563 1.1 christos || (condition_true_at_tracepoint
4564 1.1 christos ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
4565 1.1 christos collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
4566 1.1 christos stop_pc, tpoint);
4567 1.1 christos
4568 1.1 christos if (stopping_tracepoint
4569 1.1 christos || trace_buffer_is_full
4570 1.1 christos || expr_eval_result != expr_eval_no_error)
4571 1.1 christos {
4572 1.1 christos stop_tracing ();
4573 1.1 christos }
4574 1.1 christos /* If the tracepoint had a 'while-stepping' action, then set
4575 1.1 christos the thread to collect this tracepoint on the following
4576 1.1 christos single-steps. */
4577 1.1 christos else if (tpoint->step_count > 0)
4578 1.1 christos {
4579 1.1 christos add_while_stepping_state (tinfo,
4580 1.1 christos tpoint->number, tpoint->address);
4581 1.1 christos }
4582 1.1 christos
4583 1.1 christos ret = 1;
4584 1.1 christos }
4585 1.1 christos }
4586 1.1 christos
4587 1.1 christos return ret;
4588 1.1 christos }
4589 1.1 christos
4590 1.1 christos #endif
4591 1.1 christos
4592 1.1 christos #if defined IN_PROCESS_AGENT && defined HAVE_UST
4593 1.1 christos struct ust_marker_data;
4594 1.1 christos static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4595 1.1 christos struct traceframe *tframe);
4596 1.1 christos #endif
4597 1.1 christos
4598 1.1 christos /* Create a trace frame for the hit of the given tracepoint in the
4599 1.1 christos given thread. */
4600 1.1 christos
4601 1.1 christos static void
4602 1.1 christos collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
4603 1.1 christos struct tracepoint *tpoint)
4604 1.1 christos {
4605 1.1 christos struct traceframe *tframe;
4606 1.1 christos int acti;
4607 1.1 christos
4608 1.1 christos /* Only count it as a hit when we actually collect data. */
4609 1.1 christos tpoint->hit_count++;
4610 1.1 christos
4611 1.1 christos /* If we've exceeded a defined pass count, record the event for
4612 1.1 christos later, and finish the collection for this hit. This test is only
4613 1.1 christos for nonstepping tracepoints, stepping tracepoints test at the end
4614 1.1 christos of their while-stepping loop. */
4615 1.1 christos if (tpoint->pass_count > 0
4616 1.1 christos && tpoint->hit_count >= tpoint->pass_count
4617 1.1 christos && tpoint->step_count == 0
4618 1.1 christos && stopping_tracepoint == NULL)
4619 1.1 christos stopping_tracepoint = tpoint;
4620 1.1 christos
4621 1.1 christos trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64,
4622 1.1 christos tpoint->number, paddress (tpoint->address), tpoint->hit_count);
4623 1.1 christos
4624 1.1 christos tframe = add_traceframe (tpoint);
4625 1.1 christos
4626 1.1 christos if (tframe)
4627 1.1 christos {
4628 1.1 christos for (acti = 0; acti < tpoint->numactions; ++acti)
4629 1.1 christos {
4630 1.1 christos #ifndef IN_PROCESS_AGENT
4631 1.1 christos trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4632 1.1 christos tpoint->number, paddress (tpoint->address),
4633 1.1 christos tpoint->actions_str[acti]);
4634 1.1 christos #endif
4635 1.1 christos
4636 1.1 christos do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4637 1.1 christos tpoint->actions[acti]);
4638 1.1 christos }
4639 1.1 christos
4640 1.1 christos finish_traceframe (tframe);
4641 1.1 christos }
4642 1.1 christos
4643 1.1 christos if (tframe == NULL && tracing)
4644 1.1 christos trace_buffer_is_full = 1;
4645 1.1 christos }
4646 1.1 christos
4647 1.1 christos #ifndef IN_PROCESS_AGENT
4648 1.1 christos
4649 1.1 christos static void
4650 1.1 christos collect_data_at_step (struct tracepoint_hit_ctx *ctx,
4651 1.1 christos CORE_ADDR stop_pc,
4652 1.1 christos struct tracepoint *tpoint, int current_step)
4653 1.1 christos {
4654 1.1 christos struct traceframe *tframe;
4655 1.1 christos int acti;
4656 1.1 christos
4657 1.1 christos trace_debug ("Making new step traceframe for "
4658 1.1 christos "tracepoint %d at 0x%s, step %d of %" PRIu64 ", hit %" PRIu64,
4659 1.1 christos tpoint->number, paddress (tpoint->address),
4660 1.1 christos current_step, tpoint->step_count,
4661 1.1 christos tpoint->hit_count);
4662 1.1 christos
4663 1.1 christos tframe = add_traceframe (tpoint);
4664 1.1 christos
4665 1.1 christos if (tframe)
4666 1.1 christos {
4667 1.1 christos for (acti = 0; acti < tpoint->num_step_actions; ++acti)
4668 1.1 christos {
4669 1.1 christos trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4670 1.1 christos tpoint->number, paddress (tpoint->address),
4671 1.1 christos tpoint->step_actions_str[acti]);
4672 1.1 christos
4673 1.1 christos do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4674 1.1 christos tpoint->step_actions[acti]);
4675 1.1 christos }
4676 1.1 christos
4677 1.1 christos finish_traceframe (tframe);
4678 1.1 christos }
4679 1.1 christos
4680 1.1 christos if (tframe == NULL && tracing)
4681 1.1 christos trace_buffer_is_full = 1;
4682 1.1 christos }
4683 1.1 christos
4684 1.1 christos #endif
4685 1.1 christos
4686 1.1 christos #ifdef IN_PROCESS_AGENT
4687 1.1 christos /* The target description index for IPA. Passed from gdbserver, used
4688 1.1 christos to select ipa_tdesc. */
4689 1.1 christos EXTERN_C_PUSH
4690 1.1 christos IP_AGENT_EXPORT_VAR int ipa_tdesc_idx;
4691 1.1 christos EXTERN_C_POP
4692 1.1 christos #endif
4693 1.1 christos
4694 1.1 christos static struct regcache *
4695 1.1 christos get_context_regcache (struct tracepoint_hit_ctx *ctx)
4696 1.1 christos {
4697 1.1 christos struct regcache *regcache = NULL;
4698 1.1 christos #ifdef IN_PROCESS_AGENT
4699 1.1 christos const struct target_desc *ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);
4700 1.1 christos
4701 1.1 christos if (ctx->type == fast_tracepoint)
4702 1.1 christos {
4703 1.1 christos struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
4704 1.1 christos if (!fctx->regcache_initted)
4705 1.1 christos {
4706 1.1 christos fctx->regcache_initted = 1;
4707 1.1 christos init_register_cache (&fctx->regcache, ipa_tdesc, fctx->regspace);
4708 1.1 christos supply_regblock (&fctx->regcache, NULL);
4709 1.1 christos supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
4710 1.1 christos }
4711 1.1 christos regcache = &fctx->regcache;
4712 1.1 christos }
4713 1.1 christos #ifdef HAVE_UST
4714 1.1 christos if (ctx->type == static_tracepoint)
4715 1.1 christos {
4716 1.1 christos struct static_tracepoint_ctx *sctx
4717 1.1 christos = (struct static_tracepoint_ctx *) ctx;
4718 1.1 christos
4719 1.1 christos if (!sctx->regcache_initted)
4720 1.1 christos {
4721 1.1 christos sctx->regcache_initted = 1;
4722 1.1 christos init_register_cache (&sctx->regcache, ipa_tdesc, sctx->regspace);
4723 1.1 christos supply_regblock (&sctx->regcache, NULL);
4724 1.1 christos /* Pass down the tracepoint address, because REGS doesn't
4725 1.1 christos include the PC, but we know what it must have been. */
4726 1.1 christos supply_static_tracepoint_registers (&sctx->regcache,
4727 1.1 christos (const unsigned char *)
4728 1.1 christos sctx->regs,
4729 1.1 christos sctx->tpoint->address);
4730 1.1 christos }
4731 1.1 christos regcache = &sctx->regcache;
4732 1.1 christos }
4733 1.1 christos #endif
4734 1.1 christos #else
4735 1.1 christos if (ctx->type == trap_tracepoint)
4736 1.1 christos {
4737 1.1 christos struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
4738 1.1 christos regcache = tctx->regcache;
4739 1.1 christos }
4740 1.1 christos #endif
4741 1.1 christos
4742 1.1 christos gdb_assert (regcache != NULL);
4743 1.1 christos
4744 1.1 christos return regcache;
4745 1.1 christos }
4746 1.1 christos
4747 1.1 christos static void
4748 1.1 christos do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4749 1.1 christos CORE_ADDR stop_pc,
4750 1.1 christos struct tracepoint *tpoint,
4751 1.1 christos struct traceframe *tframe,
4752 1.1 christos struct tracepoint_action *taction)
4753 1.1 christos {
4754 1.1 christos enum eval_result_type err;
4755 1.1 christos
4756 1.1 christos switch (taction->type)
4757 1.1 christos {
4758 1.1 christos case 'M':
4759 1.1 christos {
4760 1.1 christos struct collect_memory_action *maction;
4761 1.1 christos struct eval_agent_expr_context ax_ctx;
4762 1.1 christos
4763 1.1 christos maction = (struct collect_memory_action *) taction;
4764 1.1 christos ax_ctx.regcache = NULL;
4765 1.1 christos ax_ctx.tframe = tframe;
4766 1.1 christos ax_ctx.tpoint = tpoint;
4767 1.1 christos
4768 1.1 christos trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4769 1.1 christos pulongest (maction->len),
4770 1.1 christos paddress (maction->addr), maction->basereg);
4771 1.1 christos /* (should use basereg) */
4772 1.1 christos agent_mem_read (&ax_ctx, NULL, (CORE_ADDR) maction->addr,
4773 1.1 christos maction->len);
4774 1.1 christos break;
4775 1.1 christos }
4776 1.1 christos case 'R':
4777 1.1 christos {
4778 1.1 christos unsigned char *regspace;
4779 1.1 christos struct regcache tregcache;
4780 1.1 christos struct regcache *context_regcache;
4781 1.1 christos int regcache_size;
4782 1.1 christos
4783 1.1 christos trace_debug ("Want to collect registers");
4784 1.1 christos
4785 1.1 christos context_regcache = get_context_regcache (ctx);
4786 1.1 christos regcache_size = register_cache_size (context_regcache->tdesc);
4787 1.1 christos
4788 1.1 christos /* Collect all registers for now. */
4789 1.1 christos regspace = add_traceframe_block (tframe, tpoint, 1 + regcache_size);
4790 1.1 christos if (regspace == NULL)
4791 1.1 christos {
4792 1.1 christos trace_debug ("Trace buffer block allocation failed, skipping");
4793 1.1 christos break;
4794 1.1 christos }
4795 1.1 christos /* Identify a register block. */
4796 1.1 christos *regspace = 'R';
4797 1.1 christos
4798 1.1 christos /* Wrap the regblock in a register cache (in the stack, we
4799 1.1 christos don't want to malloc here). */
4800 1.1 christos init_register_cache (&tregcache, context_regcache->tdesc,
4801 1.1 christos regspace + 1);
4802 1.1 christos
4803 1.1 christos /* Copy the register data to the regblock. */
4804 1.1 christos regcache_cpy (&tregcache, context_regcache);
4805 1.1 christos
4806 1.1 christos #ifndef IN_PROCESS_AGENT
4807 1.1 christos /* On some platforms, trap-based tracepoints will have the PC
4808 1.1 christos pointing to the next instruction after the trap, but we
4809 1.1 christos don't want the user or GDB trying to guess whether the
4810 1.1 christos saved PC needs adjusting; so always record the adjusted
4811 1.1 christos stop_pc. Note that we can't use tpoint->address instead,
4812 1.1 christos since it will be wrong for while-stepping actions. This
4813 1.1 christos adjustment is a nop for fast tracepoints collected from the
4814 1.1 christos in-process lib (but not if GDBserver is collecting one
4815 1.1 christos preemptively), since the PC had already been adjusted to
4816 1.1 christos contain the tracepoint's address by the jump pad. */
4817 1.1 christos trace_debug ("Storing stop pc (0x%s) in regblock",
4818 1.1 christos paddress (stop_pc));
4819 1.1 christos
4820 1.1 christos /* This changes the regblock, not the thread's
4821 1.1 christos regcache. */
4822 1.1 christos regcache_write_pc (&tregcache, stop_pc);
4823 1.1 christos #endif
4824 1.1 christos }
4825 1.1 christos break;
4826 1.1 christos case 'X':
4827 1.1 christos {
4828 1.1 christos struct eval_expr_action *eaction;
4829 1.1 christos struct eval_agent_expr_context ax_ctx;
4830 1.1 christos
4831 1.1 christos eaction = (struct eval_expr_action *) taction;
4832 1.1 christos ax_ctx.regcache = get_context_regcache (ctx);
4833 1.1 christos ax_ctx.tframe = tframe;
4834 1.1 christos ax_ctx.tpoint = tpoint;
4835 1.1 christos
4836 1.1 christos trace_debug ("Want to evaluate expression");
4837 1.1 christos
4838 1.1 christos err = gdb_eval_agent_expr (&ax_ctx, eaction->expr, NULL);
4839 1.1 christos
4840 1.1 christos if (err != expr_eval_no_error)
4841 1.1 christos {
4842 1.1 christos record_tracepoint_error (tpoint, "action expression", err);
4843 1.1 christos return;
4844 1.1 christos }
4845 1.1 christos }
4846 1.1 christos break;
4847 1.1 christos case 'L':
4848 1.1 christos {
4849 1.1 christos #if defined IN_PROCESS_AGENT && defined HAVE_UST
4850 1.1 christos trace_debug ("Want to collect static trace data");
4851 1.1 christos collect_ust_data_at_tracepoint (ctx, tframe);
4852 1.1 christos #else
4853 1.1 christos trace_debug ("warning: collecting static trace data, "
4854 1.1 christos "but static tracepoints are not supported");
4855 1.1 christos #endif
4856 1.1 christos }
4857 1.1 christos break;
4858 1.1 christos default:
4859 1.1 christos trace_debug ("unknown trace action '%c', ignoring", taction->type);
4860 1.1 christos break;
4861 1.1 christos }
4862 1.1 christos }
4863 1.1 christos
4864 1.1 christos static int
4865 1.1 christos condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4866 1.1 christos struct tracepoint *tpoint)
4867 1.1 christos {
4868 1.1 christos ULONGEST value = 0;
4869 1.1 christos enum eval_result_type err;
4870 1.1 christos
4871 1.1 christos /* Presently, gdbserver doesn't run compiled conditions, only the
4872 1.1 christos IPA does. If the program stops at a fast tracepoint's address
4873 1.1 christos (e.g., due to a breakpoint, trap tracepoint, or stepping),
4874 1.1 christos gdbserver preemptively collect the fast tracepoint. Later, on
4875 1.1 christos resume, gdbserver steps over the fast tracepoint like it steps
4876 1.1 christos over breakpoints, so that the IPA doesn't see that fast
4877 1.1 christos tracepoint. This avoids double collects of fast tracepoints in
4878 1.1 christos that stopping scenario. Having gdbserver itself handle the fast
4879 1.1 christos tracepoint gives the user a consistent view of when fast or trap
4880 1.1 christos tracepoints are collected, compared to an alternative where only
4881 1.1 christos trap tracepoints are collected on stop, and fast tracepoints on
4882 1.1 christos resume. When a fast tracepoint is being processed by gdbserver,
4883 1.1 christos it is always the non-compiled condition expression that is
4884 1.1 christos used. */
4885 1.1 christos #ifdef IN_PROCESS_AGENT
4886 1.1 christos if (tpoint->compiled_cond)
4887 1.1 christos {
4888 1.1 christos struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
4889 1.1 christos err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (fctx->regs, &value);
4890 1.1 christos }
4891 1.1 christos else
4892 1.1 christos #endif
4893 1.1 christos {
4894 1.1 christos struct eval_agent_expr_context ax_ctx;
4895 1.1 christos
4896 1.1 christos ax_ctx.regcache = get_context_regcache (ctx);
4897 1.1 christos ax_ctx.tframe = NULL;
4898 1.1 christos ax_ctx.tpoint = tpoint;
4899 1.1 christos
4900 1.1 christos err = gdb_eval_agent_expr (&ax_ctx, tpoint->cond, &value);
4901 1.1 christos }
4902 1.1 christos if (err != expr_eval_no_error)
4903 1.1 christos {
4904 1.1 christos record_tracepoint_error (tpoint, "condition", err);
4905 1.1 christos /* The error case must return false. */
4906 1.1 christos return 0;
4907 1.1 christos }
4908 1.1 christos
4909 1.1 christos trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4910 1.1 christos tpoint->number, paddress (tpoint->address),
4911 1.1 christos pulongest (value));
4912 1.1 christos return (value ? 1 : 0);
4913 1.1 christos }
4914 1.1 christos
4915 1.1 christos /* Do memory copies for bytecodes. */
4916 1.1 christos /* Do the recording of memory blocks for actions and bytecodes. */
4917 1.1 christos
4918 1.1 christos int
4919 1.1 christos agent_mem_read (struct eval_agent_expr_context *ctx,
4920 1.1 christos unsigned char *to, CORE_ADDR from, ULONGEST len)
4921 1.1 christos {
4922 1.1 christos unsigned char *mspace;
4923 1.1 christos ULONGEST remaining = len;
4924 1.1 christos unsigned short blocklen;
4925 1.1 christos
4926 1.1 christos /* If a 'to' buffer is specified, use it. */
4927 1.1 christos if (to != NULL)
4928 1.1 christos {
4929 1.1 christos read_inferior_memory (from, to, len);
4930 1.1 christos return 0;
4931 1.1 christos }
4932 1.1 christos
4933 1.1 christos /* Otherwise, create a new memory block in the trace buffer. */
4934 1.1 christos while (remaining > 0)
4935 1.1 christos {
4936 1.1 christos size_t sp;
4937 1.1 christos
4938 1.1 christos blocklen = (remaining > 65535 ? 65535 : remaining);
4939 1.1 christos sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4940 1.1 christos mspace = add_traceframe_block (ctx->tframe, ctx->tpoint, sp);
4941 1.1 christos if (mspace == NULL)
4942 1.1 christos return 1;
4943 1.1 christos /* Identify block as a memory block. */
4944 1.1 christos *mspace = 'M';
4945 1.1 christos ++mspace;
4946 1.1 christos /* Record address and size. */
4947 1.1 christos memcpy (mspace, &from, sizeof (from));
4948 1.1 christos mspace += sizeof (from);
4949 1.1 christos memcpy (mspace, &blocklen, sizeof (blocklen));
4950 1.1 christos mspace += sizeof (blocklen);
4951 1.1 christos /* Record the memory block proper. */
4952 1.1 christos read_inferior_memory (from, mspace, blocklen);
4953 1.1 christos trace_debug ("%d bytes recorded", blocklen);
4954 1.1 christos remaining -= blocklen;
4955 1.1 christos from += blocklen;
4956 1.1 christos }
4957 1.1 christos return 0;
4958 1.1 christos }
4959 1.1 christos
4960 1.1 christos int
4961 1.1 christos agent_mem_read_string (struct eval_agent_expr_context *ctx,
4962 1.1 christos unsigned char *to, CORE_ADDR from, ULONGEST len)
4963 1.1 christos {
4964 1.1 christos unsigned char *buf, *mspace;
4965 1.1 christos ULONGEST remaining = len;
4966 1.1 christos unsigned short blocklen, i;
4967 1.1 christos
4968 1.1 christos /* To save a bit of space, block lengths are 16-bit, so break large
4969 1.1 christos requests into multiple blocks. Bordering on overkill for strings,
4970 1.1 christos but it could happen that someone specifies a large max length. */
4971 1.1 christos while (remaining > 0)
4972 1.1 christos {
4973 1.1 christos size_t sp;
4974 1.1 christos
4975 1.1 christos blocklen = (remaining > 65535 ? 65535 : remaining);
4976 1.1 christos /* We want working space to accumulate nonzero bytes, since
4977 1.1 christos traceframes must have a predecided size (otherwise it gets
4978 1.1 christos harder to wrap correctly for the circular case, etc). */
4979 1.1 christos buf = (unsigned char *) xmalloc (blocklen + 1);
4980 1.1 christos for (i = 0; i < blocklen; ++i)
4981 1.1 christos {
4982 1.1 christos /* Read the string one byte at a time, in case the string is
4983 1.1 christos at the end of a valid memory area - we don't want a
4984 1.1 christos correctly-terminated string to engender segvio
4985 1.1 christos complaints. */
4986 1.1 christos read_inferior_memory (from + i, buf + i, 1);
4987 1.1 christos
4988 1.1 christos if (buf[i] == '\0')
4989 1.1 christos {
4990 1.1 christos blocklen = i + 1;
4991 1.1 christos /* Make sure outer loop stops now too. */
4992 1.1 christos remaining = blocklen;
4993 1.1 christos break;
4994 1.1 christos }
4995 1.1 christos }
4996 1.1 christos sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4997 1.1 christos mspace = add_traceframe_block (ctx->tframe, ctx->tpoint, sp);
4998 1.1 christos if (mspace == NULL)
4999 1.1 christos {
5000 1.1 christos xfree (buf);
5001 1.1 christos return 1;
5002 1.1 christos }
5003 1.1 christos /* Identify block as a memory block. */
5004 1.1 christos *mspace = 'M';
5005 1.1 christos ++mspace;
5006 1.1 christos /* Record address and size. */
5007 1.1 christos memcpy ((void *) mspace, (void *) &from, sizeof (from));
5008 1.1 christos mspace += sizeof (from);
5009 1.1 christos memcpy ((void *) mspace, (void *) &blocklen, sizeof (blocklen));
5010 1.1 christos mspace += sizeof (blocklen);
5011 1.1 christos /* Copy the string contents. */
5012 1.1 christos memcpy ((void *) mspace, (void *) buf, blocklen);
5013 1.1 christos remaining -= blocklen;
5014 1.1 christos from += blocklen;
5015 1.1 christos xfree (buf);
5016 1.1 christos }
5017 1.1 christos return 0;
5018 1.1 christos }
5019 1.1 christos
5020 1.1 christos /* Record the value of a trace state variable. */
5021 1.1 christos
5022 1.1 christos int
5023 1.1 christos agent_tsv_read (struct eval_agent_expr_context *ctx, int n)
5024 1.1 christos {
5025 1.1 christos unsigned char *vspace;
5026 1.1 christos LONGEST val;
5027 1.1 christos
5028 1.1 christos vspace = add_traceframe_block (ctx->tframe, ctx->tpoint,
5029 1.1 christos 1 + sizeof (n) + sizeof (LONGEST));
5030 1.1 christos if (vspace == NULL)
5031 1.1 christos return 1;
5032 1.1 christos /* Identify block as a variable. */
5033 1.1 christos *vspace = 'V';
5034 1.1 christos /* Record variable's number and value. */
5035 1.1 christos memcpy (vspace + 1, &n, sizeof (n));
5036 1.1 christos val = get_trace_state_variable_value (n);
5037 1.1 christos memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
5038 1.1 christos trace_debug ("Variable %d recorded", n);
5039 1.1 christos return 0;
5040 1.1 christos }
5041 1.1 christos
5042 1.1 christos #ifndef IN_PROCESS_AGENT
5043 1.1 christos
5044 1.1 christos /* Callback for traceframe_walk_blocks, used to find a given block
5045 1.1 christos type in a traceframe. */
5046 1.1 christos
5047 1.1 christos static int
5048 1.1 christos match_blocktype (char blocktype, unsigned char *dataptr, void *data)
5049 1.1 christos {
5050 1.1 christos char *wantedp = (char *) data;
5051 1.1 christos
5052 1.1 christos if (*wantedp == blocktype)
5053 1.1 christos return 1;
5054 1.1 christos
5055 1.1 christos return 0;
5056 1.1 christos }
5057 1.1 christos
5058 1.1 christos /* Walk over all traceframe blocks of the traceframe buffer starting
5059 1.1 christos at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5060 1.1 christos block found, passing in DATA unmodified. If CALLBACK returns true,
5061 1.1 christos this returns a pointer to where the block is found. Returns NULL
5062 1.1 christos if no callback call returned true, indicating that all blocks have
5063 1.1 christos been walked. */
5064 1.1 christos
5065 1.1 christos static unsigned char *
5066 1.1 christos traceframe_walk_blocks (unsigned char *database, unsigned int datasize,
5067 1.1 christos int tfnum,
5068 1.1 christos int (*callback) (char blocktype,
5069 1.1 christos unsigned char *dataptr,
5070 1.1 christos void *data),
5071 1.1 christos void *data)
5072 1.1 christos {
5073 1.1 christos unsigned char *dataptr;
5074 1.1 christos
5075 1.1 christos if (datasize == 0)
5076 1.1 christos {
5077 1.1 christos trace_debug ("traceframe %d has no data", tfnum);
5078 1.1 christos return NULL;
5079 1.1 christos }
5080 1.1 christos
5081 1.1 christos /* Iterate through a traceframe's blocks, looking for a block of the
5082 1.1 christos requested type. */
5083 1.1 christos for (dataptr = database;
5084 1.1 christos dataptr < database + datasize;
5085 1.1 christos /* nothing */)
5086 1.1 christos {
5087 1.1 christos char blocktype;
5088 1.1 christos unsigned short mlen;
5089 1.1 christos
5090 1.1 christos if (dataptr == trace_buffer_wrap)
5091 1.1 christos {
5092 1.1 christos /* Adjust to reflect wrapping part of the frame around to
5093 1.1 christos the beginning. */
5094 1.1 christos datasize = dataptr - database;
5095 1.1 christos dataptr = database = trace_buffer_lo;
5096 1.1 christos }
5097 1.1 christos
5098 1.1 christos blocktype = *dataptr++;
5099 1.1 christos
5100 1.1 christos if ((*callback) (blocktype, dataptr, data))
5101 1.1 christos return dataptr;
5102 1.1 christos
5103 1.1 christos switch (blocktype)
5104 1.1 christos {
5105 1.1 christos case 'R':
5106 1.1 christos /* Skip over the registers block. */
5107 1.1 christos dataptr += current_target_desc ()->registers_size;
5108 1.1 christos break;
5109 1.1 christos case 'M':
5110 1.1 christos /* Skip over the memory block. */
5111 1.1 christos dataptr += sizeof (CORE_ADDR);
5112 1.1 christos memcpy (&mlen, dataptr, sizeof (mlen));
5113 1.1 christos dataptr += (sizeof (mlen) + mlen);
5114 1.1 christos break;
5115 1.1 christos case 'V':
5116 1.1 christos /* Skip over the TSV block. */
5117 1.1 christos dataptr += (sizeof (int) + sizeof (LONGEST));
5118 1.1 christos break;
5119 1.1 christos case 'S':
5120 1.1 christos /* Skip over the static trace data block. */
5121 1.1 christos memcpy (&mlen, dataptr, sizeof (mlen));
5122 1.1 christos dataptr += (sizeof (mlen) + mlen);
5123 1.1 christos break;
5124 1.1 christos default:
5125 1.1 christos trace_debug ("traceframe %d has unknown block type 0x%x",
5126 1.1 christos tfnum, blocktype);
5127 1.1 christos return NULL;
5128 1.1 christos }
5129 1.1 christos }
5130 1.1 christos
5131 1.1 christos return NULL;
5132 1.1 christos }
5133 1.1 christos
5134 1.1 christos /* Look for the block of type TYPE_WANTED in the traceframe starting
5135 1.1 christos at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5136 1.1 christos number. */
5137 1.1 christos
5138 1.1 christos static unsigned char *
5139 1.1 christos traceframe_find_block_type (unsigned char *database, unsigned int datasize,
5140 1.1 christos int tfnum, char type_wanted)
5141 1.1 christos {
5142 1.1 christos return traceframe_walk_blocks (database, datasize, tfnum,
5143 1.1 christos match_blocktype, &type_wanted);
5144 1.1 christos }
5145 1.1 christos
5146 1.1 christos static unsigned char *
5147 1.1 christos traceframe_find_regblock (struct traceframe *tframe, int tfnum)
5148 1.1 christos {
5149 1.1 christos unsigned char *regblock;
5150 1.1 christos
5151 1.1 christos regblock = traceframe_find_block_type (tframe->data,
5152 1.1 christos tframe->data_size,
5153 1.1 christos tfnum, 'R');
5154 1.1 christos
5155 1.1 christos if (regblock == NULL)
5156 1.1 christos trace_debug ("traceframe %d has no register data", tfnum);
5157 1.1 christos
5158 1.1 christos return regblock;
5159 1.1 christos }
5160 1.1 christos
5161 1.1 christos /* Get registers from a traceframe. */
5162 1.1 christos
5163 1.1 christos int
5164 1.1 christos fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
5165 1.1 christos {
5166 1.1 christos unsigned char *dataptr;
5167 1.1 christos struct tracepoint *tpoint;
5168 1.1 christos struct traceframe *tframe;
5169 1.1 christos
5170 1.1 christos tframe = find_traceframe (tfnum);
5171 1.1 christos
5172 1.1 christos if (tframe == NULL)
5173 1.1 christos {
5174 1.1 christos trace_debug ("traceframe %d not found", tfnum);
5175 1.1 christos return 1;
5176 1.1 christos }
5177 1.1 christos
5178 1.1 christos dataptr = traceframe_find_regblock (tframe, tfnum);
5179 1.1 christos if (dataptr == NULL)
5180 1.1 christos {
5181 1.1 christos /* Mark registers unavailable. */
5182 1.1 christos supply_regblock (regcache, NULL);
5183 1.1 christos
5184 1.1 christos /* We can generally guess at a PC, although this will be
5185 1.1 christos misleading for while-stepping frames and multi-location
5186 1.1 christos tracepoints. */
5187 1.1 christos tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
5188 1.1 christos if (tpoint != NULL)
5189 1.1 christos regcache_write_pc (regcache, tpoint->address);
5190 1.1 christos }
5191 1.1 christos else
5192 1.1 christos supply_regblock (regcache, dataptr);
5193 1.1 christos
5194 1.1 christos return 0;
5195 1.1 christos }
5196 1.1 christos
5197 1.1 christos static CORE_ADDR
5198 1.1 christos traceframe_get_pc (struct traceframe *tframe)
5199 1.1 christos {
5200 1.1 christos struct regcache regcache;
5201 1.1 christos unsigned char *dataptr;
5202 1.1 christos const struct target_desc *tdesc = current_target_desc ();
5203 1.1 christos
5204 1.1 christos dataptr = traceframe_find_regblock (tframe, -1);
5205 1.1 christos if (dataptr == NULL)
5206 1.1 christos return 0;
5207 1.1 christos
5208 1.1 christos init_register_cache (®cache, tdesc, dataptr);
5209 1.1 christos return regcache_read_pc (®cache);
5210 1.1 christos }
5211 1.1 christos
5212 1.1 christos /* Read a requested block of memory from a trace frame. */
5213 1.1 christos
5214 1.1 christos int
5215 1.1 christos traceframe_read_mem (int tfnum, CORE_ADDR addr,
5216 1.1 christos unsigned char *buf, ULONGEST length,
5217 1.1 christos ULONGEST *nbytes)
5218 1.1 christos {
5219 1.1 christos struct traceframe *tframe;
5220 1.1 christos unsigned char *database, *dataptr;
5221 1.1 christos unsigned int datasize;
5222 1.1 christos CORE_ADDR maddr;
5223 1.1 christos unsigned short mlen;
5224 1.1 christos
5225 1.1 christos trace_debug ("traceframe_read_mem");
5226 1.1 christos
5227 1.1 christos tframe = find_traceframe (tfnum);
5228 1.1 christos
5229 1.1 christos if (!tframe)
5230 1.1 christos {
5231 1.1 christos trace_debug ("traceframe %d not found", tfnum);
5232 1.1 christos return 1;
5233 1.1 christos }
5234 1.1 christos
5235 1.1 christos datasize = tframe->data_size;
5236 1.1 christos database = dataptr = &tframe->data[0];
5237 1.1 christos
5238 1.1 christos /* Iterate through a traceframe's blocks, looking for memory. */
5239 1.1 christos while ((dataptr = traceframe_find_block_type (dataptr,
5240 1.1 christos datasize
5241 1.1 christos - (dataptr - database),
5242 1.1 christos tfnum, 'M')) != NULL)
5243 1.1 christos {
5244 1.1 christos memcpy (&maddr, dataptr, sizeof (maddr));
5245 1.1 christos dataptr += sizeof (maddr);
5246 1.1 christos memcpy (&mlen, dataptr, sizeof (mlen));
5247 1.1 christos dataptr += sizeof (mlen);
5248 1.1 christos trace_debug ("traceframe %d has %d bytes at %s",
5249 1.1 christos tfnum, mlen, paddress (maddr));
5250 1.1 christos
5251 1.1 christos /* If the block includes the first part of the desired range,
5252 1.1 christos return as much it has; GDB will re-request the remainder,
5253 1.1 christos which might be in a different block of this trace frame. */
5254 1.1 christos if (maddr <= addr && addr < (maddr + mlen))
5255 1.1 christos {
5256 1.1 christos ULONGEST amt = (maddr + mlen) - addr;
5257 1.1 christos if (amt > length)
5258 1.1 christos amt = length;
5259 1.1 christos
5260 1.1 christos memcpy (buf, dataptr + (addr - maddr), amt);
5261 1.1 christos *nbytes = amt;
5262 1.1 christos return 0;
5263 1.1 christos }
5264 1.1 christos
5265 1.1 christos /* Skip over this block. */
5266 1.1 christos dataptr += mlen;
5267 1.1 christos }
5268 1.1 christos
5269 1.1 christos trace_debug ("traceframe %d has no memory data for the desired region",
5270 1.1 christos tfnum);
5271 1.1 christos
5272 1.1 christos *nbytes = 0;
5273 1.1 christos return 0;
5274 1.1 christos }
5275 1.1 christos
5276 1.1 christos static int
5277 1.1 christos traceframe_read_tsv (int tsvnum, LONGEST *val)
5278 1.1 christos {
5279 1.1 christos client_state &cs = get_client_state ();
5280 1.1 christos int tfnum;
5281 1.1 christos struct traceframe *tframe;
5282 1.1 christos unsigned char *database, *dataptr;
5283 1.1 christos unsigned int datasize;
5284 1.1 christos int vnum;
5285 1.1 christos int found = 0;
5286 1.1 christos
5287 1.1 christos trace_debug ("traceframe_read_tsv");
5288 1.1 christos
5289 1.1 christos tfnum = cs.current_traceframe;
5290 1.1 christos
5291 1.1 christos if (tfnum < 0)
5292 1.1 christos {
5293 1.1 christos trace_debug ("no current traceframe");
5294 1.1 christos return 1;
5295 1.1 christos }
5296 1.1 christos
5297 1.1 christos tframe = find_traceframe (tfnum);
5298 1.1 christos
5299 1.1 christos if (tframe == NULL)
5300 1.1 christos {
5301 1.1 christos trace_debug ("traceframe %d not found", tfnum);
5302 1.1 christos return 1;
5303 1.1 christos }
5304 1.1 christos
5305 1.1 christos datasize = tframe->data_size;
5306 1.1 christos database = dataptr = &tframe->data[0];
5307 1.1 christos
5308 1.1 christos /* Iterate through a traceframe's blocks, looking for the last
5309 1.1 christos matched tsv. */
5310 1.1 christos while ((dataptr = traceframe_find_block_type (dataptr,
5311 1.1 christos datasize
5312 1.1 christos - (dataptr - database),
5313 1.1 christos tfnum, 'V')) != NULL)
5314 1.1 christos {
5315 1.1 christos memcpy (&vnum, dataptr, sizeof (vnum));
5316 1.1 christos dataptr += sizeof (vnum);
5317 1.1 christos
5318 1.1 christos trace_debug ("traceframe %d has variable %d", tfnum, vnum);
5319 1.1 christos
5320 1.1 christos /* Check that this is the variable we want. */
5321 1.1 christos if (tsvnum == vnum)
5322 1.1 christos {
5323 1.1 christos memcpy (val, dataptr, sizeof (*val));
5324 1.1 christos found = 1;
5325 1.1 christos }
5326 1.1 christos
5327 1.1 christos /* Skip over this block. */
5328 1.1 christos dataptr += sizeof (LONGEST);
5329 1.1 christos }
5330 1.1 christos
5331 1.1 christos if (!found)
5332 1.1 christos trace_debug ("traceframe %d has no data for variable %d",
5333 1.1 christos tfnum, tsvnum);
5334 1.1 christos return !found;
5335 1.1 christos }
5336 1.1 christos
5337 1.1 christos /* Read a requested block of static tracepoint data from a trace
5338 1.1 christos frame. */
5339 1.1 christos
5340 1.1 christos int
5341 1.1 christos traceframe_read_sdata (int tfnum, ULONGEST offset,
5342 1.1 christos unsigned char *buf, ULONGEST length,
5343 1.1 christos ULONGEST *nbytes)
5344 1.1 christos {
5345 1.1 christos struct traceframe *tframe;
5346 1.1 christos unsigned char *database, *dataptr;
5347 1.1 christos unsigned int datasize;
5348 1.1 christos unsigned short mlen;
5349 1.1 christos
5350 1.1 christos trace_debug ("traceframe_read_sdata");
5351 1.1 christos
5352 1.1 christos tframe = find_traceframe (tfnum);
5353 1.1 christos
5354 1.1 christos if (!tframe)
5355 1.1 christos {
5356 1.1 christos trace_debug ("traceframe %d not found", tfnum);
5357 1.1 christos return 1;
5358 1.1 christos }
5359 1.1 christos
5360 1.1 christos datasize = tframe->data_size;
5361 1.1 christos database = &tframe->data[0];
5362 1.1 christos
5363 1.1 christos /* Iterate through a traceframe's blocks, looking for static
5364 1.1 christos tracepoint data. */
5365 1.1 christos dataptr = traceframe_find_block_type (database, datasize,
5366 1.1 christos tfnum, 'S');
5367 1.1 christos if (dataptr != NULL)
5368 1.1 christos {
5369 1.1 christos memcpy (&mlen, dataptr, sizeof (mlen));
5370 1.1 christos dataptr += sizeof (mlen);
5371 1.1 christos if (offset < mlen)
5372 1.1 christos {
5373 1.1 christos if (offset + length > mlen)
5374 1.1 christos length = mlen - offset;
5375 1.1 christos
5376 1.1 christos memcpy (buf, dataptr, length);
5377 1.1 christos *nbytes = length;
5378 1.1 christos }
5379 1.1 christos else
5380 1.1 christos *nbytes = 0;
5381 1.1 christos return 0;
5382 1.1 christos }
5383 1.1 christos
5384 1.1 christos trace_debug ("traceframe %d has no static trace data", tfnum);
5385 1.1 christos
5386 1.1 christos *nbytes = 0;
5387 1.1 christos return 0;
5388 1.1 christos }
5389 1.1 christos
5390 1.1 christos /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5391 1.1 christos object. DATA is pointer to a struct buffer holding the
5392 1.1 christos traceframe-info object being built. */
5393 1.1 christos
5394 1.1 christos static int
5395 1.1 christos build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
5396 1.1 christos {
5397 1.1 christos struct buffer *buffer = (struct buffer *) data;
5398 1.1 christos
5399 1.1 christos switch (blocktype)
5400 1.1 christos {
5401 1.1 christos case 'M':
5402 1.1 christos {
5403 1.1 christos unsigned short mlen;
5404 1.1 christos CORE_ADDR maddr;
5405 1.1 christos
5406 1.1 christos memcpy (&maddr, dataptr, sizeof (maddr));
5407 1.1 christos dataptr += sizeof (maddr);
5408 1.1 christos memcpy (&mlen, dataptr, sizeof (mlen));
5409 1.1 christos dataptr += sizeof (mlen);
5410 1.1 christos buffer_xml_printf (buffer,
5411 1.1 christos "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5412 1.1 christos paddress (maddr), phex_nz (mlen, sizeof (mlen)));
5413 1.1 christos break;
5414 1.1 christos }
5415 1.1 christos case 'V':
5416 1.1 christos {
5417 1.1 christos int vnum;
5418 1.1 christos
5419 1.1 christos memcpy (&vnum, dataptr, sizeof (vnum));
5420 1.1 christos buffer_xml_printf (buffer, "<tvar id=\"%d\"/>\n", vnum);
5421 1.1 christos break;
5422 1.1 christos }
5423 1.1 christos case 'R':
5424 1.1 christos case 'S':
5425 1.1 christos {
5426 1.1 christos break;
5427 1.1 christos }
5428 1.1 christos default:
5429 1.1 christos warning ("Unhandled trace block type (%d) '%c ' "
5430 1.1 christos "while building trace frame info.",
5431 1.1 christos blocktype, blocktype);
5432 1.1 christos break;
5433 1.1 christos }
5434 1.1 christos
5435 1.1 christos return 0;
5436 1.1 christos }
5437 1.1 christos
5438 1.1 christos /* Build a traceframe-info object for traceframe number TFNUM into
5439 1.1 christos BUFFER. */
5440 1.1 christos
5441 1.1 christos int
5442 1.1 christos traceframe_read_info (int tfnum, struct buffer *buffer)
5443 1.1 christos {
5444 1.1 christos struct traceframe *tframe;
5445 1.1 christos
5446 1.1 christos trace_debug ("traceframe_read_info");
5447 1.1 christos
5448 1.1 christos tframe = find_traceframe (tfnum);
5449 1.1 christos
5450 1.1 christos if (!tframe)
5451 1.1 christos {
5452 1.1 christos trace_debug ("traceframe %d not found", tfnum);
5453 1.1 christos return 1;
5454 1.1 christos }
5455 1.1 christos
5456 1.1 christos buffer_grow_str (buffer, "<traceframe-info>\n");
5457 1.1 christos traceframe_walk_blocks (tframe->data, tframe->data_size,
5458 1.1 christos tfnum, build_traceframe_info_xml, buffer);
5459 1.1 christos buffer_grow_str0 (buffer, "</traceframe-info>\n");
5460 1.1 christos return 0;
5461 1.1 christos }
5462 1.1 christos
5463 1.1 christos /* Return the first fast tracepoint whose jump pad contains PC. */
5464 1.1 christos
5465 1.1 christos static struct tracepoint *
5466 1.1 christos fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
5467 1.1 christos {
5468 1.1 christos struct tracepoint *tpoint;
5469 1.1 christos
5470 1.1 christos for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5471 1.1 christos if (tpoint->type == fast_tracepoint)
5472 1.1 christos if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
5473 1.1 christos return tpoint;
5474 1.1 christos
5475 1.1 christos return NULL;
5476 1.1 christos }
5477 1.1 christos
5478 1.1 christos /* Return the first fast tracepoint whose trampoline contains PC. */
5479 1.1 christos
5480 1.1 christos static struct tracepoint *
5481 1.1 christos fast_tracepoint_from_trampoline_address (CORE_ADDR pc)
5482 1.1 christos {
5483 1.1 christos struct tracepoint *tpoint;
5484 1.1 christos
5485 1.1 christos for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5486 1.1 christos {
5487 1.1 christos if (tpoint->type == fast_tracepoint
5488 1.1 christos && tpoint->trampoline <= pc && pc < tpoint->trampoline_end)
5489 1.1 christos return tpoint;
5490 1.1 christos }
5491 1.1 christos
5492 1.1 christos return NULL;
5493 1.1 christos }
5494 1.1 christos
5495 1.1 christos /* Return GDBserver's tracepoint that matches the IP Agent's
5496 1.1 christos tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5497 1.1 christos address space. */
5498 1.1 christos
5499 1.1 christos static struct tracepoint *
5500 1.1 christos fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
5501 1.1 christos {
5502 1.1 christos struct tracepoint *tpoint;
5503 1.1 christos
5504 1.1 christos for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5505 1.1 christos if (tpoint->type == fast_tracepoint)
5506 1.1 christos if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
5507 1.1 christos return tpoint;
5508 1.1 christos
5509 1.1 christos return NULL;
5510 1.1 christos }
5511 1.1 christos
5512 1.1 christos #endif
5513 1.1 christos
5514 1.1 christos /* The type of the object that is used to synchronize fast tracepoint
5515 1.1 christos collection. */
5516 1.1 christos
5517 1.1 christos typedef struct collecting_t
5518 1.1 christos {
5519 1.1 christos /* The fast tracepoint number currently collecting. */
5520 1.1 christos uintptr_t tpoint;
5521 1.1 christos
5522 1.1 christos /* A number that GDBserver can use to identify the thread that is
5523 1.1 christos presently holding the collect lock. This need not (and usually
5524 1.1 christos is not) the thread id, as getting the current thread ID usually
5525 1.1 christos requires a system call, which we want to avoid like the plague.
5526 1.1 christos Usually this is thread's TCB, found in the TLS (pseudo-)
5527 1.1 christos register, which is readable with a single insn on several
5528 1.1 christos architectures. */
5529 1.1 christos uintptr_t thread_area;
5530 1.1 christos } collecting_t;
5531 1.1 christos
5532 1.1 christos #ifndef IN_PROCESS_AGENT
5533 1.1 christos
5534 1.1 christos void
5535 1.1 christos force_unlock_trace_buffer (void)
5536 1.1 christos {
5537 1.1 christos write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
5538 1.1 christos }
5539 1.1 christos
5540 1.1 christos /* Check if the thread identified by THREAD_AREA which is stopped at
5541 1.1 christos STOP_PC, is presently locking the fast tracepoint collection, and
5542 1.1 christos if so, gather some status of said collection. Returns 0 if the
5543 1.1 christos thread isn't collecting or in the jump pad at all. 1, if in the
5544 1.1 christos jump pad (or within gdb_collect) and hasn't executed the adjusted
5545 1.1 christos original insn yet (can set a breakpoint there and run to it). 2,
5546 1.1 christos if presently executing the adjusted original insn --- in which
5547 1.1 christos case, if we want to move the thread out of the jump pad, we need to
5548 1.1 christos single-step it until this function returns 0. */
5549 1.1 christos
5550 1.1 christos fast_tpoint_collect_result
5551 1.1 christos fast_tracepoint_collecting (CORE_ADDR thread_area,
5552 1.1 christos CORE_ADDR stop_pc,
5553 1.1 christos struct fast_tpoint_collect_status *status)
5554 1.1 christos {
5555 1.1 christos CORE_ADDR ipa_collecting;
5556 1.1 christos CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
5557 1.1 christos CORE_ADDR ipa_gdb_trampoline_buffer;
5558 1.1 christos CORE_ADDR ipa_gdb_trampoline_buffer_end;
5559 1.1 christos struct tracepoint *tpoint;
5560 1.1 christos int needs_breakpoint;
5561 1.1 christos
5562 1.1 christos /* The thread THREAD_AREA is either:
5563 1.1 christos
5564 1.1 christos 0. not collecting at all, not within the jump pad, or within
5565 1.1 christos gdb_collect or one of its callees.
5566 1.1 christos
5567 1.1 christos 1. in the jump pad and haven't reached gdb_collect
5568 1.1 christos
5569 1.1 christos 2. within gdb_collect (out of the jump pad) (collect is set)
5570 1.1 christos
5571 1.1 christos 3. we're in the jump pad, after gdb_collect having returned,
5572 1.1 christos possibly executing the adjusted insns.
5573 1.1 christos
5574 1.1 christos For cases 1 and 3, `collecting' may or not be set. The jump pad
5575 1.1 christos doesn't have any complicated jump logic, so we can tell if the
5576 1.1 christos thread is executing the adjust original insn or not by just
5577 1.1 christos matching STOP_PC with known jump pad addresses. If we it isn't
5578 1.1 christos yet executing the original insn, set a breakpoint there, and let
5579 1.1 christos the thread run to it, so to quickly step over a possible (many
5580 1.1 christos insns) gdb_collect call. Otherwise, or when the breakpoint is
5581 1.1 christos hit, only a few (small number of) insns are left to be executed
5582 1.1 christos in the jump pad. Single-step the thread until it leaves the
5583 1.1 christos jump pad. */
5584 1.1 christos
5585 1.1 christos again:
5586 1.1 christos tpoint = NULL;
5587 1.1 christos needs_breakpoint = 0;
5588 1.1 christos trace_debug ("fast_tracepoint_collecting");
5589 1.1 christos
5590 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
5591 1.1 christos &ipa_gdb_jump_pad_buffer))
5592 1.1 christos {
5593 1.1.1.2 christos internal_error ("error extracting `gdb_jump_pad_buffer'");
5594 1.1 christos }
5595 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
5596 1.1 christos &ipa_gdb_jump_pad_buffer_end))
5597 1.1 christos {
5598 1.1.1.2 christos internal_error ("error extracting `gdb_jump_pad_buffer_end'");
5599 1.1 christos }
5600 1.1 christos
5601 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
5602 1.1 christos &ipa_gdb_trampoline_buffer))
5603 1.1 christos {
5604 1.1.1.2 christos internal_error ("error extracting `gdb_trampoline_buffer'");
5605 1.1 christos }
5606 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
5607 1.1 christos &ipa_gdb_trampoline_buffer_end))
5608 1.1 christos {
5609 1.1.1.2 christos internal_error ("error extracting `gdb_trampoline_buffer_end'");
5610 1.1 christos }
5611 1.1 christos
5612 1.1 christos if (ipa_gdb_jump_pad_buffer <= stop_pc
5613 1.1 christos && stop_pc < ipa_gdb_jump_pad_buffer_end)
5614 1.1 christos {
5615 1.1 christos /* We can tell which tracepoint(s) the thread is collecting by
5616 1.1 christos matching the jump pad address back to the tracepoint. */
5617 1.1 christos tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
5618 1.1 christos if (tpoint == NULL)
5619 1.1 christos {
5620 1.1 christos warning ("in jump pad, but no matching tpoint?");
5621 1.1 christos return fast_tpoint_collect_result::not_collecting;
5622 1.1 christos }
5623 1.1 christos else
5624 1.1 christos {
5625 1.1 christos trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5626 1.1 christos "adj_insn(%s, %s)",
5627 1.1 christos tpoint->number, paddress (tpoint->address),
5628 1.1 christos paddress (tpoint->jump_pad),
5629 1.1 christos paddress (tpoint->jump_pad_end),
5630 1.1 christos paddress (tpoint->adjusted_insn_addr),
5631 1.1 christos paddress (tpoint->adjusted_insn_addr_end));
5632 1.1 christos }
5633 1.1 christos
5634 1.1 christos /* Definitely in the jump pad. May or may not need
5635 1.1 christos fast-exit-jump-pad breakpoint. */
5636 1.1 christos if (tpoint->jump_pad <= stop_pc
5637 1.1 christos && stop_pc < tpoint->adjusted_insn_addr)
5638 1.1 christos needs_breakpoint = 1;
5639 1.1 christos }
5640 1.1 christos else if (ipa_gdb_trampoline_buffer <= stop_pc
5641 1.1 christos && stop_pc < ipa_gdb_trampoline_buffer_end)
5642 1.1 christos {
5643 1.1 christos /* We can tell which tracepoint(s) the thread is collecting by
5644 1.1 christos matching the trampoline address back to the tracepoint. */
5645 1.1 christos tpoint = fast_tracepoint_from_trampoline_address (stop_pc);
5646 1.1 christos if (tpoint == NULL)
5647 1.1 christos {
5648 1.1 christos warning ("in trampoline, but no matching tpoint?");
5649 1.1 christos return fast_tpoint_collect_result::not_collecting;
5650 1.1 christos }
5651 1.1 christos else
5652 1.1 christos {
5653 1.1 christos trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5654 1.1 christos tpoint->number, paddress (tpoint->address),
5655 1.1 christos paddress (tpoint->trampoline),
5656 1.1 christos paddress (tpoint->trampoline_end));
5657 1.1 christos }
5658 1.1 christos
5659 1.1 christos /* Have not reached jump pad yet, but treat the trampoline as a
5660 1.1 christos part of the jump pad that is before the adjusted original
5661 1.1 christos instruction. */
5662 1.1 christos needs_breakpoint = 1;
5663 1.1 christos }
5664 1.1 christos else
5665 1.1 christos {
5666 1.1 christos collecting_t ipa_collecting_obj;
5667 1.1 christos
5668 1.1 christos /* If `collecting' is set/locked, then the THREAD_AREA thread
5669 1.1 christos may or not be the one holding the lock. We have to read the
5670 1.1 christos lock to find out. */
5671 1.1 christos
5672 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
5673 1.1 christos &ipa_collecting))
5674 1.1 christos {
5675 1.1 christos trace_debug ("fast_tracepoint_collecting:"
5676 1.1 christos " failed reading 'collecting' in the inferior");
5677 1.1 christos return fast_tpoint_collect_result::not_collecting;
5678 1.1 christos }
5679 1.1 christos
5680 1.1 christos if (!ipa_collecting)
5681 1.1 christos {
5682 1.1 christos trace_debug ("fast_tracepoint_collecting: not collecting"
5683 1.1 christos " (and nobody is).");
5684 1.1 christos return fast_tpoint_collect_result::not_collecting;
5685 1.1 christos }
5686 1.1 christos
5687 1.1 christos /* Some thread is collecting. Check which. */
5688 1.1 christos if (read_inferior_memory (ipa_collecting,
5689 1.1 christos (unsigned char *) &ipa_collecting_obj,
5690 1.1 christos sizeof (ipa_collecting_obj)) != 0)
5691 1.1 christos goto again;
5692 1.1 christos
5693 1.1 christos if (ipa_collecting_obj.thread_area != thread_area)
5694 1.1 christos {
5695 1.1 christos trace_debug ("fast_tracepoint_collecting: not collecting "
5696 1.1 christos "(another thread is)");
5697 1.1 christos return fast_tpoint_collect_result::not_collecting;
5698 1.1 christos }
5699 1.1 christos
5700 1.1 christos tpoint
5701 1.1 christos = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
5702 1.1 christos if (tpoint == NULL)
5703 1.1 christos {
5704 1.1 christos warning ("fast_tracepoint_collecting: collecting, "
5705 1.1 christos "but tpoint %s not found?",
5706 1.1 christos paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
5707 1.1 christos return fast_tpoint_collect_result::not_collecting;
5708 1.1 christos }
5709 1.1 christos
5710 1.1 christos /* The thread is within `gdb_collect', skip over the rest of
5711 1.1 christos fast tracepoint collection quickly using a breakpoint. */
5712 1.1 christos needs_breakpoint = 1;
5713 1.1 christos }
5714 1.1 christos
5715 1.1 christos /* The caller wants a bit of status detail. */
5716 1.1 christos if (status != NULL)
5717 1.1 christos {
5718 1.1 christos status->tpoint_num = tpoint->number;
5719 1.1 christos status->tpoint_addr = tpoint->address;
5720 1.1 christos status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
5721 1.1 christos status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
5722 1.1 christos }
5723 1.1 christos
5724 1.1 christos if (needs_breakpoint)
5725 1.1 christos {
5726 1.1 christos /* Hasn't executed the original instruction yet. Set breakpoint
5727 1.1 christos there, and wait till it's hit, then single-step until exiting
5728 1.1 christos the jump pad. */
5729 1.1 christos
5730 1.1 christos trace_debug ("\
5731 1.1 christos fast_tracepoint_collecting, returning continue-until-break at %s",
5732 1.1 christos paddress (tpoint->adjusted_insn_addr));
5733 1.1 christos
5734 1.1 christos return fast_tpoint_collect_result::before_insn; /* continue */
5735 1.1 christos }
5736 1.1 christos else
5737 1.1 christos {
5738 1.1 christos /* Just single-step until exiting the jump pad. */
5739 1.1 christos
5740 1.1 christos trace_debug ("fast_tracepoint_collecting, returning "
5741 1.1 christos "need-single-step (%s-%s)",
5742 1.1 christos paddress (tpoint->adjusted_insn_addr),
5743 1.1 christos paddress (tpoint->adjusted_insn_addr_end));
5744 1.1 christos
5745 1.1 christos return fast_tpoint_collect_result::at_insn; /* single-step */
5746 1.1 christos }
5747 1.1 christos }
5748 1.1 christos
5749 1.1 christos #endif
5750 1.1 christos
5751 1.1 christos #ifdef IN_PROCESS_AGENT
5752 1.1 christos
5753 1.1 christos /* The global fast tracepoint collect lock. Points to a collecting_t
5754 1.1 christos object built on the stack by the jump pad, if presently locked;
5755 1.1 christos NULL if it isn't locked. Note that this lock *must* be set while
5756 1.1 christos executing any *function other than the jump pad. See
5757 1.1 christos fast_tracepoint_collecting. */
5758 1.1 christos EXTERN_C_PUSH
5759 1.1 christos IP_AGENT_EXPORT_VAR collecting_t *collecting;
5760 1.1 christos EXTERN_C_POP
5761 1.1 christos
5762 1.1 christos /* This is needed for -Wmissing-declarations. */
5763 1.1 christos IP_AGENT_EXPORT_FUNC void gdb_collect (struct tracepoint *tpoint,
5764 1.1 christos unsigned char *regs);
5765 1.1 christos
5766 1.1 christos /* This routine, called from the jump pad (in asm) is designed to be
5767 1.1 christos called from the jump pads of fast tracepoints, thus it is on the
5768 1.1 christos critical path. */
5769 1.1 christos
5770 1.1 christos IP_AGENT_EXPORT_FUNC void
5771 1.1 christos gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
5772 1.1 christos {
5773 1.1 christos struct fast_tracepoint_ctx ctx;
5774 1.1 christos const struct target_desc *ipa_tdesc;
5775 1.1 christos
5776 1.1 christos /* Don't do anything until the trace run is completely set up. */
5777 1.1 christos if (!tracing)
5778 1.1 christos return;
5779 1.1 christos
5780 1.1 christos ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);
5781 1.1 christos ctx.base.type = fast_tracepoint;
5782 1.1 christos ctx.regs = regs;
5783 1.1 christos ctx.regcache_initted = 0;
5784 1.1 christos /* Wrap the regblock in a register cache (in the stack, we don't
5785 1.1 christos want to malloc here). */
5786 1.1 christos ctx.regspace = (unsigned char *) alloca (ipa_tdesc->registers_size);
5787 1.1 christos if (ctx.regspace == NULL)
5788 1.1 christos {
5789 1.1 christos trace_debug ("Trace buffer block allocation failed, skipping");
5790 1.1 christos return;
5791 1.1 christos }
5792 1.1 christos
5793 1.1 christos for (ctx.tpoint = tpoint;
5794 1.1 christos ctx.tpoint != NULL && ctx.tpoint->address == tpoint->address;
5795 1.1 christos ctx.tpoint = ctx.tpoint->next)
5796 1.1 christos {
5797 1.1 christos if (!ctx.tpoint->enabled)
5798 1.1 christos continue;
5799 1.1 christos
5800 1.1 christos /* Multiple tracepoints of different types, such as fast tracepoint and
5801 1.1 christos static tracepoint, can be set at the same address. */
5802 1.1 christos if (ctx.tpoint->type != tpoint->type)
5803 1.1 christos continue;
5804 1.1 christos
5805 1.1 christos /* Test the condition if present, and collect if true. */
5806 1.1 christos if (ctx.tpoint->cond == NULL
5807 1.1 christos || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5808 1.1 christos ctx.tpoint))
5809 1.1 christos {
5810 1.1 christos collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5811 1.1 christos ctx.tpoint->address, ctx.tpoint);
5812 1.1 christos
5813 1.1 christos /* Note that this will cause original insns to be written back
5814 1.1 christos to where we jumped from, but that's OK because we're jumping
5815 1.1 christos back to the next whole instruction. This will go badly if
5816 1.1 christos instruction restoration is not atomic though. */
5817 1.1 christos if (stopping_tracepoint
5818 1.1 christos || trace_buffer_is_full
5819 1.1 christos || expr_eval_result != expr_eval_no_error)
5820 1.1 christos {
5821 1.1 christos stop_tracing ();
5822 1.1 christos break;
5823 1.1 christos }
5824 1.1 christos }
5825 1.1 christos else
5826 1.1 christos {
5827 1.1 christos /* If there was a condition and it evaluated to false, the only
5828 1.1 christos way we would stop tracing is if there was an error during
5829 1.1 christos condition expression evaluation. */
5830 1.1 christos if (expr_eval_result != expr_eval_no_error)
5831 1.1 christos {
5832 1.1 christos stop_tracing ();
5833 1.1 christos break;
5834 1.1 christos }
5835 1.1 christos }
5836 1.1 christos }
5837 1.1 christos }
5838 1.1 christos
5839 1.1 christos /* These global variables points to the corresponding functions. This is
5840 1.1 christos necessary on powerpc64, where asking for function symbol address from gdb
5841 1.1 christos results in returning the actual code pointer, instead of the descriptor
5842 1.1 christos pointer. */
5843 1.1 christos
5844 1.1 christos typedef void (*gdb_collect_ptr_type) (struct tracepoint *, unsigned char *);
5845 1.1 christos typedef ULONGEST (*get_raw_reg_ptr_type) (const unsigned char *, int);
5846 1.1 christos typedef LONGEST (*get_trace_state_variable_value_ptr_type) (int);
5847 1.1 christos typedef void (*set_trace_state_variable_value_ptr_type) (int, LONGEST);
5848 1.1 christos
5849 1.1 christos EXTERN_C_PUSH
5850 1.1 christos IP_AGENT_EXPORT_VAR gdb_collect_ptr_type gdb_collect_ptr = gdb_collect;
5851 1.1 christos IP_AGENT_EXPORT_VAR get_raw_reg_ptr_type get_raw_reg_ptr = get_raw_reg;
5852 1.1 christos IP_AGENT_EXPORT_VAR get_trace_state_variable_value_ptr_type
5853 1.1 christos get_trace_state_variable_value_ptr = get_trace_state_variable_value;
5854 1.1 christos IP_AGENT_EXPORT_VAR set_trace_state_variable_value_ptr_type
5855 1.1 christos set_trace_state_variable_value_ptr = set_trace_state_variable_value;
5856 1.1 christos EXTERN_C_POP
5857 1.1 christos
5858 1.1 christos #endif
5859 1.1 christos
5860 1.1 christos #ifndef IN_PROCESS_AGENT
5861 1.1 christos
5862 1.1 christos CORE_ADDR
5863 1.1 christos get_raw_reg_func_addr (void)
5864 1.1 christos {
5865 1.1 christos CORE_ADDR res;
5866 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_get_raw_reg_ptr, &res))
5867 1.1 christos {
5868 1.1 christos error ("error extracting get_raw_reg_ptr");
5869 1.1 christos return 0;
5870 1.1 christos }
5871 1.1 christos return res;
5872 1.1 christos }
5873 1.1 christos
5874 1.1 christos CORE_ADDR
5875 1.1 christos get_get_tsv_func_addr (void)
5876 1.1 christos {
5877 1.1 christos CORE_ADDR res;
5878 1.1 christos if (read_inferior_data_pointer (
5879 1.1 christos ipa_sym_addrs.addr_get_trace_state_variable_value_ptr, &res))
5880 1.1 christos {
5881 1.1 christos error ("error extracting get_trace_state_variable_value_ptr");
5882 1.1 christos return 0;
5883 1.1 christos }
5884 1.1 christos return res;
5885 1.1 christos }
5886 1.1 christos
5887 1.1 christos CORE_ADDR
5888 1.1 christos get_set_tsv_func_addr (void)
5889 1.1 christos {
5890 1.1 christos CORE_ADDR res;
5891 1.1 christos if (read_inferior_data_pointer (
5892 1.1 christos ipa_sym_addrs.addr_set_trace_state_variable_value_ptr, &res))
5893 1.1 christos {
5894 1.1 christos error ("error extracting set_trace_state_variable_value_ptr");
5895 1.1 christos return 0;
5896 1.1 christos }
5897 1.1 christos return res;
5898 1.1 christos }
5899 1.1 christos
5900 1.1 christos static void
5901 1.1 christos compile_tracepoint_condition (struct tracepoint *tpoint,
5902 1.1 christos CORE_ADDR *jump_entry)
5903 1.1 christos {
5904 1.1 christos CORE_ADDR entry_point = *jump_entry;
5905 1.1 christos enum eval_result_type err;
5906 1.1 christos
5907 1.1 christos trace_debug ("Starting condition compilation for tracepoint %d\n",
5908 1.1 christos tpoint->number);
5909 1.1 christos
5910 1.1 christos /* Initialize the global pointer to the code being built. */
5911 1.1 christos current_insn_ptr = *jump_entry;
5912 1.1 christos
5913 1.1 christos emit_prologue ();
5914 1.1 christos
5915 1.1 christos err = compile_bytecodes (tpoint->cond);
5916 1.1 christos
5917 1.1 christos if (err == expr_eval_no_error)
5918 1.1 christos {
5919 1.1 christos emit_epilogue ();
5920 1.1 christos
5921 1.1 christos /* Record the beginning of the compiled code. */
5922 1.1 christos tpoint->compiled_cond = entry_point;
5923 1.1 christos
5924 1.1 christos trace_debug ("Condition compilation for tracepoint %d complete\n",
5925 1.1 christos tpoint->number);
5926 1.1 christos }
5927 1.1 christos else
5928 1.1 christos {
5929 1.1 christos /* Leave the unfinished code in situ, but don't point to it. */
5930 1.1 christos
5931 1.1 christos tpoint->compiled_cond = 0;
5932 1.1 christos
5933 1.1 christos trace_debug ("Condition compilation for tracepoint %d failed, "
5934 1.1 christos "error code %d",
5935 1.1 christos tpoint->number, err);
5936 1.1 christos }
5937 1.1 christos
5938 1.1 christos /* Update the code pointer passed in. Note that we do this even if
5939 1.1 christos the compile fails, so that we can look at the partial results
5940 1.1 christos instead of letting them be overwritten. */
5941 1.1 christos *jump_entry = current_insn_ptr;
5942 1.1 christos
5943 1.1 christos /* Leave a gap, to aid dump decipherment. */
5944 1.1 christos *jump_entry += 16;
5945 1.1 christos }
5946 1.1 christos
5947 1.1 christos /* The base pointer of the IPA's heap. This is the only memory the
5948 1.1 christos IPA is allowed to use. The IPA should _not_ call the inferior's
5949 1.1 christos `malloc' during operation. That'd be slow, and, most importantly,
5950 1.1 christos it may not be safe. We may be collecting a tracepoint in a signal
5951 1.1 christos handler, for example. */
5952 1.1 christos static CORE_ADDR target_tp_heap;
5953 1.1 christos
5954 1.1 christos /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5955 1.1 christos to 8 bytes. */
5956 1.1 christos
5957 1.1 christos static CORE_ADDR
5958 1.1 christos target_malloc (ULONGEST size)
5959 1.1 christos {
5960 1.1 christos CORE_ADDR ptr;
5961 1.1 christos
5962 1.1 christos if (target_tp_heap == 0)
5963 1.1 christos {
5964 1.1 christos /* We have the pointer *address*, need what it points to. */
5965 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
5966 1.1 christos &target_tp_heap))
5967 1.1 christos {
5968 1.1.1.2 christos internal_error ("couldn't get target heap head pointer");
5969 1.1 christos }
5970 1.1 christos }
5971 1.1 christos
5972 1.1 christos ptr = target_tp_heap;
5973 1.1 christos target_tp_heap += size;
5974 1.1 christos
5975 1.1 christos /* Pad to 8-byte alignment. */
5976 1.1 christos target_tp_heap = ((target_tp_heap + 7) & ~0x7);
5977 1.1 christos
5978 1.1 christos return ptr;
5979 1.1 christos }
5980 1.1 christos
5981 1.1 christos static CORE_ADDR
5982 1.1 christos download_agent_expr (struct agent_expr *expr)
5983 1.1 christos {
5984 1.1 christos CORE_ADDR expr_addr;
5985 1.1 christos CORE_ADDR expr_bytes;
5986 1.1 christos
5987 1.1 christos expr_addr = target_malloc (sizeof (*expr));
5988 1.1 christos target_write_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
5989 1.1 christos
5990 1.1 christos expr_bytes = target_malloc (expr->length);
5991 1.1 christos write_inferior_data_pointer (expr_addr + offsetof (struct agent_expr, bytes),
5992 1.1 christos expr_bytes);
5993 1.1 christos target_write_memory (expr_bytes, expr->bytes, expr->length);
5994 1.1 christos
5995 1.1 christos return expr_addr;
5996 1.1 christos }
5997 1.1 christos
5998 1.1 christos /* Align V up to N bits. */
5999 1.1 christos #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
6000 1.1 christos
6001 1.1 christos /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
6002 1.1 christos
6003 1.1 christos static void
6004 1.1 christos download_tracepoint_1 (struct tracepoint *tpoint)
6005 1.1 christos {
6006 1.1 christos struct tracepoint target_tracepoint;
6007 1.1 christos CORE_ADDR tpptr = 0;
6008 1.1 christos
6009 1.1 christos gdb_assert (tpoint->type == fast_tracepoint
6010 1.1 christos || tpoint->type == static_tracepoint);
6011 1.1 christos
6012 1.1 christos if (tpoint->cond != NULL && target_emit_ops () != NULL)
6013 1.1 christos {
6014 1.1 christos CORE_ADDR jentry, jump_entry;
6015 1.1 christos
6016 1.1 christos jentry = jump_entry = get_jump_space_head ();
6017 1.1 christos
6018 1.1 christos if (tpoint->cond != NULL)
6019 1.1 christos {
6020 1.1 christos /* Pad to 8-byte alignment. (needed?) */
6021 1.1 christos /* Actually this should be left for the target to
6022 1.1 christos decide. */
6023 1.1 christos jentry = UALIGN (jentry, 8);
6024 1.1 christos
6025 1.1 christos compile_tracepoint_condition (tpoint, &jentry);
6026 1.1 christos }
6027 1.1 christos
6028 1.1 christos /* Pad to 8-byte alignment. */
6029 1.1 christos jentry = UALIGN (jentry, 8);
6030 1.1 christos claim_jump_space (jentry - jump_entry);
6031 1.1 christos }
6032 1.1 christos
6033 1.1 christos target_tracepoint = *tpoint;
6034 1.1 christos
6035 1.1 christos tpptr = target_malloc (sizeof (*tpoint));
6036 1.1 christos tpoint->obj_addr_on_target = tpptr;
6037 1.1 christos
6038 1.1 christos /* Write the whole object. We'll fix up its pointers in a bit.
6039 1.1 christos Assume no next for now. This is fixed up above on the next
6040 1.1 christos iteration, if there's any. */
6041 1.1 christos target_tracepoint.next = NULL;
6042 1.1 christos /* Need to clear this here too, since we're downloading the
6043 1.1 christos tracepoints before clearing our own copy. */
6044 1.1 christos target_tracepoint.hit_count = 0;
6045 1.1 christos
6046 1.1 christos target_write_memory (tpptr, (unsigned char *) &target_tracepoint,
6047 1.1 christos sizeof (target_tracepoint));
6048 1.1 christos
6049 1.1 christos if (tpoint->cond)
6050 1.1 christos write_inferior_data_pointer (tpptr
6051 1.1 christos + offsetof (struct tracepoint, cond),
6052 1.1 christos download_agent_expr (tpoint->cond));
6053 1.1 christos
6054 1.1 christos if (tpoint->numactions)
6055 1.1 christos {
6056 1.1 christos int i;
6057 1.1 christos CORE_ADDR actions_array;
6058 1.1 christos
6059 1.1 christos /* The pointers array. */
6060 1.1 christos actions_array
6061 1.1 christos = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
6062 1.1 christos write_inferior_data_pointer (tpptr + offsetof (struct tracepoint,
6063 1.1 christos actions),
6064 1.1 christos actions_array);
6065 1.1 christos
6066 1.1 christos /* Now for each pointer, download the action. */
6067 1.1 christos for (i = 0; i < tpoint->numactions; i++)
6068 1.1 christos {
6069 1.1 christos struct tracepoint_action *action = tpoint->actions[i];
6070 1.1 christos CORE_ADDR ipa_action = tracepoint_action_download (action);
6071 1.1 christos
6072 1.1 christos if (ipa_action != 0)
6073 1.1 christos write_inferior_data_pointer (actions_array
6074 1.1 christos + i * sizeof (*tpoint->actions),
6075 1.1 christos ipa_action);
6076 1.1 christos }
6077 1.1 christos }
6078 1.1 christos }
6079 1.1 christos
6080 1.1 christos #define IPA_PROTO_FAST_TRACE_FLAG 0
6081 1.1 christos #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
6082 1.1 christos #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
6083 1.1 christos #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
6084 1.1 christos #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
6085 1.1 christos
6086 1.1 christos /* Send a command to agent to download and install tracepoint TPOINT. */
6087 1.1 christos
6088 1.1 christos static int
6089 1.1 christos tracepoint_send_agent (struct tracepoint *tpoint)
6090 1.1 christos {
6091 1.1 christos char buf[IPA_CMD_BUF_SIZE];
6092 1.1 christos char *p;
6093 1.1 christos int i, ret;
6094 1.1 christos
6095 1.1 christos p = buf;
6096 1.1 christos strcpy (p, "FastTrace:");
6097 1.1 christos p += 10;
6098 1.1 christos
6099 1.1 christos COPY_FIELD_TO_BUF (p, tpoint, number);
6100 1.1 christos COPY_FIELD_TO_BUF (p, tpoint, address);
6101 1.1 christos COPY_FIELD_TO_BUF (p, tpoint, type);
6102 1.1 christos COPY_FIELD_TO_BUF (p, tpoint, enabled);
6103 1.1 christos COPY_FIELD_TO_BUF (p, tpoint, step_count);
6104 1.1 christos COPY_FIELD_TO_BUF (p, tpoint, pass_count);
6105 1.1 christos COPY_FIELD_TO_BUF (p, tpoint, numactions);
6106 1.1 christos COPY_FIELD_TO_BUF (p, tpoint, hit_count);
6107 1.1 christos COPY_FIELD_TO_BUF (p, tpoint, traceframe_usage);
6108 1.1 christos COPY_FIELD_TO_BUF (p, tpoint, compiled_cond);
6109 1.1 christos COPY_FIELD_TO_BUF (p, tpoint, orig_size);
6110 1.1 christos
6111 1.1 christos /* condition */
6112 1.1 christos p = agent_expr_send (p, tpoint->cond);
6113 1.1 christos
6114 1.1 christos /* tracepoint_action */
6115 1.1 christos for (i = 0; i < tpoint->numactions; i++)
6116 1.1 christos {
6117 1.1 christos struct tracepoint_action *action = tpoint->actions[i];
6118 1.1 christos
6119 1.1 christos p[0] = action->type;
6120 1.1 christos p = tracepoint_action_send (&p[1], action);
6121 1.1 christos }
6122 1.1 christos
6123 1.1 christos get_jump_space_head ();
6124 1.1 christos /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6125 1.1 christos agent can use jump pad from it. */
6126 1.1 christos if (tpoint->type == fast_tracepoint)
6127 1.1 christos {
6128 1.1 christos memcpy (p, &gdb_jump_pad_head, 8);
6129 1.1 christos p += 8;
6130 1.1 christos }
6131 1.1 christos
6132 1.1 christos ret = run_inferior_command (buf, (int) (ptrdiff_t) (p - buf));
6133 1.1 christos if (ret)
6134 1.1 christos return ret;
6135 1.1 christos
6136 1.1 christos if (!startswith (buf, "OK"))
6137 1.1 christos return 1;
6138 1.1 christos
6139 1.1 christos /* The value of tracepoint's target address is stored in BUF. */
6140 1.1 christos memcpy (&tpoint->obj_addr_on_target,
6141 1.1 christos &buf[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET], 8);
6142 1.1 christos
6143 1.1 christos if (tpoint->type == fast_tracepoint)
6144 1.1 christos {
6145 1.1 christos unsigned char *insn
6146 1.1 christos = (unsigned char *) &buf[IPA_PROTO_FAST_TRACE_FJUMP_INSN];
6147 1.1 christos int fjump_size;
6148 1.1 christos
6149 1.1 christos trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6150 1.1 christos (unsigned int) tpoint->obj_addr_on_target,
6151 1.1 christos (unsigned int) gdb_jump_pad_head);
6152 1.1 christos
6153 1.1 christos memcpy (&gdb_jump_pad_head, &buf[IPA_PROTO_FAST_TRACE_JUMP_PAD], 8);
6154 1.1 christos
6155 1.1 christos /* This has been done in agent. We should also set up record for it. */
6156 1.1 christos memcpy (&fjump_size, &buf[IPA_PROTO_FAST_TRACE_FJUMP_SIZE], 4);
6157 1.1 christos /* Wire it in. */
6158 1.1 christos tpoint->handle
6159 1.1 christos = set_fast_tracepoint_jump (tpoint->address, insn, fjump_size);
6160 1.1 christos }
6161 1.1 christos
6162 1.1 christos return 0;
6163 1.1 christos }
6164 1.1 christos
6165 1.1 christos static void
6166 1.1 christos download_tracepoint (struct tracepoint *tpoint)
6167 1.1 christos {
6168 1.1 christos struct tracepoint *tp, *tp_prev;
6169 1.1 christos
6170 1.1 christos if (tpoint->type != fast_tracepoint
6171 1.1 christos && tpoint->type != static_tracepoint)
6172 1.1 christos return;
6173 1.1 christos
6174 1.1 christos download_tracepoint_1 (tpoint);
6175 1.1 christos
6176 1.1 christos /* Find the previous entry of TPOINT, which is fast tracepoint or
6177 1.1 christos static tracepoint. */
6178 1.1 christos tp_prev = NULL;
6179 1.1 christos for (tp = tracepoints; tp != tpoint; tp = tp->next)
6180 1.1 christos {
6181 1.1 christos if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
6182 1.1 christos tp_prev = tp;
6183 1.1 christos }
6184 1.1 christos
6185 1.1 christos if (tp_prev)
6186 1.1 christos {
6187 1.1 christos CORE_ADDR tp_prev_target_next_addr;
6188 1.1 christos
6189 1.1 christos /* Insert TPOINT after TP_PREV in IPA. */
6190 1.1 christos if (read_inferior_data_pointer (tp_prev->obj_addr_on_target
6191 1.1 christos + offsetof (struct tracepoint, next),
6192 1.1 christos &tp_prev_target_next_addr))
6193 1.1 christos {
6194 1.1.1.2 christos internal_error ("error reading `tp_prev->next'");
6195 1.1 christos }
6196 1.1 christos
6197 1.1 christos /* tpoint->next = tp_prev->next */
6198 1.1 christos write_inferior_data_pointer (tpoint->obj_addr_on_target
6199 1.1 christos + offsetof (struct tracepoint, next),
6200 1.1 christos tp_prev_target_next_addr);
6201 1.1 christos /* tp_prev->next = tpoint */
6202 1.1 christos write_inferior_data_pointer (tp_prev->obj_addr_on_target
6203 1.1 christos + offsetof (struct tracepoint, next),
6204 1.1 christos tpoint->obj_addr_on_target);
6205 1.1 christos }
6206 1.1 christos else
6207 1.1 christos /* First object in list, set the head pointer in the
6208 1.1 christos inferior. */
6209 1.1 christos write_inferior_data_pointer (ipa_sym_addrs.addr_tracepoints,
6210 1.1 christos tpoint->obj_addr_on_target);
6211 1.1 christos
6212 1.1 christos }
6213 1.1 christos
6214 1.1 christos static void
6215 1.1 christos download_trace_state_variables (void)
6216 1.1 christos {
6217 1.1 christos CORE_ADDR ptr = 0, prev_ptr = 0;
6218 1.1 christos struct trace_state_variable *tsv;
6219 1.1 christos
6220 1.1 christos /* Start out empty. */
6221 1.1 christos write_inferior_data_pointer (ipa_sym_addrs.addr_trace_state_variables, 0);
6222 1.1 christos
6223 1.1 christos for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
6224 1.1 christos {
6225 1.1 christos struct trace_state_variable target_tsv;
6226 1.1 christos
6227 1.1 christos /* TSV's with a getter have been initialized equally in both the
6228 1.1 christos inferior and GDBserver. Skip them. */
6229 1.1 christos if (tsv->getter != NULL)
6230 1.1 christos continue;
6231 1.1 christos
6232 1.1 christos target_tsv = *tsv;
6233 1.1 christos
6234 1.1 christos prev_ptr = ptr;
6235 1.1 christos ptr = target_malloc (sizeof (*tsv));
6236 1.1 christos
6237 1.1 christos if (tsv == trace_state_variables)
6238 1.1 christos {
6239 1.1 christos /* First object in list, set the head pointer in the
6240 1.1 christos inferior. */
6241 1.1 christos
6242 1.1 christos write_inferior_data_pointer (ipa_sym_addrs.addr_trace_state_variables,
6243 1.1 christos ptr);
6244 1.1 christos }
6245 1.1 christos else
6246 1.1 christos {
6247 1.1 christos write_inferior_data_pointer (prev_ptr
6248 1.1 christos + offsetof (struct trace_state_variable,
6249 1.1 christos next),
6250 1.1 christos ptr);
6251 1.1 christos }
6252 1.1 christos
6253 1.1 christos /* Write the whole object. We'll fix up its pointers in a bit.
6254 1.1 christos Assume no next, fixup when needed. */
6255 1.1 christos target_tsv.next = NULL;
6256 1.1 christos
6257 1.1 christos target_write_memory (ptr, (unsigned char *) &target_tsv,
6258 1.1 christos sizeof (target_tsv));
6259 1.1 christos
6260 1.1 christos if (tsv->name != NULL)
6261 1.1 christos {
6262 1.1 christos size_t size = strlen (tsv->name) + 1;
6263 1.1 christos CORE_ADDR name_addr = target_malloc (size);
6264 1.1 christos target_write_memory (name_addr,
6265 1.1 christos (unsigned char *) tsv->name, size);
6266 1.1 christos write_inferior_data_pointer (ptr
6267 1.1 christos + offsetof (struct trace_state_variable,
6268 1.1 christos name),
6269 1.1 christos name_addr);
6270 1.1 christos }
6271 1.1 christos
6272 1.1 christos gdb_assert (tsv->getter == NULL);
6273 1.1 christos }
6274 1.1 christos
6275 1.1 christos if (prev_ptr != 0)
6276 1.1 christos {
6277 1.1 christos /* Fixup the next pointer in the last item in the list. */
6278 1.1 christos write_inferior_data_pointer (prev_ptr
6279 1.1 christos + offsetof (struct trace_state_variable,
6280 1.1 christos next), 0);
6281 1.1 christos }
6282 1.1 christos }
6283 1.1 christos
6284 1.1 christos /* Upload complete trace frames out of the IP Agent's trace buffer
6285 1.1 christos into GDBserver's trace buffer. This always uploads either all or
6286 1.1 christos no trace frames. This is the counter part of
6287 1.1 christos `trace_alloc_trace_buffer'. See its description of the atomic
6288 1.1 christos syncing mechanism. */
6289 1.1 christos
6290 1.1 christos static void
6291 1.1 christos upload_fast_traceframes (void)
6292 1.1 christos {
6293 1.1 christos unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
6294 1.1 christos unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
6295 1.1 christos CORE_ADDR tf;
6296 1.1 christos struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
6297 1.1 christos unsigned int curr_tbctrl_idx;
6298 1.1 christos unsigned int ipa_trace_buffer_ctrl_curr;
6299 1.1 christos unsigned int ipa_trace_buffer_ctrl_curr_old;
6300 1.1 christos CORE_ADDR ipa_trace_buffer_ctrl_addr;
6301 1.1 christos struct breakpoint *about_to_request_buffer_space_bkpt;
6302 1.1 christos CORE_ADDR ipa_trace_buffer_lo;
6303 1.1 christos CORE_ADDR ipa_trace_buffer_hi;
6304 1.1 christos
6305 1.1 christos if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
6306 1.1 christos &ipa_traceframe_read_count_racy))
6307 1.1 christos {
6308 1.1 christos /* This will happen in most targets if the current thread is
6309 1.1 christos running. */
6310 1.1 christos return;
6311 1.1 christos }
6312 1.1 christos
6313 1.1 christos if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
6314 1.1 christos &ipa_traceframe_write_count_racy))
6315 1.1 christos return;
6316 1.1 christos
6317 1.1 christos trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6318 1.1 christos ipa_traceframe_write_count_racy
6319 1.1 christos - ipa_traceframe_read_count_racy,
6320 1.1 christos ipa_traceframe_write_count_racy,
6321 1.1 christos ipa_traceframe_read_count_racy);
6322 1.1 christos
6323 1.1 christos if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
6324 1.1 christos return;
6325 1.1 christos
6326 1.1 christos about_to_request_buffer_space_bkpt
6327 1.1 christos = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
6328 1.1 christos NULL);
6329 1.1 christos
6330 1.1 christos if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
6331 1.1 christos &ipa_trace_buffer_ctrl_curr))
6332 1.1 christos return;
6333 1.1 christos
6334 1.1 christos ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
6335 1.1 christos
6336 1.1 christos curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
6337 1.1 christos
6338 1.1 christos {
6339 1.1 christos unsigned int prev, counter;
6340 1.1 christos
6341 1.1 christos /* Update the token, with new counters, and the GDBserver stamp
6342 1.1 christos bit. Alway reuse the current TBC index. */
6343 1.1 christos prev = ipa_trace_buffer_ctrl_curr & GDBSERVER_FLUSH_COUNT_MASK_CURR;
6344 1.1 christos counter = (prev + 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR;
6345 1.1 christos
6346 1.1 christos ipa_trace_buffer_ctrl_curr = (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6347 1.1 christos | (prev << 12)
6348 1.1 christos | counter
6349 1.1 christos | curr_tbctrl_idx);
6350 1.1 christos }
6351 1.1 christos
6352 1.1 christos if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
6353 1.1 christos ipa_trace_buffer_ctrl_curr))
6354 1.1 christos return;
6355 1.1 christos
6356 1.1 christos trace_debug ("Lib: Committed %08x -> %08x",
6357 1.1 christos ipa_trace_buffer_ctrl_curr_old,
6358 1.1 christos ipa_trace_buffer_ctrl_curr);
6359 1.1 christos
6360 1.1 christos /* Re-read these, now that we've installed the
6361 1.1 christos `about_to_request_buffer_space' breakpoint/lock. A thread could
6362 1.1 christos have finished a traceframe between the last read of these
6363 1.1 christos counters and setting the breakpoint above. If we start
6364 1.1 christos uploading, we never want to leave this function with
6365 1.1 christos traceframe_read_count != 0, otherwise, GDBserver could end up
6366 1.1 christos incrementing the counter tokens more than once (due to event loop
6367 1.1 christos nesting), which would break the IP agent's "effective" detection
6368 1.1 christos (see trace_alloc_trace_buffer). */
6369 1.1 christos if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
6370 1.1 christos &ipa_traceframe_read_count))
6371 1.1 christos return;
6372 1.1 christos if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
6373 1.1 christos &ipa_traceframe_write_count))
6374 1.1 christos return;
6375 1.1 christos
6376 1.1 christos if (debug_threads)
6377 1.1 christos {
6378 1.1 christos trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6379 1.1 christos ipa_traceframe_write_count - ipa_traceframe_read_count,
6380 1.1 christos ipa_traceframe_write_count, ipa_traceframe_read_count);
6381 1.1 christos
6382 1.1 christos if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
6383 1.1 christos || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
6384 1.1 christos trace_debug ("note that ipa_traceframe_count's parts changed");
6385 1.1 christos }
6386 1.1 christos
6387 1.1 christos /* Get the address of the current TBC object (the IP agent has an
6388 1.1 christos array of 3 such objects). The index is stored in the TBC
6389 1.1 christos token. */
6390 1.1 christos ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
6391 1.1 christos ipa_trace_buffer_ctrl_addr
6392 1.1 christos += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
6393 1.1 christos
6394 1.1 christos if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
6395 1.1 christos (unsigned char *) &ipa_trace_buffer_ctrl,
6396 1.1 christos sizeof (struct ipa_trace_buffer_control)))
6397 1.1 christos return;
6398 1.1 christos
6399 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
6400 1.1 christos &ipa_trace_buffer_lo))
6401 1.1 christos return;
6402 1.1 christos if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
6403 1.1 christos &ipa_trace_buffer_hi))
6404 1.1 christos return;
6405 1.1 christos
6406 1.1 christos /* Offsets are easier to grok for debugging than raw addresses,
6407 1.1 christos especially for the small trace buffer sizes that are useful for
6408 1.1 christos testing. */
6409 1.1 christos trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6410 1.1 christos "endfree=%d wrap=%d hi=%d",
6411 1.1 christos curr_tbctrl_idx,
6412 1.1 christos (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6413 1.1 christos (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6414 1.1 christos (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
6415 1.1 christos (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6416 1.1 christos (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6417 1.1 christos
6418 1.1 christos /* Note that the IPA's buffer is always circular. */
6419 1.1 christos
6420 1.1 christos #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6421 1.1 christos
6422 1.1 christos #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6423 1.1 christos ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6424 1.1 christos
6425 1.1 christos #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6426 1.1 christos (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6427 1.1 christos - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6428 1.1 christos ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6429 1.1 christos : 0))
6430 1.1 christos
6431 1.1 christos tf = IPA_FIRST_TRACEFRAME ();
6432 1.1 christos
6433 1.1 christos while (ipa_traceframe_write_count - ipa_traceframe_read_count)
6434 1.1 christos {
6435 1.1 christos struct tracepoint *tpoint;
6436 1.1 christos struct traceframe *tframe;
6437 1.1 christos unsigned char *block;
6438 1.1 christos struct traceframe ipa_tframe;
6439 1.1 christos
6440 1.1 christos if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
6441 1.1 christos offsetof (struct traceframe, data)))
6442 1.1 christos error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
6443 1.1 christos
6444 1.1 christos if (ipa_tframe.tpnum == 0)
6445 1.1 christos {
6446 1.1.1.2 christos internal_error ("Uploading: No (more) fast traceframes, but"
6447 1.1 christos " ipa_traceframe_count == %u??\n",
6448 1.1 christos ipa_traceframe_write_count
6449 1.1 christos - ipa_traceframe_read_count);
6450 1.1 christos }
6451 1.1 christos
6452 1.1 christos /* Note that this will be incorrect for multi-location
6453 1.1 christos tracepoints... */
6454 1.1 christos tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
6455 1.1 christos
6456 1.1 christos tframe = add_traceframe (tpoint);
6457 1.1 christos if (tframe == NULL)
6458 1.1 christos {
6459 1.1 christos trace_buffer_is_full = 1;
6460 1.1 christos trace_debug ("Uploading: trace buffer is full");
6461 1.1 christos }
6462 1.1 christos else
6463 1.1 christos {
6464 1.1 christos /* Copy the whole set of blocks in one go for now. FIXME:
6465 1.1 christos split this in smaller blocks. */
6466 1.1 christos block = add_traceframe_block (tframe, tpoint,
6467 1.1 christos ipa_tframe.data_size);
6468 1.1 christos if (block != NULL)
6469 1.1 christos {
6470 1.1 christos if (read_inferior_memory (tf
6471 1.1 christos + offsetof (struct traceframe, data),
6472 1.1 christos block, ipa_tframe.data_size))
6473 1.1 christos error ("Uploading: Couldn't read traceframe data at %s\n",
6474 1.1 christos paddress (tf + offsetof (struct traceframe, data)));
6475 1.1 christos }
6476 1.1 christos
6477 1.1 christos trace_debug ("Uploading: traceframe didn't fit");
6478 1.1 christos finish_traceframe (tframe);
6479 1.1 christos }
6480 1.1 christos
6481 1.1 christos tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
6482 1.1 christos
6483 1.1 christos /* If we freed the traceframe that wrapped around, go back
6484 1.1 christos to the non-wrap case. */
6485 1.1 christos if (tf < ipa_trace_buffer_ctrl.start)
6486 1.1 christos {
6487 1.1 christos trace_debug ("Lib: Discarding past the wraparound");
6488 1.1 christos ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6489 1.1 christos }
6490 1.1 christos ipa_trace_buffer_ctrl.start = tf;
6491 1.1 christos ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
6492 1.1 christos ++ipa_traceframe_read_count;
6493 1.1 christos
6494 1.1 christos if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
6495 1.1 christos && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
6496 1.1 christos {
6497 1.1 christos trace_debug ("Lib: buffer is fully empty. "
6498 1.1 christos "Trace buffer [%d] start=%d free=%d endfree=%d",
6499 1.1 christos curr_tbctrl_idx,
6500 1.1 christos (int) (ipa_trace_buffer_ctrl.start
6501 1.1 christos - ipa_trace_buffer_lo),
6502 1.1 christos (int) (ipa_trace_buffer_ctrl.free
6503 1.1 christos - ipa_trace_buffer_lo),
6504 1.1 christos (int) (ipa_trace_buffer_ctrl.end_free
6505 1.1 christos - ipa_trace_buffer_lo));
6506 1.1 christos
6507 1.1 christos ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
6508 1.1 christos ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
6509 1.1 christos ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
6510 1.1 christos ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6511 1.1 christos }
6512 1.1 christos
6513 1.1 christos trace_debug ("Uploaded a traceframe\n"
6514 1.1 christos "Lib: Trace buffer [%d] start=%d free=%d "
6515 1.1 christos "endfree=%d wrap=%d hi=%d",
6516 1.1 christos curr_tbctrl_idx,
6517 1.1 christos (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6518 1.1 christos (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6519 1.1 christos (int) (ipa_trace_buffer_ctrl.end_free
6520 1.1 christos - ipa_trace_buffer_lo),
6521 1.1 christos (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6522 1.1 christos (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6523 1.1 christos }
6524 1.1 christos
6525 1.1 christos if (target_write_memory (ipa_trace_buffer_ctrl_addr,
6526 1.1 christos (unsigned char *) &ipa_trace_buffer_ctrl,
6527 1.1 christos sizeof (struct ipa_trace_buffer_control)))
6528 1.1 christos return;
6529 1.1 christos
6530 1.1 christos write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
6531 1.1 christos ipa_traceframe_read_count);
6532 1.1 christos
6533 1.1 christos trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
6534 1.1 christos
6535 1.1 christos target_pause_all (true);
6536 1.1 christos
6537 1.1 christos delete_breakpoint (about_to_request_buffer_space_bkpt);
6538 1.1 christos about_to_request_buffer_space_bkpt = NULL;
6539 1.1 christos
6540 1.1 christos target_unpause_all (true);
6541 1.1 christos
6542 1.1 christos if (trace_buffer_is_full)
6543 1.1 christos stop_tracing ();
6544 1.1 christos }
6545 1.1 christos #endif
6546 1.1 christos
6547 1.1 christos #ifdef IN_PROCESS_AGENT
6548 1.1 christos
6549 1.1 christos IP_AGENT_EXPORT_VAR int ust_loaded;
6550 1.1 christos IP_AGENT_EXPORT_VAR char cmd_buf[IPA_CMD_BUF_SIZE];
6551 1.1 christos
6552 1.1 christos #ifdef HAVE_UST
6553 1.1 christos
6554 1.1 christos /* Static tracepoints. */
6555 1.1 christos
6556 1.1 christos /* UST puts a "struct tracepoint" in the global namespace, which
6557 1.1 christos conflicts with our tracepoint. Arguably, being a library, it
6558 1.1 christos shouldn't take ownership of such a generic name. We work around it
6559 1.1 christos here. */
6560 1.1 christos #define tracepoint ust_tracepoint
6561 1.1 christos #include <ust/ust.h>
6562 1.1 christos #undef tracepoint
6563 1.1 christos
6564 1.1 christos extern int serialize_to_text (char *outbuf, int bufsize,
6565 1.1 christos const char *fmt, va_list ap);
6566 1.1 christos
6567 1.1 christos #define GDB_PROBE_NAME "gdb"
6568 1.1 christos
6569 1.1 christos /* We dynamically search for the UST symbols instead of linking them
6570 1.1 christos in. This lets the user decide if the application uses static
6571 1.1 christos tracepoints, instead of always pulling libust.so in. This vector
6572 1.1 christos holds pointers to all functions we care about. */
6573 1.1 christos
6574 1.1 christos static struct
6575 1.1 christos {
6576 1.1 christos int (*serialize_to_text) (char *outbuf, int bufsize,
6577 1.1 christos const char *fmt, va_list ap);
6578 1.1 christos
6579 1.1 christos int (*ltt_probe_register) (struct ltt_available_probe *pdata);
6580 1.1 christos int (*ltt_probe_unregister) (struct ltt_available_probe *pdata);
6581 1.1 christos
6582 1.1 christos int (*ltt_marker_connect) (const char *channel, const char *mname,
6583 1.1 christos const char *pname);
6584 1.1 christos int (*ltt_marker_disconnect) (const char *channel, const char *mname,
6585 1.1 christos const char *pname);
6586 1.1 christos
6587 1.1 christos void (*marker_iter_start) (struct marker_iter *iter);
6588 1.1 christos void (*marker_iter_next) (struct marker_iter *iter);
6589 1.1 christos void (*marker_iter_stop) (struct marker_iter *iter);
6590 1.1 christos void (*marker_iter_reset) (struct marker_iter *iter);
6591 1.1 christos } ust_ops;
6592 1.1 christos
6593 1.1 christos #include <dlfcn.h>
6594 1.1 christos
6595 1.1 christos /* Cast through typeof to catch incompatible API changes. Since UST
6596 1.1 christos only builds with gcc, we can freely use gcc extensions here
6597 1.1 christos too. */
6598 1.1 christos #define GET_UST_SYM(SYM) \
6599 1.1 christos do \
6600 1.1 christos { \
6601 1.1 christos if (ust_ops.SYM == NULL) \
6602 1.1 christos ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6603 1.1 christos if (ust_ops.SYM == NULL) \
6604 1.1 christos return 0; \
6605 1.1 christos } while (0)
6606 1.1 christos
6607 1.1 christos #define USTF(SYM) ust_ops.SYM
6608 1.1 christos
6609 1.1 christos /* Get pointers to all libust.so functions we care about. */
6610 1.1 christos
6611 1.1 christos static int
6612 1.1 christos dlsym_ust (void)
6613 1.1 christos {
6614 1.1 christos GET_UST_SYM (serialize_to_text);
6615 1.1 christos
6616 1.1 christos GET_UST_SYM (ltt_probe_register);
6617 1.1 christos GET_UST_SYM (ltt_probe_unregister);
6618 1.1 christos GET_UST_SYM (ltt_marker_connect);
6619 1.1 christos GET_UST_SYM (ltt_marker_disconnect);
6620 1.1 christos
6621 1.1 christos GET_UST_SYM (marker_iter_start);
6622 1.1 christos GET_UST_SYM (marker_iter_next);
6623 1.1 christos GET_UST_SYM (marker_iter_stop);
6624 1.1 christos GET_UST_SYM (marker_iter_reset);
6625 1.1 christos
6626 1.1 christos ust_loaded = 1;
6627 1.1 christos return 1;
6628 1.1 christos }
6629 1.1 christos
6630 1.1 christos /* Given an UST marker, return the matching gdb static tracepoint.
6631 1.1 christos The match is done by address. */
6632 1.1 christos
6633 1.1 christos static struct tracepoint *
6634 1.1 christos ust_marker_to_static_tracepoint (const struct marker *mdata)
6635 1.1 christos {
6636 1.1 christos struct tracepoint *tpoint;
6637 1.1 christos
6638 1.1 christos for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
6639 1.1 christos {
6640 1.1 christos if (tpoint->type != static_tracepoint)
6641 1.1 christos continue;
6642 1.1 christos
6643 1.1 christos if (tpoint->address == (uintptr_t) mdata->location)
6644 1.1 christos return tpoint;
6645 1.1 christos }
6646 1.1 christos
6647 1.1 christos return NULL;
6648 1.1 christos }
6649 1.1 christos
6650 1.1 christos /* The probe function we install on lttng/ust markers. Whenever a
6651 1.1 christos probed ust marker is hit, this function is called. This is similar
6652 1.1 christos to gdb_collect, only for static tracepoints, instead of fast
6653 1.1 christos tracepoints. */
6654 1.1 christos
6655 1.1 christos static void
6656 1.1 christos gdb_probe (const struct marker *mdata, void *probe_private,
6657 1.1 christos struct registers *regs, void *call_private,
6658 1.1 christos const char *fmt, va_list *args)
6659 1.1 christos {
6660 1.1 christos struct tracepoint *tpoint;
6661 1.1 christos struct static_tracepoint_ctx ctx;
6662 1.1 christos const struct target_desc *ipa_tdesc;
6663 1.1 christos
6664 1.1 christos /* Don't do anything until the trace run is completely set up. */
6665 1.1 christos if (!tracing)
6666 1.1 christos {
6667 1.1 christos trace_debug ("gdb_probe: not tracing\n");
6668 1.1 christos return;
6669 1.1 christos }
6670 1.1 christos
6671 1.1 christos ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);
6672 1.1 christos ctx.base.type = static_tracepoint;
6673 1.1 christos ctx.regcache_initted = 0;
6674 1.1 christos ctx.regs = regs;
6675 1.1 christos ctx.fmt = fmt;
6676 1.1 christos ctx.args = args;
6677 1.1 christos
6678 1.1 christos /* Wrap the regblock in a register cache (in the stack, we don't
6679 1.1 christos want to malloc here). */
6680 1.1 christos ctx.regspace = alloca (ipa_tdesc->registers_size);
6681 1.1 christos if (ctx.regspace == NULL)
6682 1.1 christos {
6683 1.1 christos trace_debug ("Trace buffer block allocation failed, skipping");
6684 1.1 christos return;
6685 1.1 christos }
6686 1.1 christos
6687 1.1 christos tpoint = ust_marker_to_static_tracepoint (mdata);
6688 1.1 christos if (tpoint == NULL)
6689 1.1 christos {
6690 1.1 christos trace_debug ("gdb_probe: marker not known: "
6691 1.1 christos "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6692 1.1 christos mdata->location, mdata->channel,
6693 1.1 christos mdata->name, mdata->format);
6694 1.1 christos return;
6695 1.1 christos }
6696 1.1 christos
6697 1.1 christos if (!tpoint->enabled)
6698 1.1 christos {
6699 1.1 christos trace_debug ("gdb_probe: tracepoint disabled");
6700 1.1 christos return;
6701 1.1 christos }
6702 1.1 christos
6703 1.1 christos ctx.tpoint = tpoint;
6704 1.1 christos
6705 1.1 christos trace_debug ("gdb_probe: collecting marker: "
6706 1.1 christos "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6707 1.1 christos mdata->location, mdata->channel,
6708 1.1 christos mdata->name, mdata->format);
6709 1.1 christos
6710 1.1 christos /* Test the condition if present, and collect if true. */
6711 1.1 christos if (tpoint->cond == NULL
6712 1.1 christos || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6713 1.1 christos tpoint))
6714 1.1 christos {
6715 1.1 christos collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6716 1.1 christos tpoint->address, tpoint);
6717 1.1 christos
6718 1.1 christos if (stopping_tracepoint
6719 1.1 christos || trace_buffer_is_full
6720 1.1 christos || expr_eval_result != expr_eval_no_error)
6721 1.1 christos stop_tracing ();
6722 1.1 christos }
6723 1.1 christos else
6724 1.1 christos {
6725 1.1 christos /* If there was a condition and it evaluated to false, the only
6726 1.1 christos way we would stop tracing is if there was an error during
6727 1.1 christos condition expression evaluation. */
6728 1.1 christos if (expr_eval_result != expr_eval_no_error)
6729 1.1 christos stop_tracing ();
6730 1.1 christos }
6731 1.1 christos }
6732 1.1 christos
6733 1.1 christos /* Called if the gdb static tracepoint requested collecting "$_sdata",
6734 1.1 christos static tracepoint string data. This is a string passed to the
6735 1.1 christos tracing library by the user, at the time of the tracepoint marker
6736 1.1 christos call. E.g., in the UST marker call:
6737 1.1 christos
6738 1.1 christos trace_mark (ust, bar33, "str %s", "FOOBAZ");
6739 1.1 christos
6740 1.1 christos the collected data is "str FOOBAZ".
6741 1.1 christos */
6742 1.1 christos
6743 1.1 christos static void
6744 1.1 christos collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
6745 1.1 christos struct traceframe *tframe)
6746 1.1 christos {
6747 1.1 christos struct static_tracepoint_ctx *umd = (struct static_tracepoint_ctx *) ctx;
6748 1.1 christos unsigned char *bufspace;
6749 1.1 christos int size;
6750 1.1 christos va_list copy;
6751 1.1 christos unsigned short blocklen;
6752 1.1 christos
6753 1.1 christos if (umd == NULL)
6754 1.1 christos {
6755 1.1 christos trace_debug ("Wanted to collect static trace data, "
6756 1.1 christos "but there's no static trace data");
6757 1.1 christos return;
6758 1.1 christos }
6759 1.1 christos
6760 1.1 christos va_copy (copy, *umd->args);
6761 1.1 christos size = USTF(serialize_to_text) (NULL, 0, umd->fmt, copy);
6762 1.1 christos va_end (copy);
6763 1.1 christos
6764 1.1 christos trace_debug ("Want to collect ust data");
6765 1.1 christos
6766 1.1 christos /* 'S' + size + string */
6767 1.1 christos bufspace = add_traceframe_block (tframe, umd->tpoint,
6768 1.1 christos 1 + sizeof (blocklen) + size + 1);
6769 1.1 christos if (bufspace == NULL)
6770 1.1 christos {
6771 1.1 christos trace_debug ("Trace buffer block allocation failed, skipping");
6772 1.1 christos return;
6773 1.1 christos }
6774 1.1 christos
6775 1.1 christos /* Identify a static trace data block. */
6776 1.1 christos *bufspace = 'S';
6777 1.1 christos
6778 1.1 christos blocklen = size + 1;
6779 1.1 christos memcpy (bufspace + 1, &blocklen, sizeof (blocklen));
6780 1.1 christos
6781 1.1 christos va_copy (copy, *umd->args);
6782 1.1 christos USTF(serialize_to_text) ((char *) bufspace + 1 + sizeof (blocklen),
6783 1.1 christos size + 1, umd->fmt, copy);
6784 1.1 christos va_end (copy);
6785 1.1 christos
6786 1.1 christos trace_debug ("Storing static tracepoint data in regblock: %s",
6787 1.1 christos bufspace + 1 + sizeof (blocklen));
6788 1.1 christos }
6789 1.1 christos
6790 1.1 christos /* The probe to register with lttng/ust. */
6791 1.1 christos static struct ltt_available_probe gdb_ust_probe =
6792 1.1 christos {
6793 1.1 christos GDB_PROBE_NAME,
6794 1.1 christos NULL,
6795 1.1 christos gdb_probe,
6796 1.1 christos };
6797 1.1 christos
6798 1.1 christos #endif /* HAVE_UST */
6799 1.1 christos #endif /* IN_PROCESS_AGENT */
6800 1.1 christos
6801 1.1 christos #ifndef IN_PROCESS_AGENT
6802 1.1 christos
6803 1.1 christos /* Ask the in-process agent to run a command. Since we don't want to
6804 1.1 christos have to handle the IPA hitting breakpoints while running the
6805 1.1 christos command, we pause all threads, remove all breakpoints, and then set
6806 1.1 christos the helper thread re-running. We communicate with the helper
6807 1.1 christos thread by means of direct memory xfering, and a socket for
6808 1.1 christos synchronization. */
6809 1.1 christos
6810 1.1 christos static int
6811 1.1 christos run_inferior_command (char *cmd, int len)
6812 1.1 christos {
6813 1.1 christos int err = -1;
6814 1.1 christos int pid = current_ptid.pid ();
6815 1.1 christos
6816 1.1 christos trace_debug ("run_inferior_command: running: %s", cmd);
6817 1.1 christos
6818 1.1 christos target_pause_all (false);
6819 1.1 christos uninsert_all_breakpoints ();
6820 1.1 christos
6821 1.1 christos err = agent_run_command (pid, (const char *) cmd, len);
6822 1.1 christos
6823 1.1 christos reinsert_all_breakpoints ();
6824 1.1 christos target_unpause_all (false);
6825 1.1 christos
6826 1.1 christos return err;
6827 1.1 christos }
6828 1.1 christos
6829 1.1 christos #else /* !IN_PROCESS_AGENT */
6830 1.1 christos
6831 1.1 christos #include <sys/socket.h>
6832 1.1 christos #include <sys/un.h>
6833 1.1 christos
6834 1.1 christos #ifndef UNIX_PATH_MAX
6835 1.1 christos #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6836 1.1 christos #endif
6837 1.1 christos
6838 1.1 christos /* Where we put the socked used for synchronization. */
6839 1.1 christos #define SOCK_DIR P_tmpdir
6840 1.1 christos
6841 1.1 christos /* Thread ID of the helper thread. GDBserver reads this to know which
6842 1.1 christos is the help thread. This is an LWP id on Linux. */
6843 1.1 christos EXTERN_C_PUSH
6844 1.1 christos IP_AGENT_EXPORT_VAR int helper_thread_id;
6845 1.1 christos EXTERN_C_POP
6846 1.1 christos
6847 1.1 christos static int
6848 1.1 christos init_named_socket (const char *name)
6849 1.1 christos {
6850 1.1 christos int result, fd;
6851 1.1 christos struct sockaddr_un addr;
6852 1.1 christos
6853 1.1 christos result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
6854 1.1 christos if (result == -1)
6855 1.1 christos {
6856 1.1 christos warning ("socket creation failed: %s", safe_strerror (errno));
6857 1.1 christos return -1;
6858 1.1 christos }
6859 1.1 christos
6860 1.1 christos addr.sun_family = AF_UNIX;
6861 1.1 christos
6862 1.1.1.2 christos if (strlen (name) >= ARRAY_SIZE (addr.sun_path))
6863 1.1.1.2 christos {
6864 1.1.1.2 christos warning ("socket name too long for sockaddr_un::sun_path field: %s", name);
6865 1.1.1.2 christos return -1;
6866 1.1.1.2 christos }
6867 1.1.1.2 christos
6868 1.1.1.2 christos strcpy (addr.sun_path, name);
6869 1.1 christos
6870 1.1 christos result = access (name, F_OK);
6871 1.1 christos if (result == 0)
6872 1.1 christos {
6873 1.1 christos /* File exists. */
6874 1.1 christos result = unlink (name);
6875 1.1 christos if (result == -1)
6876 1.1 christos {
6877 1.1 christos warning ("unlink failed: %s", safe_strerror (errno));
6878 1.1 christos close (fd);
6879 1.1 christos return -1;
6880 1.1 christos }
6881 1.1 christos warning ("socket %s already exists; overwriting", name);
6882 1.1 christos }
6883 1.1 christos
6884 1.1 christos result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
6885 1.1 christos if (result == -1)
6886 1.1 christos {
6887 1.1 christos warning ("bind failed: %s", safe_strerror (errno));
6888 1.1 christos close (fd);
6889 1.1 christos return -1;
6890 1.1 christos }
6891 1.1 christos
6892 1.1 christos result = listen (fd, 1);
6893 1.1 christos if (result == -1)
6894 1.1 christos {
6895 1.1 christos warning ("listen: %s", safe_strerror (errno));
6896 1.1 christos close (fd);
6897 1.1 christos return -1;
6898 1.1 christos }
6899 1.1 christos
6900 1.1 christos return fd;
6901 1.1 christos }
6902 1.1 christos
6903 1.1 christos static char agent_socket_name[UNIX_PATH_MAX];
6904 1.1 christos
6905 1.1 christos static int
6906 1.1 christos gdb_agent_socket_init (void)
6907 1.1 christos {
6908 1.1 christos int result, fd;
6909 1.1 christos
6910 1.1.1.2 christos result = snprintf (agent_socket_name, UNIX_PATH_MAX, "%s/gdb_ust%d",
6911 1.1.1.2 christos SOCK_DIR, getpid ());
6912 1.1 christos if (result >= UNIX_PATH_MAX)
6913 1.1 christos {
6914 1.1 christos trace_debug ("string overflow allocating socket name");
6915 1.1 christos return -1;
6916 1.1 christos }
6917 1.1 christos
6918 1.1 christos fd = init_named_socket (agent_socket_name);
6919 1.1 christos if (fd < 0)
6920 1.1 christos warning ("Error initializing named socket (%s) for communication with the "
6921 1.1 christos "ust helper thread. Check that directory exists and that it "
6922 1.1 christos "is writable.", agent_socket_name);
6923 1.1 christos
6924 1.1 christos return fd;
6925 1.1 christos }
6926 1.1 christos
6927 1.1 christos #ifdef HAVE_UST
6928 1.1 christos
6929 1.1 christos /* The next marker to be returned on a qTsSTM command. */
6930 1.1 christos static const struct marker *next_st;
6931 1.1 christos
6932 1.1 christos /* Returns the first known marker. */
6933 1.1 christos
6934 1.1 christos struct marker *
6935 1.1 christos first_marker (void)
6936 1.1 christos {
6937 1.1 christos struct marker_iter iter;
6938 1.1 christos
6939 1.1 christos USTF(marker_iter_reset) (&iter);
6940 1.1 christos USTF(marker_iter_start) (&iter);
6941 1.1 christos
6942 1.1 christos return iter.marker;
6943 1.1 christos }
6944 1.1 christos
6945 1.1 christos /* Returns the marker following M. */
6946 1.1 christos
6947 1.1 christos const struct marker *
6948 1.1 christos next_marker (const struct marker *m)
6949 1.1 christos {
6950 1.1 christos struct marker_iter iter;
6951 1.1 christos
6952 1.1 christos USTF(marker_iter_reset) (&iter);
6953 1.1 christos USTF(marker_iter_start) (&iter);
6954 1.1 christos
6955 1.1 christos for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
6956 1.1 christos {
6957 1.1 christos if (iter.marker == m)
6958 1.1 christos {
6959 1.1 christos USTF(marker_iter_next) (&iter);
6960 1.1 christos return iter.marker;
6961 1.1 christos }
6962 1.1 christos }
6963 1.1 christos
6964 1.1 christos return NULL;
6965 1.1 christos }
6966 1.1 christos
6967 1.1 christos /* Return an hexstr version of the STR C string, fit for sending to
6968 1.1 christos GDB. */
6969 1.1 christos
6970 1.1 christos static char *
6971 1.1 christos cstr_to_hexstr (const char *str)
6972 1.1 christos {
6973 1.1 christos int len = strlen (str);
6974 1.1 christos char *hexstr = xmalloc (len * 2 + 1);
6975 1.1 christos bin2hex ((gdb_byte *) str, hexstr, len);
6976 1.1 christos return hexstr;
6977 1.1 christos }
6978 1.1 christos
6979 1.1 christos /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6980 1.1 christos packets. */
6981 1.1 christos
6982 1.1 christos static void
6983 1.1 christos response_ust_marker (char *packet, const struct marker *st)
6984 1.1 christos {
6985 1.1 christos char *strid, *format, *tmp;
6986 1.1 christos
6987 1.1 christos next_st = next_marker (st);
6988 1.1 christos
6989 1.1 christos tmp = xmalloc (strlen (st->channel) + 1 +
6990 1.1 christos strlen (st->name) + 1);
6991 1.1 christos sprintf (tmp, "%s/%s", st->channel, st->name);
6992 1.1 christos
6993 1.1 christos strid = cstr_to_hexstr (tmp);
6994 1.1 christos free (tmp);
6995 1.1 christos
6996 1.1 christos format = cstr_to_hexstr (st->format);
6997 1.1 christos
6998 1.1 christos sprintf (packet, "m%s:%s:%s",
6999 1.1 christos paddress ((uintptr_t) st->location),
7000 1.1 christos strid,
7001 1.1 christos format);
7002 1.1 christos
7003 1.1 christos free (strid);
7004 1.1 christos free (format);
7005 1.1 christos }
7006 1.1 christos
7007 1.1 christos /* Return the first static tracepoint, and initialize the state
7008 1.1 christos machine that will iterate through all the static tracepoints. */
7009 1.1 christos
7010 1.1 christos static void
7011 1.1 christos cmd_qtfstm (char *packet)
7012 1.1 christos {
7013 1.1 christos trace_debug ("Returning first trace state variable definition");
7014 1.1 christos
7015 1.1 christos if (first_marker ())
7016 1.1 christos response_ust_marker (packet, first_marker ());
7017 1.1 christos else
7018 1.1 christos strcpy (packet, "l");
7019 1.1 christos }
7020 1.1 christos
7021 1.1 christos /* Return additional trace state variable definitions. */
7022 1.1 christos
7023 1.1 christos static void
7024 1.1 christos cmd_qtsstm (char *packet)
7025 1.1 christos {
7026 1.1 christos trace_debug ("Returning static tracepoint");
7027 1.1 christos
7028 1.1 christos if (next_st)
7029 1.1 christos response_ust_marker (packet, next_st);
7030 1.1 christos else
7031 1.1 christos strcpy (packet, "l");
7032 1.1 christos }
7033 1.1 christos
7034 1.1 christos /* Disconnect the GDB probe from a marker at a given address. */
7035 1.1 christos
7036 1.1 christos static void
7037 1.1 christos unprobe_marker_at (char *packet)
7038 1.1 christos {
7039 1.1 christos char *p = packet;
7040 1.1 christos ULONGEST address;
7041 1.1 christos struct marker_iter iter;
7042 1.1 christos
7043 1.1 christos p += sizeof ("unprobe_marker_at:") - 1;
7044 1.1 christos
7045 1.1 christos p = unpack_varlen_hex (p, &address);
7046 1.1 christos
7047 1.1 christos USTF(marker_iter_reset) (&iter);
7048 1.1 christos USTF(marker_iter_start) (&iter);
7049 1.1 christos for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
7050 1.1 christos if ((uintptr_t ) iter.marker->location == address)
7051 1.1 christos {
7052 1.1 christos int result;
7053 1.1 christos
7054 1.1 christos result = USTF(ltt_marker_disconnect) (iter.marker->channel,
7055 1.1 christos iter.marker->name,
7056 1.1 christos GDB_PROBE_NAME);
7057 1.1 christos if (result < 0)
7058 1.1 christos warning ("could not disable marker %s/%s",
7059 1.1 christos iter.marker->channel, iter.marker->name);
7060 1.1 christos break;
7061 1.1 christos }
7062 1.1 christos }
7063 1.1 christos
7064 1.1 christos /* Connect the GDB probe to a marker at a given address. */
7065 1.1 christos
7066 1.1 christos static int
7067 1.1 christos probe_marker_at (char *packet)
7068 1.1 christos {
7069 1.1 christos char *p = packet;
7070 1.1 christos ULONGEST address;
7071 1.1 christos struct marker_iter iter;
7072 1.1 christos struct marker *m;
7073 1.1 christos
7074 1.1 christos p += sizeof ("probe_marker_at:") - 1;
7075 1.1 christos
7076 1.1 christos p = unpack_varlen_hex (p, &address);
7077 1.1 christos
7078 1.1 christos USTF(marker_iter_reset) (&iter);
7079 1.1 christos
7080 1.1 christos for (USTF(marker_iter_start) (&iter), m = iter.marker;
7081 1.1 christos m != NULL;
7082 1.1 christos USTF(marker_iter_next) (&iter), m = iter.marker)
7083 1.1 christos if ((uintptr_t ) m->location == address)
7084 1.1 christos {
7085 1.1 christos int result;
7086 1.1 christos
7087 1.1 christos trace_debug ("found marker for address. "
7088 1.1 christos "ltt_marker_connect (marker = %s/%s)",
7089 1.1 christos m->channel, m->name);
7090 1.1 christos
7091 1.1 christos result = USTF(ltt_marker_connect) (m->channel, m->name,
7092 1.1 christos GDB_PROBE_NAME);
7093 1.1 christos if (result && result != -EEXIST)
7094 1.1 christos trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7095 1.1 christos m->channel, m->name, -result);
7096 1.1 christos
7097 1.1 christos if (result < 0)
7098 1.1 christos {
7099 1.1 christos sprintf (packet, "E.could not connect marker: channel=%s, name=%s",
7100 1.1 christos m->channel, m->name);
7101 1.1 christos return -1;
7102 1.1 christos }
7103 1.1 christos
7104 1.1 christos strcpy (packet, "OK");
7105 1.1 christos return 0;
7106 1.1 christos }
7107 1.1 christos
7108 1.1 christos sprintf (packet, "E.no marker found at 0x%s", paddress (address));
7109 1.1 christos return -1;
7110 1.1 christos }
7111 1.1 christos
7112 1.1 christos static int
7113 1.1 christos cmd_qtstmat (char *packet)
7114 1.1 christos {
7115 1.1 christos char *p = packet;
7116 1.1 christos ULONGEST address;
7117 1.1 christos struct marker_iter iter;
7118 1.1 christos struct marker *m;
7119 1.1 christos
7120 1.1 christos p += sizeof ("qTSTMat:") - 1;
7121 1.1 christos
7122 1.1 christos p = unpack_varlen_hex (p, &address);
7123 1.1 christos
7124 1.1 christos USTF(marker_iter_reset) (&iter);
7125 1.1 christos
7126 1.1 christos for (USTF(marker_iter_start) (&iter), m = iter.marker;
7127 1.1 christos m != NULL;
7128 1.1 christos USTF(marker_iter_next) (&iter), m = iter.marker)
7129 1.1 christos if ((uintptr_t ) m->location == address)
7130 1.1 christos {
7131 1.1 christos response_ust_marker (packet, m);
7132 1.1 christos return 0;
7133 1.1 christos }
7134 1.1 christos
7135 1.1 christos strcpy (packet, "l");
7136 1.1 christos return -1;
7137 1.1 christos }
7138 1.1 christos
7139 1.1 christos static void
7140 1.1 christos gdb_ust_init (void)
7141 1.1 christos {
7142 1.1 christos if (!dlsym_ust ())
7143 1.1 christos return;
7144 1.1 christos
7145 1.1 christos USTF(ltt_probe_register) (&gdb_ust_probe);
7146 1.1 christos }
7147 1.1 christos
7148 1.1 christos #endif /* HAVE_UST */
7149 1.1 christos
7150 1.1 christos #include <sys/syscall.h>
7151 1.1 christos
7152 1.1 christos static void
7153 1.1 christos gdb_agent_remove_socket (void)
7154 1.1 christos {
7155 1.1 christos unlink (agent_socket_name);
7156 1.1 christos }
7157 1.1 christos
7158 1.1 christos /* Helper thread of agent. */
7159 1.1 christos
7160 1.1 christos static void *
7161 1.1 christos gdb_agent_helper_thread (void *arg)
7162 1.1 christos {
7163 1.1 christos int listen_fd;
7164 1.1 christos
7165 1.1 christos atexit (gdb_agent_remove_socket);
7166 1.1 christos
7167 1.1 christos while (1)
7168 1.1 christos {
7169 1.1 christos listen_fd = gdb_agent_socket_init ();
7170 1.1 christos
7171 1.1 christos if (helper_thread_id == 0)
7172 1.1 christos helper_thread_id = syscall (SYS_gettid);
7173 1.1 christos
7174 1.1 christos if (listen_fd == -1)
7175 1.1 christos {
7176 1.1 christos warning ("could not create sync socket");
7177 1.1 christos break;
7178 1.1 christos }
7179 1.1 christos
7180 1.1 christos while (1)
7181 1.1 christos {
7182 1.1 christos socklen_t tmp;
7183 1.1 christos struct sockaddr_un sockaddr;
7184 1.1 christos int fd;
7185 1.1 christos char buf[1];
7186 1.1 christos int ret;
7187 1.1 christos int stop_loop = 0;
7188 1.1 christos
7189 1.1 christos tmp = sizeof (sockaddr);
7190 1.1 christos
7191 1.1 christos do
7192 1.1 christos {
7193 1.1 christos fd = accept (listen_fd, (struct sockaddr *) &sockaddr, &tmp);
7194 1.1 christos }
7195 1.1 christos /* It seems an ERESTARTSYS can escape out of accept. */
7196 1.1 christos while (fd == -512 || (fd == -1 && errno == EINTR));
7197 1.1 christos
7198 1.1 christos if (fd < 0)
7199 1.1 christos {
7200 1.1 christos warning ("Accept returned %d, error: %s",
7201 1.1 christos fd, safe_strerror (errno));
7202 1.1 christos break;
7203 1.1 christos }
7204 1.1 christos
7205 1.1 christos do
7206 1.1 christos {
7207 1.1 christos ret = read (fd, buf, 1);
7208 1.1 christos } while (ret == -1 && errno == EINTR);
7209 1.1 christos
7210 1.1 christos if (ret == -1)
7211 1.1 christos {
7212 1.1 christos warning ("reading socket (fd=%d) failed with %s",
7213 1.1 christos fd, safe_strerror (errno));
7214 1.1 christos close (fd);
7215 1.1 christos break;
7216 1.1 christos }
7217 1.1 christos
7218 1.1 christos if (cmd_buf[0])
7219 1.1 christos {
7220 1.1 christos if (startswith (cmd_buf, "close"))
7221 1.1 christos {
7222 1.1 christos stop_loop = 1;
7223 1.1 christos }
7224 1.1 christos #ifdef HAVE_UST
7225 1.1 christos else if (strcmp ("qTfSTM", cmd_buf) == 0)
7226 1.1 christos {
7227 1.1 christos cmd_qtfstm (cmd_buf);
7228 1.1 christos }
7229 1.1 christos else if (strcmp ("qTsSTM", cmd_buf) == 0)
7230 1.1 christos {
7231 1.1 christos cmd_qtsstm (cmd_buf);
7232 1.1 christos }
7233 1.1 christos else if (startswith (cmd_buf, "unprobe_marker_at:"))
7234 1.1 christos {
7235 1.1 christos unprobe_marker_at (cmd_buf);
7236 1.1 christos }
7237 1.1 christos else if (startswith (cmd_buf, "probe_marker_at:"))
7238 1.1 christos {
7239 1.1 christos probe_marker_at (cmd_buf);
7240 1.1 christos }
7241 1.1 christos else if (startswith (cmd_buf, "qTSTMat:"))
7242 1.1 christos {
7243 1.1 christos cmd_qtstmat (cmd_buf);
7244 1.1 christos }
7245 1.1 christos #endif /* HAVE_UST */
7246 1.1 christos }
7247 1.1 christos
7248 1.1 christos /* Fix compiler's warning: ignoring return value of 'write'. */
7249 1.1 christos ret = write (fd, buf, 1);
7250 1.1 christos close (fd);
7251 1.1 christos
7252 1.1 christos if (stop_loop)
7253 1.1 christos {
7254 1.1 christos close (listen_fd);
7255 1.1 christos unlink (agent_socket_name);
7256 1.1 christos
7257 1.1 christos /* Sleep endlessly to wait the whole inferior stops. This
7258 1.1 christos thread can not exit because GDB or GDBserver may still need
7259 1.1 christos 'current_thread' (representing this thread) to access
7260 1.1 christos inferior memory. Otherwise, this thread exits earlier than
7261 1.1 christos other threads, and 'current_thread' is set to NULL. */
7262 1.1 christos while (1)
7263 1.1 christos sleep (10);
7264 1.1 christos }
7265 1.1 christos }
7266 1.1 christos }
7267 1.1 christos
7268 1.1 christos return NULL;
7269 1.1 christos }
7270 1.1 christos
7271 1.1 christos #include <signal.h>
7272 1.1 christos #include <pthread.h>
7273 1.1 christos
7274 1.1 christos EXTERN_C_PUSH
7275 1.1 christos IP_AGENT_EXPORT_VAR int gdb_agent_capability = AGENT_CAPA_STATIC_TRACE;
7276 1.1 christos EXTERN_C_POP
7277 1.1 christos
7278 1.1 christos static void
7279 1.1 christos gdb_agent_init (void)
7280 1.1 christos {
7281 1.1 christos int res;
7282 1.1 christos pthread_t thread;
7283 1.1 christos sigset_t new_mask;
7284 1.1 christos sigset_t orig_mask;
7285 1.1 christos
7286 1.1 christos /* We want the helper thread to be as transparent as possible, so
7287 1.1 christos have it inherit an all-signals-blocked mask. */
7288 1.1 christos
7289 1.1 christos sigfillset (&new_mask);
7290 1.1 christos res = pthread_sigmask (SIG_SETMASK, &new_mask, &orig_mask);
7291 1.1 christos if (res)
7292 1.1 christos perror_with_name ("pthread_sigmask (1)");
7293 1.1 christos
7294 1.1 christos res = pthread_create (&thread,
7295 1.1 christos NULL,
7296 1.1 christos gdb_agent_helper_thread,
7297 1.1 christos NULL);
7298 1.1 christos
7299 1.1 christos res = pthread_sigmask (SIG_SETMASK, &orig_mask, NULL);
7300 1.1 christos if (res)
7301 1.1 christos perror_with_name ("pthread_sigmask (2)");
7302 1.1 christos
7303 1.1 christos while (helper_thread_id == 0)
7304 1.1 christos usleep (1);
7305 1.1 christos
7306 1.1 christos #ifdef HAVE_UST
7307 1.1 christos gdb_ust_init ();
7308 1.1 christos #endif
7309 1.1 christos }
7310 1.1 christos
7311 1.1 christos #include <sys/mman.h>
7312 1.1 christos
7313 1.1 christos IP_AGENT_EXPORT_VAR char *gdb_tp_heap_buffer;
7314 1.1 christos IP_AGENT_EXPORT_VAR char *gdb_jump_pad_buffer;
7315 1.1 christos IP_AGENT_EXPORT_VAR char *gdb_jump_pad_buffer_end;
7316 1.1 christos IP_AGENT_EXPORT_VAR char *gdb_trampoline_buffer;
7317 1.1 christos IP_AGENT_EXPORT_VAR char *gdb_trampoline_buffer_end;
7318 1.1 christos IP_AGENT_EXPORT_VAR char *gdb_trampoline_buffer_error;
7319 1.1 christos
7320 1.1 christos /* Record the result of getting buffer space for fast tracepoint
7321 1.1 christos trampolines. Any error message is copied, since caller may not be
7322 1.1 christos using persistent storage. */
7323 1.1 christos
7324 1.1 christos void
7325 1.1 christos set_trampoline_buffer_space (CORE_ADDR begin, CORE_ADDR end, char *errmsg)
7326 1.1 christos {
7327 1.1 christos gdb_trampoline_buffer = (char *) (uintptr_t) begin;
7328 1.1 christos gdb_trampoline_buffer_end = (char *) (uintptr_t) end;
7329 1.1 christos if (errmsg)
7330 1.1 christos strncpy (gdb_trampoline_buffer_error, errmsg, 99);
7331 1.1 christos else
7332 1.1 christos strcpy (gdb_trampoline_buffer_error, "no buffer passed");
7333 1.1 christos }
7334 1.1 christos
7335 1.1 christos static void __attribute__ ((constructor))
7336 1.1 christos initialize_tracepoint_ftlib (void)
7337 1.1 christos {
7338 1.1 christos initialize_tracepoint ();
7339 1.1 christos
7340 1.1 christos gdb_agent_init ();
7341 1.1 christos }
7342 1.1 christos
7343 1.1 christos #ifndef HAVE_GETAUXVAL
7344 1.1 christos /* Retrieve the value of TYPE from the auxiliary vector. If TYPE is not
7345 1.1 christos found, 0 is returned. This function is provided if glibc is too old. */
7346 1.1 christos
7347 1.1 christos unsigned long
7348 1.1 christos getauxval (unsigned long type)
7349 1.1 christos {
7350 1.1 christos unsigned long data[2];
7351 1.1 christos FILE *f = fopen ("/proc/self/auxv", "r");
7352 1.1 christos unsigned long value = 0;
7353 1.1 christos
7354 1.1 christos if (f == NULL)
7355 1.1 christos return 0;
7356 1.1 christos
7357 1.1 christos while (fread (data, sizeof (data), 1, f) > 0)
7358 1.1 christos {
7359 1.1 christos if (data[0] == type)
7360 1.1 christos {
7361 1.1 christos value = data[1];
7362 1.1 christos break;
7363 1.1 christos }
7364 1.1 christos }
7365 1.1 christos
7366 1.1 christos fclose (f);
7367 1.1 christos return value;
7368 1.1 christos }
7369 1.1 christos #endif
7370 1.1 christos
7371 1.1 christos #endif /* IN_PROCESS_AGENT */
7372 1.1 christos
7373 1.1 christos /* Return a timestamp, expressed as microseconds of the usual Unix
7374 1.1 christos time. (As the result is a 64-bit number, it will not overflow any
7375 1.1 christos time soon.) */
7376 1.1 christos
7377 1.1 christos static LONGEST
7378 1.1 christos get_timestamp (void)
7379 1.1 christos {
7380 1.1 christos using namespace std::chrono;
7381 1.1 christos
7382 1.1 christos steady_clock::time_point now = steady_clock::now ();
7383 1.1 christos return duration_cast<microseconds> (now.time_since_epoch ()).count ();
7384 1.1 christos }
7385 1.1 christos
7386 1.1 christos void
7387 1.1 christos initialize_tracepoint (void)
7388 1.1 christos {
7389 1.1 christos /* Start with the default size. */
7390 1.1 christos init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE);
7391 1.1 christos
7392 1.1 christos /* Wire trace state variable 1 to be the timestamp. This will be
7393 1.1 christos uploaded to GDB upon connection and become one of its trace state
7394 1.1 christos variables. (In case you're wondering, if GDB already has a trace
7395 1.1 christos variable numbered 1, it will be renumbered.) */
7396 1.1 christos create_trace_state_variable (1, 0);
7397 1.1 christos set_trace_state_variable_name (1, "trace_timestamp");
7398 1.1 christos set_trace_state_variable_getter (1, get_timestamp);
7399 1.1 christos
7400 1.1 christos #ifdef IN_PROCESS_AGENT
7401 1.1 christos {
7402 1.1 christos int pagesize;
7403 1.1 christos size_t jump_pad_size;
7404 1.1 christos
7405 1.1 christos pagesize = sysconf (_SC_PAGE_SIZE);
7406 1.1 christos if (pagesize == -1)
7407 1.1 christos perror_with_name ("sysconf");
7408 1.1 christos
7409 1.1 christos #define SCRATCH_BUFFER_NPAGES 20
7410 1.1 christos
7411 1.1 christos jump_pad_size = pagesize * SCRATCH_BUFFER_NPAGES;
7412 1.1 christos
7413 1.1 christos gdb_tp_heap_buffer = (char *) xmalloc (5 * 1024 * 1024);
7414 1.1 christos gdb_jump_pad_buffer = (char *) alloc_jump_pad_buffer (jump_pad_size);
7415 1.1 christos if (gdb_jump_pad_buffer == NULL)
7416 1.1 christos perror_with_name ("mmap");
7417 1.1 christos gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + jump_pad_size;
7418 1.1 christos }
7419 1.1 christos
7420 1.1 christos gdb_trampoline_buffer = gdb_trampoline_buffer_end = 0;
7421 1.1 christos
7422 1.1 christos /* It's not a fatal error for something to go wrong with trampoline
7423 1.1 christos buffer setup, but it can be mysterious, so create a channel to
7424 1.1 christos report back on what went wrong, using a fixed size since we may
7425 1.1 christos not be able to allocate space later when the problem occurs. */
7426 1.1 christos gdb_trampoline_buffer_error = (char *) xmalloc (IPA_BUFSIZ);
7427 1.1 christos
7428 1.1 christos strcpy (gdb_trampoline_buffer_error, "No errors reported");
7429 1.1 christos
7430 1.1 christos initialize_low_tracepoint ();
7431 1.1 christos #endif
7432 1.1 christos }
7433