1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2012 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 #ifdef IN_PROCESS_AGENT
67 fprintf (stderr
, "ipa/tracepoint: %s\n", buf
);
69 fprintf (stderr
, "gdbserver/tracepoint: %s\n", buf
);
74 #ifdef IN_PROCESS_AGENT
75 #define debug_threads debug_agent
78 #define trace_debug_1(level, fmt, args...) \
80 if (level <= debug_threads) \
81 trace_vdebug ((fmt), ##args); \
84 #define trace_debug(FMT, args...) \
85 trace_debug_1 (1, FMT, ##args)
88 # define ATTR_USED __attribute__((used))
89 # define ATTR_NOINLINE __attribute__((noinline))
90 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
93 # define ATTR_NOINLINE
94 # define ATTR_CONSTRUCTOR
97 /* Make sure the functions the IPA needs to export (symbols GDBserver
98 needs to query GDB about) are exported. */
100 #ifdef IN_PROCESS_AGENT
101 # if defined _WIN32 || defined __CYGWIN__
102 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
105 # define IP_AGENT_EXPORT \
106 __attribute__ ((visibility("default"))) ATTR_USED
108 # define IP_AGENT_EXPORT ATTR_USED
112 # define IP_AGENT_EXPORT
115 /* Prefix exported symbols, for good citizenship. All the symbols
116 that need exporting are defined in this module. */
117 #ifdef IN_PROCESS_AGENT
118 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
119 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
120 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
121 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
122 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
123 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
124 # define collecting gdb_agent_collecting
125 # define gdb_collect gdb_agent_gdb_collect
126 # define stop_tracing gdb_agent_stop_tracing
127 # define flush_trace_buffer gdb_agent_flush_trace_buffer
128 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
129 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
130 # define stopping_tracepoint gdb_agent_stopping_tracepoint
131 # define expr_eval_result gdb_agent_expr_eval_result
132 # define error_tracepoint gdb_agent_error_tracepoint
133 # define tracepoints gdb_agent_tracepoints
134 # define tracing gdb_agent_tracing
135 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
136 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
137 # define trace_buffer_lo gdb_agent_trace_buffer_lo
138 # define trace_buffer_hi gdb_agent_trace_buffer_hi
139 # define traceframe_read_count gdb_agent_traceframe_read_count
140 # define traceframe_write_count gdb_agent_traceframe_write_count
141 # define traceframes_created gdb_agent_traceframes_created
142 # define trace_state_variables gdb_agent_trace_state_variables
143 # define get_raw_reg gdb_agent_get_raw_reg
144 # define get_trace_state_variable_value \
145 gdb_agent_get_trace_state_variable_value
146 # define set_trace_state_variable_value \
147 gdb_agent_set_trace_state_variable_value
148 # define ust_loaded gdb_agent_ust_loaded
149 # define helper_thread_id gdb_agent_helper_thread_id
150 # define cmd_buf gdb_agent_cmd_buf
153 #ifndef IN_PROCESS_AGENT
155 /* Addresses of in-process agent's symbols GDBserver cares about. */
157 struct ipa_sym_addresses
159 CORE_ADDR addr_gdb_tp_heap_buffer
;
160 CORE_ADDR addr_gdb_jump_pad_buffer
;
161 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
162 CORE_ADDR addr_gdb_trampoline_buffer
;
163 CORE_ADDR addr_gdb_trampoline_buffer_end
;
164 CORE_ADDR addr_gdb_trampoline_buffer_error
;
165 CORE_ADDR addr_collecting
;
166 CORE_ADDR addr_gdb_collect
;
167 CORE_ADDR addr_stop_tracing
;
168 CORE_ADDR addr_flush_trace_buffer
;
169 CORE_ADDR addr_about_to_request_buffer_space
;
170 CORE_ADDR addr_trace_buffer_is_full
;
171 CORE_ADDR addr_stopping_tracepoint
;
172 CORE_ADDR addr_expr_eval_result
;
173 CORE_ADDR addr_error_tracepoint
;
174 CORE_ADDR addr_tracepoints
;
175 CORE_ADDR addr_tracing
;
176 CORE_ADDR addr_trace_buffer_ctrl
;
177 CORE_ADDR addr_trace_buffer_ctrl_curr
;
178 CORE_ADDR addr_trace_buffer_lo
;
179 CORE_ADDR addr_trace_buffer_hi
;
180 CORE_ADDR addr_traceframe_read_count
;
181 CORE_ADDR addr_traceframe_write_count
;
182 CORE_ADDR addr_traceframes_created
;
183 CORE_ADDR addr_trace_state_variables
;
184 CORE_ADDR addr_get_raw_reg
;
185 CORE_ADDR addr_get_trace_state_variable_value
;
186 CORE_ADDR addr_set_trace_state_variable_value
;
187 CORE_ADDR addr_ust_loaded
;
188 CORE_ADDR addr_helper_thread_id
;
189 CORE_ADDR addr_cmd_buf
;
192 #define STRINGIZE_1(STR) #STR
193 #define STRINGIZE(STR) STRINGIZE_1(STR)
194 #define IPA_SYM(SYM) \
196 STRINGIZE (gdb_agent_ ## SYM), \
197 offsetof (struct ipa_sym_addresses, addr_ ## SYM) \
206 IPA_SYM(gdb_tp_heap_buffer
),
207 IPA_SYM(gdb_jump_pad_buffer
),
208 IPA_SYM(gdb_jump_pad_buffer_end
),
209 IPA_SYM(gdb_trampoline_buffer
),
210 IPA_SYM(gdb_trampoline_buffer_end
),
211 IPA_SYM(gdb_trampoline_buffer_error
),
213 IPA_SYM(gdb_collect
),
214 IPA_SYM(stop_tracing
),
215 IPA_SYM(flush_trace_buffer
),
216 IPA_SYM(about_to_request_buffer_space
),
217 IPA_SYM(trace_buffer_is_full
),
218 IPA_SYM(stopping_tracepoint
),
219 IPA_SYM(expr_eval_result
),
220 IPA_SYM(error_tracepoint
),
221 IPA_SYM(tracepoints
),
223 IPA_SYM(trace_buffer_ctrl
),
224 IPA_SYM(trace_buffer_ctrl_curr
),
225 IPA_SYM(trace_buffer_lo
),
226 IPA_SYM(trace_buffer_hi
),
227 IPA_SYM(traceframe_read_count
),
228 IPA_SYM(traceframe_write_count
),
229 IPA_SYM(traceframes_created
),
230 IPA_SYM(trace_state_variables
),
231 IPA_SYM(get_raw_reg
),
232 IPA_SYM(get_trace_state_variable_value
),
233 IPA_SYM(set_trace_state_variable_value
),
235 IPA_SYM(helper_thread_id
),
239 struct ipa_sym_addresses ipa_sym_addrs
;
241 int all_tracepoint_symbols_looked_up
;
244 in_process_agent_loaded (void)
246 return all_tracepoint_symbols_looked_up
;
249 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
251 /* Returns true if both the in-process agent library and the static
252 tracepoints libraries are loaded in the inferior. */
255 in_process_agent_loaded_ust (void)
259 if (!in_process_agent_loaded ())
261 warning ("In-process agent not loaded");
265 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
267 warning ("Error reading ust_loaded in lib");
275 write_e_ipa_not_loaded (char *buffer
)
278 "E.In-process agent library not loaded in process. "
279 "Fast and static tracepoints unavailable.");
282 /* Write an error to BUFFER indicating that UST isn't loaded in the
286 write_e_ust_not_loaded (char *buffer
)
290 "E.UST library not loaded in process. "
291 "Static tracepoints unavailable.");
293 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
297 /* If the in-process agent library isn't loaded in the inferior, write
298 an error to BUFFER, and return 1. Otherwise, return 0. */
301 maybe_write_ipa_not_loaded (char *buffer
)
303 if (!in_process_agent_loaded ())
305 write_e_ipa_not_loaded (buffer
);
311 /* If the in-process agent library and the ust (static tracepoints)
312 library aren't loaded in the inferior, write an error to BUFFER,
313 and return 1. Otherwise, return 0. */
316 maybe_write_ipa_ust_not_loaded (char *buffer
)
318 if (!in_process_agent_loaded ())
320 write_e_ipa_not_loaded (buffer
);
323 else if (!in_process_agent_loaded_ust ())
325 write_e_ust_not_loaded (buffer
);
331 /* Cache all future symbols that the tracepoints module might request.
332 We can not request symbols at arbitrary states in the remote
333 protocol, only when the client tells us that new symbols are
334 available. So when we load the in-process library, make sure to
335 check the entire list. */
338 tracepoint_look_up_symbols (void)
342 if (all_tracepoint_symbols_looked_up
)
345 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
348 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
350 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
353 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
358 all_tracepoint_symbols_looked_up
= 1;
363 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
364 of the "stop_tracing" function. When this breakpoint is hit,
365 tracing stopped in the IPA for some reason. E.g., due to
366 tracepoint reaching the pass count, hitting conditional expression
367 evaluation error, etc.
369 The IPA's trace buffer is never in circular tracing mode: instead,
370 GDBserver's is, and whenever the in-process buffer fills, it calls
371 "flush_trace_buffer", which triggers an internal breakpoint.
372 GDBserver reacts to this breakpoint by pulling the meanwhile
373 collected data. Old frames discarding is always handled on the
376 #ifdef IN_PROCESS_AGENT
380 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
382 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
386 /* Call this in the functions where GDBserver places a breakpoint, so
387 that the compiler doesn't try to be clever and skip calling the
388 function at all. This is necessary, even if we tell the compiler
389 to not inline said functions. */
391 #if defined(__GNUC__)
392 # define UNKNOWN_SIDE_EFFECTS() asm ("")
394 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
397 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
400 /* GDBserver places breakpoint here. */
401 UNKNOWN_SIDE_EFFECTS();
404 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
405 flush_trace_buffer (void)
407 /* GDBserver places breakpoint here. */
408 UNKNOWN_SIDE_EFFECTS();
413 #ifndef IN_PROCESS_AGENT
415 tracepoint_handler (CORE_ADDR address
)
417 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
422 /* Breakpoint at "stop_tracing" in the inferior lib. */
423 struct breakpoint
*stop_tracing_bkpt
;
424 static int stop_tracing_handler (CORE_ADDR
);
426 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
427 struct breakpoint
*flush_trace_buffer_bkpt
;
428 static int flush_trace_buffer_handler (CORE_ADDR
);
430 static void download_tracepoints (void);
431 static void download_trace_state_variables (void);
432 static void upload_fast_traceframes (void);
434 static int run_inferior_command (char *cmd
);
437 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
439 return read_inferior_memory (symaddr
, (unsigned char *) val
,
444 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
446 return read_inferior_memory (symaddr
, (unsigned char *) val
,
451 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
453 void *pval
= (void *) (uintptr_t) val
;
456 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
457 *val
= (uintptr_t) pval
;
462 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
464 void *pval
= (void *) (uintptr_t) val
;
465 return write_inferior_memory (symaddr
,
466 (unsigned char *) &pval
, sizeof (pval
));
470 write_inferior_integer (CORE_ADDR symaddr
, int val
)
472 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
476 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
478 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
483 /* This enum must exactly match what is documented in
484 gdb/doc/agentexpr.texi, including all the numerical values. */
488 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
489 gdb_agent_op_ ## NAME = VALUE,
495 static const char *gdb_agent_op_names
[gdb_agent_op_last
] =
498 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
503 static const unsigned char gdb_agent_op_sizes
[gdb_agent_op_last
] =
506 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
515 unsigned char *bytes
;
518 /* Base action. Concrete actions inherit this. */
520 struct tracepoint_action
525 /* An 'M' (collect memory) action. */
526 struct collect_memory_action
528 struct tracepoint_action base
;
535 /* An 'R' (collect registers) action. */
537 struct collect_registers_action
539 struct tracepoint_action base
;
542 /* An 'X' (evaluate expression) action. */
544 struct eval_expr_action
546 struct tracepoint_action base
;
548 struct agent_expr
*expr
;
551 /* An 'L' (collect static trace data) action. */
552 struct collect_static_trace_data_action
554 struct tracepoint_action base
;
557 /* This structure describes a piece of the source-level definition of
558 the tracepoint. The contents are not interpreted by the target,
559 but preserved verbatim for uploading upon reconnection. */
563 /* The type of string, such as "cond" for a conditional. */
566 /* The source-level string itself. For the sake of target
567 debugging, we store it in plaintext, even though it is always
568 transmitted in hex. */
571 /* Link to the next one in the list. We link them in the order
572 received, in case some make up an ordered list of commands or
574 struct source_string
*next
;
579 /* Trap based tracepoint. */
582 /* A fast tracepoint implemented with a jump instead of a trap. */
585 /* A static tracepoint, implemented by a program call into a tracing
590 struct tracepoint_hit_ctx
;
592 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
595 /* The definition of a tracepoint. */
597 /* Tracepoints may have multiple locations, each at a different
598 address. This can occur with optimizations, template
599 instantiation, etc. Since the locations may be in different
600 scopes, the conditions and actions may be different for each
601 location. Our target version of tracepoints is more like GDB's
602 notion of "breakpoint locations", but we have almost nothing that
603 is not per-location, so we bother having two kinds of objects. The
604 key consequence is that numbers are not unique, and that it takes
605 both number and address to identify a tracepoint uniquely. */
609 /* The number of the tracepoint, as specified by GDB. Several
610 tracepoint objects here may share a number. */
613 /* Address at which the tracepoint is supposed to trigger. Several
614 tracepoints may share an address. */
617 /* Tracepoint type. */
618 enum tracepoint_type type
;
620 /* True if the tracepoint is currently enabled. */
623 /* The number of single steps that will be performed after each
627 /* The number of times the tracepoint may be hit before it will
628 terminate the entire tracing run. */
631 /* Pointer to the agent expression that is the tracepoint's
632 conditional, or NULL if the tracepoint is unconditional. */
633 struct agent_expr
*cond
;
635 /* The list of actions to take when the tracepoint triggers. */
637 struct tracepoint_action
**actions
;
639 /* Count of the times we've hit this tracepoint during the run.
640 Note that while-stepping steps are not counted as "hits". */
643 /* Cached sum of the sizes of traceframes created by this point. */
644 long traceframe_usage
;
646 CORE_ADDR compiled_cond
;
648 /* Link to the next tracepoint in the list. */
649 struct tracepoint
*next
;
651 #ifndef IN_PROCESS_AGENT
652 /* The list of actions to take when the tracepoint triggers, in
653 string/packet form. */
656 /* The collection of strings that describe the tracepoint as it was
657 entered into GDB. These are not used by the target, but are
658 reported back to GDB upon reconnection. */
659 struct source_string
*source_strings
;
661 /* The number of bytes displaced by fast tracepoints. It may subsume
662 multiple instructions, for multi-byte fast tracepoints. This
663 field is only valid for fast tracepoints. */
666 /* Only for fast tracepoints. */
667 CORE_ADDR obj_addr_on_target
;
669 /* Address range where the original instruction under a fast
670 tracepoint was relocated to. (_end is actually one byte past
672 CORE_ADDR adjusted_insn_addr
;
673 CORE_ADDR adjusted_insn_addr_end
;
675 /* The address range of the piece of the jump pad buffer that was
676 assigned to this fast tracepoint. (_end is actually one byte
679 CORE_ADDR jump_pad_end
;
681 /* The address range of the piece of the trampoline buffer that was
682 assigned to this fast tracepoint. (_end is actually one byte
684 CORE_ADDR trampoline
;
685 CORE_ADDR trampoline_end
;
687 /* The list of actions to take while in a stepping loop. These
688 fields are only valid for patch-based tracepoints. */
689 int num_step_actions
;
690 struct tracepoint_action
**step_actions
;
691 /* Same, but in string/packet form. */
692 char **step_actions_str
;
694 /* Handle returned by the breakpoint or tracepoint module when we
695 inserted the trap or jump, or hooked into a static tracepoint.
696 NULL if we haven't inserted it yet. */
702 #ifndef IN_PROCESS_AGENT
704 /* Given `while-stepping', a thread may be collecting data for more
705 than one tracepoint simultaneously. On the other hand, the same
706 tracepoint with a while-stepping action may be hit by more than one
707 thread simultaneously (but not quite, each thread could be handling
708 a different step). Each thread holds a list of these objects,
709 representing the current step of each while-stepping action being
714 struct wstep_state
*next
;
716 /* The tracepoint number. */
718 /* The tracepoint's address. */
719 CORE_ADDR tp_address
;
721 /* The number of the current step in this 'while-stepping'
728 /* The linked list of all tracepoints. Marked explicitly as used as
729 the in-process library doesn't use it for the fast tracepoints
731 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
733 #ifndef IN_PROCESS_AGENT
735 /* Pointer to the last tracepoint in the list, new tracepoints are
736 linked in at the end. */
738 static struct tracepoint
*last_tracepoint
;
741 /* The first tracepoint to exceed its pass count. */
743 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
745 /* True if the trace buffer is full or otherwise no longer usable. */
747 IP_AGENT_EXPORT
int trace_buffer_is_full
;
749 /* Enumeration of the different kinds of things that can happen during
750 agent expression evaluation. */
752 enum eval_result_type
755 expr_eval_empty_expression
,
756 expr_eval_empty_stack
,
757 expr_eval_stack_overflow
,
758 expr_eval_stack_underflow
,
759 expr_eval_unhandled_opcode
,
760 expr_eval_unrecognized_opcode
,
761 expr_eval_divide_by_zero
,
762 expr_eval_invalid_goto
765 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
767 #ifndef IN_PROCESS_AGENT
769 static const char *eval_result_names
[] =
771 "terror:in the attic", /* this should never be reported */
772 "terror:empty expression",
773 "terror:empty stack",
774 "terror:stack overflow",
775 "terror:stack underflow",
776 "terror:unhandled opcode",
777 "terror:unrecognized opcode",
778 "terror:divide by zero"
783 /* The tracepoint in which the error occurred. */
785 static struct tracepoint
*error_tracepoint
;
787 struct trace_state_variable
789 /* This is the name of the variable as used in GDB. The target
790 doesn't use the name, but needs to have it for saving and
791 reconnection purposes. */
794 /* This number identifies the variable uniquely. Numbers may be
795 assigned either by the target (in the case of builtin variables),
796 or by GDB, and are presumed unique during the course of a trace
800 /* The variable's initial value, a 64-bit signed integer always. */
801 LONGEST initial_value
;
803 /* The variable's value, a 64-bit signed integer always. */
806 /* Pointer to a getter function, used to supply computed values. */
807 LONGEST (*getter
) (void);
809 /* Link to the next variable. */
810 struct trace_state_variable
*next
;
813 /* Linked list of all trace state variables. */
815 #ifdef IN_PROCESS_AGENT
816 struct trace_state_variable
*alloced_trace_state_variables
;
819 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
821 /* The results of tracing go into a fixed-size space known as the
822 "trace buffer". Because usage follows a limited number of
823 patterns, we manage it ourselves rather than with malloc. Basic
824 rules are that we create only one trace frame at a time, each is
825 variable in size, they are never moved once created, and we only
826 discard if we are doing a circular buffer, and then only the oldest
827 ones. Each trace frame includes its own size, so we don't need to
828 link them together, and the trace frame number is relative to the
829 first one, so we don't need to record numbers. A trace frame also
830 records the number of the tracepoint that created it. The data
831 itself is a series of blocks, each introduced by a single character
832 and with a defined format. Each type of block has enough
833 type/length info to allow scanners to jump quickly from one block
834 to the next without reading each byte in the block. */
836 /* Trace buffer management would be simple - advance a free pointer
837 from beginning to end, then stop - were it not for the circular
838 buffer option, which is a useful way to prevent a trace run from
839 stopping prematurely because the buffer filled up. In the circular
840 case, the location of the first trace frame (trace_buffer_start)
841 moves as old trace frames are discarded. Also, since we grow trace
842 frames incrementally as actions are performed, we wrap around to
843 the beginning of the trace buffer. This is per-block, so each
844 block within a trace frame remains contiguous. Things get messy
845 when the wrapped-around trace frame is the one being discarded; the
846 free space ends up in two parts at opposite ends of the buffer. */
849 # if defined(__GNUC__)
850 # define ATTR_PACKED __attribute__ ((packed))
852 # define ATTR_PACKED /* nothing */
856 /* The data collected at a tracepoint hit. This object should be as
857 small as possible, since there may be a great many of them. We do
858 not need to keep a frame number, because they are all sequential
859 and there are no deletions; so the Nth frame in the buffer is
860 always frame number N. */
864 /* Number of the tracepoint that collected this traceframe. A value
865 of 0 indicates the current end of the trace buffer. We make this
866 a 16-bit field because it's never going to happen that GDB's
867 numbering of tracepoints reaches 32,000. */
870 /* The size of the data in this trace frame. We limit this to 32
871 bits, even on a 64-bit target, because it's just implausible that
872 one is validly going to collect 4 gigabytes of data at a single
874 unsigned int data_size
: 32;
876 /* The base of the trace data, which is contiguous from this point. */
877 unsigned char data
[0];
881 /* The traceframe to be used as the source of data to send back to
882 GDB. A value of -1 means to get data from the live program. */
884 int current_traceframe
= -1;
886 /* This flag is true if the trace buffer is circular, meaning that
887 when it fills, the oldest trace frames are discarded in order to
890 #ifndef IN_PROCESS_AGENT
891 static int circular_trace_buffer
;
894 /* Pointer to the block of memory that traceframes all go into. */
896 static unsigned char *trace_buffer_lo
;
898 /* Pointer to the end of the trace buffer, more precisely to the byte
899 after the end of the buffer. */
901 static unsigned char *trace_buffer_hi
;
903 /* Control structure holding the read/write/etc. pointers into the
904 trace buffer. We need more than one of these to implement a
905 transaction-like mechanism to garantees that both GDBserver and the
906 in-process agent can try to change the trace buffer
909 struct trace_buffer_control
911 /* Pointer to the first trace frame in the buffer. In the
912 non-circular case, this is equal to trace_buffer_lo, otherwise it
913 moves around in the buffer. */
914 unsigned char *start
;
916 /* Pointer to the free part of the trace buffer. Note that we clear
917 several bytes at and after this pointer, so that traceframe
918 scans/searches terminate properly. */
921 /* Pointer to the byte after the end of the free part. Note that
922 this may be smaller than trace_buffer_free in the circular case,
923 and means that the free part is in two pieces. Initially it is
924 equal to trace_buffer_hi, then is generally equivalent to
925 trace_buffer_start. */
926 unsigned char *end_free
;
928 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
929 this is the point at which the trace data breaks, and resumes at
934 /* Same as above, to be used by GDBserver when updating the in-process
936 struct ipa_trace_buffer_control
945 /* We have possibly both GDBserver and an inferior thread accessing
946 the same IPA trace buffer memory. The IPA is the producer (tries
947 to put new frames in the buffer), while GDBserver occasionally
948 consumes them, that is, flushes the IPA's buffer into its own
949 buffer. Both sides need to update the trace buffer control
950 pointers (current head, tail, etc.). We can't use a global lock to
951 synchronize the accesses, as otherwise we could deadlock GDBserver
952 (if the thread holding the lock stops for a signal, say). So
953 instead of that, we use a transaction scheme where GDBserver writes
954 always prevail over the IPAs writes, and, we have the IPA detect
955 the commit failure/overwrite, and retry the whole attempt. This is
956 mainly implemented by having a global token object that represents
957 who wrote last to the buffer control structure. We need to freeze
958 any inferior writing to the buffer while GDBserver touches memory,
959 so that the inferior can correctly detect that GDBserver had been
960 there, otherwise, it could mistakingly think its commit was
961 successful; that's implemented by simply having GDBserver set a
962 breakpoint the inferior hits if it is the critical region.
964 There are three cycling trace buffer control structure copies
965 (buffer head, tail, etc.), with the token object including an index
966 indicating which is current live copy. The IPA tentatively builds
967 an updated copy in a non-current control structure, while GDBserver
968 always clobbers the current version directly. The IPA then tries
969 to atomically "commit" its version; if GDBserver clobbered the
970 structure meanwhile, that will fail, and the IPA restarts the
973 Listing the step in further detail, we have:
975 In-process agent (producer):
977 - passes by `about_to_request_buffer_space' breakpoint/lock
979 - reads current token, extracts current trace buffer control index,
980 and starts tentatively updating the rightmost one (0->1, 1->2,
981 2->0). Note that only one inferior thread is executing this code
982 at any given time, due to an outer lock in the jump pads.
984 - updates counters, and tries to commit the token.
986 - passes by second `about_to_request_buffer_space' breakpoint/lock,
987 leaving the sync region.
989 - checks if the update was effective.
991 - if trace buffer was found full, hits flush_trace_buffer
992 breakpoint, and restarts later afterwards.
994 GDBserver (consumer):
996 - sets `about_to_request_buffer_space' breakpoint/lock.
998 - updates the token unconditionally, using the current buffer
999 control index, since it knows that the IP agent always writes to
1000 the rightmost, and due to the breakpoint, at most one IP thread
1001 can try to update the trace buffer concurrently to GDBserver, so
1002 there will be no danger of trace buffer control index wrap making
1003 the IPA write to the same index as GDBserver.
1005 - flushes the IP agent's trace buffer completely, and updates the
1006 current trace buffer control structure. GDBserver *always* wins.
1008 - removes the `about_to_request_buffer_space' breakpoint.
1010 The token is stored in the `trace_buffer_ctrl_curr' variable.
1011 Internally, it's bits are defined as:
1013 |-------------+-----+-------------+--------+-------------+--------------|
1014 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1015 |-------------+-----+-------------+--------+-------------+--------------|
1016 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1017 |-------------+-----+-------------+--------+-------------+--------------|
1019 GSB - GDBserver Stamp Bit
1020 PC - Previous Counter
1021 CC - Current Counter
1022 TBCI - Trace Buffer Control Index
1025 An IPA update of `trace_buffer_ctrl_curr' does:
1027 - read CC from the current token, save as PC.
1029 - atomically tries to write PC+1,CC
1031 A GDBserver update of `trace_buffer_ctrl_curr' does:
1033 - reads PC and CC from the current token.
1038 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1039 for the counters described below. The cleared bits are used to
1040 hold the index of the items of the `trace_buffer_ctrl' array that
1042 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1044 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1045 counter, and the `current' counter. */
1047 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1048 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1050 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1051 always stamps this bit as set. */
1052 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1054 #ifdef IN_PROCESS_AGENT
1055 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1056 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1058 # define TRACE_BUFFER_CTRL_CURR \
1059 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1063 /* The GDBserver side agent only needs one instance of this object, as
1064 it doesn't need to sync with itself. Define it as array anyway so
1065 that the rest of the code base doesn't need to care for the
1067 struct trace_buffer_control trace_buffer_ctrl
[1];
1068 # define TRACE_BUFFER_CTRL_CURR 0
1071 /* These are convenience macros used to access the current trace
1072 buffer control in effect. */
1073 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1074 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1075 #define trace_buffer_end_free \
1076 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1077 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1080 /* Macro that returns a pointer to the first traceframe in the buffer. */
1082 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1084 /* Macro that returns a pointer to the next traceframe in the buffer.
1085 If the computed location is beyond the wraparound point, subtract
1086 the offset of the wraparound. */
1088 #define NEXT_TRACEFRAME_1(TF) \
1089 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1091 #define NEXT_TRACEFRAME(TF) \
1092 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1093 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1094 ? (trace_buffer_wrap - trace_buffer_lo) \
1097 /* The difference between these counters represents the total number
1098 of complete traceframes present in the trace buffer. The IP agent
1099 writes to the write count, GDBserver writes to read count. */
1101 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1102 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1104 /* Convenience macro. */
1106 #define traceframe_count \
1107 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1109 /* The count of all traceframes created in the current run, including
1110 ones that were discarded to make room. */
1112 IP_AGENT_EXPORT
int traceframes_created
;
1114 #ifndef IN_PROCESS_AGENT
1116 /* Read-only regions are address ranges whose contents don't change,
1117 and so can be read from target memory even while looking at a trace
1118 frame. Without these, disassembly for instance will likely fail,
1119 because the program code is not usually collected into a trace
1120 frame. This data structure does not need to be very complicated or
1121 particularly efficient, it's only going to be used occasionally,
1122 and only by some commands. */
1124 struct readonly_region
1126 /* The bounds of the region. */
1127 CORE_ADDR start
, end
;
1129 /* Link to the next one. */
1130 struct readonly_region
*next
;
1133 /* Linked list of readonly regions. This list stays in effect from
1134 one tstart to the next. */
1136 static struct readonly_region
*readonly_regions
;
1140 /* The global that controls tracing overall. */
1142 IP_AGENT_EXPORT
int tracing
;
1144 #ifndef IN_PROCESS_AGENT
1146 /* Controls whether tracing should continue after GDB disconnects. */
1148 int disconnected_tracing
;
1150 /* The reason for the last tracing run to have stopped. We initialize
1151 to a distinct string so that GDB can distinguish between "stopped
1152 after running" and "stopped because never run" cases. */
1154 static const char *tracing_stop_reason
= "tnotrun";
1156 static int tracing_stop_tpnum
;
1158 /* 64-bit timestamps for the trace run's start and finish, expressed
1159 in microseconds from the Unix epoch. */
1161 LONGEST tracing_start_time
;
1162 LONGEST tracing_stop_time
;
1164 /* The (optional) user-supplied name of the user that started the run.
1165 This is an arbitrary string, and may be NULL. */
1167 char *tracing_user_name
;
1169 /* Optional user-supplied text describing the run. This is
1170 an arbitrary string, and may be NULL. */
1172 char *tracing_notes
;
1174 /* Optional user-supplied text explaining a tstop command. This is an
1175 arbitrary string, and may be NULL. */
1177 char *tracing_stop_note
;
1181 /* Functions local to this file. */
1183 /* Base "class" for tracepoint type specific data to be passed down to
1184 collect_data_at_tracepoint. */
1185 struct tracepoint_hit_ctx
1187 enum tracepoint_type type
;
1190 #ifdef IN_PROCESS_AGENT
1192 /* Fast/jump tracepoint specific data to be passed down to
1193 collect_data_at_tracepoint. */
1194 struct fast_tracepoint_ctx
1196 struct tracepoint_hit_ctx base
;
1198 struct regcache regcache
;
1199 int regcache_initted
;
1200 unsigned char *regspace
;
1202 unsigned char *regs
;
1203 struct tracepoint
*tpoint
;
1206 /* Static tracepoint specific data to be passed down to
1207 collect_data_at_tracepoint. */
1208 struct static_tracepoint_ctx
1210 struct tracepoint_hit_ctx base
;
1212 /* The regcache corresponding to the registers state at the time of
1213 the tracepoint hit. Initialized lazily, from REGS. */
1214 struct regcache regcache
;
1215 int regcache_initted
;
1217 /* The buffer space REGCACHE above uses. We use a separate buffer
1218 instead of letting the regcache malloc for both signal safety and
1219 performance reasons; this is allocated on the stack instead. */
1220 unsigned char *regspace
;
1222 /* The register buffer as passed on by lttng/ust. */
1223 struct registers
*regs
;
1225 /* The "printf" formatter and the args the user passed to the marker
1226 call. We use this to be able to collect "static trace data"
1231 /* The GDB tracepoint matching the probed marker that was "hit". */
1232 struct tracepoint
*tpoint
;
1237 /* Static tracepoint specific data to be passed down to
1238 collect_data_at_tracepoint. */
1239 struct trap_tracepoint_ctx
1241 struct tracepoint_hit_ctx base
;
1243 struct regcache
*regcache
;
1248 #ifndef IN_PROCESS_AGENT
1249 static struct agent_expr
*parse_agent_expr (char **actparm
);
1250 static char *unparse_agent_expr (struct agent_expr
*aexpr
);
1252 static enum eval_result_type
eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1253 struct traceframe
*tframe
,
1254 struct agent_expr
*aexpr
,
1257 static int agent_mem_read (struct traceframe
*tframe
,
1258 unsigned char *to
, CORE_ADDR from
, ULONGEST len
);
1259 static int agent_mem_read_string (struct traceframe
*tframe
,
1260 unsigned char *to
, CORE_ADDR from
,
1262 static int agent_tsv_read (struct traceframe
*tframe
, int n
);
1264 #ifndef IN_PROCESS_AGENT
1265 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1266 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1269 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1270 struct tracepoint
*tpoint
);
1272 #ifndef IN_PROCESS_AGENT
1273 static void clear_readonly_regions (void);
1274 static void clear_installed_tracepoints (void);
1277 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1279 struct tracepoint
*tpoint
);
1280 #ifndef IN_PROCESS_AGENT
1281 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1283 struct tracepoint
*tpoint
, int current_step
);
1284 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1285 CORE_ADDR
*jump_entry
);
1287 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1289 struct tracepoint
*tpoint
,
1290 struct traceframe
*tframe
,
1291 struct tracepoint_action
*taction
);
1293 #ifndef IN_PROCESS_AGENT
1294 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1296 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1297 static void download_tracepoint (struct tracepoint
*);
1298 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1301 static LONGEST
get_timestamp (void);
1303 #if defined(__GNUC__)
1304 # define memory_barrier() asm volatile ("" : : : "memory")
1306 # define memory_barrier() do {} while (0)
1309 /* We only build the IPA if this builtin is supported, and there are
1310 no uses of this in GDBserver itself, so we're safe in defining this
1312 #define cmpxchg(mem, oldval, newval) \
1313 __sync_val_compare_and_swap (mem, oldval, newval)
1315 /* The size in bytes of the buffer used to talk to the IPA helper
1317 #define CMD_BUF_SIZE 1024
1319 /* Record that an error occurred during expression evaluation. */
1322 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1323 enum eval_result_type rtype
)
1325 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1326 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1328 #ifdef IN_PROCESS_AGENT
1329 /* Only record the first error we get. */
1330 if (cmpxchg (&expr_eval_result
,
1332 rtype
) != expr_eval_no_error
)
1335 if (expr_eval_result
!= expr_eval_no_error
)
1339 error_tracepoint
= tpoint
;
1342 /* Trace buffer management. */
1345 clear_trace_buffer (void)
1347 trace_buffer_start
= trace_buffer_lo
;
1348 trace_buffer_free
= trace_buffer_lo
;
1349 trace_buffer_end_free
= trace_buffer_hi
;
1350 trace_buffer_wrap
= trace_buffer_hi
;
1351 /* A traceframe with zeroed fields marks the end of trace data. */
1352 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1353 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1354 traceframe_read_count
= traceframe_write_count
= 0;
1355 traceframes_created
= 0;
1358 #ifndef IN_PROCESS_AGENT
1361 clear_inferior_trace_buffer (void)
1363 CORE_ADDR ipa_trace_buffer_lo
;
1364 CORE_ADDR ipa_trace_buffer_hi
;
1365 struct traceframe ipa_traceframe
= { 0 };
1366 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1368 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1369 &ipa_trace_buffer_lo
);
1370 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1371 &ipa_trace_buffer_hi
);
1373 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1374 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1375 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1376 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1378 /* A traceframe with zeroed fields marks the end of trace data. */
1379 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1380 (unsigned char *) &ipa_trace_buffer_ctrl
,
1381 sizeof (ipa_trace_buffer_ctrl
));
1383 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1385 /* A traceframe with zeroed fields marks the end of trace data. */
1386 write_inferior_memory (ipa_trace_buffer_lo
,
1387 (unsigned char *) &ipa_traceframe
,
1388 sizeof (ipa_traceframe
));
1390 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1391 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1392 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1398 init_trace_buffer (unsigned char *buf
, int bufsize
)
1400 trace_buffer_lo
= buf
;
1401 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1403 clear_trace_buffer ();
1406 #ifdef IN_PROCESS_AGENT
1408 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1409 about_to_request_buffer_space (void)
1411 /* GDBserver places breakpoint here while it goes about to flush
1412 data at random times. */
1413 UNKNOWN_SIDE_EFFECTS();
1418 /* Carve out a piece of the trace buffer, returning NULL in case of
1422 trace_buffer_alloc (size_t amt
)
1424 unsigned char *rslt
;
1425 struct trace_buffer_control
*tbctrl
;
1427 #ifdef IN_PROCESS_AGENT
1428 unsigned int prev
, prev_filtered
;
1429 unsigned int commit_count
;
1430 unsigned int commit
;
1431 unsigned int readout
;
1433 struct traceframe
*oldest
;
1434 unsigned char *new_start
;
1437 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1438 (long) amt
, (long) sizeof (struct traceframe
));
1440 /* Account for the EOB marker. */
1441 amt
+= sizeof (struct traceframe
);
1443 #ifdef IN_PROCESS_AGENT
1447 /* Read the current token and extract the index to try to write to,
1448 storing it in CURR. */
1449 prev
= trace_buffer_ctrl_curr
;
1450 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1451 curr
= prev_filtered
+ 1;
1455 about_to_request_buffer_space ();
1457 /* Start out with a copy of the current state. GDBserver may be
1458 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1459 be able to commit anyway if that happens. */
1460 trace_buffer_ctrl
[curr
]
1461 = trace_buffer_ctrl
[prev_filtered
];
1462 trace_debug ("trying curr=%u", curr
);
1464 /* The GDBserver's agent doesn't need all that syncing, and always
1465 updates TCB 0 (there's only one, mind you). */
1468 tbctrl
= &trace_buffer_ctrl
[curr
];
1470 /* Offsets are easier to grok for debugging than raw addresses,
1471 especially for the small trace buffer sizes that are useful for
1473 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1475 (int) (tbctrl
->start
- trace_buffer_lo
),
1476 (int) (tbctrl
->free
- trace_buffer_lo
),
1477 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1478 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1479 (int) (trace_buffer_hi
- trace_buffer_lo
));
1481 /* The algorithm here is to keep trying to get a contiguous block of
1482 the requested size, possibly discarding older traceframes to free
1483 up space. Since free space might come in one or two pieces,
1484 depending on whether discarded traceframes wrapped around at the
1485 high end of the buffer, we test both pieces after each
1489 /* First, if we have two free parts, try the upper one first. */
1490 if (tbctrl
->end_free
< tbctrl
->free
)
1492 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1493 /* We have enough in the upper part. */
1497 /* Our high part of free space wasn't enough. Give up
1498 on it for now, set wraparound. We will recover the
1499 space later, if/when the wrapped-around traceframe is
1501 trace_debug ("Upper part too small, setting wraparound");
1502 tbctrl
->wrap
= tbctrl
->free
;
1503 tbctrl
->free
= trace_buffer_lo
;
1507 /* The normal case. */
1508 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1511 #ifdef IN_PROCESS_AGENT
1512 /* The IP Agent's buffer is always circular. It isn't used
1513 currently, but `circular_trace_buffer' could represent
1514 GDBserver's mode. If we didn't find space, ask GDBserver to
1517 flush_trace_buffer ();
1521 trace_debug ("gdbserver flushed buffer, retrying");
1525 /* GDBserver cancelled the tracing. Bail out as well. */
1528 /* If we're here, then neither part is big enough, and
1529 non-circular trace buffers are now full. */
1530 if (!circular_trace_buffer
)
1532 trace_debug ("Not enough space in the trace buffer");
1536 trace_debug ("Need more space in the trace buffer");
1538 /* If we have a circular buffer, we can try discarding the
1539 oldest traceframe and see if that helps. */
1540 oldest
= FIRST_TRACEFRAME ();
1541 if (oldest
->tpnum
== 0)
1543 /* Not good; we have no traceframes to free. Perhaps we're
1544 asking for a block that is larger than the buffer? In
1545 any case, give up. */
1546 trace_debug ("No traceframes to discard");
1550 /* We don't run this code in the in-process agent currently.
1551 E.g., we could leave the in-process agent in autonomous
1552 circular mode if we only have fast tracepoints. If we do
1553 that, then this bit becomes racy with GDBserver, which also
1554 writes to this counter. */
1555 --traceframe_write_count
;
1557 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1558 /* If we freed the traceframe that wrapped around, go back
1559 to the non-wrap case. */
1560 if (new_start
< tbctrl
->start
)
1562 trace_debug ("Discarding past the wraparound");
1563 tbctrl
->wrap
= trace_buffer_hi
;
1565 tbctrl
->start
= new_start
;
1566 tbctrl
->end_free
= tbctrl
->start
;
1568 trace_debug ("Discarded a traceframe\n"
1569 "Trace buffer [%d], start=%d free=%d "
1570 "endfree=%d wrap=%d hi=%d",
1572 (int) (tbctrl
->start
- trace_buffer_lo
),
1573 (int) (tbctrl
->free
- trace_buffer_lo
),
1574 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1575 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1576 (int) (trace_buffer_hi
- trace_buffer_lo
));
1578 /* Now go back around the loop. The discard might have resulted
1579 in either one or two pieces of free space, so we want to try
1580 both before freeing any more traceframes. */
1584 /* If we get here, we know we can provide the asked-for space. */
1586 rslt
= tbctrl
->free
;
1588 /* Adjust the request back down, now that we know we have space for
1589 the marker, but don't commit to AMT yet, we may still need to
1590 restart the operation if GDBserver touches the trace buffer
1591 (obviously only important in the in-process agent's version). */
1592 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1594 /* Or not. If GDBserver changed the trace buffer behind our back,
1595 we get to restart a new allocation attempt. */
1597 #ifdef IN_PROCESS_AGENT
1598 /* Build the tentative token. */
1599 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1600 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1601 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1605 /* Try to commit it. */
1606 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1607 if (readout
!= prev
)
1609 trace_debug ("GDBserver has touched the trace buffer, restarting."
1610 " (prev=%08x, commit=%08x, readout=%08x)",
1611 prev
, commit
, readout
);
1615 /* Hold your horses here. Even if that change was committed,
1616 GDBserver could come in, and clobber it. We need to hold to be
1617 able to tell if GDBserver clobbers before or after we committed
1618 the change. Whenever GDBserver goes about touching the IPA
1619 buffer, it sets a breakpoint in this routine, so we have a sync
1621 about_to_request_buffer_space ();
1623 /* Check if the change has been effective, even if GDBserver stopped
1624 us at the breakpoint. */
1627 unsigned int refetch
;
1631 refetch
= trace_buffer_ctrl_curr
;
1633 if (refetch
== commit
1634 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1637 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1638 "readout=%08x, refetch=%08x)",
1639 prev
, commit
, readout
, refetch
);
1643 trace_debug ("GDBserver has touched the trace buffer, not effective."
1644 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1645 prev
, commit
, readout
, refetch
);
1651 /* We have a new piece of the trace buffer. Hurray! */
1653 /* Add an EOB marker just past this allocation. */
1654 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1655 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1657 /* Adjust the request back down, now that we know we have space for
1659 amt
-= sizeof (struct traceframe
);
1663 trace_debug ("Allocated %d bytes", (int) amt
);
1664 trace_debug ("Trace buffer [%d] start=%d free=%d "
1665 "endfree=%d wrap=%d hi=%d",
1667 (int) (tbctrl
->start
- trace_buffer_lo
),
1668 (int) (tbctrl
->free
- trace_buffer_lo
),
1669 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1670 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1671 (int) (trace_buffer_hi
- trace_buffer_lo
));
1677 #ifndef IN_PROCESS_AGENT
1679 /* Return the total free space. This is not necessarily the largest
1680 block we can allocate, because of the two-part case. */
1685 if (trace_buffer_free
<= trace_buffer_end_free
)
1686 return trace_buffer_end_free
- trace_buffer_free
;
1688 return ((trace_buffer_end_free
- trace_buffer_lo
)
1689 + (trace_buffer_hi
- trace_buffer_free
));
1692 /* An 'S' in continuation packets indicates remainder are for
1695 static int seen_step_action_flag
;
1697 /* Create a tracepoint (location) with given number and address. Add this
1698 new tracepoint to list and sort this list. */
1700 static struct tracepoint
*
1701 add_tracepoint (int num
, CORE_ADDR addr
)
1703 struct tracepoint
*tpoint
, **tp_next
;
1705 tpoint
= xmalloc (sizeof (struct tracepoint
));
1706 tpoint
->number
= num
;
1707 tpoint
->address
= addr
;
1708 tpoint
->numactions
= 0;
1709 tpoint
->actions
= NULL
;
1710 tpoint
->actions_str
= NULL
;
1711 tpoint
->cond
= NULL
;
1712 tpoint
->num_step_actions
= 0;
1713 tpoint
->step_actions
= NULL
;
1714 tpoint
->step_actions_str
= NULL
;
1715 /* Start all off as regular (slow) tracepoints. */
1716 tpoint
->type
= trap_tracepoint
;
1717 tpoint
->orig_size
= -1;
1718 tpoint
->source_strings
= NULL
;
1719 tpoint
->compiled_cond
= 0;
1720 tpoint
->handle
= NULL
;
1721 tpoint
->next
= NULL
;
1723 /* Find a place to insert this tracepoint into list in order to keep
1724 the tracepoint list still in the ascending order. There may be
1725 multiple tracepoints at the same address as TPOINT's, and this
1726 guarantees TPOINT is inserted after all the tracepoints which are
1727 set at the same address. For example, fast tracepoints A, B, C are
1728 set at the same address, and D is to be insert at the same place as
1731 -->| A |--> | B |-->| C |->...
1733 One jump pad was created for tracepoint A, B, and C, and the target
1734 address of A is referenced/used in jump pad. So jump pad will let
1735 inferior jump to A. If D is inserted in front of A, like this,
1737 -->| D |-->| A |--> | B |-->| C |->...
1739 without updating jump pad, D is not reachable during collect, which
1740 is wrong. As we can see, the order of B, C and D doesn't matter, but
1741 A should always be the `first' one. */
1742 for (tp_next
= &tracepoints
;
1743 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1744 tp_next
= &(*tp_next
)->next
)
1746 tpoint
->next
= *tp_next
;
1748 last_tracepoint
= tpoint
;
1750 seen_step_action_flag
= 0;
1755 #ifndef IN_PROCESS_AGENT
1757 /* Return the tracepoint with the given number and address, or NULL. */
1759 static struct tracepoint
*
1760 find_tracepoint (int id
, CORE_ADDR addr
)
1762 struct tracepoint
*tpoint
;
1764 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1765 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1771 /* There may be several tracepoints with the same number (because they
1772 are "locations", in GDB parlance); return the next one after the
1773 given tracepoint, or search from the beginning of the list if the
1774 first argument is NULL. */
1776 static struct tracepoint
*
1777 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1779 struct tracepoint
*tpoint
;
1782 tpoint
= prev_tp
->next
;
1784 tpoint
= tracepoints
;
1785 for (; tpoint
; tpoint
= tpoint
->next
)
1786 if (tpoint
->number
== num
)
1795 save_string (const char *str
, size_t len
)
1799 s
= xmalloc (len
+ 1);
1800 memcpy (s
, str
, len
);
1806 /* Append another action to perform when the tracepoint triggers. */
1809 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1815 seen_step_action_flag
= 1;
1823 char *act_start
= act
;
1824 struct tracepoint_action
*action
= NULL
;
1830 struct collect_memory_action
*maction
;
1834 maction
= xmalloc (sizeof *maction
);
1835 maction
->base
.type
= *act
;
1836 action
= &maction
->base
;
1839 is_neg
= (*act
== '-');
1842 act
= unpack_varlen_hex (act
, &basereg
);
1844 act
= unpack_varlen_hex (act
, &maction
->addr
);
1846 act
= unpack_varlen_hex (act
, &maction
->len
);
1847 maction
->basereg
= (is_neg
1850 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1851 pulongest (maction
->len
),
1852 paddress (maction
->addr
), maction
->basereg
);
1857 struct collect_registers_action
*raction
;
1859 raction
= xmalloc (sizeof *raction
);
1860 raction
->base
.type
= *act
;
1861 action
= &raction
->base
;
1863 trace_debug ("Want to collect registers");
1865 /* skip past hex digits of mask for now */
1866 while (isxdigit(*act
))
1872 struct collect_static_trace_data_action
*raction
;
1874 raction
= xmalloc (sizeof *raction
);
1875 raction
->base
.type
= *act
;
1876 action
= &raction
->base
;
1878 trace_debug ("Want to collect static trace data");
1883 trace_debug ("Unexpected step action, ignoring");
1888 struct eval_expr_action
*xaction
;
1890 xaction
= xmalloc (sizeof (*xaction
));
1891 xaction
->base
.type
= *act
;
1892 action
= &xaction
->base
;
1894 trace_debug ("Want to evaluate expression");
1895 xaction
->expr
= parse_agent_expr (&act
);
1899 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1908 if (seen_step_action_flag
)
1910 tpoint
->num_step_actions
++;
1912 tpoint
->step_actions
1913 = xrealloc (tpoint
->step_actions
,
1914 (sizeof (*tpoint
->step_actions
)
1915 * tpoint
->num_step_actions
));
1916 tpoint
->step_actions_str
1917 = xrealloc (tpoint
->step_actions_str
,
1918 (sizeof (*tpoint
->step_actions_str
)
1919 * tpoint
->num_step_actions
));
1920 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1921 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1922 = save_string (act_start
, act
- act_start
);
1926 tpoint
->numactions
++;
1928 = xrealloc (tpoint
->actions
,
1929 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1931 = xrealloc (tpoint
->actions_str
,
1932 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1933 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1934 tpoint
->actions_str
[tpoint
->numactions
- 1]
1935 = save_string (act_start
, act
- act_start
);
1942 /* Find or create a trace state variable with the given number. */
1944 static struct trace_state_variable
*
1945 get_trace_state_variable (int num
)
1947 struct trace_state_variable
*tsv
;
1949 #ifdef IN_PROCESS_AGENT
1950 /* Search for an existing variable. */
1951 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1952 if (tsv
->number
== num
)
1956 /* Search for an existing variable. */
1957 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1958 if (tsv
->number
== num
)
1964 /* Find or create a trace state variable with the given number. */
1966 static struct trace_state_variable
*
1967 create_trace_state_variable (int num
, int gdb
)
1969 struct trace_state_variable
*tsv
;
1971 tsv
= get_trace_state_variable (num
);
1975 /* Create a new variable. */
1976 tsv
= xmalloc (sizeof (struct trace_state_variable
));
1978 tsv
->initial_value
= 0;
1982 #ifdef IN_PROCESS_AGENT
1985 tsv
->next
= alloced_trace_state_variables
;
1986 alloced_trace_state_variables
= tsv
;
1991 tsv
->next
= trace_state_variables
;
1992 trace_state_variables
= tsv
;
1997 IP_AGENT_EXPORT LONGEST
1998 get_trace_state_variable_value (int num
)
2000 struct trace_state_variable
*tsv
;
2002 tsv
= get_trace_state_variable (num
);
2006 trace_debug ("No trace state variable %d, skipping value get", num
);
2010 /* Call a getter function if we have one. While it's tempting to
2011 set up something to only call the getter once per tracepoint hit,
2012 it could run afoul of thread races. Better to let the getter
2013 handle it directly, if necessary to worry about it. */
2015 tsv
->value
= (tsv
->getter
) ();
2017 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2018 num
, plongest (tsv
->value
));
2023 IP_AGENT_EXPORT
void
2024 set_trace_state_variable_value (int num
, LONGEST val
)
2026 struct trace_state_variable
*tsv
;
2028 tsv
= get_trace_state_variable (num
);
2032 trace_debug ("No trace state variable %d, skipping value set", num
);
2040 set_trace_state_variable_name (int num
, const char *name
)
2042 struct trace_state_variable
*tsv
;
2044 tsv
= get_trace_state_variable (num
);
2048 trace_debug ("No trace state variable %d, skipping name set", num
);
2052 tsv
->name
= (char *) name
;
2056 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2058 struct trace_state_variable
*tsv
;
2060 tsv
= get_trace_state_variable (num
);
2064 trace_debug ("No trace state variable %d, skipping getter set", num
);
2068 tsv
->getter
= getter
;
2071 /* Add a raw traceframe for the given tracepoint. */
2073 static struct traceframe
*
2074 add_traceframe (struct tracepoint
*tpoint
)
2076 struct traceframe
*tframe
;
2078 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2083 tframe
->tpnum
= tpoint
->number
;
2084 tframe
->data_size
= 0;
2089 /* Add a block to the traceframe currently being worked on. */
2091 static unsigned char *
2092 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2094 unsigned char *block
;
2099 block
= trace_buffer_alloc (amt
);
2104 tframe
->data_size
+= amt
;
2109 /* Flag that the current traceframe is finished. */
2112 finish_traceframe (struct traceframe
*tframe
)
2114 ++traceframe_write_count
;
2115 ++traceframes_created
;
2118 #ifndef IN_PROCESS_AGENT
2120 /* Given a traceframe number NUM, find the NUMth traceframe in the
2123 static struct traceframe
*
2124 find_traceframe (int num
)
2126 struct traceframe
*tframe
;
2129 for (tframe
= FIRST_TRACEFRAME ();
2131 tframe
= NEXT_TRACEFRAME (tframe
))
2142 get_traceframe_address (struct traceframe
*tframe
)
2145 struct tracepoint
*tpoint
;
2147 addr
= traceframe_get_pc (tframe
);
2152 /* Fallback strategy, will be incorrect for while-stepping frames
2153 and multi-location tracepoints. */
2154 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2155 return tpoint
->address
;
2158 /* Search for the next traceframe whose address is inside or outside
2161 static struct traceframe
*
2162 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2165 struct traceframe
*tframe
;
2168 *tfnump
= current_traceframe
+ 1;
2169 tframe
= find_traceframe (*tfnump
);
2170 /* The search is not supposed to wrap around. */
2177 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2179 tfaddr
= get_traceframe_address (tframe
);
2181 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2182 : (lo
> tfaddr
|| tfaddr
> hi
))
2191 /* Search for the next traceframe recorded by the given tracepoint.
2192 Note that for multi-location tracepoints, this will find whatever
2193 location appears first. */
2195 static struct traceframe
*
2196 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2198 struct traceframe
*tframe
;
2200 *tfnump
= current_traceframe
+ 1;
2201 tframe
= find_traceframe (*tfnump
);
2202 /* The search is not supposed to wrap around. */
2209 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2211 if (tframe
->tpnum
== num
)
2222 #ifndef IN_PROCESS_AGENT
2224 /* Clear all past trace state. */
2227 cmd_qtinit (char *packet
)
2229 struct trace_state_variable
*tsv
, *prev
, *next
;
2231 /* Make sure we don't try to read from a trace frame. */
2232 current_traceframe
= -1;
2234 trace_debug ("Initializing the trace");
2236 clear_installed_tracepoints ();
2237 clear_readonly_regions ();
2240 last_tracepoint
= NULL
;
2242 /* Clear out any leftover trace state variables. Ones with target
2243 defined getters should be kept however. */
2245 tsv
= trace_state_variables
;
2248 trace_debug ("Looking at var %d", tsv
->number
);
2249 if (tsv
->getter
== NULL
)
2255 trace_state_variables
= next
;
2256 trace_debug ("Deleting var %d", tsv
->number
);
2267 clear_trace_buffer ();
2268 clear_inferior_trace_buffer ();
2273 /* Unprobe the UST marker at ADDRESS. */
2276 unprobe_marker_at (CORE_ADDR address
)
2278 char cmd
[CMD_BUF_SIZE
];
2280 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2281 run_inferior_command (cmd
);
2284 /* Restore the program to its pre-tracing state. This routine may be called
2285 in error situations, so it needs to be careful about only restoring
2286 from known-valid bits. */
2289 clear_installed_tracepoints (void)
2291 struct tracepoint
*tpoint
;
2292 struct tracepoint
*prev_stpoint
;
2295 cancel_breakpoints ();
2297 prev_stpoint
= NULL
;
2299 /* Restore any bytes overwritten by tracepoints. */
2300 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2302 /* Catch the case where we might try to remove a tracepoint that
2303 was never actually installed. */
2304 if (tpoint
->handle
== NULL
)
2306 trace_debug ("Tracepoint %d at 0x%s was "
2307 "never installed, nothing to clear",
2308 tpoint
->number
, paddress (tpoint
->address
));
2312 switch (tpoint
->type
)
2314 case trap_tracepoint
:
2315 delete_breakpoint (tpoint
->handle
);
2317 case fast_tracepoint
:
2318 delete_fast_tracepoint_jump (tpoint
->handle
);
2320 case static_tracepoint
:
2321 if (prev_stpoint
!= NULL
2322 && prev_stpoint
->address
== tpoint
->address
)
2323 /* Nothing to do. We already unprobed a tracepoint set at
2324 this marker address (and there can only be one probe
2329 unprobe_marker_at (tpoint
->address
);
2330 prev_stpoint
= tpoint
;
2335 tpoint
->handle
= NULL
;
2341 /* Parse a packet that defines a tracepoint. */
2344 cmd_qtdp (char *own_buf
)
2347 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2348 int trail_hyphen
= 0;
2352 struct tracepoint
*tpoint
;
2354 char *packet
= own_buf
;
2356 packet
+= strlen ("QTDP:");
2358 /* A hyphen at the beginning marks a packet specifying actions for a
2359 tracepoint already supplied. */
2366 packet
= unpack_varlen_hex (packet
, &num
);
2367 ++packet
; /* skip a colon */
2368 packet
= unpack_varlen_hex (packet
, &addr
);
2369 ++packet
; /* skip a colon */
2371 /* See if we already have this tracepoint. */
2372 tpoint
= find_tracepoint (num
, addr
);
2376 /* Duplicate tracepoints are never allowed. */
2379 trace_debug ("Tracepoint error: tracepoint %d"
2380 " at 0x%s already exists",
2381 (int) num
, paddress (addr
));
2382 write_enn (own_buf
);
2386 tpoint
= add_tracepoint (num
, addr
);
2388 tpoint
->enabled
= (*packet
== 'E');
2389 ++packet
; /* skip 'E' */
2390 ++packet
; /* skip a colon */
2391 packet
= unpack_varlen_hex (packet
, &count
);
2392 tpoint
->step_count
= count
;
2393 ++packet
; /* skip a colon */
2394 packet
= unpack_varlen_hex (packet
, &count
);
2395 tpoint
->pass_count
= count
;
2396 /* See if we have any of the additional optional fields. */
2397 while (*packet
== ':')
2402 tpoint
->type
= fast_tracepoint
;
2404 packet
= unpack_varlen_hex (packet
, &count
);
2405 tpoint
->orig_size
= count
;
2407 else if (*packet
== 'S')
2409 tpoint
->type
= static_tracepoint
;
2412 else if (*packet
== 'X')
2414 actparm
= (char *) packet
;
2415 tpoint
->cond
= parse_agent_expr (&actparm
);
2418 else if (*packet
== '-')
2420 else if (*packet
== '\0')
2423 trace_debug ("Unknown optional tracepoint field");
2428 trace_debug ("Also has actions\n");
2431 trace_debug ("Defined %stracepoint %d at 0x%s, "
2432 "enabled %d step %ld pass %ld",
2433 tpoint
->type
== fast_tracepoint
? "fast "
2434 : tpoint
->type
== static_tracepoint
? "static " : "",
2435 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2436 tpoint
->step_count
, tpoint
->pass_count
);
2439 add_tracepoint_action (tpoint
, packet
);
2442 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2443 (int) num
, paddress (addr
));
2444 write_enn (own_buf
);
2448 /* Install tracepoint during tracing only once for each tracepoint location.
2449 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2450 determine the last QTDP packet for one tracepoint location by checking
2451 trailing hyphen in QTDP packet. */
2452 if (tracing
&& !trail_hyphen
)
2454 /* Pause all threads temporarily while we patch tracepoints. */
2457 /* download_tracepoint will update global `tracepoints'
2458 list, so it is unsafe to leave threads in jump pad. */
2459 stabilize_threads ();
2461 /* Freeze threads. */
2464 download_tracepoint (tpoint
);
2465 install_tracepoint (tpoint
, own_buf
);
2475 cmd_qtdpsrc (char *own_buf
)
2477 ULONGEST num
, addr
, start
, slen
;
2478 struct tracepoint
*tpoint
;
2479 char *packet
= own_buf
;
2480 char *saved
, *srctype
, *src
;
2482 struct source_string
*last
, *newlast
;
2484 packet
+= strlen ("QTDPsrc:");
2486 packet
= unpack_varlen_hex (packet
, &num
);
2487 ++packet
; /* skip a colon */
2488 packet
= unpack_varlen_hex (packet
, &addr
);
2489 ++packet
; /* skip a colon */
2491 /* See if we already have this tracepoint. */
2492 tpoint
= find_tracepoint (num
, addr
);
2496 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2497 (int) num
, paddress (addr
));
2498 write_enn (own_buf
);
2503 packet
= strchr (packet
, ':');
2504 srctype
= xmalloc (packet
- saved
+ 1);
2505 memcpy (srctype
, saved
, packet
- saved
);
2506 srctype
[packet
- saved
] = '\0';
2508 packet
= unpack_varlen_hex (packet
, &start
);
2509 ++packet
; /* skip a colon */
2510 packet
= unpack_varlen_hex (packet
, &slen
);
2511 ++packet
; /* skip a colon */
2512 src
= xmalloc (slen
+ 1);
2513 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2516 newlast
= xmalloc (sizeof (struct source_string
));
2517 newlast
->type
= srctype
;
2519 newlast
->next
= NULL
;
2520 /* Always add a source string to the end of the list;
2521 this keeps sequences of actions/commands in the right
2523 if (tpoint
->source_strings
)
2525 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2527 last
->next
= newlast
;
2530 tpoint
->source_strings
= newlast
;
2536 cmd_qtdv (char *own_buf
)
2538 ULONGEST num
, val
, builtin
;
2541 struct trace_state_variable
*tsv
;
2542 char *packet
= own_buf
;
2544 packet
+= strlen ("QTDV:");
2546 packet
= unpack_varlen_hex (packet
, &num
);
2547 ++packet
; /* skip a colon */
2548 packet
= unpack_varlen_hex (packet
, &val
);
2549 ++packet
; /* skip a colon */
2550 packet
= unpack_varlen_hex (packet
, &builtin
);
2551 ++packet
; /* skip a colon */
2553 nbytes
= strlen (packet
) / 2;
2554 varname
= xmalloc (nbytes
+ 1);
2555 nbytes
= unhexify (varname
, packet
, nbytes
);
2556 varname
[nbytes
] = '\0';
2558 tsv
= create_trace_state_variable (num
, 1);
2559 tsv
->initial_value
= (LONGEST
) val
;
2560 tsv
->name
= varname
;
2562 set_trace_state_variable_value (num
, (LONGEST
) val
);
2568 cmd_qtenable_disable (char *own_buf
, int enable
)
2570 char *packet
= own_buf
;
2572 struct tracepoint
*tp
;
2574 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2575 packet
= unpack_varlen_hex (packet
, &num
);
2576 ++packet
; /* skip a colon */
2577 packet
= unpack_varlen_hex (packet
, &addr
);
2579 tp
= find_tracepoint (num
, addr
);
2583 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2585 trace_debug ("Tracepoint %d at 0x%s is already %s",
2586 (int) num
, paddress (addr
),
2587 enable
? "enabled" : "disabled");
2592 trace_debug ("%s tracepoint %d at 0x%s",
2593 enable
? "Enabling" : "Disabling",
2594 (int) num
, paddress (addr
));
2596 tp
->enabled
= enable
;
2598 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2601 int offset
= offsetof (struct tracepoint
, enabled
);
2602 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2604 ret
= prepare_to_access_memory ();
2607 trace_debug ("Failed to temporarily stop inferior threads");
2608 write_enn (own_buf
);
2612 ret
= write_inferior_integer (obj_addr
, enable
);
2613 done_accessing_memory ();
2617 trace_debug ("Cannot write enabled flag into "
2618 "inferior process memory");
2619 write_enn (own_buf
);
2628 trace_debug ("Tracepoint %d at 0x%s not found",
2629 (int) num
, paddress (addr
));
2630 write_enn (own_buf
);
2635 cmd_qtv (char *own_buf
)
2640 char *packet
= own_buf
;
2642 packet
+= strlen ("qTV:");
2643 unpack_varlen_hex (packet
, &num
);
2645 if (current_traceframe
>= 0)
2647 err
= traceframe_read_tsv ((int) num
, &val
);
2650 strcpy (own_buf
, "U");
2654 /* Only make tsv's be undefined before the first trace run. After a
2655 trace run is over, the user might want to see the last value of
2656 the tsv, and it might not be available in a traceframe. */
2657 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2659 strcpy (own_buf
, "U");
2663 val
= get_trace_state_variable_value (num
);
2665 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2668 /* Clear out the list of readonly regions. */
2671 clear_readonly_regions (void)
2673 struct readonly_region
*roreg
;
2675 while (readonly_regions
)
2677 roreg
= readonly_regions
;
2678 readonly_regions
= readonly_regions
->next
;
2683 /* Parse the collection of address ranges whose contents GDB believes
2684 to be unchanging and so can be read directly from target memory
2685 even while looking at a traceframe. */
2688 cmd_qtro (char *own_buf
)
2690 ULONGEST start
, end
;
2691 struct readonly_region
*roreg
;
2692 char *packet
= own_buf
;
2694 trace_debug ("Want to mark readonly regions");
2696 clear_readonly_regions ();
2698 packet
+= strlen ("QTro");
2700 while (*packet
== ':')
2702 ++packet
; /* skip a colon */
2703 packet
= unpack_varlen_hex (packet
, &start
);
2704 ++packet
; /* skip a comma */
2705 packet
= unpack_varlen_hex (packet
, &end
);
2706 roreg
= xmalloc (sizeof (struct readonly_region
));
2707 roreg
->start
= start
;
2709 roreg
->next
= readonly_regions
;
2710 readonly_regions
= roreg
;
2711 trace_debug ("Added readonly region from 0x%s to 0x%s",
2712 paddress (roreg
->start
), paddress (roreg
->end
));
2718 /* Test to see if the given range is in our list of readonly ranges.
2719 We only test for being entirely within a range, GDB is not going to
2720 send a single memory packet that spans multiple regions. */
2723 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2725 struct readonly_region
*roreg
;
2727 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2728 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2734 /* The maximum size of a jump pad entry. */
2735 static const int max_jump_pad_size
= 0x100;
2737 static CORE_ADDR gdb_jump_pad_head
;
2739 /* Return the address of the next free jump space. */
2742 get_jump_space_head (void)
2744 if (gdb_jump_pad_head
== 0)
2746 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2747 &gdb_jump_pad_head
))
2748 fatal ("error extracting jump_pad_buffer");
2751 return gdb_jump_pad_head
;
2754 /* Reserve USED bytes from the jump space. */
2757 claim_jump_space (ULONGEST used
)
2759 trace_debug ("claim_jump_space reserves %s bytes at %s",
2760 pulongest (used
), paddress (gdb_jump_pad_head
));
2761 gdb_jump_pad_head
+= used
;
2764 static CORE_ADDR trampoline_buffer_head
= 0;
2765 static CORE_ADDR trampoline_buffer_tail
;
2767 /* Reserve USED bytes from the trampoline buffer and return the
2768 address of the start of the reserved space in TRAMPOLINE. Returns
2769 non-zero if the space is successfully claimed. */
2772 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2774 if (!trampoline_buffer_head
)
2776 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2777 &trampoline_buffer_tail
))
2779 fatal ("error extracting trampoline_buffer");
2783 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2784 &trampoline_buffer_head
))
2786 fatal ("error extracting trampoline_buffer_end");
2791 /* Start claiming space from the top of the trampoline space. If
2792 the space is located at the bottom of the virtual address space,
2793 this reduces the possibility that corruption will occur if a null
2794 pointer is used to write to memory. */
2795 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2797 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2802 trampoline_buffer_head
-= used
;
2804 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2805 pulongest (used
), paddress (trampoline_buffer_head
));
2807 *trampoline
= trampoline_buffer_head
;
2811 /* Returns non-zero if there is space allocated for use in trampolines
2812 for fast tracepoints. */
2815 have_fast_tracepoint_trampoline_buffer (char *buf
)
2817 CORE_ADDR trampoline_end
, errbuf
;
2819 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2822 fatal ("error extracting trampoline_buffer_end");
2829 strcpy (buf
, "was claiming");
2830 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
2833 fatal ("error extracting errbuf");
2837 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
2840 return trampoline_end
!= 0;
2843 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2844 the command fails, or 0 otherwise. If the command ran
2845 successfully, but probing the marker failed, ERROUT will be filled
2846 with the error to reply to GDB, and -1 is also returned. This
2847 allows directly passing IPA errors to GDB. */
2850 probe_marker_at (CORE_ADDR address
, char *errout
)
2852 char cmd
[CMD_BUF_SIZE
];
2855 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
2856 err
= run_inferior_command (cmd
);
2862 strcpy (errout
, cmd
);
2871 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
2873 to
->jump_pad
= from
->jump_pad
;
2874 to
->jump_pad_end
= from
->jump_pad_end
;
2875 to
->trampoline
= from
->trampoline
;
2876 to
->trampoline_end
= from
->trampoline_end
;
2877 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
2878 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
2879 to
->handle
= from
->handle
;
2881 gdb_assert (from
->handle
);
2882 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
2885 #define MAX_JUMP_SIZE 20
2887 /* Install fast tracepoint. Return 0 if successful, otherwise return
2891 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
2893 CORE_ADDR jentry
, jump_entry
;
2894 CORE_ADDR trampoline
;
2895 ULONGEST trampoline_size
;
2897 /* The jump to the jump pad of the last fast tracepoint
2899 unsigned char fjump
[MAX_JUMP_SIZE
];
2900 ULONGEST fjump_size
;
2902 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
2904 trace_debug ("Requested a fast tracepoint on an instruction "
2905 "that is of less than the minimum length.");
2909 jentry
= jump_entry
= get_jump_space_head ();
2912 trampoline_size
= 0;
2914 /* Install the jump pad. */
2915 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
2917 ipa_sym_addrs
.addr_gdb_collect
,
2918 ipa_sym_addrs
.addr_collecting
,
2921 &trampoline
, &trampoline_size
,
2923 &tpoint
->adjusted_insn_addr
,
2924 &tpoint
->adjusted_insn_addr_end
,
2931 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
2934 if (tpoint
->handle
!= NULL
)
2936 tpoint
->jump_pad
= jump_entry
;
2937 tpoint
->jump_pad_end
= jentry
;
2938 tpoint
->trampoline
= trampoline
;
2939 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
2941 /* Pad to 8-byte alignment. */
2942 jentry
= ((jentry
+ 7) & ~0x7);
2943 claim_jump_space (jentry
- jump_entry
);
2950 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
2953 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
2955 tpoint
->handle
= NULL
;
2958 if (tpoint
->type
== trap_tracepoint
)
2960 /* Tracepoints are installed as memory breakpoints. Just go
2961 ahead and install the trap. The breakpoints module
2962 handles duplicated breakpoints, and the memory read
2963 routine handles un-patching traps from memory reads. */
2964 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
2965 tracepoint_handler
);
2967 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
2969 struct tracepoint
*tp
;
2971 if (!in_process_agent_loaded ())
2973 trace_debug ("Requested a %s tracepoint, but fast "
2974 "tracepoints aren't supported.",
2975 tpoint
->type
== static_tracepoint
? "static" : "fast");
2976 write_e_ipa_not_loaded (own_buf
);
2979 if (tpoint
->type
== static_tracepoint
&& !in_process_agent_loaded_ust ())
2981 trace_debug ("Requested a static tracepoint, but static "
2982 "tracepoints are not supported.");
2983 write_e_ust_not_loaded (own_buf
);
2987 /* Find another fast or static tracepoint at the same address. */
2988 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2990 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2991 && tp
->number
!= tpoint
->number
)
2995 if (tpoint
->type
== fast_tracepoint
)
2997 if (tp
) /* TPOINT is installed at the same address as TP. */
2998 clone_fast_tracepoint (tpoint
, tp
);
3000 install_fast_tracepoint (tpoint
, own_buf
);
3005 tpoint
->handle
= (void *) -1;
3008 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3009 tpoint
->handle
= (void *) -1;
3015 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3017 if (tpoint
->handle
== NULL
)
3019 if (*own_buf
== '\0')
3020 write_enn (own_buf
);
3027 cmd_qtstart (char *packet
)
3029 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3031 trace_debug ("Starting the trace");
3033 /* Pause all threads temporarily while we patch tracepoints. */
3036 /* Get threads out of jump pads. Safe to do here, since this is a
3037 top level command. And, required to do here, since we're
3038 deleting/rewriting jump pads. */
3040 stabilize_threads ();
3042 /* Freeze threads. */
3045 /* Sync the fast tracepoints list in the inferior ftlib. */
3046 if (in_process_agent_loaded ())
3048 download_tracepoints ();
3049 download_trace_state_variables ();
3052 /* No previous fast tpoint yet. */
3053 prev_ftpoint
= NULL
;
3055 /* No previous static tpoint yet. */
3056 prev_stpoint
= NULL
;
3060 /* Install tracepoints. */
3061 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3063 /* Ensure all the hit counts start at zero. */
3064 tpoint
->hit_count
= 0;
3065 tpoint
->traceframe_usage
= 0;
3067 if (tpoint
->type
== trap_tracepoint
)
3069 /* Tracepoints are installed as memory breakpoints. Just go
3070 ahead and install the trap. The breakpoints module
3071 handles duplicated breakpoints, and the memory read
3072 routine handles un-patching traps from memory reads. */
3073 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3074 tracepoint_handler
);
3076 else if (tpoint
->type
== fast_tracepoint
)
3078 if (maybe_write_ipa_not_loaded (packet
))
3080 trace_debug ("Requested a fast tracepoint, but fast "
3081 "tracepoints aren't supported.");
3085 if (prev_ftpoint
!= NULL
&& prev_ftpoint
->address
== tpoint
->address
)
3086 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3089 if (install_fast_tracepoint (tpoint
, packet
) == 0)
3090 prev_ftpoint
= tpoint
;
3093 else if (tpoint
->type
== static_tracepoint
)
3095 if (maybe_write_ipa_ust_not_loaded (packet
))
3097 trace_debug ("Requested a static tracepoint, but static "
3098 "tracepoints are not supported.");
3102 /* Can only probe a given marker once. */
3103 if (prev_stpoint
!= NULL
&& prev_stpoint
->address
== tpoint
->address
)
3105 tpoint
->handle
= (void *) -1;
3109 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3111 tpoint
->handle
= (void *) -1;
3113 /* So that we can handle multiple static tracepoints
3114 at the same address easily. */
3115 prev_stpoint
= tpoint
;
3120 /* Any failure in the inner loop is sufficient cause to give
3122 if (tpoint
->handle
== NULL
)
3126 /* Any error in tracepoint insertion is unacceptable; better to
3127 address the problem now, than end up with a useless or misleading
3131 clear_installed_tracepoints ();
3132 if (*packet
== '\0')
3138 stopping_tracepoint
= NULL
;
3139 trace_buffer_is_full
= 0;
3140 expr_eval_result
= expr_eval_no_error
;
3141 error_tracepoint
= NULL
;
3142 tracing_start_time
= get_timestamp ();
3144 /* Tracing is now active, hits will now start being logged. */
3147 if (in_process_agent_loaded ())
3149 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3150 fatal ("Error setting tracing variable in lib");
3152 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3154 fatal ("Error clearing stopping_tracepoint variable in lib");
3156 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3157 fatal ("Error clearing trace_buffer_is_full variable in lib");
3159 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3160 stop_tracing_handler
);
3161 if (stop_tracing_bkpt
== NULL
)
3162 error ("Error setting stop_tracing breakpoint");
3164 flush_trace_buffer_bkpt
3165 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3166 flush_trace_buffer_handler
);
3167 if (flush_trace_buffer_bkpt
== NULL
)
3168 error ("Error setting flush_trace_buffer breakpoint");
3176 /* End a tracing run, filling in a stop reason to report back to GDB,
3177 and removing the tracepoints from the code. */
3184 trace_debug ("Tracing is already off, ignoring");
3188 trace_debug ("Stopping the trace");
3190 /* Pause all threads before removing fast jumps from memory,
3191 breakpoints, and touching IPA state variables (inferior memory).
3192 Some thread may hit the internal tracing breakpoints, or be
3193 collecting this moment, but that's ok, we don't release the
3194 tpoint object's memory or the jump pads here (we only do that
3195 when we're sure we can move all threads out of the jump pads).
3196 We can't now, since we may be getting here due to the inferior
3197 agent calling us. */
3199 /* Since we're removing breakpoints, cancel breakpoint hits,
3200 possibly related to the breakpoints we're about to delete. */
3201 cancel_breakpoints ();
3203 /* Stop logging. Tracepoints can still be hit, but they will not be
3206 if (in_process_agent_loaded ())
3208 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3209 fatal ("Error clearing tracing variable in lib");
3212 tracing_stop_time
= get_timestamp ();
3213 tracing_stop_reason
= "t???";
3214 tracing_stop_tpnum
= 0;
3215 if (stopping_tracepoint
)
3217 trace_debug ("Stopping the trace because "
3218 "tracepoint %d was hit %ld times",
3219 stopping_tracepoint
->number
,
3220 stopping_tracepoint
->pass_count
);
3221 tracing_stop_reason
= "tpasscount";
3222 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3224 else if (trace_buffer_is_full
)
3226 trace_debug ("Stopping the trace because the trace buffer is full");
3227 tracing_stop_reason
= "tfull";
3229 else if (expr_eval_result
!= expr_eval_no_error
)
3231 trace_debug ("Stopping the trace because of an expression eval error");
3232 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3233 tracing_stop_tpnum
= error_tracepoint
->number
;
3235 #ifndef IN_PROCESS_AGENT
3236 else if (!gdb_connected ())
3238 trace_debug ("Stopping the trace because GDB disconnected");
3239 tracing_stop_reason
= "tdisconnected";
3244 trace_debug ("Stopping the trace because of a tstop command");
3245 tracing_stop_reason
= "tstop";
3248 stopping_tracepoint
= NULL
;
3249 error_tracepoint
= NULL
;
3251 /* Clear out the tracepoints. */
3252 clear_installed_tracepoints ();
3254 if (in_process_agent_loaded ())
3256 /* Pull in fast tracepoint trace frames from the inferior lib
3257 buffer into our buffer, even if our buffer is already full,
3258 because we want to present the full number of created frames
3259 in addition to what fit in the trace buffer. */
3260 upload_fast_traceframes ();
3263 if (stop_tracing_bkpt
!= NULL
)
3265 delete_breakpoint (stop_tracing_bkpt
);
3266 stop_tracing_bkpt
= NULL
;
3269 if (flush_trace_buffer_bkpt
!= NULL
)
3271 delete_breakpoint (flush_trace_buffer_bkpt
);
3272 flush_trace_buffer_bkpt
= NULL
;
3279 stop_tracing_handler (CORE_ADDR addr
)
3281 trace_debug ("lib hit stop_tracing");
3283 /* Don't actually handle it here. When we stop tracing we remove
3284 breakpoints from the inferior, and that is not allowed in a
3285 breakpoint handler (as the caller is walking the breakpoint
3291 flush_trace_buffer_handler (CORE_ADDR addr
)
3293 trace_debug ("lib hit flush_trace_buffer");
3298 cmd_qtstop (char *packet
)
3305 cmd_qtdisconnected (char *own_buf
)
3308 char *packet
= own_buf
;
3310 packet
+= strlen ("QTDisconnected:");
3312 unpack_varlen_hex (packet
, &setting
);
3316 disconnected_tracing
= setting
;
3320 cmd_qtframe (char *own_buf
)
3322 ULONGEST frame
, pc
, lo
, hi
, num
;
3324 struct traceframe
*tframe
;
3325 char *packet
= own_buf
;
3327 packet
+= strlen ("QTFrame:");
3329 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3331 packet
+= strlen ("pc:");
3332 unpack_varlen_hex (packet
, &pc
);
3333 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3334 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3336 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3338 packet
+= strlen ("range:");
3339 packet
= unpack_varlen_hex (packet
, &lo
);
3341 unpack_varlen_hex (packet
, &hi
);
3342 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3343 paddress (lo
), paddress (hi
));
3344 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3346 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3348 packet
+= strlen ("outside:");
3349 packet
= unpack_varlen_hex (packet
, &lo
);
3351 unpack_varlen_hex (packet
, &hi
);
3352 trace_debug ("Want to find next traceframe "
3353 "outside the range 0x%s to 0x%s",
3354 paddress (lo
), paddress (hi
));
3355 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3357 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3359 packet
+= strlen ("tdp:");
3360 unpack_varlen_hex (packet
, &num
);
3362 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3363 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3367 unpack_varlen_hex (packet
, &frame
);
3368 tfnum
= (int) frame
;
3371 trace_debug ("Want to stop looking at traceframes");
3372 current_traceframe
= -1;
3376 trace_debug ("Want to look at traceframe %d", tfnum
);
3377 tframe
= find_traceframe (tfnum
);
3382 current_traceframe
= tfnum
;
3383 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3386 sprintf (own_buf
, "F-1");
3390 cmd_qtstatus (char *packet
)
3392 char *stop_reason_rsp
= NULL
;
3393 char *buf1
, *buf2
, *buf3
, *str
;
3396 /* Translate the plain text of the notes back into hex for
3399 str
= (tracing_user_name
? tracing_user_name
: "");
3400 slen
= strlen (str
);
3401 buf1
= (char *) alloca (slen
* 2 + 1);
3402 hexify (buf1
, str
, slen
);
3404 str
= (tracing_notes
? tracing_notes
: "");
3405 slen
= strlen (str
);
3406 buf2
= (char *) alloca (slen
* 2 + 1);
3407 hexify (buf2
, str
, slen
);
3409 str
= (tracing_stop_note
? tracing_stop_note
: "");
3410 slen
= strlen (str
);
3411 buf3
= (char *) alloca (slen
* 2 + 1);
3412 hexify (buf3
, str
, slen
);
3414 trace_debug ("Returning trace status as %d, stop reason %s",
3415 tracing
, tracing_stop_reason
);
3417 if (in_process_agent_loaded ())
3421 upload_fast_traceframes ();
3426 stop_reason_rsp
= (char *) tracing_stop_reason
;
3428 /* The user visible error string in terror needs to be hex encoded.
3429 We leave it as plain string in `tracing_stop_reason' to ease
3431 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3433 const char *result_name
;
3437 result_name
= stop_reason_rsp
+ strlen ("terror:");
3438 hexstr_len
= strlen (result_name
) * 2;
3439 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3440 strcpy (p
, "terror:");
3442 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3445 /* If this was a forced stop, include any stop note that was supplied. */
3446 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3448 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3449 strcpy (stop_reason_rsp
, "tstop:");
3450 strcat (stop_reason_rsp
, buf3
);
3456 "tframes:%x;tcreated:%x;"
3457 "tfree:%x;tsize:%s;"
3460 "starttime:%s;stoptime:%s;"
3461 "username:%s:;notes:%s:",
3463 stop_reason_rsp
, tracing_stop_tpnum
,
3464 traceframe_count
, traceframes_created
,
3465 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3466 circular_trace_buffer
,
3467 disconnected_tracing
,
3468 plongest (tracing_start_time
), plongest (tracing_stop_time
),
3473 cmd_qtp (char *own_buf
)
3476 struct tracepoint
*tpoint
;
3477 char *packet
= own_buf
;
3479 packet
+= strlen ("qTP:");
3481 packet
= unpack_varlen_hex (packet
, &num
);
3482 ++packet
; /* skip a colon */
3483 packet
= unpack_varlen_hex (packet
, &addr
);
3485 /* See if we already have this tracepoint. */
3486 tpoint
= find_tracepoint (num
, addr
);
3490 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3491 (int) num
, paddress (addr
));
3492 write_enn (own_buf
);
3496 sprintf (own_buf
, "V%lx:%lx", tpoint
->hit_count
, tpoint
->traceframe_usage
);
3499 /* State variables to help return all the tracepoint bits. */
3500 static struct tracepoint
*cur_tpoint
;
3501 static int cur_action
;
3502 static int cur_step_action
;
3503 static struct source_string
*cur_source_string
;
3504 static struct trace_state_variable
*cur_tsv
;
3506 /* Compose a response that is an imitation of the syntax by which the
3507 tracepoint was originally downloaded. */
3510 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3514 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
3515 paddress (tpoint
->address
),
3516 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3517 tpoint
->pass_count
);
3518 if (tpoint
->type
== fast_tracepoint
)
3519 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3520 else if (tpoint
->type
== static_tracepoint
)
3521 sprintf (packet
+ strlen (packet
), ":S");
3525 buf
= unparse_agent_expr (tpoint
->cond
);
3526 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3527 tpoint
->cond
->length
, buf
);
3532 /* Compose a response that is an imitation of the syntax by which the
3533 tracepoint action was originally downloaded (with the difference
3534 that due to the way we store the actions, this will output a packet
3535 per action, while GDB could have combined more than one action
3539 response_action (char *packet
, struct tracepoint
*tpoint
,
3540 char *taction
, int step
)
3542 sprintf (packet
, "%c%x:%s:%s",
3543 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3547 /* Compose a response that is an imitation of the syntax by which the
3548 tracepoint source piece was originally downloaded. */
3551 response_source (char *packet
,
3552 struct tracepoint
*tpoint
, struct source_string
*src
)
3557 len
= strlen (src
->str
);
3558 buf
= alloca (len
* 2 + 1);
3559 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3561 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3562 tpoint
->number
, paddress (tpoint
->address
),
3563 src
->type
, 0, len
, buf
);
3566 /* Return the first piece of tracepoint definition, and initialize the
3567 state machine that will iterate through all the tracepoint
3571 cmd_qtfp (char *packet
)
3573 trace_debug ("Returning first tracepoint definition piece");
3575 cur_tpoint
= tracepoints
;
3576 cur_action
= cur_step_action
= -1;
3577 cur_source_string
= NULL
;
3580 response_tracepoint (packet
, cur_tpoint
);
3582 strcpy (packet
, "l");
3585 /* Return additional pieces of tracepoint definition. Each action and
3586 stepping action must go into its own packet, because of packet size
3587 limits, and so we use state variables to deliver one piece at a
3591 cmd_qtsp (char *packet
)
3593 trace_debug ("Returning subsequent tracepoint definition piece");
3597 /* This case would normally never occur, but be prepared for
3599 strcpy (packet
, "l");
3601 else if (cur_action
< cur_tpoint
->numactions
- 1)
3604 response_action (packet
, cur_tpoint
,
3605 cur_tpoint
->actions_str
[cur_action
], 0);
3607 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3610 response_action (packet
, cur_tpoint
,
3611 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3613 else if ((cur_source_string
3614 ? cur_source_string
->next
3615 : cur_tpoint
->source_strings
))
3617 if (cur_source_string
)
3618 cur_source_string
= cur_source_string
->next
;
3620 cur_source_string
= cur_tpoint
->source_strings
;
3621 response_source (packet
, cur_tpoint
, cur_source_string
);
3625 cur_tpoint
= cur_tpoint
->next
;
3626 cur_action
= cur_step_action
= -1;
3627 cur_source_string
= NULL
;
3629 response_tracepoint (packet
, cur_tpoint
);
3631 strcpy (packet
, "l");
3635 /* Compose a response that is an imitation of the syntax by which the
3636 trace state variable was originally downloaded. */
3639 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3641 char *buf
= (char *) "";
3646 namelen
= strlen (tsv
->name
);
3647 buf
= alloca (namelen
* 2 + 1);
3648 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3651 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3652 tsv
->getter
? 1 : 0, buf
);
3655 /* Return the first trace state variable definition, and initialize
3656 the state machine that will iterate through all the tsv bits. */
3659 cmd_qtfv (char *packet
)
3661 trace_debug ("Returning first trace state variable definition");
3663 cur_tsv
= trace_state_variables
;
3666 response_tsv (packet
, cur_tsv
);
3668 strcpy (packet
, "l");
3671 /* Return additional trace state variable definitions. */
3674 cmd_qtsv (char *packet
)
3676 trace_debug ("Returning first trace state variable definition");
3680 /* This case would normally never occur, but be prepared for
3682 strcpy (packet
, "l");
3686 cur_tsv
= cur_tsv
->next
;
3688 response_tsv (packet
, cur_tsv
);
3690 strcpy (packet
, "l");
3693 strcpy (packet
, "l");
3696 /* Return the first static tracepoint marker, and initialize the state
3697 machine that will iterate through all the static tracepoints
3701 cmd_qtfstm (char *packet
)
3703 if (!maybe_write_ipa_ust_not_loaded (packet
))
3704 run_inferior_command (packet
);
3707 /* Return additional static tracepoints markers. */
3710 cmd_qtsstm (char *packet
)
3712 if (!maybe_write_ipa_ust_not_loaded (packet
))
3713 run_inferior_command (packet
);
3716 /* Return the definition of the static tracepoint at a given address.
3717 Result packet is the same as qTsST's. */
3720 cmd_qtstmat (char *packet
)
3722 if (!maybe_write_ipa_ust_not_loaded (packet
))
3723 run_inferior_command (packet
);
3726 /* Return the minimum instruction size needed for fast tracepoints as a
3727 hexadecimal number. */
3730 cmd_qtminftpilen (char *packet
)
3732 if (current_inferior
== NULL
)
3734 /* Indicate that the minimum length is currently unknown. */
3735 strcpy (packet
, "0");
3739 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3742 /* Respond to qTBuffer packet with a block of raw data from the trace
3743 buffer. GDB may ask for a lot, but we are allowed to reply with
3744 only as much as will fit within packet limits or whatever. */
3747 cmd_qtbuffer (char *own_buf
)
3749 ULONGEST offset
, num
, tot
;
3751 char *packet
= own_buf
;
3753 packet
+= strlen ("qTBuffer:");
3755 packet
= unpack_varlen_hex (packet
, &offset
);
3756 ++packet
; /* skip a comma */
3757 unpack_varlen_hex (packet
, &num
);
3759 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3760 (int) num
, pulongest (offset
));
3762 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3764 /* If we're right at the end, reply specially that we're done. */
3767 strcpy (own_buf
, "l");
3771 /* Object to any other out-of-bounds request. */
3774 write_enn (own_buf
);
3778 /* Compute the pointer corresponding to the given offset, accounting
3780 tbp
= trace_buffer_start
+ offset
;
3781 if (tbp
>= trace_buffer_wrap
)
3782 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3784 /* Trim to the remaining bytes if we're close to the end. */
3785 if (num
> tot
- offset
)
3788 /* Trim to available packet size. */
3789 if (num
>= (PBUFSIZ
- 16) / 2 )
3790 num
= (PBUFSIZ
- 16) / 2;
3792 convert_int_to_ascii (tbp
, own_buf
, num
);
3793 own_buf
[num
] = '\0';
3797 cmd_bigqtbuffer (char *own_buf
)
3800 char *packet
= own_buf
;
3802 packet
+= strlen ("QTBuffer:");
3804 if (strncmp ("circular:", packet
, strlen ("circular:")) == 0)
3806 packet
+= strlen ("circular:");
3807 unpack_varlen_hex (packet
, &val
);
3808 circular_trace_buffer
= val
;
3809 trace_debug ("Trace buffer is now %s",
3810 circular_trace_buffer
? "circular" : "linear");
3814 write_enn (own_buf
);
3818 cmd_qtnotes (char *own_buf
)
3821 char *saved
, *user
, *notes
, *stopnote
;
3822 char *packet
= own_buf
;
3824 packet
+= strlen ("QTNotes:");
3828 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
3830 packet
+= strlen ("user:");
3832 packet
= strchr (packet
, ';');
3833 nbytes
= (packet
- saved
) / 2;
3834 user
= xmalloc (nbytes
+ 1);
3835 nbytes
= unhexify (user
, saved
, nbytes
);
3836 user
[nbytes
] = '\0';
3837 ++packet
; /* skip the semicolon */
3838 trace_debug ("User is '%s'", user
);
3839 tracing_user_name
= user
;
3841 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
3843 packet
+= strlen ("notes:");
3845 packet
= strchr (packet
, ';');
3846 nbytes
= (packet
- saved
) / 2;
3847 notes
= xmalloc (nbytes
+ 1);
3848 nbytes
= unhexify (notes
, saved
, nbytes
);
3849 notes
[nbytes
] = '\0';
3850 ++packet
; /* skip the semicolon */
3851 trace_debug ("Notes is '%s'", notes
);
3852 tracing_notes
= notes
;
3854 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
3856 packet
+= strlen ("tstop:");
3858 packet
= strchr (packet
, ';');
3859 nbytes
= (packet
- saved
) / 2;
3860 stopnote
= xmalloc (nbytes
+ 1);
3861 nbytes
= unhexify (stopnote
, saved
, nbytes
);
3862 stopnote
[nbytes
] = '\0';
3863 ++packet
; /* skip the semicolon */
3864 trace_debug ("tstop note is '%s'", stopnote
);
3865 tracing_stop_note
= stopnote
;
3875 handle_tracepoint_general_set (char *packet
)
3877 if (strcmp ("QTinit", packet
) == 0)
3879 cmd_qtinit (packet
);
3882 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3887 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3889 cmd_qtdpsrc (packet
);
3892 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
3894 cmd_qtenable_disable (packet
, 1);
3897 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
3899 cmd_qtenable_disable (packet
, 0);
3902 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3907 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3912 else if (strcmp ("QTStart", packet
) == 0)
3914 cmd_qtstart (packet
);
3917 else if (strcmp ("QTStop", packet
) == 0)
3919 cmd_qtstop (packet
);
3922 else if (strncmp ("QTDisconnected:", packet
,
3923 strlen ("QTDisconnected:")) == 0)
3925 cmd_qtdisconnected (packet
);
3928 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3930 cmd_qtframe (packet
);
3933 else if (strncmp ("QTBuffer:", packet
, strlen ("QTBuffer:")) == 0)
3935 cmd_bigqtbuffer (packet
);
3938 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
3940 cmd_qtnotes (packet
);
3948 handle_tracepoint_query (char *packet
)
3950 if (strcmp ("qTStatus", packet
) == 0)
3952 cmd_qtstatus (packet
);
3955 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
3960 else if (strcmp ("qTfP", packet
) == 0)
3965 else if (strcmp ("qTsP", packet
) == 0)
3970 else if (strcmp ("qTfV", packet
) == 0)
3975 else if (strcmp ("qTsV", packet
) == 0)
3980 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
3985 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
3987 cmd_qtbuffer (packet
);
3990 else if (strcmp ("qTfSTM", packet
) == 0)
3992 cmd_qtfstm (packet
);
3995 else if (strcmp ("qTsSTM", packet
) == 0)
3997 cmd_qtsstm (packet
);
4000 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4002 cmd_qtstmat (packet
);
4005 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4007 cmd_qtminftpilen (packet
);
4015 #ifndef IN_PROCESS_AGENT
4017 /* Call this when thread TINFO has hit the tracepoint defined by
4018 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4019 action. This adds a while-stepping collecting state item to the
4020 threads' collecting state list, so that we can keep track of
4021 multiple simultaneous while-stepping actions being collected by the
4022 same thread. This can happen in cases like:
4024 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4026 ff0003 INSN3 <-- TP2, collect $regs
4027 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4030 Notice that when instruction INSN5 is reached, the while-stepping
4031 actions of both TP1 and TP3 are still being collected, and that TP2
4032 had been collected meanwhile. The whole range of ff0001-ff0005
4033 should be single-stepped, due to at least TP1's while-stepping
4034 action covering the whole range. */
4037 add_while_stepping_state (struct thread_info
*tinfo
,
4038 int tp_number
, CORE_ADDR tp_address
)
4040 struct wstep_state
*wstep
;
4042 wstep
= xmalloc (sizeof (*wstep
));
4043 wstep
->next
= tinfo
->while_stepping
;
4045 wstep
->tp_number
= tp_number
;
4046 wstep
->tp_address
= tp_address
;
4047 wstep
->current_step
= 0;
4049 tinfo
->while_stepping
= wstep
;
4052 /* Release the while-stepping collecting state WSTEP. */
4055 release_while_stepping_state (struct wstep_state
*wstep
)
4060 /* Release all while-stepping collecting states currently associated
4061 with thread TINFO. */
4064 release_while_stepping_state_list (struct thread_info
*tinfo
)
4066 struct wstep_state
*head
;
4068 while (tinfo
->while_stepping
)
4070 head
= tinfo
->while_stepping
;
4071 tinfo
->while_stepping
= head
->next
;
4072 release_while_stepping_state (head
);
4076 /* If TINFO was handling a 'while-stepping' action, the step has
4077 finished, so collect any step data needed, and check if any more
4078 steps are required. Return true if the thread was indeed
4079 collecting tracepoint data, false otherwise. */
4082 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4084 struct tracepoint
*tpoint
;
4085 struct wstep_state
*wstep
;
4086 struct wstep_state
**wstep_link
;
4087 struct trap_tracepoint_ctx ctx
;
4089 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4091 if (in_process_agent_loaded ())
4092 upload_fast_traceframes ();
4094 /* Check if we were indeed collecting data for one of more
4095 tracepoints with a 'while-stepping' count. */
4096 if (tinfo
->while_stepping
== NULL
)
4101 /* We're not even tracing anymore. Stop this thread from
4103 release_while_stepping_state_list (tinfo
);
4105 /* The thread had stopped due to a single-step request indeed
4106 explained by a tracepoint. */
4110 wstep
= tinfo
->while_stepping
;
4111 wstep_link
= &tinfo
->while_stepping
;
4113 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4114 target_pid_to_str (tinfo
->entry
.id
),
4115 wstep
->tp_number
, paddress (wstep
->tp_address
));
4117 ctx
.base
.type
= trap_tracepoint
;
4118 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4120 while (wstep
!= NULL
)
4122 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4125 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4126 wstep
->tp_number
, paddress (wstep
->tp_address
),
4127 target_pid_to_str (tinfo
->entry
.id
));
4130 *wstep_link
= wstep
->next
;
4131 release_while_stepping_state (wstep
);
4132 wstep
= *wstep_link
;
4136 /* We've just finished one step. */
4137 ++wstep
->current_step
;
4140 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4141 stop_pc
, tpoint
, wstep
->current_step
);
4143 if (wstep
->current_step
>= tpoint
->step_count
)
4145 /* The requested numbers of steps have occurred. */
4146 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4147 target_pid_to_str (tinfo
->entry
.id
),
4148 wstep
->tp_number
, paddress (wstep
->tp_address
));
4150 /* Unlink the wstep. */
4151 *wstep_link
= wstep
->next
;
4152 release_while_stepping_state (wstep
);
4153 wstep
= *wstep_link
;
4155 /* Only check the hit count now, which ensure that we do all
4156 our stepping before stopping the run. */
4157 if (tpoint
->pass_count
> 0
4158 && tpoint
->hit_count
>= tpoint
->pass_count
4159 && stopping_tracepoint
== NULL
)
4160 stopping_tracepoint
= tpoint
;
4164 /* Keep single-stepping until the requested numbers of steps
4166 wstep_link
= &wstep
->next
;
4167 wstep
= *wstep_link
;
4170 if (stopping_tracepoint
4171 || trace_buffer_is_full
4172 || expr_eval_result
!= expr_eval_no_error
)
4182 /* Handle any internal tracing control breakpoint hits. That means,
4183 pull traceframes from the IPA to our buffer, and syncing both
4184 tracing agents when the IPA's tracing stops for some reason. */
4187 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4189 /* Pull in fast tracepoint trace frames from the inferior in-process
4190 agent's buffer into our buffer. */
4192 if (!in_process_agent_loaded ())
4195 upload_fast_traceframes ();
4197 /* Check if the in-process agent had decided we should stop
4199 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4201 int ipa_trace_buffer_is_full
;
4202 CORE_ADDR ipa_stopping_tracepoint
;
4203 int ipa_expr_eval_result
;
4204 CORE_ADDR ipa_error_tracepoint
;
4206 trace_debug ("lib stopped at stop_tracing");
4208 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4209 &ipa_trace_buffer_is_full
);
4211 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4212 &ipa_stopping_tracepoint
);
4213 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4215 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4216 &ipa_error_tracepoint
);
4217 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4219 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4220 &ipa_expr_eval_result
);
4221 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4223 trace_debug ("lib: trace_buffer_is_full: %d, "
4224 "stopping_tracepoint: %s, "
4225 "ipa_expr_eval_result: %d, "
4226 "error_tracepoint: %s, ",
4227 ipa_trace_buffer_is_full
,
4228 paddress (ipa_stopping_tracepoint
),
4229 ipa_expr_eval_result
,
4230 paddress (ipa_error_tracepoint
));
4234 if (ipa_trace_buffer_is_full
)
4235 trace_debug ("lib stopped due to full buffer.");
4236 if (ipa_stopping_tracepoint
)
4237 trace_debug ("lib stopped due to tpoint");
4238 if (ipa_stopping_tracepoint
)
4239 trace_debug ("lib stopped due to error");
4242 if (ipa_stopping_tracepoint
!= 0)
4245 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4247 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4249 expr_eval_result
= ipa_expr_eval_result
;
4251 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4256 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4258 trace_debug ("lib stopped at flush_trace_buffer");
4265 /* Return true if TINFO just hit a tracepoint. Collect data if
4269 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4271 struct tracepoint
*tpoint
;
4273 struct trap_tracepoint_ctx ctx
;
4275 /* Not tracing, don't handle. */
4279 ctx
.base
.type
= trap_tracepoint
;
4280 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4282 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4284 /* Note that we collect fast tracepoints here as well. We'll
4285 step over the fast tracepoint jump later, which avoids the
4286 double collect. However, we don't collect for static
4287 tracepoints here, because UST markers are compiled in program,
4288 and probes will be executed in program. So static tracepoints
4289 are collected there. */
4290 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4291 && tpoint
->type
!= static_tracepoint
)
4293 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4294 target_pid_to_str (tinfo
->entry
.id
),
4295 tpoint
->number
, paddress (tpoint
->address
));
4297 /* Test the condition if present, and collect if true. */
4299 || (condition_true_at_tracepoint
4300 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4301 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4304 if (stopping_tracepoint
4305 || trace_buffer_is_full
4306 || expr_eval_result
!= expr_eval_no_error
)
4310 /* If the tracepoint had a 'while-stepping' action, then set
4311 the thread to collect this tracepoint on the following
4313 else if (tpoint
->step_count
> 0)
4315 add_while_stepping_state (tinfo
,
4316 tpoint
->number
, tpoint
->address
);
4328 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4329 struct ust_marker_data
;
4330 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4332 struct tracepoint
*tpoint
,
4333 struct traceframe
*tframe
);
4336 /* Create a trace frame for the hit of the given tracepoint in the
4340 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4341 struct tracepoint
*tpoint
)
4343 struct traceframe
*tframe
;
4346 /* Only count it as a hit when we actually collect data. */
4347 tpoint
->hit_count
++;
4349 /* If we've exceeded a defined pass count, record the event for
4350 later, and finish the collection for this hit. This test is only
4351 for nonstepping tracepoints, stepping tracepoints test at the end
4352 of their while-stepping loop. */
4353 if (tpoint
->pass_count
> 0
4354 && tpoint
->hit_count
>= tpoint
->pass_count
4355 && tpoint
->step_count
== 0
4356 && stopping_tracepoint
== NULL
)
4357 stopping_tracepoint
= tpoint
;
4359 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
4360 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4362 tframe
= add_traceframe (tpoint
);
4366 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4368 #ifndef IN_PROCESS_AGENT
4369 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4370 tpoint
->number
, paddress (tpoint
->address
),
4371 tpoint
->actions_str
[acti
]);
4374 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4375 tpoint
->actions
[acti
]);
4378 finish_traceframe (tframe
);
4381 if (tframe
== NULL
&& tracing
)
4382 trace_buffer_is_full
= 1;
4385 #ifndef IN_PROCESS_AGENT
4388 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4390 struct tracepoint
*tpoint
, int current_step
)
4392 struct traceframe
*tframe
;
4395 trace_debug ("Making new step traceframe for "
4396 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4397 tpoint
->number
, paddress (tpoint
->address
),
4398 current_step
, tpoint
->step_count
,
4401 tframe
= add_traceframe (tpoint
);
4405 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4407 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4408 tpoint
->number
, paddress (tpoint
->address
),
4409 tpoint
->step_actions_str
[acti
]);
4411 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4412 tpoint
->step_actions
[acti
]);
4415 finish_traceframe (tframe
);
4418 if (tframe
== NULL
&& tracing
)
4419 trace_buffer_is_full
= 1;
4424 static struct regcache
*
4425 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4427 struct regcache
*regcache
= NULL
;
4429 #ifdef IN_PROCESS_AGENT
4430 if (ctx
->type
== fast_tracepoint
)
4432 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4433 if (!fctx
->regcache_initted
)
4435 fctx
->regcache_initted
= 1;
4436 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4437 supply_regblock (&fctx
->regcache
, NULL
);
4438 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4440 regcache
= &fctx
->regcache
;
4443 if (ctx
->type
== static_tracepoint
)
4445 struct static_tracepoint_ctx
*sctx
4446 = (struct static_tracepoint_ctx
*) ctx
;
4448 if (!sctx
->regcache_initted
)
4450 sctx
->regcache_initted
= 1;
4451 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4452 supply_regblock (&sctx
->regcache
, NULL
);
4453 /* Pass down the tracepoint address, because REGS doesn't
4454 include the PC, but we know what it must have been. */
4455 supply_static_tracepoint_registers (&sctx
->regcache
,
4456 (const unsigned char *)
4458 sctx
->tpoint
->address
);
4460 regcache
= &sctx
->regcache
;
4464 if (ctx
->type
== trap_tracepoint
)
4466 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4467 regcache
= tctx
->regcache
;
4471 gdb_assert (regcache
!= NULL
);
4477 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4479 struct tracepoint
*tpoint
,
4480 struct traceframe
*tframe
,
4481 struct tracepoint_action
*taction
)
4483 enum eval_result_type err
;
4485 switch (taction
->type
)
4489 struct collect_memory_action
*maction
;
4491 maction
= (struct collect_memory_action
*) taction
;
4493 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4494 pulongest (maction
->len
),
4495 paddress (maction
->addr
), maction
->basereg
);
4496 /* (should use basereg) */
4497 agent_mem_read (tframe
, NULL
,
4498 (CORE_ADDR
) maction
->addr
, maction
->len
);
4503 unsigned char *regspace
;
4504 struct regcache tregcache
;
4505 struct regcache
*context_regcache
;
4508 trace_debug ("Want to collect registers");
4510 /* Collect all registers for now. */
4511 regspace
= add_traceframe_block (tframe
,
4512 1 + register_cache_size ());
4513 if (regspace
== NULL
)
4515 trace_debug ("Trace buffer block allocation failed, skipping");
4518 /* Identify a register block. */
4521 context_regcache
= get_context_regcache (ctx
);
4523 /* Wrap the regblock in a register cache (in the stack, we
4524 don't want to malloc here). */
4525 init_register_cache (&tregcache
, regspace
+ 1);
4527 /* Copy the register data to the regblock. */
4528 regcache_cpy (&tregcache
, context_regcache
);
4530 #ifndef IN_PROCESS_AGENT
4531 /* On some platforms, trap-based tracepoints will have the PC
4532 pointing to the next instruction after the trap, but we
4533 don't want the user or GDB trying to guess whether the
4534 saved PC needs adjusting; so always record the adjusted
4535 stop_pc. Note that we can't use tpoint->address instead,
4536 since it will be wrong for while-stepping actions. This
4537 adjustment is a nop for fast tracepoints collected from the
4538 in-process lib (but not if GDBserver is collecting one
4539 preemptively), since the PC had already been adjusted to
4540 contain the tracepoint's address by the jump pad. */
4541 trace_debug ("Storing stop pc (0x%s) in regblock",
4542 paddress (tpoint
->address
));
4544 /* This changes the regblock, not the thread's
4546 regcache_write_pc (&tregcache
, stop_pc
);
4552 struct eval_expr_action
*eaction
;
4554 eaction
= (struct eval_expr_action
*) taction
;
4556 trace_debug ("Want to evaluate expression");
4558 err
= eval_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4560 if (err
!= expr_eval_no_error
)
4562 record_tracepoint_error (tpoint
, "action expression", err
);
4569 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4570 trace_debug ("Want to collect static trace data");
4571 collect_ust_data_at_tracepoint (ctx
, stop_pc
,
4574 trace_debug ("warning: collecting static trace data, "
4575 "but static tracepoints are not supported");
4580 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4586 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4587 struct tracepoint
*tpoint
)
4590 enum eval_result_type err
;
4592 /* Presently, gdbserver doesn't run compiled conditions, only the
4593 IPA does. If the program stops at a fast tracepoint's address
4594 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4595 gdbserver preemptively collect the fast tracepoint. Later, on
4596 resume, gdbserver steps over the fast tracepoint like it steps
4597 over breakpoints, so that the IPA doesn't see that fast
4598 tracepoint. This avoids double collects of fast tracepoints in
4599 that stopping scenario. Having gdbserver itself handle the fast
4600 tracepoint gives the user a consistent view of when fast or trap
4601 tracepoints are collected, compared to an alternative where only
4602 trap tracepoints are collected on stop, and fast tracepoints on
4603 resume. When a fast tracepoint is being processed by gdbserver,
4604 it is always the non-compiled condition expression that is
4606 #ifdef IN_PROCESS_AGENT
4607 if (tpoint
->compiled_cond
)
4608 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4611 err
= eval_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4613 if (err
!= expr_eval_no_error
)
4615 record_tracepoint_error (tpoint
, "condition", err
);
4616 /* The error case must return false. */
4620 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4621 tpoint
->number
, paddress (tpoint
->address
),
4623 return (value
? 1 : 0);
4626 #ifndef IN_PROCESS_AGENT
4628 /* The packet form of an agent expression consists of an 'X', number
4629 of bytes in expression, a comma, and then the bytes. */
4631 static struct agent_expr
*
4632 parse_agent_expr (char **actparm
)
4634 char *act
= *actparm
;
4636 struct agent_expr
*aexpr
;
4638 ++act
; /* skip the X */
4639 act
= unpack_varlen_hex (act
, &xlen
);
4640 ++act
; /* skip a comma */
4641 aexpr
= xmalloc (sizeof (struct agent_expr
));
4642 aexpr
->length
= xlen
;
4643 aexpr
->bytes
= xmalloc (xlen
);
4644 convert_ascii_to_int (act
, aexpr
->bytes
, xlen
);
4645 *actparm
= act
+ (xlen
* 2);
4649 /* Convert the bytes of an agent expression back into hex digits, so
4650 they can be printed or uploaded. This allocates the buffer,
4651 callers should free when they are done with it. */
4654 unparse_agent_expr (struct agent_expr
*aexpr
)
4658 rslt
= xmalloc (2 * aexpr
->length
+ 1);
4659 convert_int_to_ascii (aexpr
->bytes
, rslt
, aexpr
->length
);
4665 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
4668 gdb_agent_op_name (int op
)
4670 if (op
< 0 || op
>= gdb_agent_op_last
|| gdb_agent_op_names
[op
] == NULL
)
4672 return gdb_agent_op_names
[op
];
4675 /* The agent expression evaluator, as specified by the GDB docs. It
4676 returns 0 if everything went OK, and a nonzero error code
4679 static enum eval_result_type
4680 eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4681 struct traceframe
*tframe
,
4682 struct agent_expr
*aexpr
,
4686 #define STACK_MAX 100
4687 ULONGEST stack
[STACK_MAX
], top
;
4692 /* This union is a convenient way to convert representations. For
4693 now, assume a standard architecture where the hardware integer
4694 types have 8, 16, 32, 64 bit types. A more robust solution would
4695 be to import stdint.h from gnulib. */
4700 unsigned char bytes
[1];
4705 unsigned char bytes
[2];
4710 unsigned char bytes
[4];
4715 unsigned char bytes
[8];
4720 if (aexpr
->length
== 0)
4722 trace_debug ("empty agent expression");
4723 return expr_eval_empty_expression
;
4726 /* Cache the stack top in its own variable. Much of the time we can
4727 operate on this variable, rather than dinking with the stack. It
4728 needs to be copied to the stack when sp changes. */
4733 op
= aexpr
->bytes
[pc
++];
4735 trace_debug ("About to interpret byte 0x%x", op
);
4739 case gdb_agent_op_add
:
4743 case gdb_agent_op_sub
:
4744 top
= stack
[--sp
] - top
;
4747 case gdb_agent_op_mul
:
4751 case gdb_agent_op_div_signed
:
4754 trace_debug ("Attempted to divide by zero");
4755 return expr_eval_divide_by_zero
;
4757 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
4760 case gdb_agent_op_div_unsigned
:
4763 trace_debug ("Attempted to divide by zero");
4764 return expr_eval_divide_by_zero
;
4766 top
= stack
[--sp
] / top
;
4769 case gdb_agent_op_rem_signed
:
4772 trace_debug ("Attempted to divide by zero");
4773 return expr_eval_divide_by_zero
;
4775 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
4778 case gdb_agent_op_rem_unsigned
:
4781 trace_debug ("Attempted to divide by zero");
4782 return expr_eval_divide_by_zero
;
4784 top
= stack
[--sp
] % top
;
4787 case gdb_agent_op_lsh
:
4788 top
= stack
[--sp
] << top
;
4791 case gdb_agent_op_rsh_signed
:
4792 top
= ((LONGEST
) stack
[--sp
]) >> top
;
4795 case gdb_agent_op_rsh_unsigned
:
4796 top
= stack
[--sp
] >> top
;
4799 case gdb_agent_op_trace
:
4800 agent_mem_read (tframe
,
4801 NULL
, (CORE_ADDR
) stack
[--sp
], (ULONGEST
) top
);
4806 case gdb_agent_op_trace_quick
:
4807 arg
= aexpr
->bytes
[pc
++];
4808 agent_mem_read (tframe
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
4811 case gdb_agent_op_log_not
:
4815 case gdb_agent_op_bit_and
:
4819 case gdb_agent_op_bit_or
:
4823 case gdb_agent_op_bit_xor
:
4827 case gdb_agent_op_bit_not
:
4831 case gdb_agent_op_equal
:
4832 top
= (stack
[--sp
] == top
);
4835 case gdb_agent_op_less_signed
:
4836 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
4839 case gdb_agent_op_less_unsigned
:
4840 top
= (stack
[--sp
] < top
);
4843 case gdb_agent_op_ext
:
4844 arg
= aexpr
->bytes
[pc
++];
4845 if (arg
< (sizeof (LONGEST
) * 8))
4847 LONGEST mask
= 1 << (arg
- 1);
4848 top
&= ((LONGEST
) 1 << arg
) - 1;
4849 top
= (top
^ mask
) - mask
;
4853 case gdb_agent_op_ref8
:
4854 agent_mem_read (tframe
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
4858 case gdb_agent_op_ref16
:
4859 agent_mem_read (tframe
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
4863 case gdb_agent_op_ref32
:
4864 agent_mem_read (tframe
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
4868 case gdb_agent_op_ref64
:
4869 agent_mem_read (tframe
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
4873 case gdb_agent_op_if_goto
:
4875 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4882 case gdb_agent_op_goto
:
4883 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4886 case gdb_agent_op_const8
:
4887 /* Flush the cached stack top. */
4889 top
= aexpr
->bytes
[pc
++];
4892 case gdb_agent_op_const16
:
4893 /* Flush the cached stack top. */
4895 top
= aexpr
->bytes
[pc
++];
4896 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4899 case gdb_agent_op_const32
:
4900 /* Flush the cached stack top. */
4902 top
= aexpr
->bytes
[pc
++];
4903 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4904 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4905 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4908 case gdb_agent_op_const64
:
4909 /* Flush the cached stack top. */
4911 top
= aexpr
->bytes
[pc
++];
4912 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4913 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4914 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4915 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4916 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4917 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4918 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4921 case gdb_agent_op_reg
:
4922 /* Flush the cached stack top. */
4924 arg
= aexpr
->bytes
[pc
++];
4925 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4928 struct regcache
*regcache
;
4930 regcache
= get_context_regcache (ctx
);
4932 switch (register_size (regnum
))
4935 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
4939 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
4943 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
4947 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
4951 internal_error (__FILE__
, __LINE__
,
4952 "unhandled register size");
4957 case gdb_agent_op_end
:
4958 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4959 sp
, pulongest (top
));
4964 /* This should be an error */
4965 trace_debug ("Stack is empty, nothing to return");
4966 return expr_eval_empty_stack
;
4970 return expr_eval_no_error
;
4972 case gdb_agent_op_dup
:
4976 case gdb_agent_op_pop
:
4981 case gdb_agent_op_pick
:
4982 arg
= aexpr
->bytes
[pc
++];
4984 top
= stack
[sp
- arg
];
4988 case gdb_agent_op_rot
:
4990 ULONGEST tem
= stack
[sp
- 1];
4992 stack
[sp
- 1] = stack
[sp
- 2];
4993 stack
[sp
- 2] = top
;
4998 case gdb_agent_op_zero_ext
:
4999 arg
= aexpr
->bytes
[pc
++];
5000 if (arg
< (sizeof (LONGEST
) * 8))
5001 top
&= ((LONGEST
) 1 << arg
) - 1;
5004 case gdb_agent_op_swap
:
5005 /* Interchange top two stack elements, making sure top gets
5006 copied back onto stack. */
5008 top
= stack
[sp
- 1];
5009 stack
[sp
- 1] = stack
[sp
];
5012 case gdb_agent_op_getv
:
5013 /* Flush the cached stack top. */
5015 arg
= aexpr
->bytes
[pc
++];
5016 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5017 top
= get_trace_state_variable_value (arg
);
5020 case gdb_agent_op_setv
:
5021 arg
= aexpr
->bytes
[pc
++];
5022 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5023 set_trace_state_variable_value (arg
, top
);
5024 /* Note that we leave the value on the stack, for the
5025 benefit of later/enclosing expressions. */
5028 case gdb_agent_op_tracev
:
5029 arg
= aexpr
->bytes
[pc
++];
5030 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5031 agent_tsv_read (tframe
, arg
);
5034 case gdb_agent_op_tracenz
:
5035 agent_mem_read_string (tframe
, NULL
, (CORE_ADDR
) stack
[--sp
],
5041 /* GDB never (currently) generates any of these ops. */
5042 case gdb_agent_op_float
:
5043 case gdb_agent_op_ref_float
:
5044 case gdb_agent_op_ref_double
:
5045 case gdb_agent_op_ref_long_double
:
5046 case gdb_agent_op_l_to_d
:
5047 case gdb_agent_op_d_to_l
:
5048 case gdb_agent_op_trace16
:
5049 trace_debug ("Agent expression op 0x%x valid, but not handled",
5051 /* If ever GDB generates any of these, we don't have the
5052 option of ignoring. */
5056 trace_debug ("Agent expression op 0x%x not recognized", op
);
5057 /* Don't struggle on, things will just get worse. */
5058 return expr_eval_unrecognized_opcode
;
5061 /* Check for stack badness. */
5062 if (sp
>= (STACK_MAX
- 1))
5064 trace_debug ("Expression stack overflow");
5065 return expr_eval_stack_overflow
;
5070 trace_debug ("Expression stack underflow");
5071 return expr_eval_stack_underflow
;
5074 trace_debug ("Op %s -> sp=%d, top=0x%s",
5075 gdb_agent_op_name (op
), sp
, pulongest (top
));
5079 /* Do memory copies for bytecodes. */
5080 /* Do the recording of memory blocks for actions and bytecodes. */
5083 agent_mem_read (struct traceframe
*tframe
,
5084 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
5086 unsigned char *mspace
;
5087 ULONGEST remaining
= len
;
5088 unsigned short blocklen
;
5090 /* If a 'to' buffer is specified, use it. */
5093 read_inferior_memory (from
, to
, len
);
5097 /* Otherwise, create a new memory block in the trace buffer. */
5098 while (remaining
> 0)
5102 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
5103 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
5104 mspace
= add_traceframe_block (tframe
, sp
);
5107 /* Identify block as a memory block. */
5110 /* Record address and size. */
5111 memcpy (mspace
, &from
, sizeof (from
));
5112 mspace
+= sizeof (from
);
5113 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
5114 mspace
+= sizeof (blocklen
);
5115 /* Record the memory block proper. */
5116 read_inferior_memory (from
, mspace
, blocklen
);
5117 trace_debug ("%d bytes recorded", blocklen
);
5118 remaining
-= blocklen
;
5125 agent_mem_read_string (struct traceframe
*tframe
,
5126 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
5128 unsigned char *buf
, *mspace
;
5129 ULONGEST remaining
= len
;
5130 unsigned short blocklen
, i
;
5132 /* To save a bit of space, block lengths are 16-bit, so break large
5133 requests into multiple blocks. Bordering on overkill for strings,
5134 but it could happen that someone specifies a large max length. */
5135 while (remaining
> 0)
5139 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
5140 /* We want working space to accumulate nonzero bytes, since
5141 traceframes must have a predecided size (otherwise it gets
5142 harder to wrap correctly for the circular case, etc). */
5143 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
5144 for (i
= 0; i
< blocklen
; ++i
)
5146 /* Read the string one byte at a time, in case the string is
5147 at the end of a valid memory area - we don't want a
5148 correctly-terminated string to engender segvio
5150 read_inferior_memory (from
+ i
, buf
+ i
, 1);
5155 /* Make sure outer loop stops now too. */
5156 remaining
= blocklen
;
5160 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
5161 mspace
= add_traceframe_block (tframe
, sp
);
5167 /* Identify block as a memory block. */
5170 /* Record address and size. */
5171 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
5172 mspace
+= sizeof (from
);
5173 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
5174 mspace
+= sizeof (blocklen
);
5175 /* Copy the string contents. */
5176 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
5177 remaining
-= blocklen
;
5184 /* Record the value of a trace state variable. */
5187 agent_tsv_read (struct traceframe
*tframe
, int n
)
5189 unsigned char *vspace
;
5192 vspace
= add_traceframe_block (tframe
,
5193 1 + sizeof (n
) + sizeof (LONGEST
));
5196 /* Identify block as a variable. */
5198 /* Record variable's number and value. */
5199 memcpy (vspace
+ 1, &n
, sizeof (n
));
5200 val
= get_trace_state_variable_value (n
);
5201 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
5202 trace_debug ("Variable %d recorded", n
);
5206 #ifndef IN_PROCESS_AGENT
5208 /* Callback for traceframe_walk_blocks, used to find a given block
5209 type in a traceframe. */
5212 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
5214 char *wantedp
= data
;
5216 if (*wantedp
== blocktype
)
5222 /* Walk over all traceframe blocks of the traceframe buffer starting
5223 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5224 block found, passing in DATA unmodified. If CALLBACK returns true,
5225 this returns a pointer to where the block is found. Returns NULL
5226 if no callback call returned true, indicating that all blocks have
5229 static unsigned char *
5230 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
5232 int (*callback
) (char blocktype
,
5233 unsigned char *dataptr
,
5237 unsigned char *dataptr
;
5241 trace_debug ("traceframe %d has no data", tfnum
);
5245 /* Iterate through a traceframe's blocks, looking for a block of the
5247 for (dataptr
= database
;
5248 dataptr
< database
+ datasize
;
5252 unsigned short mlen
;
5254 if (dataptr
== trace_buffer_wrap
)
5256 /* Adjust to reflect wrapping part of the frame around to
5258 datasize
= dataptr
- database
;
5259 dataptr
= database
= trace_buffer_lo
;
5262 blocktype
= *dataptr
++;
5264 if ((*callback
) (blocktype
, dataptr
, data
))
5270 /* Skip over the registers block. */
5271 dataptr
+= register_cache_size ();
5274 /* Skip over the memory block. */
5275 dataptr
+= sizeof (CORE_ADDR
);
5276 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5277 dataptr
+= (sizeof (mlen
) + mlen
);
5280 /* Skip over the TSV block. */
5281 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5284 /* Skip over the static trace data block. */
5285 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5286 dataptr
+= (sizeof (mlen
) + mlen
);
5289 trace_debug ("traceframe %d has unknown block type 0x%x",
5298 /* Look for the block of type TYPE_WANTED in the trameframe starting
5299 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5302 static unsigned char *
5303 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5304 int tfnum
, char type_wanted
)
5306 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5307 match_blocktype
, &type_wanted
);
5310 static unsigned char *
5311 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5313 unsigned char *regblock
;
5315 regblock
= traceframe_find_block_type (tframe
->data
,
5319 if (regblock
== NULL
)
5320 trace_debug ("traceframe %d has no register data", tfnum
);
5325 /* Get registers from a traceframe. */
5328 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5330 unsigned char *dataptr
;
5331 struct tracepoint
*tpoint
;
5332 struct traceframe
*tframe
;
5334 tframe
= find_traceframe (tfnum
);
5338 trace_debug ("traceframe %d not found", tfnum
);
5342 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5343 if (dataptr
== NULL
)
5345 /* Mark registers unavailable. */
5346 supply_regblock (regcache
, NULL
);
5348 /* We can generally guess at a PC, although this will be
5349 misleading for while-stepping frames and multi-location
5351 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5353 regcache_write_pc (regcache
, tpoint
->address
);
5356 supply_regblock (regcache
, dataptr
);
5362 traceframe_get_pc (struct traceframe
*tframe
)
5364 struct regcache regcache
;
5365 unsigned char *dataptr
;
5367 dataptr
= traceframe_find_regblock (tframe
, -1);
5368 if (dataptr
== NULL
)
5371 init_register_cache (®cache
, dataptr
);
5372 return regcache_read_pc (®cache
);
5375 /* Read a requested block of memory from a trace frame. */
5378 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5379 unsigned char *buf
, ULONGEST length
,
5382 struct traceframe
*tframe
;
5383 unsigned char *database
, *dataptr
;
5384 unsigned int datasize
;
5386 unsigned short mlen
;
5388 trace_debug ("traceframe_read_mem");
5390 tframe
= find_traceframe (tfnum
);
5394 trace_debug ("traceframe %d not found", tfnum
);
5398 datasize
= tframe
->data_size
;
5399 database
= dataptr
= &tframe
->data
[0];
5401 /* Iterate through a traceframe's blocks, looking for memory. */
5402 while ((dataptr
= traceframe_find_block_type (dataptr
,
5404 - (dataptr
- database
),
5405 tfnum
, 'M')) != NULL
)
5407 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5408 dataptr
+= sizeof (maddr
);
5409 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5410 dataptr
+= sizeof (mlen
);
5411 trace_debug ("traceframe %d has %d bytes at %s",
5412 tfnum
, mlen
, paddress (maddr
));
5414 /* If the block includes the first part of the desired range,
5415 return as much it has; GDB will re-request the remainder,
5416 which might be in a different block of this trace frame. */
5417 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5419 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5423 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5428 /* Skip over this block. */
5432 trace_debug ("traceframe %d has no memory data for the desired region",
5440 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5443 struct traceframe
*tframe
;
5444 unsigned char *database
, *dataptr
;
5445 unsigned int datasize
;
5448 trace_debug ("traceframe_read_tsv");
5450 tfnum
= current_traceframe
;
5454 trace_debug ("no current traceframe");
5458 tframe
= find_traceframe (tfnum
);
5462 trace_debug ("traceframe %d not found", tfnum
);
5466 datasize
= tframe
->data_size
;
5467 database
= dataptr
= &tframe
->data
[0];
5469 /* Iterate through a traceframe's blocks, looking for the tsv. */
5470 while ((dataptr
= traceframe_find_block_type (dataptr
,
5472 - (dataptr
- database
),
5473 tfnum
, 'V')) != NULL
)
5475 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5476 dataptr
+= sizeof (vnum
);
5478 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5480 /* Check that this is the variable we want. */
5483 memcpy (val
, dataptr
, sizeof (*val
));
5487 /* Skip over this block. */
5488 dataptr
+= sizeof (LONGEST
);
5491 trace_debug ("traceframe %d has no data for variable %d",
5496 /* Read a requested block of static tracepoint data from a trace
5500 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5501 unsigned char *buf
, ULONGEST length
,
5504 struct traceframe
*tframe
;
5505 unsigned char *database
, *dataptr
;
5506 unsigned int datasize
;
5507 unsigned short mlen
;
5509 trace_debug ("traceframe_read_sdata");
5511 tframe
= find_traceframe (tfnum
);
5515 trace_debug ("traceframe %d not found", tfnum
);
5519 datasize
= tframe
->data_size
;
5520 database
= &tframe
->data
[0];
5522 /* Iterate through a traceframe's blocks, looking for static
5524 dataptr
= traceframe_find_block_type (database
, datasize
,
5526 if (dataptr
!= NULL
)
5528 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5529 dataptr
+= sizeof (mlen
);
5532 if (offset
+ length
> mlen
)
5533 length
= mlen
- offset
;
5535 memcpy (buf
, dataptr
, length
);
5543 trace_debug ("traceframe %d has no static trace data", tfnum
);
5549 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5550 object. DATA is pointer to a struct buffer holding the
5551 traceframe-info object being built. */
5554 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5556 struct buffer
*buffer
= data
;
5562 unsigned short mlen
;
5565 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5566 dataptr
+= sizeof (maddr
);
5567 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5568 dataptr
+= sizeof (mlen
);
5569 buffer_xml_printf (buffer
,
5570 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5571 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5581 warning ("Unhandled trace block type (%d) '%c ' "
5582 "while building trace frame info.",
5583 blocktype
, blocktype
);
5590 /* Build a traceframe-info object for traceframe number TFNUM into
5594 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5596 struct traceframe
*tframe
;
5598 trace_debug ("traceframe_read_info");
5600 tframe
= find_traceframe (tfnum
);
5604 trace_debug ("traceframe %d not found", tfnum
);
5608 buffer_grow_str (buffer
, "<traceframe-info>\n");
5609 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5610 tfnum
, build_traceframe_info_xml
, buffer
);
5611 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5615 /* Return the first fast tracepoint whose jump pad contains PC. */
5617 static struct tracepoint
*
5618 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5620 struct tracepoint
*tpoint
;
5622 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5623 if (tpoint
->type
== fast_tracepoint
)
5624 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5630 /* Return the first fast tracepoint whose trampoline contains PC. */
5632 static struct tracepoint
*
5633 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5635 struct tracepoint
*tpoint
;
5637 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5639 if (tpoint
->type
== fast_tracepoint
5640 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5647 /* Return GDBserver's tracepoint that matches the IP Agent's
5648 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5651 static struct tracepoint
*
5652 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5654 struct tracepoint
*tpoint
;
5656 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5657 if (tpoint
->type
== fast_tracepoint
)
5658 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5666 /* The type of the object that is used to synchronize fast tracepoint
5669 typedef struct collecting_t
5671 /* The fast tracepoint number currently collecting. */
5674 /* A number that GDBserver can use to identify the thread that is
5675 presently holding the collect lock. This need not (and usually
5676 is not) the thread id, as getting the current thread ID usually
5677 requires a system call, which we want to avoid like the plague.
5678 Usually this is thread's TCB, found in the TLS (pseudo-)
5679 register, which is readable with a single insn on several
5681 uintptr_t thread_area
;
5684 #ifndef IN_PROCESS_AGENT
5687 force_unlock_trace_buffer (void)
5689 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5692 /* Check if the thread identified by THREAD_AREA which is stopped at
5693 STOP_PC, is presently locking the fast tracepoint collection, and
5694 if so, gather some status of said collection. Returns 0 if the
5695 thread isn't collecting or in the jump pad at all. 1, if in the
5696 jump pad (or within gdb_collect) and hasn't executed the adjusted
5697 original insn yet (can set a breakpoint there and run to it). 2,
5698 if presently executing the adjusted original insn --- in which
5699 case, if we want to move the thread out of the jump pad, we need to
5700 single-step it until this function returns 0. */
5703 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5705 struct fast_tpoint_collect_status
*status
)
5707 CORE_ADDR ipa_collecting
;
5708 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5709 CORE_ADDR ipa_gdb_trampoline_buffer
;
5710 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5711 struct tracepoint
*tpoint
;
5712 int needs_breakpoint
;
5714 /* The thread THREAD_AREA is either:
5716 0. not collecting at all, not within the jump pad, or within
5717 gdb_collect or one of its callees.
5719 1. in the jump pad and haven't reached gdb_collect
5721 2. within gdb_collect (out of the jump pad) (collect is set)
5723 3. we're in the jump pad, after gdb_collect having returned,
5724 possibly executing the adjusted insns.
5726 For cases 1 and 3, `collecting' may or not be set. The jump pad
5727 doesn't have any complicated jump logic, so we can tell if the
5728 thread is executing the adjust original insn or not by just
5729 matching STOP_PC with known jump pad addresses. If we it isn't
5730 yet executing the original insn, set a breakpoint there, and let
5731 the thread run to it, so to quickly step over a possible (many
5732 insns) gdb_collect call. Otherwise, or when the breakpoint is
5733 hit, only a few (small number of) insns are left to be executed
5734 in the jump pad. Single-step the thread until it leaves the
5739 needs_breakpoint
= 0;
5740 trace_debug ("fast_tracepoint_collecting");
5742 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5743 &ipa_gdb_jump_pad_buffer
))
5744 fatal ("error extracting `gdb_jump_pad_buffer'");
5745 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5746 &ipa_gdb_jump_pad_buffer_end
))
5747 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5749 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5750 &ipa_gdb_trampoline_buffer
))
5751 fatal ("error extracting `gdb_trampoline_buffer'");
5752 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5753 &ipa_gdb_trampoline_buffer_end
))
5754 fatal ("error extracting `gdb_trampoline_buffer_end'");
5756 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5757 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5759 /* We can tell which tracepoint(s) the thread is collecting by
5760 matching the jump pad address back to the tracepoint. */
5761 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5764 warning ("in jump pad, but no matching tpoint?");
5769 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5771 tpoint
->number
, paddress (tpoint
->address
),
5772 paddress (tpoint
->jump_pad
),
5773 paddress (tpoint
->jump_pad_end
),
5774 paddress (tpoint
->adjusted_insn_addr
),
5775 paddress (tpoint
->adjusted_insn_addr_end
));
5778 /* Definitely in the jump pad. May or may not need
5779 fast-exit-jump-pad breakpoint. */
5780 if (tpoint
->jump_pad
<= stop_pc
5781 && stop_pc
< tpoint
->adjusted_insn_addr
)
5782 needs_breakpoint
= 1;
5784 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5785 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5787 /* We can tell which tracepoint(s) the thread is collecting by
5788 matching the trampoline address back to the tracepoint. */
5789 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5792 warning ("in trampoline, but no matching tpoint?");
5797 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5798 tpoint
->number
, paddress (tpoint
->address
),
5799 paddress (tpoint
->trampoline
),
5800 paddress (tpoint
->trampoline_end
));
5803 /* Have not reached jump pad yet, but treat the trampoline as a
5804 part of the jump pad that is before the adjusted original
5806 needs_breakpoint
= 1;
5810 collecting_t ipa_collecting_obj
;
5812 /* If `collecting' is set/locked, then the THREAD_AREA thread
5813 may or not be the one holding the lock. We have to read the
5814 lock to find out. */
5816 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5819 trace_debug ("fast_tracepoint_collecting:"
5820 " failed reading 'collecting' in the inferior");
5824 if (!ipa_collecting
)
5826 trace_debug ("fast_tracepoint_collecting: not collecting"
5827 " (and nobody is).");
5831 /* Some thread is collecting. Check which. */
5832 if (read_inferior_memory (ipa_collecting
,
5833 (unsigned char *) &ipa_collecting_obj
,
5834 sizeof (ipa_collecting_obj
)) != 0)
5837 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5839 trace_debug ("fast_tracepoint_collecting: not collecting "
5840 "(another thread is)");
5845 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5848 warning ("fast_tracepoint_collecting: collecting, "
5849 "but tpoint %s not found?",
5850 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5854 /* The thread is within `gdb_collect', skip over the rest of
5855 fast tracepoint collection quickly using a breakpoint. */
5856 needs_breakpoint
= 1;
5859 /* The caller wants a bit of status detail. */
5862 status
->tpoint_num
= tpoint
->number
;
5863 status
->tpoint_addr
= tpoint
->address
;
5864 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5865 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5868 if (needs_breakpoint
)
5870 /* Hasn't executed the original instruction yet. Set breakpoint
5871 there, and wait till it's hit, then single-step until exiting
5875 fast_tracepoint_collecting, returning continue-until-break at %s",
5876 paddress (tpoint
->adjusted_insn_addr
));
5878 return 1; /* continue */
5882 /* Just single-step until exiting the jump pad. */
5884 trace_debug ("fast_tracepoint_collecting, returning "
5885 "need-single-step (%s-%s)",
5886 paddress (tpoint
->adjusted_insn_addr
),
5887 paddress (tpoint
->adjusted_insn_addr_end
));
5889 return 2; /* single-step */
5895 #ifdef IN_PROCESS_AGENT
5897 /* The global fast tracepoint collect lock. Points to a collecting_t
5898 object built on the stack by the jump pad, if presently locked;
5899 NULL if it isn't locked. Note that this lock *must* be set while
5900 executing any *function other than the jump pad. See
5901 fast_tracepoint_collecting. */
5902 static collecting_t
* ATTR_USED collecting
;
5904 /* This routine, called from the jump pad (in asm) is designed to be
5905 called from the jump pads of fast tracepoints, thus it is on the
5908 IP_AGENT_EXPORT
void ATTR_USED
5909 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5911 struct fast_tracepoint_ctx ctx
;
5913 /* Don't do anything until the trace run is completely set up. */
5917 ctx
.base
.type
= fast_tracepoint
;
5919 ctx
.regcache_initted
= 0;
5920 /* Wrap the regblock in a register cache (in the stack, we don't
5921 want to malloc here). */
5922 ctx
.regspace
= alloca (register_cache_size ());
5923 if (ctx
.regspace
== NULL
)
5925 trace_debug ("Trace buffer block allocation failed, skipping");
5929 for (ctx
.tpoint
= tpoint
;
5930 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5931 ctx
.tpoint
= ctx
.tpoint
->next
)
5933 if (!ctx
.tpoint
->enabled
)
5936 /* Multiple tracepoints of different types, such as fast tracepoint and
5937 static tracepoint, can be set at the same address. */
5938 if (ctx
.tpoint
->type
!= tpoint
->type
)
5941 /* Test the condition if present, and collect if true. */
5942 if (ctx
.tpoint
->cond
== NULL
5943 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5946 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5947 ctx
.tpoint
->address
, ctx
.tpoint
);
5949 /* Note that this will cause original insns to be written back
5950 to where we jumped from, but that's OK because we're jumping
5951 back to the next whole instruction. This will go badly if
5952 instruction restoration is not atomic though. */
5953 if (stopping_tracepoint
5954 || trace_buffer_is_full
5955 || expr_eval_result
!= expr_eval_no_error
)
5963 /* If there was a condition and it evaluated to false, the only
5964 way we would stop tracing is if there was an error during
5965 condition expression evaluation. */
5966 if (expr_eval_result
!= expr_eval_no_error
)
5977 #ifndef IN_PROCESS_AGENT
5979 /* Bytecode compilation. */
5981 CORE_ADDR current_insn_ptr
;
5985 struct bytecode_address
5990 /* Offset and size of field to be modified in the goto block. */
5991 int from_offset
, from_size
;
5992 struct bytecode_address
*next
;
5993 } *bytecode_address_table
;
5996 get_raw_reg_func_addr (void)
5998 return ipa_sym_addrs
.addr_get_raw_reg
;
6002 emit_prologue (void)
6004 target_emit_ops ()->emit_prologue ();
6008 emit_epilogue (void)
6010 target_emit_ops ()->emit_epilogue ();
6016 target_emit_ops ()->emit_add ();
6022 target_emit_ops ()->emit_sub ();
6028 target_emit_ops ()->emit_mul ();
6034 target_emit_ops ()->emit_lsh ();
6038 emit_rsh_signed (void)
6040 target_emit_ops ()->emit_rsh_signed ();
6044 emit_rsh_unsigned (void)
6046 target_emit_ops ()->emit_rsh_unsigned ();
6052 target_emit_ops ()->emit_ext (arg
);
6058 target_emit_ops ()->emit_log_not ();
6064 target_emit_ops ()->emit_bit_and ();
6070 target_emit_ops ()->emit_bit_or ();
6076 target_emit_ops ()->emit_bit_xor ();
6082 target_emit_ops ()->emit_bit_not ();
6088 target_emit_ops ()->emit_equal ();
6092 emit_less_signed (void)
6094 target_emit_ops ()->emit_less_signed ();
6098 emit_less_unsigned (void)
6100 target_emit_ops ()->emit_less_unsigned ();
6106 target_emit_ops ()->emit_ref (size
);
6110 emit_if_goto (int *offset_p
, int *size_p
)
6112 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
6116 emit_goto (int *offset_p
, int *size_p
)
6118 target_emit_ops ()->emit_goto (offset_p
, size_p
);
6122 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
6124 target_emit_ops ()->write_goto_address (from
, to
, size
);
6128 emit_const (LONGEST num
)
6130 target_emit_ops ()->emit_const (num
);
6136 target_emit_ops ()->emit_reg (reg
);
6142 target_emit_ops ()->emit_pop ();
6146 emit_stack_flush (void)
6148 target_emit_ops ()->emit_stack_flush ();
6152 emit_zero_ext (int arg
)
6154 target_emit_ops ()->emit_zero_ext (arg
);
6160 target_emit_ops ()->emit_swap ();
6164 emit_stack_adjust (int n
)
6166 target_emit_ops ()->emit_stack_adjust (n
);
6169 /* FN's prototype is `LONGEST(*fn)(int)'. */
6172 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
6174 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
6177 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
6180 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
6182 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
6186 emit_eq_goto (int *offset_p
, int *size_p
)
6188 target_emit_ops ()->emit_eq_goto (offset_p
, size_p
);
6192 emit_ne_goto (int *offset_p
, int *size_p
)
6194 target_emit_ops ()->emit_ne_goto (offset_p
, size_p
);
6198 emit_lt_goto (int *offset_p
, int *size_p
)
6200 target_emit_ops ()->emit_lt_goto (offset_p
, size_p
);
6204 emit_ge_goto (int *offset_p
, int *size_p
)
6206 target_emit_ops ()->emit_ge_goto (offset_p
, size_p
);
6210 emit_gt_goto (int *offset_p
, int *size_p
)
6212 target_emit_ops ()->emit_gt_goto (offset_p
, size_p
);
6216 emit_le_goto (int *offset_p
, int *size_p
)
6218 target_emit_ops ()->emit_le_goto (offset_p
, size_p
);
6221 static enum eval_result_type
compile_bytecodes (struct agent_expr
*aexpr
);
6224 compile_tracepoint_condition (struct tracepoint
*tpoint
,
6225 CORE_ADDR
*jump_entry
)
6227 CORE_ADDR entry_point
= *jump_entry
;
6228 enum eval_result_type err
;
6230 trace_debug ("Starting condition compilation for tracepoint %d\n",
6233 /* Initialize the global pointer to the code being built. */
6234 current_insn_ptr
= *jump_entry
;
6238 err
= compile_bytecodes (tpoint
->cond
);
6240 if (err
== expr_eval_no_error
)
6244 /* Record the beginning of the compiled code. */
6245 tpoint
->compiled_cond
= entry_point
;
6247 trace_debug ("Condition compilation for tracepoint %d complete\n",
6252 /* Leave the unfinished code in situ, but don't point to it. */
6254 tpoint
->compiled_cond
= 0;
6256 trace_debug ("Condition compilation for tracepoint %d failed, "
6258 tpoint
->number
, err
);
6261 /* Update the code pointer passed in. Note that we do this even if
6262 the compile fails, so that we can look at the partial results
6263 instead of letting them be overwritten. */
6264 *jump_entry
= current_insn_ptr
;
6266 /* Leave a gap, to aid dump decipherment. */
6270 /* Scan an agent expression for any evidence that the given PC is the
6271 target of a jump bytecode in the expression. */
6274 is_goto_target (struct agent_expr
*aexpr
, int pc
)
6279 for (i
= 0; i
< aexpr
->length
; i
+= 1 + gdb_agent_op_sizes
[op
])
6281 op
= aexpr
->bytes
[i
];
6283 if (op
== gdb_agent_op_goto
|| op
== gdb_agent_op_if_goto
)
6285 int target
= (aexpr
->bytes
[i
+ 1] << 8) + aexpr
->bytes
[i
+ 2];
6294 /* Given an agent expression, turn it into native code. */
6296 static enum eval_result_type
6297 compile_bytecodes (struct agent_expr
*aexpr
)
6301 unsigned char op
, next_op
;
6303 /* This is only used to build 64-bit value for constants. */
6305 struct bytecode_address
*aentry
, *aentry2
;
6310 trace_debug ("Cannot compile op 0x%x\n", op); \
6311 return expr_eval_unhandled_opcode; \
6314 if (aexpr
->length
== 0)
6316 trace_debug ("empty agent expression\n");
6317 return expr_eval_empty_expression
;
6320 bytecode_address_table
= NULL
;
6324 op
= aexpr
->bytes
[pc
];
6326 trace_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
6328 /* Record the compiled-code address of the bytecode, for use by
6329 jump instructions. */
6330 aentry
= xmalloc (sizeof (struct bytecode_address
));
6332 aentry
->address
= current_insn_ptr
;
6333 aentry
->goto_pc
= -1;
6334 aentry
->from_offset
= aentry
->from_size
= 0;
6335 aentry
->next
= bytecode_address_table
;
6336 bytecode_address_table
= aentry
;
6344 case gdb_agent_op_add
:
6348 case gdb_agent_op_sub
:
6352 case gdb_agent_op_mul
:
6356 case gdb_agent_op_div_signed
:
6360 case gdb_agent_op_div_unsigned
:
6364 case gdb_agent_op_rem_signed
:
6368 case gdb_agent_op_rem_unsigned
:
6372 case gdb_agent_op_lsh
:
6376 case gdb_agent_op_rsh_signed
:
6380 case gdb_agent_op_rsh_unsigned
:
6381 emit_rsh_unsigned ();
6384 case gdb_agent_op_trace
:
6388 case gdb_agent_op_trace_quick
:
6392 case gdb_agent_op_log_not
:
6396 case gdb_agent_op_bit_and
:
6400 case gdb_agent_op_bit_or
:
6404 case gdb_agent_op_bit_xor
:
6408 case gdb_agent_op_bit_not
:
6412 case gdb_agent_op_equal
:
6413 next_op
= aexpr
->bytes
[pc
];
6414 if (next_op
== gdb_agent_op_if_goto
6415 && !is_goto_target (aexpr
, pc
)
6416 && target_emit_ops ()->emit_eq_goto
)
6418 trace_debug ("Combining equal & if_goto");
6421 arg
= aexpr
->bytes
[pc
++];
6422 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6423 aentry
->goto_pc
= arg
;
6424 emit_eq_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6426 else if (next_op
== gdb_agent_op_log_not
6427 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
6428 && !is_goto_target (aexpr
, pc
+ 1)
6429 && target_emit_ops ()->emit_ne_goto
)
6431 trace_debug ("Combining equal & log_not & if_goto");
6434 arg
= aexpr
->bytes
[pc
++];
6435 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6436 aentry
->goto_pc
= arg
;
6437 emit_ne_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6443 case gdb_agent_op_less_signed
:
6444 next_op
= aexpr
->bytes
[pc
];
6445 if (next_op
== gdb_agent_op_if_goto
6446 && !is_goto_target (aexpr
, pc
))
6448 trace_debug ("Combining less_signed & if_goto");
6451 arg
= aexpr
->bytes
[pc
++];
6452 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6453 aentry
->goto_pc
= arg
;
6454 emit_lt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6456 else if (next_op
== gdb_agent_op_log_not
6457 && !is_goto_target (aexpr
, pc
)
6458 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
6459 && !is_goto_target (aexpr
, pc
+ 1))
6461 trace_debug ("Combining less_signed & log_not & if_goto");
6464 arg
= aexpr
->bytes
[pc
++];
6465 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6466 aentry
->goto_pc
= arg
;
6467 emit_ge_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6470 emit_less_signed ();
6473 case gdb_agent_op_less_unsigned
:
6474 emit_less_unsigned ();
6477 case gdb_agent_op_ext
:
6478 arg
= aexpr
->bytes
[pc
++];
6479 if (arg
< (sizeof (LONGEST
) * 8))
6483 case gdb_agent_op_ref8
:
6487 case gdb_agent_op_ref16
:
6491 case gdb_agent_op_ref32
:
6495 case gdb_agent_op_ref64
:
6499 case gdb_agent_op_if_goto
:
6500 arg
= aexpr
->bytes
[pc
++];
6501 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6502 aentry
->goto_pc
= arg
;
6503 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6506 case gdb_agent_op_goto
:
6507 arg
= aexpr
->bytes
[pc
++];
6508 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6509 aentry
->goto_pc
= arg
;
6510 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6513 case gdb_agent_op_const8
:
6514 emit_stack_flush ();
6515 top
= aexpr
->bytes
[pc
++];
6519 case gdb_agent_op_const16
:
6520 emit_stack_flush ();
6521 top
= aexpr
->bytes
[pc
++];
6522 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6526 case gdb_agent_op_const32
:
6527 emit_stack_flush ();
6528 top
= aexpr
->bytes
[pc
++];
6529 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6530 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6531 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6535 case gdb_agent_op_const64
:
6536 emit_stack_flush ();
6537 top
= aexpr
->bytes
[pc
++];
6538 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6539 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6540 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6541 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6542 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6543 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6544 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6548 case gdb_agent_op_reg
:
6549 emit_stack_flush ();
6550 arg
= aexpr
->bytes
[pc
++];
6551 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6555 case gdb_agent_op_end
:
6556 trace_debug ("At end of expression\n");
6558 /* Assume there is one stack element left, and that it is
6559 cached in "top" where emit_epilogue can get to it. */
6560 emit_stack_adjust (1);
6565 case gdb_agent_op_dup
:
6566 /* In our design, dup is equivalent to stack flushing. */
6567 emit_stack_flush ();
6570 case gdb_agent_op_pop
:
6574 case gdb_agent_op_zero_ext
:
6575 arg
= aexpr
->bytes
[pc
++];
6576 if (arg
< (sizeof (LONGEST
) * 8))
6577 emit_zero_ext (arg
);
6580 case gdb_agent_op_swap
:
6581 next_op
= aexpr
->bytes
[pc
];
6582 /* Detect greater-than comparison sequences. */
6583 if (next_op
== gdb_agent_op_less_signed
6584 && !is_goto_target (aexpr
, pc
)
6585 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
6586 && !is_goto_target (aexpr
, pc
+ 1))
6588 trace_debug ("Combining swap & less_signed & if_goto");
6591 arg
= aexpr
->bytes
[pc
++];
6592 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6593 aentry
->goto_pc
= arg
;
6594 emit_gt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6596 else if (next_op
== gdb_agent_op_less_signed
6597 && !is_goto_target (aexpr
, pc
)
6598 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_log_not
)
6599 && !is_goto_target (aexpr
, pc
+ 1)
6600 && (aexpr
->bytes
[pc
+ 2] == gdb_agent_op_if_goto
)
6601 && !is_goto_target (aexpr
, pc
+ 2))
6603 trace_debug ("Combining swap & less_signed & log_not & if_goto");
6606 arg
= aexpr
->bytes
[pc
++];
6607 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6608 aentry
->goto_pc
= arg
;
6609 emit_le_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6615 case gdb_agent_op_getv
:
6616 emit_stack_flush ();
6617 arg
= aexpr
->bytes
[pc
++];
6618 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6619 emit_int_call_1 (ipa_sym_addrs
.addr_get_trace_state_variable_value
,
6623 case gdb_agent_op_setv
:
6624 arg
= aexpr
->bytes
[pc
++];
6625 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6626 emit_void_call_2 (ipa_sym_addrs
.addr_set_trace_state_variable_value
,
6630 case gdb_agent_op_tracev
:
6634 /* GDB never (currently) generates any of these ops. */
6635 case gdb_agent_op_float
:
6636 case gdb_agent_op_ref_float
:
6637 case gdb_agent_op_ref_double
:
6638 case gdb_agent_op_ref_long_double
:
6639 case gdb_agent_op_l_to_d
:
6640 case gdb_agent_op_d_to_l
:
6641 case gdb_agent_op_trace16
:
6646 trace_debug ("Agent expression op 0x%x not recognized\n", op
);
6647 /* Don't struggle on, things will just get worse. */
6648 return expr_eval_unrecognized_opcode
;
6651 /* This catches errors that occur in target-specific code
6655 trace_debug ("Error %d while emitting code for %s\n",
6656 emit_error
, gdb_agent_op_name (op
));
6657 return expr_eval_unhandled_opcode
;
6660 trace_debug ("Op %s compiled\n", gdb_agent_op_name (op
));
6663 /* Now fill in real addresses as goto destinations. */
6664 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
6668 if (aentry
->goto_pc
< 0)
6671 /* Find the location that we are going to, and call back into
6672 target-specific code to write the actual address or
6674 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
6676 if (aentry2
->pc
== aentry
->goto_pc
)
6678 trace_debug ("Want to jump from %s to %s\n",
6679 paddress (aentry
->address
),
6680 paddress (aentry2
->address
));
6681 write_goto_address (aentry
->address
+ aentry
->from_offset
,
6682 aentry2
->address
, aentry
->from_size
);
6688 /* Error out if we didn't find a destination. */
6691 trace_debug ("Destination of goto %d not found\n",
6693 return expr_eval_invalid_goto
;
6697 return expr_eval_no_error
;
6700 /* We'll need to adjust these when we consider bi-arch setups, and big
6704 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
6706 return write_inferior_memory (where
,
6707 (unsigned char *) &ptr
, sizeof (void *));
6710 /* The base pointer of the IPA's heap. This is the only memory the
6711 IPA is allowed to use. The IPA should _not_ call the inferior's
6712 `malloc' during operation. That'd be slow, and, most importantly,
6713 it may not be safe. We may be collecting a tracepoint in a signal
6714 handler, for example. */
6715 static CORE_ADDR target_tp_heap
;
6717 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
6721 target_malloc (ULONGEST size
)
6725 if (target_tp_heap
== 0)
6727 /* We have the pointer *address*, need what it points to. */
6728 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
6730 fatal ("could get target heap head pointer");
6733 ptr
= target_tp_heap
;
6734 target_tp_heap
+= size
;
6736 /* Pad to 8-byte alignment. */
6737 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
6743 download_agent_expr (struct agent_expr
*expr
)
6745 CORE_ADDR expr_addr
;
6746 CORE_ADDR expr_bytes
;
6748 expr_addr
= target_malloc (sizeof (*expr
));
6749 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
6751 expr_bytes
= target_malloc (expr
->length
);
6752 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
6754 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
6759 /* Align V up to N bits. */
6760 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
6762 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
6765 download_tracepoint_1 (struct tracepoint
*tpoint
)
6767 struct tracepoint target_tracepoint
;
6768 CORE_ADDR tpptr
= 0;
6770 gdb_assert (tpoint
->type
== fast_tracepoint
6771 || tpoint
->type
== static_tracepoint
);
6773 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
6775 CORE_ADDR jentry
, jump_entry
;
6777 jentry
= jump_entry
= get_jump_space_head ();
6779 if (tpoint
->cond
!= NULL
)
6781 /* Pad to 8-byte alignment. (needed?) */
6782 /* Actually this should be left for the target to
6784 jentry
= UALIGN (jentry
, 8);
6786 compile_tracepoint_condition (tpoint
, &jentry
);
6789 /* Pad to 8-byte alignment. */
6790 jentry
= UALIGN (jentry
, 8);
6791 claim_jump_space (jentry
- jump_entry
);
6794 target_tracepoint
= *tpoint
;
6796 tpptr
= target_malloc (sizeof (*tpoint
));
6797 tpoint
->obj_addr_on_target
= tpptr
;
6799 /* Write the whole object. We'll fix up its pointers in a bit.
6800 Assume no next for now. This is fixed up above on the next
6801 iteration, if there's any. */
6802 target_tracepoint
.next
= NULL
;
6803 /* Need to clear this here too, since we're downloading the
6804 tracepoints before clearing our own copy. */
6805 target_tracepoint
.hit_count
= 0;
6807 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
6808 sizeof (target_tracepoint
));
6811 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6813 download_agent_expr (tpoint
->cond
));
6815 if (tpoint
->numactions
)
6818 CORE_ADDR actions_array
;
6820 /* The pointers array. */
6822 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6823 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6827 /* Now for each pointer, download the action. */
6828 for (i
= 0; i
< tpoint
->numactions
; i
++)
6830 CORE_ADDR ipa_action
= 0;
6831 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6833 switch (action
->type
)
6837 = target_malloc (sizeof (struct collect_memory_action
));
6838 write_inferior_memory (ipa_action
,
6839 (unsigned char *) action
,
6840 sizeof (struct collect_memory_action
));
6844 = target_malloc (sizeof (struct collect_registers_action
));
6845 write_inferior_memory (ipa_action
,
6846 (unsigned char *) action
,
6847 sizeof (struct collect_registers_action
));
6852 struct eval_expr_action
*eaction
6853 = (struct eval_expr_action
*) action
;
6855 ipa_action
= target_malloc (sizeof (*eaction
));
6856 write_inferior_memory (ipa_action
,
6857 (unsigned char *) eaction
,
6860 expr
= download_agent_expr (eaction
->expr
);
6861 write_inferior_data_ptr
6862 (ipa_action
+ offsetof (struct eval_expr_action
, expr
),
6867 ipa_action
= target_malloc
6868 (sizeof (struct collect_static_trace_data_action
));
6869 write_inferior_memory
6871 (unsigned char *) action
,
6872 sizeof (struct collect_static_trace_data_action
));
6875 trace_debug ("unknown trace action '%c', ignoring",
6880 if (ipa_action
!= 0)
6881 write_inferior_data_ptr
6882 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
6889 download_tracepoint (struct tracepoint
*tpoint
)
6891 struct tracepoint
*tp
, *tp_prev
;
6893 if (tpoint
->type
!= fast_tracepoint
6894 && tpoint
->type
!= static_tracepoint
)
6897 download_tracepoint_1 (tpoint
);
6899 /* Find the previous entry of TPOINT, which is fast tracepoint or
6900 static tracepoint. */
6902 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6904 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6910 CORE_ADDR tp_prev_target_next_addr
;
6912 /* Insert TPOINT after TP_PREV in IPA. */
6913 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6914 + offsetof (struct tracepoint
, next
),
6915 &tp_prev_target_next_addr
))
6916 fatal ("error reading `tp_prev->next'");
6918 /* tpoint->next = tp_prev->next */
6919 write_inferior_data_ptr (tpoint
->obj_addr_on_target
6920 + offsetof (struct tracepoint
, next
),
6921 tp_prev_target_next_addr
);
6922 /* tp_prev->next = tpoint */
6923 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
6924 + offsetof (struct tracepoint
, next
),
6925 tpoint
->obj_addr_on_target
);
6928 /* First object in list, set the head pointer in the
6930 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
6931 tpoint
->obj_addr_on_target
);
6936 download_tracepoints (void)
6938 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
6939 struct tracepoint
*tpoint
;
6941 /* Start out empty. */
6942 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
6944 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6946 if (tpoint
->type
!= fast_tracepoint
6947 && tpoint
->type
!= static_tracepoint
)
6952 download_tracepoint_1 (tpoint
);
6954 tpptr
= tpoint
->obj_addr_on_target
;
6956 if (tpoint
== tracepoints
)
6958 /* First object in list, set the head pointer in the
6960 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
6964 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
6972 download_trace_state_variables (void)
6974 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6975 struct trace_state_variable
*tsv
;
6977 /* Start out empty. */
6978 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6980 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6982 struct trace_state_variable target_tsv
;
6984 /* TSV's with a getter have been initialized equally in both the
6985 inferior and GDBserver. Skip them. */
6986 if (tsv
->getter
!= NULL
)
6992 ptr
= target_malloc (sizeof (*tsv
));
6994 if (tsv
== trace_state_variables
)
6996 /* First object in list, set the head pointer in the
6999 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
7004 write_inferior_data_ptr (prev_ptr
7005 + offsetof (struct trace_state_variable
,
7010 /* Write the whole object. We'll fix up its pointers in a bit.
7011 Assume no next, fixup when needed. */
7012 target_tsv
.next
= NULL
;
7014 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
7015 sizeof (target_tsv
));
7017 if (tsv
->name
!= NULL
)
7019 size_t size
= strlen (tsv
->name
) + 1;
7020 CORE_ADDR name_addr
= target_malloc (size
);
7021 write_inferior_memory (name_addr
,
7022 (unsigned char *) tsv
->name
, size
);
7023 write_inferior_data_ptr (ptr
7024 + offsetof (struct trace_state_variable
,
7029 if (tsv
->getter
!= NULL
)
7031 fatal ("what to do with these?");
7037 /* Fixup the next pointer in the last item in the list. */
7038 write_inferior_data_ptr (prev_ptr
7039 + offsetof (struct trace_state_variable
,
7044 /* Upload complete trace frames out of the IP Agent's trace buffer
7045 into GDBserver's trace buffer. This always uploads either all or
7046 no trace frames. This is the counter part of
7047 `trace_alloc_trace_buffer'. See its description of the atomic
7048 synching mechanism. */
7051 upload_fast_traceframes (void)
7053 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
7054 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
7056 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
7057 unsigned int curr_tbctrl_idx
;
7058 unsigned int ipa_trace_buffer_ctrl_curr
;
7059 unsigned int ipa_trace_buffer_ctrl_curr_old
;
7060 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
7061 struct breakpoint
*about_to_request_buffer_space_bkpt
;
7062 CORE_ADDR ipa_trace_buffer_lo
;
7063 CORE_ADDR ipa_trace_buffer_hi
;
7065 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
7066 &ipa_traceframe_read_count_racy
))
7068 /* This will happen in most targets if the current thread is
7073 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
7074 &ipa_traceframe_write_count_racy
))
7077 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
7078 ipa_traceframe_write_count_racy
7079 - ipa_traceframe_read_count_racy
,
7080 ipa_traceframe_write_count_racy
,
7081 ipa_traceframe_read_count_racy
);
7083 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
7086 about_to_request_buffer_space_bkpt
7087 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
7090 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
7091 &ipa_trace_buffer_ctrl_curr
))
7094 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
7096 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
7099 unsigned int prev
, counter
;
7101 /* Update the token, with new counters, and the GDBserver stamp
7102 bit. Alway reuse the current TBC index. */
7103 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
7104 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
7106 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
7112 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
7113 ipa_trace_buffer_ctrl_curr
))
7116 trace_debug ("Lib: Committed %08x -> %08x",
7117 ipa_trace_buffer_ctrl_curr_old
,
7118 ipa_trace_buffer_ctrl_curr
);
7120 /* Re-read these, now that we've installed the
7121 `about_to_request_buffer_space' breakpoint/lock. A thread could
7122 have finished a traceframe between the last read of these
7123 counters and setting the breakpoint above. If we start
7124 uploading, we never want to leave this function with
7125 traceframe_read_count != 0, otherwise, GDBserver could end up
7126 incrementing the counter tokens more than once (due to event loop
7127 nesting), which would break the IP agent's "effective" detection
7128 (see trace_alloc_trace_buffer). */
7129 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
7130 &ipa_traceframe_read_count
))
7132 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
7133 &ipa_traceframe_write_count
))
7138 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
7139 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
7140 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
7142 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
7143 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
7144 trace_debug ("note that ipa_traceframe_count's parts changed");
7147 /* Get the address of the current TBC object (the IP agent has an
7148 array of 3 such objects). The index is stored in the TBC
7150 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
7151 ipa_trace_buffer_ctrl_addr
7152 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
7154 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
7155 (unsigned char *) &ipa_trace_buffer_ctrl
,
7156 sizeof (struct ipa_trace_buffer_control
)))
7159 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
7160 &ipa_trace_buffer_lo
))
7162 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
7163 &ipa_trace_buffer_hi
))
7166 /* Offsets are easier to grok for debugging than raw addresses,
7167 especially for the small trace buffer sizes that are useful for
7169 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
7170 "endfree=%d wrap=%d hi=%d",
7172 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
7173 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
7174 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
7175 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
7176 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
7178 /* Note that the IPA's buffer is always circular. */
7180 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
7182 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
7183 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
7185 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
7186 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
7187 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
7188 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
7191 tf
= IPA_FIRST_TRACEFRAME ();
7193 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
7195 struct tracepoint
*tpoint
;
7196 struct traceframe
*tframe
;
7197 unsigned char *block
;
7198 struct traceframe ipa_tframe
;
7200 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
7201 offsetof (struct traceframe
, data
)))
7202 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
7204 if (ipa_tframe
.tpnum
== 0)
7205 fatal ("Uploading: No (more) fast traceframes, but "
7206 "ipa_traceframe_count == %u??\n",
7207 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
7209 /* Note that this will be incorrect for multi-location
7211 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
7213 tframe
= add_traceframe (tpoint
);
7216 trace_buffer_is_full
= 1;
7217 trace_debug ("Uploading: trace buffer is full");
7221 /* Copy the whole set of blocks in one go for now. FIXME:
7222 split this in smaller blocks. */
7223 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
7226 if (read_inferior_memory (tf
7227 + offsetof (struct traceframe
, data
),
7228 block
, ipa_tframe
.data_size
))
7229 error ("Uploading: Couldn't read traceframe data at %s\n",
7230 paddress (tf
+ offsetof (struct traceframe
, data
)));
7233 trace_debug ("Uploading: traceframe didn't fit");
7234 finish_traceframe (tframe
);
7237 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
7239 /* If we freed the traceframe that wrapped around, go back
7240 to the non-wrap case. */
7241 if (tf
< ipa_trace_buffer_ctrl
.start
)
7243 trace_debug ("Lib: Discarding past the wraparound");
7244 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
7246 ipa_trace_buffer_ctrl
.start
= tf
;
7247 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
7248 ++ipa_traceframe_read_count
;
7250 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
7251 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
7253 trace_debug ("Lib: buffer is fully empty. "
7254 "Trace buffer [%d] start=%d free=%d endfree=%d",
7256 (int) (ipa_trace_buffer_ctrl
.start
7257 - ipa_trace_buffer_lo
),
7258 (int) (ipa_trace_buffer_ctrl
.free
7259 - ipa_trace_buffer_lo
),
7260 (int) (ipa_trace_buffer_ctrl
.end_free
7261 - ipa_trace_buffer_lo
));
7263 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
7264 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
7265 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
7266 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
7269 trace_debug ("Uploaded a traceframe\n"
7270 "Lib: Trace buffer [%d] start=%d free=%d "
7271 "endfree=%d wrap=%d hi=%d",
7273 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
7274 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
7275 (int) (ipa_trace_buffer_ctrl
.end_free
7276 - ipa_trace_buffer_lo
),
7277 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
7278 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
7281 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
7282 (unsigned char *) &ipa_trace_buffer_ctrl
,
7283 sizeof (struct ipa_trace_buffer_control
)))
7286 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
7287 ipa_traceframe_read_count
);
7289 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
7292 cancel_breakpoints ();
7294 delete_breakpoint (about_to_request_buffer_space_bkpt
);
7295 about_to_request_buffer_space_bkpt
= NULL
;
7299 if (trace_buffer_is_full
)
7304 #ifdef IN_PROCESS_AGENT
7306 IP_AGENT_EXPORT
int ust_loaded
;
7307 IP_AGENT_EXPORT
char cmd_buf
[CMD_BUF_SIZE
];
7311 /* Static tracepoints. */
7313 /* UST puts a "struct tracepoint" in the global namespace, which
7314 conflicts with our tracepoint. Arguably, being a library, it
7315 shouldn't take ownership of such a generic name. We work around it
7317 #define tracepoint ust_tracepoint
7318 #include <ust/ust.h>
7321 extern int serialize_to_text (char *outbuf
, int bufsize
,
7322 const char *fmt
, va_list ap
);
7324 #define GDB_PROBE_NAME "gdb"
7326 /* We dynamically search for the UST symbols instead of linking them
7327 in. This lets the user decide if the application uses static
7328 tracepoints, instead of always pulling libust.so in. This vector
7329 holds pointers to all functions we care about. */
7333 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
7334 const char *fmt
, va_list ap
);
7336 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
7337 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
7339 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
7341 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
7344 void (*marker_iter_start
) (struct marker_iter
*iter
);
7345 void (*marker_iter_next
) (struct marker_iter
*iter
);
7346 void (*marker_iter_stop
) (struct marker_iter
*iter
);
7347 void (*marker_iter_reset
) (struct marker_iter
*iter
);
7352 /* Cast through typeof to catch incompatible API changes. Since UST
7353 only builds with gcc, we can freely use gcc extensions here
7355 #define GET_UST_SYM(SYM) \
7358 if (ust_ops.SYM == NULL) \
7359 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
7360 if (ust_ops.SYM == NULL) \
7364 #define USTF(SYM) ust_ops.SYM
7366 /* Get pointers to all libust.so functions we care about. */
7371 GET_UST_SYM (serialize_to_text
);
7373 GET_UST_SYM (ltt_probe_register
);
7374 GET_UST_SYM (ltt_probe_unregister
);
7375 GET_UST_SYM (ltt_marker_connect
);
7376 GET_UST_SYM (ltt_marker_disconnect
);
7378 GET_UST_SYM (marker_iter_start
);
7379 GET_UST_SYM (marker_iter_next
);
7380 GET_UST_SYM (marker_iter_stop
);
7381 GET_UST_SYM (marker_iter_reset
);
7387 /* Given an UST marker, return the matching gdb static tracepoint.
7388 The match is done by address. */
7390 static struct tracepoint
*
7391 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
7393 struct tracepoint
*tpoint
;
7395 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
7397 if (tpoint
->type
!= static_tracepoint
)
7400 if (tpoint
->address
== (uintptr_t) mdata
->location
)
7407 /* The probe function we install on lttng/ust markers. Whenever a
7408 probed ust marker is hit, this function is called. This is similar
7409 to gdb_collect, only for static tracepoints, instead of fast
7413 gdb_probe (const struct marker
*mdata
, void *probe_private
,
7414 struct registers
*regs
, void *call_private
,
7415 const char *fmt
, va_list *args
)
7417 struct tracepoint
*tpoint
;
7418 struct static_tracepoint_ctx ctx
;
7420 /* Don't do anything until the trace run is completely set up. */
7423 trace_debug ("gdb_probe: not tracing\n");
7427 ctx
.base
.type
= static_tracepoint
;
7428 ctx
.regcache_initted
= 0;
7433 /* Wrap the regblock in a register cache (in the stack, we don't
7434 want to malloc here). */
7435 ctx
.regspace
= alloca (register_cache_size ());
7436 if (ctx
.regspace
== NULL
)
7438 trace_debug ("Trace buffer block allocation failed, skipping");
7442 tpoint
= ust_marker_to_static_tracepoint (mdata
);
7445 trace_debug ("gdb_probe: marker not known: "
7446 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
7447 mdata
->location
, mdata
->channel
,
7448 mdata
->name
, mdata
->format
);
7452 if (!tpoint
->enabled
)
7454 trace_debug ("gdb_probe: tracepoint disabled");
7458 ctx
.tpoint
= tpoint
;
7460 trace_debug ("gdb_probe: collecting marker: "
7461 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
7462 mdata
->location
, mdata
->channel
,
7463 mdata
->name
, mdata
->format
);
7465 /* Test the condition if present, and collect if true. */
7466 if (tpoint
->cond
== NULL
7467 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
7470 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
7471 tpoint
->address
, tpoint
);
7473 if (stopping_tracepoint
7474 || trace_buffer_is_full
7475 || expr_eval_result
!= expr_eval_no_error
)
7480 /* If there was a condition and it evaluated to false, the only
7481 way we would stop tracing is if there was an error during
7482 condition expression evaluation. */
7483 if (expr_eval_result
!= expr_eval_no_error
)
7488 /* Called if the gdb static tracepoint requested collecting "$_sdata",
7489 static tracepoint string data. This is a string passed to the
7490 tracing library by the user, at the time of the tracepoint marker
7491 call. E.g., in the UST marker call:
7493 trace_mark (ust, bar33, "str %s", "FOOBAZ");
7495 the collected data is "str FOOBAZ".
7499 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
7501 struct tracepoint
*tpoint
,
7502 struct traceframe
*tframe
)
7504 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
7505 unsigned char *bufspace
;
7508 unsigned short blocklen
;
7512 trace_debug ("Wanted to collect static trace data, "
7513 "but there's no static trace data");
7517 va_copy (copy
, *umd
->args
);
7518 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
7521 trace_debug ("Want to collect ust data");
7523 /* 'S' + size + string */
7524 bufspace
= add_traceframe_block (tframe
,
7525 1 + sizeof (blocklen
) + size
+ 1);
7526 if (bufspace
== NULL
)
7528 trace_debug ("Trace buffer block allocation failed, skipping");
7532 /* Identify a static trace data block. */
7535 blocklen
= size
+ 1;
7536 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
7538 va_copy (copy
, *umd
->args
);
7539 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
7540 size
+ 1, umd
->fmt
, copy
);
7543 trace_debug ("Storing static tracepoint data in regblock: %s",
7544 bufspace
+ 1 + sizeof (blocklen
));
7547 /* The probe to register with lttng/ust. */
7548 static struct ltt_available_probe gdb_ust_probe
=
7555 #endif /* HAVE_UST */
7556 #endif /* IN_PROCESS_AGENT */
7560 #include <sys/socket.h>
7563 #ifndef UNIX_PATH_MAX
7564 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
7567 /* Where we put the socked used for synchronization. */
7568 #define SOCK_DIR P_tmpdir
7570 #endif /* HAVE_UST */
7572 #ifndef IN_PROCESS_AGENT
7577 gdb_ust_connect_sync_socket (int pid
)
7579 struct sockaddr_un addr
;
7581 char path
[UNIX_PATH_MAX
];
7583 res
= xsnprintf (path
, UNIX_PATH_MAX
, "%s/gdb_ust%d", SOCK_DIR
, pid
);
7584 if (res
>= UNIX_PATH_MAX
)
7586 trace_debug ("string overflow allocating socket name");
7590 res
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
7593 warning ("error opening sync socket: %s\n", strerror (errno
));
7597 addr
.sun_family
= AF_UNIX
;
7599 res
= xsnprintf (addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
7600 if (res
>= UNIX_PATH_MAX
)
7602 warning ("string overflow allocating socket name\n");
7607 res
= connect (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
7610 warning ("error connecting sync socket (%s): %s. "
7611 "Make sure the directory exists and that it is writable.",
7612 path
, strerror (errno
));
7620 /* Resume thread PTID. */
7623 resume_thread (ptid_t ptid
)
7625 struct thread_resume resume_info
;
7627 resume_info
.thread
= ptid
;
7628 resume_info
.kind
= resume_continue
;
7629 resume_info
.sig
= TARGET_SIGNAL_0
;
7630 (*the_target
->resume
) (&resume_info
, 1);
7633 /* Stop thread PTID. */
7636 stop_thread (ptid_t ptid
)
7638 struct thread_resume resume_info
;
7640 resume_info
.thread
= ptid
;
7641 resume_info
.kind
= resume_stop
;
7642 resume_info
.sig
= TARGET_SIGNAL_0
;
7643 (*the_target
->resume
) (&resume_info
, 1);
7646 /* Ask the in-process agent to run a command. Since we don't want to
7647 have to handle the IPA hitting breakpoints while running the
7648 command, we pause all threads, remove all breakpoints, and then set
7649 the helper thread re-running. We communicate with the helper
7650 thread by means of direct memory xfering, and a socket for
7654 run_inferior_command (char *cmd
)
7658 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
7660 ptid_t ptid
= null_ptid
;
7662 trace_debug ("run_inferior_command: running: %s", cmd
);
7665 uninsert_all_breakpoints ();
7667 if (read_inferior_integer (ipa_sym_addrs
.addr_helper_thread_id
, &tid
))
7669 warning ("Error reading helper thread's id in lib");
7675 warning ("helper thread not initialized yet");
7679 if (write_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
7680 (unsigned char *) cmd
, strlen (cmd
) + 1))
7682 warning ("Error writing command");
7686 ptid
= ptid_build (pid
, tid
, 0);
7688 resume_thread (ptid
);
7690 fd
= gdb_ust_connect_sync_socket (pid
);
7696 trace_debug ("signalling helper thread");
7700 ret
= write (fd
, buf
, 1);
7701 } while (ret
== -1 && errno
== EINTR
);
7703 trace_debug ("waiting for helper thread's response");
7707 ret
= read (fd
, buf
, 1);
7708 } while (ret
== -1 && errno
== EINTR
);
7712 trace_debug ("helper thread's response received");
7717 /* Need to read response with the inferior stopped. */
7718 if (!ptid_equal (ptid
, null_ptid
))
7720 int was_non_stop
= non_stop
;
7721 struct target_waitstatus status
;
7725 mywait (ptid
, &status
, 0, 0);
7726 non_stop
= was_non_stop
;
7731 if (read_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
7732 (unsigned char *) cmd
, CMD_BUF_SIZE
))
7734 warning ("Error reading command response");
7739 trace_debug ("run_inferior_command: response: %s", cmd
);
7743 reinsert_all_breakpoints ();
7749 #else /* HAVE_UST */
7752 run_inferior_command (char *cmd
)
7757 #endif /* HAVE_UST */
7759 #else /* !IN_PROCESS_AGENT */
7761 /* Thread ID of the helper thread. GDBserver reads this to know which
7762 is the help thread. This is an LWP id on Linux. */
7763 int helper_thread_id
;
7768 init_named_socket (const char *name
)
7771 struct sockaddr_un addr
;
7773 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
7776 warning ("socket creation failed: %s", strerror (errno
));
7780 addr
.sun_family
= AF_UNIX
;
7782 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
7783 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
7785 result
= access (name
, F_OK
);
7789 result
= unlink (name
);
7792 warning ("unlink failed: %s", strerror (errno
));
7796 warning ("socket %s already exists; overwriting", name
);
7799 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
7802 warning ("bind failed: %s", strerror (errno
));
7807 result
= listen (fd
, 1);
7810 warning ("listen: %s", strerror (errno
));
7819 gdb_ust_socket_init (void)
7822 char name
[UNIX_PATH_MAX
];
7824 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
7825 SOCK_DIR
, getpid ());
7826 if (result
>= UNIX_PATH_MAX
)
7828 trace_debug ("string overflow allocating socket name");
7832 fd
= init_named_socket (name
);
7834 warning ("Error initializing named socket (%s) for communication with the "
7835 "ust helper thread. Check that directory exists and that it "
7836 "is writable.", name
);
7841 /* Return an hexstr version of the STR C string, fit for sending to
7845 cstr_to_hexstr (const char *str
)
7847 int len
= strlen (str
);
7848 char *hexstr
= xmalloc (len
* 2 + 1);
7849 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
7853 /* The next marker to be returned on a qTsSTM command. */
7854 static const struct marker
*next_st
;
7856 /* Returns the first known marker. */
7861 struct marker_iter iter
;
7863 USTF(marker_iter_reset
) (&iter
);
7864 USTF(marker_iter_start
) (&iter
);
7869 /* Returns the marker following M. */
7871 const struct marker
*
7872 next_marker (const struct marker
*m
)
7874 struct marker_iter iter
;
7876 USTF(marker_iter_reset
) (&iter
);
7877 USTF(marker_iter_start
) (&iter
);
7879 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7881 if (iter
.marker
== m
)
7883 USTF(marker_iter_next
) (&iter
);
7891 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
7895 response_ust_marker (char *packet
, const struct marker
*st
)
7897 char *strid
, *format
, *tmp
;
7899 next_st
= next_marker (st
);
7901 tmp
= xmalloc (strlen (st
->channel
) + 1 +
7902 strlen (st
->name
) + 1);
7903 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
7905 strid
= cstr_to_hexstr (tmp
);
7908 format
= cstr_to_hexstr (st
->format
);
7910 sprintf (packet
, "m%s:%s:%s",
7911 paddress ((uintptr_t) st
->location
),
7919 /* Return the first static tracepoint, and initialize the state
7920 machine that will iterate through all the static tracepoints. */
7923 cmd_qtfstm (char *packet
)
7925 trace_debug ("Returning first trace state variable definition");
7927 if (first_marker ())
7928 response_ust_marker (packet
, first_marker ());
7930 strcpy (packet
, "l");
7933 /* Return additional trace state variable definitions. */
7936 cmd_qtsstm (char *packet
)
7938 trace_debug ("Returning static tracepoint");
7941 response_ust_marker (packet
, next_st
);
7943 strcpy (packet
, "l");
7946 /* Disconnect the GDB probe from a marker at a given address. */
7949 unprobe_marker_at (char *packet
)
7953 struct marker_iter iter
;
7955 p
+= sizeof ("unprobe_marker_at:") - 1;
7957 p
= unpack_varlen_hex (p
, &address
);
7959 USTF(marker_iter_reset
) (&iter
);
7960 USTF(marker_iter_start
) (&iter
);
7961 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7962 if ((uintptr_t ) iter
.marker
->location
== address
)
7966 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7970 warning ("could not disable marker %s/%s",
7971 iter
.marker
->channel
, iter
.marker
->name
);
7976 /* Connect the GDB probe to a marker at a given address. */
7979 probe_marker_at (char *packet
)
7983 struct marker_iter iter
;
7986 p
+= sizeof ("probe_marker_at:") - 1;
7988 p
= unpack_varlen_hex (p
, &address
);
7990 USTF(marker_iter_reset
) (&iter
);
7992 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7994 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7995 if ((uintptr_t ) m
->location
== address
)
7999 trace_debug ("found marker for address. "
8000 "ltt_marker_connect (marker = %s/%s)",
8001 m
->channel
, m
->name
);
8003 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
8005 if (result
&& result
!= -EEXIST
)
8006 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
8007 m
->channel
, m
->name
, -result
);
8011 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
8012 m
->channel
, m
->name
);
8016 strcpy (packet
, "OK");
8020 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
8025 cmd_qtstmat (char *packet
)
8029 struct marker_iter iter
;
8032 p
+= sizeof ("qTSTMat:") - 1;
8034 p
= unpack_varlen_hex (p
, &address
);
8036 USTF(marker_iter_reset
) (&iter
);
8038 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
8040 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
8041 if ((uintptr_t ) m
->location
== address
)
8043 response_ust_marker (packet
, m
);
8047 strcpy (packet
, "l");
8051 #include <sys/syscall.h>
8054 gdb_ust_thread (void *arg
)
8060 listen_fd
= gdb_ust_socket_init ();
8062 if (helper_thread_id
== 0)
8063 helper_thread_id
= syscall (SYS_gettid
);
8065 if (listen_fd
== -1)
8067 warning ("could not create sync socket\n");
8074 struct sockaddr_un sockaddr
;
8079 tmp
= sizeof (sockaddr
);
8083 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
8085 /* It seems an ERESTARTSYS can escape out of accept. */
8086 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
8090 warning ("Accept returned %d, error: %s\n",
8091 fd
, strerror (errno
));
8097 ret
= read (fd
, buf
, 1);
8098 } while (ret
== -1 && errno
== EINTR
);
8102 warning ("reading socket (fd=%d) failed with %s",
8103 fd
, strerror (errno
));
8110 if (strcmp ("qTfSTM", cmd_buf
) == 0)
8112 cmd_qtfstm (cmd_buf
);
8114 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
8116 cmd_qtsstm (cmd_buf
);
8118 else if (strncmp ("unprobe_marker_at:",
8120 sizeof ("unprobe_marker_at:") - 1) == 0)
8122 unprobe_marker_at (cmd_buf
);
8124 else if (strncmp ("probe_marker_at:",
8126 sizeof ("probe_marker_at:") - 1) == 0)
8128 probe_marker_at (cmd_buf
);
8130 else if (strncmp ("qTSTMat:",
8132 sizeof ("qTSTMat:") - 1) == 0)
8134 cmd_qtstmat (cmd_buf
);
8136 else if (strcmp (cmd_buf
, "help") == 0)
8138 strcpy (cmd_buf
, "for help, press F1\n");
8141 strcpy (cmd_buf
, "");
8144 /* Fix compiler's warning: ignoring return value of 'write'. */
8145 ret
= write (fd
, buf
, 1);
8166 /* We want the helper thread to be as transparent as possible, so
8167 have it inherit an all-signals-blocked mask. */
8169 sigfillset (&new_mask
);
8170 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
8172 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
8174 res
= pthread_create (&thread
,
8179 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
8181 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
8183 while (helper_thread_id
== 0)
8186 USTF(ltt_probe_register
) (&gdb_ust_probe
);
8189 #endif /* HAVE_UST */
8191 #include <sys/mman.h>
8194 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
8195 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
8196 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
8197 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
8198 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
8199 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
8201 /* Record the result of getting buffer space for fast tracepoint
8202 trampolines. Any error message is copied, since caller may not be
8203 using persistent storage. */
8206 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
8208 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
8209 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
8211 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
8213 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
8216 static void __attribute__ ((constructor
))
8217 initialize_tracepoint_ftlib (void)
8219 initialize_tracepoint ();
8226 #endif /* IN_PROCESS_AGENT */
8228 /* Return a timestamp, expressed as microseconds of the usual Unix
8229 time. (As the result is a 64-bit number, it will not overflow any
8233 get_timestamp (void)
8237 if (gettimeofday (&tv
, 0) != 0)
8240 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
8244 initialize_tracepoint (void)
8246 /* There currently no way to change the buffer size. */
8247 const int sizeOfBuffer
= 5 * 1024 * 1024;
8248 unsigned char *buf
= xmalloc (sizeOfBuffer
);
8249 init_trace_buffer (buf
, sizeOfBuffer
);
8251 /* Wire trace state variable 1 to be the timestamp. This will be
8252 uploaded to GDB upon connection and become one of its trace state
8253 variables. (In case you're wondering, if GDB already has a trace
8254 variable numbered 1, it will be renumbered.) */
8255 create_trace_state_variable (1, 0);
8256 set_trace_state_variable_name (1, "trace_timestamp");
8257 set_trace_state_variable_getter (1, get_timestamp
);
8259 #ifdef IN_PROCESS_AGENT
8264 pagesize
= sysconf (_SC_PAGE_SIZE
);
8268 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
8270 #define SCRATCH_BUFFER_NPAGES 20
8272 /* Allocate scratch buffer aligned on a page boundary, at a low
8273 address (close to the main executable's code). */
8274 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
8276 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
8277 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
8278 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
8280 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
8286 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
8289 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
8292 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
8294 /* It's not a fatal error for something to go wrong with trampoline
8295 buffer setup, but it can be mysterious, so create a channel to
8296 report back on what went wrong, using a fixed size since we may
8297 not be able to allocate space later when the problem occurs. */
8298 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
8300 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
8302 initialize_low_tracepoint ();