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