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