1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 /* This file is built for both GDBserver, and the in-process
30 agent (IPA), a shared library that includes a tracing agent that is
31 loaded by the inferior to support fast tracepoints. Fast
32 tracepoints (or more accurately, jump based tracepoints) are
33 implemented by patching the tracepoint location with a jump into a
34 small trampoline function whose job is to save the register state,
35 call the in-process tracing agent, and then execute the original
36 instruction that was under the tracepoint jump (possibly adjusted,
37 if PC-relative, or some such).
39 The current synchronization design is pull based. That means,
40 GDBserver does most of the work, by peeking/poking at the inferior
41 agent's memory directly for downloading tracepoint and associated
42 objects, and for uploading trace frames. Whenever the IPA needs
43 something from GDBserver (trace buffer is full, tracing stopped for
44 some reason, etc.) the IPA calls a corresponding hook function
45 where GDBserver has placed a breakpoint.
47 Each of the agents has its own trace buffer. When browsing the
48 trace frames built from slow and fast tracepoints from GDB (tfind
49 mode), there's no guarantee the user is seeing the trace frames in
50 strict chronological creation order, although, GDBserver tries to
51 keep the order relatively reasonable, by syncing the trace buffers
56 static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf
, 1, 2);
59 trace_vdebug (const char *fmt
, ...)
65 vsprintf (buf
, fmt
, ap
);
66 fprintf (stderr
, "gdbserver/tracepoint: %s\n", buf
);
70 #define trace_debug_1(level, fmt, args...) \
72 if (level <= debug_threads) \
73 trace_vdebug ((fmt), ##args); \
76 #define trace_debug(FMT, args...) \
77 trace_debug_1 (1, FMT, ##args)
80 # define ATTR_USED __attribute__((used))
81 # define ATTR_NOINLINE __attribute__((noinline))
82 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
85 # define ATTR_NOINLINE
86 # define ATTR_CONSTRUCTOR
89 /* Make sure the functions the IPA needs to export (symbols GDBserver
90 needs to query GDB about) are exported. */
92 #ifdef IN_PROCESS_AGENT
93 # if defined _WIN32 || defined __CYGWIN__
94 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
97 # define IP_AGENT_EXPORT \
98 __attribute__ ((visibility("default"))) ATTR_USED
100 # define IP_AGENT_EXPORT ATTR_USED
104 # define IP_AGENT_EXPORT
107 /* Prefix exported symbols, for good citizenship. All the symbols
108 that need exporting are defined in this module. */
109 #ifdef IN_PROCESS_AGENT
110 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
111 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
112 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
113 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
114 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
115 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
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 \
137 gdb_agent_get_trace_state_variable_value
138 # define set_trace_state_variable_value \
139 gdb_agent_set_trace_state_variable_value
140 # define ust_loaded gdb_agent_ust_loaded
141 # define helper_thread_id gdb_agent_helper_thread_id
142 # define cmd_buf gdb_agent_cmd_buf
145 #ifndef IN_PROCESS_AGENT
147 /* Addresses of in-process agent's symbols GDBserver cares about. */
149 struct ipa_sym_addresses
151 CORE_ADDR addr_gdb_tp_heap_buffer
;
152 CORE_ADDR addr_gdb_jump_pad_buffer
;
153 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
154 CORE_ADDR addr_gdb_trampoline_buffer
;
155 CORE_ADDR addr_gdb_trampoline_buffer_end
;
156 CORE_ADDR addr_gdb_trampoline_buffer_error
;
157 CORE_ADDR addr_collecting
;
158 CORE_ADDR addr_gdb_collect
;
159 CORE_ADDR addr_stop_tracing
;
160 CORE_ADDR addr_flush_trace_buffer
;
161 CORE_ADDR addr_about_to_request_buffer_space
;
162 CORE_ADDR addr_trace_buffer_is_full
;
163 CORE_ADDR addr_stopping_tracepoint
;
164 CORE_ADDR addr_expr_eval_result
;
165 CORE_ADDR addr_error_tracepoint
;
166 CORE_ADDR addr_tracepoints
;
167 CORE_ADDR addr_tracing
;
168 CORE_ADDR addr_trace_buffer_ctrl
;
169 CORE_ADDR addr_trace_buffer_ctrl_curr
;
170 CORE_ADDR addr_trace_buffer_lo
;
171 CORE_ADDR addr_trace_buffer_hi
;
172 CORE_ADDR addr_traceframe_read_count
;
173 CORE_ADDR addr_traceframe_write_count
;
174 CORE_ADDR addr_traceframes_created
;
175 CORE_ADDR addr_trace_state_variables
;
176 CORE_ADDR addr_get_raw_reg
;
177 CORE_ADDR addr_get_trace_state_variable_value
;
178 CORE_ADDR addr_set_trace_state_variable_value
;
179 CORE_ADDR addr_ust_loaded
;
180 CORE_ADDR addr_helper_thread_id
;
181 CORE_ADDR addr_cmd_buf
;
184 #define STRINGIZE_1(STR) #STR
185 #define STRINGIZE(STR) STRINGIZE_1(STR)
186 #define IPA_SYM(SYM) \
188 STRINGIZE (gdb_agent_ ## SYM), \
189 offsetof (struct ipa_sym_addresses, addr_ ## SYM) \
198 IPA_SYM(gdb_tp_heap_buffer
),
199 IPA_SYM(gdb_jump_pad_buffer
),
200 IPA_SYM(gdb_jump_pad_buffer_end
),
201 IPA_SYM(gdb_trampoline_buffer
),
202 IPA_SYM(gdb_trampoline_buffer_end
),
203 IPA_SYM(gdb_trampoline_buffer_error
),
205 IPA_SYM(gdb_collect
),
206 IPA_SYM(stop_tracing
),
207 IPA_SYM(flush_trace_buffer
),
208 IPA_SYM(about_to_request_buffer_space
),
209 IPA_SYM(trace_buffer_is_full
),
210 IPA_SYM(stopping_tracepoint
),
211 IPA_SYM(expr_eval_result
),
212 IPA_SYM(error_tracepoint
),
213 IPA_SYM(tracepoints
),
215 IPA_SYM(trace_buffer_ctrl
),
216 IPA_SYM(trace_buffer_ctrl_curr
),
217 IPA_SYM(trace_buffer_lo
),
218 IPA_SYM(trace_buffer_hi
),
219 IPA_SYM(traceframe_read_count
),
220 IPA_SYM(traceframe_write_count
),
221 IPA_SYM(traceframes_created
),
222 IPA_SYM(trace_state_variables
),
223 IPA_SYM(get_raw_reg
),
224 IPA_SYM(get_trace_state_variable_value
),
225 IPA_SYM(set_trace_state_variable_value
),
227 IPA_SYM(helper_thread_id
),
231 struct ipa_sym_addresses ipa_sym_addrs
;
233 int all_tracepoint_symbols_looked_up
;
236 in_process_agent_loaded (void)
238 return all_tracepoint_symbols_looked_up
;
241 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
243 /* Returns true if both the in-process agent library and the static
244 tracepoints libraries are loaded in the inferior. */
247 in_process_agent_loaded_ust (void)
251 if (!in_process_agent_loaded ())
253 warning ("In-process agent not loaded");
257 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
259 warning ("Error reading ust_loaded in lib");
267 write_e_ipa_not_loaded (char *buffer
)
270 "E.In-process agent library not loaded in process. "
271 "Fast and static tracepoints unavailable.");
274 /* Write an error to BUFFER indicating that UST isn't loaded in the
278 write_e_ust_not_loaded (char *buffer
)
282 "E.UST library not loaded in process. "
283 "Static tracepoints unavailable.");
285 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
289 /* If the in-process agent library isn't loaded in the inferior, write
290 an error to BUFFER, and return 1. Otherwise, return 0. */
293 maybe_write_ipa_not_loaded (char *buffer
)
295 if (!in_process_agent_loaded ())
297 write_e_ipa_not_loaded (buffer
);
303 /* If the in-process agent library and the ust (static tracepoints)
304 library aren't loaded in the inferior, write an error to BUFFER,
305 and return 1. Otherwise, return 0. */
308 maybe_write_ipa_ust_not_loaded (char *buffer
)
310 if (!in_process_agent_loaded ())
312 write_e_ipa_not_loaded (buffer
);
315 else if (!in_process_agent_loaded_ust ())
317 write_e_ust_not_loaded (buffer
);
323 /* Cache all future symbols that the tracepoints module might request.
324 We can not request symbols at arbitrary states in the remote
325 protocol, only when the client tells us that new symbols are
326 available. So when we load the in-process library, make sure to
327 check the entire list. */
330 tracepoint_look_up_symbols (void)
334 if (all_tracepoint_symbols_looked_up
)
337 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
340 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
342 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
345 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
350 all_tracepoint_symbols_looked_up
= 1;
355 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
356 of the "stop_tracing" function. When this breakpoint is hit,
357 tracing stopped in the IPA for some reason. E.g., due to
358 tracepoint reaching the pass count, hitting conditional expression
359 evaluation error, etc.
361 The IPA's trace buffer is never in circular tracing mode: instead,
362 GDBserver's is, and whenever the in-process buffer fills, it calls
363 "flush_trace_buffer", which triggers an internal breakpoint.
364 GDBserver reacts to this breakpoint by pulling the meanwhile
365 collected data. Old frames discarding is always handled on the
368 #ifdef IN_PROCESS_AGENT
369 int debug_threads
= 0;
372 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
374 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
378 /* Call this in the functions where GDBserver places a breakpoint, so
379 that the compiler doesn't try to be clever and skip calling the
380 function at all. This is necessary, even if we tell the compiler
381 to not inline said functions. */
383 #if defined(__GNUC__)
384 # define UNKNOWN_SIDE_EFFECTS() asm ("")
386 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
389 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
392 /* GDBserver places breakpoint here. */
393 UNKNOWN_SIDE_EFFECTS();
396 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
397 flush_trace_buffer (void)
399 /* GDBserver places breakpoint here. */
400 UNKNOWN_SIDE_EFFECTS();
405 #ifndef IN_PROCESS_AGENT
407 tracepoint_handler (CORE_ADDR address
)
409 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
414 /* Breakpoint at "stop_tracing" in the inferior lib. */
415 struct breakpoint
*stop_tracing_bkpt
;
416 static int stop_tracing_handler (CORE_ADDR
);
418 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
419 struct breakpoint
*flush_trace_buffer_bkpt
;
420 static int flush_trace_buffer_handler (CORE_ADDR
);
422 static void download_tracepoints (void);
423 static void download_trace_state_variables (void);
424 static void upload_fast_traceframes (void);
426 static int run_inferior_command (char *cmd
);
429 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
431 return read_inferior_memory (symaddr
, (unsigned char *) val
,
436 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
438 return read_inferior_memory (symaddr
, (unsigned char *) val
,
443 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
445 void *pval
= (void *) (uintptr_t) val
;
448 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
449 *val
= (uintptr_t) pval
;
454 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
456 void *pval
= (void *) (uintptr_t) val
;
457 return write_inferior_memory (symaddr
,
458 (unsigned char *) &pval
, sizeof (pval
));
462 write_inferior_integer (CORE_ADDR symaddr
, int val
)
464 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
468 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
470 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
475 /* This enum must exactly match what is documented in
476 gdb/doc/agentexpr.texi, including all the numerical values. */
480 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
481 gdb_agent_op_ ## NAME = VALUE,
487 static const char *gdb_agent_op_names
[gdb_agent_op_last
] =
490 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
495 static const unsigned char gdb_agent_op_sizes
[gdb_agent_op_last
] =
498 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
507 unsigned char *bytes
;
510 /* Base action. Concrete actions inherit this. */
512 struct tracepoint_action
517 /* An 'M' (collect memory) action. */
518 struct collect_memory_action
520 struct tracepoint_action base
;
527 /* An 'R' (collect registers) action. */
529 struct collect_registers_action
531 struct tracepoint_action base
;
534 /* An 'X' (evaluate expression) action. */
536 struct eval_expr_action
538 struct tracepoint_action base
;
540 struct agent_expr
*expr
;
543 /* An 'L' (collect static trace data) action. */
544 struct collect_static_trace_data_action
546 struct tracepoint_action base
;
549 /* This structure describes a piece of the source-level definition of
550 the tracepoint. The contents are not interpreted by the target,
551 but preserved verbatim for uploading upon reconnection. */
555 /* The type of string, such as "cond" for a conditional. */
558 /* The source-level string itself. For the sake of target
559 debugging, we store it in plaintext, even though it is always
560 transmitted in hex. */
563 /* Link to the next one in the list. We link them in the order
564 received, in case some make up an ordered list of commands or
566 struct source_string
*next
;
571 /* Trap based tracepoint. */
574 /* A fast tracepoint implemented with a jump instead of a trap. */
577 /* A static tracepoint, implemented by a program call into a tracing
582 struct tracepoint_hit_ctx
;
584 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
587 /* The definition of a tracepoint. */
589 /* Tracepoints may have multiple locations, each at a different
590 address. This can occur with optimizations, template
591 instantiation, etc. Since the locations may be in different
592 scopes, the conditions and actions may be different for each
593 location. Our target version of tracepoints is more like GDB's
594 notion of "breakpoint locations", but we have almost nothing that
595 is not per-location, so we bother having two kinds of objects. The
596 key consequence is that numbers are not unique, and that it takes
597 both number and address to identify a tracepoint uniquely. */
601 /* The number of the tracepoint, as specified by GDB. Several
602 tracepoint objects here may share a number. */
605 /* Address at which the tracepoint is supposed to trigger. Several
606 tracepoints may share an address. */
609 /* Tracepoint type. */
610 enum tracepoint_type type
;
612 /* True if the tracepoint is currently enabled. */
615 /* The number of single steps that will be performed after each
619 /* The number of times the tracepoint may be hit before it will
620 terminate the entire tracing run. */
623 /* Pointer to the agent expression that is the tracepoint's
624 conditional, or NULL if the tracepoint is unconditional. */
625 struct agent_expr
*cond
;
627 /* The list of actions to take when the tracepoint triggers. */
629 struct tracepoint_action
**actions
;
631 /* Count of the times we've hit this tracepoint during the run.
632 Note that while-stepping steps are not counted as "hits". */
635 CORE_ADDR compiled_cond
;
637 /* Link to the next tracepoint in the list. */
638 struct tracepoint
*next
;
640 #ifndef IN_PROCESS_AGENT
641 /* The list of actions to take when the tracepoint triggers, in
642 string/packet form. */
645 /* The collection of strings that describe the tracepoint as it was
646 entered into GDB. These are not used by the target, but are
647 reported back to GDB upon reconnection. */
648 struct source_string
*source_strings
;
650 /* The number of bytes displaced by fast tracepoints. It may subsume
651 multiple instructions, for multi-byte fast tracepoints. This
652 field is only valid for fast tracepoints. */
655 /* Only for fast tracepoints. */
656 CORE_ADDR obj_addr_on_target
;
658 /* Address range where the original instruction under a fast
659 tracepoint was relocated to. (_end is actually one byte past
661 CORE_ADDR adjusted_insn_addr
;
662 CORE_ADDR adjusted_insn_addr_end
;
664 /* The address range of the piece of the jump pad buffer that was
665 assigned to this fast tracepoint. (_end is actually one byte
668 CORE_ADDR jump_pad_end
;
670 /* The address range of the piece of the trampoline buffer that was
671 assigned to this fast tracepoint. (_end is actually one byte
673 CORE_ADDR trampoline
;
674 CORE_ADDR trampoline_end
;
676 /* The list of actions to take while in a stepping loop. These
677 fields are only valid for patch-based tracepoints. */
678 int num_step_actions
;
679 struct tracepoint_action
**step_actions
;
680 /* Same, but in string/packet form. */
681 char **step_actions_str
;
683 /* Handle returned by the breakpoint or tracepoint module when we
684 inserted the trap or jump, or hooked into a static tracepoint.
685 NULL if we haven't inserted it yet. */
691 #ifndef IN_PROCESS_AGENT
693 /* Given `while-stepping', a thread may be collecting data for more
694 than one tracepoint simultaneously. On the other hand, the same
695 tracepoint with a while-stepping action may be hit by more than one
696 thread simultaneously (but not quite, each thread could be handling
697 a different step). Each thread holds a list of these objects,
698 representing the current step of each while-stepping action being
703 struct wstep_state
*next
;
705 /* The tracepoint number. */
707 /* The tracepoint's address. */
708 CORE_ADDR tp_address
;
710 /* The number of the current step in this 'while-stepping'
717 /* The linked list of all tracepoints. Marked explicitly as used as
718 the in-process library doesn't use it for the fast tracepoints
720 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
722 #ifndef IN_PROCESS_AGENT
724 /* Pointer to the last tracepoint in the list, new tracepoints are
725 linked in at the end. */
727 static struct tracepoint
*last_tracepoint
;
730 /* The first tracepoint to exceed its pass count. */
732 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
734 /* True if the trace buffer is full or otherwise no longer usable. */
736 IP_AGENT_EXPORT
int trace_buffer_is_full
;
738 /* Enumeration of the different kinds of things that can happen during
739 agent expression evaluation. */
741 enum eval_result_type
744 expr_eval_empty_expression
,
745 expr_eval_empty_stack
,
746 expr_eval_stack_overflow
,
747 expr_eval_stack_underflow
,
748 expr_eval_unhandled_opcode
,
749 expr_eval_unrecognized_opcode
,
750 expr_eval_divide_by_zero
,
751 expr_eval_invalid_goto
754 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
756 #ifndef IN_PROCESS_AGENT
758 static const char *eval_result_names
[] =
760 "terror:in the attic", /* this should never be reported */
761 "terror:empty expression",
762 "terror:empty stack",
763 "terror:stack overflow",
764 "terror:stack underflow",
765 "terror:unhandled opcode",
766 "terror:unrecognized opcode",
767 "terror:divide by zero"
772 /* The tracepoint in which the error occurred. */
774 static struct tracepoint
*error_tracepoint
;
776 struct trace_state_variable
778 /* This is the name of the variable as used in GDB. The target
779 doesn't use the name, but needs to have it for saving and
780 reconnection purposes. */
783 /* This number identifies the variable uniquely. Numbers may be
784 assigned either by the target (in the case of builtin variables),
785 or by GDB, and are presumed unique during the course of a trace
789 /* The variable's initial value, a 64-bit signed integer always. */
790 LONGEST initial_value
;
792 /* The variable's value, a 64-bit signed integer always. */
795 /* Pointer to a getter function, used to supply computed values. */
796 LONGEST (*getter
) (void);
798 /* Link to the next variable. */
799 struct trace_state_variable
*next
;
802 /* Linked list of all trace state variables. */
804 #ifdef IN_PROCESS_AGENT
805 struct trace_state_variable
*alloced_trace_state_variables
;
808 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
810 /* The results of tracing go into a fixed-size space known as the
811 "trace buffer". Because usage follows a limited number of
812 patterns, we manage it ourselves rather than with malloc. Basic
813 rules are that we create only one trace frame at a time, each is
814 variable in size, they are never moved once created, and we only
815 discard if we are doing a circular buffer, and then only the oldest
816 ones. Each trace frame includes its own size, so we don't need to
817 link them together, and the trace frame number is relative to the
818 first one, so we don't need to record numbers. A trace frame also
819 records the number of the tracepoint that created it. The data
820 itself is a series of blocks, each introduced by a single character
821 and with a defined format. Each type of block has enough
822 type/length info to allow scanners to jump quickly from one block
823 to the next without reading each byte in the block. */
825 /* Trace buffer management would be simple - advance a free pointer
826 from beginning to end, then stop - were it not for the circular
827 buffer option, which is a useful way to prevent a trace run from
828 stopping prematurely because the buffer filled up. In the circular
829 case, the location of the first trace frame (trace_buffer_start)
830 moves as old trace frames are discarded. Also, since we grow trace
831 frames incrementally as actions are performed, we wrap around to
832 the beginning of the trace buffer. This is per-block, so each
833 block within a trace frame remains contiguous. Things get messy
834 when the wrapped-around trace frame is the one being discarded; the
835 free space ends up in two parts at opposite ends of the buffer. */
838 # if defined(__GNUC__)
839 # define ATTR_PACKED __attribute__ ((packed))
841 # define ATTR_PACKED /* nothing */
845 /* The data collected at a tracepoint hit. This object should be as
846 small as possible, since there may be a great many of them. We do
847 not need to keep a frame number, because they are all sequential
848 and there are no deletions; so the Nth frame in the buffer is
849 always frame number N. */
853 /* Number of the tracepoint that collected this traceframe. A value
854 of 0 indicates the current end of the trace buffer. We make this
855 a 16-bit field because it's never going to happen that GDB's
856 numbering of tracepoints reaches 32,000. */
859 /* The size of the data in this trace frame. We limit this to 32
860 bits, even on a 64-bit target, because it's just implausible that
861 one is validly going to collect 4 gigabytes of data at a single
863 unsigned int data_size
: 32;
865 /* The base of the trace data, which is contiguous from this point. */
866 unsigned char data
[0];
870 /* The traceframe to be used as the source of data to send back to
871 GDB. A value of -1 means to get data from the live program. */
873 int current_traceframe
= -1;
875 /* This flag is true if the trace buffer is circular, meaning that
876 when it fills, the oldest trace frames are discarded in order to
879 #ifndef IN_PROCESS_AGENT
880 static int circular_trace_buffer
;
883 /* Pointer to the block of memory that traceframes all go into. */
885 static unsigned char *trace_buffer_lo
;
887 /* Pointer to the end of the trace buffer, more precisely to the byte
888 after the end of the buffer. */
890 static unsigned char *trace_buffer_hi
;
892 /* Control structure holding the read/write/etc. pointers into the
893 trace buffer. We need more than one of these to implement a
894 transaction-like mechanism to garantees that both GDBserver and the
895 in-process agent can try to change the trace buffer
898 struct trace_buffer_control
900 /* Pointer to the first trace frame in the buffer. In the
901 non-circular case, this is equal to trace_buffer_lo, otherwise it
902 moves around in the buffer. */
903 unsigned char *start
;
905 /* Pointer to the free part of the trace buffer. Note that we clear
906 several bytes at and after this pointer, so that traceframe
907 scans/searches terminate properly. */
910 /* Pointer to the byte after the end of the free part. Note that
911 this may be smaller than trace_buffer_free in the circular case,
912 and means that the free part is in two pieces. Initially it is
913 equal to trace_buffer_hi, then is generally equivalent to
914 trace_buffer_start. */
915 unsigned char *end_free
;
917 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
918 this is the point at which the trace data breaks, and resumes at
923 /* Same as above, to be used by GDBserver when updating the in-process
925 struct ipa_trace_buffer_control
934 /* We have possibly both GDBserver and an inferior thread accessing
935 the same IPA trace buffer memory. The IPA is the producer (tries
936 to put new frames in the buffer), while GDBserver occasionally
937 consumes them, that is, flushes the IPA's buffer into its own
938 buffer. Both sides need to update the trace buffer control
939 pointers (current head, tail, etc.). We can't use a global lock to
940 synchronize the accesses, as otherwise we could deadlock GDBserver
941 (if the thread holding the lock stops for a signal, say). So
942 instead of that, we use a transaction scheme where GDBserver writes
943 always prevail over the IPAs writes, and, we have the IPA detect
944 the commit failure/overwrite, and retry the whole attempt. This is
945 mainly implemented by having a global token object that represents
946 who wrote last to the buffer control structure. We need to freeze
947 any inferior writing to the buffer while GDBserver touches memory,
948 so that the inferior can correctly detect that GDBserver had been
949 there, otherwise, it could mistakingly think its commit was
950 successful; that's implemented by simply having GDBserver set a
951 breakpoint the inferior hits if it is the critical region.
953 There are three cycling trace buffer control structure copies
954 (buffer head, tail, etc.), with the token object including an index
955 indicating which is current live copy. The IPA tentatively builds
956 an updated copy in a non-current control structure, while GDBserver
957 always clobbers the current version directly. The IPA then tries
958 to atomically "commit" its version; if GDBserver clobbered the
959 structure meanwhile, that will fail, and the IPA restarts the
962 Listing the step in further detail, we have:
964 In-process agent (producer):
966 - passes by `about_to_request_buffer_space' breakpoint/lock
968 - reads current token, extracts current trace buffer control index,
969 and starts tentatively updating the rightmost one (0->1, 1->2,
970 2->0). Note that only one inferior thread is executing this code
971 at any given time, due to an outer lock in the jump pads.
973 - updates counters, and tries to commit the token.
975 - passes by second `about_to_request_buffer_space' breakpoint/lock,
976 leaving the sync region.
978 - checks if the update was effective.
980 - if trace buffer was found full, hits flush_trace_buffer
981 breakpoint, and restarts later afterwards.
983 GDBserver (consumer):
985 - sets `about_to_request_buffer_space' breakpoint/lock.
987 - updates the token unconditionally, using the current buffer
988 control index, since it knows that the IP agent always writes to
989 the rightmost, and due to the breakpoint, at most one IP thread
990 can try to update the trace buffer concurrently to GDBserver, so
991 there will be no danger of trace buffer control index wrap making
992 the IPA write to the same index as GDBserver.
994 - flushes the IP agent's trace buffer completely, and updates the
995 current trace buffer control structure. GDBserver *always* wins.
997 - removes the `about_to_request_buffer_space' breakpoint.
999 The token is stored in the `trace_buffer_ctrl_curr' variable.
1000 Internally, it's bits are defined as:
1002 |-------------+-----+-------------+--------+-------------+--------------|
1003 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1004 |-------------+-----+-------------+--------+-------------+--------------|
1005 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1006 |-------------+-----+-------------+--------+-------------+--------------|
1008 GSB - GDBserver Stamp Bit
1009 PC - Previous Counter
1010 CC - Current Counter
1011 TBCI - Trace Buffer Control Index
1014 An IPA update of `trace_buffer_ctrl_curr' does:
1016 - read CC from the current token, save as PC.
1018 - atomically tries to write PC+1,CC
1020 A GDBserver update of `trace_buffer_ctrl_curr' does:
1022 - reads PC and CC from the current token.
1027 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1028 for the counters described below. The cleared bits are used to
1029 hold the index of the items of the `trace_buffer_ctrl' array that
1031 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1033 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1034 counter, and the `current' counter. */
1036 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1037 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1039 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1040 always stamps this bit as set. */
1041 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1043 #ifdef IN_PROCESS_AGENT
1044 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1045 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1047 # define TRACE_BUFFER_CTRL_CURR \
1048 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1052 /* The GDBserver side agent only needs one instance of this object, as
1053 it doesn't need to sync with itself. Define it as array anyway so
1054 that the rest of the code base doesn't need to care for the
1056 struct trace_buffer_control trace_buffer_ctrl
[1];
1057 # define TRACE_BUFFER_CTRL_CURR 0
1060 /* These are convenience macros used to access the current trace
1061 buffer control in effect. */
1062 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1063 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1064 #define trace_buffer_end_free \
1065 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1066 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1069 /* Macro that returns a pointer to the first traceframe in the buffer. */
1071 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1073 /* Macro that returns a pointer to the next traceframe in the buffer.
1074 If the computed location is beyond the wraparound point, subtract
1075 the offset of the wraparound. */
1077 #define NEXT_TRACEFRAME_1(TF) \
1078 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1080 #define NEXT_TRACEFRAME(TF) \
1081 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1082 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1083 ? (trace_buffer_wrap - trace_buffer_lo) \
1086 /* The difference between these counters represents the total number
1087 of complete traceframes present in the trace buffer. The IP agent
1088 writes to the write count, GDBserver writes to read count. */
1090 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1091 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1093 /* Convenience macro. */
1095 #define traceframe_count \
1096 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1098 /* The count of all traceframes created in the current run, including
1099 ones that were discarded to make room. */
1101 IP_AGENT_EXPORT
int traceframes_created
;
1103 #ifndef IN_PROCESS_AGENT
1105 /* Read-only regions are address ranges whose contents don't change,
1106 and so can be read from target memory even while looking at a trace
1107 frame. Without these, disassembly for instance will likely fail,
1108 because the program code is not usually collected into a trace
1109 frame. This data structure does not need to be very complicated or
1110 particularly efficient, it's only going to be used occasionally,
1111 and only by some commands. */
1113 struct readonly_region
1115 /* The bounds of the region. */
1116 CORE_ADDR start
, end
;
1118 /* Link to the next one. */
1119 struct readonly_region
*next
;
1122 /* Linked list of readonly regions. This list stays in effect from
1123 one tstart to the next. */
1125 static struct readonly_region
*readonly_regions
;
1129 /* The global that controls tracing overall. */
1131 IP_AGENT_EXPORT
int tracing
;
1133 #ifndef IN_PROCESS_AGENT
1135 /* Controls whether tracing should continue after GDB disconnects. */
1137 int disconnected_tracing
;
1139 /* The reason for the last tracing run to have stopped. We initialize
1140 to a distinct string so that GDB can distinguish between "stopped
1141 after running" and "stopped because never run" cases. */
1143 static const char *tracing_stop_reason
= "tnotrun";
1145 static int tracing_stop_tpnum
;
1149 /* Functions local to this file. */
1151 /* Base "class" for tracepoint type specific data to be passed down to
1152 collect_data_at_tracepoint. */
1153 struct tracepoint_hit_ctx
1155 enum tracepoint_type type
;
1158 #ifdef IN_PROCESS_AGENT
1160 /* Fast/jump tracepoint specific data to be passed down to
1161 collect_data_at_tracepoint. */
1162 struct fast_tracepoint_ctx
1164 struct tracepoint_hit_ctx base
;
1166 struct regcache regcache
;
1167 int regcache_initted
;
1168 unsigned char *regspace
;
1170 unsigned char *regs
;
1171 struct tracepoint
*tpoint
;
1174 /* Static tracepoint specific data to be passed down to
1175 collect_data_at_tracepoint. */
1176 struct static_tracepoint_ctx
1178 struct tracepoint_hit_ctx base
;
1180 /* The regcache corresponding to the registers state at the time of
1181 the tracepoint hit. Initialized lazily, from REGS. */
1182 struct regcache regcache
;
1183 int regcache_initted
;
1185 /* The buffer space REGCACHE above uses. We use a separate buffer
1186 instead of letting the regcache malloc for both signal safety and
1187 performance reasons; this is allocated on the stack instead. */
1188 unsigned char *regspace
;
1190 /* The register buffer as passed on by lttng/ust. */
1191 struct registers
*regs
;
1193 /* The "printf" formatter and the args the user passed to the marker
1194 call. We use this to be able to collect "static trace data"
1199 /* The GDB tracepoint matching the probed marker that was "hit". */
1200 struct tracepoint
*tpoint
;
1205 /* Static tracepoint specific data to be passed down to
1206 collect_data_at_tracepoint. */
1207 struct trap_tracepoint_ctx
1209 struct tracepoint_hit_ctx base
;
1211 struct regcache
*regcache
;
1216 #ifndef IN_PROCESS_AGENT
1217 static struct agent_expr
*parse_agent_expr (char **actparm
);
1218 static char *unparse_agent_expr (struct agent_expr
*aexpr
);
1220 static enum eval_result_type
eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1221 struct traceframe
*tframe
,
1222 struct agent_expr
*aexpr
,
1225 static int agent_mem_read (struct traceframe
*tframe
,
1226 unsigned char *to
, CORE_ADDR from
, ULONGEST len
);
1227 static int agent_mem_read_string (struct traceframe
*tframe
,
1228 unsigned char *to
, CORE_ADDR from
,
1230 static int agent_tsv_read (struct traceframe
*tframe
, int n
);
1232 #ifndef IN_PROCESS_AGENT
1233 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1234 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1237 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1238 struct tracepoint
*tpoint
);
1240 #ifndef IN_PROCESS_AGENT
1241 static void clear_readonly_regions (void);
1242 static void clear_installed_tracepoints (void);
1245 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1247 struct tracepoint
*tpoint
);
1248 #ifndef IN_PROCESS_AGENT
1249 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1251 struct tracepoint
*tpoint
, int current_step
);
1252 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1253 CORE_ADDR
*jump_entry
);
1255 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1257 struct tracepoint
*tpoint
,
1258 struct traceframe
*tframe
,
1259 struct tracepoint_action
*taction
);
1261 #ifndef IN_PROCESS_AGENT
1262 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1264 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1265 static void download_tracepoint (struct tracepoint
*);
1266 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1269 #if defined(__GNUC__)
1270 # define memory_barrier() asm volatile ("" : : : "memory")
1272 # define memory_barrier() do {} while (0)
1275 /* We only build the IPA if this builtin is supported, and there are
1276 no uses of this in GDBserver itself, so we're safe in defining this
1278 #define cmpxchg(mem, oldval, newval) \
1279 __sync_val_compare_and_swap (mem, oldval, newval)
1281 /* The size in bytes of the buffer used to talk to the IPA helper
1283 #define CMD_BUF_SIZE 1024
1285 /* Record that an error occurred during expression evaluation. */
1288 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1289 enum eval_result_type rtype
)
1291 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1292 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1294 #ifdef IN_PROCESS_AGENT
1295 /* Only record the first error we get. */
1296 if (cmpxchg (&expr_eval_result
,
1298 rtype
) != expr_eval_no_error
)
1301 if (expr_eval_result
!= expr_eval_no_error
)
1305 error_tracepoint
= tpoint
;
1308 /* Trace buffer management. */
1311 clear_trace_buffer (void)
1313 trace_buffer_start
= trace_buffer_lo
;
1314 trace_buffer_free
= trace_buffer_lo
;
1315 trace_buffer_end_free
= trace_buffer_hi
;
1316 trace_buffer_wrap
= trace_buffer_hi
;
1317 /* A traceframe with zeroed fields marks the end of trace data. */
1318 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1319 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1320 traceframe_read_count
= traceframe_write_count
= 0;
1321 traceframes_created
= 0;
1324 #ifndef IN_PROCESS_AGENT
1327 clear_inferior_trace_buffer (void)
1329 CORE_ADDR ipa_trace_buffer_lo
;
1330 CORE_ADDR ipa_trace_buffer_hi
;
1331 struct traceframe ipa_traceframe
= { 0 };
1332 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1334 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1335 &ipa_trace_buffer_lo
);
1336 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1337 &ipa_trace_buffer_hi
);
1339 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1340 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1341 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1342 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1344 /* A traceframe with zeroed fields marks the end of trace data. */
1345 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1346 (unsigned char *) &ipa_trace_buffer_ctrl
,
1347 sizeof (ipa_trace_buffer_ctrl
));
1349 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1351 /* A traceframe with zeroed fields marks the end of trace data. */
1352 write_inferior_memory (ipa_trace_buffer_lo
,
1353 (unsigned char *) &ipa_traceframe
,
1354 sizeof (ipa_traceframe
));
1356 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1357 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1358 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1364 init_trace_buffer (unsigned char *buf
, int bufsize
)
1366 trace_buffer_lo
= buf
;
1367 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1369 clear_trace_buffer ();
1372 #ifdef IN_PROCESS_AGENT
1374 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1375 about_to_request_buffer_space (void)
1377 /* GDBserver places breakpoint here while it goes about to flush
1378 data at random times. */
1379 UNKNOWN_SIDE_EFFECTS();
1384 /* Carve out a piece of the trace buffer, returning NULL in case of
1388 trace_buffer_alloc (size_t amt
)
1390 unsigned char *rslt
;
1391 struct trace_buffer_control
*tbctrl
;
1393 #ifdef IN_PROCESS_AGENT
1394 unsigned int prev
, prev_filtered
;
1395 unsigned int commit_count
;
1396 unsigned int commit
;
1397 unsigned int readout
;
1399 struct traceframe
*oldest
;
1400 unsigned char *new_start
;
1403 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1404 (long) amt
, (long) sizeof (struct traceframe
));
1406 /* Account for the EOB marker. */
1407 amt
+= sizeof (struct traceframe
);
1409 #ifdef IN_PROCESS_AGENT
1413 /* Read the current token and extract the index to try to write to,
1414 storing it in CURR. */
1415 prev
= trace_buffer_ctrl_curr
;
1416 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1417 curr
= prev_filtered
+ 1;
1421 about_to_request_buffer_space ();
1423 /* Start out with a copy of the current state. GDBserver may be
1424 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1425 be able to commit anyway if that happens. */
1426 trace_buffer_ctrl
[curr
]
1427 = trace_buffer_ctrl
[prev_filtered
];
1428 trace_debug ("trying curr=%u", curr
);
1430 /* The GDBserver's agent doesn't need all that syncing, and always
1431 updates TCB 0 (there's only one, mind you). */
1434 tbctrl
= &trace_buffer_ctrl
[curr
];
1436 /* Offsets are easier to grok for debugging than raw addresses,
1437 especially for the small trace buffer sizes that are useful for
1439 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1441 (int) (tbctrl
->start
- trace_buffer_lo
),
1442 (int) (tbctrl
->free
- trace_buffer_lo
),
1443 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1444 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1445 (int) (trace_buffer_hi
- trace_buffer_lo
));
1447 /* The algorithm here is to keep trying to get a contiguous block of
1448 the requested size, possibly discarding older traceframes to free
1449 up space. Since free space might come in one or two pieces,
1450 depending on whether discarded traceframes wrapped around at the
1451 high end of the buffer, we test both pieces after each
1455 /* First, if we have two free parts, try the upper one first. */
1456 if (tbctrl
->end_free
< tbctrl
->free
)
1458 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1459 /* We have enough in the upper part. */
1463 /* Our high part of free space wasn't enough. Give up
1464 on it for now, set wraparound. We will recover the
1465 space later, if/when the wrapped-around traceframe is
1467 trace_debug ("Upper part too small, setting wraparound");
1468 tbctrl
->wrap
= tbctrl
->free
;
1469 tbctrl
->free
= trace_buffer_lo
;
1473 /* The normal case. */
1474 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1477 #ifdef IN_PROCESS_AGENT
1478 /* The IP Agent's buffer is always circular. It isn't used
1479 currently, but `circular_trace_buffer' could represent
1480 GDBserver's mode. If we didn't find space, ask GDBserver to
1483 flush_trace_buffer ();
1487 trace_debug ("gdbserver flushed buffer, retrying");
1491 /* GDBserver cancelled the tracing. Bail out as well. */
1494 /* If we're here, then neither part is big enough, and
1495 non-circular trace buffers are now full. */
1496 if (!circular_trace_buffer
)
1498 trace_debug ("Not enough space in the trace buffer");
1502 trace_debug ("Need more space in the trace buffer");
1504 /* If we have a circular buffer, we can try discarding the
1505 oldest traceframe and see if that helps. */
1506 oldest
= FIRST_TRACEFRAME ();
1507 if (oldest
->tpnum
== 0)
1509 /* Not good; we have no traceframes to free. Perhaps we're
1510 asking for a block that is larger than the buffer? In
1511 any case, give up. */
1512 trace_debug ("No traceframes to discard");
1516 /* We don't run this code in the in-process agent currently.
1517 E.g., we could leave the in-process agent in autonomous
1518 circular mode if we only have fast tracepoints. If we do
1519 that, then this bit becomes racy with GDBserver, which also
1520 writes to this counter. */
1521 --traceframe_write_count
;
1523 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1524 /* If we freed the traceframe that wrapped around, go back
1525 to the non-wrap case. */
1526 if (new_start
< tbctrl
->start
)
1528 trace_debug ("Discarding past the wraparound");
1529 tbctrl
->wrap
= trace_buffer_hi
;
1531 tbctrl
->start
= new_start
;
1532 tbctrl
->end_free
= tbctrl
->start
;
1534 trace_debug ("Discarded a traceframe\n"
1535 "Trace buffer [%d], start=%d free=%d "
1536 "endfree=%d wrap=%d hi=%d",
1538 (int) (tbctrl
->start
- trace_buffer_lo
),
1539 (int) (tbctrl
->free
- trace_buffer_lo
),
1540 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1541 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1542 (int) (trace_buffer_hi
- trace_buffer_lo
));
1544 /* Now go back around the loop. The discard might have resulted
1545 in either one or two pieces of free space, so we want to try
1546 both before freeing any more traceframes. */
1550 /* If we get here, we know we can provide the asked-for space. */
1552 rslt
= tbctrl
->free
;
1554 /* Adjust the request back down, now that we know we have space for
1555 the marker, but don't commit to AMT yet, we may still need to
1556 restart the operation if GDBserver touches the trace buffer
1557 (obviously only important in the in-process agent's version). */
1558 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1560 /* Or not. If GDBserver changed the trace buffer behind our back,
1561 we get to restart a new allocation attempt. */
1563 #ifdef IN_PROCESS_AGENT
1564 /* Build the tentative token. */
1565 commit_count
= (((prev
& 0x0007ff00) + 0x100) & 0x0007ff00);
1566 commit
= (((prev
& 0x0007ff00) << 12)
1570 /* Try to commit it. */
1571 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1572 if (readout
!= prev
)
1574 trace_debug ("GDBserver has touched the trace buffer, restarting."
1575 " (prev=%08x, commit=%08x, readout=%08x)",
1576 prev
, commit
, readout
);
1580 /* Hold your horses here. Even if that change was committed,
1581 GDBserver could come in, and clobber it. We need to hold to be
1582 able to tell if GDBserver clobbers before or after we committed
1583 the change. Whenever GDBserver goes about touching the IPA
1584 buffer, it sets a breakpoint in this routine, so we have a sync
1586 about_to_request_buffer_space ();
1588 /* Check if the change has been effective, even if GDBserver stopped
1589 us at the breakpoint. */
1592 unsigned int refetch
;
1596 refetch
= trace_buffer_ctrl_curr
;
1598 if ((refetch
== commit
1599 || ((refetch
& 0x7ff00000) >> 12) == commit_count
))
1602 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1603 "readout=%08x, refetch=%08x)",
1604 prev
, commit
, readout
, refetch
);
1608 trace_debug ("GDBserver has touched the trace buffer, not effective."
1609 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1610 prev
, commit
, readout
, refetch
);
1616 /* We have a new piece of the trace buffer. Hurray! */
1618 /* Add an EOB marker just past this allocation. */
1619 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1620 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1622 /* Adjust the request back down, now that we know we have space for
1624 amt
-= sizeof (struct traceframe
);
1628 trace_debug ("Allocated %d bytes", (int) amt
);
1629 trace_debug ("Trace buffer [%d] start=%d free=%d "
1630 "endfree=%d wrap=%d hi=%d",
1632 (int) (tbctrl
->start
- trace_buffer_lo
),
1633 (int) (tbctrl
->free
- trace_buffer_lo
),
1634 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1635 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1636 (int) (trace_buffer_hi
- trace_buffer_lo
));
1642 #ifndef IN_PROCESS_AGENT
1644 /* Return the total free space. This is not necessarily the largest
1645 block we can allocate, because of the two-part case. */
1650 if (trace_buffer_free
<= trace_buffer_end_free
)
1651 return trace_buffer_end_free
- trace_buffer_free
;
1653 return ((trace_buffer_end_free
- trace_buffer_lo
)
1654 + (trace_buffer_hi
- trace_buffer_free
));
1657 /* An 'S' in continuation packets indicates remainder are for
1660 static int seen_step_action_flag
;
1662 /* Create a tracepoint (location) with given number and address. Add this
1663 new tracepoint to list and sort this list. */
1665 static struct tracepoint
*
1666 add_tracepoint (int num
, CORE_ADDR addr
)
1668 struct tracepoint
*tpoint
, **tp_next
;
1670 tpoint
= xmalloc (sizeof (struct tracepoint
));
1671 tpoint
->number
= num
;
1672 tpoint
->address
= addr
;
1673 tpoint
->numactions
= 0;
1674 tpoint
->actions
= NULL
;
1675 tpoint
->actions_str
= NULL
;
1676 tpoint
->cond
= NULL
;
1677 tpoint
->num_step_actions
= 0;
1678 tpoint
->step_actions
= NULL
;
1679 tpoint
->step_actions_str
= NULL
;
1680 /* Start all off as regular (slow) tracepoints. */
1681 tpoint
->type
= trap_tracepoint
;
1682 tpoint
->orig_size
= -1;
1683 tpoint
->source_strings
= NULL
;
1684 tpoint
->compiled_cond
= 0;
1685 tpoint
->handle
= NULL
;
1686 tpoint
->next
= NULL
;
1688 /* Find a place to insert this tracepoint into list in order to keep
1689 the tracepoint list still in the ascending order. There may be
1690 multiple tracepoints at the same address as TPOINT's, and this
1691 guarantees TPOINT is inserted after all the tracepoints which are
1692 set at the same address. For example, fast tracepoints A, B, C are
1693 set at the same address, and D is to be insert at the same place as
1696 -->| A |--> | B |-->| C |->...
1698 One jump pad was created for tracepoint A, B, and C, and the target
1699 address of A is referenced/used in jump pad. So jump pad will let
1700 inferior jump to A. If D is inserted in front of A, like this,
1702 -->| D |-->| A |--> | B |-->| C |->...
1704 without updating jump pad, D is not reachable during collect, which
1705 is wrong. As we can see, the order of B, C and D doesn't matter, but
1706 A should always be the `first' one. */
1707 for (tp_next
= &tracepoints
;
1708 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1709 tp_next
= &(*tp_next
)->next
)
1711 tpoint
->next
= *tp_next
;
1713 last_tracepoint
= tpoint
;
1715 seen_step_action_flag
= 0;
1720 #ifndef IN_PROCESS_AGENT
1722 /* Return the tracepoint with the given number and address, or NULL. */
1724 static struct tracepoint
*
1725 find_tracepoint (int id
, CORE_ADDR addr
)
1727 struct tracepoint
*tpoint
;
1729 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1730 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1736 /* There may be several tracepoints with the same number (because they
1737 are "locations", in GDB parlance); return the next one after the
1738 given tracepoint, or search from the beginning of the list if the
1739 first argument is NULL. */
1741 static struct tracepoint
*
1742 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1744 struct tracepoint
*tpoint
;
1747 tpoint
= prev_tp
->next
;
1749 tpoint
= tracepoints
;
1750 for (; tpoint
; tpoint
= tpoint
->next
)
1751 if (tpoint
->number
== num
)
1760 save_string (const char *str
, size_t len
)
1764 s
= xmalloc (len
+ 1);
1765 memcpy (s
, str
, len
);
1771 /* Append another action to perform when the tracepoint triggers. */
1774 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1780 seen_step_action_flag
= 1;
1788 char *act_start
= act
;
1789 struct tracepoint_action
*action
= NULL
;
1795 struct collect_memory_action
*maction
;
1799 maction
= xmalloc (sizeof *maction
);
1800 maction
->base
.type
= *act
;
1801 action
= &maction
->base
;
1804 is_neg
= (*act
== '-');
1807 act
= unpack_varlen_hex (act
, &basereg
);
1809 act
= unpack_varlen_hex (act
, &maction
->addr
);
1811 act
= unpack_varlen_hex (act
, &maction
->len
);
1812 maction
->basereg
= (is_neg
1815 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1816 pulongest (maction
->len
),
1817 paddress (maction
->addr
), maction
->basereg
);
1822 struct collect_registers_action
*raction
;
1824 raction
= xmalloc (sizeof *raction
);
1825 raction
->base
.type
= *act
;
1826 action
= &raction
->base
;
1828 trace_debug ("Want to collect registers");
1830 /* skip past hex digits of mask for now */
1831 while (isxdigit(*act
))
1837 struct collect_static_trace_data_action
*raction
;
1839 raction
= xmalloc (sizeof *raction
);
1840 raction
->base
.type
= *act
;
1841 action
= &raction
->base
;
1843 trace_debug ("Want to collect static trace data");
1848 trace_debug ("Unexpected step action, ignoring");
1853 struct eval_expr_action
*xaction
;
1855 xaction
= xmalloc (sizeof (*xaction
));
1856 xaction
->base
.type
= *act
;
1857 action
= &xaction
->base
;
1859 trace_debug ("Want to evaluate expression");
1860 xaction
->expr
= parse_agent_expr (&act
);
1864 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1873 if (seen_step_action_flag
)
1875 tpoint
->num_step_actions
++;
1877 tpoint
->step_actions
1878 = xrealloc (tpoint
->step_actions
,
1879 (sizeof (*tpoint
->step_actions
)
1880 * tpoint
->num_step_actions
));
1881 tpoint
->step_actions_str
1882 = xrealloc (tpoint
->step_actions_str
,
1883 (sizeof (*tpoint
->step_actions_str
)
1884 * tpoint
->num_step_actions
));
1885 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1886 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1887 = save_string (act_start
, act
- act_start
);
1891 tpoint
->numactions
++;
1893 = xrealloc (tpoint
->actions
,
1894 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1896 = xrealloc (tpoint
->actions_str
,
1897 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1898 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1899 tpoint
->actions_str
[tpoint
->numactions
- 1]
1900 = save_string (act_start
, act
- act_start
);
1907 /* Find or create a trace state variable with the given number. */
1909 static struct trace_state_variable
*
1910 get_trace_state_variable (int num
)
1912 struct trace_state_variable
*tsv
;
1914 #ifdef IN_PROCESS_AGENT
1915 /* Search for an existing variable. */
1916 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1917 if (tsv
->number
== num
)
1921 /* Search for an existing variable. */
1922 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1923 if (tsv
->number
== num
)
1929 /* Find or create a trace state variable with the given number. */
1931 static struct trace_state_variable
*
1932 create_trace_state_variable (int num
, int gdb
)
1934 struct trace_state_variable
*tsv
;
1936 tsv
= get_trace_state_variable (num
);
1940 /* Create a new variable. */
1941 tsv
= xmalloc (sizeof (struct trace_state_variable
));
1943 tsv
->initial_value
= 0;
1947 #ifdef IN_PROCESS_AGENT
1950 tsv
->next
= alloced_trace_state_variables
;
1951 alloced_trace_state_variables
= tsv
;
1956 tsv
->next
= trace_state_variables
;
1957 trace_state_variables
= tsv
;
1962 IP_AGENT_EXPORT LONGEST
1963 get_trace_state_variable_value (int num
)
1965 struct trace_state_variable
*tsv
;
1967 tsv
= get_trace_state_variable (num
);
1971 trace_debug ("No trace state variable %d, skipping value get", num
);
1975 /* Call a getter function if we have one. While it's tempting to
1976 set up something to only call the getter once per tracepoint hit,
1977 it could run afoul of thread races. Better to let the getter
1978 handle it directly, if necessary to worry about it. */
1980 tsv
->value
= (tsv
->getter
) ();
1982 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1983 num
, plongest (tsv
->value
));
1988 IP_AGENT_EXPORT
void
1989 set_trace_state_variable_value (int num
, LONGEST val
)
1991 struct trace_state_variable
*tsv
;
1993 tsv
= get_trace_state_variable (num
);
1997 trace_debug ("No trace state variable %d, skipping value set", num
);
2005 set_trace_state_variable_name (int num
, const char *name
)
2007 struct trace_state_variable
*tsv
;
2009 tsv
= get_trace_state_variable (num
);
2013 trace_debug ("No trace state variable %d, skipping name set", num
);
2017 tsv
->name
= (char *) name
;
2021 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2023 struct trace_state_variable
*tsv
;
2025 tsv
= get_trace_state_variable (num
);
2029 trace_debug ("No trace state variable %d, skipping getter set", num
);
2033 tsv
->getter
= getter
;
2036 /* Add a raw traceframe for the given tracepoint. */
2038 static struct traceframe
*
2039 add_traceframe (struct tracepoint
*tpoint
)
2041 struct traceframe
*tframe
;
2043 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2048 tframe
->tpnum
= tpoint
->number
;
2049 tframe
->data_size
= 0;
2054 /* Add a block to the traceframe currently being worked on. */
2056 static unsigned char *
2057 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2059 unsigned char *block
;
2064 block
= trace_buffer_alloc (amt
);
2069 tframe
->data_size
+= amt
;
2074 /* Flag that the current traceframe is finished. */
2077 finish_traceframe (struct traceframe
*tframe
)
2079 ++traceframe_write_count
;
2080 ++traceframes_created
;
2083 #ifndef IN_PROCESS_AGENT
2085 /* Given a traceframe number NUM, find the NUMth traceframe in the
2088 static struct traceframe
*
2089 find_traceframe (int num
)
2091 struct traceframe
*tframe
;
2094 for (tframe
= FIRST_TRACEFRAME ();
2096 tframe
= NEXT_TRACEFRAME (tframe
))
2107 get_traceframe_address (struct traceframe
*tframe
)
2110 struct tracepoint
*tpoint
;
2112 addr
= traceframe_get_pc (tframe
);
2117 /* Fallback strategy, will be incorrect for while-stepping frames
2118 and multi-location tracepoints. */
2119 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2120 return tpoint
->address
;
2123 /* Search for the next traceframe whose address is inside or outside
2126 static struct traceframe
*
2127 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2130 struct traceframe
*tframe
;
2133 *tfnump
= current_traceframe
+ 1;
2134 tframe
= find_traceframe (*tfnump
);
2135 /* The search is not supposed to wrap around. */
2142 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2144 tfaddr
= get_traceframe_address (tframe
);
2146 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2147 : (lo
> tfaddr
|| tfaddr
> hi
))
2156 /* Search for the next traceframe recorded by the given tracepoint.
2157 Note that for multi-location tracepoints, this will find whatever
2158 location appears first. */
2160 static struct traceframe
*
2161 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2163 struct traceframe
*tframe
;
2165 *tfnump
= current_traceframe
+ 1;
2166 tframe
= find_traceframe (*tfnump
);
2167 /* The search is not supposed to wrap around. */
2174 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2176 if (tframe
->tpnum
== num
)
2187 #ifndef IN_PROCESS_AGENT
2189 /* Clear all past trace state. */
2192 cmd_qtinit (char *packet
)
2194 struct trace_state_variable
*tsv
, *prev
, *next
;
2196 /* Make sure we don't try to read from a trace frame. */
2197 current_traceframe
= -1;
2199 trace_debug ("Initializing the trace");
2201 clear_installed_tracepoints ();
2202 clear_readonly_regions ();
2205 last_tracepoint
= NULL
;
2207 /* Clear out any leftover trace state variables. Ones with target
2208 defined getters should be kept however. */
2210 tsv
= trace_state_variables
;
2213 trace_debug ("Looking at var %d", tsv
->number
);
2214 if (tsv
->getter
== NULL
)
2220 trace_state_variables
= next
;
2221 trace_debug ("Deleting var %d", tsv
->number
);
2232 clear_trace_buffer ();
2233 clear_inferior_trace_buffer ();
2238 /* Unprobe the UST marker at ADDRESS. */
2241 unprobe_marker_at (CORE_ADDR address
)
2243 char cmd
[CMD_BUF_SIZE
];
2245 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2246 run_inferior_command (cmd
);
2249 /* Restore the program to its pre-tracing state. This routine may be called
2250 in error situations, so it needs to be careful about only restoring
2251 from known-valid bits. */
2254 clear_installed_tracepoints (void)
2256 struct tracepoint
*tpoint
;
2257 struct tracepoint
*prev_stpoint
;
2260 cancel_breakpoints ();
2262 prev_stpoint
= NULL
;
2264 /* Restore any bytes overwritten by tracepoints. */
2265 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2267 /* Catch the case where we might try to remove a tracepoint that
2268 was never actually installed. */
2269 if (tpoint
->handle
== NULL
)
2271 trace_debug ("Tracepoint %d at 0x%s was "
2272 "never installed, nothing to clear",
2273 tpoint
->number
, paddress (tpoint
->address
));
2277 switch (tpoint
->type
)
2279 case trap_tracepoint
:
2280 delete_breakpoint (tpoint
->handle
);
2282 case fast_tracepoint
:
2283 delete_fast_tracepoint_jump (tpoint
->handle
);
2285 case static_tracepoint
:
2286 if (prev_stpoint
!= NULL
2287 && prev_stpoint
->address
== tpoint
->address
)
2288 /* Nothing to do. We already unprobed a tracepoint set at
2289 this marker address (and there can only be one probe
2294 unprobe_marker_at (tpoint
->address
);
2295 prev_stpoint
= tpoint
;
2300 tpoint
->handle
= NULL
;
2306 /* Parse a packet that defines a tracepoint. */
2309 cmd_qtdp (char *own_buf
)
2312 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2313 int trail_hyphen
= 0;
2317 struct tracepoint
*tpoint
;
2319 char *packet
= own_buf
;
2321 packet
+= strlen ("QTDP:");
2323 /* A hyphen at the beginning marks a packet specifying actions for a
2324 tracepoint already supplied. */
2331 packet
= unpack_varlen_hex (packet
, &num
);
2332 ++packet
; /* skip a colon */
2333 packet
= unpack_varlen_hex (packet
, &addr
);
2334 ++packet
; /* skip a colon */
2336 /* See if we already have this tracepoint. */
2337 tpoint
= find_tracepoint (num
, addr
);
2341 /* Duplicate tracepoints are never allowed. */
2344 trace_debug ("Tracepoint error: tracepoint %d"
2345 " at 0x%s already exists",
2346 (int) num
, paddress (addr
));
2347 write_enn (own_buf
);
2351 tpoint
= add_tracepoint (num
, addr
);
2353 tpoint
->enabled
= (*packet
== 'E');
2354 ++packet
; /* skip 'E' */
2355 ++packet
; /* skip a colon */
2356 packet
= unpack_varlen_hex (packet
, &count
);
2357 tpoint
->step_count
= count
;
2358 ++packet
; /* skip a colon */
2359 packet
= unpack_varlen_hex (packet
, &count
);
2360 tpoint
->pass_count
= count
;
2361 /* See if we have any of the additional optional fields. */
2362 while (*packet
== ':')
2367 tpoint
->type
= fast_tracepoint
;
2369 packet
= unpack_varlen_hex (packet
, &count
);
2370 tpoint
->orig_size
= count
;
2372 else if (*packet
== 'S')
2374 tpoint
->type
= static_tracepoint
;
2377 else if (*packet
== 'X')
2379 actparm
= (char *) packet
;
2380 tpoint
->cond
= parse_agent_expr (&actparm
);
2383 else if (*packet
== '-')
2385 else if (*packet
== '\0')
2388 trace_debug ("Unknown optional tracepoint field");
2393 trace_debug ("Also has actions\n");
2396 trace_debug ("Defined %stracepoint %d at 0x%s, "
2397 "enabled %d step %ld pass %ld",
2398 tpoint
->type
== fast_tracepoint
? "fast "
2400 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2401 tpoint
->step_count
, tpoint
->pass_count
);
2404 add_tracepoint_action (tpoint
, packet
);
2407 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2408 (int) num
, paddress (addr
));
2409 write_enn (own_buf
);
2413 /* Install tracepoint during tracing only once for each tracepoint location.
2414 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2415 determine the last QTDP packet for one tracepoint location by checking
2416 trailing hyphen in QTDP packet. */
2417 if (tracing
&& !trail_hyphen
)
2419 /* Pause all threads temporarily while we patch tracepoints. */
2422 /* download_tracepoint will update global `tracepoints'
2423 list, so it is unsafe to leave threads in jump pad. */
2424 stabilize_threads ();
2426 /* Freeze threads. */
2429 download_tracepoint (tpoint
);
2430 install_tracepoint (tpoint
, own_buf
);
2440 cmd_qtdpsrc (char *own_buf
)
2442 ULONGEST num
, addr
, start
, slen
;
2443 struct tracepoint
*tpoint
;
2444 char *packet
= own_buf
;
2445 char *saved
, *srctype
, *src
;
2447 struct source_string
*last
, *newlast
;
2449 packet
+= strlen ("QTDPsrc:");
2451 packet
= unpack_varlen_hex (packet
, &num
);
2452 ++packet
; /* skip a colon */
2453 packet
= unpack_varlen_hex (packet
, &addr
);
2454 ++packet
; /* skip a colon */
2456 /* See if we already have this tracepoint. */
2457 tpoint
= find_tracepoint (num
, addr
);
2461 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2462 (int) num
, paddress (addr
));
2463 write_enn (own_buf
);
2468 packet
= strchr (packet
, ':');
2469 srctype
= xmalloc (packet
- saved
+ 1);
2470 memcpy (srctype
, saved
, packet
- saved
);
2471 srctype
[packet
- saved
] = '\0';
2473 packet
= unpack_varlen_hex (packet
, &start
);
2474 ++packet
; /* skip a colon */
2475 packet
= unpack_varlen_hex (packet
, &slen
);
2476 ++packet
; /* skip a colon */
2477 src
= xmalloc (slen
+ 1);
2478 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2481 newlast
= xmalloc (sizeof (struct source_string
));
2482 newlast
->type
= srctype
;
2484 newlast
->next
= NULL
;
2485 /* Always add a source string to the end of the list;
2486 this keeps sequences of actions/commands in the right
2488 if (tpoint
->source_strings
)
2490 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2492 last
->next
= newlast
;
2495 tpoint
->source_strings
= newlast
;
2501 cmd_qtdv (char *own_buf
)
2503 ULONGEST num
, val
, builtin
;
2506 struct trace_state_variable
*tsv
;
2507 char *packet
= own_buf
;
2509 packet
+= strlen ("QTDV:");
2511 packet
= unpack_varlen_hex (packet
, &num
);
2512 ++packet
; /* skip a colon */
2513 packet
= unpack_varlen_hex (packet
, &val
);
2514 ++packet
; /* skip a colon */
2515 packet
= unpack_varlen_hex (packet
, &builtin
);
2516 ++packet
; /* skip a colon */
2518 nbytes
= strlen (packet
) / 2;
2519 varname
= xmalloc (nbytes
+ 1);
2520 nbytes
= unhexify (varname
, packet
, nbytes
);
2521 varname
[nbytes
] = '\0';
2523 tsv
= create_trace_state_variable (num
, 1);
2524 tsv
->initial_value
= (LONGEST
) val
;
2525 tsv
->name
= varname
;
2527 set_trace_state_variable_value (num
, (LONGEST
) val
);
2533 cmd_qtenable_disable (char *own_buf
, int enable
)
2535 char *packet
= own_buf
;
2537 struct tracepoint
*tp
;
2539 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2540 packet
= unpack_varlen_hex (packet
, &num
);
2541 ++packet
; /* skip a colon */
2542 packet
= unpack_varlen_hex (packet
, &addr
);
2544 tp
= find_tracepoint (num
, addr
);
2548 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2550 trace_debug ("Tracepoint %d at 0x%s is already %s",
2551 (int) num
, paddress (addr
),
2552 enable
? "enabled" : "disabled");
2557 trace_debug ("%s tracepoint %d at 0x%s",
2558 enable
? "Enabling" : "Disabling",
2559 (int) num
, paddress (addr
));
2561 tp
->enabled
= enable
;
2563 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2566 int offset
= offsetof (struct tracepoint
, enabled
);
2567 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2569 ret
= prepare_to_access_memory ();
2572 trace_debug ("Failed to temporarily stop inferior threads");
2573 write_enn (own_buf
);
2577 ret
= write_inferior_integer (obj_addr
, enable
);
2578 done_accessing_memory ();
2582 trace_debug ("Cannot write enabled flag into "
2583 "inferior process memory");
2584 write_enn (own_buf
);
2593 trace_debug ("Tracepoint %d at 0x%s not found",
2594 (int) num
, paddress (addr
));
2595 write_enn (own_buf
);
2600 cmd_qtv (char *own_buf
)
2605 char *packet
= own_buf
;
2607 packet
+= strlen ("qTV:");
2608 unpack_varlen_hex (packet
, &num
);
2610 if (current_traceframe
>= 0)
2612 err
= traceframe_read_tsv ((int) num
, &val
);
2615 strcpy (own_buf
, "U");
2619 /* Only make tsv's be undefined before the first trace run. After a
2620 trace run is over, the user might want to see the last value of
2621 the tsv, and it might not be available in a traceframe. */
2622 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2624 strcpy (own_buf
, "U");
2628 val
= get_trace_state_variable_value (num
);
2630 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2633 /* Clear out the list of readonly regions. */
2636 clear_readonly_regions (void)
2638 struct readonly_region
*roreg
;
2640 while (readonly_regions
)
2642 roreg
= readonly_regions
;
2643 readonly_regions
= readonly_regions
->next
;
2648 /* Parse the collection of address ranges whose contents GDB believes
2649 to be unchanging and so can be read directly from target memory
2650 even while looking at a traceframe. */
2653 cmd_qtro (char *own_buf
)
2655 ULONGEST start
, end
;
2656 struct readonly_region
*roreg
;
2657 char *packet
= own_buf
;
2659 trace_debug ("Want to mark readonly regions");
2661 clear_readonly_regions ();
2663 packet
+= strlen ("QTro");
2665 while (*packet
== ':')
2667 ++packet
; /* skip a colon */
2668 packet
= unpack_varlen_hex (packet
, &start
);
2669 ++packet
; /* skip a comma */
2670 packet
= unpack_varlen_hex (packet
, &end
);
2671 roreg
= xmalloc (sizeof (struct readonly_region
));
2672 roreg
->start
= start
;
2674 roreg
->next
= readonly_regions
;
2675 readonly_regions
= roreg
;
2676 trace_debug ("Added readonly region from 0x%s to 0x%s",
2677 paddress (roreg
->start
), paddress (roreg
->end
));
2683 /* Test to see if the given range is in our list of readonly ranges.
2684 We only test for being entirely within a range, GDB is not going to
2685 send a single memory packet that spans multiple regions. */
2688 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2690 struct readonly_region
*roreg
;
2692 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2693 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2699 /* The maximum size of a jump pad entry. */
2700 static const int max_jump_pad_size
= 0x100;
2702 static CORE_ADDR gdb_jump_pad_head
;
2704 /* Return the address of the next free jump space. */
2707 get_jump_space_head (void)
2709 if (gdb_jump_pad_head
== 0)
2711 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2712 &gdb_jump_pad_head
))
2713 fatal ("error extracting jump_pad_buffer");
2716 return gdb_jump_pad_head
;
2719 /* Reserve USED bytes from the jump space. */
2722 claim_jump_space (ULONGEST used
)
2724 trace_debug ("claim_jump_space reserves %s bytes at %s",
2725 pulongest (used
), paddress (gdb_jump_pad_head
));
2726 gdb_jump_pad_head
+= used
;
2729 static CORE_ADDR trampoline_buffer_head
= 0;
2730 static CORE_ADDR trampoline_buffer_tail
;
2732 /* Reserve USED bytes from the trampoline buffer and return the
2733 address of the start of the reserved space in TRAMPOLINE. Returns
2734 non-zero if the space is successfully claimed. */
2737 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2739 if (!trampoline_buffer_head
)
2741 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2742 &trampoline_buffer_tail
))
2744 fatal ("error extracting trampoline_buffer");
2748 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2749 &trampoline_buffer_head
))
2751 fatal ("error extracting trampoline_buffer_end");
2756 /* Start claiming space from the top of the trampoline space. If
2757 the space is located at the bottom of the virtual address space,
2758 this reduces the possibility that corruption will occur if a null
2759 pointer is used to write to memory. */
2760 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2762 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2767 trampoline_buffer_head
-= used
;
2769 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2770 pulongest (used
), paddress (trampoline_buffer_head
));
2772 *trampoline
= trampoline_buffer_head
;
2776 /* Returns non-zero if there is space allocated for use in trampolines
2777 for fast tracepoints. */
2780 have_fast_tracepoint_trampoline_buffer (char *buf
)
2782 CORE_ADDR trampoline_end
, errbuf
;
2784 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2787 fatal ("error extracting trampoline_buffer_end");
2794 strcpy (buf
, "was claiming");
2795 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
2798 fatal ("error extracting errbuf");
2802 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
2805 return trampoline_end
!= 0;
2808 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2809 the command fails, or 0 otherwise. If the command ran
2810 successfully, but probing the marker failed, ERROUT will be filled
2811 with the error to reply to GDB, and -1 is also returned. This
2812 allows directly passing IPA errors to GDB. */
2815 probe_marker_at (CORE_ADDR address
, char *errout
)
2817 char cmd
[CMD_BUF_SIZE
];
2820 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
2821 err
= run_inferior_command (cmd
);
2827 strcpy (errout
, cmd
);
2836 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
2838 to
->jump_pad
= from
->jump_pad
;
2839 to
->jump_pad_end
= from
->jump_pad_end
;
2840 to
->trampoline
= from
->trampoline
;
2841 to
->trampoline_end
= from
->trampoline_end
;
2842 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
2843 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
2844 to
->handle
= from
->handle
;
2846 gdb_assert (from
->handle
);
2847 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
2850 #define MAX_JUMP_SIZE 20
2852 /* Install fast tracepoint. Return 0 if successful, otherwise return
2856 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
2858 CORE_ADDR jentry
, jump_entry
;
2859 CORE_ADDR trampoline
;
2860 ULONGEST trampoline_size
;
2862 /* The jump to the jump pad of the last fast tracepoint
2864 unsigned char fjump
[MAX_JUMP_SIZE
];
2865 ULONGEST fjump_size
;
2867 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
2869 trace_debug ("Requested a fast tracepoint on an instruction "
2870 "that is of less than the minimum length.");
2874 jentry
= jump_entry
= get_jump_space_head ();
2877 trampoline_size
= 0;
2879 /* Install the jump pad. */
2880 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
2882 ipa_sym_addrs
.addr_gdb_collect
,
2883 ipa_sym_addrs
.addr_collecting
,
2886 &trampoline
, &trampoline_size
,
2888 &tpoint
->adjusted_insn_addr
,
2889 &tpoint
->adjusted_insn_addr_end
,
2896 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
2899 if (tpoint
->handle
!= NULL
)
2901 tpoint
->jump_pad
= jump_entry
;
2902 tpoint
->jump_pad_end
= jentry
;
2903 tpoint
->trampoline
= trampoline
;
2904 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
2906 /* Pad to 8-byte alignment. */
2907 jentry
= ((jentry
+ 7) & ~0x7);
2908 claim_jump_space (jentry
- jump_entry
);
2915 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
2918 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
2920 tpoint
->handle
= NULL
;
2923 if (tpoint
->type
== trap_tracepoint
)
2925 /* Tracepoints are installed as memory breakpoints. Just go
2926 ahead and install the trap. The breakpoints module
2927 handles duplicated breakpoints, and the memory read
2928 routine handles un-patching traps from memory reads. */
2929 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
2930 tracepoint_handler
);
2932 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
2934 struct tracepoint
*tp
;
2936 if (!in_process_agent_loaded ())
2938 trace_debug ("Requested a %s tracepoint, but fast "
2939 "tracepoints aren't supported.",
2940 tpoint
->type
== static_tracepoint
? "static" : "fast");
2941 write_e_ipa_not_loaded (own_buf
);
2944 if (tpoint
->type
== static_tracepoint
&& !in_process_agent_loaded_ust ())
2946 trace_debug ("Requested a static tracepoint, but static "
2947 "tracepoints are not supported.");
2948 write_e_ust_not_loaded (own_buf
);
2952 /* Find another fast or static tracepoint at the same address. */
2953 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2955 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2956 && tp
->number
!= tpoint
->number
)
2960 if (tpoint
->type
== fast_tracepoint
)
2962 if (tp
) /* TPOINT is installed at the same address as TP. */
2963 clone_fast_tracepoint (tpoint
, tp
);
2965 install_fast_tracepoint (tpoint
, own_buf
);
2970 tpoint
->handle
= (void *) -1;
2973 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
2974 tpoint
->handle
= (void *) -1;
2980 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
2982 if (tpoint
->handle
== NULL
)
2984 if (*own_buf
== '\0')
2985 write_enn (own_buf
);
2992 cmd_qtstart (char *packet
)
2994 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
2996 trace_debug ("Starting the trace");
2998 /* Pause all threads temporarily while we patch tracepoints. */
3001 /* Get threads out of jump pads. Safe to do here, since this is a
3002 top level command. And, required to do here, since we're
3003 deleting/rewriting jump pads. */
3005 stabilize_threads ();
3007 /* Freeze threads. */
3010 /* Sync the fast tracepoints list in the inferior ftlib. */
3011 if (in_process_agent_loaded ())
3013 download_tracepoints ();
3014 download_trace_state_variables ();
3017 /* No previous fast tpoint yet. */
3018 prev_ftpoint
= NULL
;
3020 /* No previous static tpoint yet. */
3021 prev_stpoint
= NULL
;
3025 /* Install tracepoints. */
3026 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3028 /* Ensure all the hit counts start at zero. */
3029 tpoint
->hit_count
= 0;
3031 if (tpoint
->type
== trap_tracepoint
)
3033 /* Tracepoints are installed as memory breakpoints. Just go
3034 ahead and install the trap. The breakpoints module
3035 handles duplicated breakpoints, and the memory read
3036 routine handles un-patching traps from memory reads. */
3037 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3038 tracepoint_handler
);
3040 else if (tpoint
->type
== fast_tracepoint
)
3042 if (maybe_write_ipa_not_loaded (packet
))
3044 trace_debug ("Requested a fast tracepoint, but fast "
3045 "tracepoints aren't supported.");
3049 if (prev_ftpoint
!= NULL
&& prev_ftpoint
->address
== tpoint
->address
)
3050 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3053 if (install_fast_tracepoint (tpoint
, packet
) == 0)
3054 prev_ftpoint
= tpoint
;
3057 else if (tpoint
->type
== static_tracepoint
)
3059 if (maybe_write_ipa_ust_not_loaded (packet
))
3061 trace_debug ("Requested a static tracepoint, but static "
3062 "tracepoints are not supported.");
3066 /* Can only probe a given marker once. */
3067 if (prev_stpoint
!= NULL
&& prev_stpoint
->address
== tpoint
->address
)
3069 tpoint
->handle
= (void *) -1;
3073 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3075 tpoint
->handle
= (void *) -1;
3077 /* So that we can handle multiple static tracepoints
3078 at the same address easily. */
3079 prev_stpoint
= tpoint
;
3084 /* Any failure in the inner loop is sufficient cause to give
3086 if (tpoint
->handle
== NULL
)
3090 /* Any error in tracepoint insertion is unacceptable; better to
3091 address the problem now, than end up with a useless or misleading
3095 clear_installed_tracepoints ();
3096 if (*packet
== '\0')
3102 stopping_tracepoint
= NULL
;
3103 trace_buffer_is_full
= 0;
3104 expr_eval_result
= expr_eval_no_error
;
3105 error_tracepoint
= NULL
;
3107 /* Tracing is now active, hits will now start being logged. */
3110 if (in_process_agent_loaded ())
3112 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3113 fatal ("Error setting tracing variable in lib");
3115 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3117 fatal ("Error clearing stopping_tracepoint variable in lib");
3119 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3120 fatal ("Error clearing trace_buffer_is_full variable in lib");
3122 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3123 stop_tracing_handler
);
3124 if (stop_tracing_bkpt
== NULL
)
3125 error ("Error setting stop_tracing breakpoint");
3127 flush_trace_buffer_bkpt
3128 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3129 flush_trace_buffer_handler
);
3130 if (flush_trace_buffer_bkpt
== NULL
)
3131 error ("Error setting flush_trace_buffer breakpoint");
3139 /* End a tracing run, filling in a stop reason to report back to GDB,
3140 and removing the tracepoints from the code. */
3147 trace_debug ("Tracing is already off, ignoring");
3151 trace_debug ("Stopping the trace");
3153 /* Pause all threads before removing fast jumps from memory,
3154 breakpoints, and touching IPA state variables (inferior memory).
3155 Some thread may hit the internal tracing breakpoints, or be
3156 collecting this moment, but that's ok, we don't release the
3157 tpoint object's memory or the jump pads here (we only do that
3158 when we're sure we can move all threads out of the jump pads).
3159 We can't now, since we may be getting here due to the inferior
3160 agent calling us. */
3162 /* Since we're removing breakpoints, cancel breakpoint hits,
3163 possibly related to the breakpoints we're about to delete. */
3164 cancel_breakpoints ();
3166 /* Stop logging. Tracepoints can still be hit, but they will not be
3169 if (in_process_agent_loaded ())
3171 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3172 fatal ("Error clearing tracing variable in lib");
3175 tracing_stop_reason
= "t???";
3176 tracing_stop_tpnum
= 0;
3177 if (stopping_tracepoint
)
3179 trace_debug ("Stopping the trace because "
3180 "tracepoint %d was hit %ld times",
3181 stopping_tracepoint
->number
,
3182 stopping_tracepoint
->pass_count
);
3183 tracing_stop_reason
= "tpasscount";
3184 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3186 else if (trace_buffer_is_full
)
3188 trace_debug ("Stopping the trace because the trace buffer is full");
3189 tracing_stop_reason
= "tfull";
3191 else if (expr_eval_result
!= expr_eval_no_error
)
3193 trace_debug ("Stopping the trace because of an expression eval error");
3194 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3195 tracing_stop_tpnum
= error_tracepoint
->number
;
3197 #ifndef IN_PROCESS_AGENT
3198 else if (!gdb_connected ())
3200 trace_debug ("Stopping the trace because GDB disconnected");
3201 tracing_stop_reason
= "tdisconnected";
3206 trace_debug ("Stopping the trace because of a tstop command");
3207 tracing_stop_reason
= "tstop";
3210 stopping_tracepoint
= NULL
;
3211 error_tracepoint
= NULL
;
3213 /* Clear out the tracepoints. */
3214 clear_installed_tracepoints ();
3216 if (in_process_agent_loaded ())
3218 /* Pull in fast tracepoint trace frames from the inferior lib
3219 buffer into our buffer, even if our buffer is already full,
3220 because we want to present the full number of created frames
3221 in addition to what fit in the trace buffer. */
3222 upload_fast_traceframes ();
3225 if (stop_tracing_bkpt
!= NULL
)
3227 delete_breakpoint (stop_tracing_bkpt
);
3228 stop_tracing_bkpt
= NULL
;
3231 if (flush_trace_buffer_bkpt
!= NULL
)
3233 delete_breakpoint (flush_trace_buffer_bkpt
);
3234 flush_trace_buffer_bkpt
= NULL
;
3241 stop_tracing_handler (CORE_ADDR addr
)
3243 trace_debug ("lib hit stop_tracing");
3245 /* Don't actually handle it here. When we stop tracing we remove
3246 breakpoints from the inferior, and that is not allowed in a
3247 breakpoint handler (as the caller is walking the breakpoint
3253 flush_trace_buffer_handler (CORE_ADDR addr
)
3255 trace_debug ("lib hit flush_trace_buffer");
3260 cmd_qtstop (char *packet
)
3267 cmd_qtdisconnected (char *own_buf
)
3270 char *packet
= own_buf
;
3272 packet
+= strlen ("QTDisconnected:");
3274 unpack_varlen_hex (packet
, &setting
);
3278 disconnected_tracing
= setting
;
3282 cmd_qtframe (char *own_buf
)
3284 ULONGEST frame
, pc
, lo
, hi
, num
;
3286 struct traceframe
*tframe
;
3287 char *packet
= own_buf
;
3289 packet
+= strlen ("QTFrame:");
3291 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3293 packet
+= strlen ("pc:");
3294 unpack_varlen_hex (packet
, &pc
);
3295 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3296 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3298 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3300 packet
+= strlen ("range:");
3301 packet
= unpack_varlen_hex (packet
, &lo
);
3303 unpack_varlen_hex (packet
, &hi
);
3304 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3305 paddress (lo
), paddress (hi
));
3306 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3308 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3310 packet
+= strlen ("outside:");
3311 packet
= unpack_varlen_hex (packet
, &lo
);
3313 unpack_varlen_hex (packet
, &hi
);
3314 trace_debug ("Want to find next traceframe "
3315 "outside the range 0x%s to 0x%s",
3316 paddress (lo
), paddress (hi
));
3317 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3319 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3321 packet
+= strlen ("tdp:");
3322 unpack_varlen_hex (packet
, &num
);
3324 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3325 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3329 unpack_varlen_hex (packet
, &frame
);
3330 tfnum
= (int) frame
;
3333 trace_debug ("Want to stop looking at traceframes");
3334 current_traceframe
= -1;
3338 trace_debug ("Want to look at traceframe %d", tfnum
);
3339 tframe
= find_traceframe (tfnum
);
3344 current_traceframe
= tfnum
;
3345 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3348 sprintf (own_buf
, "F-1");
3352 cmd_qtstatus (char *packet
)
3354 char *stop_reason_rsp
= NULL
;
3356 trace_debug ("Returning trace status as %d, stop reason %s",
3357 tracing
, tracing_stop_reason
);
3359 if (in_process_agent_loaded ())
3363 upload_fast_traceframes ();
3368 stop_reason_rsp
= (char *) tracing_stop_reason
;
3370 /* The user visible error string in terror needs to be hex encoded.
3371 We leave it as plain string in `tracepoint_stop_reason' to ease
3373 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3375 const char *result_name
;
3379 result_name
= stop_reason_rsp
+ strlen ("terror:");
3380 hexstr_len
= strlen (result_name
) * 2;
3381 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3382 strcpy (p
, "terror:");
3384 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3390 "tframes:%x;tcreated:%x;"
3391 "tfree:%x;tsize:%s;"
3395 stop_reason_rsp
, tracing_stop_tpnum
,
3396 traceframe_count
, traceframes_created
,
3397 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3398 circular_trace_buffer
,
3399 disconnected_tracing
);
3402 /* State variables to help return all the tracepoint bits. */
3403 static struct tracepoint
*cur_tpoint
;
3404 static int cur_action
;
3405 static int cur_step_action
;
3406 static struct source_string
*cur_source_string
;
3407 static struct trace_state_variable
*cur_tsv
;
3409 /* Compose a response that is an imitation of the syntax by which the
3410 tracepoint was originally downloaded. */
3413 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3417 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
3418 paddress (tpoint
->address
),
3419 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3420 tpoint
->pass_count
);
3421 if (tpoint
->type
== fast_tracepoint
)
3422 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3423 else if (tpoint
->type
== static_tracepoint
)
3424 sprintf (packet
+ strlen (packet
), ":S");
3428 buf
= unparse_agent_expr (tpoint
->cond
);
3429 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3430 tpoint
->cond
->length
, buf
);
3435 /* Compose a response that is an imitation of the syntax by which the
3436 tracepoint action was originally downloaded (with the difference
3437 that due to the way we store the actions, this will output a packet
3438 per action, while GDB could have combined more than one action
3442 response_action (char *packet
, struct tracepoint
*tpoint
,
3443 char *taction
, int step
)
3445 sprintf (packet
, "%c%x:%s:%s",
3446 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3450 /* Compose a response that is an imitation of the syntax by which the
3451 tracepoint source piece was originally downloaded. */
3454 response_source (char *packet
,
3455 struct tracepoint
*tpoint
, struct source_string
*src
)
3460 len
= strlen (src
->str
);
3461 buf
= alloca (len
* 2 + 1);
3462 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3464 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3465 tpoint
->number
, paddress (tpoint
->address
),
3466 src
->type
, 0, len
, buf
);
3469 /* Return the first piece of tracepoint definition, and initialize the
3470 state machine that will iterate through all the tracepoint
3474 cmd_qtfp (char *packet
)
3476 trace_debug ("Returning first tracepoint definition piece");
3478 cur_tpoint
= tracepoints
;
3479 cur_action
= cur_step_action
= -1;
3480 cur_source_string
= NULL
;
3483 response_tracepoint (packet
, cur_tpoint
);
3485 strcpy (packet
, "l");
3488 /* Return additional pieces of tracepoint definition. Each action and
3489 stepping action must go into its own packet, because of packet size
3490 limits, and so we use state variables to deliver one piece at a
3494 cmd_qtsp (char *packet
)
3496 trace_debug ("Returning subsequent tracepoint definition piece");
3500 /* This case would normally never occur, but be prepared for
3502 strcpy (packet
, "l");
3504 else if (cur_action
< cur_tpoint
->numactions
- 1)
3507 response_action (packet
, cur_tpoint
,
3508 cur_tpoint
->actions_str
[cur_action
], 0);
3510 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3513 response_action (packet
, cur_tpoint
,
3514 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3516 else if ((cur_source_string
3517 ? cur_source_string
->next
3518 : cur_tpoint
->source_strings
))
3520 if (cur_source_string
)
3521 cur_source_string
= cur_source_string
->next
;
3523 cur_source_string
= cur_tpoint
->source_strings
;
3524 response_source (packet
, cur_tpoint
, cur_source_string
);
3528 cur_tpoint
= cur_tpoint
->next
;
3529 cur_action
= cur_step_action
= -1;
3530 cur_source_string
= NULL
;
3532 response_tracepoint (packet
, cur_tpoint
);
3534 strcpy (packet
, "l");
3538 /* Compose a response that is an imitation of the syntax by which the
3539 trace state variable was originally downloaded. */
3542 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3544 char *buf
= (char *) "";
3549 namelen
= strlen (tsv
->name
);
3550 buf
= alloca (namelen
* 2 + 1);
3551 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3554 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3555 tsv
->getter
? 1 : 0, buf
);
3558 /* Return the first trace state variable definition, and initialize
3559 the state machine that will iterate through all the tsv bits. */
3562 cmd_qtfv (char *packet
)
3564 trace_debug ("Returning first trace state variable definition");
3566 cur_tsv
= trace_state_variables
;
3569 response_tsv (packet
, cur_tsv
);
3571 strcpy (packet
, "l");
3574 /* Return additional trace state variable definitions. */
3577 cmd_qtsv (char *packet
)
3579 trace_debug ("Returning first trace state variable definition");
3583 /* This case would normally never occur, but be prepared for
3585 strcpy (packet
, "l");
3589 cur_tsv
= cur_tsv
->next
;
3591 response_tsv (packet
, cur_tsv
);
3593 strcpy (packet
, "l");
3596 strcpy (packet
, "l");
3599 /* Return the first static tracepoint marker, and initialize the state
3600 machine that will iterate through all the static tracepoints
3604 cmd_qtfstm (char *packet
)
3606 if (!maybe_write_ipa_ust_not_loaded (packet
))
3607 run_inferior_command (packet
);
3610 /* Return additional static tracepoints markers. */
3613 cmd_qtsstm (char *packet
)
3615 if (!maybe_write_ipa_ust_not_loaded (packet
))
3616 run_inferior_command (packet
);
3619 /* Return the definition of the static tracepoint at a given address.
3620 Result packet is the same as qTsST's. */
3623 cmd_qtstmat (char *packet
)
3625 if (!maybe_write_ipa_ust_not_loaded (packet
))
3626 run_inferior_command (packet
);
3629 /* Return the minimum instruction size needed for fast tracepoints as a
3630 hexadecimal number. */
3633 cmd_qtminftpilen (char *packet
)
3635 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3638 /* Respond to qTBuffer packet with a block of raw data from the trace
3639 buffer. GDB may ask for a lot, but we are allowed to reply with
3640 only as much as will fit within packet limits or whatever. */
3643 cmd_qtbuffer (char *own_buf
)
3645 ULONGEST offset
, num
, tot
;
3647 char *packet
= own_buf
;
3649 packet
+= strlen ("qTBuffer:");
3651 packet
= unpack_varlen_hex (packet
, &offset
);
3652 ++packet
; /* skip a comma */
3653 unpack_varlen_hex (packet
, &num
);
3655 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3656 (int) num
, pulongest (offset
));
3658 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3660 /* If we're right at the end, reply specially that we're done. */
3663 strcpy (own_buf
, "l");
3667 /* Object to any other out-of-bounds request. */
3670 write_enn (own_buf
);
3674 /* Compute the pointer corresponding to the given offset, accounting
3676 tbp
= trace_buffer_start
+ offset
;
3677 if (tbp
>= trace_buffer_wrap
)
3678 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3680 /* Trim to the remaining bytes if we're close to the end. */
3681 if (num
> tot
- offset
)
3684 /* Trim to available packet size. */
3685 if (num
>= (PBUFSIZ
- 16) / 2 )
3686 num
= (PBUFSIZ
- 16) / 2;
3688 convert_int_to_ascii (tbp
, own_buf
, num
);
3689 own_buf
[num
] = '\0';
3693 cmd_bigqtbuffer (char *own_buf
)
3696 char *packet
= own_buf
;
3698 packet
+= strlen ("QTBuffer:");
3700 if (strncmp ("circular:", packet
, strlen ("circular:")) == 0)
3702 packet
+= strlen ("circular:");
3703 unpack_varlen_hex (packet
, &val
);
3704 circular_trace_buffer
= val
;
3705 trace_debug ("Trace buffer is now %s",
3706 circular_trace_buffer
? "circular" : "linear");
3710 write_enn (own_buf
);
3714 handle_tracepoint_general_set (char *packet
)
3716 if (strcmp ("QTinit", packet
) == 0)
3718 cmd_qtinit (packet
);
3721 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3726 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3728 cmd_qtdpsrc (packet
);
3731 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
3733 cmd_qtenable_disable (packet
, 1);
3736 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
3738 cmd_qtenable_disable (packet
, 0);
3741 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3746 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3751 else if (strcmp ("QTStart", packet
) == 0)
3753 cmd_qtstart (packet
);
3756 else if (strcmp ("QTStop", packet
) == 0)
3758 cmd_qtstop (packet
);
3761 else if (strncmp ("QTDisconnected:", packet
,
3762 strlen ("QTDisconnected:")) == 0)
3764 cmd_qtdisconnected (packet
);
3767 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3769 cmd_qtframe (packet
);
3772 else if (strncmp ("QTBuffer:", packet
, strlen ("QTBuffer:")) == 0)
3774 cmd_bigqtbuffer (packet
);
3782 handle_tracepoint_query (char *packet
)
3784 if (strcmp ("qTStatus", packet
) == 0)
3786 cmd_qtstatus (packet
);
3789 else if (strcmp ("qTfP", packet
) == 0)
3794 else if (strcmp ("qTsP", packet
) == 0)
3799 else if (strcmp ("qTfV", packet
) == 0)
3804 else if (strcmp ("qTsV", packet
) == 0)
3809 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
3814 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
3816 cmd_qtbuffer (packet
);
3819 else if (strcmp ("qTfSTM", packet
) == 0)
3821 cmd_qtfstm (packet
);
3824 else if (strcmp ("qTsSTM", packet
) == 0)
3826 cmd_qtsstm (packet
);
3829 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
3831 cmd_qtstmat (packet
);
3834 else if (strcmp ("qTMinFTPILen", packet
) == 0)
3836 cmd_qtminftpilen (packet
);
3844 #ifndef IN_PROCESS_AGENT
3846 /* Call this when thread TINFO has hit the tracepoint defined by
3847 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3848 action. This adds a while-stepping collecting state item to the
3849 threads' collecting state list, so that we can keep track of
3850 multiple simultaneous while-stepping actions being collected by the
3851 same thread. This can happen in cases like:
3853 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3855 ff0003 INSN3 <-- TP2, collect $regs
3856 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3859 Notice that when instruction INSN5 is reached, the while-stepping
3860 actions of both TP1 and TP3 are still being collected, and that TP2
3861 had been collected meanwhile. The whole range of ff0001-ff0005
3862 should be single-stepped, due to at least TP1's while-stepping
3863 action covering the whole range. */
3866 add_while_stepping_state (struct thread_info
*tinfo
,
3867 int tp_number
, CORE_ADDR tp_address
)
3869 struct wstep_state
*wstep
;
3871 wstep
= xmalloc (sizeof (*wstep
));
3872 wstep
->next
= tinfo
->while_stepping
;
3874 wstep
->tp_number
= tp_number
;
3875 wstep
->tp_address
= tp_address
;
3876 wstep
->current_step
= 0;
3878 tinfo
->while_stepping
= wstep
;
3881 /* Release the while-stepping collecting state WSTEP. */
3884 release_while_stepping_state (struct wstep_state
*wstep
)
3889 /* Release all while-stepping collecting states currently associated
3890 with thread TINFO. */
3893 release_while_stepping_state_list (struct thread_info
*tinfo
)
3895 struct wstep_state
*head
;
3897 while (tinfo
->while_stepping
)
3899 head
= tinfo
->while_stepping
;
3900 tinfo
->while_stepping
= head
->next
;
3901 release_while_stepping_state (head
);
3905 /* If TINFO was handling a 'while-stepping' action, the step has
3906 finished, so collect any step data needed, and check if any more
3907 steps are required. Return true if the thread was indeed
3908 collecting tracepoint data, false otherwise. */
3911 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3913 struct tracepoint
*tpoint
;
3914 struct wstep_state
*wstep
;
3915 struct wstep_state
**wstep_link
;
3916 struct trap_tracepoint_ctx ctx
;
3918 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
3920 if (in_process_agent_loaded ())
3921 upload_fast_traceframes ();
3923 /* Check if we were indeed collecting data for one of more
3924 tracepoints with a 'while-stepping' count. */
3925 if (tinfo
->while_stepping
== NULL
)
3930 /* We're not even tracing anymore. Stop this thread from
3932 release_while_stepping_state_list (tinfo
);
3934 /* The thread had stopped due to a single-step request indeed
3935 explained by a tracepoint. */
3939 wstep
= tinfo
->while_stepping
;
3940 wstep_link
= &tinfo
->while_stepping
;
3942 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
3943 target_pid_to_str (tinfo
->entry
.id
),
3944 wstep
->tp_number
, paddress (wstep
->tp_address
));
3946 ctx
.base
.type
= trap_tracepoint
;
3947 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3949 while (wstep
!= NULL
)
3951 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
3954 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
3955 wstep
->tp_number
, paddress (wstep
->tp_address
),
3956 target_pid_to_str (tinfo
->entry
.id
));
3959 *wstep_link
= wstep
->next
;
3960 release_while_stepping_state (wstep
);
3961 wstep
= *wstep_link
;
3965 /* We've just finished one step. */
3966 ++wstep
->current_step
;
3969 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
3970 stop_pc
, tpoint
, wstep
->current_step
);
3972 if (wstep
->current_step
>= tpoint
->step_count
)
3974 /* The requested numbers of steps have occurred. */
3975 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
3976 target_pid_to_str (tinfo
->entry
.id
),
3977 wstep
->tp_number
, paddress (wstep
->tp_address
));
3979 /* Unlink the wstep. */
3980 *wstep_link
= wstep
->next
;
3981 release_while_stepping_state (wstep
);
3982 wstep
= *wstep_link
;
3984 /* Only check the hit count now, which ensure that we do all
3985 our stepping before stopping the run. */
3986 if (tpoint
->pass_count
> 0
3987 && tpoint
->hit_count
>= tpoint
->pass_count
3988 && stopping_tracepoint
== NULL
)
3989 stopping_tracepoint
= tpoint
;
3993 /* Keep single-stepping until the requested numbers of steps
3995 wstep_link
= &wstep
->next
;
3996 wstep
= *wstep_link
;
3999 if (stopping_tracepoint
4000 || trace_buffer_is_full
4001 || expr_eval_result
!= expr_eval_no_error
)
4011 /* Handle any internal tracing control breakpoint hits. That means,
4012 pull traceframes from the IPA to our buffer, and syncing both
4013 tracing agents when the IPA's tracing stops for some reason. */
4016 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4018 /* Pull in fast tracepoint trace frames from the inferior in-process
4019 agent's buffer into our buffer. */
4021 if (!in_process_agent_loaded ())
4024 upload_fast_traceframes ();
4026 /* Check if the in-process agent had decided we should stop
4028 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4030 int ipa_trace_buffer_is_full
;
4031 CORE_ADDR ipa_stopping_tracepoint
;
4032 int ipa_expr_eval_result
;
4033 CORE_ADDR ipa_error_tracepoint
;
4035 trace_debug ("lib stopped at stop_tracing");
4037 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4038 &ipa_trace_buffer_is_full
);
4040 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4041 &ipa_stopping_tracepoint
);
4042 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4044 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4045 &ipa_error_tracepoint
);
4046 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4048 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4049 &ipa_expr_eval_result
);
4050 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4052 trace_debug ("lib: trace_buffer_is_full: %d, "
4053 "stopping_tracepoint: %s, "
4054 "ipa_expr_eval_result: %d, "
4055 "error_tracepoint: %s, ",
4056 ipa_trace_buffer_is_full
,
4057 paddress (ipa_stopping_tracepoint
),
4058 ipa_expr_eval_result
,
4059 paddress (ipa_error_tracepoint
));
4063 if (ipa_trace_buffer_is_full
)
4064 trace_debug ("lib stopped due to full buffer.");
4065 if (ipa_stopping_tracepoint
)
4066 trace_debug ("lib stopped due to tpoint");
4067 if (ipa_stopping_tracepoint
)
4068 trace_debug ("lib stopped due to error");
4071 if (ipa_stopping_tracepoint
!= 0)
4074 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4076 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4078 expr_eval_result
= ipa_expr_eval_result
;
4080 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4085 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4087 trace_debug ("lib stopped at flush_trace_buffer");
4094 /* Return true if TINFO just hit a tracepoint. Collect data if
4098 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4100 struct tracepoint
*tpoint
;
4102 struct trap_tracepoint_ctx ctx
;
4104 /* Not tracing, don't handle. */
4108 ctx
.base
.type
= trap_tracepoint
;
4109 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4111 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4113 /* Note that we collect fast tracepoints here as well. We'll
4114 step over the fast tracepoint jump later, which avoids the
4116 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
)
4118 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4119 target_pid_to_str (tinfo
->entry
.id
),
4120 tpoint
->number
, paddress (tpoint
->address
));
4122 /* Test the condition if present, and collect if true. */
4124 || (condition_true_at_tracepoint
4125 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4126 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4129 if (stopping_tracepoint
4130 || trace_buffer_is_full
4131 || expr_eval_result
!= expr_eval_no_error
)
4135 /* If the tracepoint had a 'while-stepping' action, then set
4136 the thread to collect this tracepoint on the following
4138 else if (tpoint
->step_count
> 0)
4140 add_while_stepping_state (tinfo
,
4141 tpoint
->number
, tpoint
->address
);
4153 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4154 struct ust_marker_data
;
4155 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4157 struct tracepoint
*tpoint
,
4158 struct traceframe
*tframe
);
4161 /* Create a trace frame for the hit of the given tracepoint in the
4165 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4166 struct tracepoint
*tpoint
)
4168 struct traceframe
*tframe
;
4171 /* Only count it as a hit when we actually collect data. */
4172 tpoint
->hit_count
++;
4174 /* If we've exceeded a defined pass count, record the event for
4175 later, and finish the collection for this hit. This test is only
4176 for nonstepping tracepoints, stepping tracepoints test at the end
4177 of their while-stepping loop. */
4178 if (tpoint
->pass_count
> 0
4179 && tpoint
->hit_count
>= tpoint
->pass_count
4180 && tpoint
->step_count
== 0
4181 && stopping_tracepoint
== NULL
)
4182 stopping_tracepoint
= tpoint
;
4184 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
4185 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4187 tframe
= add_traceframe (tpoint
);
4191 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4193 #ifndef IN_PROCESS_AGENT
4194 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4195 tpoint
->number
, paddress (tpoint
->address
),
4196 tpoint
->actions_str
[acti
]);
4199 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4200 tpoint
->actions
[acti
]);
4203 finish_traceframe (tframe
);
4206 if (tframe
== NULL
&& tracing
)
4207 trace_buffer_is_full
= 1;
4210 #ifndef IN_PROCESS_AGENT
4213 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4215 struct tracepoint
*tpoint
, int current_step
)
4217 struct traceframe
*tframe
;
4220 trace_debug ("Making new step traceframe for "
4221 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4222 tpoint
->number
, paddress (tpoint
->address
),
4223 current_step
, tpoint
->step_count
,
4226 tframe
= add_traceframe (tpoint
);
4230 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4232 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4233 tpoint
->number
, paddress (tpoint
->address
),
4234 tpoint
->step_actions_str
[acti
]);
4236 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4237 tpoint
->step_actions
[acti
]);
4240 finish_traceframe (tframe
);
4243 if (tframe
== NULL
&& tracing
)
4244 trace_buffer_is_full
= 1;
4249 static struct regcache
*
4250 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4252 struct regcache
*regcache
= NULL
;
4254 #ifdef IN_PROCESS_AGENT
4255 if (ctx
->type
== fast_tracepoint
)
4257 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4258 if (!fctx
->regcache_initted
)
4260 fctx
->regcache_initted
= 1;
4261 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4262 supply_regblock (&fctx
->regcache
, NULL
);
4263 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4265 regcache
= &fctx
->regcache
;
4268 if (ctx
->type
== static_tracepoint
)
4270 struct static_tracepoint_ctx
*sctx
4271 = (struct static_tracepoint_ctx
*) ctx
;
4273 if (!sctx
->regcache_initted
)
4275 sctx
->regcache_initted
= 1;
4276 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4277 supply_regblock (&sctx
->regcache
, NULL
);
4278 /* Pass down the tracepoint address, because REGS doesn't
4279 include the PC, but we know what it must have been. */
4280 supply_static_tracepoint_registers (&sctx
->regcache
,
4281 (const unsigned char *)
4283 sctx
->tpoint
->address
);
4285 regcache
= &sctx
->regcache
;
4289 if (ctx
->type
== trap_tracepoint
)
4291 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4292 regcache
= tctx
->regcache
;
4296 gdb_assert (regcache
!= NULL
);
4302 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4304 struct tracepoint
*tpoint
,
4305 struct traceframe
*tframe
,
4306 struct tracepoint_action
*taction
)
4308 enum eval_result_type err
;
4310 switch (taction
->type
)
4314 struct collect_memory_action
*maction
;
4316 maction
= (struct collect_memory_action
*) taction
;
4318 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4319 pulongest (maction
->len
),
4320 paddress (maction
->addr
), maction
->basereg
);
4321 /* (should use basereg) */
4322 agent_mem_read (tframe
, NULL
,
4323 (CORE_ADDR
) maction
->addr
, maction
->len
);
4328 unsigned char *regspace
;
4329 struct regcache tregcache
;
4330 struct regcache
*context_regcache
;
4333 trace_debug ("Want to collect registers");
4335 /* Collect all registers for now. */
4336 regspace
= add_traceframe_block (tframe
,
4337 1 + register_cache_size ());
4338 if (regspace
== NULL
)
4340 trace_debug ("Trace buffer block allocation failed, skipping");
4343 /* Identify a register block. */
4346 context_regcache
= get_context_regcache (ctx
);
4348 /* Wrap the regblock in a register cache (in the stack, we
4349 don't want to malloc here). */
4350 init_register_cache (&tregcache
, regspace
+ 1);
4352 /* Copy the register data to the regblock. */
4353 regcache_cpy (&tregcache
, context_regcache
);
4355 #ifndef IN_PROCESS_AGENT
4356 /* On some platforms, trap-based tracepoints will have the PC
4357 pointing to the next instruction after the trap, but we
4358 don't want the user or GDB trying to guess whether the
4359 saved PC needs adjusting; so always record the adjusted
4360 stop_pc. Note that we can't use tpoint->address instead,
4361 since it will be wrong for while-stepping actions. This
4362 adjustment is a nop for fast tracepoints collected from the
4363 in-process lib (but not if GDBserver is collecting one
4364 preemptively), since the PC had already been adjusted to
4365 contain the tracepoint's address by the jump pad. */
4366 trace_debug ("Storing stop pc (0x%s) in regblock",
4367 paddress (tpoint
->address
));
4369 /* This changes the regblock, not the thread's
4371 regcache_write_pc (&tregcache
, stop_pc
);
4377 struct eval_expr_action
*eaction
;
4379 eaction
= (struct eval_expr_action
*) taction
;
4381 trace_debug ("Want to evaluate expression");
4383 err
= eval_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4385 if (err
!= expr_eval_no_error
)
4387 record_tracepoint_error (tpoint
, "action expression", err
);
4394 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4395 trace_debug ("Want to collect static trace data");
4396 collect_ust_data_at_tracepoint (ctx
, stop_pc
,
4399 trace_debug ("warning: collecting static trace data, "
4400 "but static tracepoints are not supported");
4405 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4411 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4412 struct tracepoint
*tpoint
)
4415 enum eval_result_type err
;
4417 /* Presently, gdbserver doesn't run compiled conditions, only the
4418 IPA does. If the program stops at a fast tracepoint's address
4419 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4420 gdbserver preemptively collect the fast tracepoint. Later, on
4421 resume, gdbserver steps over the fast tracepoint like it steps
4422 over breakpoints, so that the IPA doesn't see that fast
4423 tracepoint. This avoids double collects of fast tracepoints in
4424 that stopping scenario. Having gdbserver itself handle the fast
4425 tracepoint gives the user a consistent view of when fast or trap
4426 tracepoints are collected, compared to an alternative where only
4427 trap tracepoints are collected on stop, and fast tracepoints on
4428 resume. When a fast tracepoint is being processed by gdbserver,
4429 it is always the non-compiled condition expression that is
4431 #ifdef IN_PROCESS_AGENT
4432 if (tpoint
->compiled_cond
)
4433 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4436 err
= eval_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4438 if (err
!= expr_eval_no_error
)
4440 record_tracepoint_error (tpoint
, "condition", err
);
4441 /* The error case must return false. */
4445 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4446 tpoint
->number
, paddress (tpoint
->address
),
4448 return (value
? 1 : 0);
4451 #ifndef IN_PROCESS_AGENT
4453 /* The packet form of an agent expression consists of an 'X', number
4454 of bytes in expression, a comma, and then the bytes. */
4456 static struct agent_expr
*
4457 parse_agent_expr (char **actparm
)
4459 char *act
= *actparm
;
4461 struct agent_expr
*aexpr
;
4463 ++act
; /* skip the X */
4464 act
= unpack_varlen_hex (act
, &xlen
);
4465 ++act
; /* skip a comma */
4466 aexpr
= xmalloc (sizeof (struct agent_expr
));
4467 aexpr
->length
= xlen
;
4468 aexpr
->bytes
= xmalloc (xlen
);
4469 convert_ascii_to_int (act
, aexpr
->bytes
, xlen
);
4470 *actparm
= act
+ (xlen
* 2);
4474 /* Convert the bytes of an agent expression back into hex digits, so
4475 they can be printed or uploaded. This allocates the buffer,
4476 callers should free when they are done with it. */
4479 unparse_agent_expr (struct agent_expr
*aexpr
)
4483 rslt
= xmalloc (2 * aexpr
->length
+ 1);
4484 convert_int_to_ascii (aexpr
->bytes
, rslt
, aexpr
->length
);
4490 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
4493 gdb_agent_op_name (int op
)
4495 if (op
< 0 || op
>= gdb_agent_op_last
|| gdb_agent_op_names
[op
] == NULL
)
4497 return gdb_agent_op_names
[op
];
4500 /* The agent expression evaluator, as specified by the GDB docs. It
4501 returns 0 if everything went OK, and a nonzero error code
4504 static enum eval_result_type
4505 eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4506 struct traceframe
*tframe
,
4507 struct agent_expr
*aexpr
,
4511 #define STACK_MAX 100
4512 ULONGEST stack
[STACK_MAX
], top
;
4517 /* This union is a convenient way to convert representations. For
4518 now, assume a standard architecture where the hardware integer
4519 types have 8, 16, 32, 64 bit types. A more robust solution would
4520 be to import stdint.h from gnulib. */
4525 unsigned char bytes
[1];
4530 unsigned char bytes
[2];
4535 unsigned char bytes
[4];
4540 unsigned char bytes
[8];
4545 if (aexpr
->length
== 0)
4547 trace_debug ("empty agent expression");
4548 return expr_eval_empty_expression
;
4551 /* Cache the stack top in its own variable. Much of the time we can
4552 operate on this variable, rather than dinking with the stack. It
4553 needs to be copied to the stack when sp changes. */
4558 op
= aexpr
->bytes
[pc
++];
4560 trace_debug ("About to interpret byte 0x%x", op
);
4564 case gdb_agent_op_add
:
4568 case gdb_agent_op_sub
:
4569 top
= stack
[--sp
] - top
;
4572 case gdb_agent_op_mul
:
4576 case gdb_agent_op_div_signed
:
4579 trace_debug ("Attempted to divide by zero");
4580 return expr_eval_divide_by_zero
;
4582 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
4585 case gdb_agent_op_div_unsigned
:
4588 trace_debug ("Attempted to divide by zero");
4589 return expr_eval_divide_by_zero
;
4591 top
= stack
[--sp
] / top
;
4594 case gdb_agent_op_rem_signed
:
4597 trace_debug ("Attempted to divide by zero");
4598 return expr_eval_divide_by_zero
;
4600 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
4603 case gdb_agent_op_rem_unsigned
:
4606 trace_debug ("Attempted to divide by zero");
4607 return expr_eval_divide_by_zero
;
4609 top
= stack
[--sp
] % top
;
4612 case gdb_agent_op_lsh
:
4613 top
= stack
[--sp
] << top
;
4616 case gdb_agent_op_rsh_signed
:
4617 top
= ((LONGEST
) stack
[--sp
]) >> top
;
4620 case gdb_agent_op_rsh_unsigned
:
4621 top
= stack
[--sp
] >> top
;
4624 case gdb_agent_op_trace
:
4625 agent_mem_read (tframe
,
4626 NULL
, (CORE_ADDR
) stack
[--sp
], (ULONGEST
) top
);
4631 case gdb_agent_op_trace_quick
:
4632 arg
= aexpr
->bytes
[pc
++];
4633 agent_mem_read (tframe
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
4636 case gdb_agent_op_log_not
:
4640 case gdb_agent_op_bit_and
:
4644 case gdb_agent_op_bit_or
:
4648 case gdb_agent_op_bit_xor
:
4652 case gdb_agent_op_bit_not
:
4656 case gdb_agent_op_equal
:
4657 top
= (stack
[--sp
] == top
);
4660 case gdb_agent_op_less_signed
:
4661 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
4664 case gdb_agent_op_less_unsigned
:
4665 top
= (stack
[--sp
] < top
);
4668 case gdb_agent_op_ext
:
4669 arg
= aexpr
->bytes
[pc
++];
4670 if (arg
< (sizeof (LONGEST
) * 8))
4672 LONGEST mask
= 1 << (arg
- 1);
4673 top
&= ((LONGEST
) 1 << arg
) - 1;
4674 top
= (top
^ mask
) - mask
;
4678 case gdb_agent_op_ref8
:
4679 agent_mem_read (tframe
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
4683 case gdb_agent_op_ref16
:
4684 agent_mem_read (tframe
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
4688 case gdb_agent_op_ref32
:
4689 agent_mem_read (tframe
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
4693 case gdb_agent_op_ref64
:
4694 agent_mem_read (tframe
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
4698 case gdb_agent_op_if_goto
:
4700 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4707 case gdb_agent_op_goto
:
4708 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4711 case gdb_agent_op_const8
:
4712 /* Flush the cached stack top. */
4714 top
= aexpr
->bytes
[pc
++];
4717 case gdb_agent_op_const16
:
4718 /* Flush the cached stack top. */
4720 top
= aexpr
->bytes
[pc
++];
4721 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4724 case gdb_agent_op_const32
:
4725 /* Flush the cached stack top. */
4727 top
= aexpr
->bytes
[pc
++];
4728 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4729 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4730 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4733 case gdb_agent_op_const64
:
4734 /* Flush the cached stack top. */
4736 top
= aexpr
->bytes
[pc
++];
4737 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4738 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4739 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4740 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4741 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4742 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4743 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4746 case gdb_agent_op_reg
:
4747 /* Flush the cached stack top. */
4749 arg
= aexpr
->bytes
[pc
++];
4750 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4753 struct regcache
*regcache
;
4755 regcache
= get_context_regcache (ctx
);
4757 switch (register_size (regnum
))
4760 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
4764 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
4768 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
4772 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
4776 internal_error (__FILE__
, __LINE__
,
4777 "unhandled register size");
4782 case gdb_agent_op_end
:
4783 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4784 sp
, pulongest (top
));
4789 /* This should be an error */
4790 trace_debug ("Stack is empty, nothing to return");
4791 return expr_eval_empty_stack
;
4795 return expr_eval_no_error
;
4797 case gdb_agent_op_dup
:
4801 case gdb_agent_op_pop
:
4806 case gdb_agent_op_pick
:
4807 arg
= aexpr
->bytes
[pc
++];
4809 top
= stack
[sp
- arg
];
4813 case gdb_agent_op_rot
:
4815 ULONGEST tem
= stack
[sp
- 1];
4817 stack
[sp
- 1] = stack
[sp
- 2];
4818 stack
[sp
- 2] = top
;
4823 case gdb_agent_op_zero_ext
:
4824 arg
= aexpr
->bytes
[pc
++];
4825 if (arg
< (sizeof (LONGEST
) * 8))
4826 top
&= ((LONGEST
) 1 << arg
) - 1;
4829 case gdb_agent_op_swap
:
4830 /* Interchange top two stack elements, making sure top gets
4831 copied back onto stack. */
4833 top
= stack
[sp
- 1];
4834 stack
[sp
- 1] = stack
[sp
];
4837 case gdb_agent_op_getv
:
4838 /* Flush the cached stack top. */
4840 arg
= aexpr
->bytes
[pc
++];
4841 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4842 top
= get_trace_state_variable_value (arg
);
4845 case gdb_agent_op_setv
:
4846 arg
= aexpr
->bytes
[pc
++];
4847 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4848 set_trace_state_variable_value (arg
, top
);
4849 /* Note that we leave the value on the stack, for the
4850 benefit of later/enclosing expressions. */
4853 case gdb_agent_op_tracev
:
4854 arg
= aexpr
->bytes
[pc
++];
4855 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4856 agent_tsv_read (tframe
, arg
);
4859 case gdb_agent_op_tracenz
:
4860 agent_mem_read_string (tframe
, NULL
, (CORE_ADDR
) stack
[--sp
],
4866 /* GDB never (currently) generates any of these ops. */
4867 case gdb_agent_op_float
:
4868 case gdb_agent_op_ref_float
:
4869 case gdb_agent_op_ref_double
:
4870 case gdb_agent_op_ref_long_double
:
4871 case gdb_agent_op_l_to_d
:
4872 case gdb_agent_op_d_to_l
:
4873 case gdb_agent_op_trace16
:
4874 trace_debug ("Agent expression op 0x%x valid, but not handled",
4876 /* If ever GDB generates any of these, we don't have the
4877 option of ignoring. */
4881 trace_debug ("Agent expression op 0x%x not recognized", op
);
4882 /* Don't struggle on, things will just get worse. */
4883 return expr_eval_unrecognized_opcode
;
4886 /* Check for stack badness. */
4887 if (sp
>= (STACK_MAX
- 1))
4889 trace_debug ("Expression stack overflow");
4890 return expr_eval_stack_overflow
;
4895 trace_debug ("Expression stack underflow");
4896 return expr_eval_stack_underflow
;
4899 trace_debug ("Op %s -> sp=%d, top=0x%s",
4900 gdb_agent_op_name (op
), sp
, pulongest (top
));
4904 /* Do memory copies for bytecodes. */
4905 /* Do the recording of memory blocks for actions and bytecodes. */
4908 agent_mem_read (struct traceframe
*tframe
,
4909 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4911 unsigned char *mspace
;
4912 ULONGEST remaining
= len
;
4913 unsigned short blocklen
;
4915 /* If a 'to' buffer is specified, use it. */
4918 read_inferior_memory (from
, to
, len
);
4922 /* Otherwise, create a new memory block in the trace buffer. */
4923 while (remaining
> 0)
4927 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4928 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4929 mspace
= add_traceframe_block (tframe
, sp
);
4932 /* Identify block as a memory block. */
4935 /* Record address and size. */
4936 memcpy (mspace
, &from
, sizeof (from
));
4937 mspace
+= sizeof (from
);
4938 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4939 mspace
+= sizeof (blocklen
);
4940 /* Record the memory block proper. */
4941 read_inferior_memory (from
, mspace
, blocklen
);
4942 trace_debug ("%d bytes recorded", blocklen
);
4943 remaining
-= blocklen
;
4950 agent_mem_read_string (struct traceframe
*tframe
,
4951 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4953 unsigned char *buf
, *mspace
;
4954 ULONGEST remaining
= len
;
4955 unsigned short blocklen
, i
;
4957 /* To save a bit of space, block lengths are 16-bit, so break large
4958 requests into multiple blocks. Bordering on overkill for strings,
4959 but it could happen that someone specifies a large max length. */
4960 while (remaining
> 0)
4964 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4965 /* We want working space to accumulate nonzero bytes, since
4966 traceframes must have a predecided size (otherwise it gets
4967 harder to wrap correctly for the circular case, etc). */
4968 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4969 for (i
= 0; i
< blocklen
; ++i
)
4971 /* Read the string one byte at a time, in case the string is
4972 at the end of a valid memory area - we don't want a
4973 correctly-terminated string to engender segvio
4975 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4980 /* Make sure outer loop stops now too. */
4981 remaining
= blocklen
;
4985 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4986 mspace
= add_traceframe_block (tframe
, sp
);
4992 /* Identify block as a memory block. */
4995 /* Record address and size. */
4996 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4997 mspace
+= sizeof (from
);
4998 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4999 mspace
+= sizeof (blocklen
);
5000 /* Copy the string contents. */
5001 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
5002 remaining
-= blocklen
;
5009 /* Record the value of a trace state variable. */
5012 agent_tsv_read (struct traceframe
*tframe
, int n
)
5014 unsigned char *vspace
;
5017 vspace
= add_traceframe_block (tframe
,
5018 1 + sizeof (n
) + sizeof (LONGEST
));
5021 /* Identify block as a variable. */
5023 /* Record variable's number and value. */
5024 memcpy (vspace
+ 1, &n
, sizeof (n
));
5025 val
= get_trace_state_variable_value (n
);
5026 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
5027 trace_debug ("Variable %d recorded", n
);
5031 #ifndef IN_PROCESS_AGENT
5033 /* Callback for traceframe_walk_blocks, used to find a given block
5034 type in a traceframe. */
5037 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
5039 char *wantedp
= data
;
5041 if (*wantedp
== blocktype
)
5047 /* Walk over all traceframe blocks of the traceframe buffer starting
5048 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5049 block found, passing in DATA unmodified. If CALLBACK returns true,
5050 this returns a pointer to where the block is found. Returns NULL
5051 if no callback call returned true, indicating that all blocks have
5054 static unsigned char *
5055 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
5057 int (*callback
) (char blocktype
,
5058 unsigned char *dataptr
,
5062 unsigned char *dataptr
;
5066 trace_debug ("traceframe %d has no data", tfnum
);
5070 /* Iterate through a traceframe's blocks, looking for a block of the
5072 for (dataptr
= database
;
5073 dataptr
< database
+ datasize
;
5077 unsigned short mlen
;
5079 if (dataptr
== trace_buffer_wrap
)
5081 /* Adjust to reflect wrapping part of the frame around to
5083 datasize
= dataptr
- database
;
5084 dataptr
= database
= trace_buffer_lo
;
5087 blocktype
= *dataptr
++;
5089 if ((*callback
) (blocktype
, dataptr
, data
))
5095 /* Skip over the registers block. */
5096 dataptr
+= register_cache_size ();
5099 /* Skip over the memory block. */
5100 dataptr
+= sizeof (CORE_ADDR
);
5101 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5102 dataptr
+= (sizeof (mlen
) + mlen
);
5105 /* Skip over the TSV block. */
5106 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5109 /* Skip over the static trace data block. */
5110 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5111 dataptr
+= (sizeof (mlen
) + mlen
);
5114 trace_debug ("traceframe %d has unknown block type 0x%x",
5123 /* Look for the block of type TYPE_WANTED in the trameframe starting
5124 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5127 static unsigned char *
5128 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5129 int tfnum
, char type_wanted
)
5131 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5132 match_blocktype
, &type_wanted
);
5135 static unsigned char *
5136 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5138 unsigned char *regblock
;
5140 regblock
= traceframe_find_block_type (tframe
->data
,
5144 if (regblock
== NULL
)
5145 trace_debug ("traceframe %d has no register data", tfnum
);
5150 /* Get registers from a traceframe. */
5153 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5155 unsigned char *dataptr
;
5156 struct tracepoint
*tpoint
;
5157 struct traceframe
*tframe
;
5159 tframe
= find_traceframe (tfnum
);
5163 trace_debug ("traceframe %d not found", tfnum
);
5167 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5168 if (dataptr
== NULL
)
5170 /* Mark registers unavailable. */
5171 supply_regblock (regcache
, NULL
);
5173 /* We can generally guess at a PC, although this will be
5174 misleading for while-stepping frames and multi-location
5176 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5178 regcache_write_pc (regcache
, tpoint
->address
);
5181 supply_regblock (regcache
, dataptr
);
5187 traceframe_get_pc (struct traceframe
*tframe
)
5189 struct regcache regcache
;
5190 unsigned char *dataptr
;
5192 dataptr
= traceframe_find_regblock (tframe
, -1);
5193 if (dataptr
== NULL
)
5196 init_register_cache (®cache
, dataptr
);
5197 return regcache_read_pc (®cache
);
5200 /* Read a requested block of memory from a trace frame. */
5203 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5204 unsigned char *buf
, ULONGEST length
,
5207 struct traceframe
*tframe
;
5208 unsigned char *database
, *dataptr
;
5209 unsigned int datasize
;
5211 unsigned short mlen
;
5213 trace_debug ("traceframe_read_mem");
5215 tframe
= find_traceframe (tfnum
);
5219 trace_debug ("traceframe %d not found", tfnum
);
5223 datasize
= tframe
->data_size
;
5224 database
= dataptr
= &tframe
->data
[0];
5226 /* Iterate through a traceframe's blocks, looking for memory. */
5227 while ((dataptr
= traceframe_find_block_type (dataptr
,
5229 - (dataptr
- database
),
5230 tfnum
, 'M')) != NULL
)
5232 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5233 dataptr
+= sizeof (maddr
);
5234 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5235 dataptr
+= sizeof (mlen
);
5236 trace_debug ("traceframe %d has %d bytes at %s",
5237 tfnum
, mlen
, paddress (maddr
));
5239 /* If the block includes the first part of the desired range,
5240 return as much it has; GDB will re-request the remainder,
5241 which might be in a different block of this trace frame. */
5242 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5244 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5248 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5253 /* Skip over this block. */
5257 trace_debug ("traceframe %d has no memory data for the desired region",
5265 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5268 struct traceframe
*tframe
;
5269 unsigned char *database
, *dataptr
;
5270 unsigned int datasize
;
5273 trace_debug ("traceframe_read_tsv");
5275 tfnum
= current_traceframe
;
5279 trace_debug ("no current traceframe");
5283 tframe
= find_traceframe (tfnum
);
5287 trace_debug ("traceframe %d not found", tfnum
);
5291 datasize
= tframe
->data_size
;
5292 database
= dataptr
= &tframe
->data
[0];
5294 /* Iterate through a traceframe's blocks, looking for the tsv. */
5295 while ((dataptr
= traceframe_find_block_type (dataptr
,
5297 - (dataptr
- database
),
5298 tfnum
, 'V')) != NULL
)
5300 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5301 dataptr
+= sizeof (vnum
);
5303 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5305 /* Check that this is the variable we want. */
5308 memcpy (val
, dataptr
, sizeof (*val
));
5312 /* Skip over this block. */
5313 dataptr
+= sizeof (LONGEST
);
5316 trace_debug ("traceframe %d has no data for variable %d",
5321 /* Read a requested block of static tracepoint data from a trace
5325 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5326 unsigned char *buf
, ULONGEST length
,
5329 struct traceframe
*tframe
;
5330 unsigned char *database
, *dataptr
;
5331 unsigned int datasize
;
5332 unsigned short mlen
;
5334 trace_debug ("traceframe_read_sdata");
5336 tframe
= find_traceframe (tfnum
);
5340 trace_debug ("traceframe %d not found", tfnum
);
5344 datasize
= tframe
->data_size
;
5345 database
= &tframe
->data
[0];
5347 /* Iterate through a traceframe's blocks, looking for static
5349 dataptr
= traceframe_find_block_type (database
, datasize
,
5351 if (dataptr
!= NULL
)
5353 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5354 dataptr
+= sizeof (mlen
);
5357 if (offset
+ length
> mlen
)
5358 length
= mlen
- offset
;
5360 memcpy (buf
, dataptr
, length
);
5368 trace_debug ("traceframe %d has no static trace data", tfnum
);
5374 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5375 object. DATA is pointer to a struct buffer holding the
5376 traceframe-info object being built. */
5379 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5381 struct buffer
*buffer
= data
;
5387 unsigned short mlen
;
5390 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5391 dataptr
+= sizeof (maddr
);
5392 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5393 dataptr
+= sizeof (mlen
);
5394 buffer_xml_printf (buffer
,
5395 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5396 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5406 warning ("Unhandled trace block type (%d) '%c ' "
5407 "while building trace frame info.",
5408 blocktype
, blocktype
);
5415 /* Build a traceframe-info object for traceframe number TFNUM into
5419 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5421 struct traceframe
*tframe
;
5423 trace_debug ("traceframe_read_info");
5425 tframe
= find_traceframe (tfnum
);
5429 trace_debug ("traceframe %d not found", tfnum
);
5433 buffer_grow_str (buffer
, "<traceframe-info>\n");
5434 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5435 tfnum
, build_traceframe_info_xml
, buffer
);
5436 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5440 /* Return the first fast tracepoint whose jump pad contains PC. */
5442 static struct tracepoint
*
5443 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5445 struct tracepoint
*tpoint
;
5447 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5448 if (tpoint
->type
== fast_tracepoint
)
5449 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5455 /* Return the first fast tracepoint whose trampoline contains PC. */
5457 static struct tracepoint
*
5458 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5460 struct tracepoint
*tpoint
;
5462 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5464 if (tpoint
->type
== fast_tracepoint
5465 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5472 /* Return GDBserver's tracepoint that matches the IP Agent's
5473 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5476 static struct tracepoint
*
5477 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5479 struct tracepoint
*tpoint
;
5481 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5482 if (tpoint
->type
== fast_tracepoint
)
5483 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5491 /* The type of the object that is used to synchronize fast tracepoint
5494 typedef struct collecting_t
5496 /* The fast tracepoint number currently collecting. */
5499 /* A number that GDBserver can use to identify the thread that is
5500 presently holding the collect lock. This need not (and usually
5501 is not) the thread id, as getting the current thread ID usually
5502 requires a system call, which we want to avoid like the plague.
5503 Usually this is thread's TCB, found in the TLS (pseudo-)
5504 register, which is readable with a single insn on several
5506 uintptr_t thread_area
;
5509 #ifndef IN_PROCESS_AGENT
5512 force_unlock_trace_buffer (void)
5514 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5517 /* Check if the thread identified by THREAD_AREA which is stopped at
5518 STOP_PC, is presently locking the fast tracepoint collection, and
5519 if so, gather some status of said collection. Returns 0 if the
5520 thread isn't collecting or in the jump pad at all. 1, if in the
5521 jump pad (or within gdb_collect) and hasn't executed the adjusted
5522 original insn yet (can set a breakpoint there and run to it). 2,
5523 if presently executing the adjusted original insn --- in which
5524 case, if we want to move the thread out of the jump pad, we need to
5525 single-step it until this function returns 0. */
5528 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5530 struct fast_tpoint_collect_status
*status
)
5532 CORE_ADDR ipa_collecting
;
5533 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5534 CORE_ADDR ipa_gdb_trampoline_buffer
;
5535 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5536 struct tracepoint
*tpoint
;
5537 int needs_breakpoint
;
5539 /* The thread THREAD_AREA is either:
5541 0. not collecting at all, not within the jump pad, or within
5542 gdb_collect or one of its callees.
5544 1. in the jump pad and haven't reached gdb_collect
5546 2. within gdb_collect (out of the jump pad) (collect is set)
5548 3. we're in the jump pad, after gdb_collect having returned,
5549 possibly executing the adjusted insns.
5551 For cases 1 and 3, `collecting' may or not be set. The jump pad
5552 doesn't have any complicated jump logic, so we can tell if the
5553 thread is executing the adjust original insn or not by just
5554 matching STOP_PC with known jump pad addresses. If we it isn't
5555 yet executing the original insn, set a breakpoint there, and let
5556 the thread run to it, so to quickly step over a possible (many
5557 insns) gdb_collect call. Otherwise, or when the breakpoint is
5558 hit, only a few (small number of) insns are left to be executed
5559 in the jump pad. Single-step the thread until it leaves the
5564 needs_breakpoint
= 0;
5565 trace_debug ("fast_tracepoint_collecting");
5567 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5568 &ipa_gdb_jump_pad_buffer
))
5569 fatal ("error extracting `gdb_jump_pad_buffer'");
5570 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5571 &ipa_gdb_jump_pad_buffer_end
))
5572 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5574 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5575 &ipa_gdb_trampoline_buffer
))
5576 fatal ("error extracting `gdb_trampoline_buffer'");
5577 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5578 &ipa_gdb_trampoline_buffer_end
))
5579 fatal ("error extracting `gdb_trampoline_buffer_end'");
5581 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5582 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5584 /* We can tell which tracepoint(s) the thread is collecting by
5585 matching the jump pad address back to the tracepoint. */
5586 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5589 warning ("in jump pad, but no matching tpoint?");
5594 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5596 tpoint
->number
, paddress (tpoint
->address
),
5597 paddress (tpoint
->jump_pad
),
5598 paddress (tpoint
->jump_pad_end
),
5599 paddress (tpoint
->adjusted_insn_addr
),
5600 paddress (tpoint
->adjusted_insn_addr_end
));
5603 /* Definitely in the jump pad. May or may not need
5604 fast-exit-jump-pad breakpoint. */
5605 if (tpoint
->jump_pad
<= stop_pc
5606 && stop_pc
< tpoint
->adjusted_insn_addr
)
5607 needs_breakpoint
= 1;
5609 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5610 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5612 /* We can tell which tracepoint(s) the thread is collecting by
5613 matching the trampoline address back to the tracepoint. */
5614 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5617 warning ("in trampoline, but no matching tpoint?");
5622 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5623 tpoint
->number
, paddress (tpoint
->address
),
5624 paddress (tpoint
->trampoline
),
5625 paddress (tpoint
->trampoline_end
));
5628 /* Have not reached jump pad yet, but treat the trampoline as a
5629 part of the jump pad that is before the adjusted original
5631 needs_breakpoint
= 1;
5635 collecting_t ipa_collecting_obj
;
5637 /* If `collecting' is set/locked, then the THREAD_AREA thread
5638 may or not be the one holding the lock. We have to read the
5639 lock to find out. */
5641 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5644 trace_debug ("fast_tracepoint_collecting:"
5645 " failed reading 'collecting' in the inferior");
5649 if (!ipa_collecting
)
5651 trace_debug ("fast_tracepoint_collecting: not collecting"
5652 " (and nobody is).");
5656 /* Some thread is collecting. Check which. */
5657 if (read_inferior_memory (ipa_collecting
,
5658 (unsigned char *) &ipa_collecting_obj
,
5659 sizeof (ipa_collecting_obj
)) != 0)
5662 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5664 trace_debug ("fast_tracepoint_collecting: not collecting "
5665 "(another thread is)");
5670 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5673 warning ("fast_tracepoint_collecting: collecting, "
5674 "but tpoint %s not found?",
5675 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5679 /* The thread is within `gdb_collect', skip over the rest of
5680 fast tracepoint collection quickly using a breakpoint. */
5681 needs_breakpoint
= 1;
5684 /* The caller wants a bit of status detail. */
5687 status
->tpoint_num
= tpoint
->number
;
5688 status
->tpoint_addr
= tpoint
->address
;
5689 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5690 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5693 if (needs_breakpoint
)
5695 /* Hasn't executed the original instruction yet. Set breakpoint
5696 there, and wait till it's hit, then single-step until exiting
5700 fast_tracepoint_collecting, returning continue-until-break at %s",
5701 paddress (tpoint
->adjusted_insn_addr
));
5703 return 1; /* continue */
5707 /* Just single-step until exiting the jump pad. */
5709 trace_debug ("fast_tracepoint_collecting, returning "
5710 "need-single-step (%s-%s)",
5711 paddress (tpoint
->adjusted_insn_addr
),
5712 paddress (tpoint
->adjusted_insn_addr_end
));
5714 return 2; /* single-step */
5720 #ifdef IN_PROCESS_AGENT
5722 /* The global fast tracepoint collect lock. Points to a collecting_t
5723 object built on the stack by the jump pad, if presently locked;
5724 NULL if it isn't locked. Note that this lock *must* be set while
5725 executing any *function other than the jump pad. See
5726 fast_tracepoint_collecting. */
5727 static collecting_t
* ATTR_USED collecting
;
5729 /* This routine, called from the jump pad (in asm) is designed to be
5730 called from the jump pads of fast tracepoints, thus it is on the
5733 IP_AGENT_EXPORT
void ATTR_USED
5734 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5736 struct fast_tracepoint_ctx ctx
;
5738 /* Don't do anything until the trace run is completely set up. */
5742 ctx
.base
.type
= fast_tracepoint
;
5744 ctx
.regcache_initted
= 0;
5745 /* Wrap the regblock in a register cache (in the stack, we don't
5746 want to malloc here). */
5747 ctx
.regspace
= alloca (register_cache_size ());
5748 if (ctx
.regspace
== NULL
)
5750 trace_debug ("Trace buffer block allocation failed, skipping");
5754 for (ctx
.tpoint
= tpoint
;
5755 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5756 ctx
.tpoint
= ctx
.tpoint
->next
)
5758 if (!ctx
.tpoint
->enabled
)
5761 /* Multiple tracepoints of different types, such as fast tracepoint and
5762 static tracepoint, can be set at the same address. */
5763 if (ctx
.tpoint
->type
!= tpoint
->type
)
5766 /* Test the condition if present, and collect if true. */
5767 if (ctx
.tpoint
->cond
== NULL
5768 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5771 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5772 ctx
.tpoint
->address
, ctx
.tpoint
);
5774 /* Note that this will cause original insns to be written back
5775 to where we jumped from, but that's OK because we're jumping
5776 back to the next whole instruction. This will go badly if
5777 instruction restoration is not atomic though. */
5778 if (stopping_tracepoint
5779 || trace_buffer_is_full
5780 || expr_eval_result
!= expr_eval_no_error
)
5788 /* If there was a condition and it evaluated to false, the only
5789 way we would stop tracing is if there was an error during
5790 condition expression evaluation. */
5791 if (expr_eval_result
!= expr_eval_no_error
)
5802 #ifndef IN_PROCESS_AGENT
5804 /* Bytecode compilation. */
5806 CORE_ADDR current_insn_ptr
;
5810 struct bytecode_address
5815 /* Offset and size of field to be modified in the goto block. */
5816 int from_offset
, from_size
;
5817 struct bytecode_address
*next
;
5818 } *bytecode_address_table
;
5821 get_raw_reg_func_addr (void)
5823 return ipa_sym_addrs
.addr_get_raw_reg
;
5827 emit_prologue (void)
5829 target_emit_ops ()->emit_prologue ();
5833 emit_epilogue (void)
5835 target_emit_ops ()->emit_epilogue ();
5841 target_emit_ops ()->emit_add ();
5847 target_emit_ops ()->emit_sub ();
5853 target_emit_ops ()->emit_mul ();
5859 target_emit_ops ()->emit_lsh ();
5863 emit_rsh_signed (void)
5865 target_emit_ops ()->emit_rsh_signed ();
5869 emit_rsh_unsigned (void)
5871 target_emit_ops ()->emit_rsh_unsigned ();
5877 target_emit_ops ()->emit_ext (arg
);
5883 target_emit_ops ()->emit_log_not ();
5889 target_emit_ops ()->emit_bit_and ();
5895 target_emit_ops ()->emit_bit_or ();
5901 target_emit_ops ()->emit_bit_xor ();
5907 target_emit_ops ()->emit_bit_not ();
5913 target_emit_ops ()->emit_equal ();
5917 emit_less_signed (void)
5919 target_emit_ops ()->emit_less_signed ();
5923 emit_less_unsigned (void)
5925 target_emit_ops ()->emit_less_unsigned ();
5931 target_emit_ops ()->emit_ref (size
);
5935 emit_if_goto (int *offset_p
, int *size_p
)
5937 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
5941 emit_goto (int *offset_p
, int *size_p
)
5943 target_emit_ops ()->emit_goto (offset_p
, size_p
);
5947 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
5949 target_emit_ops ()->write_goto_address (from
, to
, size
);
5953 emit_const (LONGEST num
)
5955 target_emit_ops ()->emit_const (num
);
5961 target_emit_ops ()->emit_reg (reg
);
5967 target_emit_ops ()->emit_pop ();
5971 emit_stack_flush (void)
5973 target_emit_ops ()->emit_stack_flush ();
5977 emit_zero_ext (int arg
)
5979 target_emit_ops ()->emit_zero_ext (arg
);
5985 target_emit_ops ()->emit_swap ();
5989 emit_stack_adjust (int n
)
5991 target_emit_ops ()->emit_stack_adjust (n
);
5994 /* FN's prototype is `LONGEST(*fn)(int)'. */
5997 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
5999 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
6002 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
6005 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
6007 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
6011 emit_eq_goto (int *offset_p
, int *size_p
)
6013 target_emit_ops ()->emit_eq_goto (offset_p
, size_p
);
6017 emit_ne_goto (int *offset_p
, int *size_p
)
6019 target_emit_ops ()->emit_ne_goto (offset_p
, size_p
);
6023 emit_lt_goto (int *offset_p
, int *size_p
)
6025 target_emit_ops ()->emit_lt_goto (offset_p
, size_p
);
6029 emit_ge_goto (int *offset_p
, int *size_p
)
6031 target_emit_ops ()->emit_ge_goto (offset_p
, size_p
);
6035 emit_gt_goto (int *offset_p
, int *size_p
)
6037 target_emit_ops ()->emit_gt_goto (offset_p
, size_p
);
6041 emit_le_goto (int *offset_p
, int *size_p
)
6043 target_emit_ops ()->emit_le_goto (offset_p
, size_p
);
6046 static enum eval_result_type
compile_bytecodes (struct agent_expr
*aexpr
);
6049 compile_tracepoint_condition (struct tracepoint
*tpoint
,
6050 CORE_ADDR
*jump_entry
)
6052 CORE_ADDR entry_point
= *jump_entry
;
6053 enum eval_result_type err
;
6055 trace_debug ("Starting condition compilation for tracepoint %d\n",
6058 /* Initialize the global pointer to the code being built. */
6059 current_insn_ptr
= *jump_entry
;
6063 err
= compile_bytecodes (tpoint
->cond
);
6065 if (err
== expr_eval_no_error
)
6069 /* Record the beginning of the compiled code. */
6070 tpoint
->compiled_cond
= entry_point
;
6072 trace_debug ("Condition compilation for tracepoint %d complete\n",
6077 /* Leave the unfinished code in situ, but don't point to it. */
6079 tpoint
->compiled_cond
= 0;
6081 trace_debug ("Condition compilation for tracepoint %d failed, "
6083 tpoint
->number
, err
);
6086 /* Update the code pointer passed in. Note that we do this even if
6087 the compile fails, so that we can look at the partial results
6088 instead of letting them be overwritten. */
6089 *jump_entry
= current_insn_ptr
;
6091 /* Leave a gap, to aid dump decipherment. */
6095 /* Scan an agent expression for any evidence that the given PC is the
6096 target of a jump bytecode in the expression. */
6099 is_goto_target (struct agent_expr
*aexpr
, int pc
)
6104 for (i
= 0; i
< aexpr
->length
; i
+= 1 + gdb_agent_op_sizes
[op
])
6106 op
= aexpr
->bytes
[i
];
6108 if (op
== gdb_agent_op_goto
|| op
== gdb_agent_op_if_goto
)
6110 int target
= (aexpr
->bytes
[i
+ 1] << 8) + aexpr
->bytes
[i
+ 2];
6119 /* Given an agent expression, turn it into native code. */
6121 static enum eval_result_type
6122 compile_bytecodes (struct agent_expr
*aexpr
)
6126 unsigned char op
, next_op
;
6128 /* This is only used to build 64-bit value for constants. */
6130 struct bytecode_address
*aentry
, *aentry2
;
6135 trace_debug ("Cannot compile op 0x%x\n", op); \
6136 return expr_eval_unhandled_opcode; \
6139 if (aexpr
->length
== 0)
6141 trace_debug ("empty agent expression\n");
6142 return expr_eval_empty_expression
;
6145 bytecode_address_table
= NULL
;
6149 op
= aexpr
->bytes
[pc
];
6151 trace_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
6153 /* Record the compiled-code address of the bytecode, for use by
6154 jump instructions. */
6155 aentry
= xmalloc (sizeof (struct bytecode_address
));
6157 aentry
->address
= current_insn_ptr
;
6158 aentry
->goto_pc
= -1;
6159 aentry
->from_offset
= aentry
->from_size
= 0;
6160 aentry
->next
= bytecode_address_table
;
6161 bytecode_address_table
= aentry
;
6169 case gdb_agent_op_add
:
6173 case gdb_agent_op_sub
:
6177 case gdb_agent_op_mul
:
6181 case gdb_agent_op_div_signed
:
6185 case gdb_agent_op_div_unsigned
:
6189 case gdb_agent_op_rem_signed
:
6193 case gdb_agent_op_rem_unsigned
:
6197 case gdb_agent_op_lsh
:
6201 case gdb_agent_op_rsh_signed
:
6205 case gdb_agent_op_rsh_unsigned
:
6206 emit_rsh_unsigned ();
6209 case gdb_agent_op_trace
:
6213 case gdb_agent_op_trace_quick
:
6217 case gdb_agent_op_log_not
:
6221 case gdb_agent_op_bit_and
:
6225 case gdb_agent_op_bit_or
:
6229 case gdb_agent_op_bit_xor
:
6233 case gdb_agent_op_bit_not
:
6237 case gdb_agent_op_equal
:
6238 next_op
= aexpr
->bytes
[pc
];
6239 if (next_op
== gdb_agent_op_if_goto
6240 && !is_goto_target (aexpr
, pc
)
6241 && target_emit_ops ()->emit_eq_goto
)
6243 trace_debug ("Combining equal & if_goto");
6246 arg
= aexpr
->bytes
[pc
++];
6247 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6248 aentry
->goto_pc
= arg
;
6249 emit_eq_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6251 else if (next_op
== gdb_agent_op_log_not
6252 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
6253 && !is_goto_target (aexpr
, pc
+ 1)
6254 && target_emit_ops ()->emit_ne_goto
)
6256 trace_debug ("Combining equal & log_not & if_goto");
6259 arg
= aexpr
->bytes
[pc
++];
6260 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6261 aentry
->goto_pc
= arg
;
6262 emit_ne_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6268 case gdb_agent_op_less_signed
:
6269 next_op
= aexpr
->bytes
[pc
];
6270 if (next_op
== gdb_agent_op_if_goto
6271 && !is_goto_target (aexpr
, pc
))
6273 trace_debug ("Combining less_signed & if_goto");
6276 arg
= aexpr
->bytes
[pc
++];
6277 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6278 aentry
->goto_pc
= arg
;
6279 emit_lt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6281 else if (next_op
== gdb_agent_op_log_not
6282 && !is_goto_target (aexpr
, pc
)
6283 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
6284 && !is_goto_target (aexpr
, pc
+ 1))
6286 trace_debug ("Combining less_signed & log_not & if_goto");
6289 arg
= aexpr
->bytes
[pc
++];
6290 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6291 aentry
->goto_pc
= arg
;
6292 emit_ge_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6295 emit_less_signed ();
6298 case gdb_agent_op_less_unsigned
:
6299 emit_less_unsigned ();
6302 case gdb_agent_op_ext
:
6303 arg
= aexpr
->bytes
[pc
++];
6304 if (arg
< (sizeof (LONGEST
) * 8))
6308 case gdb_agent_op_ref8
:
6312 case gdb_agent_op_ref16
:
6316 case gdb_agent_op_ref32
:
6320 case gdb_agent_op_ref64
:
6324 case gdb_agent_op_if_goto
:
6325 arg
= aexpr
->bytes
[pc
++];
6326 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6327 aentry
->goto_pc
= arg
;
6328 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6331 case gdb_agent_op_goto
:
6332 arg
= aexpr
->bytes
[pc
++];
6333 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6334 aentry
->goto_pc
= arg
;
6335 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6338 case gdb_agent_op_const8
:
6339 emit_stack_flush ();
6340 top
= aexpr
->bytes
[pc
++];
6344 case gdb_agent_op_const16
:
6345 emit_stack_flush ();
6346 top
= aexpr
->bytes
[pc
++];
6347 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6351 case gdb_agent_op_const32
:
6352 emit_stack_flush ();
6353 top
= aexpr
->bytes
[pc
++];
6354 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6355 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6356 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6360 case gdb_agent_op_const64
:
6361 emit_stack_flush ();
6362 top
= aexpr
->bytes
[pc
++];
6363 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6364 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6365 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6366 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6367 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6368 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6369 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6373 case gdb_agent_op_reg
:
6374 emit_stack_flush ();
6375 arg
= aexpr
->bytes
[pc
++];
6376 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6380 case gdb_agent_op_end
:
6381 trace_debug ("At end of expression\n");
6383 /* Assume there is one stack element left, and that it is
6384 cached in "top" where emit_epilogue can get to it. */
6385 emit_stack_adjust (1);
6390 case gdb_agent_op_dup
:
6391 /* In our design, dup is equivalent to stack flushing. */
6392 emit_stack_flush ();
6395 case gdb_agent_op_pop
:
6399 case gdb_agent_op_zero_ext
:
6400 arg
= aexpr
->bytes
[pc
++];
6401 if (arg
< (sizeof (LONGEST
) * 8))
6402 emit_zero_ext (arg
);
6405 case gdb_agent_op_swap
:
6406 next_op
= aexpr
->bytes
[pc
];
6407 /* Detect greater-than comparison sequences. */
6408 if (next_op
== gdb_agent_op_less_signed
6409 && !is_goto_target (aexpr
, pc
)
6410 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
6411 && !is_goto_target (aexpr
, pc
+ 1))
6413 trace_debug ("Combining swap & less_signed & if_goto");
6416 arg
= aexpr
->bytes
[pc
++];
6417 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6418 aentry
->goto_pc
= arg
;
6419 emit_gt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6421 else if (next_op
== gdb_agent_op_less_signed
6422 && !is_goto_target (aexpr
, pc
)
6423 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_log_not
)
6424 && !is_goto_target (aexpr
, pc
+ 1)
6425 && (aexpr
->bytes
[pc
+ 2] == gdb_agent_op_if_goto
)
6426 && !is_goto_target (aexpr
, pc
+ 2))
6428 trace_debug ("Combining swap & less_signed & log_not & if_goto");
6431 arg
= aexpr
->bytes
[pc
++];
6432 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6433 aentry
->goto_pc
= arg
;
6434 emit_le_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6440 case gdb_agent_op_getv
:
6441 emit_stack_flush ();
6442 arg
= aexpr
->bytes
[pc
++];
6443 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6444 emit_int_call_1 (ipa_sym_addrs
.addr_get_trace_state_variable_value
,
6448 case gdb_agent_op_setv
:
6449 arg
= aexpr
->bytes
[pc
++];
6450 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6451 emit_void_call_2 (ipa_sym_addrs
.addr_set_trace_state_variable_value
,
6455 case gdb_agent_op_tracev
:
6459 /* GDB never (currently) generates any of these ops. */
6460 case gdb_agent_op_float
:
6461 case gdb_agent_op_ref_float
:
6462 case gdb_agent_op_ref_double
:
6463 case gdb_agent_op_ref_long_double
:
6464 case gdb_agent_op_l_to_d
:
6465 case gdb_agent_op_d_to_l
:
6466 case gdb_agent_op_trace16
:
6471 trace_debug ("Agent expression op 0x%x not recognized\n", op
);
6472 /* Don't struggle on, things will just get worse. */
6473 return expr_eval_unrecognized_opcode
;
6476 /* This catches errors that occur in target-specific code
6480 trace_debug ("Error %d while emitting code for %s\n",
6481 emit_error
, gdb_agent_op_name (op
));
6482 return expr_eval_unhandled_opcode
;
6485 trace_debug ("Op %s compiled\n", gdb_agent_op_name (op
));
6488 /* Now fill in real addresses as goto destinations. */
6489 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
6493 if (aentry
->goto_pc
< 0)
6496 /* Find the location that we are going to, and call back into
6497 target-specific code to write the actual address or
6499 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
6501 if (aentry2
->pc
== aentry
->goto_pc
)
6503 trace_debug ("Want to jump from %s to %s\n",
6504 paddress (aentry
->address
),
6505 paddress (aentry2
->address
));
6506 write_goto_address (aentry
->address
+ aentry
->from_offset
,
6507 aentry2
->address
, aentry
->from_size
);
6513 /* Error out if we didn't find a destination. */
6516 trace_debug ("Destination of goto %d not found\n",
6518 return expr_eval_invalid_goto
;
6522 return expr_eval_no_error
;
6525 /* We'll need to adjust these when we consider bi-arch setups, and big
6529 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
6531 return write_inferior_memory (where
,
6532 (unsigned char *) &ptr
, sizeof (void *));
6535 /* The base pointer of the IPA's heap. This is the only memory the
6536 IPA is allowed to use. The IPA should _not_ call the inferior's
6537 `malloc' during operation. That'd be slow, and, most importantly,
6538 it may not be safe. We may be collecting a tracepoint in a signal
6539 handler, for example. */
6540 static CORE_ADDR target_tp_heap
;
6542 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
6546 target_malloc (ULONGEST size
)
6550 if (target_tp_heap
== 0)
6552 /* We have the pointer *address*, need what it points to. */
6553 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
6555 fatal ("could get target heap head pointer");
6558 ptr
= target_tp_heap
;
6559 target_tp_heap
+= size
;
6561 /* Pad to 8-byte alignment. */
6562 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
6568 download_agent_expr (struct agent_expr
*expr
)
6570 CORE_ADDR expr_addr
;
6571 CORE_ADDR expr_bytes
;
6573 expr_addr
= target_malloc (sizeof (*expr
));
6574 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
6576 expr_bytes
= target_malloc (expr
->length
);
6577 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
6579 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
6584 /* Align V up to N bits. */
6585 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
6587 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
6590 download_tracepoint_1 (struct tracepoint
*tpoint
)
6592 struct tracepoint target_tracepoint
;
6593 CORE_ADDR tpptr
= 0;
6595 gdb_assert (tpoint
->type
== fast_tracepoint
6596 || tpoint
->type
== static_tracepoint
);
6598 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
6600 CORE_ADDR jentry
, jump_entry
;
6602 jentry
= jump_entry
= get_jump_space_head ();
6604 if (tpoint
->cond
!= NULL
)
6606 /* Pad to 8-byte alignment. (needed?) */
6607 /* Actually this should be left for the target to
6609 jentry
= UALIGN (jentry
, 8);
6611 compile_tracepoint_condition (tpoint
, &jentry
);
6614 /* Pad to 8-byte alignment. */
6615 jentry
= UALIGN (jentry
, 8);
6616 claim_jump_space (jentry
- jump_entry
);
6619 target_tracepoint
= *tpoint
;
6621 tpptr
= target_malloc (sizeof (*tpoint
));
6622 tpoint
->obj_addr_on_target
= tpptr
;
6624 /* Write the whole object. We'll fix up its pointers in a bit.
6625 Assume no next for now. This is fixed up above on the next
6626 iteration, if there's any. */
6627 target_tracepoint
.next
= NULL
;
6628 /* Need to clear this here too, since we're downloading the
6629 tracepoints before clearing our own copy. */
6630 target_tracepoint
.hit_count
= 0;
6632 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
6633 sizeof (target_tracepoint
));
6636 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6638 download_agent_expr (tpoint
->cond
));
6640 if (tpoint
->numactions
)
6643 CORE_ADDR actions_array
;
6645 /* The pointers array. */
6647 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6648 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6652 /* Now for each pointer, download the action. */
6653 for (i
= 0; i
< tpoint
->numactions
; i
++)
6655 CORE_ADDR ipa_action
= 0;
6656 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6658 switch (action
->type
)
6662 = target_malloc (sizeof (struct collect_memory_action
));
6663 write_inferior_memory (ipa_action
,
6664 (unsigned char *) action
,
6665 sizeof (struct collect_memory_action
));
6669 = target_malloc (sizeof (struct collect_registers_action
));
6670 write_inferior_memory (ipa_action
,
6671 (unsigned char *) action
,
6672 sizeof (struct collect_registers_action
));
6677 struct eval_expr_action
*eaction
6678 = (struct eval_expr_action
*) action
;
6680 ipa_action
= target_malloc (sizeof (*eaction
));
6681 write_inferior_memory (ipa_action
,
6682 (unsigned char *) eaction
,
6685 expr
= download_agent_expr (eaction
->expr
);
6686 write_inferior_data_ptr
6687 (ipa_action
+ offsetof (struct eval_expr_action
, expr
),
6692 ipa_action
= target_malloc
6693 (sizeof (struct collect_static_trace_data_action
));
6694 write_inferior_memory
6696 (unsigned char *) action
,
6697 sizeof (struct collect_static_trace_data_action
));
6700 trace_debug ("unknown trace action '%c', ignoring",
6705 if (ipa_action
!= 0)
6706 write_inferior_data_ptr
6707 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
6714 download_tracepoint (struct tracepoint
*tpoint
)
6716 struct tracepoint
*tp
, *tp_prev
;
6718 if (tpoint
->type
!= fast_tracepoint
6719 && tpoint
->type
!= static_tracepoint
)
6722 download_tracepoint_1 (tpoint
);
6724 /* Find the previous entry of TPOINT, which is fast tracepoint or
6725 static tracepoint. */
6727 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6729 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6735 CORE_ADDR tp_prev_target_next_addr
;
6737 /* Insert TPOINT after TP_PREV in IPA. */
6738 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6739 + offsetof (struct tracepoint
, next
),
6740 &tp_prev_target_next_addr
))
6741 fatal ("error reading `tp_prev->next'");
6743 /* tpoint->next = tp_prev->next */
6744 write_inferior_data_ptr (tpoint
->obj_addr_on_target
6745 + offsetof (struct tracepoint
, next
),
6746 tp_prev_target_next_addr
);
6747 /* tp_prev->next = tpoint */
6748 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
6749 + offsetof (struct tracepoint
, next
),
6750 tpoint
->obj_addr_on_target
);
6753 /* First object in list, set the head pointer in the
6755 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
6756 tpoint
->obj_addr_on_target
);
6761 download_tracepoints (void)
6763 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
6764 struct tracepoint
*tpoint
;
6766 /* Start out empty. */
6767 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
6769 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6771 if (tpoint
->type
!= fast_tracepoint
6772 && tpoint
->type
!= static_tracepoint
)
6777 download_tracepoint_1 (tpoint
);
6779 tpptr
= tpoint
->obj_addr_on_target
;
6781 if (tpoint
== tracepoints
)
6783 /* First object in list, set the head pointer in the
6785 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
6789 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
6797 download_trace_state_variables (void)
6799 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6800 struct trace_state_variable
*tsv
;
6802 /* Start out empty. */
6803 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6805 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6807 struct trace_state_variable target_tsv
;
6809 /* TSV's with a getter have been initialized equally in both the
6810 inferior and GDBserver. Skip them. */
6811 if (tsv
->getter
!= NULL
)
6817 ptr
= target_malloc (sizeof (*tsv
));
6819 if (tsv
== trace_state_variables
)
6821 /* First object in list, set the head pointer in the
6824 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6829 write_inferior_data_ptr (prev_ptr
6830 + offsetof (struct trace_state_variable
,
6835 /* Write the whole object. We'll fix up its pointers in a bit.
6836 Assume no next, fixup when needed. */
6837 target_tsv
.next
= NULL
;
6839 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6840 sizeof (target_tsv
));
6842 if (tsv
->name
!= NULL
)
6844 size_t size
= strlen (tsv
->name
) + 1;
6845 CORE_ADDR name_addr
= target_malloc (size
);
6846 write_inferior_memory (name_addr
,
6847 (unsigned char *) tsv
->name
, size
);
6848 write_inferior_data_ptr (ptr
6849 + offsetof (struct trace_state_variable
,
6854 if (tsv
->getter
!= NULL
)
6856 fatal ("what to do with these?");
6862 /* Fixup the next pointer in the last item in the list. */
6863 write_inferior_data_ptr (prev_ptr
6864 + offsetof (struct trace_state_variable
,
6869 /* Upload complete trace frames out of the IP Agent's trace buffer
6870 into GDBserver's trace buffer. This always uploads either all or
6871 no trace frames. This is the counter part of
6872 `trace_alloc_trace_buffer'. See its description of the atomic
6873 synching mechanism. */
6876 upload_fast_traceframes (void)
6878 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6879 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6881 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6882 unsigned int curr_tbctrl_idx
;
6883 unsigned int ipa_trace_buffer_ctrl_curr
;
6884 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6885 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6886 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6887 CORE_ADDR ipa_trace_buffer_lo
;
6888 CORE_ADDR ipa_trace_buffer_hi
;
6890 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6891 &ipa_traceframe_read_count_racy
))
6893 /* This will happen in most targets if the current thread is
6898 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6899 &ipa_traceframe_write_count_racy
))
6902 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6903 ipa_traceframe_write_count_racy
6904 - ipa_traceframe_read_count_racy
,
6905 ipa_traceframe_write_count_racy
,
6906 ipa_traceframe_read_count_racy
);
6908 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6911 about_to_request_buffer_space_bkpt
6912 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6915 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6916 &ipa_trace_buffer_ctrl_curr
))
6919 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6921 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6924 unsigned int prev
, counter
;
6926 /* Update the token, with new counters, and the GDBserver stamp
6927 bit. Alway reuse the current TBC index. */
6928 prev
= ipa_trace_buffer_ctrl_curr
& 0x0007ff00;
6929 counter
= (prev
+ 0x100) & 0x0007ff00;
6931 ipa_trace_buffer_ctrl_curr
= (0x80000000
6937 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6938 ipa_trace_buffer_ctrl_curr
))
6941 trace_debug ("Lib: Committed %08x -> %08x",
6942 ipa_trace_buffer_ctrl_curr_old
,
6943 ipa_trace_buffer_ctrl_curr
);
6945 /* Re-read these, now that we've installed the
6946 `about_to_request_buffer_space' breakpoint/lock. A thread could
6947 have finished a traceframe between the last read of these
6948 counters and setting the breakpoint above. If we start
6949 uploading, we never want to leave this function with
6950 traceframe_read_count != 0, otherwise, GDBserver could end up
6951 incrementing the counter tokens more than once (due to event loop
6952 nesting), which would break the IP agent's "effective" detection
6953 (see trace_alloc_trace_buffer). */
6954 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6955 &ipa_traceframe_read_count
))
6957 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6958 &ipa_traceframe_write_count
))
6963 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6964 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6965 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6967 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6968 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6969 trace_debug ("note that ipa_traceframe_count's parts changed");
6972 /* Get the address of the current TBC object (the IP agent has an
6973 array of 3 such objects). The index is stored in the TBC
6975 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6976 ipa_trace_buffer_ctrl_addr
6977 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6979 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6980 (unsigned char *) &ipa_trace_buffer_ctrl
,
6981 sizeof (struct ipa_trace_buffer_control
)))
6984 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6985 &ipa_trace_buffer_lo
))
6987 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6988 &ipa_trace_buffer_hi
))
6991 /* Offsets are easier to grok for debugging than raw addresses,
6992 especially for the small trace buffer sizes that are useful for
6994 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6995 "endfree=%d wrap=%d hi=%d",
6997 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6998 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6999 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
7000 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
7001 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
7003 /* Note that the IPA's buffer is always circular. */
7005 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
7007 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
7008 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
7010 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
7011 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
7012 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
7013 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
7016 tf
= IPA_FIRST_TRACEFRAME ();
7018 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
7020 struct tracepoint
*tpoint
;
7021 struct traceframe
*tframe
;
7022 unsigned char *block
;
7023 struct traceframe ipa_tframe
;
7025 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
7026 offsetof (struct traceframe
, data
)))
7027 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
7029 if (ipa_tframe
.tpnum
== 0)
7030 fatal ("Uploading: No (more) fast traceframes, but "
7031 "ipa_traceframe_count == %u??\n",
7032 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
7034 /* Note that this will be incorrect for multi-location
7036 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
7038 tframe
= add_traceframe (tpoint
);
7041 trace_buffer_is_full
= 1;
7042 trace_debug ("Uploading: trace buffer is full");
7046 /* Copy the whole set of blocks in one go for now. FIXME:
7047 split this in smaller blocks. */
7048 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
7051 if (read_inferior_memory (tf
7052 + offsetof (struct traceframe
, data
),
7053 block
, ipa_tframe
.data_size
))
7054 error ("Uploading: Couldn't read traceframe data at %s\n",
7055 paddress (tf
+ offsetof (struct traceframe
, data
)));
7058 trace_debug ("Uploading: traceframe didn't fit");
7059 finish_traceframe (tframe
);
7062 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
7064 /* If we freed the traceframe that wrapped around, go back
7065 to the non-wrap case. */
7066 if (tf
< ipa_trace_buffer_ctrl
.start
)
7068 trace_debug ("Lib: Discarding past the wraparound");
7069 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
7071 ipa_trace_buffer_ctrl
.start
= tf
;
7072 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
7073 ++ipa_traceframe_read_count
;
7075 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
7076 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
7078 trace_debug ("Lib: buffer is fully empty. "
7079 "Trace buffer [%d] start=%d free=%d endfree=%d",
7081 (int) (ipa_trace_buffer_ctrl
.start
7082 - ipa_trace_buffer_lo
),
7083 (int) (ipa_trace_buffer_ctrl
.free
7084 - ipa_trace_buffer_lo
),
7085 (int) (ipa_trace_buffer_ctrl
.end_free
7086 - ipa_trace_buffer_lo
));
7088 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
7089 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
7090 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
7091 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
7094 trace_debug ("Uploaded a traceframe\n"
7095 "Lib: Trace buffer [%d] start=%d free=%d "
7096 "endfree=%d wrap=%d hi=%d",
7098 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
7099 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
7100 (int) (ipa_trace_buffer_ctrl
.end_free
7101 - ipa_trace_buffer_lo
),
7102 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
7103 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
7106 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
7107 (unsigned char *) &ipa_trace_buffer_ctrl
,
7108 sizeof (struct ipa_trace_buffer_control
)))
7111 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
7112 ipa_traceframe_read_count
);
7114 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
7117 cancel_breakpoints ();
7119 delete_breakpoint (about_to_request_buffer_space_bkpt
);
7120 about_to_request_buffer_space_bkpt
= NULL
;
7124 if (trace_buffer_is_full
)
7129 #ifdef IN_PROCESS_AGENT
7131 IP_AGENT_EXPORT
int ust_loaded
;
7132 IP_AGENT_EXPORT
char cmd_buf
[CMD_BUF_SIZE
];
7136 /* Static tracepoints. */
7138 /* UST puts a "struct tracepoint" in the global namespace, which
7139 conflicts with our tracepoint. Arguably, being a library, it
7140 shouldn't take ownership of such a generic name. We work around it
7142 #define tracepoint ust_tracepoint
7143 #include <ust/ust.h>
7146 extern int serialize_to_text (char *outbuf
, int bufsize
,
7147 const char *fmt
, va_list ap
);
7149 #define GDB_PROBE_NAME "gdb"
7151 /* We dynamically search for the UST symbols instead of linking them
7152 in. This lets the user decide if the application uses static
7153 tracepoints, instead of always pulling libust.so in. This vector
7154 holds pointers to all functions we care about. */
7158 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
7159 const char *fmt
, va_list ap
);
7161 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
7162 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
7164 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
7166 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
7169 void (*marker_iter_start
) (struct marker_iter
*iter
);
7170 void (*marker_iter_next
) (struct marker_iter
*iter
);
7171 void (*marker_iter_stop
) (struct marker_iter
*iter
);
7172 void (*marker_iter_reset
) (struct marker_iter
*iter
);
7177 /* Cast through typeof to catch incompatible API changes. Since UST
7178 only builds with gcc, we can freely use gcc extensions here
7180 #define GET_UST_SYM(SYM) \
7183 if (ust_ops.SYM == NULL) \
7184 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
7185 if (ust_ops.SYM == NULL) \
7189 #define USTF(SYM) ust_ops.SYM
7191 /* Get pointers to all libust.so functions we care about. */
7196 GET_UST_SYM (serialize_to_text
);
7198 GET_UST_SYM (ltt_probe_register
);
7199 GET_UST_SYM (ltt_probe_unregister
);
7200 GET_UST_SYM (ltt_marker_connect
);
7201 GET_UST_SYM (ltt_marker_disconnect
);
7203 GET_UST_SYM (marker_iter_start
);
7204 GET_UST_SYM (marker_iter_next
);
7205 GET_UST_SYM (marker_iter_stop
);
7206 GET_UST_SYM (marker_iter_reset
);
7212 /* Given an UST marker, return the matching gdb static tracepoint.
7213 The match is done by address. */
7215 static struct tracepoint
*
7216 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
7218 struct tracepoint
*tpoint
;
7220 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
7222 if (tpoint
->type
!= static_tracepoint
)
7225 if (tpoint
->address
== (uintptr_t) mdata
->location
)
7232 /* The probe function we install on lttng/ust markers. Whenever a
7233 probed ust marker is hit, this function is called. This is similar
7234 to gdb_collect, only for static tracepoints, instead of fast
7238 gdb_probe (const struct marker
*mdata
, void *probe_private
,
7239 struct registers
*regs
, void *call_private
,
7240 const char *fmt
, va_list *args
)
7242 struct tracepoint
*tpoint
;
7243 struct static_tracepoint_ctx ctx
;
7245 /* Don't do anything until the trace run is completely set up. */
7248 trace_debug ("gdb_probe: not tracing\n");
7252 ctx
.base
.type
= static_tracepoint
;
7253 ctx
.regcache_initted
= 0;
7258 /* Wrap the regblock in a register cache (in the stack, we don't
7259 want to malloc here). */
7260 ctx
.regspace
= alloca (register_cache_size ());
7261 if (ctx
.regspace
== NULL
)
7263 trace_debug ("Trace buffer block allocation failed, skipping");
7267 tpoint
= ust_marker_to_static_tracepoint (mdata
);
7270 trace_debug ("gdb_probe: marker not known: "
7271 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
7272 mdata
->location
, mdata
->channel
,
7273 mdata
->name
, mdata
->format
);
7277 if (!tpoint
->enabled
)
7279 trace_debug ("gdb_probe: tracepoint disabled");
7283 ctx
.tpoint
= tpoint
;
7285 trace_debug ("gdb_probe: collecting marker: "
7286 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
7287 mdata
->location
, mdata
->channel
,
7288 mdata
->name
, mdata
->format
);
7290 /* Test the condition if present, and collect if true. */
7291 if (tpoint
->cond
== NULL
7292 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
7295 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
7296 tpoint
->address
, tpoint
);
7298 if (stopping_tracepoint
7299 || trace_buffer_is_full
7300 || expr_eval_result
!= expr_eval_no_error
)
7305 /* If there was a condition and it evaluated to false, the only
7306 way we would stop tracing is if there was an error during
7307 condition expression evaluation. */
7308 if (expr_eval_result
!= expr_eval_no_error
)
7313 /* Called if the gdb static tracepoint requested collecting "$_sdata",
7314 static tracepoint string data. This is a string passed to the
7315 tracing library by the user, at the time of the tracepoint marker
7316 call. E.g., in the UST marker call:
7318 trace_mark (ust, bar33, "str %s", "FOOBAZ");
7320 the collected data is "str FOOBAZ".
7324 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
7326 struct tracepoint
*tpoint
,
7327 struct traceframe
*tframe
)
7329 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
7330 unsigned char *bufspace
;
7333 unsigned short blocklen
;
7337 trace_debug ("Wanted to collect static trace data, "
7338 "but there's no static trace data");
7342 va_copy (copy
, *umd
->args
);
7343 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
7346 trace_debug ("Want to collect ust data");
7348 /* 'S' + size + string */
7349 bufspace
= add_traceframe_block (tframe
,
7350 1 + sizeof (blocklen
) + size
+ 1);
7351 if (bufspace
== NULL
)
7353 trace_debug ("Trace buffer block allocation failed, skipping");
7357 /* Identify a static trace data block. */
7360 blocklen
= size
+ 1;
7361 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
7363 va_copy (copy
, *umd
->args
);
7364 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
7365 size
+ 1, umd
->fmt
, copy
);
7368 trace_debug ("Storing static tracepoint data in regblock: %s",
7369 bufspace
+ 1 + sizeof (blocklen
));
7372 /* The probe to register with lttng/ust. */
7373 static struct ltt_available_probe gdb_ust_probe
=
7380 #endif /* HAVE_UST */
7381 #endif /* IN_PROCESS_AGENT */
7385 #include <sys/socket.h>
7388 #ifndef UNIX_PATH_MAX
7389 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
7392 /* Where we put the socked used for synchronization. */
7393 #define SOCK_DIR P_tmpdir
7395 #endif /* HAVE_UST */
7397 #ifndef IN_PROCESS_AGENT
7402 gdb_ust_connect_sync_socket (int pid
)
7404 struct sockaddr_un addr
;
7406 char path
[UNIX_PATH_MAX
];
7408 res
= xsnprintf (path
, UNIX_PATH_MAX
, "%s/gdb_ust%d", SOCK_DIR
, pid
);
7409 if (res
>= UNIX_PATH_MAX
)
7411 trace_debug ("string overflow allocating socket name");
7415 res
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
7418 warning ("error opening sync socket: %s\n", strerror (errno
));
7422 addr
.sun_family
= AF_UNIX
;
7424 res
= xsnprintf (addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
7425 if (res
>= UNIX_PATH_MAX
)
7427 warning ("string overflow allocating socket name\n");
7432 res
= connect (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
7435 warning ("error connecting sync socket (%s): %s. "
7436 "Make sure the directory exists and that it is writable.",
7437 path
, strerror (errno
));
7445 /* Resume thread PTID. */
7448 resume_thread (ptid_t ptid
)
7450 struct thread_resume resume_info
;
7452 resume_info
.thread
= ptid
;
7453 resume_info
.kind
= resume_continue
;
7454 resume_info
.sig
= TARGET_SIGNAL_0
;
7455 (*the_target
->resume
) (&resume_info
, 1);
7458 /* Stop thread PTID. */
7461 stop_thread (ptid_t ptid
)
7463 struct thread_resume resume_info
;
7465 resume_info
.thread
= ptid
;
7466 resume_info
.kind
= resume_stop
;
7467 resume_info
.sig
= TARGET_SIGNAL_0
;
7468 (*the_target
->resume
) (&resume_info
, 1);
7471 /* Ask the in-process agent to run a command. Since we don't want to
7472 have to handle the IPA hitting breakpoints while running the
7473 command, we pause all threads, remove all breakpoints, and then set
7474 the helper thread re-running. We communicate with the helper
7475 thread by means of direct memory xfering, and a socket for
7479 run_inferior_command (char *cmd
)
7483 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
7485 ptid_t ptid
= null_ptid
;
7487 trace_debug ("run_inferior_command: running: %s", cmd
);
7490 uninsert_all_breakpoints ();
7492 if (read_inferior_integer (ipa_sym_addrs
.addr_helper_thread_id
, &tid
))
7494 warning ("Error reading helper thread's id in lib");
7500 warning ("helper thread not initialized yet");
7504 if (write_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
7505 (unsigned char *) cmd
, strlen (cmd
) + 1))
7507 warning ("Error writing command");
7511 ptid
= ptid_build (pid
, tid
, 0);
7513 resume_thread (ptid
);
7515 fd
= gdb_ust_connect_sync_socket (pid
);
7521 trace_debug ("signalling helper thread");
7525 ret
= write (fd
, buf
, 1);
7526 } while (ret
== -1 && errno
== EINTR
);
7528 trace_debug ("waiting for helper thread's response");
7532 ret
= read (fd
, buf
, 1);
7533 } while (ret
== -1 && errno
== EINTR
);
7537 trace_debug ("helper thread's response received");
7542 /* Need to read response with the inferior stopped. */
7543 if (!ptid_equal (ptid
, null_ptid
))
7545 int was_non_stop
= non_stop
;
7546 struct target_waitstatus status
;
7550 mywait (ptid
, &status
, 0, 0);
7551 non_stop
= was_non_stop
;
7556 if (read_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
7557 (unsigned char *) cmd
, CMD_BUF_SIZE
))
7559 warning ("Error reading command response");
7564 trace_debug ("run_inferior_command: response: %s", cmd
);
7568 reinsert_all_breakpoints ();
7574 #else /* HAVE_UST */
7577 run_inferior_command (char *cmd
)
7582 #endif /* HAVE_UST */
7584 #else /* !IN_PROCESS_AGENT */
7586 /* Thread ID of the helper thread. GDBserver reads this to know which
7587 is the help thread. This is an LWP id on Linux. */
7588 int helper_thread_id
;
7593 init_named_socket (const char *name
)
7596 struct sockaddr_un addr
;
7598 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
7601 warning ("socket creation failed: %s", strerror (errno
));
7605 addr
.sun_family
= AF_UNIX
;
7607 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
7608 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
7610 result
= access (name
, F_OK
);
7614 result
= unlink (name
);
7617 warning ("unlink failed: %s", strerror (errno
));
7621 warning ("socket %s already exists; overwriting", name
);
7624 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
7627 warning ("bind failed: %s", strerror (errno
));
7632 result
= listen (fd
, 1);
7635 warning ("listen: %s", strerror (errno
));
7644 gdb_ust_socket_init (void)
7647 char name
[UNIX_PATH_MAX
];
7649 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
7650 SOCK_DIR
, getpid ());
7651 if (result
>= UNIX_PATH_MAX
)
7653 trace_debug ("string overflow allocating socket name");
7657 fd
= init_named_socket (name
);
7659 warning ("Error initializing named socket (%s) for communication with the "
7660 "ust helper thread. Check that directory exists and that it "
7661 "is writable.", name
);
7666 /* Return an hexstr version of the STR C string, fit for sending to
7670 cstr_to_hexstr (const char *str
)
7672 int len
= strlen (str
);
7673 char *hexstr
= xmalloc (len
* 2 + 1);
7674 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
7678 /* The next marker to be returned on a qTsSTM command. */
7679 static const struct marker
*next_st
;
7681 /* Returns the first known marker. */
7686 struct marker_iter iter
;
7688 USTF(marker_iter_reset
) (&iter
);
7689 USTF(marker_iter_start
) (&iter
);
7694 /* Returns the marker following M. */
7696 const struct marker
*
7697 next_marker (const struct marker
*m
)
7699 struct marker_iter iter
;
7701 USTF(marker_iter_reset
) (&iter
);
7702 USTF(marker_iter_start
) (&iter
);
7704 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7706 if (iter
.marker
== m
)
7708 USTF(marker_iter_next
) (&iter
);
7716 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
7720 response_ust_marker (char *packet
, const struct marker
*st
)
7722 char *strid
, *format
, *tmp
;
7724 next_st
= next_marker (st
);
7726 tmp
= xmalloc (strlen (st
->channel
) + 1 +
7727 strlen (st
->name
) + 1);
7728 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
7730 strid
= cstr_to_hexstr (tmp
);
7733 format
= cstr_to_hexstr (st
->format
);
7735 sprintf (packet
, "m%s:%s:%s",
7736 paddress ((uintptr_t) st
->location
),
7744 /* Return the first static tracepoint, and initialize the state
7745 machine that will iterate through all the static tracepoints. */
7748 cmd_qtfstm (char *packet
)
7750 trace_debug ("Returning first trace state variable definition");
7752 if (first_marker ())
7753 response_ust_marker (packet
, first_marker ());
7755 strcpy (packet
, "l");
7758 /* Return additional trace state variable definitions. */
7761 cmd_qtsstm (char *packet
)
7763 trace_debug ("Returning static tracepoint");
7766 response_ust_marker (packet
, next_st
);
7768 strcpy (packet
, "l");
7771 /* Disconnect the GDB probe from a marker at a given address. */
7774 unprobe_marker_at (char *packet
)
7778 struct marker_iter iter
;
7780 p
+= sizeof ("unprobe_marker_at:") - 1;
7782 p
= unpack_varlen_hex (p
, &address
);
7784 USTF(marker_iter_reset
) (&iter
);
7785 USTF(marker_iter_start
) (&iter
);
7786 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7787 if ((uintptr_t ) iter
.marker
->location
== address
)
7791 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7795 warning ("could not disable marker %s/%s",
7796 iter
.marker
->channel
, iter
.marker
->name
);
7801 /* Connect the GDB probe to a marker at a given address. */
7804 probe_marker_at (char *packet
)
7808 struct marker_iter iter
;
7811 p
+= sizeof ("probe_marker_at:") - 1;
7813 p
= unpack_varlen_hex (p
, &address
);
7815 USTF(marker_iter_reset
) (&iter
);
7817 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7819 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7820 if ((uintptr_t ) m
->location
== address
)
7824 trace_debug ("found marker for address. "
7825 "ltt_marker_connect (marker = %s/%s)",
7826 m
->channel
, m
->name
);
7828 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7830 if (result
&& result
!= -EEXIST
)
7831 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7832 m
->channel
, m
->name
, -result
);
7836 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7837 m
->channel
, m
->name
);
7841 strcpy (packet
, "OK");
7845 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7850 cmd_qtstmat (char *packet
)
7854 struct marker_iter iter
;
7857 p
+= sizeof ("qTSTMat:") - 1;
7859 p
= unpack_varlen_hex (p
, &address
);
7861 USTF(marker_iter_reset
) (&iter
);
7863 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7865 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7866 if ((uintptr_t ) m
->location
== address
)
7868 response_ust_marker (packet
, m
);
7872 strcpy (packet
, "l");
7877 gdb_ust_thread (void *arg
)
7883 listen_fd
= gdb_ust_socket_init ();
7886 if (helper_thread_id
== 0)
7887 helper_thread_id
= syscall (SYS_gettid
);
7890 if (listen_fd
== -1)
7892 warning ("could not create sync socket\n");
7899 struct sockaddr_un sockaddr
;
7904 tmp
= sizeof (sockaddr
);
7908 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7910 /* It seems an ERESTARTSYS can escape out of accept. */
7911 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7915 warning ("Accept returned %d, error: %s\n",
7916 fd
, strerror (errno
));
7922 ret
= read (fd
, buf
, 1);
7923 } while (ret
== -1 && errno
== EINTR
);
7927 warning ("reading socket (fd=%d) failed with %s",
7928 fd
, strerror (errno
));
7935 if (strcmp ("qTfSTM", cmd_buf
) == 0)
7937 cmd_qtfstm (cmd_buf
);
7939 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7941 cmd_qtsstm (cmd_buf
);
7943 else if (strncmp ("unprobe_marker_at:",
7945 sizeof ("unprobe_marker_at:") - 1) == 0)
7947 unprobe_marker_at (cmd_buf
);
7949 else if (strncmp ("probe_marker_at:",
7951 sizeof ("probe_marker_at:") - 1) == 0)
7953 probe_marker_at (cmd_buf
);
7955 else if (strncmp ("qTSTMat:",
7957 sizeof ("qTSTMat:") - 1) == 0)
7959 cmd_qtstmat (cmd_buf
);
7961 else if (strcmp (cmd_buf
, "help") == 0)
7963 strcpy (cmd_buf
, "for help, press F1\n");
7966 strcpy (cmd_buf
, "");
7990 /* We want the helper thread to be as transparent as possible, so
7991 have it inherit an all-signals-blocked mask. */
7993 sigfillset (&new_mask
);
7994 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7996 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7998 res
= pthread_create (&thread
,
8003 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
8005 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
8007 while (helper_thread_id
== 0)
8010 USTF(ltt_probe_register
) (&gdb_ust_probe
);
8013 #endif /* HAVE_UST */
8015 #include <sys/mman.h>
8018 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
8019 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
8020 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
8021 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
8022 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
8023 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
8025 /* Record the result of getting buffer space for fast tracepoint
8026 trampolines. Any error message is copied, since caller may not be
8027 using persistent storage. */
8030 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
8032 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
8033 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
8035 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
8037 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
8040 static void __attribute__ ((constructor
))
8041 initialize_tracepoint_ftlib (void)
8043 initialize_tracepoint ();
8050 #endif /* IN_PROCESS_AGENT */
8053 tsv_get_timestamp (void)
8057 if (gettimeofday (&tv
, 0) != 0)
8060 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
8064 initialize_tracepoint (void)
8066 /* There currently no way to change the buffer size. */
8067 const int sizeOfBuffer
= 5 * 1024 * 1024;
8068 unsigned char *buf
= xmalloc (sizeOfBuffer
);
8069 init_trace_buffer (buf
, sizeOfBuffer
);
8071 /* Wire trace state variable 1 to be the timestamp. This will be
8072 uploaded to GDB upon connection and become one of its trace state
8073 variables. (In case you're wondering, if GDB already has a trace
8074 variable numbered 1, it will be renumbered.) */
8075 create_trace_state_variable (1, 0);
8076 set_trace_state_variable_name (1, "trace_timestamp");
8077 set_trace_state_variable_getter (1, tsv_get_timestamp
);
8079 #ifdef IN_PROCESS_AGENT
8082 pagesize
= sysconf (_SC_PAGE_SIZE
);
8086 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
8088 /* Allocate scratch buffer aligned on a page boundary. */
8089 gdb_jump_pad_buffer
= memalign (pagesize
, pagesize
* 20);
8090 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* 20;
8092 /* Make it writable and executable. */
8093 if (mprotect (gdb_jump_pad_buffer
, pagesize
* 20,
8094 PROT_READ
| PROT_WRITE
| PROT_EXEC
) != 0)
8096 initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
8097 gdb_jump_pad_buffer
, pagesize
* 20, strerror (errno
));
8100 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
8102 /* It's not a fatal error for something to go wrong with trampoline
8103 buffer setup, but it can be mysterious, so create a channel to
8104 report back on what went wrong, using a fixed size since we may
8105 not be able to allocate space later when the problem occurs. */
8106 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
8108 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
8110 initialize_low_tracepoint ();