1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009, 2010, 2011 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/>. */
29 /* This file is built for both GDBserver, and the in-process
30 agent (IPA), a shared library that includes a tracing agent that is
31 loaded by the inferior to support fast tracepoints. Fast
32 tracepoints (or more accurately, jump based tracepoints) are
33 implemented by patching the tracepoint location with a jump into a
34 small trampoline function whose job is to save the register state,
35 call the in-process tracing agent, and then execute the original
36 instruction that was under the tracepoint jump (possibly adjusted,
37 if PC-relative, or some such).
39 The current synchronization design is pull based. That means,
40 GDBserver does most of the work, by peeking/poking at the inferior
41 agent's memory directly for downloading tracepoint and associated
42 objects, and for uploading trace frames. Whenever the IPA needs
43 something from GDBserver (trace buffer is full, tracing stopped for
44 some reason, etc.) the IPA calls a corresponding hook function
45 where GDBserver has placed a breakpoint.
47 Each of the agents has its own trace buffer. When browsing the
48 trace frames built from slow and fast tracepoints from GDB (tfind
49 mode), there's no guarantee the user is seeing the trace frames in
50 strict chronological creation order, although, GDBserver tries to
51 keep the order relatively reasonable, by syncing the trace buffers
56 static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf
, 1, 2);
59 trace_vdebug (const char *fmt
, ...)
65 vsprintf (buf
, fmt
, ap
);
66 fprintf (stderr
, "gdbserver/tracepoint: %s\n", buf
);
70 #define trace_debug_1(level, fmt, args...) \
72 if (level <= debug_threads) \
73 trace_vdebug ((fmt), ##args); \
76 #define trace_debug(FMT, args...) \
77 trace_debug_1 (1, FMT, ##args)
80 # define ATTR_USED __attribute__((used))
81 # define ATTR_NOINLINE __attribute__((noinline))
82 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
85 # define ATTR_NOINLINE
86 # define ATTR_CONSTRUCTOR
89 /* Make sure the functions the IPA needs to export (symbols GDBserver
90 needs to query GDB about) are exported. */
92 #ifdef IN_PROCESS_AGENT
93 # if defined _WIN32 || defined __CYGWIN__
94 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
97 # define IP_AGENT_EXPORT \
98 __attribute__ ((visibility("default"))) ATTR_USED
100 # define IP_AGENT_EXPORT ATTR_USED
104 # define IP_AGENT_EXPORT
107 /* Prefix exported symbols, for good citizenship. All the symbols
108 that need exporting are defined in this module. */
109 #ifdef IN_PROCESS_AGENT
110 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
111 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
112 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
113 # define collecting gdb_agent_collecting
114 # define gdb_collect gdb_agent_gdb_collect
115 # define stop_tracing gdb_agent_stop_tracing
116 # define flush_trace_buffer gdb_agent_flush_trace_buffer
117 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
118 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
119 # define stopping_tracepoint gdb_agent_stopping_tracepoint
120 # define expr_eval_result gdb_agent_expr_eval_result
121 # define error_tracepoint gdb_agent_error_tracepoint
122 # define tracepoints gdb_agent_tracepoints
123 # define tracing gdb_agent_tracing
124 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
125 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
126 # define trace_buffer_lo gdb_agent_trace_buffer_lo
127 # define trace_buffer_hi gdb_agent_trace_buffer_hi
128 # define traceframe_read_count gdb_agent_traceframe_read_count
129 # define traceframe_write_count gdb_agent_traceframe_write_count
130 # define traceframes_created gdb_agent_traceframes_created
131 # define trace_state_variables gdb_agent_trace_state_variables
132 # define get_raw_reg gdb_agent_get_raw_reg
133 # define get_trace_state_variable_value \
134 gdb_agent_get_trace_state_variable_value
135 # define set_trace_state_variable_value \
136 gdb_agent_set_trace_state_variable_value
137 # define ust_loaded gdb_agent_ust_loaded
138 # define helper_thread_id gdb_agent_helper_thread_id
139 # define cmd_buf gdb_agent_cmd_buf
142 #ifndef IN_PROCESS_AGENT
144 /* Addresses of in-process agent's symbols GDBserver cares about. */
146 struct ipa_sym_addresses
148 CORE_ADDR addr_gdb_tp_heap_buffer
;
149 CORE_ADDR addr_gdb_jump_pad_buffer
;
150 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
151 CORE_ADDR addr_collecting
;
152 CORE_ADDR addr_gdb_collect
;
153 CORE_ADDR addr_stop_tracing
;
154 CORE_ADDR addr_flush_trace_buffer
;
155 CORE_ADDR addr_about_to_request_buffer_space
;
156 CORE_ADDR addr_trace_buffer_is_full
;
157 CORE_ADDR addr_stopping_tracepoint
;
158 CORE_ADDR addr_expr_eval_result
;
159 CORE_ADDR addr_error_tracepoint
;
160 CORE_ADDR addr_tracepoints
;
161 CORE_ADDR addr_tracing
;
162 CORE_ADDR addr_trace_buffer_ctrl
;
163 CORE_ADDR addr_trace_buffer_ctrl_curr
;
164 CORE_ADDR addr_trace_buffer_lo
;
165 CORE_ADDR addr_trace_buffer_hi
;
166 CORE_ADDR addr_traceframe_read_count
;
167 CORE_ADDR addr_traceframe_write_count
;
168 CORE_ADDR addr_traceframes_created
;
169 CORE_ADDR addr_trace_state_variables
;
170 CORE_ADDR addr_get_raw_reg
;
171 CORE_ADDR addr_get_trace_state_variable_value
;
172 CORE_ADDR addr_set_trace_state_variable_value
;
173 CORE_ADDR addr_ust_loaded
;
174 CORE_ADDR addr_helper_thread_id
;
175 CORE_ADDR addr_cmd_buf
;
178 #define STRINGIZE_1(STR) #STR
179 #define STRINGIZE(STR) STRINGIZE_1(STR)
180 #define IPA_SYM(SYM) \
182 STRINGIZE (gdb_agent_ ## SYM), \
183 offsetof (struct ipa_sym_addresses, addr_ ## SYM) \
192 IPA_SYM(gdb_tp_heap_buffer
),
193 IPA_SYM(gdb_jump_pad_buffer
),
194 IPA_SYM(gdb_jump_pad_buffer_end
),
196 IPA_SYM(gdb_collect
),
197 IPA_SYM(stop_tracing
),
198 IPA_SYM(flush_trace_buffer
),
199 IPA_SYM(about_to_request_buffer_space
),
200 IPA_SYM(trace_buffer_is_full
),
201 IPA_SYM(stopping_tracepoint
),
202 IPA_SYM(expr_eval_result
),
203 IPA_SYM(error_tracepoint
),
204 IPA_SYM(tracepoints
),
206 IPA_SYM(trace_buffer_ctrl
),
207 IPA_SYM(trace_buffer_ctrl_curr
),
208 IPA_SYM(trace_buffer_lo
),
209 IPA_SYM(trace_buffer_hi
),
210 IPA_SYM(traceframe_read_count
),
211 IPA_SYM(traceframe_write_count
),
212 IPA_SYM(traceframes_created
),
213 IPA_SYM(trace_state_variables
),
214 IPA_SYM(get_raw_reg
),
215 IPA_SYM(get_trace_state_variable_value
),
216 IPA_SYM(set_trace_state_variable_value
),
218 IPA_SYM(helper_thread_id
),
222 struct ipa_sym_addresses ipa_sym_addrs
;
224 int all_tracepoint_symbols_looked_up
;
227 in_process_agent_loaded (void)
229 return all_tracepoint_symbols_looked_up
;
232 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
234 /* Returns true if both the in-process agent library and the static
235 tracepoints libraries are loaded in the inferior. */
238 in_process_agent_loaded_ust (void)
242 if (!in_process_agent_loaded ())
244 warning ("In-process agent not loaded");
248 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
250 warning ("Error reading ust_loaded in lib");
258 write_e_ipa_not_loaded (char *buffer
)
261 "E.In-process agent library not loaded in process. "
262 "Fast and static tracepoints unavailable.");
265 /* Write an error to BUFFER indicating that UST isn't loaded in the
269 write_e_ust_not_loaded (char *buffer
)
273 "E.UST library not loaded in process. "
274 "Static tracepoints unavailable.");
276 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
280 /* If the in-process agent library isn't loaded in the inferior, write
281 an error to BUFFER, and return 1. Otherwise, return 0. */
284 maybe_write_ipa_not_loaded (char *buffer
)
286 if (!in_process_agent_loaded ())
288 write_e_ipa_not_loaded (buffer
);
294 /* If the in-process agent library and the ust (static tracepoints)
295 library aren't loaded in the inferior, write an error to BUFFER,
296 and return 1. Otherwise, return 0. */
299 maybe_write_ipa_ust_not_loaded (char *buffer
)
301 if (!in_process_agent_loaded ())
303 write_e_ipa_not_loaded (buffer
);
306 else if (!in_process_agent_loaded_ust ())
308 write_e_ust_not_loaded (buffer
);
314 /* Cache all future symbols that the tracepoints module might request.
315 We can not request symbols at arbitrary states in the remote
316 protocol, only when the client tells us that new symbols are
317 available. So when we load the in-process library, make sure to
318 check the entire list. */
321 tracepoint_look_up_symbols (void)
325 if (all_tracepoint_symbols_looked_up
)
328 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
331 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
333 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
336 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
341 all_tracepoint_symbols_looked_up
= 1;
346 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
347 of the "stop_tracing" function. When this breakpoint is hit,
348 tracing stopped in the IPA for some reason. E.g., due to
349 tracepoint reaching the pass count, hitting conditional expression
350 evaluation error, etc.
352 The IPA's trace buffer is never in circular tracing mode: instead,
353 GDBserver's is, and whenever the in-process buffer fills, it calls
354 "flush_trace_buffer", which triggers an internal breakpoint.
355 GDBserver reacts to this breakpoint by pulling the meanwhile
356 collected data. Old frames discarding is always handled on the
359 #ifdef IN_PROCESS_AGENT
360 int debug_threads
= 0;
363 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
365 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
369 /* Call this in the functions where GDBserver places a breakpoint, so
370 that the compiler doesn't try to be clever and skip calling the
371 function at all. This is necessary, even if we tell the compiler
372 to not inline said functions. */
374 #if defined(__GNUC__)
375 # define UNKNOWN_SIDE_EFFECTS() asm ("")
377 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
380 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
383 /* GDBserver places breakpoint here. */
384 UNKNOWN_SIDE_EFFECTS();
387 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
388 flush_trace_buffer (void)
390 /* GDBserver places breakpoint here. */
391 UNKNOWN_SIDE_EFFECTS();
396 #ifndef IN_PROCESS_AGENT
398 tracepoint_handler (CORE_ADDR address
)
400 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
405 /* Breakpoint at "stop_tracing" in the inferior lib. */
406 struct breakpoint
*stop_tracing_bkpt
;
407 static int stop_tracing_handler (CORE_ADDR
);
409 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
410 struct breakpoint
*flush_trace_buffer_bkpt
;
411 static int flush_trace_buffer_handler (CORE_ADDR
);
413 static void download_tracepoints (void);
414 static void download_trace_state_variables (void);
415 static void upload_fast_traceframes (void);
417 static int run_inferior_command (char *cmd
);
420 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
422 return read_inferior_memory (symaddr
, (unsigned char *) val
,
427 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
429 return read_inferior_memory (symaddr
, (unsigned char *) val
,
434 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
436 void *pval
= (void *) (uintptr_t) val
;
439 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
440 *val
= (uintptr_t) pval
;
445 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
447 void *pval
= (void *) (uintptr_t) val
;
448 return write_inferior_memory (symaddr
,
449 (unsigned char *) &pval
, sizeof (pval
));
453 write_inferior_integer (CORE_ADDR symaddr
, int val
)
455 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
459 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
461 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
466 /* This enum must exactly match what is documented in
467 gdb/doc/agentexpr.texi, including all the numerical values. */
471 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
472 gdb_agent_op_ ## NAME = VALUE,
478 static const char *gdb_agent_op_names
[gdb_agent_op_last
] =
481 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
486 static const unsigned char gdb_agent_op_sizes
[gdb_agent_op_last
] =
489 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
498 unsigned char *bytes
;
501 /* Base action. Concrete actions inherit this. */
503 struct tracepoint_action
508 /* An 'M' (collect memory) action. */
509 struct collect_memory_action
511 struct tracepoint_action base
;
518 /* An 'R' (collect registers) action. */
520 struct collect_registers_action
522 struct tracepoint_action base
;
525 /* An 'X' (evaluate expression) action. */
527 struct eval_expr_action
529 struct tracepoint_action base
;
531 struct agent_expr
*expr
;
534 /* An 'L' (collect static trace data) action. */
535 struct collect_static_trace_data_action
537 struct tracepoint_action base
;
540 /* This structure describes a piece of the source-level definition of
541 the tracepoint. The contents are not interpreted by the target,
542 but preserved verbatim for uploading upon reconnection. */
546 /* The type of string, such as "cond" for a conditional. */
549 /* The source-level string itself. For the sake of target
550 debugging, we store it in plaintext, even though it is always
551 transmitted in hex. */
554 /* Link to the next one in the list. We link them in the order
555 received, in case some make up an ordered list of commands or
557 struct source_string
*next
;
562 /* Trap based tracepoint. */
565 /* A fast tracepoint implemented with a jump instead of a trap. */
568 /* A static tracepoint, implemented by a program call into a tracing
573 struct tracepoint_hit_ctx
;
575 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
578 /* The definition of a tracepoint. */
580 /* Tracepoints may have multiple locations, each at a different
581 address. This can occur with optimizations, template
582 instantiation, etc. Since the locations may be in different
583 scopes, the conditions and actions may be different for each
584 location. Our target version of tracepoints is more like GDB's
585 notion of "breakpoint locations", but we have almost nothing that
586 is not per-location, so we bother having two kinds of objects. The
587 key consequence is that numbers are not unique, and that it takes
588 both number and address to identify a tracepoint uniquely. */
592 /* The number of the tracepoint, as specified by GDB. Several
593 tracepoint objects here may share a number. */
596 /* Address at which the tracepoint is supposed to trigger. Several
597 tracepoints may share an address. */
600 /* Tracepoint type. */
601 enum tracepoint_type type
;
603 /* True if the tracepoint is currently enabled. */
606 /* The number of single steps that will be performed after each
610 /* The number of times the tracepoint may be hit before it will
611 terminate the entire tracing run. */
614 /* Pointer to the agent expression that is the tracepoint's
615 conditional, or NULL if the tracepoint is unconditional. */
616 struct agent_expr
*cond
;
618 /* The list of actions to take when the tracepoint triggers. */
620 struct tracepoint_action
**actions
;
622 /* Count of the times we've hit this tracepoint during the run.
623 Note that while-stepping steps are not counted as "hits". */
626 CORE_ADDR compiled_cond
;
628 /* Link to the next tracepoint in the list. */
629 struct tracepoint
*next
;
631 #ifndef IN_PROCESS_AGENT
632 /* The list of actions to take when the tracepoint triggers, in
633 string/packet form. */
636 /* The collection of strings that describe the tracepoint as it was
637 entered into GDB. These are not used by the target, but are
638 reported back to GDB upon reconnection. */
639 struct source_string
*source_strings
;
641 /* The number of bytes displaced by fast tracepoints. It may subsume
642 multiple instructions, for multi-byte fast tracepoints. This
643 field is only valid for fast tracepoints. */
646 /* Only for fast tracepoints. */
647 CORE_ADDR obj_addr_on_target
;
649 /* Address range where the original instruction under a fast
650 tracepoint was relocated to. (_end is actually one byte past
652 CORE_ADDR adjusted_insn_addr
;
653 CORE_ADDR adjusted_insn_addr_end
;
655 /* The address range of the piece of the jump pad buffer that was
656 assigned to this fast tracepoint. (_end is actually one byte
659 CORE_ADDR jump_pad_end
;
661 /* The list of actions to take while in a stepping loop. These
662 fields are only valid for patch-based tracepoints. */
663 int num_step_actions
;
664 struct tracepoint_action
**step_actions
;
665 /* Same, but in string/packet form. */
666 char **step_actions_str
;
668 /* Handle returned by the breakpoint or tracepoint module when we
669 inserted the trap or jump, or hooked into a static tracepoint.
670 NULL if we haven't inserted it yet. */
676 #ifndef IN_PROCESS_AGENT
678 /* Given `while-stepping', a thread may be collecting data for more
679 than one tracepoint simultaneously. On the other hand, the same
680 tracepoint with a while-stepping action may be hit by more than one
681 thread simultaneously (but not quite, each thread could be handling
682 a different step). Each thread holds a list of these objects,
683 representing the current step of each while-stepping action being
688 struct wstep_state
*next
;
690 /* The tracepoint number. */
692 /* The tracepoint's address. */
693 CORE_ADDR tp_address
;
695 /* The number of the current step in this 'while-stepping'
702 /* The linked list of all tracepoints. Marked explicitly as used as
703 the in-process library doesn't use it for the fast tracepoints
705 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
707 #ifndef IN_PROCESS_AGENT
709 /* Pointer to the last tracepoint in the list, new tracepoints are
710 linked in at the end. */
712 static struct tracepoint
*last_tracepoint
;
715 /* The first tracepoint to exceed its pass count. */
717 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
719 /* True if the trace buffer is full or otherwise no longer usable. */
721 IP_AGENT_EXPORT
int trace_buffer_is_full
;
723 /* Enumeration of the different kinds of things that can happen during
724 agent expression evaluation. */
726 enum eval_result_type
729 expr_eval_empty_expression
,
730 expr_eval_empty_stack
,
731 expr_eval_stack_overflow
,
732 expr_eval_stack_underflow
,
733 expr_eval_unhandled_opcode
,
734 expr_eval_unrecognized_opcode
,
735 expr_eval_divide_by_zero
,
736 expr_eval_invalid_goto
739 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
741 #ifndef IN_PROCESS_AGENT
743 static const char *eval_result_names
[] =
745 "terror:in the attic", /* this should never be reported */
746 "terror:empty expression",
747 "terror:empty stack",
748 "terror:stack overflow",
749 "terror:stack underflow",
750 "terror:unhandled opcode",
751 "terror:unrecognized opcode",
752 "terror:divide by zero"
757 /* The tracepoint in which the error occurred. */
759 static struct tracepoint
*error_tracepoint
;
761 struct trace_state_variable
763 /* This is the name of the variable as used in GDB. The target
764 doesn't use the name, but needs to have it for saving and
765 reconnection purposes. */
768 /* This number identifies the variable uniquely. Numbers may be
769 assigned either by the target (in the case of builtin variables),
770 or by GDB, and are presumed unique during the course of a trace
774 /* The variable's initial value, a 64-bit signed integer always. */
775 LONGEST initial_value
;
777 /* The variable's value, a 64-bit signed integer always. */
780 /* Pointer to a getter function, used to supply computed values. */
781 LONGEST (*getter
) (void);
783 /* Link to the next variable. */
784 struct trace_state_variable
*next
;
787 /* Linked list of all trace state variables. */
789 #ifdef IN_PROCESS_AGENT
790 struct trace_state_variable
*alloced_trace_state_variables
;
793 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
795 /* The results of tracing go into a fixed-size space known as the
796 "trace buffer". Because usage follows a limited number of
797 patterns, we manage it ourselves rather than with malloc. Basic
798 rules are that we create only one trace frame at a time, each is
799 variable in size, they are never moved once created, and we only
800 discard if we are doing a circular buffer, and then only the oldest
801 ones. Each trace frame includes its own size, so we don't need to
802 link them together, and the trace frame number is relative to the
803 first one, so we don't need to record numbers. A trace frame also
804 records the number of the tracepoint that created it. The data
805 itself is a series of blocks, each introduced by a single character
806 and with a defined format. Each type of block has enough
807 type/length info to allow scanners to jump quickly from one block
808 to the next without reading each byte in the block. */
810 /* Trace buffer management would be simple - advance a free pointer
811 from beginning to end, then stop - were it not for the circular
812 buffer option, which is a useful way to prevent a trace run from
813 stopping prematurely because the buffer filled up. In the circular
814 case, the location of the first trace frame (trace_buffer_start)
815 moves as old trace frames are discarded. Also, since we grow trace
816 frames incrementally as actions are performed, we wrap around to
817 the beginning of the trace buffer. This is per-block, so each
818 block within a trace frame remains contiguous. Things get messy
819 when the wrapped-around trace frame is the one being discarded; the
820 free space ends up in two parts at opposite ends of the buffer. */
823 # if defined(__GNUC__)
824 # define ATTR_PACKED __attribute__ ((packed))
826 # define ATTR_PACKED /* nothing */
830 /* The data collected at a tracepoint hit. This object should be as
831 small as possible, since there may be a great many of them. We do
832 not need to keep a frame number, because they are all sequential
833 and there are no deletions; so the Nth frame in the buffer is
834 always frame number N. */
838 /* Number of the tracepoint that collected this traceframe. A value
839 of 0 indicates the current end of the trace buffer. We make this
840 a 16-bit field because it's never going to happen that GDB's
841 numbering of tracepoints reaches 32,000. */
844 /* The size of the data in this trace frame. We limit this to 32
845 bits, even on a 64-bit target, because it's just implausible that
846 one is validly going to collect 4 gigabytes of data at a single
848 unsigned int data_size
: 32;
850 /* The base of the trace data, which is contiguous from this point. */
851 unsigned char data
[0];
855 /* The traceframe to be used as the source of data to send back to
856 GDB. A value of -1 means to get data from the live program. */
858 int current_traceframe
= -1;
860 /* This flag is true if the trace buffer is circular, meaning that
861 when it fills, the oldest trace frames are discarded in order to
864 #ifndef IN_PROCESS_AGENT
865 static int circular_trace_buffer
;
868 /* Pointer to the block of memory that traceframes all go into. */
870 static unsigned char *trace_buffer_lo
;
872 /* Pointer to the end of the trace buffer, more precisely to the byte
873 after the end of the buffer. */
875 static unsigned char *trace_buffer_hi
;
877 /* Control structure holding the read/write/etc. pointers into the
878 trace buffer. We need more than one of these to implement a
879 transaction-like mechanism to garantees that both GDBserver and the
880 in-process agent can try to change the trace buffer
883 struct trace_buffer_control
885 /* Pointer to the first trace frame in the buffer. In the
886 non-circular case, this is equal to trace_buffer_lo, otherwise it
887 moves around in the buffer. */
888 unsigned char *start
;
890 /* Pointer to the free part of the trace buffer. Note that we clear
891 several bytes at and after this pointer, so that traceframe
892 scans/searches terminate properly. */
895 /* Pointer to the byte after the end of the free part. Note that
896 this may be smaller than trace_buffer_free in the circular case,
897 and means that the free part is in two pieces. Initially it is
898 equal to trace_buffer_hi, then is generally equivalent to
899 trace_buffer_start. */
900 unsigned char *end_free
;
902 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
903 this is the point at which the trace data breaks, and resumes at
908 /* Same as above, to be used by GDBserver when updating the in-process
910 struct ipa_trace_buffer_control
919 /* We have possibly both GDBserver and an inferior thread accessing
920 the same IPA trace buffer memory. The IPA is the producer (tries
921 to put new frames in the buffer), while GDBserver occasionally
922 consumes them, that is, flushes the IPA's buffer into its own
923 buffer. Both sides need to update the trace buffer control
924 pointers (current head, tail, etc.). We can't use a global lock to
925 synchronize the accesses, as otherwise we could deadlock GDBserver
926 (if the thread holding the lock stops for a signal, say). So
927 instead of that, we use a transaction scheme where GDBserver writes
928 always prevail over the IPAs writes, and, we have the IPA detect
929 the commit failure/overwrite, and retry the whole attempt. This is
930 mainly implemented by having a global token object that represents
931 who wrote last to the buffer control structure. We need to freeze
932 any inferior writing to the buffer while GDBserver touches memory,
933 so that the inferior can correctly detect that GDBserver had been
934 there, otherwise, it could mistakingly think its commit was
935 successful; that's implemented by simply having GDBserver set a
936 breakpoint the inferior hits if it is the critical region.
938 There are three cycling trace buffer control structure copies
939 (buffer head, tail, etc.), with the token object including an index
940 indicating which is current live copy. The IPA tentatively builds
941 an updated copy in a non-current control structure, while GDBserver
942 always clobbers the current version directly. The IPA then tries
943 to atomically "commit" its version; if GDBserver clobbered the
944 structure meanwhile, that will fail, and the IPA restarts the
947 Listing the step in further detail, we have:
949 In-process agent (producer):
951 - passes by `about_to_request_buffer_space' breakpoint/lock
953 - reads current token, extracts current trace buffer control index,
954 and starts tentatively updating the rightmost one (0->1, 1->2,
955 2->0). Note that only one inferior thread is executing this code
956 at any given time, due to an outer lock in the jump pads.
958 - updates counters, and tries to commit the token.
960 - passes by second `about_to_request_buffer_space' breakpoint/lock,
961 leaving the sync region.
963 - checks if the update was effective.
965 - if trace buffer was found full, hits flush_trace_buffer
966 breakpoint, and restarts later afterwards.
968 GDBserver (consumer):
970 - sets `about_to_request_buffer_space' breakpoint/lock.
972 - updates the token unconditionally, using the current buffer
973 control index, since it knows that the IP agent always writes to
974 the rightmost, and due to the breakpoint, at most one IP thread
975 can try to update the trace buffer concurrently to GDBserver, so
976 there will be no danger of trace buffer control index wrap making
977 the IPA write to the same index as GDBserver.
979 - flushes the IP agent's trace buffer completely, and updates the
980 current trace buffer control structure. GDBserver *always* wins.
982 - removes the `about_to_request_buffer_space' breakpoint.
984 The token is stored in the `trace_buffer_ctrl_curr' variable.
985 Internally, it's bits are defined as:
987 |-------------+-----+-------------+--------+-------------+--------------|
988 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
989 |-------------+-----+-------------+--------+-------------+--------------|
990 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
991 |-------------+-----+-------------+--------+-------------+--------------|
993 GSB - GDBserver Stamp Bit
994 PC - Previous Counter
996 TBCI - Trace Buffer Control Index
999 An IPA update of `trace_buffer_ctrl_curr' does:
1001 - read CC from the current token, save as PC.
1003 - atomically tries to write PC+1,CC
1005 A GDBserver update of `trace_buffer_ctrl_curr' does:
1007 - reads PC and CC from the current token.
1012 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1013 for the counters described below. The cleared bits are used to
1014 hold the index of the items of the `trace_buffer_ctrl' array that
1016 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1018 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1019 counter, and the `current' counter. */
1021 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1022 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1024 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1025 always stamps this bit as set. */
1026 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1028 #ifdef IN_PROCESS_AGENT
1029 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1030 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1032 # define TRACE_BUFFER_CTRL_CURR \
1033 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1037 /* The GDBserver side agent only needs one instance of this object, as
1038 it doesn't need to sync with itself. Define it as array anyway so
1039 that the rest of the code base doesn't need to care for the
1041 struct trace_buffer_control trace_buffer_ctrl
[1];
1042 # define TRACE_BUFFER_CTRL_CURR 0
1045 /* These are convenience macros used to access the current trace
1046 buffer control in effect. */
1047 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1048 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1049 #define trace_buffer_end_free \
1050 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1051 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1054 /* Macro that returns a pointer to the first traceframe in the buffer. */
1056 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1058 /* Macro that returns a pointer to the next traceframe in the buffer.
1059 If the computed location is beyond the wraparound point, subtract
1060 the offset of the wraparound. */
1062 #define NEXT_TRACEFRAME_1(TF) \
1063 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1065 #define NEXT_TRACEFRAME(TF) \
1066 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1067 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1068 ? (trace_buffer_wrap - trace_buffer_lo) \
1071 /* The difference between these counters represents the total number
1072 of complete traceframes present in the trace buffer. The IP agent
1073 writes to the write count, GDBserver writes to read count. */
1075 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1076 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1078 /* Convenience macro. */
1080 #define traceframe_count \
1081 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1083 /* The count of all traceframes created in the current run, including
1084 ones that were discarded to make room. */
1086 IP_AGENT_EXPORT
int traceframes_created
;
1088 #ifndef IN_PROCESS_AGENT
1090 /* Read-only regions are address ranges whose contents don't change,
1091 and so can be read from target memory even while looking at a trace
1092 frame. Without these, disassembly for instance will likely fail,
1093 because the program code is not usually collected into a trace
1094 frame. This data structure does not need to be very complicated or
1095 particularly efficient, it's only going to be used occasionally,
1096 and only by some commands. */
1098 struct readonly_region
1100 /* The bounds of the region. */
1101 CORE_ADDR start
, end
;
1103 /* Link to the next one. */
1104 struct readonly_region
*next
;
1107 /* Linked list of readonly regions. This list stays in effect from
1108 one tstart to the next. */
1110 static struct readonly_region
*readonly_regions
;
1114 /* The global that controls tracing overall. */
1116 IP_AGENT_EXPORT
int tracing
;
1118 #ifndef IN_PROCESS_AGENT
1120 /* Controls whether tracing should continue after GDB disconnects. */
1122 int disconnected_tracing
;
1124 /* The reason for the last tracing run to have stopped. We initialize
1125 to a distinct string so that GDB can distinguish between "stopped
1126 after running" and "stopped because never run" cases. */
1128 static const char *tracing_stop_reason
= "tnotrun";
1130 static int tracing_stop_tpnum
;
1134 /* Functions local to this file. */
1136 /* Base "class" for tracepoint type specific data to be passed down to
1137 collect_data_at_tracepoint. */
1138 struct tracepoint_hit_ctx
1140 enum tracepoint_type type
;
1143 #ifdef IN_PROCESS_AGENT
1145 /* Fast/jump tracepoint specific data to be passed down to
1146 collect_data_at_tracepoint. */
1147 struct fast_tracepoint_ctx
1149 struct tracepoint_hit_ctx base
;
1151 struct regcache regcache
;
1152 int regcache_initted
;
1153 unsigned char *regspace
;
1155 unsigned char *regs
;
1156 struct tracepoint
*tpoint
;
1159 /* Static tracepoint specific data to be passed down to
1160 collect_data_at_tracepoint. */
1161 struct static_tracepoint_ctx
1163 struct tracepoint_hit_ctx base
;
1165 /* The regcache corresponding to the registers state at the time of
1166 the tracepoint hit. Initialized lazily, from REGS. */
1167 struct regcache regcache
;
1168 int regcache_initted
;
1170 /* The buffer space REGCACHE above uses. We use a separate buffer
1171 instead of letting the regcache malloc for both signal safety and
1172 performance reasons; this is allocated on the stack instead. */
1173 unsigned char *regspace
;
1175 /* The register buffer as passed on by lttng/ust. */
1176 struct registers
*regs
;
1178 /* The "printf" formatter and the args the user passed to the marker
1179 call. We use this to be able to collect "static trace data"
1184 /* The GDB tracepoint matching the probed marker that was "hit". */
1185 struct tracepoint
*tpoint
;
1190 /* Static tracepoint specific data to be passed down to
1191 collect_data_at_tracepoint. */
1192 struct trap_tracepoint_ctx
1194 struct tracepoint_hit_ctx base
;
1196 struct regcache
*regcache
;
1201 #ifndef IN_PROCESS_AGENT
1202 static struct agent_expr
*parse_agent_expr (char **actparm
);
1203 static char *unparse_agent_expr (struct agent_expr
*aexpr
);
1205 static enum eval_result_type
eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1206 struct traceframe
*tframe
,
1207 struct agent_expr
*aexpr
,
1210 static int agent_mem_read (struct traceframe
*tframe
,
1211 unsigned char *to
, CORE_ADDR from
, ULONGEST len
);
1212 static int agent_mem_read_string (struct traceframe
*tframe
,
1213 unsigned char *to
, CORE_ADDR from
,
1215 static int agent_tsv_read (struct traceframe
*tframe
, int n
);
1217 #ifndef IN_PROCESS_AGENT
1218 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1219 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1222 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1223 struct tracepoint
*tpoint
);
1225 #ifndef IN_PROCESS_AGENT
1226 static void clear_readonly_regions (void);
1227 static void clear_installed_tracepoints (void);
1230 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1232 struct tracepoint
*tpoint
);
1233 #ifndef IN_PROCESS_AGENT
1234 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1236 struct tracepoint
*tpoint
, int current_step
);
1237 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1238 CORE_ADDR
*jump_entry
);
1240 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1242 struct tracepoint
*tpoint
,
1243 struct traceframe
*tframe
,
1244 struct tracepoint_action
*taction
);
1246 #ifndef IN_PROCESS_AGENT
1247 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1250 #if defined(__GNUC__)
1251 # define memory_barrier() asm volatile ("" : : : "memory")
1253 # define memory_barrier() do {} while (0)
1256 /* We only build the IPA if this builtin is supported, and there are
1257 no uses of this in GDBserver itself, so we're safe in defining this
1259 #define cmpxchg(mem, oldval, newval) \
1260 __sync_val_compare_and_swap (mem, oldval, newval)
1262 /* The size in bytes of the buffer used to talk to the IPA helper
1264 #define CMD_BUF_SIZE 1024
1266 /* Record that an error occurred during expression evaluation. */
1269 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1270 enum eval_result_type rtype
)
1272 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1273 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1275 #ifdef IN_PROCESS_AGENT
1276 /* Only record the first error we get. */
1277 if (cmpxchg (&expr_eval_result
,
1279 rtype
) != expr_eval_no_error
)
1282 if (expr_eval_result
!= expr_eval_no_error
)
1286 error_tracepoint
= tpoint
;
1289 /* Trace buffer management. */
1292 clear_trace_buffer (void)
1294 trace_buffer_start
= trace_buffer_lo
;
1295 trace_buffer_free
= trace_buffer_lo
;
1296 trace_buffer_end_free
= trace_buffer_hi
;
1297 trace_buffer_wrap
= trace_buffer_hi
;
1298 /* A traceframe with zeroed fields marks the end of trace data. */
1299 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1300 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1301 traceframe_read_count
= traceframe_write_count
= 0;
1302 traceframes_created
= 0;
1305 #ifndef IN_PROCESS_AGENT
1308 clear_inferior_trace_buffer (void)
1310 CORE_ADDR ipa_trace_buffer_lo
;
1311 CORE_ADDR ipa_trace_buffer_hi
;
1312 struct traceframe ipa_traceframe
= { 0 };
1313 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1315 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1316 &ipa_trace_buffer_lo
);
1317 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1318 &ipa_trace_buffer_hi
);
1320 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1321 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1322 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1323 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1325 /* A traceframe with zeroed fields marks the end of trace data. */
1326 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1327 (unsigned char *) &ipa_trace_buffer_ctrl
,
1328 sizeof (ipa_trace_buffer_ctrl
));
1330 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1332 /* A traceframe with zeroed fields marks the end of trace data. */
1333 write_inferior_memory (ipa_trace_buffer_lo
,
1334 (unsigned char *) &ipa_traceframe
,
1335 sizeof (ipa_traceframe
));
1337 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1338 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1339 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1345 init_trace_buffer (unsigned char *buf
, int bufsize
)
1347 trace_buffer_lo
= buf
;
1348 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1350 clear_trace_buffer ();
1353 #ifdef IN_PROCESS_AGENT
1355 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1356 about_to_request_buffer_space (void)
1358 /* GDBserver places breakpoint here while it goes about to flush
1359 data at random times. */
1360 UNKNOWN_SIDE_EFFECTS();
1365 /* Carve out a piece of the trace buffer, returning NULL in case of
1369 trace_buffer_alloc (size_t amt
)
1371 unsigned char *rslt
;
1372 struct trace_buffer_control
*tbctrl
;
1374 #ifdef IN_PROCESS_AGENT
1375 unsigned int prev
, prev_filtered
;
1376 unsigned int commit_count
;
1377 unsigned int commit
;
1378 unsigned int readout
;
1380 struct traceframe
*oldest
;
1381 unsigned char *new_start
;
1384 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1385 (long) amt
, (long) sizeof (struct traceframe
));
1387 /* Account for the EOB marker. */
1388 amt
+= sizeof (struct traceframe
);
1390 #ifdef IN_PROCESS_AGENT
1394 /* Read the current token and extract the index to try to write to,
1395 storing it in CURR. */
1396 prev
= trace_buffer_ctrl_curr
;
1397 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1398 curr
= prev_filtered
+ 1;
1402 about_to_request_buffer_space ();
1404 /* Start out with a copy of the current state. GDBserver may be
1405 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1406 be able to commit anyway if that happens. */
1407 trace_buffer_ctrl
[curr
]
1408 = trace_buffer_ctrl
[prev_filtered
];
1409 trace_debug ("trying curr=%u", curr
);
1411 /* The GDBserver's agent doesn't need all that syncing, and always
1412 updates TCB 0 (there's only one, mind you). */
1415 tbctrl
= &trace_buffer_ctrl
[curr
];
1417 /* Offsets are easier to grok for debugging than raw addresses,
1418 especially for the small trace buffer sizes that are useful for
1420 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1422 (int) (tbctrl
->start
- trace_buffer_lo
),
1423 (int) (tbctrl
->free
- trace_buffer_lo
),
1424 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1425 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1426 (int) (trace_buffer_hi
- trace_buffer_lo
));
1428 /* The algorithm here is to keep trying to get a contiguous block of
1429 the requested size, possibly discarding older traceframes to free
1430 up space. Since free space might come in one or two pieces,
1431 depending on whether discarded traceframes wrapped around at the
1432 high end of the buffer, we test both pieces after each
1436 /* First, if we have two free parts, try the upper one first. */
1437 if (tbctrl
->end_free
< tbctrl
->free
)
1439 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1440 /* We have enough in the upper part. */
1444 /* Our high part of free space wasn't enough. Give up
1445 on it for now, set wraparound. We will recover the
1446 space later, if/when the wrapped-around traceframe is
1448 trace_debug ("Upper part too small, setting wraparound");
1449 tbctrl
->wrap
= tbctrl
->free
;
1450 tbctrl
->free
= trace_buffer_lo
;
1454 /* The normal case. */
1455 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1458 #ifdef IN_PROCESS_AGENT
1459 /* The IP Agent's buffer is always circular. It isn't used
1460 currently, but `circular_trace_buffer' could represent
1461 GDBserver's mode. If we didn't find space, ask GDBserver to
1464 flush_trace_buffer ();
1468 trace_debug ("gdbserver flushed buffer, retrying");
1472 /* GDBserver cancelled the tracing. Bail out as well. */
1475 /* If we're here, then neither part is big enough, and
1476 non-circular trace buffers are now full. */
1477 if (!circular_trace_buffer
)
1479 trace_debug ("Not enough space in the trace buffer");
1483 trace_debug ("Need more space in the trace buffer");
1485 /* If we have a circular buffer, we can try discarding the
1486 oldest traceframe and see if that helps. */
1487 oldest
= FIRST_TRACEFRAME ();
1488 if (oldest
->tpnum
== 0)
1490 /* Not good; we have no traceframes to free. Perhaps we're
1491 asking for a block that is larger than the buffer? In
1492 any case, give up. */
1493 trace_debug ("No traceframes to discard");
1497 /* We don't run this code in the in-process agent currently.
1498 E.g., we could leave the in-process agent in autonomous
1499 circular mode if we only have fast tracepoints. If we do
1500 that, then this bit becomes racy with GDBserver, which also
1501 writes to this counter. */
1502 --traceframe_write_count
;
1504 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1505 /* If we freed the traceframe that wrapped around, go back
1506 to the non-wrap case. */
1507 if (new_start
< tbctrl
->start
)
1509 trace_debug ("Discarding past the wraparound");
1510 tbctrl
->wrap
= trace_buffer_hi
;
1512 tbctrl
->start
= new_start
;
1513 tbctrl
->end_free
= tbctrl
->start
;
1515 trace_debug ("Discarded a traceframe\n"
1516 "Trace buffer [%d], start=%d free=%d "
1517 "endfree=%d wrap=%d hi=%d",
1519 (int) (tbctrl
->start
- trace_buffer_lo
),
1520 (int) (tbctrl
->free
- trace_buffer_lo
),
1521 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1522 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1523 (int) (trace_buffer_hi
- trace_buffer_lo
));
1525 /* Now go back around the loop. The discard might have resulted
1526 in either one or two pieces of free space, so we want to try
1527 both before freeing any more traceframes. */
1531 /* If we get here, we know we can provide the asked-for space. */
1533 rslt
= tbctrl
->free
;
1535 /* Adjust the request back down, now that we know we have space for
1536 the marker, but don't commit to AMT yet, we may still need to
1537 restart the operation if GDBserver touches the trace buffer
1538 (obviously only important in the in-process agent's version). */
1539 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1541 /* Or not. If GDBserver changed the trace buffer behind our back,
1542 we get to restart a new allocation attempt. */
1544 #ifdef IN_PROCESS_AGENT
1545 /* Build the tentative token. */
1546 commit_count
= (((prev
& 0x0007ff00) + 0x100) & 0x0007ff00);
1547 commit
= (((prev
& 0x0007ff00) << 12)
1551 /* Try to commit it. */
1552 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1553 if (readout
!= prev
)
1555 trace_debug ("GDBserver has touched the trace buffer, restarting."
1556 " (prev=%08x, commit=%08x, readout=%08x)",
1557 prev
, commit
, readout
);
1561 /* Hold your horses here. Even if that change was committed,
1562 GDBserver could come in, and clobber it. We need to hold to be
1563 able to tell if GDBserver clobbers before or after we committed
1564 the change. Whenever GDBserver goes about touching the IPA
1565 buffer, it sets a breakpoint in this routine, so we have a sync
1567 about_to_request_buffer_space ();
1569 /* Check if the change has been effective, even if GDBserver stopped
1570 us at the breakpoint. */
1573 unsigned int refetch
;
1577 refetch
= trace_buffer_ctrl_curr
;
1579 if ((refetch
== commit
1580 || ((refetch
& 0x7ff00000) >> 12) == commit_count
))
1583 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1584 "readout=%08x, refetch=%08x)",
1585 prev
, commit
, readout
, refetch
);
1589 trace_debug ("GDBserver has touched the trace buffer, not effective."
1590 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1591 prev
, commit
, readout
, refetch
);
1597 /* We have a new piece of the trace buffer. Hurray! */
1599 /* Add an EOB marker just past this allocation. */
1600 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1601 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1603 /* Adjust the request back down, now that we know we have space for
1605 amt
-= sizeof (struct traceframe
);
1609 trace_debug ("Allocated %d bytes", (int) amt
);
1610 trace_debug ("Trace buffer [%d] start=%d free=%d "
1611 "endfree=%d wrap=%d hi=%d",
1613 (int) (tbctrl
->start
- trace_buffer_lo
),
1614 (int) (tbctrl
->free
- trace_buffer_lo
),
1615 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1616 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1617 (int) (trace_buffer_hi
- trace_buffer_lo
));
1623 #ifndef IN_PROCESS_AGENT
1625 /* Return the total free space. This is not necessarily the largest
1626 block we can allocate, because of the two-part case. */
1631 if (trace_buffer_free
<= trace_buffer_end_free
)
1632 return trace_buffer_end_free
- trace_buffer_free
;
1634 return ((trace_buffer_end_free
- trace_buffer_lo
)
1635 + (trace_buffer_hi
- trace_buffer_free
));
1638 /* An 'S' in continuation packets indicates remainder are for
1641 static int seen_step_action_flag
;
1643 /* Create a tracepoint (location) with given number and address. */
1645 static struct tracepoint
*
1646 add_tracepoint (int num
, CORE_ADDR addr
)
1648 struct tracepoint
*tpoint
;
1650 tpoint
= xmalloc (sizeof (struct tracepoint
));
1651 tpoint
->number
= num
;
1652 tpoint
->address
= addr
;
1653 tpoint
->numactions
= 0;
1654 tpoint
->actions
= NULL
;
1655 tpoint
->actions_str
= NULL
;
1656 tpoint
->cond
= NULL
;
1657 tpoint
->num_step_actions
= 0;
1658 tpoint
->step_actions
= NULL
;
1659 tpoint
->step_actions_str
= NULL
;
1660 /* Start all off as regular (slow) tracepoints. */
1661 tpoint
->type
= trap_tracepoint
;
1662 tpoint
->orig_size
= -1;
1663 tpoint
->source_strings
= NULL
;
1664 tpoint
->compiled_cond
= 0;
1665 tpoint
->handle
= NULL
;
1666 tpoint
->next
= NULL
;
1668 if (!last_tracepoint
)
1669 tracepoints
= tpoint
;
1671 last_tracepoint
->next
= tpoint
;
1672 last_tracepoint
= tpoint
;
1674 seen_step_action_flag
= 0;
1679 #ifndef IN_PROCESS_AGENT
1681 /* Return the tracepoint with the given number and address, or NULL. */
1683 static struct tracepoint
*
1684 find_tracepoint (int id
, CORE_ADDR addr
)
1686 struct tracepoint
*tpoint
;
1688 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1689 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1695 /* There may be several tracepoints with the same number (because they
1696 are "locations", in GDB parlance); return the next one after the
1697 given tracepoint, or search from the beginning of the list if the
1698 first argument is NULL. */
1700 static struct tracepoint
*
1701 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1703 struct tracepoint
*tpoint
;
1706 tpoint
= prev_tp
->next
;
1708 tpoint
= tracepoints
;
1709 for (; tpoint
; tpoint
= tpoint
->next
)
1710 if (tpoint
->number
== num
)
1719 save_string (const char *str
, size_t len
)
1723 s
= xmalloc (len
+ 1);
1724 memcpy (s
, str
, len
);
1730 /* Append another action to perform when the tracepoint triggers. */
1733 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1739 seen_step_action_flag
= 1;
1747 char *act_start
= act
;
1748 struct tracepoint_action
*action
= NULL
;
1754 struct collect_memory_action
*maction
;
1758 maction
= xmalloc (sizeof *maction
);
1759 maction
->base
.type
= *act
;
1760 action
= &maction
->base
;
1763 is_neg
= (*act
== '-');
1766 act
= unpack_varlen_hex (act
, &basereg
);
1768 act
= unpack_varlen_hex (act
, &maction
->addr
);
1770 act
= unpack_varlen_hex (act
, &maction
->len
);
1771 maction
->basereg
= (is_neg
1774 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1775 pulongest (maction
->len
),
1776 paddress (maction
->addr
), maction
->basereg
);
1781 struct collect_registers_action
*raction
;
1783 raction
= xmalloc (sizeof *raction
);
1784 raction
->base
.type
= *act
;
1785 action
= &raction
->base
;
1787 trace_debug ("Want to collect registers");
1789 /* skip past hex digits of mask for now */
1790 while (isxdigit(*act
))
1796 struct collect_static_trace_data_action
*raction
;
1798 raction
= xmalloc (sizeof *raction
);
1799 raction
->base
.type
= *act
;
1800 action
= &raction
->base
;
1802 trace_debug ("Want to collect static trace data");
1807 trace_debug ("Unexpected step action, ignoring");
1812 struct eval_expr_action
*xaction
;
1814 xaction
= xmalloc (sizeof (*xaction
));
1815 xaction
->base
.type
= *act
;
1816 action
= &xaction
->base
;
1818 trace_debug ("Want to evaluate expression");
1819 xaction
->expr
= parse_agent_expr (&act
);
1823 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1832 if (seen_step_action_flag
)
1834 tpoint
->num_step_actions
++;
1836 tpoint
->step_actions
1837 = xrealloc (tpoint
->step_actions
,
1838 (sizeof (*tpoint
->step_actions
)
1839 * tpoint
->num_step_actions
));
1840 tpoint
->step_actions_str
1841 = xrealloc (tpoint
->step_actions_str
,
1842 (sizeof (*tpoint
->step_actions_str
)
1843 * tpoint
->num_step_actions
));
1844 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1845 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1846 = save_string (act_start
, act
- act_start
);
1850 tpoint
->numactions
++;
1852 = xrealloc (tpoint
->actions
,
1853 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1855 = xrealloc (tpoint
->actions_str
,
1856 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1857 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1858 tpoint
->actions_str
[tpoint
->numactions
- 1]
1859 = save_string (act_start
, act
- act_start
);
1866 /* Find or create a trace state variable with the given number. */
1868 static struct trace_state_variable
*
1869 get_trace_state_variable (int num
)
1871 struct trace_state_variable
*tsv
;
1873 #ifdef IN_PROCESS_AGENT
1874 /* Search for an existing variable. */
1875 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1876 if (tsv
->number
== num
)
1880 /* Search for an existing variable. */
1881 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1882 if (tsv
->number
== num
)
1888 /* Find or create a trace state variable with the given number. */
1890 static struct trace_state_variable
*
1891 create_trace_state_variable (int num
, int gdb
)
1893 struct trace_state_variable
*tsv
;
1895 tsv
= get_trace_state_variable (num
);
1899 /* Create a new variable. */
1900 tsv
= xmalloc (sizeof (struct trace_state_variable
));
1902 tsv
->initial_value
= 0;
1906 #ifdef IN_PROCESS_AGENT
1909 tsv
->next
= alloced_trace_state_variables
;
1910 alloced_trace_state_variables
= tsv
;
1915 tsv
->next
= trace_state_variables
;
1916 trace_state_variables
= tsv
;
1921 IP_AGENT_EXPORT LONGEST
1922 get_trace_state_variable_value (int num
)
1924 struct trace_state_variable
*tsv
;
1926 tsv
= get_trace_state_variable (num
);
1930 trace_debug ("No trace state variable %d, skipping value get", num
);
1934 /* Call a getter function if we have one. While it's tempting to
1935 set up something to only call the getter once per tracepoint hit,
1936 it could run afoul of thread races. Better to let the getter
1937 handle it directly, if necessary to worry about it. */
1939 tsv
->value
= (tsv
->getter
) ();
1941 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1942 num
, plongest (tsv
->value
));
1947 IP_AGENT_EXPORT
void
1948 set_trace_state_variable_value (int num
, LONGEST val
)
1950 struct trace_state_variable
*tsv
;
1952 tsv
= get_trace_state_variable (num
);
1956 trace_debug ("No trace state variable %d, skipping value set", num
);
1964 set_trace_state_variable_name (int num
, const char *name
)
1966 struct trace_state_variable
*tsv
;
1968 tsv
= get_trace_state_variable (num
);
1972 trace_debug ("No trace state variable %d, skipping name set", num
);
1976 tsv
->name
= (char *) name
;
1980 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
1982 struct trace_state_variable
*tsv
;
1984 tsv
= get_trace_state_variable (num
);
1988 trace_debug ("No trace state variable %d, skipping getter set", num
);
1992 tsv
->getter
= getter
;
1995 /* Add a raw traceframe for the given tracepoint. */
1997 static struct traceframe
*
1998 add_traceframe (struct tracepoint
*tpoint
)
2000 struct traceframe
*tframe
;
2002 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2007 tframe
->tpnum
= tpoint
->number
;
2008 tframe
->data_size
= 0;
2013 /* Add a block to the traceframe currently being worked on. */
2015 static unsigned char *
2016 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2018 unsigned char *block
;
2023 block
= trace_buffer_alloc (amt
);
2028 tframe
->data_size
+= amt
;
2033 /* Flag that the current traceframe is finished. */
2036 finish_traceframe (struct traceframe
*tframe
)
2038 ++traceframe_write_count
;
2039 ++traceframes_created
;
2042 #ifndef IN_PROCESS_AGENT
2044 /* Given a traceframe number NUM, find the NUMth traceframe in the
2047 static struct traceframe
*
2048 find_traceframe (int num
)
2050 struct traceframe
*tframe
;
2053 for (tframe
= FIRST_TRACEFRAME ();
2055 tframe
= NEXT_TRACEFRAME (tframe
))
2066 get_traceframe_address (struct traceframe
*tframe
)
2069 struct tracepoint
*tpoint
;
2071 addr
= traceframe_get_pc (tframe
);
2076 /* Fallback strategy, will be incorrect for while-stepping frames
2077 and multi-location tracepoints. */
2078 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2079 return tpoint
->address
;
2082 /* Search for the next traceframe whose address is inside or outside
2085 static struct traceframe
*
2086 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2089 struct traceframe
*tframe
;
2092 *tfnump
= current_traceframe
+ 1;
2093 tframe
= find_traceframe (*tfnump
);
2094 /* The search is not supposed to wrap around. */
2101 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2103 tfaddr
= get_traceframe_address (tframe
);
2105 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2106 : (lo
> tfaddr
|| tfaddr
> hi
))
2115 /* Search for the next traceframe recorded by the given tracepoint.
2116 Note that for multi-location tracepoints, this will find whatever
2117 location appears first. */
2119 static struct traceframe
*
2120 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2122 struct traceframe
*tframe
;
2124 *tfnump
= current_traceframe
+ 1;
2125 tframe
= find_traceframe (*tfnump
);
2126 /* The search is not supposed to wrap around. */
2133 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2135 if (tframe
->tpnum
== num
)
2146 #ifndef IN_PROCESS_AGENT
2148 /* Clear all past trace state. */
2151 cmd_qtinit (char *packet
)
2153 struct trace_state_variable
*tsv
, *prev
, *next
;
2155 /* Make sure we don't try to read from a trace frame. */
2156 current_traceframe
= -1;
2158 trace_debug ("Initializing the trace");
2160 clear_installed_tracepoints ();
2161 clear_readonly_regions ();
2164 last_tracepoint
= NULL
;
2166 /* Clear out any leftover trace state variables. Ones with target
2167 defined getters should be kept however. */
2169 tsv
= trace_state_variables
;
2172 trace_debug ("Looking at var %d", tsv
->number
);
2173 if (tsv
->getter
== NULL
)
2179 trace_state_variables
= next
;
2180 trace_debug ("Deleting var %d", tsv
->number
);
2191 clear_trace_buffer ();
2192 clear_inferior_trace_buffer ();
2197 /* Unprobe the UST marker at ADDRESS. */
2200 unprobe_marker_at (CORE_ADDR address
)
2202 char cmd
[CMD_BUF_SIZE
];
2204 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2205 run_inferior_command (cmd
);
2208 /* Restore the program to its pre-tracing state. This routine may be called
2209 in error situations, so it needs to be careful about only restoring
2210 from known-valid bits. */
2213 clear_installed_tracepoints (void)
2215 struct tracepoint
*tpoint
;
2216 struct tracepoint
*prev_stpoint
;
2219 cancel_breakpoints ();
2221 prev_stpoint
= NULL
;
2223 /* Restore any bytes overwritten by tracepoints. */
2224 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2226 /* Catch the case where we might try to remove a tracepoint that
2227 was never actually installed. */
2228 if (tpoint
->handle
== NULL
)
2230 trace_debug ("Tracepoint %d at 0x%s was "
2231 "never installed, nothing to clear",
2232 tpoint
->number
, paddress (tpoint
->address
));
2236 switch (tpoint
->type
)
2238 case trap_tracepoint
:
2239 delete_breakpoint (tpoint
->handle
);
2241 case fast_tracepoint
:
2242 delete_fast_tracepoint_jump (tpoint
->handle
);
2244 case static_tracepoint
:
2245 if (prev_stpoint
!= NULL
2246 && prev_stpoint
->address
== tpoint
->address
)
2247 /* Nothing to do. We already unprobed a tracepoint set at
2248 this marker address (and there can only be one probe
2253 unprobe_marker_at (tpoint
->address
);
2254 prev_stpoint
= tpoint
;
2259 tpoint
->handle
= NULL
;
2265 /* Parse a packet that defines a tracepoint. */
2268 cmd_qtdp (char *own_buf
)
2274 struct tracepoint
*tpoint
;
2276 char *packet
= own_buf
;
2278 packet
+= strlen ("QTDP:");
2280 /* A hyphen at the beginning marks a packet specifying actions for a
2281 tracepoint already supplied. */
2288 packet
= unpack_varlen_hex (packet
, &num
);
2289 ++packet
; /* skip a colon */
2290 packet
= unpack_varlen_hex (packet
, &addr
);
2291 ++packet
; /* skip a colon */
2293 /* See if we already have this tracepoint. */
2294 tpoint
= find_tracepoint (num
, addr
);
2298 /* Duplicate tracepoints are never allowed. */
2301 trace_debug ("Tracepoint error: tracepoint %d"
2302 " at 0x%s already exists",
2303 (int) num
, paddress (addr
));
2304 write_enn (own_buf
);
2308 tpoint
= add_tracepoint (num
, addr
);
2310 tpoint
->enabled
= (*packet
== 'E');
2311 ++packet
; /* skip 'E' */
2312 ++packet
; /* skip a colon */
2313 packet
= unpack_varlen_hex (packet
, &count
);
2314 tpoint
->step_count
= count
;
2315 ++packet
; /* skip a colon */
2316 packet
= unpack_varlen_hex (packet
, &count
);
2317 tpoint
->pass_count
= count
;
2318 /* See if we have any of the additional optional fields. */
2319 while (*packet
== ':')
2324 tpoint
->type
= fast_tracepoint
;
2326 packet
= unpack_varlen_hex (packet
, &count
);
2327 tpoint
->orig_size
= count
;
2329 else if (*packet
== 'S')
2331 tpoint
->type
= static_tracepoint
;
2334 else if (*packet
== 'X')
2336 actparm
= (char *) packet
;
2337 tpoint
->cond
= parse_agent_expr (&actparm
);
2340 else if (*packet
== '-')
2342 else if (*packet
== '\0')
2345 trace_debug ("Unknown optional tracepoint field");
2348 trace_debug ("Also has actions\n");
2350 trace_debug ("Defined %stracepoint %d at 0x%s, "
2351 "enabled %d step %ld pass %ld",
2352 tpoint
->type
== fast_tracepoint
? "fast "
2354 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2355 tpoint
->step_count
, tpoint
->pass_count
);
2358 add_tracepoint_action (tpoint
, packet
);
2361 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2362 (int) num
, paddress (addr
));
2363 write_enn (own_buf
);
2371 cmd_qtdpsrc (char *own_buf
)
2373 ULONGEST num
, addr
, start
, slen
;
2374 struct tracepoint
*tpoint
;
2375 char *packet
= own_buf
;
2376 char *saved
, *srctype
, *src
;
2378 struct source_string
*last
, *newlast
;
2380 packet
+= strlen ("QTDPsrc:");
2382 packet
= unpack_varlen_hex (packet
, &num
);
2383 ++packet
; /* skip a colon */
2384 packet
= unpack_varlen_hex (packet
, &addr
);
2385 ++packet
; /* skip a colon */
2387 /* See if we already have this tracepoint. */
2388 tpoint
= find_tracepoint (num
, addr
);
2392 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2393 (int) num
, paddress (addr
));
2394 write_enn (own_buf
);
2399 packet
= strchr (packet
, ':');
2400 srctype
= xmalloc (packet
- saved
+ 1);
2401 memcpy (srctype
, saved
, packet
- saved
);
2402 srctype
[packet
- saved
] = '\0';
2404 packet
= unpack_varlen_hex (packet
, &start
);
2405 ++packet
; /* skip a colon */
2406 packet
= unpack_varlen_hex (packet
, &slen
);
2407 ++packet
; /* skip a colon */
2408 src
= xmalloc (slen
+ 1);
2409 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2412 newlast
= xmalloc (sizeof (struct source_string
));
2413 newlast
->type
= srctype
;
2415 newlast
->next
= NULL
;
2416 /* Always add a source string to the end of the list;
2417 this keeps sequences of actions/commands in the right
2419 if (tpoint
->source_strings
)
2421 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2423 last
->next
= newlast
;
2426 tpoint
->source_strings
= newlast
;
2432 cmd_qtdv (char *own_buf
)
2434 ULONGEST num
, val
, builtin
;
2437 struct trace_state_variable
*tsv
;
2438 char *packet
= own_buf
;
2440 packet
+= strlen ("QTDV:");
2442 packet
= unpack_varlen_hex (packet
, &num
);
2443 ++packet
; /* skip a colon */
2444 packet
= unpack_varlen_hex (packet
, &val
);
2445 ++packet
; /* skip a colon */
2446 packet
= unpack_varlen_hex (packet
, &builtin
);
2447 ++packet
; /* skip a colon */
2449 nbytes
= strlen (packet
) / 2;
2450 varname
= xmalloc (nbytes
+ 1);
2451 nbytes
= unhexify (varname
, packet
, nbytes
);
2452 varname
[nbytes
] = '\0';
2454 tsv
= create_trace_state_variable (num
, 1);
2455 tsv
->initial_value
= (LONGEST
) val
;
2456 tsv
->name
= varname
;
2458 set_trace_state_variable_value (num
, (LONGEST
) val
);
2464 cmd_qtenable_disable (char *own_buf
, int enable
)
2466 char *packet
= own_buf
;
2468 struct tracepoint
*tp
;
2470 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2471 packet
= unpack_varlen_hex (packet
, &num
);
2472 ++packet
; /* skip a colon */
2473 packet
= unpack_varlen_hex (packet
, &addr
);
2475 tp
= find_tracepoint (num
, addr
);
2479 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2481 trace_debug ("Tracepoint %d at 0x%s is already %s",
2482 (int) num
, paddress (addr
),
2483 enable
? "enabled" : "disabled");
2488 trace_debug ("%s tracepoint %d at 0x%s",
2489 enable
? "Enabling" : "Disabling",
2490 (int) num
, paddress (addr
));
2492 tp
->enabled
= enable
;
2494 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2497 int offset
= offsetof (struct tracepoint
, enabled
);
2498 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2500 ret
= prepare_to_access_memory ();
2503 trace_debug ("Failed to temporarily stop inferior threads");
2504 write_enn (own_buf
);
2508 ret
= write_inferior_integer (obj_addr
, enable
);
2509 done_accessing_memory ();
2513 trace_debug ("Cannot write enabled flag into "
2514 "inferior process memory");
2515 write_enn (own_buf
);
2524 trace_debug ("Tracepoint %d at 0x%s not found",
2525 (int) num
, paddress (addr
));
2526 write_enn (own_buf
);
2531 cmd_qtv (char *own_buf
)
2536 char *packet
= own_buf
;
2538 packet
+= strlen ("qTV:");
2539 unpack_varlen_hex (packet
, &num
);
2541 if (current_traceframe
>= 0)
2543 err
= traceframe_read_tsv ((int) num
, &val
);
2546 strcpy (own_buf
, "U");
2550 /* Only make tsv's be undefined before the first trace run. After a
2551 trace run is over, the user might want to see the last value of
2552 the tsv, and it might not be available in a traceframe. */
2553 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2555 strcpy (own_buf
, "U");
2559 val
= get_trace_state_variable_value (num
);
2561 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2564 /* Clear out the list of readonly regions. */
2567 clear_readonly_regions (void)
2569 struct readonly_region
*roreg
;
2571 while (readonly_regions
)
2573 roreg
= readonly_regions
;
2574 readonly_regions
= readonly_regions
->next
;
2579 /* Parse the collection of address ranges whose contents GDB believes
2580 to be unchanging and so can be read directly from target memory
2581 even while looking at a traceframe. */
2584 cmd_qtro (char *own_buf
)
2586 ULONGEST start
, end
;
2587 struct readonly_region
*roreg
;
2588 char *packet
= own_buf
;
2590 trace_debug ("Want to mark readonly regions");
2592 clear_readonly_regions ();
2594 packet
+= strlen ("QTro");
2596 while (*packet
== ':')
2598 ++packet
; /* skip a colon */
2599 packet
= unpack_varlen_hex (packet
, &start
);
2600 ++packet
; /* skip a comma */
2601 packet
= unpack_varlen_hex (packet
, &end
);
2602 roreg
= xmalloc (sizeof (struct readonly_region
));
2603 roreg
->start
= start
;
2605 roreg
->next
= readonly_regions
;
2606 readonly_regions
= roreg
;
2607 trace_debug ("Added readonly region from 0x%s to 0x%s",
2608 paddress (roreg
->start
), paddress (roreg
->end
));
2614 /* Test to see if the given range is in our list of readonly ranges.
2615 We only test for being entirely within a range, GDB is not going to
2616 send a single memory packet that spans multiple regions. */
2619 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2621 struct readonly_region
*roreg
;
2623 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2624 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2630 /* The maximum size of a jump pad entry. */
2631 static const int max_jump_pad_size
= 0x100;
2633 static CORE_ADDR gdb_jump_pad_head
;
2635 /* Return the address of the next free jump space. */
2638 get_jump_space_head (void)
2640 if (gdb_jump_pad_head
== 0)
2642 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2643 &gdb_jump_pad_head
))
2644 fatal ("error extracting jump_pad_buffer");
2647 return gdb_jump_pad_head
;
2650 /* Reserve USED bytes from the jump space. */
2653 claim_jump_space (ULONGEST used
)
2655 trace_debug ("claim_jump_space reserves %s bytes at %s",
2656 pulongest (used
), paddress (gdb_jump_pad_head
));
2657 gdb_jump_pad_head
+= used
;
2660 /* Sort tracepoints by PC, using a bubble sort. */
2663 sort_tracepoints (void)
2665 struct tracepoint
*lst
, *tmp
, *prev
= NULL
;
2668 if (tracepoints
== NULL
)
2672 for (tmp
= tracepoints
; tmp
->next
; tmp
= tmp
->next
)
2675 for (i
= 0; i
< n
- 1; i
++)
2676 for (j
= 0, lst
= tracepoints
;
2677 lst
&& lst
->next
&& (j
<= n
- 1 - i
);
2680 /* If we're at beginning, the start node is the prev
2685 /* Compare neighbors. */
2686 if (lst
->next
->address
< lst
->address
)
2688 struct tracepoint
*p
;
2691 tmp
= (lst
->next
? lst
->next
->next
: NULL
);
2693 if (j
== 0 && prev
== tracepoints
)
2694 tracepoints
= lst
->next
;
2697 prev
->next
= lst
->next
;
2698 lst
->next
->next
= lst
;
2705 /* Keep track of the previous node. We need it if we need
2713 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2714 the command fails, or 0 otherwise. If the command ran
2715 successfully, but probing the marker failed, ERROUT will be filled
2716 with the error to reply to GDB, and -1 is also returned. This
2717 allows directly passing IPA errors to GDB. */
2720 probe_marker_at (CORE_ADDR address
, char *errout
)
2722 char cmd
[CMD_BUF_SIZE
];
2725 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
2726 err
= run_inferior_command (cmd
);
2732 strcpy (errout
, cmd
);
2740 #define MAX_JUMP_SIZE 20
2743 cmd_qtstart (char *packet
)
2745 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
2746 CORE_ADDR jump_entry
;
2748 /* The jump to the jump pad of the last fast tracepoint
2750 unsigned char fjump
[MAX_JUMP_SIZE
];
2751 ULONGEST fjump_size
;
2753 trace_debug ("Starting the trace");
2755 /* Sort tracepoints by ascending address. This makes installing
2756 fast tracepoints at the same address easier to handle. */
2757 sort_tracepoints ();
2759 /* Pause all threads temporarily while we patch tracepoints. */
2762 /* Get threads out of jump pads. Safe to do here, since this is a
2763 top level command. And, required to do here, since we're
2764 deleting/rewriting jump pads. */
2766 stabilize_threads ();
2768 /* Freeze threads. */
2771 /* Sync the fast tracepoints list in the inferior ftlib. */
2772 if (in_process_agent_loaded ())
2774 download_tracepoints ();
2775 download_trace_state_variables ();
2778 /* No previous fast tpoint yet. */
2779 prev_ftpoint
= NULL
;
2781 /* No previous static tpoint yet. */
2782 prev_stpoint
= NULL
;
2786 /* Install tracepoints. */
2787 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2789 /* Ensure all the hit counts start at zero. */
2790 tpoint
->hit_count
= 0;
2792 if (tpoint
->type
== trap_tracepoint
)
2794 /* Tracepoints are installed as memory breakpoints. Just go
2795 ahead and install the trap. The breakpoints module
2796 handles duplicated breakpoints, and the memory read
2797 routine handles un-patching traps from memory reads. */
2798 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
2799 tracepoint_handler
);
2801 else if (tpoint
->type
== fast_tracepoint
)
2803 if (maybe_write_ipa_not_loaded (packet
))
2805 trace_debug ("Requested a fast tracepoint, but fast "
2806 "tracepoints aren't supported.");
2810 if (prev_ftpoint
!= NULL
&& prev_ftpoint
->address
== tpoint
->address
)
2812 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
,
2815 tpoint
->jump_pad
= prev_ftpoint
->jump_pad
;
2816 tpoint
->jump_pad_end
= prev_ftpoint
->jump_pad_end
;
2817 tpoint
->adjusted_insn_addr
= prev_ftpoint
->adjusted_insn_addr
;
2818 tpoint
->adjusted_insn_addr_end
2819 = prev_ftpoint
->adjusted_insn_addr_end
;
2826 prev_ftpoint
= NULL
;
2828 jentry
= jump_entry
= get_jump_space_head ();
2830 /* Install the jump pad. */
2831 err
= install_fast_tracepoint_jump_pad
2832 (tpoint
->obj_addr_on_target
,
2834 ipa_sym_addrs
.addr_gdb_collect
,
2835 ipa_sym_addrs
.addr_collecting
,
2839 &tpoint
->adjusted_insn_addr
,
2840 &tpoint
->adjusted_insn_addr_end
);
2844 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
,
2847 if (tpoint
->handle
!= NULL
)
2849 tpoint
->jump_pad
= jump_entry
;
2850 tpoint
->jump_pad_end
= jentry
;
2852 /* Pad to 8-byte alignment. */
2853 jentry
= ((jentry
+ 7) & ~0x7);
2854 claim_jump_space (jentry
- jump_entry
);
2856 /* So that we can handle multiple fast tracepoints
2857 at the same address easily. */
2858 prev_ftpoint
= tpoint
;
2862 else if (tpoint
->type
== static_tracepoint
)
2864 if (maybe_write_ipa_ust_not_loaded (packet
))
2866 trace_debug ("Requested a static tracepoint, but static "
2867 "tracepoints are not supported.");
2871 /* Can only probe a given marker once. */
2872 if (prev_stpoint
!= NULL
&& prev_stpoint
->address
== tpoint
->address
)
2874 tpoint
->handle
= (void *) -1;
2878 if (probe_marker_at (tpoint
->address
, packet
) == 0)
2880 tpoint
->handle
= (void *) -1;
2882 /* So that we can handle multiple static tracepoints
2883 at the same address easily. */
2884 prev_stpoint
= tpoint
;
2889 /* Any failure in the inner loop is sufficient cause to give
2891 if (tpoint
->handle
== NULL
)
2895 /* Any error in tracepoint insertion is unacceptable; better to
2896 address the problem now, than end up with a useless or misleading
2900 clear_installed_tracepoints ();
2901 if (*packet
== '\0')
2907 stopping_tracepoint
= NULL
;
2908 trace_buffer_is_full
= 0;
2909 expr_eval_result
= expr_eval_no_error
;
2910 error_tracepoint
= NULL
;
2912 /* Tracing is now active, hits will now start being logged. */
2915 if (in_process_agent_loaded ())
2917 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
2918 fatal ("Error setting tracing variable in lib");
2920 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
2922 fatal ("Error clearing stopping_tracepoint variable in lib");
2924 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
2925 fatal ("Error clearing trace_buffer_is_full variable in lib");
2927 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
2928 stop_tracing_handler
);
2929 if (stop_tracing_bkpt
== NULL
)
2930 error ("Error setting stop_tracing breakpoint");
2932 flush_trace_buffer_bkpt
2933 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
2934 flush_trace_buffer_handler
);
2935 if (flush_trace_buffer_bkpt
== NULL
)
2936 error ("Error setting flush_trace_buffer breakpoint");
2944 /* End a tracing run, filling in a stop reason to report back to GDB,
2945 and removing the tracepoints from the code. */
2952 trace_debug ("Tracing is already off, ignoring");
2956 trace_debug ("Stopping the trace");
2958 /* Pause all threads before removing fast jumps from memory,
2959 breakpoints, and touching IPA state variables (inferior memory).
2960 Some thread may hit the internal tracing breakpoints, or be
2961 collecting this moment, but that's ok, we don't release the
2962 tpoint object's memory or the jump pads here (we only do that
2963 when we're sure we can move all threads out of the jump pads).
2964 We can't now, since we may be getting here due to the inferior
2965 agent calling us. */
2967 /* Since we're removing breakpoints, cancel breakpoint hits,
2968 possibly related to the breakpoints we're about to delete. */
2969 cancel_breakpoints ();
2971 /* Stop logging. Tracepoints can still be hit, but they will not be
2974 if (in_process_agent_loaded ())
2976 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
2977 fatal ("Error clearing tracing variable in lib");
2980 tracing_stop_reason
= "t???";
2981 tracing_stop_tpnum
= 0;
2982 if (stopping_tracepoint
)
2984 trace_debug ("Stopping the trace because "
2985 "tracepoint %d was hit %ld times",
2986 stopping_tracepoint
->number
,
2987 stopping_tracepoint
->pass_count
);
2988 tracing_stop_reason
= "tpasscount";
2989 tracing_stop_tpnum
= stopping_tracepoint
->number
;
2991 else if (trace_buffer_is_full
)
2993 trace_debug ("Stopping the trace because the trace buffer is full");
2994 tracing_stop_reason
= "tfull";
2996 else if (expr_eval_result
!= expr_eval_no_error
)
2998 trace_debug ("Stopping the trace because of an expression eval error");
2999 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3000 tracing_stop_tpnum
= error_tracepoint
->number
;
3002 #ifndef IN_PROCESS_AGENT
3003 else if (!gdb_connected ())
3005 trace_debug ("Stopping the trace because GDB disconnected");
3006 tracing_stop_reason
= "tdisconnected";
3011 trace_debug ("Stopping the trace because of a tstop command");
3012 tracing_stop_reason
= "tstop";
3015 stopping_tracepoint
= NULL
;
3016 error_tracepoint
= NULL
;
3018 /* Clear out the tracepoints. */
3019 clear_installed_tracepoints ();
3021 if (in_process_agent_loaded ())
3023 /* Pull in fast tracepoint trace frames from the inferior lib
3024 buffer into our buffer, even if our buffer is already full,
3025 because we want to present the full number of created frames
3026 in addition to what fit in the trace buffer. */
3027 upload_fast_traceframes ();
3030 if (stop_tracing_bkpt
!= NULL
)
3032 delete_breakpoint (stop_tracing_bkpt
);
3033 stop_tracing_bkpt
= NULL
;
3036 if (flush_trace_buffer_bkpt
!= NULL
)
3038 delete_breakpoint (flush_trace_buffer_bkpt
);
3039 flush_trace_buffer_bkpt
= NULL
;
3046 stop_tracing_handler (CORE_ADDR addr
)
3048 trace_debug ("lib hit stop_tracing");
3050 /* Don't actually handle it here. When we stop tracing we remove
3051 breakpoints from the inferior, and that is not allowed in a
3052 breakpoint handler (as the caller is walking the breakpoint
3058 flush_trace_buffer_handler (CORE_ADDR addr
)
3060 trace_debug ("lib hit flush_trace_buffer");
3065 cmd_qtstop (char *packet
)
3072 cmd_qtdisconnected (char *own_buf
)
3075 char *packet
= own_buf
;
3077 packet
+= strlen ("QTDisconnected:");
3079 unpack_varlen_hex (packet
, &setting
);
3083 disconnected_tracing
= setting
;
3087 cmd_qtframe (char *own_buf
)
3089 ULONGEST frame
, pc
, lo
, hi
, num
;
3091 struct traceframe
*tframe
;
3092 char *packet
= own_buf
;
3094 packet
+= strlen ("QTFrame:");
3096 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3098 packet
+= strlen ("pc:");
3099 unpack_varlen_hex (packet
, &pc
);
3100 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3101 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3103 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3105 packet
+= strlen ("range:");
3106 packet
= unpack_varlen_hex (packet
, &lo
);
3108 unpack_varlen_hex (packet
, &hi
);
3109 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3110 paddress (lo
), paddress (hi
));
3111 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3113 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3115 packet
+= strlen ("outside:");
3116 packet
= unpack_varlen_hex (packet
, &lo
);
3118 unpack_varlen_hex (packet
, &hi
);
3119 trace_debug ("Want to find next traceframe "
3120 "outside the range 0x%s to 0x%s",
3121 paddress (lo
), paddress (hi
));
3122 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3124 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3126 packet
+= strlen ("tdp:");
3127 unpack_varlen_hex (packet
, &num
);
3129 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3130 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3134 unpack_varlen_hex (packet
, &frame
);
3135 tfnum
= (int) frame
;
3138 trace_debug ("Want to stop looking at traceframes");
3139 current_traceframe
= -1;
3143 trace_debug ("Want to look at traceframe %d", tfnum
);
3144 tframe
= find_traceframe (tfnum
);
3149 current_traceframe
= tfnum
;
3150 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3153 sprintf (own_buf
, "F-1");
3157 cmd_qtstatus (char *packet
)
3159 char *stop_reason_rsp
= NULL
;
3161 trace_debug ("Returning trace status as %d, stop reason %s",
3162 tracing
, tracing_stop_reason
);
3164 if (in_process_agent_loaded ())
3168 upload_fast_traceframes ();
3173 stop_reason_rsp
= (char *) tracing_stop_reason
;
3175 /* The user visible error string in terror needs to be hex encoded.
3176 We leave it as plain string in `tracepoint_stop_reason' to ease
3178 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3180 const char *result_name
;
3184 result_name
= stop_reason_rsp
+ strlen ("terror:");
3185 hexstr_len
= strlen (result_name
) * 2;
3186 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3187 strcpy (p
, "terror:");
3189 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3195 "tframes:%x;tcreated:%x;"
3196 "tfree:%x;tsize:%s;"
3200 stop_reason_rsp
, tracing_stop_tpnum
,
3201 traceframe_count
, traceframes_created
,
3202 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3203 circular_trace_buffer
,
3204 disconnected_tracing
);
3207 /* State variables to help return all the tracepoint bits. */
3208 static struct tracepoint
*cur_tpoint
;
3209 static int cur_action
;
3210 static int cur_step_action
;
3211 static struct source_string
*cur_source_string
;
3212 static struct trace_state_variable
*cur_tsv
;
3214 /* Compose a response that is an imitation of the syntax by which the
3215 tracepoint was originally downloaded. */
3218 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3222 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
3223 paddress (tpoint
->address
),
3224 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3225 tpoint
->pass_count
);
3226 if (tpoint
->type
== fast_tracepoint
)
3227 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3228 else if (tpoint
->type
== static_tracepoint
)
3229 sprintf (packet
+ strlen (packet
), ":S");
3233 buf
= unparse_agent_expr (tpoint
->cond
);
3234 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3235 tpoint
->cond
->length
, buf
);
3240 /* Compose a response that is an imitation of the syntax by which the
3241 tracepoint action was originally downloaded (with the difference
3242 that due to the way we store the actions, this will output a packet
3243 per action, while GDB could have combined more than one action
3247 response_action (char *packet
, struct tracepoint
*tpoint
,
3248 char *taction
, int step
)
3250 sprintf (packet
, "%c%x:%s:%s",
3251 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3255 /* Compose a response that is an imitation of the syntax by which the
3256 tracepoint source piece was originally downloaded. */
3259 response_source (char *packet
,
3260 struct tracepoint
*tpoint
, struct source_string
*src
)
3265 len
= strlen (src
->str
);
3266 buf
= alloca (len
* 2 + 1);
3267 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3269 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3270 tpoint
->number
, paddress (tpoint
->address
),
3271 src
->type
, 0, len
, buf
);
3274 /* Return the first piece of tracepoint definition, and initialize the
3275 state machine that will iterate through all the tracepoint
3279 cmd_qtfp (char *packet
)
3281 trace_debug ("Returning first tracepoint definition piece");
3283 cur_tpoint
= tracepoints
;
3284 cur_action
= cur_step_action
= -1;
3285 cur_source_string
= NULL
;
3288 response_tracepoint (packet
, cur_tpoint
);
3290 strcpy (packet
, "l");
3293 /* Return additional pieces of tracepoint definition. Each action and
3294 stepping action must go into its own packet, because of packet size
3295 limits, and so we use state variables to deliver one piece at a
3299 cmd_qtsp (char *packet
)
3301 trace_debug ("Returning subsequent tracepoint definition piece");
3305 /* This case would normally never occur, but be prepared for
3307 strcpy (packet
, "l");
3309 else if (cur_action
< cur_tpoint
->numactions
- 1)
3312 response_action (packet
, cur_tpoint
,
3313 cur_tpoint
->actions_str
[cur_action
], 0);
3315 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3318 response_action (packet
, cur_tpoint
,
3319 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3321 else if ((cur_source_string
3322 ? cur_source_string
->next
3323 : cur_tpoint
->source_strings
))
3325 if (cur_source_string
)
3326 cur_source_string
= cur_source_string
->next
;
3328 cur_source_string
= cur_tpoint
->source_strings
;
3329 response_source (packet
, cur_tpoint
, cur_source_string
);
3333 cur_tpoint
= cur_tpoint
->next
;
3334 cur_action
= cur_step_action
= -1;
3335 cur_source_string
= NULL
;
3337 response_tracepoint (packet
, cur_tpoint
);
3339 strcpy (packet
, "l");
3343 /* Compose a response that is an imitation of the syntax by which the
3344 trace state variable was originally downloaded. */
3347 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3349 char *buf
= (char *) "";
3354 namelen
= strlen (tsv
->name
);
3355 buf
= alloca (namelen
* 2 + 1);
3356 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3359 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3360 tsv
->getter
? 1 : 0, buf
);
3363 /* Return the first trace state variable definition, and initialize
3364 the state machine that will iterate through all the tsv bits. */
3367 cmd_qtfv (char *packet
)
3369 trace_debug ("Returning first trace state variable definition");
3371 cur_tsv
= trace_state_variables
;
3374 response_tsv (packet
, cur_tsv
);
3376 strcpy (packet
, "l");
3379 /* Return additional trace state variable definitions. */
3382 cmd_qtsv (char *packet
)
3384 trace_debug ("Returning first trace state variable definition");
3388 /* This case would normally never occur, but be prepared for
3390 strcpy (packet
, "l");
3394 cur_tsv
= cur_tsv
->next
;
3396 response_tsv (packet
, cur_tsv
);
3398 strcpy (packet
, "l");
3401 strcpy (packet
, "l");
3404 /* Return the first static tracepoint marker, and initialize the state
3405 machine that will iterate through all the static tracepoints
3409 cmd_qtfstm (char *packet
)
3411 if (!maybe_write_ipa_ust_not_loaded (packet
))
3412 run_inferior_command (packet
);
3415 /* Return additional static tracepoints markers. */
3418 cmd_qtsstm (char *packet
)
3420 if (!maybe_write_ipa_ust_not_loaded (packet
))
3421 run_inferior_command (packet
);
3424 /* Return the definition of the static tracepoint at a given address.
3425 Result packet is the same as qTsST's. */
3428 cmd_qtstmat (char *packet
)
3430 if (!maybe_write_ipa_ust_not_loaded (packet
))
3431 run_inferior_command (packet
);
3434 /* Respond to qTBuffer packet with a block of raw data from the trace
3435 buffer. GDB may ask for a lot, but we are allowed to reply with
3436 only as much as will fit within packet limits or whatever. */
3439 cmd_qtbuffer (char *own_buf
)
3441 ULONGEST offset
, num
, tot
;
3443 char *packet
= own_buf
;
3445 packet
+= strlen ("qTBuffer:");
3447 packet
= unpack_varlen_hex (packet
, &offset
);
3448 ++packet
; /* skip a comma */
3449 unpack_varlen_hex (packet
, &num
);
3451 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3452 (int) num
, pulongest (offset
));
3454 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3456 /* If we're right at the end, reply specially that we're done. */
3459 strcpy (own_buf
, "l");
3463 /* Object to any other out-of-bounds request. */
3466 write_enn (own_buf
);
3470 /* Compute the pointer corresponding to the given offset, accounting
3472 tbp
= trace_buffer_start
+ offset
;
3473 if (tbp
>= trace_buffer_wrap
)
3474 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3476 /* Trim to the remaining bytes if we're close to the end. */
3477 if (num
> tot
- offset
)
3480 /* Trim to available packet size. */
3481 if (num
>= (PBUFSIZ
- 16) / 2 )
3482 num
= (PBUFSIZ
- 16) / 2;
3484 convert_int_to_ascii (tbp
, own_buf
, num
);
3485 own_buf
[num
] = '\0';
3489 cmd_bigqtbuffer (char *own_buf
)
3492 char *packet
= own_buf
;
3494 packet
+= strlen ("QTBuffer:");
3496 if (strncmp ("circular:", packet
, strlen ("circular:")) == 0)
3498 packet
+= strlen ("circular:");
3499 unpack_varlen_hex (packet
, &val
);
3500 circular_trace_buffer
= val
;
3501 trace_debug ("Trace buffer is now %s",
3502 circular_trace_buffer
? "circular" : "linear");
3506 write_enn (own_buf
);
3510 handle_tracepoint_general_set (char *packet
)
3512 if (strcmp ("QTinit", packet
) == 0)
3514 cmd_qtinit (packet
);
3517 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3522 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3524 cmd_qtdpsrc (packet
);
3527 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
3529 cmd_qtenable_disable (packet
, 1);
3532 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
3534 cmd_qtenable_disable (packet
, 0);
3537 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3542 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3547 else if (strcmp ("QTStart", packet
) == 0)
3549 cmd_qtstart (packet
);
3552 else if (strcmp ("QTStop", packet
) == 0)
3554 cmd_qtstop (packet
);
3557 else if (strncmp ("QTDisconnected:", packet
,
3558 strlen ("QTDisconnected:")) == 0)
3560 cmd_qtdisconnected (packet
);
3563 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3565 cmd_qtframe (packet
);
3568 else if (strncmp ("QTBuffer:", packet
, strlen ("QTBuffer:")) == 0)
3570 cmd_bigqtbuffer (packet
);
3578 handle_tracepoint_query (char *packet
)
3580 if (strcmp ("qTStatus", packet
) == 0)
3582 cmd_qtstatus (packet
);
3585 else if (strcmp ("qTfP", packet
) == 0)
3590 else if (strcmp ("qTsP", packet
) == 0)
3595 else if (strcmp ("qTfV", packet
) == 0)
3600 else if (strcmp ("qTsV", packet
) == 0)
3605 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
3610 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
3612 cmd_qtbuffer (packet
);
3615 else if (strcmp ("qTfSTM", packet
) == 0)
3617 cmd_qtfstm (packet
);
3620 else if (strcmp ("qTsSTM", packet
) == 0)
3622 cmd_qtsstm (packet
);
3625 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
3627 cmd_qtstmat (packet
);
3635 #ifndef IN_PROCESS_AGENT
3637 /* Call this when thread TINFO has hit the tracepoint defined by
3638 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3639 action. This adds a while-stepping collecting state item to the
3640 threads' collecting state list, so that we can keep track of
3641 multiple simultaneous while-stepping actions being collected by the
3642 same thread. This can happen in cases like:
3644 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3646 ff0003 INSN3 <-- TP2, collect $regs
3647 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3650 Notice that when instruction INSN5 is reached, the while-stepping
3651 actions of both TP1 and TP3 are still being collected, and that TP2
3652 had been collected meanwhile. The whole range of ff0001-ff0005
3653 should be single-stepped, due to at least TP1's while-stepping
3654 action covering the whole range. */
3657 add_while_stepping_state (struct thread_info
*tinfo
,
3658 int tp_number
, CORE_ADDR tp_address
)
3660 struct wstep_state
*wstep
;
3662 wstep
= xmalloc (sizeof (*wstep
));
3663 wstep
->next
= tinfo
->while_stepping
;
3665 wstep
->tp_number
= tp_number
;
3666 wstep
->tp_address
= tp_address
;
3667 wstep
->current_step
= 0;
3669 tinfo
->while_stepping
= wstep
;
3672 /* Release the while-stepping collecting state WSTEP. */
3675 release_while_stepping_state (struct wstep_state
*wstep
)
3680 /* Release all while-stepping collecting states currently associated
3681 with thread TINFO. */
3684 release_while_stepping_state_list (struct thread_info
*tinfo
)
3686 struct wstep_state
*head
;
3688 while (tinfo
->while_stepping
)
3690 head
= tinfo
->while_stepping
;
3691 tinfo
->while_stepping
= head
->next
;
3692 release_while_stepping_state (head
);
3696 /* If TINFO was handling a 'while-stepping' action, the step has
3697 finished, so collect any step data needed, and check if any more
3698 steps are required. Return true if the thread was indeed
3699 collecting tracepoint data, false otherwise. */
3702 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3704 struct tracepoint
*tpoint
;
3705 struct wstep_state
*wstep
;
3706 struct wstep_state
**wstep_link
;
3707 struct trap_tracepoint_ctx ctx
;
3709 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
3711 if (in_process_agent_loaded ())
3712 upload_fast_traceframes ();
3714 /* Check if we were indeed collecting data for one of more
3715 tracepoints with a 'while-stepping' count. */
3716 if (tinfo
->while_stepping
== NULL
)
3721 /* We're not even tracing anymore. Stop this thread from
3723 release_while_stepping_state_list (tinfo
);
3725 /* The thread had stopped due to a single-step request indeed
3726 explained by a tracepoint. */
3730 wstep
= tinfo
->while_stepping
;
3731 wstep_link
= &tinfo
->while_stepping
;
3733 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
3734 target_pid_to_str (tinfo
->entry
.id
),
3735 wstep
->tp_number
, paddress (wstep
->tp_address
));
3737 ctx
.base
.type
= trap_tracepoint
;
3738 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3740 while (wstep
!= NULL
)
3742 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
3745 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
3746 wstep
->tp_number
, paddress (wstep
->tp_address
),
3747 target_pid_to_str (tinfo
->entry
.id
));
3750 *wstep_link
= wstep
->next
;
3751 release_while_stepping_state (wstep
);
3752 wstep
= *wstep_link
;
3756 /* We've just finished one step. */
3757 ++wstep
->current_step
;
3760 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
3761 stop_pc
, tpoint
, wstep
->current_step
);
3763 if (wstep
->current_step
>= tpoint
->step_count
)
3765 /* The requested numbers of steps have occurred. */
3766 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
3767 target_pid_to_str (tinfo
->entry
.id
),
3768 wstep
->tp_number
, paddress (wstep
->tp_address
));
3770 /* Unlink the wstep. */
3771 *wstep_link
= wstep
->next
;
3772 release_while_stepping_state (wstep
);
3773 wstep
= *wstep_link
;
3775 /* Only check the hit count now, which ensure that we do all
3776 our stepping before stopping the run. */
3777 if (tpoint
->pass_count
> 0
3778 && tpoint
->hit_count
>= tpoint
->pass_count
3779 && stopping_tracepoint
== NULL
)
3780 stopping_tracepoint
= tpoint
;
3784 /* Keep single-stepping until the requested numbers of steps
3786 wstep_link
= &wstep
->next
;
3787 wstep
= *wstep_link
;
3790 if (stopping_tracepoint
3791 || trace_buffer_is_full
3792 || expr_eval_result
!= expr_eval_no_error
)
3802 /* Handle any internal tracing control breakpoint hits. That means,
3803 pull traceframes from the IPA to our buffer, and syncing both
3804 tracing agents when the IPA's tracing stops for some reason. */
3807 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3809 /* Pull in fast tracepoint trace frames from the inferior in-process
3810 agent's buffer into our buffer. */
3812 if (!in_process_agent_loaded ())
3815 upload_fast_traceframes ();
3817 /* Check if the in-process agent had decided we should stop
3819 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
3821 int ipa_trace_buffer_is_full
;
3822 CORE_ADDR ipa_stopping_tracepoint
;
3823 int ipa_expr_eval_result
;
3824 CORE_ADDR ipa_error_tracepoint
;
3826 trace_debug ("lib stopped at stop_tracing");
3828 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
3829 &ipa_trace_buffer_is_full
);
3831 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3832 &ipa_stopping_tracepoint
);
3833 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
3835 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
3836 &ipa_error_tracepoint
);
3837 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
3839 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
3840 &ipa_expr_eval_result
);
3841 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
3843 trace_debug ("lib: trace_buffer_is_full: %d, "
3844 "stopping_tracepoint: %s, "
3845 "ipa_expr_eval_result: %d, "
3846 "error_tracepoint: %s, ",
3847 ipa_trace_buffer_is_full
,
3848 paddress (ipa_stopping_tracepoint
),
3849 ipa_expr_eval_result
,
3850 paddress (ipa_error_tracepoint
));
3854 if (ipa_trace_buffer_is_full
)
3855 trace_debug ("lib stopped due to full buffer.");
3856 if (ipa_stopping_tracepoint
)
3857 trace_debug ("lib stopped due to tpoint");
3858 if (ipa_stopping_tracepoint
)
3859 trace_debug ("lib stopped due to error");
3862 if (ipa_stopping_tracepoint
!= 0)
3865 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
3867 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
3869 expr_eval_result
= ipa_expr_eval_result
;
3871 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
3876 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
3878 trace_debug ("lib stopped at flush_trace_buffer");
3885 /* Return true if TINFO just hit a tracepoint. Collect data if
3889 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3891 struct tracepoint
*tpoint
;
3893 struct trap_tracepoint_ctx ctx
;
3895 /* Not tracing, don't handle. */
3899 ctx
.base
.type
= trap_tracepoint
;
3900 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3902 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3904 /* Note that we collect fast tracepoints here as well. We'll
3905 step over the fast tracepoint jump later, which avoids the
3907 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
)
3909 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
3910 target_pid_to_str (tinfo
->entry
.id
),
3911 tpoint
->number
, paddress (tpoint
->address
));
3913 /* Test the condition if present, and collect if true. */
3915 || (condition_true_at_tracepoint
3916 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
3917 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
3920 if (stopping_tracepoint
3921 || trace_buffer_is_full
3922 || expr_eval_result
!= expr_eval_no_error
)
3926 /* If the tracepoint had a 'while-stepping' action, then set
3927 the thread to collect this tracepoint on the following
3929 else if (tpoint
->step_count
> 0)
3931 add_while_stepping_state (tinfo
,
3932 tpoint
->number
, tpoint
->address
);
3944 #if defined IN_PROCESS_AGENT && defined HAVE_UST
3945 struct ust_marker_data
;
3946 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
3948 struct tracepoint
*tpoint
,
3949 struct traceframe
*tframe
);
3952 /* Create a trace frame for the hit of the given tracepoint in the
3956 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
3957 struct tracepoint
*tpoint
)
3959 struct traceframe
*tframe
;
3962 /* Only count it as a hit when we actually collect data. */
3963 tpoint
->hit_count
++;
3965 /* If we've exceeded a defined pass count, record the event for
3966 later, and finish the collection for this hit. This test is only
3967 for nonstepping tracepoints, stepping tracepoints test at the end
3968 of their while-stepping loop. */
3969 if (tpoint
->pass_count
> 0
3970 && tpoint
->hit_count
>= tpoint
->pass_count
3971 && tpoint
->step_count
== 0
3972 && stopping_tracepoint
== NULL
)
3973 stopping_tracepoint
= tpoint
;
3975 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
3976 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
3978 tframe
= add_traceframe (tpoint
);
3982 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
3984 #ifndef IN_PROCESS_AGENT
3985 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
3986 tpoint
->number
, paddress (tpoint
->address
),
3987 tpoint
->actions_str
[acti
]);
3990 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
3991 tpoint
->actions
[acti
]);
3994 finish_traceframe (tframe
);
3997 if (tframe
== NULL
&& tracing
)
3998 trace_buffer_is_full
= 1;
4001 #ifndef IN_PROCESS_AGENT
4004 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4006 struct tracepoint
*tpoint
, int current_step
)
4008 struct traceframe
*tframe
;
4011 trace_debug ("Making new step traceframe for "
4012 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4013 tpoint
->number
, paddress (tpoint
->address
),
4014 current_step
, tpoint
->step_count
,
4017 tframe
= add_traceframe (tpoint
);
4021 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4023 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4024 tpoint
->number
, paddress (tpoint
->address
),
4025 tpoint
->step_actions_str
[acti
]);
4027 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4028 tpoint
->step_actions
[acti
]);
4031 finish_traceframe (tframe
);
4034 if (tframe
== NULL
&& tracing
)
4035 trace_buffer_is_full
= 1;
4040 static struct regcache
*
4041 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4043 struct regcache
*regcache
= NULL
;
4045 #ifdef IN_PROCESS_AGENT
4046 if (ctx
->type
== fast_tracepoint
)
4048 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4049 if (!fctx
->regcache_initted
)
4051 fctx
->regcache_initted
= 1;
4052 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4053 supply_regblock (&fctx
->regcache
, NULL
);
4054 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4056 regcache
= &fctx
->regcache
;
4059 if (ctx
->type
== static_tracepoint
)
4061 struct static_tracepoint_ctx
*sctx
4062 = (struct static_tracepoint_ctx
*) ctx
;
4064 if (!sctx
->regcache_initted
)
4066 sctx
->regcache_initted
= 1;
4067 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4068 supply_regblock (&sctx
->regcache
, NULL
);
4069 /* Pass down the tracepoint address, because REGS doesn't
4070 include the PC, but we know what it must have been. */
4071 supply_static_tracepoint_registers (&sctx
->regcache
,
4072 (const unsigned char *)
4074 sctx
->tpoint
->address
);
4076 regcache
= &sctx
->regcache
;
4080 if (ctx
->type
== trap_tracepoint
)
4082 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4083 regcache
= tctx
->regcache
;
4087 gdb_assert (regcache
!= NULL
);
4093 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4095 struct tracepoint
*tpoint
,
4096 struct traceframe
*tframe
,
4097 struct tracepoint_action
*taction
)
4099 enum eval_result_type err
;
4101 switch (taction
->type
)
4105 struct collect_memory_action
*maction
;
4107 maction
= (struct collect_memory_action
*) taction
;
4109 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4110 pulongest (maction
->len
),
4111 paddress (maction
->addr
), maction
->basereg
);
4112 /* (should use basereg) */
4113 agent_mem_read (tframe
, NULL
,
4114 (CORE_ADDR
) maction
->addr
, maction
->len
);
4119 unsigned char *regspace
;
4120 struct regcache tregcache
;
4121 struct regcache
*context_regcache
;
4124 trace_debug ("Want to collect registers");
4126 /* Collect all registers for now. */
4127 regspace
= add_traceframe_block (tframe
,
4128 1 + register_cache_size ());
4129 if (regspace
== NULL
)
4131 trace_debug ("Trace buffer block allocation failed, skipping");
4134 /* Identify a register block. */
4137 context_regcache
= get_context_regcache (ctx
);
4139 /* Wrap the regblock in a register cache (in the stack, we
4140 don't want to malloc here). */
4141 init_register_cache (&tregcache
, regspace
+ 1);
4143 /* Copy the register data to the regblock. */
4144 regcache_cpy (&tregcache
, context_regcache
);
4146 #ifndef IN_PROCESS_AGENT
4147 /* On some platforms, trap-based tracepoints will have the PC
4148 pointing to the next instruction after the trap, but we
4149 don't want the user or GDB trying to guess whether the
4150 saved PC needs adjusting; so always record the adjusted
4151 stop_pc. Note that we can't use tpoint->address instead,
4152 since it will be wrong for while-stepping actions. This
4153 adjustment is a nop for fast tracepoints collected from the
4154 in-process lib (but not if GDBserver is collecting one
4155 preemptively), since the PC had already been adjusted to
4156 contain the tracepoint's address by the jump pad. */
4157 trace_debug ("Storing stop pc (0x%s) in regblock",
4158 paddress (tpoint
->address
));
4160 /* This changes the regblock, not the thread's
4162 regcache_write_pc (&tregcache
, stop_pc
);
4168 struct eval_expr_action
*eaction
;
4170 eaction
= (struct eval_expr_action
*) taction
;
4172 trace_debug ("Want to evaluate expression");
4174 err
= eval_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4176 if (err
!= expr_eval_no_error
)
4178 record_tracepoint_error (tpoint
, "action expression", err
);
4185 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4186 trace_debug ("Want to collect static trace data");
4187 collect_ust_data_at_tracepoint (ctx
, stop_pc
,
4190 trace_debug ("warning: collecting static trace data, "
4191 "but static tracepoints are not supported");
4196 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4202 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4203 struct tracepoint
*tpoint
)
4206 enum eval_result_type err
;
4208 /* Presently, gdbserver doesn't run compiled conditions, only the
4209 IPA does. If the program stops at a fast tracepoint's address
4210 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4211 gdbserver preemptively collect the fast tracepoint. Later, on
4212 resume, gdbserver steps over the fast tracepoint like it steps
4213 over breakpoints, so that the IPA doesn't see that fast
4214 tracepoint. This avoids double collects of fast tracepoints in
4215 that stopping scenario. Having gdbserver itself handle the fast
4216 tracepoint gives the user a consistent view of when fast or trap
4217 tracepoints are collected, compared to an alternative where only
4218 trap tracepoints are collected on stop, and fast tracepoints on
4219 resume. When a fast tracepoint is being processed by gdbserver,
4220 it is always the non-compiled condition expression that is
4222 #ifdef IN_PROCESS_AGENT
4223 if (tpoint
->compiled_cond
)
4224 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4227 err
= eval_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4229 if (err
!= expr_eval_no_error
)
4231 record_tracepoint_error (tpoint
, "condition", err
);
4232 /* The error case must return false. */
4236 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4237 tpoint
->number
, paddress (tpoint
->address
),
4239 return (value
? 1 : 0);
4242 #ifndef IN_PROCESS_AGENT
4244 /* The packet form of an agent expression consists of an 'X', number
4245 of bytes in expression, a comma, and then the bytes. */
4247 static struct agent_expr
*
4248 parse_agent_expr (char **actparm
)
4250 char *act
= *actparm
;
4252 struct agent_expr
*aexpr
;
4254 ++act
; /* skip the X */
4255 act
= unpack_varlen_hex (act
, &xlen
);
4256 ++act
; /* skip a comma */
4257 aexpr
= xmalloc (sizeof (struct agent_expr
));
4258 aexpr
->length
= xlen
;
4259 aexpr
->bytes
= xmalloc (xlen
);
4260 convert_ascii_to_int (act
, aexpr
->bytes
, xlen
);
4261 *actparm
= act
+ (xlen
* 2);
4265 /* Convert the bytes of an agent expression back into hex digits, so
4266 they can be printed or uploaded. This allocates the buffer,
4267 callers should free when they are done with it. */
4270 unparse_agent_expr (struct agent_expr
*aexpr
)
4274 rslt
= xmalloc (2 * aexpr
->length
+ 1);
4275 convert_int_to_ascii (aexpr
->bytes
, rslt
, aexpr
->length
);
4281 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
4284 gdb_agent_op_name (int op
)
4286 if (op
< 0 || op
>= gdb_agent_op_last
|| gdb_agent_op_names
[op
] == NULL
)
4288 return gdb_agent_op_names
[op
];
4291 /* The agent expression evaluator, as specified by the GDB docs. It
4292 returns 0 if everything went OK, and a nonzero error code
4295 static enum eval_result_type
4296 eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4297 struct traceframe
*tframe
,
4298 struct agent_expr
*aexpr
,
4302 #define STACK_MAX 100
4303 ULONGEST stack
[STACK_MAX
], top
;
4308 /* This union is a convenient way to convert representations. For
4309 now, assume a standard architecture where the hardware integer
4310 types have 8, 16, 32, 64 bit types. A more robust solution would
4311 be to import stdint.h from gnulib. */
4316 unsigned char bytes
[1];
4321 unsigned char bytes
[2];
4326 unsigned char bytes
[4];
4331 unsigned char bytes
[8];
4336 if (aexpr
->length
== 0)
4338 trace_debug ("empty agent expression");
4339 return expr_eval_empty_expression
;
4342 /* Cache the stack top in its own variable. Much of the time we can
4343 operate on this variable, rather than dinking with the stack. It
4344 needs to be copied to the stack when sp changes. */
4349 op
= aexpr
->bytes
[pc
++];
4351 trace_debug ("About to interpret byte 0x%x", op
);
4355 case gdb_agent_op_add
:
4359 case gdb_agent_op_sub
:
4360 top
= stack
[--sp
] - top
;
4363 case gdb_agent_op_mul
:
4367 case gdb_agent_op_div_signed
:
4370 trace_debug ("Attempted to divide by zero");
4371 return expr_eval_divide_by_zero
;
4373 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
4376 case gdb_agent_op_div_unsigned
:
4379 trace_debug ("Attempted to divide by zero");
4380 return expr_eval_divide_by_zero
;
4382 top
= stack
[--sp
] / top
;
4385 case gdb_agent_op_rem_signed
:
4388 trace_debug ("Attempted to divide by zero");
4389 return expr_eval_divide_by_zero
;
4391 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
4394 case gdb_agent_op_rem_unsigned
:
4397 trace_debug ("Attempted to divide by zero");
4398 return expr_eval_divide_by_zero
;
4400 top
= stack
[--sp
] % top
;
4403 case gdb_agent_op_lsh
:
4404 top
= stack
[--sp
] << top
;
4407 case gdb_agent_op_rsh_signed
:
4408 top
= ((LONGEST
) stack
[--sp
]) >> top
;
4411 case gdb_agent_op_rsh_unsigned
:
4412 top
= stack
[--sp
] >> top
;
4415 case gdb_agent_op_trace
:
4416 agent_mem_read (tframe
,
4417 NULL
, (CORE_ADDR
) stack
[--sp
], (ULONGEST
) top
);
4422 case gdb_agent_op_trace_quick
:
4423 arg
= aexpr
->bytes
[pc
++];
4424 agent_mem_read (tframe
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
4427 case gdb_agent_op_log_not
:
4431 case gdb_agent_op_bit_and
:
4435 case gdb_agent_op_bit_or
:
4439 case gdb_agent_op_bit_xor
:
4443 case gdb_agent_op_bit_not
:
4447 case gdb_agent_op_equal
:
4448 top
= (stack
[--sp
] == top
);
4451 case gdb_agent_op_less_signed
:
4452 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
4455 case gdb_agent_op_less_unsigned
:
4456 top
= (stack
[--sp
] < top
);
4459 case gdb_agent_op_ext
:
4460 arg
= aexpr
->bytes
[pc
++];
4461 if (arg
< (sizeof (LONGEST
) * 8))
4463 LONGEST mask
= 1 << (arg
- 1);
4464 top
&= ((LONGEST
) 1 << arg
) - 1;
4465 top
= (top
^ mask
) - mask
;
4469 case gdb_agent_op_ref8
:
4470 agent_mem_read (tframe
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
4474 case gdb_agent_op_ref16
:
4475 agent_mem_read (tframe
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
4479 case gdb_agent_op_ref32
:
4480 agent_mem_read (tframe
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
4484 case gdb_agent_op_ref64
:
4485 agent_mem_read (tframe
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
4489 case gdb_agent_op_if_goto
:
4491 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4498 case gdb_agent_op_goto
:
4499 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4502 case gdb_agent_op_const8
:
4503 /* Flush the cached stack top. */
4505 top
= aexpr
->bytes
[pc
++];
4508 case gdb_agent_op_const16
:
4509 /* Flush the cached stack top. */
4511 top
= aexpr
->bytes
[pc
++];
4512 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4515 case gdb_agent_op_const32
:
4516 /* Flush the cached stack top. */
4518 top
= aexpr
->bytes
[pc
++];
4519 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4520 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4521 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4524 case gdb_agent_op_const64
:
4525 /* Flush the cached stack top. */
4527 top
= aexpr
->bytes
[pc
++];
4528 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4529 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4530 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4531 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4532 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4533 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4534 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4537 case gdb_agent_op_reg
:
4538 /* Flush the cached stack top. */
4540 arg
= aexpr
->bytes
[pc
++];
4541 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4544 struct regcache
*regcache
;
4546 regcache
= get_context_regcache (ctx
);
4548 switch (register_size (regnum
))
4551 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
4555 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
4559 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
4563 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
4567 internal_error (__FILE__
, __LINE__
,
4568 "unhandled register size");
4573 case gdb_agent_op_end
:
4574 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4575 sp
, pulongest (top
));
4580 /* This should be an error */
4581 trace_debug ("Stack is empty, nothing to return");
4582 return expr_eval_empty_stack
;
4586 return expr_eval_no_error
;
4588 case gdb_agent_op_dup
:
4592 case gdb_agent_op_pop
:
4597 case gdb_agent_op_pick
:
4598 arg
= aexpr
->bytes
[pc
++];
4600 top
= stack
[sp
- arg
];
4604 case gdb_agent_op_rot
:
4606 ULONGEST tem
= stack
[sp
- 1];
4608 stack
[sp
- 1] = stack
[sp
- 2];
4609 stack
[sp
- 2] = top
;
4614 case gdb_agent_op_zero_ext
:
4615 arg
= aexpr
->bytes
[pc
++];
4616 if (arg
< (sizeof (LONGEST
) * 8))
4617 top
&= ((LONGEST
) 1 << arg
) - 1;
4620 case gdb_agent_op_swap
:
4621 /* Interchange top two stack elements, making sure top gets
4622 copied back onto stack. */
4624 top
= stack
[sp
- 1];
4625 stack
[sp
- 1] = stack
[sp
];
4628 case gdb_agent_op_getv
:
4629 /* Flush the cached stack top. */
4631 arg
= aexpr
->bytes
[pc
++];
4632 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4633 top
= get_trace_state_variable_value (arg
);
4636 case gdb_agent_op_setv
:
4637 arg
= aexpr
->bytes
[pc
++];
4638 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4639 set_trace_state_variable_value (arg
, top
);
4640 /* Note that we leave the value on the stack, for the
4641 benefit of later/enclosing expressions. */
4644 case gdb_agent_op_tracev
:
4645 arg
= aexpr
->bytes
[pc
++];
4646 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4647 agent_tsv_read (tframe
, arg
);
4650 case gdb_agent_op_tracenz
:
4651 agent_mem_read_string (tframe
, NULL
, (CORE_ADDR
) stack
[--sp
],
4657 /* GDB never (currently) generates any of these ops. */
4658 case gdb_agent_op_float
:
4659 case gdb_agent_op_ref_float
:
4660 case gdb_agent_op_ref_double
:
4661 case gdb_agent_op_ref_long_double
:
4662 case gdb_agent_op_l_to_d
:
4663 case gdb_agent_op_d_to_l
:
4664 case gdb_agent_op_trace16
:
4665 trace_debug ("Agent expression op 0x%x valid, but not handled",
4667 /* If ever GDB generates any of these, we don't have the
4668 option of ignoring. */
4672 trace_debug ("Agent expression op 0x%x not recognized", op
);
4673 /* Don't struggle on, things will just get worse. */
4674 return expr_eval_unrecognized_opcode
;
4677 /* Check for stack badness. */
4678 if (sp
>= (STACK_MAX
- 1))
4680 trace_debug ("Expression stack overflow");
4681 return expr_eval_stack_overflow
;
4686 trace_debug ("Expression stack underflow");
4687 return expr_eval_stack_underflow
;
4690 trace_debug ("Op %s -> sp=%d, top=0x%s",
4691 gdb_agent_op_name (op
), sp
, pulongest (top
));
4695 /* Do memory copies for bytecodes. */
4696 /* Do the recording of memory blocks for actions and bytecodes. */
4699 agent_mem_read (struct traceframe
*tframe
,
4700 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4702 unsigned char *mspace
;
4703 ULONGEST remaining
= len
;
4704 unsigned short blocklen
;
4706 /* If a 'to' buffer is specified, use it. */
4709 read_inferior_memory (from
, to
, len
);
4713 /* Otherwise, create a new memory block in the trace buffer. */
4714 while (remaining
> 0)
4718 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4719 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4720 mspace
= add_traceframe_block (tframe
, sp
);
4723 /* Identify block as a memory block. */
4726 /* Record address and size. */
4727 memcpy (mspace
, &from
, sizeof (from
));
4728 mspace
+= sizeof (from
);
4729 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4730 mspace
+= sizeof (blocklen
);
4731 /* Record the memory block proper. */
4732 read_inferior_memory (from
, mspace
, blocklen
);
4733 trace_debug ("%d bytes recorded", blocklen
);
4734 remaining
-= blocklen
;
4741 agent_mem_read_string (struct traceframe
*tframe
,
4742 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4744 unsigned char *buf
, *mspace
;
4745 ULONGEST remaining
= len
;
4746 unsigned short blocklen
, i
;
4748 /* To save a bit of space, block lengths are 16-bit, so break large
4749 requests into multiple blocks. Bordering on overkill for strings,
4750 but it could happen that someone specifies a large max length. */
4751 while (remaining
> 0)
4755 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4756 /* We want working space to accumulate nonzero bytes, since
4757 traceframes must have a predecided size (otherwise it gets
4758 harder to wrap correctly for the circular case, etc). */
4759 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4760 for (i
= 0; i
< blocklen
; ++i
)
4762 /* Read the string one byte at a time, in case the string is
4763 at the end of a valid memory area - we don't want a
4764 correctly-terminated string to engender segvio
4766 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4771 /* Make sure outer loop stops now too. */
4772 remaining
= blocklen
;
4776 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4777 mspace
= add_traceframe_block (tframe
, sp
);
4783 /* Identify block as a memory block. */
4786 /* Record address and size. */
4787 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4788 mspace
+= sizeof (from
);
4789 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4790 mspace
+= sizeof (blocklen
);
4791 /* Copy the string contents. */
4792 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
4793 remaining
-= blocklen
;
4800 /* Record the value of a trace state variable. */
4803 agent_tsv_read (struct traceframe
*tframe
, int n
)
4805 unsigned char *vspace
;
4808 vspace
= add_traceframe_block (tframe
,
4809 1 + sizeof (n
) + sizeof (LONGEST
));
4812 /* Identify block as a variable. */
4814 /* Record variable's number and value. */
4815 memcpy (vspace
+ 1, &n
, sizeof (n
));
4816 val
= get_trace_state_variable_value (n
);
4817 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4818 trace_debug ("Variable %d recorded", n
);
4822 #ifndef IN_PROCESS_AGENT
4824 /* Callback for traceframe_walk_blocks, used to find a given block
4825 type in a traceframe. */
4828 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
4830 char *wantedp
= data
;
4832 if (*wantedp
== blocktype
)
4838 /* Walk over all traceframe blocks of the traceframe buffer starting
4839 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4840 block found, passing in DATA unmodified. If CALLBACK returns true,
4841 this returns a pointer to where the block is found. Returns NULL
4842 if no callback call returned true, indicating that all blocks have
4845 static unsigned char *
4846 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
4848 int (*callback
) (char blocktype
,
4849 unsigned char *dataptr
,
4853 unsigned char *dataptr
;
4857 trace_debug ("traceframe %d has no data", tfnum
);
4861 /* Iterate through a traceframe's blocks, looking for a block of the
4863 for (dataptr
= database
;
4864 dataptr
< database
+ datasize
;
4868 unsigned short mlen
;
4870 if (dataptr
== trace_buffer_wrap
)
4872 /* Adjust to reflect wrapping part of the frame around to
4874 datasize
= dataptr
- database
;
4875 dataptr
= database
= trace_buffer_lo
;
4878 blocktype
= *dataptr
++;
4880 if ((*callback
) (blocktype
, dataptr
, data
))
4886 /* Skip over the registers block. */
4887 dataptr
+= register_cache_size ();
4890 /* Skip over the memory block. */
4891 dataptr
+= sizeof (CORE_ADDR
);
4892 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4893 dataptr
+= (sizeof (mlen
) + mlen
);
4896 /* Skip over the TSV block. */
4897 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4900 /* Skip over the static trace data block. */
4901 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4902 dataptr
+= (sizeof (mlen
) + mlen
);
4905 trace_debug ("traceframe %d has unknown block type 0x%x",
4914 /* Look for the block of type TYPE_WANTED in the trameframe starting
4915 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4918 static unsigned char *
4919 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4920 int tfnum
, char type_wanted
)
4922 return traceframe_walk_blocks (database
, datasize
, tfnum
,
4923 match_blocktype
, &type_wanted
);
4926 static unsigned char *
4927 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4929 unsigned char *regblock
;
4931 regblock
= traceframe_find_block_type (tframe
->data
,
4935 if (regblock
== NULL
)
4936 trace_debug ("traceframe %d has no register data", tfnum
);
4941 /* Get registers from a traceframe. */
4944 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4946 unsigned char *dataptr
;
4947 struct tracepoint
*tpoint
;
4948 struct traceframe
*tframe
;
4950 tframe
= find_traceframe (tfnum
);
4954 trace_debug ("traceframe %d not found", tfnum
);
4958 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4959 if (dataptr
== NULL
)
4961 /* Mark registers unavailable. */
4962 supply_regblock (regcache
, NULL
);
4964 /* We can generally guess at a PC, although this will be
4965 misleading for while-stepping frames and multi-location
4967 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4969 regcache_write_pc (regcache
, tpoint
->address
);
4972 supply_regblock (regcache
, dataptr
);
4978 traceframe_get_pc (struct traceframe
*tframe
)
4980 struct regcache regcache
;
4981 unsigned char *dataptr
;
4983 dataptr
= traceframe_find_regblock (tframe
, -1);
4984 if (dataptr
== NULL
)
4987 init_register_cache (®cache
, dataptr
);
4988 return regcache_read_pc (®cache
);
4991 /* Read a requested block of memory from a trace frame. */
4994 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
4995 unsigned char *buf
, ULONGEST length
,
4998 struct traceframe
*tframe
;
4999 unsigned char *database
, *dataptr
;
5000 unsigned int datasize
;
5002 unsigned short mlen
;
5004 trace_debug ("traceframe_read_mem");
5006 tframe
= find_traceframe (tfnum
);
5010 trace_debug ("traceframe %d not found", tfnum
);
5014 datasize
= tframe
->data_size
;
5015 database
= dataptr
= &tframe
->data
[0];
5017 /* Iterate through a traceframe's blocks, looking for memory. */
5018 while ((dataptr
= traceframe_find_block_type (dataptr
,
5020 - (dataptr
- database
),
5021 tfnum
, 'M')) != NULL
)
5023 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5024 dataptr
+= sizeof (maddr
);
5025 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5026 dataptr
+= sizeof (mlen
);
5027 trace_debug ("traceframe %d has %d bytes at %s",
5028 tfnum
, mlen
, paddress (maddr
));
5030 /* If the block includes the first part of the desired range,
5031 return as much it has; GDB will re-request the remainder,
5032 which might be in a different block of this trace frame. */
5033 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5035 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5039 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5044 /* Skip over this block. */
5048 trace_debug ("traceframe %d has no memory data for the desired region",
5056 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5059 struct traceframe
*tframe
;
5060 unsigned char *database
, *dataptr
;
5061 unsigned int datasize
;
5064 trace_debug ("traceframe_read_tsv");
5066 tfnum
= current_traceframe
;
5070 trace_debug ("no current traceframe");
5074 tframe
= find_traceframe (tfnum
);
5078 trace_debug ("traceframe %d not found", tfnum
);
5082 datasize
= tframe
->data_size
;
5083 database
= dataptr
= &tframe
->data
[0];
5085 /* Iterate through a traceframe's blocks, looking for the tsv. */
5086 while ((dataptr
= traceframe_find_block_type (dataptr
,
5088 - (dataptr
- database
),
5089 tfnum
, 'V')) != NULL
)
5091 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5092 dataptr
+= sizeof (vnum
);
5094 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5096 /* Check that this is the variable we want. */
5099 memcpy (val
, dataptr
, sizeof (*val
));
5103 /* Skip over this block. */
5104 dataptr
+= sizeof (LONGEST
);
5107 trace_debug ("traceframe %d has no data for variable %d",
5112 /* Read a requested block of static tracepoint data from a trace
5116 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5117 unsigned char *buf
, ULONGEST length
,
5120 struct traceframe
*tframe
;
5121 unsigned char *database
, *dataptr
;
5122 unsigned int datasize
;
5123 unsigned short mlen
;
5125 trace_debug ("traceframe_read_sdata");
5127 tframe
= find_traceframe (tfnum
);
5131 trace_debug ("traceframe %d not found", tfnum
);
5135 datasize
= tframe
->data_size
;
5136 database
= &tframe
->data
[0];
5138 /* Iterate through a traceframe's blocks, looking for static
5140 dataptr
= traceframe_find_block_type (database
, datasize
,
5142 if (dataptr
!= NULL
)
5144 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5145 dataptr
+= sizeof (mlen
);
5148 if (offset
+ length
> mlen
)
5149 length
= mlen
- offset
;
5151 memcpy (buf
, dataptr
, length
);
5159 trace_debug ("traceframe %d has no static trace data", tfnum
);
5165 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5166 object. DATA is pointer to a struct buffer holding the
5167 traceframe-info object being built. */
5170 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5172 struct buffer
*buffer
= data
;
5178 unsigned short mlen
;
5181 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5182 dataptr
+= sizeof (maddr
);
5183 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5184 dataptr
+= sizeof (mlen
);
5185 buffer_xml_printf (buffer
,
5186 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5187 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5197 warning ("Unhandled trace block type (%d) '%c ' "
5198 "while building trace frame info.",
5199 blocktype
, blocktype
);
5206 /* Build a traceframe-info object for traceframe number TFNUM into
5210 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5212 struct traceframe
*tframe
;
5214 trace_debug ("traceframe_read_info");
5216 tframe
= find_traceframe (tfnum
);
5220 trace_debug ("traceframe %d not found", tfnum
);
5224 buffer_grow_str (buffer
, "<traceframe-info>\n");
5225 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5226 tfnum
, build_traceframe_info_xml
, buffer
);
5227 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5231 /* Return the first fast tracepoint whose jump pad contains PC. */
5233 static struct tracepoint
*
5234 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5236 struct tracepoint
*tpoint
;
5238 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5239 if (tpoint
->type
== fast_tracepoint
)
5240 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5246 /* Return GDBserver's tracepoint that matches the IP Agent's
5247 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5250 static struct tracepoint
*
5251 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5253 struct tracepoint
*tpoint
;
5255 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5256 if (tpoint
->type
== fast_tracepoint
)
5257 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5265 /* The type of the object that is used to synchronize fast tracepoint
5268 typedef struct collecting_t
5270 /* The fast tracepoint number currently collecting. */
5273 /* A number that GDBserver can use to identify the thread that is
5274 presently holding the collect lock. This need not (and usually
5275 is not) the thread id, as getting the current thread ID usually
5276 requires a system call, which we want to avoid like the plague.
5277 Usually this is thread's TCB, found in the TLS (pseudo-)
5278 register, which is readable with a single insn on several
5280 uintptr_t thread_area
;
5283 #ifndef IN_PROCESS_AGENT
5286 force_unlock_trace_buffer (void)
5288 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5291 /* Check if the thread identified by THREAD_AREA which is stopped at
5292 STOP_PC, is presently locking the fast tracepoint collection, and
5293 if so, gather some status of said collection. Returns 0 if the
5294 thread isn't collecting or in the jump pad at all. 1, if in the
5295 jump pad (or within gdb_collect) and hasn't executed the adjusted
5296 original insn yet (can set a breakpoint there and run to it). 2,
5297 if presently executing the adjusted original insn --- in which
5298 case, if we want to move the thread out of the jump pad, we need to
5299 single-step it until this function returns 0. */
5302 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5304 struct fast_tpoint_collect_status
*status
)
5306 CORE_ADDR ipa_collecting
;
5307 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5308 struct tracepoint
*tpoint
;
5309 int needs_breakpoint
;
5311 /* The thread THREAD_AREA is either:
5313 0. not collecting at all, not within the jump pad, or within
5314 gdb_collect or one of its callees.
5316 1. in the jump pad and haven't reached gdb_collect
5318 2. within gdb_collect (out of the jump pad) (collect is set)
5320 3. we're in the jump pad, after gdb_collect having returned,
5321 possibly executing the adjusted insns.
5323 For cases 1 and 3, `collecting' may or not be set. The jump pad
5324 doesn't have any complicated jump logic, so we can tell if the
5325 thread is executing the adjust original insn or not by just
5326 matching STOP_PC with known jump pad addresses. If we it isn't
5327 yet executing the original insn, set a breakpoint there, and let
5328 the thread run to it, so to quickly step over a possible (many
5329 insns) gdb_collect call. Otherwise, or when the breakpoint is
5330 hit, only a few (small number of) insns are left to be executed
5331 in the jump pad. Single-step the thread until it leaves the
5336 needs_breakpoint
= 0;
5337 trace_debug ("fast_tracepoint_collecting");
5339 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5340 &ipa_gdb_jump_pad_buffer
))
5341 fatal ("error extracting `gdb_jump_pad_buffer'");
5342 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5343 &ipa_gdb_jump_pad_buffer_end
))
5344 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5346 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5347 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5349 /* We can tell which tracepoint(s) the thread is collecting by
5350 matching the jump pad address back to the tracepoint. */
5351 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5354 warning ("in jump pad, but no matching tpoint?");
5359 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5361 tpoint
->number
, paddress (tpoint
->address
),
5362 paddress (tpoint
->jump_pad
),
5363 paddress (tpoint
->jump_pad_end
),
5364 paddress (tpoint
->adjusted_insn_addr
),
5365 paddress (tpoint
->adjusted_insn_addr_end
));
5368 /* Definitely in the jump pad. May or may not need
5369 fast-exit-jump-pad breakpoint. */
5370 if (tpoint
->jump_pad
<= stop_pc
5371 && stop_pc
< tpoint
->adjusted_insn_addr
)
5372 needs_breakpoint
= 1;
5376 collecting_t ipa_collecting_obj
;
5378 /* If `collecting' is set/locked, then the THREAD_AREA thread
5379 may or not be the one holding the lock. We have to read the
5380 lock to find out. */
5382 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5385 trace_debug ("fast_tracepoint_collecting:"
5386 " failed reading 'collecting' in the inferior");
5390 if (!ipa_collecting
)
5392 trace_debug ("fast_tracepoint_collecting: not collecting"
5393 " (and nobody is).");
5397 /* Some thread is collecting. Check which. */
5398 if (read_inferior_memory (ipa_collecting
,
5399 (unsigned char *) &ipa_collecting_obj
,
5400 sizeof (ipa_collecting_obj
)) != 0)
5403 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5405 trace_debug ("fast_tracepoint_collecting: not collecting "
5406 "(another thread is)");
5411 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5414 warning ("fast_tracepoint_collecting: collecting, "
5415 "but tpoint %s not found?",
5416 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5420 /* The thread is within `gdb_collect', skip over the rest of
5421 fast tracepoint collection quickly using a breakpoint. */
5422 needs_breakpoint
= 1;
5425 /* The caller wants a bit of status detail. */
5428 status
->tpoint_num
= tpoint
->number
;
5429 status
->tpoint_addr
= tpoint
->address
;
5430 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5431 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5434 if (needs_breakpoint
)
5436 /* Hasn't executed the original instruction yet. Set breakpoint
5437 there, and wait till it's hit, then single-step until exiting
5441 fast_tracepoint_collecting, returning continue-until-break at %s",
5442 paddress (tpoint
->adjusted_insn_addr
));
5444 return 1; /* continue */
5448 /* Just single-step until exiting the jump pad. */
5450 trace_debug ("fast_tracepoint_collecting, returning "
5451 "need-single-step (%s-%s)",
5452 paddress (tpoint
->adjusted_insn_addr
),
5453 paddress (tpoint
->adjusted_insn_addr_end
));
5455 return 2; /* single-step */
5461 #ifdef IN_PROCESS_AGENT
5463 /* The global fast tracepoint collect lock. Points to a collecting_t
5464 object built on the stack by the jump pad, if presently locked;
5465 NULL if it isn't locked. Note that this lock *must* be set while
5466 executing any *function other than the jump pad. See
5467 fast_tracepoint_collecting. */
5468 static collecting_t
* ATTR_USED collecting
;
5470 /* This routine, called from the jump pad (in asm) is designed to be
5471 called from the jump pads of fast tracepoints, thus it is on the
5474 IP_AGENT_EXPORT
void ATTR_USED
5475 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5477 struct fast_tracepoint_ctx ctx
;
5479 /* Don't do anything until the trace run is completely set up. */
5483 ctx
.base
.type
= fast_tracepoint
;
5485 ctx
.regcache_initted
= 0;
5486 /* Wrap the regblock in a register cache (in the stack, we don't
5487 want to malloc here). */
5488 ctx
.regspace
= alloca (register_cache_size ());
5489 if (ctx
.regspace
== NULL
)
5491 trace_debug ("Trace buffer block allocation failed, skipping");
5495 for (ctx
.tpoint
= tpoint
;
5496 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5497 ctx
.tpoint
= ctx
.tpoint
->next
)
5499 if (!ctx
.tpoint
->enabled
)
5502 /* Multiple tracepoints of different types, such as fast tracepoint and
5503 static tracepoint, can be set at the same address. */
5504 if (ctx
.tpoint
->type
!= tpoint
->type
)
5507 /* Test the condition if present, and collect if true. */
5508 if (ctx
.tpoint
->cond
== NULL
5509 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5512 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5513 ctx
.tpoint
->address
, ctx
.tpoint
);
5515 /* Note that this will cause original insns to be written back
5516 to where we jumped from, but that's OK because we're jumping
5517 back to the next whole instruction. This will go badly if
5518 instruction restoration is not atomic though. */
5519 if (stopping_tracepoint
5520 || trace_buffer_is_full
5521 || expr_eval_result
!= expr_eval_no_error
)
5529 /* If there was a condition and it evaluated to false, the only
5530 way we would stop tracing is if there was an error during
5531 condition expression evaluation. */
5532 if (expr_eval_result
!= expr_eval_no_error
)
5543 #ifndef IN_PROCESS_AGENT
5545 /* Bytecode compilation. */
5547 CORE_ADDR current_insn_ptr
;
5551 struct bytecode_address
5556 /* Offset and size of field to be modified in the goto block. */
5557 int from_offset
, from_size
;
5558 struct bytecode_address
*next
;
5559 } *bytecode_address_table
;
5562 get_raw_reg_func_addr (void)
5564 return ipa_sym_addrs
.addr_get_raw_reg
;
5568 emit_prologue (void)
5570 target_emit_ops ()->emit_prologue ();
5574 emit_epilogue (void)
5576 target_emit_ops ()->emit_epilogue ();
5582 target_emit_ops ()->emit_add ();
5588 target_emit_ops ()->emit_sub ();
5594 target_emit_ops ()->emit_mul ();
5600 target_emit_ops ()->emit_lsh ();
5604 emit_rsh_signed (void)
5606 target_emit_ops ()->emit_rsh_signed ();
5610 emit_rsh_unsigned (void)
5612 target_emit_ops ()->emit_rsh_unsigned ();
5618 target_emit_ops ()->emit_ext (arg
);
5624 target_emit_ops ()->emit_log_not ();
5630 target_emit_ops ()->emit_bit_and ();
5636 target_emit_ops ()->emit_bit_or ();
5642 target_emit_ops ()->emit_bit_xor ();
5648 target_emit_ops ()->emit_bit_not ();
5654 target_emit_ops ()->emit_equal ();
5658 emit_less_signed (void)
5660 target_emit_ops ()->emit_less_signed ();
5664 emit_less_unsigned (void)
5666 target_emit_ops ()->emit_less_unsigned ();
5672 target_emit_ops ()->emit_ref (size
);
5676 emit_if_goto (int *offset_p
, int *size_p
)
5678 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
5682 emit_goto (int *offset_p
, int *size_p
)
5684 target_emit_ops ()->emit_goto (offset_p
, size_p
);
5688 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
5690 target_emit_ops ()->write_goto_address (from
, to
, size
);
5694 emit_const (LONGEST num
)
5696 target_emit_ops ()->emit_const (num
);
5702 target_emit_ops ()->emit_reg (reg
);
5708 target_emit_ops ()->emit_pop ();
5712 emit_stack_flush (void)
5714 target_emit_ops ()->emit_stack_flush ();
5718 emit_zero_ext (int arg
)
5720 target_emit_ops ()->emit_zero_ext (arg
);
5726 target_emit_ops ()->emit_swap ();
5730 emit_stack_adjust (int n
)
5732 target_emit_ops ()->emit_stack_adjust (n
);
5735 /* FN's prototype is `LONGEST(*fn)(int)'. */
5738 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
5740 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
5743 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
5746 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
5748 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
5752 emit_eq_goto (int *offset_p
, int *size_p
)
5754 target_emit_ops ()->emit_eq_goto (offset_p
, size_p
);
5758 emit_ne_goto (int *offset_p
, int *size_p
)
5760 target_emit_ops ()->emit_ne_goto (offset_p
, size_p
);
5764 emit_lt_goto (int *offset_p
, int *size_p
)
5766 target_emit_ops ()->emit_lt_goto (offset_p
, size_p
);
5770 emit_ge_goto (int *offset_p
, int *size_p
)
5772 target_emit_ops ()->emit_ge_goto (offset_p
, size_p
);
5776 emit_gt_goto (int *offset_p
, int *size_p
)
5778 target_emit_ops ()->emit_gt_goto (offset_p
, size_p
);
5782 emit_le_goto (int *offset_p
, int *size_p
)
5784 target_emit_ops ()->emit_le_goto (offset_p
, size_p
);
5787 static enum eval_result_type
compile_bytecodes (struct agent_expr
*aexpr
);
5790 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5791 CORE_ADDR
*jump_entry
)
5793 CORE_ADDR entry_point
= *jump_entry
;
5794 enum eval_result_type err
;
5796 trace_debug ("Starting condition compilation for tracepoint %d\n",
5799 /* Initialize the global pointer to the code being built. */
5800 current_insn_ptr
= *jump_entry
;
5804 err
= compile_bytecodes (tpoint
->cond
);
5806 if (err
== expr_eval_no_error
)
5810 /* Record the beginning of the compiled code. */
5811 tpoint
->compiled_cond
= entry_point
;
5813 trace_debug ("Condition compilation for tracepoint %d complete\n",
5818 /* Leave the unfinished code in situ, but don't point to it. */
5820 tpoint
->compiled_cond
= 0;
5822 trace_debug ("Condition compilation for tracepoint %d failed, "
5824 tpoint
->number
, err
);
5827 /* Update the code pointer passed in. Note that we do this even if
5828 the compile fails, so that we can look at the partial results
5829 instead of letting them be overwritten. */
5830 *jump_entry
= current_insn_ptr
;
5832 /* Leave a gap, to aid dump decipherment. */
5836 /* Scan an agent expression for any evidence that the given PC is the
5837 target of a jump bytecode in the expression. */
5840 is_goto_target (struct agent_expr
*aexpr
, int pc
)
5845 for (i
= 0; i
< aexpr
->length
; i
+= 1 + gdb_agent_op_sizes
[op
])
5847 op
= aexpr
->bytes
[i
];
5849 if (op
== gdb_agent_op_goto
|| op
== gdb_agent_op_if_goto
)
5851 int target
= (aexpr
->bytes
[i
+ 1] << 8) + aexpr
->bytes
[i
+ 2];
5860 /* Given an agent expression, turn it into native code. */
5862 static enum eval_result_type
5863 compile_bytecodes (struct agent_expr
*aexpr
)
5867 unsigned char op
, next_op
;
5869 /* This is only used to build 64-bit value for constants. */
5871 struct bytecode_address
*aentry
, *aentry2
;
5876 trace_debug ("Cannot compile op 0x%x\n", op); \
5877 return expr_eval_unhandled_opcode; \
5880 if (aexpr
->length
== 0)
5882 trace_debug ("empty agent expression\n");
5883 return expr_eval_empty_expression
;
5886 bytecode_address_table
= NULL
;
5890 op
= aexpr
->bytes
[pc
];
5892 trace_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
5894 /* Record the compiled-code address of the bytecode, for use by
5895 jump instructions. */
5896 aentry
= xmalloc (sizeof (struct bytecode_address
));
5898 aentry
->address
= current_insn_ptr
;
5899 aentry
->goto_pc
= -1;
5900 aentry
->from_offset
= aentry
->from_size
= 0;
5901 aentry
->next
= bytecode_address_table
;
5902 bytecode_address_table
= aentry
;
5910 case gdb_agent_op_add
:
5914 case gdb_agent_op_sub
:
5918 case gdb_agent_op_mul
:
5922 case gdb_agent_op_div_signed
:
5926 case gdb_agent_op_div_unsigned
:
5930 case gdb_agent_op_rem_signed
:
5934 case gdb_agent_op_rem_unsigned
:
5938 case gdb_agent_op_lsh
:
5942 case gdb_agent_op_rsh_signed
:
5946 case gdb_agent_op_rsh_unsigned
:
5947 emit_rsh_unsigned ();
5950 case gdb_agent_op_trace
:
5954 case gdb_agent_op_trace_quick
:
5958 case gdb_agent_op_log_not
:
5962 case gdb_agent_op_bit_and
:
5966 case gdb_agent_op_bit_or
:
5970 case gdb_agent_op_bit_xor
:
5974 case gdb_agent_op_bit_not
:
5978 case gdb_agent_op_equal
:
5979 next_op
= aexpr
->bytes
[pc
];
5980 if (next_op
== gdb_agent_op_if_goto
5981 && !is_goto_target (aexpr
, pc
)
5982 && target_emit_ops ()->emit_eq_goto
)
5984 trace_debug ("Combining equal & if_goto");
5987 arg
= aexpr
->bytes
[pc
++];
5988 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5989 aentry
->goto_pc
= arg
;
5990 emit_eq_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5992 else if (next_op
== gdb_agent_op_log_not
5993 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
5994 && !is_goto_target (aexpr
, pc
+ 1)
5995 && target_emit_ops ()->emit_ne_goto
)
5997 trace_debug ("Combining equal & log_not & if_goto");
6000 arg
= aexpr
->bytes
[pc
++];
6001 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6002 aentry
->goto_pc
= arg
;
6003 emit_ne_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6009 case gdb_agent_op_less_signed
:
6010 next_op
= aexpr
->bytes
[pc
];
6011 if (next_op
== gdb_agent_op_if_goto
6012 && !is_goto_target (aexpr
, pc
))
6014 trace_debug ("Combining less_signed & if_goto");
6017 arg
= aexpr
->bytes
[pc
++];
6018 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6019 aentry
->goto_pc
= arg
;
6020 emit_lt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6022 else if (next_op
== gdb_agent_op_log_not
6023 && !is_goto_target (aexpr
, pc
)
6024 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
6025 && !is_goto_target (aexpr
, pc
+ 1))
6027 trace_debug ("Combining less_signed & log_not & if_goto");
6030 arg
= aexpr
->bytes
[pc
++];
6031 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6032 aentry
->goto_pc
= arg
;
6033 emit_ge_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6036 emit_less_signed ();
6039 case gdb_agent_op_less_unsigned
:
6040 emit_less_unsigned ();
6043 case gdb_agent_op_ext
:
6044 arg
= aexpr
->bytes
[pc
++];
6045 if (arg
< (sizeof (LONGEST
) * 8))
6049 case gdb_agent_op_ref8
:
6053 case gdb_agent_op_ref16
:
6057 case gdb_agent_op_ref32
:
6061 case gdb_agent_op_ref64
:
6065 case gdb_agent_op_if_goto
:
6066 arg
= aexpr
->bytes
[pc
++];
6067 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6068 aentry
->goto_pc
= arg
;
6069 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6072 case gdb_agent_op_goto
:
6073 arg
= aexpr
->bytes
[pc
++];
6074 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6075 aentry
->goto_pc
= arg
;
6076 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6079 case gdb_agent_op_const8
:
6080 emit_stack_flush ();
6081 top
= aexpr
->bytes
[pc
++];
6085 case gdb_agent_op_const16
:
6086 emit_stack_flush ();
6087 top
= aexpr
->bytes
[pc
++];
6088 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6092 case gdb_agent_op_const32
:
6093 emit_stack_flush ();
6094 top
= aexpr
->bytes
[pc
++];
6095 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6096 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6097 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6101 case gdb_agent_op_const64
:
6102 emit_stack_flush ();
6103 top
= aexpr
->bytes
[pc
++];
6104 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6105 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6106 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6107 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6108 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6109 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6110 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6114 case gdb_agent_op_reg
:
6115 emit_stack_flush ();
6116 arg
= aexpr
->bytes
[pc
++];
6117 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6121 case gdb_agent_op_end
:
6122 trace_debug ("At end of expression\n");
6124 /* Assume there is one stack element left, and that it is
6125 cached in "top" where emit_epilogue can get to it. */
6126 emit_stack_adjust (1);
6131 case gdb_agent_op_dup
:
6132 /* In our design, dup is equivalent to stack flushing. */
6133 emit_stack_flush ();
6136 case gdb_agent_op_pop
:
6140 case gdb_agent_op_zero_ext
:
6141 arg
= aexpr
->bytes
[pc
++];
6142 if (arg
< (sizeof (LONGEST
) * 8))
6143 emit_zero_ext (arg
);
6146 case gdb_agent_op_swap
:
6147 next_op
= aexpr
->bytes
[pc
];
6148 /* Detect greater-than comparison sequences. */
6149 if (next_op
== gdb_agent_op_less_signed
6150 && !is_goto_target (aexpr
, pc
)
6151 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
6152 && !is_goto_target (aexpr
, pc
+ 1))
6154 trace_debug ("Combining swap & less_signed & if_goto");
6157 arg
= aexpr
->bytes
[pc
++];
6158 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6159 aentry
->goto_pc
= arg
;
6160 emit_gt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6162 else if (next_op
== gdb_agent_op_less_signed
6163 && !is_goto_target (aexpr
, pc
)
6164 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_log_not
)
6165 && !is_goto_target (aexpr
, pc
+ 1)
6166 && (aexpr
->bytes
[pc
+ 2] == gdb_agent_op_if_goto
)
6167 && !is_goto_target (aexpr
, pc
+ 2))
6169 trace_debug ("Combining swap & less_signed & log_not & if_goto");
6172 arg
= aexpr
->bytes
[pc
++];
6173 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6174 aentry
->goto_pc
= arg
;
6175 emit_le_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6181 case gdb_agent_op_getv
:
6182 emit_stack_flush ();
6183 arg
= aexpr
->bytes
[pc
++];
6184 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6185 emit_int_call_1 (ipa_sym_addrs
.addr_get_trace_state_variable_value
,
6189 case gdb_agent_op_setv
:
6190 arg
= aexpr
->bytes
[pc
++];
6191 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6192 emit_void_call_2 (ipa_sym_addrs
.addr_set_trace_state_variable_value
,
6196 case gdb_agent_op_tracev
:
6200 /* GDB never (currently) generates any of these ops. */
6201 case gdb_agent_op_float
:
6202 case gdb_agent_op_ref_float
:
6203 case gdb_agent_op_ref_double
:
6204 case gdb_agent_op_ref_long_double
:
6205 case gdb_agent_op_l_to_d
:
6206 case gdb_agent_op_d_to_l
:
6207 case gdb_agent_op_trace16
:
6212 trace_debug ("Agent expression op 0x%x not recognized\n", op
);
6213 /* Don't struggle on, things will just get worse. */
6214 return expr_eval_unrecognized_opcode
;
6217 /* This catches errors that occur in target-specific code
6221 trace_debug ("Error %d while emitting code for %s\n",
6222 emit_error
, gdb_agent_op_name (op
));
6223 return expr_eval_unhandled_opcode
;
6226 trace_debug ("Op %s compiled\n", gdb_agent_op_name (op
));
6229 /* Now fill in real addresses as goto destinations. */
6230 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
6234 if (aentry
->goto_pc
< 0)
6237 /* Find the location that we are going to, and call back into
6238 target-specific code to write the actual address or
6240 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
6242 if (aentry2
->pc
== aentry
->goto_pc
)
6244 trace_debug ("Want to jump from %s to %s\n",
6245 paddress (aentry
->address
),
6246 paddress (aentry2
->address
));
6247 write_goto_address (aentry
->address
+ aentry
->from_offset
,
6248 aentry2
->address
, aentry
->from_size
);
6254 /* Error out if we didn't find a destination. */
6257 trace_debug ("Destination of goto %d not found\n",
6259 return expr_eval_invalid_goto
;
6263 return expr_eval_no_error
;
6266 /* We'll need to adjust these when we consider bi-arch setups, and big
6270 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
6272 return write_inferior_memory (where
,
6273 (unsigned char *) &ptr
, sizeof (void *));
6276 /* The base pointer of the IPA's heap. This is the only memory the
6277 IPA is allowed to use. The IPA should _not_ call the inferior's
6278 `malloc' during operation. That'd be slow, and, most importantly,
6279 it may not be safe. We may be collecting a tracepoint in a signal
6280 handler, for example. */
6281 static CORE_ADDR target_tp_heap
;
6283 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
6287 target_malloc (ULONGEST size
)
6291 if (target_tp_heap
== 0)
6293 /* We have the pointer *address*, need what it points to. */
6294 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
6296 fatal ("could get target heap head pointer");
6299 ptr
= target_tp_heap
;
6300 target_tp_heap
+= size
;
6302 /* Pad to 8-byte alignment. */
6303 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
6309 download_agent_expr (struct agent_expr
*expr
)
6311 CORE_ADDR expr_addr
;
6312 CORE_ADDR expr_bytes
;
6314 expr_addr
= target_malloc (sizeof (*expr
));
6315 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
6317 expr_bytes
= target_malloc (expr
->length
);
6318 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
6320 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
6325 /* Align V up to N bits. */
6326 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
6329 download_tracepoints (void)
6331 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
6332 struct tracepoint
*tpoint
;
6334 /* Start out empty. */
6335 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
6337 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6339 struct tracepoint target_tracepoint
;
6341 if (tpoint
->type
!= fast_tracepoint
6342 && tpoint
->type
!= static_tracepoint
)
6345 /* Maybe download a compiled condition. */
6346 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
6348 CORE_ADDR jentry
, jump_entry
;
6350 jentry
= jump_entry
= get_jump_space_head ();
6352 if (tpoint
->cond
!= NULL
)
6354 /* Pad to 8-byte alignment. (needed?) */
6355 /* Actually this should be left for the target to
6357 jentry
= UALIGN (jentry
, 8);
6359 compile_tracepoint_condition (tpoint
, &jentry
);
6362 /* Pad to 8-byte alignment. */
6363 jentry
= UALIGN (jentry
, 8);
6364 claim_jump_space (jentry
- jump_entry
);
6367 target_tracepoint
= *tpoint
;
6370 tpptr
= target_malloc (sizeof (*tpoint
));
6371 tpoint
->obj_addr_on_target
= tpptr
;
6373 if (tpoint
== tracepoints
)
6375 /* First object in list, set the head pointer in the
6377 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
6381 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
6386 /* Write the whole object. We'll fix up its pointers in a bit.
6387 Assume no next for now. This is fixed up above on the next
6388 iteration, if there's any. */
6389 target_tracepoint
.next
= NULL
;
6390 /* Need to clear this here too, since we're downloading the
6391 tracepoints before clearing our own copy. */
6392 target_tracepoint
.hit_count
= 0;
6394 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
6395 sizeof (target_tracepoint
));
6398 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6400 download_agent_expr (tpoint
->cond
));
6402 if (tpoint
->numactions
)
6405 CORE_ADDR actions_array
;
6407 /* The pointers array. */
6409 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6410 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6414 /* Now for each pointer, download the action. */
6415 for (i
= 0; i
< tpoint
->numactions
; i
++)
6417 CORE_ADDR ipa_action
= 0;
6418 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6420 switch (action
->type
)
6424 = target_malloc (sizeof (struct collect_memory_action
));
6425 write_inferior_memory (ipa_action
,
6426 (unsigned char *) action
,
6427 sizeof (struct collect_memory_action
));
6431 = target_malloc (sizeof (struct collect_registers_action
));
6432 write_inferior_memory (ipa_action
,
6433 (unsigned char *) action
,
6434 sizeof (struct collect_registers_action
));
6439 struct eval_expr_action
*eaction
6440 = (struct eval_expr_action
*) action
;
6442 ipa_action
= target_malloc (sizeof (*eaction
));
6443 write_inferior_memory (ipa_action
,
6444 (unsigned char *) eaction
,
6447 expr
= download_agent_expr (eaction
->expr
);
6448 write_inferior_data_ptr
6449 (ipa_action
+ offsetof (struct eval_expr_action
, expr
),
6454 ipa_action
= target_malloc
6455 (sizeof (struct collect_static_trace_data_action
));
6456 write_inferior_memory
6458 (unsigned char *) action
,
6459 sizeof (struct collect_static_trace_data_action
));
6462 trace_debug ("unknown trace action '%c', ignoring",
6467 if (ipa_action
!= 0)
6468 write_inferior_data_ptr
6469 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
6477 download_trace_state_variables (void)
6479 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6480 struct trace_state_variable
*tsv
;
6482 /* Start out empty. */
6483 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6485 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6487 struct trace_state_variable target_tsv
;
6489 /* TSV's with a getter have been initialized equally in both the
6490 inferior and GDBserver. Skip them. */
6491 if (tsv
->getter
!= NULL
)
6497 ptr
= target_malloc (sizeof (*tsv
));
6499 if (tsv
== trace_state_variables
)
6501 /* First object in list, set the head pointer in the
6504 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6509 write_inferior_data_ptr (prev_ptr
6510 + offsetof (struct trace_state_variable
,
6515 /* Write the whole object. We'll fix up its pointers in a bit.
6516 Assume no next, fixup when needed. */
6517 target_tsv
.next
= NULL
;
6519 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6520 sizeof (target_tsv
));
6522 if (tsv
->name
!= NULL
)
6524 size_t size
= strlen (tsv
->name
) + 1;
6525 CORE_ADDR name_addr
= target_malloc (size
);
6526 write_inferior_memory (name_addr
,
6527 (unsigned char *) tsv
->name
, size
);
6528 write_inferior_data_ptr (ptr
6529 + offsetof (struct trace_state_variable
,
6534 if (tsv
->getter
!= NULL
)
6536 fatal ("what to do with these?");
6542 /* Fixup the next pointer in the last item in the list. */
6543 write_inferior_data_ptr (prev_ptr
6544 + offsetof (struct trace_state_variable
,
6549 /* Upload complete trace frames out of the IP Agent's trace buffer
6550 into GDBserver's trace buffer. This always uploads either all or
6551 no trace frames. This is the counter part of
6552 `trace_alloc_trace_buffer'. See its description of the atomic
6553 synching mechanism. */
6556 upload_fast_traceframes (void)
6558 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6559 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6561 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6562 unsigned int curr_tbctrl_idx
;
6563 unsigned int ipa_trace_buffer_ctrl_curr
;
6564 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6565 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6566 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6567 CORE_ADDR ipa_trace_buffer_lo
;
6568 CORE_ADDR ipa_trace_buffer_hi
;
6570 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6571 &ipa_traceframe_read_count_racy
))
6573 /* This will happen in most targets if the current thread is
6578 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6579 &ipa_traceframe_write_count_racy
))
6582 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6583 ipa_traceframe_write_count_racy
6584 - ipa_traceframe_read_count_racy
,
6585 ipa_traceframe_write_count_racy
,
6586 ipa_traceframe_read_count_racy
);
6588 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6591 about_to_request_buffer_space_bkpt
6592 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6595 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6596 &ipa_trace_buffer_ctrl_curr
))
6599 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6601 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6604 unsigned int prev
, counter
;
6606 /* Update the token, with new counters, and the GDBserver stamp
6607 bit. Alway reuse the current TBC index. */
6608 prev
= ipa_trace_buffer_ctrl_curr
& 0x0007ff00;
6609 counter
= (prev
+ 0x100) & 0x0007ff00;
6611 ipa_trace_buffer_ctrl_curr
= (0x80000000
6617 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6618 ipa_trace_buffer_ctrl_curr
))
6621 trace_debug ("Lib: Committed %08x -> %08x",
6622 ipa_trace_buffer_ctrl_curr_old
,
6623 ipa_trace_buffer_ctrl_curr
);
6625 /* Re-read these, now that we've installed the
6626 `about_to_request_buffer_space' breakpoint/lock. A thread could
6627 have finished a traceframe between the last read of these
6628 counters and setting the breakpoint above. If we start
6629 uploading, we never want to leave this function with
6630 traceframe_read_count != 0, otherwise, GDBserver could end up
6631 incrementing the counter tokens more than once (due to event loop
6632 nesting), which would break the IP agent's "effective" detection
6633 (see trace_alloc_trace_buffer). */
6634 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6635 &ipa_traceframe_read_count
))
6637 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6638 &ipa_traceframe_write_count
))
6643 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6644 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6645 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6647 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6648 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6649 trace_debug ("note that ipa_traceframe_count's parts changed");
6652 /* Get the address of the current TBC object (the IP agent has an
6653 array of 3 such objects). The index is stored in the TBC
6655 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6656 ipa_trace_buffer_ctrl_addr
6657 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6659 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6660 (unsigned char *) &ipa_trace_buffer_ctrl
,
6661 sizeof (struct ipa_trace_buffer_control
)))
6664 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6665 &ipa_trace_buffer_lo
))
6667 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6668 &ipa_trace_buffer_hi
))
6671 /* Offsets are easier to grok for debugging than raw addresses,
6672 especially for the small trace buffer sizes that are useful for
6674 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6675 "endfree=%d wrap=%d hi=%d",
6677 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6678 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6679 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6680 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6681 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6683 /* Note that the IPA's buffer is always circular. */
6685 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6687 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6688 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6690 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6691 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6692 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6693 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6696 tf
= IPA_FIRST_TRACEFRAME ();
6698 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6700 struct tracepoint
*tpoint
;
6701 struct traceframe
*tframe
;
6702 unsigned char *block
;
6703 struct traceframe ipa_tframe
;
6705 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6706 offsetof (struct traceframe
, data
)))
6707 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6709 if (ipa_tframe
.tpnum
== 0)
6710 fatal ("Uploading: No (more) fast traceframes, but "
6711 "ipa_traceframe_count == %u??\n",
6712 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6714 /* Note that this will be incorrect for multi-location
6716 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6718 tframe
= add_traceframe (tpoint
);
6721 trace_buffer_is_full
= 1;
6722 trace_debug ("Uploading: trace buffer is full");
6726 /* Copy the whole set of blocks in one go for now. FIXME:
6727 split this in smaller blocks. */
6728 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6731 if (read_inferior_memory (tf
6732 + offsetof (struct traceframe
, data
),
6733 block
, ipa_tframe
.data_size
))
6734 error ("Uploading: Couldn't read traceframe data at %s\n",
6735 paddress (tf
+ offsetof (struct traceframe
, data
)));
6738 trace_debug ("Uploading: traceframe didn't fit");
6739 finish_traceframe (tframe
);
6742 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6744 /* If we freed the traceframe that wrapped around, go back
6745 to the non-wrap case. */
6746 if (tf
< ipa_trace_buffer_ctrl
.start
)
6748 trace_debug ("Lib: Discarding past the wraparound");
6749 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6751 ipa_trace_buffer_ctrl
.start
= tf
;
6752 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6753 ++ipa_traceframe_read_count
;
6755 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6756 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6758 trace_debug ("Lib: buffer is fully empty. "
6759 "Trace buffer [%d] start=%d free=%d endfree=%d",
6761 (int) (ipa_trace_buffer_ctrl
.start
6762 - ipa_trace_buffer_lo
),
6763 (int) (ipa_trace_buffer_ctrl
.free
6764 - ipa_trace_buffer_lo
),
6765 (int) (ipa_trace_buffer_ctrl
.end_free
6766 - ipa_trace_buffer_lo
));
6768 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6769 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6770 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6771 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6774 trace_debug ("Uploaded a traceframe\n"
6775 "Lib: Trace buffer [%d] start=%d free=%d "
6776 "endfree=%d wrap=%d hi=%d",
6778 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6779 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6780 (int) (ipa_trace_buffer_ctrl
.end_free
6781 - ipa_trace_buffer_lo
),
6782 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6783 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6786 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6787 (unsigned char *) &ipa_trace_buffer_ctrl
,
6788 sizeof (struct ipa_trace_buffer_control
)))
6791 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6792 ipa_traceframe_read_count
);
6794 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6797 cancel_breakpoints ();
6799 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6800 about_to_request_buffer_space_bkpt
= NULL
;
6804 if (trace_buffer_is_full
)
6809 #ifdef IN_PROCESS_AGENT
6811 IP_AGENT_EXPORT
int ust_loaded
;
6812 IP_AGENT_EXPORT
char cmd_buf
[CMD_BUF_SIZE
];
6816 /* Static tracepoints. */
6818 /* UST puts a "struct tracepoint" in the global namespace, which
6819 conflicts with our tracepoint. Arguably, being a library, it
6820 shouldn't take ownership of such a generic name. We work around it
6822 #define tracepoint ust_tracepoint
6823 #include <ust/ust.h>
6826 extern int serialize_to_text (char *outbuf
, int bufsize
,
6827 const char *fmt
, va_list ap
);
6829 #define GDB_PROBE_NAME "gdb"
6831 /* We dynamically search for the UST symbols instead of linking them
6832 in. This lets the user decide if the application uses static
6833 tracepoints, instead of always pulling libust.so in. This vector
6834 holds pointers to all functions we care about. */
6838 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6839 const char *fmt
, va_list ap
);
6841 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6842 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6844 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6846 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6849 void (*marker_iter_start
) (struct marker_iter
*iter
);
6850 void (*marker_iter_next
) (struct marker_iter
*iter
);
6851 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6852 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6857 /* Cast through typeof to catch incompatible API changes. Since UST
6858 only builds with gcc, we can freely use gcc extensions here
6860 #define GET_UST_SYM(SYM) \
6863 if (ust_ops.SYM == NULL) \
6864 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6865 if (ust_ops.SYM == NULL) \
6869 #define USTF(SYM) ust_ops.SYM
6871 /* Get pointers to all libust.so functions we care about. */
6876 GET_UST_SYM (serialize_to_text
);
6878 GET_UST_SYM (ltt_probe_register
);
6879 GET_UST_SYM (ltt_probe_unregister
);
6880 GET_UST_SYM (ltt_marker_connect
);
6881 GET_UST_SYM (ltt_marker_disconnect
);
6883 GET_UST_SYM (marker_iter_start
);
6884 GET_UST_SYM (marker_iter_next
);
6885 GET_UST_SYM (marker_iter_stop
);
6886 GET_UST_SYM (marker_iter_reset
);
6892 /* Given an UST marker, return the matching gdb static tracepoint.
6893 The match is done by address. */
6895 static struct tracepoint
*
6896 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6898 struct tracepoint
*tpoint
;
6900 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6902 if (tpoint
->type
!= static_tracepoint
)
6905 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6912 /* The probe function we install on lttng/ust markers. Whenever a
6913 probed ust marker is hit, this function is called. This is similar
6914 to gdb_collect, only for static tracepoints, instead of fast
6918 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6919 struct registers
*regs
, void *call_private
,
6920 const char *fmt
, va_list *args
)
6922 struct tracepoint
*tpoint
;
6923 struct static_tracepoint_ctx ctx
;
6925 /* Don't do anything until the trace run is completely set up. */
6928 trace_debug ("gdb_probe: not tracing\n");
6932 ctx
.base
.type
= static_tracepoint
;
6933 ctx
.regcache_initted
= 0;
6938 /* Wrap the regblock in a register cache (in the stack, we don't
6939 want to malloc here). */
6940 ctx
.regspace
= alloca (register_cache_size ());
6941 if (ctx
.regspace
== NULL
)
6943 trace_debug ("Trace buffer block allocation failed, skipping");
6947 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6950 trace_debug ("gdb_probe: marker not known: "
6951 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6952 mdata
->location
, mdata
->channel
,
6953 mdata
->name
, mdata
->format
);
6957 if (!tpoint
->enabled
)
6959 trace_debug ("gdb_probe: tracepoint disabled");
6963 ctx
.tpoint
= tpoint
;
6965 trace_debug ("gdb_probe: collecting marker: "
6966 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6967 mdata
->location
, mdata
->channel
,
6968 mdata
->name
, mdata
->format
);
6970 /* Test the condition if present, and collect if true. */
6971 if (tpoint
->cond
== NULL
6972 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6975 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6976 tpoint
->address
, tpoint
);
6978 if (stopping_tracepoint
6979 || trace_buffer_is_full
6980 || expr_eval_result
!= expr_eval_no_error
)
6985 /* If there was a condition and it evaluated to false, the only
6986 way we would stop tracing is if there was an error during
6987 condition expression evaluation. */
6988 if (expr_eval_result
!= expr_eval_no_error
)
6993 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6994 static tracepoint string data. This is a string passed to the
6995 tracing library by the user, at the time of the tracepoint marker
6996 call. E.g., in the UST marker call:
6998 trace_mark (ust, bar33, "str %s", "FOOBAZ");
7000 the collected data is "str FOOBAZ".
7004 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
7006 struct tracepoint
*tpoint
,
7007 struct traceframe
*tframe
)
7009 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
7010 unsigned char *bufspace
;
7013 unsigned short blocklen
;
7017 trace_debug ("Wanted to collect static trace data, "
7018 "but there's no static trace data");
7022 va_copy (copy
, *umd
->args
);
7023 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
7026 trace_debug ("Want to collect ust data");
7028 /* 'S' + size + string */
7029 bufspace
= add_traceframe_block (tframe
,
7030 1 + sizeof (blocklen
) + size
+ 1);
7031 if (bufspace
== NULL
)
7033 trace_debug ("Trace buffer block allocation failed, skipping");
7037 /* Identify a static trace data block. */
7040 blocklen
= size
+ 1;
7041 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
7043 va_copy (copy
, *umd
->args
);
7044 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
7045 size
+ 1, umd
->fmt
, copy
);
7048 trace_debug ("Storing static tracepoint data in regblock: %s",
7049 bufspace
+ 1 + sizeof (blocklen
));
7052 /* The probe to register with lttng/ust. */
7053 static struct ltt_available_probe gdb_ust_probe
=
7060 #endif /* HAVE_UST */
7061 #endif /* IN_PROCESS_AGENT */
7065 #include <sys/socket.h>
7068 #ifndef UNIX_PATH_MAX
7069 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
7072 /* Where we put the socked used for synchronization. */
7073 #define SOCK_DIR P_tmpdir
7075 #endif /* HAVE_UST */
7077 #ifndef IN_PROCESS_AGENT
7082 gdb_ust_connect_sync_socket (int pid
)
7084 struct sockaddr_un addr
;
7086 char path
[UNIX_PATH_MAX
];
7088 res
= xsnprintf (path
, UNIX_PATH_MAX
, "%s/gdb_ust%d", SOCK_DIR
, pid
);
7089 if (res
>= UNIX_PATH_MAX
)
7091 trace_debug ("string overflow allocating socket name");
7095 res
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
7098 warning ("error opening sync socket: %s\n", strerror (errno
));
7102 addr
.sun_family
= AF_UNIX
;
7104 res
= xsnprintf (addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
7105 if (res
>= UNIX_PATH_MAX
)
7107 warning ("string overflow allocating socket name\n");
7112 res
= connect (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
7115 warning ("error connecting sync socket (%s): %s. "
7116 "Make sure the directory exists and that it is writable.",
7117 path
, strerror (errno
));
7125 /* Resume thread PTID. */
7128 resume_thread (ptid_t ptid
)
7130 struct thread_resume resume_info
;
7132 resume_info
.thread
= ptid
;
7133 resume_info
.kind
= resume_continue
;
7134 resume_info
.sig
= TARGET_SIGNAL_0
;
7135 (*the_target
->resume
) (&resume_info
, 1);
7138 /* Stop thread PTID. */
7141 stop_thread (ptid_t ptid
)
7143 struct thread_resume resume_info
;
7145 resume_info
.thread
= ptid
;
7146 resume_info
.kind
= resume_stop
;
7147 resume_info
.sig
= TARGET_SIGNAL_0
;
7148 (*the_target
->resume
) (&resume_info
, 1);
7151 /* Ask the in-process agent to run a command. Since we don't want to
7152 have to handle the IPA hitting breakpoints while running the
7153 command, we pause all threads, remove all breakpoints, and then set
7154 the helper thread re-running. We communicate with the helper
7155 thread by means of direct memory xfering, and a socket for
7159 run_inferior_command (char *cmd
)
7163 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
7165 ptid_t ptid
= null_ptid
;
7167 trace_debug ("run_inferior_command: running: %s", cmd
);
7170 uninsert_all_breakpoints ();
7172 if (read_inferior_integer (ipa_sym_addrs
.addr_helper_thread_id
, &tid
))
7174 warning ("Error reading helper thread's id in lib");
7180 warning ("helper thread not initialized yet");
7184 if (write_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
7185 (unsigned char *) cmd
, strlen (cmd
) + 1))
7187 warning ("Error writing command");
7191 ptid
= ptid_build (pid
, tid
, 0);
7193 resume_thread (ptid
);
7195 fd
= gdb_ust_connect_sync_socket (pid
);
7201 trace_debug ("signalling helper thread");
7205 ret
= write (fd
, buf
, 1);
7206 } while (ret
== -1 && errno
== EINTR
);
7208 trace_debug ("waiting for helper thread's response");
7212 ret
= read (fd
, buf
, 1);
7213 } while (ret
== -1 && errno
== EINTR
);
7217 trace_debug ("helper thread's response received");
7222 /* Need to read response with the inferior stopped. */
7223 if (!ptid_equal (ptid
, null_ptid
))
7225 int was_non_stop
= non_stop
;
7226 struct target_waitstatus status
;
7230 mywait (ptid
, &status
, 0, 0);
7231 non_stop
= was_non_stop
;
7236 if (read_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
7237 (unsigned char *) cmd
, CMD_BUF_SIZE
))
7239 warning ("Error reading command response");
7244 trace_debug ("run_inferior_command: response: %s", cmd
);
7248 reinsert_all_breakpoints ();
7254 #else /* HAVE_UST */
7257 run_inferior_command (char *cmd
)
7262 #endif /* HAVE_UST */
7264 #else /* !IN_PROCESS_AGENT */
7266 /* Thread ID of the helper thread. GDBserver reads this to know which
7267 is the help thread. This is an LWP id on Linux. */
7268 int helper_thread_id
;
7273 init_named_socket (const char *name
)
7276 struct sockaddr_un addr
;
7278 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
7281 warning ("socket creation failed: %s", strerror (errno
));
7285 addr
.sun_family
= AF_UNIX
;
7287 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
7288 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
7290 result
= access (name
, F_OK
);
7294 result
= unlink (name
);
7297 warning ("unlink failed: %s", strerror (errno
));
7301 warning ("socket %s already exists; overwriting", name
);
7304 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
7307 warning ("bind failed: %s", strerror (errno
));
7312 result
= listen (fd
, 1);
7315 warning ("listen: %s", strerror (errno
));
7324 gdb_ust_socket_init (void)
7327 char name
[UNIX_PATH_MAX
];
7329 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
7330 SOCK_DIR
, getpid ());
7331 if (result
>= UNIX_PATH_MAX
)
7333 trace_debug ("string overflow allocating socket name");
7337 fd
= init_named_socket (name
);
7339 warning ("Error initializing named socket (%s) for communication with the "
7340 "ust helper thread. Check that directory exists and that it "
7341 "is writable.", name
);
7346 /* Return an hexstr version of the STR C string, fit for sending to
7350 cstr_to_hexstr (const char *str
)
7352 int len
= strlen (str
);
7353 char *hexstr
= xmalloc (len
* 2 + 1);
7354 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
7358 /* The next marker to be returned on a qTsSTM command. */
7359 static const struct marker
*next_st
;
7361 /* Returns the first known marker. */
7366 struct marker_iter iter
;
7368 USTF(marker_iter_reset
) (&iter
);
7369 USTF(marker_iter_start
) (&iter
);
7374 /* Returns the marker following M. */
7376 const struct marker
*
7377 next_marker (const struct marker
*m
)
7379 struct marker_iter iter
;
7381 USTF(marker_iter_reset
) (&iter
);
7382 USTF(marker_iter_start
) (&iter
);
7384 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7386 if (iter
.marker
== m
)
7388 USTF(marker_iter_next
) (&iter
);
7396 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
7400 response_ust_marker (char *packet
, const struct marker
*st
)
7402 char *strid
, *format
, *tmp
;
7404 next_st
= next_marker (st
);
7406 tmp
= xmalloc (strlen (st
->channel
) + 1 +
7407 strlen (st
->name
) + 1);
7408 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
7410 strid
= cstr_to_hexstr (tmp
);
7413 format
= cstr_to_hexstr (st
->format
);
7415 sprintf (packet
, "m%s:%s:%s",
7416 paddress ((uintptr_t) st
->location
),
7424 /* Return the first static tracepoint, and initialize the state
7425 machine that will iterate through all the static tracepoints. */
7428 cmd_qtfstm (char *packet
)
7430 trace_debug ("Returning first trace state variable definition");
7432 if (first_marker ())
7433 response_ust_marker (packet
, first_marker ());
7435 strcpy (packet
, "l");
7438 /* Return additional trace state variable definitions. */
7441 cmd_qtsstm (char *packet
)
7443 trace_debug ("Returning static tracepoint");
7446 response_ust_marker (packet
, next_st
);
7448 strcpy (packet
, "l");
7451 /* Disconnect the GDB probe from a marker at a given address. */
7454 unprobe_marker_at (char *packet
)
7458 struct marker_iter iter
;
7460 p
+= sizeof ("unprobe_marker_at:") - 1;
7462 p
= unpack_varlen_hex (p
, &address
);
7464 USTF(marker_iter_reset
) (&iter
);
7465 USTF(marker_iter_start
) (&iter
);
7466 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7467 if ((uintptr_t ) iter
.marker
->location
== address
)
7471 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7475 warning ("could not disable marker %s/%s",
7476 iter
.marker
->channel
, iter
.marker
->name
);
7481 /* Connect the GDB probe to a marker at a given address. */
7484 probe_marker_at (char *packet
)
7488 struct marker_iter iter
;
7491 p
+= sizeof ("probe_marker_at:") - 1;
7493 p
= unpack_varlen_hex (p
, &address
);
7495 USTF(marker_iter_reset
) (&iter
);
7497 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7499 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7500 if ((uintptr_t ) m
->location
== address
)
7504 trace_debug ("found marker for address. "
7505 "ltt_marker_connect (marker = %s/%s)",
7506 m
->channel
, m
->name
);
7508 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7510 if (result
&& result
!= -EEXIST
)
7511 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7512 m
->channel
, m
->name
, -result
);
7516 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7517 m
->channel
, m
->name
);
7521 strcpy (packet
, "OK");
7525 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7530 cmd_qtstmat (char *packet
)
7534 struct marker_iter iter
;
7537 p
+= sizeof ("qTSTMat:") - 1;
7539 p
= unpack_varlen_hex (p
, &address
);
7541 USTF(marker_iter_reset
) (&iter
);
7543 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7545 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7546 if ((uintptr_t ) m
->location
== address
)
7548 response_ust_marker (packet
, m
);
7552 strcpy (packet
, "l");
7557 gdb_ust_thread (void *arg
)
7563 listen_fd
= gdb_ust_socket_init ();
7566 if (helper_thread_id
== 0)
7567 helper_thread_id
= syscall (SYS_gettid
);
7570 if (listen_fd
== -1)
7572 warning ("could not create sync socket\n");
7579 struct sockaddr_un sockaddr
;
7584 tmp
= sizeof (sockaddr
);
7588 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7590 /* It seems an ERESTARTSYS can escape out of accept. */
7591 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7595 warning ("Accept returned %d, error: %s\n",
7596 fd
, strerror (errno
));
7602 ret
= read (fd
, buf
, 1);
7603 } while (ret
== -1 && errno
== EINTR
);
7607 warning ("reading socket (fd=%d) failed with %s",
7608 fd
, strerror (errno
));
7615 if (strcmp ("qTfSTM", cmd_buf
) == 0)
7617 cmd_qtfstm (cmd_buf
);
7619 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7621 cmd_qtsstm (cmd_buf
);
7623 else if (strncmp ("unprobe_marker_at:",
7625 sizeof ("unprobe_marker_at:") - 1) == 0)
7627 unprobe_marker_at (cmd_buf
);
7629 else if (strncmp ("probe_marker_at:",
7631 sizeof ("probe_marker_at:") - 1) == 0)
7633 probe_marker_at (cmd_buf
);
7635 else if (strncmp ("qTSTMat:",
7637 sizeof ("qTSTMat:") - 1) == 0)
7639 cmd_qtstmat (cmd_buf
);
7641 else if (strcmp (cmd_buf
, "help") == 0)
7643 strcpy (cmd_buf
, "for help, press F1\n");
7646 strcpy (cmd_buf
, "");
7670 /* We want the helper thread to be as transparent as possible, so
7671 have it inherit an all-signals-blocked mask. */
7673 sigfillset (&new_mask
);
7674 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7676 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7678 res
= pthread_create (&thread
,
7683 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7685 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7687 while (helper_thread_id
== 0)
7690 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7693 #endif /* HAVE_UST */
7695 #include <sys/mman.h>
7698 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7699 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7700 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7702 static void __attribute__ ((constructor
))
7703 initialize_tracepoint_ftlib (void)
7705 initialize_tracepoint ();
7712 #endif /* IN_PROCESS_AGENT */
7715 tsv_get_timestamp (void)
7719 if (gettimeofday (&tv
, 0) != 0)
7722 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7726 initialize_tracepoint (void)
7728 /* There currently no way to change the buffer size. */
7729 const int sizeOfBuffer
= 5 * 1024 * 1024;
7730 unsigned char *buf
= xmalloc (sizeOfBuffer
);
7731 init_trace_buffer (buf
, sizeOfBuffer
);
7733 /* Wire trace state variable 1 to be the timestamp. This will be
7734 uploaded to GDB upon connection and become one of its trace state
7735 variables. (In case you're wondering, if GDB already has a trace
7736 variable numbered 1, it will be renumbered.) */
7737 create_trace_state_variable (1, 0);
7738 set_trace_state_variable_name (1, "trace_timestamp");
7739 set_trace_state_variable_getter (1, tsv_get_timestamp
);
7741 #ifdef IN_PROCESS_AGENT
7744 pagesize
= sysconf (_SC_PAGE_SIZE
);
7748 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7750 /* Allocate scratch buffer aligned on a page boundary. */
7751 gdb_jump_pad_buffer
= memalign (pagesize
, pagesize
* 20);
7752 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* 20;
7754 /* Make it writable and executable. */
7755 if (mprotect (gdb_jump_pad_buffer
, pagesize
* 20,
7756 PROT_READ
| PROT_WRITE
| PROT_EXEC
) != 0)
7758 initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
7759 gdb_jump_pad_buffer
, pagesize
* 20, strerror (errno
));
7762 initialize_low_tracepoint ();