Commit | Line | Data |
---|---|---|
219f2f23 PA |
1 | /* Tracepoint code for remote server for GDB. |
2 | Copyright (C) 2009, 2010 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 3 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include "server.h" | |
20 | #include <ctype.h> | |
21 | #include <fcntl.h> | |
22 | #include <unistd.h> | |
23 | #include <sys/time.h> | |
24 | ||
25 | static void trace_debug_1 (const char *, ...) ATTR_FORMAT (printf, 1, 2); | |
26 | ||
27 | static void | |
28 | trace_debug_1 (const char *fmt, ...) | |
29 | { | |
30 | char buf[1024]; | |
31 | va_list ap; | |
32 | ||
33 | va_start (ap, fmt); | |
34 | vsprintf (buf, fmt, ap); | |
35 | fprintf (stderr, "gdbserver/tracepoint: %s\n", buf); | |
36 | va_end (ap); | |
37 | } | |
38 | ||
39 | #define trace_debug(FMT, args...) \ | |
40 | do { \ | |
41 | if (debug_threads) \ | |
42 | trace_debug_1 ((FMT), ##args); \ | |
43 | } while (0) | |
44 | ||
45 | static int | |
46 | tracepoint_handler (CORE_ADDR address) | |
47 | { | |
48 | trace_debug ("tracepoint_handler: tracepoint at 0x%s hit", | |
49 | paddress (address)); | |
50 | return 0; | |
51 | } | |
52 | ||
53 | /* This enum must exactly match what is documented in | |
54 | gdb/doc/agentexpr.texi, including all the numerical values. */ | |
55 | ||
56 | enum gdb_agent_op | |
57 | { | |
58 | gdb_agent_op_float = 0x01, | |
59 | gdb_agent_op_add = 0x02, | |
60 | gdb_agent_op_sub = 0x03, | |
61 | gdb_agent_op_mul = 0x04, | |
62 | gdb_agent_op_div_signed = 0x05, | |
63 | gdb_agent_op_div_unsigned = 0x06, | |
64 | gdb_agent_op_rem_signed = 0x07, | |
65 | gdb_agent_op_rem_unsigned = 0x08, | |
66 | gdb_agent_op_lsh = 0x09, | |
67 | gdb_agent_op_rsh_signed = 0x0a, | |
68 | gdb_agent_op_rsh_unsigned = 0x0b, | |
69 | gdb_agent_op_trace = 0x0c, | |
70 | gdb_agent_op_trace_quick = 0x0d, | |
71 | gdb_agent_op_log_not = 0x0e, | |
72 | gdb_agent_op_bit_and = 0x0f, | |
73 | gdb_agent_op_bit_or = 0x10, | |
74 | gdb_agent_op_bit_xor = 0x11, | |
75 | gdb_agent_op_bit_not = 0x12, | |
76 | gdb_agent_op_equal = 0x13, | |
77 | gdb_agent_op_less_signed = 0x14, | |
78 | gdb_agent_op_less_unsigned = 0x15, | |
79 | gdb_agent_op_ext = 0x16, | |
80 | gdb_agent_op_ref8 = 0x17, | |
81 | gdb_agent_op_ref16 = 0x18, | |
82 | gdb_agent_op_ref32 = 0x19, | |
83 | gdb_agent_op_ref64 = 0x1a, | |
84 | gdb_agent_op_ref_float = 0x1b, | |
85 | gdb_agent_op_ref_double = 0x1c, | |
86 | gdb_agent_op_ref_long_double = 0x1d, | |
87 | gdb_agent_op_l_to_d = 0x1e, | |
88 | gdb_agent_op_d_to_l = 0x1f, | |
89 | gdb_agent_op_if_goto = 0x20, | |
90 | gdb_agent_op_goto = 0x21, | |
91 | gdb_agent_op_const8 = 0x22, | |
92 | gdb_agent_op_const16 = 0x23, | |
93 | gdb_agent_op_const32 = 0x24, | |
94 | gdb_agent_op_const64 = 0x25, | |
95 | gdb_agent_op_reg = 0x26, | |
96 | gdb_agent_op_end = 0x27, | |
97 | gdb_agent_op_dup = 0x28, | |
98 | gdb_agent_op_pop = 0x29, | |
99 | gdb_agent_op_zero_ext = 0x2a, | |
100 | gdb_agent_op_swap = 0x2b, | |
101 | gdb_agent_op_getv = 0x2c, | |
102 | gdb_agent_op_setv = 0x2d, | |
103 | gdb_agent_op_tracev = 0x2e, | |
104 | gdb_agent_op_trace16 = 0x30, | |
105 | gdb_agent_op_last | |
106 | }; | |
107 | ||
108 | static const char *gdb_agent_op_names [gdb_agent_op_last] = | |
109 | { | |
110 | "?undef?", | |
111 | "float", | |
112 | "add", | |
113 | "sub", | |
114 | "mul", | |
115 | "div_signed", | |
116 | "div_unsigned", | |
117 | "rem_signed", | |
118 | "rem_unsigned", | |
119 | "lsh", | |
120 | "rsh_signed", | |
121 | "rsh_unsigned", | |
122 | "trace", | |
123 | "trace_quick", | |
124 | "log_not", | |
125 | "bit_and", | |
126 | "bit_or", | |
127 | "bit_xor", | |
128 | "bit_not", | |
129 | "equal", | |
130 | "less_signed", | |
131 | "less_unsigned", | |
132 | "ext", | |
133 | "ref8", | |
134 | "ref16", | |
135 | "ref32", | |
136 | "ref64", | |
137 | "ref_float", | |
138 | "ref_double", | |
139 | "ref_long_double", | |
140 | "l_to_d", | |
141 | "d_to_l", | |
142 | "if_goto", | |
143 | "goto", | |
144 | "const8", | |
145 | "const16", | |
146 | "const32", | |
147 | "const64", | |
148 | "reg", | |
149 | "end", | |
150 | "dup", | |
151 | "pop", | |
152 | "zero_ext", | |
153 | "swap", | |
154 | "getv", | |
155 | "setv", | |
156 | "tracev", | |
157 | "?undef?", | |
158 | "trace16", | |
159 | }; | |
160 | ||
161 | struct agent_expr | |
162 | { | |
163 | int length; | |
164 | ||
165 | unsigned char *bytes; | |
166 | }; | |
167 | ||
168 | /* Base action. Concrete actions inherit this. */ | |
169 | ||
170 | struct tracepoint_action | |
171 | { | |
172 | char type; | |
173 | }; | |
174 | ||
175 | /* An 'M' (collect memory) action. */ | |
176 | struct collect_memory_action | |
177 | { | |
178 | struct tracepoint_action base; | |
179 | ||
180 | ULONGEST addr; | |
181 | ULONGEST len; | |
182 | int basereg; | |
183 | }; | |
184 | ||
185 | /* An 'R' (collect registers) action. */ | |
186 | ||
187 | struct collect_registers_action | |
188 | { | |
189 | struct tracepoint_action base; | |
190 | }; | |
191 | ||
192 | /* An 'X' (evaluate expression) action. */ | |
193 | ||
194 | struct eval_expr_action | |
195 | { | |
196 | struct tracepoint_action base; | |
197 | ||
198 | struct agent_expr *expr; | |
199 | }; | |
200 | ||
201 | /* An 'L' (collect static trace data) action. */ | |
202 | struct collect_static_trace_data_action | |
203 | { | |
204 | struct tracepoint_action base; | |
205 | }; | |
206 | ||
207 | /* This structure describes a piece of the source-level definition of | |
208 | the tracepoint. The contents are not interpreted by the target, | |
209 | but preserved verbatim for uploading upon reconnection. */ | |
210 | ||
211 | struct source_string | |
212 | { | |
213 | /* The type of string, such as "cond" for a conditional. */ | |
214 | char *type; | |
215 | ||
216 | /* The source-level string itself. For the sake of target | |
217 | debugging, we store it in plaintext, even though it is always | |
218 | transmitted in hex. */ | |
219 | char *str; | |
220 | ||
221 | /* Link to the next one in the list. We link them in the order | |
222 | received, in case some make up an ordered list of commands or | |
223 | some such. */ | |
224 | struct source_string *next; | |
225 | }; | |
226 | ||
227 | struct tracepoint_hit_ctx; | |
228 | ||
229 | /* The definition of a tracepoint. */ | |
230 | ||
231 | /* Tracepoints may have multiple locations, each at a different | |
232 | address. This can occur with optimizations, template | |
233 | instantiation, etc. Since the locations may be in different | |
234 | scopes, the conditions and actions may be different for each | |
235 | location. Our target version of tracepoints is more like GDB's | |
236 | notion of "breakpoint locations", but we have almost nothing that | |
237 | is not per-location, so we bother having two kinds of objects. The | |
238 | key consequence is that numbers are not unique, and that it takes | |
239 | both number and address to identify a tracepoint uniquely. */ | |
240 | ||
241 | struct tracepoint | |
242 | { | |
243 | /* The number of the tracepoint, as specified by GDB. Several | |
244 | tracepoint objects here may share a number. */ | |
245 | int number; | |
246 | ||
247 | /* Address at which the tracepoint is supposed to trigger. Several | |
248 | tracepoints may share an address. */ | |
249 | CORE_ADDR address; | |
250 | ||
251 | /* True if the tracepoint is currently enabled. */ | |
252 | int enabled; | |
253 | ||
254 | /* The number of single steps that will be performed after each | |
255 | tracepoint hit. */ | |
256 | long step_count; | |
257 | ||
258 | /* The number of times the tracepoint may be hit before it will | |
259 | terminate the entire tracing run. */ | |
260 | long pass_count; | |
261 | ||
262 | /* Pointer to the agent expression that is the tracepoint's | |
263 | conditional, or NULL if the tracepoint is unconditional. */ | |
264 | struct agent_expr *cond; | |
265 | ||
266 | /* The list of actions to take when the tracepoint triggers. */ | |
267 | int numactions; | |
268 | struct tracepoint_action **actions; | |
269 | /* Same, but in string/packet form. */ | |
270 | char **actions_str; | |
271 | ||
272 | /* The list of actions to take while in a stepping loop. */ | |
273 | int num_step_actions; | |
274 | struct tracepoint_action **step_actions; | |
275 | /* Same, but in string/packet form. */ | |
276 | char **step_actions_str; | |
277 | ||
278 | /* Count of the times we've hit this tracepoint during the run. | |
279 | Note that while-stepping steps are not counted as "hits". */ | |
280 | long hit_count; | |
281 | ||
282 | /* The collection of strings that describe the tracepoint as it was | |
283 | entered into GDB. These are not used by the target, but are | |
284 | reported back to GDB upon reconnection. */ | |
285 | struct source_string *source_strings; | |
286 | ||
287 | /* Handle returned by the breakpoint module when we inserted the | |
288 | trap. NULL if we haven't inserted it yet. */ | |
289 | void *handle; | |
290 | ||
291 | /* Link to the next tracepoint in the list. */ | |
292 | struct tracepoint *next; | |
293 | }; | |
294 | ||
295 | /* Given `while-stepping', a thread may be collecting data for more | |
296 | than one tracepoint simultaneously. On the other hand, the same | |
297 | tracepoint with a while-stepping action may be hit by more than one | |
298 | thread simultaneously (but not quite, each thread could be handling | |
299 | a different step). Each thread holds a list of these objects, | |
300 | representing the current step of each while-stepping action being | |
301 | collected. */ | |
302 | ||
303 | struct wstep_state | |
304 | { | |
305 | struct wstep_state *next; | |
306 | ||
307 | /* The tracepoint number. */ | |
308 | int tp_number; | |
309 | /* The tracepoint's address. */ | |
310 | CORE_ADDR tp_address; | |
311 | ||
312 | /* The number of the current step in this 'while-stepping' | |
313 | action. */ | |
314 | long current_step; | |
315 | }; | |
316 | ||
317 | /* The linked list of all tracepoints. */ | |
318 | ||
319 | static struct tracepoint *tracepoints; | |
320 | ||
321 | /* Pointer to the last tracepoint in the list, new tracepoints are | |
322 | linked in at the end. */ | |
323 | ||
324 | static struct tracepoint *last_tracepoint; | |
325 | ||
326 | /* The first tracepoint to exceed its pass count. */ | |
327 | ||
328 | static struct tracepoint *stopping_tracepoint; | |
329 | ||
330 | /* True if the trace buffer is full or otherwise no longer usable. */ | |
331 | ||
332 | static int trace_buffer_is_full; | |
333 | ||
334 | /* Enumeration of the different kinds of things that can happen during | |
335 | agent expression evaluation. */ | |
336 | ||
337 | enum eval_result_type | |
338 | { | |
339 | expr_eval_no_error, | |
340 | expr_eval_empty_expression, | |
341 | expr_eval_empty_stack, | |
342 | expr_eval_stack_overflow, | |
343 | expr_eval_stack_underflow, | |
344 | expr_eval_unhandled_opcode, | |
345 | expr_eval_unrecognized_opcode, | |
346 | expr_eval_divide_by_zero, | |
347 | expr_eval_invalid_goto | |
348 | }; | |
349 | ||
350 | static enum eval_result_type expr_eval_result = expr_eval_no_error; | |
351 | ||
352 | static const char *eval_result_names[] = | |
353 | { | |
354 | "terror:in the attic", /* this should never be reported */ | |
355 | "terror:empty expression", | |
356 | "terror:empty stack", | |
357 | "terror:stack overflow", | |
358 | "terror:stack underflow", | |
359 | "terror:unhandled opcode", | |
360 | "terror:unrecognized opcode", | |
361 | "terror:divide by zero" | |
362 | }; | |
363 | ||
364 | /* The tracepoint in which the error occurred. */ | |
365 | ||
366 | static struct tracepoint *error_tracepoint; | |
367 | ||
368 | struct trace_state_variable | |
369 | { | |
370 | /* This is the name of the variable as used in GDB. The target | |
371 | doesn't use the name, but needs to have it for saving and | |
372 | reconnection purposes. */ | |
373 | char *name; | |
374 | ||
375 | /* This number identifies the variable uniquely. Numbers may be | |
376 | assigned either by the target (in the case of builtin variables), | |
377 | or by GDB, and are presumed unique during the course of a trace | |
378 | experiment. */ | |
379 | int number; | |
380 | ||
381 | /* The variable's initial value, a 64-bit signed integer always. */ | |
382 | LONGEST initial_value; | |
383 | ||
384 | /* The variable's value, a 64-bit signed integer always. */ | |
385 | LONGEST value; | |
386 | ||
387 | /* Pointer to a getter function, used to supply computed values. */ | |
388 | LONGEST (*getter) (void); | |
389 | ||
390 | /* Link to the next variable. */ | |
391 | struct trace_state_variable *next; | |
392 | }; | |
393 | ||
394 | /* Linked list of all trace state variables. */ | |
395 | ||
396 | static struct trace_state_variable *trace_state_variables; | |
397 | ||
398 | /* The results of tracing go into a fixed-size space known as the | |
399 | "trace buffer". Because usage follows a limited number of | |
400 | patterns, we manage it ourselves rather than with malloc. Basic | |
401 | rules are that we create only one trace frame at a time, each is | |
402 | variable in size, they are never moved once created, and we only | |
403 | discard if we are doing a circular buffer, and then only the oldest | |
404 | ones. Each trace frame includes its own size, so we don't need to | |
405 | link them together, and the trace frame number is relative to the | |
406 | first one, so we don't need to record numbers. A trace frame also | |
407 | records the number of the tracepoint that created it. The data | |
408 | itself is a series of blocks, each introduced by a single character | |
409 | and with a defined format. Each type of block has enough | |
410 | type/length info to allow scanners to jump quickly from one block | |
411 | to the next without reading each byte in the block. */ | |
412 | ||
413 | /* Trace buffer management would be simple - advance a free pointer | |
414 | from beginning to end, then stop - were it not for the circular | |
415 | buffer option, which is a useful way to prevent a trace run from | |
416 | stopping prematurely because the buffer filled up. In the circular | |
417 | case, the location of the first trace frame (trace_buffer_start) | |
418 | moves as old trace frames are discarded. Also, since we grow trace | |
419 | frames incrementally as actions are performed, we wrap around to | |
420 | the beginning of the trace buffer. This is per-block, so each | |
421 | block within a trace frame remains contiguous. Things get messy | |
422 | when the wrapped-around trace frame is the one being discarded; the | |
423 | free space ends up in two parts at opposite ends of the buffer. */ | |
424 | ||
425 | #ifndef ATTR_PACKED | |
426 | # if defined(__GNUC__) | |
427 | # define ATTR_PACKED __attribute__ ((packed)) | |
428 | # else | |
429 | # define ATTR_PACKED /* nothing */ | |
430 | # endif | |
431 | #endif | |
432 | ||
433 | /* The data collected at a tracepoint hit. This object should be as | |
434 | small as possible, since there may be a great many of them. We do | |
435 | not need to keep a frame number, because they are all sequential | |
436 | and there are no deletions; so the Nth frame in the buffer is | |
437 | always frame number N. */ | |
438 | ||
439 | struct traceframe | |
440 | { | |
441 | /* Number of the tracepoint that collected this traceframe. A value | |
442 | of 0 indicates the current end of the trace buffer. We make this | |
443 | a 16-bit field because it's never going to happen that GDB's | |
444 | numbering of tracepoints reaches 32,000. */ | |
445 | int tpnum : 16; | |
446 | ||
447 | /* The size of the data in this trace frame. We limit this to 32 | |
448 | bits, even on a 64-bit target, because it's just implausible that | |
449 | one is validly going to collect 4 gigabytes of data at a single | |
450 | tracepoint hit. */ | |
451 | unsigned int data_size : 32; | |
452 | ||
453 | /* The base of the trace data, which is contiguous from this point. */ | |
454 | unsigned char data[0]; | |
455 | ||
456 | } ATTR_PACKED traceframe_t; | |
457 | ||
458 | /* The traceframe to be used as the source of data to send back to | |
459 | GDB. A value of -1 means to get data from the live program. */ | |
460 | ||
461 | int current_traceframe = -1; | |
462 | ||
463 | /* This flag is true if the trace buffer is circular, meaning that | |
464 | when it fills, the oldest trace frames are discarded in order to | |
465 | make room. */ | |
466 | ||
467 | static int circular_trace_buffer; | |
468 | ||
469 | /* Pointer to the block of memory that traceframes all go into. */ | |
470 | ||
471 | static unsigned char *trace_buffer_lo; | |
472 | ||
473 | /* Pointer to the end of the trace buffer, more precisely to the byte | |
474 | after the end of the buffer. */ | |
475 | ||
476 | static unsigned char *trace_buffer_hi; | |
477 | ||
478 | /* Pointer to the first trace frame in the buffer. In the | |
479 | non-circular case, this is equal to trace_buffer_lo, otherwise it | |
480 | moves around in the buffer. */ | |
481 | ||
482 | static unsigned char *trace_buffer_start; | |
483 | ||
484 | /* Pointer to the free part of the trace buffer. Note that we clear | |
485 | several bytes at and after this pointer, so that traceframe | |
486 | scans/searches terminate properly. */ | |
487 | ||
488 | static unsigned char *trace_buffer_free; | |
489 | ||
490 | /* Pointer to the byte after the end of the free part. Note that this | |
491 | may be smaller than trace_buffer_free in the circular case, and | |
492 | means that the free part is in two pieces. Initially it is equal | |
493 | to trace_buffer_hi, then is generally equivalent to | |
494 | trace_buffer_start. */ | |
495 | ||
496 | static unsigned char *trace_buffer_end_free; | |
497 | ||
498 | /* Pointer to the wraparound. If not equal to trace_buffer_hi, then | |
499 | this is the point at which the trace data breaks, and resumes at | |
500 | trace_buffer_lo. */ | |
501 | ||
502 | static unsigned char *trace_buffer_wrap; | |
503 | ||
504 | /* Macro that returns a pointer to the first traceframe in the buffer. */ | |
505 | ||
506 | #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start) | |
507 | ||
508 | /* Macro that returns a pointer to the next traceframe in the buffer. | |
509 | If the computed location is beyond the wraparound point, subtract | |
510 | the offset of the wraparound. */ | |
511 | ||
512 | #define NEXT_TRACEFRAME_1(TF) \ | |
513 | (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size) | |
514 | ||
515 | #define NEXT_TRACEFRAME(TF) \ | |
516 | ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \ | |
517 | - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \ | |
518 | ? (trace_buffer_wrap - trace_buffer_lo) \ | |
519 | : 0))) | |
520 | ||
521 | /* The difference between these counters represents the total number | |
522 | of complete traceframes present in the trace buffer. */ | |
523 | ||
524 | static unsigned int traceframe_write_count; | |
525 | static unsigned int traceframe_read_count; | |
526 | ||
527 | /* Convenience macro. */ | |
528 | ||
529 | #define traceframe_count \ | |
530 | ((unsigned int) (traceframe_write_count - traceframe_read_count)) | |
531 | ||
532 | /* The count of all traceframes created in the current run, including | |
533 | ones that were discarded to make room. */ | |
534 | ||
535 | static int traceframes_created; | |
536 | ||
537 | /* Read-only regions are address ranges whose contents don't change, | |
538 | and so can be read from target memory even while looking at a trace | |
539 | frame. Without these, disassembly for instance will likely fail, | |
540 | because the program code is not usually collected into a trace | |
541 | frame. This data structure does not need to be very complicated or | |
542 | particularly efficient, it's only going to be used occasionally, | |
543 | and only by some commands. */ | |
544 | ||
545 | struct readonly_region | |
546 | { | |
547 | /* The bounds of the region. */ | |
548 | CORE_ADDR start, end; | |
549 | ||
550 | /* Link to the next one. */ | |
551 | struct readonly_region *next; | |
552 | }; | |
553 | ||
554 | /* Linked list of readonly regions. This list stays in effect from | |
555 | one tstart to the next. */ | |
556 | ||
557 | static struct readonly_region *readonly_regions; | |
558 | ||
559 | /* The global that controls tracing overall. */ | |
560 | ||
8336d594 PA |
561 | int tracing; |
562 | ||
563 | /* Controls whether tracing should continue after GDB disconnects. */ | |
564 | ||
565 | int disconnected_tracing; | |
219f2f23 PA |
566 | |
567 | /* The reason for the last tracing run to have stopped. We initialize | |
568 | to a distinct string so that GDB can distinguish between "stopped | |
569 | after running" and "stopped because never run" cases. */ | |
570 | ||
571 | static const char *tracing_stop_reason = "tnotrun"; | |
572 | ||
573 | static int tracing_stop_tpnum; | |
574 | ||
575 | /* Functions local to this file. */ | |
576 | ||
577 | /* Base "class" for tracepoint type specific data to be passed down to | |
578 | collect_data_at_tracepoint. */ | |
579 | struct tracepoint_hit_ctx | |
580 | { | |
581 | /* empty */ | |
582 | }; | |
583 | ||
584 | /* Trap tracepoint specific data to be passed down to | |
585 | collect_data_at_tracepoint. */ | |
586 | ||
587 | struct trap_tracepoint_ctx | |
588 | { | |
589 | struct tracepoint_hit_ctx base; | |
590 | ||
591 | struct regcache *regcache; | |
592 | }; | |
593 | ||
594 | static struct agent_expr *parse_agent_expr (char **actparm); | |
595 | static char *unparse_agent_expr (struct agent_expr *aexpr); | |
596 | static enum eval_result_type eval_agent_expr (struct tracepoint_hit_ctx *ctx, | |
597 | struct traceframe *tframe, | |
598 | struct agent_expr *aexpr, | |
599 | ULONGEST *rslt); | |
600 | ||
601 | static int agent_mem_read (struct traceframe *tframe, | |
602 | unsigned char *to, CORE_ADDR from, ULONGEST len); | |
603 | static int agent_tsv_read (struct traceframe *tframe, int n); | |
604 | ||
605 | static CORE_ADDR traceframe_get_pc (struct traceframe *tframe); | |
606 | static int traceframe_read_tsv (int num, LONGEST *val); | |
607 | ||
608 | static int condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx, | |
609 | struct tracepoint *tpoint); | |
610 | ||
611 | static void clear_readonly_regions (void); | |
612 | static void clear_installed_tracepoints (void); | |
613 | ||
614 | static void collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, | |
615 | CORE_ADDR stop_pc, | |
616 | struct tracepoint *tpoint); | |
617 | ||
618 | static void collect_data_at_step (struct tracepoint_hit_ctx *ctx, | |
619 | CORE_ADDR stop_pc, | |
620 | struct tracepoint *tpoint, int current_step); | |
621 | ||
622 | static void do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx, | |
623 | CORE_ADDR stop_pc, | |
624 | struct tracepoint *tpoint, | |
625 | struct traceframe *tframe, | |
626 | struct tracepoint_action *taction); | |
627 | ||
628 | /* Record that an error occurred during expression evaluation. */ | |
629 | ||
630 | static void | |
631 | record_tracepoint_error (struct tracepoint *tpoint, const char *which, | |
632 | enum eval_result_type rtype) | |
633 | { | |
634 | trace_debug ("Tracepoint %d at %s %s eval reports error %d", | |
635 | tpoint->number, paddress (tpoint->address), which, rtype); | |
636 | ||
637 | expr_eval_result = rtype; | |
638 | error_tracepoint = tpoint; | |
639 | } | |
640 | ||
641 | /* Trace buffer management. */ | |
642 | ||
643 | static void | |
644 | clear_trace_buffer (void) | |
645 | { | |
646 | trace_buffer_start = trace_buffer_lo; | |
647 | trace_buffer_free = trace_buffer_lo; | |
648 | trace_buffer_end_free = trace_buffer_hi; | |
649 | trace_buffer_wrap = trace_buffer_hi; | |
650 | /* A traceframe with zeroed fields marks the end of trace data. */ | |
651 | ((struct traceframe *) trace_buffer_free)->tpnum = 0; | |
652 | ((struct traceframe *) trace_buffer_free)->data_size = 0; | |
653 | traceframe_read_count = traceframe_write_count = 0; | |
654 | traceframes_created = 0; | |
655 | } | |
656 | ||
657 | static void | |
658 | init_trace_buffer (unsigned char *buf, int bufsize) | |
659 | { | |
660 | trace_buffer_lo = buf; | |
661 | trace_buffer_hi = trace_buffer_lo + bufsize; | |
662 | ||
663 | clear_trace_buffer (); | |
664 | } | |
665 | ||
666 | /* Carve out a piece of the trace buffer, returning NULL in case of | |
667 | failure. */ | |
668 | ||
669 | static void * | |
670 | trace_buffer_alloc (size_t amt) | |
671 | { | |
672 | unsigned char *rslt; | |
673 | struct traceframe *oldest; | |
674 | unsigned char *new_start; | |
675 | ||
676 | trace_debug ("Want to allocate %ld+%ld bytes in trace buffer", | |
677 | (long) amt, (long) sizeof (struct traceframe)); | |
678 | ||
679 | /* Account for the EOB marker. */ | |
680 | amt += sizeof (struct traceframe); | |
681 | ||
682 | /* Offsets are easier to grok for debugging than raw addresses, | |
683 | especially for the small trace buffer sizes that are useful for | |
684 | testing. */ | |
685 | trace_debug ("Trace buffer start=%d free=%d endfree=%d wrap=%d hi=%d", | |
686 | (int) (trace_buffer_start - trace_buffer_lo), | |
687 | (int) (trace_buffer_free - trace_buffer_lo), | |
688 | (int) (trace_buffer_end_free - trace_buffer_lo), | |
689 | (int) (trace_buffer_wrap - trace_buffer_lo), | |
690 | (int) (trace_buffer_hi - trace_buffer_lo)); | |
691 | ||
692 | /* The algorithm here is to keep trying to get a contiguous block of | |
693 | the requested size, possibly discarding older traceframes to free | |
694 | up space. Since free space might come in one or two pieces, | |
695 | depending on whether discarded traceframes wrapped around at the | |
696 | high end of the buffer, we test both pieces after each | |
697 | discard. */ | |
698 | while (1) | |
699 | { | |
700 | /* First, if we have two free parts, try the upper one first. */ | |
701 | if (trace_buffer_end_free < trace_buffer_free) | |
702 | { | |
703 | if (trace_buffer_free + amt <= trace_buffer_hi) | |
704 | /* We have enough in the upper part. */ | |
705 | break; | |
706 | else | |
707 | { | |
708 | /* Our high part of free space wasn't enough. Give up | |
709 | on it for now, set wraparound. We will recover the | |
710 | space later, if/when the wrapped-around traceframe is | |
711 | discarded. */ | |
712 | trace_debug ("Upper part too small, setting wraparound"); | |
713 | trace_buffer_wrap = trace_buffer_free; | |
714 | trace_buffer_free = trace_buffer_lo; | |
715 | } | |
716 | } | |
717 | ||
718 | /* The normal case. */ | |
719 | if (trace_buffer_free + amt <= trace_buffer_end_free) | |
720 | break; | |
721 | ||
722 | /* If we're here, then neither part is big enough, and | |
723 | non-circular trace buffers are now full. */ | |
724 | if (!circular_trace_buffer) | |
725 | { | |
726 | trace_debug ("Not enough space in the trace buffer"); | |
727 | return NULL; | |
728 | } | |
729 | ||
730 | trace_debug ("Need more space in the trace buffer"); | |
731 | ||
732 | /* If we have a circular buffer, we can try discarding the | |
733 | oldest traceframe and see if that helps. */ | |
734 | oldest = FIRST_TRACEFRAME (); | |
735 | if (oldest->tpnum == 0) | |
736 | { | |
737 | /* Not good; we have no traceframes to free. Perhaps we're | |
738 | asking for a block that is larger than the buffer? In | |
739 | any case, give up. */ | |
740 | trace_debug ("No traceframes to discard"); | |
741 | return NULL; | |
742 | } | |
743 | ||
744 | --traceframe_write_count; | |
745 | ||
746 | new_start = (unsigned char *) NEXT_TRACEFRAME (oldest); | |
747 | /* If we freed the traceframe that wrapped around, go back | |
748 | to the non-wrap case. */ | |
749 | if (new_start < trace_buffer_start) | |
750 | { | |
751 | trace_debug ("Discarding past the wraparound"); | |
752 | trace_buffer_wrap = trace_buffer_hi; | |
753 | } | |
754 | trace_buffer_start = new_start; | |
755 | trace_buffer_end_free = trace_buffer_start; | |
756 | ||
757 | trace_debug ("Discarded a traceframe\n" | |
758 | "Trace buffer, start=%d free=%d endfree=%d wrap=%d hi=%d", | |
759 | (int) (trace_buffer_start - trace_buffer_lo), | |
760 | (int) (trace_buffer_free - trace_buffer_lo), | |
761 | (int) (trace_buffer_end_free - trace_buffer_lo), | |
762 | (int) (trace_buffer_wrap - trace_buffer_lo), | |
763 | (int) (trace_buffer_hi - trace_buffer_lo)); | |
764 | ||
765 | /* Now go back around the loop. The discard might have resulted | |
766 | in either one or two pieces of free space, so we want to try | |
767 | both before freeing any more traceframes. */ | |
768 | } | |
769 | ||
770 | /* If we get here, we know we can provide the asked-for space. */ | |
771 | ||
772 | rslt = trace_buffer_free; | |
773 | ||
774 | /* Adjust the request back down, now that we know we have space for | |
775 | the marker. */ | |
776 | trace_buffer_free += (amt - sizeof (struct traceframe)); | |
777 | ||
778 | /* We have a new piece of the trace buffer. Hurray! */ | |
779 | ||
780 | /* Add an EOB marker just past this allocation. */ | |
781 | ((struct traceframe *) trace_buffer_free)->tpnum = 0; | |
782 | ((struct traceframe *) trace_buffer_free)->data_size = 0; | |
783 | ||
784 | /* Adjust the request back down, now that we know we have space for | |
785 | the marker. */ | |
786 | amt -= sizeof (struct traceframe); | |
787 | ||
788 | if (debug_threads) | |
789 | { | |
790 | trace_debug ("Allocated %d bytes", (int) amt); | |
791 | trace_debug ("Trace buffer start=%d free=%d endfree=%d wrap=%d hi=%d", | |
792 | (int) (trace_buffer_start - trace_buffer_lo), | |
793 | (int) (trace_buffer_free - trace_buffer_lo), | |
794 | (int) (trace_buffer_end_free - trace_buffer_lo), | |
795 | (int) (trace_buffer_wrap - trace_buffer_lo), | |
796 | (int) (trace_buffer_hi - trace_buffer_lo)); | |
797 | } | |
798 | ||
799 | return rslt; | |
800 | } | |
801 | ||
802 | /* Return the total free space. This is not necessarily the largest | |
803 | block we can allocate, because of the two-part case. */ | |
804 | ||
805 | static int | |
806 | free_space (void) | |
807 | { | |
808 | if (trace_buffer_free <= trace_buffer_end_free) | |
809 | return trace_buffer_end_free - trace_buffer_free; | |
810 | else | |
811 | return ((trace_buffer_end_free - trace_buffer_lo) | |
812 | + (trace_buffer_hi - trace_buffer_free)); | |
813 | } | |
814 | ||
815 | /* An 'S' in continuation packets indicates remainder are for | |
816 | while-stepping. */ | |
817 | ||
818 | static int seen_step_action_flag; | |
819 | ||
820 | /* Create a tracepoint (location) with given number and address. */ | |
821 | ||
822 | static struct tracepoint * | |
823 | add_tracepoint (int num, CORE_ADDR addr) | |
824 | { | |
825 | struct tracepoint *tpoint; | |
826 | ||
827 | tpoint = xmalloc (sizeof (struct tracepoint)); | |
828 | tpoint->number = num; | |
829 | tpoint->address = addr; | |
830 | tpoint->numactions = 0; | |
831 | tpoint->actions = NULL; | |
832 | tpoint->actions_str = NULL; | |
833 | tpoint->cond = NULL; | |
834 | tpoint->num_step_actions = 0; | |
835 | tpoint->step_actions = NULL; | |
836 | tpoint->step_actions_str = NULL; | |
837 | tpoint->source_strings = NULL; | |
838 | tpoint->handle = NULL; | |
839 | tpoint->next = NULL; | |
840 | ||
841 | if (!last_tracepoint) | |
842 | tracepoints = tpoint; | |
843 | else | |
844 | last_tracepoint->next = tpoint; | |
845 | last_tracepoint = tpoint; | |
846 | ||
847 | seen_step_action_flag = 0; | |
848 | ||
849 | return tpoint; | |
850 | } | |
851 | ||
852 | /* Return the tracepoint with the given number and address, or NULL. */ | |
853 | ||
854 | static struct tracepoint * | |
855 | find_tracepoint (int id, CORE_ADDR addr) | |
856 | { | |
857 | struct tracepoint *tpoint; | |
858 | ||
859 | for (tpoint = tracepoints; tpoint; tpoint = tpoint->next) | |
860 | if (tpoint->number == id && tpoint->address == addr) | |
861 | return tpoint; | |
862 | ||
863 | return NULL; | |
864 | } | |
865 | ||
866 | /* There may be several tracepoints with the same number (because they | |
867 | are "locations", in GDB parlance); return the next one after the | |
868 | given tracepoint, or search from the beginning of the list if the | |
869 | first argument is NULL. */ | |
870 | ||
871 | static struct tracepoint * | |
872 | find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num) | |
873 | { | |
874 | struct tracepoint *tpoint; | |
875 | ||
876 | if (prev_tp) | |
877 | tpoint = prev_tp->next; | |
878 | else | |
879 | tpoint = tracepoints; | |
880 | for (; tpoint; tpoint = tpoint->next) | |
881 | if (tpoint->number == num) | |
882 | return tpoint; | |
883 | ||
884 | return NULL; | |
885 | } | |
886 | ||
887 | static char * | |
888 | save_string (const char *str, size_t len) | |
889 | { | |
890 | char *s; | |
891 | ||
892 | s = xmalloc (len + 1); | |
893 | memcpy (s, str, len); | |
894 | s[len] = '\0'; | |
895 | ||
896 | return s; | |
897 | } | |
898 | ||
899 | /* Append another action to perform when the tracepoint triggers. */ | |
900 | ||
901 | static void | |
902 | add_tracepoint_action (struct tracepoint *tpoint, char *packet) | |
903 | { | |
904 | char *act; | |
905 | ||
906 | if (*packet == 'S') | |
907 | { | |
908 | seen_step_action_flag = 1; | |
909 | ++packet; | |
910 | } | |
911 | ||
912 | act = packet; | |
913 | ||
914 | while (*act) | |
915 | { | |
916 | char *act_start = act; | |
917 | struct tracepoint_action *action = NULL; | |
918 | ||
919 | switch (*act) | |
920 | { | |
921 | case 'M': | |
922 | { | |
923 | struct collect_memory_action *maction; | |
924 | ULONGEST basereg; | |
925 | int is_neg; | |
926 | ||
927 | maction = xmalloc (sizeof *maction); | |
928 | maction->base.type = *act; | |
929 | action = &maction->base; | |
930 | ||
931 | ++act; | |
932 | is_neg = (*act == '-'); | |
933 | if (*act == '-') | |
934 | ++act; | |
935 | act = unpack_varlen_hex (act, &basereg); | |
936 | ++act; | |
937 | act = unpack_varlen_hex (act, &maction->addr); | |
938 | ++act; | |
939 | act = unpack_varlen_hex (act, &maction->len); | |
940 | maction->basereg = (is_neg | |
941 | ? - (int) basereg | |
942 | : (int) basereg); | |
943 | trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)", | |
944 | pulongest (maction->len), | |
945 | paddress (maction->addr), maction->basereg); | |
946 | break; | |
947 | } | |
948 | case 'R': | |
949 | { | |
950 | struct collect_registers_action *raction; | |
951 | ||
952 | raction = xmalloc (sizeof *raction); | |
953 | raction->base.type = *act; | |
954 | action = &raction->base; | |
955 | ||
956 | trace_debug ("Want to collect registers"); | |
957 | ++act; | |
958 | /* skip past hex digits of mask for now */ | |
959 | while (isxdigit(*act)) | |
960 | ++act; | |
961 | break; | |
962 | } | |
963 | case 'S': | |
964 | trace_debug ("Unexpected step action, ignoring"); | |
965 | ++act; | |
966 | break; | |
967 | case 'X': | |
968 | { | |
969 | struct eval_expr_action *xaction; | |
970 | ||
971 | xaction = xmalloc (sizeof (*xaction)); | |
972 | xaction->base.type = *act; | |
973 | action = &xaction->base; | |
974 | ||
975 | trace_debug ("Want to evaluate expression"); | |
976 | xaction->expr = parse_agent_expr (&act); | |
977 | break; | |
978 | } | |
979 | default: | |
980 | trace_debug ("unknown trace action '%c', ignoring...", *act); | |
981 | break; | |
982 | case '-': | |
983 | break; | |
984 | } | |
985 | ||
986 | if (action == NULL) | |
987 | break; | |
988 | ||
989 | if (seen_step_action_flag) | |
990 | { | |
991 | tpoint->num_step_actions++; | |
992 | ||
993 | tpoint->step_actions | |
994 | = xrealloc (tpoint->step_actions, | |
995 | (sizeof (*tpoint->step_actions) | |
996 | * tpoint->num_step_actions)); | |
997 | tpoint->step_actions_str | |
998 | = xrealloc (tpoint->step_actions_str, | |
999 | (sizeof (*tpoint->step_actions_str) | |
1000 | * tpoint->num_step_actions)); | |
1001 | tpoint->step_actions[tpoint->num_step_actions - 1] = action; | |
1002 | tpoint->step_actions_str[tpoint->num_step_actions - 1] | |
1003 | = save_string (act_start, act - act_start); | |
1004 | } | |
1005 | else | |
1006 | { | |
1007 | tpoint->numactions++; | |
1008 | tpoint->actions | |
1009 | = xrealloc (tpoint->actions, | |
1010 | sizeof (*tpoint->actions) * tpoint->numactions); | |
1011 | tpoint->actions_str | |
1012 | = xrealloc (tpoint->actions_str, | |
1013 | sizeof (*tpoint->actions_str) * tpoint->numactions); | |
1014 | tpoint->actions[tpoint->numactions - 1] = action; | |
1015 | tpoint->actions_str[tpoint->numactions - 1] | |
1016 | = save_string (act_start, act - act_start); | |
1017 | } | |
1018 | } | |
1019 | } | |
1020 | ||
1021 | /* Find or create a trace state variable with the given number. */ | |
1022 | ||
1023 | static struct trace_state_variable * | |
1024 | get_trace_state_variable (int num) | |
1025 | { | |
1026 | struct trace_state_variable *tsv; | |
1027 | ||
1028 | /* Search for an existing variable. */ | |
1029 | for (tsv = trace_state_variables; tsv; tsv = tsv->next) | |
1030 | if (tsv->number == num) | |
1031 | return tsv; | |
1032 | ||
1033 | return NULL; | |
1034 | } | |
1035 | ||
1036 | /* Find or create a trace state variable with the given number. */ | |
1037 | ||
1038 | static struct trace_state_variable * | |
1039 | create_trace_state_variable (int num) | |
1040 | { | |
1041 | struct trace_state_variable *tsv; | |
1042 | ||
1043 | tsv = get_trace_state_variable (num); | |
1044 | if (tsv != NULL) | |
1045 | return tsv; | |
1046 | ||
1047 | /* Create a new variable. */ | |
1048 | tsv = xmalloc (sizeof (struct trace_state_variable)); | |
1049 | tsv->number = num; | |
1050 | tsv->initial_value = 0; | |
1051 | tsv->value = 0; | |
1052 | tsv->getter = NULL; | |
1053 | tsv->name = NULL; | |
1054 | tsv->next = trace_state_variables; | |
1055 | trace_state_variables = tsv; | |
1056 | ||
1057 | return tsv; | |
1058 | } | |
1059 | ||
1060 | static LONGEST | |
1061 | get_trace_state_variable_value (int num) | |
1062 | { | |
1063 | struct trace_state_variable *tsv; | |
1064 | ||
1065 | tsv = get_trace_state_variable (num); | |
1066 | ||
1067 | if (!tsv) | |
1068 | { | |
1069 | trace_debug ("No trace state variable %d, skipping value get", num); | |
1070 | return 0; | |
1071 | } | |
1072 | ||
1073 | /* Call a getter function if we have one. While it's tempting to | |
1074 | set up something to only call the getter once per tracepoint hit, | |
1075 | it could run afoul of thread races. Better to let the getter | |
1076 | handle it directly, if necessary to worry about it. */ | |
1077 | if (tsv->getter) | |
1078 | tsv->value = (tsv->getter) (); | |
1079 | ||
1080 | trace_debug ("get_trace_state_variable_value(%d) ==> %s", | |
1081 | num, plongest (tsv->value)); | |
1082 | ||
1083 | return tsv->value; | |
1084 | } | |
1085 | ||
1086 | static void | |
1087 | set_trace_state_variable_value (int num, LONGEST val) | |
1088 | { | |
1089 | struct trace_state_variable *tsv; | |
1090 | ||
1091 | tsv = get_trace_state_variable (num); | |
1092 | ||
1093 | if (!tsv) | |
1094 | { | |
1095 | trace_debug ("No trace state variable %d, skipping value set", num); | |
1096 | return; | |
1097 | } | |
1098 | ||
1099 | tsv->value = val; | |
1100 | } | |
1101 | ||
1102 | static void | |
1103 | set_trace_state_variable_name (int num, const char *name) | |
1104 | { | |
1105 | struct trace_state_variable *tsv; | |
1106 | ||
1107 | tsv = get_trace_state_variable (num); | |
1108 | ||
1109 | if (!tsv) | |
1110 | { | |
1111 | trace_debug ("No trace state variable %d, skipping name set", num); | |
1112 | return; | |
1113 | } | |
1114 | ||
1115 | tsv->name = (char *) name; | |
1116 | } | |
1117 | ||
1118 | static void | |
1119 | set_trace_state_variable_getter (int num, LONGEST (*getter) (void)) | |
1120 | { | |
1121 | struct trace_state_variable *tsv; | |
1122 | ||
1123 | tsv = get_trace_state_variable (num); | |
1124 | ||
1125 | if (!tsv) | |
1126 | { | |
1127 | trace_debug ("No trace state variable %d, skipping getter set", num); | |
1128 | return; | |
1129 | } | |
1130 | ||
1131 | tsv->getter = getter; | |
1132 | } | |
1133 | ||
1134 | /* Add a raw traceframe for the given tracepoint. */ | |
1135 | ||
1136 | static struct traceframe * | |
1137 | add_traceframe (struct tracepoint *tpoint) | |
1138 | { | |
1139 | struct traceframe *tframe; | |
1140 | ||
1141 | tframe = trace_buffer_alloc (sizeof (struct traceframe)); | |
1142 | ||
1143 | if (tframe == NULL) | |
1144 | return NULL; | |
1145 | ||
1146 | tframe->tpnum = tpoint->number; | |
1147 | tframe->data_size = 0; | |
1148 | ||
1149 | return tframe; | |
1150 | } | |
1151 | ||
1152 | /* Add a block to the traceframe currently being worked on. */ | |
1153 | ||
1154 | static unsigned char * | |
1155 | add_traceframe_block (struct traceframe *tframe, int amt) | |
1156 | { | |
1157 | unsigned char *block; | |
1158 | ||
1159 | if (!tframe) | |
1160 | return NULL; | |
1161 | ||
1162 | block = trace_buffer_alloc (amt); | |
1163 | ||
1164 | if (!block) | |
1165 | return NULL; | |
1166 | ||
1167 | tframe->data_size += amt; | |
1168 | ||
1169 | return block; | |
1170 | } | |
1171 | ||
1172 | /* Flag that the current traceframe is finished. */ | |
1173 | ||
1174 | static void | |
1175 | finish_traceframe (struct traceframe *tframe) | |
1176 | { | |
1177 | ++traceframe_write_count; | |
1178 | ++traceframes_created; | |
1179 | } | |
1180 | ||
1181 | /* Given a traceframe number NUM, find the NUMth traceframe in the | |
1182 | buffer. */ | |
1183 | ||
1184 | static struct traceframe * | |
1185 | find_traceframe (int num) | |
1186 | { | |
1187 | struct traceframe *tframe; | |
1188 | int tfnum = 0; | |
1189 | ||
1190 | for (tframe = FIRST_TRACEFRAME (); | |
1191 | tframe->tpnum != 0; | |
1192 | tframe = NEXT_TRACEFRAME (tframe)) | |
1193 | { | |
1194 | if (tfnum == num) | |
1195 | return tframe; | |
1196 | ++tfnum; | |
1197 | } | |
1198 | ||
1199 | return NULL; | |
1200 | } | |
1201 | ||
1202 | static CORE_ADDR | |
1203 | get_traceframe_address (struct traceframe *tframe) | |
1204 | { | |
1205 | CORE_ADDR addr; | |
1206 | struct tracepoint *tpoint; | |
1207 | ||
1208 | addr = traceframe_get_pc (tframe); | |
1209 | ||
1210 | if (addr) | |
1211 | return addr; | |
1212 | ||
1213 | /* Fallback strategy, will be incorrect for while-stepping frames | |
1214 | and multi-location tracepoints. */ | |
1215 | tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum); | |
1216 | return tpoint->address; | |
1217 | } | |
1218 | ||
1219 | /* Search for the next traceframe whose address is inside or outside | |
1220 | the given range. */ | |
1221 | ||
1222 | static struct traceframe * | |
1223 | find_next_traceframe_in_range (CORE_ADDR lo, CORE_ADDR hi, int inside_p, | |
1224 | int *tfnump) | |
1225 | { | |
1226 | struct traceframe *tframe; | |
1227 | CORE_ADDR tfaddr; | |
1228 | ||
1229 | *tfnump = current_traceframe + 1; | |
1230 | tframe = find_traceframe (*tfnump); | |
1231 | /* The search is not supposed to wrap around. */ | |
1232 | if (!tframe) | |
1233 | { | |
1234 | *tfnump = -1; | |
1235 | return NULL; | |
1236 | } | |
1237 | ||
1238 | for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe)) | |
1239 | { | |
1240 | tfaddr = get_traceframe_address (tframe); | |
1241 | if (inside_p | |
1242 | ? (lo <= tfaddr && tfaddr <= hi) | |
1243 | : (lo > tfaddr || tfaddr > hi)) | |
1244 | return tframe; | |
1245 | ++*tfnump; | |
1246 | } | |
1247 | ||
1248 | *tfnump = -1; | |
1249 | return NULL; | |
1250 | } | |
1251 | ||
1252 | /* Search for the next traceframe recorded by the given tracepoint. | |
1253 | Note that for multi-location tracepoints, this will find whatever | |
1254 | location appears first. */ | |
1255 | ||
1256 | static struct traceframe * | |
1257 | find_next_traceframe_by_tracepoint (int num, int *tfnump) | |
1258 | { | |
1259 | struct traceframe *tframe; | |
1260 | ||
1261 | *tfnump = current_traceframe + 1; | |
1262 | tframe = find_traceframe (*tfnump); | |
1263 | /* The search is not supposed to wrap around. */ | |
1264 | if (!tframe) | |
1265 | { | |
1266 | *tfnump = -1; | |
1267 | return NULL; | |
1268 | } | |
1269 | ||
1270 | for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe)) | |
1271 | { | |
1272 | if (tframe->tpnum == num) | |
1273 | return tframe; | |
1274 | ++*tfnump; | |
1275 | } | |
1276 | ||
1277 | *tfnump = -1; | |
1278 | return NULL; | |
1279 | } | |
1280 | ||
1281 | /* Clear all past trace state. */ | |
1282 | ||
1283 | static void | |
1284 | cmd_qtinit (char *packet) | |
1285 | { | |
1286 | struct trace_state_variable *tsv, *prev, *next; | |
1287 | ||
1288 | /* Make sure we don't try to read from a trace frame. */ | |
1289 | current_traceframe = -1; | |
1290 | ||
1291 | trace_debug ("Initializing the trace"); | |
1292 | ||
1293 | clear_installed_tracepoints (); | |
1294 | clear_readonly_regions (); | |
1295 | ||
1296 | tracepoints = NULL; | |
1297 | last_tracepoint = NULL; | |
1298 | ||
1299 | /* Clear out any leftover trace state variables. Ones with target | |
1300 | defined getters should be kept however. */ | |
1301 | prev = NULL; | |
1302 | tsv = trace_state_variables; | |
1303 | while (tsv) | |
1304 | { | |
1305 | trace_debug ("Looking at var %d", tsv->number); | |
1306 | if (tsv->getter == NULL) | |
1307 | { | |
1308 | next = tsv->next; | |
1309 | if (prev) | |
1310 | prev->next = next; | |
1311 | else | |
1312 | trace_state_variables = next; | |
1313 | trace_debug ("Deleting var %d", tsv->number); | |
1314 | free (tsv); | |
1315 | tsv = next; | |
1316 | } | |
1317 | else | |
1318 | { | |
1319 | prev = tsv; | |
1320 | tsv = tsv->next; | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | clear_trace_buffer (); | |
1325 | ||
1326 | write_ok (packet); | |
1327 | } | |
1328 | ||
1329 | /* Restore the program to its pre-tracing state. This routine may be called | |
1330 | in error situations, so it needs to be careful about only restoring | |
1331 | from known-valid bits. */ | |
1332 | ||
1333 | static void | |
1334 | clear_installed_tracepoints (void) | |
1335 | { | |
1336 | struct tracepoint *tpoint; | |
1337 | struct tracepoint *prev_stpoint; | |
1338 | ||
1339 | prev_stpoint = NULL; | |
1340 | ||
1341 | /* Restore any bytes overwritten by tracepoints. */ | |
1342 | for (tpoint = tracepoints; tpoint; tpoint = tpoint->next) | |
1343 | { | |
1344 | if (!tpoint->enabled) | |
1345 | continue; | |
1346 | ||
1347 | /* Catch the case where we might try to remove a tracepoint that | |
1348 | was never actually installed. */ | |
1349 | if (tpoint->handle == NULL) | |
1350 | { | |
1351 | trace_debug ("Tracepoint %d at 0x%s was " | |
1352 | "never installed, nothing to clear", | |
1353 | tpoint->number, paddress (tpoint->address)); | |
1354 | continue; | |
1355 | } | |
1356 | ||
1357 | delete_breakpoint (tpoint->handle); | |
1358 | tpoint->handle = NULL; | |
1359 | } | |
1360 | } | |
1361 | ||
1362 | /* Parse a packet that defines a tracepoint. */ | |
1363 | ||
1364 | static void | |
1365 | cmd_qtdp (char *own_buf) | |
1366 | { | |
1367 | int tppacket; | |
1368 | ULONGEST num; | |
1369 | ULONGEST addr; | |
1370 | ULONGEST count; | |
1371 | struct tracepoint *tpoint; | |
1372 | char *actparm; | |
1373 | char *packet = own_buf; | |
1374 | ||
1375 | packet += strlen ("QTDP:"); | |
1376 | ||
1377 | /* A hyphen at the beginning marks a packet specifying actions for a | |
1378 | tracepoint already supplied. */ | |
1379 | tppacket = 1; | |
1380 | if (*packet == '-') | |
1381 | { | |
1382 | tppacket = 0; | |
1383 | ++packet; | |
1384 | } | |
1385 | packet = unpack_varlen_hex (packet, &num); | |
1386 | ++packet; /* skip a colon */ | |
1387 | packet = unpack_varlen_hex (packet, &addr); | |
1388 | ++packet; /* skip a colon */ | |
1389 | ||
1390 | /* See if we already have this tracepoint. */ | |
1391 | tpoint = find_tracepoint (num, addr); | |
1392 | ||
1393 | if (tppacket) | |
1394 | { | |
1395 | /* Duplicate tracepoints are never allowed. */ | |
1396 | if (tpoint) | |
1397 | { | |
1398 | trace_debug ("Tracepoint error: tracepoint %d" | |
1399 | " at 0x%s already exists", | |
1400 | (int) num, paddress (addr)); | |
1401 | write_enn (own_buf); | |
1402 | return; | |
1403 | } | |
1404 | ||
1405 | tpoint = add_tracepoint (num, addr); | |
1406 | ||
1407 | tpoint->enabled = (*packet == 'E'); | |
1408 | ++packet; /* skip 'E' */ | |
1409 | ++packet; /* skip a colon */ | |
1410 | packet = unpack_varlen_hex (packet, &count); | |
1411 | tpoint->step_count = count; | |
1412 | ++packet; /* skip a colon */ | |
1413 | packet = unpack_varlen_hex (packet, &count); | |
1414 | tpoint->pass_count = count; | |
1415 | /* See if we have any of the additional optional fields. */ | |
1416 | while (*packet == ':') | |
1417 | { | |
1418 | ++packet; | |
1419 | if (*packet == 'X') | |
1420 | { | |
1421 | actparm = (char *) packet; | |
1422 | tpoint->cond = parse_agent_expr (&actparm); | |
1423 | packet = actparm; | |
1424 | } | |
1425 | else if (*packet == '-') | |
1426 | break; | |
1427 | else if (*packet == '\0') | |
1428 | break; | |
1429 | else | |
1430 | trace_debug ("Unknown optional tracepoint field"); | |
1431 | } | |
1432 | if (*packet == '-') | |
1433 | trace_debug ("Also has actions\n"); | |
1434 | ||
1435 | trace_debug ("Defined tracepoint %d at 0x%s, " | |
1436 | "enabled %d step %ld pass %ld", | |
1437 | tpoint->number, paddress (tpoint->address), | |
1438 | tpoint->enabled, | |
1439 | tpoint->step_count, tpoint->pass_count); | |
1440 | } | |
1441 | else if (tpoint) | |
1442 | add_tracepoint_action (tpoint, packet); | |
1443 | else | |
1444 | { | |
1445 | trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found", | |
1446 | (int) num, paddress (addr)); | |
1447 | write_enn (own_buf); | |
1448 | return; | |
1449 | } | |
1450 | ||
1451 | write_ok (own_buf); | |
1452 | } | |
1453 | ||
1454 | static void | |
1455 | cmd_qtdpsrc (char *own_buf) | |
1456 | { | |
1457 | ULONGEST num, addr, start, slen; | |
1458 | struct tracepoint *tpoint; | |
1459 | char *packet = own_buf; | |
1460 | char *saved, *srctype, *src; | |
1461 | size_t nbytes; | |
1462 | struct source_string *last, *newlast; | |
1463 | ||
1464 | packet += strlen ("QTDPsrc:"); | |
1465 | ||
1466 | packet = unpack_varlen_hex (packet, &num); | |
1467 | ++packet; /* skip a colon */ | |
1468 | packet = unpack_varlen_hex (packet, &addr); | |
1469 | ++packet; /* skip a colon */ | |
1470 | ||
1471 | /* See if we already have this tracepoint. */ | |
1472 | tpoint = find_tracepoint (num, addr); | |
1473 | ||
1474 | if (!tpoint) | |
1475 | { | |
1476 | trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found", | |
1477 | (int) num, paddress (addr)); | |
1478 | write_enn (own_buf); | |
1479 | return; | |
1480 | } | |
1481 | ||
1482 | saved = packet; | |
1483 | packet = strchr (packet, ':'); | |
1484 | srctype = xmalloc (packet - saved + 1); | |
1485 | memcpy (srctype, saved, packet - saved); | |
1486 | srctype[packet - saved] = '\0'; | |
1487 | ++packet; | |
1488 | packet = unpack_varlen_hex (packet, &start); | |
1489 | ++packet; /* skip a colon */ | |
1490 | packet = unpack_varlen_hex (packet, &slen); | |
1491 | ++packet; /* skip a colon */ | |
1492 | src = xmalloc (slen + 1); | |
1493 | nbytes = unhexify (src, packet, strlen (packet) / 2); | |
1494 | src[nbytes] = '\0'; | |
1495 | ||
1496 | newlast = xmalloc (sizeof (struct source_string)); | |
1497 | newlast->type = srctype; | |
1498 | newlast->str = src; | |
1499 | newlast->next = NULL; | |
1500 | /* Always add a source string to the end of the list; | |
1501 | this keeps sequences of actions/commands in the right | |
1502 | order. */ | |
1503 | if (tpoint->source_strings) | |
1504 | { | |
1505 | for (last = tpoint->source_strings; last->next; last = last->next) | |
1506 | ; | |
1507 | last->next = newlast; | |
1508 | } | |
1509 | else | |
1510 | tpoint->source_strings = newlast; | |
1511 | ||
1512 | write_ok (own_buf); | |
1513 | } | |
1514 | ||
1515 | static void | |
1516 | cmd_qtdv (char *own_buf) | |
1517 | { | |
1518 | ULONGEST num, val, builtin; | |
1519 | char *varname; | |
1520 | size_t nbytes; | |
1521 | struct trace_state_variable *tsv; | |
1522 | char *packet = own_buf; | |
1523 | ||
1524 | packet += strlen ("QTDV:"); | |
1525 | ||
1526 | packet = unpack_varlen_hex (packet, &num); | |
1527 | ++packet; /* skip a colon */ | |
1528 | packet = unpack_varlen_hex (packet, &val); | |
1529 | ++packet; /* skip a colon */ | |
1530 | packet = unpack_varlen_hex (packet, &builtin); | |
1531 | ++packet; /* skip a colon */ | |
1532 | ||
1533 | nbytes = strlen (packet) / 2; | |
1534 | varname = xmalloc (nbytes + 1); | |
1535 | nbytes = unhexify (varname, packet, nbytes); | |
1536 | varname[nbytes] = '\0'; | |
1537 | ||
1538 | tsv = create_trace_state_variable (num); | |
1539 | tsv->initial_value = (LONGEST) val; | |
1540 | tsv->name = varname; | |
1541 | ||
1542 | set_trace_state_variable_value (num, (LONGEST) val); | |
1543 | ||
1544 | write_ok (own_buf); | |
1545 | } | |
1546 | ||
1547 | static void | |
1548 | cmd_qtv (char *own_buf) | |
1549 | { | |
1550 | ULONGEST num; | |
1551 | LONGEST val; | |
1552 | int err; | |
1553 | char *packet = own_buf; | |
1554 | ||
1555 | packet += strlen ("qTV:"); | |
1556 | packet = unpack_varlen_hex (packet, &num); | |
1557 | ||
1558 | if (current_traceframe >= 0) | |
1559 | { | |
1560 | err = traceframe_read_tsv ((int) num, &val); | |
1561 | if (err) | |
1562 | { | |
1563 | strcpy (own_buf, "U"); | |
1564 | return; | |
1565 | } | |
1566 | } | |
1567 | /* Only make tsv's be undefined before the first trace run. After a | |
1568 | trace run is over, the user might want to see the last value of | |
1569 | the tsv, and it might not be available in a traceframe. */ | |
1570 | else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0) | |
1571 | { | |
1572 | strcpy (own_buf, "U"); | |
1573 | return; | |
1574 | } | |
1575 | else | |
1576 | val = get_trace_state_variable_value (num); | |
1577 | ||
1578 | sprintf (own_buf, "V%s", phex_nz (val, 0)); | |
1579 | } | |
1580 | ||
1581 | /* Clear out the list of readonly regions. */ | |
1582 | ||
1583 | static void | |
1584 | clear_readonly_regions (void) | |
1585 | { | |
1586 | struct readonly_region *roreg; | |
1587 | ||
1588 | while (readonly_regions) | |
1589 | { | |
1590 | roreg = readonly_regions; | |
1591 | readonly_regions = readonly_regions->next; | |
1592 | free (roreg); | |
1593 | } | |
1594 | } | |
1595 | ||
1596 | /* Parse the collection of address ranges whose contents GDB believes | |
1597 | to be unchanging and so can be read directly from target memory | |
1598 | even while looking at a traceframe. */ | |
1599 | ||
1600 | static void | |
1601 | cmd_qtro (char *own_buf) | |
1602 | { | |
1603 | ULONGEST start, end; | |
1604 | struct readonly_region *roreg; | |
1605 | char *packet = own_buf; | |
1606 | ||
1607 | trace_debug ("Want to mark readonly regions"); | |
1608 | ||
1609 | clear_readonly_regions (); | |
1610 | ||
1611 | packet += strlen ("QTro"); | |
1612 | ||
1613 | while (*packet == ':') | |
1614 | { | |
1615 | ++packet; /* skip a colon */ | |
1616 | packet = unpack_varlen_hex (packet, &start); | |
1617 | ++packet; /* skip a comma */ | |
1618 | packet = unpack_varlen_hex (packet, &end); | |
1619 | roreg = xmalloc (sizeof (struct readonly_region)); | |
1620 | roreg->start = start; | |
1621 | roreg->end = end; | |
1622 | roreg->next = readonly_regions; | |
1623 | readonly_regions = roreg; | |
1624 | trace_debug ("Added readonly region from 0x%s to 0x%s", | |
1625 | paddress (roreg->start), paddress (roreg->end)); | |
1626 | } | |
1627 | ||
1628 | write_ok (own_buf); | |
1629 | } | |
1630 | ||
1631 | /* Test to see if the given range is in our list of readonly ranges. | |
1632 | We only test for being entirely within a range, GDB is not going to | |
1633 | send a single memory packet that spans multiple regions. */ | |
1634 | ||
1635 | int | |
1636 | in_readonly_region (CORE_ADDR addr, ULONGEST length) | |
1637 | { | |
1638 | struct readonly_region *roreg; | |
1639 | ||
1640 | for (roreg = readonly_regions; roreg; roreg = roreg->next) | |
1641 | if (roreg->start <= addr && (addr + length - 1) <= roreg->end) | |
1642 | return 1; | |
1643 | ||
1644 | return 0; | |
1645 | } | |
1646 | ||
1647 | static void | |
1648 | cmd_qtstart (char *packet) | |
1649 | { | |
1650 | struct tracepoint *tpoint; | |
1651 | int slow_tracepoint_count; | |
1652 | ||
1653 | trace_debug ("Starting the trace"); | |
1654 | ||
1655 | slow_tracepoint_count = 0; | |
1656 | ||
1657 | *packet = '\0'; | |
1658 | ||
1659 | /* Install tracepoints. */ | |
1660 | for (tpoint = tracepoints; tpoint; tpoint = tpoint->next) | |
1661 | { | |
1662 | /* Ensure all the hit counts start at zero. */ | |
1663 | tpoint->hit_count = 0; | |
1664 | ||
1665 | if (!tpoint->enabled) | |
1666 | continue; | |
1667 | ||
1668 | ++slow_tracepoint_count; | |
1669 | ||
1670 | /* Tracepoints are installed as memory breakpoints. Just go | |
1671 | ahead and install the trap. The breakpoints module handles | |
1672 | duplicated breakpoints, and the memory read routine handles | |
1673 | un-patching traps from memory reads. */ | |
1674 | tpoint->handle = set_breakpoint_at (tpoint->address, tracepoint_handler); | |
1675 | ||
1676 | /* Any failure is sufficient cause to give up. */ | |
1677 | if (tpoint->handle == NULL) | |
1678 | break; | |
1679 | } | |
1680 | ||
1681 | /* Any error in tracepoint insertion is unacceptable; better to | |
1682 | address the problem now, than end up with a useless or misleading | |
1683 | trace run. */ | |
1684 | if (tpoint != NULL) | |
1685 | { | |
1686 | clear_installed_tracepoints (); | |
1687 | if (*packet == '\0') | |
1688 | write_enn (packet); | |
1689 | return; | |
1690 | } | |
1691 | ||
1692 | stopping_tracepoint = NULL; | |
1693 | trace_buffer_is_full = 0; | |
1694 | expr_eval_result = expr_eval_no_error; | |
1695 | error_tracepoint = NULL; | |
1696 | ||
1697 | /* Tracing is now active, hits will now start being logged. */ | |
1698 | tracing = 1; | |
1699 | ||
1700 | write_ok (packet); | |
1701 | } | |
1702 | ||
1703 | /* End a tracing run, filling in a stop reason to report back to GDB, | |
1704 | and removing the tracepoints from the code. */ | |
1705 | ||
8336d594 | 1706 | void |
219f2f23 PA |
1707 | stop_tracing (void) |
1708 | { | |
1709 | if (!tracing) | |
1710 | { | |
1711 | trace_debug ("Tracing is already off, ignoring"); | |
1712 | return; | |
1713 | } | |
1714 | ||
1715 | trace_debug ("Stopping the trace"); | |
1716 | ||
1717 | /* Stop logging. Tracepoints can still be hit, but they will not be | |
1718 | recorded. */ | |
1719 | tracing = 0; | |
1720 | ||
1721 | tracing_stop_reason = "t???"; | |
1722 | tracing_stop_tpnum = 0; | |
1723 | if (stopping_tracepoint) | |
1724 | { | |
1725 | trace_debug ("Stopping the trace because " | |
1726 | "tracepoint %d was hit %ld times", | |
1727 | stopping_tracepoint->number, | |
1728 | stopping_tracepoint->pass_count); | |
1729 | tracing_stop_reason = "tpasscount"; | |
1730 | tracing_stop_tpnum = stopping_tracepoint->number; | |
1731 | } | |
1732 | else if (trace_buffer_is_full) | |
1733 | { | |
1734 | trace_debug ("Stopping the trace because the trace buffer is full"); | |
1735 | tracing_stop_reason = "tfull"; | |
1736 | } | |
1737 | else if (expr_eval_result != expr_eval_no_error) | |
1738 | { | |
1739 | trace_debug ("Stopping the trace because of an expression eval error"); | |
1740 | tracing_stop_reason = eval_result_names[expr_eval_result]; | |
1741 | tracing_stop_tpnum = error_tracepoint->number; | |
1742 | } | |
8336d594 PA |
1743 | else if (!gdb_connected ()) |
1744 | { | |
1745 | trace_debug ("Stopping the trace because GDB disconnected"); | |
1746 | tracing_stop_reason = "tdisconnected"; | |
1747 | } | |
219f2f23 PA |
1748 | else |
1749 | { | |
1750 | trace_debug ("Stopping the trace because of a tstop command"); | |
1751 | tracing_stop_reason = "tstop"; | |
1752 | } | |
1753 | ||
1754 | stopping_tracepoint = NULL; | |
1755 | error_tracepoint = NULL; | |
1756 | ||
1757 | /* Clear out the tracepoints. */ | |
1758 | clear_installed_tracepoints (); | |
1759 | } | |
1760 | ||
1761 | static void | |
1762 | cmd_qtstop (char *packet) | |
1763 | { | |
1764 | stop_tracing (); | |
1765 | write_ok (packet); | |
1766 | } | |
1767 | ||
8336d594 PA |
1768 | static void |
1769 | cmd_qtdisconnected (char *own_buf) | |
1770 | { | |
1771 | ULONGEST setting; | |
1772 | char *packet = own_buf; | |
1773 | ||
1774 | packet += strlen ("QTDisconnected:"); | |
1775 | ||
1776 | unpack_varlen_hex (packet, &setting); | |
1777 | ||
1778 | write_ok (own_buf); | |
1779 | ||
1780 | disconnected_tracing = setting; | |
1781 | } | |
1782 | ||
219f2f23 PA |
1783 | static void |
1784 | cmd_qtframe (char *own_buf) | |
1785 | { | |
1786 | ULONGEST frame, pc, lo, hi, num; | |
1787 | int tfnum, tpnum; | |
1788 | struct traceframe *tframe; | |
1789 | char *packet = own_buf; | |
1790 | ||
1791 | packet += strlen ("QTFrame:"); | |
1792 | ||
1793 | if (strncmp (packet, "pc:", strlen ("pc:")) == 0) | |
1794 | { | |
1795 | packet += strlen ("pc:"); | |
1796 | packet = unpack_varlen_hex (packet, &pc); | |
1797 | trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc)); | |
1798 | tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum); | |
1799 | } | |
1800 | else if (strncmp (packet, "range:", strlen ("range:")) == 0) | |
1801 | { | |
1802 | packet += strlen ("range:"); | |
1803 | packet = unpack_varlen_hex (packet, &lo); | |
1804 | ++packet; | |
1805 | packet = unpack_varlen_hex (packet, &hi); | |
1806 | trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s", | |
1807 | paddress (lo), paddress (hi)); | |
1808 | tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum); | |
1809 | } | |
1810 | else if (strncmp (packet, "outside:", strlen ("outside:")) == 0) | |
1811 | { | |
1812 | packet += strlen ("outside:"); | |
1813 | packet = unpack_varlen_hex (packet, &lo); | |
1814 | ++packet; | |
1815 | packet = unpack_varlen_hex (packet, &hi); | |
1816 | trace_debug ("Want to find next traceframe " | |
1817 | "outside the range 0x%s to 0x%s", | |
1818 | paddress (lo), paddress (hi)); | |
1819 | tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum); | |
1820 | } | |
1821 | else if (strncmp (packet, "tdp:", strlen ("tdp:")) == 0) | |
1822 | { | |
1823 | packet += strlen ("tdp:"); | |
1824 | packet = unpack_varlen_hex (packet, &num); | |
1825 | tpnum = (int) num; | |
1826 | trace_debug ("Want to find next traceframe for tracepoint %d", tpnum); | |
1827 | tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum); | |
1828 | } | |
1829 | else | |
1830 | { | |
1831 | unpack_varlen_hex (packet, &frame); | |
1832 | tfnum = (int) frame; | |
1833 | if (tfnum == -1) | |
1834 | { | |
1835 | trace_debug ("Want to stop looking at traceframes"); | |
1836 | current_traceframe = -1; | |
1837 | write_ok (own_buf); | |
1838 | return; | |
1839 | } | |
1840 | trace_debug ("Want to look at traceframe %d", tfnum); | |
1841 | tframe = find_traceframe (tfnum); | |
1842 | } | |
1843 | ||
1844 | if (tframe) | |
1845 | { | |
1846 | current_traceframe = tfnum; | |
1847 | sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum); | |
1848 | } | |
1849 | else | |
1850 | sprintf (own_buf, "F-1"); | |
1851 | } | |
1852 | ||
1853 | static void | |
1854 | cmd_qtstatus (char *packet) | |
1855 | { | |
1856 | char *stop_reason_rsp = NULL; | |
1857 | ||
1858 | trace_debug ("Returning trace status as %d, stop reason %s", | |
1859 | tracing, tracing_stop_reason); | |
1860 | ||
1861 | stop_reason_rsp = (char *) tracing_stop_reason; | |
1862 | ||
1863 | /* The user visible error string in terror needs to be hex encoded. | |
1864 | We leave it as plain string in `tracepoint_stop_reason' to ease | |
1865 | debugging. */ | |
1866 | if (strncmp (stop_reason_rsp, "terror:", strlen ("terror:")) == 0) | |
1867 | { | |
1868 | const char *result_name; | |
1869 | int hexstr_len; | |
1870 | char *p; | |
1871 | ||
1872 | result_name = stop_reason_rsp + strlen ("terror:"); | |
1873 | hexstr_len = strlen (result_name) * 2; | |
1874 | p = stop_reason_rsp = alloca (strlen ("terror:") + hexstr_len + 1); | |
1875 | strcpy (p, "terror:"); | |
1876 | p += strlen (p); | |
1877 | convert_int_to_ascii ((gdb_byte *) result_name, p, strlen (result_name)); | |
1878 | } | |
1879 | ||
8336d594 PA |
1880 | sprintf (packet, |
1881 | "T%d;" | |
1882 | "%s:%x;" | |
1883 | "tframes:%x;tcreated:%x;" | |
1884 | "tfree:%x;tsize:%s;" | |
1885 | "circular:%d;" | |
1886 | "disconn:%d", | |
623ccd72 | 1887 | tracing ? 1 : 0, |
219f2f23 PA |
1888 | stop_reason_rsp, tracing_stop_tpnum, |
1889 | traceframe_count, traceframes_created, | |
8336d594 PA |
1890 | free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0), |
1891 | circular_trace_buffer, | |
1892 | disconnected_tracing); | |
219f2f23 PA |
1893 | } |
1894 | ||
1895 | /* State variables to help return all the tracepoint bits. */ | |
1896 | static struct tracepoint *cur_tpoint; | |
1897 | static int cur_action; | |
1898 | static int cur_step_action; | |
1899 | static struct source_string *cur_source_string; | |
1900 | static struct trace_state_variable *cur_tsv; | |
1901 | ||
1902 | /* Compose a response that is an imitation of the syntax by which the | |
1903 | tracepoint was originally downloaded. */ | |
1904 | ||
1905 | static void | |
1906 | response_tracepoint (char *packet, struct tracepoint *tpoint) | |
1907 | { | |
1908 | char *buf; | |
1909 | ||
1910 | sprintf (packet, "T%x:%s:%c:%lx:%lx", tpoint->number, | |
1911 | paddress (tpoint->address), | |
1912 | (tpoint->enabled ? 'E' : 'D'), tpoint->step_count, | |
1913 | tpoint->pass_count); | |
1914 | ||
1915 | if (tpoint->cond) | |
1916 | { | |
1917 | buf = unparse_agent_expr (tpoint->cond); | |
1918 | sprintf (packet + strlen (packet), ":X%x,%s", | |
1919 | tpoint->cond->length, buf); | |
1920 | free (buf); | |
1921 | } | |
1922 | } | |
1923 | ||
1924 | /* Compose a response that is an imitation of the syntax by which the | |
1925 | tracepoint action was originally downloaded (with the difference | |
1926 | that due to the way we store the actions, this will output a packet | |
1927 | per action, while GDB could have combined more than one action | |
1928 | per-packet. */ | |
1929 | ||
1930 | static void | |
1931 | response_action (char *packet, struct tracepoint *tpoint, | |
1932 | char *taction, int step) | |
1933 | { | |
1934 | sprintf (packet, "%c%x:%s:%s", | |
1935 | (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address), | |
1936 | taction); | |
1937 | } | |
1938 | ||
1939 | /* Compose a response that is an imitation of the syntax by which the | |
1940 | tracepoint source piece was originally downloaded. */ | |
1941 | ||
1942 | static void | |
1943 | response_source (char *packet, | |
1944 | struct tracepoint *tpoint, struct source_string *src) | |
1945 | { | |
1946 | char *buf; | |
1947 | int len; | |
1948 | ||
1949 | len = strlen (src->str); | |
1950 | buf = alloca (len * 2 + 1); | |
1951 | convert_int_to_ascii ((gdb_byte *) src->str, buf, len); | |
1952 | ||
1953 | sprintf (packet, "Z%x:%s:%s:%x:%x:%s", | |
1954 | tpoint->number, paddress (tpoint->address), | |
1955 | src->type, 0, len, buf); | |
1956 | } | |
1957 | ||
1958 | /* Return the first piece of tracepoint definition, and initialize the | |
1959 | state machine that will iterate through all the tracepoint | |
1960 | bits. */ | |
1961 | ||
1962 | static void | |
1963 | cmd_qtfp (char *packet) | |
1964 | { | |
1965 | trace_debug ("Returning first tracepoint definition piece"); | |
1966 | ||
1967 | cur_tpoint = tracepoints; | |
1968 | cur_action = cur_step_action = -1; | |
1969 | cur_source_string = NULL; | |
1970 | ||
1971 | if (cur_tpoint) | |
1972 | response_tracepoint (packet, cur_tpoint); | |
1973 | else | |
1974 | strcpy (packet, "l"); | |
1975 | } | |
1976 | ||
1977 | /* Return additional pieces of tracepoint definition. Each action and | |
1978 | stepping action must go into its own packet, because of packet size | |
1979 | limits, and so we use state variables to deliver one piece at a | |
1980 | time. */ | |
1981 | ||
1982 | static void | |
1983 | cmd_qtsp (char *packet) | |
1984 | { | |
1985 | trace_debug ("Returning subsequent tracepoint definition piece"); | |
1986 | ||
1987 | if (!cur_tpoint) | |
1988 | { | |
1989 | /* This case would normally never occur, but be prepared for | |
1990 | GDB misbehavior. */ | |
1991 | strcpy (packet, "l"); | |
1992 | } | |
1993 | else if (cur_action < cur_tpoint->numactions - 1) | |
1994 | { | |
1995 | ++cur_action; | |
1996 | response_action (packet, cur_tpoint, | |
1997 | cur_tpoint->actions_str[cur_action], 0); | |
1998 | } | |
1999 | else if (cur_step_action < cur_tpoint->num_step_actions - 1) | |
2000 | { | |
2001 | ++cur_step_action; | |
2002 | response_action (packet, cur_tpoint, | |
2003 | cur_tpoint->step_actions_str[cur_step_action], 1); | |
2004 | } | |
2005 | else if ((cur_source_string | |
2006 | ? cur_source_string->next | |
2007 | : cur_tpoint->source_strings)) | |
2008 | { | |
2009 | if (cur_source_string) | |
2010 | cur_source_string = cur_source_string->next; | |
2011 | else | |
2012 | cur_source_string = cur_tpoint->source_strings; | |
2013 | response_source (packet, cur_tpoint, cur_source_string); | |
2014 | } | |
2015 | else | |
2016 | { | |
2017 | cur_tpoint = cur_tpoint->next; | |
2018 | cur_action = cur_step_action = -1; | |
2019 | cur_source_string = NULL; | |
2020 | if (cur_tpoint) | |
2021 | response_tracepoint (packet, cur_tpoint); | |
2022 | else | |
2023 | strcpy (packet, "l"); | |
2024 | } | |
2025 | } | |
2026 | ||
2027 | /* Compose a response that is an imitation of the syntax by which the | |
2028 | trace state variable was originally downloaded. */ | |
2029 | ||
2030 | static void | |
2031 | response_tsv (char *packet, struct trace_state_variable *tsv) | |
2032 | { | |
2033 | char *buf = (char *) ""; | |
2034 | int namelen; | |
2035 | ||
2036 | if (tsv->name) | |
2037 | { | |
2038 | namelen = strlen (tsv->name); | |
2039 | buf = alloca (namelen * 2 + 1); | |
2040 | convert_int_to_ascii ((gdb_byte *) tsv->name, buf, namelen); | |
2041 | } | |
2042 | ||
2043 | sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0), | |
2044 | tsv->getter ? 1 : 0, buf); | |
2045 | } | |
2046 | ||
2047 | /* Return the first trace state variable definition, and initialize | |
2048 | the state machine that will iterate through all the tsv bits. */ | |
2049 | ||
2050 | static void | |
2051 | cmd_qtfv (char *packet) | |
2052 | { | |
2053 | trace_debug ("Returning first trace state variable definition"); | |
2054 | ||
2055 | cur_tsv = trace_state_variables; | |
2056 | ||
2057 | if (cur_tsv) | |
2058 | response_tsv (packet, cur_tsv); | |
2059 | else | |
2060 | strcpy (packet, "l"); | |
2061 | } | |
2062 | ||
2063 | /* Return additional trace state variable definitions. */ | |
2064 | ||
2065 | static void | |
2066 | cmd_qtsv (char *packet) | |
2067 | { | |
2068 | trace_debug ("Returning first trace state variable definition"); | |
2069 | ||
2070 | if (!cur_tpoint) | |
2071 | { | |
2072 | /* This case would normally never occur, but be prepared for | |
2073 | GDB misbehavior. */ | |
2074 | strcpy (packet, "l"); | |
2075 | } | |
2076 | else if (cur_tsv) | |
2077 | { | |
2078 | cur_tsv = cur_tsv->next; | |
2079 | if (cur_tsv) | |
2080 | response_tsv (packet, cur_tsv); | |
2081 | else | |
2082 | strcpy (packet, "l"); | |
2083 | } | |
2084 | else | |
2085 | strcpy (packet, "l"); | |
2086 | } | |
2087 | ||
2088 | /* Respond to qTBuffer packet with a block of raw data from the trace | |
2089 | buffer. GDB may ask for a lot, but we are allowed to reply with | |
2090 | only as much as will fit within packet limits or whatever. */ | |
2091 | ||
2092 | static void | |
2093 | cmd_qtbuffer (char *own_buf) | |
2094 | { | |
2095 | ULONGEST offset, num, tot; | |
2096 | unsigned char *tbp; | |
2097 | char *packet = own_buf; | |
2098 | ||
2099 | packet += strlen ("qTBuffer:"); | |
2100 | ||
2101 | packet = unpack_varlen_hex (packet, &offset); | |
2102 | ++packet; /* skip a comma */ | |
2103 | packet = unpack_varlen_hex (packet, &num); | |
2104 | ||
2105 | trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s", | |
2106 | (int) num, pulongest (offset)); | |
2107 | ||
2108 | tot = (trace_buffer_hi - trace_buffer_lo) - free_space (); | |
2109 | ||
2110 | /* If we're right at the end, reply specially that we're done. */ | |
2111 | if (offset == tot) | |
2112 | { | |
2113 | strcpy (own_buf, "l"); | |
2114 | return; | |
2115 | } | |
2116 | ||
2117 | /* Object to any other out-of-bounds request. */ | |
2118 | if (offset > tot) | |
2119 | { | |
2120 | write_enn (own_buf); | |
2121 | return; | |
2122 | } | |
2123 | ||
2124 | /* Compute the pointer corresponding to the given offset, accounting | |
2125 | for wraparound. */ | |
2126 | tbp = trace_buffer_start + offset; | |
2127 | if (tbp >= trace_buffer_wrap) | |
2128 | tbp -= (trace_buffer_wrap - trace_buffer_lo); | |
2129 | ||
2130 | /* Trim to the remaining bytes if we're close to the end. */ | |
2131 | if (num > tot - offset) | |
2132 | num = tot - offset; | |
2133 | ||
2134 | /* Trim to available packet size. */ | |
2135 | if (num >= (PBUFSIZ - 16) / 2 ) | |
2136 | num = (PBUFSIZ - 16) / 2; | |
2137 | ||
2138 | convert_int_to_ascii (tbp, own_buf, num); | |
2139 | own_buf[num] = '\0'; | |
2140 | } | |
2141 | ||
2142 | static void | |
2143 | cmd_bigqtbuffer (char *own_buf) | |
2144 | { | |
2145 | ULONGEST val; | |
2146 | char *packet = own_buf; | |
2147 | ||
2148 | packet += strlen ("QTBuffer:"); | |
2149 | ||
2150 | if (strncmp ("circular:", packet, strlen ("circular:")) == 0) | |
2151 | { | |
2152 | packet += strlen ("circular:"); | |
2153 | packet = unpack_varlen_hex (packet, &val); | |
2154 | circular_trace_buffer = val; | |
2155 | trace_debug ("Trace buffer is now %s", | |
2156 | circular_trace_buffer ? "circular" : "linear"); | |
2157 | write_ok (own_buf); | |
2158 | } | |
2159 | else | |
2160 | write_enn (own_buf); | |
2161 | } | |
2162 | ||
2163 | int | |
2164 | handle_tracepoint_general_set (char *packet) | |
2165 | { | |
2166 | if (strcmp ("QTinit", packet) == 0) | |
2167 | { | |
2168 | cmd_qtinit (packet); | |
2169 | return 1; | |
2170 | } | |
2171 | else if (strncmp ("QTDP:", packet, strlen ("QTDP:")) == 0) | |
2172 | { | |
2173 | cmd_qtdp (packet); | |
2174 | return 1; | |
2175 | } | |
2176 | else if (strncmp ("QTDPsrc:", packet, strlen ("QTDPsrc:")) == 0) | |
2177 | { | |
2178 | cmd_qtdpsrc (packet); | |
2179 | return 1; | |
2180 | } | |
2181 | else if (strncmp ("QTDV:", packet, strlen ("QTDV:")) == 0) | |
2182 | { | |
2183 | cmd_qtdv (packet); | |
2184 | return 1; | |
2185 | } | |
2186 | else if (strncmp ("QTro:", packet, strlen ("QTro:")) == 0) | |
2187 | { | |
2188 | cmd_qtro (packet); | |
2189 | return 1; | |
2190 | } | |
2191 | else if (strcmp ("QTStart", packet) == 0) | |
2192 | { | |
2193 | cmd_qtstart (packet); | |
2194 | return 1; | |
2195 | } | |
2196 | else if (strcmp ("QTStop", packet) == 0) | |
2197 | { | |
2198 | cmd_qtstop (packet); | |
2199 | return 1; | |
2200 | } | |
8336d594 PA |
2201 | else if (strncmp ("QTDisconnected:", packet, |
2202 | strlen ("QTDisconnected:")) == 0) | |
2203 | { | |
2204 | cmd_qtdisconnected (packet); | |
2205 | return 1; | |
2206 | } | |
219f2f23 PA |
2207 | else if (strncmp ("QTFrame:", packet, strlen ("QTFrame:")) == 0) |
2208 | { | |
2209 | cmd_qtframe (packet); | |
2210 | return 1; | |
2211 | } | |
2212 | else if (strncmp ("QTBuffer:", packet, strlen ("QTBuffer:")) == 0) | |
2213 | { | |
2214 | cmd_bigqtbuffer (packet); | |
2215 | return 1; | |
2216 | } | |
2217 | ||
2218 | return 0; | |
2219 | } | |
2220 | ||
2221 | int | |
2222 | handle_tracepoint_query (char *packet) | |
2223 | { | |
2224 | if (strcmp ("qTStatus", packet) == 0) | |
2225 | { | |
2226 | cmd_qtstatus (packet); | |
2227 | return 1; | |
2228 | } | |
2229 | else if (strcmp ("qTfP", packet) == 0) | |
2230 | { | |
2231 | cmd_qtfp (packet); | |
2232 | return 1; | |
2233 | } | |
2234 | else if (strcmp ("qTsP", packet) == 0) | |
2235 | { | |
2236 | cmd_qtsp (packet); | |
2237 | return 1; | |
2238 | } | |
2239 | else if (strcmp ("qTfV", packet) == 0) | |
2240 | { | |
2241 | cmd_qtfv (packet); | |
2242 | return 1; | |
2243 | } | |
2244 | else if (strcmp ("qTsV", packet) == 0) | |
2245 | { | |
2246 | cmd_qtsv (packet); | |
2247 | return 1; | |
2248 | } | |
2249 | else if (strncmp ("qTV:", packet, strlen ("qTV:")) == 0) | |
2250 | { | |
2251 | cmd_qtv (packet); | |
2252 | return 1; | |
2253 | } | |
2254 | else if (strncmp ("qTBuffer:", packet, strlen ("qTBuffer:")) == 0) | |
2255 | { | |
2256 | cmd_qtbuffer (packet); | |
2257 | return 1; | |
2258 | } | |
2259 | ||
2260 | return 0; | |
2261 | } | |
2262 | ||
2263 | /* Call this when thread TINFO has hit the tracepoint defined by | |
2264 | TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping | |
2265 | action. This adds a while-stepping collecting state item to the | |
2266 | threads' collecting state list, so that we can keep track of | |
2267 | multiple simultaneous while-stepping actions being collected by the | |
2268 | same thread. This can happen in cases like: | |
2269 | ||
2270 | ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs | |
2271 | ff0002 INSN2 | |
2272 | ff0003 INSN3 <-- TP2, collect $regs | |
2273 | ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs | |
2274 | ff0005 INSN5 | |
2275 | ||
2276 | Notice that when instruction INSN5 is reached, the while-stepping | |
2277 | actions of both TP1 and TP3 are still being collected, and that TP2 | |
2278 | had been collected meanwhile. The whole range of ff0001-ff0005 | |
2279 | should be single-stepped, due to at least TP1's while-stepping | |
2280 | action covering the whole range. */ | |
2281 | ||
2282 | static void | |
2283 | add_while_stepping_state (struct thread_info *tinfo, | |
2284 | int tp_number, CORE_ADDR tp_address) | |
2285 | { | |
2286 | struct wstep_state *wstep; | |
2287 | ||
2288 | wstep = xmalloc (sizeof (*wstep)); | |
2289 | wstep->next = tinfo->while_stepping; | |
2290 | ||
2291 | wstep->tp_number = tp_number; | |
2292 | wstep->tp_address = tp_address; | |
2293 | wstep->current_step = 0; | |
2294 | ||
2295 | tinfo->while_stepping = wstep; | |
2296 | } | |
2297 | ||
2298 | /* Release the while-stepping collecting state WSTEP. */ | |
2299 | ||
2300 | static void | |
2301 | release_while_stepping_state (struct wstep_state *wstep) | |
2302 | { | |
2303 | free (wstep); | |
2304 | } | |
2305 | ||
2306 | /* Release all while-stepping collecting states currently associated | |
2307 | with thread TINFO. */ | |
2308 | ||
2309 | void | |
2310 | release_while_stepping_state_list (struct thread_info *tinfo) | |
2311 | { | |
2312 | struct wstep_state *head; | |
2313 | ||
2314 | while (tinfo->while_stepping) | |
2315 | { | |
2316 | head = tinfo->while_stepping; | |
2317 | tinfo->while_stepping = head->next; | |
2318 | release_while_stepping_state (head); | |
2319 | } | |
2320 | } | |
2321 | ||
2322 | /* If TINFO was handling a 'while-stepping' action, the step has | |
2323 | finished, so collect any step data needed, and check if any more | |
2324 | steps are required. Return true if the thread was indeed | |
2325 | collecting tracepoint data, false otherwise. */ | |
2326 | ||
2327 | int | |
2328 | tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc) | |
2329 | { | |
2330 | struct tracepoint *tpoint; | |
2331 | struct wstep_state *wstep; | |
2332 | struct wstep_state **wstep_link; | |
2333 | struct trap_tracepoint_ctx ctx; | |
2334 | ||
2335 | /* Check if we were indeed collecting data for one of more | |
2336 | tracepoints with a 'while-stepping' count. */ | |
2337 | if (tinfo->while_stepping == NULL) | |
2338 | return 0; | |
2339 | ||
2340 | if (!tracing) | |
2341 | { | |
2342 | /* We're not even tracing anymore. Stop this thread from | |
2343 | collecting. */ | |
2344 | release_while_stepping_state_list (tinfo); | |
2345 | ||
2346 | /* The thread had stopped due to a single-step request indeed | |
2347 | explained by a tracepoint. */ | |
2348 | return 1; | |
2349 | } | |
2350 | ||
2351 | wstep = tinfo->while_stepping; | |
2352 | wstep_link = &tinfo->while_stepping; | |
2353 | ||
2354 | trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s", | |
2355 | target_pid_to_str (tinfo->entry.id), | |
2356 | wstep->tp_number, paddress (wstep->tp_address)); | |
2357 | ||
2358 | ctx.regcache = get_thread_regcache (tinfo, 1); | |
2359 | ||
2360 | while (wstep != NULL) | |
2361 | { | |
2362 | tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address); | |
2363 | if (tpoint == NULL) | |
2364 | { | |
2365 | trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!", | |
2366 | wstep->tp_number, paddress (wstep->tp_address), | |
2367 | target_pid_to_str (tinfo->entry.id)); | |
2368 | ||
2369 | /* Unlink. */ | |
2370 | *wstep_link = wstep->next; | |
2371 | release_while_stepping_state (wstep); | |
2372 | continue; | |
2373 | } | |
2374 | ||
2375 | /* We've just finished one step. */ | |
2376 | ++wstep->current_step; | |
2377 | ||
2378 | /* Collect data. */ | |
2379 | collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx, | |
2380 | stop_pc, tpoint, wstep->current_step); | |
2381 | ||
2382 | if (wstep->current_step >= tpoint->step_count) | |
2383 | { | |
2384 | /* The requested numbers of steps have occurred. */ | |
2385 | trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s", | |
2386 | target_pid_to_str (tinfo->entry.id), | |
2387 | wstep->tp_number, paddress (wstep->tp_address)); | |
2388 | ||
2389 | /* Unlink the wstep. */ | |
2390 | *wstep_link = wstep->next; | |
2391 | release_while_stepping_state (wstep); | |
2392 | wstep = *wstep_link; | |
2393 | ||
2394 | /* Only check the hit count now, which ensure that we do all | |
2395 | our stepping before stopping the run. */ | |
2396 | if (tpoint->pass_count > 0 | |
2397 | && tpoint->hit_count >= tpoint->pass_count | |
2398 | && stopping_tracepoint == NULL) | |
2399 | stopping_tracepoint = tpoint; | |
2400 | } | |
2401 | else | |
2402 | { | |
2403 | /* Keep single-stepping until the requested numbers of steps | |
2404 | have occurred. */ | |
2405 | wstep_link = &wstep->next; | |
2406 | wstep = *wstep_link; | |
2407 | } | |
2408 | ||
2409 | if (stopping_tracepoint | |
2410 | || trace_buffer_is_full | |
2411 | || expr_eval_result != expr_eval_no_error) | |
2412 | { | |
2413 | stop_tracing (); | |
2414 | break; | |
2415 | } | |
2416 | } | |
2417 | ||
2418 | return 1; | |
2419 | } | |
2420 | ||
2421 | /* Return true if TINFO just hit a tracepoint. Collect data if | |
2422 | so. */ | |
2423 | ||
2424 | int | |
2425 | tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc) | |
2426 | { | |
2427 | struct tracepoint *tpoint; | |
2428 | int ret = 0; | |
2429 | struct trap_tracepoint_ctx ctx; | |
2430 | ||
2431 | /* Not tracing, don't handle. */ | |
2432 | if (!tracing) | |
2433 | return 0; | |
2434 | ||
2435 | ctx.regcache = get_thread_regcache (tinfo, 1); | |
2436 | ||
2437 | for (tpoint = tracepoints; tpoint; tpoint = tpoint->next) | |
2438 | { | |
2439 | if (tpoint->enabled && stop_pc == tpoint->address) | |
2440 | { | |
2441 | trace_debug ("Thread %s at address of tracepoint %d at 0x%s", | |
2442 | target_pid_to_str (tinfo->entry.id), | |
2443 | tpoint->number, paddress (tpoint->address)); | |
2444 | ||
2445 | /* Test the condition if present, and collect if true. */ | |
2446 | if (!tpoint->cond | |
2447 | || (condition_true_at_tracepoint | |
2448 | ((struct tracepoint_hit_ctx *) &ctx, tpoint))) | |
2449 | collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx, | |
2450 | stop_pc, tpoint); | |
2451 | ||
2452 | if (stopping_tracepoint | |
2453 | || trace_buffer_is_full | |
2454 | || expr_eval_result != expr_eval_no_error) | |
2455 | { | |
2456 | stop_tracing (); | |
2457 | } | |
2458 | /* If the tracepoint had a 'while-stepping' action, then set | |
2459 | the thread to collect this tracepoint on the following | |
2460 | single-steps. */ | |
2461 | else if (tpoint->step_count > 0) | |
2462 | { | |
2463 | add_while_stepping_state (tinfo, | |
2464 | tpoint->number, tpoint->address); | |
2465 | } | |
2466 | ||
2467 | ret = 1; | |
2468 | } | |
2469 | } | |
2470 | ||
2471 | return ret; | |
2472 | } | |
2473 | ||
2474 | /* Create a trace frame for the hit of the given tracepoint in the | |
2475 | given thread. */ | |
2476 | ||
2477 | static void | |
2478 | collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc, | |
2479 | struct tracepoint *tpoint) | |
2480 | { | |
2481 | struct traceframe *tframe; | |
2482 | int acti; | |
2483 | ||
2484 | /* Only count it as a hit when we actually collect data. */ | |
2485 | tpoint->hit_count++; | |
2486 | ||
2487 | /* If we've exceeded a defined pass count, record the event for | |
2488 | later, and finish the collection for this hit. This test is only | |
2489 | for nonstepping tracepoints, stepping tracepoints test at the end | |
2490 | of their while-stepping loop. */ | |
2491 | if (tpoint->pass_count > 0 | |
2492 | && tpoint->hit_count >= tpoint->pass_count | |
2493 | && tpoint->step_count == 0 | |
2494 | && stopping_tracepoint == NULL) | |
2495 | stopping_tracepoint = tpoint; | |
2496 | ||
2497 | trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld", | |
2498 | tpoint->number, paddress (tpoint->address), tpoint->hit_count); | |
2499 | ||
2500 | tframe = add_traceframe (tpoint); | |
2501 | ||
2502 | if (tframe) | |
2503 | { | |
2504 | for (acti = 0; acti < tpoint->numactions; ++acti) | |
2505 | { | |
2506 | trace_debug ("Tracepoint %d at 0x%s about to do action '%s'", | |
2507 | tpoint->number, paddress (tpoint->address), | |
2508 | tpoint->actions_str[acti]); | |
2509 | ||
2510 | do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe, | |
2511 | tpoint->actions[acti]); | |
2512 | } | |
2513 | ||
2514 | finish_traceframe (tframe); | |
2515 | } | |
2516 | ||
2517 | if (tframe == NULL && tracing) | |
2518 | trace_buffer_is_full = 1; | |
2519 | } | |
2520 | ||
2521 | static void | |
2522 | collect_data_at_step (struct tracepoint_hit_ctx *ctx, | |
2523 | CORE_ADDR stop_pc, | |
2524 | struct tracepoint *tpoint, int current_step) | |
2525 | { | |
2526 | struct traceframe *tframe; | |
2527 | int acti; | |
2528 | ||
2529 | trace_debug ("Making new step traceframe for " | |
2530 | "tracepoint %d at 0x%s, step %d of %ld, hit %ld", | |
2531 | tpoint->number, paddress (tpoint->address), | |
2532 | current_step, tpoint->step_count, | |
2533 | tpoint->hit_count); | |
2534 | ||
2535 | tframe = add_traceframe (tpoint); | |
2536 | ||
2537 | if (tframe) | |
2538 | { | |
2539 | for (acti = 0; acti < tpoint->num_step_actions; ++acti) | |
2540 | { | |
2541 | trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'", | |
2542 | tpoint->number, paddress (tpoint->address), | |
2543 | tpoint->step_actions_str[acti]); | |
2544 | ||
2545 | do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe, | |
2546 | tpoint->step_actions[acti]); | |
2547 | } | |
2548 | ||
2549 | finish_traceframe (tframe); | |
2550 | } | |
2551 | ||
2552 | if (tframe == NULL && tracing) | |
2553 | trace_buffer_is_full = 1; | |
2554 | } | |
2555 | ||
2556 | static struct regcache * | |
2557 | get_context_regcache (struct tracepoint_hit_ctx *ctx) | |
2558 | { | |
2559 | struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx; | |
2560 | struct regcache *regcache = tctx->regcache; | |
2561 | ||
2562 | gdb_assert (regcache != NULL); | |
2563 | ||
2564 | return regcache; | |
2565 | } | |
2566 | ||
2567 | static void | |
2568 | do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx, | |
2569 | CORE_ADDR stop_pc, | |
2570 | struct tracepoint *tpoint, | |
2571 | struct traceframe *tframe, | |
2572 | struct tracepoint_action *taction) | |
2573 | { | |
2574 | enum eval_result_type err; | |
2575 | ||
2576 | switch (taction->type) | |
2577 | { | |
2578 | case 'M': | |
2579 | { | |
2580 | struct collect_memory_action *maction; | |
2581 | ||
2582 | maction = (struct collect_memory_action *) taction; | |
2583 | ||
2584 | trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)", | |
2585 | pulongest (maction->len), | |
2586 | paddress (maction->addr), maction->basereg); | |
2587 | /* (should use basereg) */ | |
2588 | agent_mem_read (tframe, NULL, | |
2589 | (CORE_ADDR) maction->addr, maction->len); | |
2590 | break; | |
2591 | } | |
2592 | case 'R': | |
2593 | { | |
2594 | struct collect_registers_action *raction; | |
2595 | ||
2596 | unsigned char *regspace; | |
2597 | struct regcache tregcache; | |
2598 | struct regcache *context_regcache; | |
2599 | ||
2600 | raction = (struct collect_registers_action *) taction; | |
2601 | ||
2602 | trace_debug ("Want to collect registers"); | |
2603 | ||
2604 | /* Collect all registers for now. */ | |
2605 | regspace = add_traceframe_block (tframe, | |
2606 | 1 + register_cache_size ()); | |
2607 | if (regspace == NULL) | |
2608 | { | |
2609 | trace_debug ("Trace buffer block allocation failed, skipping"); | |
2610 | break; | |
2611 | } | |
2612 | /* Identify a register block. */ | |
2613 | *regspace = 'R'; | |
2614 | ||
2615 | context_regcache = get_context_regcache (ctx); | |
2616 | ||
2617 | /* Wrap the regblock in a register cache (in the stack, we | |
2618 | don't want to malloc here). */ | |
2619 | init_register_cache (&tregcache, regspace + 1); | |
2620 | ||
2621 | /* Copy the register data to the regblock. */ | |
2622 | regcache_cpy (&tregcache, context_regcache); | |
2623 | ||
2624 | /* On some platforms, trap-based tracepoints will have the PC | |
2625 | pointing to the next instruction after the trap, but we | |
2626 | don't want the user or GDB trying to guess whether the | |
2627 | saved PC needs adjusting; so always record the adjusted | |
2628 | stop_pc. Note that we can't use tpoint->address instead, | |
2629 | since it will be wrong for while-stepping actions. */ | |
2630 | trace_debug ("Storing stop pc (0x%s) in regblock", | |
2631 | paddress (tpoint->address)); | |
2632 | ||
2633 | /* This changes the regblock, not the thread's | |
2634 | regcache. */ | |
2635 | regcache_write_pc (&tregcache, stop_pc); | |
2636 | } | |
2637 | break; | |
2638 | case 'X': | |
2639 | { | |
2640 | struct eval_expr_action *eaction; | |
2641 | ||
2642 | eaction = (struct eval_expr_action *) taction; | |
2643 | ||
2644 | trace_debug ("Want to evaluate expression"); | |
2645 | ||
2646 | err = eval_agent_expr (ctx, tframe, eaction->expr, NULL); | |
2647 | ||
2648 | if (err != expr_eval_no_error) | |
2649 | { | |
2650 | record_tracepoint_error (tpoint, "action expression", err); | |
2651 | return; | |
2652 | } | |
2653 | } | |
2654 | break; | |
2655 | default: | |
2656 | trace_debug ("unknown trace action '%c', ignoring", taction->type); | |
2657 | break; | |
2658 | } | |
2659 | } | |
2660 | ||
2661 | static int | |
2662 | condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx, | |
2663 | struct tracepoint *tpoint) | |
2664 | { | |
2665 | ULONGEST value = 0; | |
2666 | enum eval_result_type err; | |
2667 | ||
2668 | err = eval_agent_expr (ctx, NULL, tpoint->cond, &value); | |
2669 | ||
2670 | if (err != expr_eval_no_error) | |
2671 | { | |
2672 | record_tracepoint_error (tpoint, "condition", err); | |
2673 | /* The error case must return false. */ | |
2674 | return 0; | |
2675 | } | |
2676 | ||
2677 | trace_debug ("Tracepoint %d at 0x%s condition evals to %s", | |
2678 | tpoint->number, paddress (tpoint->address), | |
2679 | pulongest (value)); | |
2680 | return (value ? 1 : 0); | |
2681 | } | |
2682 | ||
2683 | /* The packet form of an agent expression consists of an 'X', number | |
2684 | of bytes in expression, a comma, and then the bytes. */ | |
2685 | ||
2686 | static struct agent_expr * | |
2687 | parse_agent_expr (char **actparm) | |
2688 | { | |
2689 | char *act = *actparm; | |
2690 | ULONGEST xlen; | |
2691 | struct agent_expr *aexpr; | |
2692 | ||
2693 | ++act; /* skip the X */ | |
2694 | act = unpack_varlen_hex (act, &xlen); | |
2695 | ++act; /* skip a comma */ | |
2696 | aexpr = xmalloc (sizeof (struct agent_expr)); | |
2697 | aexpr->length = xlen; | |
2698 | aexpr->bytes = xmalloc (xlen); | |
2699 | convert_ascii_to_int (act, aexpr->bytes, xlen); | |
2700 | *actparm = act + (xlen * 2); | |
2701 | return aexpr; | |
2702 | } | |
2703 | ||
2704 | /* Convert the bytes of an agent expression back into hex digits, so | |
2705 | they can be printed or uploaded. This allocates the buffer, | |
2706 | callers should free when they are done with it. */ | |
2707 | ||
2708 | static char * | |
2709 | unparse_agent_expr (struct agent_expr *aexpr) | |
2710 | { | |
2711 | char *rslt; | |
2712 | ||
2713 | rslt = xmalloc (2 * aexpr->length + 1); | |
2714 | convert_int_to_ascii (aexpr->bytes, rslt, aexpr->length); | |
2715 | return rslt; | |
2716 | } | |
2717 | ||
2718 | /* The agent expression evaluator, as specified by the GDB docs. It | |
2719 | returns 0 if everything went OK, and a nonzero error code | |
2720 | otherwise. */ | |
2721 | ||
2722 | static enum eval_result_type | |
2723 | eval_agent_expr (struct tracepoint_hit_ctx *ctx, | |
2724 | struct traceframe *tframe, | |
2725 | struct agent_expr *aexpr, | |
2726 | ULONGEST *rslt) | |
2727 | { | |
2728 | int pc = 0; | |
2729 | #define STACK_MAX 100 | |
2730 | ULONGEST stack[STACK_MAX], top; | |
2731 | int sp = 0; | |
2732 | unsigned char op; | |
2733 | int arg; | |
2734 | ||
2735 | /* This union is a convenient way to convert representations. For | |
2736 | now, assume a standard architecture where the hardware integer | |
2737 | types have 8, 16, 32, 64 bit types. A more robust solution would | |
2738 | be to import stdint.h from gnulib. */ | |
2739 | union | |
2740 | { | |
2741 | union | |
2742 | { | |
2743 | unsigned char bytes[1]; | |
2744 | unsigned char val; | |
2745 | } u8; | |
2746 | union | |
2747 | { | |
2748 | unsigned char bytes[2]; | |
2749 | unsigned short val; | |
2750 | } u16; | |
2751 | union | |
2752 | { | |
2753 | unsigned char bytes[4]; | |
2754 | unsigned int val; | |
2755 | } u32; | |
2756 | union | |
2757 | { | |
2758 | unsigned char bytes[8]; | |
2759 | ULONGEST val; | |
2760 | } u64; | |
2761 | } cnv; | |
2762 | ||
2763 | if (aexpr->length == 0) | |
2764 | { | |
2765 | trace_debug ("empty agent expression"); | |
2766 | return expr_eval_empty_expression; | |
2767 | } | |
2768 | ||
2769 | /* Cache the stack top in its own variable. Much of the time we can | |
2770 | operate on this variable, rather than dinking with the stack. It | |
2771 | needs to be copied to the stack when sp changes. */ | |
2772 | top = 0; | |
2773 | ||
2774 | while (1) | |
2775 | { | |
2776 | op = aexpr->bytes[pc++]; | |
2777 | ||
2778 | trace_debug ("About to interpret byte 0x%x", op); | |
2779 | ||
2780 | switch (op) | |
2781 | { | |
2782 | case gdb_agent_op_add: | |
2783 | top += stack[--sp]; | |
2784 | break; | |
2785 | ||
2786 | case gdb_agent_op_sub: | |
2787 | top = stack[--sp] - top; | |
2788 | break; | |
2789 | ||
2790 | case gdb_agent_op_mul: | |
2791 | top *= stack[--sp]; | |
2792 | break; | |
2793 | ||
2794 | case gdb_agent_op_div_signed: | |
2795 | if (top == 0) | |
2796 | { | |
2797 | trace_debug ("Attempted to divide by zero"); | |
2798 | return expr_eval_divide_by_zero; | |
2799 | } | |
2800 | top = ((LONGEST) stack[--sp]) / ((LONGEST) top); | |
2801 | break; | |
2802 | ||
2803 | case gdb_agent_op_div_unsigned: | |
2804 | if (top == 0) | |
2805 | { | |
2806 | trace_debug ("Attempted to divide by zero"); | |
2807 | return expr_eval_divide_by_zero; | |
2808 | } | |
2809 | top = stack[--sp] / top; | |
2810 | break; | |
2811 | ||
2812 | case gdb_agent_op_rem_signed: | |
2813 | if (top == 0) | |
2814 | { | |
2815 | trace_debug ("Attempted to divide by zero"); | |
2816 | return expr_eval_divide_by_zero; | |
2817 | } | |
2818 | top = ((LONGEST) stack[--sp]) % ((LONGEST) top); | |
2819 | break; | |
2820 | ||
2821 | case gdb_agent_op_rem_unsigned: | |
2822 | if (top == 0) | |
2823 | { | |
2824 | trace_debug ("Attempted to divide by zero"); | |
2825 | return expr_eval_divide_by_zero; | |
2826 | } | |
2827 | top = stack[--sp] % top; | |
2828 | break; | |
2829 | ||
2830 | case gdb_agent_op_lsh: | |
2831 | top = stack[--sp] << top; | |
2832 | break; | |
2833 | ||
2834 | case gdb_agent_op_rsh_signed: | |
2835 | top = ((LONGEST) stack[--sp]) >> top; | |
2836 | break; | |
2837 | ||
2838 | case gdb_agent_op_rsh_unsigned: | |
2839 | top = stack[--sp] >> top; | |
2840 | break; | |
2841 | ||
2842 | case gdb_agent_op_trace: | |
2843 | agent_mem_read (tframe, | |
2844 | NULL, (CORE_ADDR) stack[--sp], (ULONGEST) top); | |
2845 | if (--sp >= 0) | |
2846 | top = stack[sp]; | |
2847 | break; | |
2848 | ||
2849 | case gdb_agent_op_trace_quick: | |
2850 | arg = aexpr->bytes[pc++]; | |
2851 | agent_mem_read (tframe, NULL, (CORE_ADDR) top, (ULONGEST) arg); | |
2852 | break; | |
2853 | ||
2854 | case gdb_agent_op_log_not: | |
2855 | top = !top; | |
2856 | break; | |
2857 | ||
2858 | case gdb_agent_op_bit_and: | |
2859 | top &= stack[--sp]; | |
2860 | break; | |
2861 | ||
2862 | case gdb_agent_op_bit_or: | |
2863 | top |= stack[--sp]; | |
2864 | break; | |
2865 | ||
2866 | case gdb_agent_op_bit_xor: | |
2867 | top ^= stack[--sp]; | |
2868 | break; | |
2869 | ||
2870 | case gdb_agent_op_bit_not: | |
2871 | top = ~top; | |
2872 | break; | |
2873 | ||
2874 | case gdb_agent_op_equal: | |
2875 | top = (stack[--sp] == top); | |
2876 | break; | |
2877 | ||
2878 | case gdb_agent_op_less_signed: | |
2879 | top = (((LONGEST) stack[--sp]) < ((LONGEST) top)); | |
2880 | break; | |
2881 | ||
2882 | case gdb_agent_op_less_unsigned: | |
2883 | top = (stack[--sp] < top); | |
2884 | break; | |
2885 | ||
2886 | case gdb_agent_op_ext: | |
2887 | arg = aexpr->bytes[pc++]; | |
2888 | if (arg < (sizeof (LONGEST) * 8)) | |
2889 | { | |
2890 | LONGEST mask = 1 << (arg - 1); | |
2891 | top &= ((LONGEST) 1 << arg) - 1; | |
2892 | top = (top ^ mask) - mask; | |
2893 | } | |
2894 | break; | |
2895 | ||
2896 | case gdb_agent_op_ref8: | |
2897 | agent_mem_read (tframe, cnv.u8.bytes, (CORE_ADDR) top, 1); | |
2898 | top = cnv.u8.val; | |
2899 | break; | |
2900 | ||
2901 | case gdb_agent_op_ref16: | |
2902 | agent_mem_read (tframe, cnv.u16.bytes, (CORE_ADDR) top, 2); | |
2903 | top = cnv.u16.val; | |
2904 | break; | |
2905 | ||
2906 | case gdb_agent_op_ref32: | |
2907 | agent_mem_read (tframe, cnv.u32.bytes, (CORE_ADDR) top, 4); | |
2908 | top = cnv.u32.val; | |
2909 | break; | |
2910 | ||
2911 | case gdb_agent_op_ref64: | |
2912 | agent_mem_read (tframe, cnv.u64.bytes, (CORE_ADDR) top, 8); | |
2913 | top = cnv.u64.val; | |
2914 | break; | |
2915 | ||
2916 | case gdb_agent_op_if_goto: | |
2917 | if (top) | |
2918 | pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]); | |
2919 | else | |
2920 | pc += 2; | |
2921 | if (--sp >= 0) | |
2922 | top = stack[sp]; | |
2923 | break; | |
2924 | ||
2925 | case gdb_agent_op_goto: | |
2926 | pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]); | |
2927 | break; | |
2928 | ||
2929 | case gdb_agent_op_const8: | |
2930 | /* Flush the cached stack top. */ | |
2931 | stack[sp++] = top; | |
2932 | top = aexpr->bytes[pc++]; | |
2933 | break; | |
2934 | ||
2935 | case gdb_agent_op_const16: | |
2936 | /* Flush the cached stack top. */ | |
2937 | stack[sp++] = top; | |
2938 | top = aexpr->bytes[pc++]; | |
2939 | top = (top << 8) + aexpr->bytes[pc++]; | |
2940 | break; | |
2941 | ||
2942 | case gdb_agent_op_const32: | |
2943 | /* Flush the cached stack top. */ | |
2944 | stack[sp++] = top; | |
2945 | top = aexpr->bytes[pc++]; | |
2946 | top = (top << 8) + aexpr->bytes[pc++]; | |
2947 | top = (top << 8) + aexpr->bytes[pc++]; | |
2948 | top = (top << 8) + aexpr->bytes[pc++]; | |
2949 | break; | |
2950 | ||
2951 | case gdb_agent_op_const64: | |
2952 | /* Flush the cached stack top. */ | |
2953 | stack[sp++] = top; | |
2954 | top = aexpr->bytes[pc++]; | |
2955 | top = (top << 8) + aexpr->bytes[pc++]; | |
2956 | top = (top << 8) + aexpr->bytes[pc++]; | |
2957 | top = (top << 8) + aexpr->bytes[pc++]; | |
2958 | top = (top << 8) + aexpr->bytes[pc++]; | |
2959 | top = (top << 8) + aexpr->bytes[pc++]; | |
2960 | top = (top << 8) + aexpr->bytes[pc++]; | |
2961 | top = (top << 8) + aexpr->bytes[pc++]; | |
2962 | break; | |
2963 | ||
2964 | case gdb_agent_op_reg: | |
2965 | /* Flush the cached stack top. */ | |
2966 | stack[sp++] = top; | |
2967 | arg = aexpr->bytes[pc++]; | |
2968 | arg = (arg << 8) + aexpr->bytes[pc++]; | |
2969 | { | |
2970 | int regnum = arg; | |
2971 | struct regcache *regcache; | |
2972 | ||
2973 | regcache = get_context_regcache (ctx); | |
2974 | ||
2975 | switch (register_size (regnum)) | |
2976 | { | |
2977 | case 8: | |
2978 | collect_register (regcache, regnum, cnv.u64.bytes); | |
2979 | top = cnv.u64.val; | |
2980 | break; | |
2981 | case 4: | |
2982 | collect_register (regcache, regnum, cnv.u32.bytes); | |
2983 | top = cnv.u32.val; | |
2984 | break; | |
2985 | case 2: | |
2986 | collect_register (regcache, regnum, cnv.u16.bytes); | |
2987 | top = cnv.u16.val; | |
2988 | break; | |
2989 | case 1: | |
2990 | collect_register (regcache, regnum, cnv.u8.bytes); | |
2991 | top = cnv.u8.val; | |
2992 | break; | |
2993 | default: | |
2994 | internal_error (__FILE__, __LINE__, | |
2995 | "unhandled register size"); | |
2996 | } | |
2997 | } | |
2998 | break; | |
2999 | ||
3000 | case gdb_agent_op_end: | |
3001 | trace_debug ("At end of expression, sp=%d, stack top cache=0x%s", | |
3002 | sp, pulongest (top)); | |
3003 | if (rslt) | |
3004 | { | |
3005 | if (sp <= 0) | |
3006 | { | |
3007 | /* This should be an error */ | |
3008 | trace_debug ("Stack is empty, nothing to return"); | |
3009 | return expr_eval_empty_stack; | |
3010 | } | |
3011 | *rslt = top; | |
3012 | } | |
3013 | return expr_eval_no_error; | |
3014 | ||
3015 | case gdb_agent_op_dup: | |
3016 | stack[sp++] = top; | |
3017 | break; | |
3018 | ||
3019 | case gdb_agent_op_pop: | |
3020 | if (--sp >= 0) | |
3021 | top = stack[sp]; | |
3022 | break; | |
3023 | ||
3024 | case gdb_agent_op_zero_ext: | |
3025 | arg = aexpr->bytes[pc++]; | |
3026 | if (arg < (sizeof (LONGEST) * 8)) | |
3027 | top &= ((LONGEST) 1 << arg) - 1; | |
3028 | break; | |
3029 | ||
3030 | case gdb_agent_op_swap: | |
3031 | /* Interchange top two stack elements, making sure top gets | |
3032 | copied back onto stack. */ | |
3033 | stack[sp] = top; | |
3034 | top = stack[sp - 1]; | |
3035 | stack[sp - 1] = stack[sp]; | |
3036 | break; | |
3037 | ||
3038 | case gdb_agent_op_getv: | |
3039 | /* Flush the cached stack top. */ | |
3040 | stack[sp++] = top; | |
3041 | arg = aexpr->bytes[pc++]; | |
3042 | arg = (arg << 8) + aexpr->bytes[pc++]; | |
3043 | top = get_trace_state_variable_value (arg); | |
3044 | break; | |
3045 | ||
3046 | case gdb_agent_op_setv: | |
3047 | arg = aexpr->bytes[pc++]; | |
3048 | arg = (arg << 8) + aexpr->bytes[pc++]; | |
3049 | set_trace_state_variable_value (arg, top); | |
3050 | /* Note that we leave the value on the stack, for the | |
3051 | benefit of later/enclosing expressions. */ | |
3052 | break; | |
3053 | ||
3054 | case gdb_agent_op_tracev: | |
3055 | arg = aexpr->bytes[pc++]; | |
3056 | arg = (arg << 8) + aexpr->bytes[pc++]; | |
3057 | agent_tsv_read (tframe, arg); | |
3058 | break; | |
3059 | ||
3060 | /* GDB never (currently) generates any of these ops. */ | |
3061 | case gdb_agent_op_float: | |
3062 | case gdb_agent_op_ref_float: | |
3063 | case gdb_agent_op_ref_double: | |
3064 | case gdb_agent_op_ref_long_double: | |
3065 | case gdb_agent_op_l_to_d: | |
3066 | case gdb_agent_op_d_to_l: | |
3067 | case gdb_agent_op_trace16: | |
3068 | trace_debug ("Agent expression op 0x%x valid, but not handled", | |
3069 | op); | |
3070 | /* If ever GDB generates any of these, we don't have the | |
3071 | option of ignoring. */ | |
3072 | return 1; | |
3073 | ||
3074 | default: | |
3075 | trace_debug ("Agent expression op 0x%x not recognized", op); | |
3076 | /* Don't struggle on, things will just get worse. */ | |
3077 | return expr_eval_unrecognized_opcode; | |
3078 | } | |
3079 | ||
3080 | /* Check for stack badness. */ | |
3081 | if (sp >= (STACK_MAX - 1)) | |
3082 | { | |
3083 | trace_debug ("Expression stack overflow"); | |
3084 | return expr_eval_stack_overflow; | |
3085 | } | |
3086 | ||
3087 | if (sp < 0) | |
3088 | { | |
3089 | trace_debug ("Expression stack underflow"); | |
3090 | return expr_eval_stack_underflow; | |
3091 | } | |
3092 | ||
3093 | trace_debug ("Op %s -> sp=%d, top=0x%s", | |
3094 | gdb_agent_op_names[op], sp, pulongest (top)); | |
3095 | } | |
3096 | } | |
3097 | ||
3098 | /* Do memory copies for bytecodes. */ | |
3099 | /* Do the recording of memory blocks for actions and bytecodes. */ | |
3100 | ||
3101 | static int | |
3102 | agent_mem_read (struct traceframe *tframe, | |
3103 | unsigned char *to, CORE_ADDR from, ULONGEST len) | |
3104 | { | |
3105 | unsigned char *mspace; | |
3106 | ULONGEST remaining = len; | |
3107 | unsigned short blocklen; | |
3108 | ||
3109 | /* If a 'to' buffer is specified, use it. */ | |
3110 | if (to != NULL) | |
3111 | { | |
3112 | read_inferior_memory (from, to, len); | |
3113 | return 0; | |
3114 | } | |
3115 | ||
3116 | /* Otherwise, create a new memory block in the trace buffer. */ | |
3117 | while (remaining > 0) | |
3118 | { | |
3119 | size_t sp; | |
3120 | ||
3121 | blocklen = (remaining > 65535 ? 65535 : remaining); | |
3122 | sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen; | |
3123 | mspace = add_traceframe_block (tframe, sp); | |
3124 | if (mspace == NULL) | |
3125 | return 1; | |
3126 | /* Identify block as a memory block. */ | |
3127 | *mspace = 'M'; | |
3128 | ++mspace; | |
3129 | /* Record address and size. */ | |
3130 | memcpy (mspace, &from, sizeof (from)); | |
3131 | mspace += sizeof (from); | |
3132 | memcpy (mspace, &blocklen, sizeof (blocklen)); | |
3133 | mspace += sizeof (blocklen); | |
3134 | /* Record the memory block proper. */ | |
3135 | read_inferior_memory (from, mspace, blocklen); | |
3136 | trace_debug ("%d bytes recorded", blocklen); | |
3137 | remaining -= blocklen; | |
3138 | from += blocklen; | |
3139 | } | |
3140 | return 0; | |
3141 | } | |
3142 | ||
3143 | /* Record the value of a trace state variable. */ | |
3144 | ||
3145 | static int | |
3146 | agent_tsv_read (struct traceframe *tframe, int n) | |
3147 | { | |
3148 | unsigned char *vspace; | |
3149 | LONGEST val; | |
3150 | ||
3151 | vspace = add_traceframe_block (tframe, | |
3152 | 1 + sizeof (n) + sizeof (LONGEST)); | |
3153 | if (vspace == NULL) | |
3154 | return 1; | |
3155 | /* Identify block as a variable. */ | |
3156 | *vspace = 'V'; | |
3157 | /* Record variable's number and value. */ | |
3158 | memcpy (vspace + 1, &n, sizeof (n)); | |
3159 | val = get_trace_state_variable_value (n); | |
3160 | memcpy (vspace + 1 + sizeof (n), &val, sizeof (val)); | |
3161 | trace_debug ("Variable %d recorded", n); | |
3162 | return 0; | |
3163 | } | |
3164 | ||
3165 | static unsigned char * | |
3166 | traceframe_find_block_type (unsigned char *database, unsigned int datasize, | |
3167 | int tfnum, char type_wanted) | |
3168 | { | |
3169 | unsigned char *dataptr; | |
3170 | ||
3171 | if (datasize == 0) | |
3172 | { | |
3173 | trace_debug ("traceframe %d has no data", tfnum); | |
3174 | return NULL; | |
3175 | } | |
3176 | ||
3177 | /* Iterate through a traceframe's blocks, looking for a block of the | |
3178 | requested type. */ | |
3179 | for (dataptr = database; | |
3180 | dataptr < database + datasize; | |
3181 | /* nothing */) | |
3182 | { | |
3183 | char blocktype; | |
3184 | unsigned short mlen; | |
3185 | ||
3186 | if (dataptr == trace_buffer_wrap) | |
3187 | { | |
3188 | /* Adjust to reflect wrapping part of the frame around to | |
3189 | the beginning. */ | |
3190 | datasize = dataptr - database; | |
3191 | dataptr = database = trace_buffer_lo; | |
3192 | } | |
3193 | blocktype = *dataptr++; | |
3194 | ||
3195 | if (type_wanted == blocktype) | |
3196 | return dataptr; | |
3197 | ||
3198 | switch (blocktype) | |
3199 | { | |
3200 | case 'R': | |
3201 | /* Skip over the registers block. */ | |
3202 | dataptr += register_cache_size (); | |
3203 | break; | |
3204 | case 'M': | |
3205 | /* Skip over the memory block. */ | |
3206 | dataptr += sizeof (CORE_ADDR); | |
3207 | memcpy (&mlen, dataptr, sizeof (mlen)); | |
3208 | dataptr += (sizeof (mlen) + mlen); | |
3209 | break; | |
3210 | case 'S': | |
3211 | /* Skip over the static trace data block. */ | |
3212 | memcpy (&mlen, dataptr, sizeof (mlen)); | |
3213 | dataptr += (sizeof (mlen) + mlen); | |
3214 | break; | |
3215 | case 'V': | |
3216 | /* Skip over the TSV block. */ | |
3217 | dataptr += (sizeof (int) + sizeof (LONGEST)); | |
3218 | break; | |
3219 | default: | |
3220 | trace_debug ("traceframe %d has unknown block type 0x%x", | |
3221 | tfnum, blocktype); | |
3222 | return NULL; | |
3223 | } | |
3224 | } | |
3225 | ||
3226 | return NULL; | |
3227 | } | |
3228 | ||
3229 | static unsigned char * | |
3230 | traceframe_find_regblock (struct traceframe *tframe, int tfnum) | |
3231 | { | |
3232 | unsigned char *regblock; | |
3233 | ||
3234 | regblock = traceframe_find_block_type (tframe->data, | |
3235 | tframe->data_size, | |
3236 | tfnum, 'R'); | |
3237 | ||
3238 | if (regblock == NULL) | |
3239 | trace_debug ("traceframe %d has no register data", tfnum); | |
3240 | ||
3241 | return regblock; | |
3242 | } | |
3243 | ||
3244 | /* Get registers from a traceframe. */ | |
3245 | ||
3246 | int | |
3247 | fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum) | |
3248 | { | |
3249 | unsigned char *dataptr; | |
3250 | struct tracepoint *tpoint; | |
3251 | struct traceframe *tframe; | |
3252 | ||
3253 | tframe = find_traceframe (tfnum); | |
3254 | ||
3255 | if (tframe == NULL) | |
3256 | { | |
3257 | trace_debug ("traceframe %d not found", tfnum); | |
3258 | return 1; | |
3259 | } | |
3260 | ||
3261 | dataptr = traceframe_find_regblock (tframe, tfnum); | |
3262 | if (dataptr == NULL) | |
3263 | { | |
3264 | /* We don't like making up numbers, but GDB has all manner of | |
3265 | troubles when the target says there are no registers. */ | |
3266 | supply_regblock (regcache, NULL); | |
3267 | ||
3268 | /* We can generally guess at a PC, although this will be | |
3269 | misleading for while-stepping frames and multi-location | |
3270 | tracepoints. */ | |
3271 | tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum); | |
3272 | if (tpoint != NULL) | |
3273 | regcache_write_pc (regcache, tpoint->address); | |
3274 | } | |
3275 | else | |
3276 | supply_regblock (regcache, dataptr); | |
3277 | ||
3278 | return 0; | |
3279 | } | |
3280 | ||
3281 | static CORE_ADDR | |
3282 | traceframe_get_pc (struct traceframe *tframe) | |
3283 | { | |
3284 | struct regcache regcache; | |
3285 | unsigned char *dataptr; | |
3286 | ||
3287 | dataptr = traceframe_find_regblock (tframe, -1); | |
3288 | if (dataptr == NULL) | |
3289 | return 0; | |
3290 | ||
3291 | init_register_cache (®cache, dataptr); | |
3292 | return regcache_read_pc (®cache); | |
3293 | } | |
3294 | ||
3295 | /* Read a requested block of memory from a trace frame. */ | |
3296 | ||
3297 | int | |
3298 | traceframe_read_mem (int tfnum, CORE_ADDR addr, | |
3299 | unsigned char *buf, ULONGEST length, | |
3300 | ULONGEST *nbytes) | |
3301 | { | |
3302 | struct traceframe *tframe; | |
3303 | unsigned char *database, *dataptr; | |
3304 | unsigned int datasize; | |
3305 | CORE_ADDR maddr; | |
3306 | unsigned short mlen; | |
3307 | ||
3308 | trace_debug ("traceframe_read_mem"); | |
3309 | ||
3310 | tframe = find_traceframe (tfnum); | |
3311 | ||
3312 | if (!tframe) | |
3313 | { | |
3314 | trace_debug ("traceframe %d not found", tfnum); | |
3315 | return 1; | |
3316 | } | |
3317 | ||
3318 | datasize = tframe->data_size; | |
3319 | database = dataptr = &tframe->data[0]; | |
3320 | ||
3321 | /* Iterate through a traceframe's blocks, looking for memory. */ | |
3322 | while ((dataptr = traceframe_find_block_type (dataptr, | |
3323 | datasize - (dataptr - database), | |
3324 | tfnum, 'M')) != NULL) | |
3325 | { | |
3326 | memcpy (&maddr, dataptr, sizeof (maddr)); | |
3327 | dataptr += sizeof (maddr); | |
3328 | memcpy (&mlen, dataptr, sizeof (mlen)); | |
3329 | dataptr += sizeof (mlen); | |
3330 | trace_debug ("traceframe %d has %d bytes at %s", | |
3331 | tfnum, mlen, paddress (maddr)); | |
3332 | ||
3333 | /* Check that requested data is in bounds. */ | |
3334 | if (maddr <= addr && (addr + length) <= (maddr + mlen)) | |
3335 | { | |
3336 | /* Block includes the requested range, copy it out. */ | |
3337 | memcpy (buf, dataptr + (addr - maddr), length); | |
3338 | *nbytes = length; | |
3339 | return 0; | |
3340 | } | |
3341 | ||
3342 | /* Skip over this block. */ | |
3343 | dataptr += mlen; | |
3344 | } | |
3345 | ||
3346 | trace_debug ("traceframe %d has no memory data for the desired region", | |
3347 | tfnum); | |
3348 | ||
3349 | *nbytes = 0; | |
3350 | return 0; | |
3351 | } | |
3352 | ||
3353 | static int | |
3354 | traceframe_read_tsv (int tsvnum, LONGEST *val) | |
3355 | { | |
3356 | int tfnum; | |
3357 | struct traceframe *tframe; | |
3358 | unsigned char *database, *dataptr; | |
3359 | unsigned int datasize; | |
3360 | int vnum; | |
3361 | ||
3362 | trace_debug ("traceframe_read_tsv"); | |
3363 | ||
3364 | tfnum = current_traceframe; | |
3365 | ||
3366 | if (tfnum < 0) | |
3367 | { | |
3368 | trace_debug ("no current traceframe"); | |
3369 | return 1; | |
3370 | } | |
3371 | ||
3372 | tframe = find_traceframe (tfnum); | |
3373 | ||
3374 | if (tframe == NULL) | |
3375 | { | |
3376 | trace_debug ("traceframe %d not found", tfnum); | |
3377 | return 1; | |
3378 | } | |
3379 | ||
3380 | datasize = tframe->data_size; | |
3381 | database = dataptr = &tframe->data[0]; | |
3382 | ||
3383 | /* Iterate through a traceframe's blocks, looking for the tsv. */ | |
3384 | while ((dataptr = traceframe_find_block_type (dataptr, | |
3385 | datasize - (dataptr - database), | |
3386 | tfnum, 'V')) != NULL) | |
3387 | { | |
3388 | memcpy (&vnum, dataptr, sizeof (vnum)); | |
3389 | dataptr += sizeof (vnum); | |
3390 | ||
3391 | trace_debug ("traceframe %d has variable %d", tfnum, vnum); | |
3392 | ||
3393 | /* Check that this is the variable we want. */ | |
3394 | if (tsvnum == vnum) | |
3395 | { | |
3396 | memcpy (val, dataptr, sizeof (*val)); | |
3397 | return 0; | |
3398 | } | |
3399 | ||
3400 | /* Skip over this block. */ | |
3401 | dataptr += sizeof (LONGEST); | |
3402 | } | |
3403 | ||
3404 | trace_debug ("traceframe %d has no data for variable %d", | |
3405 | tfnum, tsvnum); | |
3406 | return 1; | |
3407 | } | |
3408 | ||
3409 | static LONGEST | |
3410 | tsv_get_timestamp (void) | |
3411 | { | |
3412 | struct timeval tv; | |
3413 | ||
3414 | if (gettimeofday (&tv, 0) != 0) | |
3415 | return -1; | |
3416 | else | |
3417 | return (LONGEST) tv.tv_sec * 1000000 + tv.tv_usec; | |
3418 | } | |
3419 | ||
3420 | void | |
3421 | initialize_tracepoint (void) | |
3422 | { | |
3423 | /* There currently no way to change the buffer size. */ | |
3424 | const int sizeOfBuffer = 5 * 1024 * 1024; | |
3425 | unsigned char *buf = xmalloc (sizeOfBuffer); | |
3426 | init_trace_buffer (buf, sizeOfBuffer); | |
3427 | ||
3428 | /* Wire trace state variable 1 to be the timestamp. This will be | |
3429 | uploaded to GDB upon connection and become one of its trace state | |
3430 | variables. (In case you're wondering, if GDB already has a trace | |
3431 | variable numbered 1, it will be renumbered.) */ | |
3432 | create_trace_state_variable (1); | |
3433 | set_trace_state_variable_name (1, "trace_timestamp"); | |
3434 | set_trace_state_variable_getter (1, tsv_get_timestamp); | |
3435 | } |