1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 /* This file is built for both GDBserver, and the in-process
30 agent (IPA), a shared library that includes a tracing agent that is
31 loaded by the inferior to support fast tracepoints. Fast
32 tracepoints (or more accurately, jump based tracepoints) are
33 implemented by patching the tracepoint location with a jump into a
34 small trampoline function whose job is to save the register state,
35 call the in-process tracing agent, and then execute the original
36 instruction that was under the tracepoint jump (possibly adjusted,
37 if PC-relative, or some such).
39 The current synchronization design is pull based. That means,
40 GDBserver does most of the work, by peeking/poking at the inferior
41 agent's memory directly for downloading tracepoint and associated
42 objects, and for uploading trace frames. Whenever the IPA needs
43 something from GDBserver (trace buffer is full, tracing stopped for
44 some reason, etc.) the IPA calls a corresponding hook function
45 where GDBserver has placed a breakpoint.
47 Each of the agents has its own trace buffer. When browsing the
48 trace frames built from slow and fast tracepoints from GDB (tfind
49 mode), there's no guarantee the user is seeing the trace frames in
50 strict chronological creation order, although, GDBserver tries to
51 keep the order relatively reasonable, by syncing the trace buffers
56 static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf
, 1, 2);
59 trace_vdebug (const char *fmt
, ...)
65 vsprintf (buf
, fmt
, ap
);
66 fprintf (stderr
, "gdbserver/tracepoint: %s\n", buf
);
70 #define trace_debug_1(level, fmt, args...) \
72 if (level <= debug_threads) \
73 trace_vdebug ((fmt), ##args); \
76 #define trace_debug(FMT, args...) \
77 trace_debug_1 (1, FMT, ##args)
80 # define ATTR_USED __attribute__((used))
81 # define ATTR_NOINLINE __attribute__((noinline))
82 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
85 # define ATTR_NOINLINE
86 # define ATTR_CONSTRUCTOR
89 /* Make sure the functions the IPA needs to export (symbols GDBserver
90 needs to query GDB about) are exported. */
92 #ifdef IN_PROCESS_AGENT
93 # if defined _WIN32 || defined __CYGWIN__
94 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
97 # define IP_AGENT_EXPORT \
98 __attribute__ ((visibility("default"))) ATTR_USED
100 # define IP_AGENT_EXPORT ATTR_USED
104 # define IP_AGENT_EXPORT
107 /* Prefix exported symbols, for good citizenship. All the symbols
108 that need exporting are defined in this module. */
109 #ifdef IN_PROCESS_AGENT
110 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
111 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
112 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
113 # define collecting gdb_agent_collecting
114 # define gdb_collect gdb_agent_gdb_collect
115 # define stop_tracing gdb_agent_stop_tracing
116 # define flush_trace_buffer gdb_agent_flush_trace_buffer
117 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
118 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
119 # define stopping_tracepoint gdb_agent_stopping_tracepoint
120 # define expr_eval_result gdb_agent_expr_eval_result
121 # define error_tracepoint gdb_agent_error_tracepoint
122 # define tracepoints gdb_agent_tracepoints
123 # define tracing gdb_agent_tracing
124 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
125 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
126 # define trace_buffer_lo gdb_agent_trace_buffer_lo
127 # define trace_buffer_hi gdb_agent_trace_buffer_hi
128 # define traceframe_read_count gdb_agent_traceframe_read_count
129 # define traceframe_write_count gdb_agent_traceframe_write_count
130 # define traceframes_created gdb_agent_traceframes_created
131 # define trace_state_variables gdb_agent_trace_state_variables
132 # define get_raw_reg gdb_agent_get_raw_reg
133 # define get_trace_state_variable_value \
134 gdb_agent_get_trace_state_variable_value
135 # define set_trace_state_variable_value \
136 gdb_agent_set_trace_state_variable_value
137 # define ust_loaded gdb_agent_ust_loaded
138 # define helper_thread_id gdb_agent_helper_thread_id
139 # define cmd_buf gdb_agent_cmd_buf
142 #ifndef IN_PROCESS_AGENT
144 /* Addresses of in-process agent's symbols GDBserver cares about. */
146 struct ipa_sym_addresses
148 CORE_ADDR addr_gdb_tp_heap_buffer
;
149 CORE_ADDR addr_gdb_jump_pad_buffer
;
150 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
151 CORE_ADDR addr_collecting
;
152 CORE_ADDR addr_gdb_collect
;
153 CORE_ADDR addr_stop_tracing
;
154 CORE_ADDR addr_flush_trace_buffer
;
155 CORE_ADDR addr_about_to_request_buffer_space
;
156 CORE_ADDR addr_trace_buffer_is_full
;
157 CORE_ADDR addr_stopping_tracepoint
;
158 CORE_ADDR addr_expr_eval_result
;
159 CORE_ADDR addr_error_tracepoint
;
160 CORE_ADDR addr_tracepoints
;
161 CORE_ADDR addr_tracing
;
162 CORE_ADDR addr_trace_buffer_ctrl
;
163 CORE_ADDR addr_trace_buffer_ctrl_curr
;
164 CORE_ADDR addr_trace_buffer_lo
;
165 CORE_ADDR addr_trace_buffer_hi
;
166 CORE_ADDR addr_traceframe_read_count
;
167 CORE_ADDR addr_traceframe_write_count
;
168 CORE_ADDR addr_traceframes_created
;
169 CORE_ADDR addr_trace_state_variables
;
170 CORE_ADDR addr_get_raw_reg
;
171 CORE_ADDR addr_get_trace_state_variable_value
;
172 CORE_ADDR addr_set_trace_state_variable_value
;
173 CORE_ADDR addr_ust_loaded
;
174 CORE_ADDR addr_helper_thread_id
;
175 CORE_ADDR addr_cmd_buf
;
178 #define STRINGIZE_1(STR) #STR
179 #define STRINGIZE(STR) STRINGIZE_1(STR)
180 #define IPA_SYM(SYM) \
182 STRINGIZE (gdb_agent_ ## SYM), \
183 offsetof (struct ipa_sym_addresses, addr_ ## SYM) \
192 IPA_SYM(gdb_tp_heap_buffer
),
193 IPA_SYM(gdb_jump_pad_buffer
),
194 IPA_SYM(gdb_jump_pad_buffer_end
),
196 IPA_SYM(gdb_collect
),
197 IPA_SYM(stop_tracing
),
198 IPA_SYM(flush_trace_buffer
),
199 IPA_SYM(about_to_request_buffer_space
),
200 IPA_SYM(trace_buffer_is_full
),
201 IPA_SYM(stopping_tracepoint
),
202 IPA_SYM(expr_eval_result
),
203 IPA_SYM(error_tracepoint
),
204 IPA_SYM(tracepoints
),
206 IPA_SYM(trace_buffer_ctrl
),
207 IPA_SYM(trace_buffer_ctrl_curr
),
208 IPA_SYM(trace_buffer_lo
),
209 IPA_SYM(trace_buffer_hi
),
210 IPA_SYM(traceframe_read_count
),
211 IPA_SYM(traceframe_write_count
),
212 IPA_SYM(traceframes_created
),
213 IPA_SYM(trace_state_variables
),
214 IPA_SYM(get_raw_reg
),
215 IPA_SYM(get_trace_state_variable_value
),
216 IPA_SYM(set_trace_state_variable_value
),
218 IPA_SYM(helper_thread_id
),
222 struct ipa_sym_addresses ipa_sym_addrs
;
224 int all_tracepoint_symbols_looked_up
;
227 in_process_agent_loaded (void)
229 return all_tracepoint_symbols_looked_up
;
232 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
234 /* Returns true if both the in-process agent library and the static
235 tracepoints libraries are loaded in the inferior. */
238 in_process_agent_loaded_ust (void)
242 if (!in_process_agent_loaded ())
244 warning ("In-process agent not loaded");
248 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
250 warning ("Error reading ust_loaded in lib");
258 write_e_ipa_not_loaded (char *buffer
)
261 "E.In-process agent library not loaded in process. "
262 "Fast and static tracepoints unavailable.");
265 /* Write an error to BUFFER indicating that UST isn't loaded in the
269 write_e_ust_not_loaded (char *buffer
)
273 "E.UST library not loaded in process. "
274 "Static tracepoints unavailable.");
276 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
280 /* If the in-process agent library isn't loaded in the inferior, write
281 an error to BUFFER, and return 1. Otherwise, return 0. */
284 maybe_write_ipa_not_loaded (char *buffer
)
286 if (!in_process_agent_loaded ())
288 write_e_ipa_not_loaded (buffer
);
294 /* If the in-process agent library and the ust (static tracepoints)
295 library aren't loaded in the inferior, write an error to BUFFER,
296 and return 1. Otherwise, return 0. */
299 maybe_write_ipa_ust_not_loaded (char *buffer
)
301 if (!in_process_agent_loaded ())
303 write_e_ipa_not_loaded (buffer
);
306 else if (!in_process_agent_loaded_ust ())
308 write_e_ust_not_loaded (buffer
);
314 /* Cache all future symbols that the tracepoints module might request.
315 We can not request symbols at arbitrary states in the remote
316 protocol, only when the client tells us that new symbols are
317 available. So when we load the in-process library, make sure to
318 check the entire list. */
321 tracepoint_look_up_symbols (void)
325 if (all_tracepoint_symbols_looked_up
)
328 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
331 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
333 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
336 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
341 all_tracepoint_symbols_looked_up
= 1;
346 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
347 of the "stop_tracing" function. When this breakpoint is hit,
348 tracing stopped in the IPA for some reason. E.g., due to
349 tracepoint reaching the pass count, hitting conditional expression
350 evaluation error, etc.
352 The IPA's trace buffer is never in circular tracing mode: instead,
353 GDBserver's is, and whenever the in-process buffer fills, it calls
354 "flush_trace_buffer", which triggers an internal breakpoint.
355 GDBserver reacts to this breakpoint by pulling the meanwhile
356 collected data. Old frames discarding is always handled on the
359 #ifdef IN_PROCESS_AGENT
360 int debug_threads
= 0;
363 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
365 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
369 /* Call this in the functions where GDBserver places a breakpoint, so
370 that the compiler doesn't try to be clever and skip calling the
371 function at all. This is necessary, even if we tell the compiler
372 to not inline said functions. */
374 #if defined(__GNUC__)
375 # define UNKNOWN_SIDE_EFFECTS() asm ("")
377 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
380 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
383 /* GDBserver places breakpoint here. */
384 UNKNOWN_SIDE_EFFECTS();
387 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
388 flush_trace_buffer (void)
390 /* GDBserver places breakpoint here. */
391 UNKNOWN_SIDE_EFFECTS();
396 #ifndef IN_PROCESS_AGENT
398 tracepoint_handler (CORE_ADDR address
)
400 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
405 /* Breakpoint at "stop_tracing" in the inferior lib. */
406 struct breakpoint
*stop_tracing_bkpt
;
407 static int stop_tracing_handler (CORE_ADDR
);
409 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
410 struct breakpoint
*flush_trace_buffer_bkpt
;
411 static int flush_trace_buffer_handler (CORE_ADDR
);
413 static void download_tracepoints (void);
414 static void download_trace_state_variables (void);
415 static void upload_fast_traceframes (void);
417 static int run_inferior_command (char *cmd
);
420 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
422 return read_inferior_memory (symaddr
, (unsigned char *) val
,
427 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
429 return read_inferior_memory (symaddr
, (unsigned char *) val
,
434 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
436 void *pval
= (void *) (uintptr_t) val
;
439 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
440 *val
= (uintptr_t) pval
;
445 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
447 void *pval
= (void *) (uintptr_t) val
;
448 return write_inferior_memory (symaddr
,
449 (unsigned char *) &pval
, sizeof (pval
));
453 write_inferior_integer (CORE_ADDR symaddr
, int val
)
455 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
459 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
461 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
466 /* This enum must exactly match what is documented in
467 gdb/doc/agentexpr.texi, including all the numerical values. */
471 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
472 gdb_agent_op_ ## NAME = VALUE,
478 static const char *gdb_agent_op_names
[gdb_agent_op_last
] =
481 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
486 static const unsigned char gdb_agent_op_sizes
[gdb_agent_op_last
] =
489 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
498 unsigned char *bytes
;
501 /* Base action. Concrete actions inherit this. */
503 struct tracepoint_action
508 /* An 'M' (collect memory) action. */
509 struct collect_memory_action
511 struct tracepoint_action base
;
518 /* An 'R' (collect registers) action. */
520 struct collect_registers_action
522 struct tracepoint_action base
;
525 /* An 'X' (evaluate expression) action. */
527 struct eval_expr_action
529 struct tracepoint_action base
;
531 struct agent_expr
*expr
;
534 /* An 'L' (collect static trace data) action. */
535 struct collect_static_trace_data_action
537 struct tracepoint_action base
;
540 /* This structure describes a piece of the source-level definition of
541 the tracepoint. The contents are not interpreted by the target,
542 but preserved verbatim for uploading upon reconnection. */
546 /* The type of string, such as "cond" for a conditional. */
549 /* The source-level string itself. For the sake of target
550 debugging, we store it in plaintext, even though it is always
551 transmitted in hex. */
554 /* Link to the next one in the list. We link them in the order
555 received, in case some make up an ordered list of commands or
557 struct source_string
*next
;
562 /* Trap based tracepoint. */
565 /* A fast tracepoint implemented with a jump instead of a trap. */
568 /* A static tracepoint, implemented by a program call into a tracing
573 struct tracepoint_hit_ctx
;
575 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
578 /* The definition of a tracepoint. */
580 /* Tracepoints may have multiple locations, each at a different
581 address. This can occur with optimizations, template
582 instantiation, etc. Since the locations may be in different
583 scopes, the conditions and actions may be different for each
584 location. Our target version of tracepoints is more like GDB's
585 notion of "breakpoint locations", but we have almost nothing that
586 is not per-location, so we bother having two kinds of objects. The
587 key consequence is that numbers are not unique, and that it takes
588 both number and address to identify a tracepoint uniquely. */
592 /* The number of the tracepoint, as specified by GDB. Several
593 tracepoint objects here may share a number. */
596 /* Address at which the tracepoint is supposed to trigger. Several
597 tracepoints may share an address. */
600 /* Tracepoint type. */
601 enum tracepoint_type type
;
603 /* True if the tracepoint is currently enabled. */
606 /* The number of single steps that will be performed after each
610 /* The number of times the tracepoint may be hit before it will
611 terminate the entire tracing run. */
614 /* Pointer to the agent expression that is the tracepoint's
615 conditional, or NULL if the tracepoint is unconditional. */
616 struct agent_expr
*cond
;
618 /* The list of actions to take when the tracepoint triggers. */
620 struct tracepoint_action
**actions
;
622 /* Count of the times we've hit this tracepoint during the run.
623 Note that while-stepping steps are not counted as "hits". */
626 CORE_ADDR compiled_cond
;
628 /* Link to the next tracepoint in the list. */
629 struct tracepoint
*next
;
631 #ifndef IN_PROCESS_AGENT
632 /* The list of actions to take when the tracepoint triggers, in
633 string/packet form. */
636 /* The collection of strings that describe the tracepoint as it was
637 entered into GDB. These are not used by the target, but are
638 reported back to GDB upon reconnection. */
639 struct source_string
*source_strings
;
641 /* The number of bytes displaced by fast tracepoints. It may subsume
642 multiple instructions, for multi-byte fast tracepoints. This
643 field is only valid for fast tracepoints. */
646 /* Only for fast tracepoints. */
647 CORE_ADDR obj_addr_on_target
;
649 /* Address range where the original instruction under a fast
650 tracepoint was relocated to. (_end is actually one byte past
652 CORE_ADDR adjusted_insn_addr
;
653 CORE_ADDR adjusted_insn_addr_end
;
655 /* The address range of the piece of the jump pad buffer that was
656 assigned to this fast tracepoint. (_end is actually one byte
659 CORE_ADDR jump_pad_end
;
661 /* The list of actions to take while in a stepping loop. These
662 fields are only valid for patch-based tracepoints. */
663 int num_step_actions
;
664 struct tracepoint_action
**step_actions
;
665 /* Same, but in string/packet form. */
666 char **step_actions_str
;
668 /* Handle returned by the breakpoint or tracepoint module when we
669 inserted the trap or jump, or hooked into a static tracepoint.
670 NULL if we haven't inserted it yet. */
676 #ifndef IN_PROCESS_AGENT
678 /* Given `while-stepping', a thread may be collecting data for more
679 than one tracepoint simultaneously. On the other hand, the same
680 tracepoint with a while-stepping action may be hit by more than one
681 thread simultaneously (but not quite, each thread could be handling
682 a different step). Each thread holds a list of these objects,
683 representing the current step of each while-stepping action being
688 struct wstep_state
*next
;
690 /* The tracepoint number. */
692 /* The tracepoint's address. */
693 CORE_ADDR tp_address
;
695 /* The number of the current step in this 'while-stepping'
702 /* The linked list of all tracepoints. Marked explicitly as used as
703 the in-process library doesn't use it for the fast tracepoints
705 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
707 #ifndef IN_PROCESS_AGENT
709 /* Pointer to the last tracepoint in the list, new tracepoints are
710 linked in at the end. */
712 static struct tracepoint
*last_tracepoint
;
715 /* The first tracepoint to exceed its pass count. */
717 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
719 /* True if the trace buffer is full or otherwise no longer usable. */
721 IP_AGENT_EXPORT
int trace_buffer_is_full
;
723 /* Enumeration of the different kinds of things that can happen during
724 agent expression evaluation. */
726 enum eval_result_type
729 expr_eval_empty_expression
,
730 expr_eval_empty_stack
,
731 expr_eval_stack_overflow
,
732 expr_eval_stack_underflow
,
733 expr_eval_unhandled_opcode
,
734 expr_eval_unrecognized_opcode
,
735 expr_eval_divide_by_zero
,
736 expr_eval_invalid_goto
739 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
741 #ifndef IN_PROCESS_AGENT
743 static const char *eval_result_names
[] =
745 "terror:in the attic", /* this should never be reported */
746 "terror:empty expression",
747 "terror:empty stack",
748 "terror:stack overflow",
749 "terror:stack underflow",
750 "terror:unhandled opcode",
751 "terror:unrecognized opcode",
752 "terror:divide by zero"
757 /* The tracepoint in which the error occurred. */
759 static struct tracepoint
*error_tracepoint
;
761 struct trace_state_variable
763 /* This is the name of the variable as used in GDB. The target
764 doesn't use the name, but needs to have it for saving and
765 reconnection purposes. */
768 /* This number identifies the variable uniquely. Numbers may be
769 assigned either by the target (in the case of builtin variables),
770 or by GDB, and are presumed unique during the course of a trace
774 /* The variable's initial value, a 64-bit signed integer always. */
775 LONGEST initial_value
;
777 /* The variable's value, a 64-bit signed integer always. */
780 /* Pointer to a getter function, used to supply computed values. */
781 LONGEST (*getter
) (void);
783 /* Link to the next variable. */
784 struct trace_state_variable
*next
;
787 /* Linked list of all trace state variables. */
789 #ifdef IN_PROCESS_AGENT
790 struct trace_state_variable
*alloced_trace_state_variables
;
793 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
795 /* The results of tracing go into a fixed-size space known as the
796 "trace buffer". Because usage follows a limited number of
797 patterns, we manage it ourselves rather than with malloc. Basic
798 rules are that we create only one trace frame at a time, each is
799 variable in size, they are never moved once created, and we only
800 discard if we are doing a circular buffer, and then only the oldest
801 ones. Each trace frame includes its own size, so we don't need to
802 link them together, and the trace frame number is relative to the
803 first one, so we don't need to record numbers. A trace frame also
804 records the number of the tracepoint that created it. The data
805 itself is a series of blocks, each introduced by a single character
806 and with a defined format. Each type of block has enough
807 type/length info to allow scanners to jump quickly from one block
808 to the next without reading each byte in the block. */
810 /* Trace buffer management would be simple - advance a free pointer
811 from beginning to end, then stop - were it not for the circular
812 buffer option, which is a useful way to prevent a trace run from
813 stopping prematurely because the buffer filled up. In the circular
814 case, the location of the first trace frame (trace_buffer_start)
815 moves as old trace frames are discarded. Also, since we grow trace
816 frames incrementally as actions are performed, we wrap around to
817 the beginning of the trace buffer. This is per-block, so each
818 block within a trace frame remains contiguous. Things get messy
819 when the wrapped-around trace frame is the one being discarded; the
820 free space ends up in two parts at opposite ends of the buffer. */
823 # if defined(__GNUC__)
824 # define ATTR_PACKED __attribute__ ((packed))
826 # define ATTR_PACKED /* nothing */
830 /* The data collected at a tracepoint hit. This object should be as
831 small as possible, since there may be a great many of them. We do
832 not need to keep a frame number, because they are all sequential
833 and there are no deletions; so the Nth frame in the buffer is
834 always frame number N. */
838 /* Number of the tracepoint that collected this traceframe. A value
839 of 0 indicates the current end of the trace buffer. We make this
840 a 16-bit field because it's never going to happen that GDB's
841 numbering of tracepoints reaches 32,000. */
844 /* The size of the data in this trace frame. We limit this to 32
845 bits, even on a 64-bit target, because it's just implausible that
846 one is validly going to collect 4 gigabytes of data at a single
848 unsigned int data_size
: 32;
850 /* The base of the trace data, which is contiguous from this point. */
851 unsigned char data
[0];
855 /* The traceframe to be used as the source of data to send back to
856 GDB. A value of -1 means to get data from the live program. */
858 int current_traceframe
= -1;
860 /* This flag is true if the trace buffer is circular, meaning that
861 when it fills, the oldest trace frames are discarded in order to
864 #ifndef IN_PROCESS_AGENT
865 static int circular_trace_buffer
;
868 /* Pointer to the block of memory that traceframes all go into. */
870 static unsigned char *trace_buffer_lo
;
872 /* Pointer to the end of the trace buffer, more precisely to the byte
873 after the end of the buffer. */
875 static unsigned char *trace_buffer_hi
;
877 /* Control structure holding the read/write/etc. pointers into the
878 trace buffer. We need more than one of these to implement a
879 transaction-like mechanism to garantees that both GDBserver and the
880 in-process agent can try to change the trace buffer
883 struct trace_buffer_control
885 /* Pointer to the first trace frame in the buffer. In the
886 non-circular case, this is equal to trace_buffer_lo, otherwise it
887 moves around in the buffer. */
888 unsigned char *start
;
890 /* Pointer to the free part of the trace buffer. Note that we clear
891 several bytes at and after this pointer, so that traceframe
892 scans/searches terminate properly. */
895 /* Pointer to the byte after the end of the free part. Note that
896 this may be smaller than trace_buffer_free in the circular case,
897 and means that the free part is in two pieces. Initially it is
898 equal to trace_buffer_hi, then is generally equivalent to
899 trace_buffer_start. */
900 unsigned char *end_free
;
902 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
903 this is the point at which the trace data breaks, and resumes at
908 /* Same as above, to be used by GDBserver when updating the in-process
910 struct ipa_trace_buffer_control
919 /* We have possibly both GDBserver and an inferior thread accessing
920 the same IPA trace buffer memory. The IPA is the producer (tries
921 to put new frames in the buffer), while GDBserver occasionally
922 consumes them, that is, flushes the IPA's buffer into its own
923 buffer. Both sides need to update the trace buffer control
924 pointers (current head, tail, etc.). We can't use a global lock to
925 synchronize the accesses, as otherwise we could deadlock GDBserver
926 (if the thread holding the lock stops for a signal, say). So
927 instead of that, we use a transaction scheme where GDBserver writes
928 always prevail over the IPAs writes, and, we have the IPA detect
929 the commit failure/overwrite, and retry the whole attempt. This is
930 mainly implemented by having a global token object that represents
931 who wrote last to the buffer control structure. We need to freeze
932 any inferior writing to the buffer while GDBserver touches memory,
933 so that the inferior can correctly detect that GDBserver had been
934 there, otherwise, it could mistakingly think its commit was
935 successful; that's implemented by simply having GDBserver set a
936 breakpoint the inferior hits if it is the critical region.
938 There are three cycling trace buffer control structure copies
939 (buffer head, tail, etc.), with the token object including an index
940 indicating which is current live copy. The IPA tentatively builds
941 an updated copy in a non-current control structure, while GDBserver
942 always clobbers the current version directly. The IPA then tries
943 to atomically "commit" its version; if GDBserver clobbered the
944 structure meanwhile, that will fail, and the IPA restarts the
947 Listing the step in further detail, we have:
949 In-process agent (producer):
951 - passes by `about_to_request_buffer_space' breakpoint/lock
953 - reads current token, extracts current trace buffer control index,
954 and starts tentatively updating the rightmost one (0->1, 1->2,
955 2->0). Note that only one inferior thread is executing this code
956 at any given time, due to an outer lock in the jump pads.
958 - updates counters, and tries to commit the token.
960 - passes by second `about_to_request_buffer_space' breakpoint/lock,
961 leaving the sync region.
963 - checks if the update was effective.
965 - if trace buffer was found full, hits flush_trace_buffer
966 breakpoint, and restarts later afterwards.
968 GDBserver (consumer):
970 - sets `about_to_request_buffer_space' breakpoint/lock.
972 - updates the token unconditionally, using the current buffer
973 control index, since it knows that the IP agent always writes to
974 the rightmost, and due to the breakpoint, at most one IP thread
975 can try to update the trace buffer concurrently to GDBserver, so
976 there will be no danger of trace buffer control index wrap making
977 the IPA write to the same index as GDBserver.
979 - flushes the IP agent's trace buffer completely, and updates the
980 current trace buffer control structure. GDBserver *always* wins.
982 - removes the `about_to_request_buffer_space' breakpoint.
984 The token is stored in the `trace_buffer_ctrl_curr' variable.
985 Internally, it's bits are defined as:
987 |-------------+-----+-------------+--------+-------------+--------------|
988 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
989 |-------------+-----+-------------+--------+-------------+--------------|
990 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
991 |-------------+-----+-------------+--------+-------------+--------------|
993 GSB - GDBserver Stamp Bit
994 PC - Previous Counter
996 TBCI - Trace Buffer Control Index
999 An IPA update of `trace_buffer_ctrl_curr' does:
1001 - read CC from the current token, save as PC.
1003 - atomically tries to write PC+1,CC
1005 A GDBserver update of `trace_buffer_ctrl_curr' does:
1007 - reads PC and CC from the current token.
1012 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1013 for the counters described below. The cleared bits are used to
1014 hold the index of the items of the `trace_buffer_ctrl' array that
1016 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1018 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1019 counter, and the `current' counter. */
1021 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1022 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1024 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1025 always stamps this bit as set. */
1026 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1028 #ifdef IN_PROCESS_AGENT
1029 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1030 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1032 # define TRACE_BUFFER_CTRL_CURR \
1033 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1037 /* The GDBserver side agent only needs one instance of this object, as
1038 it doesn't need to sync with itself. Define it as array anyway so
1039 that the rest of the code base doesn't need to care for the
1041 struct trace_buffer_control trace_buffer_ctrl
[1];
1042 # define TRACE_BUFFER_CTRL_CURR 0
1045 /* These are convenience macros used to access the current trace
1046 buffer control in effect. */
1047 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1048 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1049 #define trace_buffer_end_free \
1050 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1051 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1054 /* Macro that returns a pointer to the first traceframe in the buffer. */
1056 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1058 /* Macro that returns a pointer to the next traceframe in the buffer.
1059 If the computed location is beyond the wraparound point, subtract
1060 the offset of the wraparound. */
1062 #define NEXT_TRACEFRAME_1(TF) \
1063 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1065 #define NEXT_TRACEFRAME(TF) \
1066 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1067 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1068 ? (trace_buffer_wrap - trace_buffer_lo) \
1071 /* The difference between these counters represents the total number
1072 of complete traceframes present in the trace buffer. The IP agent
1073 writes to the write count, GDBserver writes to read count. */
1075 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1076 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1078 /* Convenience macro. */
1080 #define traceframe_count \
1081 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1083 /* The count of all traceframes created in the current run, including
1084 ones that were discarded to make room. */
1086 IP_AGENT_EXPORT
int traceframes_created
;
1088 #ifndef IN_PROCESS_AGENT
1090 /* Read-only regions are address ranges whose contents don't change,
1091 and so can be read from target memory even while looking at a trace
1092 frame. Without these, disassembly for instance will likely fail,
1093 because the program code is not usually collected into a trace
1094 frame. This data structure does not need to be very complicated or
1095 particularly efficient, it's only going to be used occasionally,
1096 and only by some commands. */
1098 struct readonly_region
1100 /* The bounds of the region. */
1101 CORE_ADDR start
, end
;
1103 /* Link to the next one. */
1104 struct readonly_region
*next
;
1107 /* Linked list of readonly regions. This list stays in effect from
1108 one tstart to the next. */
1110 static struct readonly_region
*readonly_regions
;
1114 /* The global that controls tracing overall. */
1116 IP_AGENT_EXPORT
int tracing
;
1118 #ifndef IN_PROCESS_AGENT
1120 /* Controls whether tracing should continue after GDB disconnects. */
1122 int disconnected_tracing
;
1124 /* The reason for the last tracing run to have stopped. We initialize
1125 to a distinct string so that GDB can distinguish between "stopped
1126 after running" and "stopped because never run" cases. */
1128 static const char *tracing_stop_reason
= "tnotrun";
1130 static int tracing_stop_tpnum
;
1134 /* Functions local to this file. */
1136 /* Base "class" for tracepoint type specific data to be passed down to
1137 collect_data_at_tracepoint. */
1138 struct tracepoint_hit_ctx
1140 enum tracepoint_type type
;
1143 #ifdef IN_PROCESS_AGENT
1145 /* Fast/jump tracepoint specific data to be passed down to
1146 collect_data_at_tracepoint. */
1147 struct fast_tracepoint_ctx
1149 struct tracepoint_hit_ctx base
;
1151 struct regcache regcache
;
1152 int regcache_initted
;
1153 unsigned char *regspace
;
1155 unsigned char *regs
;
1156 struct tracepoint
*tpoint
;
1159 /* Static tracepoint specific data to be passed down to
1160 collect_data_at_tracepoint. */
1161 struct static_tracepoint_ctx
1163 struct tracepoint_hit_ctx base
;
1165 /* The regcache corresponding to the registers state at the time of
1166 the tracepoint hit. Initialized lazily, from REGS. */
1167 struct regcache regcache
;
1168 int regcache_initted
;
1170 /* The buffer space REGCACHE above uses. We use a separate buffer
1171 instead of letting the regcache malloc for both signal safety and
1172 performance reasons; this is allocated on the stack instead. */
1173 unsigned char *regspace
;
1175 /* The register buffer as passed on by lttng/ust. */
1176 struct registers
*regs
;
1178 /* The "printf" formatter and the args the user passed to the marker
1179 call. We use this to be able to collect "static trace data"
1184 /* The GDB tracepoint matching the probed marker that was "hit". */
1185 struct tracepoint
*tpoint
;
1190 /* Static tracepoint specific data to be passed down to
1191 collect_data_at_tracepoint. */
1192 struct trap_tracepoint_ctx
1194 struct tracepoint_hit_ctx base
;
1196 struct regcache
*regcache
;
1201 #ifndef IN_PROCESS_AGENT
1202 static struct agent_expr
*parse_agent_expr (char **actparm
);
1203 static char *unparse_agent_expr (struct agent_expr
*aexpr
);
1205 static enum eval_result_type
eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1206 struct traceframe
*tframe
,
1207 struct agent_expr
*aexpr
,
1210 static int agent_mem_read (struct traceframe
*tframe
,
1211 unsigned char *to
, CORE_ADDR from
, ULONGEST len
);
1212 static int agent_tsv_read (struct traceframe
*tframe
, int n
);
1214 #ifndef IN_PROCESS_AGENT
1215 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1216 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1219 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1220 struct tracepoint
*tpoint
);
1222 #ifndef IN_PROCESS_AGENT
1223 static void clear_readonly_regions (void);
1224 static void clear_installed_tracepoints (void);
1227 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1229 struct tracepoint
*tpoint
);
1230 #ifndef IN_PROCESS_AGENT
1231 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1233 struct tracepoint
*tpoint
, int current_step
);
1234 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1235 CORE_ADDR
*jump_entry
);
1237 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1239 struct tracepoint
*tpoint
,
1240 struct traceframe
*tframe
,
1241 struct tracepoint_action
*taction
);
1243 #ifndef IN_PROCESS_AGENT
1244 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1247 #if defined(__GNUC__)
1248 # define memory_barrier() asm volatile ("" : : : "memory")
1250 # define memory_barrier() do {} while (0)
1253 /* We only build the IPA if this builtin is supported, and there are
1254 no uses of this in GDBserver itself, so we're safe in defining this
1256 #define cmpxchg(mem, oldval, newval) \
1257 __sync_val_compare_and_swap (mem, oldval, newval)
1259 /* The size in bytes of the buffer used to talk to the IPA helper
1261 #define CMD_BUF_SIZE 1024
1263 /* Record that an error occurred during expression evaluation. */
1266 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1267 enum eval_result_type rtype
)
1269 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1270 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1272 #ifdef IN_PROCESS_AGENT
1273 /* Only record the first error we get. */
1274 if (cmpxchg (&expr_eval_result
,
1276 rtype
) != expr_eval_no_error
)
1279 if (expr_eval_result
!= expr_eval_no_error
)
1283 error_tracepoint
= tpoint
;
1286 /* Trace buffer management. */
1289 clear_trace_buffer (void)
1291 trace_buffer_start
= trace_buffer_lo
;
1292 trace_buffer_free
= trace_buffer_lo
;
1293 trace_buffer_end_free
= trace_buffer_hi
;
1294 trace_buffer_wrap
= trace_buffer_hi
;
1295 /* A traceframe with zeroed fields marks the end of trace data. */
1296 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1297 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1298 traceframe_read_count
= traceframe_write_count
= 0;
1299 traceframes_created
= 0;
1302 #ifndef IN_PROCESS_AGENT
1305 clear_inferior_trace_buffer (void)
1307 CORE_ADDR ipa_trace_buffer_lo
;
1308 CORE_ADDR ipa_trace_buffer_hi
;
1309 struct traceframe ipa_traceframe
= { 0 };
1310 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1312 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1313 &ipa_trace_buffer_lo
);
1314 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1315 &ipa_trace_buffer_hi
);
1317 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1318 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1319 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1320 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1322 /* A traceframe with zeroed fields marks the end of trace data. */
1323 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1324 (unsigned char *) &ipa_trace_buffer_ctrl
,
1325 sizeof (ipa_trace_buffer_ctrl
));
1327 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1329 /* A traceframe with zeroed fields marks the end of trace data. */
1330 write_inferior_memory (ipa_trace_buffer_lo
,
1331 (unsigned char *) &ipa_traceframe
,
1332 sizeof (ipa_traceframe
));
1334 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1335 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1336 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1342 init_trace_buffer (unsigned char *buf
, int bufsize
)
1344 trace_buffer_lo
= buf
;
1345 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1347 clear_trace_buffer ();
1350 #ifdef IN_PROCESS_AGENT
1352 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1353 about_to_request_buffer_space (void)
1355 /* GDBserver places breakpoint here while it goes about to flush
1356 data at random times. */
1357 UNKNOWN_SIDE_EFFECTS();
1362 /* Carve out a piece of the trace buffer, returning NULL in case of
1366 trace_buffer_alloc (size_t amt
)
1368 unsigned char *rslt
;
1369 struct trace_buffer_control
*tbctrl
;
1371 #ifdef IN_PROCESS_AGENT
1372 unsigned int prev
, prev_filtered
;
1373 unsigned int commit_count
;
1374 unsigned int commit
;
1375 unsigned int readout
;
1377 struct traceframe
*oldest
;
1378 unsigned char *new_start
;
1381 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1382 (long) amt
, (long) sizeof (struct traceframe
));
1384 /* Account for the EOB marker. */
1385 amt
+= sizeof (struct traceframe
);
1387 #ifdef IN_PROCESS_AGENT
1391 /* Read the current token and extract the index to try to write to,
1392 storing it in CURR. */
1393 prev
= trace_buffer_ctrl_curr
;
1394 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1395 curr
= prev_filtered
+ 1;
1399 about_to_request_buffer_space ();
1401 /* Start out with a copy of the current state. GDBserver may be
1402 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1403 be able to commit anyway if that happens. */
1404 trace_buffer_ctrl
[curr
]
1405 = trace_buffer_ctrl
[prev_filtered
];
1406 trace_debug ("trying curr=%u", curr
);
1408 /* The GDBserver's agent doesn't need all that syncing, and always
1409 updates TCB 0 (there's only one, mind you). */
1412 tbctrl
= &trace_buffer_ctrl
[curr
];
1414 /* Offsets are easier to grok for debugging than raw addresses,
1415 especially for the small trace buffer sizes that are useful for
1417 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1419 (int) (tbctrl
->start
- trace_buffer_lo
),
1420 (int) (tbctrl
->free
- trace_buffer_lo
),
1421 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1422 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1423 (int) (trace_buffer_hi
- trace_buffer_lo
));
1425 /* The algorithm here is to keep trying to get a contiguous block of
1426 the requested size, possibly discarding older traceframes to free
1427 up space. Since free space might come in one or two pieces,
1428 depending on whether discarded traceframes wrapped around at the
1429 high end of the buffer, we test both pieces after each
1433 /* First, if we have two free parts, try the upper one first. */
1434 if (tbctrl
->end_free
< tbctrl
->free
)
1436 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1437 /* We have enough in the upper part. */
1441 /* Our high part of free space wasn't enough. Give up
1442 on it for now, set wraparound. We will recover the
1443 space later, if/when the wrapped-around traceframe is
1445 trace_debug ("Upper part too small, setting wraparound");
1446 tbctrl
->wrap
= tbctrl
->free
;
1447 tbctrl
->free
= trace_buffer_lo
;
1451 /* The normal case. */
1452 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1455 #ifdef IN_PROCESS_AGENT
1456 /* The IP Agent's buffer is always circular. It isn't used
1457 currently, but `circular_trace_buffer' could represent
1458 GDBserver's mode. If we didn't find space, ask GDBserver to
1461 flush_trace_buffer ();
1465 trace_debug ("gdbserver flushed buffer, retrying");
1469 /* GDBserver cancelled the tracing. Bail out as well. */
1472 /* If we're here, then neither part is big enough, and
1473 non-circular trace buffers are now full. */
1474 if (!circular_trace_buffer
)
1476 trace_debug ("Not enough space in the trace buffer");
1480 trace_debug ("Need more space in the trace buffer");
1482 /* If we have a circular buffer, we can try discarding the
1483 oldest traceframe and see if that helps. */
1484 oldest
= FIRST_TRACEFRAME ();
1485 if (oldest
->tpnum
== 0)
1487 /* Not good; we have no traceframes to free. Perhaps we're
1488 asking for a block that is larger than the buffer? In
1489 any case, give up. */
1490 trace_debug ("No traceframes to discard");
1494 /* We don't run this code in the in-process agent currently.
1495 E.g., we could leave the in-process agent in autonomous
1496 circular mode if we only have fast tracepoints. If we do
1497 that, then this bit becomes racy with GDBserver, which also
1498 writes to this counter. */
1499 --traceframe_write_count
;
1501 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1502 /* If we freed the traceframe that wrapped around, go back
1503 to the non-wrap case. */
1504 if (new_start
< tbctrl
->start
)
1506 trace_debug ("Discarding past the wraparound");
1507 tbctrl
->wrap
= trace_buffer_hi
;
1509 tbctrl
->start
= new_start
;
1510 tbctrl
->end_free
= tbctrl
->start
;
1512 trace_debug ("Discarded a traceframe\n"
1513 "Trace buffer [%d], start=%d free=%d "
1514 "endfree=%d wrap=%d hi=%d",
1516 (int) (tbctrl
->start
- trace_buffer_lo
),
1517 (int) (tbctrl
->free
- trace_buffer_lo
),
1518 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1519 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1520 (int) (trace_buffer_hi
- trace_buffer_lo
));
1522 /* Now go back around the loop. The discard might have resulted
1523 in either one or two pieces of free space, so we want to try
1524 both before freeing any more traceframes. */
1528 /* If we get here, we know we can provide the asked-for space. */
1530 rslt
= tbctrl
->free
;
1532 /* Adjust the request back down, now that we know we have space for
1533 the marker, but don't commit to AMT yet, we may still need to
1534 restart the operation if GDBserver touches the trace buffer
1535 (obviously only important in the in-process agent's version). */
1536 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1538 /* Or not. If GDBserver changed the trace buffer behind our back,
1539 we get to restart a new allocation attempt. */
1541 #ifdef IN_PROCESS_AGENT
1542 /* Build the tentative token. */
1543 commit_count
= (((prev
& 0x0007ff00) + 0x100) & 0x0007ff00);
1544 commit
= (((prev
& 0x0007ff00) << 12)
1548 /* Try to commit it. */
1549 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1550 if (readout
!= prev
)
1552 trace_debug ("GDBserver has touched the trace buffer, restarting."
1553 " (prev=%08x, commit=%08x, readout=%08x)",
1554 prev
, commit
, readout
);
1558 /* Hold your horses here. Even if that change was committed,
1559 GDBserver could come in, and clobber it. We need to hold to be
1560 able to tell if GDBserver clobbers before or after we committed
1561 the change. Whenever GDBserver goes about touching the IPA
1562 buffer, it sets a breakpoint in this routine, so we have a sync
1564 about_to_request_buffer_space ();
1566 /* Check if the change has been effective, even if GDBserver stopped
1567 us at the breakpoint. */
1570 unsigned int refetch
;
1574 refetch
= trace_buffer_ctrl_curr
;
1576 if ((refetch
== commit
1577 || ((refetch
& 0x7ff00000) >> 12) == commit_count
))
1580 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1581 "readout=%08x, refetch=%08x)",
1582 prev
, commit
, readout
, refetch
);
1586 trace_debug ("GDBserver has touched the trace buffer, not effective."
1587 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1588 prev
, commit
, readout
, refetch
);
1594 /* We have a new piece of the trace buffer. Hurray! */
1596 /* Add an EOB marker just past this allocation. */
1597 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1598 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1600 /* Adjust the request back down, now that we know we have space for
1602 amt
-= sizeof (struct traceframe
);
1606 trace_debug ("Allocated %d bytes", (int) amt
);
1607 trace_debug ("Trace buffer [%d] start=%d free=%d "
1608 "endfree=%d wrap=%d hi=%d",
1610 (int) (tbctrl
->start
- trace_buffer_lo
),
1611 (int) (tbctrl
->free
- trace_buffer_lo
),
1612 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1613 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1614 (int) (trace_buffer_hi
- trace_buffer_lo
));
1620 #ifndef IN_PROCESS_AGENT
1622 /* Return the total free space. This is not necessarily the largest
1623 block we can allocate, because of the two-part case. */
1628 if (trace_buffer_free
<= trace_buffer_end_free
)
1629 return trace_buffer_end_free
- trace_buffer_free
;
1631 return ((trace_buffer_end_free
- trace_buffer_lo
)
1632 + (trace_buffer_hi
- trace_buffer_free
));
1635 /* An 'S' in continuation packets indicates remainder are for
1638 static int seen_step_action_flag
;
1640 /* Create a tracepoint (location) with given number and address. */
1642 static struct tracepoint
*
1643 add_tracepoint (int num
, CORE_ADDR addr
)
1645 struct tracepoint
*tpoint
;
1647 tpoint
= xmalloc (sizeof (struct tracepoint
));
1648 tpoint
->number
= num
;
1649 tpoint
->address
= addr
;
1650 tpoint
->numactions
= 0;
1651 tpoint
->actions
= NULL
;
1652 tpoint
->actions_str
= NULL
;
1653 tpoint
->cond
= NULL
;
1654 tpoint
->num_step_actions
= 0;
1655 tpoint
->step_actions
= NULL
;
1656 tpoint
->step_actions_str
= NULL
;
1657 /* Start all off as regular (slow) tracepoints. */
1658 tpoint
->type
= trap_tracepoint
;
1659 tpoint
->orig_size
= -1;
1660 tpoint
->source_strings
= NULL
;
1661 tpoint
->compiled_cond
= 0;
1662 tpoint
->handle
= NULL
;
1663 tpoint
->next
= NULL
;
1665 if (!last_tracepoint
)
1666 tracepoints
= tpoint
;
1668 last_tracepoint
->next
= tpoint
;
1669 last_tracepoint
= tpoint
;
1671 seen_step_action_flag
= 0;
1676 #ifndef IN_PROCESS_AGENT
1678 /* Return the tracepoint with the given number and address, or NULL. */
1680 static struct tracepoint
*
1681 find_tracepoint (int id
, CORE_ADDR addr
)
1683 struct tracepoint
*tpoint
;
1685 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1686 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1692 /* There may be several tracepoints with the same number (because they
1693 are "locations", in GDB parlance); return the next one after the
1694 given tracepoint, or search from the beginning of the list if the
1695 first argument is NULL. */
1697 static struct tracepoint
*
1698 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1700 struct tracepoint
*tpoint
;
1703 tpoint
= prev_tp
->next
;
1705 tpoint
= tracepoints
;
1706 for (; tpoint
; tpoint
= tpoint
->next
)
1707 if (tpoint
->number
== num
)
1716 save_string (const char *str
, size_t len
)
1720 s
= xmalloc (len
+ 1);
1721 memcpy (s
, str
, len
);
1727 /* Append another action to perform when the tracepoint triggers. */
1730 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1736 seen_step_action_flag
= 1;
1744 char *act_start
= act
;
1745 struct tracepoint_action
*action
= NULL
;
1751 struct collect_memory_action
*maction
;
1755 maction
= xmalloc (sizeof *maction
);
1756 maction
->base
.type
= *act
;
1757 action
= &maction
->base
;
1760 is_neg
= (*act
== '-');
1763 act
= unpack_varlen_hex (act
, &basereg
);
1765 act
= unpack_varlen_hex (act
, &maction
->addr
);
1767 act
= unpack_varlen_hex (act
, &maction
->len
);
1768 maction
->basereg
= (is_neg
1771 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1772 pulongest (maction
->len
),
1773 paddress (maction
->addr
), maction
->basereg
);
1778 struct collect_registers_action
*raction
;
1780 raction
= xmalloc (sizeof *raction
);
1781 raction
->base
.type
= *act
;
1782 action
= &raction
->base
;
1784 trace_debug ("Want to collect registers");
1786 /* skip past hex digits of mask for now */
1787 while (isxdigit(*act
))
1793 struct collect_static_trace_data_action
*raction
;
1795 raction
= xmalloc (sizeof *raction
);
1796 raction
->base
.type
= *act
;
1797 action
= &raction
->base
;
1799 trace_debug ("Want to collect static trace data");
1804 trace_debug ("Unexpected step action, ignoring");
1809 struct eval_expr_action
*xaction
;
1811 xaction
= xmalloc (sizeof (*xaction
));
1812 xaction
->base
.type
= *act
;
1813 action
= &xaction
->base
;
1815 trace_debug ("Want to evaluate expression");
1816 xaction
->expr
= parse_agent_expr (&act
);
1820 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1829 if (seen_step_action_flag
)
1831 tpoint
->num_step_actions
++;
1833 tpoint
->step_actions
1834 = xrealloc (tpoint
->step_actions
,
1835 (sizeof (*tpoint
->step_actions
)
1836 * tpoint
->num_step_actions
));
1837 tpoint
->step_actions_str
1838 = xrealloc (tpoint
->step_actions_str
,
1839 (sizeof (*tpoint
->step_actions_str
)
1840 * tpoint
->num_step_actions
));
1841 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1842 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1843 = save_string (act_start
, act
- act_start
);
1847 tpoint
->numactions
++;
1849 = xrealloc (tpoint
->actions
,
1850 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1852 = xrealloc (tpoint
->actions_str
,
1853 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1854 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1855 tpoint
->actions_str
[tpoint
->numactions
- 1]
1856 = save_string (act_start
, act
- act_start
);
1863 /* Find or create a trace state variable with the given number. */
1865 static struct trace_state_variable
*
1866 get_trace_state_variable (int num
)
1868 struct trace_state_variable
*tsv
;
1870 #ifdef IN_PROCESS_AGENT
1871 /* Search for an existing variable. */
1872 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1873 if (tsv
->number
== num
)
1877 /* Search for an existing variable. */
1878 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1879 if (tsv
->number
== num
)
1885 /* Find or create a trace state variable with the given number. */
1887 static struct trace_state_variable
*
1888 create_trace_state_variable (int num
, int gdb
)
1890 struct trace_state_variable
*tsv
;
1892 tsv
= get_trace_state_variable (num
);
1896 /* Create a new variable. */
1897 tsv
= xmalloc (sizeof (struct trace_state_variable
));
1899 tsv
->initial_value
= 0;
1903 #ifdef IN_PROCESS_AGENT
1906 tsv
->next
= alloced_trace_state_variables
;
1907 alloced_trace_state_variables
= tsv
;
1912 tsv
->next
= trace_state_variables
;
1913 trace_state_variables
= tsv
;
1918 IP_AGENT_EXPORT LONGEST
1919 get_trace_state_variable_value (int num
)
1921 struct trace_state_variable
*tsv
;
1923 tsv
= get_trace_state_variable (num
);
1927 trace_debug ("No trace state variable %d, skipping value get", num
);
1931 /* Call a getter function if we have one. While it's tempting to
1932 set up something to only call the getter once per tracepoint hit,
1933 it could run afoul of thread races. Better to let the getter
1934 handle it directly, if necessary to worry about it. */
1936 tsv
->value
= (tsv
->getter
) ();
1938 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1939 num
, plongest (tsv
->value
));
1944 IP_AGENT_EXPORT
void
1945 set_trace_state_variable_value (int num
, LONGEST val
)
1947 struct trace_state_variable
*tsv
;
1949 tsv
= get_trace_state_variable (num
);
1953 trace_debug ("No trace state variable %d, skipping value set", num
);
1961 set_trace_state_variable_name (int num
, const char *name
)
1963 struct trace_state_variable
*tsv
;
1965 tsv
= get_trace_state_variable (num
);
1969 trace_debug ("No trace state variable %d, skipping name set", num
);
1973 tsv
->name
= (char *) name
;
1977 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
1979 struct trace_state_variable
*tsv
;
1981 tsv
= get_trace_state_variable (num
);
1985 trace_debug ("No trace state variable %d, skipping getter set", num
);
1989 tsv
->getter
= getter
;
1992 /* Add a raw traceframe for the given tracepoint. */
1994 static struct traceframe
*
1995 add_traceframe (struct tracepoint
*tpoint
)
1997 struct traceframe
*tframe
;
1999 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2004 tframe
->tpnum
= tpoint
->number
;
2005 tframe
->data_size
= 0;
2010 /* Add a block to the traceframe currently being worked on. */
2012 static unsigned char *
2013 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2015 unsigned char *block
;
2020 block
= trace_buffer_alloc (amt
);
2025 tframe
->data_size
+= amt
;
2030 /* Flag that the current traceframe is finished. */
2033 finish_traceframe (struct traceframe
*tframe
)
2035 ++traceframe_write_count
;
2036 ++traceframes_created
;
2039 #ifndef IN_PROCESS_AGENT
2041 /* Given a traceframe number NUM, find the NUMth traceframe in the
2044 static struct traceframe
*
2045 find_traceframe (int num
)
2047 struct traceframe
*tframe
;
2050 for (tframe
= FIRST_TRACEFRAME ();
2052 tframe
= NEXT_TRACEFRAME (tframe
))
2063 get_traceframe_address (struct traceframe
*tframe
)
2066 struct tracepoint
*tpoint
;
2068 addr
= traceframe_get_pc (tframe
);
2073 /* Fallback strategy, will be incorrect for while-stepping frames
2074 and multi-location tracepoints. */
2075 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2076 return tpoint
->address
;
2079 /* Search for the next traceframe whose address is inside or outside
2082 static struct traceframe
*
2083 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2086 struct traceframe
*tframe
;
2089 *tfnump
= current_traceframe
+ 1;
2090 tframe
= find_traceframe (*tfnump
);
2091 /* The search is not supposed to wrap around. */
2098 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2100 tfaddr
= get_traceframe_address (tframe
);
2102 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2103 : (lo
> tfaddr
|| tfaddr
> hi
))
2112 /* Search for the next traceframe recorded by the given tracepoint.
2113 Note that for multi-location tracepoints, this will find whatever
2114 location appears first. */
2116 static struct traceframe
*
2117 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2119 struct traceframe
*tframe
;
2121 *tfnump
= current_traceframe
+ 1;
2122 tframe
= find_traceframe (*tfnump
);
2123 /* The search is not supposed to wrap around. */
2130 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2132 if (tframe
->tpnum
== num
)
2143 #ifndef IN_PROCESS_AGENT
2145 /* Clear all past trace state. */
2148 cmd_qtinit (char *packet
)
2150 struct trace_state_variable
*tsv
, *prev
, *next
;
2152 /* Make sure we don't try to read from a trace frame. */
2153 current_traceframe
= -1;
2155 trace_debug ("Initializing the trace");
2157 clear_installed_tracepoints ();
2158 clear_readonly_regions ();
2161 last_tracepoint
= NULL
;
2163 /* Clear out any leftover trace state variables. Ones with target
2164 defined getters should be kept however. */
2166 tsv
= trace_state_variables
;
2169 trace_debug ("Looking at var %d", tsv
->number
);
2170 if (tsv
->getter
== NULL
)
2176 trace_state_variables
= next
;
2177 trace_debug ("Deleting var %d", tsv
->number
);
2188 clear_trace_buffer ();
2189 clear_inferior_trace_buffer ();
2194 /* Unprobe the UST marker at ADDRESS. */
2197 unprobe_marker_at (CORE_ADDR address
)
2199 char cmd
[CMD_BUF_SIZE
];
2201 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2202 run_inferior_command (cmd
);
2205 /* Restore the program to its pre-tracing state. This routine may be called
2206 in error situations, so it needs to be careful about only restoring
2207 from known-valid bits. */
2210 clear_installed_tracepoints (void)
2212 struct tracepoint
*tpoint
;
2213 struct tracepoint
*prev_stpoint
;
2216 cancel_breakpoints ();
2218 prev_stpoint
= NULL
;
2220 /* Restore any bytes overwritten by tracepoints. */
2221 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2223 /* Catch the case where we might try to remove a tracepoint that
2224 was never actually installed. */
2225 if (tpoint
->handle
== NULL
)
2227 trace_debug ("Tracepoint %d at 0x%s was "
2228 "never installed, nothing to clear",
2229 tpoint
->number
, paddress (tpoint
->address
));
2233 switch (tpoint
->type
)
2235 case trap_tracepoint
:
2236 delete_breakpoint (tpoint
->handle
);
2238 case fast_tracepoint
:
2239 delete_fast_tracepoint_jump (tpoint
->handle
);
2241 case static_tracepoint
:
2242 if (prev_stpoint
!= NULL
2243 && prev_stpoint
->address
== tpoint
->address
)
2244 /* Nothing to do. We already unprobed a tracepoint set at
2245 this marker address (and there can only be one probe
2250 unprobe_marker_at (tpoint
->address
);
2251 prev_stpoint
= tpoint
;
2256 tpoint
->handle
= NULL
;
2262 /* Parse a packet that defines a tracepoint. */
2265 cmd_qtdp (char *own_buf
)
2271 struct tracepoint
*tpoint
;
2273 char *packet
= own_buf
;
2275 packet
+= strlen ("QTDP:");
2277 /* A hyphen at the beginning marks a packet specifying actions for a
2278 tracepoint already supplied. */
2285 packet
= unpack_varlen_hex (packet
, &num
);
2286 ++packet
; /* skip a colon */
2287 packet
= unpack_varlen_hex (packet
, &addr
);
2288 ++packet
; /* skip a colon */
2290 /* See if we already have this tracepoint. */
2291 tpoint
= find_tracepoint (num
, addr
);
2295 /* Duplicate tracepoints are never allowed. */
2298 trace_debug ("Tracepoint error: tracepoint %d"
2299 " at 0x%s already exists",
2300 (int) num
, paddress (addr
));
2301 write_enn (own_buf
);
2305 tpoint
= add_tracepoint (num
, addr
);
2307 tpoint
->enabled
= (*packet
== 'E');
2308 ++packet
; /* skip 'E' */
2309 ++packet
; /* skip a colon */
2310 packet
= unpack_varlen_hex (packet
, &count
);
2311 tpoint
->step_count
= count
;
2312 ++packet
; /* skip a colon */
2313 packet
= unpack_varlen_hex (packet
, &count
);
2314 tpoint
->pass_count
= count
;
2315 /* See if we have any of the additional optional fields. */
2316 while (*packet
== ':')
2321 tpoint
->type
= fast_tracepoint
;
2323 packet
= unpack_varlen_hex (packet
, &count
);
2324 tpoint
->orig_size
= count
;
2326 else if (*packet
== 'S')
2328 tpoint
->type
= static_tracepoint
;
2331 else if (*packet
== 'X')
2333 actparm
= (char *) packet
;
2334 tpoint
->cond
= parse_agent_expr (&actparm
);
2337 else if (*packet
== '-')
2339 else if (*packet
== '\0')
2342 trace_debug ("Unknown optional tracepoint field");
2345 trace_debug ("Also has actions\n");
2347 trace_debug ("Defined %stracepoint %d at 0x%s, "
2348 "enabled %d step %ld pass %ld",
2349 tpoint
->type
== fast_tracepoint
? "fast "
2351 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2352 tpoint
->step_count
, tpoint
->pass_count
);
2355 add_tracepoint_action (tpoint
, packet
);
2358 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2359 (int) num
, paddress (addr
));
2360 write_enn (own_buf
);
2368 cmd_qtdpsrc (char *own_buf
)
2370 ULONGEST num
, addr
, start
, slen
;
2371 struct tracepoint
*tpoint
;
2372 char *packet
= own_buf
;
2373 char *saved
, *srctype
, *src
;
2375 struct source_string
*last
, *newlast
;
2377 packet
+= strlen ("QTDPsrc:");
2379 packet
= unpack_varlen_hex (packet
, &num
);
2380 ++packet
; /* skip a colon */
2381 packet
= unpack_varlen_hex (packet
, &addr
);
2382 ++packet
; /* skip a colon */
2384 /* See if we already have this tracepoint. */
2385 tpoint
= find_tracepoint (num
, addr
);
2389 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2390 (int) num
, paddress (addr
));
2391 write_enn (own_buf
);
2396 packet
= strchr (packet
, ':');
2397 srctype
= xmalloc (packet
- saved
+ 1);
2398 memcpy (srctype
, saved
, packet
- saved
);
2399 srctype
[packet
- saved
] = '\0';
2401 packet
= unpack_varlen_hex (packet
, &start
);
2402 ++packet
; /* skip a colon */
2403 packet
= unpack_varlen_hex (packet
, &slen
);
2404 ++packet
; /* skip a colon */
2405 src
= xmalloc (slen
+ 1);
2406 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2409 newlast
= xmalloc (sizeof (struct source_string
));
2410 newlast
->type
= srctype
;
2412 newlast
->next
= NULL
;
2413 /* Always add a source string to the end of the list;
2414 this keeps sequences of actions/commands in the right
2416 if (tpoint
->source_strings
)
2418 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2420 last
->next
= newlast
;
2423 tpoint
->source_strings
= newlast
;
2429 cmd_qtdv (char *own_buf
)
2431 ULONGEST num
, val
, builtin
;
2434 struct trace_state_variable
*tsv
;
2435 char *packet
= own_buf
;
2437 packet
+= strlen ("QTDV:");
2439 packet
= unpack_varlen_hex (packet
, &num
);
2440 ++packet
; /* skip a colon */
2441 packet
= unpack_varlen_hex (packet
, &val
);
2442 ++packet
; /* skip a colon */
2443 packet
= unpack_varlen_hex (packet
, &builtin
);
2444 ++packet
; /* skip a colon */
2446 nbytes
= strlen (packet
) / 2;
2447 varname
= xmalloc (nbytes
+ 1);
2448 nbytes
= unhexify (varname
, packet
, nbytes
);
2449 varname
[nbytes
] = '\0';
2451 tsv
= create_trace_state_variable (num
, 1);
2452 tsv
->initial_value
= (LONGEST
) val
;
2453 tsv
->name
= varname
;
2455 set_trace_state_variable_value (num
, (LONGEST
) val
);
2461 cmd_qtenable_disable (char *own_buf
, int enable
)
2463 char *packet
= own_buf
;
2465 struct tracepoint
*tp
;
2467 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2468 packet
= unpack_varlen_hex (packet
, &num
);
2469 ++packet
; /* skip a colon */
2470 packet
= unpack_varlen_hex (packet
, &addr
);
2472 tp
= find_tracepoint (num
, addr
);
2476 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2478 trace_debug ("Tracepoint %d at 0x%s is already %s",
2479 (int) num
, paddress (addr
),
2480 enable
? "enabled" : "disabled");
2485 trace_debug ("%s tracepoint %d at 0x%s",
2486 enable
? "Enabling" : "Disabling",
2487 (int) num
, paddress (addr
));
2489 tp
->enabled
= enable
;
2491 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2494 int offset
= offsetof (struct tracepoint
, enabled
);
2495 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2497 ret
= prepare_to_access_memory ();
2500 trace_debug ("Failed to temporarily stop inferior threads");
2501 write_enn (own_buf
);
2505 ret
= write_inferior_integer (obj_addr
, enable
);
2506 done_accessing_memory ();
2510 trace_debug ("Cannot write enabled flag into "
2511 "inferior process memory");
2512 write_enn (own_buf
);
2521 trace_debug ("Tracepoint %d at 0x%s not found",
2522 (int) num
, paddress (addr
));
2523 write_enn (own_buf
);
2528 cmd_qtv (char *own_buf
)
2533 char *packet
= own_buf
;
2535 packet
+= strlen ("qTV:");
2536 unpack_varlen_hex (packet
, &num
);
2538 if (current_traceframe
>= 0)
2540 err
= traceframe_read_tsv ((int) num
, &val
);
2543 strcpy (own_buf
, "U");
2547 /* Only make tsv's be undefined before the first trace run. After a
2548 trace run is over, the user might want to see the last value of
2549 the tsv, and it might not be available in a traceframe. */
2550 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2552 strcpy (own_buf
, "U");
2556 val
= get_trace_state_variable_value (num
);
2558 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2561 /* Clear out the list of readonly regions. */
2564 clear_readonly_regions (void)
2566 struct readonly_region
*roreg
;
2568 while (readonly_regions
)
2570 roreg
= readonly_regions
;
2571 readonly_regions
= readonly_regions
->next
;
2576 /* Parse the collection of address ranges whose contents GDB believes
2577 to be unchanging and so can be read directly from target memory
2578 even while looking at a traceframe. */
2581 cmd_qtro (char *own_buf
)
2583 ULONGEST start
, end
;
2584 struct readonly_region
*roreg
;
2585 char *packet
= own_buf
;
2587 trace_debug ("Want to mark readonly regions");
2589 clear_readonly_regions ();
2591 packet
+= strlen ("QTro");
2593 while (*packet
== ':')
2595 ++packet
; /* skip a colon */
2596 packet
= unpack_varlen_hex (packet
, &start
);
2597 ++packet
; /* skip a comma */
2598 packet
= unpack_varlen_hex (packet
, &end
);
2599 roreg
= xmalloc (sizeof (struct readonly_region
));
2600 roreg
->start
= start
;
2602 roreg
->next
= readonly_regions
;
2603 readonly_regions
= roreg
;
2604 trace_debug ("Added readonly region from 0x%s to 0x%s",
2605 paddress (roreg
->start
), paddress (roreg
->end
));
2611 /* Test to see if the given range is in our list of readonly ranges.
2612 We only test for being entirely within a range, GDB is not going to
2613 send a single memory packet that spans multiple regions. */
2616 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2618 struct readonly_region
*roreg
;
2620 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2621 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2627 /* The maximum size of a jump pad entry. */
2628 static const int max_jump_pad_size
= 0x100;
2630 static CORE_ADDR gdb_jump_pad_head
;
2632 /* Return the address of the next free jump space. */
2635 get_jump_space_head (void)
2637 if (gdb_jump_pad_head
== 0)
2639 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2640 &gdb_jump_pad_head
))
2641 fatal ("error extracting jump_pad_buffer");
2644 return gdb_jump_pad_head
;
2647 /* Reserve USED bytes from the jump space. */
2650 claim_jump_space (ULONGEST used
)
2652 trace_debug ("claim_jump_space reserves %s bytes at %s",
2653 pulongest (used
), paddress (gdb_jump_pad_head
));
2654 gdb_jump_pad_head
+= used
;
2657 /* Sort tracepoints by PC, using a bubble sort. */
2660 sort_tracepoints (void)
2662 struct tracepoint
*lst
, *tmp
, *prev
= NULL
;
2665 if (tracepoints
== NULL
)
2669 for (tmp
= tracepoints
; tmp
->next
; tmp
= tmp
->next
)
2672 for (i
= 0; i
< n
- 1; i
++)
2673 for (j
= 0, lst
= tracepoints
;
2674 lst
&& lst
->next
&& (j
<= n
- 1 - i
);
2677 /* If we're at beginning, the start node is the prev
2682 /* Compare neighbors. */
2683 if (lst
->next
->address
< lst
->address
)
2685 struct tracepoint
*p
;
2688 tmp
= (lst
->next
? lst
->next
->next
: NULL
);
2690 if (j
== 0 && prev
== tracepoints
)
2691 tracepoints
= lst
->next
;
2694 prev
->next
= lst
->next
;
2695 lst
->next
->next
= lst
;
2702 /* Keep track of the previous node. We need it if we need
2710 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2711 the command fails, or 0 otherwise. If the command ran
2712 successfully, but probing the marker failed, ERROUT will be filled
2713 with the error to reply to GDB, and -1 is also returned. This
2714 allows directly passing IPA errors to GDB. */
2717 probe_marker_at (CORE_ADDR address
, char *errout
)
2719 char cmd
[CMD_BUF_SIZE
];
2722 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
2723 err
= run_inferior_command (cmd
);
2729 strcpy (errout
, cmd
);
2737 #define MAX_JUMP_SIZE 20
2740 cmd_qtstart (char *packet
)
2742 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
2743 CORE_ADDR jump_entry
;
2745 /* The jump to the jump pad of the last fast tracepoint
2747 unsigned char fjump
[MAX_JUMP_SIZE
];
2748 ULONGEST fjump_size
;
2750 trace_debug ("Starting the trace");
2752 /* Sort tracepoints by ascending address. This makes installing
2753 fast tracepoints at the same address easier to handle. */
2754 sort_tracepoints ();
2756 /* Pause all threads temporarily while we patch tracepoints. */
2759 /* Get threads out of jump pads. Safe to do here, since this is a
2760 top level command. And, required to do here, since we're
2761 deleting/rewriting jump pads. */
2763 stabilize_threads ();
2765 /* Freeze threads. */
2768 /* Sync the fast tracepoints list in the inferior ftlib. */
2769 if (in_process_agent_loaded ())
2771 download_tracepoints ();
2772 download_trace_state_variables ();
2775 /* No previous fast tpoint yet. */
2776 prev_ftpoint
= NULL
;
2778 /* No previous static tpoint yet. */
2779 prev_stpoint
= NULL
;
2783 /* Install tracepoints. */
2784 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2786 /* Ensure all the hit counts start at zero. */
2787 tpoint
->hit_count
= 0;
2789 if (tpoint
->type
== trap_tracepoint
)
2791 /* Tracepoints are installed as memory breakpoints. Just go
2792 ahead and install the trap. The breakpoints module
2793 handles duplicated breakpoints, and the memory read
2794 routine handles un-patching traps from memory reads. */
2795 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
2796 tracepoint_handler
);
2798 else if (tpoint
->type
== fast_tracepoint
)
2800 if (maybe_write_ipa_not_loaded (packet
))
2802 trace_debug ("Requested a fast tracepoint, but fast "
2803 "tracepoints aren't supported.");
2807 if (prev_ftpoint
!= NULL
&& prev_ftpoint
->address
== tpoint
->address
)
2809 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
,
2812 tpoint
->jump_pad
= prev_ftpoint
->jump_pad
;
2813 tpoint
->jump_pad_end
= prev_ftpoint
->jump_pad_end
;
2814 tpoint
->adjusted_insn_addr
= prev_ftpoint
->adjusted_insn_addr
;
2815 tpoint
->adjusted_insn_addr_end
2816 = prev_ftpoint
->adjusted_insn_addr_end
;
2823 prev_ftpoint
= NULL
;
2825 jentry
= jump_entry
= get_jump_space_head ();
2827 /* Install the jump pad. */
2828 err
= install_fast_tracepoint_jump_pad
2829 (tpoint
->obj_addr_on_target
,
2831 ipa_sym_addrs
.addr_gdb_collect
,
2832 ipa_sym_addrs
.addr_collecting
,
2836 &tpoint
->adjusted_insn_addr
,
2837 &tpoint
->adjusted_insn_addr_end
);
2841 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
,
2844 if (tpoint
->handle
!= NULL
)
2846 tpoint
->jump_pad
= jump_entry
;
2847 tpoint
->jump_pad_end
= jentry
;
2849 /* Pad to 8-byte alignment. */
2850 jentry
= ((jentry
+ 7) & ~0x7);
2851 claim_jump_space (jentry
- jump_entry
);
2853 /* So that we can handle multiple fast tracepoints
2854 at the same address easily. */
2855 prev_ftpoint
= tpoint
;
2859 else if (tpoint
->type
== static_tracepoint
)
2861 if (maybe_write_ipa_ust_not_loaded (packet
))
2863 trace_debug ("Requested a static tracepoint, but static "
2864 "tracepoints are not supported.");
2868 /* Can only probe a given marker once. */
2869 if (prev_stpoint
!= NULL
&& prev_stpoint
->address
== tpoint
->address
)
2871 tpoint
->handle
= (void *) -1;
2875 if (probe_marker_at (tpoint
->address
, packet
) == 0)
2877 tpoint
->handle
= (void *) -1;
2879 /* So that we can handle multiple static tracepoints
2880 at the same address easily. */
2881 prev_stpoint
= tpoint
;
2886 /* Any failure in the inner loop is sufficient cause to give
2888 if (tpoint
->handle
== NULL
)
2892 /* Any error in tracepoint insertion is unacceptable; better to
2893 address the problem now, than end up with a useless or misleading
2897 clear_installed_tracepoints ();
2898 if (*packet
== '\0')
2904 stopping_tracepoint
= NULL
;
2905 trace_buffer_is_full
= 0;
2906 expr_eval_result
= expr_eval_no_error
;
2907 error_tracepoint
= NULL
;
2909 /* Tracing is now active, hits will now start being logged. */
2912 if (in_process_agent_loaded ())
2914 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
2915 fatal ("Error setting tracing variable in lib");
2917 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
2919 fatal ("Error clearing stopping_tracepoint variable in lib");
2921 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
2922 fatal ("Error clearing trace_buffer_is_full variable in lib");
2924 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
2925 stop_tracing_handler
);
2926 if (stop_tracing_bkpt
== NULL
)
2927 error ("Error setting stop_tracing breakpoint");
2929 flush_trace_buffer_bkpt
2930 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
2931 flush_trace_buffer_handler
);
2932 if (flush_trace_buffer_bkpt
== NULL
)
2933 error ("Error setting flush_trace_buffer breakpoint");
2941 /* End a tracing run, filling in a stop reason to report back to GDB,
2942 and removing the tracepoints from the code. */
2949 trace_debug ("Tracing is already off, ignoring");
2953 trace_debug ("Stopping the trace");
2955 /* Pause all threads before removing fast jumps from memory,
2956 breakpoints, and touching IPA state variables (inferior memory).
2957 Some thread may hit the internal tracing breakpoints, or be
2958 collecting this moment, but that's ok, we don't release the
2959 tpoint object's memory or the jump pads here (we only do that
2960 when we're sure we can move all threads out of the jump pads).
2961 We can't now, since we may be getting here due to the inferior
2962 agent calling us. */
2964 /* Since we're removing breakpoints, cancel breakpoint hits,
2965 possibly related to the breakpoints we're about to delete. */
2966 cancel_breakpoints ();
2968 /* Stop logging. Tracepoints can still be hit, but they will not be
2971 if (in_process_agent_loaded ())
2973 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
2974 fatal ("Error clearing tracing variable in lib");
2977 tracing_stop_reason
= "t???";
2978 tracing_stop_tpnum
= 0;
2979 if (stopping_tracepoint
)
2981 trace_debug ("Stopping the trace because "
2982 "tracepoint %d was hit %ld times",
2983 stopping_tracepoint
->number
,
2984 stopping_tracepoint
->pass_count
);
2985 tracing_stop_reason
= "tpasscount";
2986 tracing_stop_tpnum
= stopping_tracepoint
->number
;
2988 else if (trace_buffer_is_full
)
2990 trace_debug ("Stopping the trace because the trace buffer is full");
2991 tracing_stop_reason
= "tfull";
2993 else if (expr_eval_result
!= expr_eval_no_error
)
2995 trace_debug ("Stopping the trace because of an expression eval error");
2996 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
2997 tracing_stop_tpnum
= error_tracepoint
->number
;
2999 #ifndef IN_PROCESS_AGENT
3000 else if (!gdb_connected ())
3002 trace_debug ("Stopping the trace because GDB disconnected");
3003 tracing_stop_reason
= "tdisconnected";
3008 trace_debug ("Stopping the trace because of a tstop command");
3009 tracing_stop_reason
= "tstop";
3012 stopping_tracepoint
= NULL
;
3013 error_tracepoint
= NULL
;
3015 /* Clear out the tracepoints. */
3016 clear_installed_tracepoints ();
3018 if (in_process_agent_loaded ())
3020 /* Pull in fast tracepoint trace frames from the inferior lib
3021 buffer into our buffer, even if our buffer is already full,
3022 because we want to present the full number of created frames
3023 in addition to what fit in the trace buffer. */
3024 upload_fast_traceframes ();
3027 if (stop_tracing_bkpt
!= NULL
)
3029 delete_breakpoint (stop_tracing_bkpt
);
3030 stop_tracing_bkpt
= NULL
;
3033 if (flush_trace_buffer_bkpt
!= NULL
)
3035 delete_breakpoint (flush_trace_buffer_bkpt
);
3036 flush_trace_buffer_bkpt
= NULL
;
3043 stop_tracing_handler (CORE_ADDR addr
)
3045 trace_debug ("lib hit stop_tracing");
3047 /* Don't actually handle it here. When we stop tracing we remove
3048 breakpoints from the inferior, and that is not allowed in a
3049 breakpoint handler (as the caller is walking the breakpoint
3055 flush_trace_buffer_handler (CORE_ADDR addr
)
3057 trace_debug ("lib hit flush_trace_buffer");
3062 cmd_qtstop (char *packet
)
3069 cmd_qtdisconnected (char *own_buf
)
3072 char *packet
= own_buf
;
3074 packet
+= strlen ("QTDisconnected:");
3076 unpack_varlen_hex (packet
, &setting
);
3080 disconnected_tracing
= setting
;
3084 cmd_qtframe (char *own_buf
)
3086 ULONGEST frame
, pc
, lo
, hi
, num
;
3088 struct traceframe
*tframe
;
3089 char *packet
= own_buf
;
3091 packet
+= strlen ("QTFrame:");
3093 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3095 packet
+= strlen ("pc:");
3096 unpack_varlen_hex (packet
, &pc
);
3097 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3098 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3100 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3102 packet
+= strlen ("range:");
3103 packet
= unpack_varlen_hex (packet
, &lo
);
3105 unpack_varlen_hex (packet
, &hi
);
3106 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3107 paddress (lo
), paddress (hi
));
3108 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3110 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3112 packet
+= strlen ("outside:");
3113 packet
= unpack_varlen_hex (packet
, &lo
);
3115 unpack_varlen_hex (packet
, &hi
);
3116 trace_debug ("Want to find next traceframe "
3117 "outside the range 0x%s to 0x%s",
3118 paddress (lo
), paddress (hi
));
3119 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3121 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3123 packet
+= strlen ("tdp:");
3124 unpack_varlen_hex (packet
, &num
);
3126 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3127 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3131 unpack_varlen_hex (packet
, &frame
);
3132 tfnum
= (int) frame
;
3135 trace_debug ("Want to stop looking at traceframes");
3136 current_traceframe
= -1;
3140 trace_debug ("Want to look at traceframe %d", tfnum
);
3141 tframe
= find_traceframe (tfnum
);
3146 current_traceframe
= tfnum
;
3147 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3150 sprintf (own_buf
, "F-1");
3154 cmd_qtstatus (char *packet
)
3156 char *stop_reason_rsp
= NULL
;
3158 trace_debug ("Returning trace status as %d, stop reason %s",
3159 tracing
, tracing_stop_reason
);
3161 if (in_process_agent_loaded ())
3165 upload_fast_traceframes ();
3170 stop_reason_rsp
= (char *) tracing_stop_reason
;
3172 /* The user visible error string in terror needs to be hex encoded.
3173 We leave it as plain string in `tracepoint_stop_reason' to ease
3175 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3177 const char *result_name
;
3181 result_name
= stop_reason_rsp
+ strlen ("terror:");
3182 hexstr_len
= strlen (result_name
) * 2;
3183 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3184 strcpy (p
, "terror:");
3186 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3192 "tframes:%x;tcreated:%x;"
3193 "tfree:%x;tsize:%s;"
3197 stop_reason_rsp
, tracing_stop_tpnum
,
3198 traceframe_count
, traceframes_created
,
3199 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3200 circular_trace_buffer
,
3201 disconnected_tracing
);
3204 /* State variables to help return all the tracepoint bits. */
3205 static struct tracepoint
*cur_tpoint
;
3206 static int cur_action
;
3207 static int cur_step_action
;
3208 static struct source_string
*cur_source_string
;
3209 static struct trace_state_variable
*cur_tsv
;
3211 /* Compose a response that is an imitation of the syntax by which the
3212 tracepoint was originally downloaded. */
3215 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3219 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
3220 paddress (tpoint
->address
),
3221 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3222 tpoint
->pass_count
);
3223 if (tpoint
->type
== fast_tracepoint
)
3224 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3225 else if (tpoint
->type
== static_tracepoint
)
3226 sprintf (packet
+ strlen (packet
), ":S");
3230 buf
= unparse_agent_expr (tpoint
->cond
);
3231 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3232 tpoint
->cond
->length
, buf
);
3237 /* Compose a response that is an imitation of the syntax by which the
3238 tracepoint action was originally downloaded (with the difference
3239 that due to the way we store the actions, this will output a packet
3240 per action, while GDB could have combined more than one action
3244 response_action (char *packet
, struct tracepoint
*tpoint
,
3245 char *taction
, int step
)
3247 sprintf (packet
, "%c%x:%s:%s",
3248 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3252 /* Compose a response that is an imitation of the syntax by which the
3253 tracepoint source piece was originally downloaded. */
3256 response_source (char *packet
,
3257 struct tracepoint
*tpoint
, struct source_string
*src
)
3262 len
= strlen (src
->str
);
3263 buf
= alloca (len
* 2 + 1);
3264 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3266 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3267 tpoint
->number
, paddress (tpoint
->address
),
3268 src
->type
, 0, len
, buf
);
3271 /* Return the first piece of tracepoint definition, and initialize the
3272 state machine that will iterate through all the tracepoint
3276 cmd_qtfp (char *packet
)
3278 trace_debug ("Returning first tracepoint definition piece");
3280 cur_tpoint
= tracepoints
;
3281 cur_action
= cur_step_action
= -1;
3282 cur_source_string
= NULL
;
3285 response_tracepoint (packet
, cur_tpoint
);
3287 strcpy (packet
, "l");
3290 /* Return additional pieces of tracepoint definition. Each action and
3291 stepping action must go into its own packet, because of packet size
3292 limits, and so we use state variables to deliver one piece at a
3296 cmd_qtsp (char *packet
)
3298 trace_debug ("Returning subsequent tracepoint definition piece");
3302 /* This case would normally never occur, but be prepared for
3304 strcpy (packet
, "l");
3306 else if (cur_action
< cur_tpoint
->numactions
- 1)
3309 response_action (packet
, cur_tpoint
,
3310 cur_tpoint
->actions_str
[cur_action
], 0);
3312 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3315 response_action (packet
, cur_tpoint
,
3316 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3318 else if ((cur_source_string
3319 ? cur_source_string
->next
3320 : cur_tpoint
->source_strings
))
3322 if (cur_source_string
)
3323 cur_source_string
= cur_source_string
->next
;
3325 cur_source_string
= cur_tpoint
->source_strings
;
3326 response_source (packet
, cur_tpoint
, cur_source_string
);
3330 cur_tpoint
= cur_tpoint
->next
;
3331 cur_action
= cur_step_action
= -1;
3332 cur_source_string
= NULL
;
3334 response_tracepoint (packet
, cur_tpoint
);
3336 strcpy (packet
, "l");
3340 /* Compose a response that is an imitation of the syntax by which the
3341 trace state variable was originally downloaded. */
3344 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3346 char *buf
= (char *) "";
3351 namelen
= strlen (tsv
->name
);
3352 buf
= alloca (namelen
* 2 + 1);
3353 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3356 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3357 tsv
->getter
? 1 : 0, buf
);
3360 /* Return the first trace state variable definition, and initialize
3361 the state machine that will iterate through all the tsv bits. */
3364 cmd_qtfv (char *packet
)
3366 trace_debug ("Returning first trace state variable definition");
3368 cur_tsv
= trace_state_variables
;
3371 response_tsv (packet
, cur_tsv
);
3373 strcpy (packet
, "l");
3376 /* Return additional trace state variable definitions. */
3379 cmd_qtsv (char *packet
)
3381 trace_debug ("Returning first trace state variable definition");
3385 /* This case would normally never occur, but be prepared for
3387 strcpy (packet
, "l");
3391 cur_tsv
= cur_tsv
->next
;
3393 response_tsv (packet
, cur_tsv
);
3395 strcpy (packet
, "l");
3398 strcpy (packet
, "l");
3401 /* Return the first static tracepoint marker, and initialize the state
3402 machine that will iterate through all the static tracepoints
3406 cmd_qtfstm (char *packet
)
3408 if (!maybe_write_ipa_ust_not_loaded (packet
))
3409 run_inferior_command (packet
);
3412 /* Return additional static tracepoints markers. */
3415 cmd_qtsstm (char *packet
)
3417 if (!maybe_write_ipa_ust_not_loaded (packet
))
3418 run_inferior_command (packet
);
3421 /* Return the definition of the static tracepoint at a given address.
3422 Result packet is the same as qTsST's. */
3425 cmd_qtstmat (char *packet
)
3427 if (!maybe_write_ipa_ust_not_loaded (packet
))
3428 run_inferior_command (packet
);
3431 /* Respond to qTBuffer packet with a block of raw data from the trace
3432 buffer. GDB may ask for a lot, but we are allowed to reply with
3433 only as much as will fit within packet limits or whatever. */
3436 cmd_qtbuffer (char *own_buf
)
3438 ULONGEST offset
, num
, tot
;
3440 char *packet
= own_buf
;
3442 packet
+= strlen ("qTBuffer:");
3444 packet
= unpack_varlen_hex (packet
, &offset
);
3445 ++packet
; /* skip a comma */
3446 unpack_varlen_hex (packet
, &num
);
3448 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3449 (int) num
, pulongest (offset
));
3451 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3453 /* If we're right at the end, reply specially that we're done. */
3456 strcpy (own_buf
, "l");
3460 /* Object to any other out-of-bounds request. */
3463 write_enn (own_buf
);
3467 /* Compute the pointer corresponding to the given offset, accounting
3469 tbp
= trace_buffer_start
+ offset
;
3470 if (tbp
>= trace_buffer_wrap
)
3471 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3473 /* Trim to the remaining bytes if we're close to the end. */
3474 if (num
> tot
- offset
)
3477 /* Trim to available packet size. */
3478 if (num
>= (PBUFSIZ
- 16) / 2 )
3479 num
= (PBUFSIZ
- 16) / 2;
3481 convert_int_to_ascii (tbp
, own_buf
, num
);
3482 own_buf
[num
] = '\0';
3486 cmd_bigqtbuffer (char *own_buf
)
3489 char *packet
= own_buf
;
3491 packet
+= strlen ("QTBuffer:");
3493 if (strncmp ("circular:", packet
, strlen ("circular:")) == 0)
3495 packet
+= strlen ("circular:");
3496 unpack_varlen_hex (packet
, &val
);
3497 circular_trace_buffer
= val
;
3498 trace_debug ("Trace buffer is now %s",
3499 circular_trace_buffer
? "circular" : "linear");
3503 write_enn (own_buf
);
3507 handle_tracepoint_general_set (char *packet
)
3509 if (strcmp ("QTinit", packet
) == 0)
3511 cmd_qtinit (packet
);
3514 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3519 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3521 cmd_qtdpsrc (packet
);
3524 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
3526 cmd_qtenable_disable (packet
, 1);
3529 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
3531 cmd_qtenable_disable (packet
, 0);
3534 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3539 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3544 else if (strcmp ("QTStart", packet
) == 0)
3546 cmd_qtstart (packet
);
3549 else if (strcmp ("QTStop", packet
) == 0)
3551 cmd_qtstop (packet
);
3554 else if (strncmp ("QTDisconnected:", packet
,
3555 strlen ("QTDisconnected:")) == 0)
3557 cmd_qtdisconnected (packet
);
3560 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3562 cmd_qtframe (packet
);
3565 else if (strncmp ("QTBuffer:", packet
, strlen ("QTBuffer:")) == 0)
3567 cmd_bigqtbuffer (packet
);
3575 handle_tracepoint_query (char *packet
)
3577 if (strcmp ("qTStatus", packet
) == 0)
3579 cmd_qtstatus (packet
);
3582 else if (strcmp ("qTfP", packet
) == 0)
3587 else if (strcmp ("qTsP", packet
) == 0)
3592 else if (strcmp ("qTfV", packet
) == 0)
3597 else if (strcmp ("qTsV", packet
) == 0)
3602 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
3607 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
3609 cmd_qtbuffer (packet
);
3612 else if (strcmp ("qTfSTM", packet
) == 0)
3614 cmd_qtfstm (packet
);
3617 else if (strcmp ("qTsSTM", packet
) == 0)
3619 cmd_qtsstm (packet
);
3622 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
3624 cmd_qtstmat (packet
);
3632 #ifndef IN_PROCESS_AGENT
3634 /* Call this when thread TINFO has hit the tracepoint defined by
3635 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3636 action. This adds a while-stepping collecting state item to the
3637 threads' collecting state list, so that we can keep track of
3638 multiple simultaneous while-stepping actions being collected by the
3639 same thread. This can happen in cases like:
3641 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3643 ff0003 INSN3 <-- TP2, collect $regs
3644 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3647 Notice that when instruction INSN5 is reached, the while-stepping
3648 actions of both TP1 and TP3 are still being collected, and that TP2
3649 had been collected meanwhile. The whole range of ff0001-ff0005
3650 should be single-stepped, due to at least TP1's while-stepping
3651 action covering the whole range. */
3654 add_while_stepping_state (struct thread_info
*tinfo
,
3655 int tp_number
, CORE_ADDR tp_address
)
3657 struct wstep_state
*wstep
;
3659 wstep
= xmalloc (sizeof (*wstep
));
3660 wstep
->next
= tinfo
->while_stepping
;
3662 wstep
->tp_number
= tp_number
;
3663 wstep
->tp_address
= tp_address
;
3664 wstep
->current_step
= 0;
3666 tinfo
->while_stepping
= wstep
;
3669 /* Release the while-stepping collecting state WSTEP. */
3672 release_while_stepping_state (struct wstep_state
*wstep
)
3677 /* Release all while-stepping collecting states currently associated
3678 with thread TINFO. */
3681 release_while_stepping_state_list (struct thread_info
*tinfo
)
3683 struct wstep_state
*head
;
3685 while (tinfo
->while_stepping
)
3687 head
= tinfo
->while_stepping
;
3688 tinfo
->while_stepping
= head
->next
;
3689 release_while_stepping_state (head
);
3693 /* If TINFO was handling a 'while-stepping' action, the step has
3694 finished, so collect any step data needed, and check if any more
3695 steps are required. Return true if the thread was indeed
3696 collecting tracepoint data, false otherwise. */
3699 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3701 struct tracepoint
*tpoint
;
3702 struct wstep_state
*wstep
;
3703 struct wstep_state
**wstep_link
;
3704 struct trap_tracepoint_ctx ctx
;
3706 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
3708 if (in_process_agent_loaded ())
3709 upload_fast_traceframes ();
3711 /* Check if we were indeed collecting data for one of more
3712 tracepoints with a 'while-stepping' count. */
3713 if (tinfo
->while_stepping
== NULL
)
3718 /* We're not even tracing anymore. Stop this thread from
3720 release_while_stepping_state_list (tinfo
);
3722 /* The thread had stopped due to a single-step request indeed
3723 explained by a tracepoint. */
3727 wstep
= tinfo
->while_stepping
;
3728 wstep_link
= &tinfo
->while_stepping
;
3730 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
3731 target_pid_to_str (tinfo
->entry
.id
),
3732 wstep
->tp_number
, paddress (wstep
->tp_address
));
3734 ctx
.base
.type
= trap_tracepoint
;
3735 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3737 while (wstep
!= NULL
)
3739 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
3742 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
3743 wstep
->tp_number
, paddress (wstep
->tp_address
),
3744 target_pid_to_str (tinfo
->entry
.id
));
3747 *wstep_link
= wstep
->next
;
3748 release_while_stepping_state (wstep
);
3749 wstep
= *wstep_link
;
3753 /* We've just finished one step. */
3754 ++wstep
->current_step
;
3757 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
3758 stop_pc
, tpoint
, wstep
->current_step
);
3760 if (wstep
->current_step
>= tpoint
->step_count
)
3762 /* The requested numbers of steps have occurred. */
3763 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
3764 target_pid_to_str (tinfo
->entry
.id
),
3765 wstep
->tp_number
, paddress (wstep
->tp_address
));
3767 /* Unlink the wstep. */
3768 *wstep_link
= wstep
->next
;
3769 release_while_stepping_state (wstep
);
3770 wstep
= *wstep_link
;
3772 /* Only check the hit count now, which ensure that we do all
3773 our stepping before stopping the run. */
3774 if (tpoint
->pass_count
> 0
3775 && tpoint
->hit_count
>= tpoint
->pass_count
3776 && stopping_tracepoint
== NULL
)
3777 stopping_tracepoint
= tpoint
;
3781 /* Keep single-stepping until the requested numbers of steps
3783 wstep_link
= &wstep
->next
;
3784 wstep
= *wstep_link
;
3787 if (stopping_tracepoint
3788 || trace_buffer_is_full
3789 || expr_eval_result
!= expr_eval_no_error
)
3799 /* Handle any internal tracing control breakpoint hits. That means,
3800 pull traceframes from the IPA to our buffer, and syncing both
3801 tracing agents when the IPA's tracing stops for some reason. */
3804 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3806 /* Pull in fast tracepoint trace frames from the inferior in-process
3807 agent's buffer into our buffer. */
3809 if (!in_process_agent_loaded ())
3812 upload_fast_traceframes ();
3814 /* Check if the in-process agent had decided we should stop
3816 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
3818 int ipa_trace_buffer_is_full
;
3819 CORE_ADDR ipa_stopping_tracepoint
;
3820 int ipa_expr_eval_result
;
3821 CORE_ADDR ipa_error_tracepoint
;
3823 trace_debug ("lib stopped at stop_tracing");
3825 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
3826 &ipa_trace_buffer_is_full
);
3828 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3829 &ipa_stopping_tracepoint
);
3830 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
3832 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
3833 &ipa_error_tracepoint
);
3834 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
3836 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
3837 &ipa_expr_eval_result
);
3838 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
3840 trace_debug ("lib: trace_buffer_is_full: %d, "
3841 "stopping_tracepoint: %s, "
3842 "ipa_expr_eval_result: %d, "
3843 "error_tracepoint: %s, ",
3844 ipa_trace_buffer_is_full
,
3845 paddress (ipa_stopping_tracepoint
),
3846 ipa_expr_eval_result
,
3847 paddress (ipa_error_tracepoint
));
3851 if (ipa_trace_buffer_is_full
)
3852 trace_debug ("lib stopped due to full buffer.");
3853 if (ipa_stopping_tracepoint
)
3854 trace_debug ("lib stopped due to tpoint");
3855 if (ipa_stopping_tracepoint
)
3856 trace_debug ("lib stopped due to error");
3859 if (ipa_stopping_tracepoint
!= 0)
3862 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
3864 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
3866 expr_eval_result
= ipa_expr_eval_result
;
3868 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
3873 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
3875 trace_debug ("lib stopped at flush_trace_buffer");
3882 /* Return true if TINFO just hit a tracepoint. Collect data if
3886 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3888 struct tracepoint
*tpoint
;
3890 struct trap_tracepoint_ctx ctx
;
3892 /* Not tracing, don't handle. */
3896 ctx
.base
.type
= trap_tracepoint
;
3897 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3899 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3901 /* Note that we collect fast tracepoints here as well. We'll
3902 step over the fast tracepoint jump later, which avoids the
3904 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
)
3906 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
3907 target_pid_to_str (tinfo
->entry
.id
),
3908 tpoint
->number
, paddress (tpoint
->address
));
3910 /* Test the condition if present, and collect if true. */
3912 || (condition_true_at_tracepoint
3913 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
3914 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
3917 if (stopping_tracepoint
3918 || trace_buffer_is_full
3919 || expr_eval_result
!= expr_eval_no_error
)
3923 /* If the tracepoint had a 'while-stepping' action, then set
3924 the thread to collect this tracepoint on the following
3926 else if (tpoint
->step_count
> 0)
3928 add_while_stepping_state (tinfo
,
3929 tpoint
->number
, tpoint
->address
);
3941 #if defined IN_PROCESS_AGENT && defined HAVE_UST
3942 struct ust_marker_data
;
3943 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
3945 struct tracepoint
*tpoint
,
3946 struct traceframe
*tframe
);
3949 /* Create a trace frame for the hit of the given tracepoint in the
3953 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
3954 struct tracepoint
*tpoint
)
3956 struct traceframe
*tframe
;
3959 /* Only count it as a hit when we actually collect data. */
3960 tpoint
->hit_count
++;
3962 /* If we've exceeded a defined pass count, record the event for
3963 later, and finish the collection for this hit. This test is only
3964 for nonstepping tracepoints, stepping tracepoints test at the end
3965 of their while-stepping loop. */
3966 if (tpoint
->pass_count
> 0
3967 && tpoint
->hit_count
>= tpoint
->pass_count
3968 && tpoint
->step_count
== 0
3969 && stopping_tracepoint
== NULL
)
3970 stopping_tracepoint
= tpoint
;
3972 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
3973 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
3975 tframe
= add_traceframe (tpoint
);
3979 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
3981 #ifndef IN_PROCESS_AGENT
3982 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
3983 tpoint
->number
, paddress (tpoint
->address
),
3984 tpoint
->actions_str
[acti
]);
3987 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
3988 tpoint
->actions
[acti
]);
3991 finish_traceframe (tframe
);
3994 if (tframe
== NULL
&& tracing
)
3995 trace_buffer_is_full
= 1;
3998 #ifndef IN_PROCESS_AGENT
4001 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4003 struct tracepoint
*tpoint
, int current_step
)
4005 struct traceframe
*tframe
;
4008 trace_debug ("Making new step traceframe for "
4009 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4010 tpoint
->number
, paddress (tpoint
->address
),
4011 current_step
, tpoint
->step_count
,
4014 tframe
= add_traceframe (tpoint
);
4018 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4020 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4021 tpoint
->number
, paddress (tpoint
->address
),
4022 tpoint
->step_actions_str
[acti
]);
4024 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4025 tpoint
->step_actions
[acti
]);
4028 finish_traceframe (tframe
);
4031 if (tframe
== NULL
&& tracing
)
4032 trace_buffer_is_full
= 1;
4037 static struct regcache
*
4038 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4040 struct regcache
*regcache
= NULL
;
4042 #ifdef IN_PROCESS_AGENT
4043 if (ctx
->type
== fast_tracepoint
)
4045 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4046 if (!fctx
->regcache_initted
)
4048 fctx
->regcache_initted
= 1;
4049 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4050 supply_regblock (&fctx
->regcache
, NULL
);
4051 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4053 regcache
= &fctx
->regcache
;
4056 if (ctx
->type
== static_tracepoint
)
4058 struct static_tracepoint_ctx
*sctx
4059 = (struct static_tracepoint_ctx
*) ctx
;
4061 if (!sctx
->regcache_initted
)
4063 sctx
->regcache_initted
= 1;
4064 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4065 supply_regblock (&sctx
->regcache
, NULL
);
4066 /* Pass down the tracepoint address, because REGS doesn't
4067 include the PC, but we know what it must have been. */
4068 supply_static_tracepoint_registers (&sctx
->regcache
,
4069 (const unsigned char *)
4071 sctx
->tpoint
->address
);
4073 regcache
= &sctx
->regcache
;
4077 if (ctx
->type
== trap_tracepoint
)
4079 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4080 regcache
= tctx
->regcache
;
4084 gdb_assert (regcache
!= NULL
);
4090 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4092 struct tracepoint
*tpoint
,
4093 struct traceframe
*tframe
,
4094 struct tracepoint_action
*taction
)
4096 enum eval_result_type err
;
4098 switch (taction
->type
)
4102 struct collect_memory_action
*maction
;
4104 maction
= (struct collect_memory_action
*) taction
;
4106 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4107 pulongest (maction
->len
),
4108 paddress (maction
->addr
), maction
->basereg
);
4109 /* (should use basereg) */
4110 agent_mem_read (tframe
, NULL
,
4111 (CORE_ADDR
) maction
->addr
, maction
->len
);
4116 unsigned char *regspace
;
4117 struct regcache tregcache
;
4118 struct regcache
*context_regcache
;
4121 trace_debug ("Want to collect registers");
4123 /* Collect all registers for now. */
4124 regspace
= add_traceframe_block (tframe
,
4125 1 + register_cache_size ());
4126 if (regspace
== NULL
)
4128 trace_debug ("Trace buffer block allocation failed, skipping");
4131 /* Identify a register block. */
4134 context_regcache
= get_context_regcache (ctx
);
4136 /* Wrap the regblock in a register cache (in the stack, we
4137 don't want to malloc here). */
4138 init_register_cache (&tregcache
, regspace
+ 1);
4140 /* Copy the register data to the regblock. */
4141 regcache_cpy (&tregcache
, context_regcache
);
4143 #ifndef IN_PROCESS_AGENT
4144 /* On some platforms, trap-based tracepoints will have the PC
4145 pointing to the next instruction after the trap, but we
4146 don't want the user or GDB trying to guess whether the
4147 saved PC needs adjusting; so always record the adjusted
4148 stop_pc. Note that we can't use tpoint->address instead,
4149 since it will be wrong for while-stepping actions. This
4150 adjustment is a nop for fast tracepoints collected from the
4151 in-process lib (but not if GDBserver is collecting one
4152 preemptively), since the PC had already been adjusted to
4153 contain the tracepoint's address by the jump pad. */
4154 trace_debug ("Storing stop pc (0x%s) in regblock",
4155 paddress (tpoint
->address
));
4157 /* This changes the regblock, not the thread's
4159 regcache_write_pc (&tregcache
, stop_pc
);
4165 struct eval_expr_action
*eaction
;
4167 eaction
= (struct eval_expr_action
*) taction
;
4169 trace_debug ("Want to evaluate expression");
4171 err
= eval_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4173 if (err
!= expr_eval_no_error
)
4175 record_tracepoint_error (tpoint
, "action expression", err
);
4182 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4183 trace_debug ("Want to collect static trace data");
4184 collect_ust_data_at_tracepoint (ctx
, stop_pc
,
4187 trace_debug ("warning: collecting static trace data, "
4188 "but static tracepoints are not supported");
4193 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4199 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4200 struct tracepoint
*tpoint
)
4203 enum eval_result_type err
;
4205 /* Presently, gdbserver doesn't run compiled conditions, only the
4206 IPA does. If the program stops at a fast tracepoint's address
4207 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4208 gdbserver preemptively collect the fast tracepoint. Later, on
4209 resume, gdbserver steps over the fast tracepoint like it steps
4210 over breakpoints, so that the IPA doesn't see that fast
4211 tracepoint. This avoids double collects of fast tracepoints in
4212 that stopping scenario. Having gdbserver itself handle the fast
4213 tracepoint gives the user a consistent view of when fast or trap
4214 tracepoints are collected, compared to an alternative where only
4215 trap tracepoints are collected on stop, and fast tracepoints on
4216 resume. When a fast tracepoint is being processed by gdbserver,
4217 it is always the non-compiled condition expression that is
4219 #ifdef IN_PROCESS_AGENT
4220 if (tpoint
->compiled_cond
)
4221 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4224 err
= eval_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4226 if (err
!= expr_eval_no_error
)
4228 record_tracepoint_error (tpoint
, "condition", err
);
4229 /* The error case must return false. */
4233 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4234 tpoint
->number
, paddress (tpoint
->address
),
4236 return (value
? 1 : 0);
4239 #ifndef IN_PROCESS_AGENT
4241 /* The packet form of an agent expression consists of an 'X', number
4242 of bytes in expression, a comma, and then the bytes. */
4244 static struct agent_expr
*
4245 parse_agent_expr (char **actparm
)
4247 char *act
= *actparm
;
4249 struct agent_expr
*aexpr
;
4251 ++act
; /* skip the X */
4252 act
= unpack_varlen_hex (act
, &xlen
);
4253 ++act
; /* skip a comma */
4254 aexpr
= xmalloc (sizeof (struct agent_expr
));
4255 aexpr
->length
= xlen
;
4256 aexpr
->bytes
= xmalloc (xlen
);
4257 convert_ascii_to_int (act
, aexpr
->bytes
, xlen
);
4258 *actparm
= act
+ (xlen
* 2);
4262 /* Convert the bytes of an agent expression back into hex digits, so
4263 they can be printed or uploaded. This allocates the buffer,
4264 callers should free when they are done with it. */
4267 unparse_agent_expr (struct agent_expr
*aexpr
)
4271 rslt
= xmalloc (2 * aexpr
->length
+ 1);
4272 convert_int_to_ascii (aexpr
->bytes
, rslt
, aexpr
->length
);
4278 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
4281 gdb_agent_op_name (int op
)
4283 if (op
< 0 || op
>= gdb_agent_op_last
|| gdb_agent_op_names
[op
] == NULL
)
4285 return gdb_agent_op_names
[op
];
4288 /* The agent expression evaluator, as specified by the GDB docs. It
4289 returns 0 if everything went OK, and a nonzero error code
4292 static enum eval_result_type
4293 eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4294 struct traceframe
*tframe
,
4295 struct agent_expr
*aexpr
,
4299 #define STACK_MAX 100
4300 ULONGEST stack
[STACK_MAX
], top
;
4305 /* This union is a convenient way to convert representations. For
4306 now, assume a standard architecture where the hardware integer
4307 types have 8, 16, 32, 64 bit types. A more robust solution would
4308 be to import stdint.h from gnulib. */
4313 unsigned char bytes
[1];
4318 unsigned char bytes
[2];
4323 unsigned char bytes
[4];
4328 unsigned char bytes
[8];
4333 if (aexpr
->length
== 0)
4335 trace_debug ("empty agent expression");
4336 return expr_eval_empty_expression
;
4339 /* Cache the stack top in its own variable. Much of the time we can
4340 operate on this variable, rather than dinking with the stack. It
4341 needs to be copied to the stack when sp changes. */
4346 op
= aexpr
->bytes
[pc
++];
4348 trace_debug ("About to interpret byte 0x%x", op
);
4352 case gdb_agent_op_add
:
4356 case gdb_agent_op_sub
:
4357 top
= stack
[--sp
] - top
;
4360 case gdb_agent_op_mul
:
4364 case gdb_agent_op_div_signed
:
4367 trace_debug ("Attempted to divide by zero");
4368 return expr_eval_divide_by_zero
;
4370 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
4373 case gdb_agent_op_div_unsigned
:
4376 trace_debug ("Attempted to divide by zero");
4377 return expr_eval_divide_by_zero
;
4379 top
= stack
[--sp
] / top
;
4382 case gdb_agent_op_rem_signed
:
4385 trace_debug ("Attempted to divide by zero");
4386 return expr_eval_divide_by_zero
;
4388 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
4391 case gdb_agent_op_rem_unsigned
:
4394 trace_debug ("Attempted to divide by zero");
4395 return expr_eval_divide_by_zero
;
4397 top
= stack
[--sp
] % top
;
4400 case gdb_agent_op_lsh
:
4401 top
= stack
[--sp
] << top
;
4404 case gdb_agent_op_rsh_signed
:
4405 top
= ((LONGEST
) stack
[--sp
]) >> top
;
4408 case gdb_agent_op_rsh_unsigned
:
4409 top
= stack
[--sp
] >> top
;
4412 case gdb_agent_op_trace
:
4413 agent_mem_read (tframe
,
4414 NULL
, (CORE_ADDR
) stack
[--sp
], (ULONGEST
) top
);
4419 case gdb_agent_op_trace_quick
:
4420 arg
= aexpr
->bytes
[pc
++];
4421 agent_mem_read (tframe
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
4424 case gdb_agent_op_log_not
:
4428 case gdb_agent_op_bit_and
:
4432 case gdb_agent_op_bit_or
:
4436 case gdb_agent_op_bit_xor
:
4440 case gdb_agent_op_bit_not
:
4444 case gdb_agent_op_equal
:
4445 top
= (stack
[--sp
] == top
);
4448 case gdb_agent_op_less_signed
:
4449 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
4452 case gdb_agent_op_less_unsigned
:
4453 top
= (stack
[--sp
] < top
);
4456 case gdb_agent_op_ext
:
4457 arg
= aexpr
->bytes
[pc
++];
4458 if (arg
< (sizeof (LONGEST
) * 8))
4460 LONGEST mask
= 1 << (arg
- 1);
4461 top
&= ((LONGEST
) 1 << arg
) - 1;
4462 top
= (top
^ mask
) - mask
;
4466 case gdb_agent_op_ref8
:
4467 agent_mem_read (tframe
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
4471 case gdb_agent_op_ref16
:
4472 agent_mem_read (tframe
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
4476 case gdb_agent_op_ref32
:
4477 agent_mem_read (tframe
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
4481 case gdb_agent_op_ref64
:
4482 agent_mem_read (tframe
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
4486 case gdb_agent_op_if_goto
:
4488 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4495 case gdb_agent_op_goto
:
4496 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4499 case gdb_agent_op_const8
:
4500 /* Flush the cached stack top. */
4502 top
= aexpr
->bytes
[pc
++];
4505 case gdb_agent_op_const16
:
4506 /* Flush the cached stack top. */
4508 top
= aexpr
->bytes
[pc
++];
4509 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4512 case gdb_agent_op_const32
:
4513 /* Flush the cached stack top. */
4515 top
= aexpr
->bytes
[pc
++];
4516 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4517 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4518 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4521 case gdb_agent_op_const64
:
4522 /* Flush the cached stack top. */
4524 top
= aexpr
->bytes
[pc
++];
4525 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4526 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4527 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4528 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4529 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4530 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4531 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4534 case gdb_agent_op_reg
:
4535 /* Flush the cached stack top. */
4537 arg
= aexpr
->bytes
[pc
++];
4538 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4541 struct regcache
*regcache
;
4543 regcache
= get_context_regcache (ctx
);
4545 switch (register_size (regnum
))
4548 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
4552 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
4556 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
4560 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
4564 internal_error (__FILE__
, __LINE__
,
4565 "unhandled register size");
4570 case gdb_agent_op_end
:
4571 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4572 sp
, pulongest (top
));
4577 /* This should be an error */
4578 trace_debug ("Stack is empty, nothing to return");
4579 return expr_eval_empty_stack
;
4583 return expr_eval_no_error
;
4585 case gdb_agent_op_dup
:
4589 case gdb_agent_op_pop
:
4594 case gdb_agent_op_pick
:
4595 arg
= aexpr
->bytes
[pc
++];
4597 top
= stack
[sp
- arg
];
4601 case gdb_agent_op_rot
:
4603 ULONGEST tem
= stack
[sp
- 1];
4605 stack
[sp
- 1] = stack
[sp
- 2];
4606 stack
[sp
- 2] = top
;
4611 case gdb_agent_op_zero_ext
:
4612 arg
= aexpr
->bytes
[pc
++];
4613 if (arg
< (sizeof (LONGEST
) * 8))
4614 top
&= ((LONGEST
) 1 << arg
) - 1;
4617 case gdb_agent_op_swap
:
4618 /* Interchange top two stack elements, making sure top gets
4619 copied back onto stack. */
4621 top
= stack
[sp
- 1];
4622 stack
[sp
- 1] = stack
[sp
];
4625 case gdb_agent_op_getv
:
4626 /* Flush the cached stack top. */
4628 arg
= aexpr
->bytes
[pc
++];
4629 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4630 top
= get_trace_state_variable_value (arg
);
4633 case gdb_agent_op_setv
:
4634 arg
= aexpr
->bytes
[pc
++];
4635 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4636 set_trace_state_variable_value (arg
, top
);
4637 /* Note that we leave the value on the stack, for the
4638 benefit of later/enclosing expressions. */
4641 case gdb_agent_op_tracev
:
4642 arg
= aexpr
->bytes
[pc
++];
4643 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4644 agent_tsv_read (tframe
, arg
);
4647 /* GDB never (currently) generates any of these ops. */
4648 case gdb_agent_op_float
:
4649 case gdb_agent_op_ref_float
:
4650 case gdb_agent_op_ref_double
:
4651 case gdb_agent_op_ref_long_double
:
4652 case gdb_agent_op_l_to_d
:
4653 case gdb_agent_op_d_to_l
:
4654 case gdb_agent_op_trace16
:
4655 trace_debug ("Agent expression op 0x%x valid, but not handled",
4657 /* If ever GDB generates any of these, we don't have the
4658 option of ignoring. */
4662 trace_debug ("Agent expression op 0x%x not recognized", op
);
4663 /* Don't struggle on, things will just get worse. */
4664 return expr_eval_unrecognized_opcode
;
4667 /* Check for stack badness. */
4668 if (sp
>= (STACK_MAX
- 1))
4670 trace_debug ("Expression stack overflow");
4671 return expr_eval_stack_overflow
;
4676 trace_debug ("Expression stack underflow");
4677 return expr_eval_stack_underflow
;
4680 trace_debug ("Op %s -> sp=%d, top=0x%s",
4681 gdb_agent_op_name (op
), sp
, pulongest (top
));
4685 /* Do memory copies for bytecodes. */
4686 /* Do the recording of memory blocks for actions and bytecodes. */
4689 agent_mem_read (struct traceframe
*tframe
,
4690 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4692 unsigned char *mspace
;
4693 ULONGEST remaining
= len
;
4694 unsigned short blocklen
;
4696 /* If a 'to' buffer is specified, use it. */
4699 read_inferior_memory (from
, to
, len
);
4703 /* Otherwise, create a new memory block in the trace buffer. */
4704 while (remaining
> 0)
4708 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4709 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4710 mspace
= add_traceframe_block (tframe
, sp
);
4713 /* Identify block as a memory block. */
4716 /* Record address and size. */
4717 memcpy (mspace
, &from
, sizeof (from
));
4718 mspace
+= sizeof (from
);
4719 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4720 mspace
+= sizeof (blocklen
);
4721 /* Record the memory block proper. */
4722 read_inferior_memory (from
, mspace
, blocklen
);
4723 trace_debug ("%d bytes recorded", blocklen
);
4724 remaining
-= blocklen
;
4730 /* Record the value of a trace state variable. */
4733 agent_tsv_read (struct traceframe
*tframe
, int n
)
4735 unsigned char *vspace
;
4738 vspace
= add_traceframe_block (tframe
,
4739 1 + sizeof (n
) + sizeof (LONGEST
));
4742 /* Identify block as a variable. */
4744 /* Record variable's number and value. */
4745 memcpy (vspace
+ 1, &n
, sizeof (n
));
4746 val
= get_trace_state_variable_value (n
);
4747 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4748 trace_debug ("Variable %d recorded", n
);
4752 #ifndef IN_PROCESS_AGENT
4754 /* Callback for traceframe_walk_blocks, used to find a given block
4755 type in a traceframe. */
4758 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
4760 char *wantedp
= data
;
4762 if (*wantedp
== blocktype
)
4768 /* Walk over all traceframe blocks of the traceframe buffer starting
4769 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4770 block found, passing in DATA unmodified. If CALLBACK returns true,
4771 this returns a pointer to where the block is found. Returns NULL
4772 if no callback call returned true, indicating that all blocks have
4775 static unsigned char *
4776 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
4778 int (*callback
) (char blocktype
,
4779 unsigned char *dataptr
,
4783 unsigned char *dataptr
;
4787 trace_debug ("traceframe %d has no data", tfnum
);
4791 /* Iterate through a traceframe's blocks, looking for a block of the
4793 for (dataptr
= database
;
4794 dataptr
< database
+ datasize
;
4798 unsigned short mlen
;
4800 if (dataptr
== trace_buffer_wrap
)
4802 /* Adjust to reflect wrapping part of the frame around to
4804 datasize
= dataptr
- database
;
4805 dataptr
= database
= trace_buffer_lo
;
4808 blocktype
= *dataptr
++;
4810 if ((*callback
) (blocktype
, dataptr
, data
))
4816 /* Skip over the registers block. */
4817 dataptr
+= register_cache_size ();
4820 /* Skip over the memory block. */
4821 dataptr
+= sizeof (CORE_ADDR
);
4822 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4823 dataptr
+= (sizeof (mlen
) + mlen
);
4826 /* Skip over the TSV block. */
4827 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4830 /* Skip over the static trace data block. */
4831 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4832 dataptr
+= (sizeof (mlen
) + mlen
);
4835 trace_debug ("traceframe %d has unknown block type 0x%x",
4844 /* Look for the block of type TYPE_WANTED in the trameframe starting
4845 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4848 static unsigned char *
4849 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4850 int tfnum
, char type_wanted
)
4852 return traceframe_walk_blocks (database
, datasize
, tfnum
,
4853 match_blocktype
, &type_wanted
);
4856 static unsigned char *
4857 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4859 unsigned char *regblock
;
4861 regblock
= traceframe_find_block_type (tframe
->data
,
4865 if (regblock
== NULL
)
4866 trace_debug ("traceframe %d has no register data", tfnum
);
4871 /* Get registers from a traceframe. */
4874 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4876 unsigned char *dataptr
;
4877 struct tracepoint
*tpoint
;
4878 struct traceframe
*tframe
;
4880 tframe
= find_traceframe (tfnum
);
4884 trace_debug ("traceframe %d not found", tfnum
);
4888 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4889 if (dataptr
== NULL
)
4891 /* Mark registers unavailable. */
4892 supply_regblock (regcache
, NULL
);
4894 /* We can generally guess at a PC, although this will be
4895 misleading for while-stepping frames and multi-location
4897 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4899 regcache_write_pc (regcache
, tpoint
->address
);
4902 supply_regblock (regcache
, dataptr
);
4908 traceframe_get_pc (struct traceframe
*tframe
)
4910 struct regcache regcache
;
4911 unsigned char *dataptr
;
4913 dataptr
= traceframe_find_regblock (tframe
, -1);
4914 if (dataptr
== NULL
)
4917 init_register_cache (®cache
, dataptr
);
4918 return regcache_read_pc (®cache
);
4921 /* Read a requested block of memory from a trace frame. */
4924 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
4925 unsigned char *buf
, ULONGEST length
,
4928 struct traceframe
*tframe
;
4929 unsigned char *database
, *dataptr
;
4930 unsigned int datasize
;
4932 unsigned short mlen
;
4934 trace_debug ("traceframe_read_mem");
4936 tframe
= find_traceframe (tfnum
);
4940 trace_debug ("traceframe %d not found", tfnum
);
4944 datasize
= tframe
->data_size
;
4945 database
= dataptr
= &tframe
->data
[0];
4947 /* Iterate through a traceframe's blocks, looking for memory. */
4948 while ((dataptr
= traceframe_find_block_type (dataptr
,
4950 - (dataptr
- database
),
4951 tfnum
, 'M')) != NULL
)
4953 memcpy (&maddr
, dataptr
, sizeof (maddr
));
4954 dataptr
+= sizeof (maddr
);
4955 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4956 dataptr
+= sizeof (mlen
);
4957 trace_debug ("traceframe %d has %d bytes at %s",
4958 tfnum
, mlen
, paddress (maddr
));
4960 /* If the block includes the first part of the desired range,
4961 return as much it has; GDB will re-request the remainder,
4962 which might be in a different block of this trace frame. */
4963 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
4965 ULONGEST amt
= (maddr
+ mlen
) - addr
;
4969 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
4974 /* Skip over this block. */
4978 trace_debug ("traceframe %d has no memory data for the desired region",
4986 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
4989 struct traceframe
*tframe
;
4990 unsigned char *database
, *dataptr
;
4991 unsigned int datasize
;
4994 trace_debug ("traceframe_read_tsv");
4996 tfnum
= current_traceframe
;
5000 trace_debug ("no current traceframe");
5004 tframe
= find_traceframe (tfnum
);
5008 trace_debug ("traceframe %d not found", tfnum
);
5012 datasize
= tframe
->data_size
;
5013 database
= dataptr
= &tframe
->data
[0];
5015 /* Iterate through a traceframe's blocks, looking for the tsv. */
5016 while ((dataptr
= traceframe_find_block_type (dataptr
,
5018 - (dataptr
- database
),
5019 tfnum
, 'V')) != NULL
)
5021 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5022 dataptr
+= sizeof (vnum
);
5024 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5026 /* Check that this is the variable we want. */
5029 memcpy (val
, dataptr
, sizeof (*val
));
5033 /* Skip over this block. */
5034 dataptr
+= sizeof (LONGEST
);
5037 trace_debug ("traceframe %d has no data for variable %d",
5042 /* Read a requested block of static tracepoint data from a trace
5046 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5047 unsigned char *buf
, ULONGEST length
,
5050 struct traceframe
*tframe
;
5051 unsigned char *database
, *dataptr
;
5052 unsigned int datasize
;
5053 unsigned short mlen
;
5055 trace_debug ("traceframe_read_sdata");
5057 tframe
= find_traceframe (tfnum
);
5061 trace_debug ("traceframe %d not found", tfnum
);
5065 datasize
= tframe
->data_size
;
5066 database
= &tframe
->data
[0];
5068 /* Iterate through a traceframe's blocks, looking for static
5070 dataptr
= traceframe_find_block_type (database
, datasize
,
5072 if (dataptr
!= NULL
)
5074 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5075 dataptr
+= sizeof (mlen
);
5078 if (offset
+ length
> mlen
)
5079 length
= mlen
- offset
;
5081 memcpy (buf
, dataptr
, length
);
5089 trace_debug ("traceframe %d has no static trace data", tfnum
);
5095 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5096 object. DATA is pointer to a struct buffer holding the
5097 traceframe-info object being built. */
5100 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5102 struct buffer
*buffer
= data
;
5108 unsigned short mlen
;
5111 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5112 dataptr
+= sizeof (maddr
);
5113 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5114 dataptr
+= sizeof (mlen
);
5115 buffer_xml_printf (buffer
,
5116 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5117 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5127 warning ("Unhandled trace block type (%d) '%c ' "
5128 "while building trace frame info.",
5129 blocktype
, blocktype
);
5136 /* Build a traceframe-info object for traceframe number TFNUM into
5140 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5142 struct traceframe
*tframe
;
5144 trace_debug ("traceframe_read_info");
5146 tframe
= find_traceframe (tfnum
);
5150 trace_debug ("traceframe %d not found", tfnum
);
5154 buffer_grow_str (buffer
, "<traceframe-info>\n");
5155 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5156 tfnum
, build_traceframe_info_xml
, buffer
);
5157 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5161 /* Return the first fast tracepoint whose jump pad contains PC. */
5163 static struct tracepoint
*
5164 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5166 struct tracepoint
*tpoint
;
5168 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5169 if (tpoint
->type
== fast_tracepoint
)
5170 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5176 /* Return GDBserver's tracepoint that matches the IP Agent's
5177 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5180 static struct tracepoint
*
5181 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5183 struct tracepoint
*tpoint
;
5185 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5186 if (tpoint
->type
== fast_tracepoint
)
5187 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5195 /* The type of the object that is used to synchronize fast tracepoint
5198 typedef struct collecting_t
5200 /* The fast tracepoint number currently collecting. */
5203 /* A number that GDBserver can use to identify the thread that is
5204 presently holding the collect lock. This need not (and usually
5205 is not) the thread id, as getting the current thread ID usually
5206 requires a system call, which we want to avoid like the plague.
5207 Usually this is thread's TCB, found in the TLS (pseudo-)
5208 register, which is readable with a single insn on several
5210 uintptr_t thread_area
;
5213 #ifndef IN_PROCESS_AGENT
5216 force_unlock_trace_buffer (void)
5218 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5221 /* Check if the thread identified by THREAD_AREA which is stopped at
5222 STOP_PC, is presently locking the fast tracepoint collection, and
5223 if so, gather some status of said collection. Returns 0 if the
5224 thread isn't collecting or in the jump pad at all. 1, if in the
5225 jump pad (or within gdb_collect) and hasn't executed the adjusted
5226 original insn yet (can set a breakpoint there and run to it). 2,
5227 if presently executing the adjusted original insn --- in which
5228 case, if we want to move the thread out of the jump pad, we need to
5229 single-step it until this function returns 0. */
5232 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5234 struct fast_tpoint_collect_status
*status
)
5236 CORE_ADDR ipa_collecting
;
5237 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5238 struct tracepoint
*tpoint
;
5239 int needs_breakpoint
;
5241 /* The thread THREAD_AREA is either:
5243 0. not collecting at all, not within the jump pad, or within
5244 gdb_collect or one of its callees.
5246 1. in the jump pad and haven't reached gdb_collect
5248 2. within gdb_collect (out of the jump pad) (collect is set)
5250 3. we're in the jump pad, after gdb_collect having returned,
5251 possibly executing the adjusted insns.
5253 For cases 1 and 3, `collecting' may or not be set. The jump pad
5254 doesn't have any complicated jump logic, so we can tell if the
5255 thread is executing the adjust original insn or not by just
5256 matching STOP_PC with known jump pad addresses. If we it isn't
5257 yet executing the original insn, set a breakpoint there, and let
5258 the thread run to it, so to quickly step over a possible (many
5259 insns) gdb_collect call. Otherwise, or when the breakpoint is
5260 hit, only a few (small number of) insns are left to be executed
5261 in the jump pad. Single-step the thread until it leaves the
5266 needs_breakpoint
= 0;
5267 trace_debug ("fast_tracepoint_collecting");
5269 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5270 &ipa_gdb_jump_pad_buffer
))
5271 fatal ("error extracting `gdb_jump_pad_buffer'");
5272 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5273 &ipa_gdb_jump_pad_buffer_end
))
5274 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5276 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5277 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5279 /* We can tell which tracepoint(s) the thread is collecting by
5280 matching the jump pad address back to the tracepoint. */
5281 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5284 warning ("in jump pad, but no matching tpoint?");
5289 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5291 tpoint
->number
, paddress (tpoint
->address
),
5292 paddress (tpoint
->jump_pad
),
5293 paddress (tpoint
->jump_pad_end
),
5294 paddress (tpoint
->adjusted_insn_addr
),
5295 paddress (tpoint
->adjusted_insn_addr_end
));
5298 /* Definitely in the jump pad. May or may not need
5299 fast-exit-jump-pad breakpoint. */
5300 if (tpoint
->jump_pad
<= stop_pc
5301 && stop_pc
< tpoint
->adjusted_insn_addr
)
5302 needs_breakpoint
= 1;
5306 collecting_t ipa_collecting_obj
;
5308 /* If `collecting' is set/locked, then the THREAD_AREA thread
5309 may or not be the one holding the lock. We have to read the
5310 lock to find out. */
5312 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5315 trace_debug ("fast_tracepoint_collecting:"
5316 " failed reading 'collecting' in the inferior");
5320 if (!ipa_collecting
)
5322 trace_debug ("fast_tracepoint_collecting: not collecting"
5323 " (and nobody is).");
5327 /* Some thread is collecting. Check which. */
5328 if (read_inferior_memory (ipa_collecting
,
5329 (unsigned char *) &ipa_collecting_obj
,
5330 sizeof (ipa_collecting_obj
)) != 0)
5333 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5335 trace_debug ("fast_tracepoint_collecting: not collecting "
5336 "(another thread is)");
5341 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5344 warning ("fast_tracepoint_collecting: collecting, "
5345 "but tpoint %s not found?",
5346 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5350 /* The thread is within `gdb_collect', skip over the rest of
5351 fast tracepoint collection quickly using a breakpoint. */
5352 needs_breakpoint
= 1;
5355 /* The caller wants a bit of status detail. */
5358 status
->tpoint_num
= tpoint
->number
;
5359 status
->tpoint_addr
= tpoint
->address
;
5360 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5361 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5364 if (needs_breakpoint
)
5366 /* Hasn't executed the original instruction yet. Set breakpoint
5367 there, and wait till it's hit, then single-step until exiting
5371 fast_tracepoint_collecting, returning continue-until-break at %s",
5372 paddress (tpoint
->adjusted_insn_addr
));
5374 return 1; /* continue */
5378 /* Just single-step until exiting the jump pad. */
5380 trace_debug ("fast_tracepoint_collecting, returning "
5381 "need-single-step (%s-%s)",
5382 paddress (tpoint
->adjusted_insn_addr
),
5383 paddress (tpoint
->adjusted_insn_addr_end
));
5385 return 2; /* single-step */
5391 #ifdef IN_PROCESS_AGENT
5393 /* The global fast tracepoint collect lock. Points to a collecting_t
5394 object built on the stack by the jump pad, if presently locked;
5395 NULL if it isn't locked. Note that this lock *must* be set while
5396 executing any *function other than the jump pad. See
5397 fast_tracepoint_collecting. */
5398 static collecting_t
* ATTR_USED collecting
;
5400 /* This routine, called from the jump pad (in asm) is designed to be
5401 called from the jump pads of fast tracepoints, thus it is on the
5404 IP_AGENT_EXPORT
void ATTR_USED
5405 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5407 struct fast_tracepoint_ctx ctx
;
5409 /* Don't do anything until the trace run is completely set up. */
5413 if (!tpoint
->enabled
)
5416 ctx
.base
.type
= fast_tracepoint
;
5418 ctx
.regcache_initted
= 0;
5419 ctx
.tpoint
= tpoint
;
5421 /* Wrap the regblock in a register cache (in the stack, we don't
5422 want to malloc here). */
5423 ctx
.regspace
= alloca (register_cache_size ());
5424 if (ctx
.regspace
== NULL
)
5426 trace_debug ("Trace buffer block allocation failed, skipping");
5430 /* Test the condition if present, and collect if true. */
5431 if (tpoint
->cond
== NULL
5432 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5435 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5436 tpoint
->address
, tpoint
);
5438 /* Note that this will cause original insns to be written back
5439 to where we jumped from, but that's OK because we're jumping
5440 back to the next whole instruction. This will go badly if
5441 instruction restoration is not atomic though. */
5442 if (stopping_tracepoint
5443 || trace_buffer_is_full
5444 || expr_eval_result
!= expr_eval_no_error
)
5449 /* If there was a condition and it evaluated to false, the only
5450 way we would stop tracing is if there was an error during
5451 condition expression evaluation. */
5452 if (expr_eval_result
!= expr_eval_no_error
)
5459 #ifndef IN_PROCESS_AGENT
5461 /* Bytecode compilation. */
5463 CORE_ADDR current_insn_ptr
;
5467 struct bytecode_address
5472 /* Offset and size of field to be modified in the goto block. */
5473 int from_offset
, from_size
;
5474 struct bytecode_address
*next
;
5475 } *bytecode_address_table
;
5478 get_raw_reg_func_addr (void)
5480 return ipa_sym_addrs
.addr_get_raw_reg
;
5484 emit_prologue (void)
5486 target_emit_ops ()->emit_prologue ();
5490 emit_epilogue (void)
5492 target_emit_ops ()->emit_epilogue ();
5498 target_emit_ops ()->emit_add ();
5504 target_emit_ops ()->emit_sub ();
5510 target_emit_ops ()->emit_mul ();
5516 target_emit_ops ()->emit_lsh ();
5520 emit_rsh_signed (void)
5522 target_emit_ops ()->emit_rsh_signed ();
5526 emit_rsh_unsigned (void)
5528 target_emit_ops ()->emit_rsh_unsigned ();
5534 target_emit_ops ()->emit_ext (arg
);
5540 target_emit_ops ()->emit_log_not ();
5546 target_emit_ops ()->emit_bit_and ();
5552 target_emit_ops ()->emit_bit_or ();
5558 target_emit_ops ()->emit_bit_xor ();
5564 target_emit_ops ()->emit_bit_not ();
5570 target_emit_ops ()->emit_equal ();
5574 emit_less_signed (void)
5576 target_emit_ops ()->emit_less_signed ();
5580 emit_less_unsigned (void)
5582 target_emit_ops ()->emit_less_unsigned ();
5588 target_emit_ops ()->emit_ref (size
);
5592 emit_if_goto (int *offset_p
, int *size_p
)
5594 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
5598 emit_goto (int *offset_p
, int *size_p
)
5600 target_emit_ops ()->emit_goto (offset_p
, size_p
);
5604 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
5606 target_emit_ops ()->write_goto_address (from
, to
, size
);
5610 emit_const (LONGEST num
)
5612 target_emit_ops ()->emit_const (num
);
5618 target_emit_ops ()->emit_reg (reg
);
5624 target_emit_ops ()->emit_pop ();
5628 emit_stack_flush (void)
5630 target_emit_ops ()->emit_stack_flush ();
5634 emit_zero_ext (int arg
)
5636 target_emit_ops ()->emit_zero_ext (arg
);
5642 target_emit_ops ()->emit_swap ();
5646 emit_stack_adjust (int n
)
5648 target_emit_ops ()->emit_stack_adjust (n
);
5651 /* FN's prototype is `LONGEST(*fn)(int)'. */
5654 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
5656 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
5659 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
5662 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
5664 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
5668 emit_eq_goto (int *offset_p
, int *size_p
)
5670 target_emit_ops ()->emit_eq_goto (offset_p
, size_p
);
5674 emit_ne_goto (int *offset_p
, int *size_p
)
5676 target_emit_ops ()->emit_ne_goto (offset_p
, size_p
);
5680 emit_lt_goto (int *offset_p
, int *size_p
)
5682 target_emit_ops ()->emit_lt_goto (offset_p
, size_p
);
5686 emit_ge_goto (int *offset_p
, int *size_p
)
5688 target_emit_ops ()->emit_ge_goto (offset_p
, size_p
);
5692 emit_gt_goto (int *offset_p
, int *size_p
)
5694 target_emit_ops ()->emit_gt_goto (offset_p
, size_p
);
5698 emit_le_goto (int *offset_p
, int *size_p
)
5700 target_emit_ops ()->emit_le_goto (offset_p
, size_p
);
5703 static enum eval_result_type
compile_bytecodes (struct agent_expr
*aexpr
);
5706 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5707 CORE_ADDR
*jump_entry
)
5709 CORE_ADDR entry_point
= *jump_entry
;
5710 enum eval_result_type err
;
5712 trace_debug ("Starting condition compilation for tracepoint %d\n",
5715 /* Initialize the global pointer to the code being built. */
5716 current_insn_ptr
= *jump_entry
;
5720 err
= compile_bytecodes (tpoint
->cond
);
5722 if (err
== expr_eval_no_error
)
5726 /* Record the beginning of the compiled code. */
5727 tpoint
->compiled_cond
= entry_point
;
5729 trace_debug ("Condition compilation for tracepoint %d complete\n",
5734 /* Leave the unfinished code in situ, but don't point to it. */
5736 tpoint
->compiled_cond
= 0;
5738 trace_debug ("Condition compilation for tracepoint %d failed, "
5740 tpoint
->number
, err
);
5743 /* Update the code pointer passed in. Note that we do this even if
5744 the compile fails, so that we can look at the partial results
5745 instead of letting them be overwritten. */
5746 *jump_entry
= current_insn_ptr
;
5748 /* Leave a gap, to aid dump decipherment. */
5752 /* Scan an agent expression for any evidence that the given PC is the
5753 target of a jump bytecode in the expression. */
5756 is_goto_target (struct agent_expr
*aexpr
, int pc
)
5761 for (i
= 0; i
< aexpr
->length
; i
+= 1 + gdb_agent_op_sizes
[op
])
5763 op
= aexpr
->bytes
[i
];
5765 if (op
== gdb_agent_op_goto
|| op
== gdb_agent_op_if_goto
)
5767 int target
= (aexpr
->bytes
[i
+ 1] << 8) + aexpr
->bytes
[i
+ 2];
5776 /* Given an agent expression, turn it into native code. */
5778 static enum eval_result_type
5779 compile_bytecodes (struct agent_expr
*aexpr
)
5783 unsigned char op
, next_op
;
5785 /* This is only used to build 64-bit value for constants. */
5787 struct bytecode_address
*aentry
, *aentry2
;
5792 trace_debug ("Cannot compile op 0x%x\n", op); \
5793 return expr_eval_unhandled_opcode; \
5796 if (aexpr
->length
== 0)
5798 trace_debug ("empty agent expression\n");
5799 return expr_eval_empty_expression
;
5802 bytecode_address_table
= NULL
;
5806 op
= aexpr
->bytes
[pc
];
5808 trace_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
5810 /* Record the compiled-code address of the bytecode, for use by
5811 jump instructions. */
5812 aentry
= xmalloc (sizeof (struct bytecode_address
));
5814 aentry
->address
= current_insn_ptr
;
5815 aentry
->goto_pc
= -1;
5816 aentry
->from_offset
= aentry
->from_size
= 0;
5817 aentry
->next
= bytecode_address_table
;
5818 bytecode_address_table
= aentry
;
5826 case gdb_agent_op_add
:
5830 case gdb_agent_op_sub
:
5834 case gdb_agent_op_mul
:
5838 case gdb_agent_op_div_signed
:
5842 case gdb_agent_op_div_unsigned
:
5846 case gdb_agent_op_rem_signed
:
5850 case gdb_agent_op_rem_unsigned
:
5854 case gdb_agent_op_lsh
:
5858 case gdb_agent_op_rsh_signed
:
5862 case gdb_agent_op_rsh_unsigned
:
5863 emit_rsh_unsigned ();
5866 case gdb_agent_op_trace
:
5870 case gdb_agent_op_trace_quick
:
5874 case gdb_agent_op_log_not
:
5878 case gdb_agent_op_bit_and
:
5882 case gdb_agent_op_bit_or
:
5886 case gdb_agent_op_bit_xor
:
5890 case gdb_agent_op_bit_not
:
5894 case gdb_agent_op_equal
:
5895 next_op
= aexpr
->bytes
[pc
];
5896 if (next_op
== gdb_agent_op_if_goto
5897 && !is_goto_target (aexpr
, pc
)
5898 && target_emit_ops ()->emit_eq_goto
)
5900 trace_debug ("Combining equal & if_goto");
5903 arg
= aexpr
->bytes
[pc
++];
5904 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5905 aentry
->goto_pc
= arg
;
5906 emit_eq_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5908 else if (next_op
== gdb_agent_op_log_not
5909 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
5910 && !is_goto_target (aexpr
, pc
+ 1)
5911 && target_emit_ops ()->emit_ne_goto
)
5913 trace_debug ("Combining equal & log_not & if_goto");
5916 arg
= aexpr
->bytes
[pc
++];
5917 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5918 aentry
->goto_pc
= arg
;
5919 emit_ne_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5925 case gdb_agent_op_less_signed
:
5926 next_op
= aexpr
->bytes
[pc
];
5927 if (next_op
== gdb_agent_op_if_goto
5928 && !is_goto_target (aexpr
, pc
))
5930 trace_debug ("Combining less_signed & if_goto");
5933 arg
= aexpr
->bytes
[pc
++];
5934 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5935 aentry
->goto_pc
= arg
;
5936 emit_lt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5938 else if (next_op
== gdb_agent_op_log_not
5939 && !is_goto_target (aexpr
, pc
)
5940 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
5941 && !is_goto_target (aexpr
, pc
+ 1))
5943 trace_debug ("Combining less_signed & log_not & if_goto");
5946 arg
= aexpr
->bytes
[pc
++];
5947 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5948 aentry
->goto_pc
= arg
;
5949 emit_ge_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5952 emit_less_signed ();
5955 case gdb_agent_op_less_unsigned
:
5956 emit_less_unsigned ();
5959 case gdb_agent_op_ext
:
5960 arg
= aexpr
->bytes
[pc
++];
5961 if (arg
< (sizeof (LONGEST
) * 8))
5965 case gdb_agent_op_ref8
:
5969 case gdb_agent_op_ref16
:
5973 case gdb_agent_op_ref32
:
5977 case gdb_agent_op_ref64
:
5981 case gdb_agent_op_if_goto
:
5982 arg
= aexpr
->bytes
[pc
++];
5983 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5984 aentry
->goto_pc
= arg
;
5985 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5988 case gdb_agent_op_goto
:
5989 arg
= aexpr
->bytes
[pc
++];
5990 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5991 aentry
->goto_pc
= arg
;
5992 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5995 case gdb_agent_op_const8
:
5996 emit_stack_flush ();
5997 top
= aexpr
->bytes
[pc
++];
6001 case gdb_agent_op_const16
:
6002 emit_stack_flush ();
6003 top
= aexpr
->bytes
[pc
++];
6004 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6008 case gdb_agent_op_const32
:
6009 emit_stack_flush ();
6010 top
= aexpr
->bytes
[pc
++];
6011 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6012 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6013 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6017 case gdb_agent_op_const64
:
6018 emit_stack_flush ();
6019 top
= aexpr
->bytes
[pc
++];
6020 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6021 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6022 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6023 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6024 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6025 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6026 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
6030 case gdb_agent_op_reg
:
6031 emit_stack_flush ();
6032 arg
= aexpr
->bytes
[pc
++];
6033 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6037 case gdb_agent_op_end
:
6038 trace_debug ("At end of expression\n");
6040 /* Assume there is one stack element left, and that it is
6041 cached in "top" where emit_epilogue can get to it. */
6042 emit_stack_adjust (1);
6047 case gdb_agent_op_dup
:
6048 /* In our design, dup is equivalent to stack flushing. */
6049 emit_stack_flush ();
6052 case gdb_agent_op_pop
:
6056 case gdb_agent_op_zero_ext
:
6057 arg
= aexpr
->bytes
[pc
++];
6058 if (arg
< (sizeof (LONGEST
) * 8))
6059 emit_zero_ext (arg
);
6062 case gdb_agent_op_swap
:
6063 next_op
= aexpr
->bytes
[pc
];
6064 /* Detect greater-than comparison sequences. */
6065 if (next_op
== gdb_agent_op_less_signed
6066 && !is_goto_target (aexpr
, pc
)
6067 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_if_goto
)
6068 && !is_goto_target (aexpr
, pc
+ 1))
6070 trace_debug ("Combining swap & less_signed & if_goto");
6073 arg
= aexpr
->bytes
[pc
++];
6074 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6075 aentry
->goto_pc
= arg
;
6076 emit_gt_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6078 else if (next_op
== gdb_agent_op_less_signed
6079 && !is_goto_target (aexpr
, pc
)
6080 && (aexpr
->bytes
[pc
+ 1] == gdb_agent_op_log_not
)
6081 && !is_goto_target (aexpr
, pc
+ 1)
6082 && (aexpr
->bytes
[pc
+ 2] == gdb_agent_op_if_goto
)
6083 && !is_goto_target (aexpr
, pc
+ 2))
6085 trace_debug ("Combining swap & less_signed & log_not & if_goto");
6088 arg
= aexpr
->bytes
[pc
++];
6089 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6090 aentry
->goto_pc
= arg
;
6091 emit_le_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
6097 case gdb_agent_op_getv
:
6098 emit_stack_flush ();
6099 arg
= aexpr
->bytes
[pc
++];
6100 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6101 emit_int_call_1 (ipa_sym_addrs
.addr_get_trace_state_variable_value
,
6105 case gdb_agent_op_setv
:
6106 arg
= aexpr
->bytes
[pc
++];
6107 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
6108 emit_void_call_2 (ipa_sym_addrs
.addr_set_trace_state_variable_value
,
6112 case gdb_agent_op_tracev
:
6116 /* GDB never (currently) generates any of these ops. */
6117 case gdb_agent_op_float
:
6118 case gdb_agent_op_ref_float
:
6119 case gdb_agent_op_ref_double
:
6120 case gdb_agent_op_ref_long_double
:
6121 case gdb_agent_op_l_to_d
:
6122 case gdb_agent_op_d_to_l
:
6123 case gdb_agent_op_trace16
:
6128 trace_debug ("Agent expression op 0x%x not recognized\n", op
);
6129 /* Don't struggle on, things will just get worse. */
6130 return expr_eval_unrecognized_opcode
;
6133 /* This catches errors that occur in target-specific code
6137 trace_debug ("Error %d while emitting code for %s\n",
6138 emit_error
, gdb_agent_op_name (op
));
6139 return expr_eval_unhandled_opcode
;
6142 trace_debug ("Op %s compiled\n", gdb_agent_op_name (op
));
6145 /* Now fill in real addresses as goto destinations. */
6146 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
6150 if (aentry
->goto_pc
< 0)
6153 /* Find the location that we are going to, and call back into
6154 target-specific code to write the actual address or
6156 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
6158 if (aentry2
->pc
== aentry
->goto_pc
)
6160 trace_debug ("Want to jump from %s to %s\n",
6161 paddress (aentry
->address
),
6162 paddress (aentry2
->address
));
6163 write_goto_address (aentry
->address
+ aentry
->from_offset
,
6164 aentry2
->address
, aentry
->from_size
);
6170 /* Error out if we didn't find a destination. */
6173 trace_debug ("Destination of goto %d not found\n",
6175 return expr_eval_invalid_goto
;
6179 return expr_eval_no_error
;
6182 /* We'll need to adjust these when we consider bi-arch setups, and big
6186 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
6188 return write_inferior_memory (where
,
6189 (unsigned char *) &ptr
, sizeof (void *));
6192 /* The base pointer of the IPA's heap. This is the only memory the
6193 IPA is allowed to use. The IPA should _not_ call the inferior's
6194 `malloc' during operation. That'd be slow, and, most importantly,
6195 it may not be safe. We may be collecting a tracepoint in a signal
6196 handler, for example. */
6197 static CORE_ADDR target_tp_heap
;
6199 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
6203 target_malloc (ULONGEST size
)
6207 if (target_tp_heap
== 0)
6209 /* We have the pointer *address*, need what it points to. */
6210 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
6212 fatal ("could get target heap head pointer");
6215 ptr
= target_tp_heap
;
6216 target_tp_heap
+= size
;
6218 /* Pad to 8-byte alignment. */
6219 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
6225 download_agent_expr (struct agent_expr
*expr
)
6227 CORE_ADDR expr_addr
;
6228 CORE_ADDR expr_bytes
;
6230 expr_addr
= target_malloc (sizeof (*expr
));
6231 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
6233 expr_bytes
= target_malloc (expr
->length
);
6234 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
6236 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
6241 /* Align V up to N bits. */
6242 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
6245 download_tracepoints (void)
6247 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
6248 struct tracepoint
*tpoint
;
6250 /* Start out empty. */
6251 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
6253 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6255 struct tracepoint target_tracepoint
;
6257 if (tpoint
->type
!= fast_tracepoint
6258 && tpoint
->type
!= static_tracepoint
)
6261 /* Maybe download a compiled condition. */
6262 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
6264 CORE_ADDR jentry
, jump_entry
;
6266 jentry
= jump_entry
= get_jump_space_head ();
6268 if (tpoint
->cond
!= NULL
)
6270 /* Pad to 8-byte alignment. (needed?) */
6271 /* Actually this should be left for the target to
6273 jentry
= UALIGN (jentry
, 8);
6275 compile_tracepoint_condition (tpoint
, &jentry
);
6278 /* Pad to 8-byte alignment. */
6279 jentry
= UALIGN (jentry
, 8);
6280 claim_jump_space (jentry
- jump_entry
);
6283 target_tracepoint
= *tpoint
;
6286 tpptr
= target_malloc (sizeof (*tpoint
));
6287 tpoint
->obj_addr_on_target
= tpptr
;
6289 if (tpoint
== tracepoints
)
6291 /* First object in list, set the head pointer in the
6293 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
6297 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
6302 /* Write the whole object. We'll fix up its pointers in a bit.
6303 Assume no next for now. This is fixed up above on the next
6304 iteration, if there's any. */
6305 target_tracepoint
.next
= NULL
;
6306 /* Need to clear this here too, since we're downloading the
6307 tracepoints before clearing our own copy. */
6308 target_tracepoint
.hit_count
= 0;
6310 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
6311 sizeof (target_tracepoint
));
6314 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6316 download_agent_expr (tpoint
->cond
));
6318 if (tpoint
->numactions
)
6321 CORE_ADDR actions_array
;
6323 /* The pointers array. */
6325 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6326 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6330 /* Now for each pointer, download the action. */
6331 for (i
= 0; i
< tpoint
->numactions
; i
++)
6333 CORE_ADDR ipa_action
= 0;
6334 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6336 switch (action
->type
)
6340 = target_malloc (sizeof (struct collect_memory_action
));
6341 write_inferior_memory (ipa_action
,
6342 (unsigned char *) action
,
6343 sizeof (struct collect_memory_action
));
6347 = target_malloc (sizeof (struct collect_registers_action
));
6348 write_inferior_memory (ipa_action
,
6349 (unsigned char *) action
,
6350 sizeof (struct collect_registers_action
));
6355 struct eval_expr_action
*eaction
6356 = (struct eval_expr_action
*) action
;
6358 ipa_action
= target_malloc (sizeof (*eaction
));
6359 write_inferior_memory (ipa_action
,
6360 (unsigned char *) eaction
,
6363 expr
= download_agent_expr (eaction
->expr
);
6364 write_inferior_data_ptr
6365 (ipa_action
+ offsetof (struct eval_expr_action
, expr
),
6370 ipa_action
= target_malloc
6371 (sizeof (struct collect_static_trace_data_action
));
6372 write_inferior_memory
6374 (unsigned char *) action
,
6375 sizeof (struct collect_static_trace_data_action
));
6378 trace_debug ("unknown trace action '%c', ignoring",
6383 if (ipa_action
!= 0)
6384 write_inferior_data_ptr
6385 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
6393 download_trace_state_variables (void)
6395 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6396 struct trace_state_variable
*tsv
;
6398 /* Start out empty. */
6399 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6401 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6403 struct trace_state_variable target_tsv
;
6405 /* TSV's with a getter have been initialized equally in both the
6406 inferior and GDBserver. Skip them. */
6407 if (tsv
->getter
!= NULL
)
6413 ptr
= target_malloc (sizeof (*tsv
));
6415 if (tsv
== trace_state_variables
)
6417 /* First object in list, set the head pointer in the
6420 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6425 write_inferior_data_ptr (prev_ptr
6426 + offsetof (struct trace_state_variable
,
6431 /* Write the whole object. We'll fix up its pointers in a bit.
6432 Assume no next, fixup when needed. */
6433 target_tsv
.next
= NULL
;
6435 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6436 sizeof (target_tsv
));
6438 if (tsv
->name
!= NULL
)
6440 size_t size
= strlen (tsv
->name
) + 1;
6441 CORE_ADDR name_addr
= target_malloc (size
);
6442 write_inferior_memory (name_addr
,
6443 (unsigned char *) tsv
->name
, size
);
6444 write_inferior_data_ptr (ptr
6445 + offsetof (struct trace_state_variable
,
6450 if (tsv
->getter
!= NULL
)
6452 fatal ("what to do with these?");
6458 /* Fixup the next pointer in the last item in the list. */
6459 write_inferior_data_ptr (prev_ptr
6460 + offsetof (struct trace_state_variable
,
6465 /* Upload complete trace frames out of the IP Agent's trace buffer
6466 into GDBserver's trace buffer. This always uploads either all or
6467 no trace frames. This is the counter part of
6468 `trace_alloc_trace_buffer'. See its description of the atomic
6469 synching mechanism. */
6472 upload_fast_traceframes (void)
6474 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6475 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6477 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6478 unsigned int curr_tbctrl_idx
;
6479 unsigned int ipa_trace_buffer_ctrl_curr
;
6480 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6481 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6482 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6483 CORE_ADDR ipa_trace_buffer_lo
;
6484 CORE_ADDR ipa_trace_buffer_hi
;
6486 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6487 &ipa_traceframe_read_count_racy
))
6489 /* This will happen in most targets if the current thread is
6494 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6495 &ipa_traceframe_write_count_racy
))
6498 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6499 ipa_traceframe_write_count_racy
6500 - ipa_traceframe_read_count_racy
,
6501 ipa_traceframe_write_count_racy
,
6502 ipa_traceframe_read_count_racy
);
6504 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6507 about_to_request_buffer_space_bkpt
6508 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6511 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6512 &ipa_trace_buffer_ctrl_curr
))
6515 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6517 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6520 unsigned int prev
, counter
;
6522 /* Update the token, with new counters, and the GDBserver stamp
6523 bit. Alway reuse the current TBC index. */
6524 prev
= ipa_trace_buffer_ctrl_curr
& 0x0007ff00;
6525 counter
= (prev
+ 0x100) & 0x0007ff00;
6527 ipa_trace_buffer_ctrl_curr
= (0x80000000
6533 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6534 ipa_trace_buffer_ctrl_curr
))
6537 trace_debug ("Lib: Committed %08x -> %08x",
6538 ipa_trace_buffer_ctrl_curr_old
,
6539 ipa_trace_buffer_ctrl_curr
);
6541 /* Re-read these, now that we've installed the
6542 `about_to_request_buffer_space' breakpoint/lock. A thread could
6543 have finished a traceframe between the last read of these
6544 counters and setting the breakpoint above. If we start
6545 uploading, we never want to leave this function with
6546 traceframe_read_count != 0, otherwise, GDBserver could end up
6547 incrementing the counter tokens more than once (due to event loop
6548 nesting), which would break the IP agent's "effective" detection
6549 (see trace_alloc_trace_buffer). */
6550 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6551 &ipa_traceframe_read_count
))
6553 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6554 &ipa_traceframe_write_count
))
6559 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6560 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6561 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6563 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6564 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6565 trace_debug ("note that ipa_traceframe_count's parts changed");
6568 /* Get the address of the current TBC object (the IP agent has an
6569 array of 3 such objects). The index is stored in the TBC
6571 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6572 ipa_trace_buffer_ctrl_addr
6573 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6575 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6576 (unsigned char *) &ipa_trace_buffer_ctrl
,
6577 sizeof (struct ipa_trace_buffer_control
)))
6580 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6581 &ipa_trace_buffer_lo
))
6583 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6584 &ipa_trace_buffer_hi
))
6587 /* Offsets are easier to grok for debugging than raw addresses,
6588 especially for the small trace buffer sizes that are useful for
6590 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6591 "endfree=%d wrap=%d hi=%d",
6593 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6594 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6595 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6596 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6597 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6599 /* Note that the IPA's buffer is always circular. */
6601 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6603 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6604 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6606 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6607 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6608 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6609 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6612 tf
= IPA_FIRST_TRACEFRAME ();
6614 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6616 struct tracepoint
*tpoint
;
6617 struct traceframe
*tframe
;
6618 unsigned char *block
;
6619 struct traceframe ipa_tframe
;
6621 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6622 offsetof (struct traceframe
, data
)))
6623 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6625 if (ipa_tframe
.tpnum
== 0)
6626 fatal ("Uploading: No (more) fast traceframes, but "
6627 "ipa_traceframe_count == %u??\n",
6628 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6630 /* Note that this will be incorrect for multi-location
6632 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6634 tframe
= add_traceframe (tpoint
);
6637 trace_buffer_is_full
= 1;
6638 trace_debug ("Uploading: trace buffer is full");
6642 /* Copy the whole set of blocks in one go for now. FIXME:
6643 split this in smaller blocks. */
6644 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6647 if (read_inferior_memory (tf
6648 + offsetof (struct traceframe
, data
),
6649 block
, ipa_tframe
.data_size
))
6650 error ("Uploading: Couldn't read traceframe data at %s\n",
6651 paddress (tf
+ offsetof (struct traceframe
, data
)));
6654 trace_debug ("Uploading: traceframe didn't fit");
6655 finish_traceframe (tframe
);
6658 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6660 /* If we freed the traceframe that wrapped around, go back
6661 to the non-wrap case. */
6662 if (tf
< ipa_trace_buffer_ctrl
.start
)
6664 trace_debug ("Lib: Discarding past the wraparound");
6665 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6667 ipa_trace_buffer_ctrl
.start
= tf
;
6668 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6669 ++ipa_traceframe_read_count
;
6671 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6672 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6674 trace_debug ("Lib: buffer is fully empty. "
6675 "Trace buffer [%d] start=%d free=%d endfree=%d",
6677 (int) (ipa_trace_buffer_ctrl
.start
6678 - ipa_trace_buffer_lo
),
6679 (int) (ipa_trace_buffer_ctrl
.free
6680 - ipa_trace_buffer_lo
),
6681 (int) (ipa_trace_buffer_ctrl
.end_free
6682 - ipa_trace_buffer_lo
));
6684 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6685 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6686 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6687 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6690 trace_debug ("Uploaded a traceframe\n"
6691 "Lib: Trace buffer [%d] start=%d free=%d "
6692 "endfree=%d wrap=%d hi=%d",
6694 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6695 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6696 (int) (ipa_trace_buffer_ctrl
.end_free
6697 - ipa_trace_buffer_lo
),
6698 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6699 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6702 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6703 (unsigned char *) &ipa_trace_buffer_ctrl
,
6704 sizeof (struct ipa_trace_buffer_control
)))
6707 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6708 ipa_traceframe_read_count
);
6710 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6713 cancel_breakpoints ();
6715 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6716 about_to_request_buffer_space_bkpt
= NULL
;
6720 if (trace_buffer_is_full
)
6725 #ifdef IN_PROCESS_AGENT
6727 IP_AGENT_EXPORT
int ust_loaded
;
6728 IP_AGENT_EXPORT
char cmd_buf
[CMD_BUF_SIZE
];
6732 /* Static tracepoints. */
6734 /* UST puts a "struct tracepoint" in the global namespace, which
6735 conflicts with our tracepoint. Arguably, being a library, it
6736 shouldn't take ownership of such a generic name. We work around it
6738 #define tracepoint ust_tracepoint
6739 #include <ust/ust.h>
6742 extern int serialize_to_text (char *outbuf
, int bufsize
,
6743 const char *fmt
, va_list ap
);
6745 #define GDB_PROBE_NAME "gdb"
6747 /* We dynamically search for the UST symbols instead of linking them
6748 in. This lets the user decide if the application uses static
6749 tracepoints, instead of always pulling libust.so in. This vector
6750 holds pointers to all functions we care about. */
6754 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6755 const char *fmt
, va_list ap
);
6757 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6758 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6760 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6762 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6765 void (*marker_iter_start
) (struct marker_iter
*iter
);
6766 void (*marker_iter_next
) (struct marker_iter
*iter
);
6767 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6768 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6773 /* Cast through typeof to catch incompatible API changes. Since UST
6774 only builds with gcc, we can freely use gcc extensions here
6776 #define GET_UST_SYM(SYM) \
6779 if (ust_ops.SYM == NULL) \
6780 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6781 if (ust_ops.SYM == NULL) \
6785 #define USTF(SYM) ust_ops.SYM
6787 /* Get pointers to all libust.so functions we care about. */
6792 GET_UST_SYM (serialize_to_text
);
6794 GET_UST_SYM (ltt_probe_register
);
6795 GET_UST_SYM (ltt_probe_unregister
);
6796 GET_UST_SYM (ltt_marker_connect
);
6797 GET_UST_SYM (ltt_marker_disconnect
);
6799 GET_UST_SYM (marker_iter_start
);
6800 GET_UST_SYM (marker_iter_next
);
6801 GET_UST_SYM (marker_iter_stop
);
6802 GET_UST_SYM (marker_iter_reset
);
6808 /* Given an UST marker, return the matching gdb static tracepoint.
6809 The match is done by address. */
6811 static struct tracepoint
*
6812 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6814 struct tracepoint
*tpoint
;
6816 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6818 if (tpoint
->type
!= static_tracepoint
)
6821 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6828 /* The probe function we install on lttng/ust markers. Whenever a
6829 probed ust marker is hit, this function is called. This is similar
6830 to gdb_collect, only for static tracepoints, instead of fast
6834 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6835 struct registers
*regs
, void *call_private
,
6836 const char *fmt
, va_list *args
)
6838 struct tracepoint
*tpoint
;
6839 struct static_tracepoint_ctx ctx
;
6841 /* Don't do anything until the trace run is completely set up. */
6844 trace_debug ("gdb_probe: not tracing\n");
6848 ctx
.base
.type
= static_tracepoint
;
6849 ctx
.regcache_initted
= 0;
6854 /* Wrap the regblock in a register cache (in the stack, we don't
6855 want to malloc here). */
6856 ctx
.regspace
= alloca (register_cache_size ());
6857 if (ctx
.regspace
== NULL
)
6859 trace_debug ("Trace buffer block allocation failed, skipping");
6863 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6866 trace_debug ("gdb_probe: marker not known: "
6867 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6868 mdata
->location
, mdata
->channel
,
6869 mdata
->name
, mdata
->format
);
6873 if (!tpoint
->enabled
)
6875 trace_debug ("gdb_probe: tracepoint disabled");
6879 ctx
.tpoint
= tpoint
;
6881 trace_debug ("gdb_probe: collecting marker: "
6882 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6883 mdata
->location
, mdata
->channel
,
6884 mdata
->name
, mdata
->format
);
6886 /* Test the condition if present, and collect if true. */
6887 if (tpoint
->cond
== NULL
6888 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6891 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6892 tpoint
->address
, tpoint
);
6894 if (stopping_tracepoint
6895 || trace_buffer_is_full
6896 || expr_eval_result
!= expr_eval_no_error
)
6901 /* If there was a condition and it evaluated to false, the only
6902 way we would stop tracing is if there was an error during
6903 condition expression evaluation. */
6904 if (expr_eval_result
!= expr_eval_no_error
)
6909 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6910 static tracepoint string data. This is a string passed to the
6911 tracing library by the user, at the time of the tracepoint marker
6912 call. E.g., in the UST marker call:
6914 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6916 the collected data is "str FOOBAZ".
6920 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6922 struct tracepoint
*tpoint
,
6923 struct traceframe
*tframe
)
6925 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6926 unsigned char *bufspace
;
6929 unsigned short blocklen
;
6933 trace_debug ("Wanted to collect static trace data, "
6934 "but there's no static trace data");
6938 va_copy (copy
, *umd
->args
);
6939 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6942 trace_debug ("Want to collect ust data");
6944 /* 'S' + size + string */
6945 bufspace
= add_traceframe_block (tframe
,
6946 1 + sizeof (blocklen
) + size
+ 1);
6947 if (bufspace
== NULL
)
6949 trace_debug ("Trace buffer block allocation failed, skipping");
6953 /* Identify a static trace data block. */
6956 blocklen
= size
+ 1;
6957 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6959 va_copy (copy
, *umd
->args
);
6960 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6961 size
+ 1, umd
->fmt
, copy
);
6964 trace_debug ("Storing static tracepoint data in regblock: %s",
6965 bufspace
+ 1 + sizeof (blocklen
));
6968 /* The probe to register with lttng/ust. */
6969 static struct ltt_available_probe gdb_ust_probe
=
6976 #endif /* HAVE_UST */
6977 #endif /* IN_PROCESS_AGENT */
6981 #include <sys/socket.h>
6984 #ifndef UNIX_PATH_MAX
6985 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6988 /* Where we put the socked used for synchronization. */
6989 #define SOCK_DIR P_tmpdir
6991 #endif /* HAVE_UST */
6993 #ifndef IN_PROCESS_AGENT
6998 gdb_ust_connect_sync_socket (int pid
)
7000 struct sockaddr_un addr
;
7002 char path
[UNIX_PATH_MAX
];
7004 res
= xsnprintf (path
, UNIX_PATH_MAX
, "%s/gdb_ust%d", SOCK_DIR
, pid
);
7005 if (res
>= UNIX_PATH_MAX
)
7007 trace_debug ("string overflow allocating socket name");
7011 res
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
7014 warning ("error opening sync socket: %s\n", strerror (errno
));
7018 addr
.sun_family
= AF_UNIX
;
7020 res
= xsnprintf (addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
7021 if (res
>= UNIX_PATH_MAX
)
7023 warning ("string overflow allocating socket name\n");
7028 res
= connect (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
7031 warning ("error connecting sync socket (%s): %s. "
7032 "Make sure the directory exists and that it is writable.",
7033 path
, strerror (errno
));
7041 /* Resume thread PTID. */
7044 resume_thread (ptid_t ptid
)
7046 struct thread_resume resume_info
;
7048 resume_info
.thread
= ptid
;
7049 resume_info
.kind
= resume_continue
;
7050 resume_info
.sig
= TARGET_SIGNAL_0
;
7051 (*the_target
->resume
) (&resume_info
, 1);
7054 /* Stop thread PTID. */
7057 stop_thread (ptid_t ptid
)
7059 struct thread_resume resume_info
;
7061 resume_info
.thread
= ptid
;
7062 resume_info
.kind
= resume_stop
;
7063 resume_info
.sig
= TARGET_SIGNAL_0
;
7064 (*the_target
->resume
) (&resume_info
, 1);
7067 /* Ask the in-process agent to run a command. Since we don't want to
7068 have to handle the IPA hitting breakpoints while running the
7069 command, we pause all threads, remove all breakpoints, and then set
7070 the helper thread re-running. We communicate with the helper
7071 thread by means of direct memory xfering, and a socket for
7075 run_inferior_command (char *cmd
)
7079 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
7081 ptid_t ptid
= null_ptid
;
7083 trace_debug ("run_inferior_command: running: %s", cmd
);
7086 uninsert_all_breakpoints ();
7088 if (read_inferior_integer (ipa_sym_addrs
.addr_helper_thread_id
, &tid
))
7090 warning ("Error reading helper thread's id in lib");
7096 warning ("helper thread not initialized yet");
7100 if (write_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
7101 (unsigned char *) cmd
, strlen (cmd
) + 1))
7103 warning ("Error writing command");
7107 ptid
= ptid_build (pid
, tid
, 0);
7109 resume_thread (ptid
);
7111 fd
= gdb_ust_connect_sync_socket (pid
);
7117 trace_debug ("signalling helper thread");
7121 ret
= write (fd
, buf
, 1);
7122 } while (ret
== -1 && errno
== EINTR
);
7124 trace_debug ("waiting for helper thread's response");
7128 ret
= read (fd
, buf
, 1);
7129 } while (ret
== -1 && errno
== EINTR
);
7133 trace_debug ("helper thread's response received");
7138 /* Need to read response with the inferior stopped. */
7139 if (!ptid_equal (ptid
, null_ptid
))
7141 int was_non_stop
= non_stop
;
7142 struct target_waitstatus status
;
7146 mywait (ptid
, &status
, 0, 0);
7147 non_stop
= was_non_stop
;
7152 if (read_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
7153 (unsigned char *) cmd
, CMD_BUF_SIZE
))
7155 warning ("Error reading command response");
7160 trace_debug ("run_inferior_command: response: %s", cmd
);
7164 reinsert_all_breakpoints ();
7170 #else /* HAVE_UST */
7173 run_inferior_command (char *cmd
)
7178 #endif /* HAVE_UST */
7180 #else /* !IN_PROCESS_AGENT */
7182 /* Thread ID of the helper thread. GDBserver reads this to know which
7183 is the help thread. This is an LWP id on Linux. */
7184 int helper_thread_id
;
7189 init_named_socket (const char *name
)
7192 struct sockaddr_un addr
;
7194 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
7197 warning ("socket creation failed: %s", strerror (errno
));
7201 addr
.sun_family
= AF_UNIX
;
7203 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
7204 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
7206 result
= access (name
, F_OK
);
7210 result
= unlink (name
);
7213 warning ("unlink failed: %s", strerror (errno
));
7217 warning ("socket %s already exists; overwriting", name
);
7220 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
7223 warning ("bind failed: %s", strerror (errno
));
7228 result
= listen (fd
, 1);
7231 warning ("listen: %s", strerror (errno
));
7240 gdb_ust_socket_init (void)
7243 char name
[UNIX_PATH_MAX
];
7245 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
7246 SOCK_DIR
, getpid ());
7247 if (result
>= UNIX_PATH_MAX
)
7249 trace_debug ("string overflow allocating socket name");
7253 fd
= init_named_socket (name
);
7255 warning ("Error initializing named socket (%s) for communication with the "
7256 "ust helper thread. Check that directory exists and that it "
7257 "is writable.", name
);
7262 /* Return an hexstr version of the STR C string, fit for sending to
7266 cstr_to_hexstr (const char *str
)
7268 int len
= strlen (str
);
7269 char *hexstr
= xmalloc (len
* 2 + 1);
7270 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
7274 /* The next marker to be returned on a qTsSTM command. */
7275 static const struct marker
*next_st
;
7277 /* Returns the first known marker. */
7282 struct marker_iter iter
;
7284 USTF(marker_iter_reset
) (&iter
);
7285 USTF(marker_iter_start
) (&iter
);
7290 /* Returns the marker following M. */
7292 const struct marker
*
7293 next_marker (const struct marker
*m
)
7295 struct marker_iter iter
;
7297 USTF(marker_iter_reset
) (&iter
);
7298 USTF(marker_iter_start
) (&iter
);
7300 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7302 if (iter
.marker
== m
)
7304 USTF(marker_iter_next
) (&iter
);
7312 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
7316 response_ust_marker (char *packet
, const struct marker
*st
)
7318 char *strid
, *format
, *tmp
;
7320 next_st
= next_marker (st
);
7322 tmp
= xmalloc (strlen (st
->channel
) + 1 +
7323 strlen (st
->name
) + 1);
7324 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
7326 strid
= cstr_to_hexstr (tmp
);
7329 format
= cstr_to_hexstr (st
->format
);
7331 sprintf (packet
, "m%s:%s:%s",
7332 paddress ((uintptr_t) st
->location
),
7340 /* Return the first static tracepoint, and initialize the state
7341 machine that will iterate through all the static tracepoints. */
7344 cmd_qtfstm (char *packet
)
7346 trace_debug ("Returning first trace state variable definition");
7348 if (first_marker ())
7349 response_ust_marker (packet
, first_marker ());
7351 strcpy (packet
, "l");
7354 /* Return additional trace state variable definitions. */
7357 cmd_qtsstm (char *packet
)
7359 trace_debug ("Returning static tracepoint");
7362 response_ust_marker (packet
, next_st
);
7364 strcpy (packet
, "l");
7367 /* Disconnect the GDB probe from a marker at a given address. */
7370 unprobe_marker_at (char *packet
)
7374 struct marker_iter iter
;
7376 p
+= sizeof ("unprobe_marker_at:") - 1;
7378 p
= unpack_varlen_hex (p
, &address
);
7380 USTF(marker_iter_reset
) (&iter
);
7381 USTF(marker_iter_start
) (&iter
);
7382 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7383 if ((uintptr_t ) iter
.marker
->location
== address
)
7387 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7391 warning ("could not disable marker %s/%s",
7392 iter
.marker
->channel
, iter
.marker
->name
);
7397 /* Connect the GDB probe to a marker at a given address. */
7400 probe_marker_at (char *packet
)
7404 struct marker_iter iter
;
7407 p
+= sizeof ("probe_marker_at:") - 1;
7409 p
= unpack_varlen_hex (p
, &address
);
7411 USTF(marker_iter_reset
) (&iter
);
7413 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7415 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7416 if ((uintptr_t ) m
->location
== address
)
7420 trace_debug ("found marker for address. "
7421 "ltt_marker_connect (marker = %s/%s)",
7422 m
->channel
, m
->name
);
7424 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7426 if (result
&& result
!= -EEXIST
)
7427 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7428 m
->channel
, m
->name
, -result
);
7432 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7433 m
->channel
, m
->name
);
7437 strcpy (packet
, "OK");
7441 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7446 cmd_qtstmat (char *packet
)
7450 struct marker_iter iter
;
7453 p
+= sizeof ("qTSTMat:") - 1;
7455 p
= unpack_varlen_hex (p
, &address
);
7457 USTF(marker_iter_reset
) (&iter
);
7459 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7461 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7462 if ((uintptr_t ) m
->location
== address
)
7464 response_ust_marker (packet
, m
);
7468 strcpy (packet
, "l");
7473 gdb_ust_thread (void *arg
)
7479 listen_fd
= gdb_ust_socket_init ();
7482 if (helper_thread_id
== 0)
7483 helper_thread_id
= syscall (SYS_gettid
);
7486 if (listen_fd
== -1)
7488 warning ("could not create sync socket\n");
7495 struct sockaddr_un sockaddr
;
7500 tmp
= sizeof (sockaddr
);
7504 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7506 /* It seems an ERESTARTSYS can escape out of accept. */
7507 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7511 warning ("Accept returned %d, error: %s\n",
7512 fd
, strerror (errno
));
7518 ret
= read (fd
, buf
, 1);
7519 } while (ret
== -1 && errno
== EINTR
);
7523 warning ("reading socket (fd=%d) failed with %s",
7524 fd
, strerror (errno
));
7531 if (strcmp ("qTfSTM", cmd_buf
) == 0)
7533 cmd_qtfstm (cmd_buf
);
7535 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7537 cmd_qtsstm (cmd_buf
);
7539 else if (strncmp ("unprobe_marker_at:",
7541 sizeof ("unprobe_marker_at:") - 1) == 0)
7543 unprobe_marker_at (cmd_buf
);
7545 else if (strncmp ("probe_marker_at:",
7547 sizeof ("probe_marker_at:") - 1) == 0)
7549 probe_marker_at (cmd_buf
);
7551 else if (strncmp ("qTSTMat:",
7553 sizeof ("qTSTMat:") - 1) == 0)
7555 cmd_qtstmat (cmd_buf
);
7557 else if (strcmp (cmd_buf
, "help") == 0)
7559 strcpy (cmd_buf
, "for help, press F1\n");
7562 strcpy (cmd_buf
, "");
7586 /* We want the helper thread to be as transparent as possible, so
7587 have it inherit an all-signals-blocked mask. */
7589 sigfillset (&new_mask
);
7590 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7592 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7594 res
= pthread_create (&thread
,
7599 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7601 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7603 while (helper_thread_id
== 0)
7606 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7609 #endif /* HAVE_UST */
7611 #include <sys/mman.h>
7614 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7615 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7616 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7618 static void __attribute__ ((constructor
))
7619 initialize_tracepoint_ftlib (void)
7621 initialize_tracepoint ();
7628 #endif /* IN_PROCESS_AGENT */
7631 tsv_get_timestamp (void)
7635 if (gettimeofday (&tv
, 0) != 0)
7638 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7642 initialize_tracepoint (void)
7644 /* There currently no way to change the buffer size. */
7645 const int sizeOfBuffer
= 5 * 1024 * 1024;
7646 unsigned char *buf
= xmalloc (sizeOfBuffer
);
7647 init_trace_buffer (buf
, sizeOfBuffer
);
7649 /* Wire trace state variable 1 to be the timestamp. This will be
7650 uploaded to GDB upon connection and become one of its trace state
7651 variables. (In case you're wondering, if GDB already has a trace
7652 variable numbered 1, it will be renumbered.) */
7653 create_trace_state_variable (1, 0);
7654 set_trace_state_variable_name (1, "trace_timestamp");
7655 set_trace_state_variable_getter (1, tsv_get_timestamp
);
7657 #ifdef IN_PROCESS_AGENT
7660 pagesize
= sysconf (_SC_PAGE_SIZE
);
7664 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7666 /* Allocate scratch buffer aligned on a page boundary. */
7667 gdb_jump_pad_buffer
= memalign (pagesize
, pagesize
* 20);
7668 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* 20;
7670 /* Make it writable and executable. */
7671 if (mprotect (gdb_jump_pad_buffer
, pagesize
* 20,
7672 PROT_READ
| PROT_WRITE
| PROT_EXEC
) != 0)
7674 initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
7675 gdb_jump_pad_buffer
, pagesize
* 20, strerror (errno
));
7678 initialize_low_tracepoint ();