1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2015 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"
35 #define DEFAULT_TRACE_BUFFER_SIZE 5242880 /* 5*1024*1024 */
37 /* This file is built for both GDBserver, and the in-process
38 agent (IPA), a shared library that includes a tracing agent that is
39 loaded by the inferior to support fast tracepoints. Fast
40 tracepoints (or more accurately, jump based tracepoints) are
41 implemented by patching the tracepoint location with a jump into a
42 small trampoline function whose job is to save the register state,
43 call the in-process tracing agent, and then execute the original
44 instruction that was under the tracepoint jump (possibly adjusted,
45 if PC-relative, or some such).
47 The current synchronization design is pull based. That means,
48 GDBserver does most of the work, by peeking/poking at the inferior
49 agent's memory directly for downloading tracepoint and associated
50 objects, and for uploading trace frames. Whenever the IPA needs
51 something from GDBserver (trace buffer is full, tracing stopped for
52 some reason, etc.) the IPA calls a corresponding hook function
53 where GDBserver has placed a breakpoint.
55 Each of the agents has its own trace buffer. When browsing the
56 trace frames built from slow and fast tracepoints from GDB (tfind
57 mode), there's no guarantee the user is seeing the trace frames in
58 strict chronological creation order, although, GDBserver tries to
59 keep the order relatively reasonable, by syncing the trace buffers
64 #ifdef IN_PROCESS_AGENT
66 static void trace_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
69 trace_vdebug (const char *fmt
, ...)
75 vsprintf (buf
, fmt
, ap
);
76 fprintf (stderr
, PROG
"/tracepoint: %s\n", buf
);
80 #define trace_debug_1(level, fmt, args...) \
82 if (level <= debug_threads) \
83 trace_vdebug ((fmt), ##args); \
88 #define trace_debug_1(level, fmt, args...) \
90 if (level <= debug_threads) \
92 debug_printf ((fmt), ##args); \
93 debug_printf ("\n"); \
99 #define trace_debug(FMT, args...) \
100 trace_debug_1 (1, FMT, ##args)
102 #if defined(__GNUC__)
103 # define ATTR_USED __attribute__((used))
104 # define ATTR_NOINLINE __attribute__((noinline))
105 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
108 # define ATTR_NOINLINE
109 # define ATTR_CONSTRUCTOR
112 /* Make sure the functions the IPA needs to export (symbols GDBserver
113 needs to query GDB about) are exported. */
115 #ifdef IN_PROCESS_AGENT
116 # if defined _WIN32 || defined __CYGWIN__
117 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
120 # define IP_AGENT_EXPORT \
121 __attribute__ ((visibility("default"))) ATTR_USED
123 # define IP_AGENT_EXPORT ATTR_USED
127 # define IP_AGENT_EXPORT
130 /* Prefix exported symbols, for good citizenship. All the symbols
131 that need exporting are defined in this module. */
132 #ifdef IN_PROCESS_AGENT
133 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
134 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
135 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
136 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
137 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
138 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
139 # define collecting gdb_agent_collecting
140 # define gdb_collect gdb_agent_gdb_collect
141 # define stop_tracing gdb_agent_stop_tracing
142 # define flush_trace_buffer gdb_agent_flush_trace_buffer
143 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
144 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
145 # define stopping_tracepoint gdb_agent_stopping_tracepoint
146 # define expr_eval_result gdb_agent_expr_eval_result
147 # define error_tracepoint gdb_agent_error_tracepoint
148 # define tracepoints gdb_agent_tracepoints
149 # define tracing gdb_agent_tracing
150 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
151 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
152 # define trace_buffer_lo gdb_agent_trace_buffer_lo
153 # define trace_buffer_hi gdb_agent_trace_buffer_hi
154 # define traceframe_read_count gdb_agent_traceframe_read_count
155 # define traceframe_write_count gdb_agent_traceframe_write_count
156 # define traceframes_created gdb_agent_traceframes_created
157 # define trace_state_variables gdb_agent_trace_state_variables
158 # define get_raw_reg gdb_agent_get_raw_reg
159 # define get_trace_state_variable_value \
160 gdb_agent_get_trace_state_variable_value
161 # define set_trace_state_variable_value \
162 gdb_agent_set_trace_state_variable_value
163 # define ust_loaded gdb_agent_ust_loaded
164 # define helper_thread_id gdb_agent_helper_thread_id
165 # define cmd_buf gdb_agent_cmd_buf
168 #ifndef IN_PROCESS_AGENT
170 /* Addresses of in-process agent's symbols GDBserver cares about. */
172 struct ipa_sym_addresses
174 CORE_ADDR addr_gdb_tp_heap_buffer
;
175 CORE_ADDR addr_gdb_jump_pad_buffer
;
176 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
177 CORE_ADDR addr_gdb_trampoline_buffer
;
178 CORE_ADDR addr_gdb_trampoline_buffer_end
;
179 CORE_ADDR addr_gdb_trampoline_buffer_error
;
180 CORE_ADDR addr_collecting
;
181 CORE_ADDR addr_gdb_collect
;
182 CORE_ADDR addr_stop_tracing
;
183 CORE_ADDR addr_flush_trace_buffer
;
184 CORE_ADDR addr_about_to_request_buffer_space
;
185 CORE_ADDR addr_trace_buffer_is_full
;
186 CORE_ADDR addr_stopping_tracepoint
;
187 CORE_ADDR addr_expr_eval_result
;
188 CORE_ADDR addr_error_tracepoint
;
189 CORE_ADDR addr_tracepoints
;
190 CORE_ADDR addr_tracing
;
191 CORE_ADDR addr_trace_buffer_ctrl
;
192 CORE_ADDR addr_trace_buffer_ctrl_curr
;
193 CORE_ADDR addr_trace_buffer_lo
;
194 CORE_ADDR addr_trace_buffer_hi
;
195 CORE_ADDR addr_traceframe_read_count
;
196 CORE_ADDR addr_traceframe_write_count
;
197 CORE_ADDR addr_traceframes_created
;
198 CORE_ADDR addr_trace_state_variables
;
199 CORE_ADDR addr_get_raw_reg
;
200 CORE_ADDR addr_get_trace_state_variable_value
;
201 CORE_ADDR addr_set_trace_state_variable_value
;
202 CORE_ADDR addr_ust_loaded
;
211 IPA_SYM(gdb_tp_heap_buffer
),
212 IPA_SYM(gdb_jump_pad_buffer
),
213 IPA_SYM(gdb_jump_pad_buffer_end
),
214 IPA_SYM(gdb_trampoline_buffer
),
215 IPA_SYM(gdb_trampoline_buffer_end
),
216 IPA_SYM(gdb_trampoline_buffer_error
),
218 IPA_SYM(gdb_collect
),
219 IPA_SYM(stop_tracing
),
220 IPA_SYM(flush_trace_buffer
),
221 IPA_SYM(about_to_request_buffer_space
),
222 IPA_SYM(trace_buffer_is_full
),
223 IPA_SYM(stopping_tracepoint
),
224 IPA_SYM(expr_eval_result
),
225 IPA_SYM(error_tracepoint
),
226 IPA_SYM(tracepoints
),
228 IPA_SYM(trace_buffer_ctrl
),
229 IPA_SYM(trace_buffer_ctrl_curr
),
230 IPA_SYM(trace_buffer_lo
),
231 IPA_SYM(trace_buffer_hi
),
232 IPA_SYM(traceframe_read_count
),
233 IPA_SYM(traceframe_write_count
),
234 IPA_SYM(traceframes_created
),
235 IPA_SYM(trace_state_variables
),
236 IPA_SYM(get_raw_reg
),
237 IPA_SYM(get_trace_state_variable_value
),
238 IPA_SYM(set_trace_state_variable_value
),
242 static struct ipa_sym_addresses ipa_sym_addrs
;
244 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
246 /* Returns true if both the in-process agent library and the static
247 tracepoints libraries are loaded in the inferior, and agent has
248 capability on static tracepoints. */
251 in_process_agent_supports_ust (void)
255 if (!agent_loaded_p ())
257 warning ("In-process agent not loaded");
261 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE
))
263 /* Agent understands static tracepoint, then check whether UST is in
264 fact loaded in the inferior. */
265 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
267 warning ("Error reading ust_loaded in lib");
278 write_e_ipa_not_loaded (char *buffer
)
281 "E.In-process agent library not loaded in process. "
282 "Fast and static tracepoints unavailable.");
285 /* Write an error to BUFFER indicating that UST isn't loaded in the
289 write_e_ust_not_loaded (char *buffer
)
293 "E.UST library not loaded in process. "
294 "Static tracepoints unavailable.");
296 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
300 /* If the in-process agent library isn't loaded in the inferior, write
301 an error to BUFFER, and return 1. Otherwise, return 0. */
304 maybe_write_ipa_not_loaded (char *buffer
)
306 if (!agent_loaded_p ())
308 write_e_ipa_not_loaded (buffer
);
314 /* If the in-process agent library and the ust (static tracepoints)
315 library aren't loaded in the inferior, write an error to BUFFER,
316 and return 1. Otherwise, return 0. */
319 maybe_write_ipa_ust_not_loaded (char *buffer
)
321 if (!agent_loaded_p ())
323 write_e_ipa_not_loaded (buffer
);
326 else if (!in_process_agent_supports_ust ())
328 write_e_ust_not_loaded (buffer
);
334 /* Cache all future symbols that the tracepoints module might request.
335 We can not request symbols at arbitrary states in the remote
336 protocol, only when the client tells us that new symbols are
337 available. So when we load the in-process library, make sure to
338 check the entire list. */
341 tracepoint_look_up_symbols (void)
345 if (agent_loaded_p ())
348 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
351 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
353 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
356 debug_printf ("symbol `%s' not found\n", symbol_list
[i
].name
);
361 agent_look_up_symbols (NULL
);
366 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
367 of the "stop_tracing" function. When this breakpoint is hit,
368 tracing stopped in the IPA for some reason. E.g., due to
369 tracepoint reaching the pass count, hitting conditional expression
370 evaluation error, etc.
372 The IPA's trace buffer is never in circular tracing mode: instead,
373 GDBserver's is, and whenever the in-process buffer fills, it calls
374 "flush_trace_buffer", which triggers an internal breakpoint.
375 GDBserver reacts to this breakpoint by pulling the meanwhile
376 collected data. Old frames discarding is always handled on the
379 #ifdef IN_PROCESS_AGENT
381 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
383 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
387 /* Call this in the functions where GDBserver places a breakpoint, so
388 that the compiler doesn't try to be clever and skip calling the
389 function at all. This is necessary, even if we tell the compiler
390 to not inline said functions. */
392 #if defined(__GNUC__)
393 # define UNKNOWN_SIDE_EFFECTS() asm ("")
395 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
398 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
401 /* GDBserver places breakpoint here. */
402 UNKNOWN_SIDE_EFFECTS();
405 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
406 flush_trace_buffer (void)
408 /* GDBserver places breakpoint here. */
409 UNKNOWN_SIDE_EFFECTS();
414 #ifndef IN_PROCESS_AGENT
416 tracepoint_handler (CORE_ADDR address
)
418 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
423 /* Breakpoint at "stop_tracing" in the inferior lib. */
424 struct breakpoint
*stop_tracing_bkpt
;
425 static int stop_tracing_handler (CORE_ADDR
);
427 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
428 struct breakpoint
*flush_trace_buffer_bkpt
;
429 static int flush_trace_buffer_handler (CORE_ADDR
);
431 static void download_trace_state_variables (void);
432 static void upload_fast_traceframes (void);
434 static int run_inferior_command (char *cmd
, int len
);
437 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
439 return read_inferior_memory (symaddr
, (unsigned char *) val
,
444 static int tracepoint_send_agent (struct tracepoint
*tpoint
);
447 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
449 return read_inferior_memory (symaddr
, (unsigned char *) val
,
454 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
456 void *pval
= (void *) (uintptr_t) val
;
459 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
460 *val
= (uintptr_t) pval
;
465 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
467 void *pval
= (void *) (uintptr_t) val
;
468 return write_inferior_memory (symaddr
,
469 (unsigned char *) &pval
, sizeof (pval
));
473 write_inferior_integer (CORE_ADDR symaddr
, int val
)
475 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
479 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
481 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
484 static CORE_ADDR
target_malloc (ULONGEST size
);
485 static int write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
);
487 #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD) \
489 memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \
490 BUF += sizeof ((OBJ)->FIELD); \
495 /* Operations on various types of tracepoint actions. */
497 struct tracepoint_action
;
499 struct tracepoint_action_ops
501 /* Download tracepoint action ACTION to IPA. Return the address of action
503 CORE_ADDR (*download
) (const struct tracepoint_action
*action
);
505 /* Send ACTION to agent via command buffer started from BUFFER. Return
506 updated head of command buffer. */
507 char* (*send
) (char *buffer
, const struct tracepoint_action
*action
);
510 /* Base action. Concrete actions inherit this. */
512 struct tracepoint_action
514 #ifndef IN_PROCESS_AGENT
515 const struct tracepoint_action_ops
*ops
;
520 /* An 'M' (collect memory) action. */
521 struct collect_memory_action
523 struct tracepoint_action base
;
530 /* An 'R' (collect registers) action. */
532 struct collect_registers_action
534 struct tracepoint_action base
;
537 /* An 'X' (evaluate expression) action. */
539 struct eval_expr_action
541 struct tracepoint_action base
;
543 struct agent_expr
*expr
;
546 /* An 'L' (collect static trace data) action. */
547 struct collect_static_trace_data_action
549 struct tracepoint_action base
;
552 #ifndef IN_PROCESS_AGENT
554 m_tracepoint_action_download (const struct tracepoint_action
*action
)
556 int size_in_ipa
= (sizeof (struct collect_memory_action
)
557 - offsetof (struct tracepoint_action
, type
));
558 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
560 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
566 m_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
568 struct collect_memory_action
*maction
569 = (struct collect_memory_action
*) action
;
571 COPY_FIELD_TO_BUF (buffer
, maction
, addr
);
572 COPY_FIELD_TO_BUF (buffer
, maction
, len
);
573 COPY_FIELD_TO_BUF (buffer
, maction
, basereg
);
578 static const struct tracepoint_action_ops m_tracepoint_action_ops
=
580 m_tracepoint_action_download
,
581 m_tracepoint_action_send
,
585 r_tracepoint_action_download (const struct tracepoint_action
*action
)
587 int size_in_ipa
= (sizeof (struct collect_registers_action
)
588 - offsetof (struct tracepoint_action
, type
));
589 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
591 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
598 r_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
603 static const struct tracepoint_action_ops r_tracepoint_action_ops
=
605 r_tracepoint_action_download
,
606 r_tracepoint_action_send
,
609 static CORE_ADDR
download_agent_expr (struct agent_expr
*expr
);
612 x_tracepoint_action_download (const struct tracepoint_action
*action
)
614 int size_in_ipa
= (sizeof (struct eval_expr_action
)
615 - offsetof (struct tracepoint_action
, type
));
616 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
619 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
621 expr
= download_agent_expr (((struct eval_expr_action
*)action
)->expr
);
622 write_inferior_data_ptr (ipa_action
+ offsetof (struct eval_expr_action
, expr
)
623 - offsetof (struct tracepoint_action
, type
),
629 /* Copy agent expression AEXPR to buffer pointed by P. If AEXPR is NULL,
630 copy 0 to P. Return updated header of buffer. */
633 agent_expr_send (char *p
, const struct agent_expr
*aexpr
)
635 /* Copy the length of condition first, and then copy its
644 memcpy (p
, &aexpr
->length
, 4);
647 memcpy (p
, aexpr
->bytes
, aexpr
->length
);
654 x_tracepoint_action_send ( char *buffer
, const struct tracepoint_action
*action
)
656 struct eval_expr_action
*eaction
= (struct eval_expr_action
*) action
;
658 return agent_expr_send (buffer
, eaction
->expr
);
661 static const struct tracepoint_action_ops x_tracepoint_action_ops
=
663 x_tracepoint_action_download
,
664 x_tracepoint_action_send
,
668 l_tracepoint_action_download (const struct tracepoint_action
*action
)
670 int size_in_ipa
= (sizeof (struct collect_static_trace_data_action
)
671 - offsetof (struct tracepoint_action
, type
));
672 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
674 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
681 l_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
686 static const struct tracepoint_action_ops l_tracepoint_action_ops
=
688 l_tracepoint_action_download
,
689 l_tracepoint_action_send
,
693 /* This structure describes a piece of the source-level definition of
694 the tracepoint. The contents are not interpreted by the target,
695 but preserved verbatim for uploading upon reconnection. */
699 /* The type of string, such as "cond" for a conditional. */
702 /* The source-level string itself. For the sake of target
703 debugging, we store it in plaintext, even though it is always
704 transmitted in hex. */
707 /* Link to the next one in the list. We link them in the order
708 received, in case some make up an ordered list of commands or
710 struct source_string
*next
;
715 /* Trap based tracepoint. */
718 /* A fast tracepoint implemented with a jump instead of a trap. */
721 /* A static tracepoint, implemented by a program call into a tracing
726 struct tracepoint_hit_ctx
;
728 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
731 /* The definition of a tracepoint. */
733 /* Tracepoints may have multiple locations, each at a different
734 address. This can occur with optimizations, template
735 instantiation, etc. Since the locations may be in different
736 scopes, the conditions and actions may be different for each
737 location. Our target version of tracepoints is more like GDB's
738 notion of "breakpoint locations", but we have almost nothing that
739 is not per-location, so we bother having two kinds of objects. The
740 key consequence is that numbers are not unique, and that it takes
741 both number and address to identify a tracepoint uniquely. */
745 /* The number of the tracepoint, as specified by GDB. Several
746 tracepoint objects here may share a number. */
749 /* Address at which the tracepoint is supposed to trigger. Several
750 tracepoints may share an address. */
753 /* Tracepoint type. */
754 enum tracepoint_type type
;
756 /* True if the tracepoint is currently enabled. */
759 /* The number of single steps that will be performed after each
763 /* The number of times the tracepoint may be hit before it will
764 terminate the entire tracing run. */
767 /* Pointer to the agent expression that is the tracepoint's
768 conditional, or NULL if the tracepoint is unconditional. */
769 struct agent_expr
*cond
;
771 /* The list of actions to take when the tracepoint triggers. */
773 struct tracepoint_action
**actions
;
775 /* Count of the times we've hit this tracepoint during the run.
776 Note that while-stepping steps are not counted as "hits". */
779 /* Cached sum of the sizes of traceframes created by this point. */
780 uint64_t traceframe_usage
;
782 CORE_ADDR compiled_cond
;
784 /* Link to the next tracepoint in the list. */
785 struct tracepoint
*next
;
787 #ifndef IN_PROCESS_AGENT
788 /* The list of actions to take when the tracepoint triggers, in
789 string/packet form. */
792 /* The collection of strings that describe the tracepoint as it was
793 entered into GDB. These are not used by the target, but are
794 reported back to GDB upon reconnection. */
795 struct source_string
*source_strings
;
797 /* The number of bytes displaced by fast tracepoints. It may subsume
798 multiple instructions, for multi-byte fast tracepoints. This
799 field is only valid for fast tracepoints. */
802 /* Only for fast tracepoints. */
803 CORE_ADDR obj_addr_on_target
;
805 /* Address range where the original instruction under a fast
806 tracepoint was relocated to. (_end is actually one byte past
808 CORE_ADDR adjusted_insn_addr
;
809 CORE_ADDR adjusted_insn_addr_end
;
811 /* The address range of the piece of the jump pad buffer that was
812 assigned to this fast tracepoint. (_end is actually one byte
815 CORE_ADDR jump_pad_end
;
817 /* The address range of the piece of the trampoline buffer that was
818 assigned to this fast tracepoint. (_end is actually one byte
820 CORE_ADDR trampoline
;
821 CORE_ADDR trampoline_end
;
823 /* The list of actions to take while in a stepping loop. These
824 fields are only valid for patch-based tracepoints. */
825 int num_step_actions
;
826 struct tracepoint_action
**step_actions
;
827 /* Same, but in string/packet form. */
828 char **step_actions_str
;
830 /* Handle returned by the breakpoint or tracepoint module when we
831 inserted the trap or jump, or hooked into a static tracepoint.
832 NULL if we haven't inserted it yet. */
838 #ifndef IN_PROCESS_AGENT
840 /* Given `while-stepping', a thread may be collecting data for more
841 than one tracepoint simultaneously. On the other hand, the same
842 tracepoint with a while-stepping action may be hit by more than one
843 thread simultaneously (but not quite, each thread could be handling
844 a different step). Each thread holds a list of these objects,
845 representing the current step of each while-stepping action being
850 struct wstep_state
*next
;
852 /* The tracepoint number. */
854 /* The tracepoint's address. */
855 CORE_ADDR tp_address
;
857 /* The number of the current step in this 'while-stepping'
864 /* The linked list of all tracepoints. Marked explicitly as used as
865 the in-process library doesn't use it for the fast tracepoints
867 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
869 #ifndef IN_PROCESS_AGENT
871 /* Pointer to the last tracepoint in the list, new tracepoints are
872 linked in at the end. */
874 static struct tracepoint
*last_tracepoint
;
877 /* The first tracepoint to exceed its pass count. */
879 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
881 /* True if the trace buffer is full or otherwise no longer usable. */
883 IP_AGENT_EXPORT
int trace_buffer_is_full
;
885 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
887 #ifndef IN_PROCESS_AGENT
889 static const char *eval_result_names
[] =
891 "terror:in the attic", /* this should never be reported */
892 "terror:empty expression",
893 "terror:empty stack",
894 "terror:stack overflow",
895 "terror:stack underflow",
896 "terror:unhandled opcode",
897 "terror:unrecognized opcode",
898 "terror:divide by zero"
903 /* The tracepoint in which the error occurred. */
905 static struct tracepoint
*error_tracepoint
;
907 struct trace_state_variable
909 /* This is the name of the variable as used in GDB. The target
910 doesn't use the name, but needs to have it for saving and
911 reconnection purposes. */
914 /* This number identifies the variable uniquely. Numbers may be
915 assigned either by the target (in the case of builtin variables),
916 or by GDB, and are presumed unique during the course of a trace
920 /* The variable's initial value, a 64-bit signed integer always. */
921 LONGEST initial_value
;
923 /* The variable's value, a 64-bit signed integer always. */
926 /* Pointer to a getter function, used to supply computed values. */
927 LONGEST (*getter
) (void);
929 /* Link to the next variable. */
930 struct trace_state_variable
*next
;
933 /* Linked list of all trace state variables. */
935 #ifdef IN_PROCESS_AGENT
936 struct trace_state_variable
*alloced_trace_state_variables
;
939 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
941 /* The results of tracing go into a fixed-size space known as the
942 "trace buffer". Because usage follows a limited number of
943 patterns, we manage it ourselves rather than with malloc. Basic
944 rules are that we create only one trace frame at a time, each is
945 variable in size, they are never moved once created, and we only
946 discard if we are doing a circular buffer, and then only the oldest
947 ones. Each trace frame includes its own size, so we don't need to
948 link them together, and the trace frame number is relative to the
949 first one, so we don't need to record numbers. A trace frame also
950 records the number of the tracepoint that created it. The data
951 itself is a series of blocks, each introduced by a single character
952 and with a defined format. Each type of block has enough
953 type/length info to allow scanners to jump quickly from one block
954 to the next without reading each byte in the block. */
956 /* Trace buffer management would be simple - advance a free pointer
957 from beginning to end, then stop - were it not for the circular
958 buffer option, which is a useful way to prevent a trace run from
959 stopping prematurely because the buffer filled up. In the circular
960 case, the location of the first trace frame (trace_buffer_start)
961 moves as old trace frames are discarded. Also, since we grow trace
962 frames incrementally as actions are performed, we wrap around to
963 the beginning of the trace buffer. This is per-block, so each
964 block within a trace frame remains contiguous. Things get messy
965 when the wrapped-around trace frame is the one being discarded; the
966 free space ends up in two parts at opposite ends of the buffer. */
969 # if defined(__GNUC__)
970 # define ATTR_PACKED __attribute__ ((packed))
972 # define ATTR_PACKED /* nothing */
976 /* The data collected at a tracepoint hit. This object should be as
977 small as possible, since there may be a great many of them. We do
978 not need to keep a frame number, because they are all sequential
979 and there are no deletions; so the Nth frame in the buffer is
980 always frame number N. */
984 /* Number of the tracepoint that collected this traceframe. A value
985 of 0 indicates the current end of the trace buffer. We make this
986 a 16-bit field because it's never going to happen that GDB's
987 numbering of tracepoints reaches 32,000. */
990 /* The size of the data in this trace frame. We limit this to 32
991 bits, even on a 64-bit target, because it's just implausible that
992 one is validly going to collect 4 gigabytes of data at a single
994 unsigned int data_size
: 32;
996 /* The base of the trace data, which is contiguous from this point. */
997 unsigned char data
[0];
1001 /* The size of the EOB marker, in bytes. A traceframe with zeroed
1002 fields (and no data) marks the end of trace data. */
1003 #define TRACEFRAME_EOB_MARKER_SIZE offsetof (struct traceframe, data)
1005 /* The traceframe to be used as the source of data to send back to
1006 GDB. A value of -1 means to get data from the live program. */
1008 int current_traceframe
= -1;
1010 /* This flag is true if the trace buffer is circular, meaning that
1011 when it fills, the oldest trace frames are discarded in order to
1014 #ifndef IN_PROCESS_AGENT
1015 static int circular_trace_buffer
;
1018 /* Size of the trace buffer. */
1020 static LONGEST trace_buffer_size
;
1022 /* Pointer to the block of memory that traceframes all go into. */
1024 static unsigned char *trace_buffer_lo
;
1026 /* Pointer to the end of the trace buffer, more precisely to the byte
1027 after the end of the buffer. */
1029 static unsigned char *trace_buffer_hi
;
1031 /* Control structure holding the read/write/etc. pointers into the
1032 trace buffer. We need more than one of these to implement a
1033 transaction-like mechanism to garantees that both GDBserver and the
1034 in-process agent can try to change the trace buffer
1037 struct trace_buffer_control
1039 /* Pointer to the first trace frame in the buffer. In the
1040 non-circular case, this is equal to trace_buffer_lo, otherwise it
1041 moves around in the buffer. */
1042 unsigned char *start
;
1044 /* Pointer to the free part of the trace buffer. Note that we clear
1045 several bytes at and after this pointer, so that traceframe
1046 scans/searches terminate properly. */
1047 unsigned char *free
;
1049 /* Pointer to the byte after the end of the free part. Note that
1050 this may be smaller than trace_buffer_free in the circular case,
1051 and means that the free part is in two pieces. Initially it is
1052 equal to trace_buffer_hi, then is generally equivalent to
1053 trace_buffer_start. */
1054 unsigned char *end_free
;
1056 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
1057 this is the point at which the trace data breaks, and resumes at
1059 unsigned char *wrap
;
1062 /* Same as above, to be used by GDBserver when updating the in-process
1064 struct ipa_trace_buffer_control
1073 /* We have possibly both GDBserver and an inferior thread accessing
1074 the same IPA trace buffer memory. The IPA is the producer (tries
1075 to put new frames in the buffer), while GDBserver occasionally
1076 consumes them, that is, flushes the IPA's buffer into its own
1077 buffer. Both sides need to update the trace buffer control
1078 pointers (current head, tail, etc.). We can't use a global lock to
1079 synchronize the accesses, as otherwise we could deadlock GDBserver
1080 (if the thread holding the lock stops for a signal, say). So
1081 instead of that, we use a transaction scheme where GDBserver writes
1082 always prevail over the IPAs writes, and, we have the IPA detect
1083 the commit failure/overwrite, and retry the whole attempt. This is
1084 mainly implemented by having a global token object that represents
1085 who wrote last to the buffer control structure. We need to freeze
1086 any inferior writing to the buffer while GDBserver touches memory,
1087 so that the inferior can correctly detect that GDBserver had been
1088 there, otherwise, it could mistakingly think its commit was
1089 successful; that's implemented by simply having GDBserver set a
1090 breakpoint the inferior hits if it is the critical region.
1092 There are three cycling trace buffer control structure copies
1093 (buffer head, tail, etc.), with the token object including an index
1094 indicating which is current live copy. The IPA tentatively builds
1095 an updated copy in a non-current control structure, while GDBserver
1096 always clobbers the current version directly. The IPA then tries
1097 to atomically "commit" its version; if GDBserver clobbered the
1098 structure meanwhile, that will fail, and the IPA restarts the
1101 Listing the step in further detail, we have:
1103 In-process agent (producer):
1105 - passes by `about_to_request_buffer_space' breakpoint/lock
1107 - reads current token, extracts current trace buffer control index,
1108 and starts tentatively updating the rightmost one (0->1, 1->2,
1109 2->0). Note that only one inferior thread is executing this code
1110 at any given time, due to an outer lock in the jump pads.
1112 - updates counters, and tries to commit the token.
1114 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1115 leaving the sync region.
1117 - checks if the update was effective.
1119 - if trace buffer was found full, hits flush_trace_buffer
1120 breakpoint, and restarts later afterwards.
1122 GDBserver (consumer):
1124 - sets `about_to_request_buffer_space' breakpoint/lock.
1126 - updates the token unconditionally, using the current buffer
1127 control index, since it knows that the IP agent always writes to
1128 the rightmost, and due to the breakpoint, at most one IP thread
1129 can try to update the trace buffer concurrently to GDBserver, so
1130 there will be no danger of trace buffer control index wrap making
1131 the IPA write to the same index as GDBserver.
1133 - flushes the IP agent's trace buffer completely, and updates the
1134 current trace buffer control structure. GDBserver *always* wins.
1136 - removes the `about_to_request_buffer_space' breakpoint.
1138 The token is stored in the `trace_buffer_ctrl_curr' variable.
1139 Internally, it's bits are defined as:
1141 |-------------+-----+-------------+--------+-------------+--------------|
1142 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1143 |-------------+-----+-------------+--------+-------------+--------------|
1144 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1145 |-------------+-----+-------------+--------+-------------+--------------|
1147 GSB - GDBserver Stamp Bit
1148 PC - Previous Counter
1149 CC - Current Counter
1150 TBCI - Trace Buffer Control Index
1153 An IPA update of `trace_buffer_ctrl_curr' does:
1155 - read CC from the current token, save as PC.
1157 - atomically tries to write PC+1,CC
1159 A GDBserver update of `trace_buffer_ctrl_curr' does:
1161 - reads PC and CC from the current token.
1166 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1167 for the counters described below. The cleared bits are used to
1168 hold the index of the items of the `trace_buffer_ctrl' array that
1170 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1172 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1173 counter, and the `current' counter. */
1175 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1176 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1178 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1179 always stamps this bit as set. */
1180 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1182 #ifdef IN_PROCESS_AGENT
1183 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1184 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1186 # define TRACE_BUFFER_CTRL_CURR \
1187 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1191 /* The GDBserver side agent only needs one instance of this object, as
1192 it doesn't need to sync with itself. Define it as array anyway so
1193 that the rest of the code base doesn't need to care for the
1195 struct trace_buffer_control trace_buffer_ctrl
[1];
1196 # define TRACE_BUFFER_CTRL_CURR 0
1199 /* These are convenience macros used to access the current trace
1200 buffer control in effect. */
1201 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1202 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1203 #define trace_buffer_end_free \
1204 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1205 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1208 /* Macro that returns a pointer to the first traceframe in the buffer. */
1210 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1212 /* Macro that returns a pointer to the next traceframe in the buffer.
1213 If the computed location is beyond the wraparound point, subtract
1214 the offset of the wraparound. */
1216 #define NEXT_TRACEFRAME_1(TF) \
1217 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1219 #define NEXT_TRACEFRAME(TF) \
1220 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1221 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1222 ? (trace_buffer_wrap - trace_buffer_lo) \
1225 /* The difference between these counters represents the total number
1226 of complete traceframes present in the trace buffer. The IP agent
1227 writes to the write count, GDBserver writes to read count. */
1229 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1230 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1232 /* Convenience macro. */
1234 #define traceframe_count \
1235 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1237 /* The count of all traceframes created in the current run, including
1238 ones that were discarded to make room. */
1240 IP_AGENT_EXPORT
int traceframes_created
;
1242 #ifndef IN_PROCESS_AGENT
1244 /* Read-only regions are address ranges whose contents don't change,
1245 and so can be read from target memory even while looking at a trace
1246 frame. Without these, disassembly for instance will likely fail,
1247 because the program code is not usually collected into a trace
1248 frame. This data structure does not need to be very complicated or
1249 particularly efficient, it's only going to be used occasionally,
1250 and only by some commands. */
1252 struct readonly_region
1254 /* The bounds of the region. */
1255 CORE_ADDR start
, end
;
1257 /* Link to the next one. */
1258 struct readonly_region
*next
;
1261 /* Linked list of readonly regions. This list stays in effect from
1262 one tstart to the next. */
1264 static struct readonly_region
*readonly_regions
;
1268 /* The global that controls tracing overall. */
1270 IP_AGENT_EXPORT
int tracing
;
1272 #ifndef IN_PROCESS_AGENT
1274 /* Controls whether tracing should continue after GDB disconnects. */
1276 int disconnected_tracing
;
1278 /* The reason for the last tracing run to have stopped. We initialize
1279 to a distinct string so that GDB can distinguish between "stopped
1280 after running" and "stopped because never run" cases. */
1282 static const char *tracing_stop_reason
= "tnotrun";
1284 static int tracing_stop_tpnum
;
1286 /* 64-bit timestamps for the trace run's start and finish, expressed
1287 in microseconds from the Unix epoch. */
1289 LONGEST tracing_start_time
;
1290 LONGEST tracing_stop_time
;
1292 /* The (optional) user-supplied name of the user that started the run.
1293 This is an arbitrary string, and may be NULL. */
1295 char *tracing_user_name
;
1297 /* Optional user-supplied text describing the run. This is
1298 an arbitrary string, and may be NULL. */
1300 char *tracing_notes
;
1302 /* Optional user-supplied text explaining a tstop command. This is an
1303 arbitrary string, and may be NULL. */
1305 char *tracing_stop_note
;
1309 /* Functions local to this file. */
1311 /* Base "class" for tracepoint type specific data to be passed down to
1312 collect_data_at_tracepoint. */
1313 struct tracepoint_hit_ctx
1315 enum tracepoint_type type
;
1318 #ifdef IN_PROCESS_AGENT
1320 /* Fast/jump tracepoint specific data to be passed down to
1321 collect_data_at_tracepoint. */
1322 struct fast_tracepoint_ctx
1324 struct tracepoint_hit_ctx base
;
1326 struct regcache regcache
;
1327 int regcache_initted
;
1328 unsigned char *regspace
;
1330 unsigned char *regs
;
1331 struct tracepoint
*tpoint
;
1334 /* Static tracepoint specific data to be passed down to
1335 collect_data_at_tracepoint. */
1336 struct static_tracepoint_ctx
1338 struct tracepoint_hit_ctx base
;
1340 /* The regcache corresponding to the registers state at the time of
1341 the tracepoint hit. Initialized lazily, from REGS. */
1342 struct regcache regcache
;
1343 int regcache_initted
;
1345 /* The buffer space REGCACHE above uses. We use a separate buffer
1346 instead of letting the regcache malloc for both signal safety and
1347 performance reasons; this is allocated on the stack instead. */
1348 unsigned char *regspace
;
1350 /* The register buffer as passed on by lttng/ust. */
1351 struct registers
*regs
;
1353 /* The "printf" formatter and the args the user passed to the marker
1354 call. We use this to be able to collect "static trace data"
1359 /* The GDB tracepoint matching the probed marker that was "hit". */
1360 struct tracepoint
*tpoint
;
1365 /* Static tracepoint specific data to be passed down to
1366 collect_data_at_tracepoint. */
1367 struct trap_tracepoint_ctx
1369 struct tracepoint_hit_ctx base
;
1371 struct regcache
*regcache
;
1376 #ifndef IN_PROCESS_AGENT
1377 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1378 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1381 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1382 struct tracepoint
*tpoint
);
1384 #ifndef IN_PROCESS_AGENT
1385 static void clear_readonly_regions (void);
1386 static void clear_installed_tracepoints (void);
1389 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1391 struct tracepoint
*tpoint
);
1392 #ifndef IN_PROCESS_AGENT
1393 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1395 struct tracepoint
*tpoint
, int current_step
);
1396 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1397 CORE_ADDR
*jump_entry
);
1399 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1401 struct tracepoint
*tpoint
,
1402 struct traceframe
*tframe
,
1403 struct tracepoint_action
*taction
);
1405 #ifndef IN_PROCESS_AGENT
1406 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1408 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1409 static void download_tracepoint (struct tracepoint
*);
1410 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1411 static void clone_fast_tracepoint (struct tracepoint
*to
,
1412 const struct tracepoint
*from
);
1415 static LONGEST
get_timestamp (void);
1417 #if defined(__GNUC__)
1418 # define memory_barrier() asm volatile ("" : : : "memory")
1420 # define memory_barrier() do {} while (0)
1423 /* We only build the IPA if this builtin is supported, and there are
1424 no uses of this in GDBserver itself, so we're safe in defining this
1426 #define cmpxchg(mem, oldval, newval) \
1427 __sync_val_compare_and_swap (mem, oldval, newval)
1429 /* Record that an error occurred during expression evaluation. */
1432 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1433 enum eval_result_type rtype
)
1435 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1436 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1438 #ifdef IN_PROCESS_AGENT
1439 /* Only record the first error we get. */
1440 if (cmpxchg (&expr_eval_result
,
1442 rtype
) != expr_eval_no_error
)
1445 if (expr_eval_result
!= expr_eval_no_error
)
1449 error_tracepoint
= tpoint
;
1452 /* Trace buffer management. */
1455 clear_trace_buffer (void)
1457 trace_buffer_start
= trace_buffer_lo
;
1458 trace_buffer_free
= trace_buffer_lo
;
1459 trace_buffer_end_free
= trace_buffer_hi
;
1460 trace_buffer_wrap
= trace_buffer_hi
;
1461 /* A traceframe with zeroed fields marks the end of trace data. */
1462 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1463 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1464 traceframe_read_count
= traceframe_write_count
= 0;
1465 traceframes_created
= 0;
1468 #ifndef IN_PROCESS_AGENT
1471 clear_inferior_trace_buffer (void)
1473 CORE_ADDR ipa_trace_buffer_lo
;
1474 CORE_ADDR ipa_trace_buffer_hi
;
1475 struct traceframe ipa_traceframe
= { 0 };
1476 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1478 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1479 &ipa_trace_buffer_lo
);
1480 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1481 &ipa_trace_buffer_hi
);
1483 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1484 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1485 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1486 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1488 /* A traceframe with zeroed fields marks the end of trace data. */
1489 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1490 (unsigned char *) &ipa_trace_buffer_ctrl
,
1491 sizeof (ipa_trace_buffer_ctrl
));
1493 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1495 /* A traceframe with zeroed fields marks the end of trace data. */
1496 write_inferior_memory (ipa_trace_buffer_lo
,
1497 (unsigned char *) &ipa_traceframe
,
1498 sizeof (ipa_traceframe
));
1500 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1501 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1502 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1508 init_trace_buffer (LONGEST bufsize
)
1512 trace_buffer_size
= bufsize
;
1514 /* Make sure to internally allocate at least space for the EOB
1516 alloc_size
= (bufsize
< TRACEFRAME_EOB_MARKER_SIZE
1517 ? TRACEFRAME_EOB_MARKER_SIZE
: bufsize
);
1518 trace_buffer_lo
= xrealloc (trace_buffer_lo
, alloc_size
);
1520 trace_buffer_hi
= trace_buffer_lo
+ trace_buffer_size
;
1522 clear_trace_buffer ();
1525 #ifdef IN_PROCESS_AGENT
1527 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1528 about_to_request_buffer_space (void)
1530 /* GDBserver places breakpoint here while it goes about to flush
1531 data at random times. */
1532 UNKNOWN_SIDE_EFFECTS();
1537 /* Carve out a piece of the trace buffer, returning NULL in case of
1541 trace_buffer_alloc (size_t amt
)
1543 unsigned char *rslt
;
1544 struct trace_buffer_control
*tbctrl
;
1546 #ifdef IN_PROCESS_AGENT
1547 unsigned int prev
, prev_filtered
;
1548 unsigned int commit_count
;
1549 unsigned int commit
;
1550 unsigned int readout
;
1552 struct traceframe
*oldest
;
1553 unsigned char *new_start
;
1556 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1557 (long) amt
, (long) sizeof (struct traceframe
));
1559 /* Account for the EOB marker. */
1560 amt
+= TRACEFRAME_EOB_MARKER_SIZE
;
1562 #ifdef IN_PROCESS_AGENT
1566 /* Read the current token and extract the index to try to write to,
1567 storing it in CURR. */
1568 prev
= trace_buffer_ctrl_curr
;
1569 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1570 curr
= prev_filtered
+ 1;
1574 about_to_request_buffer_space ();
1576 /* Start out with a copy of the current state. GDBserver may be
1577 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1578 be able to commit anyway if that happens. */
1579 trace_buffer_ctrl
[curr
]
1580 = trace_buffer_ctrl
[prev_filtered
];
1581 trace_debug ("trying curr=%u", curr
);
1583 /* The GDBserver's agent doesn't need all that syncing, and always
1584 updates TCB 0 (there's only one, mind you). */
1587 tbctrl
= &trace_buffer_ctrl
[curr
];
1589 /* Offsets are easier to grok for debugging than raw addresses,
1590 especially for the small trace buffer sizes that are useful for
1592 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1594 (int) (tbctrl
->start
- trace_buffer_lo
),
1595 (int) (tbctrl
->free
- trace_buffer_lo
),
1596 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1597 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1598 (int) (trace_buffer_hi
- trace_buffer_lo
));
1600 /* The algorithm here is to keep trying to get a contiguous block of
1601 the requested size, possibly discarding older traceframes to free
1602 up space. Since free space might come in one or two pieces,
1603 depending on whether discarded traceframes wrapped around at the
1604 high end of the buffer, we test both pieces after each
1608 /* First, if we have two free parts, try the upper one first. */
1609 if (tbctrl
->end_free
< tbctrl
->free
)
1611 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1612 /* We have enough in the upper part. */
1616 /* Our high part of free space wasn't enough. Give up
1617 on it for now, set wraparound. We will recover the
1618 space later, if/when the wrapped-around traceframe is
1620 trace_debug ("Upper part too small, setting wraparound");
1621 tbctrl
->wrap
= tbctrl
->free
;
1622 tbctrl
->free
= trace_buffer_lo
;
1626 /* The normal case. */
1627 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1630 #ifdef IN_PROCESS_AGENT
1631 /* The IP Agent's buffer is always circular. It isn't used
1632 currently, but `circular_trace_buffer' could represent
1633 GDBserver's mode. If we didn't find space, ask GDBserver to
1636 flush_trace_buffer ();
1640 trace_debug ("gdbserver flushed buffer, retrying");
1644 /* GDBserver cancelled the tracing. Bail out as well. */
1647 /* If we're here, then neither part is big enough, and
1648 non-circular trace buffers are now full. */
1649 if (!circular_trace_buffer
)
1651 trace_debug ("Not enough space in the trace buffer");
1655 trace_debug ("Need more space in the trace buffer");
1657 /* If we have a circular buffer, we can try discarding the
1658 oldest traceframe and see if that helps. */
1659 oldest
= FIRST_TRACEFRAME ();
1660 if (oldest
->tpnum
== 0)
1662 /* Not good; we have no traceframes to free. Perhaps we're
1663 asking for a block that is larger than the buffer? In
1664 any case, give up. */
1665 trace_debug ("No traceframes to discard");
1669 /* We don't run this code in the in-process agent currently.
1670 E.g., we could leave the in-process agent in autonomous
1671 circular mode if we only have fast tracepoints. If we do
1672 that, then this bit becomes racy with GDBserver, which also
1673 writes to this counter. */
1674 --traceframe_write_count
;
1676 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1677 /* If we freed the traceframe that wrapped around, go back
1678 to the non-wrap case. */
1679 if (new_start
< tbctrl
->start
)
1681 trace_debug ("Discarding past the wraparound");
1682 tbctrl
->wrap
= trace_buffer_hi
;
1684 tbctrl
->start
= new_start
;
1685 tbctrl
->end_free
= tbctrl
->start
;
1687 trace_debug ("Discarded a traceframe\n"
1688 "Trace buffer [%d], start=%d free=%d "
1689 "endfree=%d wrap=%d hi=%d",
1691 (int) (tbctrl
->start
- trace_buffer_lo
),
1692 (int) (tbctrl
->free
- trace_buffer_lo
),
1693 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1694 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1695 (int) (trace_buffer_hi
- trace_buffer_lo
));
1697 /* Now go back around the loop. The discard might have resulted
1698 in either one or two pieces of free space, so we want to try
1699 both before freeing any more traceframes. */
1703 /* If we get here, we know we can provide the asked-for space. */
1705 rslt
= tbctrl
->free
;
1707 /* Adjust the request back down, now that we know we have space for
1708 the marker, but don't commit to AMT yet, we may still need to
1709 restart the operation if GDBserver touches the trace buffer
1710 (obviously only important in the in-process agent's version). */
1711 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1713 /* Or not. If GDBserver changed the trace buffer behind our back,
1714 we get to restart a new allocation attempt. */
1716 #ifdef IN_PROCESS_AGENT
1717 /* Build the tentative token. */
1718 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1719 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1720 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1724 /* Try to commit it. */
1725 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1726 if (readout
!= prev
)
1728 trace_debug ("GDBserver has touched the trace buffer, restarting."
1729 " (prev=%08x, commit=%08x, readout=%08x)",
1730 prev
, commit
, readout
);
1734 /* Hold your horses here. Even if that change was committed,
1735 GDBserver could come in, and clobber it. We need to hold to be
1736 able to tell if GDBserver clobbers before or after we committed
1737 the change. Whenever GDBserver goes about touching the IPA
1738 buffer, it sets a breakpoint in this routine, so we have a sync
1740 about_to_request_buffer_space ();
1742 /* Check if the change has been effective, even if GDBserver stopped
1743 us at the breakpoint. */
1746 unsigned int refetch
;
1750 refetch
= trace_buffer_ctrl_curr
;
1752 if (refetch
== commit
1753 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1756 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1757 "readout=%08x, refetch=%08x)",
1758 prev
, commit
, readout
, refetch
);
1762 trace_debug ("GDBserver has touched the trace buffer, not effective."
1763 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1764 prev
, commit
, readout
, refetch
);
1770 /* We have a new piece of the trace buffer. Hurray! */
1772 /* Add an EOB marker just past this allocation. */
1773 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1774 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1776 /* Adjust the request back down, now that we know we have space for
1778 amt
-= sizeof (struct traceframe
);
1782 trace_debug ("Allocated %d bytes", (int) amt
);
1783 trace_debug ("Trace buffer [%d] start=%d free=%d "
1784 "endfree=%d wrap=%d hi=%d",
1786 (int) (tbctrl
->start
- trace_buffer_lo
),
1787 (int) (tbctrl
->free
- trace_buffer_lo
),
1788 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1789 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1790 (int) (trace_buffer_hi
- trace_buffer_lo
));
1796 #ifndef IN_PROCESS_AGENT
1798 /* Return the total free space. This is not necessarily the largest
1799 block we can allocate, because of the two-part case. */
1804 if (trace_buffer_free
<= trace_buffer_end_free
)
1805 return trace_buffer_end_free
- trace_buffer_free
;
1807 return ((trace_buffer_end_free
- trace_buffer_lo
)
1808 + (trace_buffer_hi
- trace_buffer_free
));
1811 /* An 'S' in continuation packets indicates remainder are for
1814 static int seen_step_action_flag
;
1816 /* Create a tracepoint (location) with given number and address. Add this
1817 new tracepoint to list and sort this list. */
1819 static struct tracepoint
*
1820 add_tracepoint (int num
, CORE_ADDR addr
)
1822 struct tracepoint
*tpoint
, **tp_next
;
1824 tpoint
= xmalloc (sizeof (struct tracepoint
));
1825 tpoint
->number
= num
;
1826 tpoint
->address
= addr
;
1827 tpoint
->numactions
= 0;
1828 tpoint
->actions
= NULL
;
1829 tpoint
->actions_str
= NULL
;
1830 tpoint
->cond
= NULL
;
1831 tpoint
->num_step_actions
= 0;
1832 tpoint
->step_actions
= NULL
;
1833 tpoint
->step_actions_str
= NULL
;
1834 /* Start all off as regular (slow) tracepoints. */
1835 tpoint
->type
= trap_tracepoint
;
1836 tpoint
->orig_size
= -1;
1837 tpoint
->source_strings
= NULL
;
1838 tpoint
->compiled_cond
= 0;
1839 tpoint
->handle
= NULL
;
1840 tpoint
->next
= NULL
;
1842 /* Find a place to insert this tracepoint into list in order to keep
1843 the tracepoint list still in the ascending order. There may be
1844 multiple tracepoints at the same address as TPOINT's, and this
1845 guarantees TPOINT is inserted after all the tracepoints which are
1846 set at the same address. For example, fast tracepoints A, B, C are
1847 set at the same address, and D is to be insert at the same place as
1850 -->| A |--> | B |-->| C |->...
1852 One jump pad was created for tracepoint A, B, and C, and the target
1853 address of A is referenced/used in jump pad. So jump pad will let
1854 inferior jump to A. If D is inserted in front of A, like this,
1856 -->| D |-->| A |--> | B |-->| C |->...
1858 without updating jump pad, D is not reachable during collect, which
1859 is wrong. As we can see, the order of B, C and D doesn't matter, but
1860 A should always be the `first' one. */
1861 for (tp_next
= &tracepoints
;
1862 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1863 tp_next
= &(*tp_next
)->next
)
1865 tpoint
->next
= *tp_next
;
1867 last_tracepoint
= tpoint
;
1869 seen_step_action_flag
= 0;
1874 #ifndef IN_PROCESS_AGENT
1876 /* Return the tracepoint with the given number and address, or NULL. */
1878 static struct tracepoint
*
1879 find_tracepoint (int id
, CORE_ADDR addr
)
1881 struct tracepoint
*tpoint
;
1883 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1884 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1890 /* Remove TPOINT from global list. */
1893 remove_tracepoint (struct tracepoint
*tpoint
)
1895 struct tracepoint
*tp
, *tp_prev
;
1897 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1898 tp_prev
= tp
, tp
= tp
->next
)
1904 tp_prev
->next
= tp
->next
;
1906 tracepoints
= tp
->next
;
1912 /* There may be several tracepoints with the same number (because they
1913 are "locations", in GDB parlance); return the next one after the
1914 given tracepoint, or search from the beginning of the list if the
1915 first argument is NULL. */
1917 static struct tracepoint
*
1918 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1920 struct tracepoint
*tpoint
;
1923 tpoint
= prev_tp
->next
;
1925 tpoint
= tracepoints
;
1926 for (; tpoint
; tpoint
= tpoint
->next
)
1927 if (tpoint
->number
== num
)
1935 /* Append another action to perform when the tracepoint triggers. */
1938 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1944 seen_step_action_flag
= 1;
1952 char *act_start
= act
;
1953 struct tracepoint_action
*action
= NULL
;
1959 struct collect_memory_action
*maction
;
1963 maction
= xmalloc (sizeof *maction
);
1964 maction
->base
.type
= *act
;
1965 maction
->base
.ops
= &m_tracepoint_action_ops
;
1966 action
= &maction
->base
;
1969 is_neg
= (*act
== '-');
1972 act
= unpack_varlen_hex (act
, &basereg
);
1974 act
= unpack_varlen_hex (act
, &maction
->addr
);
1976 act
= unpack_varlen_hex (act
, &maction
->len
);
1977 maction
->basereg
= (is_neg
1980 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1981 pulongest (maction
->len
),
1982 paddress (maction
->addr
), maction
->basereg
);
1987 struct collect_registers_action
*raction
;
1989 raction
= xmalloc (sizeof *raction
);
1990 raction
->base
.type
= *act
;
1991 raction
->base
.ops
= &r_tracepoint_action_ops
;
1992 action
= &raction
->base
;
1994 trace_debug ("Want to collect registers");
1996 /* skip past hex digits of mask for now */
1997 while (isxdigit(*act
))
2003 struct collect_static_trace_data_action
*raction
;
2005 raction
= xmalloc (sizeof *raction
);
2006 raction
->base
.type
= *act
;
2007 raction
->base
.ops
= &l_tracepoint_action_ops
;
2008 action
= &raction
->base
;
2010 trace_debug ("Want to collect static trace data");
2015 trace_debug ("Unexpected step action, ignoring");
2020 struct eval_expr_action
*xaction
;
2022 xaction
= xmalloc (sizeof (*xaction
));
2023 xaction
->base
.type
= *act
;
2024 xaction
->base
.ops
= &x_tracepoint_action_ops
;
2025 action
= &xaction
->base
;
2027 trace_debug ("Want to evaluate expression");
2028 xaction
->expr
= gdb_parse_agent_expr (&act
);
2032 trace_debug ("unknown trace action '%c', ignoring...", *act
);
2041 if (seen_step_action_flag
)
2043 tpoint
->num_step_actions
++;
2045 tpoint
->step_actions
2046 = xrealloc (tpoint
->step_actions
,
2047 (sizeof (*tpoint
->step_actions
)
2048 * tpoint
->num_step_actions
));
2049 tpoint
->step_actions_str
2050 = xrealloc (tpoint
->step_actions_str
,
2051 (sizeof (*tpoint
->step_actions_str
)
2052 * tpoint
->num_step_actions
));
2053 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
2054 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
2055 = savestring (act_start
, act
- act_start
);
2059 tpoint
->numactions
++;
2061 = xrealloc (tpoint
->actions
,
2062 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
2064 = xrealloc (tpoint
->actions_str
,
2065 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
2066 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
2067 tpoint
->actions_str
[tpoint
->numactions
- 1]
2068 = savestring (act_start
, act
- act_start
);
2075 /* Find or create a trace state variable with the given number. */
2077 static struct trace_state_variable
*
2078 get_trace_state_variable (int num
)
2080 struct trace_state_variable
*tsv
;
2082 #ifdef IN_PROCESS_AGENT
2083 /* Search for an existing variable. */
2084 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
2085 if (tsv
->number
== num
)
2089 /* Search for an existing variable. */
2090 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
2091 if (tsv
->number
== num
)
2097 /* Find or create a trace state variable with the given number. */
2099 static struct trace_state_variable
*
2100 create_trace_state_variable (int num
, int gdb
)
2102 struct trace_state_variable
*tsv
;
2104 tsv
= get_trace_state_variable (num
);
2108 /* Create a new variable. */
2109 tsv
= xmalloc (sizeof (struct trace_state_variable
));
2111 tsv
->initial_value
= 0;
2115 #ifdef IN_PROCESS_AGENT
2118 tsv
->next
= alloced_trace_state_variables
;
2119 alloced_trace_state_variables
= tsv
;
2124 tsv
->next
= trace_state_variables
;
2125 trace_state_variables
= tsv
;
2130 IP_AGENT_EXPORT LONGEST
2131 get_trace_state_variable_value (int num
)
2133 struct trace_state_variable
*tsv
;
2135 tsv
= get_trace_state_variable (num
);
2139 trace_debug ("No trace state variable %d, skipping value get", num
);
2143 /* Call a getter function if we have one. While it's tempting to
2144 set up something to only call the getter once per tracepoint hit,
2145 it could run afoul of thread races. Better to let the getter
2146 handle it directly, if necessary to worry about it. */
2148 tsv
->value
= (tsv
->getter
) ();
2150 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2151 num
, plongest (tsv
->value
));
2156 IP_AGENT_EXPORT
void
2157 set_trace_state_variable_value (int num
, LONGEST val
)
2159 struct trace_state_variable
*tsv
;
2161 tsv
= get_trace_state_variable (num
);
2165 trace_debug ("No trace state variable %d, skipping value set", num
);
2173 agent_get_trace_state_variable_value (int num
)
2175 return get_trace_state_variable_value (num
);
2179 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2181 set_trace_state_variable_value (num
, val
);
2185 set_trace_state_variable_name (int num
, const char *name
)
2187 struct trace_state_variable
*tsv
;
2189 tsv
= get_trace_state_variable (num
);
2193 trace_debug ("No trace state variable %d, skipping name set", num
);
2197 tsv
->name
= (char *) name
;
2201 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2203 struct trace_state_variable
*tsv
;
2205 tsv
= get_trace_state_variable (num
);
2209 trace_debug ("No trace state variable %d, skipping getter set", num
);
2213 tsv
->getter
= getter
;
2216 /* Add a raw traceframe for the given tracepoint. */
2218 static struct traceframe
*
2219 add_traceframe (struct tracepoint
*tpoint
)
2221 struct traceframe
*tframe
;
2223 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2228 tframe
->tpnum
= tpoint
->number
;
2229 tframe
->data_size
= 0;
2234 /* Add a block to the traceframe currently being worked on. */
2236 static unsigned char *
2237 add_traceframe_block (struct traceframe
*tframe
,
2238 struct tracepoint
*tpoint
, int amt
)
2240 unsigned char *block
;
2245 block
= trace_buffer_alloc (amt
);
2250 gdb_assert (tframe
->tpnum
== tpoint
->number
);
2252 tframe
->data_size
+= amt
;
2253 tpoint
->traceframe_usage
+= amt
;
2258 /* Flag that the current traceframe is finished. */
2261 finish_traceframe (struct traceframe
*tframe
)
2263 ++traceframe_write_count
;
2264 ++traceframes_created
;
2267 #ifndef IN_PROCESS_AGENT
2269 /* Given a traceframe number NUM, find the NUMth traceframe in the
2272 static struct traceframe
*
2273 find_traceframe (int num
)
2275 struct traceframe
*tframe
;
2278 for (tframe
= FIRST_TRACEFRAME ();
2280 tframe
= NEXT_TRACEFRAME (tframe
))
2291 get_traceframe_address (struct traceframe
*tframe
)
2294 struct tracepoint
*tpoint
;
2296 addr
= traceframe_get_pc (tframe
);
2301 /* Fallback strategy, will be incorrect for while-stepping frames
2302 and multi-location tracepoints. */
2303 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2304 return tpoint
->address
;
2307 /* Search for the next traceframe whose address is inside or outside
2310 static struct traceframe
*
2311 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2314 struct traceframe
*tframe
;
2317 *tfnump
= current_traceframe
+ 1;
2318 tframe
= find_traceframe (*tfnump
);
2319 /* The search is not supposed to wrap around. */
2326 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2328 tfaddr
= get_traceframe_address (tframe
);
2330 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2331 : (lo
> tfaddr
|| tfaddr
> hi
))
2340 /* Search for the next traceframe recorded by the given tracepoint.
2341 Note that for multi-location tracepoints, this will find whatever
2342 location appears first. */
2344 static struct traceframe
*
2345 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2347 struct traceframe
*tframe
;
2349 *tfnump
= current_traceframe
+ 1;
2350 tframe
= find_traceframe (*tfnump
);
2351 /* The search is not supposed to wrap around. */
2358 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2360 if (tframe
->tpnum
== num
)
2371 #ifndef IN_PROCESS_AGENT
2373 /* Clear all past trace state. */
2376 cmd_qtinit (char *packet
)
2378 struct trace_state_variable
*tsv
, *prev
, *next
;
2380 /* Make sure we don't try to read from a trace frame. */
2381 current_traceframe
= -1;
2385 trace_debug ("Initializing the trace");
2387 clear_installed_tracepoints ();
2388 clear_readonly_regions ();
2391 last_tracepoint
= NULL
;
2393 /* Clear out any leftover trace state variables. Ones with target
2394 defined getters should be kept however. */
2396 tsv
= trace_state_variables
;
2399 trace_debug ("Looking at var %d", tsv
->number
);
2400 if (tsv
->getter
== NULL
)
2406 trace_state_variables
= next
;
2407 trace_debug ("Deleting var %d", tsv
->number
);
2418 clear_trace_buffer ();
2419 clear_inferior_trace_buffer ();
2424 /* Unprobe the UST marker at ADDRESS. */
2427 unprobe_marker_at (CORE_ADDR address
)
2429 char cmd
[IPA_CMD_BUF_SIZE
];
2431 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2432 run_inferior_command (cmd
, strlen (cmd
) + 1);
2435 /* Restore the program to its pre-tracing state. This routine may be called
2436 in error situations, so it needs to be careful about only restoring
2437 from known-valid bits. */
2440 clear_installed_tracepoints (void)
2442 struct tracepoint
*tpoint
;
2443 struct tracepoint
*prev_stpoint
;
2447 prev_stpoint
= NULL
;
2449 /* Restore any bytes overwritten by tracepoints. */
2450 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2452 /* Catch the case where we might try to remove a tracepoint that
2453 was never actually installed. */
2454 if (tpoint
->handle
== NULL
)
2456 trace_debug ("Tracepoint %d at 0x%s was "
2457 "never installed, nothing to clear",
2458 tpoint
->number
, paddress (tpoint
->address
));
2462 switch (tpoint
->type
)
2464 case trap_tracepoint
:
2465 delete_breakpoint (tpoint
->handle
);
2467 case fast_tracepoint
:
2468 delete_fast_tracepoint_jump (tpoint
->handle
);
2470 case static_tracepoint
:
2471 if (prev_stpoint
!= NULL
2472 && prev_stpoint
->address
== tpoint
->address
)
2473 /* Nothing to do. We already unprobed a tracepoint set at
2474 this marker address (and there can only be one probe
2479 unprobe_marker_at (tpoint
->address
);
2480 prev_stpoint
= tpoint
;
2485 tpoint
->handle
= NULL
;
2491 /* Parse a packet that defines a tracepoint. */
2494 cmd_qtdp (char *own_buf
)
2497 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2498 int trail_hyphen
= 0;
2502 struct tracepoint
*tpoint
;
2504 char *packet
= own_buf
;
2506 packet
+= strlen ("QTDP:");
2508 /* A hyphen at the beginning marks a packet specifying actions for a
2509 tracepoint already supplied. */
2516 packet
= unpack_varlen_hex (packet
, &num
);
2517 ++packet
; /* skip a colon */
2518 packet
= unpack_varlen_hex (packet
, &addr
);
2519 ++packet
; /* skip a colon */
2521 /* See if we already have this tracepoint. */
2522 tpoint
= find_tracepoint (num
, addr
);
2526 /* Duplicate tracepoints are never allowed. */
2529 trace_debug ("Tracepoint error: tracepoint %d"
2530 " at 0x%s already exists",
2531 (int) num
, paddress (addr
));
2532 write_enn (own_buf
);
2536 tpoint
= add_tracepoint (num
, addr
);
2538 tpoint
->enabled
= (*packet
== 'E');
2539 ++packet
; /* skip 'E' */
2540 ++packet
; /* skip a colon */
2541 packet
= unpack_varlen_hex (packet
, &count
);
2542 tpoint
->step_count
= count
;
2543 ++packet
; /* skip a colon */
2544 packet
= unpack_varlen_hex (packet
, &count
);
2545 tpoint
->pass_count
= count
;
2546 /* See if we have any of the additional optional fields. */
2547 while (*packet
== ':')
2552 tpoint
->type
= fast_tracepoint
;
2554 packet
= unpack_varlen_hex (packet
, &count
);
2555 tpoint
->orig_size
= count
;
2557 else if (*packet
== 'S')
2559 tpoint
->type
= static_tracepoint
;
2562 else if (*packet
== 'X')
2564 actparm
= (char *) packet
;
2565 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2568 else if (*packet
== '-')
2570 else if (*packet
== '\0')
2573 trace_debug ("Unknown optional tracepoint field");
2578 trace_debug ("Also has actions\n");
2581 trace_debug ("Defined %stracepoint %d at 0x%s, "
2582 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2583 tpoint
->type
== fast_tracepoint
? "fast "
2584 : tpoint
->type
== static_tracepoint
? "static " : "",
2585 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2586 tpoint
->step_count
, tpoint
->pass_count
);
2589 add_tracepoint_action (tpoint
, packet
);
2592 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2593 (int) num
, paddress (addr
));
2594 write_enn (own_buf
);
2598 /* Install tracepoint during tracing only once for each tracepoint location.
2599 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2600 determine the last QTDP packet for one tracepoint location by checking
2601 trailing hyphen in QTDP packet. */
2602 if (tracing
&& !trail_hyphen
)
2604 struct tracepoint
*tp
= NULL
;
2606 /* Pause all threads temporarily while we patch tracepoints. */
2609 /* download_tracepoint will update global `tracepoints'
2610 list, so it is unsafe to leave threads in jump pad. */
2611 stabilize_threads ();
2613 /* Freeze threads. */
2617 if (tpoint
->type
!= trap_tracepoint
)
2619 /* Find another fast or static tracepoint at the same address. */
2620 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2622 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2623 && tp
->number
!= tpoint
->number
)
2627 /* TPOINT is installed at the same address as TP. */
2630 if (tpoint
->type
== fast_tracepoint
)
2631 clone_fast_tracepoint (tpoint
, tp
);
2632 else if (tpoint
->type
== static_tracepoint
)
2633 tpoint
->handle
= (void *) -1;
2637 if (use_agent
&& tpoint
->type
== fast_tracepoint
2638 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
2640 /* Download and install fast tracepoint by agent. */
2641 if (tracepoint_send_agent (tpoint
) == 0)
2645 write_enn (own_buf
);
2646 remove_tracepoint (tpoint
);
2651 download_tracepoint (tpoint
);
2653 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2655 install_tracepoint (tpoint
, own_buf
);
2656 if (strcmp (own_buf
, "OK") != 0)
2657 remove_tracepoint (tpoint
);
2671 cmd_qtdpsrc (char *own_buf
)
2673 ULONGEST num
, addr
, start
, slen
;
2674 struct tracepoint
*tpoint
;
2675 char *packet
= own_buf
;
2676 char *saved
, *srctype
, *src
;
2678 struct source_string
*last
, *newlast
;
2680 packet
+= strlen ("QTDPsrc:");
2682 packet
= unpack_varlen_hex (packet
, &num
);
2683 ++packet
; /* skip a colon */
2684 packet
= unpack_varlen_hex (packet
, &addr
);
2685 ++packet
; /* skip a colon */
2687 /* See if we already have this tracepoint. */
2688 tpoint
= find_tracepoint (num
, addr
);
2692 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2693 (int) num
, paddress (addr
));
2694 write_enn (own_buf
);
2699 packet
= strchr (packet
, ':');
2700 srctype
= xmalloc (packet
- saved
+ 1);
2701 memcpy (srctype
, saved
, packet
- saved
);
2702 srctype
[packet
- saved
] = '\0';
2704 packet
= unpack_varlen_hex (packet
, &start
);
2705 ++packet
; /* skip a colon */
2706 packet
= unpack_varlen_hex (packet
, &slen
);
2707 ++packet
; /* skip a colon */
2708 src
= xmalloc (slen
+ 1);
2709 nbytes
= hex2bin (packet
, (gdb_byte
*) src
, strlen (packet
) / 2);
2712 newlast
= xmalloc (sizeof (struct source_string
));
2713 newlast
->type
= srctype
;
2715 newlast
->next
= NULL
;
2716 /* Always add a source string to the end of the list;
2717 this keeps sequences of actions/commands in the right
2719 if (tpoint
->source_strings
)
2721 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2723 last
->next
= newlast
;
2726 tpoint
->source_strings
= newlast
;
2732 cmd_qtdv (char *own_buf
)
2734 ULONGEST num
, val
, builtin
;
2737 struct trace_state_variable
*tsv
;
2738 char *packet
= own_buf
;
2740 packet
+= strlen ("QTDV:");
2742 packet
= unpack_varlen_hex (packet
, &num
);
2743 ++packet
; /* skip a colon */
2744 packet
= unpack_varlen_hex (packet
, &val
);
2745 ++packet
; /* skip a colon */
2746 packet
= unpack_varlen_hex (packet
, &builtin
);
2747 ++packet
; /* skip a colon */
2749 nbytes
= strlen (packet
) / 2;
2750 varname
= xmalloc (nbytes
+ 1);
2751 nbytes
= hex2bin (packet
, (gdb_byte
*) varname
, nbytes
);
2752 varname
[nbytes
] = '\0';
2754 tsv
= create_trace_state_variable (num
, 1);
2755 tsv
->initial_value
= (LONGEST
) val
;
2756 tsv
->name
= varname
;
2758 set_trace_state_variable_value (num
, (LONGEST
) val
);
2764 cmd_qtenable_disable (char *own_buf
, int enable
)
2766 char *packet
= own_buf
;
2768 struct tracepoint
*tp
;
2770 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2771 packet
= unpack_varlen_hex (packet
, &num
);
2772 ++packet
; /* skip a colon */
2773 packet
= unpack_varlen_hex (packet
, &addr
);
2775 tp
= find_tracepoint (num
, addr
);
2779 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2781 trace_debug ("Tracepoint %d at 0x%s is already %s",
2782 (int) num
, paddress (addr
),
2783 enable
? "enabled" : "disabled");
2788 trace_debug ("%s tracepoint %d at 0x%s",
2789 enable
? "Enabling" : "Disabling",
2790 (int) num
, paddress (addr
));
2792 tp
->enabled
= enable
;
2794 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2797 int offset
= offsetof (struct tracepoint
, enabled
);
2798 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2800 ret
= prepare_to_access_memory ();
2803 trace_debug ("Failed to temporarily stop inferior threads");
2804 write_enn (own_buf
);
2808 ret
= write_inferior_integer (obj_addr
, enable
);
2809 done_accessing_memory ();
2813 trace_debug ("Cannot write enabled flag into "
2814 "inferior process memory");
2815 write_enn (own_buf
);
2824 trace_debug ("Tracepoint %d at 0x%s not found",
2825 (int) num
, paddress (addr
));
2826 write_enn (own_buf
);
2831 cmd_qtv (char *own_buf
)
2836 char *packet
= own_buf
;
2838 packet
+= strlen ("qTV:");
2839 unpack_varlen_hex (packet
, &num
);
2841 if (current_traceframe
>= 0)
2843 err
= traceframe_read_tsv ((int) num
, &val
);
2846 strcpy (own_buf
, "U");
2850 /* Only make tsv's be undefined before the first trace run. After a
2851 trace run is over, the user might want to see the last value of
2852 the tsv, and it might not be available in a traceframe. */
2853 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2855 strcpy (own_buf
, "U");
2859 val
= get_trace_state_variable_value (num
);
2861 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2864 /* Clear out the list of readonly regions. */
2867 clear_readonly_regions (void)
2869 struct readonly_region
*roreg
;
2871 while (readonly_regions
)
2873 roreg
= readonly_regions
;
2874 readonly_regions
= readonly_regions
->next
;
2879 /* Parse the collection of address ranges whose contents GDB believes
2880 to be unchanging and so can be read directly from target memory
2881 even while looking at a traceframe. */
2884 cmd_qtro (char *own_buf
)
2886 ULONGEST start
, end
;
2887 struct readonly_region
*roreg
;
2888 char *packet
= own_buf
;
2890 trace_debug ("Want to mark readonly regions");
2892 clear_readonly_regions ();
2894 packet
+= strlen ("QTro");
2896 while (*packet
== ':')
2898 ++packet
; /* skip a colon */
2899 packet
= unpack_varlen_hex (packet
, &start
);
2900 ++packet
; /* skip a comma */
2901 packet
= unpack_varlen_hex (packet
, &end
);
2902 roreg
= xmalloc (sizeof (struct readonly_region
));
2903 roreg
->start
= start
;
2905 roreg
->next
= readonly_regions
;
2906 readonly_regions
= roreg
;
2907 trace_debug ("Added readonly region from 0x%s to 0x%s",
2908 paddress (roreg
->start
), paddress (roreg
->end
));
2914 /* Test to see if the given range is in our list of readonly ranges.
2915 We only test for being entirely within a range, GDB is not going to
2916 send a single memory packet that spans multiple regions. */
2919 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2921 struct readonly_region
*roreg
;
2923 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2924 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2930 /* The maximum size of a jump pad entry. */
2931 static const int max_jump_pad_size
= 0x100;
2933 static CORE_ADDR gdb_jump_pad_head
;
2935 /* Return the address of the next free jump space. */
2938 get_jump_space_head (void)
2940 if (gdb_jump_pad_head
== 0)
2942 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2943 &gdb_jump_pad_head
))
2945 internal_error (__FILE__
, __LINE__
,
2946 "error extracting jump_pad_buffer");
2950 return gdb_jump_pad_head
;
2953 /* Reserve USED bytes from the jump space. */
2956 claim_jump_space (ULONGEST used
)
2958 trace_debug ("claim_jump_space reserves %s bytes at %s",
2959 pulongest (used
), paddress (gdb_jump_pad_head
));
2960 gdb_jump_pad_head
+= used
;
2963 static CORE_ADDR trampoline_buffer_head
= 0;
2964 static CORE_ADDR trampoline_buffer_tail
;
2966 /* Reserve USED bytes from the trampoline buffer and return the
2967 address of the start of the reserved space in TRAMPOLINE. Returns
2968 non-zero if the space is successfully claimed. */
2971 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2973 if (!trampoline_buffer_head
)
2975 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2976 &trampoline_buffer_tail
))
2978 internal_error (__FILE__
, __LINE__
,
2979 "error extracting trampoline_buffer");
2982 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2983 &trampoline_buffer_head
))
2985 internal_error (__FILE__
, __LINE__
,
2986 "error extracting trampoline_buffer_end");
2990 /* Start claiming space from the top of the trampoline space. If
2991 the space is located at the bottom of the virtual address space,
2992 this reduces the possibility that corruption will occur if a null
2993 pointer is used to write to memory. */
2994 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2996 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
3001 trampoline_buffer_head
-= used
;
3003 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
3004 pulongest (used
), paddress (trampoline_buffer_head
));
3006 *trampoline
= trampoline_buffer_head
;
3010 /* Returns non-zero if there is space allocated for use in trampolines
3011 for fast tracepoints. */
3014 have_fast_tracepoint_trampoline_buffer (char *buf
)
3016 CORE_ADDR trampoline_end
, errbuf
;
3018 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
3021 internal_error (__FILE__
, __LINE__
,
3022 "error extracting trampoline_buffer_end");
3028 strcpy (buf
, "was claiming");
3029 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
3032 internal_error (__FILE__
, __LINE__
,
3033 "error extracting errbuf");
3036 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
3039 return trampoline_end
!= 0;
3042 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3043 the command fails, or 0 otherwise. If the command ran
3044 successfully, but probing the marker failed, ERROUT will be filled
3045 with the error to reply to GDB, and -1 is also returned. This
3046 allows directly passing IPA errors to GDB. */
3049 probe_marker_at (CORE_ADDR address
, char *errout
)
3051 char cmd
[IPA_CMD_BUF_SIZE
];
3054 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
3055 err
= run_inferior_command (cmd
, strlen (cmd
) + 1);
3061 strcpy (errout
, cmd
);
3070 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
3072 to
->jump_pad
= from
->jump_pad
;
3073 to
->jump_pad_end
= from
->jump_pad_end
;
3074 to
->trampoline
= from
->trampoline
;
3075 to
->trampoline_end
= from
->trampoline_end
;
3076 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
3077 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
3078 to
->handle
= from
->handle
;
3080 gdb_assert (from
->handle
);
3081 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
3084 #define MAX_JUMP_SIZE 20
3086 /* Install fast tracepoint. Return 0 if successful, otherwise return
3090 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
3092 CORE_ADDR jentry
, jump_entry
;
3093 CORE_ADDR trampoline
;
3094 ULONGEST trampoline_size
;
3096 /* The jump to the jump pad of the last fast tracepoint
3098 unsigned char fjump
[MAX_JUMP_SIZE
];
3099 ULONGEST fjump_size
;
3101 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
3103 trace_debug ("Requested a fast tracepoint on an instruction "
3104 "that is of less than the minimum length.");
3108 jentry
= jump_entry
= get_jump_space_head ();
3111 trampoline_size
= 0;
3113 /* Install the jump pad. */
3114 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3116 ipa_sym_addrs
.addr_gdb_collect
,
3117 ipa_sym_addrs
.addr_collecting
,
3120 &trampoline
, &trampoline_size
,
3122 &tpoint
->adjusted_insn_addr
,
3123 &tpoint
->adjusted_insn_addr_end
,
3130 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3133 if (tpoint
->handle
!= NULL
)
3135 tpoint
->jump_pad
= jump_entry
;
3136 tpoint
->jump_pad_end
= jentry
;
3137 tpoint
->trampoline
= trampoline
;
3138 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3140 /* Pad to 8-byte alignment. */
3141 jentry
= ((jentry
+ 7) & ~0x7);
3142 claim_jump_space (jentry
- jump_entry
);
3149 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3152 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3154 tpoint
->handle
= NULL
;
3157 if (tpoint
->type
== trap_tracepoint
)
3159 /* Tracepoints are installed as memory breakpoints. Just go
3160 ahead and install the trap. The breakpoints module
3161 handles duplicated breakpoints, and the memory read
3162 routine handles un-patching traps from memory reads. */
3163 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3164 tracepoint_handler
);
3166 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3168 if (!agent_loaded_p ())
3170 trace_debug ("Requested a %s tracepoint, but fast "
3171 "tracepoints aren't supported.",
3172 tpoint
->type
== static_tracepoint
? "static" : "fast");
3173 write_e_ipa_not_loaded (own_buf
);
3176 if (tpoint
->type
== static_tracepoint
3177 && !in_process_agent_supports_ust ())
3179 trace_debug ("Requested a static tracepoint, but static "
3180 "tracepoints are not supported.");
3181 write_e_ust_not_loaded (own_buf
);
3185 if (tpoint
->type
== fast_tracepoint
)
3186 install_fast_tracepoint (tpoint
, own_buf
);
3189 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3190 tpoint
->handle
= (void *) -1;
3195 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3197 if (tpoint
->handle
== NULL
)
3199 if (*own_buf
== '\0')
3200 write_enn (own_buf
);
3206 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3209 cmd_qtstart (char *packet
)
3211 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3212 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3214 trace_debug ("Starting the trace");
3216 /* Pause all threads temporarily while we patch tracepoints. */
3219 /* Get threads out of jump pads. Safe to do here, since this is a
3220 top level command. And, required to do here, since we're
3221 deleting/rewriting jump pads. */
3223 stabilize_threads ();
3225 /* Freeze threads. */
3228 /* Sync the fast tracepoints list in the inferior ftlib. */
3229 if (agent_loaded_p ())
3230 download_trace_state_variables ();
3232 /* No previous fast tpoint yet. */
3233 prev_ftpoint
= NULL
;
3235 /* No previous static tpoint yet. */
3236 prev_stpoint
= NULL
;
3240 /* Start out empty. */
3241 if (agent_loaded_p ())
3242 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
3244 /* Download and install tracepoints. */
3245 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3247 /* Ensure all the hit counts start at zero. */
3248 tpoint
->hit_count
= 0;
3249 tpoint
->traceframe_usage
= 0;
3251 if (tpoint
->type
== trap_tracepoint
)
3253 /* Tracepoints are installed as memory breakpoints. Just go
3254 ahead and install the trap. The breakpoints module
3255 handles duplicated breakpoints, and the memory read
3256 routine handles un-patching traps from memory reads. */
3257 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3258 tracepoint_handler
);
3260 else if (tpoint
->type
== fast_tracepoint
3261 || tpoint
->type
== static_tracepoint
)
3263 if (maybe_write_ipa_not_loaded (packet
))
3265 trace_debug ("Requested a %s tracepoint, but fast "
3266 "tracepoints aren't supported.",
3267 tpoint
->type
== static_tracepoint
3268 ? "static" : "fast");
3272 if (tpoint
->type
== fast_tracepoint
)
3275 = use_agent
&& agent_capability_check (AGENT_CAPA_FAST_TRACE
);
3277 if (prev_ftpoint
!= NULL
3278 && prev_ftpoint
->address
== tpoint
->address
)
3281 tracepoint_send_agent (tpoint
);
3283 download_tracepoint_1 (tpoint
);
3285 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3289 /* Tracepoint is installed successfully? */
3292 /* Download and install fast tracepoint by agent. */
3294 installed
= !tracepoint_send_agent (tpoint
);
3297 download_tracepoint_1 (tpoint
);
3298 installed
= !install_fast_tracepoint (tpoint
, packet
);
3302 prev_ftpoint
= tpoint
;
3307 if (!in_process_agent_supports_ust ())
3309 trace_debug ("Requested a static tracepoint, but static "
3310 "tracepoints are not supported.");
3314 download_tracepoint_1 (tpoint
);
3315 /* Can only probe a given marker once. */
3316 if (prev_stpoint
!= NULL
3317 && prev_stpoint
->address
== tpoint
->address
)
3318 tpoint
->handle
= (void *) -1;
3321 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3323 tpoint
->handle
= (void *) -1;
3325 /* So that we can handle multiple static tracepoints
3326 at the same address easily. */
3327 prev_stpoint
= tpoint
;
3333 tpptr
= tpoint
->obj_addr_on_target
;
3335 if (tpoint
== tracepoints
)
3336 /* First object in list, set the head pointer in the
3338 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3340 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
3345 /* Any failure in the inner loop is sufficient cause to give
3347 if (tpoint
->handle
== NULL
)
3351 /* Any error in tracepoint insertion is unacceptable; better to
3352 address the problem now, than end up with a useless or misleading
3356 clear_installed_tracepoints ();
3357 if (*packet
== '\0')
3363 stopping_tracepoint
= NULL
;
3364 trace_buffer_is_full
= 0;
3365 expr_eval_result
= expr_eval_no_error
;
3366 error_tracepoint
= NULL
;
3367 tracing_start_time
= get_timestamp ();
3369 /* Tracing is now active, hits will now start being logged. */
3372 if (agent_loaded_p ())
3374 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3376 internal_error (__FILE__
, __LINE__
,
3377 "Error setting tracing variable in lib");
3380 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3383 internal_error (__FILE__
, __LINE__
,
3384 "Error clearing stopping_tracepoint variable"
3388 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3390 internal_error (__FILE__
, __LINE__
,
3391 "Error clearing trace_buffer_is_full variable"
3395 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3396 stop_tracing_handler
);
3397 if (stop_tracing_bkpt
== NULL
)
3398 error ("Error setting stop_tracing breakpoint");
3400 flush_trace_buffer_bkpt
3401 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3402 flush_trace_buffer_handler
);
3403 if (flush_trace_buffer_bkpt
== NULL
)
3404 error ("Error setting flush_trace_buffer breakpoint");
3412 /* End a tracing run, filling in a stop reason to report back to GDB,
3413 and removing the tracepoints from the code. */
3420 trace_debug ("Tracing is already off, ignoring");
3424 trace_debug ("Stopping the trace");
3426 /* Pause all threads before removing fast jumps from memory,
3427 breakpoints, and touching IPA state variables (inferior memory).
3428 Some thread may hit the internal tracing breakpoints, or be
3429 collecting this moment, but that's ok, we don't release the
3430 tpoint object's memory or the jump pads here (we only do that
3431 when we're sure we can move all threads out of the jump pads).
3432 We can't now, since we may be getting here due to the inferior
3433 agent calling us. */
3436 /* Stop logging. Tracepoints can still be hit, but they will not be
3439 if (agent_loaded_p ())
3441 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3443 internal_error (__FILE__
, __LINE__
,
3444 "Error clearing tracing variable in lib");
3448 tracing_stop_time
= get_timestamp ();
3449 tracing_stop_reason
= "t???";
3450 tracing_stop_tpnum
= 0;
3451 if (stopping_tracepoint
)
3453 trace_debug ("Stopping the trace because "
3454 "tracepoint %d was hit %" PRIu64
" times",
3455 stopping_tracepoint
->number
,
3456 stopping_tracepoint
->pass_count
);
3457 tracing_stop_reason
= "tpasscount";
3458 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3460 else if (trace_buffer_is_full
)
3462 trace_debug ("Stopping the trace because the trace buffer is full");
3463 tracing_stop_reason
= "tfull";
3465 else if (expr_eval_result
!= expr_eval_no_error
)
3467 trace_debug ("Stopping the trace because of an expression eval error");
3468 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3469 tracing_stop_tpnum
= error_tracepoint
->number
;
3471 #ifndef IN_PROCESS_AGENT
3472 else if (!gdb_connected ())
3474 trace_debug ("Stopping the trace because GDB disconnected");
3475 tracing_stop_reason
= "tdisconnected";
3480 trace_debug ("Stopping the trace because of a tstop command");
3481 tracing_stop_reason
= "tstop";
3484 stopping_tracepoint
= NULL
;
3485 error_tracepoint
= NULL
;
3487 /* Clear out the tracepoints. */
3488 clear_installed_tracepoints ();
3490 if (agent_loaded_p ())
3492 /* Pull in fast tracepoint trace frames from the inferior lib
3493 buffer into our buffer, even if our buffer is already full,
3494 because we want to present the full number of created frames
3495 in addition to what fit in the trace buffer. */
3496 upload_fast_traceframes ();
3499 if (stop_tracing_bkpt
!= NULL
)
3501 delete_breakpoint (stop_tracing_bkpt
);
3502 stop_tracing_bkpt
= NULL
;
3505 if (flush_trace_buffer_bkpt
!= NULL
)
3507 delete_breakpoint (flush_trace_buffer_bkpt
);
3508 flush_trace_buffer_bkpt
= NULL
;
3515 stop_tracing_handler (CORE_ADDR addr
)
3517 trace_debug ("lib hit stop_tracing");
3519 /* Don't actually handle it here. When we stop tracing we remove
3520 breakpoints from the inferior, and that is not allowed in a
3521 breakpoint handler (as the caller is walking the breakpoint
3527 flush_trace_buffer_handler (CORE_ADDR addr
)
3529 trace_debug ("lib hit flush_trace_buffer");
3534 cmd_qtstop (char *packet
)
3541 cmd_qtdisconnected (char *own_buf
)
3544 char *packet
= own_buf
;
3546 packet
+= strlen ("QTDisconnected:");
3548 unpack_varlen_hex (packet
, &setting
);
3552 disconnected_tracing
= setting
;
3556 cmd_qtframe (char *own_buf
)
3558 ULONGEST frame
, pc
, lo
, hi
, num
;
3560 struct traceframe
*tframe
;
3561 char *packet
= own_buf
;
3563 packet
+= strlen ("QTFrame:");
3565 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3567 packet
+= strlen ("pc:");
3568 unpack_varlen_hex (packet
, &pc
);
3569 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3570 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3572 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3574 packet
+= strlen ("range:");
3575 packet
= unpack_varlen_hex (packet
, &lo
);
3577 unpack_varlen_hex (packet
, &hi
);
3578 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3579 paddress (lo
), paddress (hi
));
3580 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3582 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3584 packet
+= strlen ("outside:");
3585 packet
= unpack_varlen_hex (packet
, &lo
);
3587 unpack_varlen_hex (packet
, &hi
);
3588 trace_debug ("Want to find next traceframe "
3589 "outside the range 0x%s to 0x%s",
3590 paddress (lo
), paddress (hi
));
3591 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3593 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3595 packet
+= strlen ("tdp:");
3596 unpack_varlen_hex (packet
, &num
);
3598 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3599 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3603 unpack_varlen_hex (packet
, &frame
);
3604 tfnum
= (int) frame
;
3607 trace_debug ("Want to stop looking at traceframes");
3608 current_traceframe
= -1;
3612 trace_debug ("Want to look at traceframe %d", tfnum
);
3613 tframe
= find_traceframe (tfnum
);
3618 current_traceframe
= tfnum
;
3619 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3622 sprintf (own_buf
, "F-1");
3626 cmd_qtstatus (char *packet
)
3628 char *stop_reason_rsp
= NULL
;
3629 char *buf1
, *buf2
, *buf3
, *str
;
3632 /* Translate the plain text of the notes back into hex for
3635 str
= (tracing_user_name
? tracing_user_name
: "");
3636 slen
= strlen (str
);
3637 buf1
= (char *) alloca (slen
* 2 + 1);
3638 bin2hex ((gdb_byte
*) str
, buf1
, slen
);
3640 str
= (tracing_notes
? tracing_notes
: "");
3641 slen
= strlen (str
);
3642 buf2
= (char *) alloca (slen
* 2 + 1);
3643 bin2hex ((gdb_byte
*) str
, buf2
, slen
);
3645 str
= (tracing_stop_note
? tracing_stop_note
: "");
3646 slen
= strlen (str
);
3647 buf3
= (char *) alloca (slen
* 2 + 1);
3648 bin2hex ((gdb_byte
*) str
, buf3
, slen
);
3650 trace_debug ("Returning trace status as %d, stop reason %s",
3651 tracing
, tracing_stop_reason
);
3653 if (agent_loaded_p ())
3657 upload_fast_traceframes ();
3662 stop_reason_rsp
= (char *) tracing_stop_reason
;
3664 /* The user visible error string in terror needs to be hex encoded.
3665 We leave it as plain string in `tracing_stop_reason' to ease
3667 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3669 const char *result_name
;
3673 result_name
= stop_reason_rsp
+ strlen ("terror:");
3674 hexstr_len
= strlen (result_name
) * 2;
3675 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3676 strcpy (p
, "terror:");
3678 bin2hex ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3681 /* If this was a forced stop, include any stop note that was supplied. */
3682 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3684 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3685 strcpy (stop_reason_rsp
, "tstop:");
3686 strcat (stop_reason_rsp
, buf3
);
3692 "tframes:%x;tcreated:%x;"
3693 "tfree:%x;tsize:%s;"
3696 "starttime:%s;stoptime:%s;"
3697 "username:%s;notes:%s:",
3699 stop_reason_rsp
, tracing_stop_tpnum
,
3700 traceframe_count
, traceframes_created
,
3701 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3702 circular_trace_buffer
,
3703 disconnected_tracing
,
3704 phex_nz (tracing_start_time
, sizeof (tracing_start_time
)),
3705 phex_nz (tracing_stop_time
, sizeof (tracing_stop_time
)),
3710 cmd_qtp (char *own_buf
)
3713 struct tracepoint
*tpoint
;
3714 char *packet
= own_buf
;
3716 packet
+= strlen ("qTP:");
3718 packet
= unpack_varlen_hex (packet
, &num
);
3719 ++packet
; /* skip a colon */
3720 packet
= unpack_varlen_hex (packet
, &addr
);
3722 /* See if we already have this tracepoint. */
3723 tpoint
= find_tracepoint (num
, addr
);
3727 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3728 (int) num
, paddress (addr
));
3729 write_enn (own_buf
);
3733 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3734 tpoint
->traceframe_usage
);
3737 /* State variables to help return all the tracepoint bits. */
3738 static struct tracepoint
*cur_tpoint
;
3739 static unsigned int cur_action
;
3740 static unsigned int cur_step_action
;
3741 static struct source_string
*cur_source_string
;
3742 static struct trace_state_variable
*cur_tsv
;
3744 /* Compose a response that is an imitation of the syntax by which the
3745 tracepoint was originally downloaded. */
3748 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3752 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3753 paddress (tpoint
->address
),
3754 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3755 tpoint
->pass_count
);
3756 if (tpoint
->type
== fast_tracepoint
)
3757 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3758 else if (tpoint
->type
== static_tracepoint
)
3759 sprintf (packet
+ strlen (packet
), ":S");
3763 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3764 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3765 tpoint
->cond
->length
, buf
);
3770 /* Compose a response that is an imitation of the syntax by which the
3771 tracepoint action was originally downloaded (with the difference
3772 that due to the way we store the actions, this will output a packet
3773 per action, while GDB could have combined more than one action
3777 response_action (char *packet
, struct tracepoint
*tpoint
,
3778 char *taction
, int step
)
3780 sprintf (packet
, "%c%x:%s:%s",
3781 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3785 /* Compose a response that is an imitation of the syntax by which the
3786 tracepoint source piece was originally downloaded. */
3789 response_source (char *packet
,
3790 struct tracepoint
*tpoint
, struct source_string
*src
)
3795 len
= strlen (src
->str
);
3796 buf
= alloca (len
* 2 + 1);
3797 bin2hex ((gdb_byte
*) src
->str
, buf
, len
);
3799 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3800 tpoint
->number
, paddress (tpoint
->address
),
3801 src
->type
, 0, len
, buf
);
3804 /* Return the first piece of tracepoint definition, and initialize the
3805 state machine that will iterate through all the tracepoint
3809 cmd_qtfp (char *packet
)
3811 trace_debug ("Returning first tracepoint definition piece");
3813 cur_tpoint
= tracepoints
;
3814 cur_action
= cur_step_action
= 0;
3815 cur_source_string
= NULL
;
3818 response_tracepoint (packet
, cur_tpoint
);
3820 strcpy (packet
, "l");
3823 /* Return additional pieces of tracepoint definition. Each action and
3824 stepping action must go into its own packet, because of packet size
3825 limits, and so we use state variables to deliver one piece at a
3829 cmd_qtsp (char *packet
)
3831 trace_debug ("Returning subsequent tracepoint definition piece");
3835 /* This case would normally never occur, but be prepared for
3837 strcpy (packet
, "l");
3839 else if (cur_action
< cur_tpoint
->numactions
)
3841 response_action (packet
, cur_tpoint
,
3842 cur_tpoint
->actions_str
[cur_action
], 0);
3845 else if (cur_step_action
< cur_tpoint
->num_step_actions
)
3847 response_action (packet
, cur_tpoint
,
3848 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3851 else if ((cur_source_string
3852 ? cur_source_string
->next
3853 : cur_tpoint
->source_strings
))
3855 if (cur_source_string
)
3856 cur_source_string
= cur_source_string
->next
;
3858 cur_source_string
= cur_tpoint
->source_strings
;
3859 response_source (packet
, cur_tpoint
, cur_source_string
);
3863 cur_tpoint
= cur_tpoint
->next
;
3864 cur_action
= cur_step_action
= 0;
3865 cur_source_string
= NULL
;
3867 response_tracepoint (packet
, cur_tpoint
);
3869 strcpy (packet
, "l");
3873 /* Compose a response that is an imitation of the syntax by which the
3874 trace state variable was originally downloaded. */
3877 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3879 char *buf
= (char *) "";
3884 namelen
= strlen (tsv
->name
);
3885 buf
= alloca (namelen
* 2 + 1);
3886 bin2hex ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3889 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3890 tsv
->getter
? 1 : 0, buf
);
3893 /* Return the first trace state variable definition, and initialize
3894 the state machine that will iterate through all the tsv bits. */
3897 cmd_qtfv (char *packet
)
3899 trace_debug ("Returning first trace state variable definition");
3901 cur_tsv
= trace_state_variables
;
3904 response_tsv (packet
, cur_tsv
);
3906 strcpy (packet
, "l");
3909 /* Return additional trace state variable definitions. */
3912 cmd_qtsv (char *packet
)
3914 trace_debug ("Returning additional trace state variable definition");
3918 cur_tsv
= cur_tsv
->next
;
3920 response_tsv (packet
, cur_tsv
);
3922 strcpy (packet
, "l");
3925 strcpy (packet
, "l");
3928 /* Return the first static tracepoint marker, and initialize the state
3929 machine that will iterate through all the static tracepoints
3933 cmd_qtfstm (char *packet
)
3935 if (!maybe_write_ipa_ust_not_loaded (packet
))
3936 run_inferior_command (packet
, strlen (packet
) + 1);
3939 /* Return additional static tracepoints markers. */
3942 cmd_qtsstm (char *packet
)
3944 if (!maybe_write_ipa_ust_not_loaded (packet
))
3945 run_inferior_command (packet
, strlen (packet
) + 1);
3948 /* Return the definition of the static tracepoint at a given address.
3949 Result packet is the same as qTsST's. */
3952 cmd_qtstmat (char *packet
)
3954 if (!maybe_write_ipa_ust_not_loaded (packet
))
3955 run_inferior_command (packet
, strlen (packet
) + 1);
3958 /* Helper for gdb_agent_about_to_close.
3959 Return non-zero if thread ENTRY is in the same process in DATA. */
3962 same_process_p (struct inferior_list_entry
*entry
, void *data
)
3966 return ptid_get_pid (entry
->id
) == *pid
;
3969 /* Sent the agent a command to close it. */
3972 gdb_agent_about_to_close (int pid
)
3974 char buf
[IPA_CMD_BUF_SIZE
];
3976 if (!maybe_write_ipa_not_loaded (buf
))
3978 struct thread_info
*saved_thread
;
3980 saved_thread
= current_thread
;
3982 /* Find any thread which belongs to process PID. */
3983 current_thread
= (struct thread_info
*)
3984 find_inferior (&all_threads
, same_process_p
, &pid
);
3986 strcpy (buf
, "close");
3988 run_inferior_command (buf
, strlen (buf
) + 1);
3990 current_thread
= saved_thread
;
3994 /* Return the minimum instruction size needed for fast tracepoints as a
3995 hexadecimal number. */
3998 cmd_qtminftpilen (char *packet
)
4000 if (current_thread
== NULL
)
4002 /* Indicate that the minimum length is currently unknown. */
4003 strcpy (packet
, "0");
4007 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
4010 /* Respond to qTBuffer packet with a block of raw data from the trace
4011 buffer. GDB may ask for a lot, but we are allowed to reply with
4012 only as much as will fit within packet limits or whatever. */
4015 cmd_qtbuffer (char *own_buf
)
4017 ULONGEST offset
, num
, tot
;
4019 char *packet
= own_buf
;
4021 packet
+= strlen ("qTBuffer:");
4023 packet
= unpack_varlen_hex (packet
, &offset
);
4024 ++packet
; /* skip a comma */
4025 unpack_varlen_hex (packet
, &num
);
4027 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
4028 (int) num
, phex_nz (offset
, 0));
4030 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
4032 /* If we're right at the end, reply specially that we're done. */
4035 strcpy (own_buf
, "l");
4039 /* Object to any other out-of-bounds request. */
4042 write_enn (own_buf
);
4046 /* Compute the pointer corresponding to the given offset, accounting
4048 tbp
= trace_buffer_start
+ offset
;
4049 if (tbp
>= trace_buffer_wrap
)
4050 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
4052 /* Trim to the remaining bytes if we're close to the end. */
4053 if (num
> tot
- offset
)
4056 /* Trim to available packet size. */
4057 if (num
>= (PBUFSIZ
- 16) / 2 )
4058 num
= (PBUFSIZ
- 16) / 2;
4060 bin2hex (tbp
, own_buf
, num
);
4064 cmd_bigqtbuffer_circular (char *own_buf
)
4067 char *packet
= own_buf
;
4069 packet
+= strlen ("QTBuffer:circular:");
4071 unpack_varlen_hex (packet
, &val
);
4072 circular_trace_buffer
= val
;
4073 trace_debug ("Trace buffer is now %s",
4074 circular_trace_buffer
? "circular" : "linear");
4079 cmd_bigqtbuffer_size (char *own_buf
)
4083 char *packet
= own_buf
;
4085 /* Can't change the size during a tracing run. */
4088 write_enn (own_buf
);
4092 packet
+= strlen ("QTBuffer:size:");
4094 /* -1 is sent as literal "-1". */
4095 if (strcmp (packet
, "-1") == 0)
4096 sval
= DEFAULT_TRACE_BUFFER_SIZE
;
4099 unpack_varlen_hex (packet
, &val
);
4100 sval
= (LONGEST
) val
;
4103 init_trace_buffer (sval
);
4104 trace_debug ("Trace buffer is now %s bytes",
4105 plongest (trace_buffer_size
));
4110 cmd_qtnotes (char *own_buf
)
4113 char *saved
, *user
, *notes
, *stopnote
;
4114 char *packet
= own_buf
;
4116 packet
+= strlen ("QTNotes:");
4120 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
4122 packet
+= strlen ("user:");
4124 packet
= strchr (packet
, ';');
4125 nbytes
= (packet
- saved
) / 2;
4126 user
= xmalloc (nbytes
+ 1);
4127 nbytes
= hex2bin (saved
, (gdb_byte
*) user
, nbytes
);
4128 user
[nbytes
] = '\0';
4129 ++packet
; /* skip the semicolon */
4130 trace_debug ("User is '%s'", user
);
4131 xfree (tracing_user_name
);
4132 tracing_user_name
= user
;
4134 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
4136 packet
+= strlen ("notes:");
4138 packet
= strchr (packet
, ';');
4139 nbytes
= (packet
- saved
) / 2;
4140 notes
= xmalloc (nbytes
+ 1);
4141 nbytes
= hex2bin (saved
, (gdb_byte
*) notes
, nbytes
);
4142 notes
[nbytes
] = '\0';
4143 ++packet
; /* skip the semicolon */
4144 trace_debug ("Notes is '%s'", notes
);
4145 xfree (tracing_notes
);
4146 tracing_notes
= notes
;
4148 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
4150 packet
+= strlen ("tstop:");
4152 packet
= strchr (packet
, ';');
4153 nbytes
= (packet
- saved
) / 2;
4154 stopnote
= xmalloc (nbytes
+ 1);
4155 nbytes
= hex2bin (saved
, (gdb_byte
*) stopnote
, nbytes
);
4156 stopnote
[nbytes
] = '\0';
4157 ++packet
; /* skip the semicolon */
4158 trace_debug ("tstop note is '%s'", stopnote
);
4159 xfree (tracing_stop_note
);
4160 tracing_stop_note
= stopnote
;
4170 handle_tracepoint_general_set (char *packet
)
4172 if (strcmp ("QTinit", packet
) == 0)
4174 cmd_qtinit (packet
);
4177 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
4182 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
4184 cmd_qtdpsrc (packet
);
4187 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
4189 cmd_qtenable_disable (packet
, 1);
4192 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
4194 cmd_qtenable_disable (packet
, 0);
4197 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
4202 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
4207 else if (strcmp ("QTStart", packet
) == 0)
4209 cmd_qtstart (packet
);
4212 else if (strcmp ("QTStop", packet
) == 0)
4214 cmd_qtstop (packet
);
4217 else if (strncmp ("QTDisconnected:", packet
,
4218 strlen ("QTDisconnected:")) == 0)
4220 cmd_qtdisconnected (packet
);
4223 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
4225 cmd_qtframe (packet
);
4228 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
4230 cmd_bigqtbuffer_circular (packet
);
4233 else if (strncmp ("QTBuffer:size:", packet
, strlen ("QTBuffer:size:")) == 0)
4235 cmd_bigqtbuffer_size (packet
);
4238 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
4240 cmd_qtnotes (packet
);
4248 handle_tracepoint_query (char *packet
)
4250 if (strcmp ("qTStatus", packet
) == 0)
4252 cmd_qtstatus (packet
);
4255 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4260 else if (strcmp ("qTfP", packet
) == 0)
4265 else if (strcmp ("qTsP", packet
) == 0)
4270 else if (strcmp ("qTfV", packet
) == 0)
4275 else if (strcmp ("qTsV", packet
) == 0)
4280 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4285 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4287 cmd_qtbuffer (packet
);
4290 else if (strcmp ("qTfSTM", packet
) == 0)
4292 cmd_qtfstm (packet
);
4295 else if (strcmp ("qTsSTM", packet
) == 0)
4297 cmd_qtsstm (packet
);
4300 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4302 cmd_qtstmat (packet
);
4305 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4307 cmd_qtminftpilen (packet
);
4315 #ifndef IN_PROCESS_AGENT
4317 /* Call this when thread TINFO has hit the tracepoint defined by
4318 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4319 action. This adds a while-stepping collecting state item to the
4320 threads' collecting state list, so that we can keep track of
4321 multiple simultaneous while-stepping actions being collected by the
4322 same thread. This can happen in cases like:
4324 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4326 ff0003 INSN3 <-- TP2, collect $regs
4327 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4330 Notice that when instruction INSN5 is reached, the while-stepping
4331 actions of both TP1 and TP3 are still being collected, and that TP2
4332 had been collected meanwhile. The whole range of ff0001-ff0005
4333 should be single-stepped, due to at least TP1's while-stepping
4334 action covering the whole range. */
4337 add_while_stepping_state (struct thread_info
*tinfo
,
4338 int tp_number
, CORE_ADDR tp_address
)
4340 struct wstep_state
*wstep
;
4342 wstep
= xmalloc (sizeof (*wstep
));
4343 wstep
->next
= tinfo
->while_stepping
;
4345 wstep
->tp_number
= tp_number
;
4346 wstep
->tp_address
= tp_address
;
4347 wstep
->current_step
= 0;
4349 tinfo
->while_stepping
= wstep
;
4352 /* Release the while-stepping collecting state WSTEP. */
4355 release_while_stepping_state (struct wstep_state
*wstep
)
4360 /* Release all while-stepping collecting states currently associated
4361 with thread TINFO. */
4364 release_while_stepping_state_list (struct thread_info
*tinfo
)
4366 struct wstep_state
*head
;
4368 while (tinfo
->while_stepping
)
4370 head
= tinfo
->while_stepping
;
4371 tinfo
->while_stepping
= head
->next
;
4372 release_while_stepping_state (head
);
4376 /* If TINFO was handling a 'while-stepping' action, the step has
4377 finished, so collect any step data needed, and check if any more
4378 steps are required. Return true if the thread was indeed
4379 collecting tracepoint data, false otherwise. */
4382 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4384 struct tracepoint
*tpoint
;
4385 struct wstep_state
*wstep
;
4386 struct wstep_state
**wstep_link
;
4387 struct trap_tracepoint_ctx ctx
;
4389 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4391 if (agent_loaded_p ())
4392 upload_fast_traceframes ();
4394 /* Check if we were indeed collecting data for one of more
4395 tracepoints with a 'while-stepping' count. */
4396 if (tinfo
->while_stepping
== NULL
)
4401 /* We're not even tracing anymore. Stop this thread from
4403 release_while_stepping_state_list (tinfo
);
4405 /* The thread had stopped due to a single-step request indeed
4406 explained by a tracepoint. */
4410 wstep
= tinfo
->while_stepping
;
4411 wstep_link
= &tinfo
->while_stepping
;
4413 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4414 target_pid_to_str (tinfo
->entry
.id
),
4415 wstep
->tp_number
, paddress (wstep
->tp_address
));
4417 ctx
.base
.type
= trap_tracepoint
;
4418 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4420 while (wstep
!= NULL
)
4422 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4425 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4426 wstep
->tp_number
, paddress (wstep
->tp_address
),
4427 target_pid_to_str (tinfo
->entry
.id
));
4430 *wstep_link
= wstep
->next
;
4431 release_while_stepping_state (wstep
);
4432 wstep
= *wstep_link
;
4436 /* We've just finished one step. */
4437 ++wstep
->current_step
;
4440 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4441 stop_pc
, tpoint
, wstep
->current_step
);
4443 if (wstep
->current_step
>= tpoint
->step_count
)
4445 /* The requested numbers of steps have occurred. */
4446 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4447 target_pid_to_str (tinfo
->entry
.id
),
4448 wstep
->tp_number
, paddress (wstep
->tp_address
));
4450 /* Unlink the wstep. */
4451 *wstep_link
= wstep
->next
;
4452 release_while_stepping_state (wstep
);
4453 wstep
= *wstep_link
;
4455 /* Only check the hit count now, which ensure that we do all
4456 our stepping before stopping the run. */
4457 if (tpoint
->pass_count
> 0
4458 && tpoint
->hit_count
>= tpoint
->pass_count
4459 && stopping_tracepoint
== NULL
)
4460 stopping_tracepoint
= tpoint
;
4464 /* Keep single-stepping until the requested numbers of steps
4466 wstep_link
= &wstep
->next
;
4467 wstep
= *wstep_link
;
4470 if (stopping_tracepoint
4471 || trace_buffer_is_full
4472 || expr_eval_result
!= expr_eval_no_error
)
4482 /* Handle any internal tracing control breakpoint hits. That means,
4483 pull traceframes from the IPA to our buffer, and syncing both
4484 tracing agents when the IPA's tracing stops for some reason. */
4487 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4489 /* Pull in fast tracepoint trace frames from the inferior in-process
4490 agent's buffer into our buffer. */
4492 if (!agent_loaded_p ())
4495 upload_fast_traceframes ();
4497 /* Check if the in-process agent had decided we should stop
4499 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4501 int ipa_trace_buffer_is_full
;
4502 CORE_ADDR ipa_stopping_tracepoint
;
4503 int ipa_expr_eval_result
;
4504 CORE_ADDR ipa_error_tracepoint
;
4506 trace_debug ("lib stopped at stop_tracing");
4508 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4509 &ipa_trace_buffer_is_full
);
4511 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4512 &ipa_stopping_tracepoint
);
4513 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4515 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4516 &ipa_error_tracepoint
);
4517 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4519 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4520 &ipa_expr_eval_result
);
4521 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4523 trace_debug ("lib: trace_buffer_is_full: %d, "
4524 "stopping_tracepoint: %s, "
4525 "ipa_expr_eval_result: %d, "
4526 "error_tracepoint: %s, ",
4527 ipa_trace_buffer_is_full
,
4528 paddress (ipa_stopping_tracepoint
),
4529 ipa_expr_eval_result
,
4530 paddress (ipa_error_tracepoint
));
4534 if (ipa_trace_buffer_is_full
)
4535 trace_debug ("lib stopped due to full buffer.");
4536 if (ipa_stopping_tracepoint
)
4537 trace_debug ("lib stopped due to tpoint");
4538 if (ipa_stopping_tracepoint
)
4539 trace_debug ("lib stopped due to error");
4542 if (ipa_stopping_tracepoint
!= 0)
4545 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4547 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4549 expr_eval_result
= ipa_expr_eval_result
;
4551 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4556 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4558 trace_debug ("lib stopped at flush_trace_buffer");
4565 /* Return true if TINFO just hit a tracepoint. Collect data if
4569 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4571 struct tracepoint
*tpoint
;
4573 struct trap_tracepoint_ctx ctx
;
4575 /* Not tracing, don't handle. */
4579 ctx
.base
.type
= trap_tracepoint
;
4580 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4582 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4584 /* Note that we collect fast tracepoints here as well. We'll
4585 step over the fast tracepoint jump later, which avoids the
4586 double collect. However, we don't collect for static
4587 tracepoints here, because UST markers are compiled in program,
4588 and probes will be executed in program. So static tracepoints
4589 are collected there. */
4590 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4591 && tpoint
->type
!= static_tracepoint
)
4593 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4594 target_pid_to_str (tinfo
->entry
.id
),
4595 tpoint
->number
, paddress (tpoint
->address
));
4597 /* Test the condition if present, and collect if true. */
4599 || (condition_true_at_tracepoint
4600 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4601 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4604 if (stopping_tracepoint
4605 || trace_buffer_is_full
4606 || expr_eval_result
!= expr_eval_no_error
)
4610 /* If the tracepoint had a 'while-stepping' action, then set
4611 the thread to collect this tracepoint on the following
4613 else if (tpoint
->step_count
> 0)
4615 add_while_stepping_state (tinfo
,
4616 tpoint
->number
, tpoint
->address
);
4628 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4629 struct ust_marker_data
;
4630 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4631 struct traceframe
*tframe
);
4634 /* Create a trace frame for the hit of the given tracepoint in the
4638 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4639 struct tracepoint
*tpoint
)
4641 struct traceframe
*tframe
;
4644 /* Only count it as a hit when we actually collect data. */
4645 tpoint
->hit_count
++;
4647 /* If we've exceeded a defined pass count, record the event for
4648 later, and finish the collection for this hit. This test is only
4649 for nonstepping tracepoints, stepping tracepoints test at the end
4650 of their while-stepping loop. */
4651 if (tpoint
->pass_count
> 0
4652 && tpoint
->hit_count
>= tpoint
->pass_count
4653 && tpoint
->step_count
== 0
4654 && stopping_tracepoint
== NULL
)
4655 stopping_tracepoint
= tpoint
;
4657 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4658 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4660 tframe
= add_traceframe (tpoint
);
4664 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4666 #ifndef IN_PROCESS_AGENT
4667 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4668 tpoint
->number
, paddress (tpoint
->address
),
4669 tpoint
->actions_str
[acti
]);
4672 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4673 tpoint
->actions
[acti
]);
4676 finish_traceframe (tframe
);
4679 if (tframe
== NULL
&& tracing
)
4680 trace_buffer_is_full
= 1;
4683 #ifndef IN_PROCESS_AGENT
4686 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4688 struct tracepoint
*tpoint
, int current_step
)
4690 struct traceframe
*tframe
;
4693 trace_debug ("Making new step traceframe for "
4694 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4695 tpoint
->number
, paddress (tpoint
->address
),
4696 current_step
, tpoint
->step_count
,
4699 tframe
= add_traceframe (tpoint
);
4703 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4705 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4706 tpoint
->number
, paddress (tpoint
->address
),
4707 tpoint
->step_actions_str
[acti
]);
4709 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4710 tpoint
->step_actions
[acti
]);
4713 finish_traceframe (tframe
);
4716 if (tframe
== NULL
&& tracing
)
4717 trace_buffer_is_full
= 1;
4722 #ifdef IN_PROCESS_AGENT
4723 /* The target description used by the IPA. Given that the IPA library
4724 is built for a specific architecture that is loaded into the
4725 inferior, there only needs to be one such description per
4727 const struct target_desc
*ipa_tdesc
;
4730 static struct regcache
*
4731 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4733 struct regcache
*regcache
= NULL
;
4735 #ifdef IN_PROCESS_AGENT
4736 if (ctx
->type
== fast_tracepoint
)
4738 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4739 if (!fctx
->regcache_initted
)
4741 fctx
->regcache_initted
= 1;
4742 init_register_cache (&fctx
->regcache
, ipa_tdesc
, fctx
->regspace
);
4743 supply_regblock (&fctx
->regcache
, NULL
);
4744 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4746 regcache
= &fctx
->regcache
;
4749 if (ctx
->type
== static_tracepoint
)
4751 struct static_tracepoint_ctx
*sctx
4752 = (struct static_tracepoint_ctx
*) ctx
;
4754 if (!sctx
->regcache_initted
)
4756 sctx
->regcache_initted
= 1;
4757 init_register_cache (&sctx
->regcache
, ipa_tdesc
, sctx
->regspace
);
4758 supply_regblock (&sctx
->regcache
, NULL
);
4759 /* Pass down the tracepoint address, because REGS doesn't
4760 include the PC, but we know what it must have been. */
4761 supply_static_tracepoint_registers (&sctx
->regcache
,
4762 (const unsigned char *)
4764 sctx
->tpoint
->address
);
4766 regcache
= &sctx
->regcache
;
4770 if (ctx
->type
== trap_tracepoint
)
4772 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4773 regcache
= tctx
->regcache
;
4777 gdb_assert (regcache
!= NULL
);
4783 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4785 struct tracepoint
*tpoint
,
4786 struct traceframe
*tframe
,
4787 struct tracepoint_action
*taction
)
4789 enum eval_result_type err
;
4791 switch (taction
->type
)
4795 struct collect_memory_action
*maction
;
4796 struct eval_agent_expr_context ax_ctx
;
4798 maction
= (struct collect_memory_action
*) taction
;
4799 ax_ctx
.regcache
= NULL
;
4800 ax_ctx
.tframe
= tframe
;
4801 ax_ctx
.tpoint
= tpoint
;
4803 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4804 pulongest (maction
->len
),
4805 paddress (maction
->addr
), maction
->basereg
);
4806 /* (should use basereg) */
4807 agent_mem_read (&ax_ctx
, NULL
, (CORE_ADDR
) maction
->addr
,
4813 unsigned char *regspace
;
4814 struct regcache tregcache
;
4815 struct regcache
*context_regcache
;
4818 trace_debug ("Want to collect registers");
4820 context_regcache
= get_context_regcache (ctx
);
4821 regcache_size
= register_cache_size (context_regcache
->tdesc
);
4823 /* Collect all registers for now. */
4824 regspace
= add_traceframe_block (tframe
, tpoint
, 1 + regcache_size
);
4825 if (regspace
== NULL
)
4827 trace_debug ("Trace buffer block allocation failed, skipping");
4830 /* Identify a register block. */
4833 /* Wrap the regblock in a register cache (in the stack, we
4834 don't want to malloc here). */
4835 init_register_cache (&tregcache
, context_regcache
->tdesc
,
4838 /* Copy the register data to the regblock. */
4839 regcache_cpy (&tregcache
, context_regcache
);
4841 #ifndef IN_PROCESS_AGENT
4842 /* On some platforms, trap-based tracepoints will have the PC
4843 pointing to the next instruction after the trap, but we
4844 don't want the user or GDB trying to guess whether the
4845 saved PC needs adjusting; so always record the adjusted
4846 stop_pc. Note that we can't use tpoint->address instead,
4847 since it will be wrong for while-stepping actions. This
4848 adjustment is a nop for fast tracepoints collected from the
4849 in-process lib (but not if GDBserver is collecting one
4850 preemptively), since the PC had already been adjusted to
4851 contain the tracepoint's address by the jump pad. */
4852 trace_debug ("Storing stop pc (0x%s) in regblock",
4853 paddress (stop_pc
));
4855 /* This changes the regblock, not the thread's
4857 regcache_write_pc (&tregcache
, stop_pc
);
4863 struct eval_expr_action
*eaction
;
4864 struct eval_agent_expr_context ax_ctx
;
4866 eaction
= (struct eval_expr_action
*) taction
;
4867 ax_ctx
.regcache
= get_context_regcache (ctx
);
4868 ax_ctx
.tframe
= tframe
;
4869 ax_ctx
.tpoint
= tpoint
;
4871 trace_debug ("Want to evaluate expression");
4873 err
= gdb_eval_agent_expr (&ax_ctx
, eaction
->expr
, NULL
);
4875 if (err
!= expr_eval_no_error
)
4877 record_tracepoint_error (tpoint
, "action expression", err
);
4884 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4885 trace_debug ("Want to collect static trace data");
4886 collect_ust_data_at_tracepoint (ctx
, tframe
);
4888 trace_debug ("warning: collecting static trace data, "
4889 "but static tracepoints are not supported");
4894 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4900 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4901 struct tracepoint
*tpoint
)
4904 enum eval_result_type err
;
4906 /* Presently, gdbserver doesn't run compiled conditions, only the
4907 IPA does. If the program stops at a fast tracepoint's address
4908 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4909 gdbserver preemptively collect the fast tracepoint. Later, on
4910 resume, gdbserver steps over the fast tracepoint like it steps
4911 over breakpoints, so that the IPA doesn't see that fast
4912 tracepoint. This avoids double collects of fast tracepoints in
4913 that stopping scenario. Having gdbserver itself handle the fast
4914 tracepoint gives the user a consistent view of when fast or trap
4915 tracepoints are collected, compared to an alternative where only
4916 trap tracepoints are collected on stop, and fast tracepoints on
4917 resume. When a fast tracepoint is being processed by gdbserver,
4918 it is always the non-compiled condition expression that is
4920 #ifdef IN_PROCESS_AGENT
4921 if (tpoint
->compiled_cond
)
4922 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4926 struct eval_agent_expr_context ax_ctx
;
4928 ax_ctx
.regcache
= get_context_regcache (ctx
);
4929 ax_ctx
.tframe
= NULL
;
4930 ax_ctx
.tpoint
= tpoint
;
4932 err
= gdb_eval_agent_expr (&ax_ctx
, tpoint
->cond
, &value
);
4934 if (err
!= expr_eval_no_error
)
4936 record_tracepoint_error (tpoint
, "condition", err
);
4937 /* The error case must return false. */
4941 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4942 tpoint
->number
, paddress (tpoint
->address
),
4944 return (value
? 1 : 0);
4947 /* Do memory copies for bytecodes. */
4948 /* Do the recording of memory blocks for actions and bytecodes. */
4951 agent_mem_read (struct eval_agent_expr_context
*ctx
,
4952 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4954 unsigned char *mspace
;
4955 ULONGEST remaining
= len
;
4956 unsigned short blocklen
;
4958 /* If a 'to' buffer is specified, use it. */
4961 read_inferior_memory (from
, to
, len
);
4965 /* Otherwise, create a new memory block in the trace buffer. */
4966 while (remaining
> 0)
4970 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4971 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4972 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4975 /* Identify block as a memory block. */
4978 /* Record address and size. */
4979 memcpy (mspace
, &from
, sizeof (from
));
4980 mspace
+= sizeof (from
);
4981 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4982 mspace
+= sizeof (blocklen
);
4983 /* Record the memory block proper. */
4984 read_inferior_memory (from
, mspace
, blocklen
);
4985 trace_debug ("%d bytes recorded", blocklen
);
4986 remaining
-= blocklen
;
4993 agent_mem_read_string (struct eval_agent_expr_context
*ctx
,
4994 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4996 unsigned char *buf
, *mspace
;
4997 ULONGEST remaining
= len
;
4998 unsigned short blocklen
, i
;
5000 /* To save a bit of space, block lengths are 16-bit, so break large
5001 requests into multiple blocks. Bordering on overkill for strings,
5002 but it could happen that someone specifies a large max length. */
5003 while (remaining
> 0)
5007 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
5008 /* We want working space to accumulate nonzero bytes, since
5009 traceframes must have a predecided size (otherwise it gets
5010 harder to wrap correctly for the circular case, etc). */
5011 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
5012 for (i
= 0; i
< blocklen
; ++i
)
5014 /* Read the string one byte at a time, in case the string is
5015 at the end of a valid memory area - we don't want a
5016 correctly-terminated string to engender segvio
5018 read_inferior_memory (from
+ i
, buf
+ i
, 1);
5023 /* Make sure outer loop stops now too. */
5024 remaining
= blocklen
;
5028 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
5029 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
5035 /* Identify block as a memory block. */
5038 /* Record address and size. */
5039 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
5040 mspace
+= sizeof (from
);
5041 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
5042 mspace
+= sizeof (blocklen
);
5043 /* Copy the string contents. */
5044 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
5045 remaining
-= blocklen
;
5052 /* Record the value of a trace state variable. */
5055 agent_tsv_read (struct eval_agent_expr_context
*ctx
, int n
)
5057 unsigned char *vspace
;
5060 vspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
,
5061 1 + sizeof (n
) + sizeof (LONGEST
));
5064 /* Identify block as a variable. */
5066 /* Record variable's number and value. */
5067 memcpy (vspace
+ 1, &n
, sizeof (n
));
5068 val
= get_trace_state_variable_value (n
);
5069 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
5070 trace_debug ("Variable %d recorded", n
);
5074 #ifndef IN_PROCESS_AGENT
5076 /* Callback for traceframe_walk_blocks, used to find a given block
5077 type in a traceframe. */
5080 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
5082 char *wantedp
= data
;
5084 if (*wantedp
== blocktype
)
5090 /* Walk over all traceframe blocks of the traceframe buffer starting
5091 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5092 block found, passing in DATA unmodified. If CALLBACK returns true,
5093 this returns a pointer to where the block is found. Returns NULL
5094 if no callback call returned true, indicating that all blocks have
5097 static unsigned char *
5098 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
5100 int (*callback
) (char blocktype
,
5101 unsigned char *dataptr
,
5105 unsigned char *dataptr
;
5109 trace_debug ("traceframe %d has no data", tfnum
);
5113 /* Iterate through a traceframe's blocks, looking for a block of the
5115 for (dataptr
= database
;
5116 dataptr
< database
+ datasize
;
5120 unsigned short mlen
;
5122 if (dataptr
== trace_buffer_wrap
)
5124 /* Adjust to reflect wrapping part of the frame around to
5126 datasize
= dataptr
- database
;
5127 dataptr
= database
= trace_buffer_lo
;
5130 blocktype
= *dataptr
++;
5132 if ((*callback
) (blocktype
, dataptr
, data
))
5138 /* Skip over the registers block. */
5139 dataptr
+= current_target_desc ()->registers_size
;
5142 /* Skip over the memory block. */
5143 dataptr
+= sizeof (CORE_ADDR
);
5144 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5145 dataptr
+= (sizeof (mlen
) + mlen
);
5148 /* Skip over the TSV block. */
5149 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5152 /* Skip over the static trace data block. */
5153 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5154 dataptr
+= (sizeof (mlen
) + mlen
);
5157 trace_debug ("traceframe %d has unknown block type 0x%x",
5166 /* Look for the block of type TYPE_WANTED in the trameframe starting
5167 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5170 static unsigned char *
5171 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5172 int tfnum
, char type_wanted
)
5174 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5175 match_blocktype
, &type_wanted
);
5178 static unsigned char *
5179 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5181 unsigned char *regblock
;
5183 regblock
= traceframe_find_block_type (tframe
->data
,
5187 if (regblock
== NULL
)
5188 trace_debug ("traceframe %d has no register data", tfnum
);
5193 /* Get registers from a traceframe. */
5196 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5198 unsigned char *dataptr
;
5199 struct tracepoint
*tpoint
;
5200 struct traceframe
*tframe
;
5202 tframe
= find_traceframe (tfnum
);
5206 trace_debug ("traceframe %d not found", tfnum
);
5210 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5211 if (dataptr
== NULL
)
5213 /* Mark registers unavailable. */
5214 supply_regblock (regcache
, NULL
);
5216 /* We can generally guess at a PC, although this will be
5217 misleading for while-stepping frames and multi-location
5219 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5221 regcache_write_pc (regcache
, tpoint
->address
);
5224 supply_regblock (regcache
, dataptr
);
5230 traceframe_get_pc (struct traceframe
*tframe
)
5232 struct regcache regcache
;
5233 unsigned char *dataptr
;
5234 const struct target_desc
*tdesc
= current_target_desc ();
5236 dataptr
= traceframe_find_regblock (tframe
, -1);
5237 if (dataptr
== NULL
)
5240 init_register_cache (®cache
, tdesc
, dataptr
);
5241 return regcache_read_pc (®cache
);
5244 /* Read a requested block of memory from a trace frame. */
5247 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5248 unsigned char *buf
, ULONGEST length
,
5251 struct traceframe
*tframe
;
5252 unsigned char *database
, *dataptr
;
5253 unsigned int datasize
;
5255 unsigned short mlen
;
5257 trace_debug ("traceframe_read_mem");
5259 tframe
= find_traceframe (tfnum
);
5263 trace_debug ("traceframe %d not found", tfnum
);
5267 datasize
= tframe
->data_size
;
5268 database
= dataptr
= &tframe
->data
[0];
5270 /* Iterate through a traceframe's blocks, looking for memory. */
5271 while ((dataptr
= traceframe_find_block_type (dataptr
,
5273 - (dataptr
- database
),
5274 tfnum
, 'M')) != NULL
)
5276 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5277 dataptr
+= sizeof (maddr
);
5278 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5279 dataptr
+= sizeof (mlen
);
5280 trace_debug ("traceframe %d has %d bytes at %s",
5281 tfnum
, mlen
, paddress (maddr
));
5283 /* If the block includes the first part of the desired range,
5284 return as much it has; GDB will re-request the remainder,
5285 which might be in a different block of this trace frame. */
5286 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5288 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5292 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5297 /* Skip over this block. */
5301 trace_debug ("traceframe %d has no memory data for the desired region",
5309 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5312 struct traceframe
*tframe
;
5313 unsigned char *database
, *dataptr
;
5314 unsigned int datasize
;
5318 trace_debug ("traceframe_read_tsv");
5320 tfnum
= current_traceframe
;
5324 trace_debug ("no current traceframe");
5328 tframe
= find_traceframe (tfnum
);
5332 trace_debug ("traceframe %d not found", tfnum
);
5336 datasize
= tframe
->data_size
;
5337 database
= dataptr
= &tframe
->data
[0];
5339 /* Iterate through a traceframe's blocks, looking for the last
5341 while ((dataptr
= traceframe_find_block_type (dataptr
,
5343 - (dataptr
- database
),
5344 tfnum
, 'V')) != NULL
)
5346 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5347 dataptr
+= sizeof (vnum
);
5349 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5351 /* Check that this is the variable we want. */
5354 memcpy (val
, dataptr
, sizeof (*val
));
5358 /* Skip over this block. */
5359 dataptr
+= sizeof (LONGEST
);
5363 trace_debug ("traceframe %d has no data for variable %d",
5368 /* Read a requested block of static tracepoint data from a trace
5372 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5373 unsigned char *buf
, ULONGEST length
,
5376 struct traceframe
*tframe
;
5377 unsigned char *database
, *dataptr
;
5378 unsigned int datasize
;
5379 unsigned short mlen
;
5381 trace_debug ("traceframe_read_sdata");
5383 tframe
= find_traceframe (tfnum
);
5387 trace_debug ("traceframe %d not found", tfnum
);
5391 datasize
= tframe
->data_size
;
5392 database
= &tframe
->data
[0];
5394 /* Iterate through a traceframe's blocks, looking for static
5396 dataptr
= traceframe_find_block_type (database
, datasize
,
5398 if (dataptr
!= NULL
)
5400 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5401 dataptr
+= sizeof (mlen
);
5404 if (offset
+ length
> mlen
)
5405 length
= mlen
- offset
;
5407 memcpy (buf
, dataptr
, length
);
5415 trace_debug ("traceframe %d has no static trace data", tfnum
);
5421 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5422 object. DATA is pointer to a struct buffer holding the
5423 traceframe-info object being built. */
5426 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5428 struct buffer
*buffer
= data
;
5434 unsigned short mlen
;
5437 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5438 dataptr
+= sizeof (maddr
);
5439 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5440 dataptr
+= sizeof (mlen
);
5441 buffer_xml_printf (buffer
,
5442 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5443 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5450 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5451 buffer_xml_printf (buffer
, "<tvar id=\"%d\"/>\n", vnum
);
5460 warning ("Unhandled trace block type (%d) '%c ' "
5461 "while building trace frame info.",
5462 blocktype
, blocktype
);
5469 /* Build a traceframe-info object for traceframe number TFNUM into
5473 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5475 struct traceframe
*tframe
;
5477 trace_debug ("traceframe_read_info");
5479 tframe
= find_traceframe (tfnum
);
5483 trace_debug ("traceframe %d not found", tfnum
);
5487 buffer_grow_str (buffer
, "<traceframe-info>\n");
5488 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5489 tfnum
, build_traceframe_info_xml
, buffer
);
5490 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5494 /* Return the first fast tracepoint whose jump pad contains PC. */
5496 static struct tracepoint
*
5497 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5499 struct tracepoint
*tpoint
;
5501 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5502 if (tpoint
->type
== fast_tracepoint
)
5503 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5509 /* Return the first fast tracepoint whose trampoline contains PC. */
5511 static struct tracepoint
*
5512 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5514 struct tracepoint
*tpoint
;
5516 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5518 if (tpoint
->type
== fast_tracepoint
5519 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5526 /* Return GDBserver's tracepoint that matches the IP Agent's
5527 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5530 static struct tracepoint
*
5531 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5533 struct tracepoint
*tpoint
;
5535 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5536 if (tpoint
->type
== fast_tracepoint
)
5537 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5545 /* The type of the object that is used to synchronize fast tracepoint
5548 typedef struct collecting_t
5550 /* The fast tracepoint number currently collecting. */
5553 /* A number that GDBserver can use to identify the thread that is
5554 presently holding the collect lock. This need not (and usually
5555 is not) the thread id, as getting the current thread ID usually
5556 requires a system call, which we want to avoid like the plague.
5557 Usually this is thread's TCB, found in the TLS (pseudo-)
5558 register, which is readable with a single insn on several
5560 uintptr_t thread_area
;
5563 #ifndef IN_PROCESS_AGENT
5566 force_unlock_trace_buffer (void)
5568 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5571 /* Check if the thread identified by THREAD_AREA which is stopped at
5572 STOP_PC, is presently locking the fast tracepoint collection, and
5573 if so, gather some status of said collection. Returns 0 if the
5574 thread isn't collecting or in the jump pad at all. 1, if in the
5575 jump pad (or within gdb_collect) and hasn't executed the adjusted
5576 original insn yet (can set a breakpoint there and run to it). 2,
5577 if presently executing the adjusted original insn --- in which
5578 case, if we want to move the thread out of the jump pad, we need to
5579 single-step it until this function returns 0. */
5582 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5584 struct fast_tpoint_collect_status
*status
)
5586 CORE_ADDR ipa_collecting
;
5587 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5588 CORE_ADDR ipa_gdb_trampoline_buffer
;
5589 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5590 struct tracepoint
*tpoint
;
5591 int needs_breakpoint
;
5593 /* The thread THREAD_AREA is either:
5595 0. not collecting at all, not within the jump pad, or within
5596 gdb_collect or one of its callees.
5598 1. in the jump pad and haven't reached gdb_collect
5600 2. within gdb_collect (out of the jump pad) (collect is set)
5602 3. we're in the jump pad, after gdb_collect having returned,
5603 possibly executing the adjusted insns.
5605 For cases 1 and 3, `collecting' may or not be set. The jump pad
5606 doesn't have any complicated jump logic, so we can tell if the
5607 thread is executing the adjust original insn or not by just
5608 matching STOP_PC with known jump pad addresses. If we it isn't
5609 yet executing the original insn, set a breakpoint there, and let
5610 the thread run to it, so to quickly step over a possible (many
5611 insns) gdb_collect call. Otherwise, or when the breakpoint is
5612 hit, only a few (small number of) insns are left to be executed
5613 in the jump pad. Single-step the thread until it leaves the
5618 needs_breakpoint
= 0;
5619 trace_debug ("fast_tracepoint_collecting");
5621 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5622 &ipa_gdb_jump_pad_buffer
))
5624 internal_error (__FILE__
, __LINE__
,
5625 "error extracting `gdb_jump_pad_buffer'");
5627 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5628 &ipa_gdb_jump_pad_buffer_end
))
5630 internal_error (__FILE__
, __LINE__
,
5631 "error extracting `gdb_jump_pad_buffer_end'");
5634 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5635 &ipa_gdb_trampoline_buffer
))
5637 internal_error (__FILE__
, __LINE__
,
5638 "error extracting `gdb_trampoline_buffer'");
5640 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5641 &ipa_gdb_trampoline_buffer_end
))
5643 internal_error (__FILE__
, __LINE__
,
5644 "error extracting `gdb_trampoline_buffer_end'");
5647 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5648 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5650 /* We can tell which tracepoint(s) the thread is collecting by
5651 matching the jump pad address back to the tracepoint. */
5652 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5655 warning ("in jump pad, but no matching tpoint?");
5660 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5662 tpoint
->number
, paddress (tpoint
->address
),
5663 paddress (tpoint
->jump_pad
),
5664 paddress (tpoint
->jump_pad_end
),
5665 paddress (tpoint
->adjusted_insn_addr
),
5666 paddress (tpoint
->adjusted_insn_addr_end
));
5669 /* Definitely in the jump pad. May or may not need
5670 fast-exit-jump-pad breakpoint. */
5671 if (tpoint
->jump_pad
<= stop_pc
5672 && stop_pc
< tpoint
->adjusted_insn_addr
)
5673 needs_breakpoint
= 1;
5675 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5676 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5678 /* We can tell which tracepoint(s) the thread is collecting by
5679 matching the trampoline address back to the tracepoint. */
5680 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5683 warning ("in trampoline, but no matching tpoint?");
5688 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5689 tpoint
->number
, paddress (tpoint
->address
),
5690 paddress (tpoint
->trampoline
),
5691 paddress (tpoint
->trampoline_end
));
5694 /* Have not reached jump pad yet, but treat the trampoline as a
5695 part of the jump pad that is before the adjusted original
5697 needs_breakpoint
= 1;
5701 collecting_t ipa_collecting_obj
;
5703 /* If `collecting' is set/locked, then the THREAD_AREA thread
5704 may or not be the one holding the lock. We have to read the
5705 lock to find out. */
5707 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5710 trace_debug ("fast_tracepoint_collecting:"
5711 " failed reading 'collecting' in the inferior");
5715 if (!ipa_collecting
)
5717 trace_debug ("fast_tracepoint_collecting: not collecting"
5718 " (and nobody is).");
5722 /* Some thread is collecting. Check which. */
5723 if (read_inferior_memory (ipa_collecting
,
5724 (unsigned char *) &ipa_collecting_obj
,
5725 sizeof (ipa_collecting_obj
)) != 0)
5728 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5730 trace_debug ("fast_tracepoint_collecting: not collecting "
5731 "(another thread is)");
5736 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5739 warning ("fast_tracepoint_collecting: collecting, "
5740 "but tpoint %s not found?",
5741 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5745 /* The thread is within `gdb_collect', skip over the rest of
5746 fast tracepoint collection quickly using a breakpoint. */
5747 needs_breakpoint
= 1;
5750 /* The caller wants a bit of status detail. */
5753 status
->tpoint_num
= tpoint
->number
;
5754 status
->tpoint_addr
= tpoint
->address
;
5755 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5756 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5759 if (needs_breakpoint
)
5761 /* Hasn't executed the original instruction yet. Set breakpoint
5762 there, and wait till it's hit, then single-step until exiting
5766 fast_tracepoint_collecting, returning continue-until-break at %s",
5767 paddress (tpoint
->adjusted_insn_addr
));
5769 return 1; /* continue */
5773 /* Just single-step until exiting the jump pad. */
5775 trace_debug ("fast_tracepoint_collecting, returning "
5776 "need-single-step (%s-%s)",
5777 paddress (tpoint
->adjusted_insn_addr
),
5778 paddress (tpoint
->adjusted_insn_addr_end
));
5780 return 2; /* single-step */
5786 #ifdef IN_PROCESS_AGENT
5788 /* The global fast tracepoint collect lock. Points to a collecting_t
5789 object built on the stack by the jump pad, if presently locked;
5790 NULL if it isn't locked. Note that this lock *must* be set while
5791 executing any *function other than the jump pad. See
5792 fast_tracepoint_collecting. */
5793 static collecting_t
* ATTR_USED collecting
;
5795 /* This routine, called from the jump pad (in asm) is designed to be
5796 called from the jump pads of fast tracepoints, thus it is on the
5799 IP_AGENT_EXPORT
void ATTR_USED
5800 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5802 struct fast_tracepoint_ctx ctx
;
5804 /* Don't do anything until the trace run is completely set up. */
5808 ctx
.base
.type
= fast_tracepoint
;
5810 ctx
.regcache_initted
= 0;
5811 /* Wrap the regblock in a register cache (in the stack, we don't
5812 want to malloc here). */
5813 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
5814 if (ctx
.regspace
== NULL
)
5816 trace_debug ("Trace buffer block allocation failed, skipping");
5820 for (ctx
.tpoint
= tpoint
;
5821 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5822 ctx
.tpoint
= ctx
.tpoint
->next
)
5824 if (!ctx
.tpoint
->enabled
)
5827 /* Multiple tracepoints of different types, such as fast tracepoint and
5828 static tracepoint, can be set at the same address. */
5829 if (ctx
.tpoint
->type
!= tpoint
->type
)
5832 /* Test the condition if present, and collect if true. */
5833 if (ctx
.tpoint
->cond
== NULL
5834 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5837 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5838 ctx
.tpoint
->address
, ctx
.tpoint
);
5840 /* Note that this will cause original insns to be written back
5841 to where we jumped from, but that's OK because we're jumping
5842 back to the next whole instruction. This will go badly if
5843 instruction restoration is not atomic though. */
5844 if (stopping_tracepoint
5845 || trace_buffer_is_full
5846 || expr_eval_result
!= expr_eval_no_error
)
5854 /* If there was a condition and it evaluated to false, the only
5855 way we would stop tracing is if there was an error during
5856 condition expression evaluation. */
5857 if (expr_eval_result
!= expr_eval_no_error
)
5868 #ifndef IN_PROCESS_AGENT
5871 get_raw_reg_func_addr (void)
5873 return ipa_sym_addrs
.addr_get_raw_reg
;
5877 get_get_tsv_func_addr (void)
5879 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5883 get_set_tsv_func_addr (void)
5885 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5889 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5890 CORE_ADDR
*jump_entry
)
5892 CORE_ADDR entry_point
= *jump_entry
;
5893 enum eval_result_type err
;
5895 trace_debug ("Starting condition compilation for tracepoint %d\n",
5898 /* Initialize the global pointer to the code being built. */
5899 current_insn_ptr
= *jump_entry
;
5903 err
= compile_bytecodes (tpoint
->cond
);
5905 if (err
== expr_eval_no_error
)
5909 /* Record the beginning of the compiled code. */
5910 tpoint
->compiled_cond
= entry_point
;
5912 trace_debug ("Condition compilation for tracepoint %d complete\n",
5917 /* Leave the unfinished code in situ, but don't point to it. */
5919 tpoint
->compiled_cond
= 0;
5921 trace_debug ("Condition compilation for tracepoint %d failed, "
5923 tpoint
->number
, err
);
5926 /* Update the code pointer passed in. Note that we do this even if
5927 the compile fails, so that we can look at the partial results
5928 instead of letting them be overwritten. */
5929 *jump_entry
= current_insn_ptr
;
5931 /* Leave a gap, to aid dump decipherment. */
5935 /* We'll need to adjust these when we consider bi-arch setups, and big
5939 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5941 return write_inferior_memory (where
,
5942 (unsigned char *) &ptr
, sizeof (void *));
5945 /* The base pointer of the IPA's heap. This is the only memory the
5946 IPA is allowed to use. The IPA should _not_ call the inferior's
5947 `malloc' during operation. That'd be slow, and, most importantly,
5948 it may not be safe. We may be collecting a tracepoint in a signal
5949 handler, for example. */
5950 static CORE_ADDR target_tp_heap
;
5952 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5956 target_malloc (ULONGEST size
)
5960 if (target_tp_heap
== 0)
5962 /* We have the pointer *address*, need what it points to. */
5963 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5966 internal_error (__FILE__
, __LINE__
,
5967 "couldn't get target heap head pointer");
5971 ptr
= target_tp_heap
;
5972 target_tp_heap
+= size
;
5974 /* Pad to 8-byte alignment. */
5975 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5981 download_agent_expr (struct agent_expr
*expr
)
5983 CORE_ADDR expr_addr
;
5984 CORE_ADDR expr_bytes
;
5986 expr_addr
= target_malloc (sizeof (*expr
));
5987 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5989 expr_bytes
= target_malloc (expr
->length
);
5990 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5992 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5997 /* Align V up to N bits. */
5998 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
6000 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
6003 download_tracepoint_1 (struct tracepoint
*tpoint
)
6005 struct tracepoint target_tracepoint
;
6006 CORE_ADDR tpptr
= 0;
6008 gdb_assert (tpoint
->type
== fast_tracepoint
6009 || tpoint
->type
== static_tracepoint
);
6011 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
6013 CORE_ADDR jentry
, jump_entry
;
6015 jentry
= jump_entry
= get_jump_space_head ();
6017 if (tpoint
->cond
!= NULL
)
6019 /* Pad to 8-byte alignment. (needed?) */
6020 /* Actually this should be left for the target to
6022 jentry
= UALIGN (jentry
, 8);
6024 compile_tracepoint_condition (tpoint
, &jentry
);
6027 /* Pad to 8-byte alignment. */
6028 jentry
= UALIGN (jentry
, 8);
6029 claim_jump_space (jentry
- jump_entry
);
6032 target_tracepoint
= *tpoint
;
6034 tpptr
= target_malloc (sizeof (*tpoint
));
6035 tpoint
->obj_addr_on_target
= tpptr
;
6037 /* Write the whole object. We'll fix up its pointers in a bit.
6038 Assume no next for now. This is fixed up above on the next
6039 iteration, if there's any. */
6040 target_tracepoint
.next
= NULL
;
6041 /* Need to clear this here too, since we're downloading the
6042 tracepoints before clearing our own copy. */
6043 target_tracepoint
.hit_count
= 0;
6045 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
6046 sizeof (target_tracepoint
));
6049 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6051 download_agent_expr (tpoint
->cond
));
6053 if (tpoint
->numactions
)
6056 CORE_ADDR actions_array
;
6058 /* The pointers array. */
6060 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6061 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6065 /* Now for each pointer, download the action. */
6066 for (i
= 0; i
< tpoint
->numactions
; i
++)
6068 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6069 CORE_ADDR ipa_action
= action
->ops
->download (action
);
6071 if (ipa_action
!= 0)
6072 write_inferior_data_ptr
6073 (actions_array
+ i
* sizeof (*tpoint
->actions
),
6079 #define IPA_PROTO_FAST_TRACE_FLAG 0
6080 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
6081 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
6082 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
6083 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
6085 /* Send a command to agent to download and install tracepoint TPOINT. */
6088 tracepoint_send_agent (struct tracepoint
*tpoint
)
6090 char buf
[IPA_CMD_BUF_SIZE
];
6095 strcpy (p
, "FastTrace:");
6098 COPY_FIELD_TO_BUF (p
, tpoint
, number
);
6099 COPY_FIELD_TO_BUF (p
, tpoint
, address
);
6100 COPY_FIELD_TO_BUF (p
, tpoint
, type
);
6101 COPY_FIELD_TO_BUF (p
, tpoint
, enabled
);
6102 COPY_FIELD_TO_BUF (p
, tpoint
, step_count
);
6103 COPY_FIELD_TO_BUF (p
, tpoint
, pass_count
);
6104 COPY_FIELD_TO_BUF (p
, tpoint
, numactions
);
6105 COPY_FIELD_TO_BUF (p
, tpoint
, hit_count
);
6106 COPY_FIELD_TO_BUF (p
, tpoint
, traceframe_usage
);
6107 COPY_FIELD_TO_BUF (p
, tpoint
, compiled_cond
);
6108 COPY_FIELD_TO_BUF (p
, tpoint
, orig_size
);
6111 p
= agent_expr_send (p
, tpoint
->cond
);
6113 /* tracepoint_action */
6114 for (i
= 0; i
< tpoint
->numactions
; i
++)
6116 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6118 p
[0] = action
->type
;
6119 p
= action
->ops
->send (&p
[1], action
);
6122 get_jump_space_head ();
6123 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6124 agent can use jump pad from it. */
6125 if (tpoint
->type
== fast_tracepoint
)
6127 memcpy (p
, &gdb_jump_pad_head
, 8);
6131 ret
= run_inferior_command (buf
, (int) (ptrdiff_t) (p
- buf
));
6135 if (strncmp (buf
, "OK", 2) != 0)
6138 /* The value of tracepoint's target address is stored in BUF. */
6139 memcpy (&tpoint
->obj_addr_on_target
,
6140 &buf
[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET
], 8);
6142 if (tpoint
->type
== fast_tracepoint
)
6145 = (unsigned char *) &buf
[IPA_PROTO_FAST_TRACE_FJUMP_INSN
];
6148 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6149 (unsigned int) tpoint
->obj_addr_on_target
,
6150 (unsigned int) gdb_jump_pad_head
);
6152 memcpy (&gdb_jump_pad_head
, &buf
[IPA_PROTO_FAST_TRACE_JUMP_PAD
], 8);
6154 /* This has been done in agent. We should also set up record for it. */
6155 memcpy (&fjump_size
, &buf
[IPA_PROTO_FAST_TRACE_FJUMP_SIZE
], 4);
6158 = set_fast_tracepoint_jump (tpoint
->address
, insn
, fjump_size
);
6165 download_tracepoint (struct tracepoint
*tpoint
)
6167 struct tracepoint
*tp
, *tp_prev
;
6169 if (tpoint
->type
!= fast_tracepoint
6170 && tpoint
->type
!= static_tracepoint
)
6173 download_tracepoint_1 (tpoint
);
6175 /* Find the previous entry of TPOINT, which is fast tracepoint or
6176 static tracepoint. */
6178 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6180 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6186 CORE_ADDR tp_prev_target_next_addr
;
6188 /* Insert TPOINT after TP_PREV in IPA. */
6189 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6190 + offsetof (struct tracepoint
, next
),
6191 &tp_prev_target_next_addr
))
6193 internal_error (__FILE__
, __LINE__
,
6194 "error reading `tp_prev->next'");
6197 /* tpoint->next = tp_prev->next */
6198 write_inferior_data_ptr (tpoint
->obj_addr_on_target
6199 + offsetof (struct tracepoint
, next
),
6200 tp_prev_target_next_addr
);
6201 /* tp_prev->next = tpoint */
6202 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
6203 + offsetof (struct tracepoint
, next
),
6204 tpoint
->obj_addr_on_target
);
6207 /* First object in list, set the head pointer in the
6209 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
6210 tpoint
->obj_addr_on_target
);
6215 download_trace_state_variables (void)
6217 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6218 struct trace_state_variable
*tsv
;
6220 /* Start out empty. */
6221 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6223 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6225 struct trace_state_variable target_tsv
;
6227 /* TSV's with a getter have been initialized equally in both the
6228 inferior and GDBserver. Skip them. */
6229 if (tsv
->getter
!= NULL
)
6235 ptr
= target_malloc (sizeof (*tsv
));
6237 if (tsv
== trace_state_variables
)
6239 /* First object in list, set the head pointer in the
6242 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6247 write_inferior_data_ptr (prev_ptr
6248 + offsetof (struct trace_state_variable
,
6253 /* Write the whole object. We'll fix up its pointers in a bit.
6254 Assume no next, fixup when needed. */
6255 target_tsv
.next
= NULL
;
6257 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6258 sizeof (target_tsv
));
6260 if (tsv
->name
!= NULL
)
6262 size_t size
= strlen (tsv
->name
) + 1;
6263 CORE_ADDR name_addr
= target_malloc (size
);
6264 write_inferior_memory (name_addr
,
6265 (unsigned char *) tsv
->name
, size
);
6266 write_inferior_data_ptr (ptr
6267 + offsetof (struct trace_state_variable
,
6272 gdb_assert (tsv
->getter
== NULL
);
6277 /* Fixup the next pointer in the last item in the list. */
6278 write_inferior_data_ptr (prev_ptr
6279 + offsetof (struct trace_state_variable
,
6284 /* Upload complete trace frames out of the IP Agent's trace buffer
6285 into GDBserver's trace buffer. This always uploads either all or
6286 no trace frames. This is the counter part of
6287 `trace_alloc_trace_buffer'. See its description of the atomic
6288 synching mechanism. */
6291 upload_fast_traceframes (void)
6293 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6294 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6296 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6297 unsigned int curr_tbctrl_idx
;
6298 unsigned int ipa_trace_buffer_ctrl_curr
;
6299 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6300 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6301 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6302 CORE_ADDR ipa_trace_buffer_lo
;
6303 CORE_ADDR ipa_trace_buffer_hi
;
6305 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6306 &ipa_traceframe_read_count_racy
))
6308 /* This will happen in most targets if the current thread is
6313 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6314 &ipa_traceframe_write_count_racy
))
6317 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6318 ipa_traceframe_write_count_racy
6319 - ipa_traceframe_read_count_racy
,
6320 ipa_traceframe_write_count_racy
,
6321 ipa_traceframe_read_count_racy
);
6323 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6326 about_to_request_buffer_space_bkpt
6327 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6330 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6331 &ipa_trace_buffer_ctrl_curr
))
6334 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6336 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6339 unsigned int prev
, counter
;
6341 /* Update the token, with new counters, and the GDBserver stamp
6342 bit. Alway reuse the current TBC index. */
6343 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6344 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6346 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6352 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6353 ipa_trace_buffer_ctrl_curr
))
6356 trace_debug ("Lib: Committed %08x -> %08x",
6357 ipa_trace_buffer_ctrl_curr_old
,
6358 ipa_trace_buffer_ctrl_curr
);
6360 /* Re-read these, now that we've installed the
6361 `about_to_request_buffer_space' breakpoint/lock. A thread could
6362 have finished a traceframe between the last read of these
6363 counters and setting the breakpoint above. If we start
6364 uploading, we never want to leave this function with
6365 traceframe_read_count != 0, otherwise, GDBserver could end up
6366 incrementing the counter tokens more than once (due to event loop
6367 nesting), which would break the IP agent's "effective" detection
6368 (see trace_alloc_trace_buffer). */
6369 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6370 &ipa_traceframe_read_count
))
6372 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6373 &ipa_traceframe_write_count
))
6378 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6379 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6380 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6382 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6383 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6384 trace_debug ("note that ipa_traceframe_count's parts changed");
6387 /* Get the address of the current TBC object (the IP agent has an
6388 array of 3 such objects). The index is stored in the TBC
6390 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6391 ipa_trace_buffer_ctrl_addr
6392 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6394 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6395 (unsigned char *) &ipa_trace_buffer_ctrl
,
6396 sizeof (struct ipa_trace_buffer_control
)))
6399 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6400 &ipa_trace_buffer_lo
))
6402 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6403 &ipa_trace_buffer_hi
))
6406 /* Offsets are easier to grok for debugging than raw addresses,
6407 especially for the small trace buffer sizes that are useful for
6409 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6410 "endfree=%d wrap=%d hi=%d",
6412 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6413 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6414 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6415 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6416 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6418 /* Note that the IPA's buffer is always circular. */
6420 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6422 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6423 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6425 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6426 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6427 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6428 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6431 tf
= IPA_FIRST_TRACEFRAME ();
6433 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6435 struct tracepoint
*tpoint
;
6436 struct traceframe
*tframe
;
6437 unsigned char *block
;
6438 struct traceframe ipa_tframe
;
6440 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6441 offsetof (struct traceframe
, data
)))
6442 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6444 if (ipa_tframe
.tpnum
== 0)
6446 internal_error (__FILE__
, __LINE__
,
6447 "Uploading: No (more) fast traceframes, but"
6448 " ipa_traceframe_count == %u??\n",
6449 ipa_traceframe_write_count
6450 - ipa_traceframe_read_count
);
6453 /* Note that this will be incorrect for multi-location
6455 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6457 tframe
= add_traceframe (tpoint
);
6460 trace_buffer_is_full
= 1;
6461 trace_debug ("Uploading: trace buffer is full");
6465 /* Copy the whole set of blocks in one go for now. FIXME:
6466 split this in smaller blocks. */
6467 block
= add_traceframe_block (tframe
, tpoint
,
6468 ipa_tframe
.data_size
);
6471 if (read_inferior_memory (tf
6472 + offsetof (struct traceframe
, data
),
6473 block
, ipa_tframe
.data_size
))
6474 error ("Uploading: Couldn't read traceframe data at %s\n",
6475 paddress (tf
+ offsetof (struct traceframe
, data
)));
6478 trace_debug ("Uploading: traceframe didn't fit");
6479 finish_traceframe (tframe
);
6482 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6484 /* If we freed the traceframe that wrapped around, go back
6485 to the non-wrap case. */
6486 if (tf
< ipa_trace_buffer_ctrl
.start
)
6488 trace_debug ("Lib: Discarding past the wraparound");
6489 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6491 ipa_trace_buffer_ctrl
.start
= tf
;
6492 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6493 ++ipa_traceframe_read_count
;
6495 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6496 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6498 trace_debug ("Lib: buffer is fully empty. "
6499 "Trace buffer [%d] start=%d free=%d endfree=%d",
6501 (int) (ipa_trace_buffer_ctrl
.start
6502 - ipa_trace_buffer_lo
),
6503 (int) (ipa_trace_buffer_ctrl
.free
6504 - ipa_trace_buffer_lo
),
6505 (int) (ipa_trace_buffer_ctrl
.end_free
6506 - ipa_trace_buffer_lo
));
6508 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6509 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6510 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6511 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6514 trace_debug ("Uploaded a traceframe\n"
6515 "Lib: Trace buffer [%d] start=%d free=%d "
6516 "endfree=%d wrap=%d hi=%d",
6518 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6519 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6520 (int) (ipa_trace_buffer_ctrl
.end_free
6521 - ipa_trace_buffer_lo
),
6522 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6523 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6526 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6527 (unsigned char *) &ipa_trace_buffer_ctrl
,
6528 sizeof (struct ipa_trace_buffer_control
)))
6531 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6532 ipa_traceframe_read_count
);
6534 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6538 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6539 about_to_request_buffer_space_bkpt
= NULL
;
6543 if (trace_buffer_is_full
)
6548 #ifdef IN_PROCESS_AGENT
6550 IP_AGENT_EXPORT
int ust_loaded
;
6551 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6555 /* Static tracepoints. */
6557 /* UST puts a "struct tracepoint" in the global namespace, which
6558 conflicts with our tracepoint. Arguably, being a library, it
6559 shouldn't take ownership of such a generic name. We work around it
6561 #define tracepoint ust_tracepoint
6562 #include <ust/ust.h>
6565 extern int serialize_to_text (char *outbuf
, int bufsize
,
6566 const char *fmt
, va_list ap
);
6568 #define GDB_PROBE_NAME "gdb"
6570 /* We dynamically search for the UST symbols instead of linking them
6571 in. This lets the user decide if the application uses static
6572 tracepoints, instead of always pulling libust.so in. This vector
6573 holds pointers to all functions we care about. */
6577 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6578 const char *fmt
, va_list ap
);
6580 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6581 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6583 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6585 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6588 void (*marker_iter_start
) (struct marker_iter
*iter
);
6589 void (*marker_iter_next
) (struct marker_iter
*iter
);
6590 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6591 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6596 /* Cast through typeof to catch incompatible API changes. Since UST
6597 only builds with gcc, we can freely use gcc extensions here
6599 #define GET_UST_SYM(SYM) \
6602 if (ust_ops.SYM == NULL) \
6603 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6604 if (ust_ops.SYM == NULL) \
6608 #define USTF(SYM) ust_ops.SYM
6610 /* Get pointers to all libust.so functions we care about. */
6615 GET_UST_SYM (serialize_to_text
);
6617 GET_UST_SYM (ltt_probe_register
);
6618 GET_UST_SYM (ltt_probe_unregister
);
6619 GET_UST_SYM (ltt_marker_connect
);
6620 GET_UST_SYM (ltt_marker_disconnect
);
6622 GET_UST_SYM (marker_iter_start
);
6623 GET_UST_SYM (marker_iter_next
);
6624 GET_UST_SYM (marker_iter_stop
);
6625 GET_UST_SYM (marker_iter_reset
);
6631 /* Given an UST marker, return the matching gdb static tracepoint.
6632 The match is done by address. */
6634 static struct tracepoint
*
6635 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6637 struct tracepoint
*tpoint
;
6639 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6641 if (tpoint
->type
!= static_tracepoint
)
6644 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6651 /* The probe function we install on lttng/ust markers. Whenever a
6652 probed ust marker is hit, this function is called. This is similar
6653 to gdb_collect, only for static tracepoints, instead of fast
6657 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6658 struct registers
*regs
, void *call_private
,
6659 const char *fmt
, va_list *args
)
6661 struct tracepoint
*tpoint
;
6662 struct static_tracepoint_ctx ctx
;
6664 /* Don't do anything until the trace run is completely set up. */
6667 trace_debug ("gdb_probe: not tracing\n");
6671 ctx
.base
.type
= static_tracepoint
;
6672 ctx
.regcache_initted
= 0;
6677 /* Wrap the regblock in a register cache (in the stack, we don't
6678 want to malloc here). */
6679 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
6680 if (ctx
.regspace
== NULL
)
6682 trace_debug ("Trace buffer block allocation failed, skipping");
6686 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6689 trace_debug ("gdb_probe: marker not known: "
6690 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6691 mdata
->location
, mdata
->channel
,
6692 mdata
->name
, mdata
->format
);
6696 if (!tpoint
->enabled
)
6698 trace_debug ("gdb_probe: tracepoint disabled");
6702 ctx
.tpoint
= tpoint
;
6704 trace_debug ("gdb_probe: collecting marker: "
6705 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6706 mdata
->location
, mdata
->channel
,
6707 mdata
->name
, mdata
->format
);
6709 /* Test the condition if present, and collect if true. */
6710 if (tpoint
->cond
== NULL
6711 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6714 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6715 tpoint
->address
, tpoint
);
6717 if (stopping_tracepoint
6718 || trace_buffer_is_full
6719 || expr_eval_result
!= expr_eval_no_error
)
6724 /* If there was a condition and it evaluated to false, the only
6725 way we would stop tracing is if there was an error during
6726 condition expression evaluation. */
6727 if (expr_eval_result
!= expr_eval_no_error
)
6732 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6733 static tracepoint string data. This is a string passed to the
6734 tracing library by the user, at the time of the tracepoint marker
6735 call. E.g., in the UST marker call:
6737 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6739 the collected data is "str FOOBAZ".
6743 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6744 struct traceframe
*tframe
)
6746 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6747 unsigned char *bufspace
;
6750 unsigned short blocklen
;
6754 trace_debug ("Wanted to collect static trace data, "
6755 "but there's no static trace data");
6759 va_copy (copy
, *umd
->args
);
6760 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6763 trace_debug ("Want to collect ust data");
6765 /* 'S' + size + string */
6766 bufspace
= add_traceframe_block (tframe
, umd
->tpoint
,
6767 1 + sizeof (blocklen
) + size
+ 1);
6768 if (bufspace
== NULL
)
6770 trace_debug ("Trace buffer block allocation failed, skipping");
6774 /* Identify a static trace data block. */
6777 blocklen
= size
+ 1;
6778 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6780 va_copy (copy
, *umd
->args
);
6781 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6782 size
+ 1, umd
->fmt
, copy
);
6785 trace_debug ("Storing static tracepoint data in regblock: %s",
6786 bufspace
+ 1 + sizeof (blocklen
));
6789 /* The probe to register with lttng/ust. */
6790 static struct ltt_available_probe gdb_ust_probe
=
6797 #endif /* HAVE_UST */
6798 #endif /* IN_PROCESS_AGENT */
6800 #ifndef IN_PROCESS_AGENT
6802 /* Ask the in-process agent to run a command. Since we don't want to
6803 have to handle the IPA hitting breakpoints while running the
6804 command, we pause all threads, remove all breakpoints, and then set
6805 the helper thread re-running. We communicate with the helper
6806 thread by means of direct memory xfering, and a socket for
6810 run_inferior_command (char *cmd
, int len
)
6813 int pid
= ptid_get_pid (current_ptid
);
6815 trace_debug ("run_inferior_command: running: %s", cmd
);
6818 uninsert_all_breakpoints ();
6820 err
= agent_run_command (pid
, (const char *) cmd
, len
);
6822 reinsert_all_breakpoints ();
6828 #else /* !IN_PROCESS_AGENT */
6830 #include <sys/socket.h>
6833 #ifndef UNIX_PATH_MAX
6834 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6837 /* Where we put the socked used for synchronization. */
6838 #define SOCK_DIR P_tmpdir
6840 /* Thread ID of the helper thread. GDBserver reads this to know which
6841 is the help thread. This is an LWP id on Linux. */
6842 int helper_thread_id
;
6845 init_named_socket (const char *name
)
6848 struct sockaddr_un addr
;
6850 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6853 warning ("socket creation failed: %s", strerror (errno
));
6857 addr
.sun_family
= AF_UNIX
;
6859 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6860 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6862 result
= access (name
, F_OK
);
6866 result
= unlink (name
);
6869 warning ("unlink failed: %s", strerror (errno
));
6873 warning ("socket %s already exists; overwriting", name
);
6876 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6879 warning ("bind failed: %s", strerror (errno
));
6884 result
= listen (fd
, 1);
6887 warning ("listen: %s", strerror (errno
));
6895 static char agent_socket_name
[UNIX_PATH_MAX
];
6898 gdb_agent_socket_init (void)
6902 result
= xsnprintf (agent_socket_name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6903 SOCK_DIR
, getpid ());
6904 if (result
>= UNIX_PATH_MAX
)
6906 trace_debug ("string overflow allocating socket name");
6910 fd
= init_named_socket (agent_socket_name
);
6912 warning ("Error initializing named socket (%s) for communication with the "
6913 "ust helper thread. Check that directory exists and that it "
6914 "is writable.", agent_socket_name
);
6921 /* The next marker to be returned on a qTsSTM command. */
6922 static const struct marker
*next_st
;
6924 /* Returns the first known marker. */
6929 struct marker_iter iter
;
6931 USTF(marker_iter_reset
) (&iter
);
6932 USTF(marker_iter_start
) (&iter
);
6937 /* Returns the marker following M. */
6939 const struct marker
*
6940 next_marker (const struct marker
*m
)
6942 struct marker_iter iter
;
6944 USTF(marker_iter_reset
) (&iter
);
6945 USTF(marker_iter_start
) (&iter
);
6947 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6949 if (iter
.marker
== m
)
6951 USTF(marker_iter_next
) (&iter
);
6959 /* Return an hexstr version of the STR C string, fit for sending to
6963 cstr_to_hexstr (const char *str
)
6965 int len
= strlen (str
);
6966 char *hexstr
= xmalloc (len
* 2 + 1);
6967 bin2hex ((gdb_byte
*) str
, hexstr
, len
);
6971 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6975 response_ust_marker (char *packet
, const struct marker
*st
)
6977 char *strid
, *format
, *tmp
;
6979 next_st
= next_marker (st
);
6981 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6982 strlen (st
->name
) + 1);
6983 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6985 strid
= cstr_to_hexstr (tmp
);
6988 format
= cstr_to_hexstr (st
->format
);
6990 sprintf (packet
, "m%s:%s:%s",
6991 paddress ((uintptr_t) st
->location
),
6999 /* Return the first static tracepoint, and initialize the state
7000 machine that will iterate through all the static tracepoints. */
7003 cmd_qtfstm (char *packet
)
7005 trace_debug ("Returning first trace state variable definition");
7007 if (first_marker ())
7008 response_ust_marker (packet
, first_marker ());
7010 strcpy (packet
, "l");
7013 /* Return additional trace state variable definitions. */
7016 cmd_qtsstm (char *packet
)
7018 trace_debug ("Returning static tracepoint");
7021 response_ust_marker (packet
, next_st
);
7023 strcpy (packet
, "l");
7026 /* Disconnect the GDB probe from a marker at a given address. */
7029 unprobe_marker_at (char *packet
)
7033 struct marker_iter iter
;
7035 p
+= sizeof ("unprobe_marker_at:") - 1;
7037 p
= unpack_varlen_hex (p
, &address
);
7039 USTF(marker_iter_reset
) (&iter
);
7040 USTF(marker_iter_start
) (&iter
);
7041 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7042 if ((uintptr_t ) iter
.marker
->location
== address
)
7046 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7050 warning ("could not disable marker %s/%s",
7051 iter
.marker
->channel
, iter
.marker
->name
);
7056 /* Connect the GDB probe to a marker at a given address. */
7059 probe_marker_at (char *packet
)
7063 struct marker_iter iter
;
7066 p
+= sizeof ("probe_marker_at:") - 1;
7068 p
= unpack_varlen_hex (p
, &address
);
7070 USTF(marker_iter_reset
) (&iter
);
7072 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7074 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7075 if ((uintptr_t ) m
->location
== address
)
7079 trace_debug ("found marker for address. "
7080 "ltt_marker_connect (marker = %s/%s)",
7081 m
->channel
, m
->name
);
7083 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7085 if (result
&& result
!= -EEXIST
)
7086 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7087 m
->channel
, m
->name
, -result
);
7091 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7092 m
->channel
, m
->name
);
7096 strcpy (packet
, "OK");
7100 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7105 cmd_qtstmat (char *packet
)
7109 struct marker_iter iter
;
7112 p
+= sizeof ("qTSTMat:") - 1;
7114 p
= unpack_varlen_hex (p
, &address
);
7116 USTF(marker_iter_reset
) (&iter
);
7118 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7120 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7121 if ((uintptr_t ) m
->location
== address
)
7123 response_ust_marker (packet
, m
);
7127 strcpy (packet
, "l");
7137 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7140 #endif /* HAVE_UST */
7142 #include <sys/syscall.h>
7145 gdb_agent_remove_socket (void)
7147 unlink (agent_socket_name
);
7150 /* Helper thread of agent. */
7153 gdb_agent_helper_thread (void *arg
)
7157 atexit (gdb_agent_remove_socket
);
7161 listen_fd
= gdb_agent_socket_init ();
7163 if (helper_thread_id
== 0)
7164 helper_thread_id
= syscall (SYS_gettid
);
7166 if (listen_fd
== -1)
7168 warning ("could not create sync socket\n");
7175 struct sockaddr_un sockaddr
;
7181 tmp
= sizeof (sockaddr
);
7185 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7187 /* It seems an ERESTARTSYS can escape out of accept. */
7188 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7192 warning ("Accept returned %d, error: %s\n",
7193 fd
, strerror (errno
));
7199 ret
= read (fd
, buf
, 1);
7200 } while (ret
== -1 && errno
== EINTR
);
7204 warning ("reading socket (fd=%d) failed with %s",
7205 fd
, strerror (errno
));
7212 if (strncmp ("close", cmd_buf
, 5) == 0)
7217 else if (strcmp ("qTfSTM", cmd_buf
) == 0)
7219 cmd_qtfstm (cmd_buf
);
7221 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7223 cmd_qtsstm (cmd_buf
);
7225 else if (strncmp ("unprobe_marker_at:",
7227 sizeof ("unprobe_marker_at:") - 1) == 0)
7229 unprobe_marker_at (cmd_buf
);
7231 else if (strncmp ("probe_marker_at:",
7233 sizeof ("probe_marker_at:") - 1) == 0)
7235 probe_marker_at (cmd_buf
);
7237 else if (strncmp ("qTSTMat:",
7239 sizeof ("qTSTMat:") - 1) == 0)
7241 cmd_qtstmat (cmd_buf
);
7243 #endif /* HAVE_UST */
7246 /* Fix compiler's warning: ignoring return value of 'write'. */
7247 ret
= write (fd
, buf
, 1);
7253 unlink (agent_socket_name
);
7255 /* Sleep endlessly to wait the whole inferior stops. This
7256 thread can not exit because GDB or GDBserver may still need
7257 'current_thread' (representing this thread) to access
7258 inferior memory. Otherwise, this thread exits earlier than
7259 other threads, and 'current_thread' is set to NULL. */
7270 #include <pthread.h>
7272 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
7275 gdb_agent_init (void)
7282 /* We want the helper thread to be as transparent as possible, so
7283 have it inherit an all-signals-blocked mask. */
7285 sigfillset (&new_mask
);
7286 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7288 perror_with_name ("pthread_sigmask (1)");
7290 res
= pthread_create (&thread
,
7292 gdb_agent_helper_thread
,
7295 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7297 perror_with_name ("pthread_sigmask (2)");
7299 while (helper_thread_id
== 0)
7307 #include <sys/mman.h>
7310 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7311 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7312 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7313 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
7314 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
7315 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
7317 /* Record the result of getting buffer space for fast tracepoint
7318 trampolines. Any error message is copied, since caller may not be
7319 using persistent storage. */
7322 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7324 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7325 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7327 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7329 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7332 static void __attribute__ ((constructor
))
7333 initialize_tracepoint_ftlib (void)
7335 initialize_tracepoint ();
7340 #endif /* IN_PROCESS_AGENT */
7342 /* Return a timestamp, expressed as microseconds of the usual Unix
7343 time. (As the result is a 64-bit number, it will not overflow any
7347 get_timestamp (void)
7351 if (gettimeofday (&tv
, 0) != 0)
7354 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7358 initialize_tracepoint (void)
7360 /* Start with the default size. */
7361 init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE
);
7363 /* Wire trace state variable 1 to be the timestamp. This will be
7364 uploaded to GDB upon connection and become one of its trace state
7365 variables. (In case you're wondering, if GDB already has a trace
7366 variable numbered 1, it will be renumbered.) */
7367 create_trace_state_variable (1, 0);
7368 set_trace_state_variable_name (1, "trace_timestamp");
7369 set_trace_state_variable_getter (1, get_timestamp
);
7371 #ifdef IN_PROCESS_AGENT
7376 pagesize
= sysconf (_SC_PAGE_SIZE
);
7378 perror_with_name ("sysconf");
7380 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7382 #define SCRATCH_BUFFER_NPAGES 20
7384 /* Allocate scratch buffer aligned on a page boundary, at a low
7385 address (close to the main executable's code). */
7386 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7388 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7389 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7390 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7392 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7397 perror_with_name ("mmap");
7399 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7402 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7404 /* It's not a fatal error for something to go wrong with trampoline
7405 buffer setup, but it can be mysterious, so create a channel to
7406 report back on what went wrong, using a fixed size since we may
7407 not be able to allocate space later when the problem occurs. */
7408 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7410 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7412 initialize_low_tracepoint ();