1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2014 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/>. */
20 #include "tracepoint.h"
21 #include "gdbthread.h"
36 #define DEFAULT_TRACE_BUFFER_SIZE 5242880 /* 5*1024*1024 */
38 /* This file is built for both GDBserver, and the in-process
39 agent (IPA), a shared library that includes a tracing agent that is
40 loaded by the inferior to support fast tracepoints. Fast
41 tracepoints (or more accurately, jump based tracepoints) are
42 implemented by patching the tracepoint location with a jump into a
43 small trampoline function whose job is to save the register state,
44 call the in-process tracing agent, and then execute the original
45 instruction that was under the tracepoint jump (possibly adjusted,
46 if PC-relative, or some such).
48 The current synchronization design is pull based. That means,
49 GDBserver does most of the work, by peeking/poking at the inferior
50 agent's memory directly for downloading tracepoint and associated
51 objects, and for uploading trace frames. Whenever the IPA needs
52 something from GDBserver (trace buffer is full, tracing stopped for
53 some reason, etc.) the IPA calls a corresponding hook function
54 where GDBserver has placed a breakpoint.
56 Each of the agents has its own trace buffer. When browsing the
57 trace frames built from slow and fast tracepoints from GDB (tfind
58 mode), there's no guarantee the user is seeing the trace frames in
59 strict chronological creation order, although, GDBserver tries to
60 keep the order relatively reasonable, by syncing the trace buffers
65 #ifdef IN_PROCESS_AGENT
67 static void trace_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
70 trace_vdebug (const char *fmt
, ...)
76 vsprintf (buf
, fmt
, ap
);
77 fprintf (stderr
, PROG
"/tracepoint: %s\n", buf
);
81 #define trace_debug_1(level, fmt, args...) \
83 if (level <= debug_threads) \
84 trace_vdebug ((fmt), ##args); \
89 #define trace_debug_1(level, fmt, args...) \
91 if (level <= debug_threads) \
93 debug_printf ((fmt), ##args); \
94 debug_printf ("\n"); \
100 #define trace_debug(FMT, args...) \
101 trace_debug_1 (1, FMT, ##args)
103 #if defined(__GNUC__)
104 # define ATTR_USED __attribute__((used))
105 # define ATTR_NOINLINE __attribute__((noinline))
106 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
109 # define ATTR_NOINLINE
110 # define ATTR_CONSTRUCTOR
113 /* Make sure the functions the IPA needs to export (symbols GDBserver
114 needs to query GDB about) are exported. */
116 #ifdef IN_PROCESS_AGENT
117 # if defined _WIN32 || defined __CYGWIN__
118 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
121 # define IP_AGENT_EXPORT \
122 __attribute__ ((visibility("default"))) ATTR_USED
124 # define IP_AGENT_EXPORT ATTR_USED
128 # define IP_AGENT_EXPORT
131 /* Prefix exported symbols, for good citizenship. All the symbols
132 that need exporting are defined in this module. */
133 #ifdef IN_PROCESS_AGENT
134 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
135 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
136 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
137 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
138 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
139 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
140 # define collecting gdb_agent_collecting
141 # define gdb_collect gdb_agent_gdb_collect
142 # define stop_tracing gdb_agent_stop_tracing
143 # define flush_trace_buffer gdb_agent_flush_trace_buffer
144 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
145 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
146 # define stopping_tracepoint gdb_agent_stopping_tracepoint
147 # define expr_eval_result gdb_agent_expr_eval_result
148 # define error_tracepoint gdb_agent_error_tracepoint
149 # define tracepoints gdb_agent_tracepoints
150 # define tracing gdb_agent_tracing
151 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
152 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
153 # define trace_buffer_lo gdb_agent_trace_buffer_lo
154 # define trace_buffer_hi gdb_agent_trace_buffer_hi
155 # define traceframe_read_count gdb_agent_traceframe_read_count
156 # define traceframe_write_count gdb_agent_traceframe_write_count
157 # define traceframes_created gdb_agent_traceframes_created
158 # define trace_state_variables gdb_agent_trace_state_variables
159 # define get_raw_reg gdb_agent_get_raw_reg
160 # define get_trace_state_variable_value \
161 gdb_agent_get_trace_state_variable_value
162 # define set_trace_state_variable_value \
163 gdb_agent_set_trace_state_variable_value
164 # define ust_loaded gdb_agent_ust_loaded
165 # define helper_thread_id gdb_agent_helper_thread_id
166 # define cmd_buf gdb_agent_cmd_buf
169 #ifndef IN_PROCESS_AGENT
171 /* Addresses of in-process agent's symbols GDBserver cares about. */
173 struct ipa_sym_addresses
175 CORE_ADDR addr_gdb_tp_heap_buffer
;
176 CORE_ADDR addr_gdb_jump_pad_buffer
;
177 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
178 CORE_ADDR addr_gdb_trampoline_buffer
;
179 CORE_ADDR addr_gdb_trampoline_buffer_end
;
180 CORE_ADDR addr_gdb_trampoline_buffer_error
;
181 CORE_ADDR addr_collecting
;
182 CORE_ADDR addr_gdb_collect
;
183 CORE_ADDR addr_stop_tracing
;
184 CORE_ADDR addr_flush_trace_buffer
;
185 CORE_ADDR addr_about_to_request_buffer_space
;
186 CORE_ADDR addr_trace_buffer_is_full
;
187 CORE_ADDR addr_stopping_tracepoint
;
188 CORE_ADDR addr_expr_eval_result
;
189 CORE_ADDR addr_error_tracepoint
;
190 CORE_ADDR addr_tracepoints
;
191 CORE_ADDR addr_tracing
;
192 CORE_ADDR addr_trace_buffer_ctrl
;
193 CORE_ADDR addr_trace_buffer_ctrl_curr
;
194 CORE_ADDR addr_trace_buffer_lo
;
195 CORE_ADDR addr_trace_buffer_hi
;
196 CORE_ADDR addr_traceframe_read_count
;
197 CORE_ADDR addr_traceframe_write_count
;
198 CORE_ADDR addr_traceframes_created
;
199 CORE_ADDR addr_trace_state_variables
;
200 CORE_ADDR addr_get_raw_reg
;
201 CORE_ADDR addr_get_trace_state_variable_value
;
202 CORE_ADDR addr_set_trace_state_variable_value
;
203 CORE_ADDR addr_ust_loaded
;
212 IPA_SYM(gdb_tp_heap_buffer
),
213 IPA_SYM(gdb_jump_pad_buffer
),
214 IPA_SYM(gdb_jump_pad_buffer_end
),
215 IPA_SYM(gdb_trampoline_buffer
),
216 IPA_SYM(gdb_trampoline_buffer_end
),
217 IPA_SYM(gdb_trampoline_buffer_error
),
219 IPA_SYM(gdb_collect
),
220 IPA_SYM(stop_tracing
),
221 IPA_SYM(flush_trace_buffer
),
222 IPA_SYM(about_to_request_buffer_space
),
223 IPA_SYM(trace_buffer_is_full
),
224 IPA_SYM(stopping_tracepoint
),
225 IPA_SYM(expr_eval_result
),
226 IPA_SYM(error_tracepoint
),
227 IPA_SYM(tracepoints
),
229 IPA_SYM(trace_buffer_ctrl
),
230 IPA_SYM(trace_buffer_ctrl_curr
),
231 IPA_SYM(trace_buffer_lo
),
232 IPA_SYM(trace_buffer_hi
),
233 IPA_SYM(traceframe_read_count
),
234 IPA_SYM(traceframe_write_count
),
235 IPA_SYM(traceframes_created
),
236 IPA_SYM(trace_state_variables
),
237 IPA_SYM(get_raw_reg
),
238 IPA_SYM(get_trace_state_variable_value
),
239 IPA_SYM(set_trace_state_variable_value
),
243 static struct ipa_sym_addresses ipa_sym_addrs
;
245 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
247 /* Returns true if both the in-process agent library and the static
248 tracepoints libraries are loaded in the inferior, and agent has
249 capability on static tracepoints. */
252 in_process_agent_supports_ust (void)
256 if (!agent_loaded_p ())
258 warning ("In-process agent not loaded");
262 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE
))
264 /* Agent understands static tracepoint, then check whether UST is in
265 fact loaded in the inferior. */
266 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
268 warning ("Error reading ust_loaded in lib");
279 write_e_ipa_not_loaded (char *buffer
)
282 "E.In-process agent library not loaded in process. "
283 "Fast and static tracepoints unavailable.");
286 /* Write an error to BUFFER indicating that UST isn't loaded in the
290 write_e_ust_not_loaded (char *buffer
)
294 "E.UST library not loaded in process. "
295 "Static tracepoints unavailable.");
297 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
301 /* If the in-process agent library isn't loaded in the inferior, write
302 an error to BUFFER, and return 1. Otherwise, return 0. */
305 maybe_write_ipa_not_loaded (char *buffer
)
307 if (!agent_loaded_p ())
309 write_e_ipa_not_loaded (buffer
);
315 /* If the in-process agent library and the ust (static tracepoints)
316 library aren't loaded in the inferior, write an error to BUFFER,
317 and return 1. Otherwise, return 0. */
320 maybe_write_ipa_ust_not_loaded (char *buffer
)
322 if (!agent_loaded_p ())
324 write_e_ipa_not_loaded (buffer
);
327 else if (!in_process_agent_supports_ust ())
329 write_e_ust_not_loaded (buffer
);
335 /* Cache all future symbols that the tracepoints module might request.
336 We can not request symbols at arbitrary states in the remote
337 protocol, only when the client tells us that new symbols are
338 available. So when we load the in-process library, make sure to
339 check the entire list. */
342 tracepoint_look_up_symbols (void)
346 if (agent_loaded_p ())
349 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
352 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
354 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
357 debug_printf ("symbol `%s' not found\n", symbol_list
[i
].name
);
362 agent_look_up_symbols (NULL
);
367 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
368 of the "stop_tracing" function. When this breakpoint is hit,
369 tracing stopped in the IPA for some reason. E.g., due to
370 tracepoint reaching the pass count, hitting conditional expression
371 evaluation error, etc.
373 The IPA's trace buffer is never in circular tracing mode: instead,
374 GDBserver's is, and whenever the in-process buffer fills, it calls
375 "flush_trace_buffer", which triggers an internal breakpoint.
376 GDBserver reacts to this breakpoint by pulling the meanwhile
377 collected data. Old frames discarding is always handled on the
380 #ifdef IN_PROCESS_AGENT
382 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
384 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
388 /* Call this in the functions where GDBserver places a breakpoint, so
389 that the compiler doesn't try to be clever and skip calling the
390 function at all. This is necessary, even if we tell the compiler
391 to not inline said functions. */
393 #if defined(__GNUC__)
394 # define UNKNOWN_SIDE_EFFECTS() asm ("")
396 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
399 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
402 /* GDBserver places breakpoint here. */
403 UNKNOWN_SIDE_EFFECTS();
406 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
407 flush_trace_buffer (void)
409 /* GDBserver places breakpoint here. */
410 UNKNOWN_SIDE_EFFECTS();
415 #ifndef IN_PROCESS_AGENT
417 tracepoint_handler (CORE_ADDR address
)
419 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
424 /* Breakpoint at "stop_tracing" in the inferior lib. */
425 struct breakpoint
*stop_tracing_bkpt
;
426 static int stop_tracing_handler (CORE_ADDR
);
428 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
429 struct breakpoint
*flush_trace_buffer_bkpt
;
430 static int flush_trace_buffer_handler (CORE_ADDR
);
432 static void download_trace_state_variables (void);
433 static void upload_fast_traceframes (void);
435 static int run_inferior_command (char *cmd
, int len
);
438 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
440 return read_inferior_memory (symaddr
, (unsigned char *) val
,
445 static int tracepoint_send_agent (struct tracepoint
*tpoint
);
448 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
450 return read_inferior_memory (symaddr
, (unsigned char *) val
,
455 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
457 void *pval
= (void *) (uintptr_t) val
;
460 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
461 *val
= (uintptr_t) pval
;
466 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
468 void *pval
= (void *) (uintptr_t) val
;
469 return write_inferior_memory (symaddr
,
470 (unsigned char *) &pval
, sizeof (pval
));
474 write_inferior_integer (CORE_ADDR symaddr
, int val
)
476 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
480 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
482 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
485 static CORE_ADDR
target_malloc (ULONGEST size
);
486 static int write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
);
488 #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD) \
490 memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \
491 BUF += sizeof ((OBJ)->FIELD); \
496 /* Operations on various types of tracepoint actions. */
498 struct tracepoint_action
;
500 struct tracepoint_action_ops
502 /* Download tracepoint action ACTION to IPA. Return the address of action
504 CORE_ADDR (*download
) (const struct tracepoint_action
*action
);
506 /* Send ACTION to agent via command buffer started from BUFFER. Return
507 updated head of command buffer. */
508 char* (*send
) (char *buffer
, const struct tracepoint_action
*action
);
511 /* Base action. Concrete actions inherit this. */
513 struct tracepoint_action
515 #ifndef IN_PROCESS_AGENT
516 const struct tracepoint_action_ops
*ops
;
521 /* An 'M' (collect memory) action. */
522 struct collect_memory_action
524 struct tracepoint_action base
;
531 /* An 'R' (collect registers) action. */
533 struct collect_registers_action
535 struct tracepoint_action base
;
538 /* An 'X' (evaluate expression) action. */
540 struct eval_expr_action
542 struct tracepoint_action base
;
544 struct agent_expr
*expr
;
547 /* An 'L' (collect static trace data) action. */
548 struct collect_static_trace_data_action
550 struct tracepoint_action base
;
553 #ifndef IN_PROCESS_AGENT
555 m_tracepoint_action_download (const struct tracepoint_action
*action
)
557 int size_in_ipa
= (sizeof (struct collect_memory_action
)
558 - offsetof (struct tracepoint_action
, type
));
559 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
561 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
567 m_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
569 struct collect_memory_action
*maction
570 = (struct collect_memory_action
*) action
;
572 COPY_FIELD_TO_BUF (buffer
, maction
, addr
);
573 COPY_FIELD_TO_BUF (buffer
, maction
, len
);
574 COPY_FIELD_TO_BUF (buffer
, maction
, basereg
);
579 static const struct tracepoint_action_ops m_tracepoint_action_ops
=
581 m_tracepoint_action_download
,
582 m_tracepoint_action_send
,
586 r_tracepoint_action_download (const struct tracepoint_action
*action
)
588 int size_in_ipa
= (sizeof (struct collect_registers_action
)
589 - offsetof (struct tracepoint_action
, type
));
590 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
592 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
599 r_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
604 static const struct tracepoint_action_ops r_tracepoint_action_ops
=
606 r_tracepoint_action_download
,
607 r_tracepoint_action_send
,
610 static CORE_ADDR
download_agent_expr (struct agent_expr
*expr
);
613 x_tracepoint_action_download (const struct tracepoint_action
*action
)
615 int size_in_ipa
= (sizeof (struct eval_expr_action
)
616 - offsetof (struct tracepoint_action
, type
));
617 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
620 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
622 expr
= download_agent_expr (((struct eval_expr_action
*)action
)->expr
);
623 write_inferior_data_ptr (ipa_action
+ offsetof (struct eval_expr_action
, expr
)
624 - offsetof (struct tracepoint_action
, type
),
630 /* Copy agent expression AEXPR to buffer pointed by P. If AEXPR is NULL,
631 copy 0 to P. Return updated header of buffer. */
634 agent_expr_send (char *p
, const struct agent_expr
*aexpr
)
636 /* Copy the length of condition first, and then copy its
645 memcpy (p
, &aexpr
->length
, 4);
648 memcpy (p
, aexpr
->bytes
, aexpr
->length
);
655 x_tracepoint_action_send ( char *buffer
, const struct tracepoint_action
*action
)
657 struct eval_expr_action
*eaction
= (struct eval_expr_action
*) action
;
659 return agent_expr_send (buffer
, eaction
->expr
);
662 static const struct tracepoint_action_ops x_tracepoint_action_ops
=
664 x_tracepoint_action_download
,
665 x_tracepoint_action_send
,
669 l_tracepoint_action_download (const struct tracepoint_action
*action
)
671 int size_in_ipa
= (sizeof (struct collect_static_trace_data_action
)
672 - offsetof (struct tracepoint_action
, type
));
673 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
675 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
682 l_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
687 static const struct tracepoint_action_ops l_tracepoint_action_ops
=
689 l_tracepoint_action_download
,
690 l_tracepoint_action_send
,
694 /* This structure describes a piece of the source-level definition of
695 the tracepoint. The contents are not interpreted by the target,
696 but preserved verbatim for uploading upon reconnection. */
700 /* The type of string, such as "cond" for a conditional. */
703 /* The source-level string itself. For the sake of target
704 debugging, we store it in plaintext, even though it is always
705 transmitted in hex. */
708 /* Link to the next one in the list. We link them in the order
709 received, in case some make up an ordered list of commands or
711 struct source_string
*next
;
716 /* Trap based tracepoint. */
719 /* A fast tracepoint implemented with a jump instead of a trap. */
722 /* A static tracepoint, implemented by a program call into a tracing
727 struct tracepoint_hit_ctx
;
729 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
732 /* The definition of a tracepoint. */
734 /* Tracepoints may have multiple locations, each at a different
735 address. This can occur with optimizations, template
736 instantiation, etc. Since the locations may be in different
737 scopes, the conditions and actions may be different for each
738 location. Our target version of tracepoints is more like GDB's
739 notion of "breakpoint locations", but we have almost nothing that
740 is not per-location, so we bother having two kinds of objects. The
741 key consequence is that numbers are not unique, and that it takes
742 both number and address to identify a tracepoint uniquely. */
746 /* The number of the tracepoint, as specified by GDB. Several
747 tracepoint objects here may share a number. */
750 /* Address at which the tracepoint is supposed to trigger. Several
751 tracepoints may share an address. */
754 /* Tracepoint type. */
755 enum tracepoint_type type
;
757 /* True if the tracepoint is currently enabled. */
760 /* The number of single steps that will be performed after each
764 /* The number of times the tracepoint may be hit before it will
765 terminate the entire tracing run. */
768 /* Pointer to the agent expression that is the tracepoint's
769 conditional, or NULL if the tracepoint is unconditional. */
770 struct agent_expr
*cond
;
772 /* The list of actions to take when the tracepoint triggers. */
774 struct tracepoint_action
**actions
;
776 /* Count of the times we've hit this tracepoint during the run.
777 Note that while-stepping steps are not counted as "hits". */
780 /* Cached sum of the sizes of traceframes created by this point. */
781 uint64_t traceframe_usage
;
783 CORE_ADDR compiled_cond
;
785 /* Link to the next tracepoint in the list. */
786 struct tracepoint
*next
;
788 #ifndef IN_PROCESS_AGENT
789 /* The list of actions to take when the tracepoint triggers, in
790 string/packet form. */
793 /* The collection of strings that describe the tracepoint as it was
794 entered into GDB. These are not used by the target, but are
795 reported back to GDB upon reconnection. */
796 struct source_string
*source_strings
;
798 /* The number of bytes displaced by fast tracepoints. It may subsume
799 multiple instructions, for multi-byte fast tracepoints. This
800 field is only valid for fast tracepoints. */
803 /* Only for fast tracepoints. */
804 CORE_ADDR obj_addr_on_target
;
806 /* Address range where the original instruction under a fast
807 tracepoint was relocated to. (_end is actually one byte past
809 CORE_ADDR adjusted_insn_addr
;
810 CORE_ADDR adjusted_insn_addr_end
;
812 /* The address range of the piece of the jump pad buffer that was
813 assigned to this fast tracepoint. (_end is actually one byte
816 CORE_ADDR jump_pad_end
;
818 /* The address range of the piece of the trampoline buffer that was
819 assigned to this fast tracepoint. (_end is actually one byte
821 CORE_ADDR trampoline
;
822 CORE_ADDR trampoline_end
;
824 /* The list of actions to take while in a stepping loop. These
825 fields are only valid for patch-based tracepoints. */
826 int num_step_actions
;
827 struct tracepoint_action
**step_actions
;
828 /* Same, but in string/packet form. */
829 char **step_actions_str
;
831 /* Handle returned by the breakpoint or tracepoint module when we
832 inserted the trap or jump, or hooked into a static tracepoint.
833 NULL if we haven't inserted it yet. */
839 #ifndef IN_PROCESS_AGENT
841 /* Given `while-stepping', a thread may be collecting data for more
842 than one tracepoint simultaneously. On the other hand, the same
843 tracepoint with a while-stepping action may be hit by more than one
844 thread simultaneously (but not quite, each thread could be handling
845 a different step). Each thread holds a list of these objects,
846 representing the current step of each while-stepping action being
851 struct wstep_state
*next
;
853 /* The tracepoint number. */
855 /* The tracepoint's address. */
856 CORE_ADDR tp_address
;
858 /* The number of the current step in this 'while-stepping'
865 /* The linked list of all tracepoints. Marked explicitly as used as
866 the in-process library doesn't use it for the fast tracepoints
868 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
870 #ifndef IN_PROCESS_AGENT
872 /* Pointer to the last tracepoint in the list, new tracepoints are
873 linked in at the end. */
875 static struct tracepoint
*last_tracepoint
;
878 /* The first tracepoint to exceed its pass count. */
880 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
882 /* True if the trace buffer is full or otherwise no longer usable. */
884 IP_AGENT_EXPORT
int trace_buffer_is_full
;
886 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
888 #ifndef IN_PROCESS_AGENT
890 static const char *eval_result_names
[] =
892 "terror:in the attic", /* this should never be reported */
893 "terror:empty expression",
894 "terror:empty stack",
895 "terror:stack overflow",
896 "terror:stack underflow",
897 "terror:unhandled opcode",
898 "terror:unrecognized opcode",
899 "terror:divide by zero"
904 /* The tracepoint in which the error occurred. */
906 static struct tracepoint
*error_tracepoint
;
908 struct trace_state_variable
910 /* This is the name of the variable as used in GDB. The target
911 doesn't use the name, but needs to have it for saving and
912 reconnection purposes. */
915 /* This number identifies the variable uniquely. Numbers may be
916 assigned either by the target (in the case of builtin variables),
917 or by GDB, and are presumed unique during the course of a trace
921 /* The variable's initial value, a 64-bit signed integer always. */
922 LONGEST initial_value
;
924 /* The variable's value, a 64-bit signed integer always. */
927 /* Pointer to a getter function, used to supply computed values. */
928 LONGEST (*getter
) (void);
930 /* Link to the next variable. */
931 struct trace_state_variable
*next
;
934 /* Linked list of all trace state variables. */
936 #ifdef IN_PROCESS_AGENT
937 struct trace_state_variable
*alloced_trace_state_variables
;
940 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
942 /* The results of tracing go into a fixed-size space known as the
943 "trace buffer". Because usage follows a limited number of
944 patterns, we manage it ourselves rather than with malloc. Basic
945 rules are that we create only one trace frame at a time, each is
946 variable in size, they are never moved once created, and we only
947 discard if we are doing a circular buffer, and then only the oldest
948 ones. Each trace frame includes its own size, so we don't need to
949 link them together, and the trace frame number is relative to the
950 first one, so we don't need to record numbers. A trace frame also
951 records the number of the tracepoint that created it. The data
952 itself is a series of blocks, each introduced by a single character
953 and with a defined format. Each type of block has enough
954 type/length info to allow scanners to jump quickly from one block
955 to the next without reading each byte in the block. */
957 /* Trace buffer management would be simple - advance a free pointer
958 from beginning to end, then stop - were it not for the circular
959 buffer option, which is a useful way to prevent a trace run from
960 stopping prematurely because the buffer filled up. In the circular
961 case, the location of the first trace frame (trace_buffer_start)
962 moves as old trace frames are discarded. Also, since we grow trace
963 frames incrementally as actions are performed, we wrap around to
964 the beginning of the trace buffer. This is per-block, so each
965 block within a trace frame remains contiguous. Things get messy
966 when the wrapped-around trace frame is the one being discarded; the
967 free space ends up in two parts at opposite ends of the buffer. */
970 # if defined(__GNUC__)
971 # define ATTR_PACKED __attribute__ ((packed))
973 # define ATTR_PACKED /* nothing */
977 /* The data collected at a tracepoint hit. This object should be as
978 small as possible, since there may be a great many of them. We do
979 not need to keep a frame number, because they are all sequential
980 and there are no deletions; so the Nth frame in the buffer is
981 always frame number N. */
985 /* Number of the tracepoint that collected this traceframe. A value
986 of 0 indicates the current end of the trace buffer. We make this
987 a 16-bit field because it's never going to happen that GDB's
988 numbering of tracepoints reaches 32,000. */
991 /* The size of the data in this trace frame. We limit this to 32
992 bits, even on a 64-bit target, because it's just implausible that
993 one is validly going to collect 4 gigabytes of data at a single
995 unsigned int data_size
: 32;
997 /* The base of the trace data, which is contiguous from this point. */
998 unsigned char data
[0];
1002 /* The size of the EOB marker, in bytes. A traceframe with zeroed
1003 fields (and no data) marks the end of trace data. */
1004 #define TRACEFRAME_EOB_MARKER_SIZE offsetof (struct traceframe, data)
1006 /* The traceframe to be used as the source of data to send back to
1007 GDB. A value of -1 means to get data from the live program. */
1009 int current_traceframe
= -1;
1011 /* This flag is true if the trace buffer is circular, meaning that
1012 when it fills, the oldest trace frames are discarded in order to
1015 #ifndef IN_PROCESS_AGENT
1016 static int circular_trace_buffer
;
1019 /* Size of the trace buffer. */
1021 static LONGEST trace_buffer_size
;
1023 /* Pointer to the block of memory that traceframes all go into. */
1025 static unsigned char *trace_buffer_lo
;
1027 /* Pointer to the end of the trace buffer, more precisely to the byte
1028 after the end of the buffer. */
1030 static unsigned char *trace_buffer_hi
;
1032 /* Control structure holding the read/write/etc. pointers into the
1033 trace buffer. We need more than one of these to implement a
1034 transaction-like mechanism to garantees that both GDBserver and the
1035 in-process agent can try to change the trace buffer
1038 struct trace_buffer_control
1040 /* Pointer to the first trace frame in the buffer. In the
1041 non-circular case, this is equal to trace_buffer_lo, otherwise it
1042 moves around in the buffer. */
1043 unsigned char *start
;
1045 /* Pointer to the free part of the trace buffer. Note that we clear
1046 several bytes at and after this pointer, so that traceframe
1047 scans/searches terminate properly. */
1048 unsigned char *free
;
1050 /* Pointer to the byte after the end of the free part. Note that
1051 this may be smaller than trace_buffer_free in the circular case,
1052 and means that the free part is in two pieces. Initially it is
1053 equal to trace_buffer_hi, then is generally equivalent to
1054 trace_buffer_start. */
1055 unsigned char *end_free
;
1057 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
1058 this is the point at which the trace data breaks, and resumes at
1060 unsigned char *wrap
;
1063 /* Same as above, to be used by GDBserver when updating the in-process
1065 struct ipa_trace_buffer_control
1074 /* We have possibly both GDBserver and an inferior thread accessing
1075 the same IPA trace buffer memory. The IPA is the producer (tries
1076 to put new frames in the buffer), while GDBserver occasionally
1077 consumes them, that is, flushes the IPA's buffer into its own
1078 buffer. Both sides need to update the trace buffer control
1079 pointers (current head, tail, etc.). We can't use a global lock to
1080 synchronize the accesses, as otherwise we could deadlock GDBserver
1081 (if the thread holding the lock stops for a signal, say). So
1082 instead of that, we use a transaction scheme where GDBserver writes
1083 always prevail over the IPAs writes, and, we have the IPA detect
1084 the commit failure/overwrite, and retry the whole attempt. This is
1085 mainly implemented by having a global token object that represents
1086 who wrote last to the buffer control structure. We need to freeze
1087 any inferior writing to the buffer while GDBserver touches memory,
1088 so that the inferior can correctly detect that GDBserver had been
1089 there, otherwise, it could mistakingly think its commit was
1090 successful; that's implemented by simply having GDBserver set a
1091 breakpoint the inferior hits if it is the critical region.
1093 There are three cycling trace buffer control structure copies
1094 (buffer head, tail, etc.), with the token object including an index
1095 indicating which is current live copy. The IPA tentatively builds
1096 an updated copy in a non-current control structure, while GDBserver
1097 always clobbers the current version directly. The IPA then tries
1098 to atomically "commit" its version; if GDBserver clobbered the
1099 structure meanwhile, that will fail, and the IPA restarts the
1102 Listing the step in further detail, we have:
1104 In-process agent (producer):
1106 - passes by `about_to_request_buffer_space' breakpoint/lock
1108 - reads current token, extracts current trace buffer control index,
1109 and starts tentatively updating the rightmost one (0->1, 1->2,
1110 2->0). Note that only one inferior thread is executing this code
1111 at any given time, due to an outer lock in the jump pads.
1113 - updates counters, and tries to commit the token.
1115 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1116 leaving the sync region.
1118 - checks if the update was effective.
1120 - if trace buffer was found full, hits flush_trace_buffer
1121 breakpoint, and restarts later afterwards.
1123 GDBserver (consumer):
1125 - sets `about_to_request_buffer_space' breakpoint/lock.
1127 - updates the token unconditionally, using the current buffer
1128 control index, since it knows that the IP agent always writes to
1129 the rightmost, and due to the breakpoint, at most one IP thread
1130 can try to update the trace buffer concurrently to GDBserver, so
1131 there will be no danger of trace buffer control index wrap making
1132 the IPA write to the same index as GDBserver.
1134 - flushes the IP agent's trace buffer completely, and updates the
1135 current trace buffer control structure. GDBserver *always* wins.
1137 - removes the `about_to_request_buffer_space' breakpoint.
1139 The token is stored in the `trace_buffer_ctrl_curr' variable.
1140 Internally, it's bits are defined as:
1142 |-------------+-----+-------------+--------+-------------+--------------|
1143 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1144 |-------------+-----+-------------+--------+-------------+--------------|
1145 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1146 |-------------+-----+-------------+--------+-------------+--------------|
1148 GSB - GDBserver Stamp Bit
1149 PC - Previous Counter
1150 CC - Current Counter
1151 TBCI - Trace Buffer Control Index
1154 An IPA update of `trace_buffer_ctrl_curr' does:
1156 - read CC from the current token, save as PC.
1158 - atomically tries to write PC+1,CC
1160 A GDBserver update of `trace_buffer_ctrl_curr' does:
1162 - reads PC and CC from the current token.
1167 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1168 for the counters described below. The cleared bits are used to
1169 hold the index of the items of the `trace_buffer_ctrl' array that
1171 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1173 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1174 counter, and the `current' counter. */
1176 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1177 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1179 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1180 always stamps this bit as set. */
1181 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1183 #ifdef IN_PROCESS_AGENT
1184 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1185 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1187 # define TRACE_BUFFER_CTRL_CURR \
1188 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1192 /* The GDBserver side agent only needs one instance of this object, as
1193 it doesn't need to sync with itself. Define it as array anyway so
1194 that the rest of the code base doesn't need to care for the
1196 struct trace_buffer_control trace_buffer_ctrl
[1];
1197 # define TRACE_BUFFER_CTRL_CURR 0
1200 /* These are convenience macros used to access the current trace
1201 buffer control in effect. */
1202 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1203 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1204 #define trace_buffer_end_free \
1205 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1206 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1209 /* Macro that returns a pointer to the first traceframe in the buffer. */
1211 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1213 /* Macro that returns a pointer to the next traceframe in the buffer.
1214 If the computed location is beyond the wraparound point, subtract
1215 the offset of the wraparound. */
1217 #define NEXT_TRACEFRAME_1(TF) \
1218 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1220 #define NEXT_TRACEFRAME(TF) \
1221 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1222 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1223 ? (trace_buffer_wrap - trace_buffer_lo) \
1226 /* The difference between these counters represents the total number
1227 of complete traceframes present in the trace buffer. The IP agent
1228 writes to the write count, GDBserver writes to read count. */
1230 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1231 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1233 /* Convenience macro. */
1235 #define traceframe_count \
1236 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1238 /* The count of all traceframes created in the current run, including
1239 ones that were discarded to make room. */
1241 IP_AGENT_EXPORT
int traceframes_created
;
1243 #ifndef IN_PROCESS_AGENT
1245 /* Read-only regions are address ranges whose contents don't change,
1246 and so can be read from target memory even while looking at a trace
1247 frame. Without these, disassembly for instance will likely fail,
1248 because the program code is not usually collected into a trace
1249 frame. This data structure does not need to be very complicated or
1250 particularly efficient, it's only going to be used occasionally,
1251 and only by some commands. */
1253 struct readonly_region
1255 /* The bounds of the region. */
1256 CORE_ADDR start
, end
;
1258 /* Link to the next one. */
1259 struct readonly_region
*next
;
1262 /* Linked list of readonly regions. This list stays in effect from
1263 one tstart to the next. */
1265 static struct readonly_region
*readonly_regions
;
1269 /* The global that controls tracing overall. */
1271 IP_AGENT_EXPORT
int tracing
;
1273 #ifndef IN_PROCESS_AGENT
1275 /* Controls whether tracing should continue after GDB disconnects. */
1277 int disconnected_tracing
;
1279 /* The reason for the last tracing run to have stopped. We initialize
1280 to a distinct string so that GDB can distinguish between "stopped
1281 after running" and "stopped because never run" cases. */
1283 static const char *tracing_stop_reason
= "tnotrun";
1285 static int tracing_stop_tpnum
;
1287 /* 64-bit timestamps for the trace run's start and finish, expressed
1288 in microseconds from the Unix epoch. */
1290 LONGEST tracing_start_time
;
1291 LONGEST tracing_stop_time
;
1293 /* The (optional) user-supplied name of the user that started the run.
1294 This is an arbitrary string, and may be NULL. */
1296 char *tracing_user_name
;
1298 /* Optional user-supplied text describing the run. This is
1299 an arbitrary string, and may be NULL. */
1301 char *tracing_notes
;
1303 /* Optional user-supplied text explaining a tstop command. This is an
1304 arbitrary string, and may be NULL. */
1306 char *tracing_stop_note
;
1310 /* Functions local to this file. */
1312 /* Base "class" for tracepoint type specific data to be passed down to
1313 collect_data_at_tracepoint. */
1314 struct tracepoint_hit_ctx
1316 enum tracepoint_type type
;
1319 #ifdef IN_PROCESS_AGENT
1321 /* Fast/jump tracepoint specific data to be passed down to
1322 collect_data_at_tracepoint. */
1323 struct fast_tracepoint_ctx
1325 struct tracepoint_hit_ctx base
;
1327 struct regcache regcache
;
1328 int regcache_initted
;
1329 unsigned char *regspace
;
1331 unsigned char *regs
;
1332 struct tracepoint
*tpoint
;
1335 /* Static tracepoint specific data to be passed down to
1336 collect_data_at_tracepoint. */
1337 struct static_tracepoint_ctx
1339 struct tracepoint_hit_ctx base
;
1341 /* The regcache corresponding to the registers state at the time of
1342 the tracepoint hit. Initialized lazily, from REGS. */
1343 struct regcache regcache
;
1344 int regcache_initted
;
1346 /* The buffer space REGCACHE above uses. We use a separate buffer
1347 instead of letting the regcache malloc for both signal safety and
1348 performance reasons; this is allocated on the stack instead. */
1349 unsigned char *regspace
;
1351 /* The register buffer as passed on by lttng/ust. */
1352 struct registers
*regs
;
1354 /* The "printf" formatter and the args the user passed to the marker
1355 call. We use this to be able to collect "static trace data"
1360 /* The GDB tracepoint matching the probed marker that was "hit". */
1361 struct tracepoint
*tpoint
;
1366 /* Static tracepoint specific data to be passed down to
1367 collect_data_at_tracepoint. */
1368 struct trap_tracepoint_ctx
1370 struct tracepoint_hit_ctx base
;
1372 struct regcache
*regcache
;
1377 #ifndef IN_PROCESS_AGENT
1378 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1379 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1382 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1383 struct tracepoint
*tpoint
);
1385 #ifndef IN_PROCESS_AGENT
1386 static void clear_readonly_regions (void);
1387 static void clear_installed_tracepoints (void);
1390 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1392 struct tracepoint
*tpoint
);
1393 #ifndef IN_PROCESS_AGENT
1394 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1396 struct tracepoint
*tpoint
, int current_step
);
1397 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1398 CORE_ADDR
*jump_entry
);
1400 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1402 struct tracepoint
*tpoint
,
1403 struct traceframe
*tframe
,
1404 struct tracepoint_action
*taction
);
1406 #ifndef IN_PROCESS_AGENT
1407 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1409 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1410 static void download_tracepoint (struct tracepoint
*);
1411 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1412 static void clone_fast_tracepoint (struct tracepoint
*to
,
1413 const struct tracepoint
*from
);
1416 static LONGEST
get_timestamp (void);
1418 #if defined(__GNUC__)
1419 # define memory_barrier() asm volatile ("" : : : "memory")
1421 # define memory_barrier() do {} while (0)
1424 /* We only build the IPA if this builtin is supported, and there are
1425 no uses of this in GDBserver itself, so we're safe in defining this
1427 #define cmpxchg(mem, oldval, newval) \
1428 __sync_val_compare_and_swap (mem, oldval, newval)
1430 /* Record that an error occurred during expression evaluation. */
1433 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1434 enum eval_result_type rtype
)
1436 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1437 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1439 #ifdef IN_PROCESS_AGENT
1440 /* Only record the first error we get. */
1441 if (cmpxchg (&expr_eval_result
,
1443 rtype
) != expr_eval_no_error
)
1446 if (expr_eval_result
!= expr_eval_no_error
)
1450 error_tracepoint
= tpoint
;
1453 /* Trace buffer management. */
1456 clear_trace_buffer (void)
1458 trace_buffer_start
= trace_buffer_lo
;
1459 trace_buffer_free
= trace_buffer_lo
;
1460 trace_buffer_end_free
= trace_buffer_hi
;
1461 trace_buffer_wrap
= trace_buffer_hi
;
1462 /* A traceframe with zeroed fields marks the end of trace data. */
1463 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1464 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1465 traceframe_read_count
= traceframe_write_count
= 0;
1466 traceframes_created
= 0;
1469 #ifndef IN_PROCESS_AGENT
1472 clear_inferior_trace_buffer (void)
1474 CORE_ADDR ipa_trace_buffer_lo
;
1475 CORE_ADDR ipa_trace_buffer_hi
;
1476 struct traceframe ipa_traceframe
= { 0 };
1477 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1479 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1480 &ipa_trace_buffer_lo
);
1481 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1482 &ipa_trace_buffer_hi
);
1484 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1485 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1486 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1487 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1489 /* A traceframe with zeroed fields marks the end of trace data. */
1490 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1491 (unsigned char *) &ipa_trace_buffer_ctrl
,
1492 sizeof (ipa_trace_buffer_ctrl
));
1494 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1496 /* A traceframe with zeroed fields marks the end of trace data. */
1497 write_inferior_memory (ipa_trace_buffer_lo
,
1498 (unsigned char *) &ipa_traceframe
,
1499 sizeof (ipa_traceframe
));
1501 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1502 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1503 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1509 init_trace_buffer (LONGEST bufsize
)
1513 trace_buffer_size
= bufsize
;
1515 /* Make sure to internally allocate at least space for the EOB
1517 alloc_size
= (bufsize
< TRACEFRAME_EOB_MARKER_SIZE
1518 ? TRACEFRAME_EOB_MARKER_SIZE
: bufsize
);
1519 trace_buffer_lo
= xrealloc (trace_buffer_lo
, alloc_size
);
1521 trace_buffer_hi
= trace_buffer_lo
+ trace_buffer_size
;
1523 clear_trace_buffer ();
1526 #ifdef IN_PROCESS_AGENT
1528 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1529 about_to_request_buffer_space (void)
1531 /* GDBserver places breakpoint here while it goes about to flush
1532 data at random times. */
1533 UNKNOWN_SIDE_EFFECTS();
1538 /* Carve out a piece of the trace buffer, returning NULL in case of
1542 trace_buffer_alloc (size_t amt
)
1544 unsigned char *rslt
;
1545 struct trace_buffer_control
*tbctrl
;
1547 #ifdef IN_PROCESS_AGENT
1548 unsigned int prev
, prev_filtered
;
1549 unsigned int commit_count
;
1550 unsigned int commit
;
1551 unsigned int readout
;
1553 struct traceframe
*oldest
;
1554 unsigned char *new_start
;
1557 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1558 (long) amt
, (long) sizeof (struct traceframe
));
1560 /* Account for the EOB marker. */
1561 amt
+= TRACEFRAME_EOB_MARKER_SIZE
;
1563 #ifdef IN_PROCESS_AGENT
1567 /* Read the current token and extract the index to try to write to,
1568 storing it in CURR. */
1569 prev
= trace_buffer_ctrl_curr
;
1570 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1571 curr
= prev_filtered
+ 1;
1575 about_to_request_buffer_space ();
1577 /* Start out with a copy of the current state. GDBserver may be
1578 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1579 be able to commit anyway if that happens. */
1580 trace_buffer_ctrl
[curr
]
1581 = trace_buffer_ctrl
[prev_filtered
];
1582 trace_debug ("trying curr=%u", curr
);
1584 /* The GDBserver's agent doesn't need all that syncing, and always
1585 updates TCB 0 (there's only one, mind you). */
1588 tbctrl
= &trace_buffer_ctrl
[curr
];
1590 /* Offsets are easier to grok for debugging than raw addresses,
1591 especially for the small trace buffer sizes that are useful for
1593 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1595 (int) (tbctrl
->start
- trace_buffer_lo
),
1596 (int) (tbctrl
->free
- trace_buffer_lo
),
1597 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1598 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1599 (int) (trace_buffer_hi
- trace_buffer_lo
));
1601 /* The algorithm here is to keep trying to get a contiguous block of
1602 the requested size, possibly discarding older traceframes to free
1603 up space. Since free space might come in one or two pieces,
1604 depending on whether discarded traceframes wrapped around at the
1605 high end of the buffer, we test both pieces after each
1609 /* First, if we have two free parts, try the upper one first. */
1610 if (tbctrl
->end_free
< tbctrl
->free
)
1612 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1613 /* We have enough in the upper part. */
1617 /* Our high part of free space wasn't enough. Give up
1618 on it for now, set wraparound. We will recover the
1619 space later, if/when the wrapped-around traceframe is
1621 trace_debug ("Upper part too small, setting wraparound");
1622 tbctrl
->wrap
= tbctrl
->free
;
1623 tbctrl
->free
= trace_buffer_lo
;
1627 /* The normal case. */
1628 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1631 #ifdef IN_PROCESS_AGENT
1632 /* The IP Agent's buffer is always circular. It isn't used
1633 currently, but `circular_trace_buffer' could represent
1634 GDBserver's mode. If we didn't find space, ask GDBserver to
1637 flush_trace_buffer ();
1641 trace_debug ("gdbserver flushed buffer, retrying");
1645 /* GDBserver cancelled the tracing. Bail out as well. */
1648 /* If we're here, then neither part is big enough, and
1649 non-circular trace buffers are now full. */
1650 if (!circular_trace_buffer
)
1652 trace_debug ("Not enough space in the trace buffer");
1656 trace_debug ("Need more space in the trace buffer");
1658 /* If we have a circular buffer, we can try discarding the
1659 oldest traceframe and see if that helps. */
1660 oldest
= FIRST_TRACEFRAME ();
1661 if (oldest
->tpnum
== 0)
1663 /* Not good; we have no traceframes to free. Perhaps we're
1664 asking for a block that is larger than the buffer? In
1665 any case, give up. */
1666 trace_debug ("No traceframes to discard");
1670 /* We don't run this code in the in-process agent currently.
1671 E.g., we could leave the in-process agent in autonomous
1672 circular mode if we only have fast tracepoints. If we do
1673 that, then this bit becomes racy with GDBserver, which also
1674 writes to this counter. */
1675 --traceframe_write_count
;
1677 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1678 /* If we freed the traceframe that wrapped around, go back
1679 to the non-wrap case. */
1680 if (new_start
< tbctrl
->start
)
1682 trace_debug ("Discarding past the wraparound");
1683 tbctrl
->wrap
= trace_buffer_hi
;
1685 tbctrl
->start
= new_start
;
1686 tbctrl
->end_free
= tbctrl
->start
;
1688 trace_debug ("Discarded a traceframe\n"
1689 "Trace buffer [%d], start=%d free=%d "
1690 "endfree=%d wrap=%d hi=%d",
1692 (int) (tbctrl
->start
- trace_buffer_lo
),
1693 (int) (tbctrl
->free
- trace_buffer_lo
),
1694 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1695 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1696 (int) (trace_buffer_hi
- trace_buffer_lo
));
1698 /* Now go back around the loop. The discard might have resulted
1699 in either one or two pieces of free space, so we want to try
1700 both before freeing any more traceframes. */
1704 /* If we get here, we know we can provide the asked-for space. */
1706 rslt
= tbctrl
->free
;
1708 /* Adjust the request back down, now that we know we have space for
1709 the marker, but don't commit to AMT yet, we may still need to
1710 restart the operation if GDBserver touches the trace buffer
1711 (obviously only important in the in-process agent's version). */
1712 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1714 /* Or not. If GDBserver changed the trace buffer behind our back,
1715 we get to restart a new allocation attempt. */
1717 #ifdef IN_PROCESS_AGENT
1718 /* Build the tentative token. */
1719 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1720 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1721 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1725 /* Try to commit it. */
1726 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1727 if (readout
!= prev
)
1729 trace_debug ("GDBserver has touched the trace buffer, restarting."
1730 " (prev=%08x, commit=%08x, readout=%08x)",
1731 prev
, commit
, readout
);
1735 /* Hold your horses here. Even if that change was committed,
1736 GDBserver could come in, and clobber it. We need to hold to be
1737 able to tell if GDBserver clobbers before or after we committed
1738 the change. Whenever GDBserver goes about touching the IPA
1739 buffer, it sets a breakpoint in this routine, so we have a sync
1741 about_to_request_buffer_space ();
1743 /* Check if the change has been effective, even if GDBserver stopped
1744 us at the breakpoint. */
1747 unsigned int refetch
;
1751 refetch
= trace_buffer_ctrl_curr
;
1753 if (refetch
== commit
1754 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1757 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1758 "readout=%08x, refetch=%08x)",
1759 prev
, commit
, readout
, refetch
);
1763 trace_debug ("GDBserver has touched the trace buffer, not effective."
1764 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1765 prev
, commit
, readout
, refetch
);
1771 /* We have a new piece of the trace buffer. Hurray! */
1773 /* Add an EOB marker just past this allocation. */
1774 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1775 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1777 /* Adjust the request back down, now that we know we have space for
1779 amt
-= sizeof (struct traceframe
);
1783 trace_debug ("Allocated %d bytes", (int) amt
);
1784 trace_debug ("Trace buffer [%d] start=%d free=%d "
1785 "endfree=%d wrap=%d hi=%d",
1787 (int) (tbctrl
->start
- trace_buffer_lo
),
1788 (int) (tbctrl
->free
- trace_buffer_lo
),
1789 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1790 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1791 (int) (trace_buffer_hi
- trace_buffer_lo
));
1797 #ifndef IN_PROCESS_AGENT
1799 /* Return the total free space. This is not necessarily the largest
1800 block we can allocate, because of the two-part case. */
1805 if (trace_buffer_free
<= trace_buffer_end_free
)
1806 return trace_buffer_end_free
- trace_buffer_free
;
1808 return ((trace_buffer_end_free
- trace_buffer_lo
)
1809 + (trace_buffer_hi
- trace_buffer_free
));
1812 /* An 'S' in continuation packets indicates remainder are for
1815 static int seen_step_action_flag
;
1817 /* Create a tracepoint (location) with given number and address. Add this
1818 new tracepoint to list and sort this list. */
1820 static struct tracepoint
*
1821 add_tracepoint (int num
, CORE_ADDR addr
)
1823 struct tracepoint
*tpoint
, **tp_next
;
1825 tpoint
= xmalloc (sizeof (struct tracepoint
));
1826 tpoint
->number
= num
;
1827 tpoint
->address
= addr
;
1828 tpoint
->numactions
= 0;
1829 tpoint
->actions
= NULL
;
1830 tpoint
->actions_str
= NULL
;
1831 tpoint
->cond
= NULL
;
1832 tpoint
->num_step_actions
= 0;
1833 tpoint
->step_actions
= NULL
;
1834 tpoint
->step_actions_str
= NULL
;
1835 /* Start all off as regular (slow) tracepoints. */
1836 tpoint
->type
= trap_tracepoint
;
1837 tpoint
->orig_size
= -1;
1838 tpoint
->source_strings
= NULL
;
1839 tpoint
->compiled_cond
= 0;
1840 tpoint
->handle
= NULL
;
1841 tpoint
->next
= NULL
;
1843 /* Find a place to insert this tracepoint into list in order to keep
1844 the tracepoint list still in the ascending order. There may be
1845 multiple tracepoints at the same address as TPOINT's, and this
1846 guarantees TPOINT is inserted after all the tracepoints which are
1847 set at the same address. For example, fast tracepoints A, B, C are
1848 set at the same address, and D is to be insert at the same place as
1851 -->| A |--> | B |-->| C |->...
1853 One jump pad was created for tracepoint A, B, and C, and the target
1854 address of A is referenced/used in jump pad. So jump pad will let
1855 inferior jump to A. If D is inserted in front of A, like this,
1857 -->| D |-->| A |--> | B |-->| C |->...
1859 without updating jump pad, D is not reachable during collect, which
1860 is wrong. As we can see, the order of B, C and D doesn't matter, but
1861 A should always be the `first' one. */
1862 for (tp_next
= &tracepoints
;
1863 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1864 tp_next
= &(*tp_next
)->next
)
1866 tpoint
->next
= *tp_next
;
1868 last_tracepoint
= tpoint
;
1870 seen_step_action_flag
= 0;
1875 #ifndef IN_PROCESS_AGENT
1877 /* Return the tracepoint with the given number and address, or NULL. */
1879 static struct tracepoint
*
1880 find_tracepoint (int id
, CORE_ADDR addr
)
1882 struct tracepoint
*tpoint
;
1884 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1885 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1891 /* Remove TPOINT from global list. */
1894 remove_tracepoint (struct tracepoint
*tpoint
)
1896 struct tracepoint
*tp
, *tp_prev
;
1898 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1899 tp_prev
= tp
, tp
= tp
->next
)
1905 tp_prev
->next
= tp
->next
;
1907 tracepoints
= tp
->next
;
1913 /* There may be several tracepoints with the same number (because they
1914 are "locations", in GDB parlance); return the next one after the
1915 given tracepoint, or search from the beginning of the list if the
1916 first argument is NULL. */
1918 static struct tracepoint
*
1919 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1921 struct tracepoint
*tpoint
;
1924 tpoint
= prev_tp
->next
;
1926 tpoint
= tracepoints
;
1927 for (; tpoint
; tpoint
= tpoint
->next
)
1928 if (tpoint
->number
== num
)
1936 /* Append another action to perform when the tracepoint triggers. */
1939 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1945 seen_step_action_flag
= 1;
1953 char *act_start
= act
;
1954 struct tracepoint_action
*action
= NULL
;
1960 struct collect_memory_action
*maction
;
1964 maction
= xmalloc (sizeof *maction
);
1965 maction
->base
.type
= *act
;
1966 maction
->base
.ops
= &m_tracepoint_action_ops
;
1967 action
= &maction
->base
;
1970 is_neg
= (*act
== '-');
1973 act
= unpack_varlen_hex (act
, &basereg
);
1975 act
= unpack_varlen_hex (act
, &maction
->addr
);
1977 act
= unpack_varlen_hex (act
, &maction
->len
);
1978 maction
->basereg
= (is_neg
1981 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1982 pulongest (maction
->len
),
1983 paddress (maction
->addr
), maction
->basereg
);
1988 struct collect_registers_action
*raction
;
1990 raction
= xmalloc (sizeof *raction
);
1991 raction
->base
.type
= *act
;
1992 raction
->base
.ops
= &r_tracepoint_action_ops
;
1993 action
= &raction
->base
;
1995 trace_debug ("Want to collect registers");
1997 /* skip past hex digits of mask for now */
1998 while (isxdigit(*act
))
2004 struct collect_static_trace_data_action
*raction
;
2006 raction
= xmalloc (sizeof *raction
);
2007 raction
->base
.type
= *act
;
2008 raction
->base
.ops
= &l_tracepoint_action_ops
;
2009 action
= &raction
->base
;
2011 trace_debug ("Want to collect static trace data");
2016 trace_debug ("Unexpected step action, ignoring");
2021 struct eval_expr_action
*xaction
;
2023 xaction
= xmalloc (sizeof (*xaction
));
2024 xaction
->base
.type
= *act
;
2025 xaction
->base
.ops
= &x_tracepoint_action_ops
;
2026 action
= &xaction
->base
;
2028 trace_debug ("Want to evaluate expression");
2029 xaction
->expr
= gdb_parse_agent_expr (&act
);
2033 trace_debug ("unknown trace action '%c', ignoring...", *act
);
2042 if (seen_step_action_flag
)
2044 tpoint
->num_step_actions
++;
2046 tpoint
->step_actions
2047 = xrealloc (tpoint
->step_actions
,
2048 (sizeof (*tpoint
->step_actions
)
2049 * tpoint
->num_step_actions
));
2050 tpoint
->step_actions_str
2051 = xrealloc (tpoint
->step_actions_str
,
2052 (sizeof (*tpoint
->step_actions_str
)
2053 * tpoint
->num_step_actions
));
2054 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
2055 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
2056 = savestring (act_start
, act
- act_start
);
2060 tpoint
->numactions
++;
2062 = xrealloc (tpoint
->actions
,
2063 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
2065 = xrealloc (tpoint
->actions_str
,
2066 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
2067 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
2068 tpoint
->actions_str
[tpoint
->numactions
- 1]
2069 = savestring (act_start
, act
- act_start
);
2076 /* Find or create a trace state variable with the given number. */
2078 static struct trace_state_variable
*
2079 get_trace_state_variable (int num
)
2081 struct trace_state_variable
*tsv
;
2083 #ifdef IN_PROCESS_AGENT
2084 /* Search for an existing variable. */
2085 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
2086 if (tsv
->number
== num
)
2090 /* Search for an existing variable. */
2091 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
2092 if (tsv
->number
== num
)
2098 /* Find or create a trace state variable with the given number. */
2100 static struct trace_state_variable
*
2101 create_trace_state_variable (int num
, int gdb
)
2103 struct trace_state_variable
*tsv
;
2105 tsv
= get_trace_state_variable (num
);
2109 /* Create a new variable. */
2110 tsv
= xmalloc (sizeof (struct trace_state_variable
));
2112 tsv
->initial_value
= 0;
2116 #ifdef IN_PROCESS_AGENT
2119 tsv
->next
= alloced_trace_state_variables
;
2120 alloced_trace_state_variables
= tsv
;
2125 tsv
->next
= trace_state_variables
;
2126 trace_state_variables
= tsv
;
2131 IP_AGENT_EXPORT LONGEST
2132 get_trace_state_variable_value (int num
)
2134 struct trace_state_variable
*tsv
;
2136 tsv
= get_trace_state_variable (num
);
2140 trace_debug ("No trace state variable %d, skipping value get", num
);
2144 /* Call a getter function if we have one. While it's tempting to
2145 set up something to only call the getter once per tracepoint hit,
2146 it could run afoul of thread races. Better to let the getter
2147 handle it directly, if necessary to worry about it. */
2149 tsv
->value
= (tsv
->getter
) ();
2151 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2152 num
, plongest (tsv
->value
));
2157 IP_AGENT_EXPORT
void
2158 set_trace_state_variable_value (int num
, LONGEST val
)
2160 struct trace_state_variable
*tsv
;
2162 tsv
= get_trace_state_variable (num
);
2166 trace_debug ("No trace state variable %d, skipping value set", num
);
2174 agent_get_trace_state_variable_value (int num
)
2176 return get_trace_state_variable_value (num
);
2180 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2182 set_trace_state_variable_value (num
, val
);
2186 set_trace_state_variable_name (int num
, const char *name
)
2188 struct trace_state_variable
*tsv
;
2190 tsv
= get_trace_state_variable (num
);
2194 trace_debug ("No trace state variable %d, skipping name set", num
);
2198 tsv
->name
= (char *) name
;
2202 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2204 struct trace_state_variable
*tsv
;
2206 tsv
= get_trace_state_variable (num
);
2210 trace_debug ("No trace state variable %d, skipping getter set", num
);
2214 tsv
->getter
= getter
;
2217 /* Add a raw traceframe for the given tracepoint. */
2219 static struct traceframe
*
2220 add_traceframe (struct tracepoint
*tpoint
)
2222 struct traceframe
*tframe
;
2224 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2229 tframe
->tpnum
= tpoint
->number
;
2230 tframe
->data_size
= 0;
2235 /* Add a block to the traceframe currently being worked on. */
2237 static unsigned char *
2238 add_traceframe_block (struct traceframe
*tframe
,
2239 struct tracepoint
*tpoint
, int amt
)
2241 unsigned char *block
;
2246 block
= trace_buffer_alloc (amt
);
2251 gdb_assert (tframe
->tpnum
== tpoint
->number
);
2253 tframe
->data_size
+= amt
;
2254 tpoint
->traceframe_usage
+= amt
;
2259 /* Flag that the current traceframe is finished. */
2262 finish_traceframe (struct traceframe
*tframe
)
2264 ++traceframe_write_count
;
2265 ++traceframes_created
;
2268 #ifndef IN_PROCESS_AGENT
2270 /* Given a traceframe number NUM, find the NUMth traceframe in the
2273 static struct traceframe
*
2274 find_traceframe (int num
)
2276 struct traceframe
*tframe
;
2279 for (tframe
= FIRST_TRACEFRAME ();
2281 tframe
= NEXT_TRACEFRAME (tframe
))
2292 get_traceframe_address (struct traceframe
*tframe
)
2295 struct tracepoint
*tpoint
;
2297 addr
= traceframe_get_pc (tframe
);
2302 /* Fallback strategy, will be incorrect for while-stepping frames
2303 and multi-location tracepoints. */
2304 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2305 return tpoint
->address
;
2308 /* Search for the next traceframe whose address is inside or outside
2311 static struct traceframe
*
2312 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2315 struct traceframe
*tframe
;
2318 *tfnump
= current_traceframe
+ 1;
2319 tframe
= find_traceframe (*tfnump
);
2320 /* The search is not supposed to wrap around. */
2327 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2329 tfaddr
= get_traceframe_address (tframe
);
2331 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2332 : (lo
> tfaddr
|| tfaddr
> hi
))
2341 /* Search for the next traceframe recorded by the given tracepoint.
2342 Note that for multi-location tracepoints, this will find whatever
2343 location appears first. */
2345 static struct traceframe
*
2346 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2348 struct traceframe
*tframe
;
2350 *tfnump
= current_traceframe
+ 1;
2351 tframe
= find_traceframe (*tfnump
);
2352 /* The search is not supposed to wrap around. */
2359 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2361 if (tframe
->tpnum
== num
)
2372 #ifndef IN_PROCESS_AGENT
2374 /* Clear all past trace state. */
2377 cmd_qtinit (char *packet
)
2379 struct trace_state_variable
*tsv
, *prev
, *next
;
2381 /* Make sure we don't try to read from a trace frame. */
2382 current_traceframe
= -1;
2386 trace_debug ("Initializing the trace");
2388 clear_installed_tracepoints ();
2389 clear_readonly_regions ();
2392 last_tracepoint
= NULL
;
2394 /* Clear out any leftover trace state variables. Ones with target
2395 defined getters should be kept however. */
2397 tsv
= trace_state_variables
;
2400 trace_debug ("Looking at var %d", tsv
->number
);
2401 if (tsv
->getter
== NULL
)
2407 trace_state_variables
= next
;
2408 trace_debug ("Deleting var %d", tsv
->number
);
2419 clear_trace_buffer ();
2420 clear_inferior_trace_buffer ();
2425 /* Unprobe the UST marker at ADDRESS. */
2428 unprobe_marker_at (CORE_ADDR address
)
2430 char cmd
[IPA_CMD_BUF_SIZE
];
2432 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2433 run_inferior_command (cmd
, strlen (cmd
) + 1);
2436 /* Restore the program to its pre-tracing state. This routine may be called
2437 in error situations, so it needs to be careful about only restoring
2438 from known-valid bits. */
2441 clear_installed_tracepoints (void)
2443 struct tracepoint
*tpoint
;
2444 struct tracepoint
*prev_stpoint
;
2447 cancel_breakpoints ();
2449 prev_stpoint
= NULL
;
2451 /* Restore any bytes overwritten by tracepoints. */
2452 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2454 /* Catch the case where we might try to remove a tracepoint that
2455 was never actually installed. */
2456 if (tpoint
->handle
== NULL
)
2458 trace_debug ("Tracepoint %d at 0x%s was "
2459 "never installed, nothing to clear",
2460 tpoint
->number
, paddress (tpoint
->address
));
2464 switch (tpoint
->type
)
2466 case trap_tracepoint
:
2467 delete_breakpoint (tpoint
->handle
);
2469 case fast_tracepoint
:
2470 delete_fast_tracepoint_jump (tpoint
->handle
);
2472 case static_tracepoint
:
2473 if (prev_stpoint
!= NULL
2474 && prev_stpoint
->address
== tpoint
->address
)
2475 /* Nothing to do. We already unprobed a tracepoint set at
2476 this marker address (and there can only be one probe
2481 unprobe_marker_at (tpoint
->address
);
2482 prev_stpoint
= tpoint
;
2487 tpoint
->handle
= NULL
;
2493 /* Parse a packet that defines a tracepoint. */
2496 cmd_qtdp (char *own_buf
)
2499 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2500 int trail_hyphen
= 0;
2504 struct tracepoint
*tpoint
;
2506 char *packet
= own_buf
;
2508 packet
+= strlen ("QTDP:");
2510 /* A hyphen at the beginning marks a packet specifying actions for a
2511 tracepoint already supplied. */
2518 packet
= unpack_varlen_hex (packet
, &num
);
2519 ++packet
; /* skip a colon */
2520 packet
= unpack_varlen_hex (packet
, &addr
);
2521 ++packet
; /* skip a colon */
2523 /* See if we already have this tracepoint. */
2524 tpoint
= find_tracepoint (num
, addr
);
2528 /* Duplicate tracepoints are never allowed. */
2531 trace_debug ("Tracepoint error: tracepoint %d"
2532 " at 0x%s already exists",
2533 (int) num
, paddress (addr
));
2534 write_enn (own_buf
);
2538 tpoint
= add_tracepoint (num
, addr
);
2540 tpoint
->enabled
= (*packet
== 'E');
2541 ++packet
; /* skip 'E' */
2542 ++packet
; /* skip a colon */
2543 packet
= unpack_varlen_hex (packet
, &count
);
2544 tpoint
->step_count
= count
;
2545 ++packet
; /* skip a colon */
2546 packet
= unpack_varlen_hex (packet
, &count
);
2547 tpoint
->pass_count
= count
;
2548 /* See if we have any of the additional optional fields. */
2549 while (*packet
== ':')
2554 tpoint
->type
= fast_tracepoint
;
2556 packet
= unpack_varlen_hex (packet
, &count
);
2557 tpoint
->orig_size
= count
;
2559 else if (*packet
== 'S')
2561 tpoint
->type
= static_tracepoint
;
2564 else if (*packet
== 'X')
2566 actparm
= (char *) packet
;
2567 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2570 else if (*packet
== '-')
2572 else if (*packet
== '\0')
2575 trace_debug ("Unknown optional tracepoint field");
2580 trace_debug ("Also has actions\n");
2583 trace_debug ("Defined %stracepoint %d at 0x%s, "
2584 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2585 tpoint
->type
== fast_tracepoint
? "fast "
2586 : tpoint
->type
== static_tracepoint
? "static " : "",
2587 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2588 tpoint
->step_count
, tpoint
->pass_count
);
2591 add_tracepoint_action (tpoint
, packet
);
2594 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2595 (int) num
, paddress (addr
));
2596 write_enn (own_buf
);
2600 /* Install tracepoint during tracing only once for each tracepoint location.
2601 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2602 determine the last QTDP packet for one tracepoint location by checking
2603 trailing hyphen in QTDP packet. */
2604 if (tracing
&& !trail_hyphen
)
2606 struct tracepoint
*tp
= NULL
;
2608 /* Pause all threads temporarily while we patch tracepoints. */
2611 /* download_tracepoint will update global `tracepoints'
2612 list, so it is unsafe to leave threads in jump pad. */
2613 stabilize_threads ();
2615 /* Freeze threads. */
2619 if (tpoint
->type
!= trap_tracepoint
)
2621 /* Find another fast or static tracepoint at the same address. */
2622 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2624 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2625 && tp
->number
!= tpoint
->number
)
2629 /* TPOINT is installed at the same address as TP. */
2632 if (tpoint
->type
== fast_tracepoint
)
2633 clone_fast_tracepoint (tpoint
, tp
);
2634 else if (tpoint
->type
== static_tracepoint
)
2635 tpoint
->handle
= (void *) -1;
2639 if (use_agent
&& tpoint
->type
== fast_tracepoint
2640 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
2642 /* Download and install fast tracepoint by agent. */
2643 if (tracepoint_send_agent (tpoint
) == 0)
2647 write_enn (own_buf
);
2648 remove_tracepoint (tpoint
);
2653 download_tracepoint (tpoint
);
2655 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2657 install_tracepoint (tpoint
, own_buf
);
2658 if (strcmp (own_buf
, "OK") != 0)
2659 remove_tracepoint (tpoint
);
2673 cmd_qtdpsrc (char *own_buf
)
2675 ULONGEST num
, addr
, start
, slen
;
2676 struct tracepoint
*tpoint
;
2677 char *packet
= own_buf
;
2678 char *saved
, *srctype
, *src
;
2680 struct source_string
*last
, *newlast
;
2682 packet
+= strlen ("QTDPsrc:");
2684 packet
= unpack_varlen_hex (packet
, &num
);
2685 ++packet
; /* skip a colon */
2686 packet
= unpack_varlen_hex (packet
, &addr
);
2687 ++packet
; /* skip a colon */
2689 /* See if we already have this tracepoint. */
2690 tpoint
= find_tracepoint (num
, addr
);
2694 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2695 (int) num
, paddress (addr
));
2696 write_enn (own_buf
);
2701 packet
= strchr (packet
, ':');
2702 srctype
= xmalloc (packet
- saved
+ 1);
2703 memcpy (srctype
, saved
, packet
- saved
);
2704 srctype
[packet
- saved
] = '\0';
2706 packet
= unpack_varlen_hex (packet
, &start
);
2707 ++packet
; /* skip a colon */
2708 packet
= unpack_varlen_hex (packet
, &slen
);
2709 ++packet
; /* skip a colon */
2710 src
= xmalloc (slen
+ 1);
2711 nbytes
= hex2bin (packet
, (gdb_byte
*) src
, strlen (packet
) / 2);
2714 newlast
= xmalloc (sizeof (struct source_string
));
2715 newlast
->type
= srctype
;
2717 newlast
->next
= NULL
;
2718 /* Always add a source string to the end of the list;
2719 this keeps sequences of actions/commands in the right
2721 if (tpoint
->source_strings
)
2723 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2725 last
->next
= newlast
;
2728 tpoint
->source_strings
= newlast
;
2734 cmd_qtdv (char *own_buf
)
2736 ULONGEST num
, val
, builtin
;
2739 struct trace_state_variable
*tsv
;
2740 char *packet
= own_buf
;
2742 packet
+= strlen ("QTDV:");
2744 packet
= unpack_varlen_hex (packet
, &num
);
2745 ++packet
; /* skip a colon */
2746 packet
= unpack_varlen_hex (packet
, &val
);
2747 ++packet
; /* skip a colon */
2748 packet
= unpack_varlen_hex (packet
, &builtin
);
2749 ++packet
; /* skip a colon */
2751 nbytes
= strlen (packet
) / 2;
2752 varname
= xmalloc (nbytes
+ 1);
2753 nbytes
= hex2bin (packet
, (gdb_byte
*) varname
, nbytes
);
2754 varname
[nbytes
] = '\0';
2756 tsv
= create_trace_state_variable (num
, 1);
2757 tsv
->initial_value
= (LONGEST
) val
;
2758 tsv
->name
= varname
;
2760 set_trace_state_variable_value (num
, (LONGEST
) val
);
2766 cmd_qtenable_disable (char *own_buf
, int enable
)
2768 char *packet
= own_buf
;
2770 struct tracepoint
*tp
;
2772 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2773 packet
= unpack_varlen_hex (packet
, &num
);
2774 ++packet
; /* skip a colon */
2775 packet
= unpack_varlen_hex (packet
, &addr
);
2777 tp
= find_tracepoint (num
, addr
);
2781 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2783 trace_debug ("Tracepoint %d at 0x%s is already %s",
2784 (int) num
, paddress (addr
),
2785 enable
? "enabled" : "disabled");
2790 trace_debug ("%s tracepoint %d at 0x%s",
2791 enable
? "Enabling" : "Disabling",
2792 (int) num
, paddress (addr
));
2794 tp
->enabled
= enable
;
2796 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2799 int offset
= offsetof (struct tracepoint
, enabled
);
2800 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2802 ret
= prepare_to_access_memory ();
2805 trace_debug ("Failed to temporarily stop inferior threads");
2806 write_enn (own_buf
);
2810 ret
= write_inferior_integer (obj_addr
, enable
);
2811 done_accessing_memory ();
2815 trace_debug ("Cannot write enabled flag into "
2816 "inferior process memory");
2817 write_enn (own_buf
);
2826 trace_debug ("Tracepoint %d at 0x%s not found",
2827 (int) num
, paddress (addr
));
2828 write_enn (own_buf
);
2833 cmd_qtv (char *own_buf
)
2838 char *packet
= own_buf
;
2840 packet
+= strlen ("qTV:");
2841 unpack_varlen_hex (packet
, &num
);
2843 if (current_traceframe
>= 0)
2845 err
= traceframe_read_tsv ((int) num
, &val
);
2848 strcpy (own_buf
, "U");
2852 /* Only make tsv's be undefined before the first trace run. After a
2853 trace run is over, the user might want to see the last value of
2854 the tsv, and it might not be available in a traceframe. */
2855 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2857 strcpy (own_buf
, "U");
2861 val
= get_trace_state_variable_value (num
);
2863 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2866 /* Clear out the list of readonly regions. */
2869 clear_readonly_regions (void)
2871 struct readonly_region
*roreg
;
2873 while (readonly_regions
)
2875 roreg
= readonly_regions
;
2876 readonly_regions
= readonly_regions
->next
;
2881 /* Parse the collection of address ranges whose contents GDB believes
2882 to be unchanging and so can be read directly from target memory
2883 even while looking at a traceframe. */
2886 cmd_qtro (char *own_buf
)
2888 ULONGEST start
, end
;
2889 struct readonly_region
*roreg
;
2890 char *packet
= own_buf
;
2892 trace_debug ("Want to mark readonly regions");
2894 clear_readonly_regions ();
2896 packet
+= strlen ("QTro");
2898 while (*packet
== ':')
2900 ++packet
; /* skip a colon */
2901 packet
= unpack_varlen_hex (packet
, &start
);
2902 ++packet
; /* skip a comma */
2903 packet
= unpack_varlen_hex (packet
, &end
);
2904 roreg
= xmalloc (sizeof (struct readonly_region
));
2905 roreg
->start
= start
;
2907 roreg
->next
= readonly_regions
;
2908 readonly_regions
= roreg
;
2909 trace_debug ("Added readonly region from 0x%s to 0x%s",
2910 paddress (roreg
->start
), paddress (roreg
->end
));
2916 /* Test to see if the given range is in our list of readonly ranges.
2917 We only test for being entirely within a range, GDB is not going to
2918 send a single memory packet that spans multiple regions. */
2921 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2923 struct readonly_region
*roreg
;
2925 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2926 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2932 /* The maximum size of a jump pad entry. */
2933 static const int max_jump_pad_size
= 0x100;
2935 static CORE_ADDR gdb_jump_pad_head
;
2937 /* Return the address of the next free jump space. */
2940 get_jump_space_head (void)
2942 if (gdb_jump_pad_head
== 0)
2944 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2945 &gdb_jump_pad_head
))
2946 fatal ("error extracting jump_pad_buffer");
2949 return gdb_jump_pad_head
;
2952 /* Reserve USED bytes from the jump space. */
2955 claim_jump_space (ULONGEST used
)
2957 trace_debug ("claim_jump_space reserves %s bytes at %s",
2958 pulongest (used
), paddress (gdb_jump_pad_head
));
2959 gdb_jump_pad_head
+= used
;
2962 static CORE_ADDR trampoline_buffer_head
= 0;
2963 static CORE_ADDR trampoline_buffer_tail
;
2965 /* Reserve USED bytes from the trampoline buffer and return the
2966 address of the start of the reserved space in TRAMPOLINE. Returns
2967 non-zero if the space is successfully claimed. */
2970 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2972 if (!trampoline_buffer_head
)
2974 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2975 &trampoline_buffer_tail
))
2977 fatal ("error extracting trampoline_buffer");
2981 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2982 &trampoline_buffer_head
))
2984 fatal ("error extracting trampoline_buffer_end");
2989 /* Start claiming space from the top of the trampoline space. If
2990 the space is located at the bottom of the virtual address space,
2991 this reduces the possibility that corruption will occur if a null
2992 pointer is used to write to memory. */
2993 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2995 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
3000 trampoline_buffer_head
-= used
;
3002 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
3003 pulongest (used
), paddress (trampoline_buffer_head
));
3005 *trampoline
= trampoline_buffer_head
;
3009 /* Returns non-zero if there is space allocated for use in trampolines
3010 for fast tracepoints. */
3013 have_fast_tracepoint_trampoline_buffer (char *buf
)
3015 CORE_ADDR trampoline_end
, errbuf
;
3017 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
3020 fatal ("error extracting trampoline_buffer_end");
3027 strcpy (buf
, "was claiming");
3028 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
3031 fatal ("error extracting errbuf");
3035 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
3038 return trampoline_end
!= 0;
3041 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3042 the command fails, or 0 otherwise. If the command ran
3043 successfully, but probing the marker failed, ERROUT will be filled
3044 with the error to reply to GDB, and -1 is also returned. This
3045 allows directly passing IPA errors to GDB. */
3048 probe_marker_at (CORE_ADDR address
, char *errout
)
3050 char cmd
[IPA_CMD_BUF_SIZE
];
3053 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
3054 err
= run_inferior_command (cmd
, strlen (cmd
) + 1);
3060 strcpy (errout
, cmd
);
3069 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
3071 to
->jump_pad
= from
->jump_pad
;
3072 to
->jump_pad_end
= from
->jump_pad_end
;
3073 to
->trampoline
= from
->trampoline
;
3074 to
->trampoline_end
= from
->trampoline_end
;
3075 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
3076 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
3077 to
->handle
= from
->handle
;
3079 gdb_assert (from
->handle
);
3080 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
3083 #define MAX_JUMP_SIZE 20
3085 /* Install fast tracepoint. Return 0 if successful, otherwise return
3089 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
3091 CORE_ADDR jentry
, jump_entry
;
3092 CORE_ADDR trampoline
;
3093 ULONGEST trampoline_size
;
3095 /* The jump to the jump pad of the last fast tracepoint
3097 unsigned char fjump
[MAX_JUMP_SIZE
];
3098 ULONGEST fjump_size
;
3100 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
3102 trace_debug ("Requested a fast tracepoint on an instruction "
3103 "that is of less than the minimum length.");
3107 jentry
= jump_entry
= get_jump_space_head ();
3110 trampoline_size
= 0;
3112 /* Install the jump pad. */
3113 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3115 ipa_sym_addrs
.addr_gdb_collect
,
3116 ipa_sym_addrs
.addr_collecting
,
3119 &trampoline
, &trampoline_size
,
3121 &tpoint
->adjusted_insn_addr
,
3122 &tpoint
->adjusted_insn_addr_end
,
3129 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3132 if (tpoint
->handle
!= NULL
)
3134 tpoint
->jump_pad
= jump_entry
;
3135 tpoint
->jump_pad_end
= jentry
;
3136 tpoint
->trampoline
= trampoline
;
3137 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3139 /* Pad to 8-byte alignment. */
3140 jentry
= ((jentry
+ 7) & ~0x7);
3141 claim_jump_space (jentry
- jump_entry
);
3148 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3151 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3153 tpoint
->handle
= NULL
;
3156 if (tpoint
->type
== trap_tracepoint
)
3158 /* Tracepoints are installed as memory breakpoints. Just go
3159 ahead and install the trap. The breakpoints module
3160 handles duplicated breakpoints, and the memory read
3161 routine handles un-patching traps from memory reads. */
3162 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3163 tracepoint_handler
);
3165 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3167 if (!agent_loaded_p ())
3169 trace_debug ("Requested a %s tracepoint, but fast "
3170 "tracepoints aren't supported.",
3171 tpoint
->type
== static_tracepoint
? "static" : "fast");
3172 write_e_ipa_not_loaded (own_buf
);
3175 if (tpoint
->type
== static_tracepoint
3176 && !in_process_agent_supports_ust ())
3178 trace_debug ("Requested a static tracepoint, but static "
3179 "tracepoints are not supported.");
3180 write_e_ust_not_loaded (own_buf
);
3184 if (tpoint
->type
== fast_tracepoint
)
3185 install_fast_tracepoint (tpoint
, own_buf
);
3188 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3189 tpoint
->handle
= (void *) -1;
3194 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3196 if (tpoint
->handle
== NULL
)
3198 if (*own_buf
== '\0')
3199 write_enn (own_buf
);
3205 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3208 cmd_qtstart (char *packet
)
3210 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3211 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3213 trace_debug ("Starting the trace");
3215 /* Pause all threads temporarily while we patch tracepoints. */
3218 /* Get threads out of jump pads. Safe to do here, since this is a
3219 top level command. And, required to do here, since we're
3220 deleting/rewriting jump pads. */
3222 stabilize_threads ();
3224 /* Freeze threads. */
3227 /* Sync the fast tracepoints list in the inferior ftlib. */
3228 if (agent_loaded_p ())
3229 download_trace_state_variables ();
3231 /* No previous fast tpoint yet. */
3232 prev_ftpoint
= NULL
;
3234 /* No previous static tpoint yet. */
3235 prev_stpoint
= NULL
;
3239 /* Start out empty. */
3240 if (agent_loaded_p ())
3241 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
3243 /* Download and install tracepoints. */
3244 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3246 /* Ensure all the hit counts start at zero. */
3247 tpoint
->hit_count
= 0;
3248 tpoint
->traceframe_usage
= 0;
3250 if (tpoint
->type
== trap_tracepoint
)
3252 /* Tracepoints are installed as memory breakpoints. Just go
3253 ahead and install the trap. The breakpoints module
3254 handles duplicated breakpoints, and the memory read
3255 routine handles un-patching traps from memory reads. */
3256 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3257 tracepoint_handler
);
3259 else if (tpoint
->type
== fast_tracepoint
3260 || tpoint
->type
== static_tracepoint
)
3262 if (maybe_write_ipa_not_loaded (packet
))
3264 trace_debug ("Requested a %s tracepoint, but fast "
3265 "tracepoints aren't supported.",
3266 tpoint
->type
== static_tracepoint
3267 ? "static" : "fast");
3271 if (tpoint
->type
== fast_tracepoint
)
3274 = use_agent
&& agent_capability_check (AGENT_CAPA_FAST_TRACE
);
3276 if (prev_ftpoint
!= NULL
3277 && prev_ftpoint
->address
== tpoint
->address
)
3280 tracepoint_send_agent (tpoint
);
3282 download_tracepoint_1 (tpoint
);
3284 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3288 /* Tracepoint is installed successfully? */
3291 /* Download and install fast tracepoint by agent. */
3293 installed
= !tracepoint_send_agent (tpoint
);
3296 download_tracepoint_1 (tpoint
);
3297 installed
= !install_fast_tracepoint (tpoint
, packet
);
3301 prev_ftpoint
= tpoint
;
3306 if (!in_process_agent_supports_ust ())
3308 trace_debug ("Requested a static tracepoint, but static "
3309 "tracepoints are not supported.");
3313 download_tracepoint_1 (tpoint
);
3314 /* Can only probe a given marker once. */
3315 if (prev_stpoint
!= NULL
3316 && prev_stpoint
->address
== tpoint
->address
)
3317 tpoint
->handle
= (void *) -1;
3320 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3322 tpoint
->handle
= (void *) -1;
3324 /* So that we can handle multiple static tracepoints
3325 at the same address easily. */
3326 prev_stpoint
= tpoint
;
3332 tpptr
= tpoint
->obj_addr_on_target
;
3334 if (tpoint
== tracepoints
)
3335 /* First object in list, set the head pointer in the
3337 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3339 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
3344 /* Any failure in the inner loop is sufficient cause to give
3346 if (tpoint
->handle
== NULL
)
3350 /* Any error in tracepoint insertion is unacceptable; better to
3351 address the problem now, than end up with a useless or misleading
3355 clear_installed_tracepoints ();
3356 if (*packet
== '\0')
3362 stopping_tracepoint
= NULL
;
3363 trace_buffer_is_full
= 0;
3364 expr_eval_result
= expr_eval_no_error
;
3365 error_tracepoint
= NULL
;
3366 tracing_start_time
= get_timestamp ();
3368 /* Tracing is now active, hits will now start being logged. */
3371 if (agent_loaded_p ())
3373 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3374 fatal ("Error setting tracing variable in lib");
3376 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3378 fatal ("Error clearing stopping_tracepoint variable in lib");
3380 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3381 fatal ("Error clearing trace_buffer_is_full variable in lib");
3383 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3384 stop_tracing_handler
);
3385 if (stop_tracing_bkpt
== NULL
)
3386 error ("Error setting stop_tracing breakpoint");
3388 flush_trace_buffer_bkpt
3389 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3390 flush_trace_buffer_handler
);
3391 if (flush_trace_buffer_bkpt
== NULL
)
3392 error ("Error setting flush_trace_buffer breakpoint");
3400 /* End a tracing run, filling in a stop reason to report back to GDB,
3401 and removing the tracepoints from the code. */
3408 trace_debug ("Tracing is already off, ignoring");
3412 trace_debug ("Stopping the trace");
3414 /* Pause all threads before removing fast jumps from memory,
3415 breakpoints, and touching IPA state variables (inferior memory).
3416 Some thread may hit the internal tracing breakpoints, or be
3417 collecting this moment, but that's ok, we don't release the
3418 tpoint object's memory or the jump pads here (we only do that
3419 when we're sure we can move all threads out of the jump pads).
3420 We can't now, since we may be getting here due to the inferior
3421 agent calling us. */
3423 /* Since we're removing breakpoints, cancel breakpoint hits,
3424 possibly related to the breakpoints we're about to delete. */
3425 cancel_breakpoints ();
3427 /* Stop logging. Tracepoints can still be hit, but they will not be
3430 if (agent_loaded_p ())
3432 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3433 fatal ("Error clearing tracing variable in lib");
3436 tracing_stop_time
= get_timestamp ();
3437 tracing_stop_reason
= "t???";
3438 tracing_stop_tpnum
= 0;
3439 if (stopping_tracepoint
)
3441 trace_debug ("Stopping the trace because "
3442 "tracepoint %d was hit %" PRIu64
" times",
3443 stopping_tracepoint
->number
,
3444 stopping_tracepoint
->pass_count
);
3445 tracing_stop_reason
= "tpasscount";
3446 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3448 else if (trace_buffer_is_full
)
3450 trace_debug ("Stopping the trace because the trace buffer is full");
3451 tracing_stop_reason
= "tfull";
3453 else if (expr_eval_result
!= expr_eval_no_error
)
3455 trace_debug ("Stopping the trace because of an expression eval error");
3456 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3457 tracing_stop_tpnum
= error_tracepoint
->number
;
3459 #ifndef IN_PROCESS_AGENT
3460 else if (!gdb_connected ())
3462 trace_debug ("Stopping the trace because GDB disconnected");
3463 tracing_stop_reason
= "tdisconnected";
3468 trace_debug ("Stopping the trace because of a tstop command");
3469 tracing_stop_reason
= "tstop";
3472 stopping_tracepoint
= NULL
;
3473 error_tracepoint
= NULL
;
3475 /* Clear out the tracepoints. */
3476 clear_installed_tracepoints ();
3478 if (agent_loaded_p ())
3480 /* Pull in fast tracepoint trace frames from the inferior lib
3481 buffer into our buffer, even if our buffer is already full,
3482 because we want to present the full number of created frames
3483 in addition to what fit in the trace buffer. */
3484 upload_fast_traceframes ();
3487 if (stop_tracing_bkpt
!= NULL
)
3489 delete_breakpoint (stop_tracing_bkpt
);
3490 stop_tracing_bkpt
= NULL
;
3493 if (flush_trace_buffer_bkpt
!= NULL
)
3495 delete_breakpoint (flush_trace_buffer_bkpt
);
3496 flush_trace_buffer_bkpt
= NULL
;
3503 stop_tracing_handler (CORE_ADDR addr
)
3505 trace_debug ("lib hit stop_tracing");
3507 /* Don't actually handle it here. When we stop tracing we remove
3508 breakpoints from the inferior, and that is not allowed in a
3509 breakpoint handler (as the caller is walking the breakpoint
3515 flush_trace_buffer_handler (CORE_ADDR addr
)
3517 trace_debug ("lib hit flush_trace_buffer");
3522 cmd_qtstop (char *packet
)
3529 cmd_qtdisconnected (char *own_buf
)
3532 char *packet
= own_buf
;
3534 packet
+= strlen ("QTDisconnected:");
3536 unpack_varlen_hex (packet
, &setting
);
3540 disconnected_tracing
= setting
;
3544 cmd_qtframe (char *own_buf
)
3546 ULONGEST frame
, pc
, lo
, hi
, num
;
3548 struct traceframe
*tframe
;
3549 char *packet
= own_buf
;
3551 packet
+= strlen ("QTFrame:");
3553 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3555 packet
+= strlen ("pc:");
3556 unpack_varlen_hex (packet
, &pc
);
3557 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3558 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3560 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3562 packet
+= strlen ("range:");
3563 packet
= unpack_varlen_hex (packet
, &lo
);
3565 unpack_varlen_hex (packet
, &hi
);
3566 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3567 paddress (lo
), paddress (hi
));
3568 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3570 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3572 packet
+= strlen ("outside:");
3573 packet
= unpack_varlen_hex (packet
, &lo
);
3575 unpack_varlen_hex (packet
, &hi
);
3576 trace_debug ("Want to find next traceframe "
3577 "outside the range 0x%s to 0x%s",
3578 paddress (lo
), paddress (hi
));
3579 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3581 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3583 packet
+= strlen ("tdp:");
3584 unpack_varlen_hex (packet
, &num
);
3586 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3587 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3591 unpack_varlen_hex (packet
, &frame
);
3592 tfnum
= (int) frame
;
3595 trace_debug ("Want to stop looking at traceframes");
3596 current_traceframe
= -1;
3600 trace_debug ("Want to look at traceframe %d", tfnum
);
3601 tframe
= find_traceframe (tfnum
);
3606 current_traceframe
= tfnum
;
3607 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3610 sprintf (own_buf
, "F-1");
3614 cmd_qtstatus (char *packet
)
3616 char *stop_reason_rsp
= NULL
;
3617 char *buf1
, *buf2
, *buf3
, *str
;
3620 /* Translate the plain text of the notes back into hex for
3623 str
= (tracing_user_name
? tracing_user_name
: "");
3624 slen
= strlen (str
);
3625 buf1
= (char *) alloca (slen
* 2 + 1);
3626 bin2hex ((gdb_byte
*) str
, buf1
, slen
);
3628 str
= (tracing_notes
? tracing_notes
: "");
3629 slen
= strlen (str
);
3630 buf2
= (char *) alloca (slen
* 2 + 1);
3631 bin2hex ((gdb_byte
*) str
, buf2
, slen
);
3633 str
= (tracing_stop_note
? tracing_stop_note
: "");
3634 slen
= strlen (str
);
3635 buf3
= (char *) alloca (slen
* 2 + 1);
3636 bin2hex ((gdb_byte
*) str
, buf3
, slen
);
3638 trace_debug ("Returning trace status as %d, stop reason %s",
3639 tracing
, tracing_stop_reason
);
3641 if (agent_loaded_p ())
3645 upload_fast_traceframes ();
3650 stop_reason_rsp
= (char *) tracing_stop_reason
;
3652 /* The user visible error string in terror needs to be hex encoded.
3653 We leave it as plain string in `tracing_stop_reason' to ease
3655 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3657 const char *result_name
;
3661 result_name
= stop_reason_rsp
+ strlen ("terror:");
3662 hexstr_len
= strlen (result_name
) * 2;
3663 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3664 strcpy (p
, "terror:");
3666 bin2hex ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3669 /* If this was a forced stop, include any stop note that was supplied. */
3670 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3672 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3673 strcpy (stop_reason_rsp
, "tstop:");
3674 strcat (stop_reason_rsp
, buf3
);
3680 "tframes:%x;tcreated:%x;"
3681 "tfree:%x;tsize:%s;"
3684 "starttime:%s;stoptime:%s;"
3685 "username:%s;notes:%s:",
3687 stop_reason_rsp
, tracing_stop_tpnum
,
3688 traceframe_count
, traceframes_created
,
3689 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3690 circular_trace_buffer
,
3691 disconnected_tracing
,
3692 phex_nz (tracing_start_time
, sizeof (tracing_start_time
)),
3693 phex_nz (tracing_stop_time
, sizeof (tracing_stop_time
)),
3698 cmd_qtp (char *own_buf
)
3701 struct tracepoint
*tpoint
;
3702 char *packet
= own_buf
;
3704 packet
+= strlen ("qTP:");
3706 packet
= unpack_varlen_hex (packet
, &num
);
3707 ++packet
; /* skip a colon */
3708 packet
= unpack_varlen_hex (packet
, &addr
);
3710 /* See if we already have this tracepoint. */
3711 tpoint
= find_tracepoint (num
, addr
);
3715 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3716 (int) num
, paddress (addr
));
3717 write_enn (own_buf
);
3721 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3722 tpoint
->traceframe_usage
);
3725 /* State variables to help return all the tracepoint bits. */
3726 static struct tracepoint
*cur_tpoint
;
3727 static unsigned int cur_action
;
3728 static unsigned int cur_step_action
;
3729 static struct source_string
*cur_source_string
;
3730 static struct trace_state_variable
*cur_tsv
;
3732 /* Compose a response that is an imitation of the syntax by which the
3733 tracepoint was originally downloaded. */
3736 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3740 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3741 paddress (tpoint
->address
),
3742 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3743 tpoint
->pass_count
);
3744 if (tpoint
->type
== fast_tracepoint
)
3745 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3746 else if (tpoint
->type
== static_tracepoint
)
3747 sprintf (packet
+ strlen (packet
), ":S");
3751 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3752 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3753 tpoint
->cond
->length
, buf
);
3758 /* Compose a response that is an imitation of the syntax by which the
3759 tracepoint action was originally downloaded (with the difference
3760 that due to the way we store the actions, this will output a packet
3761 per action, while GDB could have combined more than one action
3765 response_action (char *packet
, struct tracepoint
*tpoint
,
3766 char *taction
, int step
)
3768 sprintf (packet
, "%c%x:%s:%s",
3769 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3773 /* Compose a response that is an imitation of the syntax by which the
3774 tracepoint source piece was originally downloaded. */
3777 response_source (char *packet
,
3778 struct tracepoint
*tpoint
, struct source_string
*src
)
3783 len
= strlen (src
->str
);
3784 buf
= alloca (len
* 2 + 1);
3785 bin2hex ((gdb_byte
*) src
->str
, buf
, len
);
3787 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3788 tpoint
->number
, paddress (tpoint
->address
),
3789 src
->type
, 0, len
, buf
);
3792 /* Return the first piece of tracepoint definition, and initialize the
3793 state machine that will iterate through all the tracepoint
3797 cmd_qtfp (char *packet
)
3799 trace_debug ("Returning first tracepoint definition piece");
3801 cur_tpoint
= tracepoints
;
3802 cur_action
= cur_step_action
= 0;
3803 cur_source_string
= NULL
;
3806 response_tracepoint (packet
, cur_tpoint
);
3808 strcpy (packet
, "l");
3811 /* Return additional pieces of tracepoint definition. Each action and
3812 stepping action must go into its own packet, because of packet size
3813 limits, and so we use state variables to deliver one piece at a
3817 cmd_qtsp (char *packet
)
3819 trace_debug ("Returning subsequent tracepoint definition piece");
3823 /* This case would normally never occur, but be prepared for
3825 strcpy (packet
, "l");
3827 else if (cur_action
< cur_tpoint
->numactions
)
3829 response_action (packet
, cur_tpoint
,
3830 cur_tpoint
->actions_str
[cur_action
], 0);
3833 else if (cur_step_action
< cur_tpoint
->num_step_actions
)
3835 response_action (packet
, cur_tpoint
,
3836 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3839 else if ((cur_source_string
3840 ? cur_source_string
->next
3841 : cur_tpoint
->source_strings
))
3843 if (cur_source_string
)
3844 cur_source_string
= cur_source_string
->next
;
3846 cur_source_string
= cur_tpoint
->source_strings
;
3847 response_source (packet
, cur_tpoint
, cur_source_string
);
3851 cur_tpoint
= cur_tpoint
->next
;
3852 cur_action
= cur_step_action
= 0;
3853 cur_source_string
= NULL
;
3855 response_tracepoint (packet
, cur_tpoint
);
3857 strcpy (packet
, "l");
3861 /* Compose a response that is an imitation of the syntax by which the
3862 trace state variable was originally downloaded. */
3865 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3867 char *buf
= (char *) "";
3872 namelen
= strlen (tsv
->name
);
3873 buf
= alloca (namelen
* 2 + 1);
3874 bin2hex ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3877 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3878 tsv
->getter
? 1 : 0, buf
);
3881 /* Return the first trace state variable definition, and initialize
3882 the state machine that will iterate through all the tsv bits. */
3885 cmd_qtfv (char *packet
)
3887 trace_debug ("Returning first trace state variable definition");
3889 cur_tsv
= trace_state_variables
;
3892 response_tsv (packet
, cur_tsv
);
3894 strcpy (packet
, "l");
3897 /* Return additional trace state variable definitions. */
3900 cmd_qtsv (char *packet
)
3902 trace_debug ("Returning additional trace state variable definition");
3906 cur_tsv
= cur_tsv
->next
;
3908 response_tsv (packet
, cur_tsv
);
3910 strcpy (packet
, "l");
3913 strcpy (packet
, "l");
3916 /* Return the first static tracepoint marker, and initialize the state
3917 machine that will iterate through all the static tracepoints
3921 cmd_qtfstm (char *packet
)
3923 if (!maybe_write_ipa_ust_not_loaded (packet
))
3924 run_inferior_command (packet
, strlen (packet
) + 1);
3927 /* Return additional static tracepoints markers. */
3930 cmd_qtsstm (char *packet
)
3932 if (!maybe_write_ipa_ust_not_loaded (packet
))
3933 run_inferior_command (packet
, strlen (packet
) + 1);
3936 /* Return the definition of the static tracepoint at a given address.
3937 Result packet is the same as qTsST's. */
3940 cmd_qtstmat (char *packet
)
3942 if (!maybe_write_ipa_ust_not_loaded (packet
))
3943 run_inferior_command (packet
, strlen (packet
) + 1);
3946 /* Helper for gdb_agent_about_to_close.
3947 Return non-zero if thread ENTRY is in the same process in DATA. */
3950 same_process_p (struct inferior_list_entry
*entry
, void *data
)
3954 return ptid_get_pid (entry
->id
) == *pid
;
3957 /* Sent the agent a command to close it. */
3960 gdb_agent_about_to_close (int pid
)
3962 char buf
[IPA_CMD_BUF_SIZE
];
3964 if (!maybe_write_ipa_not_loaded (buf
))
3966 struct thread_info
*save_inferior
;
3968 save_inferior
= current_inferior
;
3970 /* Find any thread which belongs to process PID. */
3971 current_inferior
= (struct thread_info
*)
3972 find_inferior (&all_threads
, same_process_p
, &pid
);
3974 strcpy (buf
, "close");
3976 run_inferior_command (buf
, strlen (buf
) + 1);
3978 current_inferior
= save_inferior
;
3982 /* Return the minimum instruction size needed for fast tracepoints as a
3983 hexadecimal number. */
3986 cmd_qtminftpilen (char *packet
)
3988 if (current_inferior
== NULL
)
3990 /* Indicate that the minimum length is currently unknown. */
3991 strcpy (packet
, "0");
3995 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3998 /* Respond to qTBuffer packet with a block of raw data from the trace
3999 buffer. GDB may ask for a lot, but we are allowed to reply with
4000 only as much as will fit within packet limits or whatever. */
4003 cmd_qtbuffer (char *own_buf
)
4005 ULONGEST offset
, num
, tot
;
4007 char *packet
= own_buf
;
4009 packet
+= strlen ("qTBuffer:");
4011 packet
= unpack_varlen_hex (packet
, &offset
);
4012 ++packet
; /* skip a comma */
4013 unpack_varlen_hex (packet
, &num
);
4015 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
4016 (int) num
, phex_nz (offset
, 0));
4018 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
4020 /* If we're right at the end, reply specially that we're done. */
4023 strcpy (own_buf
, "l");
4027 /* Object to any other out-of-bounds request. */
4030 write_enn (own_buf
);
4034 /* Compute the pointer corresponding to the given offset, accounting
4036 tbp
= trace_buffer_start
+ offset
;
4037 if (tbp
>= trace_buffer_wrap
)
4038 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
4040 /* Trim to the remaining bytes if we're close to the end. */
4041 if (num
> tot
- offset
)
4044 /* Trim to available packet size. */
4045 if (num
>= (PBUFSIZ
- 16) / 2 )
4046 num
= (PBUFSIZ
- 16) / 2;
4048 bin2hex (tbp
, own_buf
, num
);
4052 cmd_bigqtbuffer_circular (char *own_buf
)
4055 char *packet
= own_buf
;
4057 packet
+= strlen ("QTBuffer:circular:");
4059 unpack_varlen_hex (packet
, &val
);
4060 circular_trace_buffer
= val
;
4061 trace_debug ("Trace buffer is now %s",
4062 circular_trace_buffer
? "circular" : "linear");
4067 cmd_bigqtbuffer_size (char *own_buf
)
4071 char *packet
= own_buf
;
4073 /* Can't change the size during a tracing run. */
4076 write_enn (own_buf
);
4080 packet
+= strlen ("QTBuffer:size:");
4082 /* -1 is sent as literal "-1". */
4083 if (strcmp (packet
, "-1") == 0)
4084 sval
= DEFAULT_TRACE_BUFFER_SIZE
;
4087 unpack_varlen_hex (packet
, &val
);
4088 sval
= (LONGEST
) val
;
4091 init_trace_buffer (sval
);
4092 trace_debug ("Trace buffer is now %s bytes",
4093 plongest (trace_buffer_size
));
4098 cmd_qtnotes (char *own_buf
)
4101 char *saved
, *user
, *notes
, *stopnote
;
4102 char *packet
= own_buf
;
4104 packet
+= strlen ("QTNotes:");
4108 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
4110 packet
+= strlen ("user:");
4112 packet
= strchr (packet
, ';');
4113 nbytes
= (packet
- saved
) / 2;
4114 user
= xmalloc (nbytes
+ 1);
4115 nbytes
= hex2bin (saved
, (gdb_byte
*) user
, nbytes
);
4116 user
[nbytes
] = '\0';
4117 ++packet
; /* skip the semicolon */
4118 trace_debug ("User is '%s'", user
);
4119 xfree (tracing_user_name
);
4120 tracing_user_name
= user
;
4122 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
4124 packet
+= strlen ("notes:");
4126 packet
= strchr (packet
, ';');
4127 nbytes
= (packet
- saved
) / 2;
4128 notes
= xmalloc (nbytes
+ 1);
4129 nbytes
= hex2bin (saved
, (gdb_byte
*) notes
, nbytes
);
4130 notes
[nbytes
] = '\0';
4131 ++packet
; /* skip the semicolon */
4132 trace_debug ("Notes is '%s'", notes
);
4133 xfree (tracing_notes
);
4134 tracing_notes
= notes
;
4136 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
4138 packet
+= strlen ("tstop:");
4140 packet
= strchr (packet
, ';');
4141 nbytes
= (packet
- saved
) / 2;
4142 stopnote
= xmalloc (nbytes
+ 1);
4143 nbytes
= hex2bin (saved
, (gdb_byte
*) stopnote
, nbytes
);
4144 stopnote
[nbytes
] = '\0';
4145 ++packet
; /* skip the semicolon */
4146 trace_debug ("tstop note is '%s'", stopnote
);
4147 xfree (tracing_stop_note
);
4148 tracing_stop_note
= stopnote
;
4158 handle_tracepoint_general_set (char *packet
)
4160 if (strcmp ("QTinit", packet
) == 0)
4162 cmd_qtinit (packet
);
4165 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
4170 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
4172 cmd_qtdpsrc (packet
);
4175 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
4177 cmd_qtenable_disable (packet
, 1);
4180 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
4182 cmd_qtenable_disable (packet
, 0);
4185 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
4190 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
4195 else if (strcmp ("QTStart", packet
) == 0)
4197 cmd_qtstart (packet
);
4200 else if (strcmp ("QTStop", packet
) == 0)
4202 cmd_qtstop (packet
);
4205 else if (strncmp ("QTDisconnected:", packet
,
4206 strlen ("QTDisconnected:")) == 0)
4208 cmd_qtdisconnected (packet
);
4211 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
4213 cmd_qtframe (packet
);
4216 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
4218 cmd_bigqtbuffer_circular (packet
);
4221 else if (strncmp ("QTBuffer:size:", packet
, strlen ("QTBuffer:size:")) == 0)
4223 cmd_bigqtbuffer_size (packet
);
4226 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
4228 cmd_qtnotes (packet
);
4236 handle_tracepoint_query (char *packet
)
4238 if (strcmp ("qTStatus", packet
) == 0)
4240 cmd_qtstatus (packet
);
4243 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4248 else if (strcmp ("qTfP", packet
) == 0)
4253 else if (strcmp ("qTsP", packet
) == 0)
4258 else if (strcmp ("qTfV", packet
) == 0)
4263 else if (strcmp ("qTsV", packet
) == 0)
4268 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4273 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4275 cmd_qtbuffer (packet
);
4278 else if (strcmp ("qTfSTM", packet
) == 0)
4280 cmd_qtfstm (packet
);
4283 else if (strcmp ("qTsSTM", packet
) == 0)
4285 cmd_qtsstm (packet
);
4288 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4290 cmd_qtstmat (packet
);
4293 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4295 cmd_qtminftpilen (packet
);
4303 #ifndef IN_PROCESS_AGENT
4305 /* Call this when thread TINFO has hit the tracepoint defined by
4306 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4307 action. This adds a while-stepping collecting state item to the
4308 threads' collecting state list, so that we can keep track of
4309 multiple simultaneous while-stepping actions being collected by the
4310 same thread. This can happen in cases like:
4312 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4314 ff0003 INSN3 <-- TP2, collect $regs
4315 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4318 Notice that when instruction INSN5 is reached, the while-stepping
4319 actions of both TP1 and TP3 are still being collected, and that TP2
4320 had been collected meanwhile. The whole range of ff0001-ff0005
4321 should be single-stepped, due to at least TP1's while-stepping
4322 action covering the whole range. */
4325 add_while_stepping_state (struct thread_info
*tinfo
,
4326 int tp_number
, CORE_ADDR tp_address
)
4328 struct wstep_state
*wstep
;
4330 wstep
= xmalloc (sizeof (*wstep
));
4331 wstep
->next
= tinfo
->while_stepping
;
4333 wstep
->tp_number
= tp_number
;
4334 wstep
->tp_address
= tp_address
;
4335 wstep
->current_step
= 0;
4337 tinfo
->while_stepping
= wstep
;
4340 /* Release the while-stepping collecting state WSTEP. */
4343 release_while_stepping_state (struct wstep_state
*wstep
)
4348 /* Release all while-stepping collecting states currently associated
4349 with thread TINFO. */
4352 release_while_stepping_state_list (struct thread_info
*tinfo
)
4354 struct wstep_state
*head
;
4356 while (tinfo
->while_stepping
)
4358 head
= tinfo
->while_stepping
;
4359 tinfo
->while_stepping
= head
->next
;
4360 release_while_stepping_state (head
);
4364 /* If TINFO was handling a 'while-stepping' action, the step has
4365 finished, so collect any step data needed, and check if any more
4366 steps are required. Return true if the thread was indeed
4367 collecting tracepoint data, false otherwise. */
4370 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4372 struct tracepoint
*tpoint
;
4373 struct wstep_state
*wstep
;
4374 struct wstep_state
**wstep_link
;
4375 struct trap_tracepoint_ctx ctx
;
4377 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4379 if (agent_loaded_p ())
4380 upload_fast_traceframes ();
4382 /* Check if we were indeed collecting data for one of more
4383 tracepoints with a 'while-stepping' count. */
4384 if (tinfo
->while_stepping
== NULL
)
4389 /* We're not even tracing anymore. Stop this thread from
4391 release_while_stepping_state_list (tinfo
);
4393 /* The thread had stopped due to a single-step request indeed
4394 explained by a tracepoint. */
4398 wstep
= tinfo
->while_stepping
;
4399 wstep_link
= &tinfo
->while_stepping
;
4401 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4402 target_pid_to_str (tinfo
->entry
.id
),
4403 wstep
->tp_number
, paddress (wstep
->tp_address
));
4405 ctx
.base
.type
= trap_tracepoint
;
4406 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4408 while (wstep
!= NULL
)
4410 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4413 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4414 wstep
->tp_number
, paddress (wstep
->tp_address
),
4415 target_pid_to_str (tinfo
->entry
.id
));
4418 *wstep_link
= wstep
->next
;
4419 release_while_stepping_state (wstep
);
4420 wstep
= *wstep_link
;
4424 /* We've just finished one step. */
4425 ++wstep
->current_step
;
4428 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4429 stop_pc
, tpoint
, wstep
->current_step
);
4431 if (wstep
->current_step
>= tpoint
->step_count
)
4433 /* The requested numbers of steps have occurred. */
4434 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4435 target_pid_to_str (tinfo
->entry
.id
),
4436 wstep
->tp_number
, paddress (wstep
->tp_address
));
4438 /* Unlink the wstep. */
4439 *wstep_link
= wstep
->next
;
4440 release_while_stepping_state (wstep
);
4441 wstep
= *wstep_link
;
4443 /* Only check the hit count now, which ensure that we do all
4444 our stepping before stopping the run. */
4445 if (tpoint
->pass_count
> 0
4446 && tpoint
->hit_count
>= tpoint
->pass_count
4447 && stopping_tracepoint
== NULL
)
4448 stopping_tracepoint
= tpoint
;
4452 /* Keep single-stepping until the requested numbers of steps
4454 wstep_link
= &wstep
->next
;
4455 wstep
= *wstep_link
;
4458 if (stopping_tracepoint
4459 || trace_buffer_is_full
4460 || expr_eval_result
!= expr_eval_no_error
)
4470 /* Handle any internal tracing control breakpoint hits. That means,
4471 pull traceframes from the IPA to our buffer, and syncing both
4472 tracing agents when the IPA's tracing stops for some reason. */
4475 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4477 /* Pull in fast tracepoint trace frames from the inferior in-process
4478 agent's buffer into our buffer. */
4480 if (!agent_loaded_p ())
4483 upload_fast_traceframes ();
4485 /* Check if the in-process agent had decided we should stop
4487 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4489 int ipa_trace_buffer_is_full
;
4490 CORE_ADDR ipa_stopping_tracepoint
;
4491 int ipa_expr_eval_result
;
4492 CORE_ADDR ipa_error_tracepoint
;
4494 trace_debug ("lib stopped at stop_tracing");
4496 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4497 &ipa_trace_buffer_is_full
);
4499 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4500 &ipa_stopping_tracepoint
);
4501 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4503 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4504 &ipa_error_tracepoint
);
4505 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4507 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4508 &ipa_expr_eval_result
);
4509 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4511 trace_debug ("lib: trace_buffer_is_full: %d, "
4512 "stopping_tracepoint: %s, "
4513 "ipa_expr_eval_result: %d, "
4514 "error_tracepoint: %s, ",
4515 ipa_trace_buffer_is_full
,
4516 paddress (ipa_stopping_tracepoint
),
4517 ipa_expr_eval_result
,
4518 paddress (ipa_error_tracepoint
));
4522 if (ipa_trace_buffer_is_full
)
4523 trace_debug ("lib stopped due to full buffer.");
4524 if (ipa_stopping_tracepoint
)
4525 trace_debug ("lib stopped due to tpoint");
4526 if (ipa_stopping_tracepoint
)
4527 trace_debug ("lib stopped due to error");
4530 if (ipa_stopping_tracepoint
!= 0)
4533 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4535 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4537 expr_eval_result
= ipa_expr_eval_result
;
4539 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4544 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4546 trace_debug ("lib stopped at flush_trace_buffer");
4553 /* Return true if TINFO just hit a tracepoint. Collect data if
4557 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4559 struct tracepoint
*tpoint
;
4561 struct trap_tracepoint_ctx ctx
;
4563 /* Not tracing, don't handle. */
4567 ctx
.base
.type
= trap_tracepoint
;
4568 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4570 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4572 /* Note that we collect fast tracepoints here as well. We'll
4573 step over the fast tracepoint jump later, which avoids the
4574 double collect. However, we don't collect for static
4575 tracepoints here, because UST markers are compiled in program,
4576 and probes will be executed in program. So static tracepoints
4577 are collected there. */
4578 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4579 && tpoint
->type
!= static_tracepoint
)
4581 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4582 target_pid_to_str (tinfo
->entry
.id
),
4583 tpoint
->number
, paddress (tpoint
->address
));
4585 /* Test the condition if present, and collect if true. */
4587 || (condition_true_at_tracepoint
4588 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4589 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4592 if (stopping_tracepoint
4593 || trace_buffer_is_full
4594 || expr_eval_result
!= expr_eval_no_error
)
4598 /* If the tracepoint had a 'while-stepping' action, then set
4599 the thread to collect this tracepoint on the following
4601 else if (tpoint
->step_count
> 0)
4603 add_while_stepping_state (tinfo
,
4604 tpoint
->number
, tpoint
->address
);
4616 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4617 struct ust_marker_data
;
4618 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4619 struct traceframe
*tframe
);
4622 /* Create a trace frame for the hit of the given tracepoint in the
4626 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4627 struct tracepoint
*tpoint
)
4629 struct traceframe
*tframe
;
4632 /* Only count it as a hit when we actually collect data. */
4633 tpoint
->hit_count
++;
4635 /* If we've exceeded a defined pass count, record the event for
4636 later, and finish the collection for this hit. This test is only
4637 for nonstepping tracepoints, stepping tracepoints test at the end
4638 of their while-stepping loop. */
4639 if (tpoint
->pass_count
> 0
4640 && tpoint
->hit_count
>= tpoint
->pass_count
4641 && tpoint
->step_count
== 0
4642 && stopping_tracepoint
== NULL
)
4643 stopping_tracepoint
= tpoint
;
4645 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4646 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4648 tframe
= add_traceframe (tpoint
);
4652 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4654 #ifndef IN_PROCESS_AGENT
4655 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4656 tpoint
->number
, paddress (tpoint
->address
),
4657 tpoint
->actions_str
[acti
]);
4660 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4661 tpoint
->actions
[acti
]);
4664 finish_traceframe (tframe
);
4667 if (tframe
== NULL
&& tracing
)
4668 trace_buffer_is_full
= 1;
4671 #ifndef IN_PROCESS_AGENT
4674 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4676 struct tracepoint
*tpoint
, int current_step
)
4678 struct traceframe
*tframe
;
4681 trace_debug ("Making new step traceframe for "
4682 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4683 tpoint
->number
, paddress (tpoint
->address
),
4684 current_step
, tpoint
->step_count
,
4687 tframe
= add_traceframe (tpoint
);
4691 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4693 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4694 tpoint
->number
, paddress (tpoint
->address
),
4695 tpoint
->step_actions_str
[acti
]);
4697 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4698 tpoint
->step_actions
[acti
]);
4701 finish_traceframe (tframe
);
4704 if (tframe
== NULL
&& tracing
)
4705 trace_buffer_is_full
= 1;
4710 #ifdef IN_PROCESS_AGENT
4711 /* The target description used by the IPA. Given that the IPA library
4712 is built for a specific architecture that is loaded into the
4713 inferior, there only needs to be one such description per
4715 const struct target_desc
*ipa_tdesc
;
4718 static struct regcache
*
4719 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4721 struct regcache
*regcache
= NULL
;
4723 #ifdef IN_PROCESS_AGENT
4724 if (ctx
->type
== fast_tracepoint
)
4726 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4727 if (!fctx
->regcache_initted
)
4729 fctx
->regcache_initted
= 1;
4730 init_register_cache (&fctx
->regcache
, ipa_tdesc
, fctx
->regspace
);
4731 supply_regblock (&fctx
->regcache
, NULL
);
4732 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4734 regcache
= &fctx
->regcache
;
4737 if (ctx
->type
== static_tracepoint
)
4739 struct static_tracepoint_ctx
*sctx
4740 = (struct static_tracepoint_ctx
*) ctx
;
4742 if (!sctx
->regcache_initted
)
4744 sctx
->regcache_initted
= 1;
4745 init_register_cache (&sctx
->regcache
, ipa_tdesc
, sctx
->regspace
);
4746 supply_regblock (&sctx
->regcache
, NULL
);
4747 /* Pass down the tracepoint address, because REGS doesn't
4748 include the PC, but we know what it must have been. */
4749 supply_static_tracepoint_registers (&sctx
->regcache
,
4750 (const unsigned char *)
4752 sctx
->tpoint
->address
);
4754 regcache
= &sctx
->regcache
;
4758 if (ctx
->type
== trap_tracepoint
)
4760 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4761 regcache
= tctx
->regcache
;
4765 gdb_assert (regcache
!= NULL
);
4771 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4773 struct tracepoint
*tpoint
,
4774 struct traceframe
*tframe
,
4775 struct tracepoint_action
*taction
)
4777 enum eval_result_type err
;
4779 switch (taction
->type
)
4783 struct collect_memory_action
*maction
;
4784 struct eval_agent_expr_context ax_ctx
;
4786 maction
= (struct collect_memory_action
*) taction
;
4787 ax_ctx
.regcache
= NULL
;
4788 ax_ctx
.tframe
= tframe
;
4789 ax_ctx
.tpoint
= tpoint
;
4791 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4792 pulongest (maction
->len
),
4793 paddress (maction
->addr
), maction
->basereg
);
4794 /* (should use basereg) */
4795 agent_mem_read (&ax_ctx
, NULL
, (CORE_ADDR
) maction
->addr
,
4801 unsigned char *regspace
;
4802 struct regcache tregcache
;
4803 struct regcache
*context_regcache
;
4806 trace_debug ("Want to collect registers");
4808 context_regcache
= get_context_regcache (ctx
);
4809 regcache_size
= register_cache_size (context_regcache
->tdesc
);
4811 /* Collect all registers for now. */
4812 regspace
= add_traceframe_block (tframe
, tpoint
, 1 + regcache_size
);
4813 if (regspace
== NULL
)
4815 trace_debug ("Trace buffer block allocation failed, skipping");
4818 /* Identify a register block. */
4821 /* Wrap the regblock in a register cache (in the stack, we
4822 don't want to malloc here). */
4823 init_register_cache (&tregcache
, context_regcache
->tdesc
,
4826 /* Copy the register data to the regblock. */
4827 regcache_cpy (&tregcache
, context_regcache
);
4829 #ifndef IN_PROCESS_AGENT
4830 /* On some platforms, trap-based tracepoints will have the PC
4831 pointing to the next instruction after the trap, but we
4832 don't want the user or GDB trying to guess whether the
4833 saved PC needs adjusting; so always record the adjusted
4834 stop_pc. Note that we can't use tpoint->address instead,
4835 since it will be wrong for while-stepping actions. This
4836 adjustment is a nop for fast tracepoints collected from the
4837 in-process lib (but not if GDBserver is collecting one
4838 preemptively), since the PC had already been adjusted to
4839 contain the tracepoint's address by the jump pad. */
4840 trace_debug ("Storing stop pc (0x%s) in regblock",
4841 paddress (stop_pc
));
4843 /* This changes the regblock, not the thread's
4845 regcache_write_pc (&tregcache
, stop_pc
);
4851 struct eval_expr_action
*eaction
;
4852 struct eval_agent_expr_context ax_ctx
;
4854 eaction
= (struct eval_expr_action
*) taction
;
4855 ax_ctx
.regcache
= get_context_regcache (ctx
);
4856 ax_ctx
.tframe
= tframe
;
4857 ax_ctx
.tpoint
= tpoint
;
4859 trace_debug ("Want to evaluate expression");
4861 err
= gdb_eval_agent_expr (&ax_ctx
, eaction
->expr
, NULL
);
4863 if (err
!= expr_eval_no_error
)
4865 record_tracepoint_error (tpoint
, "action expression", err
);
4872 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4873 trace_debug ("Want to collect static trace data");
4874 collect_ust_data_at_tracepoint (ctx
, tframe
);
4876 trace_debug ("warning: collecting static trace data, "
4877 "but static tracepoints are not supported");
4882 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4888 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4889 struct tracepoint
*tpoint
)
4892 enum eval_result_type err
;
4894 /* Presently, gdbserver doesn't run compiled conditions, only the
4895 IPA does. If the program stops at a fast tracepoint's address
4896 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4897 gdbserver preemptively collect the fast tracepoint. Later, on
4898 resume, gdbserver steps over the fast tracepoint like it steps
4899 over breakpoints, so that the IPA doesn't see that fast
4900 tracepoint. This avoids double collects of fast tracepoints in
4901 that stopping scenario. Having gdbserver itself handle the fast
4902 tracepoint gives the user a consistent view of when fast or trap
4903 tracepoints are collected, compared to an alternative where only
4904 trap tracepoints are collected on stop, and fast tracepoints on
4905 resume. When a fast tracepoint is being processed by gdbserver,
4906 it is always the non-compiled condition expression that is
4908 #ifdef IN_PROCESS_AGENT
4909 if (tpoint
->compiled_cond
)
4910 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4914 struct eval_agent_expr_context ax_ctx
;
4916 ax_ctx
.regcache
= get_context_regcache (ctx
);
4917 ax_ctx
.tframe
= NULL
;
4918 ax_ctx
.tpoint
= tpoint
;
4920 err
= gdb_eval_agent_expr (&ax_ctx
, tpoint
->cond
, &value
);
4922 if (err
!= expr_eval_no_error
)
4924 record_tracepoint_error (tpoint
, "condition", err
);
4925 /* The error case must return false. */
4929 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4930 tpoint
->number
, paddress (tpoint
->address
),
4932 return (value
? 1 : 0);
4935 /* Do memory copies for bytecodes. */
4936 /* Do the recording of memory blocks for actions and bytecodes. */
4939 agent_mem_read (struct eval_agent_expr_context
*ctx
,
4940 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4942 unsigned char *mspace
;
4943 ULONGEST remaining
= len
;
4944 unsigned short blocklen
;
4946 /* If a 'to' buffer is specified, use it. */
4949 read_inferior_memory (from
, to
, len
);
4953 /* Otherwise, create a new memory block in the trace buffer. */
4954 while (remaining
> 0)
4958 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4959 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4960 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4963 /* Identify block as a memory block. */
4966 /* Record address and size. */
4967 memcpy (mspace
, &from
, sizeof (from
));
4968 mspace
+= sizeof (from
);
4969 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4970 mspace
+= sizeof (blocklen
);
4971 /* Record the memory block proper. */
4972 read_inferior_memory (from
, mspace
, blocklen
);
4973 trace_debug ("%d bytes recorded", blocklen
);
4974 remaining
-= blocklen
;
4981 agent_mem_read_string (struct eval_agent_expr_context
*ctx
,
4982 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4984 unsigned char *buf
, *mspace
;
4985 ULONGEST remaining
= len
;
4986 unsigned short blocklen
, i
;
4988 /* To save a bit of space, block lengths are 16-bit, so break large
4989 requests into multiple blocks. Bordering on overkill for strings,
4990 but it could happen that someone specifies a large max length. */
4991 while (remaining
> 0)
4995 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4996 /* We want working space to accumulate nonzero bytes, since
4997 traceframes must have a predecided size (otherwise it gets
4998 harder to wrap correctly for the circular case, etc). */
4999 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
5000 for (i
= 0; i
< blocklen
; ++i
)
5002 /* Read the string one byte at a time, in case the string is
5003 at the end of a valid memory area - we don't want a
5004 correctly-terminated string to engender segvio
5006 read_inferior_memory (from
+ i
, buf
+ i
, 1);
5011 /* Make sure outer loop stops now too. */
5012 remaining
= blocklen
;
5016 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
5017 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
5023 /* Identify block as a memory block. */
5026 /* Record address and size. */
5027 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
5028 mspace
+= sizeof (from
);
5029 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
5030 mspace
+= sizeof (blocklen
);
5031 /* Copy the string contents. */
5032 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
5033 remaining
-= blocklen
;
5040 /* Record the value of a trace state variable. */
5043 agent_tsv_read (struct eval_agent_expr_context
*ctx
, int n
)
5045 unsigned char *vspace
;
5048 vspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
,
5049 1 + sizeof (n
) + sizeof (LONGEST
));
5052 /* Identify block as a variable. */
5054 /* Record variable's number and value. */
5055 memcpy (vspace
+ 1, &n
, sizeof (n
));
5056 val
= get_trace_state_variable_value (n
);
5057 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
5058 trace_debug ("Variable %d recorded", n
);
5062 #ifndef IN_PROCESS_AGENT
5064 /* Callback for traceframe_walk_blocks, used to find a given block
5065 type in a traceframe. */
5068 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
5070 char *wantedp
= data
;
5072 if (*wantedp
== blocktype
)
5078 /* Walk over all traceframe blocks of the traceframe buffer starting
5079 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5080 block found, passing in DATA unmodified. If CALLBACK returns true,
5081 this returns a pointer to where the block is found. Returns NULL
5082 if no callback call returned true, indicating that all blocks have
5085 static unsigned char *
5086 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
5088 int (*callback
) (char blocktype
,
5089 unsigned char *dataptr
,
5093 unsigned char *dataptr
;
5097 trace_debug ("traceframe %d has no data", tfnum
);
5101 /* Iterate through a traceframe's blocks, looking for a block of the
5103 for (dataptr
= database
;
5104 dataptr
< database
+ datasize
;
5108 unsigned short mlen
;
5110 if (dataptr
== trace_buffer_wrap
)
5112 /* Adjust to reflect wrapping part of the frame around to
5114 datasize
= dataptr
- database
;
5115 dataptr
= database
= trace_buffer_lo
;
5118 blocktype
= *dataptr
++;
5120 if ((*callback
) (blocktype
, dataptr
, data
))
5126 /* Skip over the registers block. */
5127 dataptr
+= current_target_desc ()->registers_size
;
5130 /* Skip over the memory block. */
5131 dataptr
+= sizeof (CORE_ADDR
);
5132 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5133 dataptr
+= (sizeof (mlen
) + mlen
);
5136 /* Skip over the TSV block. */
5137 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5140 /* Skip over the static trace data block. */
5141 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5142 dataptr
+= (sizeof (mlen
) + mlen
);
5145 trace_debug ("traceframe %d has unknown block type 0x%x",
5154 /* Look for the block of type TYPE_WANTED in the trameframe starting
5155 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5158 static unsigned char *
5159 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5160 int tfnum
, char type_wanted
)
5162 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5163 match_blocktype
, &type_wanted
);
5166 static unsigned char *
5167 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5169 unsigned char *regblock
;
5171 regblock
= traceframe_find_block_type (tframe
->data
,
5175 if (regblock
== NULL
)
5176 trace_debug ("traceframe %d has no register data", tfnum
);
5181 /* Get registers from a traceframe. */
5184 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5186 unsigned char *dataptr
;
5187 struct tracepoint
*tpoint
;
5188 struct traceframe
*tframe
;
5190 tframe
= find_traceframe (tfnum
);
5194 trace_debug ("traceframe %d not found", tfnum
);
5198 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5199 if (dataptr
== NULL
)
5201 /* Mark registers unavailable. */
5202 supply_regblock (regcache
, NULL
);
5204 /* We can generally guess at a PC, although this will be
5205 misleading for while-stepping frames and multi-location
5207 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5209 regcache_write_pc (regcache
, tpoint
->address
);
5212 supply_regblock (regcache
, dataptr
);
5218 traceframe_get_pc (struct traceframe
*tframe
)
5220 struct regcache regcache
;
5221 unsigned char *dataptr
;
5222 const struct target_desc
*tdesc
= current_target_desc ();
5224 dataptr
= traceframe_find_regblock (tframe
, -1);
5225 if (dataptr
== NULL
)
5228 init_register_cache (®cache
, tdesc
, dataptr
);
5229 return regcache_read_pc (®cache
);
5232 /* Read a requested block of memory from a trace frame. */
5235 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5236 unsigned char *buf
, ULONGEST length
,
5239 struct traceframe
*tframe
;
5240 unsigned char *database
, *dataptr
;
5241 unsigned int datasize
;
5243 unsigned short mlen
;
5245 trace_debug ("traceframe_read_mem");
5247 tframe
= find_traceframe (tfnum
);
5251 trace_debug ("traceframe %d not found", tfnum
);
5255 datasize
= tframe
->data_size
;
5256 database
= dataptr
= &tframe
->data
[0];
5258 /* Iterate through a traceframe's blocks, looking for memory. */
5259 while ((dataptr
= traceframe_find_block_type (dataptr
,
5261 - (dataptr
- database
),
5262 tfnum
, 'M')) != NULL
)
5264 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5265 dataptr
+= sizeof (maddr
);
5266 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5267 dataptr
+= sizeof (mlen
);
5268 trace_debug ("traceframe %d has %d bytes at %s",
5269 tfnum
, mlen
, paddress (maddr
));
5271 /* If the block includes the first part of the desired range,
5272 return as much it has; GDB will re-request the remainder,
5273 which might be in a different block of this trace frame. */
5274 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5276 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5280 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5285 /* Skip over this block. */
5289 trace_debug ("traceframe %d has no memory data for the desired region",
5297 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5300 struct traceframe
*tframe
;
5301 unsigned char *database
, *dataptr
;
5302 unsigned int datasize
;
5306 trace_debug ("traceframe_read_tsv");
5308 tfnum
= current_traceframe
;
5312 trace_debug ("no current traceframe");
5316 tframe
= find_traceframe (tfnum
);
5320 trace_debug ("traceframe %d not found", tfnum
);
5324 datasize
= tframe
->data_size
;
5325 database
= dataptr
= &tframe
->data
[0];
5327 /* Iterate through a traceframe's blocks, looking for the last
5329 while ((dataptr
= traceframe_find_block_type (dataptr
,
5331 - (dataptr
- database
),
5332 tfnum
, 'V')) != NULL
)
5334 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5335 dataptr
+= sizeof (vnum
);
5337 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5339 /* Check that this is the variable we want. */
5342 memcpy (val
, dataptr
, sizeof (*val
));
5346 /* Skip over this block. */
5347 dataptr
+= sizeof (LONGEST
);
5351 trace_debug ("traceframe %d has no data for variable %d",
5356 /* Read a requested block of static tracepoint data from a trace
5360 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5361 unsigned char *buf
, ULONGEST length
,
5364 struct traceframe
*tframe
;
5365 unsigned char *database
, *dataptr
;
5366 unsigned int datasize
;
5367 unsigned short mlen
;
5369 trace_debug ("traceframe_read_sdata");
5371 tframe
= find_traceframe (tfnum
);
5375 trace_debug ("traceframe %d not found", tfnum
);
5379 datasize
= tframe
->data_size
;
5380 database
= &tframe
->data
[0];
5382 /* Iterate through a traceframe's blocks, looking for static
5384 dataptr
= traceframe_find_block_type (database
, datasize
,
5386 if (dataptr
!= NULL
)
5388 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5389 dataptr
+= sizeof (mlen
);
5392 if (offset
+ length
> mlen
)
5393 length
= mlen
- offset
;
5395 memcpy (buf
, dataptr
, length
);
5403 trace_debug ("traceframe %d has no static trace data", tfnum
);
5409 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5410 object. DATA is pointer to a struct buffer holding the
5411 traceframe-info object being built. */
5414 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5416 struct buffer
*buffer
= data
;
5422 unsigned short mlen
;
5425 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5426 dataptr
+= sizeof (maddr
);
5427 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5428 dataptr
+= sizeof (mlen
);
5429 buffer_xml_printf (buffer
,
5430 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5431 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5438 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5439 buffer_xml_printf (buffer
, "<tvar id=\"%d\"/>\n", vnum
);
5448 warning ("Unhandled trace block type (%d) '%c ' "
5449 "while building trace frame info.",
5450 blocktype
, blocktype
);
5457 /* Build a traceframe-info object for traceframe number TFNUM into
5461 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5463 struct traceframe
*tframe
;
5465 trace_debug ("traceframe_read_info");
5467 tframe
= find_traceframe (tfnum
);
5471 trace_debug ("traceframe %d not found", tfnum
);
5475 buffer_grow_str (buffer
, "<traceframe-info>\n");
5476 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5477 tfnum
, build_traceframe_info_xml
, buffer
);
5478 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5482 /* Return the first fast tracepoint whose jump pad contains PC. */
5484 static struct tracepoint
*
5485 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5487 struct tracepoint
*tpoint
;
5489 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5490 if (tpoint
->type
== fast_tracepoint
)
5491 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5497 /* Return the first fast tracepoint whose trampoline contains PC. */
5499 static struct tracepoint
*
5500 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5502 struct tracepoint
*tpoint
;
5504 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5506 if (tpoint
->type
== fast_tracepoint
5507 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5514 /* Return GDBserver's tracepoint that matches the IP Agent's
5515 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5518 static struct tracepoint
*
5519 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5521 struct tracepoint
*tpoint
;
5523 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5524 if (tpoint
->type
== fast_tracepoint
)
5525 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5533 /* The type of the object that is used to synchronize fast tracepoint
5536 typedef struct collecting_t
5538 /* The fast tracepoint number currently collecting. */
5541 /* A number that GDBserver can use to identify the thread that is
5542 presently holding the collect lock. This need not (and usually
5543 is not) the thread id, as getting the current thread ID usually
5544 requires a system call, which we want to avoid like the plague.
5545 Usually this is thread's TCB, found in the TLS (pseudo-)
5546 register, which is readable with a single insn on several
5548 uintptr_t thread_area
;
5551 #ifndef IN_PROCESS_AGENT
5554 force_unlock_trace_buffer (void)
5556 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5559 /* Check if the thread identified by THREAD_AREA which is stopped at
5560 STOP_PC, is presently locking the fast tracepoint collection, and
5561 if so, gather some status of said collection. Returns 0 if the
5562 thread isn't collecting or in the jump pad at all. 1, if in the
5563 jump pad (or within gdb_collect) and hasn't executed the adjusted
5564 original insn yet (can set a breakpoint there and run to it). 2,
5565 if presently executing the adjusted original insn --- in which
5566 case, if we want to move the thread out of the jump pad, we need to
5567 single-step it until this function returns 0. */
5570 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5572 struct fast_tpoint_collect_status
*status
)
5574 CORE_ADDR ipa_collecting
;
5575 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5576 CORE_ADDR ipa_gdb_trampoline_buffer
;
5577 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5578 struct tracepoint
*tpoint
;
5579 int needs_breakpoint
;
5581 /* The thread THREAD_AREA is either:
5583 0. not collecting at all, not within the jump pad, or within
5584 gdb_collect or one of its callees.
5586 1. in the jump pad and haven't reached gdb_collect
5588 2. within gdb_collect (out of the jump pad) (collect is set)
5590 3. we're in the jump pad, after gdb_collect having returned,
5591 possibly executing the adjusted insns.
5593 For cases 1 and 3, `collecting' may or not be set. The jump pad
5594 doesn't have any complicated jump logic, so we can tell if the
5595 thread is executing the adjust original insn or not by just
5596 matching STOP_PC with known jump pad addresses. If we it isn't
5597 yet executing the original insn, set a breakpoint there, and let
5598 the thread run to it, so to quickly step over a possible (many
5599 insns) gdb_collect call. Otherwise, or when the breakpoint is
5600 hit, only a few (small number of) insns are left to be executed
5601 in the jump pad. Single-step the thread until it leaves the
5606 needs_breakpoint
= 0;
5607 trace_debug ("fast_tracepoint_collecting");
5609 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5610 &ipa_gdb_jump_pad_buffer
))
5611 fatal ("error extracting `gdb_jump_pad_buffer'");
5612 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5613 &ipa_gdb_jump_pad_buffer_end
))
5614 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5616 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5617 &ipa_gdb_trampoline_buffer
))
5618 fatal ("error extracting `gdb_trampoline_buffer'");
5619 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5620 &ipa_gdb_trampoline_buffer_end
))
5621 fatal ("error extracting `gdb_trampoline_buffer_end'");
5623 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5624 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5626 /* We can tell which tracepoint(s) the thread is collecting by
5627 matching the jump pad address back to the tracepoint. */
5628 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5631 warning ("in jump pad, but no matching tpoint?");
5636 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5638 tpoint
->number
, paddress (tpoint
->address
),
5639 paddress (tpoint
->jump_pad
),
5640 paddress (tpoint
->jump_pad_end
),
5641 paddress (tpoint
->adjusted_insn_addr
),
5642 paddress (tpoint
->adjusted_insn_addr_end
));
5645 /* Definitely in the jump pad. May or may not need
5646 fast-exit-jump-pad breakpoint. */
5647 if (tpoint
->jump_pad
<= stop_pc
5648 && stop_pc
< tpoint
->adjusted_insn_addr
)
5649 needs_breakpoint
= 1;
5651 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5652 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5654 /* We can tell which tracepoint(s) the thread is collecting by
5655 matching the trampoline address back to the tracepoint. */
5656 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5659 warning ("in trampoline, but no matching tpoint?");
5664 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5665 tpoint
->number
, paddress (tpoint
->address
),
5666 paddress (tpoint
->trampoline
),
5667 paddress (tpoint
->trampoline_end
));
5670 /* Have not reached jump pad yet, but treat the trampoline as a
5671 part of the jump pad that is before the adjusted original
5673 needs_breakpoint
= 1;
5677 collecting_t ipa_collecting_obj
;
5679 /* If `collecting' is set/locked, then the THREAD_AREA thread
5680 may or not be the one holding the lock. We have to read the
5681 lock to find out. */
5683 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5686 trace_debug ("fast_tracepoint_collecting:"
5687 " failed reading 'collecting' in the inferior");
5691 if (!ipa_collecting
)
5693 trace_debug ("fast_tracepoint_collecting: not collecting"
5694 " (and nobody is).");
5698 /* Some thread is collecting. Check which. */
5699 if (read_inferior_memory (ipa_collecting
,
5700 (unsigned char *) &ipa_collecting_obj
,
5701 sizeof (ipa_collecting_obj
)) != 0)
5704 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5706 trace_debug ("fast_tracepoint_collecting: not collecting "
5707 "(another thread is)");
5712 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5715 warning ("fast_tracepoint_collecting: collecting, "
5716 "but tpoint %s not found?",
5717 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5721 /* The thread is within `gdb_collect', skip over the rest of
5722 fast tracepoint collection quickly using a breakpoint. */
5723 needs_breakpoint
= 1;
5726 /* The caller wants a bit of status detail. */
5729 status
->tpoint_num
= tpoint
->number
;
5730 status
->tpoint_addr
= tpoint
->address
;
5731 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5732 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5735 if (needs_breakpoint
)
5737 /* Hasn't executed the original instruction yet. Set breakpoint
5738 there, and wait till it's hit, then single-step until exiting
5742 fast_tracepoint_collecting, returning continue-until-break at %s",
5743 paddress (tpoint
->adjusted_insn_addr
));
5745 return 1; /* continue */
5749 /* Just single-step until exiting the jump pad. */
5751 trace_debug ("fast_tracepoint_collecting, returning "
5752 "need-single-step (%s-%s)",
5753 paddress (tpoint
->adjusted_insn_addr
),
5754 paddress (tpoint
->adjusted_insn_addr_end
));
5756 return 2; /* single-step */
5762 #ifdef IN_PROCESS_AGENT
5764 /* The global fast tracepoint collect lock. Points to a collecting_t
5765 object built on the stack by the jump pad, if presently locked;
5766 NULL if it isn't locked. Note that this lock *must* be set while
5767 executing any *function other than the jump pad. See
5768 fast_tracepoint_collecting. */
5769 static collecting_t
* ATTR_USED collecting
;
5771 /* This routine, called from the jump pad (in asm) is designed to be
5772 called from the jump pads of fast tracepoints, thus it is on the
5775 IP_AGENT_EXPORT
void ATTR_USED
5776 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5778 struct fast_tracepoint_ctx ctx
;
5780 /* Don't do anything until the trace run is completely set up. */
5784 ctx
.base
.type
= fast_tracepoint
;
5786 ctx
.regcache_initted
= 0;
5787 /* Wrap the regblock in a register cache (in the stack, we don't
5788 want to malloc here). */
5789 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
5790 if (ctx
.regspace
== NULL
)
5792 trace_debug ("Trace buffer block allocation failed, skipping");
5796 for (ctx
.tpoint
= tpoint
;
5797 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5798 ctx
.tpoint
= ctx
.tpoint
->next
)
5800 if (!ctx
.tpoint
->enabled
)
5803 /* Multiple tracepoints of different types, such as fast tracepoint and
5804 static tracepoint, can be set at the same address. */
5805 if (ctx
.tpoint
->type
!= tpoint
->type
)
5808 /* Test the condition if present, and collect if true. */
5809 if (ctx
.tpoint
->cond
== NULL
5810 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5813 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5814 ctx
.tpoint
->address
, ctx
.tpoint
);
5816 /* Note that this will cause original insns to be written back
5817 to where we jumped from, but that's OK because we're jumping
5818 back to the next whole instruction. This will go badly if
5819 instruction restoration is not atomic though. */
5820 if (stopping_tracepoint
5821 || trace_buffer_is_full
5822 || expr_eval_result
!= expr_eval_no_error
)
5830 /* If there was a condition and it evaluated to false, the only
5831 way we would stop tracing is if there was an error during
5832 condition expression evaluation. */
5833 if (expr_eval_result
!= expr_eval_no_error
)
5844 #ifndef IN_PROCESS_AGENT
5847 get_raw_reg_func_addr (void)
5849 return ipa_sym_addrs
.addr_get_raw_reg
;
5853 get_get_tsv_func_addr (void)
5855 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5859 get_set_tsv_func_addr (void)
5861 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5865 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5866 CORE_ADDR
*jump_entry
)
5868 CORE_ADDR entry_point
= *jump_entry
;
5869 enum eval_result_type err
;
5871 trace_debug ("Starting condition compilation for tracepoint %d\n",
5874 /* Initialize the global pointer to the code being built. */
5875 current_insn_ptr
= *jump_entry
;
5879 err
= compile_bytecodes (tpoint
->cond
);
5881 if (err
== expr_eval_no_error
)
5885 /* Record the beginning of the compiled code. */
5886 tpoint
->compiled_cond
= entry_point
;
5888 trace_debug ("Condition compilation for tracepoint %d complete\n",
5893 /* Leave the unfinished code in situ, but don't point to it. */
5895 tpoint
->compiled_cond
= 0;
5897 trace_debug ("Condition compilation for tracepoint %d failed, "
5899 tpoint
->number
, err
);
5902 /* Update the code pointer passed in. Note that we do this even if
5903 the compile fails, so that we can look at the partial results
5904 instead of letting them be overwritten. */
5905 *jump_entry
= current_insn_ptr
;
5907 /* Leave a gap, to aid dump decipherment. */
5911 /* We'll need to adjust these when we consider bi-arch setups, and big
5915 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5917 return write_inferior_memory (where
,
5918 (unsigned char *) &ptr
, sizeof (void *));
5921 /* The base pointer of the IPA's heap. This is the only memory the
5922 IPA is allowed to use. The IPA should _not_ call the inferior's
5923 `malloc' during operation. That'd be slow, and, most importantly,
5924 it may not be safe. We may be collecting a tracepoint in a signal
5925 handler, for example. */
5926 static CORE_ADDR target_tp_heap
;
5928 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5932 target_malloc (ULONGEST size
)
5936 if (target_tp_heap
== 0)
5938 /* We have the pointer *address*, need what it points to. */
5939 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5941 fatal ("could get target heap head pointer");
5944 ptr
= target_tp_heap
;
5945 target_tp_heap
+= size
;
5947 /* Pad to 8-byte alignment. */
5948 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5954 download_agent_expr (struct agent_expr
*expr
)
5956 CORE_ADDR expr_addr
;
5957 CORE_ADDR expr_bytes
;
5959 expr_addr
= target_malloc (sizeof (*expr
));
5960 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5962 expr_bytes
= target_malloc (expr
->length
);
5963 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5965 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5970 /* Align V up to N bits. */
5971 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5973 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5976 download_tracepoint_1 (struct tracepoint
*tpoint
)
5978 struct tracepoint target_tracepoint
;
5979 CORE_ADDR tpptr
= 0;
5981 gdb_assert (tpoint
->type
== fast_tracepoint
5982 || tpoint
->type
== static_tracepoint
);
5984 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5986 CORE_ADDR jentry
, jump_entry
;
5988 jentry
= jump_entry
= get_jump_space_head ();
5990 if (tpoint
->cond
!= NULL
)
5992 /* Pad to 8-byte alignment. (needed?) */
5993 /* Actually this should be left for the target to
5995 jentry
= UALIGN (jentry
, 8);
5997 compile_tracepoint_condition (tpoint
, &jentry
);
6000 /* Pad to 8-byte alignment. */
6001 jentry
= UALIGN (jentry
, 8);
6002 claim_jump_space (jentry
- jump_entry
);
6005 target_tracepoint
= *tpoint
;
6007 tpptr
= target_malloc (sizeof (*tpoint
));
6008 tpoint
->obj_addr_on_target
= tpptr
;
6010 /* Write the whole object. We'll fix up its pointers in a bit.
6011 Assume no next for now. This is fixed up above on the next
6012 iteration, if there's any. */
6013 target_tracepoint
.next
= NULL
;
6014 /* Need to clear this here too, since we're downloading the
6015 tracepoints before clearing our own copy. */
6016 target_tracepoint
.hit_count
= 0;
6018 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
6019 sizeof (target_tracepoint
));
6022 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6024 download_agent_expr (tpoint
->cond
));
6026 if (tpoint
->numactions
)
6029 CORE_ADDR actions_array
;
6031 /* The pointers array. */
6033 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6034 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6038 /* Now for each pointer, download the action. */
6039 for (i
= 0; i
< tpoint
->numactions
; i
++)
6041 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6042 CORE_ADDR ipa_action
= action
->ops
->download (action
);
6044 if (ipa_action
!= 0)
6045 write_inferior_data_ptr
6046 (actions_array
+ i
* sizeof (*tpoint
->actions
),
6052 #define IPA_PROTO_FAST_TRACE_FLAG 0
6053 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
6054 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
6055 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
6056 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
6058 /* Send a command to agent to download and install tracepoint TPOINT. */
6061 tracepoint_send_agent (struct tracepoint
*tpoint
)
6063 char buf
[IPA_CMD_BUF_SIZE
];
6068 strcpy (p
, "FastTrace:");
6071 COPY_FIELD_TO_BUF (p
, tpoint
, number
);
6072 COPY_FIELD_TO_BUF (p
, tpoint
, address
);
6073 COPY_FIELD_TO_BUF (p
, tpoint
, type
);
6074 COPY_FIELD_TO_BUF (p
, tpoint
, enabled
);
6075 COPY_FIELD_TO_BUF (p
, tpoint
, step_count
);
6076 COPY_FIELD_TO_BUF (p
, tpoint
, pass_count
);
6077 COPY_FIELD_TO_BUF (p
, tpoint
, numactions
);
6078 COPY_FIELD_TO_BUF (p
, tpoint
, hit_count
);
6079 COPY_FIELD_TO_BUF (p
, tpoint
, traceframe_usage
);
6080 COPY_FIELD_TO_BUF (p
, tpoint
, compiled_cond
);
6081 COPY_FIELD_TO_BUF (p
, tpoint
, orig_size
);
6084 p
= agent_expr_send (p
, tpoint
->cond
);
6086 /* tracepoint_action */
6087 for (i
= 0; i
< tpoint
->numactions
; i
++)
6089 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6091 p
[0] = action
->type
;
6092 p
= action
->ops
->send (&p
[1], action
);
6095 get_jump_space_head ();
6096 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6097 agent can use jump pad from it. */
6098 if (tpoint
->type
== fast_tracepoint
)
6100 memcpy (p
, &gdb_jump_pad_head
, 8);
6104 ret
= run_inferior_command (buf
, (int) (ptrdiff_t) (p
- buf
));
6108 if (strncmp (buf
, "OK", 2) != 0)
6111 /* The value of tracepoint's target address is stored in BUF. */
6112 memcpy (&tpoint
->obj_addr_on_target
,
6113 &buf
[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET
], 8);
6115 if (tpoint
->type
== fast_tracepoint
)
6118 = (unsigned char *) &buf
[IPA_PROTO_FAST_TRACE_FJUMP_INSN
];
6121 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6122 (unsigned int) tpoint
->obj_addr_on_target
,
6123 (unsigned int) gdb_jump_pad_head
);
6125 memcpy (&gdb_jump_pad_head
, &buf
[IPA_PROTO_FAST_TRACE_JUMP_PAD
], 8);
6127 /* This has been done in agent. We should also set up record for it. */
6128 memcpy (&fjump_size
, &buf
[IPA_PROTO_FAST_TRACE_FJUMP_SIZE
], 4);
6131 = set_fast_tracepoint_jump (tpoint
->address
, insn
, fjump_size
);
6138 download_tracepoint (struct tracepoint
*tpoint
)
6140 struct tracepoint
*tp
, *tp_prev
;
6142 if (tpoint
->type
!= fast_tracepoint
6143 && tpoint
->type
!= static_tracepoint
)
6146 download_tracepoint_1 (tpoint
);
6148 /* Find the previous entry of TPOINT, which is fast tracepoint or
6149 static tracepoint. */
6151 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6153 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6159 CORE_ADDR tp_prev_target_next_addr
;
6161 /* Insert TPOINT after TP_PREV in IPA. */
6162 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6163 + offsetof (struct tracepoint
, next
),
6164 &tp_prev_target_next_addr
))
6165 fatal ("error reading `tp_prev->next'");
6167 /* tpoint->next = tp_prev->next */
6168 write_inferior_data_ptr (tpoint
->obj_addr_on_target
6169 + offsetof (struct tracepoint
, next
),
6170 tp_prev_target_next_addr
);
6171 /* tp_prev->next = tpoint */
6172 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
6173 + offsetof (struct tracepoint
, next
),
6174 tpoint
->obj_addr_on_target
);
6177 /* First object in list, set the head pointer in the
6179 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
6180 tpoint
->obj_addr_on_target
);
6185 download_trace_state_variables (void)
6187 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6188 struct trace_state_variable
*tsv
;
6190 /* Start out empty. */
6191 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6193 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6195 struct trace_state_variable target_tsv
;
6197 /* TSV's with a getter have been initialized equally in both the
6198 inferior and GDBserver. Skip them. */
6199 if (tsv
->getter
!= NULL
)
6205 ptr
= target_malloc (sizeof (*tsv
));
6207 if (tsv
== trace_state_variables
)
6209 /* First object in list, set the head pointer in the
6212 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6217 write_inferior_data_ptr (prev_ptr
6218 + offsetof (struct trace_state_variable
,
6223 /* Write the whole object. We'll fix up its pointers in a bit.
6224 Assume no next, fixup when needed. */
6225 target_tsv
.next
= NULL
;
6227 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6228 sizeof (target_tsv
));
6230 if (tsv
->name
!= NULL
)
6232 size_t size
= strlen (tsv
->name
) + 1;
6233 CORE_ADDR name_addr
= target_malloc (size
);
6234 write_inferior_memory (name_addr
,
6235 (unsigned char *) tsv
->name
, size
);
6236 write_inferior_data_ptr (ptr
6237 + offsetof (struct trace_state_variable
,
6242 if (tsv
->getter
!= NULL
)
6244 fatal ("what to do with these?");
6250 /* Fixup the next pointer in the last item in the list. */
6251 write_inferior_data_ptr (prev_ptr
6252 + offsetof (struct trace_state_variable
,
6257 /* Upload complete trace frames out of the IP Agent's trace buffer
6258 into GDBserver's trace buffer. This always uploads either all or
6259 no trace frames. This is the counter part of
6260 `trace_alloc_trace_buffer'. See its description of the atomic
6261 synching mechanism. */
6264 upload_fast_traceframes (void)
6266 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6267 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6269 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6270 unsigned int curr_tbctrl_idx
;
6271 unsigned int ipa_trace_buffer_ctrl_curr
;
6272 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6273 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6274 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6275 CORE_ADDR ipa_trace_buffer_lo
;
6276 CORE_ADDR ipa_trace_buffer_hi
;
6278 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6279 &ipa_traceframe_read_count_racy
))
6281 /* This will happen in most targets if the current thread is
6286 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6287 &ipa_traceframe_write_count_racy
))
6290 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6291 ipa_traceframe_write_count_racy
6292 - ipa_traceframe_read_count_racy
,
6293 ipa_traceframe_write_count_racy
,
6294 ipa_traceframe_read_count_racy
);
6296 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6299 about_to_request_buffer_space_bkpt
6300 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6303 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6304 &ipa_trace_buffer_ctrl_curr
))
6307 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6309 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6312 unsigned int prev
, counter
;
6314 /* Update the token, with new counters, and the GDBserver stamp
6315 bit. Alway reuse the current TBC index. */
6316 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6317 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6319 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6325 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6326 ipa_trace_buffer_ctrl_curr
))
6329 trace_debug ("Lib: Committed %08x -> %08x",
6330 ipa_trace_buffer_ctrl_curr_old
,
6331 ipa_trace_buffer_ctrl_curr
);
6333 /* Re-read these, now that we've installed the
6334 `about_to_request_buffer_space' breakpoint/lock. A thread could
6335 have finished a traceframe between the last read of these
6336 counters and setting the breakpoint above. If we start
6337 uploading, we never want to leave this function with
6338 traceframe_read_count != 0, otherwise, GDBserver could end up
6339 incrementing the counter tokens more than once (due to event loop
6340 nesting), which would break the IP agent's "effective" detection
6341 (see trace_alloc_trace_buffer). */
6342 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6343 &ipa_traceframe_read_count
))
6345 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6346 &ipa_traceframe_write_count
))
6351 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6352 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6353 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6355 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6356 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6357 trace_debug ("note that ipa_traceframe_count's parts changed");
6360 /* Get the address of the current TBC object (the IP agent has an
6361 array of 3 such objects). The index is stored in the TBC
6363 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6364 ipa_trace_buffer_ctrl_addr
6365 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6367 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6368 (unsigned char *) &ipa_trace_buffer_ctrl
,
6369 sizeof (struct ipa_trace_buffer_control
)))
6372 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6373 &ipa_trace_buffer_lo
))
6375 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6376 &ipa_trace_buffer_hi
))
6379 /* Offsets are easier to grok for debugging than raw addresses,
6380 especially for the small trace buffer sizes that are useful for
6382 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6383 "endfree=%d wrap=%d hi=%d",
6385 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6386 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6387 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6388 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6389 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6391 /* Note that the IPA's buffer is always circular. */
6393 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6395 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6396 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6398 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6399 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6400 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6401 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6404 tf
= IPA_FIRST_TRACEFRAME ();
6406 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6408 struct tracepoint
*tpoint
;
6409 struct traceframe
*tframe
;
6410 unsigned char *block
;
6411 struct traceframe ipa_tframe
;
6413 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6414 offsetof (struct traceframe
, data
)))
6415 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6417 if (ipa_tframe
.tpnum
== 0)
6418 fatal ("Uploading: No (more) fast traceframes, but "
6419 "ipa_traceframe_count == %u??\n",
6420 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6422 /* Note that this will be incorrect for multi-location
6424 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6426 tframe
= add_traceframe (tpoint
);
6429 trace_buffer_is_full
= 1;
6430 trace_debug ("Uploading: trace buffer is full");
6434 /* Copy the whole set of blocks in one go for now. FIXME:
6435 split this in smaller blocks. */
6436 block
= add_traceframe_block (tframe
, tpoint
,
6437 ipa_tframe
.data_size
);
6440 if (read_inferior_memory (tf
6441 + offsetof (struct traceframe
, data
),
6442 block
, ipa_tframe
.data_size
))
6443 error ("Uploading: Couldn't read traceframe data at %s\n",
6444 paddress (tf
+ offsetof (struct traceframe
, data
)));
6447 trace_debug ("Uploading: traceframe didn't fit");
6448 finish_traceframe (tframe
);
6451 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6453 /* If we freed the traceframe that wrapped around, go back
6454 to the non-wrap case. */
6455 if (tf
< ipa_trace_buffer_ctrl
.start
)
6457 trace_debug ("Lib: Discarding past the wraparound");
6458 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6460 ipa_trace_buffer_ctrl
.start
= tf
;
6461 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6462 ++ipa_traceframe_read_count
;
6464 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6465 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6467 trace_debug ("Lib: buffer is fully empty. "
6468 "Trace buffer [%d] start=%d free=%d endfree=%d",
6470 (int) (ipa_trace_buffer_ctrl
.start
6471 - ipa_trace_buffer_lo
),
6472 (int) (ipa_trace_buffer_ctrl
.free
6473 - ipa_trace_buffer_lo
),
6474 (int) (ipa_trace_buffer_ctrl
.end_free
6475 - ipa_trace_buffer_lo
));
6477 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6478 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6479 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6480 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6483 trace_debug ("Uploaded a traceframe\n"
6484 "Lib: Trace buffer [%d] start=%d free=%d "
6485 "endfree=%d wrap=%d hi=%d",
6487 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6488 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6489 (int) (ipa_trace_buffer_ctrl
.end_free
6490 - ipa_trace_buffer_lo
),
6491 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6492 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6495 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6496 (unsigned char *) &ipa_trace_buffer_ctrl
,
6497 sizeof (struct ipa_trace_buffer_control
)))
6500 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6501 ipa_traceframe_read_count
);
6503 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6506 cancel_breakpoints ();
6508 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6509 about_to_request_buffer_space_bkpt
= NULL
;
6513 if (trace_buffer_is_full
)
6518 #ifdef IN_PROCESS_AGENT
6520 IP_AGENT_EXPORT
int ust_loaded
;
6521 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6525 /* Static tracepoints. */
6527 /* UST puts a "struct tracepoint" in the global namespace, which
6528 conflicts with our tracepoint. Arguably, being a library, it
6529 shouldn't take ownership of such a generic name. We work around it
6531 #define tracepoint ust_tracepoint
6532 #include <ust/ust.h>
6535 extern int serialize_to_text (char *outbuf
, int bufsize
,
6536 const char *fmt
, va_list ap
);
6538 #define GDB_PROBE_NAME "gdb"
6540 /* We dynamically search for the UST symbols instead of linking them
6541 in. This lets the user decide if the application uses static
6542 tracepoints, instead of always pulling libust.so in. This vector
6543 holds pointers to all functions we care about. */
6547 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6548 const char *fmt
, va_list ap
);
6550 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6551 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6553 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6555 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6558 void (*marker_iter_start
) (struct marker_iter
*iter
);
6559 void (*marker_iter_next
) (struct marker_iter
*iter
);
6560 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6561 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6566 /* Cast through typeof to catch incompatible API changes. Since UST
6567 only builds with gcc, we can freely use gcc extensions here
6569 #define GET_UST_SYM(SYM) \
6572 if (ust_ops.SYM == NULL) \
6573 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6574 if (ust_ops.SYM == NULL) \
6578 #define USTF(SYM) ust_ops.SYM
6580 /* Get pointers to all libust.so functions we care about. */
6585 GET_UST_SYM (serialize_to_text
);
6587 GET_UST_SYM (ltt_probe_register
);
6588 GET_UST_SYM (ltt_probe_unregister
);
6589 GET_UST_SYM (ltt_marker_connect
);
6590 GET_UST_SYM (ltt_marker_disconnect
);
6592 GET_UST_SYM (marker_iter_start
);
6593 GET_UST_SYM (marker_iter_next
);
6594 GET_UST_SYM (marker_iter_stop
);
6595 GET_UST_SYM (marker_iter_reset
);
6601 /* Given an UST marker, return the matching gdb static tracepoint.
6602 The match is done by address. */
6604 static struct tracepoint
*
6605 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6607 struct tracepoint
*tpoint
;
6609 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6611 if (tpoint
->type
!= static_tracepoint
)
6614 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6621 /* The probe function we install on lttng/ust markers. Whenever a
6622 probed ust marker is hit, this function is called. This is similar
6623 to gdb_collect, only for static tracepoints, instead of fast
6627 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6628 struct registers
*regs
, void *call_private
,
6629 const char *fmt
, va_list *args
)
6631 struct tracepoint
*tpoint
;
6632 struct static_tracepoint_ctx ctx
;
6634 /* Don't do anything until the trace run is completely set up. */
6637 trace_debug ("gdb_probe: not tracing\n");
6641 ctx
.base
.type
= static_tracepoint
;
6642 ctx
.regcache_initted
= 0;
6647 /* Wrap the regblock in a register cache (in the stack, we don't
6648 want to malloc here). */
6649 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
6650 if (ctx
.regspace
== NULL
)
6652 trace_debug ("Trace buffer block allocation failed, skipping");
6656 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6659 trace_debug ("gdb_probe: marker not known: "
6660 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6661 mdata
->location
, mdata
->channel
,
6662 mdata
->name
, mdata
->format
);
6666 if (!tpoint
->enabled
)
6668 trace_debug ("gdb_probe: tracepoint disabled");
6672 ctx
.tpoint
= tpoint
;
6674 trace_debug ("gdb_probe: collecting marker: "
6675 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6676 mdata
->location
, mdata
->channel
,
6677 mdata
->name
, mdata
->format
);
6679 /* Test the condition if present, and collect if true. */
6680 if (tpoint
->cond
== NULL
6681 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6684 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6685 tpoint
->address
, tpoint
);
6687 if (stopping_tracepoint
6688 || trace_buffer_is_full
6689 || expr_eval_result
!= expr_eval_no_error
)
6694 /* If there was a condition and it evaluated to false, the only
6695 way we would stop tracing is if there was an error during
6696 condition expression evaluation. */
6697 if (expr_eval_result
!= expr_eval_no_error
)
6702 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6703 static tracepoint string data. This is a string passed to the
6704 tracing library by the user, at the time of the tracepoint marker
6705 call. E.g., in the UST marker call:
6707 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6709 the collected data is "str FOOBAZ".
6713 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6714 struct traceframe
*tframe
)
6716 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6717 unsigned char *bufspace
;
6720 unsigned short blocklen
;
6724 trace_debug ("Wanted to collect static trace data, "
6725 "but there's no static trace data");
6729 va_copy (copy
, *umd
->args
);
6730 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6733 trace_debug ("Want to collect ust data");
6735 /* 'S' + size + string */
6736 bufspace
= add_traceframe_block (tframe
, umd
->tpoint
,
6737 1 + sizeof (blocklen
) + size
+ 1);
6738 if (bufspace
== NULL
)
6740 trace_debug ("Trace buffer block allocation failed, skipping");
6744 /* Identify a static trace data block. */
6747 blocklen
= size
+ 1;
6748 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6750 va_copy (copy
, *umd
->args
);
6751 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6752 size
+ 1, umd
->fmt
, copy
);
6755 trace_debug ("Storing static tracepoint data in regblock: %s",
6756 bufspace
+ 1 + sizeof (blocklen
));
6759 /* The probe to register with lttng/ust. */
6760 static struct ltt_available_probe gdb_ust_probe
=
6767 #endif /* HAVE_UST */
6768 #endif /* IN_PROCESS_AGENT */
6770 #ifndef IN_PROCESS_AGENT
6772 /* Ask the in-process agent to run a command. Since we don't want to
6773 have to handle the IPA hitting breakpoints while running the
6774 command, we pause all threads, remove all breakpoints, and then set
6775 the helper thread re-running. We communicate with the helper
6776 thread by means of direct memory xfering, and a socket for
6780 run_inferior_command (char *cmd
, int len
)
6783 int pid
= ptid_get_pid (current_ptid
);
6785 trace_debug ("run_inferior_command: running: %s", cmd
);
6788 uninsert_all_breakpoints ();
6790 err
= agent_run_command (pid
, (const char *) cmd
, len
);
6792 reinsert_all_breakpoints ();
6798 #else /* !IN_PROCESS_AGENT */
6800 #include <sys/socket.h>
6803 #ifndef UNIX_PATH_MAX
6804 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6807 /* Where we put the socked used for synchronization. */
6808 #define SOCK_DIR P_tmpdir
6810 /* Thread ID of the helper thread. GDBserver reads this to know which
6811 is the help thread. This is an LWP id on Linux. */
6812 int helper_thread_id
;
6815 init_named_socket (const char *name
)
6818 struct sockaddr_un addr
;
6820 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6823 warning ("socket creation failed: %s", strerror (errno
));
6827 addr
.sun_family
= AF_UNIX
;
6829 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6830 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6832 result
= access (name
, F_OK
);
6836 result
= unlink (name
);
6839 warning ("unlink failed: %s", strerror (errno
));
6843 warning ("socket %s already exists; overwriting", name
);
6846 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6849 warning ("bind failed: %s", strerror (errno
));
6854 result
= listen (fd
, 1);
6857 warning ("listen: %s", strerror (errno
));
6865 static char agent_socket_name
[UNIX_PATH_MAX
];
6868 gdb_agent_socket_init (void)
6872 result
= xsnprintf (agent_socket_name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6873 SOCK_DIR
, getpid ());
6874 if (result
>= UNIX_PATH_MAX
)
6876 trace_debug ("string overflow allocating socket name");
6880 fd
= init_named_socket (agent_socket_name
);
6882 warning ("Error initializing named socket (%s) for communication with the "
6883 "ust helper thread. Check that directory exists and that it "
6884 "is writable.", agent_socket_name
);
6891 /* The next marker to be returned on a qTsSTM command. */
6892 static const struct marker
*next_st
;
6894 /* Returns the first known marker. */
6899 struct marker_iter iter
;
6901 USTF(marker_iter_reset
) (&iter
);
6902 USTF(marker_iter_start
) (&iter
);
6907 /* Returns the marker following M. */
6909 const struct marker
*
6910 next_marker (const struct marker
*m
)
6912 struct marker_iter iter
;
6914 USTF(marker_iter_reset
) (&iter
);
6915 USTF(marker_iter_start
) (&iter
);
6917 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6919 if (iter
.marker
== m
)
6921 USTF(marker_iter_next
) (&iter
);
6929 /* Return an hexstr version of the STR C string, fit for sending to
6933 cstr_to_hexstr (const char *str
)
6935 int len
= strlen (str
);
6936 char *hexstr
= xmalloc (len
* 2 + 1);
6937 bin2hex ((gdb_byte
*) str
, hexstr
, len
);
6941 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6945 response_ust_marker (char *packet
, const struct marker
*st
)
6947 char *strid
, *format
, *tmp
;
6949 next_st
= next_marker (st
);
6951 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6952 strlen (st
->name
) + 1);
6953 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6955 strid
= cstr_to_hexstr (tmp
);
6958 format
= cstr_to_hexstr (st
->format
);
6960 sprintf (packet
, "m%s:%s:%s",
6961 paddress ((uintptr_t) st
->location
),
6969 /* Return the first static tracepoint, and initialize the state
6970 machine that will iterate through all the static tracepoints. */
6973 cmd_qtfstm (char *packet
)
6975 trace_debug ("Returning first trace state variable definition");
6977 if (first_marker ())
6978 response_ust_marker (packet
, first_marker ());
6980 strcpy (packet
, "l");
6983 /* Return additional trace state variable definitions. */
6986 cmd_qtsstm (char *packet
)
6988 trace_debug ("Returning static tracepoint");
6991 response_ust_marker (packet
, next_st
);
6993 strcpy (packet
, "l");
6996 /* Disconnect the GDB probe from a marker at a given address. */
6999 unprobe_marker_at (char *packet
)
7003 struct marker_iter iter
;
7005 p
+= sizeof ("unprobe_marker_at:") - 1;
7007 p
= unpack_varlen_hex (p
, &address
);
7009 USTF(marker_iter_reset
) (&iter
);
7010 USTF(marker_iter_start
) (&iter
);
7011 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7012 if ((uintptr_t ) iter
.marker
->location
== address
)
7016 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7020 warning ("could not disable marker %s/%s",
7021 iter
.marker
->channel
, iter
.marker
->name
);
7026 /* Connect the GDB probe to a marker at a given address. */
7029 probe_marker_at (char *packet
)
7033 struct marker_iter iter
;
7036 p
+= sizeof ("probe_marker_at:") - 1;
7038 p
= unpack_varlen_hex (p
, &address
);
7040 USTF(marker_iter_reset
) (&iter
);
7042 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7044 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7045 if ((uintptr_t ) m
->location
== address
)
7049 trace_debug ("found marker for address. "
7050 "ltt_marker_connect (marker = %s/%s)",
7051 m
->channel
, m
->name
);
7053 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7055 if (result
&& result
!= -EEXIST
)
7056 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7057 m
->channel
, m
->name
, -result
);
7061 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7062 m
->channel
, m
->name
);
7066 strcpy (packet
, "OK");
7070 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7075 cmd_qtstmat (char *packet
)
7079 struct marker_iter iter
;
7082 p
+= sizeof ("qTSTMat:") - 1;
7084 p
= unpack_varlen_hex (p
, &address
);
7086 USTF(marker_iter_reset
) (&iter
);
7088 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7090 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7091 if ((uintptr_t ) m
->location
== address
)
7093 response_ust_marker (packet
, m
);
7097 strcpy (packet
, "l");
7107 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7110 #endif /* HAVE_UST */
7112 #include <sys/syscall.h>
7116 gdb_agent_remove_socket (void)
7118 unlink (agent_socket_name
);
7121 /* Helper thread of agent. */
7124 gdb_agent_helper_thread (void *arg
)
7128 atexit (gdb_agent_remove_socket
);
7132 listen_fd
= gdb_agent_socket_init ();
7134 if (helper_thread_id
== 0)
7135 helper_thread_id
= syscall (SYS_gettid
);
7137 if (listen_fd
== -1)
7139 warning ("could not create sync socket\n");
7146 struct sockaddr_un sockaddr
;
7152 tmp
= sizeof (sockaddr
);
7156 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7158 /* It seems an ERESTARTSYS can escape out of accept. */
7159 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7163 warning ("Accept returned %d, error: %s\n",
7164 fd
, strerror (errno
));
7170 ret
= read (fd
, buf
, 1);
7171 } while (ret
== -1 && errno
== EINTR
);
7175 warning ("reading socket (fd=%d) failed with %s",
7176 fd
, strerror (errno
));
7183 if (strncmp ("close", cmd_buf
, 5) == 0)
7188 else if (strcmp ("qTfSTM", cmd_buf
) == 0)
7190 cmd_qtfstm (cmd_buf
);
7192 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7194 cmd_qtsstm (cmd_buf
);
7196 else if (strncmp ("unprobe_marker_at:",
7198 sizeof ("unprobe_marker_at:") - 1) == 0)
7200 unprobe_marker_at (cmd_buf
);
7202 else if (strncmp ("probe_marker_at:",
7204 sizeof ("probe_marker_at:") - 1) == 0)
7206 probe_marker_at (cmd_buf
);
7208 else if (strncmp ("qTSTMat:",
7210 sizeof ("qTSTMat:") - 1) == 0)
7212 cmd_qtstmat (cmd_buf
);
7214 #endif /* HAVE_UST */
7217 /* Fix compiler's warning: ignoring return value of 'write'. */
7218 ret
= write (fd
, buf
, 1);
7224 unlink (agent_socket_name
);
7226 /* Sleep endlessly to wait the whole inferior stops. This
7227 thread can not exit because GDB or GDBserver may still need
7228 'current_inferior' (representing this thread) to access
7229 inferior memory. Otherwise, this thread exits earlier than
7230 other threads, and 'current_inferior' is set to NULL. */
7241 #include <pthread.h>
7243 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
7246 gdb_agent_init (void)
7253 /* We want the helper thread to be as transparent as possible, so
7254 have it inherit an all-signals-blocked mask. */
7256 sigfillset (&new_mask
);
7257 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7259 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7261 res
= pthread_create (&thread
,
7263 gdb_agent_helper_thread
,
7266 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7268 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7270 while (helper_thread_id
== 0)
7278 #include <sys/mman.h>
7281 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7282 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7283 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7284 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
7285 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
7286 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
7288 /* Record the result of getting buffer space for fast tracepoint
7289 trampolines. Any error message is copied, since caller may not be
7290 using persistent storage. */
7293 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7295 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7296 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7298 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7300 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7303 static void __attribute__ ((constructor
))
7304 initialize_tracepoint_ftlib (void)
7306 initialize_tracepoint ();
7311 #endif /* IN_PROCESS_AGENT */
7313 /* Return a timestamp, expressed as microseconds of the usual Unix
7314 time. (As the result is a 64-bit number, it will not overflow any
7318 get_timestamp (void)
7322 if (gettimeofday (&tv
, 0) != 0)
7325 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7329 initialize_tracepoint (void)
7331 /* Start with the default size. */
7332 init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE
);
7334 /* Wire trace state variable 1 to be the timestamp. This will be
7335 uploaded to GDB upon connection and become one of its trace state
7336 variables. (In case you're wondering, if GDB already has a trace
7337 variable numbered 1, it will be renumbered.) */
7338 create_trace_state_variable (1, 0);
7339 set_trace_state_variable_name (1, "trace_timestamp");
7340 set_trace_state_variable_getter (1, get_timestamp
);
7342 #ifdef IN_PROCESS_AGENT
7347 pagesize
= sysconf (_SC_PAGE_SIZE
);
7351 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7353 #define SCRATCH_BUFFER_NPAGES 20
7355 /* Allocate scratch buffer aligned on a page boundary, at a low
7356 address (close to the main executable's code). */
7357 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7359 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7360 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7361 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7363 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7369 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7372 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7375 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7377 /* It's not a fatal error for something to go wrong with trampoline
7378 buffer setup, but it can be mysterious, so create a channel to
7379 report back on what went wrong, using a fixed size since we may
7380 not be able to allocate space later when the problem occurs. */
7381 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7383 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7385 initialize_low_tracepoint ();