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 if (tpoint
->compiled_cond
)
3927 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
3929 err
= eval_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
3931 if (err
!= expr_eval_no_error
)
3933 record_tracepoint_error (tpoint
, "condition", err
);
3934 /* The error case must return false. */
3938 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
3939 tpoint
->number
, paddress (tpoint
->address
),
3941 return (value
? 1 : 0);
3944 #ifndef IN_PROCESS_AGENT
3946 /* The packet form of an agent expression consists of an 'X', number
3947 of bytes in expression, a comma, and then the bytes. */
3949 static struct agent_expr
*
3950 parse_agent_expr (char **actparm
)
3952 char *act
= *actparm
;
3954 struct agent_expr
*aexpr
;
3956 ++act
; /* skip the X */
3957 act
= unpack_varlen_hex (act
, &xlen
);
3958 ++act
; /* skip a comma */
3959 aexpr
= xmalloc (sizeof (struct agent_expr
));
3960 aexpr
->length
= xlen
;
3961 aexpr
->bytes
= xmalloc (xlen
);
3962 convert_ascii_to_int (act
, aexpr
->bytes
, xlen
);
3963 *actparm
= act
+ (xlen
* 2);
3967 /* Convert the bytes of an agent expression back into hex digits, so
3968 they can be printed or uploaded. This allocates the buffer,
3969 callers should free when they are done with it. */
3972 unparse_agent_expr (struct agent_expr
*aexpr
)
3976 rslt
= xmalloc (2 * aexpr
->length
+ 1);
3977 convert_int_to_ascii (aexpr
->bytes
, rslt
, aexpr
->length
);
3983 /* The agent expression evaluator, as specified by the GDB docs. It
3984 returns 0 if everything went OK, and a nonzero error code
3987 static enum eval_result_type
3988 eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
3989 struct traceframe
*tframe
,
3990 struct agent_expr
*aexpr
,
3994 #define STACK_MAX 100
3995 ULONGEST stack
[STACK_MAX
], top
;
4000 /* This union is a convenient way to convert representations. For
4001 now, assume a standard architecture where the hardware integer
4002 types have 8, 16, 32, 64 bit types. A more robust solution would
4003 be to import stdint.h from gnulib. */
4008 unsigned char bytes
[1];
4013 unsigned char bytes
[2];
4018 unsigned char bytes
[4];
4023 unsigned char bytes
[8];
4028 if (aexpr
->length
== 0)
4030 trace_debug ("empty agent expression");
4031 return expr_eval_empty_expression
;
4034 /* Cache the stack top in its own variable. Much of the time we can
4035 operate on this variable, rather than dinking with the stack. It
4036 needs to be copied to the stack when sp changes. */
4041 op
= aexpr
->bytes
[pc
++];
4043 trace_debug ("About to interpret byte 0x%x", op
);
4047 case gdb_agent_op_add
:
4051 case gdb_agent_op_sub
:
4052 top
= stack
[--sp
] - top
;
4055 case gdb_agent_op_mul
:
4059 case gdb_agent_op_div_signed
:
4062 trace_debug ("Attempted to divide by zero");
4063 return expr_eval_divide_by_zero
;
4065 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
4068 case gdb_agent_op_div_unsigned
:
4071 trace_debug ("Attempted to divide by zero");
4072 return expr_eval_divide_by_zero
;
4074 top
= stack
[--sp
] / top
;
4077 case gdb_agent_op_rem_signed
:
4080 trace_debug ("Attempted to divide by zero");
4081 return expr_eval_divide_by_zero
;
4083 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
4086 case gdb_agent_op_rem_unsigned
:
4089 trace_debug ("Attempted to divide by zero");
4090 return expr_eval_divide_by_zero
;
4092 top
= stack
[--sp
] % top
;
4095 case gdb_agent_op_lsh
:
4096 top
= stack
[--sp
] << top
;
4099 case gdb_agent_op_rsh_signed
:
4100 top
= ((LONGEST
) stack
[--sp
]) >> top
;
4103 case gdb_agent_op_rsh_unsigned
:
4104 top
= stack
[--sp
] >> top
;
4107 case gdb_agent_op_trace
:
4108 agent_mem_read (tframe
,
4109 NULL
, (CORE_ADDR
) stack
[--sp
], (ULONGEST
) top
);
4114 case gdb_agent_op_trace_quick
:
4115 arg
= aexpr
->bytes
[pc
++];
4116 agent_mem_read (tframe
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
4119 case gdb_agent_op_log_not
:
4123 case gdb_agent_op_bit_and
:
4127 case gdb_agent_op_bit_or
:
4131 case gdb_agent_op_bit_xor
:
4135 case gdb_agent_op_bit_not
:
4139 case gdb_agent_op_equal
:
4140 top
= (stack
[--sp
] == top
);
4143 case gdb_agent_op_less_signed
:
4144 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
4147 case gdb_agent_op_less_unsigned
:
4148 top
= (stack
[--sp
] < top
);
4151 case gdb_agent_op_ext
:
4152 arg
= aexpr
->bytes
[pc
++];
4153 if (arg
< (sizeof (LONGEST
) * 8))
4155 LONGEST mask
= 1 << (arg
- 1);
4156 top
&= ((LONGEST
) 1 << arg
) - 1;
4157 top
= (top
^ mask
) - mask
;
4161 case gdb_agent_op_ref8
:
4162 agent_mem_read (tframe
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
4166 case gdb_agent_op_ref16
:
4167 agent_mem_read (tframe
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
4171 case gdb_agent_op_ref32
:
4172 agent_mem_read (tframe
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
4176 case gdb_agent_op_ref64
:
4177 agent_mem_read (tframe
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
4181 case gdb_agent_op_if_goto
:
4183 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4190 case gdb_agent_op_goto
:
4191 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4194 case gdb_agent_op_const8
:
4195 /* Flush the cached stack top. */
4197 top
= aexpr
->bytes
[pc
++];
4200 case gdb_agent_op_const16
:
4201 /* Flush the cached stack top. */
4203 top
= aexpr
->bytes
[pc
++];
4204 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4207 case gdb_agent_op_const32
:
4208 /* Flush the cached stack top. */
4210 top
= aexpr
->bytes
[pc
++];
4211 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4212 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4213 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4216 case gdb_agent_op_const64
:
4217 /* Flush the cached stack top. */
4219 top
= aexpr
->bytes
[pc
++];
4220 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4221 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4222 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4223 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4224 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4225 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4226 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4229 case gdb_agent_op_reg
:
4230 /* Flush the cached stack top. */
4232 arg
= aexpr
->bytes
[pc
++];
4233 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4236 struct regcache
*regcache
;
4238 regcache
= get_context_regcache (ctx
);
4240 switch (register_size (regnum
))
4243 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
4247 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
4251 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
4255 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
4259 internal_error (__FILE__
, __LINE__
,
4260 "unhandled register size");
4265 case gdb_agent_op_end
:
4266 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4267 sp
, pulongest (top
));
4272 /* This should be an error */
4273 trace_debug ("Stack is empty, nothing to return");
4274 return expr_eval_empty_stack
;
4278 return expr_eval_no_error
;
4280 case gdb_agent_op_dup
:
4284 case gdb_agent_op_pop
:
4289 case gdb_agent_op_zero_ext
:
4290 arg
= aexpr
->bytes
[pc
++];
4291 if (arg
< (sizeof (LONGEST
) * 8))
4292 top
&= ((LONGEST
) 1 << arg
) - 1;
4295 case gdb_agent_op_swap
:
4296 /* Interchange top two stack elements, making sure top gets
4297 copied back onto stack. */
4299 top
= stack
[sp
- 1];
4300 stack
[sp
- 1] = stack
[sp
];
4303 case gdb_agent_op_getv
:
4304 /* Flush the cached stack top. */
4306 arg
= aexpr
->bytes
[pc
++];
4307 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4308 top
= get_trace_state_variable_value (arg
);
4311 case gdb_agent_op_setv
:
4312 arg
= aexpr
->bytes
[pc
++];
4313 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4314 set_trace_state_variable_value (arg
, top
);
4315 /* Note that we leave the value on the stack, for the
4316 benefit of later/enclosing expressions. */
4319 case gdb_agent_op_tracev
:
4320 arg
= aexpr
->bytes
[pc
++];
4321 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4322 agent_tsv_read (tframe
, arg
);
4325 /* GDB never (currently) generates any of these ops. */
4326 case gdb_agent_op_float
:
4327 case gdb_agent_op_ref_float
:
4328 case gdb_agent_op_ref_double
:
4329 case gdb_agent_op_ref_long_double
:
4330 case gdb_agent_op_l_to_d
:
4331 case gdb_agent_op_d_to_l
:
4332 case gdb_agent_op_trace16
:
4333 trace_debug ("Agent expression op 0x%x valid, but not handled",
4335 /* If ever GDB generates any of these, we don't have the
4336 option of ignoring. */
4340 trace_debug ("Agent expression op 0x%x not recognized", op
);
4341 /* Don't struggle on, things will just get worse. */
4342 return expr_eval_unrecognized_opcode
;
4345 /* Check for stack badness. */
4346 if (sp
>= (STACK_MAX
- 1))
4348 trace_debug ("Expression stack overflow");
4349 return expr_eval_stack_overflow
;
4354 trace_debug ("Expression stack underflow");
4355 return expr_eval_stack_underflow
;
4358 trace_debug ("Op %s -> sp=%d, top=0x%s",
4359 gdb_agent_op_names
[op
], sp
, pulongest (top
));
4363 /* Do memory copies for bytecodes. */
4364 /* Do the recording of memory blocks for actions and bytecodes. */
4367 agent_mem_read (struct traceframe
*tframe
,
4368 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4370 unsigned char *mspace
;
4371 ULONGEST remaining
= len
;
4372 unsigned short blocklen
;
4374 /* If a 'to' buffer is specified, use it. */
4377 read_inferior_memory (from
, to
, len
);
4381 /* Otherwise, create a new memory block in the trace buffer. */
4382 while (remaining
> 0)
4386 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4387 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4388 mspace
= add_traceframe_block (tframe
, sp
);
4391 /* Identify block as a memory block. */
4394 /* Record address and size. */
4395 memcpy (mspace
, &from
, sizeof (from
));
4396 mspace
+= sizeof (from
);
4397 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4398 mspace
+= sizeof (blocklen
);
4399 /* Record the memory block proper. */
4400 read_inferior_memory (from
, mspace
, blocklen
);
4401 trace_debug ("%d bytes recorded", blocklen
);
4402 remaining
-= blocklen
;
4408 /* Record the value of a trace state variable. */
4411 agent_tsv_read (struct traceframe
*tframe
, int n
)
4413 unsigned char *vspace
;
4416 vspace
= add_traceframe_block (tframe
,
4417 1 + sizeof (n
) + sizeof (LONGEST
));
4420 /* Identify block as a variable. */
4422 /* Record variable's number and value. */
4423 memcpy (vspace
+ 1, &n
, sizeof (n
));
4424 val
= get_trace_state_variable_value (n
);
4425 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4426 trace_debug ("Variable %d recorded", n
);
4430 #ifndef IN_PROCESS_AGENT
4432 static unsigned char *
4433 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4434 int tfnum
, char type_wanted
)
4436 unsigned char *dataptr
;
4440 trace_debug ("traceframe %d has no data", tfnum
);
4444 /* Iterate through a traceframe's blocks, looking for a block of the
4446 for (dataptr
= database
;
4447 dataptr
< database
+ datasize
;
4451 unsigned short mlen
;
4453 if (dataptr
== trace_buffer_wrap
)
4455 /* Adjust to reflect wrapping part of the frame around to
4457 datasize
= dataptr
- database
;
4458 dataptr
= database
= trace_buffer_lo
;
4460 blocktype
= *dataptr
++;
4462 if (type_wanted
== blocktype
)
4468 /* Skip over the registers block. */
4469 dataptr
+= register_cache_size ();
4472 /* Skip over the memory block. */
4473 dataptr
+= sizeof (CORE_ADDR
);
4474 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4475 dataptr
+= (sizeof (mlen
) + mlen
);
4478 /* Skip over the TSV block. */
4479 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4482 trace_debug ("traceframe %d has unknown block type 0x%x",
4491 static unsigned char *
4492 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4494 unsigned char *regblock
;
4496 regblock
= traceframe_find_block_type (tframe
->data
,
4500 if (regblock
== NULL
)
4501 trace_debug ("traceframe %d has no register data", tfnum
);
4506 /* Get registers from a traceframe. */
4509 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4511 unsigned char *dataptr
;
4512 struct tracepoint
*tpoint
;
4513 struct traceframe
*tframe
;
4515 tframe
= find_traceframe (tfnum
);
4519 trace_debug ("traceframe %d not found", tfnum
);
4523 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4524 if (dataptr
== NULL
)
4526 /* We don't like making up numbers, but GDB has all manner of
4527 troubles when the target says there are no registers. */
4528 supply_regblock (regcache
, NULL
);
4530 /* We can generally guess at a PC, although this will be
4531 misleading for while-stepping frames and multi-location
4533 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4535 regcache_write_pc (regcache
, tpoint
->address
);
4538 supply_regblock (regcache
, dataptr
);
4544 traceframe_get_pc (struct traceframe
*tframe
)
4546 struct regcache regcache
;
4547 unsigned char *dataptr
;
4549 dataptr
= traceframe_find_regblock (tframe
, -1);
4550 if (dataptr
== NULL
)
4553 init_register_cache (®cache
, dataptr
);
4554 return regcache_read_pc (®cache
);
4557 /* Read a requested block of memory from a trace frame. */
4560 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
4561 unsigned char *buf
, ULONGEST length
,
4564 struct traceframe
*tframe
;
4565 unsigned char *database
, *dataptr
;
4566 unsigned int datasize
;
4568 unsigned short mlen
;
4570 trace_debug ("traceframe_read_mem");
4572 tframe
= find_traceframe (tfnum
);
4576 trace_debug ("traceframe %d not found", tfnum
);
4580 datasize
= tframe
->data_size
;
4581 database
= dataptr
= &tframe
->data
[0];
4583 /* Iterate through a traceframe's blocks, looking for memory. */
4584 while ((dataptr
= traceframe_find_block_type (dataptr
,
4585 datasize
- (dataptr
- database
),
4586 tfnum
, 'M')) != NULL
)
4588 memcpy (&maddr
, dataptr
, sizeof (maddr
));
4589 dataptr
+= sizeof (maddr
);
4590 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4591 dataptr
+= sizeof (mlen
);
4592 trace_debug ("traceframe %d has %d bytes at %s",
4593 tfnum
, mlen
, paddress (maddr
));
4595 /* Check that requested data is in bounds. */
4596 if (maddr
<= addr
&& (addr
+ length
) <= (maddr
+ mlen
))
4598 /* Block includes the requested range, copy it out. */
4599 memcpy (buf
, dataptr
+ (addr
- maddr
), length
);
4604 /* Skip over this block. */
4608 trace_debug ("traceframe %d has no memory data for the desired region",
4616 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
4619 struct traceframe
*tframe
;
4620 unsigned char *database
, *dataptr
;
4621 unsigned int datasize
;
4624 trace_debug ("traceframe_read_tsv");
4626 tfnum
= current_traceframe
;
4630 trace_debug ("no current traceframe");
4634 tframe
= find_traceframe (tfnum
);
4638 trace_debug ("traceframe %d not found", tfnum
);
4642 datasize
= tframe
->data_size
;
4643 database
= dataptr
= &tframe
->data
[0];
4645 /* Iterate through a traceframe's blocks, looking for the tsv. */
4646 while ((dataptr
= traceframe_find_block_type (dataptr
,
4647 datasize
- (dataptr
- database
),
4648 tfnum
, 'V')) != NULL
)
4650 memcpy (&vnum
, dataptr
, sizeof (vnum
));
4651 dataptr
+= sizeof (vnum
);
4653 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
4655 /* Check that this is the variable we want. */
4658 memcpy (val
, dataptr
, sizeof (*val
));
4662 /* Skip over this block. */
4663 dataptr
+= sizeof (LONGEST
);
4666 trace_debug ("traceframe %d has no data for variable %d",
4671 /* Return the first fast tracepoint whose jump pad contains PC. */
4673 static struct tracepoint
*
4674 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
4676 struct tracepoint
*tpoint
;
4678 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4679 if (tpoint
->type
== fast_tracepoint
)
4680 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
4686 /* Return GDBserver's tracepoint that matches the IP Agent's
4687 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
4690 static struct tracepoint
*
4691 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
4693 struct tracepoint
*tpoint
;
4695 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4696 if (tpoint
->type
== fast_tracepoint
)
4697 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
4705 /* The type of the object that is used to synchronize fast tracepoint
4708 typedef struct collecting_t
4710 /* The fast tracepoint number currently collecting. */
4713 /* A number that GDBserver can use to identify the thread that is
4714 presently holding the collect lock. This need not (and usually
4715 is not) the thread id, as getting the current thread ID usually
4716 requires a system call, which we want to avoid like the plague.
4717 Usually this is thread's TCB, found in the TLS (pseudo-)
4718 register, which is readable with a single insn on several
4720 uintptr_t thread_area
;
4723 #ifndef IN_PROCESS_AGENT
4726 force_unlock_trace_buffer (void)
4728 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
4731 /* Check if the thread identified by THREAD_AREA which is stopped at
4732 STOP_PC, is presently locking the fast tracepoint collection, and
4733 if so, gather some status of said collection. Returns 0 if the
4734 thread isn't collecting or in the jump pad at all. 1, if in the
4735 jump pad (or within gdb_collect) and hasn't executed the adjusted
4736 original insn yet (can set a breakpoint there and run to it). 2,
4737 if presently executing the adjusted original insn --- in which
4738 case, if we want to move the thread out of the jump pad, we need to
4739 single-step it until this function returns 0. */
4742 fast_tracepoint_collecting (CORE_ADDR thread_area
,
4744 struct fast_tpoint_collect_status
*status
)
4746 CORE_ADDR ipa_collecting
;
4747 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
4748 struct tracepoint
*tpoint
;
4749 int needs_breakpoint
;
4751 /* The thread THREAD_AREA is either:
4753 0. not collecting at all, not within the jump pad, or within
4754 gdb_collect or one of its callees.
4756 1. in the jump pad and haven't reached gdb_collect
4758 2. within gdb_collect (out of the jump pad) (collect is set)
4760 3. we're in the jump pad, after gdb_collect having returned,
4761 possibly executing the adjusted insns.
4763 For cases 1 and 3, `collecting' may or not be set. The jump pad
4764 doesn't have any complicated jump logic, so we can tell if the
4765 thread is executing the adjust original insn or not by just
4766 matching STOP_PC with known jump pad addresses. If we it isn't
4767 yet executing the original insn, set a breakpoint there, and let
4768 the thread run to it, so to quickly step over a possible (many
4769 insns) gdb_collect call. Otherwise, or when the breakpoint is
4770 hit, only a few (small number of) insns are left to be executed
4771 in the jump pad. Single-step the thread until it leaves the
4776 needs_breakpoint
= 0;
4777 trace_debug ("fast_tracepoint_collecting");
4779 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
4780 &ipa_gdb_jump_pad_buffer
))
4781 fatal ("error extracting `gdb_jump_pad_buffer'");
4782 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
4783 &ipa_gdb_jump_pad_buffer_end
))
4784 fatal ("error extracting `gdb_jump_pad_buffer_end'");
4786 if (ipa_gdb_jump_pad_buffer
<= stop_pc
&& stop_pc
< ipa_gdb_jump_pad_buffer_end
)
4788 /* We can tell which tracepoint(s) the thread is collecting by
4789 matching the jump pad address back to the tracepoint. */
4790 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
4793 warning ("in jump pad, but no matching tpoint?");
4798 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
4800 tpoint
->number
, paddress (tpoint
->address
),
4801 paddress (tpoint
->jump_pad
),
4802 paddress (tpoint
->jump_pad_end
),
4803 paddress (tpoint
->adjusted_insn_addr
),
4804 paddress (tpoint
->adjusted_insn_addr_end
));
4807 /* Definitely in the jump pad. May or may not need
4808 fast-exit-jump-pad breakpoint. */
4809 if (tpoint
->jump_pad
<= stop_pc
4810 && stop_pc
< tpoint
->adjusted_insn_addr
)
4811 needs_breakpoint
= 1;
4815 collecting_t ipa_collecting_obj
;
4817 /* If `collecting' is set/locked, then the THREAD_AREA thread
4818 may or not be the one holding the lock. We have to read the
4819 lock to find out. */
4821 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
4824 trace_debug ("fast_tracepoint_collecting:"
4825 " failed reading 'collecting' in the inferior");
4829 if (!ipa_collecting
)
4831 trace_debug ("fast_tracepoint_collecting: not collecting"
4832 " (and nobody is).");
4836 /* Some thread is collecting. Check which. */
4837 if (read_inferior_memory (ipa_collecting
,
4838 (unsigned char *) &ipa_collecting_obj
,
4839 sizeof (ipa_collecting_obj
)) != 0)
4842 if (ipa_collecting_obj
.thread_area
!= thread_area
)
4844 trace_debug ("fast_tracepoint_collecting: not collecting "
4845 "(another thread is)");
4850 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
4853 warning ("fast_tracepoint_collecting: collecting, "
4854 "but tpoint %s not found?",
4855 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
4859 /* The thread is within `gdb_collect', skip over the rest of
4860 fast tracepoint collection quickly using a breakpoint. */
4861 needs_breakpoint
= 1;
4864 /* The caller wants a bit of status detail. */
4867 status
->tpoint_num
= tpoint
->number
;
4868 status
->tpoint_addr
= tpoint
->address
;
4869 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
4870 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
4873 if (needs_breakpoint
)
4875 /* Hasn't executed the original instruction yet. Set breakpoint
4876 there, and wait till it's hit, then single-step until exiting
4880 fast_tracepoint_collecting, returning continue-until-break at %s",
4881 paddress (tpoint
->adjusted_insn_addr
));
4883 return 1; /* continue */
4887 /* Just single-step until exiting the jump pad. */
4889 trace_debug ("fast_tracepoint_collecting, returning "
4890 "need-single-step (%s-%s)",
4891 paddress (tpoint
->adjusted_insn_addr
),
4892 paddress (tpoint
->adjusted_insn_addr_end
));
4894 return 2; /* single-step */
4900 #ifdef IN_PROCESS_AGENT
4902 /* The global fast tracepoint collect lock. Points to a collecting_t
4903 object built on the stack by the jump pad, if presently locked;
4904 NULL if it isn't locked. Note that this lock *must* be set while
4905 executing any *function other than the jump pad. See
4906 fast_tracepoint_collecting. */
4907 static collecting_t
* ATTR_USED collecting
;
4909 /* This routine, called from the jump pad (in asm) is designed to be
4910 called from the jump pads of fast tracepoints, thus it is on the
4913 IP_AGENT_EXPORT
void ATTR_USED
4914 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
4916 struct fast_tracepoint_ctx ctx
;
4918 /* Don't do anything until the trace run is completely set up. */
4922 ctx
.base
.type
= fast_tracepoint
;
4924 ctx
.regcache_initted
= 0;
4925 ctx
.tpoint
= tpoint
;
4927 /* Wrap the regblock in a register cache (in the stack, we don't
4928 want to malloc here). */
4929 ctx
.regspace
= alloca (register_cache_size ());
4930 if (ctx
.regspace
== NULL
)
4932 trace_debug ("Trace buffer block allocation failed, skipping");
4936 /* Test the condition if present, and collect if true. */
4937 if (tpoint
->cond
== NULL
4938 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4941 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4942 tpoint
->address
, tpoint
);
4944 /* Note that this will cause original insns to be written back
4945 to where we jumped from, but that's OK because we're jumping
4946 back to the next whole instruction. This will go badly if
4947 instruction restoration is not atomic though. */
4948 if (stopping_tracepoint
4949 || trace_buffer_is_full
4950 || expr_eval_result
!= expr_eval_no_error
)
4955 /* If there was a condition and it evaluated to false, the only
4956 way we would stop tracing is if there was an error during
4957 condition expression evaluation. */
4958 if (expr_eval_result
!= expr_eval_no_error
)
4965 #ifndef IN_PROCESS_AGENT
4967 /* Bytecode compilation. */
4969 CORE_ADDR current_insn_ptr
;
4973 struct bytecode_address
4978 /* Offset and size of field to be modified in the goto block. */
4979 int from_offset
, from_size
;
4980 struct bytecode_address
*next
;
4981 } *bytecode_address_table
;
4984 get_raw_reg_func_addr (void)
4986 return ipa_sym_addrs
.addr_get_raw_reg
;
4990 emit_prologue (void)
4992 target_emit_ops ()->emit_prologue ();
4996 emit_epilogue (void)
4998 target_emit_ops ()->emit_epilogue ();
5004 target_emit_ops ()->emit_add ();
5010 target_emit_ops ()->emit_sub ();
5016 target_emit_ops ()->emit_mul ();
5022 target_emit_ops ()->emit_lsh ();
5026 emit_rsh_signed (void)
5028 target_emit_ops ()->emit_rsh_signed ();
5032 emit_rsh_unsigned (void)
5034 target_emit_ops ()->emit_rsh_unsigned ();
5040 target_emit_ops ()->emit_ext (arg
);
5046 target_emit_ops ()->emit_log_not ();
5052 target_emit_ops ()->emit_bit_and ();
5058 target_emit_ops ()->emit_bit_or ();
5064 target_emit_ops ()->emit_bit_xor ();
5070 target_emit_ops ()->emit_bit_not ();
5076 target_emit_ops ()->emit_equal ();
5080 emit_less_signed (void)
5082 target_emit_ops ()->emit_less_signed ();
5086 emit_less_unsigned (void)
5088 target_emit_ops ()->emit_less_unsigned ();
5094 target_emit_ops ()->emit_ref (size
);
5098 emit_if_goto (int *offset_p
, int *size_p
)
5100 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
5104 emit_goto (int *offset_p
, int *size_p
)
5106 target_emit_ops ()->emit_goto (offset_p
, size_p
);
5110 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
5112 target_emit_ops ()->write_goto_address (from
, to
, size
);
5116 emit_const (LONGEST num
)
5118 target_emit_ops ()->emit_const (num
);
5124 target_emit_ops ()->emit_reg (reg
);
5130 target_emit_ops ()->emit_pop ();
5134 emit_stack_flush (void)
5136 target_emit_ops ()->emit_stack_flush ();
5140 emit_zero_ext (int arg
)
5142 target_emit_ops ()->emit_zero_ext (arg
);
5148 target_emit_ops ()->emit_swap ();
5152 emit_stack_adjust (int n
)
5154 target_emit_ops ()->emit_stack_adjust (n
);
5157 /* FN's prototype is `LONGEST(*fn)(int)'. */
5160 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
5162 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
5165 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
5168 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
5170 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
5173 static enum eval_result_type
compile_bytecodes (struct agent_expr
*aexpr
);
5176 compile_tracepoint_condition (struct tracepoint
*tpoint
, CORE_ADDR
*jump_entry
)
5178 CORE_ADDR entry_point
= *jump_entry
;
5179 enum eval_result_type err
;
5181 trace_debug ("Starting condition compilation for tracepoint %d\n",
5184 /* Initialize the global pointer to the code being built. */
5185 current_insn_ptr
= *jump_entry
;
5189 err
= compile_bytecodes (tpoint
->cond
);
5191 if (err
== expr_eval_no_error
)
5195 /* Record the beginning of the compiled code. */
5196 tpoint
->compiled_cond
= entry_point
;
5198 trace_debug ("Condition compilation for tracepoint %d complete\n",
5203 /* Leave the unfinished code in situ, but don't point to it. */
5205 tpoint
->compiled_cond
= 0;
5207 trace_debug ("Condition compilation for tracepoint %d failed, "
5209 tpoint
->number
, err
);
5212 /* Update the code pointer passed in. Note that we do this even if
5213 the compile fails, so that we can look at the partial results
5214 instead of letting them be overwritten. */
5215 *jump_entry
= current_insn_ptr
;
5217 /* Leave a gap, to aid dump decipherment. */
5221 /* Given an agent expression, turn it into native code. */
5223 static enum eval_result_type
5224 compile_bytecodes (struct agent_expr
*aexpr
)
5230 /* This is only used to build 64-bit value for constants. */
5232 struct bytecode_address
*aentry
, *aentry2
;
5237 trace_debug ("Cannot compile op 0x%x\n", op); \
5238 return expr_eval_unhandled_opcode; \
5241 if (aexpr
->length
== 0)
5243 trace_debug ("empty agent expression\n");
5244 return expr_eval_empty_expression
;
5247 bytecode_address_table
= NULL
;
5251 op
= aexpr
->bytes
[pc
];
5253 trace_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
5255 /* Record the compiled-code address of the bytecode, for use by
5256 jump instructions. */
5257 aentry
= xmalloc (sizeof (struct bytecode_address
));
5259 aentry
->address
= current_insn_ptr
;
5260 aentry
->goto_pc
= -1;
5261 aentry
->from_offset
= aentry
->from_size
= 0;
5262 aentry
->next
= bytecode_address_table
;
5263 bytecode_address_table
= aentry
;
5271 case gdb_agent_op_add
:
5275 case gdb_agent_op_sub
:
5279 case gdb_agent_op_mul
:
5283 case gdb_agent_op_div_signed
:
5287 case gdb_agent_op_div_unsigned
:
5291 case gdb_agent_op_rem_signed
:
5295 case gdb_agent_op_rem_unsigned
:
5299 case gdb_agent_op_lsh
:
5303 case gdb_agent_op_rsh_signed
:
5307 case gdb_agent_op_rsh_unsigned
:
5308 emit_rsh_unsigned ();
5311 case gdb_agent_op_trace
:
5315 case gdb_agent_op_trace_quick
:
5319 case gdb_agent_op_log_not
:
5323 case gdb_agent_op_bit_and
:
5327 case gdb_agent_op_bit_or
:
5331 case gdb_agent_op_bit_xor
:
5335 case gdb_agent_op_bit_not
:
5339 case gdb_agent_op_equal
:
5343 case gdb_agent_op_less_signed
:
5344 emit_less_signed ();
5347 case gdb_agent_op_less_unsigned
:
5348 emit_less_unsigned ();
5351 case gdb_agent_op_ext
:
5352 arg
= aexpr
->bytes
[pc
++];
5353 if (arg
< (sizeof (LONGEST
) * 8))
5357 case gdb_agent_op_ref8
:
5361 case gdb_agent_op_ref16
:
5365 case gdb_agent_op_ref32
:
5369 case gdb_agent_op_ref64
:
5373 case gdb_agent_op_if_goto
:
5374 arg
= aexpr
->bytes
[pc
++];
5375 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5376 aentry
->goto_pc
= arg
;
5377 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5380 case gdb_agent_op_goto
:
5381 arg
= aexpr
->bytes
[pc
++];
5382 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5383 aentry
->goto_pc
= arg
;
5384 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5387 case gdb_agent_op_const8
:
5388 emit_stack_flush ();
5389 top
= aexpr
->bytes
[pc
++];
5393 case gdb_agent_op_const16
:
5394 emit_stack_flush ();
5395 top
= aexpr
->bytes
[pc
++];
5396 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5400 case gdb_agent_op_const32
:
5401 emit_stack_flush ();
5402 top
= aexpr
->bytes
[pc
++];
5403 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5404 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5405 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5409 case gdb_agent_op_const64
:
5410 emit_stack_flush ();
5411 top
= aexpr
->bytes
[pc
++];
5412 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5413 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5414 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5415 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5416 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5417 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5418 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5422 case gdb_agent_op_reg
:
5423 emit_stack_flush ();
5424 arg
= aexpr
->bytes
[pc
++];
5425 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5429 case gdb_agent_op_end
:
5430 trace_debug ("At end of expression\n");
5432 /* Assume there is one stack element left, and that it is
5433 cached in "top" where emit_epilogue can get to it. */
5434 emit_stack_adjust (1);
5439 case gdb_agent_op_dup
:
5440 /* In our design, dup is equivalent to stack flushing. */
5441 emit_stack_flush ();
5444 case gdb_agent_op_pop
:
5448 case gdb_agent_op_zero_ext
:
5449 arg
= aexpr
->bytes
[pc
++];
5450 if (arg
< (sizeof (LONGEST
) * 8))
5451 emit_zero_ext (arg
);
5454 case gdb_agent_op_swap
:
5458 case gdb_agent_op_getv
:
5459 emit_stack_flush ();
5460 arg
= aexpr
->bytes
[pc
++];
5461 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5462 emit_int_call_1 (ipa_sym_addrs
.addr_get_trace_state_variable_value
,
5466 case gdb_agent_op_setv
:
5467 arg
= aexpr
->bytes
[pc
++];
5468 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5469 emit_void_call_2 (ipa_sym_addrs
.addr_set_trace_state_variable_value
,
5473 case gdb_agent_op_tracev
:
5477 /* GDB never (currently) generates any of these ops. */
5478 case gdb_agent_op_float
:
5479 case gdb_agent_op_ref_float
:
5480 case gdb_agent_op_ref_double
:
5481 case gdb_agent_op_ref_long_double
:
5482 case gdb_agent_op_l_to_d
:
5483 case gdb_agent_op_d_to_l
:
5484 case gdb_agent_op_trace16
:
5489 trace_debug ("Agent expression op 0x%x not recognized\n", op
);
5490 /* Don't struggle on, things will just get worse. */
5491 return expr_eval_unrecognized_opcode
;
5494 /* This catches errors that occur in target-specific code
5498 trace_debug ("Error %d while emitting code for %s\n",
5499 emit_error
, gdb_agent_op_names
[op
]);
5500 return expr_eval_unhandled_opcode
;
5503 trace_debug ("Op %s compiled\n", gdb_agent_op_names
[op
]);
5506 /* Now fill in real addresses as goto destinations. */
5507 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
5511 if (aentry
->goto_pc
< 0)
5514 /* Find the location that we are going to, and call back into
5515 target-specific code to write the actual address or
5517 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
5519 if (aentry2
->pc
== aentry
->goto_pc
)
5521 trace_debug ("Want to jump from %s to %s\n",
5522 paddress (aentry
->address
),
5523 paddress (aentry2
->address
));
5524 write_goto_address (aentry
->address
+ aentry
->from_offset
,
5525 aentry2
->address
, aentry
->from_size
);
5531 /* Error out if we didn't find a destination. */
5534 trace_debug ("Destination of goto %d not found\n",
5536 return expr_eval_invalid_goto
;
5540 return expr_eval_no_error
;
5543 /* We'll need to adjust these when we consider bi-arch setups, and big
5547 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5549 return write_inferior_memory (where
,
5550 (unsigned char *) &ptr
, sizeof (void *));
5553 /* The base pointer of the IPA's heap. This is the only memory the
5554 IPA is allowed to use. The IPA should _not_ call the inferior's
5555 `malloc' during operation. That'd be slow, and, most importantly,
5556 it may not be safe. We may be collecting a tracepoint in a signal
5557 handler, for example. */
5558 static CORE_ADDR target_tp_heap
;
5560 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5564 target_malloc (ULONGEST size
)
5568 if (target_tp_heap
== 0)
5570 /* We have the pointer *address*, need what it points to. */
5571 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5573 fatal ("could get target heap head pointer");
5576 ptr
= target_tp_heap
;
5577 target_tp_heap
+= size
;
5579 /* Pad to 8-byte alignment. */
5580 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5586 download_agent_expr (struct agent_expr
*expr
)
5588 CORE_ADDR expr_addr
;
5589 CORE_ADDR expr_bytes
;
5591 expr_addr
= target_malloc (sizeof (*expr
));
5592 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5594 expr_bytes
= target_malloc (expr
->length
);
5595 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5597 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5602 /* Align V up to N bits. */
5603 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5606 download_tracepoints (void)
5608 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
5609 struct tracepoint
*tpoint
;
5611 /* Start out empty. */
5612 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
5614 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5616 struct tracepoint target_tracepoint
;
5618 if (tpoint
->type
!= fast_tracepoint
)
5621 /* Maybe download a compiled condition. */
5622 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5624 CORE_ADDR jentry
, jump_entry
;
5626 jentry
= jump_entry
= get_jump_space_head ();
5628 if (tpoint
->cond
!= NULL
)
5630 /* Pad to 8-byte alignment. (needed?) */
5631 /* Actually this should be left for the target to
5633 jentry
= UALIGN (jentry
, 8);
5635 compile_tracepoint_condition (tpoint
, &jentry
);
5638 /* Pad to 8-byte alignment. */
5639 jentry
= UALIGN (jentry
, 8);
5640 claim_jump_space (jentry
- jump_entry
);
5643 target_tracepoint
= *tpoint
;
5646 tpptr
= target_malloc (sizeof (*tpoint
));
5647 tpoint
->obj_addr_on_target
= tpptr
;
5649 if (tpoint
== tracepoints
)
5651 /* First object in list, set the head pointer in the
5653 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
5657 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
5662 /* Write the whole object. We'll fix up its pointers in a bit.
5663 Assume no next for now. This is fixed up above on the next
5664 iteration, if there's any. */
5665 target_tracepoint
.next
= NULL
;
5666 /* Need to clear this here too, since we're downloading the
5667 tracepoints before clearing our own copy. */
5668 target_tracepoint
.hit_count
= 0;
5670 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5671 sizeof (target_tracepoint
));
5674 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5676 download_agent_expr (tpoint
->cond
));
5678 if (tpoint
->numactions
)
5681 CORE_ADDR actions_array
;
5683 /* The pointers array. */
5685 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
5686 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5690 /* Now for each pointer, download the action. */
5691 for (i
= 0; i
< tpoint
->numactions
; i
++)
5693 CORE_ADDR ipa_action
= 0;
5694 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5696 switch (action
->type
)
5700 = target_malloc (sizeof (struct collect_memory_action
));
5701 write_inferior_memory (ipa_action
,
5702 (unsigned char *) action
,
5703 sizeof (struct collect_memory_action
));
5707 = target_malloc (sizeof (struct collect_registers_action
));
5708 write_inferior_memory (ipa_action
,
5709 (unsigned char *) action
,
5710 sizeof (struct collect_registers_action
));
5715 struct eval_expr_action
*eaction
5716 = (struct eval_expr_action
*) action
;
5718 ipa_action
= target_malloc (sizeof (*eaction
));
5719 write_inferior_memory (ipa_action
,
5720 (unsigned char *) eaction
,
5723 expr
= download_agent_expr (eaction
->expr
);
5724 write_inferior_data_ptr
5725 (ipa_action
+ offsetof (struct eval_expr_action
, expr
),
5730 trace_debug ("unknown trace action '%c', ignoring",
5735 if (ipa_action
!= 0)
5736 write_inferior_data_ptr
5737 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
5745 download_trace_state_variables (void)
5747 CORE_ADDR ptr
= 0, prev_ptr
= 0;
5748 struct trace_state_variable
*tsv
;
5750 /* Start out empty. */
5751 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
5753 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
5755 struct trace_state_variable target_tsv
;
5757 /* TSV's with a getter have been initialized equally in both the
5758 inferior and GDBserver. Skip them. */
5759 if (tsv
->getter
!= NULL
)
5765 ptr
= target_malloc (sizeof (*tsv
));
5767 if (tsv
== trace_state_variables
)
5769 /* First object in list, set the head pointer in the
5772 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
5777 write_inferior_data_ptr (prev_ptr
5778 + offsetof (struct trace_state_variable
,
5783 /* Write the whole object. We'll fix up its pointers in a bit.
5784 Assume no next, fixup when needed. */
5785 target_tsv
.next
= NULL
;
5787 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
5788 sizeof (target_tsv
));
5790 if (tsv
->name
!= NULL
)
5792 size_t size
= strlen (tsv
->name
) + 1;
5793 CORE_ADDR name_addr
= target_malloc (size
);
5794 write_inferior_memory (name_addr
,
5795 (unsigned char *) tsv
->name
, size
);
5796 write_inferior_data_ptr (ptr
5797 + offsetof (struct trace_state_variable
,
5802 if (tsv
->getter
!= NULL
)
5804 fatal ("what to do with these?");
5810 /* Fixup the next pointer in the last item in the list. */
5811 write_inferior_data_ptr (prev_ptr
+ offsetof (struct trace_state_variable
,
5816 /* Upload complete trace frames out of the IP Agent's trace buffer
5817 into GDBserver's trace buffer. This always uploads either all or
5818 no trace frames. This is the counter part of
5819 `trace_alloc_trace_buffer'. See its description of the atomic
5820 synching mechanism. */
5823 upload_fast_traceframes (void)
5825 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
5826 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
5828 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
5829 unsigned int curr_tbctrl_idx
;
5830 unsigned int ipa_trace_buffer_ctrl_curr
;
5831 unsigned int ipa_trace_buffer_ctrl_curr_old
;
5832 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
5833 struct breakpoint
*about_to_request_buffer_space_bkpt
;
5834 CORE_ADDR ipa_trace_buffer_lo
;
5835 CORE_ADDR ipa_trace_buffer_hi
;
5837 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
5838 &ipa_traceframe_read_count_racy
))
5840 /* This will happen in most targets if the current thread is
5845 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
5846 &ipa_traceframe_write_count_racy
))
5849 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
5850 ipa_traceframe_write_count_racy
- ipa_traceframe_read_count_racy
,
5851 ipa_traceframe_write_count_racy
, ipa_traceframe_read_count_racy
);
5853 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
5856 about_to_request_buffer_space_bkpt
5857 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
5860 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
5861 &ipa_trace_buffer_ctrl_curr
))
5864 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
5866 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
5869 unsigned int prev
, counter
;
5871 /* Update the token, with new counters, and the GDBserver stamp
5872 bit. Alway reuse the current TBC index. */
5873 prev
= ipa_trace_buffer_ctrl_curr
& 0x0007ff00;
5874 counter
= (prev
+ 0x100) & 0x0007ff00;
5876 ipa_trace_buffer_ctrl_curr
= (0x80000000
5882 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
5883 ipa_trace_buffer_ctrl_curr
))
5886 trace_debug ("Lib: Committed %08x -> %08x",
5887 ipa_trace_buffer_ctrl_curr_old
,
5888 ipa_trace_buffer_ctrl_curr
);
5890 /* Re-read these, now that we've installed the
5891 `about_to_request_buffer_space' breakpoint/lock. A thread could
5892 have finished a traceframe between the last read of these
5893 counters and setting the breakpoint above. If we start
5894 uploading, we never want to leave this function with
5895 traceframe_read_count != 0, otherwise, GDBserver could end up
5896 incrementing the counter tokens more than once (due to event loop
5897 nesting), which would break the IP agent's "effective" detection
5898 (see trace_alloc_trace_buffer). */
5899 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
5900 &ipa_traceframe_read_count
))
5902 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
5903 &ipa_traceframe_write_count
))
5908 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
5909 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
5910 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
5912 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
5913 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
5914 trace_debug ("note that ipa_traceframe_count's parts changed");
5917 /* Get the address of the current TBC object (the IP agent has an
5918 array of 3 such objects). The index is stored in the TBC
5920 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
5921 ipa_trace_buffer_ctrl_addr
5922 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
5924 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
5925 (unsigned char *) &ipa_trace_buffer_ctrl
,
5926 sizeof (struct ipa_trace_buffer_control
)))
5929 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
5930 &ipa_trace_buffer_lo
))
5932 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
5933 &ipa_trace_buffer_hi
))
5936 /* Offsets are easier to grok for debugging than raw addresses,
5937 especially for the small trace buffer sizes that are useful for
5939 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
5940 "endfree=%d wrap=%d hi=%d",
5942 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
5943 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
5944 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
5945 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
5946 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
5948 /* Note that the IPA's buffer is always circular. */
5950 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
5952 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
5953 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
5955 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
5956 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
5957 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
5958 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
5961 tf
= IPA_FIRST_TRACEFRAME ();
5963 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
5965 struct tracepoint
*tpoint
;
5966 struct traceframe
*tframe
;
5967 unsigned char *block
;
5968 struct traceframe ipa_tframe
;
5970 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
5971 offsetof (struct traceframe
, data
)))
5972 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
5974 if (ipa_tframe
.tpnum
== 0)
5975 fatal ("Uploading: No (more) fast traceframes, but "
5976 "ipa_traceframe_count == %u??\n",
5977 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
5979 /* Note that this will be incorrect for multi-location
5981 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
5983 tframe
= add_traceframe (tpoint
);
5986 trace_buffer_is_full
= 1;
5987 trace_debug ("Uploading: trace buffer is full");
5991 /* Copy the whole set of blocks in one go for now. FIXME:
5992 split this in smaller blocks. */
5993 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
5996 if (read_inferior_memory (tf
+ offsetof (struct traceframe
, data
),
5997 block
, ipa_tframe
.data_size
))
5998 error ("Uploading: Couldn't read traceframe data at %s\n",
5999 paddress (tf
+ offsetof (struct traceframe
, data
)));
6002 trace_debug ("Uploading: traceframe didn't fit");
6003 finish_traceframe (tframe
);
6006 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6008 /* If we freed the traceframe that wrapped around, go back
6009 to the non-wrap case. */
6010 if (tf
< ipa_trace_buffer_ctrl
.start
)
6012 trace_debug ("Lib: Discarding past the wraparound");
6013 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6015 ipa_trace_buffer_ctrl
.start
= tf
;
6016 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6017 ++ipa_traceframe_read_count
;
6019 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6020 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6022 trace_debug ("Lib: buffer is fully empty. "
6023 "Trace buffer [%d] start=%d free=%d endfree=%d",
6025 (int) (ipa_trace_buffer_ctrl
.start
6026 - ipa_trace_buffer_lo
),
6027 (int) (ipa_trace_buffer_ctrl
.free
6028 - ipa_trace_buffer_lo
),
6029 (int) (ipa_trace_buffer_ctrl
.end_free
6030 - ipa_trace_buffer_lo
));
6032 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6033 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6034 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6035 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6038 trace_debug ("Uploaded a traceframe\n"
6039 "Lib: Trace buffer [%d] start=%d free=%d "
6040 "endfree=%d wrap=%d hi=%d",
6042 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6043 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6044 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6045 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6046 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6049 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6050 (unsigned char *) &ipa_trace_buffer_ctrl
,
6051 sizeof (struct ipa_trace_buffer_control
)))
6054 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6055 ipa_traceframe_read_count
);
6057 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6060 cancel_breakpoints ();
6062 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6063 about_to_request_buffer_space_bkpt
= NULL
;
6067 if (trace_buffer_is_full
)
6072 #ifdef IN_PROCESS_AGENT
6074 #include <sys/mman.h>
6077 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
6078 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
6079 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
6081 static void __attribute__ ((constructor
))
6082 initialize_tracepoint_ftlib (void)
6084 initialize_tracepoint ();
6090 tsv_get_timestamp (void)
6094 if (gettimeofday (&tv
, 0) != 0)
6097 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
6101 initialize_tracepoint (void)
6103 /* There currently no way to change the buffer size. */
6104 const int sizeOfBuffer
= 5 * 1024 * 1024;
6105 unsigned char *buf
= xmalloc (sizeOfBuffer
);
6106 init_trace_buffer (buf
, sizeOfBuffer
);
6108 /* Wire trace state variable 1 to be the timestamp. This will be
6109 uploaded to GDB upon connection and become one of its trace state
6110 variables. (In case you're wondering, if GDB already has a trace
6111 variable numbered 1, it will be renumbered.) */
6112 create_trace_state_variable (1, 0);
6113 set_trace_state_variable_name (1, "trace_timestamp");
6114 set_trace_state_variable_getter (1, tsv_get_timestamp
);
6116 #ifdef IN_PROCESS_AGENT
6119 pagesize
= sysconf (_SC_PAGE_SIZE
);
6123 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
6125 /* Allocate scratch buffer aligned on a page boundary. */
6126 gdb_jump_pad_buffer
= memalign (pagesize
, pagesize
* 20);
6127 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* 20;
6129 /* Make it writable and executable. */
6130 if (mprotect (gdb_jump_pad_buffer
, pagesize
* 20,
6131 PROT_READ
| PROT_WRITE
| PROT_EXEC
) != 0)
6133 initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
6134 gdb_jump_pad_buffer
, pagesize
* 20, strerror (errno
));
6137 initialize_low_tracepoint ();