1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 /* This file is built for both 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 gdb_agent_get_trace_state_variable_value
134 # define set_trace_state_variable_value gdb_agent_set_trace_state_variable_value
135 # define ust_loaded gdb_agent_ust_loaded
136 # define helper_thread_id gdb_agent_helper_thread_id
137 # define cmd_buf gdb_agent_cmd_buf
140 #ifndef IN_PROCESS_AGENT
142 /* Addresses of in-process agent's symbols GDBserver cares about. */
144 struct ipa_sym_addresses
146 CORE_ADDR addr_gdb_tp_heap_buffer
;
147 CORE_ADDR addr_gdb_jump_pad_buffer
;
148 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
149 CORE_ADDR addr_collecting
;
150 CORE_ADDR addr_gdb_collect
;
151 CORE_ADDR addr_stop_tracing
;
152 CORE_ADDR addr_flush_trace_buffer
;
153 CORE_ADDR addr_about_to_request_buffer_space
;
154 CORE_ADDR addr_trace_buffer_is_full
;
155 CORE_ADDR addr_stopping_tracepoint
;
156 CORE_ADDR addr_expr_eval_result
;
157 CORE_ADDR addr_error_tracepoint
;
158 CORE_ADDR addr_tracepoints
;
159 CORE_ADDR addr_tracing
;
160 CORE_ADDR addr_trace_buffer_ctrl
;
161 CORE_ADDR addr_trace_buffer_ctrl_curr
;
162 CORE_ADDR addr_trace_buffer_lo
;
163 CORE_ADDR addr_trace_buffer_hi
;
164 CORE_ADDR addr_traceframe_read_count
;
165 CORE_ADDR addr_traceframe_write_count
;
166 CORE_ADDR addr_traceframes_created
;
167 CORE_ADDR addr_trace_state_variables
;
168 CORE_ADDR addr_get_raw_reg
;
169 CORE_ADDR addr_get_trace_state_variable_value
;
170 CORE_ADDR addr_set_trace_state_variable_value
;
171 CORE_ADDR addr_ust_loaded
;
172 CORE_ADDR addr_helper_thread_id
;
173 CORE_ADDR addr_cmd_buf
;
176 #define STRINGIZE_1(STR) #STR
177 #define STRINGIZE(STR) STRINGIZE_1(STR)
178 #define IPA_SYM(SYM) \
180 STRINGIZE (gdb_agent_ ## SYM), \
181 offsetof (struct ipa_sym_addresses, addr_ ## SYM) \
190 IPA_SYM(gdb_tp_heap_buffer
),
191 IPA_SYM(gdb_jump_pad_buffer
),
192 IPA_SYM(gdb_jump_pad_buffer_end
),
194 IPA_SYM(gdb_collect
),
195 IPA_SYM(stop_tracing
),
196 IPA_SYM(flush_trace_buffer
),
197 IPA_SYM(about_to_request_buffer_space
),
198 IPA_SYM(trace_buffer_is_full
),
199 IPA_SYM(stopping_tracepoint
),
200 IPA_SYM(expr_eval_result
),
201 IPA_SYM(error_tracepoint
),
202 IPA_SYM(tracepoints
),
204 IPA_SYM(trace_buffer_ctrl
),
205 IPA_SYM(trace_buffer_ctrl_curr
),
206 IPA_SYM(trace_buffer_lo
),
207 IPA_SYM(trace_buffer_hi
),
208 IPA_SYM(traceframe_read_count
),
209 IPA_SYM(traceframe_write_count
),
210 IPA_SYM(traceframes_created
),
211 IPA_SYM(trace_state_variables
),
212 IPA_SYM(get_raw_reg
),
213 IPA_SYM(get_trace_state_variable_value
),
214 IPA_SYM(set_trace_state_variable_value
),
216 IPA_SYM(helper_thread_id
),
220 struct ipa_sym_addresses ipa_sym_addrs
;
222 int all_tracepoint_symbols_looked_up
;
225 in_process_agent_loaded (void)
227 return all_tracepoint_symbols_looked_up
;
230 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
232 /* Returns true if both the in-process agent library and the static
233 tracepoints libraries are loaded in the inferior. */
236 in_process_agent_loaded_ust (void)
240 if (!in_process_agent_loaded ())
242 warning ("In-process agent not loaded");
246 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
248 warning ("Error reading ust_loaded in lib");
256 write_e_ipa_not_loaded (char *buffer
)
259 "E.In-process agent library not loaded in process. "
260 "Fast and static tracepoints unavailable.");
263 /* Write an error to BUFFER indicating that UST isn't loaded in the
267 write_e_ust_not_loaded (char *buffer
)
271 "E.UST library not loaded in process. "
272 "Static tracepoints unavailable.");
274 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
278 /* If the in-process agent library isn't loaded in the inferior, write
279 an error to BUFFER, and return 1. Otherwise, return 0. */
282 maybe_write_ipa_not_loaded (char *buffer
)
284 if (!in_process_agent_loaded ())
286 write_e_ipa_not_loaded (buffer
);
292 /* If the in-process agent library and the ust (static tracepoints)
293 library aren't loaded in the inferior, write an error to BUFFER,
294 and return 1. Otherwise, return 0. */
297 maybe_write_ipa_ust_not_loaded (char *buffer
)
299 if (!in_process_agent_loaded ())
301 write_e_ipa_not_loaded (buffer
);
304 else if (!in_process_agent_loaded_ust ())
306 write_e_ust_not_loaded (buffer
);
312 /* Cache all future symbols that the tracepoints module might request.
313 We can not request symbols at arbitrary states in the remote
314 protocol, only when the client tells us that new symbols are
315 available. So when we load the in-process library, make sure to
316 check the entire list. */
319 tracepoint_look_up_symbols (void)
324 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
= all_ok
;
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 gdb_agent_op_float
= 0x01,
472 gdb_agent_op_add
= 0x02,
473 gdb_agent_op_sub
= 0x03,
474 gdb_agent_op_mul
= 0x04,
475 gdb_agent_op_div_signed
= 0x05,
476 gdb_agent_op_div_unsigned
= 0x06,
477 gdb_agent_op_rem_signed
= 0x07,
478 gdb_agent_op_rem_unsigned
= 0x08,
479 gdb_agent_op_lsh
= 0x09,
480 gdb_agent_op_rsh_signed
= 0x0a,
481 gdb_agent_op_rsh_unsigned
= 0x0b,
482 gdb_agent_op_trace
= 0x0c,
483 gdb_agent_op_trace_quick
= 0x0d,
484 gdb_agent_op_log_not
= 0x0e,
485 gdb_agent_op_bit_and
= 0x0f,
486 gdb_agent_op_bit_or
= 0x10,
487 gdb_agent_op_bit_xor
= 0x11,
488 gdb_agent_op_bit_not
= 0x12,
489 gdb_agent_op_equal
= 0x13,
490 gdb_agent_op_less_signed
= 0x14,
491 gdb_agent_op_less_unsigned
= 0x15,
492 gdb_agent_op_ext
= 0x16,
493 gdb_agent_op_ref8
= 0x17,
494 gdb_agent_op_ref16
= 0x18,
495 gdb_agent_op_ref32
= 0x19,
496 gdb_agent_op_ref64
= 0x1a,
497 gdb_agent_op_ref_float
= 0x1b,
498 gdb_agent_op_ref_double
= 0x1c,
499 gdb_agent_op_ref_long_double
= 0x1d,
500 gdb_agent_op_l_to_d
= 0x1e,
501 gdb_agent_op_d_to_l
= 0x1f,
502 gdb_agent_op_if_goto
= 0x20,
503 gdb_agent_op_goto
= 0x21,
504 gdb_agent_op_const8
= 0x22,
505 gdb_agent_op_const16
= 0x23,
506 gdb_agent_op_const32
= 0x24,
507 gdb_agent_op_const64
= 0x25,
508 gdb_agent_op_reg
= 0x26,
509 gdb_agent_op_end
= 0x27,
510 gdb_agent_op_dup
= 0x28,
511 gdb_agent_op_pop
= 0x29,
512 gdb_agent_op_zero_ext
= 0x2a,
513 gdb_agent_op_swap
= 0x2b,
514 gdb_agent_op_getv
= 0x2c,
515 gdb_agent_op_setv
= 0x2d,
516 gdb_agent_op_tracev
= 0x2e,
517 gdb_agent_op_trace16
= 0x30,
521 static const char *gdb_agent_op_names
[gdb_agent_op_last
] =
578 unsigned char *bytes
;
581 /* Base action. Concrete actions inherit this. */
583 struct tracepoint_action
588 /* An 'M' (collect memory) action. */
589 struct collect_memory_action
591 struct tracepoint_action base
;
598 /* An 'R' (collect registers) action. */
600 struct collect_registers_action
602 struct tracepoint_action base
;
605 /* An 'X' (evaluate expression) action. */
607 struct eval_expr_action
609 struct tracepoint_action base
;
611 struct agent_expr
*expr
;
614 /* An 'L' (collect static trace data) action. */
615 struct collect_static_trace_data_action
617 struct tracepoint_action base
;
620 /* This structure describes a piece of the source-level definition of
621 the tracepoint. The contents are not interpreted by the target,
622 but preserved verbatim for uploading upon reconnection. */
626 /* The type of string, such as "cond" for a conditional. */
629 /* The source-level string itself. For the sake of target
630 debugging, we store it in plaintext, even though it is always
631 transmitted in hex. */
634 /* Link to the next one in the list. We link them in the order
635 received, in case some make up an ordered list of commands or
637 struct source_string
*next
;
642 /* Trap based tracepoint. */
645 /* A fast tracepoint implemented with a jump instead of a trap. */
648 /* A static tracepoint, implemented by a program call into a tracing
653 struct tracepoint_hit_ctx
;
655 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
658 /* The definition of a tracepoint. */
660 /* Tracepoints may have multiple locations, each at a different
661 address. This can occur with optimizations, template
662 instantiation, etc. Since the locations may be in different
663 scopes, the conditions and actions may be different for each
664 location. Our target version of tracepoints is more like GDB's
665 notion of "breakpoint locations", but we have almost nothing that
666 is not per-location, so we bother having two kinds of objects. The
667 key consequence is that numbers are not unique, and that it takes
668 both number and address to identify a tracepoint uniquely. */
672 /* The number of the tracepoint, as specified by GDB. Several
673 tracepoint objects here may share a number. */
676 /* Address at which the tracepoint is supposed to trigger. Several
677 tracepoints may share an address. */
680 /* Tracepoint type. */
681 enum tracepoint_type type
;
683 /* True if the tracepoint is currently enabled. */
686 /* The number of single steps that will be performed after each
690 /* The number of times the tracepoint may be hit before it will
691 terminate the entire tracing run. */
694 /* Pointer to the agent expression that is the tracepoint's
695 conditional, or NULL if the tracepoint is unconditional. */
696 struct agent_expr
*cond
;
698 /* The list of actions to take when the tracepoint triggers. */
700 struct tracepoint_action
**actions
;
702 /* Count of the times we've hit this tracepoint during the run.
703 Note that while-stepping steps are not counted as "hits". */
706 CORE_ADDR compiled_cond
;
708 /* Link to the next tracepoint in the list. */
709 struct tracepoint
*next
;
711 #ifndef IN_PROCESS_AGENT
712 /* The list of actions to take when the tracepoint triggers, in
713 string/packet form. */
716 /* The collection of strings that describe the tracepoint as it was
717 entered into GDB. These are not used by the target, but are
718 reported back to GDB upon reconnection. */
719 struct source_string
*source_strings
;
721 /* The number of bytes displaced by fast tracepoints. It may subsume
722 multiple instructions, for multi-byte fast tracepoints. This
723 field is only valid for fast tracepoints. */
726 /* Only for fast tracepoints. */
727 CORE_ADDR obj_addr_on_target
;
729 /* Address range where the original instruction under a fast
730 tracepoint was relocated to. (_end is actually one byte past
732 CORE_ADDR adjusted_insn_addr
;
733 CORE_ADDR adjusted_insn_addr_end
;
735 /* The address range of the piece of the jump pad buffer that was
736 assigned to this fast tracepoint. (_end is actually one byte
739 CORE_ADDR jump_pad_end
;
741 /* The list of actions to take while in a stepping loop. These
742 fields are only valid for patch-based tracepoints. */
743 int num_step_actions
;
744 struct tracepoint_action
**step_actions
;
745 /* Same, but in string/packet form. */
746 char **step_actions_str
;
748 /* Handle returned by the breakpoint or tracepoint module when we
749 inserted the trap or jump, or hooked into a static tracepoint.
750 NULL if we haven't inserted it yet. */
756 #ifndef IN_PROCESS_AGENT
758 /* Given `while-stepping', a thread may be collecting data for more
759 than one tracepoint simultaneously. On the other hand, the same
760 tracepoint with a while-stepping action may be hit by more than one
761 thread simultaneously (but not quite, each thread could be handling
762 a different step). Each thread holds a list of these objects,
763 representing the current step of each while-stepping action being
768 struct wstep_state
*next
;
770 /* The tracepoint number. */
772 /* The tracepoint's address. */
773 CORE_ADDR tp_address
;
775 /* The number of the current step in this 'while-stepping'
782 /* The linked list of all tracepoints. Marked explicitly as used as
783 the in-process library doesn't use it for the fast tracepoints
785 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
787 #ifndef IN_PROCESS_AGENT
789 /* Pointer to the last tracepoint in the list, new tracepoints are
790 linked in at the end. */
792 static struct tracepoint
*last_tracepoint
;
795 /* The first tracepoint to exceed its pass count. */
797 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
799 /* True if the trace buffer is full or otherwise no longer usable. */
801 IP_AGENT_EXPORT
int trace_buffer_is_full
;
803 /* Enumeration of the different kinds of things that can happen during
804 agent expression evaluation. */
806 enum eval_result_type
809 expr_eval_empty_expression
,
810 expr_eval_empty_stack
,
811 expr_eval_stack_overflow
,
812 expr_eval_stack_underflow
,
813 expr_eval_unhandled_opcode
,
814 expr_eval_unrecognized_opcode
,
815 expr_eval_divide_by_zero
,
816 expr_eval_invalid_goto
819 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
821 #ifndef IN_PROCESS_AGENT
823 static const char *eval_result_names
[] =
825 "terror:in the attic", /* this should never be reported */
826 "terror:empty expression",
827 "terror:empty stack",
828 "terror:stack overflow",
829 "terror:stack underflow",
830 "terror:unhandled opcode",
831 "terror:unrecognized opcode",
832 "terror:divide by zero"
837 /* The tracepoint in which the error occurred. */
839 static struct tracepoint
*error_tracepoint
;
841 struct trace_state_variable
843 /* This is the name of the variable as used in GDB. The target
844 doesn't use the name, but needs to have it for saving and
845 reconnection purposes. */
848 /* This number identifies the variable uniquely. Numbers may be
849 assigned either by the target (in the case of builtin variables),
850 or by GDB, and are presumed unique during the course of a trace
854 /* The variable's initial value, a 64-bit signed integer always. */
855 LONGEST initial_value
;
857 /* The variable's value, a 64-bit signed integer always. */
860 /* Pointer to a getter function, used to supply computed values. */
861 LONGEST (*getter
) (void);
863 /* Link to the next variable. */
864 struct trace_state_variable
*next
;
867 /* Linked list of all trace state variables. */
869 #ifdef IN_PROCESS_AGENT
870 struct trace_state_variable
*alloced_trace_state_variables
;
873 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
875 /* The results of tracing go into a fixed-size space known as the
876 "trace buffer". Because usage follows a limited number of
877 patterns, we manage it ourselves rather than with malloc. Basic
878 rules are that we create only one trace frame at a time, each is
879 variable in size, they are never moved once created, and we only
880 discard if we are doing a circular buffer, and then only the oldest
881 ones. Each trace frame includes its own size, so we don't need to
882 link them together, and the trace frame number is relative to the
883 first one, so we don't need to record numbers. A trace frame also
884 records the number of the tracepoint that created it. The data
885 itself is a series of blocks, each introduced by a single character
886 and with a defined format. Each type of block has enough
887 type/length info to allow scanners to jump quickly from one block
888 to the next without reading each byte in the block. */
890 /* Trace buffer management would be simple - advance a free pointer
891 from beginning to end, then stop - were it not for the circular
892 buffer option, which is a useful way to prevent a trace run from
893 stopping prematurely because the buffer filled up. In the circular
894 case, the location of the first trace frame (trace_buffer_start)
895 moves as old trace frames are discarded. Also, since we grow trace
896 frames incrementally as actions are performed, we wrap around to
897 the beginning of the trace buffer. This is per-block, so each
898 block within a trace frame remains contiguous. Things get messy
899 when the wrapped-around trace frame is the one being discarded; the
900 free space ends up in two parts at opposite ends of the buffer. */
903 # if defined(__GNUC__)
904 # define ATTR_PACKED __attribute__ ((packed))
906 # define ATTR_PACKED /* nothing */
910 /* The data collected at a tracepoint hit. This object should be as
911 small as possible, since there may be a great many of them. We do
912 not need to keep a frame number, because they are all sequential
913 and there are no deletions; so the Nth frame in the buffer is
914 always frame number N. */
918 /* Number of the tracepoint that collected this traceframe. A value
919 of 0 indicates the current end of the trace buffer. We make this
920 a 16-bit field because it's never going to happen that GDB's
921 numbering of tracepoints reaches 32,000. */
924 /* The size of the data in this trace frame. We limit this to 32
925 bits, even on a 64-bit target, because it's just implausible that
926 one is validly going to collect 4 gigabytes of data at a single
928 unsigned int data_size
: 32;
930 /* The base of the trace data, which is contiguous from this point. */
931 unsigned char data
[0];
935 /* The traceframe to be used as the source of data to send back to
936 GDB. A value of -1 means to get data from the live program. */
938 int current_traceframe
= -1;
940 /* This flag is true if the trace buffer is circular, meaning that
941 when it fills, the oldest trace frames are discarded in order to
944 #ifndef IN_PROCESS_AGENT
945 static int circular_trace_buffer
;
948 /* Pointer to the block of memory that traceframes all go into. */
950 static unsigned char *trace_buffer_lo
;
952 /* Pointer to the end of the trace buffer, more precisely to the byte
953 after the end of the buffer. */
955 static unsigned char *trace_buffer_hi
;
957 /* Control structure holding the read/write/etc. pointers into the
958 trace buffer. We need more than one of these to implement a
959 transaction-like mechanism to garantees that both GDBserver and the
960 in-process agent can try to change the trace buffer
963 struct trace_buffer_control
965 /* Pointer to the first trace frame in the buffer. In the
966 non-circular case, this is equal to trace_buffer_lo, otherwise it
967 moves around in the buffer. */
968 unsigned char *start
;
970 /* Pointer to the free part of the trace buffer. Note that we clear
971 several bytes at and after this pointer, so that traceframe
972 scans/searches terminate properly. */
975 /* Pointer to the byte after the end of the free part. Note that
976 this may be smaller than trace_buffer_free in the circular case,
977 and means that the free part is in two pieces. Initially it is
978 equal to trace_buffer_hi, then is generally equivalent to
979 trace_buffer_start. */
980 unsigned char *end_free
;
982 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
983 this is the point at which the trace data breaks, and resumes at
988 /* Same as above, to be used by GDBserver when updating the in-process
990 struct ipa_trace_buffer_control
999 /* We have possibly both GDBserver and an inferior thread accessing
1000 the same IPA trace buffer memory. The IPA is the producer (tries
1001 to put new frames in the buffer), while GDBserver occasionally
1002 consumes them, that is, flushes the IPA's buffer into its own
1003 buffer. Both sides need to update the trace buffer control
1004 pointers (current head, tail, etc.). We can't use a global lock to
1005 synchronize the accesses, as otherwise we could deadlock GDBserver
1006 (if the thread holding the lock stops for a signal, say). So
1007 instead of that, we use a transaction scheme where GDBserver writes
1008 always prevail over the IPAs writes, and, we have the IPA detect
1009 the commit failure/overwrite, and retry the whole attempt. This is
1010 mainly implemented by having a global token object that represents
1011 who wrote last to the buffer control structure. We need to freeze
1012 any inferior writing to the buffer while GDBserver touches memory,
1013 so that the inferior can correctly detect that GDBserver had been
1014 there, otherwise, it could mistakingly think its commit was
1015 successful; that's implemented by simply having GDBserver set a
1016 breakpoint the inferior hits if it is the critical region.
1018 There are three cycling trace buffer control structure copies
1019 (buffer head, tail, etc.), with the token object including an index
1020 indicating which is current live copy. The IPA tentatively builds
1021 an updated copy in a non-current control structure, while GDBserver
1022 always clobbers the current version directly. The IPA then tries
1023 to atomically "commit" its version; if GDBserver clobbered the
1024 structure meanwhile, that will fail, and the IPA restarts the
1027 Listing the step in further detail, we have:
1029 In-process agent (producer):
1031 - passes by `about_to_request_buffer_space' breakpoint/lock
1033 - reads current token, extracts current trace buffer control index,
1034 and starts tentatively updating the rightmost one (0->1, 1->2,
1035 2->0). Note that only one inferior thread is executing this code
1036 at any given time, due to an outer lock in the jump pads.
1038 - updates counters, and tries to commit the token.
1040 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1041 leaving the sync region.
1043 - checks if the update was effective.
1045 - if trace buffer was found full, hits flush_trace_buffer
1046 breakpoint, and restarts later afterwards.
1048 GDBserver (consumer):
1050 - sets `about_to_request_buffer_space' breakpoint/lock.
1052 - updates the token unconditionally, using the current buffer
1053 control index, since it knows that the IP agent always writes to
1054 the rightmost, and due to the breakpoint, at most one IP thread
1055 can try to update the trace buffer concurrently to GDBserver, so
1056 there will be no danger of trace buffer control index wrap making
1057 the IPA write to the same index as GDBserver.
1059 - flushes the IP agent's trace buffer completely, and updates the
1060 current trace buffer control structure. GDBserver *always* wins.
1062 - removes the `about_to_request_buffer_space' breakpoint.
1064 The token is stored in the `trace_buffer_ctrl_curr' variable.
1065 Internally, it's bits are defined as:
1067 |-------------+-----+-------------+--------+-------------+--------------|
1068 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1069 |-------------+-----+-------------+--------+-------------+--------------|
1070 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1071 |-------------+-----+-------------+--------+-------------+--------------|
1073 GSB - GDBserver Stamp Bit
1074 PC - Previous Counter
1075 CC - Current Counter
1076 TBCI - Trace Buffer Control Index
1079 An IPA update of `trace_buffer_ctrl_curr' does:
1081 - read CC from the current token, save as PC.
1083 - atomically tries to write PC+1,CC
1085 A GDBserver update of `trace_buffer_ctrl_curr' does:
1087 - reads PC and CC from the current token.
1092 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1093 for the counters described below. The cleared bits are used to
1094 hold the index of the items of the `trace_buffer_ctrl' array that
1096 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1098 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1099 counter, and the `current' counter. */
1101 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1102 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1104 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1105 always stamps this bit as set. */
1106 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1108 #ifdef IN_PROCESS_AGENT
1109 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1110 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1112 # define TRACE_BUFFER_CTRL_CURR \
1113 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1117 /* The GDBserver side agent only needs one instance of this object, as
1118 it doesn't need to sync with itself. Define it as array anyway so
1119 that the rest of the code base doesn't need to care for the
1121 struct trace_buffer_control trace_buffer_ctrl
[1];
1122 # define TRACE_BUFFER_CTRL_CURR 0
1125 /* These are convenience macros used to access the current trace
1126 buffer control in effect. */
1127 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1128 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1129 #define trace_buffer_end_free \
1130 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1131 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1134 /* Macro that returns a pointer to the first traceframe in the buffer. */
1136 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1138 /* Macro that returns a pointer to the next traceframe in the buffer.
1139 If the computed location is beyond the wraparound point, subtract
1140 the offset of the wraparound. */
1142 #define NEXT_TRACEFRAME_1(TF) \
1143 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1145 #define NEXT_TRACEFRAME(TF) \
1146 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1147 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1148 ? (trace_buffer_wrap - trace_buffer_lo) \
1151 /* The difference between these counters represents the total number
1152 of complete traceframes present in the trace buffer. The IP agent
1153 writes to the write count, GDBserver writes to read count. */
1155 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1156 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1158 /* Convenience macro. */
1160 #define traceframe_count \
1161 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1163 /* The count of all traceframes created in the current run, including
1164 ones that were discarded to make room. */
1166 IP_AGENT_EXPORT
int traceframes_created
;
1168 #ifndef IN_PROCESS_AGENT
1170 /* Read-only regions are address ranges whose contents don't change,
1171 and so can be read from target memory even while looking at a trace
1172 frame. Without these, disassembly for instance will likely fail,
1173 because the program code is not usually collected into a trace
1174 frame. This data structure does not need to be very complicated or
1175 particularly efficient, it's only going to be used occasionally,
1176 and only by some commands. */
1178 struct readonly_region
1180 /* The bounds of the region. */
1181 CORE_ADDR start
, end
;
1183 /* Link to the next one. */
1184 struct readonly_region
*next
;
1187 /* Linked list of readonly regions. This list stays in effect from
1188 one tstart to the next. */
1190 static struct readonly_region
*readonly_regions
;
1194 /* The global that controls tracing overall. */
1196 IP_AGENT_EXPORT
int tracing
;
1198 #ifndef IN_PROCESS_AGENT
1200 /* Controls whether tracing should continue after GDB disconnects. */
1202 int disconnected_tracing
;
1204 /* The reason for the last tracing run to have stopped. We initialize
1205 to a distinct string so that GDB can distinguish between "stopped
1206 after running" and "stopped because never run" cases. */
1208 static const char *tracing_stop_reason
= "tnotrun";
1210 static int tracing_stop_tpnum
;
1214 /* Functions local to this file. */
1216 /* Base "class" for tracepoint type specific data to be passed down to
1217 collect_data_at_tracepoint. */
1218 struct tracepoint_hit_ctx
1220 enum tracepoint_type type
;
1223 #ifdef IN_PROCESS_AGENT
1225 /* Fast/jump tracepoint specific data to be passed down to
1226 collect_data_at_tracepoint. */
1227 struct fast_tracepoint_ctx
1229 struct tracepoint_hit_ctx base
;
1231 struct regcache regcache
;
1232 int regcache_initted
;
1233 unsigned char *regspace
;
1235 unsigned char *regs
;
1236 struct tracepoint
*tpoint
;
1239 /* Static tracepoint specific data to be passed down to
1240 collect_data_at_tracepoint. */
1241 struct static_tracepoint_ctx
1243 struct tracepoint_hit_ctx base
;
1245 /* The regcache corresponding to the registers state at the time of
1246 the tracepoint hit. Initialized lazily, from REGS. */
1247 struct regcache regcache
;
1248 int regcache_initted
;
1250 /* The buffer space REGCACHE above uses. We use a separate buffer
1251 instead of letting the regcache malloc for both signal safety and
1252 performance reasons; this is allocated on the stack instead. */
1253 unsigned char *regspace
;
1255 /* The register buffer as passed on by lttng/ust. */
1256 struct registers
*regs
;
1258 /* The "printf" formatter and the args the user passed to the marker
1259 call. We use this to be able to collect "static trace data"
1264 /* The GDB tracepoint matching the probed marker that was "hit". */
1265 struct tracepoint
*tpoint
;
1270 /* Static tracepoint specific data to be passed down to
1271 collect_data_at_tracepoint. */
1272 struct trap_tracepoint_ctx
1274 struct tracepoint_hit_ctx base
;
1276 struct regcache
*regcache
;
1281 #ifndef IN_PROCESS_AGENT
1282 static struct agent_expr
*parse_agent_expr (char **actparm
);
1283 static char *unparse_agent_expr (struct agent_expr
*aexpr
);
1285 static enum eval_result_type
eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1286 struct traceframe
*tframe
,
1287 struct agent_expr
*aexpr
,
1290 static int agent_mem_read (struct traceframe
*tframe
,
1291 unsigned char *to
, CORE_ADDR from
, ULONGEST len
);
1292 static int agent_tsv_read (struct traceframe
*tframe
, int n
);
1294 #ifndef IN_PROCESS_AGENT
1295 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1296 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1299 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1300 struct tracepoint
*tpoint
);
1302 #ifndef IN_PROCESS_AGENT
1303 static void clear_readonly_regions (void);
1304 static void clear_installed_tracepoints (void);
1307 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1309 struct tracepoint
*tpoint
);
1310 #ifndef IN_PROCESS_AGENT
1311 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1313 struct tracepoint
*tpoint
, int current_step
);
1314 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1315 CORE_ADDR
*jump_entry
);
1317 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1319 struct tracepoint
*tpoint
,
1320 struct traceframe
*tframe
,
1321 struct tracepoint_action
*taction
);
1323 #ifndef IN_PROCESS_AGENT
1324 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1327 #if defined(__GNUC__)
1328 # define memory_barrier() asm volatile ("" : : : "memory")
1330 # define memory_barrier() do {} while (0)
1333 /* We only build the IPA if this builtin is supported, and there are
1334 no uses of this in GDBserver itself, so we're safe in defining this
1336 #define cmpxchg(mem, oldval, newval) \
1337 __sync_val_compare_and_swap (mem, oldval, newval)
1339 /* The size in bytes of the buffer used to talk to the IPA helper
1341 #define CMD_BUF_SIZE 1024
1343 /* Record that an error occurred during expression evaluation. */
1346 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1347 enum eval_result_type rtype
)
1349 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1350 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1352 #ifdef IN_PROCESS_AGENT
1353 /* Only record the first error we get. */
1354 if (cmpxchg (&expr_eval_result
,
1356 rtype
) != expr_eval_no_error
)
1359 if (expr_eval_result
!= expr_eval_no_error
)
1363 error_tracepoint
= tpoint
;
1366 /* Trace buffer management. */
1369 clear_trace_buffer (void)
1371 trace_buffer_start
= trace_buffer_lo
;
1372 trace_buffer_free
= trace_buffer_lo
;
1373 trace_buffer_end_free
= trace_buffer_hi
;
1374 trace_buffer_wrap
= trace_buffer_hi
;
1375 /* A traceframe with zeroed fields marks the end of trace data. */
1376 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1377 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1378 traceframe_read_count
= traceframe_write_count
= 0;
1379 traceframes_created
= 0;
1382 #ifndef IN_PROCESS_AGENT
1385 clear_inferior_trace_buffer (void)
1387 CORE_ADDR ipa_trace_buffer_lo
;
1388 CORE_ADDR ipa_trace_buffer_hi
;
1389 struct traceframe ipa_traceframe
= { 0 };
1390 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1392 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1393 &ipa_trace_buffer_lo
);
1394 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1395 &ipa_trace_buffer_hi
);
1397 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1398 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1399 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1400 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1402 /* A traceframe with zeroed fields marks the end of trace data. */
1403 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1404 (unsigned char *) &ipa_trace_buffer_ctrl
,
1405 sizeof (ipa_trace_buffer_ctrl
));
1407 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1409 /* A traceframe with zeroed fields marks the end of trace data. */
1410 write_inferior_memory (ipa_trace_buffer_lo
,
1411 (unsigned char *) &ipa_traceframe
,
1412 sizeof (ipa_traceframe
));
1414 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1415 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1416 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1422 init_trace_buffer (unsigned char *buf
, int bufsize
)
1424 trace_buffer_lo
= buf
;
1425 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1427 clear_trace_buffer ();
1430 #ifdef IN_PROCESS_AGENT
1432 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1433 about_to_request_buffer_space (void)
1435 /* GDBserver places breakpoint here while it goes about to flush
1436 data at random times. */
1437 UNKNOWN_SIDE_EFFECTS();
1442 /* Carve out a piece of the trace buffer, returning NULL in case of
1446 trace_buffer_alloc (size_t amt
)
1448 unsigned char *rslt
;
1449 struct trace_buffer_control
*tbctrl
;
1451 #ifdef IN_PROCESS_AGENT
1452 unsigned int prev
, prev_filtered
;
1453 unsigned int commit_count
;
1454 unsigned int commit
;
1455 unsigned int readout
;
1457 struct traceframe
*oldest
;
1458 unsigned char *new_start
;
1461 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1462 (long) amt
, (long) sizeof (struct traceframe
));
1464 /* Account for the EOB marker. */
1465 amt
+= sizeof (struct traceframe
);
1467 #ifdef IN_PROCESS_AGENT
1471 /* Read the current token and extract the index to try to write to,
1472 storing it in CURR. */
1473 prev
= trace_buffer_ctrl_curr
;
1474 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1475 curr
= prev_filtered
+ 1;
1479 about_to_request_buffer_space ();
1481 /* Start out with a copy of the current state. GDBserver may be
1482 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1483 be able to commit anyway if that happens. */
1484 trace_buffer_ctrl
[curr
]
1485 = trace_buffer_ctrl
[prev_filtered
];
1486 trace_debug ("trying curr=%u", curr
);
1488 /* The GDBserver's agent doesn't need all that syncing, and always
1489 updates TCB 0 (there's only one, mind you). */
1492 tbctrl
= &trace_buffer_ctrl
[curr
];
1494 /* Offsets are easier to grok for debugging than raw addresses,
1495 especially for the small trace buffer sizes that are useful for
1497 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1499 (int) (tbctrl
->start
- trace_buffer_lo
),
1500 (int) (tbctrl
->free
- trace_buffer_lo
),
1501 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1502 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1503 (int) (trace_buffer_hi
- trace_buffer_lo
));
1505 /* The algorithm here is to keep trying to get a contiguous block of
1506 the requested size, possibly discarding older traceframes to free
1507 up space. Since free space might come in one or two pieces,
1508 depending on whether discarded traceframes wrapped around at the
1509 high end of the buffer, we test both pieces after each
1513 /* First, if we have two free parts, try the upper one first. */
1514 if (tbctrl
->end_free
< tbctrl
->free
)
1516 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1517 /* We have enough in the upper part. */
1521 /* Our high part of free space wasn't enough. Give up
1522 on it for now, set wraparound. We will recover the
1523 space later, if/when the wrapped-around traceframe is
1525 trace_debug ("Upper part too small, setting wraparound");
1526 tbctrl
->wrap
= tbctrl
->free
;
1527 tbctrl
->free
= trace_buffer_lo
;
1531 /* The normal case. */
1532 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1535 #ifdef IN_PROCESS_AGENT
1536 /* The IP Agent's buffer is always circular. It isn't used
1537 currently, but `circular_trace_buffer' could represent
1538 GDBserver's mode. If we didn't find space, ask GDBserver to
1541 flush_trace_buffer ();
1545 trace_debug ("gdbserver flushed buffer, retrying");
1549 /* GDBserver cancelled the tracing. Bail out as well. */
1552 /* If we're here, then neither part is big enough, and
1553 non-circular trace buffers are now full. */
1554 if (!circular_trace_buffer
)
1556 trace_debug ("Not enough space in the trace buffer");
1560 trace_debug ("Need more space in the trace buffer");
1562 /* If we have a circular buffer, we can try discarding the
1563 oldest traceframe and see if that helps. */
1564 oldest
= FIRST_TRACEFRAME ();
1565 if (oldest
->tpnum
== 0)
1567 /* Not good; we have no traceframes to free. Perhaps we're
1568 asking for a block that is larger than the buffer? In
1569 any case, give up. */
1570 trace_debug ("No traceframes to discard");
1574 /* We don't run this code in the in-process agent currently.
1575 E.g., we could leave the in-process agent in autonomous
1576 circular mode if we only have fast tracepoints. If we do
1577 that, then this bit becomes racy with GDBserver, which also
1578 writes to this counter. */
1579 --traceframe_write_count
;
1581 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1582 /* If we freed the traceframe that wrapped around, go back
1583 to the non-wrap case. */
1584 if (new_start
< tbctrl
->start
)
1586 trace_debug ("Discarding past the wraparound");
1587 tbctrl
->wrap
= trace_buffer_hi
;
1589 tbctrl
->start
= new_start
;
1590 tbctrl
->end_free
= tbctrl
->start
;
1592 trace_debug ("Discarded a traceframe\n"
1593 "Trace buffer [%d], start=%d free=%d "
1594 "endfree=%d wrap=%d hi=%d",
1596 (int) (tbctrl
->start
- trace_buffer_lo
),
1597 (int) (tbctrl
->free
- trace_buffer_lo
),
1598 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1599 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1600 (int) (trace_buffer_hi
- trace_buffer_lo
));
1602 /* Now go back around the loop. The discard might have resulted
1603 in either one or two pieces of free space, so we want to try
1604 both before freeing any more traceframes. */
1608 /* If we get here, we know we can provide the asked-for space. */
1610 rslt
= tbctrl
->free
;
1612 /* Adjust the request back down, now that we know we have space for
1613 the marker, but don't commit to AMT yet, we may still need to
1614 restart the operation if GDBserver touches the trace buffer
1615 (obviously only important in the in-process agent's version). */
1616 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1618 /* Or not. If GDBserver changed the trace buffer behind our back,
1619 we get to restart a new allocation attempt. */
1621 #ifdef IN_PROCESS_AGENT
1622 /* Build the tentative token. */
1623 commit_count
= (((prev
& 0x0007ff00) + 0x100) & 0x0007ff00);
1624 commit
= (((prev
& 0x0007ff00) << 12)
1628 /* Try to commit it. */
1629 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1630 if (readout
!= prev
)
1632 trace_debug ("GDBserver has touched the trace buffer, restarting."
1633 " (prev=%08x, commit=%08x, readout=%08x)",
1634 prev
, commit
, readout
);
1638 /* Hold your horses here. Even if that change was committed,
1639 GDBserver could come in, and clobber it. We need to hold to be
1640 able to tell if GDBserver clobbers before or after we committed
1641 the change. Whenever GDBserver goes about touching the IPA
1642 buffer, it sets a breakpoint in this routine, so we have a sync
1644 about_to_request_buffer_space ();
1646 /* Check if the change has been effective, even if GDBserver stopped
1647 us at the breakpoint. */
1650 unsigned int refetch
;
1654 refetch
= trace_buffer_ctrl_curr
;
1656 if ((refetch
== commit
1657 || ((refetch
& 0x7ff00000) >> 12) == commit_count
))
1660 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1661 "readout=%08x, refetch=%08x)",
1662 prev
, commit
, readout
, refetch
);
1666 trace_debug ("GDBserver has touched the trace buffer, not effective."
1667 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1668 prev
, commit
, readout
, refetch
);
1674 /* We have a new piece of the trace buffer. Hurray! */
1676 /* Add an EOB marker just past this allocation. */
1677 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1678 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1680 /* Adjust the request back down, now that we know we have space for
1682 amt
-= sizeof (struct traceframe
);
1686 trace_debug ("Allocated %d bytes", (int) amt
);
1687 trace_debug ("Trace buffer [%d] start=%d free=%d "
1688 "endfree=%d wrap=%d hi=%d",
1690 (int) (tbctrl
->start
- trace_buffer_lo
),
1691 (int) (tbctrl
->free
- trace_buffer_lo
),
1692 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1693 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1694 (int) (trace_buffer_hi
- trace_buffer_lo
));
1700 #ifndef IN_PROCESS_AGENT
1702 /* Return the total free space. This is not necessarily the largest
1703 block we can allocate, because of the two-part case. */
1708 if (trace_buffer_free
<= trace_buffer_end_free
)
1709 return trace_buffer_end_free
- trace_buffer_free
;
1711 return ((trace_buffer_end_free
- trace_buffer_lo
)
1712 + (trace_buffer_hi
- trace_buffer_free
));
1715 /* An 'S' in continuation packets indicates remainder are for
1718 static int seen_step_action_flag
;
1720 /* Create a tracepoint (location) with given number and address. */
1722 static struct tracepoint
*
1723 add_tracepoint (int num
, CORE_ADDR addr
)
1725 struct tracepoint
*tpoint
;
1727 tpoint
= xmalloc (sizeof (struct tracepoint
));
1728 tpoint
->number
= num
;
1729 tpoint
->address
= addr
;
1730 tpoint
->numactions
= 0;
1731 tpoint
->actions
= NULL
;
1732 tpoint
->actions_str
= NULL
;
1733 tpoint
->cond
= NULL
;
1734 tpoint
->num_step_actions
= 0;
1735 tpoint
->step_actions
= NULL
;
1736 tpoint
->step_actions_str
= NULL
;
1737 /* Start all off as regular (slow) tracepoints. */
1738 tpoint
->type
= trap_tracepoint
;
1739 tpoint
->orig_size
= -1;
1740 tpoint
->source_strings
= NULL
;
1741 tpoint
->compiled_cond
= 0;
1742 tpoint
->handle
= NULL
;
1743 tpoint
->next
= NULL
;
1745 if (!last_tracepoint
)
1746 tracepoints
= tpoint
;
1748 last_tracepoint
->next
= tpoint
;
1749 last_tracepoint
= tpoint
;
1751 seen_step_action_flag
= 0;
1756 #ifndef IN_PROCESS_AGENT
1758 /* Return the tracepoint with the given number and address, or NULL. */
1760 static struct tracepoint
*
1761 find_tracepoint (int id
, CORE_ADDR addr
)
1763 struct tracepoint
*tpoint
;
1765 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1766 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1772 /* There may be several tracepoints with the same number (because they
1773 are "locations", in GDB parlance); return the next one after the
1774 given tracepoint, or search from the beginning of the list if the
1775 first argument is NULL. */
1777 static struct tracepoint
*
1778 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1780 struct tracepoint
*tpoint
;
1783 tpoint
= prev_tp
->next
;
1785 tpoint
= tracepoints
;
1786 for (; tpoint
; tpoint
= tpoint
->next
)
1787 if (tpoint
->number
== num
)
1796 save_string (const char *str
, size_t len
)
1800 s
= xmalloc (len
+ 1);
1801 memcpy (s
, str
, len
);
1807 /* Append another action to perform when the tracepoint triggers. */
1810 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1816 seen_step_action_flag
= 1;
1824 char *act_start
= act
;
1825 struct tracepoint_action
*action
= NULL
;
1831 struct collect_memory_action
*maction
;
1835 maction
= xmalloc (sizeof *maction
);
1836 maction
->base
.type
= *act
;
1837 action
= &maction
->base
;
1840 is_neg
= (*act
== '-');
1843 act
= unpack_varlen_hex (act
, &basereg
);
1845 act
= unpack_varlen_hex (act
, &maction
->addr
);
1847 act
= unpack_varlen_hex (act
, &maction
->len
);
1848 maction
->basereg
= (is_neg
1851 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1852 pulongest (maction
->len
),
1853 paddress (maction
->addr
), maction
->basereg
);
1858 struct collect_registers_action
*raction
;
1860 raction
= xmalloc (sizeof *raction
);
1861 raction
->base
.type
= *act
;
1862 action
= &raction
->base
;
1864 trace_debug ("Want to collect registers");
1866 /* skip past hex digits of mask for now */
1867 while (isxdigit(*act
))
1873 struct collect_static_trace_data_action
*raction
;
1875 raction
= xmalloc (sizeof *raction
);
1876 raction
->base
.type
= *act
;
1877 action
= &raction
->base
;
1879 trace_debug ("Want to collect static trace data");
1884 trace_debug ("Unexpected step action, ignoring");
1889 struct eval_expr_action
*xaction
;
1891 xaction
= xmalloc (sizeof (*xaction
));
1892 xaction
->base
.type
= *act
;
1893 action
= &xaction
->base
;
1895 trace_debug ("Want to evaluate expression");
1896 xaction
->expr
= parse_agent_expr (&act
);
1900 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1909 if (seen_step_action_flag
)
1911 tpoint
->num_step_actions
++;
1913 tpoint
->step_actions
1914 = xrealloc (tpoint
->step_actions
,
1915 (sizeof (*tpoint
->step_actions
)
1916 * tpoint
->num_step_actions
));
1917 tpoint
->step_actions_str
1918 = xrealloc (tpoint
->step_actions_str
,
1919 (sizeof (*tpoint
->step_actions_str
)
1920 * tpoint
->num_step_actions
));
1921 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1922 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1923 = save_string (act_start
, act
- act_start
);
1927 tpoint
->numactions
++;
1929 = xrealloc (tpoint
->actions
,
1930 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1932 = xrealloc (tpoint
->actions_str
,
1933 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1934 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1935 tpoint
->actions_str
[tpoint
->numactions
- 1]
1936 = save_string (act_start
, act
- act_start
);
1943 /* Find or create a trace state variable with the given number. */
1945 static struct trace_state_variable
*
1946 get_trace_state_variable (int num
)
1948 struct trace_state_variable
*tsv
;
1950 #ifdef IN_PROCESS_AGENT
1951 /* Search for an existing variable. */
1952 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1953 if (tsv
->number
== num
)
1957 /* Search for an existing variable. */
1958 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1959 if (tsv
->number
== num
)
1965 /* Find or create a trace state variable with the given number. */
1967 static struct trace_state_variable
*
1968 create_trace_state_variable (int num
, int gdb
)
1970 struct trace_state_variable
*tsv
;
1972 tsv
= get_trace_state_variable (num
);
1976 /* Create a new variable. */
1977 tsv
= xmalloc (sizeof (struct trace_state_variable
));
1979 tsv
->initial_value
= 0;
1983 #ifdef IN_PROCESS_AGENT
1986 tsv
->next
= alloced_trace_state_variables
;
1987 alloced_trace_state_variables
= tsv
;
1992 tsv
->next
= trace_state_variables
;
1993 trace_state_variables
= tsv
;
1998 IP_AGENT_EXPORT LONGEST
1999 get_trace_state_variable_value (int num
)
2001 struct trace_state_variable
*tsv
;
2003 tsv
= get_trace_state_variable (num
);
2007 trace_debug ("No trace state variable %d, skipping value get", num
);
2011 /* Call a getter function if we have one. While it's tempting to
2012 set up something to only call the getter once per tracepoint hit,
2013 it could run afoul of thread races. Better to let the getter
2014 handle it directly, if necessary to worry about it. */
2016 tsv
->value
= (tsv
->getter
) ();
2018 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2019 num
, plongest (tsv
->value
));
2024 IP_AGENT_EXPORT
void
2025 set_trace_state_variable_value (int num
, LONGEST val
)
2027 struct trace_state_variable
*tsv
;
2029 tsv
= get_trace_state_variable (num
);
2033 trace_debug ("No trace state variable %d, skipping value set", num
);
2041 set_trace_state_variable_name (int num
, const char *name
)
2043 struct trace_state_variable
*tsv
;
2045 tsv
= get_trace_state_variable (num
);
2049 trace_debug ("No trace state variable %d, skipping name set", num
);
2053 tsv
->name
= (char *) name
;
2057 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2059 struct trace_state_variable
*tsv
;
2061 tsv
= get_trace_state_variable (num
);
2065 trace_debug ("No trace state variable %d, skipping getter set", num
);
2069 tsv
->getter
= getter
;
2072 /* Add a raw traceframe for the given tracepoint. */
2074 static struct traceframe
*
2075 add_traceframe (struct tracepoint
*tpoint
)
2077 struct traceframe
*tframe
;
2079 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2084 tframe
->tpnum
= tpoint
->number
;
2085 tframe
->data_size
= 0;
2090 /* Add a block to the traceframe currently being worked on. */
2092 static unsigned char *
2093 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2095 unsigned char *block
;
2100 block
= trace_buffer_alloc (amt
);
2105 tframe
->data_size
+= amt
;
2110 /* Flag that the current traceframe is finished. */
2113 finish_traceframe (struct traceframe
*tframe
)
2115 ++traceframe_write_count
;
2116 ++traceframes_created
;
2119 #ifndef IN_PROCESS_AGENT
2121 /* Given a traceframe number NUM, find the NUMth traceframe in the
2124 static struct traceframe
*
2125 find_traceframe (int num
)
2127 struct traceframe
*tframe
;
2130 for (tframe
= FIRST_TRACEFRAME ();
2132 tframe
= NEXT_TRACEFRAME (tframe
))
2143 get_traceframe_address (struct traceframe
*tframe
)
2146 struct tracepoint
*tpoint
;
2148 addr
= traceframe_get_pc (tframe
);
2153 /* Fallback strategy, will be incorrect for while-stepping frames
2154 and multi-location tracepoints. */
2155 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2156 return tpoint
->address
;
2159 /* Search for the next traceframe whose address is inside or outside
2162 static struct traceframe
*
2163 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2166 struct traceframe
*tframe
;
2169 *tfnump
= current_traceframe
+ 1;
2170 tframe
= find_traceframe (*tfnump
);
2171 /* The search is not supposed to wrap around. */
2178 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2180 tfaddr
= get_traceframe_address (tframe
);
2182 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2183 : (lo
> tfaddr
|| tfaddr
> hi
))
2192 /* Search for the next traceframe recorded by the given tracepoint.
2193 Note that for multi-location tracepoints, this will find whatever
2194 location appears first. */
2196 static struct traceframe
*
2197 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2199 struct traceframe
*tframe
;
2201 *tfnump
= current_traceframe
+ 1;
2202 tframe
= find_traceframe (*tfnump
);
2203 /* The search is not supposed to wrap around. */
2210 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2212 if (tframe
->tpnum
== num
)
2223 #ifndef IN_PROCESS_AGENT
2225 /* Clear all past trace state. */
2228 cmd_qtinit (char *packet
)
2230 struct trace_state_variable
*tsv
, *prev
, *next
;
2232 /* Make sure we don't try to read from a trace frame. */
2233 current_traceframe
= -1;
2235 trace_debug ("Initializing the trace");
2237 clear_installed_tracepoints ();
2238 clear_readonly_regions ();
2241 last_tracepoint
= NULL
;
2243 /* Clear out any leftover trace state variables. Ones with target
2244 defined getters should be kept however. */
2246 tsv
= trace_state_variables
;
2249 trace_debug ("Looking at var %d", tsv
->number
);
2250 if (tsv
->getter
== NULL
)
2256 trace_state_variables
= next
;
2257 trace_debug ("Deleting var %d", tsv
->number
);
2268 clear_trace_buffer ();
2269 clear_inferior_trace_buffer ();
2274 /* Unprobe the UST marker at ADDRESS. */
2277 unprobe_marker_at (CORE_ADDR address
)
2279 char cmd
[CMD_BUF_SIZE
];
2281 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2282 run_inferior_command (cmd
);
2285 /* Restore the program to its pre-tracing state. This routine may be called
2286 in error situations, so it needs to be careful about only restoring
2287 from known-valid bits. */
2290 clear_installed_tracepoints (void)
2292 struct tracepoint
*tpoint
;
2293 struct tracepoint
*prev_stpoint
;
2296 cancel_breakpoints ();
2298 prev_stpoint
= NULL
;
2300 /* Restore any bytes overwritten by tracepoints. */
2301 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2303 if (!tpoint
->enabled
)
2306 /* Catch the case where we might try to remove a tracepoint that
2307 was never actually installed. */
2308 if (tpoint
->handle
== NULL
)
2310 trace_debug ("Tracepoint %d at 0x%s was "
2311 "never installed, nothing to clear",
2312 tpoint
->number
, paddress (tpoint
->address
));
2316 switch (tpoint
->type
)
2318 case trap_tracepoint
:
2319 delete_breakpoint (tpoint
->handle
);
2321 case fast_tracepoint
:
2322 delete_fast_tracepoint_jump (tpoint
->handle
);
2324 case static_tracepoint
:
2325 if (prev_stpoint
!= NULL
2326 && prev_stpoint
->address
== tpoint
->address
)
2327 /* Nothing to do. We already unprobed a tracepoint set at
2328 this marker address (and there can only be one probe
2333 unprobe_marker_at (tpoint
->address
);
2334 prev_stpoint
= tpoint
;
2339 tpoint
->handle
= NULL
;
2345 /* Parse a packet that defines a tracepoint. */
2348 cmd_qtdp (char *own_buf
)
2354 struct tracepoint
*tpoint
;
2356 char *packet
= own_buf
;
2358 packet
+= strlen ("QTDP:");
2360 /* A hyphen at the beginning marks a packet specifying actions for a
2361 tracepoint already supplied. */
2368 packet
= unpack_varlen_hex (packet
, &num
);
2369 ++packet
; /* skip a colon */
2370 packet
= unpack_varlen_hex (packet
, &addr
);
2371 ++packet
; /* skip a colon */
2373 /* See if we already have this tracepoint. */
2374 tpoint
= find_tracepoint (num
, addr
);
2378 /* Duplicate tracepoints are never allowed. */
2381 trace_debug ("Tracepoint error: tracepoint %d"
2382 " at 0x%s already exists",
2383 (int) num
, paddress (addr
));
2384 write_enn (own_buf
);
2388 tpoint
= add_tracepoint (num
, addr
);
2390 tpoint
->enabled
= (*packet
== 'E');
2391 ++packet
; /* skip 'E' */
2392 ++packet
; /* skip a colon */
2393 packet
= unpack_varlen_hex (packet
, &count
);
2394 tpoint
->step_count
= count
;
2395 ++packet
; /* skip a colon */
2396 packet
= unpack_varlen_hex (packet
, &count
);
2397 tpoint
->pass_count
= count
;
2398 /* See if we have any of the additional optional fields. */
2399 while (*packet
== ':')
2404 tpoint
->type
= fast_tracepoint
;
2406 packet
= unpack_varlen_hex (packet
, &count
);
2407 tpoint
->orig_size
= count
;
2409 else if (*packet
== 'S')
2411 tpoint
->type
= static_tracepoint
;
2414 else if (*packet
== 'X')
2416 actparm
= (char *) packet
;
2417 tpoint
->cond
= parse_agent_expr (&actparm
);
2420 else if (*packet
== '-')
2422 else if (*packet
== '\0')
2425 trace_debug ("Unknown optional tracepoint field");
2428 trace_debug ("Also has actions\n");
2430 trace_debug ("Defined %stracepoint %d at 0x%s, "
2431 "enabled %d step %ld pass %ld",
2432 tpoint
->type
== fast_tracepoint
? "fast "
2434 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2435 tpoint
->step_count
, tpoint
->pass_count
);
2438 add_tracepoint_action (tpoint
, packet
);
2441 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2442 (int) num
, paddress (addr
));
2443 write_enn (own_buf
);
2451 cmd_qtdpsrc (char *own_buf
)
2453 ULONGEST num
, addr
, start
, slen
;
2454 struct tracepoint
*tpoint
;
2455 char *packet
= own_buf
;
2456 char *saved
, *srctype
, *src
;
2458 struct source_string
*last
, *newlast
;
2460 packet
+= strlen ("QTDPsrc:");
2462 packet
= unpack_varlen_hex (packet
, &num
);
2463 ++packet
; /* skip a colon */
2464 packet
= unpack_varlen_hex (packet
, &addr
);
2465 ++packet
; /* skip a colon */
2467 /* See if we already have this tracepoint. */
2468 tpoint
= find_tracepoint (num
, addr
);
2472 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2473 (int) num
, paddress (addr
));
2474 write_enn (own_buf
);
2479 packet
= strchr (packet
, ':');
2480 srctype
= xmalloc (packet
- saved
+ 1);
2481 memcpy (srctype
, saved
, packet
- saved
);
2482 srctype
[packet
- saved
] = '\0';
2484 packet
= unpack_varlen_hex (packet
, &start
);
2485 ++packet
; /* skip a colon */
2486 packet
= unpack_varlen_hex (packet
, &slen
);
2487 ++packet
; /* skip a colon */
2488 src
= xmalloc (slen
+ 1);
2489 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2492 newlast
= xmalloc (sizeof (struct source_string
));
2493 newlast
->type
= srctype
;
2495 newlast
->next
= NULL
;
2496 /* Always add a source string to the end of the list;
2497 this keeps sequences of actions/commands in the right
2499 if (tpoint
->source_strings
)
2501 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2503 last
->next
= newlast
;
2506 tpoint
->source_strings
= newlast
;
2512 cmd_qtdv (char *own_buf
)
2514 ULONGEST num
, val
, builtin
;
2517 struct trace_state_variable
*tsv
;
2518 char *packet
= own_buf
;
2520 packet
+= strlen ("QTDV:");
2522 packet
= unpack_varlen_hex (packet
, &num
);
2523 ++packet
; /* skip a colon */
2524 packet
= unpack_varlen_hex (packet
, &val
);
2525 ++packet
; /* skip a colon */
2526 packet
= unpack_varlen_hex (packet
, &builtin
);
2527 ++packet
; /* skip a colon */
2529 nbytes
= strlen (packet
) / 2;
2530 varname
= xmalloc (nbytes
+ 1);
2531 nbytes
= unhexify (varname
, packet
, nbytes
);
2532 varname
[nbytes
] = '\0';
2534 tsv
= create_trace_state_variable (num
, 1);
2535 tsv
->initial_value
= (LONGEST
) val
;
2536 tsv
->name
= varname
;
2538 set_trace_state_variable_value (num
, (LONGEST
) val
);
2544 cmd_qtv (char *own_buf
)
2549 char *packet
= own_buf
;
2551 packet
+= strlen ("qTV:");
2552 packet
= unpack_varlen_hex (packet
, &num
);
2554 if (current_traceframe
>= 0)
2556 err
= traceframe_read_tsv ((int) num
, &val
);
2559 strcpy (own_buf
, "U");
2563 /* Only make tsv's be undefined before the first trace run. After a
2564 trace run is over, the user might want to see the last value of
2565 the tsv, and it might not be available in a traceframe. */
2566 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2568 strcpy (own_buf
, "U");
2572 val
= get_trace_state_variable_value (num
);
2574 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2577 /* Clear out the list of readonly regions. */
2580 clear_readonly_regions (void)
2582 struct readonly_region
*roreg
;
2584 while (readonly_regions
)
2586 roreg
= readonly_regions
;
2587 readonly_regions
= readonly_regions
->next
;
2592 /* Parse the collection of address ranges whose contents GDB believes
2593 to be unchanging and so can be read directly from target memory
2594 even while looking at a traceframe. */
2597 cmd_qtro (char *own_buf
)
2599 ULONGEST start
, end
;
2600 struct readonly_region
*roreg
;
2601 char *packet
= own_buf
;
2603 trace_debug ("Want to mark readonly regions");
2605 clear_readonly_regions ();
2607 packet
+= strlen ("QTro");
2609 while (*packet
== ':')
2611 ++packet
; /* skip a colon */
2612 packet
= unpack_varlen_hex (packet
, &start
);
2613 ++packet
; /* skip a comma */
2614 packet
= unpack_varlen_hex (packet
, &end
);
2615 roreg
= xmalloc (sizeof (struct readonly_region
));
2616 roreg
->start
= start
;
2618 roreg
->next
= readonly_regions
;
2619 readonly_regions
= roreg
;
2620 trace_debug ("Added readonly region from 0x%s to 0x%s",
2621 paddress (roreg
->start
), paddress (roreg
->end
));
2627 /* Test to see if the given range is in our list of readonly ranges.
2628 We only test for being entirely within a range, GDB is not going to
2629 send a single memory packet that spans multiple regions. */
2632 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2634 struct readonly_region
*roreg
;
2636 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2637 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2643 /* The maximum size of a jump pad entry. */
2644 static const int max_jump_pad_size
= 0x100;
2646 static CORE_ADDR gdb_jump_pad_head
;
2648 /* Return the address of the next free jump space. */
2651 get_jump_space_head (void)
2653 if (gdb_jump_pad_head
== 0)
2655 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2656 &gdb_jump_pad_head
))
2657 fatal ("error extracting jump_pad_buffer");
2660 return gdb_jump_pad_head
;
2663 /* Reserve USED bytes from the jump space. */
2666 claim_jump_space (ULONGEST used
)
2668 trace_debug ("claim_jump_space reserves %s bytes at %s",
2669 pulongest (used
), paddress (gdb_jump_pad_head
));
2670 gdb_jump_pad_head
+= used
;
2673 /* Sort tracepoints by PC, using a bubble sort. */
2676 sort_tracepoints (void)
2678 struct tracepoint
*lst
, *tmp
, *prev
= NULL
;
2681 if (tracepoints
== NULL
)
2685 for (tmp
= tracepoints
; tmp
->next
; tmp
= tmp
->next
)
2688 for (i
= 0; i
< n
- 1; i
++)
2689 for (j
= 0, lst
= tracepoints
;
2690 lst
&& lst
->next
&& (j
<= n
- 1 - i
);
2693 /* If we're at beginning, the start node is the prev
2698 /* Compare neighbors. */
2699 if (lst
->next
->address
< lst
->address
)
2701 struct tracepoint
*p
;
2704 tmp
= (lst
->next
? lst
->next
->next
: NULL
);
2706 if (j
== 0 && prev
== tracepoints
)
2707 tracepoints
= lst
->next
;
2710 prev
->next
= lst
->next
;
2711 lst
->next
->next
= lst
;
2718 /* Keep track of the previous node. We need it if we need
2726 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2727 the command fails, or 0 otherwise. If the command ran
2728 successfully, but probing the marker failed, ERROUT will be filled
2729 with the error to reply to GDB, and -1 is also returned. This
2730 allows directly passing IPA errors to GDB. */
2733 probe_marker_at (CORE_ADDR address
, char *errout
)
2735 char cmd
[CMD_BUF_SIZE
];
2738 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
2739 err
= run_inferior_command (cmd
);
2745 strcpy (errout
, cmd
);
2753 #define MAX_JUMP_SIZE 20
2756 cmd_qtstart (char *packet
)
2758 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
2759 int slow_tracepoint_count
, fast_count
;
2760 CORE_ADDR jump_entry
;
2762 /* The jump to the jump pad of the last fast tracepoint
2764 unsigned char fjump
[MAX_JUMP_SIZE
];
2765 ULONGEST fjump_size
;
2767 trace_debug ("Starting the trace");
2769 slow_tracepoint_count
= fast_count
= 0;
2771 /* Sort tracepoints by ascending address. This makes installing
2772 fast tracepoints at the same address easier to handle. */
2773 sort_tracepoints ();
2775 /* Pause all threads temporarily while we patch tracepoints. */
2778 /* Get threads out of jump pads. Safe to do here, since this is a
2779 top level command. And, required to do here, since we're
2780 deleting/rewriting jump pads. */
2782 stabilize_threads ();
2784 /* Freeze threads. */
2787 /* Sync the fast tracepoints list in the inferior ftlib. */
2788 if (in_process_agent_loaded ())
2790 download_tracepoints ();
2791 download_trace_state_variables ();
2794 /* No previous fast tpoint yet. */
2795 prev_ftpoint
= NULL
;
2797 /* No previous static tpoint yet. */
2798 prev_stpoint
= NULL
;
2802 /* Install tracepoints. */
2803 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2805 /* Ensure all the hit counts start at zero. */
2806 tpoint
->hit_count
= 0;
2808 if (!tpoint
->enabled
)
2811 if (tpoint
->type
== trap_tracepoint
)
2813 ++slow_tracepoint_count
;
2815 /* Tracepoints are installed as memory breakpoints. Just go
2816 ahead and install the trap. The breakpoints module
2817 handles duplicated breakpoints, and the memory read
2818 routine handles un-patching traps from memory reads. */
2819 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
2820 tracepoint_handler
);
2822 else if (tpoint
->type
== fast_tracepoint
)
2826 if (maybe_write_ipa_not_loaded (packet
))
2828 trace_debug ("Requested a fast tracepoint, but fast "
2829 "tracepoints aren't supported.");
2833 if (prev_ftpoint
!= NULL
&& prev_ftpoint
->address
== tpoint
->address
)
2835 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
,
2838 tpoint
->jump_pad
= prev_ftpoint
->jump_pad
;
2839 tpoint
->jump_pad_end
= prev_ftpoint
->jump_pad_end
;
2840 tpoint
->adjusted_insn_addr
= prev_ftpoint
->adjusted_insn_addr
;
2841 tpoint
->adjusted_insn_addr_end
2842 = prev_ftpoint
->adjusted_insn_addr_end
;
2849 prev_ftpoint
= NULL
;
2851 jentry
= jump_entry
= get_jump_space_head ();
2853 /* Install the jump pad. */
2854 err
= install_fast_tracepoint_jump_pad
2855 (tpoint
->obj_addr_on_target
,
2857 ipa_sym_addrs
.addr_gdb_collect
,
2858 ipa_sym_addrs
.addr_collecting
,
2862 &tpoint
->adjusted_insn_addr
,
2863 &tpoint
->adjusted_insn_addr_end
);
2867 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
,
2870 if (tpoint
->handle
!= NULL
)
2872 tpoint
->jump_pad
= jump_entry
;
2873 tpoint
->jump_pad_end
= jentry
;
2875 /* Pad to 8-byte alignment. */
2876 jentry
= ((jentry
+ 7) & ~0x7);
2877 claim_jump_space (jentry
- jump_entry
);
2879 /* So that we can handle multiple fast tracepoints
2880 at the same address easily. */
2881 prev_ftpoint
= tpoint
;
2885 else if (tpoint
->type
== static_tracepoint
)
2887 if (maybe_write_ipa_ust_not_loaded (packet
))
2889 trace_debug ("Requested a static tracepoint, but static "
2890 "tracepoints are not supported.");
2894 /* Can only probe a given marker once. */
2895 if (prev_stpoint
!= NULL
&& prev_stpoint
->address
== tpoint
->address
)
2897 tpoint
->handle
= (void *) -1;
2901 if (probe_marker_at (tpoint
->address
, packet
) == 0)
2903 tpoint
->handle
= (void *) -1;
2905 /* So that we can handle multiple static tracepoints
2906 at the same address easily. */
2907 prev_stpoint
= tpoint
;
2912 /* Any failure in the inner loop is sufficient cause to give
2914 if (tpoint
->handle
== NULL
)
2918 /* Any error in tracepoint insertion is unacceptable; better to
2919 address the problem now, than end up with a useless or misleading
2923 clear_installed_tracepoints ();
2924 if (*packet
== '\0')
2930 stopping_tracepoint
= NULL
;
2931 trace_buffer_is_full
= 0;
2932 expr_eval_result
= expr_eval_no_error
;
2933 error_tracepoint
= NULL
;
2935 /* Tracing is now active, hits will now start being logged. */
2938 if (in_process_agent_loaded ())
2940 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
2941 fatal ("Error setting tracing variable in lib");
2943 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
2945 fatal ("Error clearing stopping_tracepoint variable in lib");
2947 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
2948 fatal ("Error clearing trace_buffer_is_full variable in lib");
2950 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
2951 stop_tracing_handler
);
2952 if (stop_tracing_bkpt
== NULL
)
2953 error ("Error setting stop_tracing breakpoint");
2955 flush_trace_buffer_bkpt
2956 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
2957 flush_trace_buffer_handler
);
2958 if (flush_trace_buffer_bkpt
== NULL
)
2959 error ("Error setting flush_trace_buffer breakpoint");
2967 /* End a tracing run, filling in a stop reason to report back to GDB,
2968 and removing the tracepoints from the code. */
2975 trace_debug ("Tracing is already off, ignoring");
2979 trace_debug ("Stopping the trace");
2981 /* Pause all threads before removing fast jumps from memory,
2982 breakpoints, and touching IPA state variables (inferior memory).
2983 Some thread may hit the internal tracing breakpoints, or be
2984 collecting this moment, but that's ok, we don't release the
2985 tpoint object's memory or the jump pads here (we only do that
2986 when we're sure we can move all threads out of the jump pads).
2987 We can't now, since we may be getting here due to the inferior
2988 agent calling us. */
2990 /* Since we're removing breakpoints, cancel breakpoint hits,
2991 possibly related to the breakpoints we're about to delete. */
2992 cancel_breakpoints ();
2994 /* Stop logging. Tracepoints can still be hit, but they will not be
2997 if (in_process_agent_loaded ())
2999 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3000 fatal ("Error clearing tracing variable in lib");
3003 tracing_stop_reason
= "t???";
3004 tracing_stop_tpnum
= 0;
3005 if (stopping_tracepoint
)
3007 trace_debug ("Stopping the trace because "
3008 "tracepoint %d was hit %ld times",
3009 stopping_tracepoint
->number
,
3010 stopping_tracepoint
->pass_count
);
3011 tracing_stop_reason
= "tpasscount";
3012 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3014 else if (trace_buffer_is_full
)
3016 trace_debug ("Stopping the trace because the trace buffer is full");
3017 tracing_stop_reason
= "tfull";
3019 else if (expr_eval_result
!= expr_eval_no_error
)
3021 trace_debug ("Stopping the trace because of an expression eval error");
3022 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3023 tracing_stop_tpnum
= error_tracepoint
->number
;
3025 #ifndef IN_PROCESS_AGENT
3026 else if (!gdb_connected ())
3028 trace_debug ("Stopping the trace because GDB disconnected");
3029 tracing_stop_reason
= "tdisconnected";
3034 trace_debug ("Stopping the trace because of a tstop command");
3035 tracing_stop_reason
= "tstop";
3038 stopping_tracepoint
= NULL
;
3039 error_tracepoint
= NULL
;
3041 /* Clear out the tracepoints. */
3042 clear_installed_tracepoints ();
3044 if (in_process_agent_loaded ())
3046 /* Pull in fast tracepoint trace frames from the inferior lib
3047 buffer into our buffer, even if our buffer is already full,
3048 because we want to present the full number of created frames
3049 in addition to what fit in the trace buffer. */
3050 upload_fast_traceframes ();
3053 if (stop_tracing_bkpt
!= NULL
)
3055 delete_breakpoint (stop_tracing_bkpt
);
3056 stop_tracing_bkpt
= NULL
;
3059 if (flush_trace_buffer_bkpt
!= NULL
)
3061 delete_breakpoint (flush_trace_buffer_bkpt
);
3062 flush_trace_buffer_bkpt
= NULL
;
3069 stop_tracing_handler (CORE_ADDR addr
)
3071 trace_debug ("lib hit stop_tracing");
3073 /* Don't actually handle it here. When we stop tracing we remove
3074 breakpoints from the inferior, and that is not allowed in a
3075 breakpoint handler (as the caller is walking the breakpoint
3081 flush_trace_buffer_handler (CORE_ADDR addr
)
3083 trace_debug ("lib hit flush_trace_buffer");
3088 cmd_qtstop (char *packet
)
3095 cmd_qtdisconnected (char *own_buf
)
3098 char *packet
= own_buf
;
3100 packet
+= strlen ("QTDisconnected:");
3102 unpack_varlen_hex (packet
, &setting
);
3106 disconnected_tracing
= setting
;
3110 cmd_qtframe (char *own_buf
)
3112 ULONGEST frame
, pc
, lo
, hi
, num
;
3114 struct traceframe
*tframe
;
3115 char *packet
= own_buf
;
3117 packet
+= strlen ("QTFrame:");
3119 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3121 packet
+= strlen ("pc:");
3122 packet
= unpack_varlen_hex (packet
, &pc
);
3123 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3124 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3126 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3128 packet
+= strlen ("range:");
3129 packet
= unpack_varlen_hex (packet
, &lo
);
3131 packet
= unpack_varlen_hex (packet
, &hi
);
3132 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3133 paddress (lo
), paddress (hi
));
3134 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3136 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3138 packet
+= strlen ("outside:");
3139 packet
= unpack_varlen_hex (packet
, &lo
);
3141 packet
= unpack_varlen_hex (packet
, &hi
);
3142 trace_debug ("Want to find next traceframe "
3143 "outside the range 0x%s to 0x%s",
3144 paddress (lo
), paddress (hi
));
3145 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3147 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3149 packet
+= strlen ("tdp:");
3150 packet
= unpack_varlen_hex (packet
, &num
);
3152 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3153 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3157 unpack_varlen_hex (packet
, &frame
);
3158 tfnum
= (int) frame
;
3161 trace_debug ("Want to stop looking at traceframes");
3162 current_traceframe
= -1;
3166 trace_debug ("Want to look at traceframe %d", tfnum
);
3167 tframe
= find_traceframe (tfnum
);
3172 current_traceframe
= tfnum
;
3173 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3176 sprintf (own_buf
, "F-1");
3180 cmd_qtstatus (char *packet
)
3182 char *stop_reason_rsp
= NULL
;
3184 trace_debug ("Returning trace status as %d, stop reason %s",
3185 tracing
, tracing_stop_reason
);
3187 if (in_process_agent_loaded ())
3191 upload_fast_traceframes ();
3196 stop_reason_rsp
= (char *) tracing_stop_reason
;
3198 /* The user visible error string in terror needs to be hex encoded.
3199 We leave it as plain string in `tracepoint_stop_reason' to ease
3201 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3203 const char *result_name
;
3207 result_name
= stop_reason_rsp
+ strlen ("terror:");
3208 hexstr_len
= strlen (result_name
) * 2;
3209 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3210 strcpy (p
, "terror:");
3212 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3218 "tframes:%x;tcreated:%x;"
3219 "tfree:%x;tsize:%s;"
3223 stop_reason_rsp
, tracing_stop_tpnum
,
3224 traceframe_count
, traceframes_created
,
3225 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3226 circular_trace_buffer
,
3227 disconnected_tracing
);
3230 /* State variables to help return all the tracepoint bits. */
3231 static struct tracepoint
*cur_tpoint
;
3232 static int cur_action
;
3233 static int cur_step_action
;
3234 static struct source_string
*cur_source_string
;
3235 static struct trace_state_variable
*cur_tsv
;
3237 /* Compose a response that is an imitation of the syntax by which the
3238 tracepoint was originally downloaded. */
3241 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3245 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
3246 paddress (tpoint
->address
),
3247 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3248 tpoint
->pass_count
);
3249 if (tpoint
->type
== fast_tracepoint
)
3250 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3251 else if (tpoint
->type
== static_tracepoint
)
3252 sprintf (packet
+ strlen (packet
), ":S");
3256 buf
= unparse_agent_expr (tpoint
->cond
);
3257 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3258 tpoint
->cond
->length
, buf
);
3263 /* Compose a response that is an imitation of the syntax by which the
3264 tracepoint action was originally downloaded (with the difference
3265 that due to the way we store the actions, this will output a packet
3266 per action, while GDB could have combined more than one action
3270 response_action (char *packet
, struct tracepoint
*tpoint
,
3271 char *taction
, int step
)
3273 sprintf (packet
, "%c%x:%s:%s",
3274 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3278 /* Compose a response that is an imitation of the syntax by which the
3279 tracepoint source piece was originally downloaded. */
3282 response_source (char *packet
,
3283 struct tracepoint
*tpoint
, struct source_string
*src
)
3288 len
= strlen (src
->str
);
3289 buf
= alloca (len
* 2 + 1);
3290 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3292 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3293 tpoint
->number
, paddress (tpoint
->address
),
3294 src
->type
, 0, len
, buf
);
3297 /* Return the first piece of tracepoint definition, and initialize the
3298 state machine that will iterate through all the tracepoint
3302 cmd_qtfp (char *packet
)
3304 trace_debug ("Returning first tracepoint definition piece");
3306 cur_tpoint
= tracepoints
;
3307 cur_action
= cur_step_action
= -1;
3308 cur_source_string
= NULL
;
3311 response_tracepoint (packet
, cur_tpoint
);
3313 strcpy (packet
, "l");
3316 /* Return additional pieces of tracepoint definition. Each action and
3317 stepping action must go into its own packet, because of packet size
3318 limits, and so we use state variables to deliver one piece at a
3322 cmd_qtsp (char *packet
)
3324 trace_debug ("Returning subsequent tracepoint definition piece");
3328 /* This case would normally never occur, but be prepared for
3330 strcpy (packet
, "l");
3332 else if (cur_action
< cur_tpoint
->numactions
- 1)
3335 response_action (packet
, cur_tpoint
,
3336 cur_tpoint
->actions_str
[cur_action
], 0);
3338 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3341 response_action (packet
, cur_tpoint
,
3342 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3344 else if ((cur_source_string
3345 ? cur_source_string
->next
3346 : cur_tpoint
->source_strings
))
3348 if (cur_source_string
)
3349 cur_source_string
= cur_source_string
->next
;
3351 cur_source_string
= cur_tpoint
->source_strings
;
3352 response_source (packet
, cur_tpoint
, cur_source_string
);
3356 cur_tpoint
= cur_tpoint
->next
;
3357 cur_action
= cur_step_action
= -1;
3358 cur_source_string
= NULL
;
3360 response_tracepoint (packet
, cur_tpoint
);
3362 strcpy (packet
, "l");
3366 /* Compose a response that is an imitation of the syntax by which the
3367 trace state variable was originally downloaded. */
3370 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3372 char *buf
= (char *) "";
3377 namelen
= strlen (tsv
->name
);
3378 buf
= alloca (namelen
* 2 + 1);
3379 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3382 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3383 tsv
->getter
? 1 : 0, buf
);
3386 /* Return the first trace state variable definition, and initialize
3387 the state machine that will iterate through all the tsv bits. */
3390 cmd_qtfv (char *packet
)
3392 trace_debug ("Returning first trace state variable definition");
3394 cur_tsv
= trace_state_variables
;
3397 response_tsv (packet
, cur_tsv
);
3399 strcpy (packet
, "l");
3402 /* Return additional trace state variable definitions. */
3405 cmd_qtsv (char *packet
)
3407 trace_debug ("Returning first trace state variable definition");
3411 /* This case would normally never occur, but be prepared for
3413 strcpy (packet
, "l");
3417 cur_tsv
= cur_tsv
->next
;
3419 response_tsv (packet
, cur_tsv
);
3421 strcpy (packet
, "l");
3424 strcpy (packet
, "l");
3427 /* Return the first static tracepoint marker, and initialize the state
3428 machine that will iterate through all the static tracepoints
3432 cmd_qtfstm (char *packet
)
3434 if (!maybe_write_ipa_ust_not_loaded (packet
))
3435 run_inferior_command (packet
);
3438 /* Return additional static tracepoints markers. */
3441 cmd_qtsstm (char *packet
)
3443 if (!maybe_write_ipa_ust_not_loaded (packet
))
3444 run_inferior_command (packet
);
3447 /* Return the definition of the static tracepoint at a given address.
3448 Result packet is the same as qTsST's. */
3451 cmd_qtstmat (char *packet
)
3453 if (!maybe_write_ipa_ust_not_loaded (packet
))
3454 run_inferior_command (packet
);
3457 /* Respond to qTBuffer packet with a block of raw data from the trace
3458 buffer. GDB may ask for a lot, but we are allowed to reply with
3459 only as much as will fit within packet limits or whatever. */
3462 cmd_qtbuffer (char *own_buf
)
3464 ULONGEST offset
, num
, tot
;
3466 char *packet
= own_buf
;
3468 packet
+= strlen ("qTBuffer:");
3470 packet
= unpack_varlen_hex (packet
, &offset
);
3471 ++packet
; /* skip a comma */
3472 packet
= unpack_varlen_hex (packet
, &num
);
3474 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3475 (int) num
, pulongest (offset
));
3477 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3479 /* If we're right at the end, reply specially that we're done. */
3482 strcpy (own_buf
, "l");
3486 /* Object to any other out-of-bounds request. */
3489 write_enn (own_buf
);
3493 /* Compute the pointer corresponding to the given offset, accounting
3495 tbp
= trace_buffer_start
+ offset
;
3496 if (tbp
>= trace_buffer_wrap
)
3497 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3499 /* Trim to the remaining bytes if we're close to the end. */
3500 if (num
> tot
- offset
)
3503 /* Trim to available packet size. */
3504 if (num
>= (PBUFSIZ
- 16) / 2 )
3505 num
= (PBUFSIZ
- 16) / 2;
3507 convert_int_to_ascii (tbp
, own_buf
, num
);
3508 own_buf
[num
] = '\0';
3512 cmd_bigqtbuffer (char *own_buf
)
3515 char *packet
= own_buf
;
3517 packet
+= strlen ("QTBuffer:");
3519 if (strncmp ("circular:", packet
, strlen ("circular:")) == 0)
3521 packet
+= strlen ("circular:");
3522 packet
= unpack_varlen_hex (packet
, &val
);
3523 circular_trace_buffer
= val
;
3524 trace_debug ("Trace buffer is now %s",
3525 circular_trace_buffer
? "circular" : "linear");
3529 write_enn (own_buf
);
3533 handle_tracepoint_general_set (char *packet
)
3535 if (strcmp ("QTinit", packet
) == 0)
3537 cmd_qtinit (packet
);
3540 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3545 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3547 cmd_qtdpsrc (packet
);
3550 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3555 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3560 else if (strcmp ("QTStart", packet
) == 0)
3562 cmd_qtstart (packet
);
3565 else if (strcmp ("QTStop", packet
) == 0)
3567 cmd_qtstop (packet
);
3570 else if (strncmp ("QTDisconnected:", packet
,
3571 strlen ("QTDisconnected:")) == 0)
3573 cmd_qtdisconnected (packet
);
3576 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3578 cmd_qtframe (packet
);
3581 else if (strncmp ("QTBuffer:", packet
, strlen ("QTBuffer:")) == 0)
3583 cmd_bigqtbuffer (packet
);
3591 handle_tracepoint_query (char *packet
)
3593 if (strcmp ("qTStatus", packet
) == 0)
3595 cmd_qtstatus (packet
);
3598 else if (strcmp ("qTfP", packet
) == 0)
3603 else if (strcmp ("qTsP", packet
) == 0)
3608 else if (strcmp ("qTfV", packet
) == 0)
3613 else if (strcmp ("qTsV", packet
) == 0)
3618 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
3623 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
3625 cmd_qtbuffer (packet
);
3628 else if (strcmp ("qTfSTM", packet
) == 0)
3630 cmd_qtfstm (packet
);
3633 else if (strcmp ("qTsSTM", packet
) == 0)
3635 cmd_qtsstm (packet
);
3638 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
3640 cmd_qtstmat (packet
);
3648 #ifndef IN_PROCESS_AGENT
3650 /* Call this when thread TINFO has hit the tracepoint defined by
3651 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3652 action. This adds a while-stepping collecting state item to the
3653 threads' collecting state list, so that we can keep track of
3654 multiple simultaneous while-stepping actions being collected by the
3655 same thread. This can happen in cases like:
3657 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3659 ff0003 INSN3 <-- TP2, collect $regs
3660 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3663 Notice that when instruction INSN5 is reached, the while-stepping
3664 actions of both TP1 and TP3 are still being collected, and that TP2
3665 had been collected meanwhile. The whole range of ff0001-ff0005
3666 should be single-stepped, due to at least TP1's while-stepping
3667 action covering the whole range. */
3670 add_while_stepping_state (struct thread_info
*tinfo
,
3671 int tp_number
, CORE_ADDR tp_address
)
3673 struct wstep_state
*wstep
;
3675 wstep
= xmalloc (sizeof (*wstep
));
3676 wstep
->next
= tinfo
->while_stepping
;
3678 wstep
->tp_number
= tp_number
;
3679 wstep
->tp_address
= tp_address
;
3680 wstep
->current_step
= 0;
3682 tinfo
->while_stepping
= wstep
;
3685 /* Release the while-stepping collecting state WSTEP. */
3688 release_while_stepping_state (struct wstep_state
*wstep
)
3693 /* Release all while-stepping collecting states currently associated
3694 with thread TINFO. */
3697 release_while_stepping_state_list (struct thread_info
*tinfo
)
3699 struct wstep_state
*head
;
3701 while (tinfo
->while_stepping
)
3703 head
= tinfo
->while_stepping
;
3704 tinfo
->while_stepping
= head
->next
;
3705 release_while_stepping_state (head
);
3709 /* If TINFO was handling a 'while-stepping' action, the step has
3710 finished, so collect any step data needed, and check if any more
3711 steps are required. Return true if the thread was indeed
3712 collecting tracepoint data, false otherwise. */
3715 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3717 struct tracepoint
*tpoint
;
3718 struct wstep_state
*wstep
;
3719 struct wstep_state
**wstep_link
;
3720 struct trap_tracepoint_ctx ctx
;
3722 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
3724 if (in_process_agent_loaded ())
3725 upload_fast_traceframes ();
3727 /* Check if we were indeed collecting data for one of more
3728 tracepoints with a 'while-stepping' count. */
3729 if (tinfo
->while_stepping
== NULL
)
3734 /* We're not even tracing anymore. Stop this thread from
3736 release_while_stepping_state_list (tinfo
);
3738 /* The thread had stopped due to a single-step request indeed
3739 explained by a tracepoint. */
3743 wstep
= tinfo
->while_stepping
;
3744 wstep_link
= &tinfo
->while_stepping
;
3746 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
3747 target_pid_to_str (tinfo
->entry
.id
),
3748 wstep
->tp_number
, paddress (wstep
->tp_address
));
3750 ctx
.base
.type
= trap_tracepoint
;
3751 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3753 while (wstep
!= NULL
)
3755 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
3758 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
3759 wstep
->tp_number
, paddress (wstep
->tp_address
),
3760 target_pid_to_str (tinfo
->entry
.id
));
3763 *wstep_link
= wstep
->next
;
3764 release_while_stepping_state (wstep
);
3768 /* We've just finished one step. */
3769 ++wstep
->current_step
;
3772 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
3773 stop_pc
, tpoint
, wstep
->current_step
);
3775 if (wstep
->current_step
>= tpoint
->step_count
)
3777 /* The requested numbers of steps have occurred. */
3778 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
3779 target_pid_to_str (tinfo
->entry
.id
),
3780 wstep
->tp_number
, paddress (wstep
->tp_address
));
3782 /* Unlink the wstep. */
3783 *wstep_link
= wstep
->next
;
3784 release_while_stepping_state (wstep
);
3785 wstep
= *wstep_link
;
3787 /* Only check the hit count now, which ensure that we do all
3788 our stepping before stopping the run. */
3789 if (tpoint
->pass_count
> 0
3790 && tpoint
->hit_count
>= tpoint
->pass_count
3791 && stopping_tracepoint
== NULL
)
3792 stopping_tracepoint
= tpoint
;
3796 /* Keep single-stepping until the requested numbers of steps
3798 wstep_link
= &wstep
->next
;
3799 wstep
= *wstep_link
;
3802 if (stopping_tracepoint
3803 || trace_buffer_is_full
3804 || expr_eval_result
!= expr_eval_no_error
)
3814 /* Handle any internal tracing control breakpoint hits. That means,
3815 pull traceframes from the IPA to our buffer, and syncing both
3816 tracing agents when the IPA's tracing stops for some reason. */
3819 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3821 /* Pull in fast tracepoint trace frames from the inferior in-process
3822 agent's buffer into our buffer. */
3824 if (!in_process_agent_loaded ())
3827 upload_fast_traceframes ();
3829 /* Check if the in-process agent had decided we should stop
3831 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
3833 int ipa_trace_buffer_is_full
;
3834 CORE_ADDR ipa_stopping_tracepoint
;
3835 int ipa_expr_eval_result
;
3836 CORE_ADDR ipa_error_tracepoint
;
3838 trace_debug ("lib stopped at stop_tracing");
3840 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
3841 &ipa_trace_buffer_is_full
);
3843 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3844 &ipa_stopping_tracepoint
);
3845 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
3847 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
3848 &ipa_error_tracepoint
);
3849 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
3851 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
3852 &ipa_expr_eval_result
);
3853 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
3855 trace_debug ("lib: trace_buffer_is_full: %d, "
3856 "stopping_tracepoint: %s, "
3857 "ipa_expr_eval_result: %d, "
3858 "error_tracepoint: %s, ",
3859 ipa_trace_buffer_is_full
,
3860 paddress (ipa_stopping_tracepoint
),
3861 ipa_expr_eval_result
,
3862 paddress (ipa_error_tracepoint
));
3866 if (ipa_trace_buffer_is_full
)
3867 trace_debug ("lib stopped due to full buffer.");
3868 if (ipa_stopping_tracepoint
)
3869 trace_debug ("lib stopped due to tpoint");
3870 if (ipa_stopping_tracepoint
)
3871 trace_debug ("lib stopped due to error");
3874 if (ipa_stopping_tracepoint
!= 0)
3877 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
3879 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
3881 expr_eval_result
= ipa_expr_eval_result
;
3883 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
3888 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
3890 trace_debug ("lib stopped at flush_trace_buffer");
3897 /* Return true if TINFO just hit a tracepoint. Collect data if
3901 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3903 struct tracepoint
*tpoint
;
3905 struct trap_tracepoint_ctx ctx
;
3907 /* Not tracing, don't handle. */
3911 ctx
.base
.type
= trap_tracepoint
;
3912 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3914 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3916 /* Note that we collect fast tracepoints here as well. We'll
3917 step over the fast tracepoint jump later, which avoids the
3919 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
)
3921 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
3922 target_pid_to_str (tinfo
->entry
.id
),
3923 tpoint
->number
, paddress (tpoint
->address
));
3925 /* Test the condition if present, and collect if true. */
3927 || (condition_true_at_tracepoint
3928 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
3929 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
3932 if (stopping_tracepoint
3933 || trace_buffer_is_full
3934 || expr_eval_result
!= expr_eval_no_error
)
3938 /* If the tracepoint had a 'while-stepping' action, then set
3939 the thread to collect this tracepoint on the following
3941 else if (tpoint
->step_count
> 0)
3943 add_while_stepping_state (tinfo
,
3944 tpoint
->number
, tpoint
->address
);
3956 #if defined IN_PROCESS_AGENT && defined HAVE_UST
3957 struct ust_marker_data
;
3958 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
3960 struct tracepoint
*tpoint
,
3961 struct traceframe
*tframe
);
3964 /* Create a trace frame for the hit of the given tracepoint in the
3968 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
3969 struct tracepoint
*tpoint
)
3971 struct traceframe
*tframe
;
3974 /* Only count it as a hit when we actually collect data. */
3975 tpoint
->hit_count
++;
3977 /* If we've exceeded a defined pass count, record the event for
3978 later, and finish the collection for this hit. This test is only
3979 for nonstepping tracepoints, stepping tracepoints test at the end
3980 of their while-stepping loop. */
3981 if (tpoint
->pass_count
> 0
3982 && tpoint
->hit_count
>= tpoint
->pass_count
3983 && tpoint
->step_count
== 0
3984 && stopping_tracepoint
== NULL
)
3985 stopping_tracepoint
= tpoint
;
3987 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
3988 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
3990 tframe
= add_traceframe (tpoint
);
3994 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
3996 #ifndef IN_PROCESS_AGENT
3997 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
3998 tpoint
->number
, paddress (tpoint
->address
),
3999 tpoint
->actions_str
[acti
]);
4002 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4003 tpoint
->actions
[acti
]);
4006 finish_traceframe (tframe
);
4009 if (tframe
== NULL
&& tracing
)
4010 trace_buffer_is_full
= 1;
4013 #ifndef IN_PROCESS_AGENT
4016 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4018 struct tracepoint
*tpoint
, int current_step
)
4020 struct traceframe
*tframe
;
4023 trace_debug ("Making new step traceframe for "
4024 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4025 tpoint
->number
, paddress (tpoint
->address
),
4026 current_step
, tpoint
->step_count
,
4029 tframe
= add_traceframe (tpoint
);
4033 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4035 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4036 tpoint
->number
, paddress (tpoint
->address
),
4037 tpoint
->step_actions_str
[acti
]);
4039 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4040 tpoint
->step_actions
[acti
]);
4043 finish_traceframe (tframe
);
4046 if (tframe
== NULL
&& tracing
)
4047 trace_buffer_is_full
= 1;
4052 static struct regcache
*
4053 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4055 struct regcache
*regcache
= NULL
;
4057 #ifdef IN_PROCESS_AGENT
4058 if (ctx
->type
== fast_tracepoint
)
4060 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4061 if (!fctx
->regcache_initted
)
4063 fctx
->regcache_initted
= 1;
4064 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4065 supply_regblock (&fctx
->regcache
, NULL
);
4066 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4068 regcache
= &fctx
->regcache
;
4071 if (ctx
->type
== static_tracepoint
)
4073 struct static_tracepoint_ctx
*sctx
= (struct static_tracepoint_ctx
*) ctx
;
4074 if (!sctx
->regcache_initted
)
4076 sctx
->regcache_initted
= 1;
4077 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4078 supply_regblock (&sctx
->regcache
, NULL
);
4079 /* Pass down the tracepoint address, because REGS doesn't
4080 include the PC, but we know what it must have been. */
4081 supply_static_tracepoint_registers (&sctx
->regcache
,
4082 (const unsigned char *)
4084 sctx
->tpoint
->address
);
4086 regcache
= &sctx
->regcache
;
4090 if (ctx
->type
== trap_tracepoint
)
4092 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4093 regcache
= tctx
->regcache
;
4097 gdb_assert (regcache
!= NULL
);
4103 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4105 struct tracepoint
*tpoint
,
4106 struct traceframe
*tframe
,
4107 struct tracepoint_action
*taction
)
4109 enum eval_result_type err
;
4111 switch (taction
->type
)
4115 struct collect_memory_action
*maction
;
4117 maction
= (struct collect_memory_action
*) taction
;
4119 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4120 pulongest (maction
->len
),
4121 paddress (maction
->addr
), maction
->basereg
);
4122 /* (should use basereg) */
4123 agent_mem_read (tframe
, NULL
,
4124 (CORE_ADDR
) maction
->addr
, maction
->len
);
4129 struct collect_registers_action
*raction
;
4131 unsigned char *regspace
;
4132 struct regcache tregcache
;
4133 struct regcache
*context_regcache
;
4135 raction
= (struct collect_registers_action
*) taction
;
4137 trace_debug ("Want to collect registers");
4139 /* Collect all registers for now. */
4140 regspace
= add_traceframe_block (tframe
,
4141 1 + register_cache_size ());
4142 if (regspace
== NULL
)
4144 trace_debug ("Trace buffer block allocation failed, skipping");
4147 /* Identify a register block. */
4150 context_regcache
= get_context_regcache (ctx
);
4152 /* Wrap the regblock in a register cache (in the stack, we
4153 don't want to malloc here). */
4154 init_register_cache (&tregcache
, regspace
+ 1);
4156 /* Copy the register data to the regblock. */
4157 regcache_cpy (&tregcache
, context_regcache
);
4159 #ifndef IN_PROCESS_AGENT
4160 /* On some platforms, trap-based tracepoints will have the PC
4161 pointing to the next instruction after the trap, but we
4162 don't want the user or GDB trying to guess whether the
4163 saved PC needs adjusting; so always record the adjusted
4164 stop_pc. Note that we can't use tpoint->address instead,
4165 since it will be wrong for while-stepping actions. This
4166 adjustment is a nop for fast tracepoints collected from the
4167 in-process lib (but not if GDBserver is collecting one
4168 preemptively), since the PC had already been adjusted to
4169 contain the tracepoint's address by the jump pad. */
4170 trace_debug ("Storing stop pc (0x%s) in regblock",
4171 paddress (tpoint
->address
));
4173 /* This changes the regblock, not the thread's
4175 regcache_write_pc (&tregcache
, stop_pc
);
4181 struct eval_expr_action
*eaction
;
4183 eaction
= (struct eval_expr_action
*) taction
;
4185 trace_debug ("Want to evaluate expression");
4187 err
= eval_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4189 if (err
!= expr_eval_no_error
)
4191 record_tracepoint_error (tpoint
, "action expression", err
);
4198 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4199 trace_debug ("Want to collect static trace data");
4200 collect_ust_data_at_tracepoint (ctx
, stop_pc
,
4203 trace_debug ("warning: collecting static trace data, "
4204 "but static tracepoints are not supported");
4209 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4215 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4216 struct tracepoint
*tpoint
)
4219 enum eval_result_type err
;
4221 /* Presently, gdbserver doesn't run compiled conditions, only the
4222 IPA does. If the program stops at a fast tracepoint's address
4223 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4224 gdbserver preemptively collect the fast tracepoint. Later, on
4225 resume, gdbserver steps over the fast tracepoint like it steps
4226 over breakpoints, so that the IPA doesn't see that fast
4227 tracepoint. This avoids double collects of fast tracepoints in
4228 that stopping scenario. Having gdbserver itself handle the fast
4229 tracepoint gives the user a consistent view of when fast or trap
4230 tracepoints are collected, compared to an alternative where only
4231 trap tracepoints are collected on stop, and fast tracepoints on
4232 resume. When a fast tracepoint is being processed by gdbserver,
4233 it is always the non-compiled condition expression that is
4235 #ifdef IN_PROCESS_AGENT
4236 if (tpoint
->compiled_cond
)
4237 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4240 err
= eval_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4242 if (err
!= expr_eval_no_error
)
4244 record_tracepoint_error (tpoint
, "condition", err
);
4245 /* The error case must return false. */
4249 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4250 tpoint
->number
, paddress (tpoint
->address
),
4252 return (value
? 1 : 0);
4255 #ifndef IN_PROCESS_AGENT
4257 /* The packet form of an agent expression consists of an 'X', number
4258 of bytes in expression, a comma, and then the bytes. */
4260 static struct agent_expr
*
4261 parse_agent_expr (char **actparm
)
4263 char *act
= *actparm
;
4265 struct agent_expr
*aexpr
;
4267 ++act
; /* skip the X */
4268 act
= unpack_varlen_hex (act
, &xlen
);
4269 ++act
; /* skip a comma */
4270 aexpr
= xmalloc (sizeof (struct agent_expr
));
4271 aexpr
->length
= xlen
;
4272 aexpr
->bytes
= xmalloc (xlen
);
4273 convert_ascii_to_int (act
, aexpr
->bytes
, xlen
);
4274 *actparm
= act
+ (xlen
* 2);
4278 /* Convert the bytes of an agent expression back into hex digits, so
4279 they can be printed or uploaded. This allocates the buffer,
4280 callers should free when they are done with it. */
4283 unparse_agent_expr (struct agent_expr
*aexpr
)
4287 rslt
= xmalloc (2 * aexpr
->length
+ 1);
4288 convert_int_to_ascii (aexpr
->bytes
, rslt
, aexpr
->length
);
4294 /* The agent expression evaluator, as specified by the GDB docs. It
4295 returns 0 if everything went OK, and a nonzero error code
4298 static enum eval_result_type
4299 eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4300 struct traceframe
*tframe
,
4301 struct agent_expr
*aexpr
,
4305 #define STACK_MAX 100
4306 ULONGEST stack
[STACK_MAX
], top
;
4311 /* This union is a convenient way to convert representations. For
4312 now, assume a standard architecture where the hardware integer
4313 types have 8, 16, 32, 64 bit types. A more robust solution would
4314 be to import stdint.h from gnulib. */
4319 unsigned char bytes
[1];
4324 unsigned char bytes
[2];
4329 unsigned char bytes
[4];
4334 unsigned char bytes
[8];
4339 if (aexpr
->length
== 0)
4341 trace_debug ("empty agent expression");
4342 return expr_eval_empty_expression
;
4345 /* Cache the stack top in its own variable. Much of the time we can
4346 operate on this variable, rather than dinking with the stack. It
4347 needs to be copied to the stack when sp changes. */
4352 op
= aexpr
->bytes
[pc
++];
4354 trace_debug ("About to interpret byte 0x%x", op
);
4358 case gdb_agent_op_add
:
4362 case gdb_agent_op_sub
:
4363 top
= stack
[--sp
] - top
;
4366 case gdb_agent_op_mul
:
4370 case gdb_agent_op_div_signed
:
4373 trace_debug ("Attempted to divide by zero");
4374 return expr_eval_divide_by_zero
;
4376 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
4379 case gdb_agent_op_div_unsigned
:
4382 trace_debug ("Attempted to divide by zero");
4383 return expr_eval_divide_by_zero
;
4385 top
= stack
[--sp
] / top
;
4388 case gdb_agent_op_rem_signed
:
4391 trace_debug ("Attempted to divide by zero");
4392 return expr_eval_divide_by_zero
;
4394 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
4397 case gdb_agent_op_rem_unsigned
:
4400 trace_debug ("Attempted to divide by zero");
4401 return expr_eval_divide_by_zero
;
4403 top
= stack
[--sp
] % top
;
4406 case gdb_agent_op_lsh
:
4407 top
= stack
[--sp
] << top
;
4410 case gdb_agent_op_rsh_signed
:
4411 top
= ((LONGEST
) stack
[--sp
]) >> top
;
4414 case gdb_agent_op_rsh_unsigned
:
4415 top
= stack
[--sp
] >> top
;
4418 case gdb_agent_op_trace
:
4419 agent_mem_read (tframe
,
4420 NULL
, (CORE_ADDR
) stack
[--sp
], (ULONGEST
) top
);
4425 case gdb_agent_op_trace_quick
:
4426 arg
= aexpr
->bytes
[pc
++];
4427 agent_mem_read (tframe
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
4430 case gdb_agent_op_log_not
:
4434 case gdb_agent_op_bit_and
:
4438 case gdb_agent_op_bit_or
:
4442 case gdb_agent_op_bit_xor
:
4446 case gdb_agent_op_bit_not
:
4450 case gdb_agent_op_equal
:
4451 top
= (stack
[--sp
] == top
);
4454 case gdb_agent_op_less_signed
:
4455 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
4458 case gdb_agent_op_less_unsigned
:
4459 top
= (stack
[--sp
] < top
);
4462 case gdb_agent_op_ext
:
4463 arg
= aexpr
->bytes
[pc
++];
4464 if (arg
< (sizeof (LONGEST
) * 8))
4466 LONGEST mask
= 1 << (arg
- 1);
4467 top
&= ((LONGEST
) 1 << arg
) - 1;
4468 top
= (top
^ mask
) - mask
;
4472 case gdb_agent_op_ref8
:
4473 agent_mem_read (tframe
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
4477 case gdb_agent_op_ref16
:
4478 agent_mem_read (tframe
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
4482 case gdb_agent_op_ref32
:
4483 agent_mem_read (tframe
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
4487 case gdb_agent_op_ref64
:
4488 agent_mem_read (tframe
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
4492 case gdb_agent_op_if_goto
:
4494 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4501 case gdb_agent_op_goto
:
4502 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4505 case gdb_agent_op_const8
:
4506 /* Flush the cached stack top. */
4508 top
= aexpr
->bytes
[pc
++];
4511 case gdb_agent_op_const16
:
4512 /* Flush the cached stack top. */
4514 top
= aexpr
->bytes
[pc
++];
4515 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4518 case gdb_agent_op_const32
:
4519 /* Flush the cached stack top. */
4521 top
= aexpr
->bytes
[pc
++];
4522 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4523 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4524 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4527 case gdb_agent_op_const64
:
4528 /* Flush the cached stack top. */
4530 top
= aexpr
->bytes
[pc
++];
4531 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4532 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4533 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4534 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4535 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4536 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4537 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4540 case gdb_agent_op_reg
:
4541 /* Flush the cached stack top. */
4543 arg
= aexpr
->bytes
[pc
++];
4544 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4547 struct regcache
*regcache
;
4549 regcache
= get_context_regcache (ctx
);
4551 switch (register_size (regnum
))
4554 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
4558 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
4562 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
4566 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
4570 internal_error (__FILE__
, __LINE__
,
4571 "unhandled register size");
4576 case gdb_agent_op_end
:
4577 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4578 sp
, pulongest (top
));
4583 /* This should be an error */
4584 trace_debug ("Stack is empty, nothing to return");
4585 return expr_eval_empty_stack
;
4589 return expr_eval_no_error
;
4591 case gdb_agent_op_dup
:
4595 case gdb_agent_op_pop
:
4600 case gdb_agent_op_zero_ext
:
4601 arg
= aexpr
->bytes
[pc
++];
4602 if (arg
< (sizeof (LONGEST
) * 8))
4603 top
&= ((LONGEST
) 1 << arg
) - 1;
4606 case gdb_agent_op_swap
:
4607 /* Interchange top two stack elements, making sure top gets
4608 copied back onto stack. */
4610 top
= stack
[sp
- 1];
4611 stack
[sp
- 1] = stack
[sp
];
4614 case gdb_agent_op_getv
:
4615 /* Flush the cached stack top. */
4617 arg
= aexpr
->bytes
[pc
++];
4618 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4619 top
= get_trace_state_variable_value (arg
);
4622 case gdb_agent_op_setv
:
4623 arg
= aexpr
->bytes
[pc
++];
4624 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4625 set_trace_state_variable_value (arg
, top
);
4626 /* Note that we leave the value on the stack, for the
4627 benefit of later/enclosing expressions. */
4630 case gdb_agent_op_tracev
:
4631 arg
= aexpr
->bytes
[pc
++];
4632 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4633 agent_tsv_read (tframe
, arg
);
4636 /* GDB never (currently) generates any of these ops. */
4637 case gdb_agent_op_float
:
4638 case gdb_agent_op_ref_float
:
4639 case gdb_agent_op_ref_double
:
4640 case gdb_agent_op_ref_long_double
:
4641 case gdb_agent_op_l_to_d
:
4642 case gdb_agent_op_d_to_l
:
4643 case gdb_agent_op_trace16
:
4644 trace_debug ("Agent expression op 0x%x valid, but not handled",
4646 /* If ever GDB generates any of these, we don't have the
4647 option of ignoring. */
4651 trace_debug ("Agent expression op 0x%x not recognized", op
);
4652 /* Don't struggle on, things will just get worse. */
4653 return expr_eval_unrecognized_opcode
;
4656 /* Check for stack badness. */
4657 if (sp
>= (STACK_MAX
- 1))
4659 trace_debug ("Expression stack overflow");
4660 return expr_eval_stack_overflow
;
4665 trace_debug ("Expression stack underflow");
4666 return expr_eval_stack_underflow
;
4669 trace_debug ("Op %s -> sp=%d, top=0x%s",
4670 gdb_agent_op_names
[op
], sp
, pulongest (top
));
4674 /* Do memory copies for bytecodes. */
4675 /* Do the recording of memory blocks for actions and bytecodes. */
4678 agent_mem_read (struct traceframe
*tframe
,
4679 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4681 unsigned char *mspace
;
4682 ULONGEST remaining
= len
;
4683 unsigned short blocklen
;
4685 /* If a 'to' buffer is specified, use it. */
4688 read_inferior_memory (from
, to
, len
);
4692 /* Otherwise, create a new memory block in the trace buffer. */
4693 while (remaining
> 0)
4697 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4698 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4699 mspace
= add_traceframe_block (tframe
, sp
);
4702 /* Identify block as a memory block. */
4705 /* Record address and size. */
4706 memcpy (mspace
, &from
, sizeof (from
));
4707 mspace
+= sizeof (from
);
4708 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4709 mspace
+= sizeof (blocklen
);
4710 /* Record the memory block proper. */
4711 read_inferior_memory (from
, mspace
, blocklen
);
4712 trace_debug ("%d bytes recorded", blocklen
);
4713 remaining
-= blocklen
;
4719 /* Record the value of a trace state variable. */
4722 agent_tsv_read (struct traceframe
*tframe
, int n
)
4724 unsigned char *vspace
;
4727 vspace
= add_traceframe_block (tframe
,
4728 1 + sizeof (n
) + sizeof (LONGEST
));
4731 /* Identify block as a variable. */
4733 /* Record variable's number and value. */
4734 memcpy (vspace
+ 1, &n
, sizeof (n
));
4735 val
= get_trace_state_variable_value (n
);
4736 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4737 trace_debug ("Variable %d recorded", n
);
4741 #ifndef IN_PROCESS_AGENT
4743 static unsigned char *
4744 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4745 int tfnum
, char type_wanted
)
4747 unsigned char *dataptr
;
4751 trace_debug ("traceframe %d has no data", tfnum
);
4755 /* Iterate through a traceframe's blocks, looking for a block of the
4757 for (dataptr
= database
;
4758 dataptr
< database
+ datasize
;
4762 unsigned short mlen
;
4764 if (dataptr
== trace_buffer_wrap
)
4766 /* Adjust to reflect wrapping part of the frame around to
4768 datasize
= dataptr
- database
;
4769 dataptr
= database
= trace_buffer_lo
;
4771 blocktype
= *dataptr
++;
4773 if (type_wanted
== blocktype
)
4779 /* Skip over the registers block. */
4780 dataptr
+= register_cache_size ();
4783 /* Skip over the memory block. */
4784 dataptr
+= sizeof (CORE_ADDR
);
4785 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4786 dataptr
+= (sizeof (mlen
) + mlen
);
4789 /* Skip over the TSV block. */
4790 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4793 /* Skip over the static trace data block. */
4794 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4795 dataptr
+= (sizeof (mlen
) + mlen
);
4798 trace_debug ("traceframe %d has unknown block type 0x%x",
4807 static unsigned char *
4808 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4810 unsigned char *regblock
;
4812 regblock
= traceframe_find_block_type (tframe
->data
,
4816 if (regblock
== NULL
)
4817 trace_debug ("traceframe %d has no register data", tfnum
);
4822 /* Get registers from a traceframe. */
4825 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4827 unsigned char *dataptr
;
4828 struct tracepoint
*tpoint
;
4829 struct traceframe
*tframe
;
4831 tframe
= find_traceframe (tfnum
);
4835 trace_debug ("traceframe %d not found", tfnum
);
4839 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4840 if (dataptr
== NULL
)
4842 /* We don't like making up numbers, but GDB has all manner of
4843 troubles when the target says there are no registers. */
4844 supply_regblock (regcache
, NULL
);
4846 /* We can generally guess at a PC, although this will be
4847 misleading for while-stepping frames and multi-location
4849 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4851 regcache_write_pc (regcache
, tpoint
->address
);
4854 supply_regblock (regcache
, dataptr
);
4860 traceframe_get_pc (struct traceframe
*tframe
)
4862 struct regcache regcache
;
4863 unsigned char *dataptr
;
4865 dataptr
= traceframe_find_regblock (tframe
, -1);
4866 if (dataptr
== NULL
)
4869 init_register_cache (®cache
, dataptr
);
4870 return regcache_read_pc (®cache
);
4873 /* Read a requested block of memory from a trace frame. */
4876 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
4877 unsigned char *buf
, ULONGEST length
,
4880 struct traceframe
*tframe
;
4881 unsigned char *database
, *dataptr
;
4882 unsigned int datasize
;
4884 unsigned short mlen
;
4886 trace_debug ("traceframe_read_mem");
4888 tframe
= find_traceframe (tfnum
);
4892 trace_debug ("traceframe %d not found", tfnum
);
4896 datasize
= tframe
->data_size
;
4897 database
= dataptr
= &tframe
->data
[0];
4899 /* Iterate through a traceframe's blocks, looking for memory. */
4900 while ((dataptr
= traceframe_find_block_type (dataptr
,
4901 datasize
- (dataptr
- database
),
4902 tfnum
, 'M')) != NULL
)
4904 memcpy (&maddr
, dataptr
, sizeof (maddr
));
4905 dataptr
+= sizeof (maddr
);
4906 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4907 dataptr
+= sizeof (mlen
);
4908 trace_debug ("traceframe %d has %d bytes at %s",
4909 tfnum
, mlen
, paddress (maddr
));
4911 /* Check that requested data is in bounds. */
4912 if (maddr
<= addr
&& (addr
+ length
) <= (maddr
+ mlen
))
4914 /* Block includes the requested range, copy it out. */
4915 memcpy (buf
, dataptr
+ (addr
- maddr
), length
);
4920 /* Skip over this block. */
4924 trace_debug ("traceframe %d has no memory data for the desired region",
4932 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
4935 struct traceframe
*tframe
;
4936 unsigned char *database
, *dataptr
;
4937 unsigned int datasize
;
4940 trace_debug ("traceframe_read_tsv");
4942 tfnum
= current_traceframe
;
4946 trace_debug ("no current traceframe");
4950 tframe
= find_traceframe (tfnum
);
4954 trace_debug ("traceframe %d not found", tfnum
);
4958 datasize
= tframe
->data_size
;
4959 database
= dataptr
= &tframe
->data
[0];
4961 /* Iterate through a traceframe's blocks, looking for the tsv. */
4962 while ((dataptr
= traceframe_find_block_type (dataptr
,
4963 datasize
- (dataptr
- database
),
4964 tfnum
, 'V')) != NULL
)
4966 memcpy (&vnum
, dataptr
, sizeof (vnum
));
4967 dataptr
+= sizeof (vnum
);
4969 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
4971 /* Check that this is the variable we want. */
4974 memcpy (val
, dataptr
, sizeof (*val
));
4978 /* Skip over this block. */
4979 dataptr
+= sizeof (LONGEST
);
4982 trace_debug ("traceframe %d has no data for variable %d",
4987 /* Read a requested block of static tracepoint data from a trace
4991 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
4992 unsigned char *buf
, ULONGEST length
,
4995 struct traceframe
*tframe
;
4996 unsigned char *database
, *dataptr
;
4997 unsigned int datasize
;
4998 unsigned short mlen
;
5000 trace_debug ("traceframe_read_sdata");
5002 tframe
= find_traceframe (tfnum
);
5006 trace_debug ("traceframe %d not found", tfnum
);
5010 datasize
= tframe
->data_size
;
5011 database
= &tframe
->data
[0];
5013 /* Iterate through a traceframe's blocks, looking for static
5015 dataptr
= traceframe_find_block_type (database
, datasize
,
5017 if (dataptr
!= NULL
)
5019 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5020 dataptr
+= sizeof (mlen
);
5023 if (offset
+ length
> mlen
)
5024 length
= mlen
- offset
;
5026 memcpy (buf
, dataptr
, length
);
5034 trace_debug ("traceframe %d has no static trace data", tfnum
);
5040 /* Return the first fast tracepoint whose jump pad contains PC. */
5042 static struct tracepoint
*
5043 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5045 struct tracepoint
*tpoint
;
5047 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5048 if (tpoint
->type
== fast_tracepoint
)
5049 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5055 /* Return GDBserver's tracepoint that matches the IP Agent's
5056 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5059 static struct tracepoint
*
5060 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5062 struct tracepoint
*tpoint
;
5064 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5065 if (tpoint
->type
== fast_tracepoint
)
5066 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5074 /* The type of the object that is used to synchronize fast tracepoint
5077 typedef struct collecting_t
5079 /* The fast tracepoint number currently collecting. */
5082 /* A number that GDBserver can use to identify the thread that is
5083 presently holding the collect lock. This need not (and usually
5084 is not) the thread id, as getting the current thread ID usually
5085 requires a system call, which we want to avoid like the plague.
5086 Usually this is thread's TCB, found in the TLS (pseudo-)
5087 register, which is readable with a single insn on several
5089 uintptr_t thread_area
;
5092 #ifndef IN_PROCESS_AGENT
5095 force_unlock_trace_buffer (void)
5097 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5100 /* Check if the thread identified by THREAD_AREA which is stopped at
5101 STOP_PC, is presently locking the fast tracepoint collection, and
5102 if so, gather some status of said collection. Returns 0 if the
5103 thread isn't collecting or in the jump pad at all. 1, if in the
5104 jump pad (or within gdb_collect) and hasn't executed the adjusted
5105 original insn yet (can set a breakpoint there and run to it). 2,
5106 if presently executing the adjusted original insn --- in which
5107 case, if we want to move the thread out of the jump pad, we need to
5108 single-step it until this function returns 0. */
5111 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5113 struct fast_tpoint_collect_status
*status
)
5115 CORE_ADDR ipa_collecting
;
5116 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5117 struct tracepoint
*tpoint
;
5118 int needs_breakpoint
;
5120 /* The thread THREAD_AREA is either:
5122 0. not collecting at all, not within the jump pad, or within
5123 gdb_collect or one of its callees.
5125 1. in the jump pad and haven't reached gdb_collect
5127 2. within gdb_collect (out of the jump pad) (collect is set)
5129 3. we're in the jump pad, after gdb_collect having returned,
5130 possibly executing the adjusted insns.
5132 For cases 1 and 3, `collecting' may or not be set. The jump pad
5133 doesn't have any complicated jump logic, so we can tell if the
5134 thread is executing the adjust original insn or not by just
5135 matching STOP_PC with known jump pad addresses. If we it isn't
5136 yet executing the original insn, set a breakpoint there, and let
5137 the thread run to it, so to quickly step over a possible (many
5138 insns) gdb_collect call. Otherwise, or when the breakpoint is
5139 hit, only a few (small number of) insns are left to be executed
5140 in the jump pad. Single-step the thread until it leaves the
5145 needs_breakpoint
= 0;
5146 trace_debug ("fast_tracepoint_collecting");
5148 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5149 &ipa_gdb_jump_pad_buffer
))
5150 fatal ("error extracting `gdb_jump_pad_buffer'");
5151 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5152 &ipa_gdb_jump_pad_buffer_end
))
5153 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5155 if (ipa_gdb_jump_pad_buffer
<= stop_pc
&& stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5157 /* We can tell which tracepoint(s) the thread is collecting by
5158 matching the jump pad address back to the tracepoint. */
5159 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5162 warning ("in jump pad, but no matching tpoint?");
5167 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5169 tpoint
->number
, paddress (tpoint
->address
),
5170 paddress (tpoint
->jump_pad
),
5171 paddress (tpoint
->jump_pad_end
),
5172 paddress (tpoint
->adjusted_insn_addr
),
5173 paddress (tpoint
->adjusted_insn_addr_end
));
5176 /* Definitely in the jump pad. May or may not need
5177 fast-exit-jump-pad breakpoint. */
5178 if (tpoint
->jump_pad
<= stop_pc
5179 && stop_pc
< tpoint
->adjusted_insn_addr
)
5180 needs_breakpoint
= 1;
5184 collecting_t ipa_collecting_obj
;
5186 /* If `collecting' is set/locked, then the THREAD_AREA thread
5187 may or not be the one holding the lock. We have to read the
5188 lock to find out. */
5190 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5193 trace_debug ("fast_tracepoint_collecting:"
5194 " failed reading 'collecting' in the inferior");
5198 if (!ipa_collecting
)
5200 trace_debug ("fast_tracepoint_collecting: not collecting"
5201 " (and nobody is).");
5205 /* Some thread is collecting. Check which. */
5206 if (read_inferior_memory (ipa_collecting
,
5207 (unsigned char *) &ipa_collecting_obj
,
5208 sizeof (ipa_collecting_obj
)) != 0)
5211 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5213 trace_debug ("fast_tracepoint_collecting: not collecting "
5214 "(another thread is)");
5219 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5222 warning ("fast_tracepoint_collecting: collecting, "
5223 "but tpoint %s not found?",
5224 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5228 /* The thread is within `gdb_collect', skip over the rest of
5229 fast tracepoint collection quickly using a breakpoint. */
5230 needs_breakpoint
= 1;
5233 /* The caller wants a bit of status detail. */
5236 status
->tpoint_num
= tpoint
->number
;
5237 status
->tpoint_addr
= tpoint
->address
;
5238 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5239 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5242 if (needs_breakpoint
)
5244 /* Hasn't executed the original instruction yet. Set breakpoint
5245 there, and wait till it's hit, then single-step until exiting
5249 fast_tracepoint_collecting, returning continue-until-break at %s",
5250 paddress (tpoint
->adjusted_insn_addr
));
5252 return 1; /* continue */
5256 /* Just single-step until exiting the jump pad. */
5258 trace_debug ("fast_tracepoint_collecting, returning "
5259 "need-single-step (%s-%s)",
5260 paddress (tpoint
->adjusted_insn_addr
),
5261 paddress (tpoint
->adjusted_insn_addr_end
));
5263 return 2; /* single-step */
5269 #ifdef IN_PROCESS_AGENT
5271 /* The global fast tracepoint collect lock. Points to a collecting_t
5272 object built on the stack by the jump pad, if presently locked;
5273 NULL if it isn't locked. Note that this lock *must* be set while
5274 executing any *function other than the jump pad. See
5275 fast_tracepoint_collecting. */
5276 static collecting_t
* ATTR_USED collecting
;
5278 /* This routine, called from the jump pad (in asm) is designed to be
5279 called from the jump pads of fast tracepoints, thus it is on the
5282 IP_AGENT_EXPORT
void ATTR_USED
5283 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5285 struct fast_tracepoint_ctx ctx
;
5287 /* Don't do anything until the trace run is completely set up. */
5291 ctx
.base
.type
= fast_tracepoint
;
5293 ctx
.regcache_initted
= 0;
5294 ctx
.tpoint
= tpoint
;
5296 /* Wrap the regblock in a register cache (in the stack, we don't
5297 want to malloc here). */
5298 ctx
.regspace
= alloca (register_cache_size ());
5299 if (ctx
.regspace
== NULL
)
5301 trace_debug ("Trace buffer block allocation failed, skipping");
5305 /* Test the condition if present, and collect if true. */
5306 if (tpoint
->cond
== NULL
5307 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5310 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5311 tpoint
->address
, tpoint
);
5313 /* Note that this will cause original insns to be written back
5314 to where we jumped from, but that's OK because we're jumping
5315 back to the next whole instruction. This will go badly if
5316 instruction restoration is not atomic though. */
5317 if (stopping_tracepoint
5318 || trace_buffer_is_full
5319 || expr_eval_result
!= expr_eval_no_error
)
5324 /* If there was a condition and it evaluated to false, the only
5325 way we would stop tracing is if there was an error during
5326 condition expression evaluation. */
5327 if (expr_eval_result
!= expr_eval_no_error
)
5334 #ifndef IN_PROCESS_AGENT
5336 /* Bytecode compilation. */
5338 CORE_ADDR current_insn_ptr
;
5342 struct bytecode_address
5347 /* Offset and size of field to be modified in the goto block. */
5348 int from_offset
, from_size
;
5349 struct bytecode_address
*next
;
5350 } *bytecode_address_table
;
5353 get_raw_reg_func_addr (void)
5355 return ipa_sym_addrs
.addr_get_raw_reg
;
5359 emit_prologue (void)
5361 target_emit_ops ()->emit_prologue ();
5365 emit_epilogue (void)
5367 target_emit_ops ()->emit_epilogue ();
5373 target_emit_ops ()->emit_add ();
5379 target_emit_ops ()->emit_sub ();
5385 target_emit_ops ()->emit_mul ();
5391 target_emit_ops ()->emit_lsh ();
5395 emit_rsh_signed (void)
5397 target_emit_ops ()->emit_rsh_signed ();
5401 emit_rsh_unsigned (void)
5403 target_emit_ops ()->emit_rsh_unsigned ();
5409 target_emit_ops ()->emit_ext (arg
);
5415 target_emit_ops ()->emit_log_not ();
5421 target_emit_ops ()->emit_bit_and ();
5427 target_emit_ops ()->emit_bit_or ();
5433 target_emit_ops ()->emit_bit_xor ();
5439 target_emit_ops ()->emit_bit_not ();
5445 target_emit_ops ()->emit_equal ();
5449 emit_less_signed (void)
5451 target_emit_ops ()->emit_less_signed ();
5455 emit_less_unsigned (void)
5457 target_emit_ops ()->emit_less_unsigned ();
5463 target_emit_ops ()->emit_ref (size
);
5467 emit_if_goto (int *offset_p
, int *size_p
)
5469 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
5473 emit_goto (int *offset_p
, int *size_p
)
5475 target_emit_ops ()->emit_goto (offset_p
, size_p
);
5479 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
5481 target_emit_ops ()->write_goto_address (from
, to
, size
);
5485 emit_const (LONGEST num
)
5487 target_emit_ops ()->emit_const (num
);
5493 target_emit_ops ()->emit_reg (reg
);
5499 target_emit_ops ()->emit_pop ();
5503 emit_stack_flush (void)
5505 target_emit_ops ()->emit_stack_flush ();
5509 emit_zero_ext (int arg
)
5511 target_emit_ops ()->emit_zero_ext (arg
);
5517 target_emit_ops ()->emit_swap ();
5521 emit_stack_adjust (int n
)
5523 target_emit_ops ()->emit_stack_adjust (n
);
5526 /* FN's prototype is `LONGEST(*fn)(int)'. */
5529 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
5531 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
5534 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
5537 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
5539 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
5542 static enum eval_result_type
compile_bytecodes (struct agent_expr
*aexpr
);
5545 compile_tracepoint_condition (struct tracepoint
*tpoint
, CORE_ADDR
*jump_entry
)
5547 CORE_ADDR entry_point
= *jump_entry
;
5548 enum eval_result_type err
;
5550 trace_debug ("Starting condition compilation for tracepoint %d\n",
5553 /* Initialize the global pointer to the code being built. */
5554 current_insn_ptr
= *jump_entry
;
5558 err
= compile_bytecodes (tpoint
->cond
);
5560 if (err
== expr_eval_no_error
)
5564 /* Record the beginning of the compiled code. */
5565 tpoint
->compiled_cond
= entry_point
;
5567 trace_debug ("Condition compilation for tracepoint %d complete\n",
5572 /* Leave the unfinished code in situ, but don't point to it. */
5574 tpoint
->compiled_cond
= 0;
5576 trace_debug ("Condition compilation for tracepoint %d failed, "
5578 tpoint
->number
, err
);
5581 /* Update the code pointer passed in. Note that we do this even if
5582 the compile fails, so that we can look at the partial results
5583 instead of letting them be overwritten. */
5584 *jump_entry
= current_insn_ptr
;
5586 /* Leave a gap, to aid dump decipherment. */
5590 /* Given an agent expression, turn it into native code. */
5592 static enum eval_result_type
5593 compile_bytecodes (struct agent_expr
*aexpr
)
5599 /* This is only used to build 64-bit value for constants. */
5601 struct bytecode_address
*aentry
, *aentry2
;
5606 trace_debug ("Cannot compile op 0x%x\n", op); \
5607 return expr_eval_unhandled_opcode; \
5610 if (aexpr
->length
== 0)
5612 trace_debug ("empty agent expression\n");
5613 return expr_eval_empty_expression
;
5616 bytecode_address_table
= NULL
;
5620 op
= aexpr
->bytes
[pc
];
5622 trace_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
5624 /* Record the compiled-code address of the bytecode, for use by
5625 jump instructions. */
5626 aentry
= xmalloc (sizeof (struct bytecode_address
));
5628 aentry
->address
= current_insn_ptr
;
5629 aentry
->goto_pc
= -1;
5630 aentry
->from_offset
= aentry
->from_size
= 0;
5631 aentry
->next
= bytecode_address_table
;
5632 bytecode_address_table
= aentry
;
5640 case gdb_agent_op_add
:
5644 case gdb_agent_op_sub
:
5648 case gdb_agent_op_mul
:
5652 case gdb_agent_op_div_signed
:
5656 case gdb_agent_op_div_unsigned
:
5660 case gdb_agent_op_rem_signed
:
5664 case gdb_agent_op_rem_unsigned
:
5668 case gdb_agent_op_lsh
:
5672 case gdb_agent_op_rsh_signed
:
5676 case gdb_agent_op_rsh_unsigned
:
5677 emit_rsh_unsigned ();
5680 case gdb_agent_op_trace
:
5684 case gdb_agent_op_trace_quick
:
5688 case gdb_agent_op_log_not
:
5692 case gdb_agent_op_bit_and
:
5696 case gdb_agent_op_bit_or
:
5700 case gdb_agent_op_bit_xor
:
5704 case gdb_agent_op_bit_not
:
5708 case gdb_agent_op_equal
:
5712 case gdb_agent_op_less_signed
:
5713 emit_less_signed ();
5716 case gdb_agent_op_less_unsigned
:
5717 emit_less_unsigned ();
5720 case gdb_agent_op_ext
:
5721 arg
= aexpr
->bytes
[pc
++];
5722 if (arg
< (sizeof (LONGEST
) * 8))
5726 case gdb_agent_op_ref8
:
5730 case gdb_agent_op_ref16
:
5734 case gdb_agent_op_ref32
:
5738 case gdb_agent_op_ref64
:
5742 case gdb_agent_op_if_goto
:
5743 arg
= aexpr
->bytes
[pc
++];
5744 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5745 aentry
->goto_pc
= arg
;
5746 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5749 case gdb_agent_op_goto
:
5750 arg
= aexpr
->bytes
[pc
++];
5751 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5752 aentry
->goto_pc
= arg
;
5753 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5756 case gdb_agent_op_const8
:
5757 emit_stack_flush ();
5758 top
= aexpr
->bytes
[pc
++];
5762 case gdb_agent_op_const16
:
5763 emit_stack_flush ();
5764 top
= aexpr
->bytes
[pc
++];
5765 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5769 case gdb_agent_op_const32
:
5770 emit_stack_flush ();
5771 top
= aexpr
->bytes
[pc
++];
5772 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5773 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5774 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5778 case gdb_agent_op_const64
:
5779 emit_stack_flush ();
5780 top
= aexpr
->bytes
[pc
++];
5781 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5782 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5783 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5784 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5785 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5786 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5787 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5791 case gdb_agent_op_reg
:
5792 emit_stack_flush ();
5793 arg
= aexpr
->bytes
[pc
++];
5794 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5798 case gdb_agent_op_end
:
5799 trace_debug ("At end of expression\n");
5801 /* Assume there is one stack element left, and that it is
5802 cached in "top" where emit_epilogue can get to it. */
5803 emit_stack_adjust (1);
5808 case gdb_agent_op_dup
:
5809 /* In our design, dup is equivalent to stack flushing. */
5810 emit_stack_flush ();
5813 case gdb_agent_op_pop
:
5817 case gdb_agent_op_zero_ext
:
5818 arg
= aexpr
->bytes
[pc
++];
5819 if (arg
< (sizeof (LONGEST
) * 8))
5820 emit_zero_ext (arg
);
5823 case gdb_agent_op_swap
:
5827 case gdb_agent_op_getv
:
5828 emit_stack_flush ();
5829 arg
= aexpr
->bytes
[pc
++];
5830 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5831 emit_int_call_1 (ipa_sym_addrs
.addr_get_trace_state_variable_value
,
5835 case gdb_agent_op_setv
:
5836 arg
= aexpr
->bytes
[pc
++];
5837 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5838 emit_void_call_2 (ipa_sym_addrs
.addr_set_trace_state_variable_value
,
5842 case gdb_agent_op_tracev
:
5846 /* GDB never (currently) generates any of these ops. */
5847 case gdb_agent_op_float
:
5848 case gdb_agent_op_ref_float
:
5849 case gdb_agent_op_ref_double
:
5850 case gdb_agent_op_ref_long_double
:
5851 case gdb_agent_op_l_to_d
:
5852 case gdb_agent_op_d_to_l
:
5853 case gdb_agent_op_trace16
:
5858 trace_debug ("Agent expression op 0x%x not recognized\n", op
);
5859 /* Don't struggle on, things will just get worse. */
5860 return expr_eval_unrecognized_opcode
;
5863 /* This catches errors that occur in target-specific code
5867 trace_debug ("Error %d while emitting code for %s\n",
5868 emit_error
, gdb_agent_op_names
[op
]);
5869 return expr_eval_unhandled_opcode
;
5872 trace_debug ("Op %s compiled\n", gdb_agent_op_names
[op
]);
5875 /* Now fill in real addresses as goto destinations. */
5876 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
5880 if (aentry
->goto_pc
< 0)
5883 /* Find the location that we are going to, and call back into
5884 target-specific code to write the actual address or
5886 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
5888 if (aentry2
->pc
== aentry
->goto_pc
)
5890 trace_debug ("Want to jump from %s to %s\n",
5891 paddress (aentry
->address
),
5892 paddress (aentry2
->address
));
5893 write_goto_address (aentry
->address
+ aentry
->from_offset
,
5894 aentry2
->address
, aentry
->from_size
);
5900 /* Error out if we didn't find a destination. */
5903 trace_debug ("Destination of goto %d not found\n",
5905 return expr_eval_invalid_goto
;
5909 return expr_eval_no_error
;
5912 /* We'll need to adjust these when we consider bi-arch setups, and big
5916 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5918 return write_inferior_memory (where
,
5919 (unsigned char *) &ptr
, sizeof (void *));
5922 /* The base pointer of the IPA's heap. This is the only memory the
5923 IPA is allowed to use. The IPA should _not_ call the inferior's
5924 `malloc' during operation. That'd be slow, and, most importantly,
5925 it may not be safe. We may be collecting a tracepoint in a signal
5926 handler, for example. */
5927 static CORE_ADDR target_tp_heap
;
5929 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5933 target_malloc (ULONGEST size
)
5937 if (target_tp_heap
== 0)
5939 /* We have the pointer *address*, need what it points to. */
5940 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5942 fatal ("could get target heap head pointer");
5945 ptr
= target_tp_heap
;
5946 target_tp_heap
+= size
;
5948 /* Pad to 8-byte alignment. */
5949 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5955 download_agent_expr (struct agent_expr
*expr
)
5957 CORE_ADDR expr_addr
;
5958 CORE_ADDR expr_bytes
;
5960 expr_addr
= target_malloc (sizeof (*expr
));
5961 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5963 expr_bytes
= target_malloc (expr
->length
);
5964 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5966 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5971 /* Align V up to N bits. */
5972 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5975 download_tracepoints (void)
5977 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
5978 struct tracepoint
*tpoint
;
5980 /* Start out empty. */
5981 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
5983 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5985 struct tracepoint target_tracepoint
;
5987 if (tpoint
->type
!= fast_tracepoint
5988 && tpoint
->type
!= static_tracepoint
)
5991 /* Maybe download a compiled condition. */
5992 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5994 CORE_ADDR jentry
, jump_entry
;
5996 jentry
= jump_entry
= get_jump_space_head ();
5998 if (tpoint
->cond
!= NULL
)
6000 /* Pad to 8-byte alignment. (needed?) */
6001 /* Actually this should be left for the target to
6003 jentry
= UALIGN (jentry
, 8);
6005 compile_tracepoint_condition (tpoint
, &jentry
);
6008 /* Pad to 8-byte alignment. */
6009 jentry
= UALIGN (jentry
, 8);
6010 claim_jump_space (jentry
- jump_entry
);
6013 target_tracepoint
= *tpoint
;
6016 tpptr
= target_malloc (sizeof (*tpoint
));
6017 tpoint
->obj_addr_on_target
= tpptr
;
6019 if (tpoint
== tracepoints
)
6021 /* First object in list, set the head pointer in the
6023 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
6027 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
6032 /* Write the whole object. We'll fix up its pointers in a bit.
6033 Assume no next for now. This is fixed up above on the next
6034 iteration, if there's any. */
6035 target_tracepoint
.next
= NULL
;
6036 /* Need to clear this here too, since we're downloading the
6037 tracepoints before clearing our own copy. */
6038 target_tracepoint
.hit_count
= 0;
6040 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
6041 sizeof (target_tracepoint
));
6044 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6046 download_agent_expr (tpoint
->cond
));
6048 if (tpoint
->numactions
)
6051 CORE_ADDR actions_array
;
6053 /* The pointers array. */
6055 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6056 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6060 /* Now for each pointer, download the action. */
6061 for (i
= 0; i
< tpoint
->numactions
; i
++)
6063 CORE_ADDR ipa_action
= 0;
6064 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6066 switch (action
->type
)
6070 = target_malloc (sizeof (struct collect_memory_action
));
6071 write_inferior_memory (ipa_action
,
6072 (unsigned char *) action
,
6073 sizeof (struct collect_memory_action
));
6077 = target_malloc (sizeof (struct collect_registers_action
));
6078 write_inferior_memory (ipa_action
,
6079 (unsigned char *) action
,
6080 sizeof (struct collect_registers_action
));
6085 struct eval_expr_action
*eaction
6086 = (struct eval_expr_action
*) action
;
6088 ipa_action
= target_malloc (sizeof (*eaction
));
6089 write_inferior_memory (ipa_action
,
6090 (unsigned char *) eaction
,
6093 expr
= download_agent_expr (eaction
->expr
);
6094 write_inferior_data_ptr
6095 (ipa_action
+ offsetof (struct eval_expr_action
, expr
),
6100 ipa_action
= target_malloc
6101 (sizeof (struct collect_static_trace_data_action
));
6102 write_inferior_memory
6104 (unsigned char *) action
,
6105 sizeof (struct collect_static_trace_data_action
));
6108 trace_debug ("unknown trace action '%c', ignoring",
6113 if (ipa_action
!= 0)
6114 write_inferior_data_ptr
6115 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
6123 download_trace_state_variables (void)
6125 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6126 struct trace_state_variable
*tsv
;
6128 /* Start out empty. */
6129 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6131 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6133 struct trace_state_variable target_tsv
;
6135 /* TSV's with a getter have been initialized equally in both the
6136 inferior and GDBserver. Skip them. */
6137 if (tsv
->getter
!= NULL
)
6143 ptr
= target_malloc (sizeof (*tsv
));
6145 if (tsv
== trace_state_variables
)
6147 /* First object in list, set the head pointer in the
6150 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6155 write_inferior_data_ptr (prev_ptr
6156 + offsetof (struct trace_state_variable
,
6161 /* Write the whole object. We'll fix up its pointers in a bit.
6162 Assume no next, fixup when needed. */
6163 target_tsv
.next
= NULL
;
6165 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6166 sizeof (target_tsv
));
6168 if (tsv
->name
!= NULL
)
6170 size_t size
= strlen (tsv
->name
) + 1;
6171 CORE_ADDR name_addr
= target_malloc (size
);
6172 write_inferior_memory (name_addr
,
6173 (unsigned char *) tsv
->name
, size
);
6174 write_inferior_data_ptr (ptr
6175 + offsetof (struct trace_state_variable
,
6180 if (tsv
->getter
!= NULL
)
6182 fatal ("what to do with these?");
6188 /* Fixup the next pointer in the last item in the list. */
6189 write_inferior_data_ptr (prev_ptr
+ offsetof (struct trace_state_variable
,
6194 /* Upload complete trace frames out of the IP Agent's trace buffer
6195 into GDBserver's trace buffer. This always uploads either all or
6196 no trace frames. This is the counter part of
6197 `trace_alloc_trace_buffer'. See its description of the atomic
6198 synching mechanism. */
6201 upload_fast_traceframes (void)
6203 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6204 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6206 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6207 unsigned int curr_tbctrl_idx
;
6208 unsigned int ipa_trace_buffer_ctrl_curr
;
6209 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6210 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6211 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6212 CORE_ADDR ipa_trace_buffer_lo
;
6213 CORE_ADDR ipa_trace_buffer_hi
;
6215 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6216 &ipa_traceframe_read_count_racy
))
6218 /* This will happen in most targets if the current thread is
6223 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6224 &ipa_traceframe_write_count_racy
))
6227 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6228 ipa_traceframe_write_count_racy
- ipa_traceframe_read_count_racy
,
6229 ipa_traceframe_write_count_racy
, ipa_traceframe_read_count_racy
);
6231 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6234 about_to_request_buffer_space_bkpt
6235 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6238 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6239 &ipa_trace_buffer_ctrl_curr
))
6242 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6244 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6247 unsigned int prev
, counter
;
6249 /* Update the token, with new counters, and the GDBserver stamp
6250 bit. Alway reuse the current TBC index. */
6251 prev
= ipa_trace_buffer_ctrl_curr
& 0x0007ff00;
6252 counter
= (prev
+ 0x100) & 0x0007ff00;
6254 ipa_trace_buffer_ctrl_curr
= (0x80000000
6260 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6261 ipa_trace_buffer_ctrl_curr
))
6264 trace_debug ("Lib: Committed %08x -> %08x",
6265 ipa_trace_buffer_ctrl_curr_old
,
6266 ipa_trace_buffer_ctrl_curr
);
6268 /* Re-read these, now that we've installed the
6269 `about_to_request_buffer_space' breakpoint/lock. A thread could
6270 have finished a traceframe between the last read of these
6271 counters and setting the breakpoint above. If we start
6272 uploading, we never want to leave this function with
6273 traceframe_read_count != 0, otherwise, GDBserver could end up
6274 incrementing the counter tokens more than once (due to event loop
6275 nesting), which would break the IP agent's "effective" detection
6276 (see trace_alloc_trace_buffer). */
6277 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6278 &ipa_traceframe_read_count
))
6280 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6281 &ipa_traceframe_write_count
))
6286 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6287 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6288 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6290 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6291 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6292 trace_debug ("note that ipa_traceframe_count's parts changed");
6295 /* Get the address of the current TBC object (the IP agent has an
6296 array of 3 such objects). The index is stored in the TBC
6298 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6299 ipa_trace_buffer_ctrl_addr
6300 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6302 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6303 (unsigned char *) &ipa_trace_buffer_ctrl
,
6304 sizeof (struct ipa_trace_buffer_control
)))
6307 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6308 &ipa_trace_buffer_lo
))
6310 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6311 &ipa_trace_buffer_hi
))
6314 /* Offsets are easier to grok for debugging than raw addresses,
6315 especially for the small trace buffer sizes that are useful for
6317 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6318 "endfree=%d wrap=%d hi=%d",
6320 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6321 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6322 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6323 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6324 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6326 /* Note that the IPA's buffer is always circular. */
6328 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6330 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6331 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6333 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6334 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6335 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6336 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6339 tf
= IPA_FIRST_TRACEFRAME ();
6341 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6343 struct tracepoint
*tpoint
;
6344 struct traceframe
*tframe
;
6345 unsigned char *block
;
6346 struct traceframe ipa_tframe
;
6348 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6349 offsetof (struct traceframe
, data
)))
6350 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6352 if (ipa_tframe
.tpnum
== 0)
6353 fatal ("Uploading: No (more) fast traceframes, but "
6354 "ipa_traceframe_count == %u??\n",
6355 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6357 /* Note that this will be incorrect for multi-location
6359 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6361 tframe
= add_traceframe (tpoint
);
6364 trace_buffer_is_full
= 1;
6365 trace_debug ("Uploading: trace buffer is full");
6369 /* Copy the whole set of blocks in one go for now. FIXME:
6370 split this in smaller blocks. */
6371 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6374 if (read_inferior_memory (tf
+ offsetof (struct traceframe
, data
),
6375 block
, ipa_tframe
.data_size
))
6376 error ("Uploading: Couldn't read traceframe data at %s\n",
6377 paddress (tf
+ offsetof (struct traceframe
, data
)));
6380 trace_debug ("Uploading: traceframe didn't fit");
6381 finish_traceframe (tframe
);
6384 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6386 /* If we freed the traceframe that wrapped around, go back
6387 to the non-wrap case. */
6388 if (tf
< ipa_trace_buffer_ctrl
.start
)
6390 trace_debug ("Lib: Discarding past the wraparound");
6391 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6393 ipa_trace_buffer_ctrl
.start
= tf
;
6394 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6395 ++ipa_traceframe_read_count
;
6397 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6398 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6400 trace_debug ("Lib: buffer is fully empty. "
6401 "Trace buffer [%d] start=%d free=%d endfree=%d",
6403 (int) (ipa_trace_buffer_ctrl
.start
6404 - ipa_trace_buffer_lo
),
6405 (int) (ipa_trace_buffer_ctrl
.free
6406 - ipa_trace_buffer_lo
),
6407 (int) (ipa_trace_buffer_ctrl
.end_free
6408 - ipa_trace_buffer_lo
));
6410 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6411 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6412 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6413 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6416 trace_debug ("Uploaded a traceframe\n"
6417 "Lib: Trace buffer [%d] start=%d free=%d "
6418 "endfree=%d wrap=%d hi=%d",
6420 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6421 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6422 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6423 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6424 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6427 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6428 (unsigned char *) &ipa_trace_buffer_ctrl
,
6429 sizeof (struct ipa_trace_buffer_control
)))
6432 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6433 ipa_traceframe_read_count
);
6435 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6438 cancel_breakpoints ();
6440 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6441 about_to_request_buffer_space_bkpt
= NULL
;
6445 if (trace_buffer_is_full
)
6450 #ifdef IN_PROCESS_AGENT
6452 IP_AGENT_EXPORT
int ust_loaded
;
6453 IP_AGENT_EXPORT
char cmd_buf
[CMD_BUF_SIZE
];
6457 /* Static tracepoints. */
6459 /* UST puts a "struct tracepoint" in the global namespace, which
6460 conflicts with our tracepoint. Arguably, being a library, it
6461 shouldn't take ownership of such a generic name. We work around it
6463 #define tracepoint ust_tracepoint
6464 #include <ust/ust.h>
6467 extern int serialize_to_text (char *outbuf
, int bufsize
,
6468 const char *fmt
, va_list ap
);
6470 #define GDB_PROBE_NAME "gdb"
6472 /* We dynamically search for the UST symbols instead of linking them
6473 in. This lets the user decide if the application uses static
6474 tracepoints, instead of always pulling libust.so in. This vector
6475 holds pointers to all functions we care about. */
6479 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6480 const char *fmt
, va_list ap
);
6482 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6483 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6485 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6487 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6490 void (*marker_iter_start
) (struct marker_iter
*iter
);
6491 void (*marker_iter_next
) (struct marker_iter
*iter
);
6492 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6493 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6498 /* Cast through typeof to catch incompatible API changes. Since UST
6499 only builds with gcc, we can freely use gcc extensions here
6501 #define GET_UST_SYM(SYM) \
6504 if (ust_ops.SYM == NULL) \
6505 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6506 if (ust_ops.SYM == NULL) \
6510 #define USTF(SYM) ust_ops.SYM
6512 /* Get pointers to all libust.so functions we care about. */
6517 GET_UST_SYM (serialize_to_text
);
6519 GET_UST_SYM (ltt_probe_register
);
6520 GET_UST_SYM (ltt_probe_unregister
);
6521 GET_UST_SYM (ltt_marker_connect
);
6522 GET_UST_SYM (ltt_marker_disconnect
);
6524 GET_UST_SYM (marker_iter_start
);
6525 GET_UST_SYM (marker_iter_next
);
6526 GET_UST_SYM (marker_iter_stop
);
6527 GET_UST_SYM (marker_iter_reset
);
6533 /* Given an UST marker, return the matching gdb static tracepoint.
6534 The match is done by address. */
6536 static struct tracepoint
*
6537 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6539 struct tracepoint
*tpoint
;
6541 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6543 if (!tpoint
->enabled
|| tpoint
->type
!= static_tracepoint
)
6546 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6553 /* The probe function we install on lttng/ust markers. Whenever a
6554 probed ust marker is hit, this function is called. This is similar
6555 to gdb_collect, only for static tracepoints, instead of fast
6559 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6560 struct registers
*regs
, void *call_private
,
6561 const char *fmt
, va_list *args
)
6563 struct tracepoint
*tpoint
;
6564 struct static_tracepoint_ctx ctx
;
6566 /* Don't do anything until the trace run is completely set up. */
6569 trace_debug ("gdb_probe: not tracing\n");
6573 ctx
.base
.type
= static_tracepoint
;
6574 ctx
.regcache_initted
= 0;
6579 /* Wrap the regblock in a register cache (in the stack, we don't
6580 want to malloc here). */
6581 ctx
.regspace
= alloca (register_cache_size ());
6582 if (ctx
.regspace
== NULL
)
6584 trace_debug ("Trace buffer block allocation failed, skipping");
6588 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6591 trace_debug ("gdb_probe: marker not known: "
6592 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6593 mdata
->location
, mdata
->channel
,
6594 mdata
->name
, mdata
->format
);
6598 ctx
.tpoint
= tpoint
;
6600 trace_debug ("gdb_probe: collecting marker: "
6601 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6602 mdata
->location
, mdata
->channel
,
6603 mdata
->name
, mdata
->format
);
6605 /* Test the condition if present, and collect if true. */
6606 if (tpoint
->cond
== NULL
6607 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6610 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6611 tpoint
->address
, tpoint
);
6613 if (stopping_tracepoint
6614 || trace_buffer_is_full
6615 || expr_eval_result
!= expr_eval_no_error
)
6620 /* If there was a condition and it evaluated to false, the only
6621 way we would stop tracing is if there was an error during
6622 condition expression evaluation. */
6623 if (expr_eval_result
!= expr_eval_no_error
)
6628 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6629 static tracepoint string data. This is a string passed to the
6630 tracing library by the user, at the time of the tracepoint marker
6631 call. E.g., in the UST marker call:
6633 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6635 the collected data is "str FOOBAZ".
6639 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6641 struct tracepoint
*tpoint
,
6642 struct traceframe
*tframe
)
6644 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6645 unsigned char *bufspace
;
6648 unsigned short blocklen
;
6652 trace_debug ("Wanted to collect static trace data, "
6653 "but there's no static trace data");
6657 va_copy (copy
, *umd
->args
);
6658 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6661 trace_debug ("Want to collect ust data");
6663 /* 'S' + size + string */
6664 bufspace
= add_traceframe_block (tframe
,
6665 1 + sizeof (blocklen
) + size
+ 1);
6666 if (bufspace
== NULL
)
6668 trace_debug ("Trace buffer block allocation failed, skipping");
6672 /* Identify a static trace data block. */
6675 blocklen
= size
+ 1;
6676 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6678 va_copy (copy
, *umd
->args
);
6679 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6680 size
+ 1, umd
->fmt
, copy
);
6683 trace_debug ("Storing static tracepoint data in regblock: %s",
6684 bufspace
+ 1 + sizeof (blocklen
));
6687 /* The probe to register with lttng/ust. */
6688 static struct ltt_available_probe gdb_ust_probe
=
6695 #endif /* HAVE_UST */
6696 #endif /* IN_PROCESS_AGENT */
6700 #include <sys/socket.h>
6703 #ifndef UNIX_PATH_MAX
6704 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6707 /* Where we put the socked used for synchronization. */
6708 #define SOCK_DIR P_tmpdir
6710 #endif /* HAVE_UST */
6712 #ifndef IN_PROCESS_AGENT
6717 gdb_ust_connect_sync_socket (int pid
)
6719 struct sockaddr_un addr
;
6721 char path
[UNIX_PATH_MAX
];
6723 res
= xsnprintf (path
, UNIX_PATH_MAX
, "%s/gdb_ust%d", SOCK_DIR
, pid
);
6724 if (res
>= UNIX_PATH_MAX
)
6726 trace_debug ("string overflow allocating socket name");
6730 res
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6733 warning ("error opening sync socket: %s\n", strerror (errno
));
6737 addr
.sun_family
= AF_UNIX
;
6739 res
= xsnprintf (addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
6740 if (res
>= UNIX_PATH_MAX
)
6742 warning ("string overflow allocating socket name\n");
6747 res
= connect (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6750 warning ("error connecting sync socket (%s): %s. "
6751 "Make sure the directory exists and that it is writable.",
6752 path
, strerror (errno
));
6760 /* Resume thread PTID. */
6763 resume_thread (ptid_t ptid
)
6765 struct thread_resume resume_info
;
6767 resume_info
.thread
= ptid
;
6768 resume_info
.kind
= resume_continue
;
6769 resume_info
.sig
= TARGET_SIGNAL_0
;
6770 (*the_target
->resume
) (&resume_info
, 1);
6773 /* Stop thread PTID. */
6776 stop_thread (ptid_t ptid
)
6778 struct thread_resume resume_info
;
6780 resume_info
.thread
= ptid
;
6781 resume_info
.kind
= resume_stop
;
6782 resume_info
.sig
= TARGET_SIGNAL_0
;
6783 (*the_target
->resume
) (&resume_info
, 1);
6786 /* Ask the in-process agent to run a command. Since we don't want to
6787 have to handle the IPA hitting breakpoints while running the
6788 command, we pause all threads, remove all breakpoints, and then set
6789 the helper thread re-running. We communicate with the helper
6790 thread by means of direct memory xfering, and a socket for
6794 run_inferior_command (char *cmd
)
6798 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
6800 ptid_t ptid
= null_ptid
;
6802 trace_debug ("run_inferior_command: running: %s", cmd
);
6805 uninsert_all_breakpoints ();
6807 if (read_inferior_integer (ipa_sym_addrs
.addr_helper_thread_id
, &tid
))
6809 warning ("Error reading helper thread's id in lib");
6815 warning ("helper thread not initialized yet");
6819 if (write_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
6820 (unsigned char *) cmd
, strlen (cmd
) + 1))
6822 warning ("Error writing command");
6826 ptid
= ptid_build (pid
, tid
, 0);
6828 resume_thread (ptid
);
6830 fd
= gdb_ust_connect_sync_socket (pid
);
6836 trace_debug ("signalling helper thread");
6840 ret
= write (fd
, buf
, 1);
6841 } while (ret
== -1 && errno
== EINTR
);
6843 trace_debug ("waiting for helper thread's response");
6847 ret
= read (fd
, buf
, 1);
6848 } while (ret
== -1 && errno
== EINTR
);
6852 trace_debug ("helper thread's response received");
6857 /* Need to read response with the inferior stopped. */
6858 if (!ptid_equal (ptid
, null_ptid
))
6860 int was_non_stop
= non_stop
;
6861 struct target_waitstatus status
;
6865 mywait (ptid
, &status
, 0, 0);
6866 non_stop
= was_non_stop
;
6871 if (read_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
6872 (unsigned char *) cmd
, CMD_BUF_SIZE
))
6874 warning ("Error reading command response");
6879 trace_debug ("run_inferior_command: response: %s", cmd
);
6883 reinsert_all_breakpoints ();
6889 #else /* HAVE_UST */
6892 run_inferior_command (char *cmd
)
6897 #endif /* HAVE_UST */
6899 #else /* !IN_PROCESS_AGENT */
6901 /* Thread ID of the helper thread. GDBserver reads this to know which
6902 is the help thread. This is an LWP id on Linux. */
6903 int helper_thread_id
;
6908 init_named_socket (const char *name
)
6911 struct sockaddr_un addr
;
6913 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6916 warning ("socket creation failed: %s", strerror (errno
));
6920 addr
.sun_family
= AF_UNIX
;
6922 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6923 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6925 result
= access (name
, F_OK
);
6929 result
= unlink (name
);
6932 warning ("unlink failed: %s", strerror (errno
));
6936 warning ("socket %s already exists; overwriting", name
);
6939 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6942 warning ("bind failed: %s", strerror (errno
));
6947 result
= listen (fd
, 1);
6950 warning ("listen: %s", strerror (errno
));
6959 gdb_ust_socket_init (void)
6962 char name
[UNIX_PATH_MAX
];
6964 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6965 SOCK_DIR
, getpid ());
6966 if (result
>= UNIX_PATH_MAX
)
6968 trace_debug ("string overflow allocating socket name");
6972 fd
= init_named_socket (name
);
6974 warning ("Error initializing named socket (%s) for communication with the "
6975 "ust helper thread. Check that directory exists and that it "
6976 "is writable.", name
);
6981 /* Return an hexstr version of the STR C string, fit for sending to
6985 cstr_to_hexstr (const char *str
)
6987 int len
= strlen (str
);
6988 char *hexstr
= xmalloc (len
* 2 + 1);
6989 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6993 /* The next marker to be returned on a qTsSTM command. */
6994 static const struct marker
*next_st
;
6996 /* Returns the first known marker. */
7001 struct marker_iter iter
;
7003 USTF(marker_iter_reset
) (&iter
);
7004 USTF(marker_iter_start
) (&iter
);
7009 /* Returns the marker following M. */
7011 const struct marker
*
7012 next_marker (const struct marker
*m
)
7014 struct marker_iter iter
;
7016 USTF(marker_iter_reset
) (&iter
);
7017 USTF(marker_iter_start
) (&iter
);
7019 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7021 if (iter
.marker
== m
)
7023 USTF(marker_iter_next
) (&iter
);
7031 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
7035 response_ust_marker (char *packet
, const struct marker
*st
)
7037 char *strid
, *format
, *tmp
;
7039 next_st
= next_marker (st
);
7041 tmp
= xmalloc (strlen (st
->channel
) + 1 +
7042 strlen (st
->name
) + 1);
7043 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
7045 strid
= cstr_to_hexstr (tmp
);
7048 format
= cstr_to_hexstr (st
->format
);
7050 sprintf (packet
, "m%s:%s:%s",
7051 paddress ((uintptr_t) st
->location
),
7059 /* Return the first static tracepoint, and initialize the state
7060 machine that will iterate through all the static tracepoints. */
7063 cmd_qtfstm (char *packet
)
7065 trace_debug ("Returning first trace state variable definition");
7067 if (first_marker ())
7068 response_ust_marker (packet
, first_marker ());
7070 strcpy (packet
, "l");
7073 /* Return additional trace state variable definitions. */
7076 cmd_qtsstm (char *packet
)
7078 trace_debug ("Returning static tracepoint");
7081 response_ust_marker (packet
, next_st
);
7083 strcpy (packet
, "l");
7086 /* Disconnect the GDB probe from a marker at a given address. */
7089 unprobe_marker_at (char *packet
)
7093 struct marker_iter iter
;
7095 p
+= sizeof ("unprobe_marker_at:") - 1;
7097 p
= unpack_varlen_hex (p
, &address
);
7099 USTF(marker_iter_reset
) (&iter
);
7100 USTF(marker_iter_start
) (&iter
);
7101 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7102 if ((uintptr_t ) iter
.marker
->location
== address
)
7106 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7110 warning ("could not disable marker %s/%s",
7111 iter
.marker
->channel
, iter
.marker
->name
);
7116 /* Connect the GDB probe to a marker at a given address. */
7119 probe_marker_at (char *packet
)
7123 struct marker_iter iter
;
7126 p
+= sizeof ("probe_marker_at:") - 1;
7128 p
= unpack_varlen_hex (p
, &address
);
7130 USTF(marker_iter_reset
) (&iter
);
7132 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7134 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7135 if ((uintptr_t ) m
->location
== address
)
7139 trace_debug ("found marker for address. "
7140 "ltt_marker_connect (marker = %s/%s)",
7141 m
->channel
, m
->name
);
7143 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
, GDB_PROBE_NAME
);
7144 if (result
&& result
!= -EEXIST
)
7145 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7146 m
->channel
, m
->name
, -result
);
7150 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7151 m
->channel
, m
->name
);
7155 strcpy (packet
, "OK");
7159 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7164 cmd_qtstmat (char *packet
)
7168 struct marker_iter iter
;
7171 p
+= sizeof ("qTSTMat:") - 1;
7173 p
= unpack_varlen_hex (p
, &address
);
7175 USTF(marker_iter_reset
) (&iter
);
7177 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7179 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7180 if ((uintptr_t ) m
->location
== address
)
7182 response_ust_marker (packet
, m
);
7186 strcpy (packet
, "l");
7191 gdb_ust_thread (void *arg
)
7197 listen_fd
= gdb_ust_socket_init ();
7200 if (helper_thread_id
== 0)
7201 helper_thread_id
= syscall (SYS_gettid
);
7204 if (listen_fd
== -1)
7206 warning ("could not create sync socket\n");
7213 struct sockaddr_un sockaddr
;
7218 tmp
= sizeof (sockaddr
);
7222 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7224 /* It seems an ERESTARTSYS can escape out of accept. */
7225 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7229 warning ("Accept returned %d, error: %s\n",
7230 fd
, strerror (errno
));
7236 ret
= read (fd
, buf
, 1);
7237 } while (ret
== -1 && errno
== EINTR
);
7241 warning ("reading socket (fd=%d) failed with %s",
7242 fd
, strerror (errno
));
7249 if (strcmp ("qTfSTM", cmd_buf
) == 0)
7251 cmd_qtfstm (cmd_buf
);
7253 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7255 cmd_qtsstm (cmd_buf
);
7257 else if (strncmp ("unprobe_marker_at:",
7259 sizeof ("unprobe_marker_at:") - 1) == 0)
7261 unprobe_marker_at (cmd_buf
);
7263 else if (strncmp ("probe_marker_at:",
7265 sizeof ("probe_marker_at:") - 1) == 0)
7267 probe_marker_at (cmd_buf
);
7269 else if (strncmp ("qTSTMat:",
7271 sizeof ("qTSTMat:") - 1) == 0)
7273 cmd_qtstmat (cmd_buf
);
7275 else if (strcmp (cmd_buf
, "help") == 0)
7277 strcpy (cmd_buf
, "for help, press F1\n");
7280 strcpy (cmd_buf
, "");
7304 /* We want the helper thread to be as transparent as possible, so
7305 have it inherit an all-signals-blocked mask. */
7307 sigfillset (&new_mask
);
7308 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7310 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7312 res
= pthread_create (&thread
,
7317 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7319 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7321 while (helper_thread_id
== 0)
7324 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7327 #endif /* HAVE_UST */
7329 #include <sys/mman.h>
7332 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7333 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7334 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7336 static void __attribute__ ((constructor
))
7337 initialize_tracepoint_ftlib (void)
7339 initialize_tracepoint ();
7346 #endif /* IN_PROCESS_AGENT */
7349 tsv_get_timestamp (void)
7353 if (gettimeofday (&tv
, 0) != 0)
7356 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7360 initialize_tracepoint (void)
7362 /* There currently no way to change the buffer size. */
7363 const int sizeOfBuffer
= 5 * 1024 * 1024;
7364 unsigned char *buf
= xmalloc (sizeOfBuffer
);
7365 init_trace_buffer (buf
, sizeOfBuffer
);
7367 /* Wire trace state variable 1 to be the timestamp. This will be
7368 uploaded to GDB upon connection and become one of its trace state
7369 variables. (In case you're wondering, if GDB already has a trace
7370 variable numbered 1, it will be renumbered.) */
7371 create_trace_state_variable (1, 0);
7372 set_trace_state_variable_name (1, "trace_timestamp");
7373 set_trace_state_variable_getter (1, tsv_get_timestamp
);
7375 #ifdef IN_PROCESS_AGENT
7378 pagesize
= sysconf (_SC_PAGE_SIZE
);
7382 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7384 /* Allocate scratch buffer aligned on a page boundary. */
7385 gdb_jump_pad_buffer
= memalign (pagesize
, pagesize
* 20);
7386 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* 20;
7388 /* Make it writable and executable. */
7389 if (mprotect (gdb_jump_pad_buffer
, pagesize
* 20,
7390 PROT_READ
| PROT_WRITE
| PROT_EXEC
) != 0)
7392 initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
7393 gdb_jump_pad_buffer
, pagesize
* 20, strerror (errno
));
7396 initialize_low_tracepoint ();