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