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
);
1248 static int install_fast_tracepoint (struct tracepoint
*);
1251 #if defined(__GNUC__)
1252 # define memory_barrier() asm volatile ("" : : : "memory")
1254 # define memory_barrier() do {} while (0)
1257 /* We only build the IPA if this builtin is supported, and there are
1258 no uses of this in GDBserver itself, so we're safe in defining this
1260 #define cmpxchg(mem, oldval, newval) \
1261 __sync_val_compare_and_swap (mem, oldval, newval)
1263 /* The size in bytes of the buffer used to talk to the IPA helper
1265 #define CMD_BUF_SIZE 1024
1267 /* Record that an error occurred during expression evaluation. */
1270 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1271 enum eval_result_type rtype
)
1273 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1274 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1276 #ifdef IN_PROCESS_AGENT
1277 /* Only record the first error we get. */
1278 if (cmpxchg (&expr_eval_result
,
1280 rtype
) != expr_eval_no_error
)
1283 if (expr_eval_result
!= expr_eval_no_error
)
1287 error_tracepoint
= tpoint
;
1290 /* Trace buffer management. */
1293 clear_trace_buffer (void)
1295 trace_buffer_start
= trace_buffer_lo
;
1296 trace_buffer_free
= trace_buffer_lo
;
1297 trace_buffer_end_free
= trace_buffer_hi
;
1298 trace_buffer_wrap
= trace_buffer_hi
;
1299 /* A traceframe with zeroed fields marks the end of trace data. */
1300 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1301 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1302 traceframe_read_count
= traceframe_write_count
= 0;
1303 traceframes_created
= 0;
1306 #ifndef IN_PROCESS_AGENT
1309 clear_inferior_trace_buffer (void)
1311 CORE_ADDR ipa_trace_buffer_lo
;
1312 CORE_ADDR ipa_trace_buffer_hi
;
1313 struct traceframe ipa_traceframe
= { 0 };
1314 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1316 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1317 &ipa_trace_buffer_lo
);
1318 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1319 &ipa_trace_buffer_hi
);
1321 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1322 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1323 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1324 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1326 /* A traceframe with zeroed fields marks the end of trace data. */
1327 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1328 (unsigned char *) &ipa_trace_buffer_ctrl
,
1329 sizeof (ipa_trace_buffer_ctrl
));
1331 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1333 /* A traceframe with zeroed fields marks the end of trace data. */
1334 write_inferior_memory (ipa_trace_buffer_lo
,
1335 (unsigned char *) &ipa_traceframe
,
1336 sizeof (ipa_traceframe
));
1338 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1339 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1340 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1346 init_trace_buffer (unsigned char *buf
, int bufsize
)
1348 trace_buffer_lo
= buf
;
1349 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1351 clear_trace_buffer ();
1354 #ifdef IN_PROCESS_AGENT
1356 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1357 about_to_request_buffer_space (void)
1359 /* GDBserver places breakpoint here while it goes about to flush
1360 data at random times. */
1361 UNKNOWN_SIDE_EFFECTS();
1366 /* Carve out a piece of the trace buffer, returning NULL in case of
1370 trace_buffer_alloc (size_t amt
)
1372 unsigned char *rslt
;
1373 struct trace_buffer_control
*tbctrl
;
1375 #ifdef IN_PROCESS_AGENT
1376 unsigned int prev
, prev_filtered
;
1377 unsigned int commit_count
;
1378 unsigned int commit
;
1379 unsigned int readout
;
1381 struct traceframe
*oldest
;
1382 unsigned char *new_start
;
1385 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1386 (long) amt
, (long) sizeof (struct traceframe
));
1388 /* Account for the EOB marker. */
1389 amt
+= sizeof (struct traceframe
);
1391 #ifdef IN_PROCESS_AGENT
1395 /* Read the current token and extract the index to try to write to,
1396 storing it in CURR. */
1397 prev
= trace_buffer_ctrl_curr
;
1398 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1399 curr
= prev_filtered
+ 1;
1403 about_to_request_buffer_space ();
1405 /* Start out with a copy of the current state. GDBserver may be
1406 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1407 be able to commit anyway if that happens. */
1408 trace_buffer_ctrl
[curr
]
1409 = trace_buffer_ctrl
[prev_filtered
];
1410 trace_debug ("trying curr=%u", curr
);
1412 /* The GDBserver's agent doesn't need all that syncing, and always
1413 updates TCB 0 (there's only one, mind you). */
1416 tbctrl
= &trace_buffer_ctrl
[curr
];
1418 /* Offsets are easier to grok for debugging than raw addresses,
1419 especially for the small trace buffer sizes that are useful for
1421 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1423 (int) (tbctrl
->start
- trace_buffer_lo
),
1424 (int) (tbctrl
->free
- trace_buffer_lo
),
1425 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1426 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1427 (int) (trace_buffer_hi
- trace_buffer_lo
));
1429 /* The algorithm here is to keep trying to get a contiguous block of
1430 the requested size, possibly discarding older traceframes to free
1431 up space. Since free space might come in one or two pieces,
1432 depending on whether discarded traceframes wrapped around at the
1433 high end of the buffer, we test both pieces after each
1437 /* First, if we have two free parts, try the upper one first. */
1438 if (tbctrl
->end_free
< tbctrl
->free
)
1440 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1441 /* We have enough in the upper part. */
1445 /* Our high part of free space wasn't enough. Give up
1446 on it for now, set wraparound. We will recover the
1447 space later, if/when the wrapped-around traceframe is
1449 trace_debug ("Upper part too small, setting wraparound");
1450 tbctrl
->wrap
= tbctrl
->free
;
1451 tbctrl
->free
= trace_buffer_lo
;
1455 /* The normal case. */
1456 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1459 #ifdef IN_PROCESS_AGENT
1460 /* The IP Agent's buffer is always circular. It isn't used
1461 currently, but `circular_trace_buffer' could represent
1462 GDBserver's mode. If we didn't find space, ask GDBserver to
1465 flush_trace_buffer ();
1469 trace_debug ("gdbserver flushed buffer, retrying");
1473 /* GDBserver cancelled the tracing. Bail out as well. */
1476 /* If we're here, then neither part is big enough, and
1477 non-circular trace buffers are now full. */
1478 if (!circular_trace_buffer
)
1480 trace_debug ("Not enough space in the trace buffer");
1484 trace_debug ("Need more space in the trace buffer");
1486 /* If we have a circular buffer, we can try discarding the
1487 oldest traceframe and see if that helps. */
1488 oldest
= FIRST_TRACEFRAME ();
1489 if (oldest
->tpnum
== 0)
1491 /* Not good; we have no traceframes to free. Perhaps we're
1492 asking for a block that is larger than the buffer? In
1493 any case, give up. */
1494 trace_debug ("No traceframes to discard");
1498 /* We don't run this code in the in-process agent currently.
1499 E.g., we could leave the in-process agent in autonomous
1500 circular mode if we only have fast tracepoints. If we do
1501 that, then this bit becomes racy with GDBserver, which also
1502 writes to this counter. */
1503 --traceframe_write_count
;
1505 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1506 /* If we freed the traceframe that wrapped around, go back
1507 to the non-wrap case. */
1508 if (new_start
< tbctrl
->start
)
1510 trace_debug ("Discarding past the wraparound");
1511 tbctrl
->wrap
= trace_buffer_hi
;
1513 tbctrl
->start
= new_start
;
1514 tbctrl
->end_free
= tbctrl
->start
;
1516 trace_debug ("Discarded a traceframe\n"
1517 "Trace buffer [%d], start=%d free=%d "
1518 "endfree=%d wrap=%d hi=%d",
1520 (int) (tbctrl
->start
- trace_buffer_lo
),
1521 (int) (tbctrl
->free
- trace_buffer_lo
),
1522 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1523 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1524 (int) (trace_buffer_hi
- trace_buffer_lo
));
1526 /* Now go back around the loop. The discard might have resulted
1527 in either one or two pieces of free space, so we want to try
1528 both before freeing any more traceframes. */
1532 /* If we get here, we know we can provide the asked-for space. */
1534 rslt
= tbctrl
->free
;
1536 /* Adjust the request back down, now that we know we have space for
1537 the marker, but don't commit to AMT yet, we may still need to
1538 restart the operation if GDBserver touches the trace buffer
1539 (obviously only important in the in-process agent's version). */
1540 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1542 /* Or not. If GDBserver changed the trace buffer behind our back,
1543 we get to restart a new allocation attempt. */
1545 #ifdef IN_PROCESS_AGENT
1546 /* Build the tentative token. */
1547 commit_count
= (((prev
& 0x0007ff00) + 0x100) & 0x0007ff00);
1548 commit
= (((prev
& 0x0007ff00) << 12)
1552 /* Try to commit it. */
1553 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1554 if (readout
!= prev
)
1556 trace_debug ("GDBserver has touched the trace buffer, restarting."
1557 " (prev=%08x, commit=%08x, readout=%08x)",
1558 prev
, commit
, readout
);
1562 /* Hold your horses here. Even if that change was committed,
1563 GDBserver could come in, and clobber it. We need to hold to be
1564 able to tell if GDBserver clobbers before or after we committed
1565 the change. Whenever GDBserver goes about touching the IPA
1566 buffer, it sets a breakpoint in this routine, so we have a sync
1568 about_to_request_buffer_space ();
1570 /* Check if the change has been effective, even if GDBserver stopped
1571 us at the breakpoint. */
1574 unsigned int refetch
;
1578 refetch
= trace_buffer_ctrl_curr
;
1580 if ((refetch
== commit
1581 || ((refetch
& 0x7ff00000) >> 12) == commit_count
))
1584 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1585 "readout=%08x, refetch=%08x)",
1586 prev
, commit
, readout
, refetch
);
1590 trace_debug ("GDBserver has touched the trace buffer, not effective."
1591 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1592 prev
, commit
, readout
, refetch
);
1598 /* We have a new piece of the trace buffer. Hurray! */
1600 /* Add an EOB marker just past this allocation. */
1601 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1602 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1604 /* Adjust the request back down, now that we know we have space for
1606 amt
-= sizeof (struct traceframe
);
1610 trace_debug ("Allocated %d bytes", (int) amt
);
1611 trace_debug ("Trace buffer [%d] start=%d free=%d "
1612 "endfree=%d wrap=%d hi=%d",
1614 (int) (tbctrl
->start
- trace_buffer_lo
),
1615 (int) (tbctrl
->free
- trace_buffer_lo
),
1616 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1617 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1618 (int) (trace_buffer_hi
- trace_buffer_lo
));
1624 #ifndef IN_PROCESS_AGENT
1626 /* Return the total free space. This is not necessarily the largest
1627 block we can allocate, because of the two-part case. */
1632 if (trace_buffer_free
<= trace_buffer_end_free
)
1633 return trace_buffer_end_free
- trace_buffer_free
;
1635 return ((trace_buffer_end_free
- trace_buffer_lo
)
1636 + (trace_buffer_hi
- trace_buffer_free
));
1639 /* An 'S' in continuation packets indicates remainder are for
1642 static int seen_step_action_flag
;
1644 /* Create a tracepoint (location) with given number and address. */
1646 static struct tracepoint
*
1647 add_tracepoint (int num
, CORE_ADDR addr
)
1649 struct tracepoint
*tpoint
;
1651 tpoint
= xmalloc (sizeof (struct tracepoint
));
1652 tpoint
->number
= num
;
1653 tpoint
->address
= addr
;
1654 tpoint
->numactions
= 0;
1655 tpoint
->actions
= NULL
;
1656 tpoint
->actions_str
= NULL
;
1657 tpoint
->cond
= NULL
;
1658 tpoint
->num_step_actions
= 0;
1659 tpoint
->step_actions
= NULL
;
1660 tpoint
->step_actions_str
= NULL
;
1661 /* Start all off as regular (slow) tracepoints. */
1662 tpoint
->type
= trap_tracepoint
;
1663 tpoint
->orig_size
= -1;
1664 tpoint
->source_strings
= NULL
;
1665 tpoint
->compiled_cond
= 0;
1666 tpoint
->handle
= NULL
;
1667 tpoint
->next
= NULL
;
1669 if (!last_tracepoint
)
1670 tracepoints
= tpoint
;
1672 last_tracepoint
->next
= tpoint
;
1673 last_tracepoint
= tpoint
;
1675 seen_step_action_flag
= 0;
1680 #ifndef IN_PROCESS_AGENT
1682 /* Return the tracepoint with the given number and address, or NULL. */
1684 static struct tracepoint
*
1685 find_tracepoint (int id
, CORE_ADDR addr
)
1687 struct tracepoint
*tpoint
;
1689 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1690 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1696 /* There may be several tracepoints with the same number (because they
1697 are "locations", in GDB parlance); return the next one after the
1698 given tracepoint, or search from the beginning of the list if the
1699 first argument is NULL. */
1701 static struct tracepoint
*
1702 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1704 struct tracepoint
*tpoint
;
1707 tpoint
= prev_tp
->next
;
1709 tpoint
= tracepoints
;
1710 for (; tpoint
; tpoint
= tpoint
->next
)
1711 if (tpoint
->number
== num
)
1720 save_string (const char *str
, size_t len
)
1724 s
= xmalloc (len
+ 1);
1725 memcpy (s
, str
, len
);
1731 /* Append another action to perform when the tracepoint triggers. */
1734 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1740 seen_step_action_flag
= 1;
1748 char *act_start
= act
;
1749 struct tracepoint_action
*action
= NULL
;
1755 struct collect_memory_action
*maction
;
1759 maction
= xmalloc (sizeof *maction
);
1760 maction
->base
.type
= *act
;
1761 action
= &maction
->base
;
1764 is_neg
= (*act
== '-');
1767 act
= unpack_varlen_hex (act
, &basereg
);
1769 act
= unpack_varlen_hex (act
, &maction
->addr
);
1771 act
= unpack_varlen_hex (act
, &maction
->len
);
1772 maction
->basereg
= (is_neg
1775 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1776 pulongest (maction
->len
),
1777 paddress (maction
->addr
), maction
->basereg
);
1782 struct collect_registers_action
*raction
;
1784 raction
= xmalloc (sizeof *raction
);
1785 raction
->base
.type
= *act
;
1786 action
= &raction
->base
;
1788 trace_debug ("Want to collect registers");
1790 /* skip past hex digits of mask for now */
1791 while (isxdigit(*act
))
1797 struct collect_static_trace_data_action
*raction
;
1799 raction
= xmalloc (sizeof *raction
);
1800 raction
->base
.type
= *act
;
1801 action
= &raction
->base
;
1803 trace_debug ("Want to collect static trace data");
1808 trace_debug ("Unexpected step action, ignoring");
1813 struct eval_expr_action
*xaction
;
1815 xaction
= xmalloc (sizeof (*xaction
));
1816 xaction
->base
.type
= *act
;
1817 action
= &xaction
->base
;
1819 trace_debug ("Want to evaluate expression");
1820 xaction
->expr
= parse_agent_expr (&act
);
1824 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1833 if (seen_step_action_flag
)
1835 tpoint
->num_step_actions
++;
1837 tpoint
->step_actions
1838 = xrealloc (tpoint
->step_actions
,
1839 (sizeof (*tpoint
->step_actions
)
1840 * tpoint
->num_step_actions
));
1841 tpoint
->step_actions_str
1842 = xrealloc (tpoint
->step_actions_str
,
1843 (sizeof (*tpoint
->step_actions_str
)
1844 * tpoint
->num_step_actions
));
1845 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1846 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1847 = save_string (act_start
, act
- act_start
);
1851 tpoint
->numactions
++;
1853 = xrealloc (tpoint
->actions
,
1854 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1856 = xrealloc (tpoint
->actions_str
,
1857 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1858 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1859 tpoint
->actions_str
[tpoint
->numactions
- 1]
1860 = save_string (act_start
, act
- act_start
);
1867 /* Find or create a trace state variable with the given number. */
1869 static struct trace_state_variable
*
1870 get_trace_state_variable (int num
)
1872 struct trace_state_variable
*tsv
;
1874 #ifdef IN_PROCESS_AGENT
1875 /* Search for an existing variable. */
1876 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1877 if (tsv
->number
== num
)
1881 /* Search for an existing variable. */
1882 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1883 if (tsv
->number
== num
)
1889 /* Find or create a trace state variable with the given number. */
1891 static struct trace_state_variable
*
1892 create_trace_state_variable (int num
, int gdb
)
1894 struct trace_state_variable
*tsv
;
1896 tsv
= get_trace_state_variable (num
);
1900 /* Create a new variable. */
1901 tsv
= xmalloc (sizeof (struct trace_state_variable
));
1903 tsv
->initial_value
= 0;
1907 #ifdef IN_PROCESS_AGENT
1910 tsv
->next
= alloced_trace_state_variables
;
1911 alloced_trace_state_variables
= tsv
;
1916 tsv
->next
= trace_state_variables
;
1917 trace_state_variables
= tsv
;
1922 IP_AGENT_EXPORT LONGEST
1923 get_trace_state_variable_value (int num
)
1925 struct trace_state_variable
*tsv
;
1927 tsv
= get_trace_state_variable (num
);
1931 trace_debug ("No trace state variable %d, skipping value get", num
);
1935 /* Call a getter function if we have one. While it's tempting to
1936 set up something to only call the getter once per tracepoint hit,
1937 it could run afoul of thread races. Better to let the getter
1938 handle it directly, if necessary to worry about it. */
1940 tsv
->value
= (tsv
->getter
) ();
1942 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1943 num
, plongest (tsv
->value
));
1948 IP_AGENT_EXPORT
void
1949 set_trace_state_variable_value (int num
, LONGEST val
)
1951 struct trace_state_variable
*tsv
;
1953 tsv
= get_trace_state_variable (num
);
1957 trace_debug ("No trace state variable %d, skipping value set", num
);
1965 set_trace_state_variable_name (int num
, const char *name
)
1967 struct trace_state_variable
*tsv
;
1969 tsv
= get_trace_state_variable (num
);
1973 trace_debug ("No trace state variable %d, skipping name set", num
);
1977 tsv
->name
= (char *) name
;
1981 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
1983 struct trace_state_variable
*tsv
;
1985 tsv
= get_trace_state_variable (num
);
1989 trace_debug ("No trace state variable %d, skipping getter set", num
);
1993 tsv
->getter
= getter
;
1996 /* Add a raw traceframe for the given tracepoint. */
1998 static struct traceframe
*
1999 add_traceframe (struct tracepoint
*tpoint
)
2001 struct traceframe
*tframe
;
2003 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2008 tframe
->tpnum
= tpoint
->number
;
2009 tframe
->data_size
= 0;
2014 /* Add a block to the traceframe currently being worked on. */
2016 static unsigned char *
2017 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2019 unsigned char *block
;
2024 block
= trace_buffer_alloc (amt
);
2029 tframe
->data_size
+= amt
;
2034 /* Flag that the current traceframe is finished. */
2037 finish_traceframe (struct traceframe
*tframe
)
2039 ++traceframe_write_count
;
2040 ++traceframes_created
;
2043 #ifndef IN_PROCESS_AGENT
2045 /* Given a traceframe number NUM, find the NUMth traceframe in the
2048 static struct traceframe
*
2049 find_traceframe (int num
)
2051 struct traceframe
*tframe
;
2054 for (tframe
= FIRST_TRACEFRAME ();
2056 tframe
= NEXT_TRACEFRAME (tframe
))
2067 get_traceframe_address (struct traceframe
*tframe
)
2070 struct tracepoint
*tpoint
;
2072 addr
= traceframe_get_pc (tframe
);
2077 /* Fallback strategy, will be incorrect for while-stepping frames
2078 and multi-location tracepoints. */
2079 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2080 return tpoint
->address
;
2083 /* Search for the next traceframe whose address is inside or outside
2086 static struct traceframe
*
2087 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2090 struct traceframe
*tframe
;
2093 *tfnump
= current_traceframe
+ 1;
2094 tframe
= find_traceframe (*tfnump
);
2095 /* The search is not supposed to wrap around. */
2102 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2104 tfaddr
= get_traceframe_address (tframe
);
2106 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2107 : (lo
> tfaddr
|| tfaddr
> hi
))
2116 /* Search for the next traceframe recorded by the given tracepoint.
2117 Note that for multi-location tracepoints, this will find whatever
2118 location appears first. */
2120 static struct traceframe
*
2121 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2123 struct traceframe
*tframe
;
2125 *tfnump
= current_traceframe
+ 1;
2126 tframe
= find_traceframe (*tfnump
);
2127 /* The search is not supposed to wrap around. */
2134 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2136 if (tframe
->tpnum
== num
)
2147 #ifndef IN_PROCESS_AGENT
2149 /* Clear all past trace state. */
2152 cmd_qtinit (char *packet
)
2154 struct trace_state_variable
*tsv
, *prev
, *next
;
2156 /* Make sure we don't try to read from a trace frame. */
2157 current_traceframe
= -1;
2159 trace_debug ("Initializing the trace");
2161 clear_installed_tracepoints ();
2162 clear_readonly_regions ();
2165 last_tracepoint
= NULL
;
2167 /* Clear out any leftover trace state variables. Ones with target
2168 defined getters should be kept however. */
2170 tsv
= trace_state_variables
;
2173 trace_debug ("Looking at var %d", tsv
->number
);
2174 if (tsv
->getter
== NULL
)
2180 trace_state_variables
= next
;
2181 trace_debug ("Deleting var %d", tsv
->number
);
2192 clear_trace_buffer ();
2193 clear_inferior_trace_buffer ();
2198 /* Unprobe the UST marker at ADDRESS. */
2201 unprobe_marker_at (CORE_ADDR address
)
2203 char cmd
[CMD_BUF_SIZE
];
2205 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2206 run_inferior_command (cmd
);
2209 /* Restore the program to its pre-tracing state. This routine may be called
2210 in error situations, so it needs to be careful about only restoring
2211 from known-valid bits. */
2214 clear_installed_tracepoints (void)
2216 struct tracepoint
*tpoint
;
2217 struct tracepoint
*prev_stpoint
;
2220 cancel_breakpoints ();
2222 prev_stpoint
= NULL
;
2224 /* Restore any bytes overwritten by tracepoints. */
2225 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2227 /* Catch the case where we might try to remove a tracepoint that
2228 was never actually installed. */
2229 if (tpoint
->handle
== NULL
)
2231 trace_debug ("Tracepoint %d at 0x%s was "
2232 "never installed, nothing to clear",
2233 tpoint
->number
, paddress (tpoint
->address
));
2237 switch (tpoint
->type
)
2239 case trap_tracepoint
:
2240 delete_breakpoint (tpoint
->handle
);
2242 case fast_tracepoint
:
2243 delete_fast_tracepoint_jump (tpoint
->handle
);
2245 case static_tracepoint
:
2246 if (prev_stpoint
!= NULL
2247 && prev_stpoint
->address
== tpoint
->address
)
2248 /* Nothing to do. We already unprobed a tracepoint set at
2249 this marker address (and there can only be one probe
2254 unprobe_marker_at (tpoint
->address
);
2255 prev_stpoint
= tpoint
;
2260 tpoint
->handle
= NULL
;
2266 /* Parse a packet that defines a tracepoint. */
2269 cmd_qtdp (char *own_buf
)
2275 struct tracepoint
*tpoint
;
2277 char *packet
= own_buf
;
2279 packet
+= strlen ("QTDP:");
2281 /* A hyphen at the beginning marks a packet specifying actions for a
2282 tracepoint already supplied. */
2289 packet
= unpack_varlen_hex (packet
, &num
);
2290 ++packet
; /* skip a colon */
2291 packet
= unpack_varlen_hex (packet
, &addr
);
2292 ++packet
; /* skip a colon */
2294 /* See if we already have this tracepoint. */
2295 tpoint
= find_tracepoint (num
, addr
);
2299 /* Duplicate tracepoints are never allowed. */
2302 trace_debug ("Tracepoint error: tracepoint %d"
2303 " at 0x%s already exists",
2304 (int) num
, paddress (addr
));
2305 write_enn (own_buf
);
2309 tpoint
= add_tracepoint (num
, addr
);
2311 tpoint
->enabled
= (*packet
== 'E');
2312 ++packet
; /* skip 'E' */
2313 ++packet
; /* skip a colon */
2314 packet
= unpack_varlen_hex (packet
, &count
);
2315 tpoint
->step_count
= count
;
2316 ++packet
; /* skip a colon */
2317 packet
= unpack_varlen_hex (packet
, &count
);
2318 tpoint
->pass_count
= count
;
2319 /* See if we have any of the additional optional fields. */
2320 while (*packet
== ':')
2325 tpoint
->type
= fast_tracepoint
;
2327 packet
= unpack_varlen_hex (packet
, &count
);
2328 tpoint
->orig_size
= count
;
2330 else if (*packet
== 'S')
2332 tpoint
->type
= static_tracepoint
;
2335 else if (*packet
== 'X')
2337 actparm
= (char *) packet
;
2338 tpoint
->cond
= parse_agent_expr (&actparm
);
2341 else if (*packet
== '-')
2343 else if (*packet
== '\0')
2346 trace_debug ("Unknown optional tracepoint field");
2349 trace_debug ("Also has actions\n");
2351 trace_debug ("Defined %stracepoint %d at 0x%s, "
2352 "enabled %d step %ld pass %ld",
2353 tpoint
->type
== fast_tracepoint
? "fast "
2355 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2356 tpoint
->step_count
, tpoint
->pass_count
);
2359 add_tracepoint_action (tpoint
, packet
);
2362 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2363 (int) num
, paddress (addr
));
2364 write_enn (own_buf
);
2372 cmd_qtdpsrc (char *own_buf
)
2374 ULONGEST num
, addr
, start
, slen
;
2375 struct tracepoint
*tpoint
;
2376 char *packet
= own_buf
;
2377 char *saved
, *srctype
, *src
;
2379 struct source_string
*last
, *newlast
;
2381 packet
+= strlen ("QTDPsrc:");
2383 packet
= unpack_varlen_hex (packet
, &num
);
2384 ++packet
; /* skip a colon */
2385 packet
= unpack_varlen_hex (packet
, &addr
);
2386 ++packet
; /* skip a colon */
2388 /* See if we already have this tracepoint. */
2389 tpoint
= find_tracepoint (num
, addr
);
2393 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2394 (int) num
, paddress (addr
));
2395 write_enn (own_buf
);
2400 packet
= strchr (packet
, ':');
2401 srctype
= xmalloc (packet
- saved
+ 1);
2402 memcpy (srctype
, saved
, packet
- saved
);
2403 srctype
[packet
- saved
] = '\0';
2405 packet
= unpack_varlen_hex (packet
, &start
);
2406 ++packet
; /* skip a colon */
2407 packet
= unpack_varlen_hex (packet
, &slen
);
2408 ++packet
; /* skip a colon */
2409 src
= xmalloc (slen
+ 1);
2410 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2413 newlast
= xmalloc (sizeof (struct source_string
));
2414 newlast
->type
= srctype
;
2416 newlast
->next
= NULL
;
2417 /* Always add a source string to the end of the list;
2418 this keeps sequences of actions/commands in the right
2420 if (tpoint
->source_strings
)
2422 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2424 last
->next
= newlast
;
2427 tpoint
->source_strings
= newlast
;
2433 cmd_qtdv (char *own_buf
)
2435 ULONGEST num
, val
, builtin
;
2438 struct trace_state_variable
*tsv
;
2439 char *packet
= own_buf
;
2441 packet
+= strlen ("QTDV:");
2443 packet
= unpack_varlen_hex (packet
, &num
);
2444 ++packet
; /* skip a colon */
2445 packet
= unpack_varlen_hex (packet
, &val
);
2446 ++packet
; /* skip a colon */
2447 packet
= unpack_varlen_hex (packet
, &builtin
);
2448 ++packet
; /* skip a colon */
2450 nbytes
= strlen (packet
) / 2;
2451 varname
= xmalloc (nbytes
+ 1);
2452 nbytes
= unhexify (varname
, packet
, nbytes
);
2453 varname
[nbytes
] = '\0';
2455 tsv
= create_trace_state_variable (num
, 1);
2456 tsv
->initial_value
= (LONGEST
) val
;
2457 tsv
->name
= varname
;
2459 set_trace_state_variable_value (num
, (LONGEST
) val
);
2465 cmd_qtenable_disable (char *own_buf
, int enable
)
2467 char *packet
= own_buf
;
2469 struct tracepoint
*tp
;
2471 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2472 packet
= unpack_varlen_hex (packet
, &num
);
2473 ++packet
; /* skip a colon */
2474 packet
= unpack_varlen_hex (packet
, &addr
);
2476 tp
= find_tracepoint (num
, addr
);
2480 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2482 trace_debug ("Tracepoint %d at 0x%s is already %s",
2483 (int) num
, paddress (addr
),
2484 enable
? "enabled" : "disabled");
2489 trace_debug ("%s tracepoint %d at 0x%s",
2490 enable
? "Enabling" : "Disabling",
2491 (int) num
, paddress (addr
));
2493 tp
->enabled
= enable
;
2495 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2498 int offset
= offsetof (struct tracepoint
, enabled
);
2499 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2501 ret
= prepare_to_access_memory ();
2504 trace_debug ("Failed to temporarily stop inferior threads");
2505 write_enn (own_buf
);
2509 ret
= write_inferior_integer (obj_addr
, enable
);
2510 done_accessing_memory ();
2514 trace_debug ("Cannot write enabled flag into "
2515 "inferior process memory");
2516 write_enn (own_buf
);
2525 trace_debug ("Tracepoint %d at 0x%s not found",
2526 (int) num
, paddress (addr
));
2527 write_enn (own_buf
);
2532 cmd_qtv (char *own_buf
)
2537 char *packet
= own_buf
;
2539 packet
+= strlen ("qTV:");
2540 unpack_varlen_hex (packet
, &num
);
2542 if (current_traceframe
>= 0)
2544 err
= traceframe_read_tsv ((int) num
, &val
);
2547 strcpy (own_buf
, "U");
2551 /* Only make tsv's be undefined before the first trace run. After a
2552 trace run is over, the user might want to see the last value of
2553 the tsv, and it might not be available in a traceframe. */
2554 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2556 strcpy (own_buf
, "U");
2560 val
= get_trace_state_variable_value (num
);
2562 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2565 /* Clear out the list of readonly regions. */
2568 clear_readonly_regions (void)
2570 struct readonly_region
*roreg
;
2572 while (readonly_regions
)
2574 roreg
= readonly_regions
;
2575 readonly_regions
= readonly_regions
->next
;
2580 /* Parse the collection of address ranges whose contents GDB believes
2581 to be unchanging and so can be read directly from target memory
2582 even while looking at a traceframe. */
2585 cmd_qtro (char *own_buf
)
2587 ULONGEST start
, end
;
2588 struct readonly_region
*roreg
;
2589 char *packet
= own_buf
;
2591 trace_debug ("Want to mark readonly regions");
2593 clear_readonly_regions ();
2595 packet
+= strlen ("QTro");
2597 while (*packet
== ':')
2599 ++packet
; /* skip a colon */
2600 packet
= unpack_varlen_hex (packet
, &start
);
2601 ++packet
; /* skip a comma */
2602 packet
= unpack_varlen_hex (packet
, &end
);
2603 roreg
= xmalloc (sizeof (struct readonly_region
));
2604 roreg
->start
= start
;
2606 roreg
->next
= readonly_regions
;
2607 readonly_regions
= roreg
;
2608 trace_debug ("Added readonly region from 0x%s to 0x%s",
2609 paddress (roreg
->start
), paddress (roreg
->end
));
2615 /* Test to see if the given range is in our list of readonly ranges.
2616 We only test for being entirely within a range, GDB is not going to
2617 send a single memory packet that spans multiple regions. */
2620 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2622 struct readonly_region
*roreg
;
2624 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2625 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2631 /* The maximum size of a jump pad entry. */
2632 static const int max_jump_pad_size
= 0x100;
2634 static CORE_ADDR gdb_jump_pad_head
;
2636 /* Return the address of the next free jump space. */
2639 get_jump_space_head (void)
2641 if (gdb_jump_pad_head
== 0)
2643 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2644 &gdb_jump_pad_head
))
2645 fatal ("error extracting jump_pad_buffer");
2648 return gdb_jump_pad_head
;
2651 /* Reserve USED bytes from the jump space. */
2654 claim_jump_space (ULONGEST used
)
2656 trace_debug ("claim_jump_space reserves %s bytes at %s",
2657 pulongest (used
), paddress (gdb_jump_pad_head
));
2658 gdb_jump_pad_head
+= used
;
2661 /* Sort tracepoints by PC, using a bubble sort. */
2664 sort_tracepoints (void)
2666 struct tracepoint
*lst
, *tmp
, *prev
= NULL
;
2669 if (tracepoints
== NULL
)
2673 for (tmp
= tracepoints
; tmp
->next
; tmp
= tmp
->next
)
2676 for (i
= 0; i
< n
- 1; i
++)
2677 for (j
= 0, lst
= tracepoints
;
2678 lst
&& lst
->next
&& (j
<= n
- 1 - i
);
2681 /* If we're at beginning, the start node is the prev
2686 /* Compare neighbors. */
2687 if (lst
->next
->address
< lst
->address
)
2689 struct tracepoint
*p
;
2692 tmp
= (lst
->next
? lst
->next
->next
: NULL
);
2694 if (j
== 0 && prev
== tracepoints
)
2695 tracepoints
= lst
->next
;
2698 prev
->next
= lst
->next
;
2699 lst
->next
->next
= lst
;
2706 /* Keep track of the previous node. We need it if we need
2714 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2715 the command fails, or 0 otherwise. If the command ran
2716 successfully, but probing the marker failed, ERROUT will be filled
2717 with the error to reply to GDB, and -1 is also returned. This
2718 allows directly passing IPA errors to GDB. */
2721 probe_marker_at (CORE_ADDR address
, char *errout
)
2723 char cmd
[CMD_BUF_SIZE
];
2726 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
2727 err
= run_inferior_command (cmd
);
2733 strcpy (errout
, cmd
);
2742 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
2744 to
->jump_pad
= from
->jump_pad
;
2745 to
->jump_pad_end
= from
->jump_pad_end
;
2746 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
2747 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
2748 to
->handle
= from
->handle
;
2750 gdb_assert (from
->handle
);
2751 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
2754 #define MAX_JUMP_SIZE 20
2756 /* Install fast tracepoint. Return 0 if successful, otherwise return
2760 install_fast_tracepoint (struct tracepoint
*tpoint
)
2762 CORE_ADDR jentry
, jump_entry
;
2764 /* The jump to the jump pad of the last fast tracepoint
2766 unsigned char fjump
[MAX_JUMP_SIZE
];
2767 ULONGEST fjump_size
;
2769 jentry
= jump_entry
= get_jump_space_head ();
2771 /* Install the jump pad. */
2772 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
2774 ipa_sym_addrs
.addr_gdb_collect
,
2775 ipa_sym_addrs
.addr_collecting
,
2777 &jentry
, fjump
, &fjump_size
,
2778 &tpoint
->adjusted_insn_addr
,
2779 &tpoint
->adjusted_insn_addr_end
);
2785 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
2788 if (tpoint
->handle
!= NULL
)
2790 tpoint
->jump_pad
= jump_entry
;
2791 tpoint
->jump_pad_end
= jentry
;
2793 /* Pad to 8-byte alignment. */
2794 jentry
= ((jentry
+ 7) & ~0x7);
2795 claim_jump_space (jentry
- jump_entry
);
2802 cmd_qtstart (char *packet
)
2804 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
2806 trace_debug ("Starting the trace");
2808 /* Sort tracepoints by ascending address. This makes installing
2809 fast tracepoints at the same address easier to handle. */
2810 sort_tracepoints ();
2812 /* Pause all threads temporarily while we patch tracepoints. */
2815 /* Get threads out of jump pads. Safe to do here, since this is a
2816 top level command. And, required to do here, since we're
2817 deleting/rewriting jump pads. */
2819 stabilize_threads ();
2821 /* Freeze threads. */
2824 /* Sync the fast tracepoints list in the inferior ftlib. */
2825 if (in_process_agent_loaded ())
2827 download_tracepoints ();
2828 download_trace_state_variables ();
2831 /* No previous fast tpoint yet. */
2832 prev_ftpoint
= NULL
;
2834 /* No previous static tpoint yet. */
2835 prev_stpoint
= NULL
;
2839 /* Install tracepoints. */
2840 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2842 /* Ensure all the hit counts start at zero. */
2843 tpoint
->hit_count
= 0;
2845 if (tpoint
->type
== trap_tracepoint
)
2847 /* Tracepoints are installed as memory breakpoints. Just go
2848 ahead and install the trap. The breakpoints module
2849 handles duplicated breakpoints, and the memory read
2850 routine handles un-patching traps from memory reads. */
2851 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
2852 tracepoint_handler
);
2854 else if (tpoint
->type
== fast_tracepoint
)
2856 if (maybe_write_ipa_not_loaded (packet
))
2858 trace_debug ("Requested a fast tracepoint, but fast "
2859 "tracepoints aren't supported.");
2863 if (prev_ftpoint
!= NULL
&& prev_ftpoint
->address
== tpoint
->address
)
2864 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
2867 if (install_fast_tracepoint (tpoint
) == 0)
2868 prev_ftpoint
= tpoint
;
2872 else if (tpoint
->type
== static_tracepoint
)
2874 if (maybe_write_ipa_ust_not_loaded (packet
))
2876 trace_debug ("Requested a static tracepoint, but static "
2877 "tracepoints are not supported.");
2881 /* Can only probe a given marker once. */
2882 if (prev_stpoint
!= NULL
&& prev_stpoint
->address
== tpoint
->address
)
2884 tpoint
->handle
= (void *) -1;
2888 if (probe_marker_at (tpoint
->address
, packet
) == 0)
2890 tpoint
->handle
= (void *) -1;
2892 /* So that we can handle multiple static tracepoints
2893 at the same address easily. */
2894 prev_stpoint
= tpoint
;
2899 /* Any failure in the inner loop is sufficient cause to give
2901 if (tpoint
->handle
== NULL
)
2905 /* Any error in tracepoint insertion is unacceptable; better to
2906 address the problem now, than end up with a useless or misleading
2910 clear_installed_tracepoints ();
2911 if (*packet
== '\0')
2917 stopping_tracepoint
= NULL
;
2918 trace_buffer_is_full
= 0;
2919 expr_eval_result
= expr_eval_no_error
;
2920 error_tracepoint
= NULL
;
2922 /* Tracing is now active, hits will now start being logged. */
2925 if (in_process_agent_loaded ())
2927 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
2928 fatal ("Error setting tracing variable in lib");
2930 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
2932 fatal ("Error clearing stopping_tracepoint variable in lib");
2934 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
2935 fatal ("Error clearing trace_buffer_is_full variable in lib");
2937 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
2938 stop_tracing_handler
);
2939 if (stop_tracing_bkpt
== NULL
)
2940 error ("Error setting stop_tracing breakpoint");
2942 flush_trace_buffer_bkpt
2943 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
2944 flush_trace_buffer_handler
);
2945 if (flush_trace_buffer_bkpt
== NULL
)
2946 error ("Error setting flush_trace_buffer breakpoint");
2954 /* End a tracing run, filling in a stop reason to report back to GDB,
2955 and removing the tracepoints from the code. */
2962 trace_debug ("Tracing is already off, ignoring");
2966 trace_debug ("Stopping the trace");
2968 /* Pause all threads before removing fast jumps from memory,
2969 breakpoints, and touching IPA state variables (inferior memory).
2970 Some thread may hit the internal tracing breakpoints, or be
2971 collecting this moment, but that's ok, we don't release the
2972 tpoint object's memory or the jump pads here (we only do that
2973 when we're sure we can move all threads out of the jump pads).
2974 We can't now, since we may be getting here due to the inferior
2975 agent calling us. */
2977 /* Since we're removing breakpoints, cancel breakpoint hits,
2978 possibly related to the breakpoints we're about to delete. */
2979 cancel_breakpoints ();
2981 /* Stop logging. Tracepoints can still be hit, but they will not be
2984 if (in_process_agent_loaded ())
2986 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
2987 fatal ("Error clearing tracing variable in lib");
2990 tracing_stop_reason
= "t???";
2991 tracing_stop_tpnum
= 0;
2992 if (stopping_tracepoint
)
2994 trace_debug ("Stopping the trace because "
2995 "tracepoint %d was hit %ld times",
2996 stopping_tracepoint
->number
,
2997 stopping_tracepoint
->pass_count
);
2998 tracing_stop_reason
= "tpasscount";
2999 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3001 else if (trace_buffer_is_full
)
3003 trace_debug ("Stopping the trace because the trace buffer is full");
3004 tracing_stop_reason
= "tfull";
3006 else if (expr_eval_result
!= expr_eval_no_error
)
3008 trace_debug ("Stopping the trace because of an expression eval error");
3009 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3010 tracing_stop_tpnum
= error_tracepoint
->number
;
3012 #ifndef IN_PROCESS_AGENT
3013 else if (!gdb_connected ())
3015 trace_debug ("Stopping the trace because GDB disconnected");
3016 tracing_stop_reason
= "tdisconnected";
3021 trace_debug ("Stopping the trace because of a tstop command");
3022 tracing_stop_reason
= "tstop";
3025 stopping_tracepoint
= NULL
;
3026 error_tracepoint
= NULL
;
3028 /* Clear out the tracepoints. */
3029 clear_installed_tracepoints ();
3031 if (in_process_agent_loaded ())
3033 /* Pull in fast tracepoint trace frames from the inferior lib
3034 buffer into our buffer, even if our buffer is already full,
3035 because we want to present the full number of created frames
3036 in addition to what fit in the trace buffer. */
3037 upload_fast_traceframes ();
3040 if (stop_tracing_bkpt
!= NULL
)
3042 delete_breakpoint (stop_tracing_bkpt
);
3043 stop_tracing_bkpt
= NULL
;
3046 if (flush_trace_buffer_bkpt
!= NULL
)
3048 delete_breakpoint (flush_trace_buffer_bkpt
);
3049 flush_trace_buffer_bkpt
= NULL
;
3056 stop_tracing_handler (CORE_ADDR addr
)
3058 trace_debug ("lib hit stop_tracing");
3060 /* Don't actually handle it here. When we stop tracing we remove
3061 breakpoints from the inferior, and that is not allowed in a
3062 breakpoint handler (as the caller is walking the breakpoint
3068 flush_trace_buffer_handler (CORE_ADDR addr
)
3070 trace_debug ("lib hit flush_trace_buffer");
3075 cmd_qtstop (char *packet
)
3082 cmd_qtdisconnected (char *own_buf
)
3085 char *packet
= own_buf
;
3087 packet
+= strlen ("QTDisconnected:");
3089 unpack_varlen_hex (packet
, &setting
);
3093 disconnected_tracing
= setting
;
3097 cmd_qtframe (char *own_buf
)
3099 ULONGEST frame
, pc
, lo
, hi
, num
;
3101 struct traceframe
*tframe
;
3102 char *packet
= own_buf
;
3104 packet
+= strlen ("QTFrame:");
3106 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3108 packet
+= strlen ("pc:");
3109 unpack_varlen_hex (packet
, &pc
);
3110 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3111 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3113 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3115 packet
+= strlen ("range:");
3116 packet
= unpack_varlen_hex (packet
, &lo
);
3118 unpack_varlen_hex (packet
, &hi
);
3119 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3120 paddress (lo
), paddress (hi
));
3121 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3123 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3125 packet
+= strlen ("outside:");
3126 packet
= unpack_varlen_hex (packet
, &lo
);
3128 unpack_varlen_hex (packet
, &hi
);
3129 trace_debug ("Want to find next traceframe "
3130 "outside the range 0x%s to 0x%s",
3131 paddress (lo
), paddress (hi
));
3132 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3134 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3136 packet
+= strlen ("tdp:");
3137 unpack_varlen_hex (packet
, &num
);
3139 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3140 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3144 unpack_varlen_hex (packet
, &frame
);
3145 tfnum
= (int) frame
;
3148 trace_debug ("Want to stop looking at traceframes");
3149 current_traceframe
= -1;
3153 trace_debug ("Want to look at traceframe %d", tfnum
);
3154 tframe
= find_traceframe (tfnum
);
3159 current_traceframe
= tfnum
;
3160 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3163 sprintf (own_buf
, "F-1");
3167 cmd_qtstatus (char *packet
)
3169 char *stop_reason_rsp
= NULL
;
3171 trace_debug ("Returning trace status as %d, stop reason %s",
3172 tracing
, tracing_stop_reason
);
3174 if (in_process_agent_loaded ())
3178 upload_fast_traceframes ();
3183 stop_reason_rsp
= (char *) tracing_stop_reason
;
3185 /* The user visible error string in terror needs to be hex encoded.
3186 We leave it as plain string in `tracepoint_stop_reason' to ease
3188 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3190 const char *result_name
;
3194 result_name
= stop_reason_rsp
+ strlen ("terror:");
3195 hexstr_len
= strlen (result_name
) * 2;
3196 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3197 strcpy (p
, "terror:");
3199 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3205 "tframes:%x;tcreated:%x;"
3206 "tfree:%x;tsize:%s;"
3210 stop_reason_rsp
, tracing_stop_tpnum
,
3211 traceframe_count
, traceframes_created
,
3212 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3213 circular_trace_buffer
,
3214 disconnected_tracing
);
3217 /* State variables to help return all the tracepoint bits. */
3218 static struct tracepoint
*cur_tpoint
;
3219 static int cur_action
;
3220 static int cur_step_action
;
3221 static struct source_string
*cur_source_string
;
3222 static struct trace_state_variable
*cur_tsv
;
3224 /* Compose a response that is an imitation of the syntax by which the
3225 tracepoint was originally downloaded. */
3228 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3232 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
3233 paddress (tpoint
->address
),
3234 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3235 tpoint
->pass_count
);
3236 if (tpoint
->type
== fast_tracepoint
)
3237 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3238 else if (tpoint
->type
== static_tracepoint
)
3239 sprintf (packet
+ strlen (packet
), ":S");
3243 buf
= unparse_agent_expr (tpoint
->cond
);
3244 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3245 tpoint
->cond
->length
, buf
);
3250 /* Compose a response that is an imitation of the syntax by which the
3251 tracepoint action was originally downloaded (with the difference
3252 that due to the way we store the actions, this will output a packet
3253 per action, while GDB could have combined more than one action
3257 response_action (char *packet
, struct tracepoint
*tpoint
,
3258 char *taction
, int step
)
3260 sprintf (packet
, "%c%x:%s:%s",
3261 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3265 /* Compose a response that is an imitation of the syntax by which the
3266 tracepoint source piece was originally downloaded. */
3269 response_source (char *packet
,
3270 struct tracepoint
*tpoint
, struct source_string
*src
)
3275 len
= strlen (src
->str
);
3276 buf
= alloca (len
* 2 + 1);
3277 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3279 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3280 tpoint
->number
, paddress (tpoint
->address
),
3281 src
->type
, 0, len
, buf
);
3284 /* Return the first piece of tracepoint definition, and initialize the
3285 state machine that will iterate through all the tracepoint
3289 cmd_qtfp (char *packet
)
3291 trace_debug ("Returning first tracepoint definition piece");
3293 cur_tpoint
= tracepoints
;
3294 cur_action
= cur_step_action
= -1;
3295 cur_source_string
= NULL
;
3298 response_tracepoint (packet
, cur_tpoint
);
3300 strcpy (packet
, "l");
3303 /* Return additional pieces of tracepoint definition. Each action and
3304 stepping action must go into its own packet, because of packet size
3305 limits, and so we use state variables to deliver one piece at a
3309 cmd_qtsp (char *packet
)
3311 trace_debug ("Returning subsequent tracepoint definition piece");
3315 /* This case would normally never occur, but be prepared for
3317 strcpy (packet
, "l");
3319 else if (cur_action
< cur_tpoint
->numactions
- 1)
3322 response_action (packet
, cur_tpoint
,
3323 cur_tpoint
->actions_str
[cur_action
], 0);
3325 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3328 response_action (packet
, cur_tpoint
,
3329 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3331 else if ((cur_source_string
3332 ? cur_source_string
->next
3333 : cur_tpoint
->source_strings
))
3335 if (cur_source_string
)
3336 cur_source_string
= cur_source_string
->next
;
3338 cur_source_string
= cur_tpoint
->source_strings
;
3339 response_source (packet
, cur_tpoint
, cur_source_string
);
3343 cur_tpoint
= cur_tpoint
->next
;
3344 cur_action
= cur_step_action
= -1;
3345 cur_source_string
= NULL
;
3347 response_tracepoint (packet
, cur_tpoint
);
3349 strcpy (packet
, "l");
3353 /* Compose a response that is an imitation of the syntax by which the
3354 trace state variable was originally downloaded. */
3357 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3359 char *buf
= (char *) "";
3364 namelen
= strlen (tsv
->name
);
3365 buf
= alloca (namelen
* 2 + 1);
3366 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3369 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3370 tsv
->getter
? 1 : 0, buf
);
3373 /* Return the first trace state variable definition, and initialize
3374 the state machine that will iterate through all the tsv bits. */
3377 cmd_qtfv (char *packet
)
3379 trace_debug ("Returning first trace state variable definition");
3381 cur_tsv
= trace_state_variables
;
3384 response_tsv (packet
, cur_tsv
);
3386 strcpy (packet
, "l");
3389 /* Return additional trace state variable definitions. */
3392 cmd_qtsv (char *packet
)
3394 trace_debug ("Returning first trace state variable definition");
3398 /* This case would normally never occur, but be prepared for
3400 strcpy (packet
, "l");
3404 cur_tsv
= cur_tsv
->next
;
3406 response_tsv (packet
, cur_tsv
);
3408 strcpy (packet
, "l");
3411 strcpy (packet
, "l");
3414 /* Return the first static tracepoint marker, and initialize the state
3415 machine that will iterate through all the static tracepoints
3419 cmd_qtfstm (char *packet
)
3421 if (!maybe_write_ipa_ust_not_loaded (packet
))
3422 run_inferior_command (packet
);
3425 /* Return additional static tracepoints markers. */
3428 cmd_qtsstm (char *packet
)
3430 if (!maybe_write_ipa_ust_not_loaded (packet
))
3431 run_inferior_command (packet
);
3434 /* Return the definition of the static tracepoint at a given address.
3435 Result packet is the same as qTsST's. */
3438 cmd_qtstmat (char *packet
)
3440 if (!maybe_write_ipa_ust_not_loaded (packet
))
3441 run_inferior_command (packet
);
3444 /* Respond to qTBuffer packet with a block of raw data from the trace
3445 buffer. GDB may ask for a lot, but we are allowed to reply with
3446 only as much as will fit within packet limits or whatever. */
3449 cmd_qtbuffer (char *own_buf
)
3451 ULONGEST offset
, num
, tot
;
3453 char *packet
= own_buf
;
3455 packet
+= strlen ("qTBuffer:");
3457 packet
= unpack_varlen_hex (packet
, &offset
);
3458 ++packet
; /* skip a comma */
3459 unpack_varlen_hex (packet
, &num
);
3461 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3462 (int) num
, pulongest (offset
));
3464 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3466 /* If we're right at the end, reply specially that we're done. */
3469 strcpy (own_buf
, "l");
3473 /* Object to any other out-of-bounds request. */
3476 write_enn (own_buf
);
3480 /* Compute the pointer corresponding to the given offset, accounting
3482 tbp
= trace_buffer_start
+ offset
;
3483 if (tbp
>= trace_buffer_wrap
)
3484 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3486 /* Trim to the remaining bytes if we're close to the end. */
3487 if (num
> tot
- offset
)
3490 /* Trim to available packet size. */
3491 if (num
>= (PBUFSIZ
- 16) / 2 )
3492 num
= (PBUFSIZ
- 16) / 2;
3494 convert_int_to_ascii (tbp
, own_buf
, num
);
3495 own_buf
[num
] = '\0';
3499 cmd_bigqtbuffer (char *own_buf
)
3502 char *packet
= own_buf
;
3504 packet
+= strlen ("QTBuffer:");
3506 if (strncmp ("circular:", packet
, strlen ("circular:")) == 0)
3508 packet
+= strlen ("circular:");
3509 unpack_varlen_hex (packet
, &val
);
3510 circular_trace_buffer
= val
;
3511 trace_debug ("Trace buffer is now %s",
3512 circular_trace_buffer
? "circular" : "linear");
3516 write_enn (own_buf
);
3520 handle_tracepoint_general_set (char *packet
)
3522 if (strcmp ("QTinit", packet
) == 0)
3524 cmd_qtinit (packet
);
3527 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3532 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3534 cmd_qtdpsrc (packet
);
3537 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
3539 cmd_qtenable_disable (packet
, 1);
3542 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
3544 cmd_qtenable_disable (packet
, 0);
3547 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3552 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3557 else if (strcmp ("QTStart", packet
) == 0)
3559 cmd_qtstart (packet
);
3562 else if (strcmp ("QTStop", packet
) == 0)
3564 cmd_qtstop (packet
);
3567 else if (strncmp ("QTDisconnected:", packet
,
3568 strlen ("QTDisconnected:")) == 0)
3570 cmd_qtdisconnected (packet
);
3573 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3575 cmd_qtframe (packet
);
3578 else if (strncmp ("QTBuffer:", packet
, strlen ("QTBuffer:")) == 0)
3580 cmd_bigqtbuffer (packet
);
3588 handle_tracepoint_query (char *packet
)
3590 if (strcmp ("qTStatus", packet
) == 0)
3592 cmd_qtstatus (packet
);
3595 else if (strcmp ("qTfP", packet
) == 0)
3600 else if (strcmp ("qTsP", packet
) == 0)
3605 else if (strcmp ("qTfV", packet
) == 0)
3610 else if (strcmp ("qTsV", packet
) == 0)
3615 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
3620 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
3622 cmd_qtbuffer (packet
);
3625 else if (strcmp ("qTfSTM", packet
) == 0)
3627 cmd_qtfstm (packet
);
3630 else if (strcmp ("qTsSTM", packet
) == 0)
3632 cmd_qtsstm (packet
);
3635 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
3637 cmd_qtstmat (packet
);
3645 #ifndef IN_PROCESS_AGENT
3647 /* Call this when thread TINFO has hit the tracepoint defined by
3648 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3649 action. This adds a while-stepping collecting state item to the
3650 threads' collecting state list, so that we can keep track of
3651 multiple simultaneous while-stepping actions being collected by the
3652 same thread. This can happen in cases like:
3654 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3656 ff0003 INSN3 <-- TP2, collect $regs
3657 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3660 Notice that when instruction INSN5 is reached, the while-stepping
3661 actions of both TP1 and TP3 are still being collected, and that TP2
3662 had been collected meanwhile. The whole range of ff0001-ff0005
3663 should be single-stepped, due to at least TP1's while-stepping
3664 action covering the whole range. */
3667 add_while_stepping_state (struct thread_info
*tinfo
,
3668 int tp_number
, CORE_ADDR tp_address
)
3670 struct wstep_state
*wstep
;
3672 wstep
= xmalloc (sizeof (*wstep
));
3673 wstep
->next
= tinfo
->while_stepping
;
3675 wstep
->tp_number
= tp_number
;
3676 wstep
->tp_address
= tp_address
;
3677 wstep
->current_step
= 0;
3679 tinfo
->while_stepping
= wstep
;
3682 /* Release the while-stepping collecting state WSTEP. */
3685 release_while_stepping_state (struct wstep_state
*wstep
)
3690 /* Release all while-stepping collecting states currently associated
3691 with thread TINFO. */
3694 release_while_stepping_state_list (struct thread_info
*tinfo
)
3696 struct wstep_state
*head
;
3698 while (tinfo
->while_stepping
)
3700 head
= tinfo
->while_stepping
;
3701 tinfo
->while_stepping
= head
->next
;
3702 release_while_stepping_state (head
);
3706 /* If TINFO was handling a 'while-stepping' action, the step has
3707 finished, so collect any step data needed, and check if any more
3708 steps are required. Return true if the thread was indeed
3709 collecting tracepoint data, false otherwise. */
3712 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3714 struct tracepoint
*tpoint
;
3715 struct wstep_state
*wstep
;
3716 struct wstep_state
**wstep_link
;
3717 struct trap_tracepoint_ctx ctx
;
3719 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
3721 if (in_process_agent_loaded ())
3722 upload_fast_traceframes ();
3724 /* Check if we were indeed collecting data for one of more
3725 tracepoints with a 'while-stepping' count. */
3726 if (tinfo
->while_stepping
== NULL
)
3731 /* We're not even tracing anymore. Stop this thread from
3733 release_while_stepping_state_list (tinfo
);
3735 /* The thread had stopped due to a single-step request indeed
3736 explained by a tracepoint. */
3740 wstep
= tinfo
->while_stepping
;
3741 wstep_link
= &tinfo
->while_stepping
;
3743 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
3744 target_pid_to_str (tinfo
->entry
.id
),
3745 wstep
->tp_number
, paddress (wstep
->tp_address
));
3747 ctx
.base
.type
= trap_tracepoint
;
3748 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3750 while (wstep
!= NULL
)
3752 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
3755 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
3756 wstep
->tp_number
, paddress (wstep
->tp_address
),
3757 target_pid_to_str (tinfo
->entry
.id
));
3760 *wstep_link
= wstep
->next
;
3761 release_while_stepping_state (wstep
);
3762 wstep
= *wstep_link
;
3766 /* We've just finished one step. */
3767 ++wstep
->current_step
;
3770 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
3771 stop_pc
, tpoint
, wstep
->current_step
);
3773 if (wstep
->current_step
>= tpoint
->step_count
)
3775 /* The requested numbers of steps have occurred. */
3776 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
3777 target_pid_to_str (tinfo
->entry
.id
),
3778 wstep
->tp_number
, paddress (wstep
->tp_address
));
3780 /* Unlink the wstep. */
3781 *wstep_link
= wstep
->next
;
3782 release_while_stepping_state (wstep
);
3783 wstep
= *wstep_link
;
3785 /* Only check the hit count now, which ensure that we do all
3786 our stepping before stopping the run. */
3787 if (tpoint
->pass_count
> 0
3788 && tpoint
->hit_count
>= tpoint
->pass_count
3789 && stopping_tracepoint
== NULL
)
3790 stopping_tracepoint
= tpoint
;
3794 /* Keep single-stepping until the requested numbers of steps
3796 wstep_link
= &wstep
->next
;
3797 wstep
= *wstep_link
;
3800 if (stopping_tracepoint
3801 || trace_buffer_is_full
3802 || expr_eval_result
!= expr_eval_no_error
)
3812 /* Handle any internal tracing control breakpoint hits. That means,
3813 pull traceframes from the IPA to our buffer, and syncing both
3814 tracing agents when the IPA's tracing stops for some reason. */
3817 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3819 /* Pull in fast tracepoint trace frames from the inferior in-process
3820 agent's buffer into our buffer. */
3822 if (!in_process_agent_loaded ())
3825 upload_fast_traceframes ();
3827 /* Check if the in-process agent had decided we should stop
3829 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
3831 int ipa_trace_buffer_is_full
;
3832 CORE_ADDR ipa_stopping_tracepoint
;
3833 int ipa_expr_eval_result
;
3834 CORE_ADDR ipa_error_tracepoint
;
3836 trace_debug ("lib stopped at stop_tracing");
3838 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
3839 &ipa_trace_buffer_is_full
);
3841 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3842 &ipa_stopping_tracepoint
);
3843 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
3845 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
3846 &ipa_error_tracepoint
);
3847 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
3849 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
3850 &ipa_expr_eval_result
);
3851 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
3853 trace_debug ("lib: trace_buffer_is_full: %d, "
3854 "stopping_tracepoint: %s, "
3855 "ipa_expr_eval_result: %d, "
3856 "error_tracepoint: %s, ",
3857 ipa_trace_buffer_is_full
,
3858 paddress (ipa_stopping_tracepoint
),
3859 ipa_expr_eval_result
,
3860 paddress (ipa_error_tracepoint
));
3864 if (ipa_trace_buffer_is_full
)
3865 trace_debug ("lib stopped due to full buffer.");
3866 if (ipa_stopping_tracepoint
)
3867 trace_debug ("lib stopped due to tpoint");
3868 if (ipa_stopping_tracepoint
)
3869 trace_debug ("lib stopped due to error");
3872 if (ipa_stopping_tracepoint
!= 0)
3875 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
3877 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
3879 expr_eval_result
= ipa_expr_eval_result
;
3881 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
3886 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
3888 trace_debug ("lib stopped at flush_trace_buffer");
3895 /* Return true if TINFO just hit a tracepoint. Collect data if
3899 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3901 struct tracepoint
*tpoint
;
3903 struct trap_tracepoint_ctx ctx
;
3905 /* Not tracing, don't handle. */
3909 ctx
.base
.type
= trap_tracepoint
;
3910 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3912 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3914 /* Note that we collect fast tracepoints here as well. We'll
3915 step over the fast tracepoint jump later, which avoids the
3917 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
)
3919 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
3920 target_pid_to_str (tinfo
->entry
.id
),
3921 tpoint
->number
, paddress (tpoint
->address
));
3923 /* Test the condition if present, and collect if true. */
3925 || (condition_true_at_tracepoint
3926 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
3927 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
3930 if (stopping_tracepoint
3931 || trace_buffer_is_full
3932 || expr_eval_result
!= expr_eval_no_error
)
3936 /* If the tracepoint had a 'while-stepping' action, then set
3937 the thread to collect this tracepoint on the following
3939 else if (tpoint
->step_count
> 0)
3941 add_while_stepping_state (tinfo
,
3942 tpoint
->number
, tpoint
->address
);
3954 #if defined IN_PROCESS_AGENT && defined HAVE_UST
3955 struct ust_marker_data
;
3956 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
3958 struct tracepoint
*tpoint
,
3959 struct traceframe
*tframe
);
3962 /* Create a trace frame for the hit of the given tracepoint in the
3966 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
3967 struct tracepoint
*tpoint
)
3969 struct traceframe
*tframe
;
3972 /* Only count it as a hit when we actually collect data. */
3973 tpoint
->hit_count
++;
3975 /* If we've exceeded a defined pass count, record the event for
3976 later, and finish the collection for this hit. This test is only
3977 for nonstepping tracepoints, stepping tracepoints test at the end
3978 of their while-stepping loop. */
3979 if (tpoint
->pass_count
> 0
3980 && tpoint
->hit_count
>= tpoint
->pass_count
3981 && tpoint
->step_count
== 0
3982 && stopping_tracepoint
== NULL
)
3983 stopping_tracepoint
= tpoint
;
3985 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
3986 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
3988 tframe
= add_traceframe (tpoint
);
3992 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
3994 #ifndef IN_PROCESS_AGENT
3995 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
3996 tpoint
->number
, paddress (tpoint
->address
),
3997 tpoint
->actions_str
[acti
]);
4000 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4001 tpoint
->actions
[acti
]);
4004 finish_traceframe (tframe
);
4007 if (tframe
== NULL
&& tracing
)
4008 trace_buffer_is_full
= 1;
4011 #ifndef IN_PROCESS_AGENT
4014 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4016 struct tracepoint
*tpoint
, int current_step
)
4018 struct traceframe
*tframe
;
4021 trace_debug ("Making new step traceframe for "
4022 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4023 tpoint
->number
, paddress (tpoint
->address
),
4024 current_step
, tpoint
->step_count
,
4027 tframe
= add_traceframe (tpoint
);
4031 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4033 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4034 tpoint
->number
, paddress (tpoint
->address
),
4035 tpoint
->step_actions_str
[acti
]);
4037 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4038 tpoint
->step_actions
[acti
]);
4041 finish_traceframe (tframe
);
4044 if (tframe
== NULL
&& tracing
)
4045 trace_buffer_is_full
= 1;
4050 static struct regcache
*
4051 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4053 struct regcache
*regcache
= NULL
;
4055 #ifdef IN_PROCESS_AGENT
4056 if (ctx
->type
== fast_tracepoint
)
4058 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4059 if (!fctx
->regcache_initted
)
4061 fctx
->regcache_initted
= 1;
4062 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4063 supply_regblock (&fctx
->regcache
, NULL
);
4064 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4066 regcache
= &fctx
->regcache
;
4069 if (ctx
->type
== static_tracepoint
)
4071 struct static_tracepoint_ctx
*sctx
4072 = (struct static_tracepoint_ctx
*) ctx
;
4074 if (!sctx
->regcache_initted
)
4076 sctx
->regcache_initted
= 1;
4077 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4078 supply_regblock (&sctx
->regcache
, NULL
);
4079 /* Pass down the tracepoint address, because REGS doesn't
4080 include the PC, but we know what it must have been. */
4081 supply_static_tracepoint_registers (&sctx
->regcache
,
4082 (const unsigned char *)
4084 sctx
->tpoint
->address
);
4086 regcache
= &sctx
->regcache
;
4090 if (ctx
->type
== trap_tracepoint
)
4092 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4093 regcache
= tctx
->regcache
;
4097 gdb_assert (regcache
!= NULL
);
4103 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4105 struct tracepoint
*tpoint
,
4106 struct traceframe
*tframe
,
4107 struct tracepoint_action
*taction
)
4109 enum eval_result_type err
;
4111 switch (taction
->type
)
4115 struct collect_memory_action
*maction
;
4117 maction
= (struct collect_memory_action
*) taction
;
4119 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4120 pulongest (maction
->len
),
4121 paddress (maction
->addr
), maction
->basereg
);
4122 /* (should use basereg) */
4123 agent_mem_read (tframe
, NULL
,
4124 (CORE_ADDR
) maction
->addr
, maction
->len
);
4129 unsigned char *regspace
;
4130 struct regcache tregcache
;
4131 struct regcache
*context_regcache
;
4134 trace_debug ("Want to collect registers");
4136 /* Collect all registers for now. */
4137 regspace
= add_traceframe_block (tframe
,
4138 1 + register_cache_size ());
4139 if (regspace
== NULL
)
4141 trace_debug ("Trace buffer block allocation failed, skipping");
4144 /* Identify a register block. */
4147 context_regcache
= get_context_regcache (ctx
);
4149 /* Wrap the regblock in a register cache (in the stack, we
4150 don't want to malloc here). */
4151 init_register_cache (&tregcache
, regspace
+ 1);
4153 /* Copy the register data to the regblock. */
4154 regcache_cpy (&tregcache
, context_regcache
);
4156 #ifndef IN_PROCESS_AGENT
4157 /* On some platforms, trap-based tracepoints will have the PC
4158 pointing to the next instruction after the trap, but we
4159 don't want the user or GDB trying to guess whether the
4160 saved PC needs adjusting; so always record the adjusted
4161 stop_pc. Note that we can't use tpoint->address instead,
4162 since it will be wrong for while-stepping actions. This
4163 adjustment is a nop for fast tracepoints collected from the
4164 in-process lib (but not if GDBserver is collecting one
4165 preemptively), since the PC had already been adjusted to
4166 contain the tracepoint's address by the jump pad. */
4167 trace_debug ("Storing stop pc (0x%s) in regblock",
4168 paddress (tpoint
->address
));
4170 /* This changes the regblock, not the thread's
4172 regcache_write_pc (&tregcache
, stop_pc
);
4178 struct eval_expr_action
*eaction
;
4180 eaction
= (struct eval_expr_action
*) taction
;
4182 trace_debug ("Want to evaluate expression");
4184 err
= eval_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4186 if (err
!= expr_eval_no_error
)
4188 record_tracepoint_error (tpoint
, "action expression", err
);
4195 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4196 trace_debug ("Want to collect static trace data");
4197 collect_ust_data_at_tracepoint (ctx
, stop_pc
,
4200 trace_debug ("warning: collecting static trace data, "
4201 "but static tracepoints are not supported");
4206 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4212 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4213 struct tracepoint
*tpoint
)
4216 enum eval_result_type err
;
4218 /* Presently, gdbserver doesn't run compiled conditions, only the
4219 IPA does. If the program stops at a fast tracepoint's address
4220 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4221 gdbserver preemptively collect the fast tracepoint. Later, on
4222 resume, gdbserver steps over the fast tracepoint like it steps
4223 over breakpoints, so that the IPA doesn't see that fast
4224 tracepoint. This avoids double collects of fast tracepoints in
4225 that stopping scenario. Having gdbserver itself handle the fast
4226 tracepoint gives the user a consistent view of when fast or trap
4227 tracepoints are collected, compared to an alternative where only
4228 trap tracepoints are collected on stop, and fast tracepoints on
4229 resume. When a fast tracepoint is being processed by gdbserver,
4230 it is always the non-compiled condition expression that is
4232 #ifdef IN_PROCESS_AGENT
4233 if (tpoint
->compiled_cond
)
4234 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4237 err
= eval_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4239 if (err
!= expr_eval_no_error
)
4241 record_tracepoint_error (tpoint
, "condition", err
);
4242 /* The error case must return false. */
4246 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4247 tpoint
->number
, paddress (tpoint
->address
),
4249 return (value
? 1 : 0);
4252 #ifndef IN_PROCESS_AGENT
4254 /* The packet form of an agent expression consists of an 'X', number
4255 of bytes in expression, a comma, and then the bytes. */
4257 static struct agent_expr
*
4258 parse_agent_expr (char **actparm
)
4260 char *act
= *actparm
;
4262 struct agent_expr
*aexpr
;
4264 ++act
; /* skip the X */
4265 act
= unpack_varlen_hex (act
, &xlen
);
4266 ++act
; /* skip a comma */
4267 aexpr
= xmalloc (sizeof (struct agent_expr
));
4268 aexpr
->length
= xlen
;
4269 aexpr
->bytes
= xmalloc (xlen
);
4270 convert_ascii_to_int (act
, aexpr
->bytes
, xlen
);
4271 *actparm
= act
+ (xlen
* 2);
4275 /* Convert the bytes of an agent expression back into hex digits, so
4276 they can be printed or uploaded. This allocates the buffer,
4277 callers should free when they are done with it. */
4280 unparse_agent_expr (struct agent_expr
*aexpr
)
4284 rslt
= xmalloc (2 * aexpr
->length
+ 1);
4285 convert_int_to_ascii (aexpr
->bytes
, rslt
, aexpr
->length
);
4291 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
4294 gdb_agent_op_name (int op
)
4296 if (op
< 0 || op
>= gdb_agent_op_last
|| gdb_agent_op_names
[op
] == NULL
)
4298 return gdb_agent_op_names
[op
];
4301 /* The agent expression evaluator, as specified by the GDB docs. It
4302 returns 0 if everything went OK, and a nonzero error code
4305 static enum eval_result_type
4306 eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4307 struct traceframe
*tframe
,
4308 struct agent_expr
*aexpr
,
4312 #define STACK_MAX 100
4313 ULONGEST stack
[STACK_MAX
], top
;
4318 /* This union is a convenient way to convert representations. For
4319 now, assume a standard architecture where the hardware integer
4320 types have 8, 16, 32, 64 bit types. A more robust solution would
4321 be to import stdint.h from gnulib. */
4326 unsigned char bytes
[1];
4331 unsigned char bytes
[2];
4336 unsigned char bytes
[4];
4341 unsigned char bytes
[8];
4346 if (aexpr
->length
== 0)
4348 trace_debug ("empty agent expression");
4349 return expr_eval_empty_expression
;
4352 /* Cache the stack top in its own variable. Much of the time we can
4353 operate on this variable, rather than dinking with the stack. It
4354 needs to be copied to the stack when sp changes. */
4359 op
= aexpr
->bytes
[pc
++];
4361 trace_debug ("About to interpret byte 0x%x", op
);
4365 case gdb_agent_op_add
:
4369 case gdb_agent_op_sub
:
4370 top
= stack
[--sp
] - top
;
4373 case gdb_agent_op_mul
:
4377 case gdb_agent_op_div_signed
:
4380 trace_debug ("Attempted to divide by zero");
4381 return expr_eval_divide_by_zero
;
4383 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
4386 case gdb_agent_op_div_unsigned
:
4389 trace_debug ("Attempted to divide by zero");
4390 return expr_eval_divide_by_zero
;
4392 top
= stack
[--sp
] / top
;
4395 case gdb_agent_op_rem_signed
:
4398 trace_debug ("Attempted to divide by zero");
4399 return expr_eval_divide_by_zero
;
4401 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
4404 case gdb_agent_op_rem_unsigned
:
4407 trace_debug ("Attempted to divide by zero");
4408 return expr_eval_divide_by_zero
;
4410 top
= stack
[--sp
] % top
;
4413 case gdb_agent_op_lsh
:
4414 top
= stack
[--sp
] << top
;
4417 case gdb_agent_op_rsh_signed
:
4418 top
= ((LONGEST
) stack
[--sp
]) >> top
;
4421 case gdb_agent_op_rsh_unsigned
:
4422 top
= stack
[--sp
] >> top
;
4425 case gdb_agent_op_trace
:
4426 agent_mem_read (tframe
,
4427 NULL
, (CORE_ADDR
) stack
[--sp
], (ULONGEST
) top
);
4432 case gdb_agent_op_trace_quick
:
4433 arg
= aexpr
->bytes
[pc
++];
4434 agent_mem_read (tframe
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
4437 case gdb_agent_op_log_not
:
4441 case gdb_agent_op_bit_and
:
4445 case gdb_agent_op_bit_or
:
4449 case gdb_agent_op_bit_xor
:
4453 case gdb_agent_op_bit_not
:
4457 case gdb_agent_op_equal
:
4458 top
= (stack
[--sp
] == top
);
4461 case gdb_agent_op_less_signed
:
4462 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
4465 case gdb_agent_op_less_unsigned
:
4466 top
= (stack
[--sp
] < top
);
4469 case gdb_agent_op_ext
:
4470 arg
= aexpr
->bytes
[pc
++];
4471 if (arg
< (sizeof (LONGEST
) * 8))
4473 LONGEST mask
= 1 << (arg
- 1);
4474 top
&= ((LONGEST
) 1 << arg
) - 1;
4475 top
= (top
^ mask
) - mask
;
4479 case gdb_agent_op_ref8
:
4480 agent_mem_read (tframe
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
4484 case gdb_agent_op_ref16
:
4485 agent_mem_read (tframe
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
4489 case gdb_agent_op_ref32
:
4490 agent_mem_read (tframe
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
4494 case gdb_agent_op_ref64
:
4495 agent_mem_read (tframe
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
4499 case gdb_agent_op_if_goto
:
4501 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4508 case gdb_agent_op_goto
:
4509 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4512 case gdb_agent_op_const8
:
4513 /* Flush the cached stack top. */
4515 top
= aexpr
->bytes
[pc
++];
4518 case gdb_agent_op_const16
:
4519 /* Flush the cached stack top. */
4521 top
= aexpr
->bytes
[pc
++];
4522 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4525 case gdb_agent_op_const32
:
4526 /* Flush the cached stack top. */
4528 top
= aexpr
->bytes
[pc
++];
4529 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4530 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4531 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4534 case gdb_agent_op_const64
:
4535 /* Flush the cached stack top. */
4537 top
= aexpr
->bytes
[pc
++];
4538 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4539 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4540 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4541 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4542 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4543 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4544 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4547 case gdb_agent_op_reg
:
4548 /* Flush the cached stack top. */
4550 arg
= aexpr
->bytes
[pc
++];
4551 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4554 struct regcache
*regcache
;
4556 regcache
= get_context_regcache (ctx
);
4558 switch (register_size (regnum
))
4561 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
4565 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
4569 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
4573 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
4577 internal_error (__FILE__
, __LINE__
,
4578 "unhandled register size");
4583 case gdb_agent_op_end
:
4584 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4585 sp
, pulongest (top
));
4590 /* This should be an error */
4591 trace_debug ("Stack is empty, nothing to return");
4592 return expr_eval_empty_stack
;
4596 return expr_eval_no_error
;
4598 case gdb_agent_op_dup
:
4602 case gdb_agent_op_pop
:
4607 case gdb_agent_op_pick
:
4608 arg
= aexpr
->bytes
[pc
++];
4610 top
= stack
[sp
- arg
];
4614 case gdb_agent_op_rot
:
4616 ULONGEST tem
= stack
[sp
- 1];
4618 stack
[sp
- 1] = stack
[sp
- 2];
4619 stack
[sp
- 2] = top
;
4624 case gdb_agent_op_zero_ext
:
4625 arg
= aexpr
->bytes
[pc
++];
4626 if (arg
< (sizeof (LONGEST
) * 8))
4627 top
&= ((LONGEST
) 1 << arg
) - 1;
4630 case gdb_agent_op_swap
:
4631 /* Interchange top two stack elements, making sure top gets
4632 copied back onto stack. */
4634 top
= stack
[sp
- 1];
4635 stack
[sp
- 1] = stack
[sp
];
4638 case gdb_agent_op_getv
:
4639 /* Flush the cached stack top. */
4641 arg
= aexpr
->bytes
[pc
++];
4642 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4643 top
= get_trace_state_variable_value (arg
);
4646 case gdb_agent_op_setv
:
4647 arg
= aexpr
->bytes
[pc
++];
4648 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4649 set_trace_state_variable_value (arg
, top
);
4650 /* Note that we leave the value on the stack, for the
4651 benefit of later/enclosing expressions. */
4654 case gdb_agent_op_tracev
:
4655 arg
= aexpr
->bytes
[pc
++];
4656 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4657 agent_tsv_read (tframe
, arg
);
4660 case gdb_agent_op_tracenz
:
4661 agent_mem_read_string (tframe
, NULL
, (CORE_ADDR
) stack
[--sp
],
4667 /* GDB never (currently) generates any of these ops. */
4668 case gdb_agent_op_float
:
4669 case gdb_agent_op_ref_float
:
4670 case gdb_agent_op_ref_double
:
4671 case gdb_agent_op_ref_long_double
:
4672 case gdb_agent_op_l_to_d
:
4673 case gdb_agent_op_d_to_l
:
4674 case gdb_agent_op_trace16
:
4675 trace_debug ("Agent expression op 0x%x valid, but not handled",
4677 /* If ever GDB generates any of these, we don't have the
4678 option of ignoring. */
4682 trace_debug ("Agent expression op 0x%x not recognized", op
);
4683 /* Don't struggle on, things will just get worse. */
4684 return expr_eval_unrecognized_opcode
;
4687 /* Check for stack badness. */
4688 if (sp
>= (STACK_MAX
- 1))
4690 trace_debug ("Expression stack overflow");
4691 return expr_eval_stack_overflow
;
4696 trace_debug ("Expression stack underflow");
4697 return expr_eval_stack_underflow
;
4700 trace_debug ("Op %s -> sp=%d, top=0x%s",
4701 gdb_agent_op_name (op
), sp
, pulongest (top
));
4705 /* Do memory copies for bytecodes. */
4706 /* Do the recording of memory blocks for actions and bytecodes. */
4709 agent_mem_read (struct traceframe
*tframe
,
4710 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4712 unsigned char *mspace
;
4713 ULONGEST remaining
= len
;
4714 unsigned short blocklen
;
4716 /* If a 'to' buffer is specified, use it. */
4719 read_inferior_memory (from
, to
, len
);
4723 /* Otherwise, create a new memory block in the trace buffer. */
4724 while (remaining
> 0)
4728 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4729 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4730 mspace
= add_traceframe_block (tframe
, sp
);
4733 /* Identify block as a memory block. */
4736 /* Record address and size. */
4737 memcpy (mspace
, &from
, sizeof (from
));
4738 mspace
+= sizeof (from
);
4739 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4740 mspace
+= sizeof (blocklen
);
4741 /* Record the memory block proper. */
4742 read_inferior_memory (from
, mspace
, blocklen
);
4743 trace_debug ("%d bytes recorded", blocklen
);
4744 remaining
-= blocklen
;
4751 agent_mem_read_string (struct traceframe
*tframe
,
4752 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4754 unsigned char *buf
, *mspace
;
4755 ULONGEST remaining
= len
;
4756 unsigned short blocklen
, i
;
4758 /* To save a bit of space, block lengths are 16-bit, so break large
4759 requests into multiple blocks. Bordering on overkill for strings,
4760 but it could happen that someone specifies a large max length. */
4761 while (remaining
> 0)
4765 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4766 /* We want working space to accumulate nonzero bytes, since
4767 traceframes must have a predecided size (otherwise it gets
4768 harder to wrap correctly for the circular case, etc). */
4769 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4770 for (i
= 0; i
< blocklen
; ++i
)
4772 /* Read the string one byte at a time, in case the string is
4773 at the end of a valid memory area - we don't want a
4774 correctly-terminated string to engender segvio
4776 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4781 /* Make sure outer loop stops now too. */
4782 remaining
= blocklen
;
4786 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4787 mspace
= add_traceframe_block (tframe
, sp
);
4793 /* Identify block as a memory block. */
4796 /* Record address and size. */
4797 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4798 mspace
+= sizeof (from
);
4799 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4800 mspace
+= sizeof (blocklen
);
4801 /* Copy the string contents. */
4802 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
4803 remaining
-= blocklen
;
4810 /* Record the value of a trace state variable. */
4813 agent_tsv_read (struct traceframe
*tframe
, int n
)
4815 unsigned char *vspace
;
4818 vspace
= add_traceframe_block (tframe
,
4819 1 + sizeof (n
) + sizeof (LONGEST
));
4822 /* Identify block as a variable. */
4824 /* Record variable's number and value. */
4825 memcpy (vspace
+ 1, &n
, sizeof (n
));
4826 val
= get_trace_state_variable_value (n
);
4827 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4828 trace_debug ("Variable %d recorded", n
);
4832 #ifndef IN_PROCESS_AGENT
4834 /* Callback for traceframe_walk_blocks, used to find a given block
4835 type in a traceframe. */
4838 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
4840 char *wantedp
= data
;
4842 if (*wantedp
== blocktype
)
4848 /* Walk over all traceframe blocks of the traceframe buffer starting
4849 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4850 block found, passing in DATA unmodified. If CALLBACK returns true,
4851 this returns a pointer to where the block is found. Returns NULL
4852 if no callback call returned true, indicating that all blocks have
4855 static unsigned char *
4856 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
4858 int (*callback
) (char blocktype
,
4859 unsigned char *dataptr
,
4863 unsigned char *dataptr
;
4867 trace_debug ("traceframe %d has no data", tfnum
);
4871 /* Iterate through a traceframe's blocks, looking for a block of the
4873 for (dataptr
= database
;
4874 dataptr
< database
+ datasize
;
4878 unsigned short mlen
;
4880 if (dataptr
== trace_buffer_wrap
)
4882 /* Adjust to reflect wrapping part of the frame around to
4884 datasize
= dataptr
- database
;
4885 dataptr
= database
= trace_buffer_lo
;
4888 blocktype
= *dataptr
++;
4890 if ((*callback
) (blocktype
, dataptr
, data
))
4896 /* Skip over the registers block. */
4897 dataptr
+= register_cache_size ();
4900 /* Skip over the memory block. */
4901 dataptr
+= sizeof (CORE_ADDR
);
4902 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4903 dataptr
+= (sizeof (mlen
) + mlen
);
4906 /* Skip over the TSV block. */
4907 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4910 /* Skip over the static trace data block. */
4911 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4912 dataptr
+= (sizeof (mlen
) + mlen
);
4915 trace_debug ("traceframe %d has unknown block type 0x%x",
4924 /* Look for the block of type TYPE_WANTED in the trameframe starting
4925 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4928 static unsigned char *
4929 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4930 int tfnum
, char type_wanted
)
4932 return traceframe_walk_blocks (database
, datasize
, tfnum
,
4933 match_blocktype
, &type_wanted
);
4936 static unsigned char *
4937 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4939 unsigned char *regblock
;
4941 regblock
= traceframe_find_block_type (tframe
->data
,
4945 if (regblock
== NULL
)
4946 trace_debug ("traceframe %d has no register data", tfnum
);
4951 /* Get registers from a traceframe. */
4954 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4956 unsigned char *dataptr
;
4957 struct tracepoint
*tpoint
;
4958 struct traceframe
*tframe
;
4960 tframe
= find_traceframe (tfnum
);
4964 trace_debug ("traceframe %d not found", tfnum
);
4968 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4969 if (dataptr
== NULL
)
4971 /* Mark registers unavailable. */
4972 supply_regblock (regcache
, NULL
);
4974 /* We can generally guess at a PC, although this will be
4975 misleading for while-stepping frames and multi-location
4977 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4979 regcache_write_pc (regcache
, tpoint
->address
);
4982 supply_regblock (regcache
, dataptr
);
4988 traceframe_get_pc (struct traceframe
*tframe
)
4990 struct regcache regcache
;
4991 unsigned char *dataptr
;
4993 dataptr
= traceframe_find_regblock (tframe
, -1);
4994 if (dataptr
== NULL
)
4997 init_register_cache (®cache
, dataptr
);
4998 return regcache_read_pc (®cache
);
5001 /* Read a requested block of memory from a trace frame. */
5004 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5005 unsigned char *buf
, ULONGEST length
,
5008 struct traceframe
*tframe
;
5009 unsigned char *database
, *dataptr
;
5010 unsigned int datasize
;
5012 unsigned short mlen
;
5014 trace_debug ("traceframe_read_mem");
5016 tframe
= find_traceframe (tfnum
);
5020 trace_debug ("traceframe %d not found", tfnum
);
5024 datasize
= tframe
->data_size
;
5025 database
= dataptr
= &tframe
->data
[0];
5027 /* Iterate through a traceframe's blocks, looking for memory. */
5028 while ((dataptr
= traceframe_find_block_type (dataptr
,
5030 - (dataptr
- database
),
5031 tfnum
, 'M')) != NULL
)
5033 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5034 dataptr
+= sizeof (maddr
);
5035 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5036 dataptr
+= sizeof (mlen
);
5037 trace_debug ("traceframe %d has %d bytes at %s",
5038 tfnum
, mlen
, paddress (maddr
));
5040 /* If the block includes the first part of the desired range,
5041 return as much it has; GDB will re-request the remainder,
5042 which might be in a different block of this trace frame. */
5043 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5045 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5049 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5054 /* Skip over this block. */
5058 trace_debug ("traceframe %d has no memory data for the desired region",
5066 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5069 struct traceframe
*tframe
;
5070 unsigned char *database
, *dataptr
;
5071 unsigned int datasize
;
5074 trace_debug ("traceframe_read_tsv");
5076 tfnum
= current_traceframe
;
5080 trace_debug ("no current traceframe");
5084 tframe
= find_traceframe (tfnum
);
5088 trace_debug ("traceframe %d not found", tfnum
);
5092 datasize
= tframe
->data_size
;
5093 database
= dataptr
= &tframe
->data
[0];
5095 /* Iterate through a traceframe's blocks, looking for the tsv. */
5096 while ((dataptr
= traceframe_find_block_type (dataptr
,
5098 - (dataptr
- database
),
5099 tfnum
, 'V')) != NULL
)
5101 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5102 dataptr
+= sizeof (vnum
);
5104 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5106 /* Check that this is the variable we want. */
5109 memcpy (val
, dataptr
, sizeof (*val
));
5113 /* Skip over this block. */
5114 dataptr
+= sizeof (LONGEST
);
5117 trace_debug ("traceframe %d has no data for variable %d",
5122 /* Read a requested block of static tracepoint data from a trace
5126 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5127 unsigned char *buf
, ULONGEST length
,
5130 struct traceframe
*tframe
;
5131 unsigned char *database
, *dataptr
;
5132 unsigned int datasize
;
5133 unsigned short mlen
;
5135 trace_debug ("traceframe_read_sdata");
5137 tframe
= find_traceframe (tfnum
);
5141 trace_debug ("traceframe %d not found", tfnum
);
5145 datasize
= tframe
->data_size
;
5146 database
= &tframe
->data
[0];
5148 /* Iterate through a traceframe's blocks, looking for static
5150 dataptr
= traceframe_find_block_type (database
, datasize
,
5152 if (dataptr
!= NULL
)
5154 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5155 dataptr
+= sizeof (mlen
);
5158 if (offset
+ length
> mlen
)
5159 length
= mlen
- offset
;
5161 memcpy (buf
, dataptr
, length
);
5169 trace_debug ("traceframe %d has no static trace data", tfnum
);
5175 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5176 object. DATA is pointer to a struct buffer holding the
5177 traceframe-info object being built. */
5180 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5182 struct buffer
*buffer
= data
;
5188 unsigned short mlen
;
5191 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5192 dataptr
+= sizeof (maddr
);
5193 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5194 dataptr
+= sizeof (mlen
);
5195 buffer_xml_printf (buffer
,
5196 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5197 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5207 warning ("Unhandled trace block type (%d) '%c ' "
5208 "while building trace frame info.",
5209 blocktype
, blocktype
);
5216 /* Build a traceframe-info object for traceframe number TFNUM into
5220 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5222 struct traceframe
*tframe
;
5224 trace_debug ("traceframe_read_info");
5226 tframe
= find_traceframe (tfnum
);
5230 trace_debug ("traceframe %d not found", tfnum
);
5234 buffer_grow_str (buffer
, "<traceframe-info>\n");
5235 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5236 tfnum
, build_traceframe_info_xml
, buffer
);
5237 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5241 /* Return the first fast tracepoint whose jump pad contains PC. */
5243 static struct tracepoint
*
5244 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5246 struct tracepoint
*tpoint
;
5248 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5249 if (tpoint
->type
== fast_tracepoint
)
5250 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5256 /* Return GDBserver's tracepoint that matches the IP Agent's
5257 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5260 static struct tracepoint
*
5261 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5263 struct tracepoint
*tpoint
;
5265 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5266 if (tpoint
->type
== fast_tracepoint
)
5267 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5275 /* The type of the object that is used to synchronize fast tracepoint
5278 typedef struct collecting_t
5280 /* The fast tracepoint number currently collecting. */
5283 /* A number that GDBserver can use to identify the thread that is
5284 presently holding the collect lock. This need not (and usually
5285 is not) the thread id, as getting the current thread ID usually
5286 requires a system call, which we want to avoid like the plague.
5287 Usually this is thread's TCB, found in the TLS (pseudo-)
5288 register, which is readable with a single insn on several
5290 uintptr_t thread_area
;
5293 #ifndef IN_PROCESS_AGENT
5296 force_unlock_trace_buffer (void)
5298 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5301 /* Check if the thread identified by THREAD_AREA which is stopped at
5302 STOP_PC, is presently locking the fast tracepoint collection, and
5303 if so, gather some status of said collection. Returns 0 if the
5304 thread isn't collecting or in the jump pad at all. 1, if in the
5305 jump pad (or within gdb_collect) and hasn't executed the adjusted
5306 original insn yet (can set a breakpoint there and run to it). 2,
5307 if presently executing the adjusted original insn --- in which
5308 case, if we want to move the thread out of the jump pad, we need to
5309 single-step it until this function returns 0. */
5312 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5314 struct fast_tpoint_collect_status
*status
)
5316 CORE_ADDR ipa_collecting
;
5317 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5318 struct tracepoint
*tpoint
;
5319 int needs_breakpoint
;
5321 /* The thread THREAD_AREA is either:
5323 0. not collecting at all, not within the jump pad, or within
5324 gdb_collect or one of its callees.
5326 1. in the jump pad and haven't reached gdb_collect
5328 2. within gdb_collect (out of the jump pad) (collect is set)
5330 3. we're in the jump pad, after gdb_collect having returned,
5331 possibly executing the adjusted insns.
5333 For cases 1 and 3, `collecting' may or not be set. The jump pad
5334 doesn't have any complicated jump logic, so we can tell if the
5335 thread is executing the adjust original insn or not by just
5336 matching STOP_PC with known jump pad addresses. If we it isn't
5337 yet executing the original insn, set a breakpoint there, and let
5338 the thread run to it, so to quickly step over a possible (many
5339 insns) gdb_collect call. Otherwise, or when the breakpoint is
5340 hit, only a few (small number of) insns are left to be executed
5341 in the jump pad. Single-step the thread until it leaves the
5346 needs_breakpoint
= 0;
5347 trace_debug ("fast_tracepoint_collecting");
5349 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5350 &ipa_gdb_jump_pad_buffer
))
5351 fatal ("error extracting `gdb_jump_pad_buffer'");
5352 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5353 &ipa_gdb_jump_pad_buffer_end
))
5354 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5356 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5357 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5359 /* We can tell which tracepoint(s) the thread is collecting by
5360 matching the jump pad address back to the tracepoint. */
5361 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5364 warning ("in jump pad, but no matching tpoint?");
5369 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5371 tpoint
->number
, paddress (tpoint
->address
),
5372 paddress (tpoint
->jump_pad
),
5373 paddress (tpoint
->jump_pad_end
),
5374 paddress (tpoint
->adjusted_insn_addr
),
5375 paddress (tpoint
->adjusted_insn_addr_end
));
5378 /* Definitely in the jump pad. May or may not need
5379 fast-exit-jump-pad breakpoint. */
5380 if (tpoint
->jump_pad
<= stop_pc
5381 && stop_pc
< tpoint
->adjusted_insn_addr
)
5382 needs_breakpoint
= 1;
5386 collecting_t ipa_collecting_obj
;
5388 /* If `collecting' is set/locked, then the THREAD_AREA thread
5389 may or not be the one holding the lock. We have to read the
5390 lock to find out. */
5392 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5395 trace_debug ("fast_tracepoint_collecting:"
5396 " failed reading 'collecting' in the inferior");
5400 if (!ipa_collecting
)
5402 trace_debug ("fast_tracepoint_collecting: not collecting"
5403 " (and nobody is).");
5407 /* Some thread is collecting. Check which. */
5408 if (read_inferior_memory (ipa_collecting
,
5409 (unsigned char *) &ipa_collecting_obj
,
5410 sizeof (ipa_collecting_obj
)) != 0)
5413 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5415 trace_debug ("fast_tracepoint_collecting: not collecting "
5416 "(another thread is)");
5421 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5424 warning ("fast_tracepoint_collecting: collecting, "
5425 "but tpoint %s not found?",
5426 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5430 /* The thread is within `gdb_collect', skip over the rest of
5431 fast tracepoint collection quickly using a breakpoint. */
5432 needs_breakpoint
= 1;
5435 /* The caller wants a bit of status detail. */
5438 status
->tpoint_num
= tpoint
->number
;
5439 status
->tpoint_addr
= tpoint
->address
;
5440 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5441 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5444 if (needs_breakpoint
)
5446 /* Hasn't executed the original instruction yet. Set breakpoint
5447 there, and wait till it's hit, then single-step until exiting
5451 fast_tracepoint_collecting, returning continue-until-break at %s",
5452 paddress (tpoint
->adjusted_insn_addr
));
5454 return 1; /* continue */
5458 /* Just single-step until exiting the jump pad. */
5460 trace_debug ("fast_tracepoint_collecting, returning "
5461 "need-single-step (%s-%s)",
5462 paddress (tpoint
->adjusted_insn_addr
),
5463 paddress (tpoint
->adjusted_insn_addr_end
));
5465 return 2; /* single-step */
5471 #ifdef IN_PROCESS_AGENT
5473 /* The global fast tracepoint collect lock. Points to a collecting_t
5474 object built on the stack by the jump pad, if presently locked;
5475 NULL if it isn't locked. Note that this lock *must* be set while
5476 executing any *function other than the jump pad. See
5477 fast_tracepoint_collecting. */
5478 static collecting_t
* ATTR_USED collecting
;
5480 /* This routine, called from the jump pad (in asm) is designed to be
5481 called from the jump pads of fast tracepoints, thus it is on the
5484 IP_AGENT_EXPORT
void ATTR_USED
5485 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5487 struct fast_tracepoint_ctx ctx
;
5489 /* Don't do anything until the trace run is completely set up. */
5493 ctx
.base
.type
= fast_tracepoint
;
5495 ctx
.regcache_initted
= 0;
5496 /* Wrap the regblock in a register cache (in the stack, we don't
5497 want to malloc here). */
5498 ctx
.regspace
= alloca (register_cache_size ());
5499 if (ctx
.regspace
== NULL
)
5501 trace_debug ("Trace buffer block allocation failed, skipping");
5505 for (ctx
.tpoint
= tpoint
;
5506 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5507 ctx
.tpoint
= ctx
.tpoint
->next
)
5509 if (!ctx
.tpoint
->enabled
)
5512 /* Multiple tracepoints of different types, such as fast tracepoint and
5513 static tracepoint, can be set at the same address. */
5514 if (ctx
.tpoint
->type
!= tpoint
->type
)
5517 /* Test the condition if present, and collect if true. */
5518 if (ctx
.tpoint
->cond
== NULL
5519 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5522 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5523 ctx
.tpoint
->address
, ctx
.tpoint
);
5525 /* Note that this will cause original insns to be written back
5526 to where we jumped from, but that's OK because we're jumping
5527 back to the next whole instruction. This will go badly if
5528 instruction restoration is not atomic though. */
5529 if (stopping_tracepoint
5530 || trace_buffer_is_full
5531 || expr_eval_result
!= expr_eval_no_error
)
5539 /* If there was a condition and it evaluated to false, the only
5540 way we would stop tracing is if there was an error during
5541 condition expression evaluation. */
5542 if (expr_eval_result
!= expr_eval_no_error
)
5553 #ifndef IN_PROCESS_AGENT
5555 /* Bytecode compilation. */
5557 CORE_ADDR current_insn_ptr
;
5561 struct bytecode_address
5566 /* Offset and size of field to be modified in the goto block. */
5567 int from_offset
, from_size
;
5568 struct bytecode_address
*next
;
5569 } *bytecode_address_table
;
5572 get_raw_reg_func_addr (void)
5574 return ipa_sym_addrs
.addr_get_raw_reg
;
5578 emit_prologue (void)
5580 target_emit_ops ()->emit_prologue ();
5584 emit_epilogue (void)
5586 target_emit_ops ()->emit_epilogue ();
5592 target_emit_ops ()->emit_add ();
5598 target_emit_ops ()->emit_sub ();
5604 target_emit_ops ()->emit_mul ();
5610 target_emit_ops ()->emit_lsh ();
5614 emit_rsh_signed (void)
5616 target_emit_ops ()->emit_rsh_signed ();
5620 emit_rsh_unsigned (void)
5622 target_emit_ops ()->emit_rsh_unsigned ();
5628 target_emit_ops ()->emit_ext (arg
);
5634 target_emit_ops ()->emit_log_not ();
5640 target_emit_ops ()->emit_bit_and ();
5646 target_emit_ops ()->emit_bit_or ();
5652 target_emit_ops ()->emit_bit_xor ();
5658 target_emit_ops ()->emit_bit_not ();
5664 target_emit_ops ()->emit_equal ();
5668 emit_less_signed (void)
5670 target_emit_ops ()->emit_less_signed ();
5674 emit_less_unsigned (void)
5676 target_emit_ops ()->emit_less_unsigned ();
5682 target_emit_ops ()->emit_ref (size
);
5686 emit_if_goto (int *offset_p
, int *size_p
)
5688 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
5692 emit_goto (int *offset_p
, int *size_p
)
5694 target_emit_ops ()->emit_goto (offset_p
, size_p
);
5698 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
5700 target_emit_ops ()->write_goto_address (from
, to
, size
);
5704 emit_const (LONGEST num
)
5706 target_emit_ops ()->emit_const (num
);
5712 target_emit_ops ()->emit_reg (reg
);
5718 target_emit_ops ()->emit_pop ();
5722 emit_stack_flush (void)
5724 target_emit_ops ()->emit_stack_flush ();
5728 emit_zero_ext (int arg
)
5730 target_emit_ops ()->emit_zero_ext (arg
);
5736 target_emit_ops ()->emit_swap ();
5740 emit_stack_adjust (int n
)
5742 target_emit_ops ()->emit_stack_adjust (n
);
5745 /* FN's prototype is `LONGEST(*fn)(int)'. */
5748 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
5750 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
5753 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
5756 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
5758 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
5762 emit_eq_goto (int *offset_p
, int *size_p
)
5764 target_emit_ops ()->emit_eq_goto (offset_p
, size_p
);
5768 emit_ne_goto (int *offset_p
, int *size_p
)
5770 target_emit_ops ()->emit_ne_goto (offset_p
, size_p
);
5774 emit_lt_goto (int *offset_p
, int *size_p
)
5776 target_emit_ops ()->emit_lt_goto (offset_p
, size_p
);
5780 emit_ge_goto (int *offset_p
, int *size_p
)
5782 target_emit_ops ()->emit_ge_goto (offset_p
, size_p
);
5786 emit_gt_goto (int *offset_p
, int *size_p
)
5788 target_emit_ops ()->emit_gt_goto (offset_p
, size_p
);
5792 emit_le_goto (int *offset_p
, int *size_p
)
5794 target_emit_ops ()->emit_le_goto (offset_p
, size_p
);
5797 static enum eval_result_type
compile_bytecodes (struct agent_expr
*aexpr
);
5800 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5801 CORE_ADDR
*jump_entry
)
5803 CORE_ADDR entry_point
= *jump_entry
;
5804 enum eval_result_type err
;
5806 trace_debug ("Starting condition compilation for tracepoint %d\n",
5809 /* Initialize the global pointer to the code being built. */
5810 current_insn_ptr
= *jump_entry
;
5814 err
= compile_bytecodes (tpoint
->cond
);
5816 if (err
== expr_eval_no_error
)
5820 /* Record the beginning of the compiled code. */
5821 tpoint
->compiled_cond
= entry_point
;
5823 trace_debug ("Condition compilation for tracepoint %d complete\n",
5828 /* Leave the unfinished code in situ, but don't point to it. */
5830 tpoint
->compiled_cond
= 0;
5832 trace_debug ("Condition compilation for tracepoint %d failed, "
5834 tpoint
->number
, err
);
5837 /* Update the code pointer passed in. Note that we do this even if
5838 the compile fails, so that we can look at the partial results
5839 instead of letting them be overwritten. */
5840 *jump_entry
= current_insn_ptr
;
5842 /* Leave a gap, to aid dump decipherment. */
5846 /* Scan an agent expression for any evidence that the given PC is the
5847 target of a jump bytecode in the expression. */
5850 is_goto_target (struct agent_expr
*aexpr
, int pc
)
5855 for (i
= 0; i
< aexpr
->length
; i
+= 1 + gdb_agent_op_sizes
[op
])
5857 op
= aexpr
->bytes
[i
];
5859 if (op
== gdb_agent_op_goto
|| op
== gdb_agent_op_if_goto
)
5861 int target
= (aexpr
->bytes
[i
+ 1] << 8) + aexpr
->bytes
[i
+ 2];
5870 /* Given an agent expression, turn it into native code. */
5872 static enum eval_result_type
5873 compile_bytecodes (struct agent_expr
*aexpr
)
5877 unsigned char op
, next_op
;
5879 /* This is only used to build 64-bit value for constants. */
5881 struct bytecode_address
*aentry
, *aentry2
;
5886 trace_debug ("Cannot compile op 0x%x\n", op); \
5887 return expr_eval_unhandled_opcode; \
5890 if (aexpr
->length
== 0)
5892 trace_debug ("empty agent expression\n");
5893 return expr_eval_empty_expression
;
5896 bytecode_address_table
= NULL
;
5900 op
= aexpr
->bytes
[pc
];
5902 trace_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
5904 /* Record the compiled-code address of the bytecode, for use by
5905 jump instructions. */
5906 aentry
= xmalloc (sizeof (struct bytecode_address
));
5908 aentry
->address
= current_insn_ptr
;
5909 aentry
->goto_pc
= -1;
5910 aentry
->from_offset
= aentry
->from_size
= 0;
5911 aentry
->next
= bytecode_address_table
;
5912 bytecode_address_table
= aentry
;
5920 case gdb_agent_op_add
:
5924 case gdb_agent_op_sub
:
5928 case gdb_agent_op_mul
:
5932 case gdb_agent_op_div_signed
:
5936 case gdb_agent_op_div_unsigned
:
5940 case gdb_agent_op_rem_signed
:
5944 case gdb_agent_op_rem_unsigned
:
5948 case gdb_agent_op_lsh
:
5952 case gdb_agent_op_rsh_signed
:
5956 case gdb_agent_op_rsh_unsigned
:
5957 emit_rsh_unsigned ();
5960 case gdb_agent_op_trace
:
5964 case gdb_agent_op_trace_quick
:
5968 case gdb_agent_op_log_not
:
5972 case gdb_agent_op_bit_and
:
5976 case gdb_agent_op_bit_or
:
5980 case gdb_agent_op_bit_xor
:
5984 case gdb_agent_op_bit_not
:
5988 case gdb_agent_op_equal
:
5989 next_op
= aexpr
->bytes
[pc
];
5990 if (next_op
== gdb_agent_op_if_goto
5991 && !is_goto_target (aexpr
, pc
)
5992 && target_emit_ops ()->emit_eq_goto
)
5994 trace_debug ("Combining equal & if_goto");
5997 arg
= aexpr
->bytes
[pc
++];
5998 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5999 aentry
->goto_pc
= arg
;
6000 emit_eq_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6002 else if (next_op
== gdb_agent_op_log_not
6003 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
6004 && !is_goto_target (aexpr
, pc
+ 1)
6005 && target_emit_ops ()->emit_ne_goto
)
6007 trace_debug ("Combining equal & log_not & if_goto");
6010 arg
= aexpr
->bytes
[pc
++];
6011 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6012 aentry
->goto_pc
= arg
;
6013 emit_ne_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6019 case gdb_agent_op_less_signed
:
6020 next_op
= aexpr
->bytes
[pc
];
6021 if (next_op
== gdb_agent_op_if_goto
6022 && !is_goto_target (aexpr
, pc
))
6024 trace_debug ("Combining less_signed & if_goto");
6027 arg
= aexpr
->bytes
[pc
++];
6028 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6029 aentry
->goto_pc
= arg
;
6030 emit_lt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6032 else if (next_op
== gdb_agent_op_log_not
6033 && !is_goto_target (aexpr
, pc
)
6034 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
6035 && !is_goto_target (aexpr
, pc
+ 1))
6037 trace_debug ("Combining less_signed & log_not & if_goto");
6040 arg
= aexpr
->bytes
[pc
++];
6041 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6042 aentry
->goto_pc
= arg
;
6043 emit_ge_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6046 emit_less_signed ();
6049 case gdb_agent_op_less_unsigned
:
6050 emit_less_unsigned ();
6053 case gdb_agent_op_ext
:
6054 arg
= aexpr
->bytes
[pc
++];
6055 if (arg
< (sizeof (LONGEST
) * 8))
6059 case gdb_agent_op_ref8
:
6063 case gdb_agent_op_ref16
:
6067 case gdb_agent_op_ref32
:
6071 case gdb_agent_op_ref64
:
6075 case gdb_agent_op_if_goto
:
6076 arg
= aexpr
->bytes
[pc
++];
6077 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6078 aentry
->goto_pc
= arg
;
6079 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6082 case gdb_agent_op_goto
:
6083 arg
= aexpr
->bytes
[pc
++];
6084 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6085 aentry
->goto_pc
= arg
;
6086 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6089 case gdb_agent_op_const8
:
6090 emit_stack_flush ();
6091 top
= aexpr
->bytes
[pc
++];
6095 case gdb_agent_op_const16
:
6096 emit_stack_flush ();
6097 top
= aexpr
->bytes
[pc
++];
6098 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6102 case gdb_agent_op_const32
:
6103 emit_stack_flush ();
6104 top
= aexpr
->bytes
[pc
++];
6105 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6106 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6107 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6111 case gdb_agent_op_const64
:
6112 emit_stack_flush ();
6113 top
= aexpr
->bytes
[pc
++];
6114 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6115 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6116 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6117 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6118 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6119 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6120 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6124 case gdb_agent_op_reg
:
6125 emit_stack_flush ();
6126 arg
= aexpr
->bytes
[pc
++];
6127 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6131 case gdb_agent_op_end
:
6132 trace_debug ("At end of expression\n");
6134 /* Assume there is one stack element left, and that it is
6135 cached in "top" where emit_epilogue can get to it. */
6136 emit_stack_adjust (1);
6141 case gdb_agent_op_dup
:
6142 /* In our design, dup is equivalent to stack flushing. */
6143 emit_stack_flush ();
6146 case gdb_agent_op_pop
:
6150 case gdb_agent_op_zero_ext
:
6151 arg
= aexpr
->bytes
[pc
++];
6152 if (arg
< (sizeof (LONGEST
) * 8))
6153 emit_zero_ext (arg
);
6156 case gdb_agent_op_swap
:
6157 next_op
= aexpr
->bytes
[pc
];
6158 /* Detect greater-than comparison sequences. */
6159 if (next_op
== gdb_agent_op_less_signed
6160 && !is_goto_target (aexpr
, pc
)
6161 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
6162 && !is_goto_target (aexpr
, pc
+ 1))
6164 trace_debug ("Combining swap & less_signed & if_goto");
6167 arg
= aexpr
->bytes
[pc
++];
6168 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6169 aentry
->goto_pc
= arg
;
6170 emit_gt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6172 else if (next_op
== gdb_agent_op_less_signed
6173 && !is_goto_target (aexpr
, pc
)
6174 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_log_not
)
6175 && !is_goto_target (aexpr
, pc
+ 1)
6176 && (aexpr
->bytes
[pc
+ 2] == gdb_agent_op_if_goto
)
6177 && !is_goto_target (aexpr
, pc
+ 2))
6179 trace_debug ("Combining swap & less_signed & log_not & if_goto");
6182 arg
= aexpr
->bytes
[pc
++];
6183 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6184 aentry
->goto_pc
= arg
;
6185 emit_le_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6191 case gdb_agent_op_getv
:
6192 emit_stack_flush ();
6193 arg
= aexpr
->bytes
[pc
++];
6194 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6195 emit_int_call_1 (ipa_sym_addrs
.addr_get_trace_state_variable_value
,
6199 case gdb_agent_op_setv
:
6200 arg
= aexpr
->bytes
[pc
++];
6201 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6202 emit_void_call_2 (ipa_sym_addrs
.addr_set_trace_state_variable_value
,
6206 case gdb_agent_op_tracev
:
6210 /* GDB never (currently) generates any of these ops. */
6211 case gdb_agent_op_float
:
6212 case gdb_agent_op_ref_float
:
6213 case gdb_agent_op_ref_double
:
6214 case gdb_agent_op_ref_long_double
:
6215 case gdb_agent_op_l_to_d
:
6216 case gdb_agent_op_d_to_l
:
6217 case gdb_agent_op_trace16
:
6222 trace_debug ("Agent expression op 0x%x not recognized\n", op
);
6223 /* Don't struggle on, things will just get worse. */
6224 return expr_eval_unrecognized_opcode
;
6227 /* This catches errors that occur in target-specific code
6231 trace_debug ("Error %d while emitting code for %s\n",
6232 emit_error
, gdb_agent_op_name (op
));
6233 return expr_eval_unhandled_opcode
;
6236 trace_debug ("Op %s compiled\n", gdb_agent_op_name (op
));
6239 /* Now fill in real addresses as goto destinations. */
6240 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
6244 if (aentry
->goto_pc
< 0)
6247 /* Find the location that we are going to, and call back into
6248 target-specific code to write the actual address or
6250 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
6252 if (aentry2
->pc
== aentry
->goto_pc
)
6254 trace_debug ("Want to jump from %s to %s\n",
6255 paddress (aentry
->address
),
6256 paddress (aentry2
->address
));
6257 write_goto_address (aentry
->address
+ aentry
->from_offset
,
6258 aentry2
->address
, aentry
->from_size
);
6264 /* Error out if we didn't find a destination. */
6267 trace_debug ("Destination of goto %d not found\n",
6269 return expr_eval_invalid_goto
;
6273 return expr_eval_no_error
;
6276 /* We'll need to adjust these when we consider bi-arch setups, and big
6280 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
6282 return write_inferior_memory (where
,
6283 (unsigned char *) &ptr
, sizeof (void *));
6286 /* The base pointer of the IPA's heap. This is the only memory the
6287 IPA is allowed to use. The IPA should _not_ call the inferior's
6288 `malloc' during operation. That'd be slow, and, most importantly,
6289 it may not be safe. We may be collecting a tracepoint in a signal
6290 handler, for example. */
6291 static CORE_ADDR target_tp_heap
;
6293 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
6297 target_malloc (ULONGEST size
)
6301 if (target_tp_heap
== 0)
6303 /* We have the pointer *address*, need what it points to. */
6304 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
6306 fatal ("could get target heap head pointer");
6309 ptr
= target_tp_heap
;
6310 target_tp_heap
+= size
;
6312 /* Pad to 8-byte alignment. */
6313 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
6319 download_agent_expr (struct agent_expr
*expr
)
6321 CORE_ADDR expr_addr
;
6322 CORE_ADDR expr_bytes
;
6324 expr_addr
= target_malloc (sizeof (*expr
));
6325 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
6327 expr_bytes
= target_malloc (expr
->length
);
6328 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
6330 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
6335 /* Align V up to N bits. */
6336 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
6338 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
6341 download_tracepoint_1 (struct tracepoint
*tpoint
)
6343 struct tracepoint target_tracepoint
;
6344 CORE_ADDR tpptr
= 0;
6346 gdb_assert (tpoint
->type
== fast_tracepoint
6347 || tpoint
->type
== static_tracepoint
);
6349 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
6351 CORE_ADDR jentry
, jump_entry
;
6353 jentry
= jump_entry
= get_jump_space_head ();
6355 if (tpoint
->cond
!= NULL
)
6357 /* Pad to 8-byte alignment. (needed?) */
6358 /* Actually this should be left for the target to
6360 jentry
= UALIGN (jentry
, 8);
6362 compile_tracepoint_condition (tpoint
, &jentry
);
6365 /* Pad to 8-byte alignment. */
6366 jentry
= UALIGN (jentry
, 8);
6367 claim_jump_space (jentry
- jump_entry
);
6370 target_tracepoint
= *tpoint
;
6372 tpptr
= target_malloc (sizeof (*tpoint
));
6373 tpoint
->obj_addr_on_target
= tpptr
;
6375 /* Write the whole object. We'll fix up its pointers in a bit.
6376 Assume no next for now. This is fixed up above on the next
6377 iteration, if there's any. */
6378 target_tracepoint
.next
= NULL
;
6379 /* Need to clear this here too, since we're downloading the
6380 tracepoints before clearing our own copy. */
6381 target_tracepoint
.hit_count
= 0;
6383 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
6384 sizeof (target_tracepoint
));
6387 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6389 download_agent_expr (tpoint
->cond
));
6391 if (tpoint
->numactions
)
6394 CORE_ADDR actions_array
;
6396 /* The pointers array. */
6398 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6399 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6403 /* Now for each pointer, download the action. */
6404 for (i
= 0; i
< tpoint
->numactions
; i
++)
6406 CORE_ADDR ipa_action
= 0;
6407 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6409 switch (action
->type
)
6413 = target_malloc (sizeof (struct collect_memory_action
));
6414 write_inferior_memory (ipa_action
,
6415 (unsigned char *) action
,
6416 sizeof (struct collect_memory_action
));
6420 = target_malloc (sizeof (struct collect_registers_action
));
6421 write_inferior_memory (ipa_action
,
6422 (unsigned char *) action
,
6423 sizeof (struct collect_registers_action
));
6428 struct eval_expr_action
*eaction
6429 = (struct eval_expr_action
*) action
;
6431 ipa_action
= target_malloc (sizeof (*eaction
));
6432 write_inferior_memory (ipa_action
,
6433 (unsigned char *) eaction
,
6436 expr
= download_agent_expr (eaction
->expr
);
6437 write_inferior_data_ptr
6438 (ipa_action
+ offsetof (struct eval_expr_action
, expr
),
6443 ipa_action
= target_malloc
6444 (sizeof (struct collect_static_trace_data_action
));
6445 write_inferior_memory
6447 (unsigned char *) action
,
6448 sizeof (struct collect_static_trace_data_action
));
6451 trace_debug ("unknown trace action '%c', ignoring",
6456 if (ipa_action
!= 0)
6457 write_inferior_data_ptr
6458 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
6465 download_tracepoints (void)
6467 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
6468 struct tracepoint
*tpoint
;
6470 /* Start out empty. */
6471 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
6473 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6475 if (tpoint
->type
!= fast_tracepoint
6476 && tpoint
->type
!= static_tracepoint
)
6481 download_tracepoint_1 (tpoint
);
6483 tpptr
= tpoint
->obj_addr_on_target
;
6485 if (tpoint
== tracepoints
)
6487 /* First object in list, set the head pointer in the
6489 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
6493 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
6501 download_trace_state_variables (void)
6503 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6504 struct trace_state_variable
*tsv
;
6506 /* Start out empty. */
6507 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6509 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6511 struct trace_state_variable target_tsv
;
6513 /* TSV's with a getter have been initialized equally in both the
6514 inferior and GDBserver. Skip them. */
6515 if (tsv
->getter
!= NULL
)
6521 ptr
= target_malloc (sizeof (*tsv
));
6523 if (tsv
== trace_state_variables
)
6525 /* First object in list, set the head pointer in the
6528 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6533 write_inferior_data_ptr (prev_ptr
6534 + offsetof (struct trace_state_variable
,
6539 /* Write the whole object. We'll fix up its pointers in a bit.
6540 Assume no next, fixup when needed. */
6541 target_tsv
.next
= NULL
;
6543 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6544 sizeof (target_tsv
));
6546 if (tsv
->name
!= NULL
)
6548 size_t size
= strlen (tsv
->name
) + 1;
6549 CORE_ADDR name_addr
= target_malloc (size
);
6550 write_inferior_memory (name_addr
,
6551 (unsigned char *) tsv
->name
, size
);
6552 write_inferior_data_ptr (ptr
6553 + offsetof (struct trace_state_variable
,
6558 if (tsv
->getter
!= NULL
)
6560 fatal ("what to do with these?");
6566 /* Fixup the next pointer in the last item in the list. */
6567 write_inferior_data_ptr (prev_ptr
6568 + offsetof (struct trace_state_variable
,
6573 /* Upload complete trace frames out of the IP Agent's trace buffer
6574 into GDBserver's trace buffer. This always uploads either all or
6575 no trace frames. This is the counter part of
6576 `trace_alloc_trace_buffer'. See its description of the atomic
6577 synching mechanism. */
6580 upload_fast_traceframes (void)
6582 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6583 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6585 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6586 unsigned int curr_tbctrl_idx
;
6587 unsigned int ipa_trace_buffer_ctrl_curr
;
6588 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6589 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6590 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6591 CORE_ADDR ipa_trace_buffer_lo
;
6592 CORE_ADDR ipa_trace_buffer_hi
;
6594 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6595 &ipa_traceframe_read_count_racy
))
6597 /* This will happen in most targets if the current thread is
6602 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6603 &ipa_traceframe_write_count_racy
))
6606 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6607 ipa_traceframe_write_count_racy
6608 - ipa_traceframe_read_count_racy
,
6609 ipa_traceframe_write_count_racy
,
6610 ipa_traceframe_read_count_racy
);
6612 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6615 about_to_request_buffer_space_bkpt
6616 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6619 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6620 &ipa_trace_buffer_ctrl_curr
))
6623 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6625 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6628 unsigned int prev
, counter
;
6630 /* Update the token, with new counters, and the GDBserver stamp
6631 bit. Alway reuse the current TBC index. */
6632 prev
= ipa_trace_buffer_ctrl_curr
& 0x0007ff00;
6633 counter
= (prev
+ 0x100) & 0x0007ff00;
6635 ipa_trace_buffer_ctrl_curr
= (0x80000000
6641 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6642 ipa_trace_buffer_ctrl_curr
))
6645 trace_debug ("Lib: Committed %08x -> %08x",
6646 ipa_trace_buffer_ctrl_curr_old
,
6647 ipa_trace_buffer_ctrl_curr
);
6649 /* Re-read these, now that we've installed the
6650 `about_to_request_buffer_space' breakpoint/lock. A thread could
6651 have finished a traceframe between the last read of these
6652 counters and setting the breakpoint above. If we start
6653 uploading, we never want to leave this function with
6654 traceframe_read_count != 0, otherwise, GDBserver could end up
6655 incrementing the counter tokens more than once (due to event loop
6656 nesting), which would break the IP agent's "effective" detection
6657 (see trace_alloc_trace_buffer). */
6658 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6659 &ipa_traceframe_read_count
))
6661 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6662 &ipa_traceframe_write_count
))
6667 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6668 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6669 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6671 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6672 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6673 trace_debug ("note that ipa_traceframe_count's parts changed");
6676 /* Get the address of the current TBC object (the IP agent has an
6677 array of 3 such objects). The index is stored in the TBC
6679 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6680 ipa_trace_buffer_ctrl_addr
6681 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6683 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6684 (unsigned char *) &ipa_trace_buffer_ctrl
,
6685 sizeof (struct ipa_trace_buffer_control
)))
6688 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6689 &ipa_trace_buffer_lo
))
6691 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6692 &ipa_trace_buffer_hi
))
6695 /* Offsets are easier to grok for debugging than raw addresses,
6696 especially for the small trace buffer sizes that are useful for
6698 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6699 "endfree=%d wrap=%d hi=%d",
6701 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6702 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6703 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6704 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6705 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6707 /* Note that the IPA's buffer is always circular. */
6709 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6711 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6712 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6714 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6715 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6716 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6717 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6720 tf
= IPA_FIRST_TRACEFRAME ();
6722 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6724 struct tracepoint
*tpoint
;
6725 struct traceframe
*tframe
;
6726 unsigned char *block
;
6727 struct traceframe ipa_tframe
;
6729 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6730 offsetof (struct traceframe
, data
)))
6731 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6733 if (ipa_tframe
.tpnum
== 0)
6734 fatal ("Uploading: No (more) fast traceframes, but "
6735 "ipa_traceframe_count == %u??\n",
6736 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6738 /* Note that this will be incorrect for multi-location
6740 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6742 tframe
= add_traceframe (tpoint
);
6745 trace_buffer_is_full
= 1;
6746 trace_debug ("Uploading: trace buffer is full");
6750 /* Copy the whole set of blocks in one go for now. FIXME:
6751 split this in smaller blocks. */
6752 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6755 if (read_inferior_memory (tf
6756 + offsetof (struct traceframe
, data
),
6757 block
, ipa_tframe
.data_size
))
6758 error ("Uploading: Couldn't read traceframe data at %s\n",
6759 paddress (tf
+ offsetof (struct traceframe
, data
)));
6762 trace_debug ("Uploading: traceframe didn't fit");
6763 finish_traceframe (tframe
);
6766 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6768 /* If we freed the traceframe that wrapped around, go back
6769 to the non-wrap case. */
6770 if (tf
< ipa_trace_buffer_ctrl
.start
)
6772 trace_debug ("Lib: Discarding past the wraparound");
6773 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6775 ipa_trace_buffer_ctrl
.start
= tf
;
6776 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6777 ++ipa_traceframe_read_count
;
6779 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6780 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6782 trace_debug ("Lib: buffer is fully empty. "
6783 "Trace buffer [%d] start=%d free=%d endfree=%d",
6785 (int) (ipa_trace_buffer_ctrl
.start
6786 - ipa_trace_buffer_lo
),
6787 (int) (ipa_trace_buffer_ctrl
.free
6788 - ipa_trace_buffer_lo
),
6789 (int) (ipa_trace_buffer_ctrl
.end_free
6790 - ipa_trace_buffer_lo
));
6792 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6793 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6794 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6795 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6798 trace_debug ("Uploaded a traceframe\n"
6799 "Lib: Trace buffer [%d] start=%d free=%d "
6800 "endfree=%d wrap=%d hi=%d",
6802 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6803 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6804 (int) (ipa_trace_buffer_ctrl
.end_free
6805 - ipa_trace_buffer_lo
),
6806 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6807 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6810 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6811 (unsigned char *) &ipa_trace_buffer_ctrl
,
6812 sizeof (struct ipa_trace_buffer_control
)))
6815 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6816 ipa_traceframe_read_count
);
6818 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6821 cancel_breakpoints ();
6823 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6824 about_to_request_buffer_space_bkpt
= NULL
;
6828 if (trace_buffer_is_full
)
6833 #ifdef IN_PROCESS_AGENT
6835 IP_AGENT_EXPORT
int ust_loaded
;
6836 IP_AGENT_EXPORT
char cmd_buf
[CMD_BUF_SIZE
];
6840 /* Static tracepoints. */
6842 /* UST puts a "struct tracepoint" in the global namespace, which
6843 conflicts with our tracepoint. Arguably, being a library, it
6844 shouldn't take ownership of such a generic name. We work around it
6846 #define tracepoint ust_tracepoint
6847 #include <ust/ust.h>
6850 extern int serialize_to_text (char *outbuf
, int bufsize
,
6851 const char *fmt
, va_list ap
);
6853 #define GDB_PROBE_NAME "gdb"
6855 /* We dynamically search for the UST symbols instead of linking them
6856 in. This lets the user decide if the application uses static
6857 tracepoints, instead of always pulling libust.so in. This vector
6858 holds pointers to all functions we care about. */
6862 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6863 const char *fmt
, va_list ap
);
6865 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6866 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6868 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6870 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6873 void (*marker_iter_start
) (struct marker_iter
*iter
);
6874 void (*marker_iter_next
) (struct marker_iter
*iter
);
6875 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6876 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6881 /* Cast through typeof to catch incompatible API changes. Since UST
6882 only builds with gcc, we can freely use gcc extensions here
6884 #define GET_UST_SYM(SYM) \
6887 if (ust_ops.SYM == NULL) \
6888 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6889 if (ust_ops.SYM == NULL) \
6893 #define USTF(SYM) ust_ops.SYM
6895 /* Get pointers to all libust.so functions we care about. */
6900 GET_UST_SYM (serialize_to_text
);
6902 GET_UST_SYM (ltt_probe_register
);
6903 GET_UST_SYM (ltt_probe_unregister
);
6904 GET_UST_SYM (ltt_marker_connect
);
6905 GET_UST_SYM (ltt_marker_disconnect
);
6907 GET_UST_SYM (marker_iter_start
);
6908 GET_UST_SYM (marker_iter_next
);
6909 GET_UST_SYM (marker_iter_stop
);
6910 GET_UST_SYM (marker_iter_reset
);
6916 /* Given an UST marker, return the matching gdb static tracepoint.
6917 The match is done by address. */
6919 static struct tracepoint
*
6920 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6922 struct tracepoint
*tpoint
;
6924 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6926 if (tpoint
->type
!= static_tracepoint
)
6929 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6936 /* The probe function we install on lttng/ust markers. Whenever a
6937 probed ust marker is hit, this function is called. This is similar
6938 to gdb_collect, only for static tracepoints, instead of fast
6942 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6943 struct registers
*regs
, void *call_private
,
6944 const char *fmt
, va_list *args
)
6946 struct tracepoint
*tpoint
;
6947 struct static_tracepoint_ctx ctx
;
6949 /* Don't do anything until the trace run is completely set up. */
6952 trace_debug ("gdb_probe: not tracing\n");
6956 ctx
.base
.type
= static_tracepoint
;
6957 ctx
.regcache_initted
= 0;
6962 /* Wrap the regblock in a register cache (in the stack, we don't
6963 want to malloc here). */
6964 ctx
.regspace
= alloca (register_cache_size ());
6965 if (ctx
.regspace
== NULL
)
6967 trace_debug ("Trace buffer block allocation failed, skipping");
6971 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6974 trace_debug ("gdb_probe: marker not known: "
6975 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6976 mdata
->location
, mdata
->channel
,
6977 mdata
->name
, mdata
->format
);
6981 if (!tpoint
->enabled
)
6983 trace_debug ("gdb_probe: tracepoint disabled");
6987 ctx
.tpoint
= tpoint
;
6989 trace_debug ("gdb_probe: collecting marker: "
6990 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6991 mdata
->location
, mdata
->channel
,
6992 mdata
->name
, mdata
->format
);
6994 /* Test the condition if present, and collect if true. */
6995 if (tpoint
->cond
== NULL
6996 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6999 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
7000 tpoint
->address
, tpoint
);
7002 if (stopping_tracepoint
7003 || trace_buffer_is_full
7004 || expr_eval_result
!= expr_eval_no_error
)
7009 /* If there was a condition and it evaluated to false, the only
7010 way we would stop tracing is if there was an error during
7011 condition expression evaluation. */
7012 if (expr_eval_result
!= expr_eval_no_error
)
7017 /* Called if the gdb static tracepoint requested collecting "$_sdata",
7018 static tracepoint string data. This is a string passed to the
7019 tracing library by the user, at the time of the tracepoint marker
7020 call. E.g., in the UST marker call:
7022 trace_mark (ust, bar33, "str %s", "FOOBAZ");
7024 the collected data is "str FOOBAZ".
7028 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
7030 struct tracepoint
*tpoint
,
7031 struct traceframe
*tframe
)
7033 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
7034 unsigned char *bufspace
;
7037 unsigned short blocklen
;
7041 trace_debug ("Wanted to collect static trace data, "
7042 "but there's no static trace data");
7046 va_copy (copy
, *umd
->args
);
7047 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
7050 trace_debug ("Want to collect ust data");
7052 /* 'S' + size + string */
7053 bufspace
= add_traceframe_block (tframe
,
7054 1 + sizeof (blocklen
) + size
+ 1);
7055 if (bufspace
== NULL
)
7057 trace_debug ("Trace buffer block allocation failed, skipping");
7061 /* Identify a static trace data block. */
7064 blocklen
= size
+ 1;
7065 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
7067 va_copy (copy
, *umd
->args
);
7068 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
7069 size
+ 1, umd
->fmt
, copy
);
7072 trace_debug ("Storing static tracepoint data in regblock: %s",
7073 bufspace
+ 1 + sizeof (blocklen
));
7076 /* The probe to register with lttng/ust. */
7077 static struct ltt_available_probe gdb_ust_probe
=
7084 #endif /* HAVE_UST */
7085 #endif /* IN_PROCESS_AGENT */
7089 #include <sys/socket.h>
7092 #ifndef UNIX_PATH_MAX
7093 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
7096 /* Where we put the socked used for synchronization. */
7097 #define SOCK_DIR P_tmpdir
7099 #endif /* HAVE_UST */
7101 #ifndef IN_PROCESS_AGENT
7106 gdb_ust_connect_sync_socket (int pid
)
7108 struct sockaddr_un addr
;
7110 char path
[UNIX_PATH_MAX
];
7112 res
= xsnprintf (path
, UNIX_PATH_MAX
, "%s/gdb_ust%d", SOCK_DIR
, pid
);
7113 if (res
>= UNIX_PATH_MAX
)
7115 trace_debug ("string overflow allocating socket name");
7119 res
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
7122 warning ("error opening sync socket: %s\n", strerror (errno
));
7126 addr
.sun_family
= AF_UNIX
;
7128 res
= xsnprintf (addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
7129 if (res
>= UNIX_PATH_MAX
)
7131 warning ("string overflow allocating socket name\n");
7136 res
= connect (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
7139 warning ("error connecting sync socket (%s): %s. "
7140 "Make sure the directory exists and that it is writable.",
7141 path
, strerror (errno
));
7149 /* Resume thread PTID. */
7152 resume_thread (ptid_t ptid
)
7154 struct thread_resume resume_info
;
7156 resume_info
.thread
= ptid
;
7157 resume_info
.kind
= resume_continue
;
7158 resume_info
.sig
= TARGET_SIGNAL_0
;
7159 (*the_target
->resume
) (&resume_info
, 1);
7162 /* Stop thread PTID. */
7165 stop_thread (ptid_t ptid
)
7167 struct thread_resume resume_info
;
7169 resume_info
.thread
= ptid
;
7170 resume_info
.kind
= resume_stop
;
7171 resume_info
.sig
= TARGET_SIGNAL_0
;
7172 (*the_target
->resume
) (&resume_info
, 1);
7175 /* Ask the in-process agent to run a command. Since we don't want to
7176 have to handle the IPA hitting breakpoints while running the
7177 command, we pause all threads, remove all breakpoints, and then set
7178 the helper thread re-running. We communicate with the helper
7179 thread by means of direct memory xfering, and a socket for
7183 run_inferior_command (char *cmd
)
7187 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
7189 ptid_t ptid
= null_ptid
;
7191 trace_debug ("run_inferior_command: running: %s", cmd
);
7194 uninsert_all_breakpoints ();
7196 if (read_inferior_integer (ipa_sym_addrs
.addr_helper_thread_id
, &tid
))
7198 warning ("Error reading helper thread's id in lib");
7204 warning ("helper thread not initialized yet");
7208 if (write_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
7209 (unsigned char *) cmd
, strlen (cmd
) + 1))
7211 warning ("Error writing command");
7215 ptid
= ptid_build (pid
, tid
, 0);
7217 resume_thread (ptid
);
7219 fd
= gdb_ust_connect_sync_socket (pid
);
7225 trace_debug ("signalling helper thread");
7229 ret
= write (fd
, buf
, 1);
7230 } while (ret
== -1 && errno
== EINTR
);
7232 trace_debug ("waiting for helper thread's response");
7236 ret
= read (fd
, buf
, 1);
7237 } while (ret
== -1 && errno
== EINTR
);
7241 trace_debug ("helper thread's response received");
7246 /* Need to read response with the inferior stopped. */
7247 if (!ptid_equal (ptid
, null_ptid
))
7249 int was_non_stop
= non_stop
;
7250 struct target_waitstatus status
;
7254 mywait (ptid
, &status
, 0, 0);
7255 non_stop
= was_non_stop
;
7260 if (read_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
7261 (unsigned char *) cmd
, CMD_BUF_SIZE
))
7263 warning ("Error reading command response");
7268 trace_debug ("run_inferior_command: response: %s", cmd
);
7272 reinsert_all_breakpoints ();
7278 #else /* HAVE_UST */
7281 run_inferior_command (char *cmd
)
7286 #endif /* HAVE_UST */
7288 #else /* !IN_PROCESS_AGENT */
7290 /* Thread ID of the helper thread. GDBserver reads this to know which
7291 is the help thread. This is an LWP id on Linux. */
7292 int helper_thread_id
;
7297 init_named_socket (const char *name
)
7300 struct sockaddr_un addr
;
7302 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
7305 warning ("socket creation failed: %s", strerror (errno
));
7309 addr
.sun_family
= AF_UNIX
;
7311 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
7312 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
7314 result
= access (name
, F_OK
);
7318 result
= unlink (name
);
7321 warning ("unlink failed: %s", strerror (errno
));
7325 warning ("socket %s already exists; overwriting", name
);
7328 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
7331 warning ("bind failed: %s", strerror (errno
));
7336 result
= listen (fd
, 1);
7339 warning ("listen: %s", strerror (errno
));
7348 gdb_ust_socket_init (void)
7351 char name
[UNIX_PATH_MAX
];
7353 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
7354 SOCK_DIR
, getpid ());
7355 if (result
>= UNIX_PATH_MAX
)
7357 trace_debug ("string overflow allocating socket name");
7361 fd
= init_named_socket (name
);
7363 warning ("Error initializing named socket (%s) for communication with the "
7364 "ust helper thread. Check that directory exists and that it "
7365 "is writable.", name
);
7370 /* Return an hexstr version of the STR C string, fit for sending to
7374 cstr_to_hexstr (const char *str
)
7376 int len
= strlen (str
);
7377 char *hexstr
= xmalloc (len
* 2 + 1);
7378 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
7382 /* The next marker to be returned on a qTsSTM command. */
7383 static const struct marker
*next_st
;
7385 /* Returns the first known marker. */
7390 struct marker_iter iter
;
7392 USTF(marker_iter_reset
) (&iter
);
7393 USTF(marker_iter_start
) (&iter
);
7398 /* Returns the marker following M. */
7400 const struct marker
*
7401 next_marker (const struct marker
*m
)
7403 struct marker_iter iter
;
7405 USTF(marker_iter_reset
) (&iter
);
7406 USTF(marker_iter_start
) (&iter
);
7408 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7410 if (iter
.marker
== m
)
7412 USTF(marker_iter_next
) (&iter
);
7420 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
7424 response_ust_marker (char *packet
, const struct marker
*st
)
7426 char *strid
, *format
, *tmp
;
7428 next_st
= next_marker (st
);
7430 tmp
= xmalloc (strlen (st
->channel
) + 1 +
7431 strlen (st
->name
) + 1);
7432 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
7434 strid
= cstr_to_hexstr (tmp
);
7437 format
= cstr_to_hexstr (st
->format
);
7439 sprintf (packet
, "m%s:%s:%s",
7440 paddress ((uintptr_t) st
->location
),
7448 /* Return the first static tracepoint, and initialize the state
7449 machine that will iterate through all the static tracepoints. */
7452 cmd_qtfstm (char *packet
)
7454 trace_debug ("Returning first trace state variable definition");
7456 if (first_marker ())
7457 response_ust_marker (packet
, first_marker ());
7459 strcpy (packet
, "l");
7462 /* Return additional trace state variable definitions. */
7465 cmd_qtsstm (char *packet
)
7467 trace_debug ("Returning static tracepoint");
7470 response_ust_marker (packet
, next_st
);
7472 strcpy (packet
, "l");
7475 /* Disconnect the GDB probe from a marker at a given address. */
7478 unprobe_marker_at (char *packet
)
7482 struct marker_iter iter
;
7484 p
+= sizeof ("unprobe_marker_at:") - 1;
7486 p
= unpack_varlen_hex (p
, &address
);
7488 USTF(marker_iter_reset
) (&iter
);
7489 USTF(marker_iter_start
) (&iter
);
7490 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7491 if ((uintptr_t ) iter
.marker
->location
== address
)
7495 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7499 warning ("could not disable marker %s/%s",
7500 iter
.marker
->channel
, iter
.marker
->name
);
7505 /* Connect the GDB probe to a marker at a given address. */
7508 probe_marker_at (char *packet
)
7512 struct marker_iter iter
;
7515 p
+= sizeof ("probe_marker_at:") - 1;
7517 p
= unpack_varlen_hex (p
, &address
);
7519 USTF(marker_iter_reset
) (&iter
);
7521 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7523 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7524 if ((uintptr_t ) m
->location
== address
)
7528 trace_debug ("found marker for address. "
7529 "ltt_marker_connect (marker = %s/%s)",
7530 m
->channel
, m
->name
);
7532 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7534 if (result
&& result
!= -EEXIST
)
7535 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7536 m
->channel
, m
->name
, -result
);
7540 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7541 m
->channel
, m
->name
);
7545 strcpy (packet
, "OK");
7549 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7554 cmd_qtstmat (char *packet
)
7558 struct marker_iter iter
;
7561 p
+= sizeof ("qTSTMat:") - 1;
7563 p
= unpack_varlen_hex (p
, &address
);
7565 USTF(marker_iter_reset
) (&iter
);
7567 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7569 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7570 if ((uintptr_t ) m
->location
== address
)
7572 response_ust_marker (packet
, m
);
7576 strcpy (packet
, "l");
7581 gdb_ust_thread (void *arg
)
7587 listen_fd
= gdb_ust_socket_init ();
7590 if (helper_thread_id
== 0)
7591 helper_thread_id
= syscall (SYS_gettid
);
7594 if (listen_fd
== -1)
7596 warning ("could not create sync socket\n");
7603 struct sockaddr_un sockaddr
;
7608 tmp
= sizeof (sockaddr
);
7612 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7614 /* It seems an ERESTARTSYS can escape out of accept. */
7615 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7619 warning ("Accept returned %d, error: %s\n",
7620 fd
, strerror (errno
));
7626 ret
= read (fd
, buf
, 1);
7627 } while (ret
== -1 && errno
== EINTR
);
7631 warning ("reading socket (fd=%d) failed with %s",
7632 fd
, strerror (errno
));
7639 if (strcmp ("qTfSTM", cmd_buf
) == 0)
7641 cmd_qtfstm (cmd_buf
);
7643 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7645 cmd_qtsstm (cmd_buf
);
7647 else if (strncmp ("unprobe_marker_at:",
7649 sizeof ("unprobe_marker_at:") - 1) == 0)
7651 unprobe_marker_at (cmd_buf
);
7653 else if (strncmp ("probe_marker_at:",
7655 sizeof ("probe_marker_at:") - 1) == 0)
7657 probe_marker_at (cmd_buf
);
7659 else if (strncmp ("qTSTMat:",
7661 sizeof ("qTSTMat:") - 1) == 0)
7663 cmd_qtstmat (cmd_buf
);
7665 else if (strcmp (cmd_buf
, "help") == 0)
7667 strcpy (cmd_buf
, "for help, press F1\n");
7670 strcpy (cmd_buf
, "");
7694 /* We want the helper thread to be as transparent as possible, so
7695 have it inherit an all-signals-blocked mask. */
7697 sigfillset (&new_mask
);
7698 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7700 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7702 res
= pthread_create (&thread
,
7707 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7709 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7711 while (helper_thread_id
== 0)
7714 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7717 #endif /* HAVE_UST */
7719 #include <sys/mman.h>
7722 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7723 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7724 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7726 static void __attribute__ ((constructor
))
7727 initialize_tracepoint_ftlib (void)
7729 initialize_tracepoint ();
7736 #endif /* IN_PROCESS_AGENT */
7739 tsv_get_timestamp (void)
7743 if (gettimeofday (&tv
, 0) != 0)
7746 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7750 initialize_tracepoint (void)
7752 /* There currently no way to change the buffer size. */
7753 const int sizeOfBuffer
= 5 * 1024 * 1024;
7754 unsigned char *buf
= xmalloc (sizeOfBuffer
);
7755 init_trace_buffer (buf
, sizeOfBuffer
);
7757 /* Wire trace state variable 1 to be the timestamp. This will be
7758 uploaded to GDB upon connection and become one of its trace state
7759 variables. (In case you're wondering, if GDB already has a trace
7760 variable numbered 1, it will be renumbered.) */
7761 create_trace_state_variable (1, 0);
7762 set_trace_state_variable_name (1, "trace_timestamp");
7763 set_trace_state_variable_getter (1, tsv_get_timestamp
);
7765 #ifdef IN_PROCESS_AGENT
7768 pagesize
= sysconf (_SC_PAGE_SIZE
);
7772 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7774 /* Allocate scratch buffer aligned on a page boundary. */
7775 gdb_jump_pad_buffer
= memalign (pagesize
, pagesize
* 20);
7776 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* 20;
7778 /* Make it writable and executable. */
7779 if (mprotect (gdb_jump_pad_buffer
, pagesize
* 20,
7780 PROT_READ
| PROT_WRITE
| PROT_EXEC
) != 0)
7782 initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
7783 gdb_jump_pad_buffer
, pagesize
* 20, strerror (errno
));
7786 initialize_low_tracepoint ();