1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 /* This file is built for both both GDBserver, and the in-process
33 agent (IPA), a shared library that includes a tracing agent that is
34 loaded by the inferior to support fast tracepoints. Fast
35 tracepoints (or more accurately, jump based tracepoints) are
36 implemented by patching the tracepoint location with a jump into a
37 small trampoline function whose job is to save the register state,
38 call the in-process tracing agent, and then execute the original
39 instruction that was under the tracepoint jump (possibly adjusted,
40 if PC-relative, or some such).
42 The current synchronization design is pull based. That means,
43 GDBserver does most of the work, by peeking/poking at the inferior
44 agent's memory directly for downloading tracepoint and associated
45 objects, and for uploading trace frames. Whenever the IPA needs
46 something from GDBserver (trace buffer is full, tracing stopped for
47 some reason, etc.) the IPA calls a corresponding hook function
48 where GDBserver has placed a breakpoint.
50 Each of the agents has its own trace buffer. When browsing the
51 trace frames built from slow and fast tracepoints from GDB (tfind
52 mode), there's no guarantee the user is seeing the trace frames in
53 strict chronological creation order, although, GDBserver tries to
54 keep the order relatively reasonable, by syncing the trace buffers
59 static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf
, 1, 2);
62 trace_vdebug (const char *fmt
, ...)
68 vsprintf (buf
, fmt
, ap
);
69 fprintf (stderr
, "gdbserver/tracepoint: %s\n", buf
);
73 #define trace_debug_1(level, fmt, args...) \
75 if (level <= debug_threads) \
76 trace_vdebug ((fmt), ##args); \
79 #define trace_debug(FMT, args...) \
80 trace_debug_1 (1, FMT, ##args)
83 # define ATTR_USED __attribute__((used))
84 # define ATTR_NOINLINE __attribute__((noinline))
85 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
88 # define ATTR_NOINLINE
89 # define ATTR_CONSTRUCTOR
92 /* Make sure the functions the IPA needs to export (symbols GDBserver
93 needs to query GDB about) are exported. */
95 #ifdef IN_PROCESS_AGENT
96 # if defined _WIN32 || defined __CYGWIN__
97 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
100 # define IP_AGENT_EXPORT \
101 __attribute__ ((visibility("default"))) ATTR_USED
103 # define IP_AGENT_EXPORT ATTR_USED
107 # define IP_AGENT_EXPORT
110 /* Prefix exported symbols, for good citizenship. All the symbols
111 that need exporting are defined in this module. */
112 #ifdef IN_PROCESS_AGENT
113 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
114 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
115 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
116 # define collecting gdb_agent_collecting
117 # define gdb_collect gdb_agent_gdb_collect
118 # define stop_tracing gdb_agent_stop_tracing
119 # define flush_trace_buffer gdb_agent_flush_trace_buffer
120 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
121 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
122 # define stopping_tracepoint gdb_agent_stopping_tracepoint
123 # define expr_eval_result gdb_agent_expr_eval_result
124 # define error_tracepoint gdb_agent_error_tracepoint
125 # define tracepoints gdb_agent_tracepoints
126 # define tracing gdb_agent_tracing
127 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
128 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
129 # define trace_buffer_lo gdb_agent_trace_buffer_lo
130 # define trace_buffer_hi gdb_agent_trace_buffer_hi
131 # define traceframe_read_count gdb_agent_traceframe_read_count
132 # define traceframe_write_count gdb_agent_traceframe_write_count
133 # define traceframes_created gdb_agent_traceframes_created
134 # define trace_state_variables gdb_agent_trace_state_variables
135 # define get_raw_reg gdb_agent_get_raw_reg
136 # define get_trace_state_variable_value gdb_agent_get_trace_state_variable_value
137 # define set_trace_state_variable_value gdb_agent_set_trace_state_variable_value
138 # define ust_loaded gdb_agent_ust_loaded
139 # define helper_thread_id gdb_agent_helper_thread_id
140 # define cmd_buf gdb_agent_cmd_buf
143 #ifndef IN_PROCESS_AGENT
145 /* Addresses of in-process agent's symbols GDBserver cares about. */
147 struct ipa_sym_addresses
149 CORE_ADDR addr_gdb_tp_heap_buffer
;
150 CORE_ADDR addr_gdb_jump_pad_buffer
;
151 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
152 CORE_ADDR addr_collecting
;
153 CORE_ADDR addr_gdb_collect
;
154 CORE_ADDR addr_stop_tracing
;
155 CORE_ADDR addr_flush_trace_buffer
;
156 CORE_ADDR addr_about_to_request_buffer_space
;
157 CORE_ADDR addr_trace_buffer_is_full
;
158 CORE_ADDR addr_stopping_tracepoint
;
159 CORE_ADDR addr_expr_eval_result
;
160 CORE_ADDR addr_error_tracepoint
;
161 CORE_ADDR addr_tracepoints
;
162 CORE_ADDR addr_tracing
;
163 CORE_ADDR addr_trace_buffer_ctrl
;
164 CORE_ADDR addr_trace_buffer_ctrl_curr
;
165 CORE_ADDR addr_trace_buffer_lo
;
166 CORE_ADDR addr_trace_buffer_hi
;
167 CORE_ADDR addr_traceframe_read_count
;
168 CORE_ADDR addr_traceframe_write_count
;
169 CORE_ADDR addr_traceframes_created
;
170 CORE_ADDR addr_trace_state_variables
;
171 CORE_ADDR addr_get_raw_reg
;
172 CORE_ADDR addr_get_trace_state_variable_value
;
173 CORE_ADDR addr_set_trace_state_variable_value
;
174 CORE_ADDR addr_ust_loaded
;
175 CORE_ADDR addr_helper_thread_id
;
176 CORE_ADDR addr_cmd_buf
;
179 #define STRINGIZE_1(STR) #STR
180 #define STRINGIZE(STR) STRINGIZE_1(STR)
181 #define IPA_SYM(SYM) \
183 STRINGIZE (gdb_agent_ ## SYM), \
184 offsetof (struct ipa_sym_addresses, addr_ ## SYM) \
193 IPA_SYM(gdb_tp_heap_buffer
),
194 IPA_SYM(gdb_jump_pad_buffer
),
195 IPA_SYM(gdb_jump_pad_buffer_end
),
197 IPA_SYM(gdb_collect
),
198 IPA_SYM(stop_tracing
),
199 IPA_SYM(flush_trace_buffer
),
200 IPA_SYM(about_to_request_buffer_space
),
201 IPA_SYM(trace_buffer_is_full
),
202 IPA_SYM(stopping_tracepoint
),
203 IPA_SYM(expr_eval_result
),
204 IPA_SYM(error_tracepoint
),
205 IPA_SYM(tracepoints
),
207 IPA_SYM(trace_buffer_ctrl
),
208 IPA_SYM(trace_buffer_ctrl_curr
),
209 IPA_SYM(trace_buffer_lo
),
210 IPA_SYM(trace_buffer_hi
),
211 IPA_SYM(traceframe_read_count
),
212 IPA_SYM(traceframe_write_count
),
213 IPA_SYM(traceframes_created
),
214 IPA_SYM(trace_state_variables
),
215 IPA_SYM(get_raw_reg
),
216 IPA_SYM(get_trace_state_variable_value
),
217 IPA_SYM(set_trace_state_variable_value
),
219 IPA_SYM(helper_thread_id
),
223 struct ipa_sym_addresses ipa_sym_addrs
;
225 int all_tracepoint_symbols_looked_up
;
228 in_process_agent_loaded (void)
230 return all_tracepoint_symbols_looked_up
;
233 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
235 /* Returns true if both the in-process agent library and the static
236 tracepoints libraries are loaded in the inferior. */
239 in_process_agent_loaded_ust (void)
243 if (!in_process_agent_loaded ())
245 warning ("In-process agent not loaded");
249 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
251 warning ("Error reading ust_loaded in lib");
259 write_e_ipa_not_loaded (char *buffer
)
262 "E.In-process agent library not loaded in process. "
263 "Fast and static tracepoints unavailable.");
266 /* Write an error to BUFFER indicating that UST isn't loaded in the
270 write_e_ust_not_loaded (char *buffer
)
274 "E.UST library not loaded in process. "
275 "Static tracepoints unavailable.");
277 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
281 /* If the in-process agent library isn't loaded in the inferior, write
282 an error to BUFFER, and return 1. Otherwise, return 0. */
285 maybe_write_ipa_not_loaded (char *buffer
)
287 if (!in_process_agent_loaded ())
289 write_e_ipa_not_loaded (buffer
);
295 /* If the in-process agent library and the ust (static tracepoints)
296 library aren't loaded in the inferior, write an error to BUFFER,
297 and return 1. Otherwise, return 0. */
300 maybe_write_ipa_ust_not_loaded (char *buffer
)
302 if (!in_process_agent_loaded ())
304 write_e_ipa_not_loaded (buffer
);
307 else if (!in_process_agent_loaded_ust ())
309 write_e_ust_not_loaded (buffer
);
315 /* Cache all future symbols that the tracepoints module might request.
316 We can not request symbols at arbitrary states in the remote
317 protocol, only when the client tells us that new symbols are
318 available. So when we load the in-process library, make sure to
319 check the entire list. */
322 tracepoint_look_up_symbols (void)
327 if (all_tracepoint_symbols_looked_up
)
331 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
334 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
336 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
339 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
344 all_tracepoint_symbols_looked_up
= all_ok
;
349 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
350 of the "stop_tracing" function. When this breakpoint is hit,
351 tracing stopped in the IPA for some reason. E.g., due to
352 tracepoint reaching the pass count, hitting conditional expression
353 evaluation error, etc.
355 The IPA's trace buffer is never in circular tracing mode: instead,
356 GDBserver's is, and whenever the in-process buffer fills, it calls
357 "flush_trace_buffer", which triggers an internal breakpoint.
358 GDBserver reacts to this breakpoint by pulling the meanwhile
359 collected data. Old frames discarding is always handled on the
362 #ifdef IN_PROCESS_AGENT
363 int debug_threads
= 0;
366 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
368 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
372 /* Call this in the functions where GDBserver places a breakpoint, so
373 that the compiler doesn't try to be clever and skip calling the
374 function at all. This is necessary, even if we tell the compiler
375 to not inline said functions. */
377 #if defined(__GNUC__)
378 # define UNKNOWN_SIDE_EFFECTS() asm ("")
380 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
383 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
386 /* GDBserver places breakpoint here. */
387 UNKNOWN_SIDE_EFFECTS();
390 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
391 flush_trace_buffer (void)
393 /* GDBserver places breakpoint here. */
394 UNKNOWN_SIDE_EFFECTS();
399 #ifndef IN_PROCESS_AGENT
401 tracepoint_handler (CORE_ADDR address
)
403 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
408 /* Breakpoint at "stop_tracing" in the inferior lib. */
409 struct breakpoint
*stop_tracing_bkpt
;
410 static int stop_tracing_handler (CORE_ADDR
);
412 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
413 struct breakpoint
*flush_trace_buffer_bkpt
;
414 static int flush_trace_buffer_handler (CORE_ADDR
);
416 static void download_tracepoints (void);
417 static void download_trace_state_variables (void);
418 static void upload_fast_traceframes (void);
420 static int run_inferior_command (char *cmd
);
423 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
425 return read_inferior_memory (symaddr
, (unsigned char *) val
,
430 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
432 return read_inferior_memory (symaddr
, (unsigned char *) val
,
437 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
439 void *pval
= (void *) (uintptr_t) val
;
442 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
443 *val
= (uintptr_t) pval
;
448 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
450 void *pval
= (void *) (uintptr_t) val
;
451 return write_inferior_memory (symaddr
,
452 (unsigned char *) &pval
, sizeof (pval
));
456 write_inferior_integer (CORE_ADDR symaddr
, int val
)
458 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
462 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
464 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
469 /* This enum must exactly match what is documented in
470 gdb/doc/agentexpr.texi, including all the numerical values. */
474 gdb_agent_op_float
= 0x01,
475 gdb_agent_op_add
= 0x02,
476 gdb_agent_op_sub
= 0x03,
477 gdb_agent_op_mul
= 0x04,
478 gdb_agent_op_div_signed
= 0x05,
479 gdb_agent_op_div_unsigned
= 0x06,
480 gdb_agent_op_rem_signed
= 0x07,
481 gdb_agent_op_rem_unsigned
= 0x08,
482 gdb_agent_op_lsh
= 0x09,
483 gdb_agent_op_rsh_signed
= 0x0a,
484 gdb_agent_op_rsh_unsigned
= 0x0b,
485 gdb_agent_op_trace
= 0x0c,
486 gdb_agent_op_trace_quick
= 0x0d,
487 gdb_agent_op_log_not
= 0x0e,
488 gdb_agent_op_bit_and
= 0x0f,
489 gdb_agent_op_bit_or
= 0x10,
490 gdb_agent_op_bit_xor
= 0x11,
491 gdb_agent_op_bit_not
= 0x12,
492 gdb_agent_op_equal
= 0x13,
493 gdb_agent_op_less_signed
= 0x14,
494 gdb_agent_op_less_unsigned
= 0x15,
495 gdb_agent_op_ext
= 0x16,
496 gdb_agent_op_ref8
= 0x17,
497 gdb_agent_op_ref16
= 0x18,
498 gdb_agent_op_ref32
= 0x19,
499 gdb_agent_op_ref64
= 0x1a,
500 gdb_agent_op_ref_float
= 0x1b,
501 gdb_agent_op_ref_double
= 0x1c,
502 gdb_agent_op_ref_long_double
= 0x1d,
503 gdb_agent_op_l_to_d
= 0x1e,
504 gdb_agent_op_d_to_l
= 0x1f,
505 gdb_agent_op_if_goto
= 0x20,
506 gdb_agent_op_goto
= 0x21,
507 gdb_agent_op_const8
= 0x22,
508 gdb_agent_op_const16
= 0x23,
509 gdb_agent_op_const32
= 0x24,
510 gdb_agent_op_const64
= 0x25,
511 gdb_agent_op_reg
= 0x26,
512 gdb_agent_op_end
= 0x27,
513 gdb_agent_op_dup
= 0x28,
514 gdb_agent_op_pop
= 0x29,
515 gdb_agent_op_zero_ext
= 0x2a,
516 gdb_agent_op_swap
= 0x2b,
517 gdb_agent_op_getv
= 0x2c,
518 gdb_agent_op_setv
= 0x2d,
519 gdb_agent_op_tracev
= 0x2e,
520 gdb_agent_op_trace16
= 0x30,
524 static const char *gdb_agent_op_names
[gdb_agent_op_last
] =
581 unsigned char *bytes
;
584 /* Base action. Concrete actions inherit this. */
586 struct tracepoint_action
591 /* An 'M' (collect memory) action. */
592 struct collect_memory_action
594 struct tracepoint_action base
;
601 /* An 'R' (collect registers) action. */
603 struct collect_registers_action
605 struct tracepoint_action base
;
608 /* An 'X' (evaluate expression) action. */
610 struct eval_expr_action
612 struct tracepoint_action base
;
614 struct agent_expr
*expr
;
617 /* An 'L' (collect static trace data) action. */
618 struct collect_static_trace_data_action
620 struct tracepoint_action base
;
623 /* This structure describes a piece of the source-level definition of
624 the tracepoint. The contents are not interpreted by the target,
625 but preserved verbatim for uploading upon reconnection. */
629 /* The type of string, such as "cond" for a conditional. */
632 /* The source-level string itself. For the sake of target
633 debugging, we store it in plaintext, even though it is always
634 transmitted in hex. */
637 /* Link to the next one in the list. We link them in the order
638 received, in case some make up an ordered list of commands or
640 struct source_string
*next
;
645 /* Trap based tracepoint. */
648 /* A fast tracepoint implemented with a jump instead of a trap. */
651 /* A static tracepoint, implemented by a program call into a tracing
656 struct tracepoint_hit_ctx
;
658 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
661 /* The definition of a tracepoint. */
663 /* Tracepoints may have multiple locations, each at a different
664 address. This can occur with optimizations, template
665 instantiation, etc. Since the locations may be in different
666 scopes, the conditions and actions may be different for each
667 location. Our target version of tracepoints is more like GDB's
668 notion of "breakpoint locations", but we have almost nothing that
669 is not per-location, so we bother having two kinds of objects. The
670 key consequence is that numbers are not unique, and that it takes
671 both number and address to identify a tracepoint uniquely. */
675 /* The number of the tracepoint, as specified by GDB. Several
676 tracepoint objects here may share a number. */
679 /* Address at which the tracepoint is supposed to trigger. Several
680 tracepoints may share an address. */
683 /* Tracepoint type. */
684 enum tracepoint_type type
;
686 /* True if the tracepoint is currently enabled. */
689 /* The number of single steps that will be performed after each
693 /* The number of times the tracepoint may be hit before it will
694 terminate the entire tracing run. */
697 /* Pointer to the agent expression that is the tracepoint's
698 conditional, or NULL if the tracepoint is unconditional. */
699 struct agent_expr
*cond
;
701 /* The list of actions to take when the tracepoint triggers. */
703 struct tracepoint_action
**actions
;
705 /* Count of the times we've hit this tracepoint during the run.
706 Note that while-stepping steps are not counted as "hits". */
709 CORE_ADDR compiled_cond
;
711 /* Link to the next tracepoint in the list. */
712 struct tracepoint
*next
;
714 #ifndef IN_PROCESS_AGENT
715 /* The list of actions to take when the tracepoint triggers, in
716 string/packet form. */
719 /* The collection of strings that describe the tracepoint as it was
720 entered into GDB. These are not used by the target, but are
721 reported back to GDB upon reconnection. */
722 struct source_string
*source_strings
;
724 /* The number of bytes displaced by fast tracepoints. It may subsume
725 multiple instructions, for multi-byte fast tracepoints. This
726 field is only valid for fast tracepoints. */
729 /* Only for fast tracepoints. */
730 CORE_ADDR obj_addr_on_target
;
732 /* Address range where the original instruction under a fast
733 tracepoint was relocated to. (_end is actually one byte past
735 CORE_ADDR adjusted_insn_addr
;
736 CORE_ADDR adjusted_insn_addr_end
;
738 /* The address range of the piece of the jump pad buffer that was
739 assigned to this fast tracepoint. (_end is actually one byte
742 CORE_ADDR jump_pad_end
;
744 /* The list of actions to take while in a stepping loop. These
745 fields are only valid for patch-based tracepoints. */
746 int num_step_actions
;
747 struct tracepoint_action
**step_actions
;
748 /* Same, but in string/packet form. */
749 char **step_actions_str
;
751 /* Handle returned by the breakpoint or tracepoint module when we
752 inserted the trap or jump, or hooked into a static tracepoint.
753 NULL if we haven't inserted it yet. */
759 #ifndef IN_PROCESS_AGENT
761 /* Given `while-stepping', a thread may be collecting data for more
762 than one tracepoint simultaneously. On the other hand, the same
763 tracepoint with a while-stepping action may be hit by more than one
764 thread simultaneously (but not quite, each thread could be handling
765 a different step). Each thread holds a list of these objects,
766 representing the current step of each while-stepping action being
771 struct wstep_state
*next
;
773 /* The tracepoint number. */
775 /* The tracepoint's address. */
776 CORE_ADDR tp_address
;
778 /* The number of the current step in this 'while-stepping'
785 /* The linked list of all tracepoints. Marked explicitly as used as
786 the in-process library doesn't use it for the fast tracepoints
788 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
790 #ifndef IN_PROCESS_AGENT
792 /* Pointer to the last tracepoint in the list, new tracepoints are
793 linked in at the end. */
795 static struct tracepoint
*last_tracepoint
;
798 /* The first tracepoint to exceed its pass count. */
800 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
802 /* True if the trace buffer is full or otherwise no longer usable. */
804 IP_AGENT_EXPORT
int trace_buffer_is_full
;
806 /* Enumeration of the different kinds of things that can happen during
807 agent expression evaluation. */
809 enum eval_result_type
812 expr_eval_empty_expression
,
813 expr_eval_empty_stack
,
814 expr_eval_stack_overflow
,
815 expr_eval_stack_underflow
,
816 expr_eval_unhandled_opcode
,
817 expr_eval_unrecognized_opcode
,
818 expr_eval_divide_by_zero
,
819 expr_eval_invalid_goto
822 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
824 #ifndef IN_PROCESS_AGENT
826 static const char *eval_result_names
[] =
828 "terror:in the attic", /* this should never be reported */
829 "terror:empty expression",
830 "terror:empty stack",
831 "terror:stack overflow",
832 "terror:stack underflow",
833 "terror:unhandled opcode",
834 "terror:unrecognized opcode",
835 "terror:divide by zero"
840 /* The tracepoint in which the error occurred. */
842 static struct tracepoint
*error_tracepoint
;
844 struct trace_state_variable
846 /* This is the name of the variable as used in GDB. The target
847 doesn't use the name, but needs to have it for saving and
848 reconnection purposes. */
851 /* This number identifies the variable uniquely. Numbers may be
852 assigned either by the target (in the case of builtin variables),
853 or by GDB, and are presumed unique during the course of a trace
857 /* The variable's initial value, a 64-bit signed integer always. */
858 LONGEST initial_value
;
860 /* The variable's value, a 64-bit signed integer always. */
863 /* Pointer to a getter function, used to supply computed values. */
864 LONGEST (*getter
) (void);
866 /* Link to the next variable. */
867 struct trace_state_variable
*next
;
870 /* Linked list of all trace state variables. */
872 #ifdef IN_PROCESS_AGENT
873 struct trace_state_variable
*alloced_trace_state_variables
;
876 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
878 /* The results of tracing go into a fixed-size space known as the
879 "trace buffer". Because usage follows a limited number of
880 patterns, we manage it ourselves rather than with malloc. Basic
881 rules are that we create only one trace frame at a time, each is
882 variable in size, they are never moved once created, and we only
883 discard if we are doing a circular buffer, and then only the oldest
884 ones. Each trace frame includes its own size, so we don't need to
885 link them together, and the trace frame number is relative to the
886 first one, so we don't need to record numbers. A trace frame also
887 records the number of the tracepoint that created it. The data
888 itself is a series of blocks, each introduced by a single character
889 and with a defined format. Each type of block has enough
890 type/length info to allow scanners to jump quickly from one block
891 to the next without reading each byte in the block. */
893 /* Trace buffer management would be simple - advance a free pointer
894 from beginning to end, then stop - were it not for the circular
895 buffer option, which is a useful way to prevent a trace run from
896 stopping prematurely because the buffer filled up. In the circular
897 case, the location of the first trace frame (trace_buffer_start)
898 moves as old trace frames are discarded. Also, since we grow trace
899 frames incrementally as actions are performed, we wrap around to
900 the beginning of the trace buffer. This is per-block, so each
901 block within a trace frame remains contiguous. Things get messy
902 when the wrapped-around trace frame is the one being discarded; the
903 free space ends up in two parts at opposite ends of the buffer. */
906 # if defined(__GNUC__)
907 # define ATTR_PACKED __attribute__ ((packed))
909 # define ATTR_PACKED /* nothing */
913 /* The data collected at a tracepoint hit. This object should be as
914 small as possible, since there may be a great many of them. We do
915 not need to keep a frame number, because they are all sequential
916 and there are no deletions; so the Nth frame in the buffer is
917 always frame number N. */
921 /* Number of the tracepoint that collected this traceframe. A value
922 of 0 indicates the current end of the trace buffer. We make this
923 a 16-bit field because it's never going to happen that GDB's
924 numbering of tracepoints reaches 32,000. */
927 /* The size of the data in this trace frame. We limit this to 32
928 bits, even on a 64-bit target, because it's just implausible that
929 one is validly going to collect 4 gigabytes of data at a single
931 unsigned int data_size
: 32;
933 /* The base of the trace data, which is contiguous from this point. */
934 unsigned char data
[0];
938 /* The traceframe to be used as the source of data to send back to
939 GDB. A value of -1 means to get data from the live program. */
941 int current_traceframe
= -1;
943 /* This flag is true if the trace buffer is circular, meaning that
944 when it fills, the oldest trace frames are discarded in order to
947 #ifndef IN_PROCESS_AGENT
948 static int circular_trace_buffer
;
951 /* Pointer to the block of memory that traceframes all go into. */
953 static unsigned char *trace_buffer_lo
;
955 /* Pointer to the end of the trace buffer, more precisely to the byte
956 after the end of the buffer. */
958 static unsigned char *trace_buffer_hi
;
960 /* Control structure holding the read/write/etc. pointers into the
961 trace buffer. We need more than one of these to implement a
962 transaction-like mechanism to garantees that both GDBserver and the
963 in-process agent can try to change the trace buffer
966 struct trace_buffer_control
968 /* Pointer to the first trace frame in the buffer. In the
969 non-circular case, this is equal to trace_buffer_lo, otherwise it
970 moves around in the buffer. */
971 unsigned char *start
;
973 /* Pointer to the free part of the trace buffer. Note that we clear
974 several bytes at and after this pointer, so that traceframe
975 scans/searches terminate properly. */
978 /* Pointer to the byte after the end of the free part. Note that
979 this may be smaller than trace_buffer_free in the circular case,
980 and means that the free part is in two pieces. Initially it is
981 equal to trace_buffer_hi, then is generally equivalent to
982 trace_buffer_start. */
983 unsigned char *end_free
;
985 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
986 this is the point at which the trace data breaks, and resumes at
991 /* Same as above, to be used by GDBserver when updating the in-process
993 struct ipa_trace_buffer_control
1002 /* We have possibly both GDBserver and an inferior thread accessing
1003 the same IPA trace buffer memory. The IPA is the producer (tries
1004 to put new frames in the buffer), while GDBserver occasionally
1005 consumes them, that is, flushes the IPA's buffer into its own
1006 buffer. Both sides need to update the trace buffer control
1007 pointers (current head, tail, etc.). We can't use a global lock to
1008 synchronize the accesses, as otherwise we could deadlock GDBserver
1009 (if the thread holding the lock stops for a signal, say). So
1010 instead of that, we use a transaction scheme where GDBserver writes
1011 always prevail over the IPAs writes, and, we have the IPA detect
1012 the commit failure/overwrite, and retry the whole attempt. This is
1013 mainly implemented by having a global token object that represents
1014 who wrote last to the buffer control structure. We need to freeze
1015 any inferior writing to the buffer while GDBserver touches memory,
1016 so that the inferior can correctly detect that GDBserver had been
1017 there, otherwise, it could mistakingly think its commit was
1018 successful; that's implemented by simply having GDBserver set a
1019 breakpoint the inferior hits if it is the critical region.
1021 There are three cycling trace buffer control structure copies
1022 (buffer head, tail, etc.), with the token object including an index
1023 indicating which is current live copy. The IPA tentatively builds
1024 an updated copy in a non-current control structure, while GDBserver
1025 always clobbers the current version directly. The IPA then tries
1026 to atomically "commit" its version; if GDBserver clobbered the
1027 structure meanwhile, that will fail, and the IPA restarts the
1030 Listing the step in further detail, we have:
1032 In-process agent (producer):
1034 - passes by `about_to_request_buffer_space' breakpoint/lock
1036 - reads current token, extracts current trace buffer control index,
1037 and starts tentatively updating the rightmost one (0->1, 1->2,
1038 2->0). Note that only one inferior thread is executing this code
1039 at any given time, due to an outer lock in the jump pads.
1041 - updates counters, and tries to commit the token.
1043 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1044 leaving the sync region.
1046 - checks if the update was effective.
1048 - if trace buffer was found full, hits flush_trace_buffer
1049 breakpoint, and restarts later afterwards.
1051 GDBserver (consumer):
1053 - sets `about_to_request_buffer_space' breakpoint/lock.
1055 - updates the token unconditionally, using the current buffer
1056 control index, since it knows that the IP agent always writes to
1057 the rightmost, and due to the breakpoint, at most one IP thread
1058 can try to update the trace buffer concurrently to GDBserver, so
1059 there will be no danger of trace buffer control index wrap making
1060 the IPA write to the same index as GDBserver.
1062 - flushes the IP agent's trace buffer completely, and updates the
1063 current trace buffer control structure. GDBserver *always* wins.
1065 - removes the `about_to_request_buffer_space' breakpoint.
1067 The token is stored in the `trace_buffer_ctrl_curr' variable.
1068 Internally, it's bits are defined as:
1070 |-------------+-----+-------------+--------+-------------+--------------|
1071 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1072 |-------------+-----+-------------+--------+-------------+--------------|
1073 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1074 |-------------+-----+-------------+--------+-------------+--------------|
1076 GSB - GDBserver Stamp Bit
1077 PC - Previous Counter
1078 CC - Current Counter
1079 TBCI - Trace Buffer Control Index
1082 An IPA update of `trace_buffer_ctrl_curr' does:
1084 - read CC from the current token, save as PC.
1086 - atomically tries to write PC+1,CC
1088 A GDBserver update of `trace_buffer_ctrl_curr' does:
1090 - reads PC and CC from the current token.
1095 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1096 for the counters described below. The cleared bits are used to
1097 hold the index of the items of the `trace_buffer_ctrl' array that
1099 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1101 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1102 counter, and the `current' counter. */
1104 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1105 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1107 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1108 always stamps this bit as set. */
1109 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1111 #ifdef IN_PROCESS_AGENT
1112 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1113 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1115 # define TRACE_BUFFER_CTRL_CURR \
1116 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1120 /* The GDBserver side agent only needs one instance of this object, as
1121 it doesn't need to sync with itself. Define it as array anyway so
1122 that the rest of the code base doesn't need to care for the
1124 struct trace_buffer_control trace_buffer_ctrl
[1];
1125 # define TRACE_BUFFER_CTRL_CURR 0
1128 /* These are convenience macros used to access the current trace
1129 buffer control in effect. */
1130 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1131 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1132 #define trace_buffer_end_free \
1133 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1134 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1137 /* Macro that returns a pointer to the first traceframe in the buffer. */
1139 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1141 /* Macro that returns a pointer to the next traceframe in the buffer.
1142 If the computed location is beyond the wraparound point, subtract
1143 the offset of the wraparound. */
1145 #define NEXT_TRACEFRAME_1(TF) \
1146 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1148 #define NEXT_TRACEFRAME(TF) \
1149 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1150 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1151 ? (trace_buffer_wrap - trace_buffer_lo) \
1154 /* The difference between these counters represents the total number
1155 of complete traceframes present in the trace buffer. The IP agent
1156 writes to the write count, GDBserver writes to read count. */
1158 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1159 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1161 /* Convenience macro. */
1163 #define traceframe_count \
1164 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1166 /* The count of all traceframes created in the current run, including
1167 ones that were discarded to make room. */
1169 IP_AGENT_EXPORT
int traceframes_created
;
1171 #ifndef IN_PROCESS_AGENT
1173 /* Read-only regions are address ranges whose contents don't change,
1174 and so can be read from target memory even while looking at a trace
1175 frame. Without these, disassembly for instance will likely fail,
1176 because the program code is not usually collected into a trace
1177 frame. This data structure does not need to be very complicated or
1178 particularly efficient, it's only going to be used occasionally,
1179 and only by some commands. */
1181 struct readonly_region
1183 /* The bounds of the region. */
1184 CORE_ADDR start
, end
;
1186 /* Link to the next one. */
1187 struct readonly_region
*next
;
1190 /* Linked list of readonly regions. This list stays in effect from
1191 one tstart to the next. */
1193 static struct readonly_region
*readonly_regions
;
1197 /* The global that controls tracing overall. */
1199 IP_AGENT_EXPORT
int tracing
;
1201 #ifndef IN_PROCESS_AGENT
1203 /* Controls whether tracing should continue after GDB disconnects. */
1205 int disconnected_tracing
;
1207 /* The reason for the last tracing run to have stopped. We initialize
1208 to a distinct string so that GDB can distinguish between "stopped
1209 after running" and "stopped because never run" cases. */
1211 static const char *tracing_stop_reason
= "tnotrun";
1213 static int tracing_stop_tpnum
;
1217 /* Functions local to this file. */
1219 /* Base "class" for tracepoint type specific data to be passed down to
1220 collect_data_at_tracepoint. */
1221 struct tracepoint_hit_ctx
1223 enum tracepoint_type type
;
1226 #ifdef IN_PROCESS_AGENT
1228 /* Fast/jump tracepoint specific data to be passed down to
1229 collect_data_at_tracepoint. */
1230 struct fast_tracepoint_ctx
1232 struct tracepoint_hit_ctx base
;
1234 struct regcache regcache
;
1235 int regcache_initted
;
1236 unsigned char *regspace
;
1238 unsigned char *regs
;
1239 struct tracepoint
*tpoint
;
1242 /* Static tracepoint specific data to be passed down to
1243 collect_data_at_tracepoint. */
1244 struct static_tracepoint_ctx
1246 struct tracepoint_hit_ctx base
;
1248 /* The regcache corresponding to the registers state at the time of
1249 the tracepoint hit. Initialized lazily, from REGS. */
1250 struct regcache regcache
;
1251 int regcache_initted
;
1253 /* The buffer space REGCACHE above uses. We use a separate buffer
1254 instead of letting the regcache malloc for both signal safety and
1255 performance reasons; this is allocated on the stack instead. */
1256 unsigned char *regspace
;
1258 /* The register buffer as passed on by lttng/ust. */
1259 struct registers
*regs
;
1261 /* The "printf" formatter and the args the user passed to the marker
1262 call. We use this to be able to collect "static trace data"
1267 /* The GDB tracepoint matching the probed marker that was "hit". */
1268 struct tracepoint
*tpoint
;
1273 /* Static tracepoint specific data to be passed down to
1274 collect_data_at_tracepoint. */
1275 struct trap_tracepoint_ctx
1277 struct tracepoint_hit_ctx base
;
1279 struct regcache
*regcache
;
1284 #ifndef IN_PROCESS_AGENT
1285 static struct agent_expr
*parse_agent_expr (char **actparm
);
1286 static char *unparse_agent_expr (struct agent_expr
*aexpr
);
1288 static enum eval_result_type
eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1289 struct traceframe
*tframe
,
1290 struct agent_expr
*aexpr
,
1293 static int agent_mem_read (struct traceframe
*tframe
,
1294 unsigned char *to
, CORE_ADDR from
, ULONGEST len
);
1295 static int agent_tsv_read (struct traceframe
*tframe
, int n
);
1297 #ifndef IN_PROCESS_AGENT
1298 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1299 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1302 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1303 struct tracepoint
*tpoint
);
1305 #ifndef IN_PROCESS_AGENT
1306 static void clear_readonly_regions (void);
1307 static void clear_installed_tracepoints (void);
1310 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1312 struct tracepoint
*tpoint
);
1313 #ifndef IN_PROCESS_AGENT
1314 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1316 struct tracepoint
*tpoint
, int current_step
);
1317 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1318 CORE_ADDR
*jump_entry
);
1320 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1322 struct tracepoint
*tpoint
,
1323 struct traceframe
*tframe
,
1324 struct tracepoint_action
*taction
);
1326 #ifndef IN_PROCESS_AGENT
1327 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1330 #if defined(__GNUC__)
1331 # define memory_barrier() asm volatile ("" : : : "memory")
1333 # define memory_barrier() do {} while (0)
1336 /* We only build the IPA if this builtin is supported, and there are
1337 no uses of this in GDBserver itself, so we're safe in defining this
1339 #define cmpxchg(mem, oldval, newval) \
1340 __sync_val_compare_and_swap (mem, oldval, newval)
1342 /* The size in bytes of the buffer used to talk to the IPA helper
1344 #define CMD_BUF_SIZE 1024
1346 /* Record that an error occurred during expression evaluation. */
1349 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1350 enum eval_result_type rtype
)
1352 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1353 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1355 #ifdef IN_PROCESS_AGENT
1356 /* Only record the first error we get. */
1357 if (cmpxchg (&expr_eval_result
,
1359 rtype
) != expr_eval_no_error
)
1362 if (expr_eval_result
!= expr_eval_no_error
)
1366 error_tracepoint
= tpoint
;
1369 /* Trace buffer management. */
1372 clear_trace_buffer (void)
1374 trace_buffer_start
= trace_buffer_lo
;
1375 trace_buffer_free
= trace_buffer_lo
;
1376 trace_buffer_end_free
= trace_buffer_hi
;
1377 trace_buffer_wrap
= trace_buffer_hi
;
1378 /* A traceframe with zeroed fields marks the end of trace data. */
1379 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1380 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1381 traceframe_read_count
= traceframe_write_count
= 0;
1382 traceframes_created
= 0;
1385 #ifndef IN_PROCESS_AGENT
1388 clear_inferior_trace_buffer (void)
1390 CORE_ADDR ipa_trace_buffer_lo
;
1391 CORE_ADDR ipa_trace_buffer_hi
;
1392 struct traceframe ipa_traceframe
= { 0 };
1393 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1395 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1396 &ipa_trace_buffer_lo
);
1397 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1398 &ipa_trace_buffer_hi
);
1400 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1401 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1402 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1403 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1405 /* A traceframe with zeroed fields marks the end of trace data. */
1406 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1407 (unsigned char *) &ipa_trace_buffer_ctrl
,
1408 sizeof (ipa_trace_buffer_ctrl
));
1410 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1412 /* A traceframe with zeroed fields marks the end of trace data. */
1413 write_inferior_memory (ipa_trace_buffer_lo
,
1414 (unsigned char *) &ipa_traceframe
,
1415 sizeof (ipa_traceframe
));
1417 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1418 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1419 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1425 init_trace_buffer (unsigned char *buf
, int bufsize
)
1427 trace_buffer_lo
= buf
;
1428 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1430 clear_trace_buffer ();
1433 #ifdef IN_PROCESS_AGENT
1435 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1436 about_to_request_buffer_space (void)
1438 /* GDBserver places breakpoint here while it goes about to flush
1439 data at random times. */
1440 UNKNOWN_SIDE_EFFECTS();
1445 /* Carve out a piece of the trace buffer, returning NULL in case of
1449 trace_buffer_alloc (size_t amt
)
1451 unsigned char *rslt
;
1452 struct trace_buffer_control
*tbctrl
;
1454 #ifdef IN_PROCESS_AGENT
1455 unsigned int prev
, prev_filtered
;
1456 unsigned int commit_count
;
1457 unsigned int commit
;
1458 unsigned int readout
;
1460 struct traceframe
*oldest
;
1461 unsigned char *new_start
;
1464 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1465 (long) amt
, (long) sizeof (struct traceframe
));
1467 /* Account for the EOB marker. */
1468 amt
+= sizeof (struct traceframe
);
1470 #ifdef IN_PROCESS_AGENT
1474 /* Read the current token and extract the index to try to write to,
1475 storing it in CURR. */
1476 prev
= trace_buffer_ctrl_curr
;
1477 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1478 curr
= prev_filtered
+ 1;
1482 about_to_request_buffer_space ();
1484 /* Start out with a copy of the current state. GDBserver may be
1485 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1486 be able to commit anyway if that happens. */
1487 trace_buffer_ctrl
[curr
]
1488 = trace_buffer_ctrl
[prev_filtered
];
1489 trace_debug ("trying curr=%u", curr
);
1491 /* The GDBserver's agent doesn't need all that syncing, and always
1492 updates TCB 0 (there's only one, mind you). */
1495 tbctrl
= &trace_buffer_ctrl
[curr
];
1497 /* Offsets are easier to grok for debugging than raw addresses,
1498 especially for the small trace buffer sizes that are useful for
1500 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1502 (int) (tbctrl
->start
- trace_buffer_lo
),
1503 (int) (tbctrl
->free
- trace_buffer_lo
),
1504 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1505 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1506 (int) (trace_buffer_hi
- trace_buffer_lo
));
1508 /* The algorithm here is to keep trying to get a contiguous block of
1509 the requested size, possibly discarding older traceframes to free
1510 up space. Since free space might come in one or two pieces,
1511 depending on whether discarded traceframes wrapped around at the
1512 high end of the buffer, we test both pieces after each
1516 /* First, if we have two free parts, try the upper one first. */
1517 if (tbctrl
->end_free
< tbctrl
->free
)
1519 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1520 /* We have enough in the upper part. */
1524 /* Our high part of free space wasn't enough. Give up
1525 on it for now, set wraparound. We will recover the
1526 space later, if/when the wrapped-around traceframe is
1528 trace_debug ("Upper part too small, setting wraparound");
1529 tbctrl
->wrap
= tbctrl
->free
;
1530 tbctrl
->free
= trace_buffer_lo
;
1534 /* The normal case. */
1535 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1538 #ifdef IN_PROCESS_AGENT
1539 /* The IP Agent's buffer is always circular. It isn't used
1540 currently, but `circular_trace_buffer' could represent
1541 GDBserver's mode. If we didn't find space, ask GDBserver to
1544 flush_trace_buffer ();
1548 trace_debug ("gdbserver flushed buffer, retrying");
1552 /* GDBserver cancelled the tracing. Bail out as well. */
1555 /* If we're here, then neither part is big enough, and
1556 non-circular trace buffers are now full. */
1557 if (!circular_trace_buffer
)
1559 trace_debug ("Not enough space in the trace buffer");
1563 trace_debug ("Need more space in the trace buffer");
1565 /* If we have a circular buffer, we can try discarding the
1566 oldest traceframe and see if that helps. */
1567 oldest
= FIRST_TRACEFRAME ();
1568 if (oldest
->tpnum
== 0)
1570 /* Not good; we have no traceframes to free. Perhaps we're
1571 asking for a block that is larger than the buffer? In
1572 any case, give up. */
1573 trace_debug ("No traceframes to discard");
1577 /* We don't run this code in the in-process agent currently.
1578 E.g., we could leave the in-process agent in autonomous
1579 circular mode if we only have fast tracepoints. If we do
1580 that, then this bit becomes racy with GDBserver, which also
1581 writes to this counter. */
1582 --traceframe_write_count
;
1584 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1585 /* If we freed the traceframe that wrapped around, go back
1586 to the non-wrap case. */
1587 if (new_start
< tbctrl
->start
)
1589 trace_debug ("Discarding past the wraparound");
1590 tbctrl
->wrap
= trace_buffer_hi
;
1592 tbctrl
->start
= new_start
;
1593 tbctrl
->end_free
= tbctrl
->start
;
1595 trace_debug ("Discarded a traceframe\n"
1596 "Trace buffer [%d], start=%d free=%d "
1597 "endfree=%d wrap=%d hi=%d",
1599 (int) (tbctrl
->start
- trace_buffer_lo
),
1600 (int) (tbctrl
->free
- trace_buffer_lo
),
1601 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1602 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1603 (int) (trace_buffer_hi
- trace_buffer_lo
));
1605 /* Now go back around the loop. The discard might have resulted
1606 in either one or two pieces of free space, so we want to try
1607 both before freeing any more traceframes. */
1611 /* If we get here, we know we can provide the asked-for space. */
1613 rslt
= tbctrl
->free
;
1615 /* Adjust the request back down, now that we know we have space for
1616 the marker, but don't commit to AMT yet, we may still need to
1617 restart the operation if GDBserver touches the trace buffer
1618 (obviously only important in the in-process agent's version). */
1619 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1621 /* Or not. If GDBserver changed the trace buffer behind our back,
1622 we get to restart a new allocation attempt. */
1624 #ifdef IN_PROCESS_AGENT
1625 /* Build the tentative token. */
1626 commit_count
= (((prev
& 0x0007ff00) + 0x100) & 0x0007ff00);
1627 commit
= (((prev
& 0x0007ff00) << 12)
1631 /* Try to commit it. */
1632 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1633 if (readout
!= prev
)
1635 trace_debug ("GDBserver has touched the trace buffer, restarting."
1636 " (prev=%08x, commit=%08x, readout=%08x)",
1637 prev
, commit
, readout
);
1641 /* Hold your horses here. Even if that change was committed,
1642 GDBserver could come in, and clobber it. We need to hold to be
1643 able to tell if GDBserver clobbers before or after we committed
1644 the change. Whenever GDBserver goes about touching the IPA
1645 buffer, it sets a breakpoint in this routine, so we have a sync
1647 about_to_request_buffer_space ();
1649 /* Check if the change has been effective, even if GDBserver stopped
1650 us at the breakpoint. */
1653 unsigned int refetch
;
1657 refetch
= trace_buffer_ctrl_curr
;
1659 if ((refetch
== commit
1660 || ((refetch
& 0x7ff00000) >> 12) == commit_count
))
1663 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1664 "readout=%08x, refetch=%08x)",
1665 prev
, commit
, readout
, refetch
);
1669 trace_debug ("GDBserver has touched the trace buffer, not effective."
1670 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1671 prev
, commit
, readout
, refetch
);
1677 /* We have a new piece of the trace buffer. Hurray! */
1679 /* Add an EOB marker just past this allocation. */
1680 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1681 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1683 /* Adjust the request back down, now that we know we have space for
1685 amt
-= sizeof (struct traceframe
);
1689 trace_debug ("Allocated %d bytes", (int) amt
);
1690 trace_debug ("Trace buffer [%d] start=%d free=%d "
1691 "endfree=%d wrap=%d hi=%d",
1693 (int) (tbctrl
->start
- trace_buffer_lo
),
1694 (int) (tbctrl
->free
- trace_buffer_lo
),
1695 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1696 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1697 (int) (trace_buffer_hi
- trace_buffer_lo
));
1703 #ifndef IN_PROCESS_AGENT
1705 /* Return the total free space. This is not necessarily the largest
1706 block we can allocate, because of the two-part case. */
1711 if (trace_buffer_free
<= trace_buffer_end_free
)
1712 return trace_buffer_end_free
- trace_buffer_free
;
1714 return ((trace_buffer_end_free
- trace_buffer_lo
)
1715 + (trace_buffer_hi
- trace_buffer_free
));
1718 /* An 'S' in continuation packets indicates remainder are for
1721 static int seen_step_action_flag
;
1723 /* Create a tracepoint (location) with given number and address. */
1725 static struct tracepoint
*
1726 add_tracepoint (int num
, CORE_ADDR addr
)
1728 struct tracepoint
*tpoint
;
1730 tpoint
= xmalloc (sizeof (struct tracepoint
));
1731 tpoint
->number
= num
;
1732 tpoint
->address
= addr
;
1733 tpoint
->numactions
= 0;
1734 tpoint
->actions
= NULL
;
1735 tpoint
->actions_str
= NULL
;
1736 tpoint
->cond
= NULL
;
1737 tpoint
->num_step_actions
= 0;
1738 tpoint
->step_actions
= NULL
;
1739 tpoint
->step_actions_str
= NULL
;
1740 /* Start all off as regular (slow) tracepoints. */
1741 tpoint
->type
= trap_tracepoint
;
1742 tpoint
->orig_size
= -1;
1743 tpoint
->source_strings
= NULL
;
1744 tpoint
->compiled_cond
= 0;
1745 tpoint
->handle
= NULL
;
1746 tpoint
->next
= NULL
;
1748 if (!last_tracepoint
)
1749 tracepoints
= tpoint
;
1751 last_tracepoint
->next
= tpoint
;
1752 last_tracepoint
= tpoint
;
1754 seen_step_action_flag
= 0;
1759 #ifndef IN_PROCESS_AGENT
1761 /* Return the tracepoint with the given number and address, or NULL. */
1763 static struct tracepoint
*
1764 find_tracepoint (int id
, CORE_ADDR addr
)
1766 struct tracepoint
*tpoint
;
1768 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1769 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1775 /* There may be several tracepoints with the same number (because they
1776 are "locations", in GDB parlance); return the next one after the
1777 given tracepoint, or search from the beginning of the list if the
1778 first argument is NULL. */
1780 static struct tracepoint
*
1781 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1783 struct tracepoint
*tpoint
;
1786 tpoint
= prev_tp
->next
;
1788 tpoint
= tracepoints
;
1789 for (; tpoint
; tpoint
= tpoint
->next
)
1790 if (tpoint
->number
== num
)
1799 save_string (const char *str
, size_t len
)
1803 s
= xmalloc (len
+ 1);
1804 memcpy (s
, str
, len
);
1810 /* Append another action to perform when the tracepoint triggers. */
1813 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1819 seen_step_action_flag
= 1;
1827 char *act_start
= act
;
1828 struct tracepoint_action
*action
= NULL
;
1834 struct collect_memory_action
*maction
;
1838 maction
= xmalloc (sizeof *maction
);
1839 maction
->base
.type
= *act
;
1840 action
= &maction
->base
;
1843 is_neg
= (*act
== '-');
1846 act
= unpack_varlen_hex (act
, &basereg
);
1848 act
= unpack_varlen_hex (act
, &maction
->addr
);
1850 act
= unpack_varlen_hex (act
, &maction
->len
);
1851 maction
->basereg
= (is_neg
1854 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1855 pulongest (maction
->len
),
1856 paddress (maction
->addr
), maction
->basereg
);
1861 struct collect_registers_action
*raction
;
1863 raction
= xmalloc (sizeof *raction
);
1864 raction
->base
.type
= *act
;
1865 action
= &raction
->base
;
1867 trace_debug ("Want to collect registers");
1869 /* skip past hex digits of mask for now */
1870 while (isxdigit(*act
))
1876 struct collect_static_trace_data_action
*raction
;
1878 raction
= xmalloc (sizeof *raction
);
1879 raction
->base
.type
= *act
;
1880 action
= &raction
->base
;
1882 trace_debug ("Want to collect static trace data");
1887 trace_debug ("Unexpected step action, ignoring");
1892 struct eval_expr_action
*xaction
;
1894 xaction
= xmalloc (sizeof (*xaction
));
1895 xaction
->base
.type
= *act
;
1896 action
= &xaction
->base
;
1898 trace_debug ("Want to evaluate expression");
1899 xaction
->expr
= parse_agent_expr (&act
);
1903 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1912 if (seen_step_action_flag
)
1914 tpoint
->num_step_actions
++;
1916 tpoint
->step_actions
1917 = xrealloc (tpoint
->step_actions
,
1918 (sizeof (*tpoint
->step_actions
)
1919 * tpoint
->num_step_actions
));
1920 tpoint
->step_actions_str
1921 = xrealloc (tpoint
->step_actions_str
,
1922 (sizeof (*tpoint
->step_actions_str
)
1923 * tpoint
->num_step_actions
));
1924 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1925 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1926 = save_string (act_start
, act
- act_start
);
1930 tpoint
->numactions
++;
1932 = xrealloc (tpoint
->actions
,
1933 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1935 = xrealloc (tpoint
->actions_str
,
1936 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1937 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1938 tpoint
->actions_str
[tpoint
->numactions
- 1]
1939 = save_string (act_start
, act
- act_start
);
1946 /* Find or create a trace state variable with the given number. */
1948 static struct trace_state_variable
*
1949 get_trace_state_variable (int num
)
1951 struct trace_state_variable
*tsv
;
1953 #ifdef IN_PROCESS_AGENT
1954 /* Search for an existing variable. */
1955 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1956 if (tsv
->number
== num
)
1960 /* Search for an existing variable. */
1961 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1962 if (tsv
->number
== num
)
1968 /* Find or create a trace state variable with the given number. */
1970 static struct trace_state_variable
*
1971 create_trace_state_variable (int num
, int gdb
)
1973 struct trace_state_variable
*tsv
;
1975 tsv
= get_trace_state_variable (num
);
1979 /* Create a new variable. */
1980 tsv
= xmalloc (sizeof (struct trace_state_variable
));
1982 tsv
->initial_value
= 0;
1986 #ifdef IN_PROCESS_AGENT
1989 tsv
->next
= alloced_trace_state_variables
;
1990 alloced_trace_state_variables
= tsv
;
1995 tsv
->next
= trace_state_variables
;
1996 trace_state_variables
= tsv
;
2001 IP_AGENT_EXPORT LONGEST
2002 get_trace_state_variable_value (int num
)
2004 struct trace_state_variable
*tsv
;
2006 tsv
= get_trace_state_variable (num
);
2010 trace_debug ("No trace state variable %d, skipping value get", num
);
2014 /* Call a getter function if we have one. While it's tempting to
2015 set up something to only call the getter once per tracepoint hit,
2016 it could run afoul of thread races. Better to let the getter
2017 handle it directly, if necessary to worry about it. */
2019 tsv
->value
= (tsv
->getter
) ();
2021 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2022 num
, plongest (tsv
->value
));
2027 IP_AGENT_EXPORT
void
2028 set_trace_state_variable_value (int num
, LONGEST val
)
2030 struct trace_state_variable
*tsv
;
2032 tsv
= get_trace_state_variable (num
);
2036 trace_debug ("No trace state variable %d, skipping value set", num
);
2044 set_trace_state_variable_name (int num
, const char *name
)
2046 struct trace_state_variable
*tsv
;
2048 tsv
= get_trace_state_variable (num
);
2052 trace_debug ("No trace state variable %d, skipping name set", num
);
2056 tsv
->name
= (char *) name
;
2060 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2062 struct trace_state_variable
*tsv
;
2064 tsv
= get_trace_state_variable (num
);
2068 trace_debug ("No trace state variable %d, skipping getter set", num
);
2072 tsv
->getter
= getter
;
2075 /* Add a raw traceframe for the given tracepoint. */
2077 static struct traceframe
*
2078 add_traceframe (struct tracepoint
*tpoint
)
2080 struct traceframe
*tframe
;
2082 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2087 tframe
->tpnum
= tpoint
->number
;
2088 tframe
->data_size
= 0;
2093 /* Add a block to the traceframe currently being worked on. */
2095 static unsigned char *
2096 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2098 unsigned char *block
;
2103 block
= trace_buffer_alloc (amt
);
2108 tframe
->data_size
+= amt
;
2113 /* Flag that the current traceframe is finished. */
2116 finish_traceframe (struct traceframe
*tframe
)
2118 ++traceframe_write_count
;
2119 ++traceframes_created
;
2122 #ifndef IN_PROCESS_AGENT
2124 /* Given a traceframe number NUM, find the NUMth traceframe in the
2127 static struct traceframe
*
2128 find_traceframe (int num
)
2130 struct traceframe
*tframe
;
2133 for (tframe
= FIRST_TRACEFRAME ();
2135 tframe
= NEXT_TRACEFRAME (tframe
))
2146 get_traceframe_address (struct traceframe
*tframe
)
2149 struct tracepoint
*tpoint
;
2151 addr
= traceframe_get_pc (tframe
);
2156 /* Fallback strategy, will be incorrect for while-stepping frames
2157 and multi-location tracepoints. */
2158 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2159 return tpoint
->address
;
2162 /* Search for the next traceframe whose address is inside or outside
2165 static struct traceframe
*
2166 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2169 struct traceframe
*tframe
;
2172 *tfnump
= current_traceframe
+ 1;
2173 tframe
= find_traceframe (*tfnump
);
2174 /* The search is not supposed to wrap around. */
2181 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2183 tfaddr
= get_traceframe_address (tframe
);
2185 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2186 : (lo
> tfaddr
|| tfaddr
> hi
))
2195 /* Search for the next traceframe recorded by the given tracepoint.
2196 Note that for multi-location tracepoints, this will find whatever
2197 location appears first. */
2199 static struct traceframe
*
2200 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2202 struct traceframe
*tframe
;
2204 *tfnump
= current_traceframe
+ 1;
2205 tframe
= find_traceframe (*tfnump
);
2206 /* The search is not supposed to wrap around. */
2213 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2215 if (tframe
->tpnum
== num
)
2226 #ifndef IN_PROCESS_AGENT
2228 /* Clear all past trace state. */
2231 cmd_qtinit (char *packet
)
2233 struct trace_state_variable
*tsv
, *prev
, *next
;
2235 /* Make sure we don't try to read from a trace frame. */
2236 current_traceframe
= -1;
2238 trace_debug ("Initializing the trace");
2240 clear_installed_tracepoints ();
2241 clear_readonly_regions ();
2244 last_tracepoint
= NULL
;
2246 /* Clear out any leftover trace state variables. Ones with target
2247 defined getters should be kept however. */
2249 tsv
= trace_state_variables
;
2252 trace_debug ("Looking at var %d", tsv
->number
);
2253 if (tsv
->getter
== NULL
)
2259 trace_state_variables
= next
;
2260 trace_debug ("Deleting var %d", tsv
->number
);
2271 clear_trace_buffer ();
2272 clear_inferior_trace_buffer ();
2277 /* Unprobe the UST marker at ADDRESS. */
2280 unprobe_marker_at (CORE_ADDR address
)
2282 char cmd
[CMD_BUF_SIZE
];
2284 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2285 run_inferior_command (cmd
);
2288 /* Restore the program to its pre-tracing state. This routine may be called
2289 in error situations, so it needs to be careful about only restoring
2290 from known-valid bits. */
2293 clear_installed_tracepoints (void)
2295 struct tracepoint
*tpoint
;
2296 struct tracepoint
*prev_stpoint
;
2299 cancel_breakpoints ();
2301 prev_stpoint
= NULL
;
2303 /* Restore any bytes overwritten by tracepoints. */
2304 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2306 if (!tpoint
->enabled
)
2309 /* Catch the case where we might try to remove a tracepoint that
2310 was never actually installed. */
2311 if (tpoint
->handle
== NULL
)
2313 trace_debug ("Tracepoint %d at 0x%s was "
2314 "never installed, nothing to clear",
2315 tpoint
->number
, paddress (tpoint
->address
));
2319 switch (tpoint
->type
)
2321 case trap_tracepoint
:
2322 delete_breakpoint (tpoint
->handle
);
2324 case fast_tracepoint
:
2325 delete_fast_tracepoint_jump (tpoint
->handle
);
2327 case static_tracepoint
:
2328 if (prev_stpoint
!= NULL
2329 && prev_stpoint
->address
== tpoint
->address
)
2330 /* Nothing to do. We already unprobed a tracepoint set at
2331 this marker address (and there can only be one probe
2336 unprobe_marker_at (tpoint
->address
);
2337 prev_stpoint
= tpoint
;
2342 tpoint
->handle
= NULL
;
2348 /* Parse a packet that defines a tracepoint. */
2351 cmd_qtdp (char *own_buf
)
2357 struct tracepoint
*tpoint
;
2359 char *packet
= own_buf
;
2361 packet
+= strlen ("QTDP:");
2363 /* A hyphen at the beginning marks a packet specifying actions for a
2364 tracepoint already supplied. */
2371 packet
= unpack_varlen_hex (packet
, &num
);
2372 ++packet
; /* skip a colon */
2373 packet
= unpack_varlen_hex (packet
, &addr
);
2374 ++packet
; /* skip a colon */
2376 /* See if we already have this tracepoint. */
2377 tpoint
= find_tracepoint (num
, addr
);
2381 /* Duplicate tracepoints are never allowed. */
2384 trace_debug ("Tracepoint error: tracepoint %d"
2385 " at 0x%s already exists",
2386 (int) num
, paddress (addr
));
2387 write_enn (own_buf
);
2391 tpoint
= add_tracepoint (num
, addr
);
2393 tpoint
->enabled
= (*packet
== 'E');
2394 ++packet
; /* skip 'E' */
2395 ++packet
; /* skip a colon */
2396 packet
= unpack_varlen_hex (packet
, &count
);
2397 tpoint
->step_count
= count
;
2398 ++packet
; /* skip a colon */
2399 packet
= unpack_varlen_hex (packet
, &count
);
2400 tpoint
->pass_count
= count
;
2401 /* See if we have any of the additional optional fields. */
2402 while (*packet
== ':')
2407 tpoint
->type
= fast_tracepoint
;
2409 packet
= unpack_varlen_hex (packet
, &count
);
2410 tpoint
->orig_size
= count
;
2412 else if (*packet
== 'S')
2414 tpoint
->type
= static_tracepoint
;
2417 else if (*packet
== 'X')
2419 actparm
= (char *) packet
;
2420 tpoint
->cond
= parse_agent_expr (&actparm
);
2423 else if (*packet
== '-')
2425 else if (*packet
== '\0')
2428 trace_debug ("Unknown optional tracepoint field");
2431 trace_debug ("Also has actions\n");
2433 trace_debug ("Defined %stracepoint %d at 0x%s, "
2434 "enabled %d step %ld pass %ld",
2435 tpoint
->type
== fast_tracepoint
? "fast "
2437 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2438 tpoint
->step_count
, tpoint
->pass_count
);
2441 add_tracepoint_action (tpoint
, packet
);
2444 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2445 (int) num
, paddress (addr
));
2446 write_enn (own_buf
);
2454 cmd_qtdpsrc (char *own_buf
)
2456 ULONGEST num
, addr
, start
, slen
;
2457 struct tracepoint
*tpoint
;
2458 char *packet
= own_buf
;
2459 char *saved
, *srctype
, *src
;
2461 struct source_string
*last
, *newlast
;
2463 packet
+= strlen ("QTDPsrc:");
2465 packet
= unpack_varlen_hex (packet
, &num
);
2466 ++packet
; /* skip a colon */
2467 packet
= unpack_varlen_hex (packet
, &addr
);
2468 ++packet
; /* skip a colon */
2470 /* See if we already have this tracepoint. */
2471 tpoint
= find_tracepoint (num
, addr
);
2475 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2476 (int) num
, paddress (addr
));
2477 write_enn (own_buf
);
2482 packet
= strchr (packet
, ':');
2483 srctype
= xmalloc (packet
- saved
+ 1);
2484 memcpy (srctype
, saved
, packet
- saved
);
2485 srctype
[packet
- saved
] = '\0';
2487 packet
= unpack_varlen_hex (packet
, &start
);
2488 ++packet
; /* skip a colon */
2489 packet
= unpack_varlen_hex (packet
, &slen
);
2490 ++packet
; /* skip a colon */
2491 src
= xmalloc (slen
+ 1);
2492 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2495 newlast
= xmalloc (sizeof (struct source_string
));
2496 newlast
->type
= srctype
;
2498 newlast
->next
= NULL
;
2499 /* Always add a source string to the end of the list;
2500 this keeps sequences of actions/commands in the right
2502 if (tpoint
->source_strings
)
2504 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2506 last
->next
= newlast
;
2509 tpoint
->source_strings
= newlast
;
2515 cmd_qtdv (char *own_buf
)
2517 ULONGEST num
, val
, builtin
;
2520 struct trace_state_variable
*tsv
;
2521 char *packet
= own_buf
;
2523 packet
+= strlen ("QTDV:");
2525 packet
= unpack_varlen_hex (packet
, &num
);
2526 ++packet
; /* skip a colon */
2527 packet
= unpack_varlen_hex (packet
, &val
);
2528 ++packet
; /* skip a colon */
2529 packet
= unpack_varlen_hex (packet
, &builtin
);
2530 ++packet
; /* skip a colon */
2532 nbytes
= strlen (packet
) / 2;
2533 varname
= xmalloc (nbytes
+ 1);
2534 nbytes
= unhexify (varname
, packet
, nbytes
);
2535 varname
[nbytes
] = '\0';
2537 tsv
= create_trace_state_variable (num
, 1);
2538 tsv
->initial_value
= (LONGEST
) val
;
2539 tsv
->name
= varname
;
2541 set_trace_state_variable_value (num
, (LONGEST
) val
);
2547 cmd_qtv (char *own_buf
)
2552 char *packet
= own_buf
;
2554 packet
+= strlen ("qTV:");
2555 packet
= unpack_varlen_hex (packet
, &num
);
2557 if (current_traceframe
>= 0)
2559 err
= traceframe_read_tsv ((int) num
, &val
);
2562 strcpy (own_buf
, "U");
2566 /* Only make tsv's be undefined before the first trace run. After a
2567 trace run is over, the user might want to see the last value of
2568 the tsv, and it might not be available in a traceframe. */
2569 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2571 strcpy (own_buf
, "U");
2575 val
= get_trace_state_variable_value (num
);
2577 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2580 /* Clear out the list of readonly regions. */
2583 clear_readonly_regions (void)
2585 struct readonly_region
*roreg
;
2587 while (readonly_regions
)
2589 roreg
= readonly_regions
;
2590 readonly_regions
= readonly_regions
->next
;
2595 /* Parse the collection of address ranges whose contents GDB believes
2596 to be unchanging and so can be read directly from target memory
2597 even while looking at a traceframe. */
2600 cmd_qtro (char *own_buf
)
2602 ULONGEST start
, end
;
2603 struct readonly_region
*roreg
;
2604 char *packet
= own_buf
;
2606 trace_debug ("Want to mark readonly regions");
2608 clear_readonly_regions ();
2610 packet
+= strlen ("QTro");
2612 while (*packet
== ':')
2614 ++packet
; /* skip a colon */
2615 packet
= unpack_varlen_hex (packet
, &start
);
2616 ++packet
; /* skip a comma */
2617 packet
= unpack_varlen_hex (packet
, &end
);
2618 roreg
= xmalloc (sizeof (struct readonly_region
));
2619 roreg
->start
= start
;
2621 roreg
->next
= readonly_regions
;
2622 readonly_regions
= roreg
;
2623 trace_debug ("Added readonly region from 0x%s to 0x%s",
2624 paddress (roreg
->start
), paddress (roreg
->end
));
2630 /* Test to see if the given range is in our list of readonly ranges.
2631 We only test for being entirely within a range, GDB is not going to
2632 send a single memory packet that spans multiple regions. */
2635 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2637 struct readonly_region
*roreg
;
2639 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2640 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2646 /* The maximum size of a jump pad entry. */
2647 static const int max_jump_pad_size
= 0x100;
2649 static CORE_ADDR gdb_jump_pad_head
;
2651 /* Return the address of the next free jump space. */
2654 get_jump_space_head (void)
2656 if (gdb_jump_pad_head
== 0)
2658 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2659 &gdb_jump_pad_head
))
2660 fatal ("error extracting jump_pad_buffer");
2663 return gdb_jump_pad_head
;
2666 /* Reserve USED bytes from the jump space. */
2669 claim_jump_space (ULONGEST used
)
2671 trace_debug ("claim_jump_space reserves %s bytes at %s",
2672 pulongest (used
), paddress (gdb_jump_pad_head
));
2673 gdb_jump_pad_head
+= used
;
2676 /* Sort tracepoints by PC, using a bubble sort. */
2679 sort_tracepoints (void)
2681 struct tracepoint
*lst
, *tmp
, *prev
= NULL
;
2684 if (tracepoints
== NULL
)
2688 for (tmp
= tracepoints
; tmp
->next
; tmp
= tmp
->next
)
2691 for (i
= 0; i
< n
- 1; i
++)
2692 for (j
= 0, lst
= tracepoints
;
2693 lst
&& lst
->next
&& (j
<= n
- 1 - i
);
2696 /* If we're at beginning, the start node is the prev
2701 /* Compare neighbors. */
2702 if (lst
->next
->address
< lst
->address
)
2704 struct tracepoint
*p
;
2707 tmp
= (lst
->next
? lst
->next
->next
: NULL
);
2709 if (j
== 0 && prev
== tracepoints
)
2710 tracepoints
= lst
->next
;
2713 prev
->next
= lst
->next
;
2714 lst
->next
->next
= lst
;
2721 /* Keep track of the previous node. We need it if we need
2729 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2730 the command fails, or 0 otherwise. If the command ran
2731 successfully, but probing the marker failed, ERROUT will be filled
2732 with the error to reply to GDB, and -1 is also returned. This
2733 allows directly passing IPA errors to GDB. */
2736 probe_marker_at (CORE_ADDR address
, char *errout
)
2738 char cmd
[CMD_BUF_SIZE
];
2741 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
2742 err
= run_inferior_command (cmd
);
2748 strcpy (errout
, cmd
);
2756 #define MAX_JUMP_SIZE 20
2759 cmd_qtstart (char *packet
)
2761 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
2762 int slow_tracepoint_count
, fast_count
;
2763 CORE_ADDR jump_entry
;
2765 /* The jump to the jump pad of the last fast tracepoint
2767 unsigned char fjump
[MAX_JUMP_SIZE
];
2768 ULONGEST fjump_size
;
2770 trace_debug ("Starting the trace");
2772 slow_tracepoint_count
= fast_count
= 0;
2774 /* Sort tracepoints by ascending address. This makes installing
2775 fast tracepoints at the same address easier to handle. */
2776 sort_tracepoints ();
2778 /* Pause all threads temporarily while we patch tracepoints. */
2781 /* Get threads out of jump pads. Safe to do here, since this is a
2782 top level command. And, required to do here, since we're
2783 deleting/rewriting jump pads. */
2785 stabilize_threads ();
2787 /* Freeze threads. */
2790 /* Sync the fast tracepoints list in the inferior ftlib. */
2791 if (in_process_agent_loaded ())
2793 download_tracepoints ();
2794 download_trace_state_variables ();
2797 /* No previous fast tpoint yet. */
2798 prev_ftpoint
= NULL
;
2800 /* No previous static tpoint yet. */
2801 prev_stpoint
= NULL
;
2805 /* Install tracepoints. */
2806 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2808 /* Ensure all the hit counts start at zero. */
2809 tpoint
->hit_count
= 0;
2811 if (!tpoint
->enabled
)
2814 if (tpoint
->type
== trap_tracepoint
)
2816 ++slow_tracepoint_count
;
2818 /* Tracepoints are installed as memory breakpoints. Just go
2819 ahead and install the trap. The breakpoints module
2820 handles duplicated breakpoints, and the memory read
2821 routine handles un-patching traps from memory reads. */
2822 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
2823 tracepoint_handler
);
2825 else if (tpoint
->type
== fast_tracepoint
)
2829 if (maybe_write_ipa_not_loaded (packet
))
2831 trace_debug ("Requested a fast tracepoint, but fast "
2832 "tracepoints aren't supported.");
2836 if (prev_ftpoint
!= NULL
&& prev_ftpoint
->address
== tpoint
->address
)
2838 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
,
2841 tpoint
->jump_pad
= prev_ftpoint
->jump_pad
;
2842 tpoint
->jump_pad_end
= prev_ftpoint
->jump_pad_end
;
2843 tpoint
->adjusted_insn_addr
= prev_ftpoint
->adjusted_insn_addr
;
2844 tpoint
->adjusted_insn_addr_end
2845 = prev_ftpoint
->adjusted_insn_addr_end
;
2852 prev_ftpoint
= NULL
;
2854 jentry
= jump_entry
= get_jump_space_head ();
2856 /* Install the jump pad. */
2857 err
= install_fast_tracepoint_jump_pad
2858 (tpoint
->obj_addr_on_target
,
2860 ipa_sym_addrs
.addr_gdb_collect
,
2861 ipa_sym_addrs
.addr_collecting
,
2865 &tpoint
->adjusted_insn_addr
,
2866 &tpoint
->adjusted_insn_addr_end
);
2870 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
,
2873 if (tpoint
->handle
!= NULL
)
2875 tpoint
->jump_pad
= jump_entry
;
2876 tpoint
->jump_pad_end
= jentry
;
2878 /* Pad to 8-byte alignment. */
2879 jentry
= ((jentry
+ 7) & ~0x7);
2880 claim_jump_space (jentry
- jump_entry
);
2882 /* So that we can handle multiple fast tracepoints
2883 at the same address easily. */
2884 prev_ftpoint
= tpoint
;
2888 else if (tpoint
->type
== static_tracepoint
)
2890 if (maybe_write_ipa_ust_not_loaded (packet
))
2892 trace_debug ("Requested a static tracepoint, but static "
2893 "tracepoints are not supported.");
2897 /* Can only probe a given marker once. */
2898 if (prev_stpoint
!= NULL
&& prev_stpoint
->address
== tpoint
->address
)
2900 tpoint
->handle
= (void *) -1;
2904 if (probe_marker_at (tpoint
->address
, packet
) == 0)
2906 tpoint
->handle
= (void *) -1;
2908 /* So that we can handle multiple static tracepoints
2909 at the same address easily. */
2910 prev_stpoint
= tpoint
;
2915 /* Any failure in the inner loop is sufficient cause to give
2917 if (tpoint
->handle
== NULL
)
2921 /* Any error in tracepoint insertion is unacceptable; better to
2922 address the problem now, than end up with a useless or misleading
2926 clear_installed_tracepoints ();
2927 if (*packet
== '\0')
2933 stopping_tracepoint
= NULL
;
2934 trace_buffer_is_full
= 0;
2935 expr_eval_result
= expr_eval_no_error
;
2936 error_tracepoint
= NULL
;
2938 /* Tracing is now active, hits will now start being logged. */
2941 if (in_process_agent_loaded ())
2943 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
2944 fatal ("Error setting tracing variable in lib");
2946 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
2948 fatal ("Error clearing stopping_tracepoint variable in lib");
2950 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
2951 fatal ("Error clearing trace_buffer_is_full variable in lib");
2953 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
2954 stop_tracing_handler
);
2955 if (stop_tracing_bkpt
== NULL
)
2956 error ("Error setting stop_tracing breakpoint");
2958 flush_trace_buffer_bkpt
2959 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
2960 flush_trace_buffer_handler
);
2961 if (flush_trace_buffer_bkpt
== NULL
)
2962 error ("Error setting flush_trace_buffer breakpoint");
2970 /* End a tracing run, filling in a stop reason to report back to GDB,
2971 and removing the tracepoints from the code. */
2978 trace_debug ("Tracing is already off, ignoring");
2982 trace_debug ("Stopping the trace");
2984 /* Pause all threads before removing fast jumps from memory,
2985 breakpoints, and touching IPA state variables (inferior memory).
2986 Some thread may hit the internal tracing breakpoints, or be
2987 collecting this moment, but that's ok, we don't release the
2988 tpoint object's memory or the jump pads here (we only do that
2989 when we're sure we can move all threads out of the jump pads).
2990 We can't now, since we may be getting here due to the inferior
2991 agent calling us. */
2993 /* Since we're removing breakpoints, cancel breakpoint hits,
2994 possibly related to the breakpoints we're about to delete. */
2995 cancel_breakpoints ();
2997 /* Stop logging. Tracepoints can still be hit, but they will not be
3000 if (in_process_agent_loaded ())
3002 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3003 fatal ("Error clearing tracing variable in lib");
3006 tracing_stop_reason
= "t???";
3007 tracing_stop_tpnum
= 0;
3008 if (stopping_tracepoint
)
3010 trace_debug ("Stopping the trace because "
3011 "tracepoint %d was hit %ld times",
3012 stopping_tracepoint
->number
,
3013 stopping_tracepoint
->pass_count
);
3014 tracing_stop_reason
= "tpasscount";
3015 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3017 else if (trace_buffer_is_full
)
3019 trace_debug ("Stopping the trace because the trace buffer is full");
3020 tracing_stop_reason
= "tfull";
3022 else if (expr_eval_result
!= expr_eval_no_error
)
3024 trace_debug ("Stopping the trace because of an expression eval error");
3025 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3026 tracing_stop_tpnum
= error_tracepoint
->number
;
3028 #ifndef IN_PROCESS_AGENT
3029 else if (!gdb_connected ())
3031 trace_debug ("Stopping the trace because GDB disconnected");
3032 tracing_stop_reason
= "tdisconnected";
3037 trace_debug ("Stopping the trace because of a tstop command");
3038 tracing_stop_reason
= "tstop";
3041 stopping_tracepoint
= NULL
;
3042 error_tracepoint
= NULL
;
3044 /* Clear out the tracepoints. */
3045 clear_installed_tracepoints ();
3047 if (in_process_agent_loaded ())
3049 /* Pull in fast tracepoint trace frames from the inferior lib
3050 buffer into our buffer, even if our buffer is already full,
3051 because we want to present the full number of created frames
3052 in addition to what fit in the trace buffer. */
3053 upload_fast_traceframes ();
3056 if (stop_tracing_bkpt
!= NULL
)
3058 delete_breakpoint (stop_tracing_bkpt
);
3059 stop_tracing_bkpt
= NULL
;
3062 if (flush_trace_buffer_bkpt
!= NULL
)
3064 delete_breakpoint (flush_trace_buffer_bkpt
);
3065 flush_trace_buffer_bkpt
= NULL
;
3072 stop_tracing_handler (CORE_ADDR addr
)
3074 trace_debug ("lib hit stop_tracing");
3076 /* Don't actually handle it here. When we stop tracing we remove
3077 breakpoints from the inferior, and that is not allowed in a
3078 breakpoint handler (as the caller is walking the breakpoint
3084 flush_trace_buffer_handler (CORE_ADDR addr
)
3086 trace_debug ("lib hit flush_trace_buffer");
3091 cmd_qtstop (char *packet
)
3098 cmd_qtdisconnected (char *own_buf
)
3101 char *packet
= own_buf
;
3103 packet
+= strlen ("QTDisconnected:");
3105 unpack_varlen_hex (packet
, &setting
);
3109 disconnected_tracing
= setting
;
3113 cmd_qtframe (char *own_buf
)
3115 ULONGEST frame
, pc
, lo
, hi
, num
;
3117 struct traceframe
*tframe
;
3118 char *packet
= own_buf
;
3120 packet
+= strlen ("QTFrame:");
3122 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3124 packet
+= strlen ("pc:");
3125 packet
= unpack_varlen_hex (packet
, &pc
);
3126 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3127 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3129 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3131 packet
+= strlen ("range:");
3132 packet
= unpack_varlen_hex (packet
, &lo
);
3134 packet
= unpack_varlen_hex (packet
, &hi
);
3135 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3136 paddress (lo
), paddress (hi
));
3137 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3139 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3141 packet
+= strlen ("outside:");
3142 packet
= unpack_varlen_hex (packet
, &lo
);
3144 packet
= unpack_varlen_hex (packet
, &hi
);
3145 trace_debug ("Want to find next traceframe "
3146 "outside the range 0x%s to 0x%s",
3147 paddress (lo
), paddress (hi
));
3148 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3150 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3152 packet
+= strlen ("tdp:");
3153 packet
= unpack_varlen_hex (packet
, &num
);
3155 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3156 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3160 unpack_varlen_hex (packet
, &frame
);
3161 tfnum
= (int) frame
;
3164 trace_debug ("Want to stop looking at traceframes");
3165 current_traceframe
= -1;
3169 trace_debug ("Want to look at traceframe %d", tfnum
);
3170 tframe
= find_traceframe (tfnum
);
3175 current_traceframe
= tfnum
;
3176 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3179 sprintf (own_buf
, "F-1");
3183 cmd_qtstatus (char *packet
)
3185 char *stop_reason_rsp
= NULL
;
3187 trace_debug ("Returning trace status as %d, stop reason %s",
3188 tracing
, tracing_stop_reason
);
3190 if (in_process_agent_loaded ())
3194 upload_fast_traceframes ();
3199 stop_reason_rsp
= (char *) tracing_stop_reason
;
3201 /* The user visible error string in terror needs to be hex encoded.
3202 We leave it as plain string in `tracepoint_stop_reason' to ease
3204 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3206 const char *result_name
;
3210 result_name
= stop_reason_rsp
+ strlen ("terror:");
3211 hexstr_len
= strlen (result_name
) * 2;
3212 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3213 strcpy (p
, "terror:");
3215 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3221 "tframes:%x;tcreated:%x;"
3222 "tfree:%x;tsize:%s;"
3226 stop_reason_rsp
, tracing_stop_tpnum
,
3227 traceframe_count
, traceframes_created
,
3228 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3229 circular_trace_buffer
,
3230 disconnected_tracing
);
3233 /* State variables to help return all the tracepoint bits. */
3234 static struct tracepoint
*cur_tpoint
;
3235 static int cur_action
;
3236 static int cur_step_action
;
3237 static struct source_string
*cur_source_string
;
3238 static struct trace_state_variable
*cur_tsv
;
3240 /* Compose a response that is an imitation of the syntax by which the
3241 tracepoint was originally downloaded. */
3244 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3248 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
3249 paddress (tpoint
->address
),
3250 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3251 tpoint
->pass_count
);
3252 if (tpoint
->type
== fast_tracepoint
)
3253 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3254 else if (tpoint
->type
== static_tracepoint
)
3255 sprintf (packet
+ strlen (packet
), ":S");
3259 buf
= unparse_agent_expr (tpoint
->cond
);
3260 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3261 tpoint
->cond
->length
, buf
);
3266 /* Compose a response that is an imitation of the syntax by which the
3267 tracepoint action was originally downloaded (with the difference
3268 that due to the way we store the actions, this will output a packet
3269 per action, while GDB could have combined more than one action
3273 response_action (char *packet
, struct tracepoint
*tpoint
,
3274 char *taction
, int step
)
3276 sprintf (packet
, "%c%x:%s:%s",
3277 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3281 /* Compose a response that is an imitation of the syntax by which the
3282 tracepoint source piece was originally downloaded. */
3285 response_source (char *packet
,
3286 struct tracepoint
*tpoint
, struct source_string
*src
)
3291 len
= strlen (src
->str
);
3292 buf
= alloca (len
* 2 + 1);
3293 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3295 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3296 tpoint
->number
, paddress (tpoint
->address
),
3297 src
->type
, 0, len
, buf
);
3300 /* Return the first piece of tracepoint definition, and initialize the
3301 state machine that will iterate through all the tracepoint
3305 cmd_qtfp (char *packet
)
3307 trace_debug ("Returning first tracepoint definition piece");
3309 cur_tpoint
= tracepoints
;
3310 cur_action
= cur_step_action
= -1;
3311 cur_source_string
= NULL
;
3314 response_tracepoint (packet
, cur_tpoint
);
3316 strcpy (packet
, "l");
3319 /* Return additional pieces of tracepoint definition. Each action and
3320 stepping action must go into its own packet, because of packet size
3321 limits, and so we use state variables to deliver one piece at a
3325 cmd_qtsp (char *packet
)
3327 trace_debug ("Returning subsequent tracepoint definition piece");
3331 /* This case would normally never occur, but be prepared for
3333 strcpy (packet
, "l");
3335 else if (cur_action
< cur_tpoint
->numactions
- 1)
3338 response_action (packet
, cur_tpoint
,
3339 cur_tpoint
->actions_str
[cur_action
], 0);
3341 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3344 response_action (packet
, cur_tpoint
,
3345 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3347 else if ((cur_source_string
3348 ? cur_source_string
->next
3349 : cur_tpoint
->source_strings
))
3351 if (cur_source_string
)
3352 cur_source_string
= cur_source_string
->next
;
3354 cur_source_string
= cur_tpoint
->source_strings
;
3355 response_source (packet
, cur_tpoint
, cur_source_string
);
3359 cur_tpoint
= cur_tpoint
->next
;
3360 cur_action
= cur_step_action
= -1;
3361 cur_source_string
= NULL
;
3363 response_tracepoint (packet
, cur_tpoint
);
3365 strcpy (packet
, "l");
3369 /* Compose a response that is an imitation of the syntax by which the
3370 trace state variable was originally downloaded. */
3373 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3375 char *buf
= (char *) "";
3380 namelen
= strlen (tsv
->name
);
3381 buf
= alloca (namelen
* 2 + 1);
3382 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3385 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3386 tsv
->getter
? 1 : 0, buf
);
3389 /* Return the first trace state variable definition, and initialize
3390 the state machine that will iterate through all the tsv bits. */
3393 cmd_qtfv (char *packet
)
3395 trace_debug ("Returning first trace state variable definition");
3397 cur_tsv
= trace_state_variables
;
3400 response_tsv (packet
, cur_tsv
);
3402 strcpy (packet
, "l");
3405 /* Return additional trace state variable definitions. */
3408 cmd_qtsv (char *packet
)
3410 trace_debug ("Returning first trace state variable definition");
3414 /* This case would normally never occur, but be prepared for
3416 strcpy (packet
, "l");
3420 cur_tsv
= cur_tsv
->next
;
3422 response_tsv (packet
, cur_tsv
);
3424 strcpy (packet
, "l");
3427 strcpy (packet
, "l");
3430 /* Return the first static tracepoint marker, and initialize the state
3431 machine that will iterate through all the static tracepoints
3435 cmd_qtfstm (char *packet
)
3437 if (!maybe_write_ipa_ust_not_loaded (packet
))
3438 run_inferior_command (packet
);
3441 /* Return additional static tracepoints markers. */
3444 cmd_qtsstm (char *packet
)
3446 if (!maybe_write_ipa_ust_not_loaded (packet
))
3447 run_inferior_command (packet
);
3450 /* Return the definition of the static tracepoint at a given address.
3451 Result packet is the same as qTsST's. */
3454 cmd_qtstmat (char *packet
)
3456 if (!maybe_write_ipa_ust_not_loaded (packet
))
3457 run_inferior_command (packet
);
3460 /* Respond to qTBuffer packet with a block of raw data from the trace
3461 buffer. GDB may ask for a lot, but we are allowed to reply with
3462 only as much as will fit within packet limits or whatever. */
3465 cmd_qtbuffer (char *own_buf
)
3467 ULONGEST offset
, num
, tot
;
3469 char *packet
= own_buf
;
3471 packet
+= strlen ("qTBuffer:");
3473 packet
= unpack_varlen_hex (packet
, &offset
);
3474 ++packet
; /* skip a comma */
3475 packet
= unpack_varlen_hex (packet
, &num
);
3477 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3478 (int) num
, pulongest (offset
));
3480 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3482 /* If we're right at the end, reply specially that we're done. */
3485 strcpy (own_buf
, "l");
3489 /* Object to any other out-of-bounds request. */
3492 write_enn (own_buf
);
3496 /* Compute the pointer corresponding to the given offset, accounting
3498 tbp
= trace_buffer_start
+ offset
;
3499 if (tbp
>= trace_buffer_wrap
)
3500 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3502 /* Trim to the remaining bytes if we're close to the end. */
3503 if (num
> tot
- offset
)
3506 /* Trim to available packet size. */
3507 if (num
>= (PBUFSIZ
- 16) / 2 )
3508 num
= (PBUFSIZ
- 16) / 2;
3510 convert_int_to_ascii (tbp
, own_buf
, num
);
3511 own_buf
[num
] = '\0';
3515 cmd_bigqtbuffer (char *own_buf
)
3518 char *packet
= own_buf
;
3520 packet
+= strlen ("QTBuffer:");
3522 if (strncmp ("circular:", packet
, strlen ("circular:")) == 0)
3524 packet
+= strlen ("circular:");
3525 packet
= unpack_varlen_hex (packet
, &val
);
3526 circular_trace_buffer
= val
;
3527 trace_debug ("Trace buffer is now %s",
3528 circular_trace_buffer
? "circular" : "linear");
3532 write_enn (own_buf
);
3536 handle_tracepoint_general_set (char *packet
)
3538 if (strcmp ("QTinit", packet
) == 0)
3540 cmd_qtinit (packet
);
3543 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3548 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3550 cmd_qtdpsrc (packet
);
3553 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3558 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3563 else if (strcmp ("QTStart", packet
) == 0)
3565 cmd_qtstart (packet
);
3568 else if (strcmp ("QTStop", packet
) == 0)
3570 cmd_qtstop (packet
);
3573 else if (strncmp ("QTDisconnected:", packet
,
3574 strlen ("QTDisconnected:")) == 0)
3576 cmd_qtdisconnected (packet
);
3579 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3581 cmd_qtframe (packet
);
3584 else if (strncmp ("QTBuffer:", packet
, strlen ("QTBuffer:")) == 0)
3586 cmd_bigqtbuffer (packet
);
3594 handle_tracepoint_query (char *packet
)
3596 if (strcmp ("qTStatus", packet
) == 0)
3598 cmd_qtstatus (packet
);
3601 else if (strcmp ("qTfP", packet
) == 0)
3606 else if (strcmp ("qTsP", packet
) == 0)
3611 else if (strcmp ("qTfV", packet
) == 0)
3616 else if (strcmp ("qTsV", packet
) == 0)
3621 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
3626 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
3628 cmd_qtbuffer (packet
);
3631 else if (strcmp ("qTfSTM", packet
) == 0)
3633 cmd_qtfstm (packet
);
3636 else if (strcmp ("qTsSTM", packet
) == 0)
3638 cmd_qtsstm (packet
);
3641 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
3643 cmd_qtstmat (packet
);
3651 #ifndef IN_PROCESS_AGENT
3653 /* Call this when thread TINFO has hit the tracepoint defined by
3654 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3655 action. This adds a while-stepping collecting state item to the
3656 threads' collecting state list, so that we can keep track of
3657 multiple simultaneous while-stepping actions being collected by the
3658 same thread. This can happen in cases like:
3660 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3662 ff0003 INSN3 <-- TP2, collect $regs
3663 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3666 Notice that when instruction INSN5 is reached, the while-stepping
3667 actions of both TP1 and TP3 are still being collected, and that TP2
3668 had been collected meanwhile. The whole range of ff0001-ff0005
3669 should be single-stepped, due to at least TP1's while-stepping
3670 action covering the whole range. */
3673 add_while_stepping_state (struct thread_info
*tinfo
,
3674 int tp_number
, CORE_ADDR tp_address
)
3676 struct wstep_state
*wstep
;
3678 wstep
= xmalloc (sizeof (*wstep
));
3679 wstep
->next
= tinfo
->while_stepping
;
3681 wstep
->tp_number
= tp_number
;
3682 wstep
->tp_address
= tp_address
;
3683 wstep
->current_step
= 0;
3685 tinfo
->while_stepping
= wstep
;
3688 /* Release the while-stepping collecting state WSTEP. */
3691 release_while_stepping_state (struct wstep_state
*wstep
)
3696 /* Release all while-stepping collecting states currently associated
3697 with thread TINFO. */
3700 release_while_stepping_state_list (struct thread_info
*tinfo
)
3702 struct wstep_state
*head
;
3704 while (tinfo
->while_stepping
)
3706 head
= tinfo
->while_stepping
;
3707 tinfo
->while_stepping
= head
->next
;
3708 release_while_stepping_state (head
);
3712 /* If TINFO was handling a 'while-stepping' action, the step has
3713 finished, so collect any step data needed, and check if any more
3714 steps are required. Return true if the thread was indeed
3715 collecting tracepoint data, false otherwise. */
3718 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3720 struct tracepoint
*tpoint
;
3721 struct wstep_state
*wstep
;
3722 struct wstep_state
**wstep_link
;
3723 struct trap_tracepoint_ctx ctx
;
3725 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
3727 if (in_process_agent_loaded ())
3728 upload_fast_traceframes ();
3730 /* Check if we were indeed collecting data for one of more
3731 tracepoints with a 'while-stepping' count. */
3732 if (tinfo
->while_stepping
== NULL
)
3737 /* We're not even tracing anymore. Stop this thread from
3739 release_while_stepping_state_list (tinfo
);
3741 /* The thread had stopped due to a single-step request indeed
3742 explained by a tracepoint. */
3746 wstep
= tinfo
->while_stepping
;
3747 wstep_link
= &tinfo
->while_stepping
;
3749 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
3750 target_pid_to_str (tinfo
->entry
.id
),
3751 wstep
->tp_number
, paddress (wstep
->tp_address
));
3753 ctx
.base
.type
= trap_tracepoint
;
3754 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3756 while (wstep
!= NULL
)
3758 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
3761 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
3762 wstep
->tp_number
, paddress (wstep
->tp_address
),
3763 target_pid_to_str (tinfo
->entry
.id
));
3766 *wstep_link
= wstep
->next
;
3767 release_while_stepping_state (wstep
);
3771 /* We've just finished one step. */
3772 ++wstep
->current_step
;
3775 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
3776 stop_pc
, tpoint
, wstep
->current_step
);
3778 if (wstep
->current_step
>= tpoint
->step_count
)
3780 /* The requested numbers of steps have occurred. */
3781 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
3782 target_pid_to_str (tinfo
->entry
.id
),
3783 wstep
->tp_number
, paddress (wstep
->tp_address
));
3785 /* Unlink the wstep. */
3786 *wstep_link
= wstep
->next
;
3787 release_while_stepping_state (wstep
);
3788 wstep
= *wstep_link
;
3790 /* Only check the hit count now, which ensure that we do all
3791 our stepping before stopping the run. */
3792 if (tpoint
->pass_count
> 0
3793 && tpoint
->hit_count
>= tpoint
->pass_count
3794 && stopping_tracepoint
== NULL
)
3795 stopping_tracepoint
= tpoint
;
3799 /* Keep single-stepping until the requested numbers of steps
3801 wstep_link
= &wstep
->next
;
3802 wstep
= *wstep_link
;
3805 if (stopping_tracepoint
3806 || trace_buffer_is_full
3807 || expr_eval_result
!= expr_eval_no_error
)
3817 /* Handle any internal tracing control breakpoint hits. That means,
3818 pull traceframes from the IPA to our buffer, and syncing both
3819 tracing agents when the IPA's tracing stops for some reason. */
3822 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3824 /* Pull in fast tracepoint trace frames from the inferior in-process
3825 agent's buffer into our buffer. */
3827 if (!in_process_agent_loaded ())
3830 upload_fast_traceframes ();
3832 /* Check if the in-process agent had decided we should stop
3834 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
3836 int ipa_trace_buffer_is_full
;
3837 CORE_ADDR ipa_stopping_tracepoint
;
3838 int ipa_expr_eval_result
;
3839 CORE_ADDR ipa_error_tracepoint
;
3841 trace_debug ("lib stopped at stop_tracing");
3843 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
3844 &ipa_trace_buffer_is_full
);
3846 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3847 &ipa_stopping_tracepoint
);
3848 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
3850 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
3851 &ipa_error_tracepoint
);
3852 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
3854 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
3855 &ipa_expr_eval_result
);
3856 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
3858 trace_debug ("lib: trace_buffer_is_full: %d, "
3859 "stopping_tracepoint: %s, "
3860 "ipa_expr_eval_result: %d, "
3861 "error_tracepoint: %s, ",
3862 ipa_trace_buffer_is_full
,
3863 paddress (ipa_stopping_tracepoint
),
3864 ipa_expr_eval_result
,
3865 paddress (ipa_error_tracepoint
));
3869 if (ipa_trace_buffer_is_full
)
3870 trace_debug ("lib stopped due to full buffer.");
3871 if (ipa_stopping_tracepoint
)
3872 trace_debug ("lib stopped due to tpoint");
3873 if (ipa_stopping_tracepoint
)
3874 trace_debug ("lib stopped due to error");
3877 if (ipa_stopping_tracepoint
!= 0)
3880 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
3882 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
3884 expr_eval_result
= ipa_expr_eval_result
;
3886 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
3891 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
3893 trace_debug ("lib stopped at flush_trace_buffer");
3900 /* Return true if TINFO just hit a tracepoint. Collect data if
3904 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3906 struct tracepoint
*tpoint
;
3908 struct trap_tracepoint_ctx ctx
;
3910 /* Not tracing, don't handle. */
3914 ctx
.base
.type
= trap_tracepoint
;
3915 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3917 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3919 /* Note that we collect fast tracepoints here as well. We'll
3920 step over the fast tracepoint jump later, which avoids the
3922 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
)
3924 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
3925 target_pid_to_str (tinfo
->entry
.id
),
3926 tpoint
->number
, paddress (tpoint
->address
));
3928 /* Test the condition if present, and collect if true. */
3930 || (condition_true_at_tracepoint
3931 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
3932 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
3935 if (stopping_tracepoint
3936 || trace_buffer_is_full
3937 || expr_eval_result
!= expr_eval_no_error
)
3941 /* If the tracepoint had a 'while-stepping' action, then set
3942 the thread to collect this tracepoint on the following
3944 else if (tpoint
->step_count
> 0)
3946 add_while_stepping_state (tinfo
,
3947 tpoint
->number
, tpoint
->address
);
3959 #if defined IN_PROCESS_AGENT && defined HAVE_UST
3960 struct ust_marker_data
;
3961 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
3963 struct tracepoint
*tpoint
,
3964 struct traceframe
*tframe
);
3967 /* Create a trace frame for the hit of the given tracepoint in the
3971 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
3972 struct tracepoint
*tpoint
)
3974 struct traceframe
*tframe
;
3977 /* Only count it as a hit when we actually collect data. */
3978 tpoint
->hit_count
++;
3980 /* If we've exceeded a defined pass count, record the event for
3981 later, and finish the collection for this hit. This test is only
3982 for nonstepping tracepoints, stepping tracepoints test at the end
3983 of their while-stepping loop. */
3984 if (tpoint
->pass_count
> 0
3985 && tpoint
->hit_count
>= tpoint
->pass_count
3986 && tpoint
->step_count
== 0
3987 && stopping_tracepoint
== NULL
)
3988 stopping_tracepoint
= tpoint
;
3990 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
3991 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
3993 tframe
= add_traceframe (tpoint
);
3997 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
3999 #ifndef IN_PROCESS_AGENT
4000 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4001 tpoint
->number
, paddress (tpoint
->address
),
4002 tpoint
->actions_str
[acti
]);
4005 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4006 tpoint
->actions
[acti
]);
4009 finish_traceframe (tframe
);
4012 if (tframe
== NULL
&& tracing
)
4013 trace_buffer_is_full
= 1;
4016 #ifndef IN_PROCESS_AGENT
4019 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4021 struct tracepoint
*tpoint
, int current_step
)
4023 struct traceframe
*tframe
;
4026 trace_debug ("Making new step traceframe for "
4027 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4028 tpoint
->number
, paddress (tpoint
->address
),
4029 current_step
, tpoint
->step_count
,
4032 tframe
= add_traceframe (tpoint
);
4036 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4038 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4039 tpoint
->number
, paddress (tpoint
->address
),
4040 tpoint
->step_actions_str
[acti
]);
4042 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4043 tpoint
->step_actions
[acti
]);
4046 finish_traceframe (tframe
);
4049 if (tframe
== NULL
&& tracing
)
4050 trace_buffer_is_full
= 1;
4055 static struct regcache
*
4056 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4058 struct regcache
*regcache
= NULL
;
4060 #ifdef IN_PROCESS_AGENT
4061 if (ctx
->type
== fast_tracepoint
)
4063 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4064 if (!fctx
->regcache_initted
)
4066 fctx
->regcache_initted
= 1;
4067 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4068 supply_regblock (&fctx
->regcache
, NULL
);
4069 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4071 regcache
= &fctx
->regcache
;
4074 if (ctx
->type
== static_tracepoint
)
4076 struct static_tracepoint_ctx
*sctx
= (struct static_tracepoint_ctx
*) ctx
;
4077 if (!sctx
->regcache_initted
)
4079 sctx
->regcache_initted
= 1;
4080 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4081 supply_regblock (&sctx
->regcache
, NULL
);
4082 /* Pass down the tracepoint address, because REGS doesn't
4083 include the PC, but we know what it must have been. */
4084 supply_static_tracepoint_registers (&sctx
->regcache
,
4085 (const unsigned char *)
4087 sctx
->tpoint
->address
);
4089 regcache
= &sctx
->regcache
;
4093 if (ctx
->type
== trap_tracepoint
)
4095 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4096 regcache
= tctx
->regcache
;
4100 gdb_assert (regcache
!= NULL
);
4106 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4108 struct tracepoint
*tpoint
,
4109 struct traceframe
*tframe
,
4110 struct tracepoint_action
*taction
)
4112 enum eval_result_type err
;
4114 switch (taction
->type
)
4118 struct collect_memory_action
*maction
;
4120 maction
= (struct collect_memory_action
*) taction
;
4122 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4123 pulongest (maction
->len
),
4124 paddress (maction
->addr
), maction
->basereg
);
4125 /* (should use basereg) */
4126 agent_mem_read (tframe
, NULL
,
4127 (CORE_ADDR
) maction
->addr
, maction
->len
);
4132 struct collect_registers_action
*raction
;
4134 unsigned char *regspace
;
4135 struct regcache tregcache
;
4136 struct regcache
*context_regcache
;
4138 raction
= (struct collect_registers_action
*) taction
;
4140 trace_debug ("Want to collect registers");
4142 /* Collect all registers for now. */
4143 regspace
= add_traceframe_block (tframe
,
4144 1 + register_cache_size ());
4145 if (regspace
== NULL
)
4147 trace_debug ("Trace buffer block allocation failed, skipping");
4150 /* Identify a register block. */
4153 context_regcache
= get_context_regcache (ctx
);
4155 /* Wrap the regblock in a register cache (in the stack, we
4156 don't want to malloc here). */
4157 init_register_cache (&tregcache
, regspace
+ 1);
4159 /* Copy the register data to the regblock. */
4160 regcache_cpy (&tregcache
, context_regcache
);
4162 #ifndef IN_PROCESS_AGENT
4163 /* On some platforms, trap-based tracepoints will have the PC
4164 pointing to the next instruction after the trap, but we
4165 don't want the user or GDB trying to guess whether the
4166 saved PC needs adjusting; so always record the adjusted
4167 stop_pc. Note that we can't use tpoint->address instead,
4168 since it will be wrong for while-stepping actions. This
4169 adjustment is a nop for fast tracepoints collected from the
4170 in-process lib (but not if GDBserver is collecting one
4171 preemptively), since the PC had already been adjusted to
4172 contain the tracepoint's address by the jump pad. */
4173 trace_debug ("Storing stop pc (0x%s) in regblock",
4174 paddress (tpoint
->address
));
4176 /* This changes the regblock, not the thread's
4178 regcache_write_pc (&tregcache
, stop_pc
);
4184 struct eval_expr_action
*eaction
;
4186 eaction
= (struct eval_expr_action
*) taction
;
4188 trace_debug ("Want to evaluate expression");
4190 err
= eval_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4192 if (err
!= expr_eval_no_error
)
4194 record_tracepoint_error (tpoint
, "action expression", err
);
4201 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4202 trace_debug ("Want to collect static trace data");
4203 collect_ust_data_at_tracepoint (ctx
, stop_pc
,
4206 trace_debug ("warning: collecting static trace data, "
4207 "but static tracepoints are not supported");
4212 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4218 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4219 struct tracepoint
*tpoint
)
4222 enum eval_result_type err
;
4224 /* Presently, gdbserver doesn't run compiled conditions, only the
4225 IPA does. If the program stops at a fast tracepoint's address
4226 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4227 gdbserver preemptively collect the fast tracepoint. Later, on
4228 resume, gdbserver steps over the fast tracepoint like it steps
4229 over breakpoints, so that the IPA doesn't see that fast
4230 tracepoint. This avoids double collects of fast tracepoints in
4231 that stopping scenario. Having gdbserver itself handle the fast
4232 tracepoint gives the user a consistent view of when fast or trap
4233 tracepoints are collected, compared to an alternative where only
4234 trap tracepoints are collected on stop, and fast tracepoints on
4235 resume. When a fast tracepoint is being processed by gdbserver,
4236 it is always the non-compiled condition expression that is
4238 #ifdef IN_PROCESS_AGENT
4239 if (tpoint
->compiled_cond
)
4240 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4243 err
= eval_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4245 if (err
!= expr_eval_no_error
)
4247 record_tracepoint_error (tpoint
, "condition", err
);
4248 /* The error case must return false. */
4252 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4253 tpoint
->number
, paddress (tpoint
->address
),
4255 return (value
? 1 : 0);
4258 #ifndef IN_PROCESS_AGENT
4260 /* The packet form of an agent expression consists of an 'X', number
4261 of bytes in expression, a comma, and then the bytes. */
4263 static struct agent_expr
*
4264 parse_agent_expr (char **actparm
)
4266 char *act
= *actparm
;
4268 struct agent_expr
*aexpr
;
4270 ++act
; /* skip the X */
4271 act
= unpack_varlen_hex (act
, &xlen
);
4272 ++act
; /* skip a comma */
4273 aexpr
= xmalloc (sizeof (struct agent_expr
));
4274 aexpr
->length
= xlen
;
4275 aexpr
->bytes
= xmalloc (xlen
);
4276 convert_ascii_to_int (act
, aexpr
->bytes
, xlen
);
4277 *actparm
= act
+ (xlen
* 2);
4281 /* Convert the bytes of an agent expression back into hex digits, so
4282 they can be printed or uploaded. This allocates the buffer,
4283 callers should free when they are done with it. */
4286 unparse_agent_expr (struct agent_expr
*aexpr
)
4290 rslt
= xmalloc (2 * aexpr
->length
+ 1);
4291 convert_int_to_ascii (aexpr
->bytes
, rslt
, aexpr
->length
);
4297 /* The agent expression evaluator, as specified by the GDB docs. It
4298 returns 0 if everything went OK, and a nonzero error code
4301 static enum eval_result_type
4302 eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4303 struct traceframe
*tframe
,
4304 struct agent_expr
*aexpr
,
4308 #define STACK_MAX 100
4309 ULONGEST stack
[STACK_MAX
], top
;
4314 /* This union is a convenient way to convert representations. For
4315 now, assume a standard architecture where the hardware integer
4316 types have 8, 16, 32, 64 bit types. A more robust solution would
4317 be to import stdint.h from gnulib. */
4322 unsigned char bytes
[1];
4327 unsigned char bytes
[2];
4332 unsigned char bytes
[4];
4337 unsigned char bytes
[8];
4342 if (aexpr
->length
== 0)
4344 trace_debug ("empty agent expression");
4345 return expr_eval_empty_expression
;
4348 /* Cache the stack top in its own variable. Much of the time we can
4349 operate on this variable, rather than dinking with the stack. It
4350 needs to be copied to the stack when sp changes. */
4355 op
= aexpr
->bytes
[pc
++];
4357 trace_debug ("About to interpret byte 0x%x", op
);
4361 case gdb_agent_op_add
:
4365 case gdb_agent_op_sub
:
4366 top
= stack
[--sp
] - top
;
4369 case gdb_agent_op_mul
:
4373 case gdb_agent_op_div_signed
:
4376 trace_debug ("Attempted to divide by zero");
4377 return expr_eval_divide_by_zero
;
4379 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
4382 case gdb_agent_op_div_unsigned
:
4385 trace_debug ("Attempted to divide by zero");
4386 return expr_eval_divide_by_zero
;
4388 top
= stack
[--sp
] / top
;
4391 case gdb_agent_op_rem_signed
:
4394 trace_debug ("Attempted to divide by zero");
4395 return expr_eval_divide_by_zero
;
4397 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
4400 case gdb_agent_op_rem_unsigned
:
4403 trace_debug ("Attempted to divide by zero");
4404 return expr_eval_divide_by_zero
;
4406 top
= stack
[--sp
] % top
;
4409 case gdb_agent_op_lsh
:
4410 top
= stack
[--sp
] << top
;
4413 case gdb_agent_op_rsh_signed
:
4414 top
= ((LONGEST
) stack
[--sp
]) >> top
;
4417 case gdb_agent_op_rsh_unsigned
:
4418 top
= stack
[--sp
] >> top
;
4421 case gdb_agent_op_trace
:
4422 agent_mem_read (tframe
,
4423 NULL
, (CORE_ADDR
) stack
[--sp
], (ULONGEST
) top
);
4428 case gdb_agent_op_trace_quick
:
4429 arg
= aexpr
->bytes
[pc
++];
4430 agent_mem_read (tframe
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
4433 case gdb_agent_op_log_not
:
4437 case gdb_agent_op_bit_and
:
4441 case gdb_agent_op_bit_or
:
4445 case gdb_agent_op_bit_xor
:
4449 case gdb_agent_op_bit_not
:
4453 case gdb_agent_op_equal
:
4454 top
= (stack
[--sp
] == top
);
4457 case gdb_agent_op_less_signed
:
4458 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
4461 case gdb_agent_op_less_unsigned
:
4462 top
= (stack
[--sp
] < top
);
4465 case gdb_agent_op_ext
:
4466 arg
= aexpr
->bytes
[pc
++];
4467 if (arg
< (sizeof (LONGEST
) * 8))
4469 LONGEST mask
= 1 << (arg
- 1);
4470 top
&= ((LONGEST
) 1 << arg
) - 1;
4471 top
= (top
^ mask
) - mask
;
4475 case gdb_agent_op_ref8
:
4476 agent_mem_read (tframe
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
4480 case gdb_agent_op_ref16
:
4481 agent_mem_read (tframe
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
4485 case gdb_agent_op_ref32
:
4486 agent_mem_read (tframe
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
4490 case gdb_agent_op_ref64
:
4491 agent_mem_read (tframe
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
4495 case gdb_agent_op_if_goto
:
4497 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4504 case gdb_agent_op_goto
:
4505 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4508 case gdb_agent_op_const8
:
4509 /* Flush the cached stack top. */
4511 top
= aexpr
->bytes
[pc
++];
4514 case gdb_agent_op_const16
:
4515 /* Flush the cached stack top. */
4517 top
= aexpr
->bytes
[pc
++];
4518 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4521 case gdb_agent_op_const32
:
4522 /* Flush the cached stack top. */
4524 top
= aexpr
->bytes
[pc
++];
4525 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4526 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4527 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4530 case gdb_agent_op_const64
:
4531 /* Flush the cached stack top. */
4533 top
= 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
++];
4538 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4539 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4540 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4543 case gdb_agent_op_reg
:
4544 /* Flush the cached stack top. */
4546 arg
= aexpr
->bytes
[pc
++];
4547 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4550 struct regcache
*regcache
;
4552 regcache
= get_context_regcache (ctx
);
4554 switch (register_size (regnum
))
4557 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
4561 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
4565 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
4569 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
4573 internal_error (__FILE__
, __LINE__
,
4574 "unhandled register size");
4579 case gdb_agent_op_end
:
4580 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4581 sp
, pulongest (top
));
4586 /* This should be an error */
4587 trace_debug ("Stack is empty, nothing to return");
4588 return expr_eval_empty_stack
;
4592 return expr_eval_no_error
;
4594 case gdb_agent_op_dup
:
4598 case gdb_agent_op_pop
:
4603 case gdb_agent_op_zero_ext
:
4604 arg
= aexpr
->bytes
[pc
++];
4605 if (arg
< (sizeof (LONGEST
) * 8))
4606 top
&= ((LONGEST
) 1 << arg
) - 1;
4609 case gdb_agent_op_swap
:
4610 /* Interchange top two stack elements, making sure top gets
4611 copied back onto stack. */
4613 top
= stack
[sp
- 1];
4614 stack
[sp
- 1] = stack
[sp
];
4617 case gdb_agent_op_getv
:
4618 /* Flush the cached stack top. */
4620 arg
= aexpr
->bytes
[pc
++];
4621 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4622 top
= get_trace_state_variable_value (arg
);
4625 case gdb_agent_op_setv
:
4626 arg
= aexpr
->bytes
[pc
++];
4627 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4628 set_trace_state_variable_value (arg
, top
);
4629 /* Note that we leave the value on the stack, for the
4630 benefit of later/enclosing expressions. */
4633 case gdb_agent_op_tracev
:
4634 arg
= aexpr
->bytes
[pc
++];
4635 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4636 agent_tsv_read (tframe
, arg
);
4639 /* GDB never (currently) generates any of these ops. */
4640 case gdb_agent_op_float
:
4641 case gdb_agent_op_ref_float
:
4642 case gdb_agent_op_ref_double
:
4643 case gdb_agent_op_ref_long_double
:
4644 case gdb_agent_op_l_to_d
:
4645 case gdb_agent_op_d_to_l
:
4646 case gdb_agent_op_trace16
:
4647 trace_debug ("Agent expression op 0x%x valid, but not handled",
4649 /* If ever GDB generates any of these, we don't have the
4650 option of ignoring. */
4654 trace_debug ("Agent expression op 0x%x not recognized", op
);
4655 /* Don't struggle on, things will just get worse. */
4656 return expr_eval_unrecognized_opcode
;
4659 /* Check for stack badness. */
4660 if (sp
>= (STACK_MAX
- 1))
4662 trace_debug ("Expression stack overflow");
4663 return expr_eval_stack_overflow
;
4668 trace_debug ("Expression stack underflow");
4669 return expr_eval_stack_underflow
;
4672 trace_debug ("Op %s -> sp=%d, top=0x%s",
4673 gdb_agent_op_names
[op
], sp
, pulongest (top
));
4677 /* Do memory copies for bytecodes. */
4678 /* Do the recording of memory blocks for actions and bytecodes. */
4681 agent_mem_read (struct traceframe
*tframe
,
4682 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4684 unsigned char *mspace
;
4685 ULONGEST remaining
= len
;
4686 unsigned short blocklen
;
4688 /* If a 'to' buffer is specified, use it. */
4691 read_inferior_memory (from
, to
, len
);
4695 /* Otherwise, create a new memory block in the trace buffer. */
4696 while (remaining
> 0)
4700 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4701 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4702 mspace
= add_traceframe_block (tframe
, sp
);
4705 /* Identify block as a memory block. */
4708 /* Record address and size. */
4709 memcpy (mspace
, &from
, sizeof (from
));
4710 mspace
+= sizeof (from
);
4711 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4712 mspace
+= sizeof (blocklen
);
4713 /* Record the memory block proper. */
4714 read_inferior_memory (from
, mspace
, blocklen
);
4715 trace_debug ("%d bytes recorded", blocklen
);
4716 remaining
-= blocklen
;
4722 /* Record the value of a trace state variable. */
4725 agent_tsv_read (struct traceframe
*tframe
, int n
)
4727 unsigned char *vspace
;
4730 vspace
= add_traceframe_block (tframe
,
4731 1 + sizeof (n
) + sizeof (LONGEST
));
4734 /* Identify block as a variable. */
4736 /* Record variable's number and value. */
4737 memcpy (vspace
+ 1, &n
, sizeof (n
));
4738 val
= get_trace_state_variable_value (n
);
4739 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4740 trace_debug ("Variable %d recorded", n
);
4744 #ifndef IN_PROCESS_AGENT
4746 static unsigned char *
4747 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4748 int tfnum
, char type_wanted
)
4750 unsigned char *dataptr
;
4754 trace_debug ("traceframe %d has no data", tfnum
);
4758 /* Iterate through a traceframe's blocks, looking for a block of the
4760 for (dataptr
= database
;
4761 dataptr
< database
+ datasize
;
4765 unsigned short mlen
;
4767 if (dataptr
== trace_buffer_wrap
)
4769 /* Adjust to reflect wrapping part of the frame around to
4771 datasize
= dataptr
- database
;
4772 dataptr
= database
= trace_buffer_lo
;
4774 blocktype
= *dataptr
++;
4776 if (type_wanted
== blocktype
)
4782 /* Skip over the registers block. */
4783 dataptr
+= register_cache_size ();
4786 /* Skip over the memory block. */
4787 dataptr
+= sizeof (CORE_ADDR
);
4788 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4789 dataptr
+= (sizeof (mlen
) + mlen
);
4792 /* Skip over the TSV block. */
4793 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4796 /* Skip over the static trace data block. */
4797 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4798 dataptr
+= (sizeof (mlen
) + mlen
);
4801 trace_debug ("traceframe %d has unknown block type 0x%x",
4810 static unsigned char *
4811 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4813 unsigned char *regblock
;
4815 regblock
= traceframe_find_block_type (tframe
->data
,
4819 if (regblock
== NULL
)
4820 trace_debug ("traceframe %d has no register data", tfnum
);
4825 /* Get registers from a traceframe. */
4828 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4830 unsigned char *dataptr
;
4831 struct tracepoint
*tpoint
;
4832 struct traceframe
*tframe
;
4834 tframe
= find_traceframe (tfnum
);
4838 trace_debug ("traceframe %d not found", tfnum
);
4842 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4843 if (dataptr
== NULL
)
4845 /* We don't like making up numbers, but GDB has all manner of
4846 troubles when the target says there are no registers. */
4847 supply_regblock (regcache
, NULL
);
4849 /* We can generally guess at a PC, although this will be
4850 misleading for while-stepping frames and multi-location
4852 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4854 regcache_write_pc (regcache
, tpoint
->address
);
4857 supply_regblock (regcache
, dataptr
);
4863 traceframe_get_pc (struct traceframe
*tframe
)
4865 struct regcache regcache
;
4866 unsigned char *dataptr
;
4868 dataptr
= traceframe_find_regblock (tframe
, -1);
4869 if (dataptr
== NULL
)
4872 init_register_cache (®cache
, dataptr
);
4873 return regcache_read_pc (®cache
);
4876 /* Read a requested block of memory from a trace frame. */
4879 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
4880 unsigned char *buf
, ULONGEST length
,
4883 struct traceframe
*tframe
;
4884 unsigned char *database
, *dataptr
;
4885 unsigned int datasize
;
4887 unsigned short mlen
;
4889 trace_debug ("traceframe_read_mem");
4891 tframe
= find_traceframe (tfnum
);
4895 trace_debug ("traceframe %d not found", tfnum
);
4899 datasize
= tframe
->data_size
;
4900 database
= dataptr
= &tframe
->data
[0];
4902 /* Iterate through a traceframe's blocks, looking for memory. */
4903 while ((dataptr
= traceframe_find_block_type (dataptr
,
4904 datasize
- (dataptr
- database
),
4905 tfnum
, 'M')) != NULL
)
4907 memcpy (&maddr
, dataptr
, sizeof (maddr
));
4908 dataptr
+= sizeof (maddr
);
4909 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4910 dataptr
+= sizeof (mlen
);
4911 trace_debug ("traceframe %d has %d bytes at %s",
4912 tfnum
, mlen
, paddress (maddr
));
4914 /* Check that requested data is in bounds. */
4915 if (maddr
<= addr
&& (addr
+ length
) <= (maddr
+ mlen
))
4917 /* Block includes the requested range, copy it out. */
4918 memcpy (buf
, dataptr
+ (addr
- maddr
), length
);
4923 /* Skip over this block. */
4927 trace_debug ("traceframe %d has no memory data for the desired region",
4935 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
4938 struct traceframe
*tframe
;
4939 unsigned char *database
, *dataptr
;
4940 unsigned int datasize
;
4943 trace_debug ("traceframe_read_tsv");
4945 tfnum
= current_traceframe
;
4949 trace_debug ("no current traceframe");
4953 tframe
= find_traceframe (tfnum
);
4957 trace_debug ("traceframe %d not found", tfnum
);
4961 datasize
= tframe
->data_size
;
4962 database
= dataptr
= &tframe
->data
[0];
4964 /* Iterate through a traceframe's blocks, looking for the tsv. */
4965 while ((dataptr
= traceframe_find_block_type (dataptr
,
4966 datasize
- (dataptr
- database
),
4967 tfnum
, 'V')) != NULL
)
4969 memcpy (&vnum
, dataptr
, sizeof (vnum
));
4970 dataptr
+= sizeof (vnum
);
4972 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
4974 /* Check that this is the variable we want. */
4977 memcpy (val
, dataptr
, sizeof (*val
));
4981 /* Skip over this block. */
4982 dataptr
+= sizeof (LONGEST
);
4985 trace_debug ("traceframe %d has no data for variable %d",
4990 /* Read a requested block of static tracepoint data from a trace
4994 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
4995 unsigned char *buf
, ULONGEST length
,
4998 struct traceframe
*tframe
;
4999 unsigned char *database
, *dataptr
;
5000 unsigned int datasize
;
5001 unsigned short mlen
;
5003 trace_debug ("traceframe_read_sdata");
5005 tframe
= find_traceframe (tfnum
);
5009 trace_debug ("traceframe %d not found", tfnum
);
5013 datasize
= tframe
->data_size
;
5014 database
= &tframe
->data
[0];
5016 /* Iterate through a traceframe's blocks, looking for static
5018 dataptr
= traceframe_find_block_type (database
, datasize
,
5020 if (dataptr
!= NULL
)
5022 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5023 dataptr
+= sizeof (mlen
);
5026 if (offset
+ length
> mlen
)
5027 length
= mlen
- offset
;
5029 memcpy (buf
, dataptr
, length
);
5037 trace_debug ("traceframe %d has no static trace data", tfnum
);
5043 /* Return the first fast tracepoint whose jump pad contains PC. */
5045 static struct tracepoint
*
5046 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5048 struct tracepoint
*tpoint
;
5050 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5051 if (tpoint
->type
== fast_tracepoint
)
5052 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5058 /* Return GDBserver's tracepoint that matches the IP Agent's
5059 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5062 static struct tracepoint
*
5063 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5065 struct tracepoint
*tpoint
;
5067 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5068 if (tpoint
->type
== fast_tracepoint
)
5069 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5077 /* The type of the object that is used to synchronize fast tracepoint
5080 typedef struct collecting_t
5082 /* The fast tracepoint number currently collecting. */
5085 /* A number that GDBserver can use to identify the thread that is
5086 presently holding the collect lock. This need not (and usually
5087 is not) the thread id, as getting the current thread ID usually
5088 requires a system call, which we want to avoid like the plague.
5089 Usually this is thread's TCB, found in the TLS (pseudo-)
5090 register, which is readable with a single insn on several
5092 uintptr_t thread_area
;
5095 #ifndef IN_PROCESS_AGENT
5098 force_unlock_trace_buffer (void)
5100 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5103 /* Check if the thread identified by THREAD_AREA which is stopped at
5104 STOP_PC, is presently locking the fast tracepoint collection, and
5105 if so, gather some status of said collection. Returns 0 if the
5106 thread isn't collecting or in the jump pad at all. 1, if in the
5107 jump pad (or within gdb_collect) and hasn't executed the adjusted
5108 original insn yet (can set a breakpoint there and run to it). 2,
5109 if presently executing the adjusted original insn --- in which
5110 case, if we want to move the thread out of the jump pad, we need to
5111 single-step it until this function returns 0. */
5114 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5116 struct fast_tpoint_collect_status
*status
)
5118 CORE_ADDR ipa_collecting
;
5119 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5120 struct tracepoint
*tpoint
;
5121 int needs_breakpoint
;
5123 /* The thread THREAD_AREA is either:
5125 0. not collecting at all, not within the jump pad, or within
5126 gdb_collect or one of its callees.
5128 1. in the jump pad and haven't reached gdb_collect
5130 2. within gdb_collect (out of the jump pad) (collect is set)
5132 3. we're in the jump pad, after gdb_collect having returned,
5133 possibly executing the adjusted insns.
5135 For cases 1 and 3, `collecting' may or not be set. The jump pad
5136 doesn't have any complicated jump logic, so we can tell if the
5137 thread is executing the adjust original insn or not by just
5138 matching STOP_PC with known jump pad addresses. If we it isn't
5139 yet executing the original insn, set a breakpoint there, and let
5140 the thread run to it, so to quickly step over a possible (many
5141 insns) gdb_collect call. Otherwise, or when the breakpoint is
5142 hit, only a few (small number of) insns are left to be executed
5143 in the jump pad. Single-step the thread until it leaves the
5148 needs_breakpoint
= 0;
5149 trace_debug ("fast_tracepoint_collecting");
5151 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5152 &ipa_gdb_jump_pad_buffer
))
5153 fatal ("error extracting `gdb_jump_pad_buffer'");
5154 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5155 &ipa_gdb_jump_pad_buffer_end
))
5156 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5158 if (ipa_gdb_jump_pad_buffer
<= stop_pc
&& stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5160 /* We can tell which tracepoint(s) the thread is collecting by
5161 matching the jump pad address back to the tracepoint. */
5162 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5165 warning ("in jump pad, but no matching tpoint?");
5170 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5172 tpoint
->number
, paddress (tpoint
->address
),
5173 paddress (tpoint
->jump_pad
),
5174 paddress (tpoint
->jump_pad_end
),
5175 paddress (tpoint
->adjusted_insn_addr
),
5176 paddress (tpoint
->adjusted_insn_addr_end
));
5179 /* Definitely in the jump pad. May or may not need
5180 fast-exit-jump-pad breakpoint. */
5181 if (tpoint
->jump_pad
<= stop_pc
5182 && stop_pc
< tpoint
->adjusted_insn_addr
)
5183 needs_breakpoint
= 1;
5187 collecting_t ipa_collecting_obj
;
5189 /* If `collecting' is set/locked, then the THREAD_AREA thread
5190 may or not be the one holding the lock. We have to read the
5191 lock to find out. */
5193 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5196 trace_debug ("fast_tracepoint_collecting:"
5197 " failed reading 'collecting' in the inferior");
5201 if (!ipa_collecting
)
5203 trace_debug ("fast_tracepoint_collecting: not collecting"
5204 " (and nobody is).");
5208 /* Some thread is collecting. Check which. */
5209 if (read_inferior_memory (ipa_collecting
,
5210 (unsigned char *) &ipa_collecting_obj
,
5211 sizeof (ipa_collecting_obj
)) != 0)
5214 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5216 trace_debug ("fast_tracepoint_collecting: not collecting "
5217 "(another thread is)");
5222 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5225 warning ("fast_tracepoint_collecting: collecting, "
5226 "but tpoint %s not found?",
5227 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5231 /* The thread is within `gdb_collect', skip over the rest of
5232 fast tracepoint collection quickly using a breakpoint. */
5233 needs_breakpoint
= 1;
5236 /* The caller wants a bit of status detail. */
5239 status
->tpoint_num
= tpoint
->number
;
5240 status
->tpoint_addr
= tpoint
->address
;
5241 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5242 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5245 if (needs_breakpoint
)
5247 /* Hasn't executed the original instruction yet. Set breakpoint
5248 there, and wait till it's hit, then single-step until exiting
5252 fast_tracepoint_collecting, returning continue-until-break at %s",
5253 paddress (tpoint
->adjusted_insn_addr
));
5255 return 1; /* continue */
5259 /* Just single-step until exiting the jump pad. */
5261 trace_debug ("fast_tracepoint_collecting, returning "
5262 "need-single-step (%s-%s)",
5263 paddress (tpoint
->adjusted_insn_addr
),
5264 paddress (tpoint
->adjusted_insn_addr_end
));
5266 return 2; /* single-step */
5272 #ifdef IN_PROCESS_AGENT
5274 /* The global fast tracepoint collect lock. Points to a collecting_t
5275 object built on the stack by the jump pad, if presently locked;
5276 NULL if it isn't locked. Note that this lock *must* be set while
5277 executing any *function other than the jump pad. See
5278 fast_tracepoint_collecting. */
5279 static collecting_t
* ATTR_USED collecting
;
5281 /* This routine, called from the jump pad (in asm) is designed to be
5282 called from the jump pads of fast tracepoints, thus it is on the
5285 IP_AGENT_EXPORT
void ATTR_USED
5286 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5288 struct fast_tracepoint_ctx ctx
;
5290 /* Don't do anything until the trace run is completely set up. */
5294 ctx
.base
.type
= fast_tracepoint
;
5296 ctx
.regcache_initted
= 0;
5297 ctx
.tpoint
= tpoint
;
5299 /* Wrap the regblock in a register cache (in the stack, we don't
5300 want to malloc here). */
5301 ctx
.regspace
= alloca (register_cache_size ());
5302 if (ctx
.regspace
== NULL
)
5304 trace_debug ("Trace buffer block allocation failed, skipping");
5308 /* Test the condition if present, and collect if true. */
5309 if (tpoint
->cond
== NULL
5310 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5313 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5314 tpoint
->address
, tpoint
);
5316 /* Note that this will cause original insns to be written back
5317 to where we jumped from, but that's OK because we're jumping
5318 back to the next whole instruction. This will go badly if
5319 instruction restoration is not atomic though. */
5320 if (stopping_tracepoint
5321 || trace_buffer_is_full
5322 || expr_eval_result
!= expr_eval_no_error
)
5327 /* If there was a condition and it evaluated to false, the only
5328 way we would stop tracing is if there was an error during
5329 condition expression evaluation. */
5330 if (expr_eval_result
!= expr_eval_no_error
)
5337 #ifndef IN_PROCESS_AGENT
5339 /* Bytecode compilation. */
5341 CORE_ADDR current_insn_ptr
;
5345 struct bytecode_address
5350 /* Offset and size of field to be modified in the goto block. */
5351 int from_offset
, from_size
;
5352 struct bytecode_address
*next
;
5353 } *bytecode_address_table
;
5356 get_raw_reg_func_addr (void)
5358 return ipa_sym_addrs
.addr_get_raw_reg
;
5362 emit_prologue (void)
5364 target_emit_ops ()->emit_prologue ();
5368 emit_epilogue (void)
5370 target_emit_ops ()->emit_epilogue ();
5376 target_emit_ops ()->emit_add ();
5382 target_emit_ops ()->emit_sub ();
5388 target_emit_ops ()->emit_mul ();
5394 target_emit_ops ()->emit_lsh ();
5398 emit_rsh_signed (void)
5400 target_emit_ops ()->emit_rsh_signed ();
5404 emit_rsh_unsigned (void)
5406 target_emit_ops ()->emit_rsh_unsigned ();
5412 target_emit_ops ()->emit_ext (arg
);
5418 target_emit_ops ()->emit_log_not ();
5424 target_emit_ops ()->emit_bit_and ();
5430 target_emit_ops ()->emit_bit_or ();
5436 target_emit_ops ()->emit_bit_xor ();
5442 target_emit_ops ()->emit_bit_not ();
5448 target_emit_ops ()->emit_equal ();
5452 emit_less_signed (void)
5454 target_emit_ops ()->emit_less_signed ();
5458 emit_less_unsigned (void)
5460 target_emit_ops ()->emit_less_unsigned ();
5466 target_emit_ops ()->emit_ref (size
);
5470 emit_if_goto (int *offset_p
, int *size_p
)
5472 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
5476 emit_goto (int *offset_p
, int *size_p
)
5478 target_emit_ops ()->emit_goto (offset_p
, size_p
);
5482 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
5484 target_emit_ops ()->write_goto_address (from
, to
, size
);
5488 emit_const (LONGEST num
)
5490 target_emit_ops ()->emit_const (num
);
5496 target_emit_ops ()->emit_reg (reg
);
5502 target_emit_ops ()->emit_pop ();
5506 emit_stack_flush (void)
5508 target_emit_ops ()->emit_stack_flush ();
5512 emit_zero_ext (int arg
)
5514 target_emit_ops ()->emit_zero_ext (arg
);
5520 target_emit_ops ()->emit_swap ();
5524 emit_stack_adjust (int n
)
5526 target_emit_ops ()->emit_stack_adjust (n
);
5529 /* FN's prototype is `LONGEST(*fn)(int)'. */
5532 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
5534 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
5537 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
5540 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
5542 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
5545 static enum eval_result_type
compile_bytecodes (struct agent_expr
*aexpr
);
5548 compile_tracepoint_condition (struct tracepoint
*tpoint
, CORE_ADDR
*jump_entry
)
5550 CORE_ADDR entry_point
= *jump_entry
;
5551 enum eval_result_type err
;
5553 trace_debug ("Starting condition compilation for tracepoint %d\n",
5556 /* Initialize the global pointer to the code being built. */
5557 current_insn_ptr
= *jump_entry
;
5561 err
= compile_bytecodes (tpoint
->cond
);
5563 if (err
== expr_eval_no_error
)
5567 /* Record the beginning of the compiled code. */
5568 tpoint
->compiled_cond
= entry_point
;
5570 trace_debug ("Condition compilation for tracepoint %d complete\n",
5575 /* Leave the unfinished code in situ, but don't point to it. */
5577 tpoint
->compiled_cond
= 0;
5579 trace_debug ("Condition compilation for tracepoint %d failed, "
5581 tpoint
->number
, err
);
5584 /* Update the code pointer passed in. Note that we do this even if
5585 the compile fails, so that we can look at the partial results
5586 instead of letting them be overwritten. */
5587 *jump_entry
= current_insn_ptr
;
5589 /* Leave a gap, to aid dump decipherment. */
5593 /* Given an agent expression, turn it into native code. */
5595 static enum eval_result_type
5596 compile_bytecodes (struct agent_expr
*aexpr
)
5602 /* This is only used to build 64-bit value for constants. */
5604 struct bytecode_address
*aentry
, *aentry2
;
5609 trace_debug ("Cannot compile op 0x%x\n", op); \
5610 return expr_eval_unhandled_opcode; \
5613 if (aexpr
->length
== 0)
5615 trace_debug ("empty agent expression\n");
5616 return expr_eval_empty_expression
;
5619 bytecode_address_table
= NULL
;
5623 op
= aexpr
->bytes
[pc
];
5625 trace_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
5627 /* Record the compiled-code address of the bytecode, for use by
5628 jump instructions. */
5629 aentry
= xmalloc (sizeof (struct bytecode_address
));
5631 aentry
->address
= current_insn_ptr
;
5632 aentry
->goto_pc
= -1;
5633 aentry
->from_offset
= aentry
->from_size
= 0;
5634 aentry
->next
= bytecode_address_table
;
5635 bytecode_address_table
= aentry
;
5643 case gdb_agent_op_add
:
5647 case gdb_agent_op_sub
:
5651 case gdb_agent_op_mul
:
5655 case gdb_agent_op_div_signed
:
5659 case gdb_agent_op_div_unsigned
:
5663 case gdb_agent_op_rem_signed
:
5667 case gdb_agent_op_rem_unsigned
:
5671 case gdb_agent_op_lsh
:
5675 case gdb_agent_op_rsh_signed
:
5679 case gdb_agent_op_rsh_unsigned
:
5680 emit_rsh_unsigned ();
5683 case gdb_agent_op_trace
:
5687 case gdb_agent_op_trace_quick
:
5691 case gdb_agent_op_log_not
:
5695 case gdb_agent_op_bit_and
:
5699 case gdb_agent_op_bit_or
:
5703 case gdb_agent_op_bit_xor
:
5707 case gdb_agent_op_bit_not
:
5711 case gdb_agent_op_equal
:
5715 case gdb_agent_op_less_signed
:
5716 emit_less_signed ();
5719 case gdb_agent_op_less_unsigned
:
5720 emit_less_unsigned ();
5723 case gdb_agent_op_ext
:
5724 arg
= aexpr
->bytes
[pc
++];
5725 if (arg
< (sizeof (LONGEST
) * 8))
5729 case gdb_agent_op_ref8
:
5733 case gdb_agent_op_ref16
:
5737 case gdb_agent_op_ref32
:
5741 case gdb_agent_op_ref64
:
5745 case gdb_agent_op_if_goto
:
5746 arg
= aexpr
->bytes
[pc
++];
5747 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5748 aentry
->goto_pc
= arg
;
5749 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5752 case gdb_agent_op_goto
:
5753 arg
= aexpr
->bytes
[pc
++];
5754 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5755 aentry
->goto_pc
= arg
;
5756 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5759 case gdb_agent_op_const8
:
5760 emit_stack_flush ();
5761 top
= aexpr
->bytes
[pc
++];
5765 case gdb_agent_op_const16
:
5766 emit_stack_flush ();
5767 top
= aexpr
->bytes
[pc
++];
5768 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5772 case gdb_agent_op_const32
:
5773 emit_stack_flush ();
5774 top
= aexpr
->bytes
[pc
++];
5775 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5776 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5777 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5781 case gdb_agent_op_const64
:
5782 emit_stack_flush ();
5783 top
= 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
++];
5788 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5789 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5790 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5794 case gdb_agent_op_reg
:
5795 emit_stack_flush ();
5796 arg
= aexpr
->bytes
[pc
++];
5797 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5801 case gdb_agent_op_end
:
5802 trace_debug ("At end of expression\n");
5804 /* Assume there is one stack element left, and that it is
5805 cached in "top" where emit_epilogue can get to it. */
5806 emit_stack_adjust (1);
5811 case gdb_agent_op_dup
:
5812 /* In our design, dup is equivalent to stack flushing. */
5813 emit_stack_flush ();
5816 case gdb_agent_op_pop
:
5820 case gdb_agent_op_zero_ext
:
5821 arg
= aexpr
->bytes
[pc
++];
5822 if (arg
< (sizeof (LONGEST
) * 8))
5823 emit_zero_ext (arg
);
5826 case gdb_agent_op_swap
:
5830 case gdb_agent_op_getv
:
5831 emit_stack_flush ();
5832 arg
= aexpr
->bytes
[pc
++];
5833 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5834 emit_int_call_1 (ipa_sym_addrs
.addr_get_trace_state_variable_value
,
5838 case gdb_agent_op_setv
:
5839 arg
= aexpr
->bytes
[pc
++];
5840 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5841 emit_void_call_2 (ipa_sym_addrs
.addr_set_trace_state_variable_value
,
5845 case gdb_agent_op_tracev
:
5849 /* GDB never (currently) generates any of these ops. */
5850 case gdb_agent_op_float
:
5851 case gdb_agent_op_ref_float
:
5852 case gdb_agent_op_ref_double
:
5853 case gdb_agent_op_ref_long_double
:
5854 case gdb_agent_op_l_to_d
:
5855 case gdb_agent_op_d_to_l
:
5856 case gdb_agent_op_trace16
:
5861 trace_debug ("Agent expression op 0x%x not recognized\n", op
);
5862 /* Don't struggle on, things will just get worse. */
5863 return expr_eval_unrecognized_opcode
;
5866 /* This catches errors that occur in target-specific code
5870 trace_debug ("Error %d while emitting code for %s\n",
5871 emit_error
, gdb_agent_op_names
[op
]);
5872 return expr_eval_unhandled_opcode
;
5875 trace_debug ("Op %s compiled\n", gdb_agent_op_names
[op
]);
5878 /* Now fill in real addresses as goto destinations. */
5879 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
5883 if (aentry
->goto_pc
< 0)
5886 /* Find the location that we are going to, and call back into
5887 target-specific code to write the actual address or
5889 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
5891 if (aentry2
->pc
== aentry
->goto_pc
)
5893 trace_debug ("Want to jump from %s to %s\n",
5894 paddress (aentry
->address
),
5895 paddress (aentry2
->address
));
5896 write_goto_address (aentry
->address
+ aentry
->from_offset
,
5897 aentry2
->address
, aentry
->from_size
);
5903 /* Error out if we didn't find a destination. */
5906 trace_debug ("Destination of goto %d not found\n",
5908 return expr_eval_invalid_goto
;
5912 return expr_eval_no_error
;
5915 /* We'll need to adjust these when we consider bi-arch setups, and big
5919 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5921 return write_inferior_memory (where
,
5922 (unsigned char *) &ptr
, sizeof (void *));
5925 /* The base pointer of the IPA's heap. This is the only memory the
5926 IPA is allowed to use. The IPA should _not_ call the inferior's
5927 `malloc' during operation. That'd be slow, and, most importantly,
5928 it may not be safe. We may be collecting a tracepoint in a signal
5929 handler, for example. */
5930 static CORE_ADDR target_tp_heap
;
5932 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5936 target_malloc (ULONGEST size
)
5940 if (target_tp_heap
== 0)
5942 /* We have the pointer *address*, need what it points to. */
5943 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5945 fatal ("could get target heap head pointer");
5948 ptr
= target_tp_heap
;
5949 target_tp_heap
+= size
;
5951 /* Pad to 8-byte alignment. */
5952 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5958 download_agent_expr (struct agent_expr
*expr
)
5960 CORE_ADDR expr_addr
;
5961 CORE_ADDR expr_bytes
;
5963 expr_addr
= target_malloc (sizeof (*expr
));
5964 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5966 expr_bytes
= target_malloc (expr
->length
);
5967 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5969 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5974 /* Align V up to N bits. */
5975 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5978 download_tracepoints (void)
5980 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
5981 struct tracepoint
*tpoint
;
5983 /* Start out empty. */
5984 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
5986 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5988 struct tracepoint target_tracepoint
;
5990 if (tpoint
->type
!= fast_tracepoint
5991 && tpoint
->type
!= static_tracepoint
)
5994 /* Maybe download a compiled condition. */
5995 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5997 CORE_ADDR jentry
, jump_entry
;
5999 jentry
= jump_entry
= get_jump_space_head ();
6001 if (tpoint
->cond
!= NULL
)
6003 /* Pad to 8-byte alignment. (needed?) */
6004 /* Actually this should be left for the target to
6006 jentry
= UALIGN (jentry
, 8);
6008 compile_tracepoint_condition (tpoint
, &jentry
);
6011 /* Pad to 8-byte alignment. */
6012 jentry
= UALIGN (jentry
, 8);
6013 claim_jump_space (jentry
- jump_entry
);
6016 target_tracepoint
= *tpoint
;
6019 tpptr
= target_malloc (sizeof (*tpoint
));
6020 tpoint
->obj_addr_on_target
= tpptr
;
6022 if (tpoint
== tracepoints
)
6024 /* First object in list, set the head pointer in the
6026 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
6030 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
6035 /* Write the whole object. We'll fix up its pointers in a bit.
6036 Assume no next for now. This is fixed up above on the next
6037 iteration, if there's any. */
6038 target_tracepoint
.next
= NULL
;
6039 /* Need to clear this here too, since we're downloading the
6040 tracepoints before clearing our own copy. */
6041 target_tracepoint
.hit_count
= 0;
6043 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
6044 sizeof (target_tracepoint
));
6047 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6049 download_agent_expr (tpoint
->cond
));
6051 if (tpoint
->numactions
)
6054 CORE_ADDR actions_array
;
6056 /* The pointers array. */
6058 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6059 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6063 /* Now for each pointer, download the action. */
6064 for (i
= 0; i
< tpoint
->numactions
; i
++)
6066 CORE_ADDR ipa_action
= 0;
6067 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6069 switch (action
->type
)
6073 = target_malloc (sizeof (struct collect_memory_action
));
6074 write_inferior_memory (ipa_action
,
6075 (unsigned char *) action
,
6076 sizeof (struct collect_memory_action
));
6080 = target_malloc (sizeof (struct collect_registers_action
));
6081 write_inferior_memory (ipa_action
,
6082 (unsigned char *) action
,
6083 sizeof (struct collect_registers_action
));
6088 struct eval_expr_action
*eaction
6089 = (struct eval_expr_action
*) action
;
6091 ipa_action
= target_malloc (sizeof (*eaction
));
6092 write_inferior_memory (ipa_action
,
6093 (unsigned char *) eaction
,
6096 expr
= download_agent_expr (eaction
->expr
);
6097 write_inferior_data_ptr
6098 (ipa_action
+ offsetof (struct eval_expr_action
, expr
),
6103 ipa_action
= target_malloc
6104 (sizeof (struct collect_static_trace_data_action
));
6105 write_inferior_memory
6107 (unsigned char *) action
,
6108 sizeof (struct collect_static_trace_data_action
));
6111 trace_debug ("unknown trace action '%c', ignoring",
6116 if (ipa_action
!= 0)
6117 write_inferior_data_ptr
6118 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
6126 download_trace_state_variables (void)
6128 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6129 struct trace_state_variable
*tsv
;
6131 /* Start out empty. */
6132 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6134 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6136 struct trace_state_variable target_tsv
;
6138 /* TSV's with a getter have been initialized equally in both the
6139 inferior and GDBserver. Skip them. */
6140 if (tsv
->getter
!= NULL
)
6146 ptr
= target_malloc (sizeof (*tsv
));
6148 if (tsv
== trace_state_variables
)
6150 /* First object in list, set the head pointer in the
6153 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6158 write_inferior_data_ptr (prev_ptr
6159 + offsetof (struct trace_state_variable
,
6164 /* Write the whole object. We'll fix up its pointers in a bit.
6165 Assume no next, fixup when needed. */
6166 target_tsv
.next
= NULL
;
6168 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6169 sizeof (target_tsv
));
6171 if (tsv
->name
!= NULL
)
6173 size_t size
= strlen (tsv
->name
) + 1;
6174 CORE_ADDR name_addr
= target_malloc (size
);
6175 write_inferior_memory (name_addr
,
6176 (unsigned char *) tsv
->name
, size
);
6177 write_inferior_data_ptr (ptr
6178 + offsetof (struct trace_state_variable
,
6183 if (tsv
->getter
!= NULL
)
6185 fatal ("what to do with these?");
6191 /* Fixup the next pointer in the last item in the list. */
6192 write_inferior_data_ptr (prev_ptr
+ offsetof (struct trace_state_variable
,
6197 /* Upload complete trace frames out of the IP Agent's trace buffer
6198 into GDBserver's trace buffer. This always uploads either all or
6199 no trace frames. This is the counter part of
6200 `trace_alloc_trace_buffer'. See its description of the atomic
6201 synching mechanism. */
6204 upload_fast_traceframes (void)
6206 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6207 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6209 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6210 unsigned int curr_tbctrl_idx
;
6211 unsigned int ipa_trace_buffer_ctrl_curr
;
6212 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6213 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6214 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6215 CORE_ADDR ipa_trace_buffer_lo
;
6216 CORE_ADDR ipa_trace_buffer_hi
;
6218 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6219 &ipa_traceframe_read_count_racy
))
6221 /* This will happen in most targets if the current thread is
6226 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6227 &ipa_traceframe_write_count_racy
))
6230 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6231 ipa_traceframe_write_count_racy
- ipa_traceframe_read_count_racy
,
6232 ipa_traceframe_write_count_racy
, ipa_traceframe_read_count_racy
);
6234 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6237 about_to_request_buffer_space_bkpt
6238 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6241 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6242 &ipa_trace_buffer_ctrl_curr
))
6245 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6247 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6250 unsigned int prev
, counter
;
6252 /* Update the token, with new counters, and the GDBserver stamp
6253 bit. Alway reuse the current TBC index. */
6254 prev
= ipa_trace_buffer_ctrl_curr
& 0x0007ff00;
6255 counter
= (prev
+ 0x100) & 0x0007ff00;
6257 ipa_trace_buffer_ctrl_curr
= (0x80000000
6263 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6264 ipa_trace_buffer_ctrl_curr
))
6267 trace_debug ("Lib: Committed %08x -> %08x",
6268 ipa_trace_buffer_ctrl_curr_old
,
6269 ipa_trace_buffer_ctrl_curr
);
6271 /* Re-read these, now that we've installed the
6272 `about_to_request_buffer_space' breakpoint/lock. A thread could
6273 have finished a traceframe between the last read of these
6274 counters and setting the breakpoint above. If we start
6275 uploading, we never want to leave this function with
6276 traceframe_read_count != 0, otherwise, GDBserver could end up
6277 incrementing the counter tokens more than once (due to event loop
6278 nesting), which would break the IP agent's "effective" detection
6279 (see trace_alloc_trace_buffer). */
6280 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6281 &ipa_traceframe_read_count
))
6283 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6284 &ipa_traceframe_write_count
))
6289 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6290 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6291 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6293 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6294 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6295 trace_debug ("note that ipa_traceframe_count's parts changed");
6298 /* Get the address of the current TBC object (the IP agent has an
6299 array of 3 such objects). The index is stored in the TBC
6301 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6302 ipa_trace_buffer_ctrl_addr
6303 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6305 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6306 (unsigned char *) &ipa_trace_buffer_ctrl
,
6307 sizeof (struct ipa_trace_buffer_control
)))
6310 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6311 &ipa_trace_buffer_lo
))
6313 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6314 &ipa_trace_buffer_hi
))
6317 /* Offsets are easier to grok for debugging than raw addresses,
6318 especially for the small trace buffer sizes that are useful for
6320 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6321 "endfree=%d wrap=%d hi=%d",
6323 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6324 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6325 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6326 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6327 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6329 /* Note that the IPA's buffer is always circular. */
6331 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6333 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6334 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6336 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6337 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6338 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6339 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6342 tf
= IPA_FIRST_TRACEFRAME ();
6344 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6346 struct tracepoint
*tpoint
;
6347 struct traceframe
*tframe
;
6348 unsigned char *block
;
6349 struct traceframe ipa_tframe
;
6351 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6352 offsetof (struct traceframe
, data
)))
6353 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6355 if (ipa_tframe
.tpnum
== 0)
6356 fatal ("Uploading: No (more) fast traceframes, but "
6357 "ipa_traceframe_count == %u??\n",
6358 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6360 /* Note that this will be incorrect for multi-location
6362 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6364 tframe
= add_traceframe (tpoint
);
6367 trace_buffer_is_full
= 1;
6368 trace_debug ("Uploading: trace buffer is full");
6372 /* Copy the whole set of blocks in one go for now. FIXME:
6373 split this in smaller blocks. */
6374 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6377 if (read_inferior_memory (tf
+ offsetof (struct traceframe
, data
),
6378 block
, ipa_tframe
.data_size
))
6379 error ("Uploading: Couldn't read traceframe data at %s\n",
6380 paddress (tf
+ offsetof (struct traceframe
, data
)));
6383 trace_debug ("Uploading: traceframe didn't fit");
6384 finish_traceframe (tframe
);
6387 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6389 /* If we freed the traceframe that wrapped around, go back
6390 to the non-wrap case. */
6391 if (tf
< ipa_trace_buffer_ctrl
.start
)
6393 trace_debug ("Lib: Discarding past the wraparound");
6394 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6396 ipa_trace_buffer_ctrl
.start
= tf
;
6397 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6398 ++ipa_traceframe_read_count
;
6400 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6401 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6403 trace_debug ("Lib: buffer is fully empty. "
6404 "Trace buffer [%d] start=%d free=%d endfree=%d",
6406 (int) (ipa_trace_buffer_ctrl
.start
6407 - ipa_trace_buffer_lo
),
6408 (int) (ipa_trace_buffer_ctrl
.free
6409 - ipa_trace_buffer_lo
),
6410 (int) (ipa_trace_buffer_ctrl
.end_free
6411 - ipa_trace_buffer_lo
));
6413 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6414 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6415 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6416 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6419 trace_debug ("Uploaded a traceframe\n"
6420 "Lib: Trace buffer [%d] start=%d free=%d "
6421 "endfree=%d wrap=%d hi=%d",
6423 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6424 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6425 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6426 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6427 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6430 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6431 (unsigned char *) &ipa_trace_buffer_ctrl
,
6432 sizeof (struct ipa_trace_buffer_control
)))
6435 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6436 ipa_traceframe_read_count
);
6438 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6441 cancel_breakpoints ();
6443 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6444 about_to_request_buffer_space_bkpt
= NULL
;
6448 if (trace_buffer_is_full
)
6453 #ifdef IN_PROCESS_AGENT
6455 IP_AGENT_EXPORT
int ust_loaded
;
6456 IP_AGENT_EXPORT
char cmd_buf
[CMD_BUF_SIZE
];
6460 /* Static tracepoints. */
6462 /* UST puts a "struct tracepoint" in the global namespace, which
6463 conflicts with our tracepoint. Arguably, being a library, it
6464 shouldn't take ownership of such a generic name. We work around it
6466 #define tracepoint ust_tracepoint
6467 #include <ust/ust.h>
6470 extern int serialize_to_text (char *outbuf
, int bufsize
,
6471 const char *fmt
, va_list ap
);
6473 #define GDB_PROBE_NAME "gdb"
6475 /* We dynamically search for the UST symbols instead of linking them
6476 in. This lets the user decide if the application uses static
6477 tracepoints, instead of always pulling libust.so in. This vector
6478 holds pointers to all functions we care about. */
6482 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6483 const char *fmt
, va_list ap
);
6485 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6486 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6488 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6490 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6493 void (*marker_iter_start
) (struct marker_iter
*iter
);
6494 void (*marker_iter_next
) (struct marker_iter
*iter
);
6495 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6496 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6501 /* Cast through typeof to catch incompatible API changes. Since UST
6502 only builds with gcc, we can freely use gcc extensions here
6504 #define GET_UST_SYM(SYM) \
6507 if (ust_ops.SYM == NULL) \
6508 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6509 if (ust_ops.SYM == NULL) \
6513 #define USTF(SYM) ust_ops.SYM
6515 /* Get pointers to all libust.so functions we care about. */
6520 GET_UST_SYM (serialize_to_text
);
6522 GET_UST_SYM (ltt_probe_register
);
6523 GET_UST_SYM (ltt_probe_unregister
);
6524 GET_UST_SYM (ltt_marker_connect
);
6525 GET_UST_SYM (ltt_marker_disconnect
);
6527 GET_UST_SYM (marker_iter_start
);
6528 GET_UST_SYM (marker_iter_next
);
6529 GET_UST_SYM (marker_iter_stop
);
6530 GET_UST_SYM (marker_iter_reset
);
6536 /* Given an UST marker, return the matching gdb static tracepoint.
6537 The match is done by address. */
6539 static struct tracepoint
*
6540 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6542 struct tracepoint
*tpoint
;
6544 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6546 if (!tpoint
->enabled
|| tpoint
->type
!= static_tracepoint
)
6549 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6556 /* The probe function we install on lttng/ust markers. Whenever a
6557 probed ust marker is hit, this function is called. This is similar
6558 to gdb_collect, only for static tracepoints, instead of fast
6562 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6563 struct registers
*regs
, void *call_private
,
6564 const char *fmt
, va_list *args
)
6566 struct tracepoint
*tpoint
;
6567 struct static_tracepoint_ctx ctx
;
6569 /* Don't do anything until the trace run is completely set up. */
6572 trace_debug ("gdb_probe: not tracing\n");
6576 ctx
.base
.type
= static_tracepoint
;
6577 ctx
.regcache_initted
= 0;
6582 /* Wrap the regblock in a register cache (in the stack, we don't
6583 want to malloc here). */
6584 ctx
.regspace
= alloca (register_cache_size ());
6585 if (ctx
.regspace
== NULL
)
6587 trace_debug ("Trace buffer block allocation failed, skipping");
6591 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6594 trace_debug ("gdb_probe: marker not known: "
6595 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6596 mdata
->location
, mdata
->channel
,
6597 mdata
->name
, mdata
->format
);
6601 ctx
.tpoint
= tpoint
;
6603 trace_debug ("gdb_probe: collecting marker: "
6604 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6605 mdata
->location
, mdata
->channel
,
6606 mdata
->name
, mdata
->format
);
6608 /* Test the condition if present, and collect if true. */
6609 if (tpoint
->cond
== NULL
6610 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6613 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6614 tpoint
->address
, tpoint
);
6616 if (stopping_tracepoint
6617 || trace_buffer_is_full
6618 || expr_eval_result
!= expr_eval_no_error
)
6623 /* If there was a condition and it evaluated to false, the only
6624 way we would stop tracing is if there was an error during
6625 condition expression evaluation. */
6626 if (expr_eval_result
!= expr_eval_no_error
)
6631 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6632 static tracepoint string data. This is a string passed to the
6633 tracing library by the user, at the time of the tracepoint marker
6634 call. E.g., in the UST marker call:
6636 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6638 the collected data is "str FOOBAZ".
6642 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6644 struct tracepoint
*tpoint
,
6645 struct traceframe
*tframe
)
6647 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6648 unsigned char *bufspace
;
6651 unsigned short blocklen
;
6655 trace_debug ("Wanted to collect static trace data, "
6656 "but there's no static trace data");
6660 va_copy (copy
, *umd
->args
);
6661 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6664 trace_debug ("Want to collect ust data");
6666 /* 'S' + size + string */
6667 bufspace
= add_traceframe_block (tframe
,
6668 1 + sizeof (blocklen
) + size
+ 1);
6669 if (bufspace
== NULL
)
6671 trace_debug ("Trace buffer block allocation failed, skipping");
6675 /* Identify a static trace data block. */
6678 blocklen
= size
+ 1;
6679 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6681 va_copy (copy
, *umd
->args
);
6682 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6683 size
+ 1, umd
->fmt
, copy
);
6686 trace_debug ("Storing static tracepoint data in regblock: %s",
6687 bufspace
+ 1 + sizeof (blocklen
));
6690 /* The probe to register with lttng/ust. */
6691 static struct ltt_available_probe gdb_ust_probe
=
6698 #endif /* HAVE_UST */
6699 #endif /* IN_PROCESS_AGENT */
6703 #include <sys/socket.h>
6706 #ifndef UNIX_PATH_MAX
6707 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6710 /* Where we put the socked used for synchronization. */
6711 #define SOCK_DIR P_tmpdir
6713 #endif /* HAVE_UST */
6715 #ifndef IN_PROCESS_AGENT
6720 gdb_ust_connect_sync_socket (int pid
)
6722 struct sockaddr_un addr
;
6724 char path
[UNIX_PATH_MAX
];
6726 res
= snprintf (path
, UNIX_PATH_MAX
, "%s/gdb_ust%d", SOCK_DIR
, pid
);
6727 if (res
>= UNIX_PATH_MAX
)
6729 trace_debug ("string overflow allocating socket name");
6733 res
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6736 warning ("error opening sync socket: %s\n", strerror (errno
));
6740 addr
.sun_family
= AF_UNIX
;
6742 res
= snprintf (addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
6743 if (res
>= UNIX_PATH_MAX
)
6745 warning ("string overflow allocating socket name\n");
6750 res
= connect (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6753 warning ("error connecting sync socket (%s): %s. "
6754 "Make sure the directory exists and that it is writable.",
6755 path
, strerror (errno
));
6763 /* Resume thread PTID. */
6766 resume_thread (ptid_t ptid
)
6768 struct thread_resume resume_info
;
6770 resume_info
.thread
= ptid
;
6771 resume_info
.kind
= resume_continue
;
6772 resume_info
.sig
= TARGET_SIGNAL_0
;
6773 (*the_target
->resume
) (&resume_info
, 1);
6776 /* Stop thread PTID. */
6779 stop_thread (ptid_t ptid
)
6781 struct thread_resume resume_info
;
6783 resume_info
.thread
= ptid
;
6784 resume_info
.kind
= resume_stop
;
6785 resume_info
.sig
= TARGET_SIGNAL_0
;
6786 (*the_target
->resume
) (&resume_info
, 1);
6789 /* Ask the in-process agent to run a command. Since we don't want to
6790 have to handle the IPA hitting breakpoints while running the
6791 command, we pause all threads, remove all breakpoints, and then set
6792 the helper thread re-running. We communicate with the helper
6793 thread by means of direct memory xfering, and a socket for
6797 run_inferior_command (char *cmd
)
6801 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
6803 ptid_t ptid
= null_ptid
;
6805 trace_debug ("run_inferior_command: running: %s", cmd
);
6808 uninsert_all_breakpoints ();
6810 if (read_inferior_integer (ipa_sym_addrs
.addr_helper_thread_id
, &tid
))
6812 warning ("Error reading helper thread's id in lib");
6818 warning ("helper thread not initialized yet");
6822 if (write_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
6823 (unsigned char *) cmd
, strlen (cmd
) + 1))
6825 warning ("Error writing command");
6829 ptid
= ptid_build (pid
, tid
, 0);
6831 resume_thread (ptid
);
6833 fd
= gdb_ust_connect_sync_socket (pid
);
6839 trace_debug ("signalling helper thread");
6843 ret
= write (fd
, buf
, 1);
6844 } while (ret
== -1 && errno
== EINTR
);
6846 trace_debug ("waiting for helper thread's response");
6850 ret
= read (fd
, buf
, 1);
6851 } while (ret
== -1 && errno
== EINTR
);
6855 trace_debug ("helper thread's response received");
6860 /* Need to read response with the inferior stopped. */
6861 if (!ptid_equal (ptid
, null_ptid
))
6863 int was_non_stop
= non_stop
;
6864 struct target_waitstatus status
;
6868 mywait (ptid
, &status
, 0, 0);
6869 non_stop
= was_non_stop
;
6874 if (read_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
6875 (unsigned char *) cmd
, CMD_BUF_SIZE
))
6877 warning ("Error reading command response");
6882 trace_debug ("run_inferior_command: response: %s", cmd
);
6886 reinsert_all_breakpoints ();
6892 #else /* HAVE_UST */
6895 run_inferior_command (char *cmd
)
6900 #endif /* HAVE_UST */
6902 #else /* !IN_PROCESS_AGENT */
6904 /* Thread ID of the helper thread. GDBserver reads this to know which
6905 is the help thread. This is an LWP id on Linux. */
6906 int helper_thread_id
;
6911 init_named_socket (const char *name
)
6914 struct sockaddr_un addr
;
6916 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6919 warning ("socket creation failed: %s", strerror (errno
));
6923 addr
.sun_family
= AF_UNIX
;
6925 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6926 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6928 result
= access (name
, F_OK
);
6932 result
= unlink (name
);
6935 warning ("unlink failed: %s", strerror (errno
));
6939 warning ("socket %s already exists; overwriting", name
);
6942 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6945 warning ("bind failed: %s", strerror (errno
));
6950 result
= listen (fd
, 1);
6953 warning ("listen: %s", strerror (errno
));
6962 gdb_ust_socket_init (void)
6965 char name
[UNIX_PATH_MAX
];
6967 result
= snprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6968 SOCK_DIR
, getpid ());
6969 if (result
>= UNIX_PATH_MAX
)
6971 trace_debug ("string overflow allocating socket name");
6975 fd
= init_named_socket (name
);
6977 warning ("Error initializing named socket (%s) for communication with the "
6978 "ust helper thread. Check that directory exists and that it "
6979 "is writable.", name
);
6984 /* Return an hexstr version of the STR C string, fit for sending to
6988 cstr_to_hexstr (const char *str
)
6990 int len
= strlen (str
);
6991 char *hexstr
= xmalloc (len
* 2 + 1);
6992 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6996 /* The next marker to be returned on a qTsSTM command. */
6997 static const struct marker
*next_st
;
6999 /* Returns the first known marker. */
7004 struct marker_iter iter
;
7006 USTF(marker_iter_reset
) (&iter
);
7007 USTF(marker_iter_start
) (&iter
);
7012 /* Returns the marker following M. */
7014 const struct marker
*
7015 next_marker (const struct marker
*m
)
7017 struct marker_iter iter
;
7019 USTF(marker_iter_reset
) (&iter
);
7020 USTF(marker_iter_start
) (&iter
);
7022 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7024 if (iter
.marker
== m
)
7026 USTF(marker_iter_next
) (&iter
);
7034 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
7038 response_ust_marker (char *packet
, const struct marker
*st
)
7040 char *strid
, *format
, *tmp
;
7042 next_st
= next_marker (st
);
7044 tmp
= xmalloc (strlen (st
->channel
) + 1 +
7045 strlen (st
->name
) + 1);
7046 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
7048 strid
= cstr_to_hexstr (tmp
);
7051 format
= cstr_to_hexstr (st
->format
);
7053 sprintf (packet
, "m%s:%s:%s",
7054 paddress ((uintptr_t) st
->location
),
7062 /* Return the first static tracepoint, and initialize the state
7063 machine that will iterate through all the static tracepoints. */
7066 cmd_qtfstm (char *packet
)
7068 trace_debug ("Returning first trace state variable definition");
7070 if (first_marker ())
7071 response_ust_marker (packet
, first_marker ());
7073 strcpy (packet
, "l");
7076 /* Return additional trace state variable definitions. */
7079 cmd_qtsstm (char *packet
)
7081 trace_debug ("Returning static tracepoint");
7084 response_ust_marker (packet
, next_st
);
7086 strcpy (packet
, "l");
7089 /* Disconnect the GDB probe from a marker at a given address. */
7092 unprobe_marker_at (char *packet
)
7096 struct marker_iter iter
;
7098 p
+= sizeof ("unprobe_marker_at:") - 1;
7100 p
= unpack_varlen_hex (p
, &address
);
7102 USTF(marker_iter_reset
) (&iter
);
7103 USTF(marker_iter_start
) (&iter
);
7104 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7105 if ((uintptr_t ) iter
.marker
->location
== address
)
7109 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7113 warning ("could not disable marker %s/%s",
7114 iter
.marker
->channel
, iter
.marker
->name
);
7119 /* Connect the GDB probe to a marker at a given address. */
7122 probe_marker_at (char *packet
)
7126 struct marker_iter iter
;
7129 p
+= sizeof ("probe_marker_at:") - 1;
7131 p
= unpack_varlen_hex (p
, &address
);
7133 USTF(marker_iter_reset
) (&iter
);
7135 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7137 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7138 if ((uintptr_t ) m
->location
== address
)
7142 trace_debug ("found marker for address. "
7143 "ltt_marker_connect (marker = %s/%s)",
7144 m
->channel
, m
->name
);
7146 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
, GDB_PROBE_NAME
);
7147 if (result
&& result
!= -EEXIST
)
7148 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7149 m
->channel
, m
->name
, -result
);
7153 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7154 m
->channel
, m
->name
);
7158 strcpy (packet
, "OK");
7162 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7167 cmd_qtstmat (char *packet
)
7171 struct marker_iter iter
;
7174 p
+= sizeof ("qTSTMat:") - 1;
7176 p
= unpack_varlen_hex (p
, &address
);
7178 USTF(marker_iter_reset
) (&iter
);
7180 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7182 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7183 if ((uintptr_t ) m
->location
== address
)
7185 response_ust_marker (packet
, m
);
7189 strcpy (packet
, "l");
7194 gdb_ust_thread (void *arg
)
7200 listen_fd
= gdb_ust_socket_init ();
7203 if (helper_thread_id
== 0)
7204 helper_thread_id
= syscall (SYS_gettid
);
7207 if (listen_fd
== -1)
7209 warning ("could not create sync socket\n");
7216 struct sockaddr_un sockaddr
;
7221 tmp
= sizeof (sockaddr
);
7225 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7227 /* It seems an ERESTARTSYS can escape out of accept. */
7228 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7232 warning ("Accept returned %d, error: %s\n",
7233 fd
, strerror (errno
));
7239 ret
= read (fd
, buf
, 1);
7240 } while (ret
== -1 && errno
== EINTR
);
7244 warning ("reading socket (fd=%d) failed with %s",
7245 fd
, strerror (errno
));
7252 if (strcmp ("qTfSTM", cmd_buf
) == 0)
7254 cmd_qtfstm (cmd_buf
);
7256 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7258 cmd_qtsstm (cmd_buf
);
7260 else if (strncmp ("unprobe_marker_at:",
7262 sizeof ("unprobe_marker_at:") - 1) == 0)
7264 unprobe_marker_at (cmd_buf
);
7266 else if (strncmp ("probe_marker_at:",
7268 sizeof ("probe_marker_at:") - 1) == 0)
7270 probe_marker_at (cmd_buf
);
7272 else if (strncmp ("qTSTMat:",
7274 sizeof ("qTSTMat:") - 1) == 0)
7276 cmd_qtstmat (cmd_buf
);
7278 else if (strcmp (cmd_buf
, "help") == 0)
7280 strcpy (cmd_buf
, "for help, press F1\n");
7283 strcpy (cmd_buf
, "");
7307 /* We want the helper thread to be as transparent as possible, so
7308 have it inherit an all-signals-blocked mask. */
7310 sigfillset (&new_mask
);
7311 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7313 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7315 res
= pthread_create (&thread
,
7320 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7322 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7324 while (helper_thread_id
== 0)
7327 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7330 #endif /* HAVE_UST */
7332 #include <sys/mman.h>
7335 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7336 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7337 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7339 static void __attribute__ ((constructor
))
7340 initialize_tracepoint_ftlib (void)
7342 initialize_tracepoint ();
7349 #endif /* IN_PROCESS_AGENT */
7352 tsv_get_timestamp (void)
7356 if (gettimeofday (&tv
, 0) != 0)
7359 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7363 initialize_tracepoint (void)
7365 /* There currently no way to change the buffer size. */
7366 const int sizeOfBuffer
= 5 * 1024 * 1024;
7367 unsigned char *buf
= xmalloc (sizeOfBuffer
);
7368 init_trace_buffer (buf
, sizeOfBuffer
);
7370 /* Wire trace state variable 1 to be the timestamp. This will be
7371 uploaded to GDB upon connection and become one of its trace state
7372 variables. (In case you're wondering, if GDB already has a trace
7373 variable numbered 1, it will be renumbered.) */
7374 create_trace_state_variable (1, 0);
7375 set_trace_state_variable_name (1, "trace_timestamp");
7376 set_trace_state_variable_getter (1, tsv_get_timestamp
);
7378 #ifdef IN_PROCESS_AGENT
7381 pagesize
= sysconf (_SC_PAGE_SIZE
);
7385 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7387 /* Allocate scratch buffer aligned on a page boundary. */
7388 gdb_jump_pad_buffer
= memalign (pagesize
, pagesize
* 20);
7389 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* 20;
7391 /* Make it writable and executable. */
7392 if (mprotect (gdb_jump_pad_buffer
, pagesize
* 20,
7393 PROT_READ
| PROT_WRITE
| PROT_EXEC
) != 0)
7395 initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
7396 gdb_jump_pad_buffer
, pagesize
* 20, strerror (errno
));
7399 initialize_low_tracepoint ();