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