1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 /* This file is built for both both GDBserver, and the in-process
33 agent (IPA), a shared library that includes a tracing agent that is
34 loaded by the inferior to support fast tracepoints. Fast
35 tracepoints (or more accurately, jump based tracepoints) are
36 implemented by patching the tracepoint location with a jump into a
37 small trampoline function whose job is to save the register state,
38 call the in-process tracing agent, and then execute the original
39 instruction that was under the tracepoint jump (possibly adjusted,
40 if PC-relative, or some such).
42 The current synchronization design is pull based. That means,
43 GDBserver does most of the work, by peeking/poking at the inferior
44 agent's memory directly for downloading tracepoint and associated
45 objects, and for uploading trace frames. Whenever the IPA needs
46 something from GDBserver (trace buffer is full, tracing stopped for
47 some reason, etc.) the IPA calls a corresponding hook function
48 where GDBserver has placed a breakpoint.
50 Each of the agents has its own trace buffer. When browsing the
51 trace frames built from slow and fast tracepoints from GDB (tfind
52 mode), there's no guarantee the user is seeing the trace frames in
53 strict chronological creation order, although, GDBserver tries to
54 keep the order relatively reasonable, by syncing the trace buffers
59 static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf
, 1, 2);
62 trace_vdebug (const char *fmt
, ...)
68 vsprintf (buf
, fmt
, ap
);
69 fprintf (stderr
, "gdbserver/tracepoint: %s\n", buf
);
73 #define trace_debug_1(level, fmt, args...) \
75 if (level <= debug_threads) \
76 trace_vdebug ((fmt), ##args); \
79 #define trace_debug(FMT, args...) \
80 trace_debug_1 (1, FMT, ##args)
83 # define ATTR_USED __attribute__((used))
84 # define ATTR_NOINLINE __attribute__((noinline))
85 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
88 # define ATTR_NOINLINE
89 # define ATTR_CONSTRUCTOR
92 /* Make sure the functions the IPA needs to export (symbols GDBserver
93 needs to query GDB about) are exported. */
95 #ifdef IN_PROCESS_AGENT
96 # if defined _WIN32 || defined __CYGWIN__
97 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
100 # define IP_AGENT_EXPORT \
101 __attribute__ ((visibility("default"))) ATTR_USED
103 # define IP_AGENT_EXPORT ATTR_USED
107 # define IP_AGENT_EXPORT
110 /* Prefix exported symbols, for good citizenship. All the symbols
111 that need exporting are defined in this module. */
112 #ifdef IN_PROCESS_AGENT
113 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
114 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
115 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
116 # define collecting gdb_agent_collecting
117 # define gdb_collect gdb_agent_gdb_collect
118 # define stop_tracing gdb_agent_stop_tracing
119 # define flush_trace_buffer gdb_agent_flush_trace_buffer
120 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
121 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
122 # define stopping_tracepoint gdb_agent_stopping_tracepoint
123 # define expr_eval_result gdb_agent_expr_eval_result
124 # define error_tracepoint gdb_agent_error_tracepoint
125 # define tracepoints gdb_agent_tracepoints
126 # define tracing gdb_agent_tracing
127 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
128 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
129 # define trace_buffer_lo gdb_agent_trace_buffer_lo
130 # define trace_buffer_hi gdb_agent_trace_buffer_hi
131 # define traceframe_read_count gdb_agent_traceframe_read_count
132 # define traceframe_write_count gdb_agent_traceframe_write_count
133 # define traceframes_created gdb_agent_traceframes_created
134 # define trace_state_variables gdb_agent_trace_state_variables
135 # define get_raw_reg gdb_agent_get_raw_reg
136 # define get_trace_state_variable_value gdb_agent_get_trace_state_variable_value
137 # define set_trace_state_variable_value gdb_agent_set_trace_state_variable_value
140 #ifndef IN_PROCESS_AGENT
142 /* Addresses of in-process agent's symbols GDBserver cares about. */
144 struct ipa_sym_addresses
146 CORE_ADDR addr_gdb_tp_heap_buffer
;
147 CORE_ADDR addr_gdb_jump_pad_buffer
;
148 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
149 CORE_ADDR addr_collecting
;
150 CORE_ADDR addr_gdb_collect
;
151 CORE_ADDR addr_stop_tracing
;
152 CORE_ADDR addr_flush_trace_buffer
;
153 CORE_ADDR addr_about_to_request_buffer_space
;
154 CORE_ADDR addr_trace_buffer_is_full
;
155 CORE_ADDR addr_stopping_tracepoint
;
156 CORE_ADDR addr_expr_eval_result
;
157 CORE_ADDR addr_error_tracepoint
;
158 CORE_ADDR addr_tracepoints
;
159 CORE_ADDR addr_tracing
;
160 CORE_ADDR addr_trace_buffer_ctrl
;
161 CORE_ADDR addr_trace_buffer_ctrl_curr
;
162 CORE_ADDR addr_trace_buffer_lo
;
163 CORE_ADDR addr_trace_buffer_hi
;
164 CORE_ADDR addr_traceframe_read_count
;
165 CORE_ADDR addr_traceframe_write_count
;
166 CORE_ADDR addr_traceframes_created
;
167 CORE_ADDR addr_trace_state_variables
;
168 CORE_ADDR addr_get_raw_reg
;
169 CORE_ADDR addr_get_trace_state_variable_value
;
170 CORE_ADDR addr_set_trace_state_variable_value
;
173 #define STRINGIZE_1(STR) #STR
174 #define STRINGIZE(STR) STRINGIZE_1(STR)
175 #define IPA_SYM(SYM) \
177 STRINGIZE (gdb_agent_ ## SYM), \
178 offsetof (struct ipa_sym_addresses, addr_ ## SYM) \
187 IPA_SYM(gdb_tp_heap_buffer
),
188 IPA_SYM(gdb_jump_pad_buffer
),
189 IPA_SYM(gdb_jump_pad_buffer_end
),
191 IPA_SYM(gdb_collect
),
192 IPA_SYM(stop_tracing
),
193 IPA_SYM(flush_trace_buffer
),
194 IPA_SYM(about_to_request_buffer_space
),
195 IPA_SYM(trace_buffer_is_full
),
196 IPA_SYM(stopping_tracepoint
),
197 IPA_SYM(expr_eval_result
),
198 IPA_SYM(error_tracepoint
),
199 IPA_SYM(tracepoints
),
201 IPA_SYM(trace_buffer_ctrl
),
202 IPA_SYM(trace_buffer_ctrl_curr
),
203 IPA_SYM(trace_buffer_lo
),
204 IPA_SYM(trace_buffer_hi
),
205 IPA_SYM(traceframe_read_count
),
206 IPA_SYM(traceframe_write_count
),
207 IPA_SYM(traceframes_created
),
208 IPA_SYM(trace_state_variables
),
209 IPA_SYM(get_raw_reg
),
210 IPA_SYM(get_trace_state_variable_value
),
211 IPA_SYM(set_trace_state_variable_value
),
214 struct ipa_sym_addresses ipa_sym_addrs
;
216 int all_tracepoint_symbols_looked_up
;
219 in_process_agent_loaded (void)
221 return all_tracepoint_symbols_looked_up
;
224 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
227 write_e_ipa_not_loaded (char *buffer
)
230 "E.In-process agent library not loaded in process. "
231 "Dynamic tracepoints unavailable.");
235 maybe_write_ipa_not_loaded (char *buffer
)
237 if (!in_process_agent_loaded ())
239 write_e_ipa_not_loaded (buffer
);
245 /* Cache all future symbols that the tracepoints module might request.
246 We can not request symbols at arbitrary states in the remote
247 protocol, only when the client tells us that new symbols are
248 available. So when we load the in-process library, make sure to
249 check the entire list. */
252 tracepoint_look_up_symbols (void)
257 if (all_tracepoint_symbols_looked_up
)
261 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
264 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
266 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
269 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
274 all_tracepoint_symbols_looked_up
= all_ok
;
279 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
280 of the "stop_tracing" function. When this breakpoint is hit,
281 tracing stopped in the IPA for some reason. E.g., due to
282 tracepoint reaching the pass count, hitting conditional expression
283 evaluation error, etc.
285 The IPA's trace buffer is never in circular tracing mode: instead,
286 GDBserver's is, and whenever the in-process buffer fills, it calls
287 "flush_trace_buffer", which triggers an internal breakpoint.
288 GDBserver reacts to this breakpoint by pulling the meanwhile
289 collected data. Old frames discarding is always handled on the
292 #ifdef IN_PROCESS_AGENT
293 int debug_threads
= 0;
296 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
298 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
302 /* Call this in the functions where GDBserver places a breakpoint, so
303 that the compiler doesn't try to be clever and skip calling the
304 function at all. This is necessary, even if we tell the compiler
305 to not inline said functions. */
307 #if defined(__GNUC__)
308 # define UNKNOWN_SIDE_EFFECTS() asm ("")
310 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
313 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
316 /* GDBserver places breakpoint here. */
317 UNKNOWN_SIDE_EFFECTS();
320 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
321 flush_trace_buffer (void)
323 /* GDBserver places breakpoint here. */
324 UNKNOWN_SIDE_EFFECTS();
329 #ifndef IN_PROCESS_AGENT
331 tracepoint_handler (CORE_ADDR address
)
333 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
338 /* Breakpoint at "stop_tracing" in the inferior lib. */
339 struct breakpoint
*stop_tracing_bkpt
;
340 static int stop_tracing_handler (CORE_ADDR
);
342 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
343 struct breakpoint
*flush_trace_buffer_bkpt
;
344 static int flush_trace_buffer_handler (CORE_ADDR
);
346 static void download_tracepoints (void);
347 static void download_trace_state_variables (void);
348 static void upload_fast_traceframes (void);
351 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
353 return read_inferior_memory (symaddr
, (unsigned char *) val
,
358 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
360 return read_inferior_memory (symaddr
, (unsigned char *) val
,
365 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
367 void *pval
= (void *) (uintptr_t) val
;
370 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
371 *val
= (uintptr_t) pval
;
376 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
378 void *pval
= (void *) (uintptr_t) val
;
379 return write_inferior_memory (symaddr
,
380 (unsigned char *) &pval
, sizeof (pval
));
384 write_inferior_integer (CORE_ADDR symaddr
, int val
)
386 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
390 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
392 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
397 /* This enum must exactly match what is documented in
398 gdb/doc/agentexpr.texi, including all the numerical values. */
402 gdb_agent_op_float
= 0x01,
403 gdb_agent_op_add
= 0x02,
404 gdb_agent_op_sub
= 0x03,
405 gdb_agent_op_mul
= 0x04,
406 gdb_agent_op_div_signed
= 0x05,
407 gdb_agent_op_div_unsigned
= 0x06,
408 gdb_agent_op_rem_signed
= 0x07,
409 gdb_agent_op_rem_unsigned
= 0x08,
410 gdb_agent_op_lsh
= 0x09,
411 gdb_agent_op_rsh_signed
= 0x0a,
412 gdb_agent_op_rsh_unsigned
= 0x0b,
413 gdb_agent_op_trace
= 0x0c,
414 gdb_agent_op_trace_quick
= 0x0d,
415 gdb_agent_op_log_not
= 0x0e,
416 gdb_agent_op_bit_and
= 0x0f,
417 gdb_agent_op_bit_or
= 0x10,
418 gdb_agent_op_bit_xor
= 0x11,
419 gdb_agent_op_bit_not
= 0x12,
420 gdb_agent_op_equal
= 0x13,
421 gdb_agent_op_less_signed
= 0x14,
422 gdb_agent_op_less_unsigned
= 0x15,
423 gdb_agent_op_ext
= 0x16,
424 gdb_agent_op_ref8
= 0x17,
425 gdb_agent_op_ref16
= 0x18,
426 gdb_agent_op_ref32
= 0x19,
427 gdb_agent_op_ref64
= 0x1a,
428 gdb_agent_op_ref_float
= 0x1b,
429 gdb_agent_op_ref_double
= 0x1c,
430 gdb_agent_op_ref_long_double
= 0x1d,
431 gdb_agent_op_l_to_d
= 0x1e,
432 gdb_agent_op_d_to_l
= 0x1f,
433 gdb_agent_op_if_goto
= 0x20,
434 gdb_agent_op_goto
= 0x21,
435 gdb_agent_op_const8
= 0x22,
436 gdb_agent_op_const16
= 0x23,
437 gdb_agent_op_const32
= 0x24,
438 gdb_agent_op_const64
= 0x25,
439 gdb_agent_op_reg
= 0x26,
440 gdb_agent_op_end
= 0x27,
441 gdb_agent_op_dup
= 0x28,
442 gdb_agent_op_pop
= 0x29,
443 gdb_agent_op_zero_ext
= 0x2a,
444 gdb_agent_op_swap
= 0x2b,
445 gdb_agent_op_getv
= 0x2c,
446 gdb_agent_op_setv
= 0x2d,
447 gdb_agent_op_tracev
= 0x2e,
448 gdb_agent_op_trace16
= 0x30,
452 static const char *gdb_agent_op_names
[gdb_agent_op_last
] =
509 unsigned char *bytes
;
512 /* Base action. Concrete actions inherit this. */
514 struct tracepoint_action
519 /* An 'M' (collect memory) action. */
520 struct collect_memory_action
522 struct tracepoint_action base
;
529 /* An 'R' (collect registers) action. */
531 struct collect_registers_action
533 struct tracepoint_action base
;
536 /* An 'X' (evaluate expression) action. */
538 struct eval_expr_action
540 struct tracepoint_action base
;
542 struct agent_expr
*expr
;
545 /* This structure describes a piece of the source-level definition of
546 the tracepoint. The contents are not interpreted by the target,
547 but preserved verbatim for uploading upon reconnection. */
551 /* The type of string, such as "cond" for a conditional. */
554 /* The source-level string itself. For the sake of target
555 debugging, we store it in plaintext, even though it is always
556 transmitted in hex. */
559 /* Link to the next one in the list. We link them in the order
560 received, in case some make up an ordered list of commands or
562 struct source_string
*next
;
567 /* Trap based tracepoint. */
570 /* A fast tracepoint implemented with a jump instead of a trap. */
574 struct tracepoint_hit_ctx
;
576 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
579 /* The definition of a tracepoint. */
581 /* Tracepoints may have multiple locations, each at a different
582 address. This can occur with optimizations, template
583 instantiation, etc. Since the locations may be in different
584 scopes, the conditions and actions may be different for each
585 location. Our target version of tracepoints is more like GDB's
586 notion of "breakpoint locations", but we have almost nothing that
587 is not per-location, so we bother having two kinds of objects. The
588 key consequence is that numbers are not unique, and that it takes
589 both number and address to identify a tracepoint uniquely. */
593 /* The number of the tracepoint, as specified by GDB. Several
594 tracepoint objects here may share a number. */
597 /* Address at which the tracepoint is supposed to trigger. Several
598 tracepoints may share an address. */
601 /* Tracepoint type. */
602 enum tracepoint_type type
;
604 /* True if the tracepoint is currently enabled. */
607 /* The number of single steps that will be performed after each
611 /* The number of times the tracepoint may be hit before it will
612 terminate the entire tracing run. */
615 /* Pointer to the agent expression that is the tracepoint's
616 conditional, or NULL if the tracepoint is unconditional. */
617 struct agent_expr
*cond
;
619 /* The list of actions to take when the tracepoint triggers. */
621 struct tracepoint_action
**actions
;
623 /* Count of the times we've hit this tracepoint during the run.
624 Note that while-stepping steps are not counted as "hits". */
627 CORE_ADDR compiled_cond
;
629 /* Link to the next tracepoint in the list. */
630 struct tracepoint
*next
;
632 #ifndef IN_PROCESS_AGENT
633 /* The list of actions to take when the tracepoint triggers, in
634 string/packet form. */
637 /* The collection of strings that describe the tracepoint as it was
638 entered into GDB. These are not used by the target, but are
639 reported back to GDB upon reconnection. */
640 struct source_string
*source_strings
;
642 /* The number of bytes displaced by fast tracepoints. It may subsume
643 multiple instructions, for multi-byte fast tracepoints. This
644 field is only valid for fast tracepoints. */
647 /* Only for fast tracepoints. */
648 CORE_ADDR obj_addr_on_target
;
650 /* Address range where the original instruction under a fast
651 tracepoint was relocated to. (_end is actually one byte past
653 CORE_ADDR adjusted_insn_addr
;
654 CORE_ADDR adjusted_insn_addr_end
;
656 /* The address range of the piece of the jump pad buffer that was
657 assigned to this fast tracepoint. (_end is actually one byte
660 CORE_ADDR jump_pad_end
;
662 /* The list of actions to take while in a stepping loop. These
663 fields are only valid for patch-based tracepoints. */
664 int num_step_actions
;
665 struct tracepoint_action
**step_actions
;
666 /* Same, but in string/packet form. */
667 char **step_actions_str
;
669 /* Handle returned by the breakpoint or tracepoint module when we
670 inserted the trap or jump. NULL if we haven't inserted it
677 #ifndef IN_PROCESS_AGENT
679 /* Given `while-stepping', a thread may be collecting data for more
680 than one tracepoint simultaneously. On the other hand, the same
681 tracepoint with a while-stepping action may be hit by more than one
682 thread simultaneously (but not quite, each thread could be handling
683 a different step). Each thread holds a list of these objects,
684 representing the current step of each while-stepping action being
689 struct wstep_state
*next
;
691 /* The tracepoint number. */
693 /* The tracepoint's address. */
694 CORE_ADDR tp_address
;
696 /* The number of the current step in this 'while-stepping'
703 /* The linked list of all tracepoints. Marked explicitly as used as
704 the in-process library doesn't use it for the fast tracepoints
706 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
708 #ifndef IN_PROCESS_AGENT
710 /* Pointer to the last tracepoint in the list, new tracepoints are
711 linked in at the end. */
713 static struct tracepoint
*last_tracepoint
;
716 /* The first tracepoint to exceed its pass count. */
718 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
720 /* True if the trace buffer is full or otherwise no longer usable. */
722 IP_AGENT_EXPORT
int trace_buffer_is_full
;
724 /* Enumeration of the different kinds of things that can happen during
725 agent expression evaluation. */
727 enum eval_result_type
730 expr_eval_empty_expression
,
731 expr_eval_empty_stack
,
732 expr_eval_stack_overflow
,
733 expr_eval_stack_underflow
,
734 expr_eval_unhandled_opcode
,
735 expr_eval_unrecognized_opcode
,
736 expr_eval_divide_by_zero
,
737 expr_eval_invalid_goto
740 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
742 #ifndef IN_PROCESS_AGENT
744 static const char *eval_result_names
[] =
746 "terror:in the attic", /* this should never be reported */
747 "terror:empty expression",
748 "terror:empty stack",
749 "terror:stack overflow",
750 "terror:stack underflow",
751 "terror:unhandled opcode",
752 "terror:unrecognized opcode",
753 "terror:divide by zero"
758 /* The tracepoint in which the error occurred. */
760 static struct tracepoint
*error_tracepoint
;
762 struct trace_state_variable
764 /* This is the name of the variable as used in GDB. The target
765 doesn't use the name, but needs to have it for saving and
766 reconnection purposes. */
769 /* This number identifies the variable uniquely. Numbers may be
770 assigned either by the target (in the case of builtin variables),
771 or by GDB, and are presumed unique during the course of a trace
775 /* The variable's initial value, a 64-bit signed integer always. */
776 LONGEST initial_value
;
778 /* The variable's value, a 64-bit signed integer always. */
781 /* Pointer to a getter function, used to supply computed values. */
782 LONGEST (*getter
) (void);
784 /* Link to the next variable. */
785 struct trace_state_variable
*next
;
788 /* Linked list of all trace state variables. */
790 #ifdef IN_PROCESS_AGENT
791 struct trace_state_variable
*alloced_trace_state_variables
;
794 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
796 /* The results of tracing go into a fixed-size space known as the
797 "trace buffer". Because usage follows a limited number of
798 patterns, we manage it ourselves rather than with malloc. Basic
799 rules are that we create only one trace frame at a time, each is
800 variable in size, they are never moved once created, and we only
801 discard if we are doing a circular buffer, and then only the oldest
802 ones. Each trace frame includes its own size, so we don't need to
803 link them together, and the trace frame number is relative to the
804 first one, so we don't need to record numbers. A trace frame also
805 records the number of the tracepoint that created it. The data
806 itself is a series of blocks, each introduced by a single character
807 and with a defined format. Each type of block has enough
808 type/length info to allow scanners to jump quickly from one block
809 to the next without reading each byte in the block. */
811 /* Trace buffer management would be simple - advance a free pointer
812 from beginning to end, then stop - were it not for the circular
813 buffer option, which is a useful way to prevent a trace run from
814 stopping prematurely because the buffer filled up. In the circular
815 case, the location of the first trace frame (trace_buffer_start)
816 moves as old trace frames are discarded. Also, since we grow trace
817 frames incrementally as actions are performed, we wrap around to
818 the beginning of the trace buffer. This is per-block, so each
819 block within a trace frame remains contiguous. Things get messy
820 when the wrapped-around trace frame is the one being discarded; the
821 free space ends up in two parts at opposite ends of the buffer. */
824 # if defined(__GNUC__)
825 # define ATTR_PACKED __attribute__ ((packed))
827 # define ATTR_PACKED /* nothing */
831 /* The data collected at a tracepoint hit. This object should be as
832 small as possible, since there may be a great many of them. We do
833 not need to keep a frame number, because they are all sequential
834 and there are no deletions; so the Nth frame in the buffer is
835 always frame number N. */
839 /* Number of the tracepoint that collected this traceframe. A value
840 of 0 indicates the current end of the trace buffer. We make this
841 a 16-bit field because it's never going to happen that GDB's
842 numbering of tracepoints reaches 32,000. */
845 /* The size of the data in this trace frame. We limit this to 32
846 bits, even on a 64-bit target, because it's just implausible that
847 one is validly going to collect 4 gigabytes of data at a single
849 unsigned int data_size
: 32;
851 /* The base of the trace data, which is contiguous from this point. */
852 unsigned char data
[0];
856 /* The traceframe to be used as the source of data to send back to
857 GDB. A value of -1 means to get data from the live program. */
859 int current_traceframe
= -1;
861 /* This flag is true if the trace buffer is circular, meaning that
862 when it fills, the oldest trace frames are discarded in order to
865 #ifndef IN_PROCESS_AGENT
866 static int circular_trace_buffer
;
869 /* Pointer to the block of memory that traceframes all go into. */
871 static unsigned char *trace_buffer_lo
;
873 /* Pointer to the end of the trace buffer, more precisely to the byte
874 after the end of the buffer. */
876 static unsigned char *trace_buffer_hi
;
878 /* Control structure holding the read/write/etc. pointers into the
879 trace buffer. We need more than one of these to implement a
880 transaction-like mechanism to garantees that both GDBserver and the
881 in-process agent can try to change the trace buffer
884 struct trace_buffer_control
886 /* Pointer to the first trace frame in the buffer. In the
887 non-circular case, this is equal to trace_buffer_lo, otherwise it
888 moves around in the buffer. */
889 unsigned char *start
;
891 /* Pointer to the free part of the trace buffer. Note that we clear
892 several bytes at and after this pointer, so that traceframe
893 scans/searches terminate properly. */
896 /* Pointer to the byte after the end of the free part. Note that
897 this may be smaller than trace_buffer_free in the circular case,
898 and means that the free part is in two pieces. Initially it is
899 equal to trace_buffer_hi, then is generally equivalent to
900 trace_buffer_start. */
901 unsigned char *end_free
;
903 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
904 this is the point at which the trace data breaks, and resumes at
909 /* Same as above, to be used by GDBserver when updating the in-process
911 struct ipa_trace_buffer_control
920 /* We have possibly both GDBserver and an inferior thread accessing
921 the same IPA trace buffer memory. The IPA is the producer (tries
922 to put new frames in the buffer), while GDBserver occasionally
923 consumes them, that is, flushes the IPA's buffer into its own
924 buffer. Both sides need to update the trace buffer control
925 pointers (current head, tail, etc.). We can't use a global lock to
926 synchronize the accesses, as otherwise we could deadlock GDBserver
927 (if the thread holding the lock stops for a signal, say). So
928 instead of that, we use a transaction scheme where GDBserver writes
929 always prevail over the IPAs writes, and, we have the IPA detect
930 the commit failure/overwrite, and retry the whole attempt. This is
931 mainly implemented by having a global token object that represents
932 who wrote last to the buffer control structure. We need to freeze
933 any inferior writing to the buffer while GDBserver touches memory,
934 so that the inferior can correctly detect that GDBserver had been
935 there, otherwise, it could mistakingly think its commit was
936 successful; that's implemented by simply having GDBserver set a
937 breakpoint the inferior hits if it is the critical region.
939 There are three cycling trace buffer control structure copies
940 (buffer head, tail, etc.), with the token object including an index
941 indicating which is current live copy. The IPA tentatively builds
942 an updated copy in a non-current control structure, while GDBserver
943 always clobbers the current version directly. The IPA then tries
944 to atomically "commit" its version; if GDBserver clobbered the
945 structure meanwhile, that will fail, and the IPA restarts the
948 Listing the step in further detail, we have:
950 In-process agent (producer):
952 - passes by `about_to_request_buffer_space' breakpoint/lock
954 - reads current token, extracts current trace buffer control index,
955 and starts tentatively updating the rightmost one (0->1, 1->2,
956 2->0). Note that only one inferior thread is executing this code
957 at any given time, due to an outer lock in the jump pads.
959 - updates counters, and tries to commit the token.
961 - passes by second `about_to_request_buffer_space' breakpoint/lock,
962 leaving the sync region.
964 - checks if the update was effective.
966 - if trace buffer was found full, hits flush_trace_buffer
967 breakpoint, and restarts later afterwards.
969 GDBserver (consumer):
971 - sets `about_to_request_buffer_space' breakpoint/lock.
973 - updates the token unconditionally, using the current buffer
974 control index, since it knows that the IP agent always writes to
975 the rightmost, and due to the breakpoint, at most one IP thread
976 can try to update the trace buffer concurrently to GDBserver, so
977 there will be no danger of trace buffer control index wrap making
978 the IPA write to the same index as GDBserver.
980 - flushes the IP agent's trace buffer completely, and updates the
981 current trace buffer control structure. GDBserver *always* wins.
983 - removes the `about_to_request_buffer_space' breakpoint.
985 The token is stored in the `trace_buffer_ctrl_curr' variable.
986 Internally, it's bits are defined as:
988 |-------------+-----+-------------+--------+-------------+--------------|
989 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
990 |-------------+-----+-------------+--------+-------------+--------------|
991 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
992 |-------------+-----+-------------+--------+-------------+--------------|
994 GSB - GDBserver Stamp Bit
995 PC - Previous Counter
997 TBCI - Trace Buffer Control Index
1000 An IPA update of `trace_buffer_ctrl_curr' does:
1002 - read CC from the current token, save as PC.
1004 - atomically tries to write PC+1,CC
1006 A GDBserver update of `trace_buffer_ctrl_curr' does:
1008 - reads PC and CC from the current token.
1013 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1014 for the counters described below. The cleared bits are used to
1015 hold the index of the items of the `trace_buffer_ctrl' array that
1017 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1019 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1020 counter, and the `current' counter. */
1022 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1023 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1025 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1026 always stamps this bit as set. */
1027 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1029 #ifdef IN_PROCESS_AGENT
1030 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1031 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1033 # define TRACE_BUFFER_CTRL_CURR \
1034 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1038 /* The GDBserver side agent only needs one instance of this object, as
1039 it doesn't need to sync with itself. Define it as array anyway so
1040 that the rest of the code base doesn't need to care for the
1042 struct trace_buffer_control trace_buffer_ctrl
[1];
1043 # define TRACE_BUFFER_CTRL_CURR 0
1046 /* These are convenience macros used to access the current trace
1047 buffer control in effect. */
1048 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1049 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1050 #define trace_buffer_end_free \
1051 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1052 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1055 /* Macro that returns a pointer to the first traceframe in the buffer. */
1057 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1059 /* Macro that returns a pointer to the next traceframe in the buffer.
1060 If the computed location is beyond the wraparound point, subtract
1061 the offset of the wraparound. */
1063 #define NEXT_TRACEFRAME_1(TF) \
1064 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1066 #define NEXT_TRACEFRAME(TF) \
1067 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1068 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1069 ? (trace_buffer_wrap - trace_buffer_lo) \
1072 /* The difference between these counters represents the total number
1073 of complete traceframes present in the trace buffer. The IP agent
1074 writes to the write count, GDBserver writes to read count. */
1076 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1077 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1079 /* Convenience macro. */
1081 #define traceframe_count \
1082 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1084 /* The count of all traceframes created in the current run, including
1085 ones that were discarded to make room. */
1087 IP_AGENT_EXPORT
int traceframes_created
;
1089 #ifndef IN_PROCESS_AGENT
1091 /* Read-only regions are address ranges whose contents don't change,
1092 and so can be read from target memory even while looking at a trace
1093 frame. Without these, disassembly for instance will likely fail,
1094 because the program code is not usually collected into a trace
1095 frame. This data structure does not need to be very complicated or
1096 particularly efficient, it's only going to be used occasionally,
1097 and only by some commands. */
1099 struct readonly_region
1101 /* The bounds of the region. */
1102 CORE_ADDR start
, end
;
1104 /* Link to the next one. */
1105 struct readonly_region
*next
;
1108 /* Linked list of readonly regions. This list stays in effect from
1109 one tstart to the next. */
1111 static struct readonly_region
*readonly_regions
;
1115 /* The global that controls tracing overall. */
1117 IP_AGENT_EXPORT
int tracing
;
1119 #ifndef IN_PROCESS_AGENT
1121 /* Controls whether tracing should continue after GDB disconnects. */
1123 int disconnected_tracing
;
1125 /* The reason for the last tracing run to have stopped. We initialize
1126 to a distinct string so that GDB can distinguish between "stopped
1127 after running" and "stopped because never run" cases. */
1129 static const char *tracing_stop_reason
= "tnotrun";
1131 static int tracing_stop_tpnum
;
1135 /* Functions local to this file. */
1137 /* Base "class" for tracepoint type specific data to be passed down to
1138 collect_data_at_tracepoint. */
1139 struct tracepoint_hit_ctx
1141 enum tracepoint_type type
;
1144 #ifdef IN_PROCESS_AGENT
1146 /* Fast/jump tracepoint specific data to be passed down to
1147 collect_data_at_tracepoint. */
1148 struct fast_tracepoint_ctx
1150 struct tracepoint_hit_ctx base
;
1152 struct regcache regcache
;
1153 int regcache_initted
;
1154 unsigned char *regspace
;
1156 unsigned char *regs
;
1157 struct tracepoint
*tpoint
;
1162 /* Static tracepoint specific data to be passed down to
1163 collect_data_at_tracepoint. */
1164 struct trap_tracepoint_ctx
1166 struct tracepoint_hit_ctx base
;
1168 struct regcache
*regcache
;
1173 #ifndef IN_PROCESS_AGENT
1174 static struct agent_expr
*parse_agent_expr (char **actparm
);
1175 static char *unparse_agent_expr (struct agent_expr
*aexpr
);
1177 static enum eval_result_type
eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1178 struct traceframe
*tframe
,
1179 struct agent_expr
*aexpr
,
1182 static int agent_mem_read (struct traceframe
*tframe
,
1183 unsigned char *to
, CORE_ADDR from
, ULONGEST len
);
1184 static int agent_tsv_read (struct traceframe
*tframe
, int n
);
1186 #ifndef IN_PROCESS_AGENT
1187 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1188 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1191 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1192 struct tracepoint
*tpoint
);
1194 #ifndef IN_PROCESS_AGENT
1195 static void clear_readonly_regions (void);
1196 static void clear_installed_tracepoints (void);
1199 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1201 struct tracepoint
*tpoint
);
1202 #ifndef IN_PROCESS_AGENT
1203 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1205 struct tracepoint
*tpoint
, int current_step
);
1206 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1207 CORE_ADDR
*jump_entry
);
1209 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1211 struct tracepoint
*tpoint
,
1212 struct traceframe
*tframe
,
1213 struct tracepoint_action
*taction
);
1215 #ifndef IN_PROCESS_AGENT
1216 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1219 #if defined(__GNUC__)
1220 # define memory_barrier() asm volatile ("" : : : "memory")
1222 # define memory_barrier() do {} while (0)
1225 /* We only build the IPA if this builtin is supported, and there are
1226 no uses of this in GDBserver itself, so we're safe in defining this
1228 #define cmpxchg(mem, oldval, newval) \
1229 __sync_val_compare_and_swap (mem, oldval, newval)
1231 /* Record that an error occurred during expression evaluation. */
1234 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1235 enum eval_result_type rtype
)
1237 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1238 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1240 #ifdef IN_PROCESS_AGENT
1241 /* Only record the first error we get. */
1242 if (cmpxchg (&expr_eval_result
,
1244 rtype
) != expr_eval_no_error
)
1247 if (expr_eval_result
!= expr_eval_no_error
)
1251 error_tracepoint
= tpoint
;
1254 /* Trace buffer management. */
1257 clear_trace_buffer (void)
1259 trace_buffer_start
= trace_buffer_lo
;
1260 trace_buffer_free
= trace_buffer_lo
;
1261 trace_buffer_end_free
= trace_buffer_hi
;
1262 trace_buffer_wrap
= trace_buffer_hi
;
1263 /* A traceframe with zeroed fields marks the end of trace data. */
1264 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1265 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1266 traceframe_read_count
= traceframe_write_count
= 0;
1267 traceframes_created
= 0;
1270 #ifndef IN_PROCESS_AGENT
1273 clear_inferior_trace_buffer (void)
1275 CORE_ADDR ipa_trace_buffer_lo
;
1276 CORE_ADDR ipa_trace_buffer_hi
;
1277 struct traceframe ipa_traceframe
= { 0 };
1278 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1280 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1281 &ipa_trace_buffer_lo
);
1282 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1283 &ipa_trace_buffer_hi
);
1285 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1286 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1287 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1288 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1290 /* A traceframe with zeroed fields marks the end of trace data. */
1291 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1292 (unsigned char *) &ipa_trace_buffer_ctrl
,
1293 sizeof (ipa_trace_buffer_ctrl
));
1295 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1297 /* A traceframe with zeroed fields marks the end of trace data. */
1298 write_inferior_memory (ipa_trace_buffer_lo
,
1299 (unsigned char *) &ipa_traceframe
,
1300 sizeof (ipa_traceframe
));
1302 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1303 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1304 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1310 init_trace_buffer (unsigned char *buf
, int bufsize
)
1312 trace_buffer_lo
= buf
;
1313 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1315 clear_trace_buffer ();
1318 #ifdef IN_PROCESS_AGENT
1320 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1321 about_to_request_buffer_space (void)
1323 /* GDBserver places breakpoint here while it goes about to flush
1324 data at random times. */
1325 UNKNOWN_SIDE_EFFECTS();
1330 /* Carve out a piece of the trace buffer, returning NULL in case of
1334 trace_buffer_alloc (size_t amt
)
1336 unsigned char *rslt
;
1337 struct trace_buffer_control
*tbctrl
;
1339 #ifdef IN_PROCESS_AGENT
1340 unsigned int prev
, prev_filtered
;
1341 unsigned int commit_count
;
1342 unsigned int commit
;
1343 unsigned int readout
;
1345 struct traceframe
*oldest
;
1346 unsigned char *new_start
;
1349 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1350 (long) amt
, (long) sizeof (struct traceframe
));
1352 /* Account for the EOB marker. */
1353 amt
+= sizeof (struct traceframe
);
1355 #ifdef IN_PROCESS_AGENT
1359 /* Read the current token and extract the index to try to write to,
1360 storing it in CURR. */
1361 prev
= trace_buffer_ctrl_curr
;
1362 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1363 curr
= prev_filtered
+ 1;
1367 about_to_request_buffer_space ();
1369 /* Start out with a copy of the current state. GDBserver may be
1370 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1371 be able to commit anyway if that happens. */
1372 trace_buffer_ctrl
[curr
]
1373 = trace_buffer_ctrl
[prev_filtered
];
1374 trace_debug ("trying curr=%u", curr
);
1376 /* The GDBserver's agent doesn't need all that syncing, and always
1377 updates TCB 0 (there's only one, mind you). */
1380 tbctrl
= &trace_buffer_ctrl
[curr
];
1382 /* Offsets are easier to grok for debugging than raw addresses,
1383 especially for the small trace buffer sizes that are useful for
1385 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1387 (int) (tbctrl
->start
- trace_buffer_lo
),
1388 (int) (tbctrl
->free
- trace_buffer_lo
),
1389 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1390 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1391 (int) (trace_buffer_hi
- trace_buffer_lo
));
1393 /* The algorithm here is to keep trying to get a contiguous block of
1394 the requested size, possibly discarding older traceframes to free
1395 up space. Since free space might come in one or two pieces,
1396 depending on whether discarded traceframes wrapped around at the
1397 high end of the buffer, we test both pieces after each
1401 /* First, if we have two free parts, try the upper one first. */
1402 if (tbctrl
->end_free
< tbctrl
->free
)
1404 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1405 /* We have enough in the upper part. */
1409 /* Our high part of free space wasn't enough. Give up
1410 on it for now, set wraparound. We will recover the
1411 space later, if/when the wrapped-around traceframe is
1413 trace_debug ("Upper part too small, setting wraparound");
1414 tbctrl
->wrap
= tbctrl
->free
;
1415 tbctrl
->free
= trace_buffer_lo
;
1419 /* The normal case. */
1420 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1423 #ifdef IN_PROCESS_AGENT
1424 /* The IP Agent's buffer is always circular. It isn't used
1425 currently, but `circular_trace_buffer' could represent
1426 GDBserver's mode. If we didn't find space, ask GDBserver to
1429 flush_trace_buffer ();
1433 trace_debug ("gdbserver flushed buffer, retrying");
1437 /* GDBserver cancelled the tracing. Bail out as well. */
1440 /* If we're here, then neither part is big enough, and
1441 non-circular trace buffers are now full. */
1442 if (!circular_trace_buffer
)
1444 trace_debug ("Not enough space in the trace buffer");
1448 trace_debug ("Need more space in the trace buffer");
1450 /* If we have a circular buffer, we can try discarding the
1451 oldest traceframe and see if that helps. */
1452 oldest
= FIRST_TRACEFRAME ();
1453 if (oldest
->tpnum
== 0)
1455 /* Not good; we have no traceframes to free. Perhaps we're
1456 asking for a block that is larger than the buffer? In
1457 any case, give up. */
1458 trace_debug ("No traceframes to discard");
1462 /* We don't run this code in the in-process agent currently.
1463 E.g., we could leave the in-process agent in autonomous
1464 circular mode if we only have fast tracepoints. If we do
1465 that, then this bit becomes racy with GDBserver, which also
1466 writes to this counter. */
1467 --traceframe_write_count
;
1469 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1470 /* If we freed the traceframe that wrapped around, go back
1471 to the non-wrap case. */
1472 if (new_start
< tbctrl
->start
)
1474 trace_debug ("Discarding past the wraparound");
1475 tbctrl
->wrap
= trace_buffer_hi
;
1477 tbctrl
->start
= new_start
;
1478 tbctrl
->end_free
= tbctrl
->start
;
1480 trace_debug ("Discarded a traceframe\n"
1481 "Trace buffer [%d], start=%d free=%d "
1482 "endfree=%d wrap=%d hi=%d",
1484 (int) (tbctrl
->start
- trace_buffer_lo
),
1485 (int) (tbctrl
->free
- trace_buffer_lo
),
1486 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1487 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1488 (int) (trace_buffer_hi
- trace_buffer_lo
));
1490 /* Now go back around the loop. The discard might have resulted
1491 in either one or two pieces of free space, so we want to try
1492 both before freeing any more traceframes. */
1496 /* If we get here, we know we can provide the asked-for space. */
1498 rslt
= tbctrl
->free
;
1500 /* Adjust the request back down, now that we know we have space for
1501 the marker, but don't commit to AMT yet, we may still need to
1502 restart the operation if GDBserver touches the trace buffer
1503 (obviously only important in the in-process agent's version). */
1504 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1506 /* Or not. If GDBserver changed the trace buffer behind our back,
1507 we get to restart a new allocation attempt. */
1509 #ifdef IN_PROCESS_AGENT
1510 /* Build the tentative token. */
1511 commit_count
= (((prev
& 0x0007ff00) + 0x100) & 0x0007ff00);
1512 commit
= (((prev
& 0x0007ff00) << 12)
1516 /* Try to commit it. */
1517 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1518 if (readout
!= prev
)
1520 trace_debug ("GDBserver has touched the trace buffer, restarting."
1521 " (prev=%08x, commit=%08x, readout=%08x)",
1522 prev
, commit
, readout
);
1526 /* Hold your horses here. Even if that change was committed,
1527 GDBserver could come in, and clobber it. We need to hold to be
1528 able to tell if GDBserver clobbers before or after we committed
1529 the change. Whenever GDBserver goes about touching the IPA
1530 buffer, it sets a breakpoint in this routine, so we have a sync
1532 about_to_request_buffer_space ();
1534 /* Check if the change has been effective, even if GDBserver stopped
1535 us at the breakpoint. */
1538 unsigned int refetch
;
1542 refetch
= trace_buffer_ctrl_curr
;
1544 if ((refetch
== commit
1545 || ((refetch
& 0x7ff00000) >> 12) == commit_count
))
1548 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1549 "readout=%08x, refetch=%08x)",
1550 prev
, commit
, readout
, refetch
);
1554 trace_debug ("GDBserver has touched the trace buffer, not effective."
1555 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1556 prev
, commit
, readout
, refetch
);
1562 /* We have a new piece of the trace buffer. Hurray! */
1564 /* Add an EOB marker just past this allocation. */
1565 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1566 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1568 /* Adjust the request back down, now that we know we have space for
1570 amt
-= sizeof (struct traceframe
);
1574 trace_debug ("Allocated %d bytes", (int) amt
);
1575 trace_debug ("Trace buffer [%d] start=%d free=%d "
1576 "endfree=%d wrap=%d hi=%d",
1578 (int) (tbctrl
->start
- trace_buffer_lo
),
1579 (int) (tbctrl
->free
- trace_buffer_lo
),
1580 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1581 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1582 (int) (trace_buffer_hi
- trace_buffer_lo
));
1588 #ifndef IN_PROCESS_AGENT
1590 /* Return the total free space. This is not necessarily the largest
1591 block we can allocate, because of the two-part case. */
1596 if (trace_buffer_free
<= trace_buffer_end_free
)
1597 return trace_buffer_end_free
- trace_buffer_free
;
1599 return ((trace_buffer_end_free
- trace_buffer_lo
)
1600 + (trace_buffer_hi
- trace_buffer_free
));
1603 /* An 'S' in continuation packets indicates remainder are for
1606 static int seen_step_action_flag
;
1608 /* Create a tracepoint (location) with given number and address. */
1610 static struct tracepoint
*
1611 add_tracepoint (int num
, CORE_ADDR addr
)
1613 struct tracepoint
*tpoint
;
1615 tpoint
= xmalloc (sizeof (struct tracepoint
));
1616 tpoint
->number
= num
;
1617 tpoint
->address
= addr
;
1618 tpoint
->numactions
= 0;
1619 tpoint
->actions
= NULL
;
1620 tpoint
->actions_str
= NULL
;
1621 tpoint
->cond
= NULL
;
1622 tpoint
->num_step_actions
= 0;
1623 tpoint
->step_actions
= NULL
;
1624 tpoint
->step_actions_str
= NULL
;
1625 /* Start all off as regular (slow) tracepoints. */
1626 tpoint
->type
= trap_tracepoint
;
1627 tpoint
->orig_size
= -1;
1628 tpoint
->source_strings
= NULL
;
1629 tpoint
->compiled_cond
= 0;
1630 tpoint
->handle
= NULL
;
1631 tpoint
->next
= NULL
;
1633 if (!last_tracepoint
)
1634 tracepoints
= tpoint
;
1636 last_tracepoint
->next
= tpoint
;
1637 last_tracepoint
= tpoint
;
1639 seen_step_action_flag
= 0;
1644 #ifndef IN_PROCESS_AGENT
1646 /* Return the tracepoint with the given number and address, or NULL. */
1648 static struct tracepoint
*
1649 find_tracepoint (int id
, CORE_ADDR addr
)
1651 struct tracepoint
*tpoint
;
1653 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1654 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1660 /* There may be several tracepoints with the same number (because they
1661 are "locations", in GDB parlance); return the next one after the
1662 given tracepoint, or search from the beginning of the list if the
1663 first argument is NULL. */
1665 static struct tracepoint
*
1666 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1668 struct tracepoint
*tpoint
;
1671 tpoint
= prev_tp
->next
;
1673 tpoint
= tracepoints
;
1674 for (; tpoint
; tpoint
= tpoint
->next
)
1675 if (tpoint
->number
== num
)
1684 save_string (const char *str
, size_t len
)
1688 s
= xmalloc (len
+ 1);
1689 memcpy (s
, str
, len
);
1695 /* Append another action to perform when the tracepoint triggers. */
1698 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1704 seen_step_action_flag
= 1;
1712 char *act_start
= act
;
1713 struct tracepoint_action
*action
= NULL
;
1719 struct collect_memory_action
*maction
;
1723 maction
= xmalloc (sizeof *maction
);
1724 maction
->base
.type
= *act
;
1725 action
= &maction
->base
;
1728 is_neg
= (*act
== '-');
1731 act
= unpack_varlen_hex (act
, &basereg
);
1733 act
= unpack_varlen_hex (act
, &maction
->addr
);
1735 act
= unpack_varlen_hex (act
, &maction
->len
);
1736 maction
->basereg
= (is_neg
1739 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1740 pulongest (maction
->len
),
1741 paddress (maction
->addr
), maction
->basereg
);
1746 struct collect_registers_action
*raction
;
1748 raction
= xmalloc (sizeof *raction
);
1749 raction
->base
.type
= *act
;
1750 action
= &raction
->base
;
1752 trace_debug ("Want to collect registers");
1754 /* skip past hex digits of mask for now */
1755 while (isxdigit(*act
))
1760 trace_debug ("Unexpected step action, ignoring");
1765 struct eval_expr_action
*xaction
;
1767 xaction
= xmalloc (sizeof (*xaction
));
1768 xaction
->base
.type
= *act
;
1769 action
= &xaction
->base
;
1771 trace_debug ("Want to evaluate expression");
1772 xaction
->expr
= parse_agent_expr (&act
);
1776 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1785 if (seen_step_action_flag
)
1787 tpoint
->num_step_actions
++;
1789 tpoint
->step_actions
1790 = xrealloc (tpoint
->step_actions
,
1791 (sizeof (*tpoint
->step_actions
)
1792 * tpoint
->num_step_actions
));
1793 tpoint
->step_actions_str
1794 = xrealloc (tpoint
->step_actions_str
,
1795 (sizeof (*tpoint
->step_actions_str
)
1796 * tpoint
->num_step_actions
));
1797 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1798 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1799 = save_string (act_start
, act
- act_start
);
1803 tpoint
->numactions
++;
1805 = xrealloc (tpoint
->actions
,
1806 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1808 = xrealloc (tpoint
->actions_str
,
1809 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1810 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1811 tpoint
->actions_str
[tpoint
->numactions
- 1]
1812 = save_string (act_start
, act
- act_start
);
1819 /* Find or create a trace state variable with the given number. */
1821 static struct trace_state_variable
*
1822 get_trace_state_variable (int num
)
1824 struct trace_state_variable
*tsv
;
1826 #ifdef IN_PROCESS_AGENT
1827 /* Search for an existing variable. */
1828 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1829 if (tsv
->number
== num
)
1833 /* Search for an existing variable. */
1834 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1835 if (tsv
->number
== num
)
1841 /* Find or create a trace state variable with the given number. */
1843 static struct trace_state_variable
*
1844 create_trace_state_variable (int num
, int gdb
)
1846 struct trace_state_variable
*tsv
;
1848 tsv
= get_trace_state_variable (num
);
1852 /* Create a new variable. */
1853 tsv
= xmalloc (sizeof (struct trace_state_variable
));
1855 tsv
->initial_value
= 0;
1859 #ifdef IN_PROCESS_AGENT
1862 tsv
->next
= alloced_trace_state_variables
;
1863 alloced_trace_state_variables
= tsv
;
1868 tsv
->next
= trace_state_variables
;
1869 trace_state_variables
= tsv
;
1874 IP_AGENT_EXPORT LONGEST
1875 get_trace_state_variable_value (int num
)
1877 struct trace_state_variable
*tsv
;
1879 tsv
= get_trace_state_variable (num
);
1883 trace_debug ("No trace state variable %d, skipping value get", num
);
1887 /* Call a getter function if we have one. While it's tempting to
1888 set up something to only call the getter once per tracepoint hit,
1889 it could run afoul of thread races. Better to let the getter
1890 handle it directly, if necessary to worry about it. */
1892 tsv
->value
= (tsv
->getter
) ();
1894 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1895 num
, plongest (tsv
->value
));
1900 IP_AGENT_EXPORT
void
1901 set_trace_state_variable_value (int num
, LONGEST val
)
1903 struct trace_state_variable
*tsv
;
1905 tsv
= get_trace_state_variable (num
);
1909 trace_debug ("No trace state variable %d, skipping value set", num
);
1917 set_trace_state_variable_name (int num
, const char *name
)
1919 struct trace_state_variable
*tsv
;
1921 tsv
= get_trace_state_variable (num
);
1925 trace_debug ("No trace state variable %d, skipping name set", num
);
1929 tsv
->name
= (char *) name
;
1933 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
1935 struct trace_state_variable
*tsv
;
1937 tsv
= get_trace_state_variable (num
);
1941 trace_debug ("No trace state variable %d, skipping getter set", num
);
1945 tsv
->getter
= getter
;
1948 /* Add a raw traceframe for the given tracepoint. */
1950 static struct traceframe
*
1951 add_traceframe (struct tracepoint
*tpoint
)
1953 struct traceframe
*tframe
;
1955 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
1960 tframe
->tpnum
= tpoint
->number
;
1961 tframe
->data_size
= 0;
1966 /* Add a block to the traceframe currently being worked on. */
1968 static unsigned char *
1969 add_traceframe_block (struct traceframe
*tframe
, int amt
)
1971 unsigned char *block
;
1976 block
= trace_buffer_alloc (amt
);
1981 tframe
->data_size
+= amt
;
1986 /* Flag that the current traceframe is finished. */
1989 finish_traceframe (struct traceframe
*tframe
)
1991 ++traceframe_write_count
;
1992 ++traceframes_created
;
1995 #ifndef IN_PROCESS_AGENT
1997 /* Given a traceframe number NUM, find the NUMth traceframe in the
2000 static struct traceframe
*
2001 find_traceframe (int num
)
2003 struct traceframe
*tframe
;
2006 for (tframe
= FIRST_TRACEFRAME ();
2008 tframe
= NEXT_TRACEFRAME (tframe
))
2019 get_traceframe_address (struct traceframe
*tframe
)
2022 struct tracepoint
*tpoint
;
2024 addr
= traceframe_get_pc (tframe
);
2029 /* Fallback strategy, will be incorrect for while-stepping frames
2030 and multi-location tracepoints. */
2031 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2032 return tpoint
->address
;
2035 /* Search for the next traceframe whose address is inside or outside
2038 static struct traceframe
*
2039 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2042 struct traceframe
*tframe
;
2045 *tfnump
= current_traceframe
+ 1;
2046 tframe
= find_traceframe (*tfnump
);
2047 /* The search is not supposed to wrap around. */
2054 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2056 tfaddr
= get_traceframe_address (tframe
);
2058 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2059 : (lo
> tfaddr
|| tfaddr
> hi
))
2068 /* Search for the next traceframe recorded by the given tracepoint.
2069 Note that for multi-location tracepoints, this will find whatever
2070 location appears first. */
2072 static struct traceframe
*
2073 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2075 struct traceframe
*tframe
;
2077 *tfnump
= current_traceframe
+ 1;
2078 tframe
= find_traceframe (*tfnump
);
2079 /* The search is not supposed to wrap around. */
2086 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2088 if (tframe
->tpnum
== num
)
2099 #ifndef IN_PROCESS_AGENT
2101 /* Clear all past trace state. */
2104 cmd_qtinit (char *packet
)
2106 struct trace_state_variable
*tsv
, *prev
, *next
;
2108 /* Make sure we don't try to read from a trace frame. */
2109 current_traceframe
= -1;
2111 trace_debug ("Initializing the trace");
2113 clear_installed_tracepoints ();
2114 clear_readonly_regions ();
2117 last_tracepoint
= NULL
;
2119 /* Clear out any leftover trace state variables. Ones with target
2120 defined getters should be kept however. */
2122 tsv
= trace_state_variables
;
2125 trace_debug ("Looking at var %d", tsv
->number
);
2126 if (tsv
->getter
== NULL
)
2132 trace_state_variables
= next
;
2133 trace_debug ("Deleting var %d", tsv
->number
);
2144 clear_trace_buffer ();
2145 clear_inferior_trace_buffer ();
2150 /* Restore the program to its pre-tracing state. This routine may be called
2151 in error situations, so it needs to be careful about only restoring
2152 from known-valid bits. */
2155 clear_installed_tracepoints (void)
2157 struct tracepoint
*tpoint
;
2158 struct tracepoint
*prev_stpoint
;
2161 cancel_breakpoints ();
2163 prev_stpoint
= NULL
;
2165 /* Restore any bytes overwritten by tracepoints. */
2166 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2168 if (!tpoint
->enabled
)
2171 /* Catch the case where we might try to remove a tracepoint that
2172 was never actually installed. */
2173 if (tpoint
->handle
== NULL
)
2175 trace_debug ("Tracepoint %d at 0x%s was "
2176 "never installed, nothing to clear",
2177 tpoint
->number
, paddress (tpoint
->address
));
2181 switch (tpoint
->type
)
2183 case trap_tracepoint
:
2184 delete_breakpoint (tpoint
->handle
);
2186 case fast_tracepoint
:
2187 delete_fast_tracepoint_jump (tpoint
->handle
);
2191 tpoint
->handle
= NULL
;
2197 /* Parse a packet that defines a tracepoint. */
2200 cmd_qtdp (char *own_buf
)
2206 struct tracepoint
*tpoint
;
2208 char *packet
= own_buf
;
2210 packet
+= strlen ("QTDP:");
2212 /* A hyphen at the beginning marks a packet specifying actions for a
2213 tracepoint already supplied. */
2220 packet
= unpack_varlen_hex (packet
, &num
);
2221 ++packet
; /* skip a colon */
2222 packet
= unpack_varlen_hex (packet
, &addr
);
2223 ++packet
; /* skip a colon */
2225 /* See if we already have this tracepoint. */
2226 tpoint
= find_tracepoint (num
, addr
);
2230 /* Duplicate tracepoints are never allowed. */
2233 trace_debug ("Tracepoint error: tracepoint %d"
2234 " at 0x%s already exists",
2235 (int) num
, paddress (addr
));
2236 write_enn (own_buf
);
2240 tpoint
= add_tracepoint (num
, addr
);
2242 tpoint
->enabled
= (*packet
== 'E');
2243 ++packet
; /* skip 'E' */
2244 ++packet
; /* skip a colon */
2245 packet
= unpack_varlen_hex (packet
, &count
);
2246 tpoint
->step_count
= count
;
2247 ++packet
; /* skip a colon */
2248 packet
= unpack_varlen_hex (packet
, &count
);
2249 tpoint
->pass_count
= count
;
2250 /* See if we have any of the additional optional fields. */
2251 while (*packet
== ':')
2256 tpoint
->type
= fast_tracepoint
;
2258 packet
= unpack_varlen_hex (packet
, &count
);
2259 tpoint
->orig_size
= count
;
2261 else if (*packet
== 'X')
2263 actparm
= (char *) packet
;
2264 tpoint
->cond
= parse_agent_expr (&actparm
);
2267 else if (*packet
== '-')
2269 else if (*packet
== '\0')
2272 trace_debug ("Unknown optional tracepoint field");
2275 trace_debug ("Also has actions\n");
2277 trace_debug ("Defined %stracepoint %d at 0x%s, "
2278 "enabled %d step %ld pass %ld",
2279 tpoint
->type
== fast_tracepoint
? "fast "
2281 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2282 tpoint
->step_count
, tpoint
->pass_count
);
2285 add_tracepoint_action (tpoint
, packet
);
2288 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2289 (int) num
, paddress (addr
));
2290 write_enn (own_buf
);
2298 cmd_qtdpsrc (char *own_buf
)
2300 ULONGEST num
, addr
, start
, slen
;
2301 struct tracepoint
*tpoint
;
2302 char *packet
= own_buf
;
2303 char *saved
, *srctype
, *src
;
2305 struct source_string
*last
, *newlast
;
2307 packet
+= strlen ("QTDPsrc:");
2309 packet
= unpack_varlen_hex (packet
, &num
);
2310 ++packet
; /* skip a colon */
2311 packet
= unpack_varlen_hex (packet
, &addr
);
2312 ++packet
; /* skip a colon */
2314 /* See if we already have this tracepoint. */
2315 tpoint
= find_tracepoint (num
, addr
);
2319 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2320 (int) num
, paddress (addr
));
2321 write_enn (own_buf
);
2326 packet
= strchr (packet
, ':');
2327 srctype
= xmalloc (packet
- saved
+ 1);
2328 memcpy (srctype
, saved
, packet
- saved
);
2329 srctype
[packet
- saved
] = '\0';
2331 packet
= unpack_varlen_hex (packet
, &start
);
2332 ++packet
; /* skip a colon */
2333 packet
= unpack_varlen_hex (packet
, &slen
);
2334 ++packet
; /* skip a colon */
2335 src
= xmalloc (slen
+ 1);
2336 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2339 newlast
= xmalloc (sizeof (struct source_string
));
2340 newlast
->type
= srctype
;
2342 newlast
->next
= NULL
;
2343 /* Always add a source string to the end of the list;
2344 this keeps sequences of actions/commands in the right
2346 if (tpoint
->source_strings
)
2348 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2350 last
->next
= newlast
;
2353 tpoint
->source_strings
= newlast
;
2359 cmd_qtdv (char *own_buf
)
2361 ULONGEST num
, val
, builtin
;
2364 struct trace_state_variable
*tsv
;
2365 char *packet
= own_buf
;
2367 packet
+= strlen ("QTDV:");
2369 packet
= unpack_varlen_hex (packet
, &num
);
2370 ++packet
; /* skip a colon */
2371 packet
= unpack_varlen_hex (packet
, &val
);
2372 ++packet
; /* skip a colon */
2373 packet
= unpack_varlen_hex (packet
, &builtin
);
2374 ++packet
; /* skip a colon */
2376 nbytes
= strlen (packet
) / 2;
2377 varname
= xmalloc (nbytes
+ 1);
2378 nbytes
= unhexify (varname
, packet
, nbytes
);
2379 varname
[nbytes
] = '\0';
2381 tsv
= create_trace_state_variable (num
, 1);
2382 tsv
->initial_value
= (LONGEST
) val
;
2383 tsv
->name
= varname
;
2385 set_trace_state_variable_value (num
, (LONGEST
) val
);
2391 cmd_qtv (char *own_buf
)
2396 char *packet
= own_buf
;
2398 packet
+= strlen ("qTV:");
2399 packet
= unpack_varlen_hex (packet
, &num
);
2401 if (current_traceframe
>= 0)
2403 err
= traceframe_read_tsv ((int) num
, &val
);
2406 strcpy (own_buf
, "U");
2410 /* Only make tsv's be undefined before the first trace run. After a
2411 trace run is over, the user might want to see the last value of
2412 the tsv, and it might not be available in a traceframe. */
2413 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2415 strcpy (own_buf
, "U");
2419 val
= get_trace_state_variable_value (num
);
2421 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2424 /* Clear out the list of readonly regions. */
2427 clear_readonly_regions (void)
2429 struct readonly_region
*roreg
;
2431 while (readonly_regions
)
2433 roreg
= readonly_regions
;
2434 readonly_regions
= readonly_regions
->next
;
2439 /* Parse the collection of address ranges whose contents GDB believes
2440 to be unchanging and so can be read directly from target memory
2441 even while looking at a traceframe. */
2444 cmd_qtro (char *own_buf
)
2446 ULONGEST start
, end
;
2447 struct readonly_region
*roreg
;
2448 char *packet
= own_buf
;
2450 trace_debug ("Want to mark readonly regions");
2452 clear_readonly_regions ();
2454 packet
+= strlen ("QTro");
2456 while (*packet
== ':')
2458 ++packet
; /* skip a colon */
2459 packet
= unpack_varlen_hex (packet
, &start
);
2460 ++packet
; /* skip a comma */
2461 packet
= unpack_varlen_hex (packet
, &end
);
2462 roreg
= xmalloc (sizeof (struct readonly_region
));
2463 roreg
->start
= start
;
2465 roreg
->next
= readonly_regions
;
2466 readonly_regions
= roreg
;
2467 trace_debug ("Added readonly region from 0x%s to 0x%s",
2468 paddress (roreg
->start
), paddress (roreg
->end
));
2474 /* Test to see if the given range is in our list of readonly ranges.
2475 We only test for being entirely within a range, GDB is not going to
2476 send a single memory packet that spans multiple regions. */
2479 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2481 struct readonly_region
*roreg
;
2483 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2484 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2490 /* The maximum size of a jump pad entry. */
2491 static const int max_jump_pad_size
= 0x100;
2493 static CORE_ADDR gdb_jump_pad_head
;
2495 /* Return the address of the next free jump space. */
2498 get_jump_space_head (void)
2500 if (gdb_jump_pad_head
== 0)
2502 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2503 &gdb_jump_pad_head
))
2504 fatal ("error extracting jump_pad_buffer");
2507 return gdb_jump_pad_head
;
2510 /* Reserve USED bytes from the jump space. */
2513 claim_jump_space (ULONGEST used
)
2515 trace_debug ("claim_jump_space reserves %s bytes at %s",
2516 pulongest (used
), paddress (gdb_jump_pad_head
));
2517 gdb_jump_pad_head
+= used
;
2520 /* Sort tracepoints by PC, using a bubble sort. */
2523 sort_tracepoints (void)
2525 struct tracepoint
*lst
, *tmp
, *prev
= NULL
;
2528 if (tracepoints
== NULL
)
2532 for (tmp
= tracepoints
; tmp
->next
; tmp
= tmp
->next
)
2535 for (i
= 0; i
< n
- 1; i
++)
2536 for (j
= 0, lst
= tracepoints
;
2537 lst
&& lst
->next
&& (j
<= n
- 1 - i
);
2540 /* If we're at beginning, the start node is the prev
2545 /* Compare neighbors. */
2546 if (lst
->next
->address
< lst
->address
)
2548 struct tracepoint
*p
;
2551 tmp
= (lst
->next
? lst
->next
->next
: NULL
);
2553 if (j
== 0 && prev
== tracepoints
)
2554 tracepoints
= lst
->next
;
2557 prev
->next
= lst
->next
;
2558 lst
->next
->next
= lst
;
2565 /* Keep track of the previous node. We need it if we need
2573 #define MAX_JUMP_SIZE 20
2576 cmd_qtstart (char *packet
)
2578 struct tracepoint
*tpoint
, *prev_ftpoint
;
2579 int slow_tracepoint_count
, fast_count
;
2580 CORE_ADDR jump_entry
;
2582 /* The jump to the jump pad of the last fast tracepoint
2584 unsigned char fjump
[MAX_JUMP_SIZE
];
2585 ULONGEST fjump_size
;
2587 trace_debug ("Starting the trace");
2589 slow_tracepoint_count
= fast_count
= 0;
2591 /* Sort tracepoints by ascending address. This makes installing
2592 fast tracepoints at the same address easier to handle. */
2593 sort_tracepoints ();
2595 /* Pause all threads temporarily while we patch tracepoints. */
2598 /* Get threads out of jump pads. Safe to do here, since this is a
2599 top level command. And, required to do here, since we're
2600 deleting/rewriting jump pads. */
2602 stabilize_threads ();
2604 /* Freeze threads. */
2607 /* Sync the fast tracepoints list in the inferior ftlib. */
2608 if (in_process_agent_loaded ())
2610 download_tracepoints ();
2611 download_trace_state_variables ();
2614 /* No previous fast tpoint yet. */
2615 prev_ftpoint
= NULL
;
2619 /* Install tracepoints. */
2620 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2622 /* Ensure all the hit counts start at zero. */
2623 tpoint
->hit_count
= 0;
2625 if (!tpoint
->enabled
)
2628 if (tpoint
->type
== trap_tracepoint
)
2630 ++slow_tracepoint_count
;
2632 /* Tracepoints are installed as memory breakpoints. Just go
2633 ahead and install the trap. The breakpoints module
2634 handles duplicated breakpoints, and the memory read
2635 routine handles un-patching traps from memory reads. */
2636 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
2637 tracepoint_handler
);
2639 else if (tpoint
->type
== fast_tracepoint
)
2643 if (maybe_write_ipa_not_loaded (packet
))
2645 trace_debug ("Requested a fast tracepoint, but fast "
2646 "tracepoints aren't supported.");
2650 if (prev_ftpoint
!= NULL
&& prev_ftpoint
->address
== tpoint
->address
)
2652 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
,
2655 tpoint
->jump_pad
= prev_ftpoint
->jump_pad
;
2656 tpoint
->jump_pad_end
= prev_ftpoint
->jump_pad_end
;
2657 tpoint
->adjusted_insn_addr
= prev_ftpoint
->adjusted_insn_addr
;
2658 tpoint
->adjusted_insn_addr_end
2659 = prev_ftpoint
->adjusted_insn_addr_end
;
2666 prev_ftpoint
= NULL
;
2668 jentry
= jump_entry
= get_jump_space_head ();
2670 /* Install the jump pad. */
2671 err
= install_fast_tracepoint_jump_pad
2672 (tpoint
->obj_addr_on_target
,
2674 ipa_sym_addrs
.addr_gdb_collect
,
2675 ipa_sym_addrs
.addr_collecting
,
2679 &tpoint
->adjusted_insn_addr
,
2680 &tpoint
->adjusted_insn_addr_end
);
2684 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
,
2687 if (tpoint
->handle
!= NULL
)
2689 tpoint
->jump_pad
= jump_entry
;
2690 tpoint
->jump_pad_end
= jentry
;
2692 /* Pad to 8-byte alignment. */
2693 jentry
= ((jentry
+ 7) & ~0x7);
2694 claim_jump_space (jentry
- jump_entry
);
2696 /* So that we can handle multiple fast tracepoints
2697 at the same address easily. */
2698 prev_ftpoint
= tpoint
;
2703 /* Any failure in the inner loop is sufficient cause to give
2705 if (tpoint
->handle
== NULL
)
2709 /* Any error in tracepoint insertion is unacceptable; better to
2710 address the problem now, than end up with a useless or misleading
2714 clear_installed_tracepoints ();
2715 if (*packet
== '\0')
2721 stopping_tracepoint
= NULL
;
2722 trace_buffer_is_full
= 0;
2723 expr_eval_result
= expr_eval_no_error
;
2724 error_tracepoint
= NULL
;
2726 /* Tracing is now active, hits will now start being logged. */
2729 if (in_process_agent_loaded ())
2731 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
2732 fatal ("Error setting tracing variable in lib");
2734 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
2736 fatal ("Error clearing stopping_tracepoint variable in lib");
2738 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
2739 fatal ("Error clearing trace_buffer_is_full variable in lib");
2741 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
2742 stop_tracing_handler
);
2743 if (stop_tracing_bkpt
== NULL
)
2744 error ("Error setting stop_tracing breakpoint");
2746 flush_trace_buffer_bkpt
2747 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
2748 flush_trace_buffer_handler
);
2749 if (flush_trace_buffer_bkpt
== NULL
)
2750 error ("Error setting flush_trace_buffer breakpoint");
2758 /* End a tracing run, filling in a stop reason to report back to GDB,
2759 and removing the tracepoints from the code. */
2766 trace_debug ("Tracing is already off, ignoring");
2770 trace_debug ("Stopping the trace");
2772 /* Pause all threads before removing fast jumps from memory,
2773 breakpoints, and touching IPA state variables (inferior memory).
2774 Some thread may hit the internal tracing breakpoints, or be
2775 collecting this moment, but that's ok, we don't release the
2776 tpoint object's memory or the jump pads here (we only do that
2777 when we're sure we can move all threads out of the jump pads).
2778 We can't now, since we may be getting here due to the inferior
2779 agent calling us. */
2781 /* Since we're removing breakpoints, cancel breakpoint hits,
2782 possibly related to the breakpoints we're about to delete. */
2783 cancel_breakpoints ();
2785 /* Stop logging. Tracepoints can still be hit, but they will not be
2788 if (in_process_agent_loaded ())
2790 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
2791 fatal ("Error clearing tracing variable in lib");
2794 tracing_stop_reason
= "t???";
2795 tracing_stop_tpnum
= 0;
2796 if (stopping_tracepoint
)
2798 trace_debug ("Stopping the trace because "
2799 "tracepoint %d was hit %ld times",
2800 stopping_tracepoint
->number
,
2801 stopping_tracepoint
->pass_count
);
2802 tracing_stop_reason
= "tpasscount";
2803 tracing_stop_tpnum
= stopping_tracepoint
->number
;
2805 else if (trace_buffer_is_full
)
2807 trace_debug ("Stopping the trace because the trace buffer is full");
2808 tracing_stop_reason
= "tfull";
2810 else if (expr_eval_result
!= expr_eval_no_error
)
2812 trace_debug ("Stopping the trace because of an expression eval error");
2813 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
2814 tracing_stop_tpnum
= error_tracepoint
->number
;
2816 #ifndef IN_PROCESS_AGENT
2817 else if (!gdb_connected ())
2819 trace_debug ("Stopping the trace because GDB disconnected");
2820 tracing_stop_reason
= "tdisconnected";
2825 trace_debug ("Stopping the trace because of a tstop command");
2826 tracing_stop_reason
= "tstop";
2829 stopping_tracepoint
= NULL
;
2830 error_tracepoint
= NULL
;
2832 /* Clear out the tracepoints. */
2833 clear_installed_tracepoints ();
2835 if (in_process_agent_loaded ())
2837 /* Pull in fast tracepoint trace frames from the inferior lib
2838 buffer into our buffer, even if our buffer is already full,
2839 because we want to present the full number of created frames
2840 in addition to what fit in the trace buffer. */
2841 upload_fast_traceframes ();
2844 if (stop_tracing_bkpt
!= NULL
)
2846 delete_breakpoint (stop_tracing_bkpt
);
2847 stop_tracing_bkpt
= NULL
;
2850 if (flush_trace_buffer_bkpt
!= NULL
)
2852 delete_breakpoint (flush_trace_buffer_bkpt
);
2853 flush_trace_buffer_bkpt
= NULL
;
2860 stop_tracing_handler (CORE_ADDR addr
)
2862 trace_debug ("lib hit stop_tracing");
2864 /* Don't actually handle it here. When we stop tracing we remove
2865 breakpoints from the inferior, and that is not allowed in a
2866 breakpoint handler (as the caller is walking the breakpoint
2872 flush_trace_buffer_handler (CORE_ADDR addr
)
2874 trace_debug ("lib hit flush_trace_buffer");
2879 cmd_qtstop (char *packet
)
2886 cmd_qtdisconnected (char *own_buf
)
2889 char *packet
= own_buf
;
2891 packet
+= strlen ("QTDisconnected:");
2893 unpack_varlen_hex (packet
, &setting
);
2897 disconnected_tracing
= setting
;
2901 cmd_qtframe (char *own_buf
)
2903 ULONGEST frame
, pc
, lo
, hi
, num
;
2905 struct traceframe
*tframe
;
2906 char *packet
= own_buf
;
2908 packet
+= strlen ("QTFrame:");
2910 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
2912 packet
+= strlen ("pc:");
2913 packet
= unpack_varlen_hex (packet
, &pc
);
2914 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
2915 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
2917 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
2919 packet
+= strlen ("range:");
2920 packet
= unpack_varlen_hex (packet
, &lo
);
2922 packet
= unpack_varlen_hex (packet
, &hi
);
2923 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
2924 paddress (lo
), paddress (hi
));
2925 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
2927 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
2929 packet
+= strlen ("outside:");
2930 packet
= unpack_varlen_hex (packet
, &lo
);
2932 packet
= unpack_varlen_hex (packet
, &hi
);
2933 trace_debug ("Want to find next traceframe "
2934 "outside the range 0x%s to 0x%s",
2935 paddress (lo
), paddress (hi
));
2936 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
2938 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
2940 packet
+= strlen ("tdp:");
2941 packet
= unpack_varlen_hex (packet
, &num
);
2943 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
2944 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
2948 unpack_varlen_hex (packet
, &frame
);
2949 tfnum
= (int) frame
;
2952 trace_debug ("Want to stop looking at traceframes");
2953 current_traceframe
= -1;
2957 trace_debug ("Want to look at traceframe %d", tfnum
);
2958 tframe
= find_traceframe (tfnum
);
2963 current_traceframe
= tfnum
;
2964 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
2967 sprintf (own_buf
, "F-1");
2971 cmd_qtstatus (char *packet
)
2973 char *stop_reason_rsp
= NULL
;
2975 trace_debug ("Returning trace status as %d, stop reason %s",
2976 tracing
, tracing_stop_reason
);
2978 if (in_process_agent_loaded ())
2982 upload_fast_traceframes ();
2987 stop_reason_rsp
= (char *) tracing_stop_reason
;
2989 /* The user visible error string in terror needs to be hex encoded.
2990 We leave it as plain string in `tracepoint_stop_reason' to ease
2992 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
2994 const char *result_name
;
2998 result_name
= stop_reason_rsp
+ strlen ("terror:");
2999 hexstr_len
= strlen (result_name
) * 2;
3000 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3001 strcpy (p
, "terror:");
3003 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3009 "tframes:%x;tcreated:%x;"
3010 "tfree:%x;tsize:%s;"
3014 stop_reason_rsp
, tracing_stop_tpnum
,
3015 traceframe_count
, traceframes_created
,
3016 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3017 circular_trace_buffer
,
3018 disconnected_tracing
);
3021 /* State variables to help return all the tracepoint bits. */
3022 static struct tracepoint
*cur_tpoint
;
3023 static int cur_action
;
3024 static int cur_step_action
;
3025 static struct source_string
*cur_source_string
;
3026 static struct trace_state_variable
*cur_tsv
;
3028 /* Compose a response that is an imitation of the syntax by which the
3029 tracepoint was originally downloaded. */
3032 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3036 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
3037 paddress (tpoint
->address
),
3038 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3039 tpoint
->pass_count
);
3040 if (tpoint
->type
== fast_tracepoint
)
3041 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3045 buf
= unparse_agent_expr (tpoint
->cond
);
3046 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3047 tpoint
->cond
->length
, buf
);
3052 /* Compose a response that is an imitation of the syntax by which the
3053 tracepoint action was originally downloaded (with the difference
3054 that due to the way we store the actions, this will output a packet
3055 per action, while GDB could have combined more than one action
3059 response_action (char *packet
, struct tracepoint
*tpoint
,
3060 char *taction
, int step
)
3062 sprintf (packet
, "%c%x:%s:%s",
3063 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3067 /* Compose a response that is an imitation of the syntax by which the
3068 tracepoint source piece was originally downloaded. */
3071 response_source (char *packet
,
3072 struct tracepoint
*tpoint
, struct source_string
*src
)
3077 len
= strlen (src
->str
);
3078 buf
= alloca (len
* 2 + 1);
3079 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3081 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3082 tpoint
->number
, paddress (tpoint
->address
),
3083 src
->type
, 0, len
, buf
);
3086 /* Return the first piece of tracepoint definition, and initialize the
3087 state machine that will iterate through all the tracepoint
3091 cmd_qtfp (char *packet
)
3093 trace_debug ("Returning first tracepoint definition piece");
3095 cur_tpoint
= tracepoints
;
3096 cur_action
= cur_step_action
= -1;
3097 cur_source_string
= NULL
;
3100 response_tracepoint (packet
, cur_tpoint
);
3102 strcpy (packet
, "l");
3105 /* Return additional pieces of tracepoint definition. Each action and
3106 stepping action must go into its own packet, because of packet size
3107 limits, and so we use state variables to deliver one piece at a
3111 cmd_qtsp (char *packet
)
3113 trace_debug ("Returning subsequent tracepoint definition piece");
3117 /* This case would normally never occur, but be prepared for
3119 strcpy (packet
, "l");
3121 else if (cur_action
< cur_tpoint
->numactions
- 1)
3124 response_action (packet
, cur_tpoint
,
3125 cur_tpoint
->actions_str
[cur_action
], 0);
3127 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3130 response_action (packet
, cur_tpoint
,
3131 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3133 else if ((cur_source_string
3134 ? cur_source_string
->next
3135 : cur_tpoint
->source_strings
))
3137 if (cur_source_string
)
3138 cur_source_string
= cur_source_string
->next
;
3140 cur_source_string
= cur_tpoint
->source_strings
;
3141 response_source (packet
, cur_tpoint
, cur_source_string
);
3145 cur_tpoint
= cur_tpoint
->next
;
3146 cur_action
= cur_step_action
= -1;
3147 cur_source_string
= NULL
;
3149 response_tracepoint (packet
, cur_tpoint
);
3151 strcpy (packet
, "l");
3155 /* Compose a response that is an imitation of the syntax by which the
3156 trace state variable was originally downloaded. */
3159 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3161 char *buf
= (char *) "";
3166 namelen
= strlen (tsv
->name
);
3167 buf
= alloca (namelen
* 2 + 1);
3168 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3171 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3172 tsv
->getter
? 1 : 0, buf
);
3175 /* Return the first trace state variable definition, and initialize
3176 the state machine that will iterate through all the tsv bits. */
3179 cmd_qtfv (char *packet
)
3181 trace_debug ("Returning first trace state variable definition");
3183 cur_tsv
= trace_state_variables
;
3186 response_tsv (packet
, cur_tsv
);
3188 strcpy (packet
, "l");
3191 /* Return additional trace state variable definitions. */
3194 cmd_qtsv (char *packet
)
3196 trace_debug ("Returning first trace state variable definition");
3200 /* This case would normally never occur, but be prepared for
3202 strcpy (packet
, "l");
3206 cur_tsv
= cur_tsv
->next
;
3208 response_tsv (packet
, cur_tsv
);
3210 strcpy (packet
, "l");
3213 strcpy (packet
, "l");
3216 /* Respond to qTBuffer packet with a block of raw data from the trace
3217 buffer. GDB may ask for a lot, but we are allowed to reply with
3218 only as much as will fit within packet limits or whatever. */
3221 cmd_qtbuffer (char *own_buf
)
3223 ULONGEST offset
, num
, tot
;
3225 char *packet
= own_buf
;
3227 packet
+= strlen ("qTBuffer:");
3229 packet
= unpack_varlen_hex (packet
, &offset
);
3230 ++packet
; /* skip a comma */
3231 packet
= unpack_varlen_hex (packet
, &num
);
3233 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3234 (int) num
, pulongest (offset
));
3236 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3238 /* If we're right at the end, reply specially that we're done. */
3241 strcpy (own_buf
, "l");
3245 /* Object to any other out-of-bounds request. */
3248 write_enn (own_buf
);
3252 /* Compute the pointer corresponding to the given offset, accounting
3254 tbp
= trace_buffer_start
+ offset
;
3255 if (tbp
>= trace_buffer_wrap
)
3256 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3258 /* Trim to the remaining bytes if we're close to the end. */
3259 if (num
> tot
- offset
)
3262 /* Trim to available packet size. */
3263 if (num
>= (PBUFSIZ
- 16) / 2 )
3264 num
= (PBUFSIZ
- 16) / 2;
3266 convert_int_to_ascii (tbp
, own_buf
, num
);
3267 own_buf
[num
] = '\0';
3271 cmd_bigqtbuffer (char *own_buf
)
3274 char *packet
= own_buf
;
3276 packet
+= strlen ("QTBuffer:");
3278 if (strncmp ("circular:", packet
, strlen ("circular:")) == 0)
3280 packet
+= strlen ("circular:");
3281 packet
= unpack_varlen_hex (packet
, &val
);
3282 circular_trace_buffer
= val
;
3283 trace_debug ("Trace buffer is now %s",
3284 circular_trace_buffer
? "circular" : "linear");
3288 write_enn (own_buf
);
3292 handle_tracepoint_general_set (char *packet
)
3294 if (strcmp ("QTinit", packet
) == 0)
3296 cmd_qtinit (packet
);
3299 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3304 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3306 cmd_qtdpsrc (packet
);
3309 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3314 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3319 else if (strcmp ("QTStart", packet
) == 0)
3321 cmd_qtstart (packet
);
3324 else if (strcmp ("QTStop", packet
) == 0)
3326 cmd_qtstop (packet
);
3329 else if (strncmp ("QTDisconnected:", packet
,
3330 strlen ("QTDisconnected:")) == 0)
3332 cmd_qtdisconnected (packet
);
3335 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3337 cmd_qtframe (packet
);
3340 else if (strncmp ("QTBuffer:", packet
, strlen ("QTBuffer:")) == 0)
3342 cmd_bigqtbuffer (packet
);
3350 handle_tracepoint_query (char *packet
)
3352 if (strcmp ("qTStatus", packet
) == 0)
3354 cmd_qtstatus (packet
);
3357 else if (strcmp ("qTfP", packet
) == 0)
3362 else if (strcmp ("qTsP", packet
) == 0)
3367 else if (strcmp ("qTfV", packet
) == 0)
3372 else if (strcmp ("qTsV", packet
) == 0)
3377 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
3382 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
3384 cmd_qtbuffer (packet
);
3392 #ifndef IN_PROCESS_AGENT
3394 /* Call this when thread TINFO has hit the tracepoint defined by
3395 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3396 action. This adds a while-stepping collecting state item to the
3397 threads' collecting state list, so that we can keep track of
3398 multiple simultaneous while-stepping actions being collected by the
3399 same thread. This can happen in cases like:
3401 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3403 ff0003 INSN3 <-- TP2, collect $regs
3404 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3407 Notice that when instruction INSN5 is reached, the while-stepping
3408 actions of both TP1 and TP3 are still being collected, and that TP2
3409 had been collected meanwhile. The whole range of ff0001-ff0005
3410 should be single-stepped, due to at least TP1's while-stepping
3411 action covering the whole range. */
3414 add_while_stepping_state (struct thread_info
*tinfo
,
3415 int tp_number
, CORE_ADDR tp_address
)
3417 struct wstep_state
*wstep
;
3419 wstep
= xmalloc (sizeof (*wstep
));
3420 wstep
->next
= tinfo
->while_stepping
;
3422 wstep
->tp_number
= tp_number
;
3423 wstep
->tp_address
= tp_address
;
3424 wstep
->current_step
= 0;
3426 tinfo
->while_stepping
= wstep
;
3429 /* Release the while-stepping collecting state WSTEP. */
3432 release_while_stepping_state (struct wstep_state
*wstep
)
3437 /* Release all while-stepping collecting states currently associated
3438 with thread TINFO. */
3441 release_while_stepping_state_list (struct thread_info
*tinfo
)
3443 struct wstep_state
*head
;
3445 while (tinfo
->while_stepping
)
3447 head
= tinfo
->while_stepping
;
3448 tinfo
->while_stepping
= head
->next
;
3449 release_while_stepping_state (head
);
3453 /* If TINFO was handling a 'while-stepping' action, the step has
3454 finished, so collect any step data needed, and check if any more
3455 steps are required. Return true if the thread was indeed
3456 collecting tracepoint data, false otherwise. */
3459 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3461 struct tracepoint
*tpoint
;
3462 struct wstep_state
*wstep
;
3463 struct wstep_state
**wstep_link
;
3464 struct trap_tracepoint_ctx ctx
;
3466 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
3468 if (in_process_agent_loaded ())
3469 upload_fast_traceframes ();
3471 /* Check if we were indeed collecting data for one of more
3472 tracepoints with a 'while-stepping' count. */
3473 if (tinfo
->while_stepping
== NULL
)
3478 /* We're not even tracing anymore. Stop this thread from
3480 release_while_stepping_state_list (tinfo
);
3482 /* The thread had stopped due to a single-step request indeed
3483 explained by a tracepoint. */
3487 wstep
= tinfo
->while_stepping
;
3488 wstep_link
= &tinfo
->while_stepping
;
3490 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
3491 target_pid_to_str (tinfo
->entry
.id
),
3492 wstep
->tp_number
, paddress (wstep
->tp_address
));
3494 ctx
.base
.type
= trap_tracepoint
;
3495 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3497 while (wstep
!= NULL
)
3499 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
3502 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
3503 wstep
->tp_number
, paddress (wstep
->tp_address
),
3504 target_pid_to_str (tinfo
->entry
.id
));
3507 *wstep_link
= wstep
->next
;
3508 release_while_stepping_state (wstep
);
3512 /* We've just finished one step. */
3513 ++wstep
->current_step
;
3516 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
3517 stop_pc
, tpoint
, wstep
->current_step
);
3519 if (wstep
->current_step
>= tpoint
->step_count
)
3521 /* The requested numbers of steps have occurred. */
3522 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
3523 target_pid_to_str (tinfo
->entry
.id
),
3524 wstep
->tp_number
, paddress (wstep
->tp_address
));
3526 /* Unlink the wstep. */
3527 *wstep_link
= wstep
->next
;
3528 release_while_stepping_state (wstep
);
3529 wstep
= *wstep_link
;
3531 /* Only check the hit count now, which ensure that we do all
3532 our stepping before stopping the run. */
3533 if (tpoint
->pass_count
> 0
3534 && tpoint
->hit_count
>= tpoint
->pass_count
3535 && stopping_tracepoint
== NULL
)
3536 stopping_tracepoint
= tpoint
;
3540 /* Keep single-stepping until the requested numbers of steps
3542 wstep_link
= &wstep
->next
;
3543 wstep
= *wstep_link
;
3546 if (stopping_tracepoint
3547 || trace_buffer_is_full
3548 || expr_eval_result
!= expr_eval_no_error
)
3558 /* Handle any internal tracing control breakpoint hits. That means,
3559 pull traceframes from the IPA to our buffer, and syncing both
3560 tracing agents when the IPA's tracing stops for some reason. */
3563 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3565 /* Pull in fast tracepoint trace frames from the inferior in-process
3566 agent's buffer into our buffer. */
3568 if (!in_process_agent_loaded ())
3571 upload_fast_traceframes ();
3573 /* Check if the in-process agent had decided we should stop
3575 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
3577 int ipa_trace_buffer_is_full
;
3578 CORE_ADDR ipa_stopping_tracepoint
;
3579 int ipa_expr_eval_result
;
3580 CORE_ADDR ipa_error_tracepoint
;
3582 trace_debug ("lib stopped at stop_tracing");
3584 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
3585 &ipa_trace_buffer_is_full
);
3587 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3588 &ipa_stopping_tracepoint
);
3589 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
3591 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
3592 &ipa_error_tracepoint
);
3593 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
3595 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
3596 &ipa_expr_eval_result
);
3597 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
3599 trace_debug ("lib: trace_buffer_is_full: %d, "
3600 "stopping_tracepoint: %s, "
3601 "ipa_expr_eval_result: %d, "
3602 "error_tracepoint: %s, ",
3603 ipa_trace_buffer_is_full
,
3604 paddress (ipa_stopping_tracepoint
),
3605 ipa_expr_eval_result
,
3606 paddress (ipa_error_tracepoint
));
3610 if (ipa_trace_buffer_is_full
)
3611 trace_debug ("lib stopped due to full buffer.");
3612 if (ipa_stopping_tracepoint
)
3613 trace_debug ("lib stopped due to tpoint");
3614 if (ipa_stopping_tracepoint
)
3615 trace_debug ("lib stopped due to error");
3618 if (ipa_stopping_tracepoint
!= 0)
3621 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
3623 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
3625 expr_eval_result
= ipa_expr_eval_result
;
3627 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
3632 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
3634 trace_debug ("lib stopped at flush_trace_buffer");
3641 /* Return true if TINFO just hit a tracepoint. Collect data if
3645 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3647 struct tracepoint
*tpoint
;
3649 struct trap_tracepoint_ctx ctx
;
3651 /* Not tracing, don't handle. */
3655 ctx
.base
.type
= trap_tracepoint
;
3656 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3658 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3660 /* Note that we collect fast tracepoints here as well. We'll
3661 step over the fast tracepoint jump later, which avoids the
3663 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
)
3665 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
3666 target_pid_to_str (tinfo
->entry
.id
),
3667 tpoint
->number
, paddress (tpoint
->address
));
3669 /* Test the condition if present, and collect if true. */
3671 || (condition_true_at_tracepoint
3672 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
3673 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
3676 if (stopping_tracepoint
3677 || trace_buffer_is_full
3678 || expr_eval_result
!= expr_eval_no_error
)
3682 /* If the tracepoint had a 'while-stepping' action, then set
3683 the thread to collect this tracepoint on the following
3685 else if (tpoint
->step_count
> 0)
3687 add_while_stepping_state (tinfo
,
3688 tpoint
->number
, tpoint
->address
);
3700 /* Create a trace frame for the hit of the given tracepoint in the
3704 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
3705 struct tracepoint
*tpoint
)
3707 struct traceframe
*tframe
;
3710 /* Only count it as a hit when we actually collect data. */
3711 tpoint
->hit_count
++;
3713 /* If we've exceeded a defined pass count, record the event for
3714 later, and finish the collection for this hit. This test is only
3715 for nonstepping tracepoints, stepping tracepoints test at the end
3716 of their while-stepping loop. */
3717 if (tpoint
->pass_count
> 0
3718 && tpoint
->hit_count
>= tpoint
->pass_count
3719 && tpoint
->step_count
== 0
3720 && stopping_tracepoint
== NULL
)
3721 stopping_tracepoint
= tpoint
;
3723 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
3724 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
3726 tframe
= add_traceframe (tpoint
);
3730 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
3732 #ifndef IN_PROCESS_AGENT
3733 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
3734 tpoint
->number
, paddress (tpoint
->address
),
3735 tpoint
->actions_str
[acti
]);
3738 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
3739 tpoint
->actions
[acti
]);
3742 finish_traceframe (tframe
);
3745 if (tframe
== NULL
&& tracing
)
3746 trace_buffer_is_full
= 1;
3749 #ifndef IN_PROCESS_AGENT
3752 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
3754 struct tracepoint
*tpoint
, int current_step
)
3756 struct traceframe
*tframe
;
3759 trace_debug ("Making new step traceframe for "
3760 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
3761 tpoint
->number
, paddress (tpoint
->address
),
3762 current_step
, tpoint
->step_count
,
3765 tframe
= add_traceframe (tpoint
);
3769 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
3771 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
3772 tpoint
->number
, paddress (tpoint
->address
),
3773 tpoint
->step_actions_str
[acti
]);
3775 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
3776 tpoint
->step_actions
[acti
]);
3779 finish_traceframe (tframe
);
3782 if (tframe
== NULL
&& tracing
)
3783 trace_buffer_is_full
= 1;
3788 static struct regcache
*
3789 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
3791 struct regcache
*regcache
= NULL
;
3793 #ifdef IN_PROCESS_AGENT
3794 if (ctx
->type
== fast_tracepoint
)
3796 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
3797 if (!fctx
->regcache_initted
)
3799 fctx
->regcache_initted
= 1;
3800 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
3801 supply_regblock (&fctx
->regcache
, NULL
);
3802 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
3804 regcache
= &fctx
->regcache
;
3807 if (ctx
->type
== trap_tracepoint
)
3809 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
3810 regcache
= tctx
->regcache
;
3814 gdb_assert (regcache
!= NULL
);
3820 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
3822 struct tracepoint
*tpoint
,
3823 struct traceframe
*tframe
,
3824 struct tracepoint_action
*taction
)
3826 enum eval_result_type err
;
3828 switch (taction
->type
)
3832 struct collect_memory_action
*maction
;
3834 maction
= (struct collect_memory_action
*) taction
;
3836 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
3837 pulongest (maction
->len
),
3838 paddress (maction
->addr
), maction
->basereg
);
3839 /* (should use basereg) */
3840 agent_mem_read (tframe
, NULL
,
3841 (CORE_ADDR
) maction
->addr
, maction
->len
);
3846 struct collect_registers_action
*raction
;
3848 unsigned char *regspace
;
3849 struct regcache tregcache
;
3850 struct regcache
*context_regcache
;
3852 raction
= (struct collect_registers_action
*) taction
;
3854 trace_debug ("Want to collect registers");
3856 /* Collect all registers for now. */
3857 regspace
= add_traceframe_block (tframe
,
3858 1 + register_cache_size ());
3859 if (regspace
== NULL
)
3861 trace_debug ("Trace buffer block allocation failed, skipping");
3864 /* Identify a register block. */
3867 context_regcache
= get_context_regcache (ctx
);
3869 /* Wrap the regblock in a register cache (in the stack, we
3870 don't want to malloc here). */
3871 init_register_cache (&tregcache
, regspace
+ 1);
3873 /* Copy the register data to the regblock. */
3874 regcache_cpy (&tregcache
, context_regcache
);
3876 #ifndef IN_PROCESS_AGENT
3877 /* On some platforms, trap-based tracepoints will have the PC
3878 pointing to the next instruction after the trap, but we
3879 don't want the user or GDB trying to guess whether the
3880 saved PC needs adjusting; so always record the adjusted
3881 stop_pc. Note that we can't use tpoint->address instead,
3882 since it will be wrong for while-stepping actions. This
3883 adjustment is a nop for fast tracepoints collected from the
3884 in-process lib (but not if GDBserver is collecting one
3885 preemptively), since the PC had already been adjusted to
3886 contain the tracepoint's address by the jump pad. */
3887 trace_debug ("Storing stop pc (0x%s) in regblock",
3888 paddress (tpoint
->address
));
3890 /* This changes the regblock, not the thread's
3892 regcache_write_pc (&tregcache
, stop_pc
);
3898 struct eval_expr_action
*eaction
;
3900 eaction
= (struct eval_expr_action
*) taction
;
3902 trace_debug ("Want to evaluate expression");
3904 err
= eval_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
3906 if (err
!= expr_eval_no_error
)
3908 record_tracepoint_error (tpoint
, "action expression", err
);
3914 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
3920 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
3921 struct tracepoint
*tpoint
)
3924 enum eval_result_type err
;
3926 /* Presently, gdbserver doesn't run compiled conditions, only the
3927 IPA does. If the program stops at a fast tracepoint's address
3928 (e.g., due to a breakpoint, trap tracepoint, or stepping),
3929 gdbserver preemptively collect the fast tracepoint. Later, on
3930 resume, gdbserver steps over the fast tracepoint like it steps
3931 over breakpoints, so that the IPA doesn't see that fast
3932 tracepoint. This avoids double collects of fast tracepoints in
3933 that stopping scenario. Having gdbserver itself handle the fast
3934 tracepoint gives the user a consistent view of when fast or trap
3935 tracepoints are collected, compared to an alternative where only
3936 trap tracepoints are collected on stop, and fast tracepoints on
3937 resume. When a fast tracepoint is being processed by gdbserver,
3938 it is always the non-compiled condition expression that is
3940 #ifdef IN_PROCESS_AGENT
3941 if (tpoint
->compiled_cond
)
3942 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
3945 err
= eval_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
3947 if (err
!= expr_eval_no_error
)
3949 record_tracepoint_error (tpoint
, "condition", err
);
3950 /* The error case must return false. */
3954 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
3955 tpoint
->number
, paddress (tpoint
->address
),
3957 return (value
? 1 : 0);
3960 #ifndef IN_PROCESS_AGENT
3962 /* The packet form of an agent expression consists of an 'X', number
3963 of bytes in expression, a comma, and then the bytes. */
3965 static struct agent_expr
*
3966 parse_agent_expr (char **actparm
)
3968 char *act
= *actparm
;
3970 struct agent_expr
*aexpr
;
3972 ++act
; /* skip the X */
3973 act
= unpack_varlen_hex (act
, &xlen
);
3974 ++act
; /* skip a comma */
3975 aexpr
= xmalloc (sizeof (struct agent_expr
));
3976 aexpr
->length
= xlen
;
3977 aexpr
->bytes
= xmalloc (xlen
);
3978 convert_ascii_to_int (act
, aexpr
->bytes
, xlen
);
3979 *actparm
= act
+ (xlen
* 2);
3983 /* Convert the bytes of an agent expression back into hex digits, so
3984 they can be printed or uploaded. This allocates the buffer,
3985 callers should free when they are done with it. */
3988 unparse_agent_expr (struct agent_expr
*aexpr
)
3992 rslt
= xmalloc (2 * aexpr
->length
+ 1);
3993 convert_int_to_ascii (aexpr
->bytes
, rslt
, aexpr
->length
);
3999 /* The agent expression evaluator, as specified by the GDB docs. It
4000 returns 0 if everything went OK, and a nonzero error code
4003 static enum eval_result_type
4004 eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4005 struct traceframe
*tframe
,
4006 struct agent_expr
*aexpr
,
4010 #define STACK_MAX 100
4011 ULONGEST stack
[STACK_MAX
], top
;
4016 /* This union is a convenient way to convert representations. For
4017 now, assume a standard architecture where the hardware integer
4018 types have 8, 16, 32, 64 bit types. A more robust solution would
4019 be to import stdint.h from gnulib. */
4024 unsigned char bytes
[1];
4029 unsigned char bytes
[2];
4034 unsigned char bytes
[4];
4039 unsigned char bytes
[8];
4044 if (aexpr
->length
== 0)
4046 trace_debug ("empty agent expression");
4047 return expr_eval_empty_expression
;
4050 /* Cache the stack top in its own variable. Much of the time we can
4051 operate on this variable, rather than dinking with the stack. It
4052 needs to be copied to the stack when sp changes. */
4057 op
= aexpr
->bytes
[pc
++];
4059 trace_debug ("About to interpret byte 0x%x", op
);
4063 case gdb_agent_op_add
:
4067 case gdb_agent_op_sub
:
4068 top
= stack
[--sp
] - top
;
4071 case gdb_agent_op_mul
:
4075 case gdb_agent_op_div_signed
:
4078 trace_debug ("Attempted to divide by zero");
4079 return expr_eval_divide_by_zero
;
4081 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
4084 case gdb_agent_op_div_unsigned
:
4087 trace_debug ("Attempted to divide by zero");
4088 return expr_eval_divide_by_zero
;
4090 top
= stack
[--sp
] / top
;
4093 case gdb_agent_op_rem_signed
:
4096 trace_debug ("Attempted to divide by zero");
4097 return expr_eval_divide_by_zero
;
4099 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
4102 case gdb_agent_op_rem_unsigned
:
4105 trace_debug ("Attempted to divide by zero");
4106 return expr_eval_divide_by_zero
;
4108 top
= stack
[--sp
] % top
;
4111 case gdb_agent_op_lsh
:
4112 top
= stack
[--sp
] << top
;
4115 case gdb_agent_op_rsh_signed
:
4116 top
= ((LONGEST
) stack
[--sp
]) >> top
;
4119 case gdb_agent_op_rsh_unsigned
:
4120 top
= stack
[--sp
] >> top
;
4123 case gdb_agent_op_trace
:
4124 agent_mem_read (tframe
,
4125 NULL
, (CORE_ADDR
) stack
[--sp
], (ULONGEST
) top
);
4130 case gdb_agent_op_trace_quick
:
4131 arg
= aexpr
->bytes
[pc
++];
4132 agent_mem_read (tframe
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
4135 case gdb_agent_op_log_not
:
4139 case gdb_agent_op_bit_and
:
4143 case gdb_agent_op_bit_or
:
4147 case gdb_agent_op_bit_xor
:
4151 case gdb_agent_op_bit_not
:
4155 case gdb_agent_op_equal
:
4156 top
= (stack
[--sp
] == top
);
4159 case gdb_agent_op_less_signed
:
4160 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
4163 case gdb_agent_op_less_unsigned
:
4164 top
= (stack
[--sp
] < top
);
4167 case gdb_agent_op_ext
:
4168 arg
= aexpr
->bytes
[pc
++];
4169 if (arg
< (sizeof (LONGEST
) * 8))
4171 LONGEST mask
= 1 << (arg
- 1);
4172 top
&= ((LONGEST
) 1 << arg
) - 1;
4173 top
= (top
^ mask
) - mask
;
4177 case gdb_agent_op_ref8
:
4178 agent_mem_read (tframe
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
4182 case gdb_agent_op_ref16
:
4183 agent_mem_read (tframe
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
4187 case gdb_agent_op_ref32
:
4188 agent_mem_read (tframe
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
4192 case gdb_agent_op_ref64
:
4193 agent_mem_read (tframe
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
4197 case gdb_agent_op_if_goto
:
4199 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4206 case gdb_agent_op_goto
:
4207 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4210 case gdb_agent_op_const8
:
4211 /* Flush the cached stack top. */
4213 top
= aexpr
->bytes
[pc
++];
4216 case gdb_agent_op_const16
:
4217 /* Flush the cached stack top. */
4219 top
= aexpr
->bytes
[pc
++];
4220 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4223 case gdb_agent_op_const32
:
4224 /* Flush the cached stack top. */
4226 top
= aexpr
->bytes
[pc
++];
4227 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4228 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4229 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4232 case gdb_agent_op_const64
:
4233 /* Flush the cached stack top. */
4235 top
= aexpr
->bytes
[pc
++];
4236 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4237 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4238 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4239 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4240 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4241 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4242 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4245 case gdb_agent_op_reg
:
4246 /* Flush the cached stack top. */
4248 arg
= aexpr
->bytes
[pc
++];
4249 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4252 struct regcache
*regcache
;
4254 regcache
= get_context_regcache (ctx
);
4256 switch (register_size (regnum
))
4259 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
4263 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
4267 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
4271 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
4275 internal_error (__FILE__
, __LINE__
,
4276 "unhandled register size");
4281 case gdb_agent_op_end
:
4282 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4283 sp
, pulongest (top
));
4288 /* This should be an error */
4289 trace_debug ("Stack is empty, nothing to return");
4290 return expr_eval_empty_stack
;
4294 return expr_eval_no_error
;
4296 case gdb_agent_op_dup
:
4300 case gdb_agent_op_pop
:
4305 case gdb_agent_op_zero_ext
:
4306 arg
= aexpr
->bytes
[pc
++];
4307 if (arg
< (sizeof (LONGEST
) * 8))
4308 top
&= ((LONGEST
) 1 << arg
) - 1;
4311 case gdb_agent_op_swap
:
4312 /* Interchange top two stack elements, making sure top gets
4313 copied back onto stack. */
4315 top
= stack
[sp
- 1];
4316 stack
[sp
- 1] = stack
[sp
];
4319 case gdb_agent_op_getv
:
4320 /* Flush the cached stack top. */
4322 arg
= aexpr
->bytes
[pc
++];
4323 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4324 top
= get_trace_state_variable_value (arg
);
4327 case gdb_agent_op_setv
:
4328 arg
= aexpr
->bytes
[pc
++];
4329 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4330 set_trace_state_variable_value (arg
, top
);
4331 /* Note that we leave the value on the stack, for the
4332 benefit of later/enclosing expressions. */
4335 case gdb_agent_op_tracev
:
4336 arg
= aexpr
->bytes
[pc
++];
4337 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4338 agent_tsv_read (tframe
, arg
);
4341 /* GDB never (currently) generates any of these ops. */
4342 case gdb_agent_op_float
:
4343 case gdb_agent_op_ref_float
:
4344 case gdb_agent_op_ref_double
:
4345 case gdb_agent_op_ref_long_double
:
4346 case gdb_agent_op_l_to_d
:
4347 case gdb_agent_op_d_to_l
:
4348 case gdb_agent_op_trace16
:
4349 trace_debug ("Agent expression op 0x%x valid, but not handled",
4351 /* If ever GDB generates any of these, we don't have the
4352 option of ignoring. */
4356 trace_debug ("Agent expression op 0x%x not recognized", op
);
4357 /* Don't struggle on, things will just get worse. */
4358 return expr_eval_unrecognized_opcode
;
4361 /* Check for stack badness. */
4362 if (sp
>= (STACK_MAX
- 1))
4364 trace_debug ("Expression stack overflow");
4365 return expr_eval_stack_overflow
;
4370 trace_debug ("Expression stack underflow");
4371 return expr_eval_stack_underflow
;
4374 trace_debug ("Op %s -> sp=%d, top=0x%s",
4375 gdb_agent_op_names
[op
], sp
, pulongest (top
));
4379 /* Do memory copies for bytecodes. */
4380 /* Do the recording of memory blocks for actions and bytecodes. */
4383 agent_mem_read (struct traceframe
*tframe
,
4384 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4386 unsigned char *mspace
;
4387 ULONGEST remaining
= len
;
4388 unsigned short blocklen
;
4390 /* If a 'to' buffer is specified, use it. */
4393 read_inferior_memory (from
, to
, len
);
4397 /* Otherwise, create a new memory block in the trace buffer. */
4398 while (remaining
> 0)
4402 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4403 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4404 mspace
= add_traceframe_block (tframe
, sp
);
4407 /* Identify block as a memory block. */
4410 /* Record address and size. */
4411 memcpy (mspace
, &from
, sizeof (from
));
4412 mspace
+= sizeof (from
);
4413 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4414 mspace
+= sizeof (blocklen
);
4415 /* Record the memory block proper. */
4416 read_inferior_memory (from
, mspace
, blocklen
);
4417 trace_debug ("%d bytes recorded", blocklen
);
4418 remaining
-= blocklen
;
4424 /* Record the value of a trace state variable. */
4427 agent_tsv_read (struct traceframe
*tframe
, int n
)
4429 unsigned char *vspace
;
4432 vspace
= add_traceframe_block (tframe
,
4433 1 + sizeof (n
) + sizeof (LONGEST
));
4436 /* Identify block as a variable. */
4438 /* Record variable's number and value. */
4439 memcpy (vspace
+ 1, &n
, sizeof (n
));
4440 val
= get_trace_state_variable_value (n
);
4441 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4442 trace_debug ("Variable %d recorded", n
);
4446 #ifndef IN_PROCESS_AGENT
4448 static unsigned char *
4449 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4450 int tfnum
, char type_wanted
)
4452 unsigned char *dataptr
;
4456 trace_debug ("traceframe %d has no data", tfnum
);
4460 /* Iterate through a traceframe's blocks, looking for a block of the
4462 for (dataptr
= database
;
4463 dataptr
< database
+ datasize
;
4467 unsigned short mlen
;
4469 if (dataptr
== trace_buffer_wrap
)
4471 /* Adjust to reflect wrapping part of the frame around to
4473 datasize
= dataptr
- database
;
4474 dataptr
= database
= trace_buffer_lo
;
4476 blocktype
= *dataptr
++;
4478 if (type_wanted
== blocktype
)
4484 /* Skip over the registers block. */
4485 dataptr
+= register_cache_size ();
4488 /* Skip over the memory block. */
4489 dataptr
+= sizeof (CORE_ADDR
);
4490 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4491 dataptr
+= (sizeof (mlen
) + mlen
);
4494 /* Skip over the TSV block. */
4495 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4498 trace_debug ("traceframe %d has unknown block type 0x%x",
4507 static unsigned char *
4508 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4510 unsigned char *regblock
;
4512 regblock
= traceframe_find_block_type (tframe
->data
,
4516 if (regblock
== NULL
)
4517 trace_debug ("traceframe %d has no register data", tfnum
);
4522 /* Get registers from a traceframe. */
4525 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4527 unsigned char *dataptr
;
4528 struct tracepoint
*tpoint
;
4529 struct traceframe
*tframe
;
4531 tframe
= find_traceframe (tfnum
);
4535 trace_debug ("traceframe %d not found", tfnum
);
4539 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4540 if (dataptr
== NULL
)
4542 /* We don't like making up numbers, but GDB has all manner of
4543 troubles when the target says there are no registers. */
4544 supply_regblock (regcache
, NULL
);
4546 /* We can generally guess at a PC, although this will be
4547 misleading for while-stepping frames and multi-location
4549 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4551 regcache_write_pc (regcache
, tpoint
->address
);
4554 supply_regblock (regcache
, dataptr
);
4560 traceframe_get_pc (struct traceframe
*tframe
)
4562 struct regcache regcache
;
4563 unsigned char *dataptr
;
4565 dataptr
= traceframe_find_regblock (tframe
, -1);
4566 if (dataptr
== NULL
)
4569 init_register_cache (®cache
, dataptr
);
4570 return regcache_read_pc (®cache
);
4573 /* Read a requested block of memory from a trace frame. */
4576 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
4577 unsigned char *buf
, ULONGEST length
,
4580 struct traceframe
*tframe
;
4581 unsigned char *database
, *dataptr
;
4582 unsigned int datasize
;
4584 unsigned short mlen
;
4586 trace_debug ("traceframe_read_mem");
4588 tframe
= find_traceframe (tfnum
);
4592 trace_debug ("traceframe %d not found", tfnum
);
4596 datasize
= tframe
->data_size
;
4597 database
= dataptr
= &tframe
->data
[0];
4599 /* Iterate through a traceframe's blocks, looking for memory. */
4600 while ((dataptr
= traceframe_find_block_type (dataptr
,
4601 datasize
- (dataptr
- database
),
4602 tfnum
, 'M')) != NULL
)
4604 memcpy (&maddr
, dataptr
, sizeof (maddr
));
4605 dataptr
+= sizeof (maddr
);
4606 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4607 dataptr
+= sizeof (mlen
);
4608 trace_debug ("traceframe %d has %d bytes at %s",
4609 tfnum
, mlen
, paddress (maddr
));
4611 /* Check that requested data is in bounds. */
4612 if (maddr
<= addr
&& (addr
+ length
) <= (maddr
+ mlen
))
4614 /* Block includes the requested range, copy it out. */
4615 memcpy (buf
, dataptr
+ (addr
- maddr
), length
);
4620 /* Skip over this block. */
4624 trace_debug ("traceframe %d has no memory data for the desired region",
4632 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
4635 struct traceframe
*tframe
;
4636 unsigned char *database
, *dataptr
;
4637 unsigned int datasize
;
4640 trace_debug ("traceframe_read_tsv");
4642 tfnum
= current_traceframe
;
4646 trace_debug ("no current traceframe");
4650 tframe
= find_traceframe (tfnum
);
4654 trace_debug ("traceframe %d not found", tfnum
);
4658 datasize
= tframe
->data_size
;
4659 database
= dataptr
= &tframe
->data
[0];
4661 /* Iterate through a traceframe's blocks, looking for the tsv. */
4662 while ((dataptr
= traceframe_find_block_type (dataptr
,
4663 datasize
- (dataptr
- database
),
4664 tfnum
, 'V')) != NULL
)
4666 memcpy (&vnum
, dataptr
, sizeof (vnum
));
4667 dataptr
+= sizeof (vnum
);
4669 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
4671 /* Check that this is the variable we want. */
4674 memcpy (val
, dataptr
, sizeof (*val
));
4678 /* Skip over this block. */
4679 dataptr
+= sizeof (LONGEST
);
4682 trace_debug ("traceframe %d has no data for variable %d",
4687 /* Return the first fast tracepoint whose jump pad contains PC. */
4689 static struct tracepoint
*
4690 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
4692 struct tracepoint
*tpoint
;
4694 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4695 if (tpoint
->type
== fast_tracepoint
)
4696 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
4702 /* Return GDBserver's tracepoint that matches the IP Agent's
4703 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
4706 static struct tracepoint
*
4707 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
4709 struct tracepoint
*tpoint
;
4711 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4712 if (tpoint
->type
== fast_tracepoint
)
4713 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
4721 /* The type of the object that is used to synchronize fast tracepoint
4724 typedef struct collecting_t
4726 /* The fast tracepoint number currently collecting. */
4729 /* A number that GDBserver can use to identify the thread that is
4730 presently holding the collect lock. This need not (and usually
4731 is not) the thread id, as getting the current thread ID usually
4732 requires a system call, which we want to avoid like the plague.
4733 Usually this is thread's TCB, found in the TLS (pseudo-)
4734 register, which is readable with a single insn on several
4736 uintptr_t thread_area
;
4739 #ifndef IN_PROCESS_AGENT
4742 force_unlock_trace_buffer (void)
4744 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
4747 /* Check if the thread identified by THREAD_AREA which is stopped at
4748 STOP_PC, is presently locking the fast tracepoint collection, and
4749 if so, gather some status of said collection. Returns 0 if the
4750 thread isn't collecting or in the jump pad at all. 1, if in the
4751 jump pad (or within gdb_collect) and hasn't executed the adjusted
4752 original insn yet (can set a breakpoint there and run to it). 2,
4753 if presently executing the adjusted original insn --- in which
4754 case, if we want to move the thread out of the jump pad, we need to
4755 single-step it until this function returns 0. */
4758 fast_tracepoint_collecting (CORE_ADDR thread_area
,
4760 struct fast_tpoint_collect_status
*status
)
4762 CORE_ADDR ipa_collecting
;
4763 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
4764 struct tracepoint
*tpoint
;
4765 int needs_breakpoint
;
4767 /* The thread THREAD_AREA is either:
4769 0. not collecting at all, not within the jump pad, or within
4770 gdb_collect or one of its callees.
4772 1. in the jump pad and haven't reached gdb_collect
4774 2. within gdb_collect (out of the jump pad) (collect is set)
4776 3. we're in the jump pad, after gdb_collect having returned,
4777 possibly executing the adjusted insns.
4779 For cases 1 and 3, `collecting' may or not be set. The jump pad
4780 doesn't have any complicated jump logic, so we can tell if the
4781 thread is executing the adjust original insn or not by just
4782 matching STOP_PC with known jump pad addresses. If we it isn't
4783 yet executing the original insn, set a breakpoint there, and let
4784 the thread run to it, so to quickly step over a possible (many
4785 insns) gdb_collect call. Otherwise, or when the breakpoint is
4786 hit, only a few (small number of) insns are left to be executed
4787 in the jump pad. Single-step the thread until it leaves the
4792 needs_breakpoint
= 0;
4793 trace_debug ("fast_tracepoint_collecting");
4795 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
4796 &ipa_gdb_jump_pad_buffer
))
4797 fatal ("error extracting `gdb_jump_pad_buffer'");
4798 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
4799 &ipa_gdb_jump_pad_buffer_end
))
4800 fatal ("error extracting `gdb_jump_pad_buffer_end'");
4802 if (ipa_gdb_jump_pad_buffer
<= stop_pc
&& stop_pc
< ipa_gdb_jump_pad_buffer_end
)
4804 /* We can tell which tracepoint(s) the thread is collecting by
4805 matching the jump pad address back to the tracepoint. */
4806 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
4809 warning ("in jump pad, but no matching tpoint?");
4814 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
4816 tpoint
->number
, paddress (tpoint
->address
),
4817 paddress (tpoint
->jump_pad
),
4818 paddress (tpoint
->jump_pad_end
),
4819 paddress (tpoint
->adjusted_insn_addr
),
4820 paddress (tpoint
->adjusted_insn_addr_end
));
4823 /* Definitely in the jump pad. May or may not need
4824 fast-exit-jump-pad breakpoint. */
4825 if (tpoint
->jump_pad
<= stop_pc
4826 && stop_pc
< tpoint
->adjusted_insn_addr
)
4827 needs_breakpoint
= 1;
4831 collecting_t ipa_collecting_obj
;
4833 /* If `collecting' is set/locked, then the THREAD_AREA thread
4834 may or not be the one holding the lock. We have to read the
4835 lock to find out. */
4837 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
4840 trace_debug ("fast_tracepoint_collecting:"
4841 " failed reading 'collecting' in the inferior");
4845 if (!ipa_collecting
)
4847 trace_debug ("fast_tracepoint_collecting: not collecting"
4848 " (and nobody is).");
4852 /* Some thread is collecting. Check which. */
4853 if (read_inferior_memory (ipa_collecting
,
4854 (unsigned char *) &ipa_collecting_obj
,
4855 sizeof (ipa_collecting_obj
)) != 0)
4858 if (ipa_collecting_obj
.thread_area
!= thread_area
)
4860 trace_debug ("fast_tracepoint_collecting: not collecting "
4861 "(another thread is)");
4866 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
4869 warning ("fast_tracepoint_collecting: collecting, "
4870 "but tpoint %s not found?",
4871 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
4875 /* The thread is within `gdb_collect', skip over the rest of
4876 fast tracepoint collection quickly using a breakpoint. */
4877 needs_breakpoint
= 1;
4880 /* The caller wants a bit of status detail. */
4883 status
->tpoint_num
= tpoint
->number
;
4884 status
->tpoint_addr
= tpoint
->address
;
4885 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
4886 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
4889 if (needs_breakpoint
)
4891 /* Hasn't executed the original instruction yet. Set breakpoint
4892 there, and wait till it's hit, then single-step until exiting
4896 fast_tracepoint_collecting, returning continue-until-break at %s",
4897 paddress (tpoint
->adjusted_insn_addr
));
4899 return 1; /* continue */
4903 /* Just single-step until exiting the jump pad. */
4905 trace_debug ("fast_tracepoint_collecting, returning "
4906 "need-single-step (%s-%s)",
4907 paddress (tpoint
->adjusted_insn_addr
),
4908 paddress (tpoint
->adjusted_insn_addr_end
));
4910 return 2; /* single-step */
4916 #ifdef IN_PROCESS_AGENT
4918 /* The global fast tracepoint collect lock. Points to a collecting_t
4919 object built on the stack by the jump pad, if presently locked;
4920 NULL if it isn't locked. Note that this lock *must* be set while
4921 executing any *function other than the jump pad. See
4922 fast_tracepoint_collecting. */
4923 static collecting_t
* ATTR_USED collecting
;
4925 /* This routine, called from the jump pad (in asm) is designed to be
4926 called from the jump pads of fast tracepoints, thus it is on the
4929 IP_AGENT_EXPORT
void ATTR_USED
4930 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
4932 struct fast_tracepoint_ctx ctx
;
4934 /* Don't do anything until the trace run is completely set up. */
4938 ctx
.base
.type
= fast_tracepoint
;
4940 ctx
.regcache_initted
= 0;
4941 ctx
.tpoint
= tpoint
;
4943 /* Wrap the regblock in a register cache (in the stack, we don't
4944 want to malloc here). */
4945 ctx
.regspace
= alloca (register_cache_size ());
4946 if (ctx
.regspace
== NULL
)
4948 trace_debug ("Trace buffer block allocation failed, skipping");
4952 /* Test the condition if present, and collect if true. */
4953 if (tpoint
->cond
== NULL
4954 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4957 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4958 tpoint
->address
, tpoint
);
4960 /* Note that this will cause original insns to be written back
4961 to where we jumped from, but that's OK because we're jumping
4962 back to the next whole instruction. This will go badly if
4963 instruction restoration is not atomic though. */
4964 if (stopping_tracepoint
4965 || trace_buffer_is_full
4966 || expr_eval_result
!= expr_eval_no_error
)
4971 /* If there was a condition and it evaluated to false, the only
4972 way we would stop tracing is if there was an error during
4973 condition expression evaluation. */
4974 if (expr_eval_result
!= expr_eval_no_error
)
4981 #ifndef IN_PROCESS_AGENT
4983 /* Bytecode compilation. */
4985 CORE_ADDR current_insn_ptr
;
4989 struct bytecode_address
4994 /* Offset and size of field to be modified in the goto block. */
4995 int from_offset
, from_size
;
4996 struct bytecode_address
*next
;
4997 } *bytecode_address_table
;
5000 get_raw_reg_func_addr (void)
5002 return ipa_sym_addrs
.addr_get_raw_reg
;
5006 emit_prologue (void)
5008 target_emit_ops ()->emit_prologue ();
5012 emit_epilogue (void)
5014 target_emit_ops ()->emit_epilogue ();
5020 target_emit_ops ()->emit_add ();
5026 target_emit_ops ()->emit_sub ();
5032 target_emit_ops ()->emit_mul ();
5038 target_emit_ops ()->emit_lsh ();
5042 emit_rsh_signed (void)
5044 target_emit_ops ()->emit_rsh_signed ();
5048 emit_rsh_unsigned (void)
5050 target_emit_ops ()->emit_rsh_unsigned ();
5056 target_emit_ops ()->emit_ext (arg
);
5062 target_emit_ops ()->emit_log_not ();
5068 target_emit_ops ()->emit_bit_and ();
5074 target_emit_ops ()->emit_bit_or ();
5080 target_emit_ops ()->emit_bit_xor ();
5086 target_emit_ops ()->emit_bit_not ();
5092 target_emit_ops ()->emit_equal ();
5096 emit_less_signed (void)
5098 target_emit_ops ()->emit_less_signed ();
5102 emit_less_unsigned (void)
5104 target_emit_ops ()->emit_less_unsigned ();
5110 target_emit_ops ()->emit_ref (size
);
5114 emit_if_goto (int *offset_p
, int *size_p
)
5116 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
5120 emit_goto (int *offset_p
, int *size_p
)
5122 target_emit_ops ()->emit_goto (offset_p
, size_p
);
5126 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
5128 target_emit_ops ()->write_goto_address (from
, to
, size
);
5132 emit_const (LONGEST num
)
5134 target_emit_ops ()->emit_const (num
);
5140 target_emit_ops ()->emit_reg (reg
);
5146 target_emit_ops ()->emit_pop ();
5150 emit_stack_flush (void)
5152 target_emit_ops ()->emit_stack_flush ();
5156 emit_zero_ext (int arg
)
5158 target_emit_ops ()->emit_zero_ext (arg
);
5164 target_emit_ops ()->emit_swap ();
5168 emit_stack_adjust (int n
)
5170 target_emit_ops ()->emit_stack_adjust (n
);
5173 /* FN's prototype is `LONGEST(*fn)(int)'. */
5176 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
5178 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
5181 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
5184 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
5186 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
5189 static enum eval_result_type
compile_bytecodes (struct agent_expr
*aexpr
);
5192 compile_tracepoint_condition (struct tracepoint
*tpoint
, CORE_ADDR
*jump_entry
)
5194 CORE_ADDR entry_point
= *jump_entry
;
5195 enum eval_result_type err
;
5197 trace_debug ("Starting condition compilation for tracepoint %d\n",
5200 /* Initialize the global pointer to the code being built. */
5201 current_insn_ptr
= *jump_entry
;
5205 err
= compile_bytecodes (tpoint
->cond
);
5207 if (err
== expr_eval_no_error
)
5211 /* Record the beginning of the compiled code. */
5212 tpoint
->compiled_cond
= entry_point
;
5214 trace_debug ("Condition compilation for tracepoint %d complete\n",
5219 /* Leave the unfinished code in situ, but don't point to it. */
5221 tpoint
->compiled_cond
= 0;
5223 trace_debug ("Condition compilation for tracepoint %d failed, "
5225 tpoint
->number
, err
);
5228 /* Update the code pointer passed in. Note that we do this even if
5229 the compile fails, so that we can look at the partial results
5230 instead of letting them be overwritten. */
5231 *jump_entry
= current_insn_ptr
;
5233 /* Leave a gap, to aid dump decipherment. */
5237 /* Given an agent expression, turn it into native code. */
5239 static enum eval_result_type
5240 compile_bytecodes (struct agent_expr
*aexpr
)
5246 /* This is only used to build 64-bit value for constants. */
5248 struct bytecode_address
*aentry
, *aentry2
;
5253 trace_debug ("Cannot compile op 0x%x\n", op); \
5254 return expr_eval_unhandled_opcode; \
5257 if (aexpr
->length
== 0)
5259 trace_debug ("empty agent expression\n");
5260 return expr_eval_empty_expression
;
5263 bytecode_address_table
= NULL
;
5267 op
= aexpr
->bytes
[pc
];
5269 trace_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
5271 /* Record the compiled-code address of the bytecode, for use by
5272 jump instructions. */
5273 aentry
= xmalloc (sizeof (struct bytecode_address
));
5275 aentry
->address
= current_insn_ptr
;
5276 aentry
->goto_pc
= -1;
5277 aentry
->from_offset
= aentry
->from_size
= 0;
5278 aentry
->next
= bytecode_address_table
;
5279 bytecode_address_table
= aentry
;
5287 case gdb_agent_op_add
:
5291 case gdb_agent_op_sub
:
5295 case gdb_agent_op_mul
:
5299 case gdb_agent_op_div_signed
:
5303 case gdb_agent_op_div_unsigned
:
5307 case gdb_agent_op_rem_signed
:
5311 case gdb_agent_op_rem_unsigned
:
5315 case gdb_agent_op_lsh
:
5319 case gdb_agent_op_rsh_signed
:
5323 case gdb_agent_op_rsh_unsigned
:
5324 emit_rsh_unsigned ();
5327 case gdb_agent_op_trace
:
5331 case gdb_agent_op_trace_quick
:
5335 case gdb_agent_op_log_not
:
5339 case gdb_agent_op_bit_and
:
5343 case gdb_agent_op_bit_or
:
5347 case gdb_agent_op_bit_xor
:
5351 case gdb_agent_op_bit_not
:
5355 case gdb_agent_op_equal
:
5359 case gdb_agent_op_less_signed
:
5360 emit_less_signed ();
5363 case gdb_agent_op_less_unsigned
:
5364 emit_less_unsigned ();
5367 case gdb_agent_op_ext
:
5368 arg
= aexpr
->bytes
[pc
++];
5369 if (arg
< (sizeof (LONGEST
) * 8))
5373 case gdb_agent_op_ref8
:
5377 case gdb_agent_op_ref16
:
5381 case gdb_agent_op_ref32
:
5385 case gdb_agent_op_ref64
:
5389 case gdb_agent_op_if_goto
:
5390 arg
= aexpr
->bytes
[pc
++];
5391 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5392 aentry
->goto_pc
= arg
;
5393 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5396 case gdb_agent_op_goto
:
5397 arg
= aexpr
->bytes
[pc
++];
5398 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5399 aentry
->goto_pc
= arg
;
5400 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5403 case gdb_agent_op_const8
:
5404 emit_stack_flush ();
5405 top
= aexpr
->bytes
[pc
++];
5409 case gdb_agent_op_const16
:
5410 emit_stack_flush ();
5411 top
= aexpr
->bytes
[pc
++];
5412 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5416 case gdb_agent_op_const32
:
5417 emit_stack_flush ();
5418 top
= aexpr
->bytes
[pc
++];
5419 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5420 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5421 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5425 case gdb_agent_op_const64
:
5426 emit_stack_flush ();
5427 top
= aexpr
->bytes
[pc
++];
5428 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5429 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5430 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5431 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5432 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5433 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5434 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5438 case gdb_agent_op_reg
:
5439 emit_stack_flush ();
5440 arg
= aexpr
->bytes
[pc
++];
5441 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5445 case gdb_agent_op_end
:
5446 trace_debug ("At end of expression\n");
5448 /* Assume there is one stack element left, and that it is
5449 cached in "top" where emit_epilogue can get to it. */
5450 emit_stack_adjust (1);
5455 case gdb_agent_op_dup
:
5456 /* In our design, dup is equivalent to stack flushing. */
5457 emit_stack_flush ();
5460 case gdb_agent_op_pop
:
5464 case gdb_agent_op_zero_ext
:
5465 arg
= aexpr
->bytes
[pc
++];
5466 if (arg
< (sizeof (LONGEST
) * 8))
5467 emit_zero_ext (arg
);
5470 case gdb_agent_op_swap
:
5474 case gdb_agent_op_getv
:
5475 emit_stack_flush ();
5476 arg
= aexpr
->bytes
[pc
++];
5477 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5478 emit_int_call_1 (ipa_sym_addrs
.addr_get_trace_state_variable_value
,
5482 case gdb_agent_op_setv
:
5483 arg
= aexpr
->bytes
[pc
++];
5484 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5485 emit_void_call_2 (ipa_sym_addrs
.addr_set_trace_state_variable_value
,
5489 case gdb_agent_op_tracev
:
5493 /* GDB never (currently) generates any of these ops. */
5494 case gdb_agent_op_float
:
5495 case gdb_agent_op_ref_float
:
5496 case gdb_agent_op_ref_double
:
5497 case gdb_agent_op_ref_long_double
:
5498 case gdb_agent_op_l_to_d
:
5499 case gdb_agent_op_d_to_l
:
5500 case gdb_agent_op_trace16
:
5505 trace_debug ("Agent expression op 0x%x not recognized\n", op
);
5506 /* Don't struggle on, things will just get worse. */
5507 return expr_eval_unrecognized_opcode
;
5510 /* This catches errors that occur in target-specific code
5514 trace_debug ("Error %d while emitting code for %s\n",
5515 emit_error
, gdb_agent_op_names
[op
]);
5516 return expr_eval_unhandled_opcode
;
5519 trace_debug ("Op %s compiled\n", gdb_agent_op_names
[op
]);
5522 /* Now fill in real addresses as goto destinations. */
5523 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
5527 if (aentry
->goto_pc
< 0)
5530 /* Find the location that we are going to, and call back into
5531 target-specific code to write the actual address or
5533 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
5535 if (aentry2
->pc
== aentry
->goto_pc
)
5537 trace_debug ("Want to jump from %s to %s\n",
5538 paddress (aentry
->address
),
5539 paddress (aentry2
->address
));
5540 write_goto_address (aentry
->address
+ aentry
->from_offset
,
5541 aentry2
->address
, aentry
->from_size
);
5547 /* Error out if we didn't find a destination. */
5550 trace_debug ("Destination of goto %d not found\n",
5552 return expr_eval_invalid_goto
;
5556 return expr_eval_no_error
;
5559 /* We'll need to adjust these when we consider bi-arch setups, and big
5563 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5565 return write_inferior_memory (where
,
5566 (unsigned char *) &ptr
, sizeof (void *));
5569 /* The base pointer of the IPA's heap. This is the only memory the
5570 IPA is allowed to use. The IPA should _not_ call the inferior's
5571 `malloc' during operation. That'd be slow, and, most importantly,
5572 it may not be safe. We may be collecting a tracepoint in a signal
5573 handler, for example. */
5574 static CORE_ADDR target_tp_heap
;
5576 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5580 target_malloc (ULONGEST size
)
5584 if (target_tp_heap
== 0)
5586 /* We have the pointer *address*, need what it points to. */
5587 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5589 fatal ("could get target heap head pointer");
5592 ptr
= target_tp_heap
;
5593 target_tp_heap
+= size
;
5595 /* Pad to 8-byte alignment. */
5596 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5602 download_agent_expr (struct agent_expr
*expr
)
5604 CORE_ADDR expr_addr
;
5605 CORE_ADDR expr_bytes
;
5607 expr_addr
= target_malloc (sizeof (*expr
));
5608 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5610 expr_bytes
= target_malloc (expr
->length
);
5611 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5613 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5618 /* Align V up to N bits. */
5619 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5622 download_tracepoints (void)
5624 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
5625 struct tracepoint
*tpoint
;
5627 /* Start out empty. */
5628 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
5630 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5632 struct tracepoint target_tracepoint
;
5634 if (tpoint
->type
!= fast_tracepoint
)
5637 /* Maybe download a compiled condition. */
5638 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5640 CORE_ADDR jentry
, jump_entry
;
5642 jentry
= jump_entry
= get_jump_space_head ();
5644 if (tpoint
->cond
!= NULL
)
5646 /* Pad to 8-byte alignment. (needed?) */
5647 /* Actually this should be left for the target to
5649 jentry
= UALIGN (jentry
, 8);
5651 compile_tracepoint_condition (tpoint
, &jentry
);
5654 /* Pad to 8-byte alignment. */
5655 jentry
= UALIGN (jentry
, 8);
5656 claim_jump_space (jentry
- jump_entry
);
5659 target_tracepoint
= *tpoint
;
5662 tpptr
= target_malloc (sizeof (*tpoint
));
5663 tpoint
->obj_addr_on_target
= tpptr
;
5665 if (tpoint
== tracepoints
)
5667 /* First object in list, set the head pointer in the
5669 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
5673 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
5678 /* Write the whole object. We'll fix up its pointers in a bit.
5679 Assume no next for now. This is fixed up above on the next
5680 iteration, if there's any. */
5681 target_tracepoint
.next
= NULL
;
5682 /* Need to clear this here too, since we're downloading the
5683 tracepoints before clearing our own copy. */
5684 target_tracepoint
.hit_count
= 0;
5686 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5687 sizeof (target_tracepoint
));
5690 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5692 download_agent_expr (tpoint
->cond
));
5694 if (tpoint
->numactions
)
5697 CORE_ADDR actions_array
;
5699 /* The pointers array. */
5701 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
5702 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5706 /* Now for each pointer, download the action. */
5707 for (i
= 0; i
< tpoint
->numactions
; i
++)
5709 CORE_ADDR ipa_action
= 0;
5710 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5712 switch (action
->type
)
5716 = target_malloc (sizeof (struct collect_memory_action
));
5717 write_inferior_memory (ipa_action
,
5718 (unsigned char *) action
,
5719 sizeof (struct collect_memory_action
));
5723 = target_malloc (sizeof (struct collect_registers_action
));
5724 write_inferior_memory (ipa_action
,
5725 (unsigned char *) action
,
5726 sizeof (struct collect_registers_action
));
5731 struct eval_expr_action
*eaction
5732 = (struct eval_expr_action
*) action
;
5734 ipa_action
= target_malloc (sizeof (*eaction
));
5735 write_inferior_memory (ipa_action
,
5736 (unsigned char *) eaction
,
5739 expr
= download_agent_expr (eaction
->expr
);
5740 write_inferior_data_ptr
5741 (ipa_action
+ offsetof (struct eval_expr_action
, expr
),
5746 trace_debug ("unknown trace action '%c', ignoring",
5751 if (ipa_action
!= 0)
5752 write_inferior_data_ptr
5753 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
5761 download_trace_state_variables (void)
5763 CORE_ADDR ptr
= 0, prev_ptr
= 0;
5764 struct trace_state_variable
*tsv
;
5766 /* Start out empty. */
5767 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
5769 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
5771 struct trace_state_variable target_tsv
;
5773 /* TSV's with a getter have been initialized equally in both the
5774 inferior and GDBserver. Skip them. */
5775 if (tsv
->getter
!= NULL
)
5781 ptr
= target_malloc (sizeof (*tsv
));
5783 if (tsv
== trace_state_variables
)
5785 /* First object in list, set the head pointer in the
5788 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
5793 write_inferior_data_ptr (prev_ptr
5794 + offsetof (struct trace_state_variable
,
5799 /* Write the whole object. We'll fix up its pointers in a bit.
5800 Assume no next, fixup when needed. */
5801 target_tsv
.next
= NULL
;
5803 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
5804 sizeof (target_tsv
));
5806 if (tsv
->name
!= NULL
)
5808 size_t size
= strlen (tsv
->name
) + 1;
5809 CORE_ADDR name_addr
= target_malloc (size
);
5810 write_inferior_memory (name_addr
,
5811 (unsigned char *) tsv
->name
, size
);
5812 write_inferior_data_ptr (ptr
5813 + offsetof (struct trace_state_variable
,
5818 if (tsv
->getter
!= NULL
)
5820 fatal ("what to do with these?");
5826 /* Fixup the next pointer in the last item in the list. */
5827 write_inferior_data_ptr (prev_ptr
+ offsetof (struct trace_state_variable
,
5832 /* Upload complete trace frames out of the IP Agent's trace buffer
5833 into GDBserver's trace buffer. This always uploads either all or
5834 no trace frames. This is the counter part of
5835 `trace_alloc_trace_buffer'. See its description of the atomic
5836 synching mechanism. */
5839 upload_fast_traceframes (void)
5841 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
5842 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
5844 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
5845 unsigned int curr_tbctrl_idx
;
5846 unsigned int ipa_trace_buffer_ctrl_curr
;
5847 unsigned int ipa_trace_buffer_ctrl_curr_old
;
5848 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
5849 struct breakpoint
*about_to_request_buffer_space_bkpt
;
5850 CORE_ADDR ipa_trace_buffer_lo
;
5851 CORE_ADDR ipa_trace_buffer_hi
;
5853 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
5854 &ipa_traceframe_read_count_racy
))
5856 /* This will happen in most targets if the current thread is
5861 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
5862 &ipa_traceframe_write_count_racy
))
5865 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
5866 ipa_traceframe_write_count_racy
- ipa_traceframe_read_count_racy
,
5867 ipa_traceframe_write_count_racy
, ipa_traceframe_read_count_racy
);
5869 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
5872 about_to_request_buffer_space_bkpt
5873 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
5876 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
5877 &ipa_trace_buffer_ctrl_curr
))
5880 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
5882 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
5885 unsigned int prev
, counter
;
5887 /* Update the token, with new counters, and the GDBserver stamp
5888 bit. Alway reuse the current TBC index. */
5889 prev
= ipa_trace_buffer_ctrl_curr
& 0x0007ff00;
5890 counter
= (prev
+ 0x100) & 0x0007ff00;
5892 ipa_trace_buffer_ctrl_curr
= (0x80000000
5898 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
5899 ipa_trace_buffer_ctrl_curr
))
5902 trace_debug ("Lib: Committed %08x -> %08x",
5903 ipa_trace_buffer_ctrl_curr_old
,
5904 ipa_trace_buffer_ctrl_curr
);
5906 /* Re-read these, now that we've installed the
5907 `about_to_request_buffer_space' breakpoint/lock. A thread could
5908 have finished a traceframe between the last read of these
5909 counters and setting the breakpoint above. If we start
5910 uploading, we never want to leave this function with
5911 traceframe_read_count != 0, otherwise, GDBserver could end up
5912 incrementing the counter tokens more than once (due to event loop
5913 nesting), which would break the IP agent's "effective" detection
5914 (see trace_alloc_trace_buffer). */
5915 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
5916 &ipa_traceframe_read_count
))
5918 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
5919 &ipa_traceframe_write_count
))
5924 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
5925 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
5926 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
5928 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
5929 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
5930 trace_debug ("note that ipa_traceframe_count's parts changed");
5933 /* Get the address of the current TBC object (the IP agent has an
5934 array of 3 such objects). The index is stored in the TBC
5936 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
5937 ipa_trace_buffer_ctrl_addr
5938 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
5940 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
5941 (unsigned char *) &ipa_trace_buffer_ctrl
,
5942 sizeof (struct ipa_trace_buffer_control
)))
5945 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
5946 &ipa_trace_buffer_lo
))
5948 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
5949 &ipa_trace_buffer_hi
))
5952 /* Offsets are easier to grok for debugging than raw addresses,
5953 especially for the small trace buffer sizes that are useful for
5955 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
5956 "endfree=%d wrap=%d hi=%d",
5958 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
5959 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
5960 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
5961 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
5962 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
5964 /* Note that the IPA's buffer is always circular. */
5966 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
5968 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
5969 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
5971 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
5972 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
5973 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
5974 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
5977 tf
= IPA_FIRST_TRACEFRAME ();
5979 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
5981 struct tracepoint
*tpoint
;
5982 struct traceframe
*tframe
;
5983 unsigned char *block
;
5984 struct traceframe ipa_tframe
;
5986 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
5987 offsetof (struct traceframe
, data
)))
5988 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
5990 if (ipa_tframe
.tpnum
== 0)
5991 fatal ("Uploading: No (more) fast traceframes, but "
5992 "ipa_traceframe_count == %u??\n",
5993 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
5995 /* Note that this will be incorrect for multi-location
5997 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
5999 tframe
= add_traceframe (tpoint
);
6002 trace_buffer_is_full
= 1;
6003 trace_debug ("Uploading: trace buffer is full");
6007 /* Copy the whole set of blocks in one go for now. FIXME:
6008 split this in smaller blocks. */
6009 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6012 if (read_inferior_memory (tf
+ offsetof (struct traceframe
, data
),
6013 block
, ipa_tframe
.data_size
))
6014 error ("Uploading: Couldn't read traceframe data at %s\n",
6015 paddress (tf
+ offsetof (struct traceframe
, data
)));
6018 trace_debug ("Uploading: traceframe didn't fit");
6019 finish_traceframe (tframe
);
6022 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6024 /* If we freed the traceframe that wrapped around, go back
6025 to the non-wrap case. */
6026 if (tf
< ipa_trace_buffer_ctrl
.start
)
6028 trace_debug ("Lib: Discarding past the wraparound");
6029 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6031 ipa_trace_buffer_ctrl
.start
= tf
;
6032 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6033 ++ipa_traceframe_read_count
;
6035 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6036 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6038 trace_debug ("Lib: buffer is fully empty. "
6039 "Trace buffer [%d] start=%d free=%d endfree=%d",
6041 (int) (ipa_trace_buffer_ctrl
.start
6042 - ipa_trace_buffer_lo
),
6043 (int) (ipa_trace_buffer_ctrl
.free
6044 - ipa_trace_buffer_lo
),
6045 (int) (ipa_trace_buffer_ctrl
.end_free
6046 - ipa_trace_buffer_lo
));
6048 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6049 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6050 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6051 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6054 trace_debug ("Uploaded a traceframe\n"
6055 "Lib: Trace buffer [%d] start=%d free=%d "
6056 "endfree=%d wrap=%d hi=%d",
6058 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6059 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6060 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6061 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6062 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6065 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6066 (unsigned char *) &ipa_trace_buffer_ctrl
,
6067 sizeof (struct ipa_trace_buffer_control
)))
6070 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6071 ipa_traceframe_read_count
);
6073 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6076 cancel_breakpoints ();
6078 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6079 about_to_request_buffer_space_bkpt
= NULL
;
6083 if (trace_buffer_is_full
)
6088 #ifdef IN_PROCESS_AGENT
6090 #include <sys/mman.h>
6093 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
6094 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
6095 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
6097 static void __attribute__ ((constructor
))
6098 initialize_tracepoint_ftlib (void)
6100 initialize_tracepoint ();
6106 tsv_get_timestamp (void)
6110 if (gettimeofday (&tv
, 0) != 0)
6113 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
6117 initialize_tracepoint (void)
6119 /* There currently no way to change the buffer size. */
6120 const int sizeOfBuffer
= 5 * 1024 * 1024;
6121 unsigned char *buf
= xmalloc (sizeOfBuffer
);
6122 init_trace_buffer (buf
, sizeOfBuffer
);
6124 /* Wire trace state variable 1 to be the timestamp. This will be
6125 uploaded to GDB upon connection and become one of its trace state
6126 variables. (In case you're wondering, if GDB already has a trace
6127 variable numbered 1, it will be renumbered.) */
6128 create_trace_state_variable (1, 0);
6129 set_trace_state_variable_name (1, "trace_timestamp");
6130 set_trace_state_variable_getter (1, tsv_get_timestamp
);
6132 #ifdef IN_PROCESS_AGENT
6135 pagesize
= sysconf (_SC_PAGE_SIZE
);
6139 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
6141 /* Allocate scratch buffer aligned on a page boundary. */
6142 gdb_jump_pad_buffer
= memalign (pagesize
, pagesize
* 20);
6143 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* 20;
6145 /* Make it writable and executable. */
6146 if (mprotect (gdb_jump_pad_buffer
, pagesize
* 20,
6147 PROT_READ
| PROT_WRITE
| PROT_EXEC
) != 0)
6149 initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
6150 gdb_jump_pad_buffer
, pagesize
* 20, strerror (errno
));
6153 initialize_low_tracepoint ();