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