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