1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
25 static void trace_debug_1 (const char *, ...) ATTR_FORMAT (printf
, 1, 2);
28 trace_debug_1 (const char *fmt
, ...)
34 vsprintf (buf
, fmt
, ap
);
35 fprintf (stderr
, "gdbserver/tracepoint: %s\n", buf
);
39 #define trace_debug(FMT, args...) \
42 trace_debug_1 ((FMT), ##args); \
46 tracepoint_handler (CORE_ADDR address
)
48 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
53 /* This enum must exactly match what is documented in
54 gdb/doc/agentexpr.texi, including all the numerical values. */
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,
108 static const char *gdb_agent_op_names
[gdb_agent_op_last
] =
165 unsigned char *bytes
;
168 /* Base action. Concrete actions inherit this. */
170 struct tracepoint_action
175 /* An 'M' (collect memory) action. */
176 struct collect_memory_action
178 struct tracepoint_action base
;
185 /* An 'R' (collect registers) action. */
187 struct collect_registers_action
189 struct tracepoint_action base
;
192 /* An 'X' (evaluate expression) action. */
194 struct eval_expr_action
196 struct tracepoint_action base
;
198 struct agent_expr
*expr
;
201 /* An 'L' (collect static trace data) action. */
202 struct collect_static_trace_data_action
204 struct tracepoint_action base
;
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. */
213 /* The type of string, such as "cond" for a conditional. */
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. */
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
224 struct source_string
*next
;
227 struct tracepoint_hit_ctx
;
229 /* The definition of a tracepoint. */
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. */
243 /* The number of the tracepoint, as specified by GDB. Several
244 tracepoint objects here may share a number. */
247 /* Address at which the tracepoint is supposed to trigger. Several
248 tracepoints may share an address. */
251 /* True if the tracepoint is currently enabled. */
254 /* The number of single steps that will be performed after each
258 /* The number of times the tracepoint may be hit before it will
259 terminate the entire tracing run. */
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
;
266 /* The list of actions to take when the tracepoint triggers. */
268 struct tracepoint_action
**actions
;
269 /* Same, but in string/packet form. */
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
;
278 /* Count of the times we've hit this tracepoint during the run.
279 Note that while-stepping steps are not counted as "hits". */
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
;
287 /* Handle returned by the breakpoint module when we inserted the
288 trap. NULL if we haven't inserted it yet. */
291 /* Link to the next tracepoint in the list. */
292 struct tracepoint
*next
;
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
305 struct wstep_state
*next
;
307 /* The tracepoint number. */
309 /* The tracepoint's address. */
310 CORE_ADDR tp_address
;
312 /* The number of the current step in this 'while-stepping'
317 /* The linked list of all tracepoints. */
319 static struct tracepoint
*tracepoints
;
321 /* Pointer to the last tracepoint in the list, new tracepoints are
322 linked in at the end. */
324 static struct tracepoint
*last_tracepoint
;
326 /* The first tracepoint to exceed its pass count. */
328 static struct tracepoint
*stopping_tracepoint
;
330 /* True if the trace buffer is full or otherwise no longer usable. */
332 static int trace_buffer_is_full
;
334 /* Enumeration of the different kinds of things that can happen during
335 agent expression evaluation. */
337 enum eval_result_type
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
350 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
352 static const char *eval_result_names
[] =
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"
364 /* The tracepoint in which the error occurred. */
366 static struct tracepoint
*error_tracepoint
;
368 struct trace_state_variable
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. */
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
381 /* The variable's initial value, a 64-bit signed integer always. */
382 LONGEST initial_value
;
384 /* The variable's value, a 64-bit signed integer always. */
387 /* Pointer to a getter function, used to supply computed values. */
388 LONGEST (*getter
) (void);
390 /* Link to the next variable. */
391 struct trace_state_variable
*next
;
394 /* Linked list of all trace state variables. */
396 static struct trace_state_variable
*trace_state_variables
;
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. */
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. */
426 # if defined(__GNUC__)
427 # define ATTR_PACKED __attribute__ ((packed))
429 # define ATTR_PACKED /* nothing */
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. */
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. */
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
451 unsigned int data_size
: 32;
453 /* The base of the trace data, which is contiguous from this point. */
454 unsigned char data
[0];
456 } ATTR_PACKED traceframe_t
;
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. */
461 int current_traceframe
= -1;
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
467 static int circular_trace_buffer
;
469 /* Pointer to the block of memory that traceframes all go into. */
471 static unsigned char *trace_buffer_lo
;
473 /* Pointer to the end of the trace buffer, more precisely to the byte
474 after the end of the buffer. */
476 static unsigned char *trace_buffer_hi
;
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. */
482 static unsigned char *trace_buffer_start
;
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. */
488 static unsigned char *trace_buffer_free
;
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. */
496 static unsigned char *trace_buffer_end_free
;
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
502 static unsigned char *trace_buffer_wrap
;
504 /* Macro that returns a pointer to the first traceframe in the buffer. */
506 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
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. */
512 #define NEXT_TRACEFRAME_1(TF) \
513 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
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) \
521 /* The difference between these counters represents the total number
522 of complete traceframes present in the trace buffer. */
524 static unsigned int traceframe_write_count
;
525 static unsigned int traceframe_read_count
;
527 /* Convenience macro. */
529 #define traceframe_count \
530 ((unsigned int) (traceframe_write_count - traceframe_read_count))
532 /* The count of all traceframes created in the current run, including
533 ones that were discarded to make room. */
535 static int traceframes_created
;
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. */
545 struct readonly_region
547 /* The bounds of the region. */
548 CORE_ADDR start
, end
;
550 /* Link to the next one. */
551 struct readonly_region
*next
;
554 /* Linked list of readonly regions. This list stays in effect from
555 one tstart to the next. */
557 static struct readonly_region
*readonly_regions
;
559 /* The global that controls tracing overall. */
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. */
567 static const char *tracing_stop_reason
= "tnotrun";
569 static int tracing_stop_tpnum
;
571 /* Functions local to this file. */
573 /* Base "class" for tracepoint type specific data to be passed down to
574 collect_data_at_tracepoint. */
575 struct tracepoint_hit_ctx
580 /* Trap tracepoint specific data to be passed down to
581 collect_data_at_tracepoint. */
583 struct trap_tracepoint_ctx
585 struct tracepoint_hit_ctx base
;
587 struct regcache
*regcache
;
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
,
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
);
601 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
602 static int traceframe_read_tsv (int num
, LONGEST
*val
);
604 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
605 struct tracepoint
*tpoint
);
607 static void clear_readonly_regions (void);
608 static void clear_installed_tracepoints (void);
610 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
612 struct tracepoint
*tpoint
);
614 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
616 struct tracepoint
*tpoint
, int current_step
);
618 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
620 struct tracepoint
*tpoint
,
621 struct traceframe
*tframe
,
622 struct tracepoint_action
*taction
);
624 /* Record that an error occurred during expression evaluation. */
627 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
628 enum eval_result_type rtype
)
630 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
631 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
633 expr_eval_result
= rtype
;
634 error_tracepoint
= tpoint
;
637 /* Trace buffer management. */
640 clear_trace_buffer (void)
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;
654 init_trace_buffer (unsigned char *buf
, int bufsize
)
656 trace_buffer_lo
= buf
;
657 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
659 clear_trace_buffer ();
662 /* Carve out a piece of the trace buffer, returning NULL in case of
666 trace_buffer_alloc (size_t amt
)
669 struct traceframe
*oldest
;
670 unsigned char *new_start
;
672 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
673 (long) amt
, (long) sizeof (struct traceframe
));
675 /* Account for the EOB marker. */
676 amt
+= sizeof (struct traceframe
);
678 /* Offsets are easier to grok for debugging than raw addresses,
679 especially for the small trace buffer sizes that are useful for
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
));
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
696 /* First, if we have two free parts, try the upper one first. */
697 if (trace_buffer_end_free
< trace_buffer_free
)
699 if (trace_buffer_free
+ amt
<= trace_buffer_hi
)
700 /* We have enough in the upper part. */
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
708 trace_debug ("Upper part too small, setting wraparound");
709 trace_buffer_wrap
= trace_buffer_free
;
710 trace_buffer_free
= trace_buffer_lo
;
714 /* The normal case. */
715 if (trace_buffer_free
+ amt
<= trace_buffer_end_free
)
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
)
722 trace_debug ("Not enough space in the trace buffer");
726 trace_debug ("Need more space in the trace buffer");
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)
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");
740 --traceframe_write_count
;
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
)
747 trace_debug ("Discarding past the wraparound");
748 trace_buffer_wrap
= trace_buffer_hi
;
750 trace_buffer_start
= new_start
;
751 trace_buffer_end_free
= trace_buffer_start
;
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
));
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. */
766 /* If we get here, we know we can provide the asked-for space. */
768 rslt
= trace_buffer_free
;
770 /* Adjust the request back down, now that we know we have space for
772 trace_buffer_free
+= (amt
- sizeof (struct traceframe
));
774 /* We have a new piece of the trace buffer. Hurray! */
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;
780 /* Adjust the request back down, now that we know we have space for
782 amt
-= sizeof (struct traceframe
);
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
));
798 /* Return the total free space. This is not necessarily the largest
799 block we can allocate, because of the two-part case. */
804 if (trace_buffer_free
<= trace_buffer_end_free
)
805 return trace_buffer_end_free
- trace_buffer_free
;
807 return ((trace_buffer_end_free
- trace_buffer_lo
)
808 + (trace_buffer_hi
- trace_buffer_free
));
811 /* An 'S' in continuation packets indicates remainder are for
814 static int seen_step_action_flag
;
816 /* Create a tracepoint (location) with given number and address. */
818 static struct tracepoint
*
819 add_tracepoint (int num
, CORE_ADDR addr
)
821 struct tracepoint
*tpoint
;
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
;
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
;
837 if (!last_tracepoint
)
838 tracepoints
= tpoint
;
840 last_tracepoint
->next
= tpoint
;
841 last_tracepoint
= tpoint
;
843 seen_step_action_flag
= 0;
848 /* Return the tracepoint with the given number and address, or NULL. */
850 static struct tracepoint
*
851 find_tracepoint (int id
, CORE_ADDR addr
)
853 struct tracepoint
*tpoint
;
855 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
856 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
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. */
867 static struct tracepoint
*
868 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
870 struct tracepoint
*tpoint
;
873 tpoint
= prev_tp
->next
;
875 tpoint
= tracepoints
;
876 for (; tpoint
; tpoint
= tpoint
->next
)
877 if (tpoint
->number
== num
)
884 save_string (const char *str
, size_t len
)
888 s
= xmalloc (len
+ 1);
889 memcpy (s
, str
, len
);
895 /* Append another action to perform when the tracepoint triggers. */
898 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
904 seen_step_action_flag
= 1;
912 char *act_start
= act
;
913 struct tracepoint_action
*action
= NULL
;
919 struct collect_memory_action
*maction
;
923 maction
= xmalloc (sizeof *maction
);
924 maction
->base
.type
= *act
;
925 action
= &maction
->base
;
928 is_neg
= (*act
== '-');
931 act
= unpack_varlen_hex (act
, &basereg
);
933 act
= unpack_varlen_hex (act
, &maction
->addr
);
935 act
= unpack_varlen_hex (act
, &maction
->len
);
936 maction
->basereg
= (is_neg
939 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
940 pulongest (maction
->len
),
941 paddress (maction
->addr
), maction
->basereg
);
946 struct collect_registers_action
*raction
;
948 raction
= xmalloc (sizeof *raction
);
949 raction
->base
.type
= *act
;
950 action
= &raction
->base
;
952 trace_debug ("Want to collect registers");
954 /* skip past hex digits of mask for now */
955 while (isxdigit(*act
))
960 trace_debug ("Unexpected step action, ignoring");
965 struct eval_expr_action
*xaction
;
967 xaction
= xmalloc (sizeof (*xaction
));
968 xaction
->base
.type
= *act
;
969 action
= &xaction
->base
;
971 trace_debug ("Want to evaluate expression");
972 xaction
->expr
= parse_agent_expr (&act
);
976 trace_debug ("unknown trace action '%c', ignoring...", *act
);
985 if (seen_step_action_flag
)
987 tpoint
->num_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
);
1003 tpoint
->numactions
++;
1005 = xrealloc (tpoint
->actions
,
1006 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
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
);
1017 /* Find or create a trace state variable with the given number. */
1019 static struct trace_state_variable
*
1020 get_trace_state_variable (int num
)
1022 struct trace_state_variable
*tsv
;
1024 /* Search for an existing variable. */
1025 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1026 if (tsv
->number
== num
)
1032 /* Find or create a trace state variable with the given number. */
1034 static struct trace_state_variable
*
1035 create_trace_state_variable (int num
)
1037 struct trace_state_variable
*tsv
;
1039 tsv
= get_trace_state_variable (num
);
1043 /* Create a new variable. */
1044 tsv
= xmalloc (sizeof (struct trace_state_variable
));
1046 tsv
->initial_value
= 0;
1050 tsv
->next
= trace_state_variables
;
1051 trace_state_variables
= tsv
;
1057 get_trace_state_variable_value (int num
)
1059 struct trace_state_variable
*tsv
;
1061 tsv
= get_trace_state_variable (num
);
1065 trace_debug ("No trace state variable %d, skipping value get", num
);
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. */
1074 tsv
->value
= (tsv
->getter
) ();
1076 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1077 num
, plongest (tsv
->value
));
1083 set_trace_state_variable_value (int num
, LONGEST val
)
1085 struct trace_state_variable
*tsv
;
1087 tsv
= get_trace_state_variable (num
);
1091 trace_debug ("No trace state variable %d, skipping value set", num
);
1099 set_trace_state_variable_name (int num
, const char *name
)
1101 struct trace_state_variable
*tsv
;
1103 tsv
= get_trace_state_variable (num
);
1107 trace_debug ("No trace state variable %d, skipping name set", num
);
1111 tsv
->name
= (char *) name
;
1115 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
1117 struct trace_state_variable
*tsv
;
1119 tsv
= get_trace_state_variable (num
);
1123 trace_debug ("No trace state variable %d, skipping getter set", num
);
1127 tsv
->getter
= getter
;
1130 /* Add a raw traceframe for the given tracepoint. */
1132 static struct traceframe
*
1133 add_traceframe (struct tracepoint
*tpoint
)
1135 struct traceframe
*tframe
;
1137 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
1142 tframe
->tpnum
= tpoint
->number
;
1143 tframe
->data_size
= 0;
1148 /* Add a block to the traceframe currently being worked on. */
1150 static unsigned char *
1151 add_traceframe_block (struct traceframe
*tframe
, int amt
)
1153 unsigned char *block
;
1158 block
= trace_buffer_alloc (amt
);
1163 tframe
->data_size
+= amt
;
1168 /* Flag that the current traceframe is finished. */
1171 finish_traceframe (struct traceframe
*tframe
)
1173 ++traceframe_write_count
;
1174 ++traceframes_created
;
1177 /* Given a traceframe number NUM, find the NUMth traceframe in the
1180 static struct traceframe
*
1181 find_traceframe (int num
)
1183 struct traceframe
*tframe
;
1186 for (tframe
= FIRST_TRACEFRAME ();
1188 tframe
= NEXT_TRACEFRAME (tframe
))
1199 get_traceframe_address (struct traceframe
*tframe
)
1202 struct tracepoint
*tpoint
;
1204 addr
= traceframe_get_pc (tframe
);
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
;
1215 /* Search for the next traceframe whose address is inside or outside
1218 static struct traceframe
*
1219 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
1222 struct traceframe
*tframe
;
1225 *tfnump
= current_traceframe
+ 1;
1226 tframe
= find_traceframe (*tfnump
);
1227 /* The search is not supposed to wrap around. */
1234 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
1236 tfaddr
= get_traceframe_address (tframe
);
1238 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
1239 : (lo
> tfaddr
|| tfaddr
> hi
))
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. */
1252 static struct traceframe
*
1253 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
1255 struct traceframe
*tframe
;
1257 *tfnump
= current_traceframe
+ 1;
1258 tframe
= find_traceframe (*tfnump
);
1259 /* The search is not supposed to wrap around. */
1266 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
1268 if (tframe
->tpnum
== num
)
1277 /* Clear all past trace state. */
1280 cmd_qtinit (char *packet
)
1282 struct trace_state_variable
*tsv
, *prev
, *next
;
1284 /* Make sure we don't try to read from a trace frame. */
1285 current_traceframe
= -1;
1287 trace_debug ("Initializing the trace");
1289 clear_installed_tracepoints ();
1290 clear_readonly_regions ();
1293 last_tracepoint
= NULL
;
1295 /* Clear out any leftover trace state variables. Ones with target
1296 defined getters should be kept however. */
1298 tsv
= trace_state_variables
;
1301 trace_debug ("Looking at var %d", tsv
->number
);
1302 if (tsv
->getter
== NULL
)
1308 trace_state_variables
= next
;
1309 trace_debug ("Deleting var %d", tsv
->number
);
1320 clear_trace_buffer ();
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. */
1330 clear_installed_tracepoints (void)
1332 struct tracepoint
*tpoint
;
1333 struct tracepoint
*prev_stpoint
;
1335 prev_stpoint
= NULL
;
1337 /* Restore any bytes overwritten by tracepoints. */
1338 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1340 if (!tpoint
->enabled
)
1343 /* Catch the case where we might try to remove a tracepoint that
1344 was never actually installed. */
1345 if (tpoint
->handle
== NULL
)
1347 trace_debug ("Tracepoint %d at 0x%s was "
1348 "never installed, nothing to clear",
1349 tpoint
->number
, paddress (tpoint
->address
));
1353 delete_breakpoint (tpoint
->handle
);
1354 tpoint
->handle
= NULL
;
1358 /* Parse a packet that defines a tracepoint. */
1361 cmd_qtdp (char *own_buf
)
1367 struct tracepoint
*tpoint
;
1369 char *packet
= own_buf
;
1371 packet
+= strlen ("QTDP:");
1373 /* A hyphen at the beginning marks a packet specifying actions for a
1374 tracepoint already supplied. */
1381 packet
= unpack_varlen_hex (packet
, &num
);
1382 ++packet
; /* skip a colon */
1383 packet
= unpack_varlen_hex (packet
, &addr
);
1384 ++packet
; /* skip a colon */
1386 /* See if we already have this tracepoint. */
1387 tpoint
= find_tracepoint (num
, addr
);
1391 /* Duplicate tracepoints are never allowed. */
1394 trace_debug ("Tracepoint error: tracepoint %d"
1395 " at 0x%s already exists",
1396 (int) num
, paddress (addr
));
1397 write_enn (own_buf
);
1401 tpoint
= add_tracepoint (num
, addr
);
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
== ':')
1417 actparm
= (char *) packet
;
1418 tpoint
->cond
= parse_agent_expr (&actparm
);
1421 else if (*packet
== '-')
1423 else if (*packet
== '\0')
1426 trace_debug ("Unknown optional tracepoint field");
1429 trace_debug ("Also has actions\n");
1431 trace_debug ("Defined tracepoint %d at 0x%s, "
1432 "enabled %d step %ld pass %ld",
1433 tpoint
->number
, paddress (tpoint
->address
),
1435 tpoint
->step_count
, tpoint
->pass_count
);
1438 add_tracepoint_action (tpoint
, packet
);
1441 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
1442 (int) num
, paddress (addr
));
1443 write_enn (own_buf
);
1451 cmd_qtdpsrc (char *own_buf
)
1453 ULONGEST num
, addr
, start
, slen
;
1454 struct tracepoint
*tpoint
;
1455 char *packet
= own_buf
;
1456 char *saved
, *srctype
, *src
;
1458 struct source_string
*last
, *newlast
;
1460 packet
+= strlen ("QTDPsrc:");
1462 packet
= unpack_varlen_hex (packet
, &num
);
1463 ++packet
; /* skip a colon */
1464 packet
= unpack_varlen_hex (packet
, &addr
);
1465 ++packet
; /* skip a colon */
1467 /* See if we already have this tracepoint. */
1468 tpoint
= find_tracepoint (num
, addr
);
1472 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
1473 (int) num
, paddress (addr
));
1474 write_enn (own_buf
);
1479 packet
= strchr (packet
, ':');
1480 srctype
= xmalloc (packet
- saved
+ 1);
1481 memcpy (srctype
, saved
, packet
- saved
);
1482 srctype
[packet
- saved
] = '\0';
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);
1492 newlast
= xmalloc (sizeof (struct source_string
));
1493 newlast
->type
= srctype
;
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
1499 if (tpoint
->source_strings
)
1501 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
1503 last
->next
= newlast
;
1506 tpoint
->source_strings
= newlast
;
1512 cmd_qtdv (char *own_buf
)
1514 ULONGEST num
, val
, builtin
;
1517 struct trace_state_variable
*tsv
;
1518 char *packet
= own_buf
;
1520 packet
+= strlen ("QTDV:");
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 */
1529 nbytes
= strlen (packet
) / 2;
1530 varname
= xmalloc (nbytes
+ 1);
1531 nbytes
= unhexify (varname
, packet
, nbytes
);
1532 varname
[nbytes
] = '\0';
1534 tsv
= create_trace_state_variable (num
);
1535 tsv
->initial_value
= (LONGEST
) val
;
1536 tsv
->name
= varname
;
1538 set_trace_state_variable_value (num
, (LONGEST
) val
);
1544 cmd_qtv (char *own_buf
)
1549 char *packet
= own_buf
;
1551 packet
+= strlen ("qTV:");
1552 packet
= unpack_varlen_hex (packet
, &num
);
1554 if (current_traceframe
>= 0)
1556 err
= traceframe_read_tsv ((int) num
, &val
);
1559 strcpy (own_buf
, "U");
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)
1568 strcpy (own_buf
, "U");
1572 val
= get_trace_state_variable_value (num
);
1574 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
1577 /* Clear out the list of readonly regions. */
1580 clear_readonly_regions (void)
1582 struct readonly_region
*roreg
;
1584 while (readonly_regions
)
1586 roreg
= readonly_regions
;
1587 readonly_regions
= readonly_regions
->next
;
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. */
1597 cmd_qtro (char *own_buf
)
1599 ULONGEST start
, end
;
1600 struct readonly_region
*roreg
;
1601 char *packet
= own_buf
;
1603 trace_debug ("Want to mark readonly regions");
1605 clear_readonly_regions ();
1607 packet
+= strlen ("QTro");
1609 while (*packet
== ':')
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
;
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
));
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. */
1632 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
1634 struct readonly_region
*roreg
;
1636 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
1637 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
1644 cmd_qtstart (char *packet
)
1646 struct tracepoint
*tpoint
;
1647 int slow_tracepoint_count
;
1649 trace_debug ("Starting the trace");
1651 slow_tracepoint_count
= 0;
1655 /* Install tracepoints. */
1656 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1658 /* Ensure all the hit counts start at zero. */
1659 tpoint
->hit_count
= 0;
1661 if (!tpoint
->enabled
)
1664 ++slow_tracepoint_count
;
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
);
1672 /* Any failure is sufficient cause to give up. */
1673 if (tpoint
->handle
== NULL
)
1677 /* Any error in tracepoint insertion is unacceptable; better to
1678 address the problem now, than end up with a useless or misleading
1682 clear_installed_tracepoints ();
1683 if (*packet
== '\0')
1688 stopping_tracepoint
= NULL
;
1689 trace_buffer_is_full
= 0;
1690 expr_eval_result
= expr_eval_no_error
;
1691 error_tracepoint
= NULL
;
1693 /* Tracing is now active, hits will now start being logged. */
1699 /* End a tracing run, filling in a stop reason to report back to GDB,
1700 and removing the tracepoints from the code. */
1707 trace_debug ("Tracing is already off, ignoring");
1711 trace_debug ("Stopping the trace");
1713 /* Stop logging. Tracepoints can still be hit, but they will not be
1717 tracing_stop_reason
= "t???";
1718 tracing_stop_tpnum
= 0;
1719 if (stopping_tracepoint
)
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
;
1728 else if (trace_buffer_is_full
)
1730 trace_debug ("Stopping the trace because the trace buffer is full");
1731 tracing_stop_reason
= "tfull";
1733 else if (expr_eval_result
!= expr_eval_no_error
)
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
;
1741 trace_debug ("Stopping the trace because of a tstop command");
1742 tracing_stop_reason
= "tstop";
1745 stopping_tracepoint
= NULL
;
1746 error_tracepoint
= NULL
;
1748 /* Clear out the tracepoints. */
1749 clear_installed_tracepoints ();
1753 cmd_qtstop (char *packet
)
1760 cmd_qtframe (char *own_buf
)
1762 ULONGEST frame
, pc
, lo
, hi
, num
;
1764 struct traceframe
*tframe
;
1765 char *packet
= own_buf
;
1767 packet
+= strlen ("QTFrame:");
1769 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
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
);
1776 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
1778 packet
+= strlen ("range:");
1779 packet
= unpack_varlen_hex (packet
, &lo
);
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
);
1786 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
1788 packet
+= strlen ("outside:");
1789 packet
= unpack_varlen_hex (packet
, &lo
);
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
);
1797 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
1799 packet
+= strlen ("tdp:");
1800 packet
= unpack_varlen_hex (packet
, &num
);
1802 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
1803 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
1807 unpack_varlen_hex (packet
, &frame
);
1808 tfnum
= (int) frame
;
1811 trace_debug ("Want to stop looking at traceframes");
1812 current_traceframe
= -1;
1816 trace_debug ("Want to look at traceframe %d", tfnum
);
1817 tframe
= find_traceframe (tfnum
);
1822 current_traceframe
= tfnum
;
1823 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
1826 sprintf (own_buf
, "F-1");
1830 cmd_qtstatus (char *packet
)
1832 char *stop_reason_rsp
= NULL
;
1834 trace_debug ("Returning trace status as %d, stop reason %s",
1835 tracing
, tracing_stop_reason
);
1837 stop_reason_rsp
= (char *) tracing_stop_reason
;
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
1842 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
1844 const char *result_name
;
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:");
1853 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
1856 sprintf (packet
, "T%d;%s:%x;tframes:%x;tcreated:%x;tfree:%x;tsize:%s;circular:%d",
1858 stop_reason_rsp
, tracing_stop_tpnum
,
1859 traceframe_count
, traceframes_created
,
1861 phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
1862 circular_trace_buffer
);
1865 /* State variables to help return all the tracepoint bits. */
1866 static struct tracepoint
*cur_tpoint
;
1867 static int cur_action
;
1868 static int cur_step_action
;
1869 static struct source_string
*cur_source_string
;
1870 static struct trace_state_variable
*cur_tsv
;
1872 /* Compose a response that is an imitation of the syntax by which the
1873 tracepoint was originally downloaded. */
1876 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
1880 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
1881 paddress (tpoint
->address
),
1882 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
1883 tpoint
->pass_count
);
1887 buf
= unparse_agent_expr (tpoint
->cond
);
1888 sprintf (packet
+ strlen (packet
), ":X%x,%s",
1889 tpoint
->cond
->length
, buf
);
1894 /* Compose a response that is an imitation of the syntax by which the
1895 tracepoint action was originally downloaded (with the difference
1896 that due to the way we store the actions, this will output a packet
1897 per action, while GDB could have combined more than one action
1901 response_action (char *packet
, struct tracepoint
*tpoint
,
1902 char *taction
, int step
)
1904 sprintf (packet
, "%c%x:%s:%s",
1905 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
1909 /* Compose a response that is an imitation of the syntax by which the
1910 tracepoint source piece was originally downloaded. */
1913 response_source (char *packet
,
1914 struct tracepoint
*tpoint
, struct source_string
*src
)
1919 len
= strlen (src
->str
);
1920 buf
= alloca (len
* 2 + 1);
1921 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
1923 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
1924 tpoint
->number
, paddress (tpoint
->address
),
1925 src
->type
, 0, len
, buf
);
1928 /* Return the first piece of tracepoint definition, and initialize the
1929 state machine that will iterate through all the tracepoint
1933 cmd_qtfp (char *packet
)
1935 trace_debug ("Returning first tracepoint definition piece");
1937 cur_tpoint
= tracepoints
;
1938 cur_action
= cur_step_action
= -1;
1939 cur_source_string
= NULL
;
1942 response_tracepoint (packet
, cur_tpoint
);
1944 strcpy (packet
, "l");
1947 /* Return additional pieces of tracepoint definition. Each action and
1948 stepping action must go into its own packet, because of packet size
1949 limits, and so we use state variables to deliver one piece at a
1953 cmd_qtsp (char *packet
)
1955 trace_debug ("Returning subsequent tracepoint definition piece");
1959 /* This case would normally never occur, but be prepared for
1961 strcpy (packet
, "l");
1963 else if (cur_action
< cur_tpoint
->numactions
- 1)
1966 response_action (packet
, cur_tpoint
,
1967 cur_tpoint
->actions_str
[cur_action
], 0);
1969 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
1972 response_action (packet
, cur_tpoint
,
1973 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
1975 else if ((cur_source_string
1976 ? cur_source_string
->next
1977 : cur_tpoint
->source_strings
))
1979 if (cur_source_string
)
1980 cur_source_string
= cur_source_string
->next
;
1982 cur_source_string
= cur_tpoint
->source_strings
;
1983 response_source (packet
, cur_tpoint
, cur_source_string
);
1987 cur_tpoint
= cur_tpoint
->next
;
1988 cur_action
= cur_step_action
= -1;
1989 cur_source_string
= NULL
;
1991 response_tracepoint (packet
, cur_tpoint
);
1993 strcpy (packet
, "l");
1997 /* Compose a response that is an imitation of the syntax by which the
1998 trace state variable was originally downloaded. */
2001 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
2003 char *buf
= (char *) "";
2008 namelen
= strlen (tsv
->name
);
2009 buf
= alloca (namelen
* 2 + 1);
2010 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
2013 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
2014 tsv
->getter
? 1 : 0, buf
);
2017 /* Return the first trace state variable definition, and initialize
2018 the state machine that will iterate through all the tsv bits. */
2021 cmd_qtfv (char *packet
)
2023 trace_debug ("Returning first trace state variable definition");
2025 cur_tsv
= trace_state_variables
;
2028 response_tsv (packet
, cur_tsv
);
2030 strcpy (packet
, "l");
2033 /* Return additional trace state variable definitions. */
2036 cmd_qtsv (char *packet
)
2038 trace_debug ("Returning first trace state variable definition");
2042 /* This case would normally never occur, but be prepared for
2044 strcpy (packet
, "l");
2048 cur_tsv
= cur_tsv
->next
;
2050 response_tsv (packet
, cur_tsv
);
2052 strcpy (packet
, "l");
2055 strcpy (packet
, "l");
2058 /* Respond to qTBuffer packet with a block of raw data from the trace
2059 buffer. GDB may ask for a lot, but we are allowed to reply with
2060 only as much as will fit within packet limits or whatever. */
2063 cmd_qtbuffer (char *own_buf
)
2065 ULONGEST offset
, num
, tot
;
2067 char *packet
= own_buf
;
2069 packet
+= strlen ("qTBuffer:");
2071 packet
= unpack_varlen_hex (packet
, &offset
);
2072 ++packet
; /* skip a comma */
2073 packet
= unpack_varlen_hex (packet
, &num
);
2075 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
2076 (int) num
, pulongest (offset
));
2078 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
2080 /* If we're right at the end, reply specially that we're done. */
2083 strcpy (own_buf
, "l");
2087 /* Object to any other out-of-bounds request. */
2090 write_enn (own_buf
);
2094 /* Compute the pointer corresponding to the given offset, accounting
2096 tbp
= trace_buffer_start
+ offset
;
2097 if (tbp
>= trace_buffer_wrap
)
2098 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
2100 /* Trim to the remaining bytes if we're close to the end. */
2101 if (num
> tot
- offset
)
2104 /* Trim to available packet size. */
2105 if (num
>= (PBUFSIZ
- 16) / 2 )
2106 num
= (PBUFSIZ
- 16) / 2;
2108 convert_int_to_ascii (tbp
, own_buf
, num
);
2109 own_buf
[num
] = '\0';
2113 cmd_bigqtbuffer (char *own_buf
)
2116 char *packet
= own_buf
;
2118 packet
+= strlen ("QTBuffer:");
2120 if (strncmp ("circular:", packet
, strlen ("circular:")) == 0)
2122 packet
+= strlen ("circular:");
2123 packet
= unpack_varlen_hex (packet
, &val
);
2124 circular_trace_buffer
= val
;
2125 trace_debug ("Trace buffer is now %s",
2126 circular_trace_buffer
? "circular" : "linear");
2130 write_enn (own_buf
);
2134 handle_tracepoint_general_set (char *packet
)
2136 if (strcmp ("QTinit", packet
) == 0)
2138 cmd_qtinit (packet
);
2141 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
2146 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
2148 cmd_qtdpsrc (packet
);
2151 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
2156 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
2161 else if (strcmp ("QTStart", packet
) == 0)
2163 cmd_qtstart (packet
);
2166 else if (strcmp ("QTStop", packet
) == 0)
2168 cmd_qtstop (packet
);
2171 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
2173 cmd_qtframe (packet
);
2176 else if (strncmp ("QTBuffer:", packet
, strlen ("QTBuffer:")) == 0)
2178 cmd_bigqtbuffer (packet
);
2186 handle_tracepoint_query (char *packet
)
2188 if (strcmp ("qTStatus", packet
) == 0)
2190 cmd_qtstatus (packet
);
2193 else if (strcmp ("qTfP", packet
) == 0)
2198 else if (strcmp ("qTsP", packet
) == 0)
2203 else if (strcmp ("qTfV", packet
) == 0)
2208 else if (strcmp ("qTsV", packet
) == 0)
2213 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
2218 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
2220 cmd_qtbuffer (packet
);
2227 /* Call this when thread TINFO has hit the tracepoint defined by
2228 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
2229 action. This adds a while-stepping collecting state item to the
2230 threads' collecting state list, so that we can keep track of
2231 multiple simultaneous while-stepping actions being collected by the
2232 same thread. This can happen in cases like:
2234 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
2236 ff0003 INSN3 <-- TP2, collect $regs
2237 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
2240 Notice that when instruction INSN5 is reached, the while-stepping
2241 actions of both TP1 and TP3 are still being collected, and that TP2
2242 had been collected meanwhile. The whole range of ff0001-ff0005
2243 should be single-stepped, due to at least TP1's while-stepping
2244 action covering the whole range. */
2247 add_while_stepping_state (struct thread_info
*tinfo
,
2248 int tp_number
, CORE_ADDR tp_address
)
2250 struct wstep_state
*wstep
;
2252 wstep
= xmalloc (sizeof (*wstep
));
2253 wstep
->next
= tinfo
->while_stepping
;
2255 wstep
->tp_number
= tp_number
;
2256 wstep
->tp_address
= tp_address
;
2257 wstep
->current_step
= 0;
2259 tinfo
->while_stepping
= wstep
;
2262 /* Release the while-stepping collecting state WSTEP. */
2265 release_while_stepping_state (struct wstep_state
*wstep
)
2270 /* Release all while-stepping collecting states currently associated
2271 with thread TINFO. */
2274 release_while_stepping_state_list (struct thread_info
*tinfo
)
2276 struct wstep_state
*head
;
2278 while (tinfo
->while_stepping
)
2280 head
= tinfo
->while_stepping
;
2281 tinfo
->while_stepping
= head
->next
;
2282 release_while_stepping_state (head
);
2286 /* If TINFO was handling a 'while-stepping' action, the step has
2287 finished, so collect any step data needed, and check if any more
2288 steps are required. Return true if the thread was indeed
2289 collecting tracepoint data, false otherwise. */
2292 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
2294 struct tracepoint
*tpoint
;
2295 struct wstep_state
*wstep
;
2296 struct wstep_state
**wstep_link
;
2297 struct trap_tracepoint_ctx ctx
;
2299 /* Check if we were indeed collecting data for one of more
2300 tracepoints with a 'while-stepping' count. */
2301 if (tinfo
->while_stepping
== NULL
)
2306 /* We're not even tracing anymore. Stop this thread from
2308 release_while_stepping_state_list (tinfo
);
2310 /* The thread had stopped due to a single-step request indeed
2311 explained by a tracepoint. */
2315 wstep
= tinfo
->while_stepping
;
2316 wstep_link
= &tinfo
->while_stepping
;
2318 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
2319 target_pid_to_str (tinfo
->entry
.id
),
2320 wstep
->tp_number
, paddress (wstep
->tp_address
));
2322 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
2324 while (wstep
!= NULL
)
2326 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
2329 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
2330 wstep
->tp_number
, paddress (wstep
->tp_address
),
2331 target_pid_to_str (tinfo
->entry
.id
));
2334 *wstep_link
= wstep
->next
;
2335 release_while_stepping_state (wstep
);
2339 /* We've just finished one step. */
2340 ++wstep
->current_step
;
2343 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
2344 stop_pc
, tpoint
, wstep
->current_step
);
2346 if (wstep
->current_step
>= tpoint
->step_count
)
2348 /* The requested numbers of steps have occurred. */
2349 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
2350 target_pid_to_str (tinfo
->entry
.id
),
2351 wstep
->tp_number
, paddress (wstep
->tp_address
));
2353 /* Unlink the wstep. */
2354 *wstep_link
= wstep
->next
;
2355 release_while_stepping_state (wstep
);
2356 wstep
= *wstep_link
;
2358 /* Only check the hit count now, which ensure that we do all
2359 our stepping before stopping the run. */
2360 if (tpoint
->pass_count
> 0
2361 && tpoint
->hit_count
>= tpoint
->pass_count
2362 && stopping_tracepoint
== NULL
)
2363 stopping_tracepoint
= tpoint
;
2367 /* Keep single-stepping until the requested numbers of steps
2369 wstep_link
= &wstep
->next
;
2370 wstep
= *wstep_link
;
2373 if (stopping_tracepoint
2374 || trace_buffer_is_full
2375 || expr_eval_result
!= expr_eval_no_error
)
2385 /* Return true if TINFO just hit a tracepoint. Collect data if
2389 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
2391 struct tracepoint
*tpoint
;
2393 struct trap_tracepoint_ctx ctx
;
2395 /* Not tracing, don't handle. */
2399 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
2401 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2403 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
)
2405 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
2406 target_pid_to_str (tinfo
->entry
.id
),
2407 tpoint
->number
, paddress (tpoint
->address
));
2409 /* Test the condition if present, and collect if true. */
2411 || (condition_true_at_tracepoint
2412 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
2413 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
2416 if (stopping_tracepoint
2417 || trace_buffer_is_full
2418 || expr_eval_result
!= expr_eval_no_error
)
2422 /* If the tracepoint had a 'while-stepping' action, then set
2423 the thread to collect this tracepoint on the following
2425 else if (tpoint
->step_count
> 0)
2427 add_while_stepping_state (tinfo
,
2428 tpoint
->number
, tpoint
->address
);
2438 /* Create a trace frame for the hit of the given tracepoint in the
2442 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
2443 struct tracepoint
*tpoint
)
2445 struct traceframe
*tframe
;
2448 /* Only count it as a hit when we actually collect data. */
2449 tpoint
->hit_count
++;
2451 /* If we've exceeded a defined pass count, record the event for
2452 later, and finish the collection for this hit. This test is only
2453 for nonstepping tracepoints, stepping tracepoints test at the end
2454 of their while-stepping loop. */
2455 if (tpoint
->pass_count
> 0
2456 && tpoint
->hit_count
>= tpoint
->pass_count
2457 && tpoint
->step_count
== 0
2458 && stopping_tracepoint
== NULL
)
2459 stopping_tracepoint
= tpoint
;
2461 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
2462 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
2464 tframe
= add_traceframe (tpoint
);
2468 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
2470 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
2471 tpoint
->number
, paddress (tpoint
->address
),
2472 tpoint
->actions_str
[acti
]);
2474 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
2475 tpoint
->actions
[acti
]);
2478 finish_traceframe (tframe
);
2481 if (tframe
== NULL
&& tracing
)
2482 trace_buffer_is_full
= 1;
2486 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
2488 struct tracepoint
*tpoint
, int current_step
)
2490 struct traceframe
*tframe
;
2493 trace_debug ("Making new step traceframe for "
2494 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
2495 tpoint
->number
, paddress (tpoint
->address
),
2496 current_step
, tpoint
->step_count
,
2499 tframe
= add_traceframe (tpoint
);
2503 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
2505 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
2506 tpoint
->number
, paddress (tpoint
->address
),
2507 tpoint
->step_actions_str
[acti
]);
2509 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
2510 tpoint
->step_actions
[acti
]);
2513 finish_traceframe (tframe
);
2516 if (tframe
== NULL
&& tracing
)
2517 trace_buffer_is_full
= 1;
2520 static struct regcache
*
2521 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
2523 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
2524 struct regcache
*regcache
= tctx
->regcache
;
2526 gdb_assert (regcache
!= NULL
);
2532 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
2534 struct tracepoint
*tpoint
,
2535 struct traceframe
*tframe
,
2536 struct tracepoint_action
*taction
)
2538 enum eval_result_type err
;
2540 switch (taction
->type
)
2544 struct collect_memory_action
*maction
;
2546 maction
= (struct collect_memory_action
*) taction
;
2548 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
2549 pulongest (maction
->len
),
2550 paddress (maction
->addr
), maction
->basereg
);
2551 /* (should use basereg) */
2552 agent_mem_read (tframe
, NULL
,
2553 (CORE_ADDR
) maction
->addr
, maction
->len
);
2558 struct collect_registers_action
*raction
;
2560 unsigned char *regspace
;
2561 struct regcache tregcache
;
2562 struct regcache
*context_regcache
;
2564 raction
= (struct collect_registers_action
*) taction
;
2566 trace_debug ("Want to collect registers");
2568 /* Collect all registers for now. */
2569 regspace
= add_traceframe_block (tframe
,
2570 1 + register_cache_size ());
2571 if (regspace
== NULL
)
2573 trace_debug ("Trace buffer block allocation failed, skipping");
2576 /* Identify a register block. */
2579 context_regcache
= get_context_regcache (ctx
);
2581 /* Wrap the regblock in a register cache (in the stack, we
2582 don't want to malloc here). */
2583 init_register_cache (&tregcache
, regspace
+ 1);
2585 /* Copy the register data to the regblock. */
2586 regcache_cpy (&tregcache
, context_regcache
);
2588 /* On some platforms, trap-based tracepoints will have the PC
2589 pointing to the next instruction after the trap, but we
2590 don't want the user or GDB trying to guess whether the
2591 saved PC needs adjusting; so always record the adjusted
2592 stop_pc. Note that we can't use tpoint->address instead,
2593 since it will be wrong for while-stepping actions. */
2594 trace_debug ("Storing stop pc (0x%s) in regblock",
2595 paddress (tpoint
->address
));
2597 /* This changes the regblock, not the thread's
2599 regcache_write_pc (&tregcache
, stop_pc
);
2604 struct eval_expr_action
*eaction
;
2606 eaction
= (struct eval_expr_action
*) taction
;
2608 trace_debug ("Want to evaluate expression");
2610 err
= eval_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
2612 if (err
!= expr_eval_no_error
)
2614 record_tracepoint_error (tpoint
, "action expression", err
);
2620 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
2626 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
2627 struct tracepoint
*tpoint
)
2630 enum eval_result_type err
;
2632 err
= eval_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
2634 if (err
!= expr_eval_no_error
)
2636 record_tracepoint_error (tpoint
, "condition", err
);
2637 /* The error case must return false. */
2641 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
2642 tpoint
->number
, paddress (tpoint
->address
),
2644 return (value
? 1 : 0);
2647 /* The packet form of an agent expression consists of an 'X', number
2648 of bytes in expression, a comma, and then the bytes. */
2650 static struct agent_expr
*
2651 parse_agent_expr (char **actparm
)
2653 char *act
= *actparm
;
2655 struct agent_expr
*aexpr
;
2657 ++act
; /* skip the X */
2658 act
= unpack_varlen_hex (act
, &xlen
);
2659 ++act
; /* skip a comma */
2660 aexpr
= xmalloc (sizeof (struct agent_expr
));
2661 aexpr
->length
= xlen
;
2662 aexpr
->bytes
= xmalloc (xlen
);
2663 convert_ascii_to_int (act
, aexpr
->bytes
, xlen
);
2664 *actparm
= act
+ (xlen
* 2);
2668 /* Convert the bytes of an agent expression back into hex digits, so
2669 they can be printed or uploaded. This allocates the buffer,
2670 callers should free when they are done with it. */
2673 unparse_agent_expr (struct agent_expr
*aexpr
)
2677 rslt
= xmalloc (2 * aexpr
->length
+ 1);
2678 convert_int_to_ascii (aexpr
->bytes
, rslt
, aexpr
->length
);
2682 /* The agent expression evaluator, as specified by the GDB docs. It
2683 returns 0 if everything went OK, and a nonzero error code
2686 static enum eval_result_type
2687 eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
2688 struct traceframe
*tframe
,
2689 struct agent_expr
*aexpr
,
2693 #define STACK_MAX 100
2694 ULONGEST stack
[STACK_MAX
], top
;
2699 /* This union is a convenient way to convert representations. For
2700 now, assume a standard architecture where the hardware integer
2701 types have 8, 16, 32, 64 bit types. A more robust solution would
2702 be to import stdint.h from gnulib. */
2707 unsigned char bytes
[1];
2712 unsigned char bytes
[2];
2717 unsigned char bytes
[4];
2722 unsigned char bytes
[8];
2727 if (aexpr
->length
== 0)
2729 trace_debug ("empty agent expression");
2730 return expr_eval_empty_expression
;
2733 /* Cache the stack top in its own variable. Much of the time we can
2734 operate on this variable, rather than dinking with the stack. It
2735 needs to be copied to the stack when sp changes. */
2740 op
= aexpr
->bytes
[pc
++];
2742 trace_debug ("About to interpret byte 0x%x", op
);
2746 case gdb_agent_op_add
:
2750 case gdb_agent_op_sub
:
2751 top
= stack
[--sp
] - top
;
2754 case gdb_agent_op_mul
:
2758 case gdb_agent_op_div_signed
:
2761 trace_debug ("Attempted to divide by zero");
2762 return expr_eval_divide_by_zero
;
2764 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
2767 case gdb_agent_op_div_unsigned
:
2770 trace_debug ("Attempted to divide by zero");
2771 return expr_eval_divide_by_zero
;
2773 top
= stack
[--sp
] / top
;
2776 case gdb_agent_op_rem_signed
:
2779 trace_debug ("Attempted to divide by zero");
2780 return expr_eval_divide_by_zero
;
2782 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
2785 case gdb_agent_op_rem_unsigned
:
2788 trace_debug ("Attempted to divide by zero");
2789 return expr_eval_divide_by_zero
;
2791 top
= stack
[--sp
] % top
;
2794 case gdb_agent_op_lsh
:
2795 top
= stack
[--sp
] << top
;
2798 case gdb_agent_op_rsh_signed
:
2799 top
= ((LONGEST
) stack
[--sp
]) >> top
;
2802 case gdb_agent_op_rsh_unsigned
:
2803 top
= stack
[--sp
] >> top
;
2806 case gdb_agent_op_trace
:
2807 agent_mem_read (tframe
,
2808 NULL
, (CORE_ADDR
) stack
[--sp
], (ULONGEST
) top
);
2813 case gdb_agent_op_trace_quick
:
2814 arg
= aexpr
->bytes
[pc
++];
2815 agent_mem_read (tframe
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
2818 case gdb_agent_op_log_not
:
2822 case gdb_agent_op_bit_and
:
2826 case gdb_agent_op_bit_or
:
2830 case gdb_agent_op_bit_xor
:
2834 case gdb_agent_op_bit_not
:
2838 case gdb_agent_op_equal
:
2839 top
= (stack
[--sp
] == top
);
2842 case gdb_agent_op_less_signed
:
2843 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
2846 case gdb_agent_op_less_unsigned
:
2847 top
= (stack
[--sp
] < top
);
2850 case gdb_agent_op_ext
:
2851 arg
= aexpr
->bytes
[pc
++];
2852 if (arg
< (sizeof (LONGEST
) * 8))
2854 LONGEST mask
= 1 << (arg
- 1);
2855 top
&= ((LONGEST
) 1 << arg
) - 1;
2856 top
= (top
^ mask
) - mask
;
2860 case gdb_agent_op_ref8
:
2861 agent_mem_read (tframe
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
2865 case gdb_agent_op_ref16
:
2866 agent_mem_read (tframe
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
2870 case gdb_agent_op_ref32
:
2871 agent_mem_read (tframe
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
2875 case gdb_agent_op_ref64
:
2876 agent_mem_read (tframe
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
2880 case gdb_agent_op_if_goto
:
2882 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
2889 case gdb_agent_op_goto
:
2890 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
2893 case gdb_agent_op_const8
:
2894 /* Flush the cached stack top. */
2896 top
= aexpr
->bytes
[pc
++];
2899 case gdb_agent_op_const16
:
2900 /* Flush the cached stack top. */
2902 top
= aexpr
->bytes
[pc
++];
2903 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2906 case gdb_agent_op_const32
:
2907 /* Flush the cached stack top. */
2909 top
= aexpr
->bytes
[pc
++];
2910 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2911 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2912 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2915 case gdb_agent_op_const64
:
2916 /* Flush the cached stack top. */
2918 top
= 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 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2928 case gdb_agent_op_reg
:
2929 /* Flush the cached stack top. */
2931 arg
= aexpr
->bytes
[pc
++];
2932 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
2935 struct regcache
*regcache
;
2937 regcache
= get_context_regcache (ctx
);
2939 switch (register_size (regnum
))
2942 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
2946 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
2950 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
2954 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
2958 internal_error (__FILE__
, __LINE__
,
2959 "unhandled register size");
2964 case gdb_agent_op_end
:
2965 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
2966 sp
, pulongest (top
));
2971 /* This should be an error */
2972 trace_debug ("Stack is empty, nothing to return");
2973 return expr_eval_empty_stack
;
2977 return expr_eval_no_error
;
2979 case gdb_agent_op_dup
:
2983 case gdb_agent_op_pop
:
2988 case gdb_agent_op_zero_ext
:
2989 arg
= aexpr
->bytes
[pc
++];
2990 if (arg
< (sizeof (LONGEST
) * 8))
2991 top
&= ((LONGEST
) 1 << arg
) - 1;
2994 case gdb_agent_op_swap
:
2995 /* Interchange top two stack elements, making sure top gets
2996 copied back onto stack. */
2998 top
= stack
[sp
- 1];
2999 stack
[sp
- 1] = stack
[sp
];
3002 case gdb_agent_op_getv
:
3003 /* Flush the cached stack top. */
3005 arg
= aexpr
->bytes
[pc
++];
3006 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
3007 top
= get_trace_state_variable_value (arg
);
3010 case gdb_agent_op_setv
:
3011 arg
= aexpr
->bytes
[pc
++];
3012 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
3013 set_trace_state_variable_value (arg
, top
);
3014 /* Note that we leave the value on the stack, for the
3015 benefit of later/enclosing expressions. */
3018 case gdb_agent_op_tracev
:
3019 arg
= aexpr
->bytes
[pc
++];
3020 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
3021 agent_tsv_read (tframe
, arg
);
3024 /* GDB never (currently) generates any of these ops. */
3025 case gdb_agent_op_float
:
3026 case gdb_agent_op_ref_float
:
3027 case gdb_agent_op_ref_double
:
3028 case gdb_agent_op_ref_long_double
:
3029 case gdb_agent_op_l_to_d
:
3030 case gdb_agent_op_d_to_l
:
3031 case gdb_agent_op_trace16
:
3032 trace_debug ("Agent expression op 0x%x valid, but not handled",
3034 /* If ever GDB generates any of these, we don't have the
3035 option of ignoring. */
3039 trace_debug ("Agent expression op 0x%x not recognized", op
);
3040 /* Don't struggle on, things will just get worse. */
3041 return expr_eval_unrecognized_opcode
;
3044 /* Check for stack badness. */
3045 if (sp
>= (STACK_MAX
- 1))
3047 trace_debug ("Expression stack overflow");
3048 return expr_eval_stack_overflow
;
3053 trace_debug ("Expression stack underflow");
3054 return expr_eval_stack_underflow
;
3057 trace_debug ("Op %s -> sp=%d, top=0x%s",
3058 gdb_agent_op_names
[op
], sp
, pulongest (top
));
3062 /* Do memory copies for bytecodes. */
3063 /* Do the recording of memory blocks for actions and bytecodes. */
3066 agent_mem_read (struct traceframe
*tframe
,
3067 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
3069 unsigned char *mspace
;
3070 ULONGEST remaining
= len
;
3071 unsigned short blocklen
;
3073 /* If a 'to' buffer is specified, use it. */
3076 read_inferior_memory (from
, to
, len
);
3080 /* Otherwise, create a new memory block in the trace buffer. */
3081 while (remaining
> 0)
3085 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
3086 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
3087 mspace
= add_traceframe_block (tframe
, sp
);
3090 /* Identify block as a memory block. */
3093 /* Record address and size. */
3094 memcpy (mspace
, &from
, sizeof (from
));
3095 mspace
+= sizeof (from
);
3096 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
3097 mspace
+= sizeof (blocklen
);
3098 /* Record the memory block proper. */
3099 read_inferior_memory (from
, mspace
, blocklen
);
3100 trace_debug ("%d bytes recorded", blocklen
);
3101 remaining
-= blocklen
;
3107 /* Record the value of a trace state variable. */
3110 agent_tsv_read (struct traceframe
*tframe
, int n
)
3112 unsigned char *vspace
;
3115 vspace
= add_traceframe_block (tframe
,
3116 1 + sizeof (n
) + sizeof (LONGEST
));
3119 /* Identify block as a variable. */
3121 /* Record variable's number and value. */
3122 memcpy (vspace
+ 1, &n
, sizeof (n
));
3123 val
= get_trace_state_variable_value (n
);
3124 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
3125 trace_debug ("Variable %d recorded", n
);
3129 static unsigned char *
3130 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
3131 int tfnum
, char type_wanted
)
3133 unsigned char *dataptr
;
3137 trace_debug ("traceframe %d has no data", tfnum
);
3141 /* Iterate through a traceframe's blocks, looking for a block of the
3143 for (dataptr
= database
;
3144 dataptr
< database
+ datasize
;
3148 unsigned short mlen
;
3150 if (dataptr
== trace_buffer_wrap
)
3152 /* Adjust to reflect wrapping part of the frame around to
3154 datasize
= dataptr
- database
;
3155 dataptr
= database
= trace_buffer_lo
;
3157 blocktype
= *dataptr
++;
3159 if (type_wanted
== blocktype
)
3165 /* Skip over the registers block. */
3166 dataptr
+= register_cache_size ();
3169 /* Skip over the memory block. */
3170 dataptr
+= sizeof (CORE_ADDR
);
3171 memcpy (&mlen
, dataptr
, sizeof (mlen
));
3172 dataptr
+= (sizeof (mlen
) + mlen
);
3175 /* Skip over the static trace data block. */
3176 memcpy (&mlen
, dataptr
, sizeof (mlen
));
3177 dataptr
+= (sizeof (mlen
) + mlen
);
3180 /* Skip over the TSV block. */
3181 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
3184 trace_debug ("traceframe %d has unknown block type 0x%x",
3193 static unsigned char *
3194 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
3196 unsigned char *regblock
;
3198 regblock
= traceframe_find_block_type (tframe
->data
,
3202 if (regblock
== NULL
)
3203 trace_debug ("traceframe %d has no register data", tfnum
);
3208 /* Get registers from a traceframe. */
3211 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
3213 unsigned char *dataptr
;
3214 struct tracepoint
*tpoint
;
3215 struct traceframe
*tframe
;
3217 tframe
= find_traceframe (tfnum
);
3221 trace_debug ("traceframe %d not found", tfnum
);
3225 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
3226 if (dataptr
== NULL
)
3228 /* We don't like making up numbers, but GDB has all manner of
3229 troubles when the target says there are no registers. */
3230 supply_regblock (regcache
, NULL
);
3232 /* We can generally guess at a PC, although this will be
3233 misleading for while-stepping frames and multi-location
3235 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
3237 regcache_write_pc (regcache
, tpoint
->address
);
3240 supply_regblock (regcache
, dataptr
);
3246 traceframe_get_pc (struct traceframe
*tframe
)
3248 struct regcache regcache
;
3249 unsigned char *dataptr
;
3251 dataptr
= traceframe_find_regblock (tframe
, -1);
3252 if (dataptr
== NULL
)
3255 init_register_cache (®cache
, dataptr
);
3256 return regcache_read_pc (®cache
);
3259 /* Read a requested block of memory from a trace frame. */
3262 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
3263 unsigned char *buf
, ULONGEST length
,
3266 struct traceframe
*tframe
;
3267 unsigned char *database
, *dataptr
;
3268 unsigned int datasize
;
3270 unsigned short mlen
;
3272 trace_debug ("traceframe_read_mem");
3274 tframe
= find_traceframe (tfnum
);
3278 trace_debug ("traceframe %d not found", tfnum
);
3282 datasize
= tframe
->data_size
;
3283 database
= dataptr
= &tframe
->data
[0];
3285 /* Iterate through a traceframe's blocks, looking for memory. */
3286 while ((dataptr
= traceframe_find_block_type (dataptr
,
3287 datasize
- (dataptr
- database
),
3288 tfnum
, 'M')) != NULL
)
3290 memcpy (&maddr
, dataptr
, sizeof (maddr
));
3291 dataptr
+= sizeof (maddr
);
3292 memcpy (&mlen
, dataptr
, sizeof (mlen
));
3293 dataptr
+= sizeof (mlen
);
3294 trace_debug ("traceframe %d has %d bytes at %s",
3295 tfnum
, mlen
, paddress (maddr
));
3297 /* Check that requested data is in bounds. */
3298 if (maddr
<= addr
&& (addr
+ length
) <= (maddr
+ mlen
))
3300 /* Block includes the requested range, copy it out. */
3301 memcpy (buf
, dataptr
+ (addr
- maddr
), length
);
3306 /* Skip over this block. */
3310 trace_debug ("traceframe %d has no memory data for the desired region",
3318 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
3321 struct traceframe
*tframe
;
3322 unsigned char *database
, *dataptr
;
3323 unsigned int datasize
;
3326 trace_debug ("traceframe_read_tsv");
3328 tfnum
= current_traceframe
;
3332 trace_debug ("no current traceframe");
3336 tframe
= find_traceframe (tfnum
);
3340 trace_debug ("traceframe %d not found", tfnum
);
3344 datasize
= tframe
->data_size
;
3345 database
= dataptr
= &tframe
->data
[0];
3347 /* Iterate through a traceframe's blocks, looking for the tsv. */
3348 while ((dataptr
= traceframe_find_block_type (dataptr
,
3349 datasize
- (dataptr
- database
),
3350 tfnum
, 'V')) != NULL
)
3352 memcpy (&vnum
, dataptr
, sizeof (vnum
));
3353 dataptr
+= sizeof (vnum
);
3355 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
3357 /* Check that this is the variable we want. */
3360 memcpy (val
, dataptr
, sizeof (*val
));
3364 /* Skip over this block. */
3365 dataptr
+= sizeof (LONGEST
);
3368 trace_debug ("traceframe %d has no data for variable %d",
3374 tsv_get_timestamp (void)
3378 if (gettimeofday (&tv
, 0) != 0)
3381 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
3385 initialize_tracepoint (void)
3387 /* There currently no way to change the buffer size. */
3388 const int sizeOfBuffer
= 5 * 1024 * 1024;
3389 unsigned char *buf
= xmalloc (sizeOfBuffer
);
3390 init_trace_buffer (buf
, sizeOfBuffer
);
3392 /* Wire trace state variable 1 to be the timestamp. This will be
3393 uploaded to GDB upon connection and become one of its trace state
3394 variables. (In case you're wondering, if GDB already has a trace
3395 variable numbered 1, it will be renumbered.) */
3396 create_trace_state_variable (1);
3397 set_trace_state_variable_name (1, "trace_timestamp");
3398 set_trace_state_variable_getter (1, tsv_get_timestamp
);