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 /* Controls whether tracing should continue after GDB disconnects. */
565 int disconnected_tracing
;
567 /* The reason for the last tracing run to have stopped. We initialize
568 to a distinct string so that GDB can distinguish between "stopped
569 after running" and "stopped because never run" cases. */
571 static const char *tracing_stop_reason
= "tnotrun";
573 static int tracing_stop_tpnum
;
575 /* Functions local to this file. */
577 /* Base "class" for tracepoint type specific data to be passed down to
578 collect_data_at_tracepoint. */
579 struct tracepoint_hit_ctx
584 /* Trap tracepoint specific data to be passed down to
585 collect_data_at_tracepoint. */
587 struct trap_tracepoint_ctx
589 struct tracepoint_hit_ctx base
;
591 struct regcache
*regcache
;
594 static struct agent_expr
*parse_agent_expr (char **actparm
);
595 static char *unparse_agent_expr (struct agent_expr
*aexpr
);
596 static enum eval_result_type
eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
597 struct traceframe
*tframe
,
598 struct agent_expr
*aexpr
,
601 static int agent_mem_read (struct traceframe
*tframe
,
602 unsigned char *to
, CORE_ADDR from
, ULONGEST len
);
603 static int agent_tsv_read (struct traceframe
*tframe
, int n
);
605 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
606 static int traceframe_read_tsv (int num
, LONGEST
*val
);
608 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
609 struct tracepoint
*tpoint
);
611 static void clear_readonly_regions (void);
612 static void clear_installed_tracepoints (void);
614 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
616 struct tracepoint
*tpoint
);
618 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
620 struct tracepoint
*tpoint
, int current_step
);
622 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
624 struct tracepoint
*tpoint
,
625 struct traceframe
*tframe
,
626 struct tracepoint_action
*taction
);
628 /* Record that an error occurred during expression evaluation. */
631 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
632 enum eval_result_type rtype
)
634 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
635 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
637 expr_eval_result
= rtype
;
638 error_tracepoint
= tpoint
;
641 /* Trace buffer management. */
644 clear_trace_buffer (void)
646 trace_buffer_start
= trace_buffer_lo
;
647 trace_buffer_free
= trace_buffer_lo
;
648 trace_buffer_end_free
= trace_buffer_hi
;
649 trace_buffer_wrap
= trace_buffer_hi
;
650 /* A traceframe with zeroed fields marks the end of trace data. */
651 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
652 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
653 traceframe_read_count
= traceframe_write_count
= 0;
654 traceframes_created
= 0;
658 init_trace_buffer (unsigned char *buf
, int bufsize
)
660 trace_buffer_lo
= buf
;
661 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
663 clear_trace_buffer ();
666 /* Carve out a piece of the trace buffer, returning NULL in case of
670 trace_buffer_alloc (size_t amt
)
673 struct traceframe
*oldest
;
674 unsigned char *new_start
;
676 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
677 (long) amt
, (long) sizeof (struct traceframe
));
679 /* Account for the EOB marker. */
680 amt
+= sizeof (struct traceframe
);
682 /* Offsets are easier to grok for debugging than raw addresses,
683 especially for the small trace buffer sizes that are useful for
685 trace_debug ("Trace buffer start=%d free=%d endfree=%d wrap=%d hi=%d",
686 (int) (trace_buffer_start
- trace_buffer_lo
),
687 (int) (trace_buffer_free
- trace_buffer_lo
),
688 (int) (trace_buffer_end_free
- trace_buffer_lo
),
689 (int) (trace_buffer_wrap
- trace_buffer_lo
),
690 (int) (trace_buffer_hi
- trace_buffer_lo
));
692 /* The algorithm here is to keep trying to get a contiguous block of
693 the requested size, possibly discarding older traceframes to free
694 up space. Since free space might come in one or two pieces,
695 depending on whether discarded traceframes wrapped around at the
696 high end of the buffer, we test both pieces after each
700 /* First, if we have two free parts, try the upper one first. */
701 if (trace_buffer_end_free
< trace_buffer_free
)
703 if (trace_buffer_free
+ amt
<= trace_buffer_hi
)
704 /* We have enough in the upper part. */
708 /* Our high part of free space wasn't enough. Give up
709 on it for now, set wraparound. We will recover the
710 space later, if/when the wrapped-around traceframe is
712 trace_debug ("Upper part too small, setting wraparound");
713 trace_buffer_wrap
= trace_buffer_free
;
714 trace_buffer_free
= trace_buffer_lo
;
718 /* The normal case. */
719 if (trace_buffer_free
+ amt
<= trace_buffer_end_free
)
722 /* If we're here, then neither part is big enough, and
723 non-circular trace buffers are now full. */
724 if (!circular_trace_buffer
)
726 trace_debug ("Not enough space in the trace buffer");
730 trace_debug ("Need more space in the trace buffer");
732 /* If we have a circular buffer, we can try discarding the
733 oldest traceframe and see if that helps. */
734 oldest
= FIRST_TRACEFRAME ();
735 if (oldest
->tpnum
== 0)
737 /* Not good; we have no traceframes to free. Perhaps we're
738 asking for a block that is larger than the buffer? In
739 any case, give up. */
740 trace_debug ("No traceframes to discard");
744 --traceframe_write_count
;
746 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
747 /* If we freed the traceframe that wrapped around, go back
748 to the non-wrap case. */
749 if (new_start
< trace_buffer_start
)
751 trace_debug ("Discarding past the wraparound");
752 trace_buffer_wrap
= trace_buffer_hi
;
754 trace_buffer_start
= new_start
;
755 trace_buffer_end_free
= trace_buffer_start
;
757 trace_debug ("Discarded a traceframe\n"
758 "Trace buffer, start=%d free=%d endfree=%d wrap=%d hi=%d",
759 (int) (trace_buffer_start
- trace_buffer_lo
),
760 (int) (trace_buffer_free
- trace_buffer_lo
),
761 (int) (trace_buffer_end_free
- trace_buffer_lo
),
762 (int) (trace_buffer_wrap
- trace_buffer_lo
),
763 (int) (trace_buffer_hi
- trace_buffer_lo
));
765 /* Now go back around the loop. The discard might have resulted
766 in either one or two pieces of free space, so we want to try
767 both before freeing any more traceframes. */
770 /* If we get here, we know we can provide the asked-for space. */
772 rslt
= trace_buffer_free
;
774 /* Adjust the request back down, now that we know we have space for
776 trace_buffer_free
+= (amt
- sizeof (struct traceframe
));
778 /* We have a new piece of the trace buffer. Hurray! */
780 /* Add an EOB marker just past this allocation. */
781 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
782 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
784 /* Adjust the request back down, now that we know we have space for
786 amt
-= sizeof (struct traceframe
);
790 trace_debug ("Allocated %d bytes", (int) amt
);
791 trace_debug ("Trace buffer start=%d free=%d endfree=%d wrap=%d hi=%d",
792 (int) (trace_buffer_start
- trace_buffer_lo
),
793 (int) (trace_buffer_free
- trace_buffer_lo
),
794 (int) (trace_buffer_end_free
- trace_buffer_lo
),
795 (int) (trace_buffer_wrap
- trace_buffer_lo
),
796 (int) (trace_buffer_hi
- trace_buffer_lo
));
802 /* Return the total free space. This is not necessarily the largest
803 block we can allocate, because of the two-part case. */
808 if (trace_buffer_free
<= trace_buffer_end_free
)
809 return trace_buffer_end_free
- trace_buffer_free
;
811 return ((trace_buffer_end_free
- trace_buffer_lo
)
812 + (trace_buffer_hi
- trace_buffer_free
));
815 /* An 'S' in continuation packets indicates remainder are for
818 static int seen_step_action_flag
;
820 /* Create a tracepoint (location) with given number and address. */
822 static struct tracepoint
*
823 add_tracepoint (int num
, CORE_ADDR addr
)
825 struct tracepoint
*tpoint
;
827 tpoint
= xmalloc (sizeof (struct tracepoint
));
828 tpoint
->number
= num
;
829 tpoint
->address
= addr
;
830 tpoint
->numactions
= 0;
831 tpoint
->actions
= NULL
;
832 tpoint
->actions_str
= NULL
;
834 tpoint
->num_step_actions
= 0;
835 tpoint
->step_actions
= NULL
;
836 tpoint
->step_actions_str
= NULL
;
837 tpoint
->source_strings
= NULL
;
838 tpoint
->handle
= NULL
;
841 if (!last_tracepoint
)
842 tracepoints
= tpoint
;
844 last_tracepoint
->next
= tpoint
;
845 last_tracepoint
= tpoint
;
847 seen_step_action_flag
= 0;
852 /* Return the tracepoint with the given number and address, or NULL. */
854 static struct tracepoint
*
855 find_tracepoint (int id
, CORE_ADDR addr
)
857 struct tracepoint
*tpoint
;
859 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
860 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
866 /* There may be several tracepoints with the same number (because they
867 are "locations", in GDB parlance); return the next one after the
868 given tracepoint, or search from the beginning of the list if the
869 first argument is NULL. */
871 static struct tracepoint
*
872 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
874 struct tracepoint
*tpoint
;
877 tpoint
= prev_tp
->next
;
879 tpoint
= tracepoints
;
880 for (; tpoint
; tpoint
= tpoint
->next
)
881 if (tpoint
->number
== num
)
888 save_string (const char *str
, size_t len
)
892 s
= xmalloc (len
+ 1);
893 memcpy (s
, str
, len
);
899 /* Append another action to perform when the tracepoint triggers. */
902 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
908 seen_step_action_flag
= 1;
916 char *act_start
= act
;
917 struct tracepoint_action
*action
= NULL
;
923 struct collect_memory_action
*maction
;
927 maction
= xmalloc (sizeof *maction
);
928 maction
->base
.type
= *act
;
929 action
= &maction
->base
;
932 is_neg
= (*act
== '-');
935 act
= unpack_varlen_hex (act
, &basereg
);
937 act
= unpack_varlen_hex (act
, &maction
->addr
);
939 act
= unpack_varlen_hex (act
, &maction
->len
);
940 maction
->basereg
= (is_neg
943 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
944 pulongest (maction
->len
),
945 paddress (maction
->addr
), maction
->basereg
);
950 struct collect_registers_action
*raction
;
952 raction
= xmalloc (sizeof *raction
);
953 raction
->base
.type
= *act
;
954 action
= &raction
->base
;
956 trace_debug ("Want to collect registers");
958 /* skip past hex digits of mask for now */
959 while (isxdigit(*act
))
964 trace_debug ("Unexpected step action, ignoring");
969 struct eval_expr_action
*xaction
;
971 xaction
= xmalloc (sizeof (*xaction
));
972 xaction
->base
.type
= *act
;
973 action
= &xaction
->base
;
975 trace_debug ("Want to evaluate expression");
976 xaction
->expr
= parse_agent_expr (&act
);
980 trace_debug ("unknown trace action '%c', ignoring...", *act
);
989 if (seen_step_action_flag
)
991 tpoint
->num_step_actions
++;
994 = xrealloc (tpoint
->step_actions
,
995 (sizeof (*tpoint
->step_actions
)
996 * tpoint
->num_step_actions
));
997 tpoint
->step_actions_str
998 = xrealloc (tpoint
->step_actions_str
,
999 (sizeof (*tpoint
->step_actions_str
)
1000 * tpoint
->num_step_actions
));
1001 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1002 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1003 = save_string (act_start
, act
- act_start
);
1007 tpoint
->numactions
++;
1009 = xrealloc (tpoint
->actions
,
1010 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1012 = xrealloc (tpoint
->actions_str
,
1013 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1014 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1015 tpoint
->actions_str
[tpoint
->numactions
- 1]
1016 = save_string (act_start
, act
- act_start
);
1021 /* Find or create a trace state variable with the given number. */
1023 static struct trace_state_variable
*
1024 get_trace_state_variable (int num
)
1026 struct trace_state_variable
*tsv
;
1028 /* Search for an existing variable. */
1029 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1030 if (tsv
->number
== num
)
1036 /* Find or create a trace state variable with the given number. */
1038 static struct trace_state_variable
*
1039 create_trace_state_variable (int num
)
1041 struct trace_state_variable
*tsv
;
1043 tsv
= get_trace_state_variable (num
);
1047 /* Create a new variable. */
1048 tsv
= xmalloc (sizeof (struct trace_state_variable
));
1050 tsv
->initial_value
= 0;
1054 tsv
->next
= trace_state_variables
;
1055 trace_state_variables
= tsv
;
1061 get_trace_state_variable_value (int num
)
1063 struct trace_state_variable
*tsv
;
1065 tsv
= get_trace_state_variable (num
);
1069 trace_debug ("No trace state variable %d, skipping value get", num
);
1073 /* Call a getter function if we have one. While it's tempting to
1074 set up something to only call the getter once per tracepoint hit,
1075 it could run afoul of thread races. Better to let the getter
1076 handle it directly, if necessary to worry about it. */
1078 tsv
->value
= (tsv
->getter
) ();
1080 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1081 num
, plongest (tsv
->value
));
1087 set_trace_state_variable_value (int num
, LONGEST val
)
1089 struct trace_state_variable
*tsv
;
1091 tsv
= get_trace_state_variable (num
);
1095 trace_debug ("No trace state variable %d, skipping value set", num
);
1103 set_trace_state_variable_name (int num
, const char *name
)
1105 struct trace_state_variable
*tsv
;
1107 tsv
= get_trace_state_variable (num
);
1111 trace_debug ("No trace state variable %d, skipping name set", num
);
1115 tsv
->name
= (char *) name
;
1119 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
1121 struct trace_state_variable
*tsv
;
1123 tsv
= get_trace_state_variable (num
);
1127 trace_debug ("No trace state variable %d, skipping getter set", num
);
1131 tsv
->getter
= getter
;
1134 /* Add a raw traceframe for the given tracepoint. */
1136 static struct traceframe
*
1137 add_traceframe (struct tracepoint
*tpoint
)
1139 struct traceframe
*tframe
;
1141 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
1146 tframe
->tpnum
= tpoint
->number
;
1147 tframe
->data_size
= 0;
1152 /* Add a block to the traceframe currently being worked on. */
1154 static unsigned char *
1155 add_traceframe_block (struct traceframe
*tframe
, int amt
)
1157 unsigned char *block
;
1162 block
= trace_buffer_alloc (amt
);
1167 tframe
->data_size
+= amt
;
1172 /* Flag that the current traceframe is finished. */
1175 finish_traceframe (struct traceframe
*tframe
)
1177 ++traceframe_write_count
;
1178 ++traceframes_created
;
1181 /* Given a traceframe number NUM, find the NUMth traceframe in the
1184 static struct traceframe
*
1185 find_traceframe (int num
)
1187 struct traceframe
*tframe
;
1190 for (tframe
= FIRST_TRACEFRAME ();
1192 tframe
= NEXT_TRACEFRAME (tframe
))
1203 get_traceframe_address (struct traceframe
*tframe
)
1206 struct tracepoint
*tpoint
;
1208 addr
= traceframe_get_pc (tframe
);
1213 /* Fallback strategy, will be incorrect for while-stepping frames
1214 and multi-location tracepoints. */
1215 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
1216 return tpoint
->address
;
1219 /* Search for the next traceframe whose address is inside or outside
1222 static struct traceframe
*
1223 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
1226 struct traceframe
*tframe
;
1229 *tfnump
= current_traceframe
+ 1;
1230 tframe
= find_traceframe (*tfnump
);
1231 /* The search is not supposed to wrap around. */
1238 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
1240 tfaddr
= get_traceframe_address (tframe
);
1242 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
1243 : (lo
> tfaddr
|| tfaddr
> hi
))
1252 /* Search for the next traceframe recorded by the given tracepoint.
1253 Note that for multi-location tracepoints, this will find whatever
1254 location appears first. */
1256 static struct traceframe
*
1257 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
1259 struct traceframe
*tframe
;
1261 *tfnump
= current_traceframe
+ 1;
1262 tframe
= find_traceframe (*tfnump
);
1263 /* The search is not supposed to wrap around. */
1270 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
1272 if (tframe
->tpnum
== num
)
1281 /* Clear all past trace state. */
1284 cmd_qtinit (char *packet
)
1286 struct trace_state_variable
*tsv
, *prev
, *next
;
1288 /* Make sure we don't try to read from a trace frame. */
1289 current_traceframe
= -1;
1291 trace_debug ("Initializing the trace");
1293 clear_installed_tracepoints ();
1294 clear_readonly_regions ();
1297 last_tracepoint
= NULL
;
1299 /* Clear out any leftover trace state variables. Ones with target
1300 defined getters should be kept however. */
1302 tsv
= trace_state_variables
;
1305 trace_debug ("Looking at var %d", tsv
->number
);
1306 if (tsv
->getter
== NULL
)
1312 trace_state_variables
= next
;
1313 trace_debug ("Deleting var %d", tsv
->number
);
1324 clear_trace_buffer ();
1329 /* Restore the program to its pre-tracing state. This routine may be called
1330 in error situations, so it needs to be careful about only restoring
1331 from known-valid bits. */
1334 clear_installed_tracepoints (void)
1336 struct tracepoint
*tpoint
;
1337 struct tracepoint
*prev_stpoint
;
1339 prev_stpoint
= NULL
;
1341 /* Restore any bytes overwritten by tracepoints. */
1342 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1344 if (!tpoint
->enabled
)
1347 /* Catch the case where we might try to remove a tracepoint that
1348 was never actually installed. */
1349 if (tpoint
->handle
== NULL
)
1351 trace_debug ("Tracepoint %d at 0x%s was "
1352 "never installed, nothing to clear",
1353 tpoint
->number
, paddress (tpoint
->address
));
1357 delete_breakpoint (tpoint
->handle
);
1358 tpoint
->handle
= NULL
;
1362 /* Parse a packet that defines a tracepoint. */
1365 cmd_qtdp (char *own_buf
)
1371 struct tracepoint
*tpoint
;
1373 char *packet
= own_buf
;
1375 packet
+= strlen ("QTDP:");
1377 /* A hyphen at the beginning marks a packet specifying actions for a
1378 tracepoint already supplied. */
1385 packet
= unpack_varlen_hex (packet
, &num
);
1386 ++packet
; /* skip a colon */
1387 packet
= unpack_varlen_hex (packet
, &addr
);
1388 ++packet
; /* skip a colon */
1390 /* See if we already have this tracepoint. */
1391 tpoint
= find_tracepoint (num
, addr
);
1395 /* Duplicate tracepoints are never allowed. */
1398 trace_debug ("Tracepoint error: tracepoint %d"
1399 " at 0x%s already exists",
1400 (int) num
, paddress (addr
));
1401 write_enn (own_buf
);
1405 tpoint
= add_tracepoint (num
, addr
);
1407 tpoint
->enabled
= (*packet
== 'E');
1408 ++packet
; /* skip 'E' */
1409 ++packet
; /* skip a colon */
1410 packet
= unpack_varlen_hex (packet
, &count
);
1411 tpoint
->step_count
= count
;
1412 ++packet
; /* skip a colon */
1413 packet
= unpack_varlen_hex (packet
, &count
);
1414 tpoint
->pass_count
= count
;
1415 /* See if we have any of the additional optional fields. */
1416 while (*packet
== ':')
1421 actparm
= (char *) packet
;
1422 tpoint
->cond
= parse_agent_expr (&actparm
);
1425 else if (*packet
== '-')
1427 else if (*packet
== '\0')
1430 trace_debug ("Unknown optional tracepoint field");
1433 trace_debug ("Also has actions\n");
1435 trace_debug ("Defined tracepoint %d at 0x%s, "
1436 "enabled %d step %ld pass %ld",
1437 tpoint
->number
, paddress (tpoint
->address
),
1439 tpoint
->step_count
, tpoint
->pass_count
);
1442 add_tracepoint_action (tpoint
, packet
);
1445 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
1446 (int) num
, paddress (addr
));
1447 write_enn (own_buf
);
1455 cmd_qtdpsrc (char *own_buf
)
1457 ULONGEST num
, addr
, start
, slen
;
1458 struct tracepoint
*tpoint
;
1459 char *packet
= own_buf
;
1460 char *saved
, *srctype
, *src
;
1462 struct source_string
*last
, *newlast
;
1464 packet
+= strlen ("QTDPsrc:");
1466 packet
= unpack_varlen_hex (packet
, &num
);
1467 ++packet
; /* skip a colon */
1468 packet
= unpack_varlen_hex (packet
, &addr
);
1469 ++packet
; /* skip a colon */
1471 /* See if we already have this tracepoint. */
1472 tpoint
= find_tracepoint (num
, addr
);
1476 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
1477 (int) num
, paddress (addr
));
1478 write_enn (own_buf
);
1483 packet
= strchr (packet
, ':');
1484 srctype
= xmalloc (packet
- saved
+ 1);
1485 memcpy (srctype
, saved
, packet
- saved
);
1486 srctype
[packet
- saved
] = '\0';
1488 packet
= unpack_varlen_hex (packet
, &start
);
1489 ++packet
; /* skip a colon */
1490 packet
= unpack_varlen_hex (packet
, &slen
);
1491 ++packet
; /* skip a colon */
1492 src
= xmalloc (slen
+ 1);
1493 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
1496 newlast
= xmalloc (sizeof (struct source_string
));
1497 newlast
->type
= srctype
;
1499 newlast
->next
= NULL
;
1500 /* Always add a source string to the end of the list;
1501 this keeps sequences of actions/commands in the right
1503 if (tpoint
->source_strings
)
1505 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
1507 last
->next
= newlast
;
1510 tpoint
->source_strings
= newlast
;
1516 cmd_qtdv (char *own_buf
)
1518 ULONGEST num
, val
, builtin
;
1521 struct trace_state_variable
*tsv
;
1522 char *packet
= own_buf
;
1524 packet
+= strlen ("QTDV:");
1526 packet
= unpack_varlen_hex (packet
, &num
);
1527 ++packet
; /* skip a colon */
1528 packet
= unpack_varlen_hex (packet
, &val
);
1529 ++packet
; /* skip a colon */
1530 packet
= unpack_varlen_hex (packet
, &builtin
);
1531 ++packet
; /* skip a colon */
1533 nbytes
= strlen (packet
) / 2;
1534 varname
= xmalloc (nbytes
+ 1);
1535 nbytes
= unhexify (varname
, packet
, nbytes
);
1536 varname
[nbytes
] = '\0';
1538 tsv
= create_trace_state_variable (num
);
1539 tsv
->initial_value
= (LONGEST
) val
;
1540 tsv
->name
= varname
;
1542 set_trace_state_variable_value (num
, (LONGEST
) val
);
1548 cmd_qtv (char *own_buf
)
1553 char *packet
= own_buf
;
1555 packet
+= strlen ("qTV:");
1556 packet
= unpack_varlen_hex (packet
, &num
);
1558 if (current_traceframe
>= 0)
1560 err
= traceframe_read_tsv ((int) num
, &val
);
1563 strcpy (own_buf
, "U");
1567 /* Only make tsv's be undefined before the first trace run. After a
1568 trace run is over, the user might want to see the last value of
1569 the tsv, and it might not be available in a traceframe. */
1570 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
1572 strcpy (own_buf
, "U");
1576 val
= get_trace_state_variable_value (num
);
1578 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
1581 /* Clear out the list of readonly regions. */
1584 clear_readonly_regions (void)
1586 struct readonly_region
*roreg
;
1588 while (readonly_regions
)
1590 roreg
= readonly_regions
;
1591 readonly_regions
= readonly_regions
->next
;
1596 /* Parse the collection of address ranges whose contents GDB believes
1597 to be unchanging and so can be read directly from target memory
1598 even while looking at a traceframe. */
1601 cmd_qtro (char *own_buf
)
1603 ULONGEST start
, end
;
1604 struct readonly_region
*roreg
;
1605 char *packet
= own_buf
;
1607 trace_debug ("Want to mark readonly regions");
1609 clear_readonly_regions ();
1611 packet
+= strlen ("QTro");
1613 while (*packet
== ':')
1615 ++packet
; /* skip a colon */
1616 packet
= unpack_varlen_hex (packet
, &start
);
1617 ++packet
; /* skip a comma */
1618 packet
= unpack_varlen_hex (packet
, &end
);
1619 roreg
= xmalloc (sizeof (struct readonly_region
));
1620 roreg
->start
= start
;
1622 roreg
->next
= readonly_regions
;
1623 readonly_regions
= roreg
;
1624 trace_debug ("Added readonly region from 0x%s to 0x%s",
1625 paddress (roreg
->start
), paddress (roreg
->end
));
1631 /* Test to see if the given range is in our list of readonly ranges.
1632 We only test for being entirely within a range, GDB is not going to
1633 send a single memory packet that spans multiple regions. */
1636 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
1638 struct readonly_region
*roreg
;
1640 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
1641 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
1648 cmd_qtstart (char *packet
)
1650 struct tracepoint
*tpoint
;
1651 int slow_tracepoint_count
;
1653 trace_debug ("Starting the trace");
1655 slow_tracepoint_count
= 0;
1659 /* Install tracepoints. */
1660 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1662 /* Ensure all the hit counts start at zero. */
1663 tpoint
->hit_count
= 0;
1665 if (!tpoint
->enabled
)
1668 ++slow_tracepoint_count
;
1670 /* Tracepoints are installed as memory breakpoints. Just go
1671 ahead and install the trap. The breakpoints module handles
1672 duplicated breakpoints, and the memory read routine handles
1673 un-patching traps from memory reads. */
1674 tpoint
->handle
= set_breakpoint_at (tpoint
->address
, tracepoint_handler
);
1676 /* Any failure is sufficient cause to give up. */
1677 if (tpoint
->handle
== NULL
)
1681 /* Any error in tracepoint insertion is unacceptable; better to
1682 address the problem now, than end up with a useless or misleading
1686 clear_installed_tracepoints ();
1687 if (*packet
== '\0')
1692 stopping_tracepoint
= NULL
;
1693 trace_buffer_is_full
= 0;
1694 expr_eval_result
= expr_eval_no_error
;
1695 error_tracepoint
= NULL
;
1697 /* Tracing is now active, hits will now start being logged. */
1703 /* End a tracing run, filling in a stop reason to report back to GDB,
1704 and removing the tracepoints from the code. */
1711 trace_debug ("Tracing is already off, ignoring");
1715 trace_debug ("Stopping the trace");
1717 /* Stop logging. Tracepoints can still be hit, but they will not be
1721 tracing_stop_reason
= "t???";
1722 tracing_stop_tpnum
= 0;
1723 if (stopping_tracepoint
)
1725 trace_debug ("Stopping the trace because "
1726 "tracepoint %d was hit %ld times",
1727 stopping_tracepoint
->number
,
1728 stopping_tracepoint
->pass_count
);
1729 tracing_stop_reason
= "tpasscount";
1730 tracing_stop_tpnum
= stopping_tracepoint
->number
;
1732 else if (trace_buffer_is_full
)
1734 trace_debug ("Stopping the trace because the trace buffer is full");
1735 tracing_stop_reason
= "tfull";
1737 else if (expr_eval_result
!= expr_eval_no_error
)
1739 trace_debug ("Stopping the trace because of an expression eval error");
1740 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
1741 tracing_stop_tpnum
= error_tracepoint
->number
;
1743 else if (!gdb_connected ())
1745 trace_debug ("Stopping the trace because GDB disconnected");
1746 tracing_stop_reason
= "tdisconnected";
1750 trace_debug ("Stopping the trace because of a tstop command");
1751 tracing_stop_reason
= "tstop";
1754 stopping_tracepoint
= NULL
;
1755 error_tracepoint
= NULL
;
1757 /* Clear out the tracepoints. */
1758 clear_installed_tracepoints ();
1762 cmd_qtstop (char *packet
)
1769 cmd_qtdisconnected (char *own_buf
)
1772 char *packet
= own_buf
;
1774 packet
+= strlen ("QTDisconnected:");
1776 unpack_varlen_hex (packet
, &setting
);
1780 disconnected_tracing
= setting
;
1784 cmd_qtframe (char *own_buf
)
1786 ULONGEST frame
, pc
, lo
, hi
, num
;
1788 struct traceframe
*tframe
;
1789 char *packet
= own_buf
;
1791 packet
+= strlen ("QTFrame:");
1793 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
1795 packet
+= strlen ("pc:");
1796 packet
= unpack_varlen_hex (packet
, &pc
);
1797 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
1798 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
1800 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
1802 packet
+= strlen ("range:");
1803 packet
= unpack_varlen_hex (packet
, &lo
);
1805 packet
= unpack_varlen_hex (packet
, &hi
);
1806 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
1807 paddress (lo
), paddress (hi
));
1808 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
1810 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
1812 packet
+= strlen ("outside:");
1813 packet
= unpack_varlen_hex (packet
, &lo
);
1815 packet
= unpack_varlen_hex (packet
, &hi
);
1816 trace_debug ("Want to find next traceframe "
1817 "outside the range 0x%s to 0x%s",
1818 paddress (lo
), paddress (hi
));
1819 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
1821 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
1823 packet
+= strlen ("tdp:");
1824 packet
= unpack_varlen_hex (packet
, &num
);
1826 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
1827 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
1831 unpack_varlen_hex (packet
, &frame
);
1832 tfnum
= (int) frame
;
1835 trace_debug ("Want to stop looking at traceframes");
1836 current_traceframe
= -1;
1840 trace_debug ("Want to look at traceframe %d", tfnum
);
1841 tframe
= find_traceframe (tfnum
);
1846 current_traceframe
= tfnum
;
1847 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
1850 sprintf (own_buf
, "F-1");
1854 cmd_qtstatus (char *packet
)
1856 char *stop_reason_rsp
= NULL
;
1858 trace_debug ("Returning trace status as %d, stop reason %s",
1859 tracing
, tracing_stop_reason
);
1861 stop_reason_rsp
= (char *) tracing_stop_reason
;
1863 /* The user visible error string in terror needs to be hex encoded.
1864 We leave it as plain string in `tracepoint_stop_reason' to ease
1866 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
1868 const char *result_name
;
1872 result_name
= stop_reason_rsp
+ strlen ("terror:");
1873 hexstr_len
= strlen (result_name
) * 2;
1874 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
1875 strcpy (p
, "terror:");
1877 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
1883 "tframes:%x;tcreated:%x;"
1884 "tfree:%x;tsize:%s;"
1888 stop_reason_rsp
, tracing_stop_tpnum
,
1889 traceframe_count
, traceframes_created
,
1890 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
1891 circular_trace_buffer
,
1892 disconnected_tracing
);
1895 /* State variables to help return all the tracepoint bits. */
1896 static struct tracepoint
*cur_tpoint
;
1897 static int cur_action
;
1898 static int cur_step_action
;
1899 static struct source_string
*cur_source_string
;
1900 static struct trace_state_variable
*cur_tsv
;
1902 /* Compose a response that is an imitation of the syntax by which the
1903 tracepoint was originally downloaded. */
1906 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
1910 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
1911 paddress (tpoint
->address
),
1912 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
1913 tpoint
->pass_count
);
1917 buf
= unparse_agent_expr (tpoint
->cond
);
1918 sprintf (packet
+ strlen (packet
), ":X%x,%s",
1919 tpoint
->cond
->length
, buf
);
1924 /* Compose a response that is an imitation of the syntax by which the
1925 tracepoint action was originally downloaded (with the difference
1926 that due to the way we store the actions, this will output a packet
1927 per action, while GDB could have combined more than one action
1931 response_action (char *packet
, struct tracepoint
*tpoint
,
1932 char *taction
, int step
)
1934 sprintf (packet
, "%c%x:%s:%s",
1935 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
1939 /* Compose a response that is an imitation of the syntax by which the
1940 tracepoint source piece was originally downloaded. */
1943 response_source (char *packet
,
1944 struct tracepoint
*tpoint
, struct source_string
*src
)
1949 len
= strlen (src
->str
);
1950 buf
= alloca (len
* 2 + 1);
1951 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
1953 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
1954 tpoint
->number
, paddress (tpoint
->address
),
1955 src
->type
, 0, len
, buf
);
1958 /* Return the first piece of tracepoint definition, and initialize the
1959 state machine that will iterate through all the tracepoint
1963 cmd_qtfp (char *packet
)
1965 trace_debug ("Returning first tracepoint definition piece");
1967 cur_tpoint
= tracepoints
;
1968 cur_action
= cur_step_action
= -1;
1969 cur_source_string
= NULL
;
1972 response_tracepoint (packet
, cur_tpoint
);
1974 strcpy (packet
, "l");
1977 /* Return additional pieces of tracepoint definition. Each action and
1978 stepping action must go into its own packet, because of packet size
1979 limits, and so we use state variables to deliver one piece at a
1983 cmd_qtsp (char *packet
)
1985 trace_debug ("Returning subsequent tracepoint definition piece");
1989 /* This case would normally never occur, but be prepared for
1991 strcpy (packet
, "l");
1993 else if (cur_action
< cur_tpoint
->numactions
- 1)
1996 response_action (packet
, cur_tpoint
,
1997 cur_tpoint
->actions_str
[cur_action
], 0);
1999 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
2002 response_action (packet
, cur_tpoint
,
2003 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
2005 else if ((cur_source_string
2006 ? cur_source_string
->next
2007 : cur_tpoint
->source_strings
))
2009 if (cur_source_string
)
2010 cur_source_string
= cur_source_string
->next
;
2012 cur_source_string
= cur_tpoint
->source_strings
;
2013 response_source (packet
, cur_tpoint
, cur_source_string
);
2017 cur_tpoint
= cur_tpoint
->next
;
2018 cur_action
= cur_step_action
= -1;
2019 cur_source_string
= NULL
;
2021 response_tracepoint (packet
, cur_tpoint
);
2023 strcpy (packet
, "l");
2027 /* Compose a response that is an imitation of the syntax by which the
2028 trace state variable was originally downloaded. */
2031 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
2033 char *buf
= (char *) "";
2038 namelen
= strlen (tsv
->name
);
2039 buf
= alloca (namelen
* 2 + 1);
2040 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
2043 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
2044 tsv
->getter
? 1 : 0, buf
);
2047 /* Return the first trace state variable definition, and initialize
2048 the state machine that will iterate through all the tsv bits. */
2051 cmd_qtfv (char *packet
)
2053 trace_debug ("Returning first trace state variable definition");
2055 cur_tsv
= trace_state_variables
;
2058 response_tsv (packet
, cur_tsv
);
2060 strcpy (packet
, "l");
2063 /* Return additional trace state variable definitions. */
2066 cmd_qtsv (char *packet
)
2068 trace_debug ("Returning first trace state variable definition");
2072 /* This case would normally never occur, but be prepared for
2074 strcpy (packet
, "l");
2078 cur_tsv
= cur_tsv
->next
;
2080 response_tsv (packet
, cur_tsv
);
2082 strcpy (packet
, "l");
2085 strcpy (packet
, "l");
2088 /* Respond to qTBuffer packet with a block of raw data from the trace
2089 buffer. GDB may ask for a lot, but we are allowed to reply with
2090 only as much as will fit within packet limits or whatever. */
2093 cmd_qtbuffer (char *own_buf
)
2095 ULONGEST offset
, num
, tot
;
2097 char *packet
= own_buf
;
2099 packet
+= strlen ("qTBuffer:");
2101 packet
= unpack_varlen_hex (packet
, &offset
);
2102 ++packet
; /* skip a comma */
2103 packet
= unpack_varlen_hex (packet
, &num
);
2105 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
2106 (int) num
, pulongest (offset
));
2108 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
2110 /* If we're right at the end, reply specially that we're done. */
2113 strcpy (own_buf
, "l");
2117 /* Object to any other out-of-bounds request. */
2120 write_enn (own_buf
);
2124 /* Compute the pointer corresponding to the given offset, accounting
2126 tbp
= trace_buffer_start
+ offset
;
2127 if (tbp
>= trace_buffer_wrap
)
2128 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
2130 /* Trim to the remaining bytes if we're close to the end. */
2131 if (num
> tot
- offset
)
2134 /* Trim to available packet size. */
2135 if (num
>= (PBUFSIZ
- 16) / 2 )
2136 num
= (PBUFSIZ
- 16) / 2;
2138 convert_int_to_ascii (tbp
, own_buf
, num
);
2139 own_buf
[num
] = '\0';
2143 cmd_bigqtbuffer (char *own_buf
)
2146 char *packet
= own_buf
;
2148 packet
+= strlen ("QTBuffer:");
2150 if (strncmp ("circular:", packet
, strlen ("circular:")) == 0)
2152 packet
+= strlen ("circular:");
2153 packet
= unpack_varlen_hex (packet
, &val
);
2154 circular_trace_buffer
= val
;
2155 trace_debug ("Trace buffer is now %s",
2156 circular_trace_buffer
? "circular" : "linear");
2160 write_enn (own_buf
);
2164 handle_tracepoint_general_set (char *packet
)
2166 if (strcmp ("QTinit", packet
) == 0)
2168 cmd_qtinit (packet
);
2171 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
2176 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
2178 cmd_qtdpsrc (packet
);
2181 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
2186 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
2191 else if (strcmp ("QTStart", packet
) == 0)
2193 cmd_qtstart (packet
);
2196 else if (strcmp ("QTStop", packet
) == 0)
2198 cmd_qtstop (packet
);
2201 else if (strncmp ("QTDisconnected:", packet
,
2202 strlen ("QTDisconnected:")) == 0)
2204 cmd_qtdisconnected (packet
);
2207 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
2209 cmd_qtframe (packet
);
2212 else if (strncmp ("QTBuffer:", packet
, strlen ("QTBuffer:")) == 0)
2214 cmd_bigqtbuffer (packet
);
2222 handle_tracepoint_query (char *packet
)
2224 if (strcmp ("qTStatus", packet
) == 0)
2226 cmd_qtstatus (packet
);
2229 else if (strcmp ("qTfP", packet
) == 0)
2234 else if (strcmp ("qTsP", packet
) == 0)
2239 else if (strcmp ("qTfV", packet
) == 0)
2244 else if (strcmp ("qTsV", packet
) == 0)
2249 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
2254 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
2256 cmd_qtbuffer (packet
);
2263 /* Call this when thread TINFO has hit the tracepoint defined by
2264 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
2265 action. This adds a while-stepping collecting state item to the
2266 threads' collecting state list, so that we can keep track of
2267 multiple simultaneous while-stepping actions being collected by the
2268 same thread. This can happen in cases like:
2270 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
2272 ff0003 INSN3 <-- TP2, collect $regs
2273 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
2276 Notice that when instruction INSN5 is reached, the while-stepping
2277 actions of both TP1 and TP3 are still being collected, and that TP2
2278 had been collected meanwhile. The whole range of ff0001-ff0005
2279 should be single-stepped, due to at least TP1's while-stepping
2280 action covering the whole range. */
2283 add_while_stepping_state (struct thread_info
*tinfo
,
2284 int tp_number
, CORE_ADDR tp_address
)
2286 struct wstep_state
*wstep
;
2288 wstep
= xmalloc (sizeof (*wstep
));
2289 wstep
->next
= tinfo
->while_stepping
;
2291 wstep
->tp_number
= tp_number
;
2292 wstep
->tp_address
= tp_address
;
2293 wstep
->current_step
= 0;
2295 tinfo
->while_stepping
= wstep
;
2298 /* Release the while-stepping collecting state WSTEP. */
2301 release_while_stepping_state (struct wstep_state
*wstep
)
2306 /* Release all while-stepping collecting states currently associated
2307 with thread TINFO. */
2310 release_while_stepping_state_list (struct thread_info
*tinfo
)
2312 struct wstep_state
*head
;
2314 while (tinfo
->while_stepping
)
2316 head
= tinfo
->while_stepping
;
2317 tinfo
->while_stepping
= head
->next
;
2318 release_while_stepping_state (head
);
2322 /* If TINFO was handling a 'while-stepping' action, the step has
2323 finished, so collect any step data needed, and check if any more
2324 steps are required. Return true if the thread was indeed
2325 collecting tracepoint data, false otherwise. */
2328 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
2330 struct tracepoint
*tpoint
;
2331 struct wstep_state
*wstep
;
2332 struct wstep_state
**wstep_link
;
2333 struct trap_tracepoint_ctx ctx
;
2335 /* Check if we were indeed collecting data for one of more
2336 tracepoints with a 'while-stepping' count. */
2337 if (tinfo
->while_stepping
== NULL
)
2342 /* We're not even tracing anymore. Stop this thread from
2344 release_while_stepping_state_list (tinfo
);
2346 /* The thread had stopped due to a single-step request indeed
2347 explained by a tracepoint. */
2351 wstep
= tinfo
->while_stepping
;
2352 wstep_link
= &tinfo
->while_stepping
;
2354 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
2355 target_pid_to_str (tinfo
->entry
.id
),
2356 wstep
->tp_number
, paddress (wstep
->tp_address
));
2358 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
2360 while (wstep
!= NULL
)
2362 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
2365 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
2366 wstep
->tp_number
, paddress (wstep
->tp_address
),
2367 target_pid_to_str (tinfo
->entry
.id
));
2370 *wstep_link
= wstep
->next
;
2371 release_while_stepping_state (wstep
);
2375 /* We've just finished one step. */
2376 ++wstep
->current_step
;
2379 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
2380 stop_pc
, tpoint
, wstep
->current_step
);
2382 if (wstep
->current_step
>= tpoint
->step_count
)
2384 /* The requested numbers of steps have occurred. */
2385 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
2386 target_pid_to_str (tinfo
->entry
.id
),
2387 wstep
->tp_number
, paddress (wstep
->tp_address
));
2389 /* Unlink the wstep. */
2390 *wstep_link
= wstep
->next
;
2391 release_while_stepping_state (wstep
);
2392 wstep
= *wstep_link
;
2394 /* Only check the hit count now, which ensure that we do all
2395 our stepping before stopping the run. */
2396 if (tpoint
->pass_count
> 0
2397 && tpoint
->hit_count
>= tpoint
->pass_count
2398 && stopping_tracepoint
== NULL
)
2399 stopping_tracepoint
= tpoint
;
2403 /* Keep single-stepping until the requested numbers of steps
2405 wstep_link
= &wstep
->next
;
2406 wstep
= *wstep_link
;
2409 if (stopping_tracepoint
2410 || trace_buffer_is_full
2411 || expr_eval_result
!= expr_eval_no_error
)
2421 /* Return true if TINFO just hit a tracepoint. Collect data if
2425 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
2427 struct tracepoint
*tpoint
;
2429 struct trap_tracepoint_ctx ctx
;
2431 /* Not tracing, don't handle. */
2435 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
2437 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2439 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
)
2441 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
2442 target_pid_to_str (tinfo
->entry
.id
),
2443 tpoint
->number
, paddress (tpoint
->address
));
2445 /* Test the condition if present, and collect if true. */
2447 || (condition_true_at_tracepoint
2448 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
2449 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
2452 if (stopping_tracepoint
2453 || trace_buffer_is_full
2454 || expr_eval_result
!= expr_eval_no_error
)
2458 /* If the tracepoint had a 'while-stepping' action, then set
2459 the thread to collect this tracepoint on the following
2461 else if (tpoint
->step_count
> 0)
2463 add_while_stepping_state (tinfo
,
2464 tpoint
->number
, tpoint
->address
);
2474 /* Create a trace frame for the hit of the given tracepoint in the
2478 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
2479 struct tracepoint
*tpoint
)
2481 struct traceframe
*tframe
;
2484 /* Only count it as a hit when we actually collect data. */
2485 tpoint
->hit_count
++;
2487 /* If we've exceeded a defined pass count, record the event for
2488 later, and finish the collection for this hit. This test is only
2489 for nonstepping tracepoints, stepping tracepoints test at the end
2490 of their while-stepping loop. */
2491 if (tpoint
->pass_count
> 0
2492 && tpoint
->hit_count
>= tpoint
->pass_count
2493 && tpoint
->step_count
== 0
2494 && stopping_tracepoint
== NULL
)
2495 stopping_tracepoint
= tpoint
;
2497 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
2498 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
2500 tframe
= add_traceframe (tpoint
);
2504 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
2506 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
2507 tpoint
->number
, paddress (tpoint
->address
),
2508 tpoint
->actions_str
[acti
]);
2510 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
2511 tpoint
->actions
[acti
]);
2514 finish_traceframe (tframe
);
2517 if (tframe
== NULL
&& tracing
)
2518 trace_buffer_is_full
= 1;
2522 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
2524 struct tracepoint
*tpoint
, int current_step
)
2526 struct traceframe
*tframe
;
2529 trace_debug ("Making new step traceframe for "
2530 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
2531 tpoint
->number
, paddress (tpoint
->address
),
2532 current_step
, tpoint
->step_count
,
2535 tframe
= add_traceframe (tpoint
);
2539 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
2541 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
2542 tpoint
->number
, paddress (tpoint
->address
),
2543 tpoint
->step_actions_str
[acti
]);
2545 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
2546 tpoint
->step_actions
[acti
]);
2549 finish_traceframe (tframe
);
2552 if (tframe
== NULL
&& tracing
)
2553 trace_buffer_is_full
= 1;
2556 static struct regcache
*
2557 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
2559 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
2560 struct regcache
*regcache
= tctx
->regcache
;
2562 gdb_assert (regcache
!= NULL
);
2568 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
2570 struct tracepoint
*tpoint
,
2571 struct traceframe
*tframe
,
2572 struct tracepoint_action
*taction
)
2574 enum eval_result_type err
;
2576 switch (taction
->type
)
2580 struct collect_memory_action
*maction
;
2582 maction
= (struct collect_memory_action
*) taction
;
2584 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
2585 pulongest (maction
->len
),
2586 paddress (maction
->addr
), maction
->basereg
);
2587 /* (should use basereg) */
2588 agent_mem_read (tframe
, NULL
,
2589 (CORE_ADDR
) maction
->addr
, maction
->len
);
2594 struct collect_registers_action
*raction
;
2596 unsigned char *regspace
;
2597 struct regcache tregcache
;
2598 struct regcache
*context_regcache
;
2600 raction
= (struct collect_registers_action
*) taction
;
2602 trace_debug ("Want to collect registers");
2604 /* Collect all registers for now. */
2605 regspace
= add_traceframe_block (tframe
,
2606 1 + register_cache_size ());
2607 if (regspace
== NULL
)
2609 trace_debug ("Trace buffer block allocation failed, skipping");
2612 /* Identify a register block. */
2615 context_regcache
= get_context_regcache (ctx
);
2617 /* Wrap the regblock in a register cache (in the stack, we
2618 don't want to malloc here). */
2619 init_register_cache (&tregcache
, regspace
+ 1);
2621 /* Copy the register data to the regblock. */
2622 regcache_cpy (&tregcache
, context_regcache
);
2624 /* On some platforms, trap-based tracepoints will have the PC
2625 pointing to the next instruction after the trap, but we
2626 don't want the user or GDB trying to guess whether the
2627 saved PC needs adjusting; so always record the adjusted
2628 stop_pc. Note that we can't use tpoint->address instead,
2629 since it will be wrong for while-stepping actions. */
2630 trace_debug ("Storing stop pc (0x%s) in regblock",
2631 paddress (tpoint
->address
));
2633 /* This changes the regblock, not the thread's
2635 regcache_write_pc (&tregcache
, stop_pc
);
2640 struct eval_expr_action
*eaction
;
2642 eaction
= (struct eval_expr_action
*) taction
;
2644 trace_debug ("Want to evaluate expression");
2646 err
= eval_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
2648 if (err
!= expr_eval_no_error
)
2650 record_tracepoint_error (tpoint
, "action expression", err
);
2656 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
2662 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
2663 struct tracepoint
*tpoint
)
2666 enum eval_result_type err
;
2668 err
= eval_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
2670 if (err
!= expr_eval_no_error
)
2672 record_tracepoint_error (tpoint
, "condition", err
);
2673 /* The error case must return false. */
2677 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
2678 tpoint
->number
, paddress (tpoint
->address
),
2680 return (value
? 1 : 0);
2683 /* The packet form of an agent expression consists of an 'X', number
2684 of bytes in expression, a comma, and then the bytes. */
2686 static struct agent_expr
*
2687 parse_agent_expr (char **actparm
)
2689 char *act
= *actparm
;
2691 struct agent_expr
*aexpr
;
2693 ++act
; /* skip the X */
2694 act
= unpack_varlen_hex (act
, &xlen
);
2695 ++act
; /* skip a comma */
2696 aexpr
= xmalloc (sizeof (struct agent_expr
));
2697 aexpr
->length
= xlen
;
2698 aexpr
->bytes
= xmalloc (xlen
);
2699 convert_ascii_to_int (act
, aexpr
->bytes
, xlen
);
2700 *actparm
= act
+ (xlen
* 2);
2704 /* Convert the bytes of an agent expression back into hex digits, so
2705 they can be printed or uploaded. This allocates the buffer,
2706 callers should free when they are done with it. */
2709 unparse_agent_expr (struct agent_expr
*aexpr
)
2713 rslt
= xmalloc (2 * aexpr
->length
+ 1);
2714 convert_int_to_ascii (aexpr
->bytes
, rslt
, aexpr
->length
);
2718 /* The agent expression evaluator, as specified by the GDB docs. It
2719 returns 0 if everything went OK, and a nonzero error code
2722 static enum eval_result_type
2723 eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
2724 struct traceframe
*tframe
,
2725 struct agent_expr
*aexpr
,
2729 #define STACK_MAX 100
2730 ULONGEST stack
[STACK_MAX
], top
;
2735 /* This union is a convenient way to convert representations. For
2736 now, assume a standard architecture where the hardware integer
2737 types have 8, 16, 32, 64 bit types. A more robust solution would
2738 be to import stdint.h from gnulib. */
2743 unsigned char bytes
[1];
2748 unsigned char bytes
[2];
2753 unsigned char bytes
[4];
2758 unsigned char bytes
[8];
2763 if (aexpr
->length
== 0)
2765 trace_debug ("empty agent expression");
2766 return expr_eval_empty_expression
;
2769 /* Cache the stack top in its own variable. Much of the time we can
2770 operate on this variable, rather than dinking with the stack. It
2771 needs to be copied to the stack when sp changes. */
2776 op
= aexpr
->bytes
[pc
++];
2778 trace_debug ("About to interpret byte 0x%x", op
);
2782 case gdb_agent_op_add
:
2786 case gdb_agent_op_sub
:
2787 top
= stack
[--sp
] - top
;
2790 case gdb_agent_op_mul
:
2794 case gdb_agent_op_div_signed
:
2797 trace_debug ("Attempted to divide by zero");
2798 return expr_eval_divide_by_zero
;
2800 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
2803 case gdb_agent_op_div_unsigned
:
2806 trace_debug ("Attempted to divide by zero");
2807 return expr_eval_divide_by_zero
;
2809 top
= stack
[--sp
] / top
;
2812 case gdb_agent_op_rem_signed
:
2815 trace_debug ("Attempted to divide by zero");
2816 return expr_eval_divide_by_zero
;
2818 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
2821 case gdb_agent_op_rem_unsigned
:
2824 trace_debug ("Attempted to divide by zero");
2825 return expr_eval_divide_by_zero
;
2827 top
= stack
[--sp
] % top
;
2830 case gdb_agent_op_lsh
:
2831 top
= stack
[--sp
] << top
;
2834 case gdb_agent_op_rsh_signed
:
2835 top
= ((LONGEST
) stack
[--sp
]) >> top
;
2838 case gdb_agent_op_rsh_unsigned
:
2839 top
= stack
[--sp
] >> top
;
2842 case gdb_agent_op_trace
:
2843 agent_mem_read (tframe
,
2844 NULL
, (CORE_ADDR
) stack
[--sp
], (ULONGEST
) top
);
2849 case gdb_agent_op_trace_quick
:
2850 arg
= aexpr
->bytes
[pc
++];
2851 agent_mem_read (tframe
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
2854 case gdb_agent_op_log_not
:
2858 case gdb_agent_op_bit_and
:
2862 case gdb_agent_op_bit_or
:
2866 case gdb_agent_op_bit_xor
:
2870 case gdb_agent_op_bit_not
:
2874 case gdb_agent_op_equal
:
2875 top
= (stack
[--sp
] == top
);
2878 case gdb_agent_op_less_signed
:
2879 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
2882 case gdb_agent_op_less_unsigned
:
2883 top
= (stack
[--sp
] < top
);
2886 case gdb_agent_op_ext
:
2887 arg
= aexpr
->bytes
[pc
++];
2888 if (arg
< (sizeof (LONGEST
) * 8))
2890 LONGEST mask
= 1 << (arg
- 1);
2891 top
&= ((LONGEST
) 1 << arg
) - 1;
2892 top
= (top
^ mask
) - mask
;
2896 case gdb_agent_op_ref8
:
2897 agent_mem_read (tframe
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
2901 case gdb_agent_op_ref16
:
2902 agent_mem_read (tframe
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
2906 case gdb_agent_op_ref32
:
2907 agent_mem_read (tframe
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
2911 case gdb_agent_op_ref64
:
2912 agent_mem_read (tframe
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
2916 case gdb_agent_op_if_goto
:
2918 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
2925 case gdb_agent_op_goto
:
2926 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
2929 case gdb_agent_op_const8
:
2930 /* Flush the cached stack top. */
2932 top
= aexpr
->bytes
[pc
++];
2935 case gdb_agent_op_const16
:
2936 /* Flush the cached stack top. */
2938 top
= aexpr
->bytes
[pc
++];
2939 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2942 case gdb_agent_op_const32
:
2943 /* Flush the cached stack top. */
2945 top
= aexpr
->bytes
[pc
++];
2946 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2947 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2948 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2951 case gdb_agent_op_const64
:
2952 /* Flush the cached stack top. */
2954 top
= aexpr
->bytes
[pc
++];
2955 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2956 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2957 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2958 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2959 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2960 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2961 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
2964 case gdb_agent_op_reg
:
2965 /* Flush the cached stack top. */
2967 arg
= aexpr
->bytes
[pc
++];
2968 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
2971 struct regcache
*regcache
;
2973 regcache
= get_context_regcache (ctx
);
2975 switch (register_size (regnum
))
2978 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
2982 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
2986 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
2990 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
2994 internal_error (__FILE__
, __LINE__
,
2995 "unhandled register size");
3000 case gdb_agent_op_end
:
3001 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
3002 sp
, pulongest (top
));
3007 /* This should be an error */
3008 trace_debug ("Stack is empty, nothing to return");
3009 return expr_eval_empty_stack
;
3013 return expr_eval_no_error
;
3015 case gdb_agent_op_dup
:
3019 case gdb_agent_op_pop
:
3024 case gdb_agent_op_zero_ext
:
3025 arg
= aexpr
->bytes
[pc
++];
3026 if (arg
< (sizeof (LONGEST
) * 8))
3027 top
&= ((LONGEST
) 1 << arg
) - 1;
3030 case gdb_agent_op_swap
:
3031 /* Interchange top two stack elements, making sure top gets
3032 copied back onto stack. */
3034 top
= stack
[sp
- 1];
3035 stack
[sp
- 1] = stack
[sp
];
3038 case gdb_agent_op_getv
:
3039 /* Flush the cached stack top. */
3041 arg
= aexpr
->bytes
[pc
++];
3042 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
3043 top
= get_trace_state_variable_value (arg
);
3046 case gdb_agent_op_setv
:
3047 arg
= aexpr
->bytes
[pc
++];
3048 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
3049 set_trace_state_variable_value (arg
, top
);
3050 /* Note that we leave the value on the stack, for the
3051 benefit of later/enclosing expressions. */
3054 case gdb_agent_op_tracev
:
3055 arg
= aexpr
->bytes
[pc
++];
3056 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
3057 agent_tsv_read (tframe
, arg
);
3060 /* GDB never (currently) generates any of these ops. */
3061 case gdb_agent_op_float
:
3062 case gdb_agent_op_ref_float
:
3063 case gdb_agent_op_ref_double
:
3064 case gdb_agent_op_ref_long_double
:
3065 case gdb_agent_op_l_to_d
:
3066 case gdb_agent_op_d_to_l
:
3067 case gdb_agent_op_trace16
:
3068 trace_debug ("Agent expression op 0x%x valid, but not handled",
3070 /* If ever GDB generates any of these, we don't have the
3071 option of ignoring. */
3075 trace_debug ("Agent expression op 0x%x not recognized", op
);
3076 /* Don't struggle on, things will just get worse. */
3077 return expr_eval_unrecognized_opcode
;
3080 /* Check for stack badness. */
3081 if (sp
>= (STACK_MAX
- 1))
3083 trace_debug ("Expression stack overflow");
3084 return expr_eval_stack_overflow
;
3089 trace_debug ("Expression stack underflow");
3090 return expr_eval_stack_underflow
;
3093 trace_debug ("Op %s -> sp=%d, top=0x%s",
3094 gdb_agent_op_names
[op
], sp
, pulongest (top
));
3098 /* Do memory copies for bytecodes. */
3099 /* Do the recording of memory blocks for actions and bytecodes. */
3102 agent_mem_read (struct traceframe
*tframe
,
3103 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
3105 unsigned char *mspace
;
3106 ULONGEST remaining
= len
;
3107 unsigned short blocklen
;
3109 /* If a 'to' buffer is specified, use it. */
3112 read_inferior_memory (from
, to
, len
);
3116 /* Otherwise, create a new memory block in the trace buffer. */
3117 while (remaining
> 0)
3121 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
3122 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
3123 mspace
= add_traceframe_block (tframe
, sp
);
3126 /* Identify block as a memory block. */
3129 /* Record address and size. */
3130 memcpy (mspace
, &from
, sizeof (from
));
3131 mspace
+= sizeof (from
);
3132 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
3133 mspace
+= sizeof (blocklen
);
3134 /* Record the memory block proper. */
3135 read_inferior_memory (from
, mspace
, blocklen
);
3136 trace_debug ("%d bytes recorded", blocklen
);
3137 remaining
-= blocklen
;
3143 /* Record the value of a trace state variable. */
3146 agent_tsv_read (struct traceframe
*tframe
, int n
)
3148 unsigned char *vspace
;
3151 vspace
= add_traceframe_block (tframe
,
3152 1 + sizeof (n
) + sizeof (LONGEST
));
3155 /* Identify block as a variable. */
3157 /* Record variable's number and value. */
3158 memcpy (vspace
+ 1, &n
, sizeof (n
));
3159 val
= get_trace_state_variable_value (n
);
3160 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
3161 trace_debug ("Variable %d recorded", n
);
3165 static unsigned char *
3166 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
3167 int tfnum
, char type_wanted
)
3169 unsigned char *dataptr
;
3173 trace_debug ("traceframe %d has no data", tfnum
);
3177 /* Iterate through a traceframe's blocks, looking for a block of the
3179 for (dataptr
= database
;
3180 dataptr
< database
+ datasize
;
3184 unsigned short mlen
;
3186 if (dataptr
== trace_buffer_wrap
)
3188 /* Adjust to reflect wrapping part of the frame around to
3190 datasize
= dataptr
- database
;
3191 dataptr
= database
= trace_buffer_lo
;
3193 blocktype
= *dataptr
++;
3195 if (type_wanted
== blocktype
)
3201 /* Skip over the registers block. */
3202 dataptr
+= register_cache_size ();
3205 /* Skip over the memory block. */
3206 dataptr
+= sizeof (CORE_ADDR
);
3207 memcpy (&mlen
, dataptr
, sizeof (mlen
));
3208 dataptr
+= (sizeof (mlen
) + mlen
);
3211 /* Skip over the static trace data block. */
3212 memcpy (&mlen
, dataptr
, sizeof (mlen
));
3213 dataptr
+= (sizeof (mlen
) + mlen
);
3216 /* Skip over the TSV block. */
3217 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
3220 trace_debug ("traceframe %d has unknown block type 0x%x",
3229 static unsigned char *
3230 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
3232 unsigned char *regblock
;
3234 regblock
= traceframe_find_block_type (tframe
->data
,
3238 if (regblock
== NULL
)
3239 trace_debug ("traceframe %d has no register data", tfnum
);
3244 /* Get registers from a traceframe. */
3247 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
3249 unsigned char *dataptr
;
3250 struct tracepoint
*tpoint
;
3251 struct traceframe
*tframe
;
3253 tframe
= find_traceframe (tfnum
);
3257 trace_debug ("traceframe %d not found", tfnum
);
3261 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
3262 if (dataptr
== NULL
)
3264 /* We don't like making up numbers, but GDB has all manner of
3265 troubles when the target says there are no registers. */
3266 supply_regblock (regcache
, NULL
);
3268 /* We can generally guess at a PC, although this will be
3269 misleading for while-stepping frames and multi-location
3271 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
3273 regcache_write_pc (regcache
, tpoint
->address
);
3276 supply_regblock (regcache
, dataptr
);
3282 traceframe_get_pc (struct traceframe
*tframe
)
3284 struct regcache regcache
;
3285 unsigned char *dataptr
;
3287 dataptr
= traceframe_find_regblock (tframe
, -1);
3288 if (dataptr
== NULL
)
3291 init_register_cache (®cache
, dataptr
);
3292 return regcache_read_pc (®cache
);
3295 /* Read a requested block of memory from a trace frame. */
3298 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
3299 unsigned char *buf
, ULONGEST length
,
3302 struct traceframe
*tframe
;
3303 unsigned char *database
, *dataptr
;
3304 unsigned int datasize
;
3306 unsigned short mlen
;
3308 trace_debug ("traceframe_read_mem");
3310 tframe
= find_traceframe (tfnum
);
3314 trace_debug ("traceframe %d not found", tfnum
);
3318 datasize
= tframe
->data_size
;
3319 database
= dataptr
= &tframe
->data
[0];
3321 /* Iterate through a traceframe's blocks, looking for memory. */
3322 while ((dataptr
= traceframe_find_block_type (dataptr
,
3323 datasize
- (dataptr
- database
),
3324 tfnum
, 'M')) != NULL
)
3326 memcpy (&maddr
, dataptr
, sizeof (maddr
));
3327 dataptr
+= sizeof (maddr
);
3328 memcpy (&mlen
, dataptr
, sizeof (mlen
));
3329 dataptr
+= sizeof (mlen
);
3330 trace_debug ("traceframe %d has %d bytes at %s",
3331 tfnum
, mlen
, paddress (maddr
));
3333 /* Check that requested data is in bounds. */
3334 if (maddr
<= addr
&& (addr
+ length
) <= (maddr
+ mlen
))
3336 /* Block includes the requested range, copy it out. */
3337 memcpy (buf
, dataptr
+ (addr
- maddr
), length
);
3342 /* Skip over this block. */
3346 trace_debug ("traceframe %d has no memory data for the desired region",
3354 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
3357 struct traceframe
*tframe
;
3358 unsigned char *database
, *dataptr
;
3359 unsigned int datasize
;
3362 trace_debug ("traceframe_read_tsv");
3364 tfnum
= current_traceframe
;
3368 trace_debug ("no current traceframe");
3372 tframe
= find_traceframe (tfnum
);
3376 trace_debug ("traceframe %d not found", tfnum
);
3380 datasize
= tframe
->data_size
;
3381 database
= dataptr
= &tframe
->data
[0];
3383 /* Iterate through a traceframe's blocks, looking for the tsv. */
3384 while ((dataptr
= traceframe_find_block_type (dataptr
,
3385 datasize
- (dataptr
- database
),
3386 tfnum
, 'V')) != NULL
)
3388 memcpy (&vnum
, dataptr
, sizeof (vnum
));
3389 dataptr
+= sizeof (vnum
);
3391 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
3393 /* Check that this is the variable we want. */
3396 memcpy (val
, dataptr
, sizeof (*val
));
3400 /* Skip over this block. */
3401 dataptr
+= sizeof (LONGEST
);
3404 trace_debug ("traceframe %d has no data for variable %d",
3410 tsv_get_timestamp (void)
3414 if (gettimeofday (&tv
, 0) != 0)
3417 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
3421 initialize_tracepoint (void)
3423 /* There currently no way to change the buffer size. */
3424 const int sizeOfBuffer
= 5 * 1024 * 1024;
3425 unsigned char *buf
= xmalloc (sizeOfBuffer
);
3426 init_trace_buffer (buf
, sizeOfBuffer
);
3428 /* Wire trace state variable 1 to be the timestamp. This will be
3429 uploaded to GDB upon connection and become one of its trace state
3430 variables. (In case you're wondering, if GDB already has a trace
3431 variable numbered 1, it will be renumbered.) */
3432 create_trace_state_variable (1);
3433 set_trace_state_variable_name (1, "trace_timestamp");
3434 set_trace_state_variable_getter (1, tsv_get_timestamp
);