1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2013 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 "gdbthread.h"
33 #define DEFAULT_TRACE_BUFFER_SIZE 5242880 /* 5*1024*1024 */
35 /* This file is built for both GDBserver, and the in-process
36 agent (IPA), a shared library that includes a tracing agent that is
37 loaded by the inferior to support fast tracepoints. Fast
38 tracepoints (or more accurately, jump based tracepoints) are
39 implemented by patching the tracepoint location with a jump into a
40 small trampoline function whose job is to save the register state,
41 call the in-process tracing agent, and then execute the original
42 instruction that was under the tracepoint jump (possibly adjusted,
43 if PC-relative, or some such).
45 The current synchronization design is pull based. That means,
46 GDBserver does most of the work, by peeking/poking at the inferior
47 agent's memory directly for downloading tracepoint and associated
48 objects, and for uploading trace frames. Whenever the IPA needs
49 something from GDBserver (trace buffer is full, tracing stopped for
50 some reason, etc.) the IPA calls a corresponding hook function
51 where GDBserver has placed a breakpoint.
53 Each of the agents has its own trace buffer. When browsing the
54 trace frames built from slow and fast tracepoints from GDB (tfind
55 mode), there's no guarantee the user is seeing the trace frames in
56 strict chronological creation order, although, GDBserver tries to
57 keep the order relatively reasonable, by syncing the trace buffers
62 static void trace_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
65 trace_vdebug (const char *fmt
, ...)
71 vsprintf (buf
, fmt
, ap
);
72 fprintf (stderr
, PROG
"/tracepoint: %s\n", buf
);
76 #define trace_debug_1(level, fmt, args...) \
78 if (level <= debug_threads) \
79 trace_vdebug ((fmt), ##args); \
82 #define trace_debug(FMT, args...) \
83 trace_debug_1 (1, FMT, ##args)
86 # define ATTR_USED __attribute__((used))
87 # define ATTR_NOINLINE __attribute__((noinline))
88 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
91 # define ATTR_NOINLINE
92 # define ATTR_CONSTRUCTOR
95 /* Make sure the functions the IPA needs to export (symbols GDBserver
96 needs to query GDB about) are exported. */
98 #ifdef IN_PROCESS_AGENT
99 # if defined _WIN32 || defined __CYGWIN__
100 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
103 # define IP_AGENT_EXPORT \
104 __attribute__ ((visibility("default"))) ATTR_USED
106 # define IP_AGENT_EXPORT ATTR_USED
110 # define IP_AGENT_EXPORT
113 /* Prefix exported symbols, for good citizenship. All the symbols
114 that need exporting are defined in this module. */
115 #ifdef IN_PROCESS_AGENT
116 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
117 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
118 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
119 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
120 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
121 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
122 # define collecting gdb_agent_collecting
123 # define gdb_collect gdb_agent_gdb_collect
124 # define stop_tracing gdb_agent_stop_tracing
125 # define flush_trace_buffer gdb_agent_flush_trace_buffer
126 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
127 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
128 # define stopping_tracepoint gdb_agent_stopping_tracepoint
129 # define expr_eval_result gdb_agent_expr_eval_result
130 # define error_tracepoint gdb_agent_error_tracepoint
131 # define tracepoints gdb_agent_tracepoints
132 # define tracing gdb_agent_tracing
133 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
134 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
135 # define trace_buffer_lo gdb_agent_trace_buffer_lo
136 # define trace_buffer_hi gdb_agent_trace_buffer_hi
137 # define traceframe_read_count gdb_agent_traceframe_read_count
138 # define traceframe_write_count gdb_agent_traceframe_write_count
139 # define traceframes_created gdb_agent_traceframes_created
140 # define trace_state_variables gdb_agent_trace_state_variables
141 # define get_raw_reg gdb_agent_get_raw_reg
142 # define get_trace_state_variable_value \
143 gdb_agent_get_trace_state_variable_value
144 # define set_trace_state_variable_value \
145 gdb_agent_set_trace_state_variable_value
146 # define ust_loaded gdb_agent_ust_loaded
147 # define helper_thread_id gdb_agent_helper_thread_id
148 # define cmd_buf gdb_agent_cmd_buf
151 #ifndef IN_PROCESS_AGENT
153 /* Addresses of in-process agent's symbols GDBserver cares about. */
155 struct ipa_sym_addresses
157 CORE_ADDR addr_gdb_tp_heap_buffer
;
158 CORE_ADDR addr_gdb_jump_pad_buffer
;
159 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
160 CORE_ADDR addr_gdb_trampoline_buffer
;
161 CORE_ADDR addr_gdb_trampoline_buffer_end
;
162 CORE_ADDR addr_gdb_trampoline_buffer_error
;
163 CORE_ADDR addr_collecting
;
164 CORE_ADDR addr_gdb_collect
;
165 CORE_ADDR addr_stop_tracing
;
166 CORE_ADDR addr_flush_trace_buffer
;
167 CORE_ADDR addr_about_to_request_buffer_space
;
168 CORE_ADDR addr_trace_buffer_is_full
;
169 CORE_ADDR addr_stopping_tracepoint
;
170 CORE_ADDR addr_expr_eval_result
;
171 CORE_ADDR addr_error_tracepoint
;
172 CORE_ADDR addr_tracepoints
;
173 CORE_ADDR addr_tracing
;
174 CORE_ADDR addr_trace_buffer_ctrl
;
175 CORE_ADDR addr_trace_buffer_ctrl_curr
;
176 CORE_ADDR addr_trace_buffer_lo
;
177 CORE_ADDR addr_trace_buffer_hi
;
178 CORE_ADDR addr_traceframe_read_count
;
179 CORE_ADDR addr_traceframe_write_count
;
180 CORE_ADDR addr_traceframes_created
;
181 CORE_ADDR addr_trace_state_variables
;
182 CORE_ADDR addr_get_raw_reg
;
183 CORE_ADDR addr_get_trace_state_variable_value
;
184 CORE_ADDR addr_set_trace_state_variable_value
;
185 CORE_ADDR addr_ust_loaded
;
194 IPA_SYM(gdb_tp_heap_buffer
),
195 IPA_SYM(gdb_jump_pad_buffer
),
196 IPA_SYM(gdb_jump_pad_buffer_end
),
197 IPA_SYM(gdb_trampoline_buffer
),
198 IPA_SYM(gdb_trampoline_buffer_end
),
199 IPA_SYM(gdb_trampoline_buffer_error
),
201 IPA_SYM(gdb_collect
),
202 IPA_SYM(stop_tracing
),
203 IPA_SYM(flush_trace_buffer
),
204 IPA_SYM(about_to_request_buffer_space
),
205 IPA_SYM(trace_buffer_is_full
),
206 IPA_SYM(stopping_tracepoint
),
207 IPA_SYM(expr_eval_result
),
208 IPA_SYM(error_tracepoint
),
209 IPA_SYM(tracepoints
),
211 IPA_SYM(trace_buffer_ctrl
),
212 IPA_SYM(trace_buffer_ctrl_curr
),
213 IPA_SYM(trace_buffer_lo
),
214 IPA_SYM(trace_buffer_hi
),
215 IPA_SYM(traceframe_read_count
),
216 IPA_SYM(traceframe_write_count
),
217 IPA_SYM(traceframes_created
),
218 IPA_SYM(trace_state_variables
),
219 IPA_SYM(get_raw_reg
),
220 IPA_SYM(get_trace_state_variable_value
),
221 IPA_SYM(set_trace_state_variable_value
),
225 static struct ipa_sym_addresses ipa_sym_addrs
;
227 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
229 /* Returns true if both the in-process agent library and the static
230 tracepoints libraries are loaded in the inferior, and agent has
231 capability on static tracepoints. */
234 in_process_agent_supports_ust (void)
238 if (!agent_loaded_p ())
240 warning ("In-process agent not loaded");
244 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE
))
246 /* Agent understands static tracepoint, then check whether UST is in
247 fact loaded in the inferior. */
248 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
250 warning ("Error reading ust_loaded in lib");
261 write_e_ipa_not_loaded (char *buffer
)
264 "E.In-process agent library not loaded in process. "
265 "Fast and static tracepoints unavailable.");
268 /* Write an error to BUFFER indicating that UST isn't loaded in the
272 write_e_ust_not_loaded (char *buffer
)
276 "E.UST library not loaded in process. "
277 "Static tracepoints unavailable.");
279 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
283 /* If the in-process agent library isn't loaded in the inferior, write
284 an error to BUFFER, and return 1. Otherwise, return 0. */
287 maybe_write_ipa_not_loaded (char *buffer
)
289 if (!agent_loaded_p ())
291 write_e_ipa_not_loaded (buffer
);
297 /* If the in-process agent library and the ust (static tracepoints)
298 library aren't loaded in the inferior, write an error to BUFFER,
299 and return 1. Otherwise, return 0. */
302 maybe_write_ipa_ust_not_loaded (char *buffer
)
304 if (!agent_loaded_p ())
306 write_e_ipa_not_loaded (buffer
);
309 else if (!in_process_agent_supports_ust ())
311 write_e_ust_not_loaded (buffer
);
317 /* Cache all future symbols that the tracepoints module might request.
318 We can not request symbols at arbitrary states in the remote
319 protocol, only when the client tells us that new symbols are
320 available. So when we load the in-process library, make sure to
321 check the entire list. */
324 tracepoint_look_up_symbols (void)
328 if (agent_loaded_p ())
331 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
334 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
336 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
339 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
344 agent_look_up_symbols (NULL
);
349 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
350 of the "stop_tracing" function. When this breakpoint is hit,
351 tracing stopped in the IPA for some reason. E.g., due to
352 tracepoint reaching the pass count, hitting conditional expression
353 evaluation error, etc.
355 The IPA's trace buffer is never in circular tracing mode: instead,
356 GDBserver's is, and whenever the in-process buffer fills, it calls
357 "flush_trace_buffer", which triggers an internal breakpoint.
358 GDBserver reacts to this breakpoint by pulling the meanwhile
359 collected data. Old frames discarding is always handled on the
362 #ifdef IN_PROCESS_AGENT
364 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
366 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
370 /* Call this in the functions where GDBserver places a breakpoint, so
371 that the compiler doesn't try to be clever and skip calling the
372 function at all. This is necessary, even if we tell the compiler
373 to not inline said functions. */
375 #if defined(__GNUC__)
376 # define UNKNOWN_SIDE_EFFECTS() asm ("")
378 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
381 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
384 /* GDBserver places breakpoint here. */
385 UNKNOWN_SIDE_EFFECTS();
388 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
389 flush_trace_buffer (void)
391 /* GDBserver places breakpoint here. */
392 UNKNOWN_SIDE_EFFECTS();
397 #ifndef IN_PROCESS_AGENT
399 tracepoint_handler (CORE_ADDR address
)
401 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
406 /* Breakpoint at "stop_tracing" in the inferior lib. */
407 struct breakpoint
*stop_tracing_bkpt
;
408 static int stop_tracing_handler (CORE_ADDR
);
410 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
411 struct breakpoint
*flush_trace_buffer_bkpt
;
412 static int flush_trace_buffer_handler (CORE_ADDR
);
414 static void download_trace_state_variables (void);
415 static void upload_fast_traceframes (void);
417 static int run_inferior_command (char *cmd
, int len
);
420 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
422 return read_inferior_memory (symaddr
, (unsigned char *) val
,
427 static int tracepoint_send_agent (struct tracepoint
*tpoint
);
430 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
432 return read_inferior_memory (symaddr
, (unsigned char *) val
,
437 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
439 void *pval
= (void *) (uintptr_t) val
;
442 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
443 *val
= (uintptr_t) pval
;
448 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
450 void *pval
= (void *) (uintptr_t) val
;
451 return write_inferior_memory (symaddr
,
452 (unsigned char *) &pval
, sizeof (pval
));
456 write_inferior_integer (CORE_ADDR symaddr
, int val
)
458 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
462 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
464 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
467 static CORE_ADDR
target_malloc (ULONGEST size
);
468 static int write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
);
470 #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD) \
472 memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \
473 BUF += sizeof ((OBJ)->FIELD); \
478 /* Operations on various types of tracepoint actions. */
480 struct tracepoint_action
;
482 struct tracepoint_action_ops
484 /* Download tracepoint action ACTION to IPA. Return the address of action
486 CORE_ADDR (*download
) (const struct tracepoint_action
*action
);
488 /* Send ACTION to agent via command buffer started from BUFFER. Return
489 updated head of command buffer. */
490 char* (*send
) (char *buffer
, const struct tracepoint_action
*action
);
493 /* Base action. Concrete actions inherit this. */
495 struct tracepoint_action
497 #ifndef IN_PROCESS_AGENT
498 const struct tracepoint_action_ops
*ops
;
503 /* An 'M' (collect memory) action. */
504 struct collect_memory_action
506 struct tracepoint_action base
;
513 /* An 'R' (collect registers) action. */
515 struct collect_registers_action
517 struct tracepoint_action base
;
520 /* An 'X' (evaluate expression) action. */
522 struct eval_expr_action
524 struct tracepoint_action base
;
526 struct agent_expr
*expr
;
529 /* An 'L' (collect static trace data) action. */
530 struct collect_static_trace_data_action
532 struct tracepoint_action base
;
535 #ifndef IN_PROCESS_AGENT
537 m_tracepoint_action_download (const struct tracepoint_action
*action
)
539 int size_in_ipa
= (sizeof (struct collect_memory_action
)
540 - offsetof (struct tracepoint_action
, type
));
541 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
543 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
549 m_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
551 struct collect_memory_action
*maction
552 = (struct collect_memory_action
*) action
;
554 COPY_FIELD_TO_BUF (buffer
, maction
, addr
);
555 COPY_FIELD_TO_BUF (buffer
, maction
, len
);
556 COPY_FIELD_TO_BUF (buffer
, maction
, basereg
);
561 static const struct tracepoint_action_ops m_tracepoint_action_ops
=
563 m_tracepoint_action_download
,
564 m_tracepoint_action_send
,
568 r_tracepoint_action_download (const struct tracepoint_action
*action
)
570 int size_in_ipa
= (sizeof (struct collect_registers_action
)
571 - offsetof (struct tracepoint_action
, type
));
572 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
574 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
581 r_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
586 static const struct tracepoint_action_ops r_tracepoint_action_ops
=
588 r_tracepoint_action_download
,
589 r_tracepoint_action_send
,
592 static CORE_ADDR
download_agent_expr (struct agent_expr
*expr
);
595 x_tracepoint_action_download (const struct tracepoint_action
*action
)
597 int size_in_ipa
= (sizeof (struct eval_expr_action
)
598 - offsetof (struct tracepoint_action
, type
));
599 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
602 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
604 expr
= download_agent_expr (((struct eval_expr_action
*)action
)->expr
);
605 write_inferior_data_ptr (ipa_action
+ offsetof (struct eval_expr_action
, expr
)
606 - offsetof (struct tracepoint_action
, type
),
612 /* Copy agent expression AEXPR to buffer pointed by P. If AEXPR is NULL,
613 copy 0 to P. Return updated header of buffer. */
616 agent_expr_send (char *p
, const struct agent_expr
*aexpr
)
618 /* Copy the length of condition first, and then copy its
627 memcpy (p
, &aexpr
->length
, 4);
630 memcpy (p
, aexpr
->bytes
, aexpr
->length
);
637 x_tracepoint_action_send ( char *buffer
, const struct tracepoint_action
*action
)
639 struct eval_expr_action
*eaction
= (struct eval_expr_action
*) action
;
641 return agent_expr_send (buffer
, eaction
->expr
);
644 static const struct tracepoint_action_ops x_tracepoint_action_ops
=
646 x_tracepoint_action_download
,
647 x_tracepoint_action_send
,
651 l_tracepoint_action_download (const struct tracepoint_action
*action
)
653 int size_in_ipa
= (sizeof (struct collect_static_trace_data_action
)
654 - offsetof (struct tracepoint_action
, type
));
655 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
657 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
664 l_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
669 static const struct tracepoint_action_ops l_tracepoint_action_ops
=
671 l_tracepoint_action_download
,
672 l_tracepoint_action_send
,
676 /* This structure describes a piece of the source-level definition of
677 the tracepoint. The contents are not interpreted by the target,
678 but preserved verbatim for uploading upon reconnection. */
682 /* The type of string, such as "cond" for a conditional. */
685 /* The source-level string itself. For the sake of target
686 debugging, we store it in plaintext, even though it is always
687 transmitted in hex. */
690 /* Link to the next one in the list. We link them in the order
691 received, in case some make up an ordered list of commands or
693 struct source_string
*next
;
698 /* Trap based tracepoint. */
701 /* A fast tracepoint implemented with a jump instead of a trap. */
704 /* A static tracepoint, implemented by a program call into a tracing
709 struct tracepoint_hit_ctx
;
711 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
714 /* The definition of a tracepoint. */
716 /* Tracepoints may have multiple locations, each at a different
717 address. This can occur with optimizations, template
718 instantiation, etc. Since the locations may be in different
719 scopes, the conditions and actions may be different for each
720 location. Our target version of tracepoints is more like GDB's
721 notion of "breakpoint locations", but we have almost nothing that
722 is not per-location, so we bother having two kinds of objects. The
723 key consequence is that numbers are not unique, and that it takes
724 both number and address to identify a tracepoint uniquely. */
728 /* The number of the tracepoint, as specified by GDB. Several
729 tracepoint objects here may share a number. */
732 /* Address at which the tracepoint is supposed to trigger. Several
733 tracepoints may share an address. */
736 /* Tracepoint type. */
737 enum tracepoint_type type
;
739 /* True if the tracepoint is currently enabled. */
742 /* The number of single steps that will be performed after each
746 /* The number of times the tracepoint may be hit before it will
747 terminate the entire tracing run. */
750 /* Pointer to the agent expression that is the tracepoint's
751 conditional, or NULL if the tracepoint is unconditional. */
752 struct agent_expr
*cond
;
754 /* The list of actions to take when the tracepoint triggers. */
756 struct tracepoint_action
**actions
;
758 /* Count of the times we've hit this tracepoint during the run.
759 Note that while-stepping steps are not counted as "hits". */
762 /* Cached sum of the sizes of traceframes created by this point. */
763 uint64_t traceframe_usage
;
765 CORE_ADDR compiled_cond
;
767 /* Link to the next tracepoint in the list. */
768 struct tracepoint
*next
;
770 #ifndef IN_PROCESS_AGENT
771 /* The list of actions to take when the tracepoint triggers, in
772 string/packet form. */
775 /* The collection of strings that describe the tracepoint as it was
776 entered into GDB. These are not used by the target, but are
777 reported back to GDB upon reconnection. */
778 struct source_string
*source_strings
;
780 /* The number of bytes displaced by fast tracepoints. It may subsume
781 multiple instructions, for multi-byte fast tracepoints. This
782 field is only valid for fast tracepoints. */
785 /* Only for fast tracepoints. */
786 CORE_ADDR obj_addr_on_target
;
788 /* Address range where the original instruction under a fast
789 tracepoint was relocated to. (_end is actually one byte past
791 CORE_ADDR adjusted_insn_addr
;
792 CORE_ADDR adjusted_insn_addr_end
;
794 /* The address range of the piece of the jump pad buffer that was
795 assigned to this fast tracepoint. (_end is actually one byte
798 CORE_ADDR jump_pad_end
;
800 /* The address range of the piece of the trampoline buffer that was
801 assigned to this fast tracepoint. (_end is actually one byte
803 CORE_ADDR trampoline
;
804 CORE_ADDR trampoline_end
;
806 /* The list of actions to take while in a stepping loop. These
807 fields are only valid for patch-based tracepoints. */
808 int num_step_actions
;
809 struct tracepoint_action
**step_actions
;
810 /* Same, but in string/packet form. */
811 char **step_actions_str
;
813 /* Handle returned by the breakpoint or tracepoint module when we
814 inserted the trap or jump, or hooked into a static tracepoint.
815 NULL if we haven't inserted it yet. */
821 #ifndef IN_PROCESS_AGENT
823 /* Given `while-stepping', a thread may be collecting data for more
824 than one tracepoint simultaneously. On the other hand, the same
825 tracepoint with a while-stepping action may be hit by more than one
826 thread simultaneously (but not quite, each thread could be handling
827 a different step). Each thread holds a list of these objects,
828 representing the current step of each while-stepping action being
833 struct wstep_state
*next
;
835 /* The tracepoint number. */
837 /* The tracepoint's address. */
838 CORE_ADDR tp_address
;
840 /* The number of the current step in this 'while-stepping'
847 /* The linked list of all tracepoints. Marked explicitly as used as
848 the in-process library doesn't use it for the fast tracepoints
850 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
852 #ifndef IN_PROCESS_AGENT
854 /* Pointer to the last tracepoint in the list, new tracepoints are
855 linked in at the end. */
857 static struct tracepoint
*last_tracepoint
;
860 /* The first tracepoint to exceed its pass count. */
862 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
864 /* True if the trace buffer is full or otherwise no longer usable. */
866 IP_AGENT_EXPORT
int trace_buffer_is_full
;
868 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
870 #ifndef IN_PROCESS_AGENT
872 static const char *eval_result_names
[] =
874 "terror:in the attic", /* this should never be reported */
875 "terror:empty expression",
876 "terror:empty stack",
877 "terror:stack overflow",
878 "terror:stack underflow",
879 "terror:unhandled opcode",
880 "terror:unrecognized opcode",
881 "terror:divide by zero"
886 /* The tracepoint in which the error occurred. */
888 static struct tracepoint
*error_tracepoint
;
890 struct trace_state_variable
892 /* This is the name of the variable as used in GDB. The target
893 doesn't use the name, but needs to have it for saving and
894 reconnection purposes. */
897 /* This number identifies the variable uniquely. Numbers may be
898 assigned either by the target (in the case of builtin variables),
899 or by GDB, and are presumed unique during the course of a trace
903 /* The variable's initial value, a 64-bit signed integer always. */
904 LONGEST initial_value
;
906 /* The variable's value, a 64-bit signed integer always. */
909 /* Pointer to a getter function, used to supply computed values. */
910 LONGEST (*getter
) (void);
912 /* Link to the next variable. */
913 struct trace_state_variable
*next
;
916 /* Linked list of all trace state variables. */
918 #ifdef IN_PROCESS_AGENT
919 struct trace_state_variable
*alloced_trace_state_variables
;
922 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
924 /* The results of tracing go into a fixed-size space known as the
925 "trace buffer". Because usage follows a limited number of
926 patterns, we manage it ourselves rather than with malloc. Basic
927 rules are that we create only one trace frame at a time, each is
928 variable in size, they are never moved once created, and we only
929 discard if we are doing a circular buffer, and then only the oldest
930 ones. Each trace frame includes its own size, so we don't need to
931 link them together, and the trace frame number is relative to the
932 first one, so we don't need to record numbers. A trace frame also
933 records the number of the tracepoint that created it. The data
934 itself is a series of blocks, each introduced by a single character
935 and with a defined format. Each type of block has enough
936 type/length info to allow scanners to jump quickly from one block
937 to the next without reading each byte in the block. */
939 /* Trace buffer management would be simple - advance a free pointer
940 from beginning to end, then stop - were it not for the circular
941 buffer option, which is a useful way to prevent a trace run from
942 stopping prematurely because the buffer filled up. In the circular
943 case, the location of the first trace frame (trace_buffer_start)
944 moves as old trace frames are discarded. Also, since we grow trace
945 frames incrementally as actions are performed, we wrap around to
946 the beginning of the trace buffer. This is per-block, so each
947 block within a trace frame remains contiguous. Things get messy
948 when the wrapped-around trace frame is the one being discarded; the
949 free space ends up in two parts at opposite ends of the buffer. */
952 # if defined(__GNUC__)
953 # define ATTR_PACKED __attribute__ ((packed))
955 # define ATTR_PACKED /* nothing */
959 /* The data collected at a tracepoint hit. This object should be as
960 small as possible, since there may be a great many of them. We do
961 not need to keep a frame number, because they are all sequential
962 and there are no deletions; so the Nth frame in the buffer is
963 always frame number N. */
967 /* Number of the tracepoint that collected this traceframe. A value
968 of 0 indicates the current end of the trace buffer. We make this
969 a 16-bit field because it's never going to happen that GDB's
970 numbering of tracepoints reaches 32,000. */
973 /* The size of the data in this trace frame. We limit this to 32
974 bits, even on a 64-bit target, because it's just implausible that
975 one is validly going to collect 4 gigabytes of data at a single
977 unsigned int data_size
: 32;
979 /* The base of the trace data, which is contiguous from this point. */
980 unsigned char data
[0];
984 /* The traceframe to be used as the source of data to send back to
985 GDB. A value of -1 means to get data from the live program. */
987 int current_traceframe
= -1;
989 /* This flag is true if the trace buffer is circular, meaning that
990 when it fills, the oldest trace frames are discarded in order to
993 #ifndef IN_PROCESS_AGENT
994 static int circular_trace_buffer
;
997 /* Size of the trace buffer. */
999 static LONGEST trace_buffer_size
;
1001 /* Pointer to the block of memory that traceframes all go into. */
1003 static unsigned char *trace_buffer_lo
;
1005 /* Pointer to the end of the trace buffer, more precisely to the byte
1006 after the end of the buffer. */
1008 static unsigned char *trace_buffer_hi
;
1010 /* Control structure holding the read/write/etc. pointers into the
1011 trace buffer. We need more than one of these to implement a
1012 transaction-like mechanism to garantees that both GDBserver and the
1013 in-process agent can try to change the trace buffer
1016 struct trace_buffer_control
1018 /* Pointer to the first trace frame in the buffer. In the
1019 non-circular case, this is equal to trace_buffer_lo, otherwise it
1020 moves around in the buffer. */
1021 unsigned char *start
;
1023 /* Pointer to the free part of the trace buffer. Note that we clear
1024 several bytes at and after this pointer, so that traceframe
1025 scans/searches terminate properly. */
1026 unsigned char *free
;
1028 /* Pointer to the byte after the end of the free part. Note that
1029 this may be smaller than trace_buffer_free in the circular case,
1030 and means that the free part is in two pieces. Initially it is
1031 equal to trace_buffer_hi, then is generally equivalent to
1032 trace_buffer_start. */
1033 unsigned char *end_free
;
1035 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
1036 this is the point at which the trace data breaks, and resumes at
1038 unsigned char *wrap
;
1041 /* Same as above, to be used by GDBserver when updating the in-process
1043 struct ipa_trace_buffer_control
1052 /* We have possibly both GDBserver and an inferior thread accessing
1053 the same IPA trace buffer memory. The IPA is the producer (tries
1054 to put new frames in the buffer), while GDBserver occasionally
1055 consumes them, that is, flushes the IPA's buffer into its own
1056 buffer. Both sides need to update the trace buffer control
1057 pointers (current head, tail, etc.). We can't use a global lock to
1058 synchronize the accesses, as otherwise we could deadlock GDBserver
1059 (if the thread holding the lock stops for a signal, say). So
1060 instead of that, we use a transaction scheme where GDBserver writes
1061 always prevail over the IPAs writes, and, we have the IPA detect
1062 the commit failure/overwrite, and retry the whole attempt. This is
1063 mainly implemented by having a global token object that represents
1064 who wrote last to the buffer control structure. We need to freeze
1065 any inferior writing to the buffer while GDBserver touches memory,
1066 so that the inferior can correctly detect that GDBserver had been
1067 there, otherwise, it could mistakingly think its commit was
1068 successful; that's implemented by simply having GDBserver set a
1069 breakpoint the inferior hits if it is the critical region.
1071 There are three cycling trace buffer control structure copies
1072 (buffer head, tail, etc.), with the token object including an index
1073 indicating which is current live copy. The IPA tentatively builds
1074 an updated copy in a non-current control structure, while GDBserver
1075 always clobbers the current version directly. The IPA then tries
1076 to atomically "commit" its version; if GDBserver clobbered the
1077 structure meanwhile, that will fail, and the IPA restarts the
1080 Listing the step in further detail, we have:
1082 In-process agent (producer):
1084 - passes by `about_to_request_buffer_space' breakpoint/lock
1086 - reads current token, extracts current trace buffer control index,
1087 and starts tentatively updating the rightmost one (0->1, 1->2,
1088 2->0). Note that only one inferior thread is executing this code
1089 at any given time, due to an outer lock in the jump pads.
1091 - updates counters, and tries to commit the token.
1093 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1094 leaving the sync region.
1096 - checks if the update was effective.
1098 - if trace buffer was found full, hits flush_trace_buffer
1099 breakpoint, and restarts later afterwards.
1101 GDBserver (consumer):
1103 - sets `about_to_request_buffer_space' breakpoint/lock.
1105 - updates the token unconditionally, using the current buffer
1106 control index, since it knows that the IP agent always writes to
1107 the rightmost, and due to the breakpoint, at most one IP thread
1108 can try to update the trace buffer concurrently to GDBserver, so
1109 there will be no danger of trace buffer control index wrap making
1110 the IPA write to the same index as GDBserver.
1112 - flushes the IP agent's trace buffer completely, and updates the
1113 current trace buffer control structure. GDBserver *always* wins.
1115 - removes the `about_to_request_buffer_space' breakpoint.
1117 The token is stored in the `trace_buffer_ctrl_curr' variable.
1118 Internally, it's bits are defined as:
1120 |-------------+-----+-------------+--------+-------------+--------------|
1121 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1122 |-------------+-----+-------------+--------+-------------+--------------|
1123 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1124 |-------------+-----+-------------+--------+-------------+--------------|
1126 GSB - GDBserver Stamp Bit
1127 PC - Previous Counter
1128 CC - Current Counter
1129 TBCI - Trace Buffer Control Index
1132 An IPA update of `trace_buffer_ctrl_curr' does:
1134 - read CC from the current token, save as PC.
1136 - atomically tries to write PC+1,CC
1138 A GDBserver update of `trace_buffer_ctrl_curr' does:
1140 - reads PC and CC from the current token.
1145 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1146 for the counters described below. The cleared bits are used to
1147 hold the index of the items of the `trace_buffer_ctrl' array that
1149 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1151 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1152 counter, and the `current' counter. */
1154 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1155 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1157 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1158 always stamps this bit as set. */
1159 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1161 #ifdef IN_PROCESS_AGENT
1162 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1163 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1165 # define TRACE_BUFFER_CTRL_CURR \
1166 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1170 /* The GDBserver side agent only needs one instance of this object, as
1171 it doesn't need to sync with itself. Define it as array anyway so
1172 that the rest of the code base doesn't need to care for the
1174 struct trace_buffer_control trace_buffer_ctrl
[1];
1175 # define TRACE_BUFFER_CTRL_CURR 0
1178 /* These are convenience macros used to access the current trace
1179 buffer control in effect. */
1180 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1181 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1182 #define trace_buffer_end_free \
1183 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1184 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1187 /* Macro that returns a pointer to the first traceframe in the buffer. */
1189 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1191 /* Macro that returns a pointer to the next traceframe in the buffer.
1192 If the computed location is beyond the wraparound point, subtract
1193 the offset of the wraparound. */
1195 #define NEXT_TRACEFRAME_1(TF) \
1196 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1198 #define NEXT_TRACEFRAME(TF) \
1199 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1200 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1201 ? (trace_buffer_wrap - trace_buffer_lo) \
1204 /* The difference between these counters represents the total number
1205 of complete traceframes present in the trace buffer. The IP agent
1206 writes to the write count, GDBserver writes to read count. */
1208 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1209 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1211 /* Convenience macro. */
1213 #define traceframe_count \
1214 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1216 /* The count of all traceframes created in the current run, including
1217 ones that were discarded to make room. */
1219 IP_AGENT_EXPORT
int traceframes_created
;
1221 #ifndef IN_PROCESS_AGENT
1223 /* Read-only regions are address ranges whose contents don't change,
1224 and so can be read from target memory even while looking at a trace
1225 frame. Without these, disassembly for instance will likely fail,
1226 because the program code is not usually collected into a trace
1227 frame. This data structure does not need to be very complicated or
1228 particularly efficient, it's only going to be used occasionally,
1229 and only by some commands. */
1231 struct readonly_region
1233 /* The bounds of the region. */
1234 CORE_ADDR start
, end
;
1236 /* Link to the next one. */
1237 struct readonly_region
*next
;
1240 /* Linked list of readonly regions. This list stays in effect from
1241 one tstart to the next. */
1243 static struct readonly_region
*readonly_regions
;
1247 /* The global that controls tracing overall. */
1249 IP_AGENT_EXPORT
int tracing
;
1251 #ifndef IN_PROCESS_AGENT
1253 /* Controls whether tracing should continue after GDB disconnects. */
1255 int disconnected_tracing
;
1257 /* The reason for the last tracing run to have stopped. We initialize
1258 to a distinct string so that GDB can distinguish between "stopped
1259 after running" and "stopped because never run" cases. */
1261 static const char *tracing_stop_reason
= "tnotrun";
1263 static int tracing_stop_tpnum
;
1265 /* 64-bit timestamps for the trace run's start and finish, expressed
1266 in microseconds from the Unix epoch. */
1268 LONGEST tracing_start_time
;
1269 LONGEST tracing_stop_time
;
1271 /* The (optional) user-supplied name of the user that started the run.
1272 This is an arbitrary string, and may be NULL. */
1274 char *tracing_user_name
;
1276 /* Optional user-supplied text describing the run. This is
1277 an arbitrary string, and may be NULL. */
1279 char *tracing_notes
;
1281 /* Optional user-supplied text explaining a tstop command. This is an
1282 arbitrary string, and may be NULL. */
1284 char *tracing_stop_note
;
1288 /* Functions local to this file. */
1290 /* Base "class" for tracepoint type specific data to be passed down to
1291 collect_data_at_tracepoint. */
1292 struct tracepoint_hit_ctx
1294 enum tracepoint_type type
;
1297 #ifdef IN_PROCESS_AGENT
1299 /* Fast/jump tracepoint specific data to be passed down to
1300 collect_data_at_tracepoint. */
1301 struct fast_tracepoint_ctx
1303 struct tracepoint_hit_ctx base
;
1305 struct regcache regcache
;
1306 int regcache_initted
;
1307 unsigned char *regspace
;
1309 unsigned char *regs
;
1310 struct tracepoint
*tpoint
;
1313 /* Static tracepoint specific data to be passed down to
1314 collect_data_at_tracepoint. */
1315 struct static_tracepoint_ctx
1317 struct tracepoint_hit_ctx base
;
1319 /* The regcache corresponding to the registers state at the time of
1320 the tracepoint hit. Initialized lazily, from REGS. */
1321 struct regcache regcache
;
1322 int regcache_initted
;
1324 /* The buffer space REGCACHE above uses. We use a separate buffer
1325 instead of letting the regcache malloc for both signal safety and
1326 performance reasons; this is allocated on the stack instead. */
1327 unsigned char *regspace
;
1329 /* The register buffer as passed on by lttng/ust. */
1330 struct registers
*regs
;
1332 /* The "printf" formatter and the args the user passed to the marker
1333 call. We use this to be able to collect "static trace data"
1338 /* The GDB tracepoint matching the probed marker that was "hit". */
1339 struct tracepoint
*tpoint
;
1344 /* Static tracepoint specific data to be passed down to
1345 collect_data_at_tracepoint. */
1346 struct trap_tracepoint_ctx
1348 struct tracepoint_hit_ctx base
;
1350 struct regcache
*regcache
;
1355 #ifndef IN_PROCESS_AGENT
1356 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1357 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1360 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1361 struct tracepoint
*tpoint
);
1363 #ifndef IN_PROCESS_AGENT
1364 static void clear_readonly_regions (void);
1365 static void clear_installed_tracepoints (void);
1368 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1370 struct tracepoint
*tpoint
);
1371 #ifndef IN_PROCESS_AGENT
1372 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1374 struct tracepoint
*tpoint
, int current_step
);
1375 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1376 CORE_ADDR
*jump_entry
);
1378 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1380 struct tracepoint
*tpoint
,
1381 struct traceframe
*tframe
,
1382 struct tracepoint_action
*taction
);
1384 #ifndef IN_PROCESS_AGENT
1385 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1387 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1388 static void download_tracepoint (struct tracepoint
*);
1389 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1390 static void clone_fast_tracepoint (struct tracepoint
*to
,
1391 const struct tracepoint
*from
);
1394 static LONGEST
get_timestamp (void);
1396 #if defined(__GNUC__)
1397 # define memory_barrier() asm volatile ("" : : : "memory")
1399 # define memory_barrier() do {} while (0)
1402 /* We only build the IPA if this builtin is supported, and there are
1403 no uses of this in GDBserver itself, so we're safe in defining this
1405 #define cmpxchg(mem, oldval, newval) \
1406 __sync_val_compare_and_swap (mem, oldval, newval)
1408 /* Record that an error occurred during expression evaluation. */
1411 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1412 enum eval_result_type rtype
)
1414 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1415 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1417 #ifdef IN_PROCESS_AGENT
1418 /* Only record the first error we get. */
1419 if (cmpxchg (&expr_eval_result
,
1421 rtype
) != expr_eval_no_error
)
1424 if (expr_eval_result
!= expr_eval_no_error
)
1428 error_tracepoint
= tpoint
;
1431 /* Trace buffer management. */
1434 clear_trace_buffer (void)
1436 trace_buffer_start
= trace_buffer_lo
;
1437 trace_buffer_free
= trace_buffer_lo
;
1438 trace_buffer_end_free
= trace_buffer_hi
;
1439 trace_buffer_wrap
= trace_buffer_hi
;
1440 /* A traceframe with zeroed fields marks the end of trace data. */
1441 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1442 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1443 traceframe_read_count
= traceframe_write_count
= 0;
1444 traceframes_created
= 0;
1447 #ifndef IN_PROCESS_AGENT
1450 clear_inferior_trace_buffer (void)
1452 CORE_ADDR ipa_trace_buffer_lo
;
1453 CORE_ADDR ipa_trace_buffer_hi
;
1454 struct traceframe ipa_traceframe
= { 0 };
1455 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1457 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1458 &ipa_trace_buffer_lo
);
1459 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1460 &ipa_trace_buffer_hi
);
1462 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1463 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1464 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1465 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1467 /* A traceframe with zeroed fields marks the end of trace data. */
1468 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1469 (unsigned char *) &ipa_trace_buffer_ctrl
,
1470 sizeof (ipa_trace_buffer_ctrl
));
1472 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1474 /* A traceframe with zeroed fields marks the end of trace data. */
1475 write_inferior_memory (ipa_trace_buffer_lo
,
1476 (unsigned char *) &ipa_traceframe
,
1477 sizeof (ipa_traceframe
));
1479 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1480 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1481 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1487 init_trace_buffer (LONGEST bufsize
)
1489 trace_buffer_size
= bufsize
;
1491 /* If we already have a trace buffer, try realloc'ing. */
1492 trace_buffer_lo
= xrealloc (trace_buffer_lo
, bufsize
);
1494 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1496 clear_trace_buffer ();
1499 #ifdef IN_PROCESS_AGENT
1501 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1502 about_to_request_buffer_space (void)
1504 /* GDBserver places breakpoint here while it goes about to flush
1505 data at random times. */
1506 UNKNOWN_SIDE_EFFECTS();
1511 /* Carve out a piece of the trace buffer, returning NULL in case of
1515 trace_buffer_alloc (size_t amt
)
1517 unsigned char *rslt
;
1518 struct trace_buffer_control
*tbctrl
;
1520 #ifdef IN_PROCESS_AGENT
1521 unsigned int prev
, prev_filtered
;
1522 unsigned int commit_count
;
1523 unsigned int commit
;
1524 unsigned int readout
;
1526 struct traceframe
*oldest
;
1527 unsigned char *new_start
;
1530 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1531 (long) amt
, (long) sizeof (struct traceframe
));
1533 /* Account for the EOB marker. */
1534 amt
+= sizeof (struct traceframe
);
1536 #ifdef IN_PROCESS_AGENT
1540 /* Read the current token and extract the index to try to write to,
1541 storing it in CURR. */
1542 prev
= trace_buffer_ctrl_curr
;
1543 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1544 curr
= prev_filtered
+ 1;
1548 about_to_request_buffer_space ();
1550 /* Start out with a copy of the current state. GDBserver may be
1551 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1552 be able to commit anyway if that happens. */
1553 trace_buffer_ctrl
[curr
]
1554 = trace_buffer_ctrl
[prev_filtered
];
1555 trace_debug ("trying curr=%u", curr
);
1557 /* The GDBserver's agent doesn't need all that syncing, and always
1558 updates TCB 0 (there's only one, mind you). */
1561 tbctrl
= &trace_buffer_ctrl
[curr
];
1563 /* Offsets are easier to grok for debugging than raw addresses,
1564 especially for the small trace buffer sizes that are useful for
1566 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1568 (int) (tbctrl
->start
- trace_buffer_lo
),
1569 (int) (tbctrl
->free
- trace_buffer_lo
),
1570 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1571 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1572 (int) (trace_buffer_hi
- trace_buffer_lo
));
1574 /* The algorithm here is to keep trying to get a contiguous block of
1575 the requested size, possibly discarding older traceframes to free
1576 up space. Since free space might come in one or two pieces,
1577 depending on whether discarded traceframes wrapped around at the
1578 high end of the buffer, we test both pieces after each
1582 /* First, if we have two free parts, try the upper one first. */
1583 if (tbctrl
->end_free
< tbctrl
->free
)
1585 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1586 /* We have enough in the upper part. */
1590 /* Our high part of free space wasn't enough. Give up
1591 on it for now, set wraparound. We will recover the
1592 space later, if/when the wrapped-around traceframe is
1594 trace_debug ("Upper part too small, setting wraparound");
1595 tbctrl
->wrap
= tbctrl
->free
;
1596 tbctrl
->free
= trace_buffer_lo
;
1600 /* The normal case. */
1601 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1604 #ifdef IN_PROCESS_AGENT
1605 /* The IP Agent's buffer is always circular. It isn't used
1606 currently, but `circular_trace_buffer' could represent
1607 GDBserver's mode. If we didn't find space, ask GDBserver to
1610 flush_trace_buffer ();
1614 trace_debug ("gdbserver flushed buffer, retrying");
1618 /* GDBserver cancelled the tracing. Bail out as well. */
1621 /* If we're here, then neither part is big enough, and
1622 non-circular trace buffers are now full. */
1623 if (!circular_trace_buffer
)
1625 trace_debug ("Not enough space in the trace buffer");
1629 trace_debug ("Need more space in the trace buffer");
1631 /* If we have a circular buffer, we can try discarding the
1632 oldest traceframe and see if that helps. */
1633 oldest
= FIRST_TRACEFRAME ();
1634 if (oldest
->tpnum
== 0)
1636 /* Not good; we have no traceframes to free. Perhaps we're
1637 asking for a block that is larger than the buffer? In
1638 any case, give up. */
1639 trace_debug ("No traceframes to discard");
1643 /* We don't run this code in the in-process agent currently.
1644 E.g., we could leave the in-process agent in autonomous
1645 circular mode if we only have fast tracepoints. If we do
1646 that, then this bit becomes racy with GDBserver, which also
1647 writes to this counter. */
1648 --traceframe_write_count
;
1650 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1651 /* If we freed the traceframe that wrapped around, go back
1652 to the non-wrap case. */
1653 if (new_start
< tbctrl
->start
)
1655 trace_debug ("Discarding past the wraparound");
1656 tbctrl
->wrap
= trace_buffer_hi
;
1658 tbctrl
->start
= new_start
;
1659 tbctrl
->end_free
= tbctrl
->start
;
1661 trace_debug ("Discarded a traceframe\n"
1662 "Trace buffer [%d], start=%d free=%d "
1663 "endfree=%d wrap=%d hi=%d",
1665 (int) (tbctrl
->start
- trace_buffer_lo
),
1666 (int) (tbctrl
->free
- trace_buffer_lo
),
1667 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1668 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1669 (int) (trace_buffer_hi
- trace_buffer_lo
));
1671 /* Now go back around the loop. The discard might have resulted
1672 in either one or two pieces of free space, so we want to try
1673 both before freeing any more traceframes. */
1677 /* If we get here, we know we can provide the asked-for space. */
1679 rslt
= tbctrl
->free
;
1681 /* Adjust the request back down, now that we know we have space for
1682 the marker, but don't commit to AMT yet, we may still need to
1683 restart the operation if GDBserver touches the trace buffer
1684 (obviously only important in the in-process agent's version). */
1685 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1687 /* Or not. If GDBserver changed the trace buffer behind our back,
1688 we get to restart a new allocation attempt. */
1690 #ifdef IN_PROCESS_AGENT
1691 /* Build the tentative token. */
1692 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1693 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1694 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1698 /* Try to commit it. */
1699 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1700 if (readout
!= prev
)
1702 trace_debug ("GDBserver has touched the trace buffer, restarting."
1703 " (prev=%08x, commit=%08x, readout=%08x)",
1704 prev
, commit
, readout
);
1708 /* Hold your horses here. Even if that change was committed,
1709 GDBserver could come in, and clobber it. We need to hold to be
1710 able to tell if GDBserver clobbers before or after we committed
1711 the change. Whenever GDBserver goes about touching the IPA
1712 buffer, it sets a breakpoint in this routine, so we have a sync
1714 about_to_request_buffer_space ();
1716 /* Check if the change has been effective, even if GDBserver stopped
1717 us at the breakpoint. */
1720 unsigned int refetch
;
1724 refetch
= trace_buffer_ctrl_curr
;
1726 if (refetch
== commit
1727 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1730 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1731 "readout=%08x, refetch=%08x)",
1732 prev
, commit
, readout
, refetch
);
1736 trace_debug ("GDBserver has touched the trace buffer, not effective."
1737 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1738 prev
, commit
, readout
, refetch
);
1744 /* We have a new piece of the trace buffer. Hurray! */
1746 /* Add an EOB marker just past this allocation. */
1747 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1748 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1750 /* Adjust the request back down, now that we know we have space for
1752 amt
-= sizeof (struct traceframe
);
1756 trace_debug ("Allocated %d bytes", (int) amt
);
1757 trace_debug ("Trace buffer [%d] start=%d free=%d "
1758 "endfree=%d wrap=%d hi=%d",
1760 (int) (tbctrl
->start
- trace_buffer_lo
),
1761 (int) (tbctrl
->free
- trace_buffer_lo
),
1762 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1763 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1764 (int) (trace_buffer_hi
- trace_buffer_lo
));
1770 #ifndef IN_PROCESS_AGENT
1772 /* Return the total free space. This is not necessarily the largest
1773 block we can allocate, because of the two-part case. */
1778 if (trace_buffer_free
<= trace_buffer_end_free
)
1779 return trace_buffer_end_free
- trace_buffer_free
;
1781 return ((trace_buffer_end_free
- trace_buffer_lo
)
1782 + (trace_buffer_hi
- trace_buffer_free
));
1785 /* An 'S' in continuation packets indicates remainder are for
1788 static int seen_step_action_flag
;
1790 /* Create a tracepoint (location) with given number and address. Add this
1791 new tracepoint to list and sort this list. */
1793 static struct tracepoint
*
1794 add_tracepoint (int num
, CORE_ADDR addr
)
1796 struct tracepoint
*tpoint
, **tp_next
;
1798 tpoint
= xmalloc (sizeof (struct tracepoint
));
1799 tpoint
->number
= num
;
1800 tpoint
->address
= addr
;
1801 tpoint
->numactions
= 0;
1802 tpoint
->actions
= NULL
;
1803 tpoint
->actions_str
= NULL
;
1804 tpoint
->cond
= NULL
;
1805 tpoint
->num_step_actions
= 0;
1806 tpoint
->step_actions
= NULL
;
1807 tpoint
->step_actions_str
= NULL
;
1808 /* Start all off as regular (slow) tracepoints. */
1809 tpoint
->type
= trap_tracepoint
;
1810 tpoint
->orig_size
= -1;
1811 tpoint
->source_strings
= NULL
;
1812 tpoint
->compiled_cond
= 0;
1813 tpoint
->handle
= NULL
;
1814 tpoint
->next
= NULL
;
1816 /* Find a place to insert this tracepoint into list in order to keep
1817 the tracepoint list still in the ascending order. There may be
1818 multiple tracepoints at the same address as TPOINT's, and this
1819 guarantees TPOINT is inserted after all the tracepoints which are
1820 set at the same address. For example, fast tracepoints A, B, C are
1821 set at the same address, and D is to be insert at the same place as
1824 -->| A |--> | B |-->| C |->...
1826 One jump pad was created for tracepoint A, B, and C, and the target
1827 address of A is referenced/used in jump pad. So jump pad will let
1828 inferior jump to A. If D is inserted in front of A, like this,
1830 -->| D |-->| A |--> | B |-->| C |->...
1832 without updating jump pad, D is not reachable during collect, which
1833 is wrong. As we can see, the order of B, C and D doesn't matter, but
1834 A should always be the `first' one. */
1835 for (tp_next
= &tracepoints
;
1836 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1837 tp_next
= &(*tp_next
)->next
)
1839 tpoint
->next
= *tp_next
;
1841 last_tracepoint
= tpoint
;
1843 seen_step_action_flag
= 0;
1848 #ifndef IN_PROCESS_AGENT
1850 /* Return the tracepoint with the given number and address, or NULL. */
1852 static struct tracepoint
*
1853 find_tracepoint (int id
, CORE_ADDR addr
)
1855 struct tracepoint
*tpoint
;
1857 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1858 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1864 /* Remove TPOINT from global list. */
1867 remove_tracepoint (struct tracepoint
*tpoint
)
1869 struct tracepoint
*tp
, *tp_prev
;
1871 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1872 tp_prev
= tp
, tp
= tp
->next
)
1878 tp_prev
->next
= tp
->next
;
1880 tracepoints
= tp
->next
;
1886 /* There may be several tracepoints with the same number (because they
1887 are "locations", in GDB parlance); return the next one after the
1888 given tracepoint, or search from the beginning of the list if the
1889 first argument is NULL. */
1891 static struct tracepoint
*
1892 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1894 struct tracepoint
*tpoint
;
1897 tpoint
= prev_tp
->next
;
1899 tpoint
= tracepoints
;
1900 for (; tpoint
; tpoint
= tpoint
->next
)
1901 if (tpoint
->number
== num
)
1909 /* Append another action to perform when the tracepoint triggers. */
1912 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1918 seen_step_action_flag
= 1;
1926 char *act_start
= act
;
1927 struct tracepoint_action
*action
= NULL
;
1933 struct collect_memory_action
*maction
;
1937 maction
= xmalloc (sizeof *maction
);
1938 maction
->base
.type
= *act
;
1939 maction
->base
.ops
= &m_tracepoint_action_ops
;
1940 action
= &maction
->base
;
1943 is_neg
= (*act
== '-');
1946 act
= unpack_varlen_hex (act
, &basereg
);
1948 act
= unpack_varlen_hex (act
, &maction
->addr
);
1950 act
= unpack_varlen_hex (act
, &maction
->len
);
1951 maction
->basereg
= (is_neg
1954 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1955 pulongest (maction
->len
),
1956 paddress (maction
->addr
), maction
->basereg
);
1961 struct collect_registers_action
*raction
;
1963 raction
= xmalloc (sizeof *raction
);
1964 raction
->base
.type
= *act
;
1965 raction
->base
.ops
= &r_tracepoint_action_ops
;
1966 action
= &raction
->base
;
1968 trace_debug ("Want to collect registers");
1970 /* skip past hex digits of mask for now */
1971 while (isxdigit(*act
))
1977 struct collect_static_trace_data_action
*raction
;
1979 raction
= xmalloc (sizeof *raction
);
1980 raction
->base
.type
= *act
;
1981 raction
->base
.ops
= &l_tracepoint_action_ops
;
1982 action
= &raction
->base
;
1984 trace_debug ("Want to collect static trace data");
1989 trace_debug ("Unexpected step action, ignoring");
1994 struct eval_expr_action
*xaction
;
1996 xaction
= xmalloc (sizeof (*xaction
));
1997 xaction
->base
.type
= *act
;
1998 xaction
->base
.ops
= &x_tracepoint_action_ops
;
1999 action
= &xaction
->base
;
2001 trace_debug ("Want to evaluate expression");
2002 xaction
->expr
= gdb_parse_agent_expr (&act
);
2006 trace_debug ("unknown trace action '%c', ignoring...", *act
);
2015 if (seen_step_action_flag
)
2017 tpoint
->num_step_actions
++;
2019 tpoint
->step_actions
2020 = xrealloc (tpoint
->step_actions
,
2021 (sizeof (*tpoint
->step_actions
)
2022 * tpoint
->num_step_actions
));
2023 tpoint
->step_actions_str
2024 = xrealloc (tpoint
->step_actions_str
,
2025 (sizeof (*tpoint
->step_actions_str
)
2026 * tpoint
->num_step_actions
));
2027 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
2028 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
2029 = savestring (act_start
, act
- act_start
);
2033 tpoint
->numactions
++;
2035 = xrealloc (tpoint
->actions
,
2036 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
2038 = xrealloc (tpoint
->actions_str
,
2039 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
2040 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
2041 tpoint
->actions_str
[tpoint
->numactions
- 1]
2042 = savestring (act_start
, act
- act_start
);
2049 /* Find or create a trace state variable with the given number. */
2051 static struct trace_state_variable
*
2052 get_trace_state_variable (int num
)
2054 struct trace_state_variable
*tsv
;
2056 #ifdef IN_PROCESS_AGENT
2057 /* Search for an existing variable. */
2058 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
2059 if (tsv
->number
== num
)
2063 /* Search for an existing variable. */
2064 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
2065 if (tsv
->number
== num
)
2071 /* Find or create a trace state variable with the given number. */
2073 static struct trace_state_variable
*
2074 create_trace_state_variable (int num
, int gdb
)
2076 struct trace_state_variable
*tsv
;
2078 tsv
= get_trace_state_variable (num
);
2082 /* Create a new variable. */
2083 tsv
= xmalloc (sizeof (struct trace_state_variable
));
2085 tsv
->initial_value
= 0;
2089 #ifdef IN_PROCESS_AGENT
2092 tsv
->next
= alloced_trace_state_variables
;
2093 alloced_trace_state_variables
= tsv
;
2098 tsv
->next
= trace_state_variables
;
2099 trace_state_variables
= tsv
;
2104 IP_AGENT_EXPORT LONGEST
2105 get_trace_state_variable_value (int num
)
2107 struct trace_state_variable
*tsv
;
2109 tsv
= get_trace_state_variable (num
);
2113 trace_debug ("No trace state variable %d, skipping value get", num
);
2117 /* Call a getter function if we have one. While it's tempting to
2118 set up something to only call the getter once per tracepoint hit,
2119 it could run afoul of thread races. Better to let the getter
2120 handle it directly, if necessary to worry about it. */
2122 tsv
->value
= (tsv
->getter
) ();
2124 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2125 num
, plongest (tsv
->value
));
2130 IP_AGENT_EXPORT
void
2131 set_trace_state_variable_value (int num
, LONGEST val
)
2133 struct trace_state_variable
*tsv
;
2135 tsv
= get_trace_state_variable (num
);
2139 trace_debug ("No trace state variable %d, skipping value set", num
);
2147 agent_get_trace_state_variable_value (int num
)
2149 return get_trace_state_variable_value (num
);
2153 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2155 set_trace_state_variable_value (num
, val
);
2159 set_trace_state_variable_name (int num
, const char *name
)
2161 struct trace_state_variable
*tsv
;
2163 tsv
= get_trace_state_variable (num
);
2167 trace_debug ("No trace state variable %d, skipping name set", num
);
2171 tsv
->name
= (char *) name
;
2175 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2177 struct trace_state_variable
*tsv
;
2179 tsv
= get_trace_state_variable (num
);
2183 trace_debug ("No trace state variable %d, skipping getter set", num
);
2187 tsv
->getter
= getter
;
2190 /* Add a raw traceframe for the given tracepoint. */
2192 static struct traceframe
*
2193 add_traceframe (struct tracepoint
*tpoint
)
2195 struct traceframe
*tframe
;
2197 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2202 tframe
->tpnum
= tpoint
->number
;
2203 tframe
->data_size
= 0;
2208 /* Add a block to the traceframe currently being worked on. */
2210 static unsigned char *
2211 add_traceframe_block (struct traceframe
*tframe
,
2212 struct tracepoint
*tpoint
, int amt
)
2214 unsigned char *block
;
2219 block
= trace_buffer_alloc (amt
);
2224 gdb_assert (tframe
->tpnum
== tpoint
->number
);
2226 tframe
->data_size
+= amt
;
2227 tpoint
->traceframe_usage
+= amt
;
2232 /* Flag that the current traceframe is finished. */
2235 finish_traceframe (struct traceframe
*tframe
)
2237 ++traceframe_write_count
;
2238 ++traceframes_created
;
2241 #ifndef IN_PROCESS_AGENT
2243 /* Given a traceframe number NUM, find the NUMth traceframe in the
2246 static struct traceframe
*
2247 find_traceframe (int num
)
2249 struct traceframe
*tframe
;
2252 for (tframe
= FIRST_TRACEFRAME ();
2254 tframe
= NEXT_TRACEFRAME (tframe
))
2265 get_traceframe_address (struct traceframe
*tframe
)
2268 struct tracepoint
*tpoint
;
2270 addr
= traceframe_get_pc (tframe
);
2275 /* Fallback strategy, will be incorrect for while-stepping frames
2276 and multi-location tracepoints. */
2277 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2278 return tpoint
->address
;
2281 /* Search for the next traceframe whose address is inside or outside
2284 static struct traceframe
*
2285 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2288 struct traceframe
*tframe
;
2291 *tfnump
= current_traceframe
+ 1;
2292 tframe
= find_traceframe (*tfnump
);
2293 /* The search is not supposed to wrap around. */
2300 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2302 tfaddr
= get_traceframe_address (tframe
);
2304 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2305 : (lo
> tfaddr
|| tfaddr
> hi
))
2314 /* Search for the next traceframe recorded by the given tracepoint.
2315 Note that for multi-location tracepoints, this will find whatever
2316 location appears first. */
2318 static struct traceframe
*
2319 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2321 struct traceframe
*tframe
;
2323 *tfnump
= current_traceframe
+ 1;
2324 tframe
= find_traceframe (*tfnump
);
2325 /* The search is not supposed to wrap around. */
2332 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2334 if (tframe
->tpnum
== num
)
2345 #ifndef IN_PROCESS_AGENT
2347 /* Clear all past trace state. */
2350 cmd_qtinit (char *packet
)
2352 struct trace_state_variable
*tsv
, *prev
, *next
;
2354 /* Make sure we don't try to read from a trace frame. */
2355 current_traceframe
= -1;
2359 trace_debug ("Initializing the trace");
2361 clear_installed_tracepoints ();
2362 clear_readonly_regions ();
2365 last_tracepoint
= NULL
;
2367 /* Clear out any leftover trace state variables. Ones with target
2368 defined getters should be kept however. */
2370 tsv
= trace_state_variables
;
2373 trace_debug ("Looking at var %d", tsv
->number
);
2374 if (tsv
->getter
== NULL
)
2380 trace_state_variables
= next
;
2381 trace_debug ("Deleting var %d", tsv
->number
);
2392 clear_trace_buffer ();
2393 clear_inferior_trace_buffer ();
2398 /* Unprobe the UST marker at ADDRESS. */
2401 unprobe_marker_at (CORE_ADDR address
)
2403 char cmd
[IPA_CMD_BUF_SIZE
];
2405 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2406 run_inferior_command (cmd
, strlen (cmd
) + 1);
2409 /* Restore the program to its pre-tracing state. This routine may be called
2410 in error situations, so it needs to be careful about only restoring
2411 from known-valid bits. */
2414 clear_installed_tracepoints (void)
2416 struct tracepoint
*tpoint
;
2417 struct tracepoint
*prev_stpoint
;
2420 cancel_breakpoints ();
2422 prev_stpoint
= NULL
;
2424 /* Restore any bytes overwritten by tracepoints. */
2425 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2427 /* Catch the case where we might try to remove a tracepoint that
2428 was never actually installed. */
2429 if (tpoint
->handle
== NULL
)
2431 trace_debug ("Tracepoint %d at 0x%s was "
2432 "never installed, nothing to clear",
2433 tpoint
->number
, paddress (tpoint
->address
));
2437 switch (tpoint
->type
)
2439 case trap_tracepoint
:
2440 delete_breakpoint (tpoint
->handle
);
2442 case fast_tracepoint
:
2443 delete_fast_tracepoint_jump (tpoint
->handle
);
2445 case static_tracepoint
:
2446 if (prev_stpoint
!= NULL
2447 && prev_stpoint
->address
== tpoint
->address
)
2448 /* Nothing to do. We already unprobed a tracepoint set at
2449 this marker address (and there can only be one probe
2454 unprobe_marker_at (tpoint
->address
);
2455 prev_stpoint
= tpoint
;
2460 tpoint
->handle
= NULL
;
2466 /* Parse a packet that defines a tracepoint. */
2469 cmd_qtdp (char *own_buf
)
2472 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2473 int trail_hyphen
= 0;
2477 struct tracepoint
*tpoint
;
2479 char *packet
= own_buf
;
2481 packet
+= strlen ("QTDP:");
2483 /* A hyphen at the beginning marks a packet specifying actions for a
2484 tracepoint already supplied. */
2491 packet
= unpack_varlen_hex (packet
, &num
);
2492 ++packet
; /* skip a colon */
2493 packet
= unpack_varlen_hex (packet
, &addr
);
2494 ++packet
; /* skip a colon */
2496 /* See if we already have this tracepoint. */
2497 tpoint
= find_tracepoint (num
, addr
);
2501 /* Duplicate tracepoints are never allowed. */
2504 trace_debug ("Tracepoint error: tracepoint %d"
2505 " at 0x%s already exists",
2506 (int) num
, paddress (addr
));
2507 write_enn (own_buf
);
2511 tpoint
= add_tracepoint (num
, addr
);
2513 tpoint
->enabled
= (*packet
== 'E');
2514 ++packet
; /* skip 'E' */
2515 ++packet
; /* skip a colon */
2516 packet
= unpack_varlen_hex (packet
, &count
);
2517 tpoint
->step_count
= count
;
2518 ++packet
; /* skip a colon */
2519 packet
= unpack_varlen_hex (packet
, &count
);
2520 tpoint
->pass_count
= count
;
2521 /* See if we have any of the additional optional fields. */
2522 while (*packet
== ':')
2527 tpoint
->type
= fast_tracepoint
;
2529 packet
= unpack_varlen_hex (packet
, &count
);
2530 tpoint
->orig_size
= count
;
2532 else if (*packet
== 'S')
2534 tpoint
->type
= static_tracepoint
;
2537 else if (*packet
== 'X')
2539 actparm
= (char *) packet
;
2540 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2543 else if (*packet
== '-')
2545 else if (*packet
== '\0')
2548 trace_debug ("Unknown optional tracepoint field");
2553 trace_debug ("Also has actions\n");
2556 trace_debug ("Defined %stracepoint %d at 0x%s, "
2557 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2558 tpoint
->type
== fast_tracepoint
? "fast "
2559 : tpoint
->type
== static_tracepoint
? "static " : "",
2560 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2561 tpoint
->step_count
, tpoint
->pass_count
);
2564 add_tracepoint_action (tpoint
, packet
);
2567 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2568 (int) num
, paddress (addr
));
2569 write_enn (own_buf
);
2573 /* Install tracepoint during tracing only once for each tracepoint location.
2574 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2575 determine the last QTDP packet for one tracepoint location by checking
2576 trailing hyphen in QTDP packet. */
2577 if (tracing
&& !trail_hyphen
)
2579 struct tracepoint
*tp
= NULL
;
2581 /* Pause all threads temporarily while we patch tracepoints. */
2584 /* download_tracepoint will update global `tracepoints'
2585 list, so it is unsafe to leave threads in jump pad. */
2586 stabilize_threads ();
2588 /* Freeze threads. */
2592 if (tpoint
->type
!= trap_tracepoint
)
2594 /* Find another fast or static tracepoint at the same address. */
2595 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2597 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2598 && tp
->number
!= tpoint
->number
)
2602 /* TPOINT is installed at the same address as TP. */
2605 if (tpoint
->type
== fast_tracepoint
)
2606 clone_fast_tracepoint (tpoint
, tp
);
2607 else if (tpoint
->type
== static_tracepoint
)
2608 tpoint
->handle
= (void *) -1;
2612 if (use_agent
&& tpoint
->type
== fast_tracepoint
2613 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
2615 /* Download and install fast tracepoint by agent. */
2616 if (tracepoint_send_agent (tpoint
) == 0)
2620 write_enn (own_buf
);
2621 remove_tracepoint (tpoint
);
2626 download_tracepoint (tpoint
);
2628 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2630 install_tracepoint (tpoint
, own_buf
);
2631 if (strcmp (own_buf
, "OK") != 0)
2632 remove_tracepoint (tpoint
);
2646 cmd_qtdpsrc (char *own_buf
)
2648 ULONGEST num
, addr
, start
, slen
;
2649 struct tracepoint
*tpoint
;
2650 char *packet
= own_buf
;
2651 char *saved
, *srctype
, *src
;
2653 struct source_string
*last
, *newlast
;
2655 packet
+= strlen ("QTDPsrc:");
2657 packet
= unpack_varlen_hex (packet
, &num
);
2658 ++packet
; /* skip a colon */
2659 packet
= unpack_varlen_hex (packet
, &addr
);
2660 ++packet
; /* skip a colon */
2662 /* See if we already have this tracepoint. */
2663 tpoint
= find_tracepoint (num
, addr
);
2667 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2668 (int) num
, paddress (addr
));
2669 write_enn (own_buf
);
2674 packet
= strchr (packet
, ':');
2675 srctype
= xmalloc (packet
- saved
+ 1);
2676 memcpy (srctype
, saved
, packet
- saved
);
2677 srctype
[packet
- saved
] = '\0';
2679 packet
= unpack_varlen_hex (packet
, &start
);
2680 ++packet
; /* skip a colon */
2681 packet
= unpack_varlen_hex (packet
, &slen
);
2682 ++packet
; /* skip a colon */
2683 src
= xmalloc (slen
+ 1);
2684 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2687 newlast
= xmalloc (sizeof (struct source_string
));
2688 newlast
->type
= srctype
;
2690 newlast
->next
= NULL
;
2691 /* Always add a source string to the end of the list;
2692 this keeps sequences of actions/commands in the right
2694 if (tpoint
->source_strings
)
2696 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2698 last
->next
= newlast
;
2701 tpoint
->source_strings
= newlast
;
2707 cmd_qtdv (char *own_buf
)
2709 ULONGEST num
, val
, builtin
;
2712 struct trace_state_variable
*tsv
;
2713 char *packet
= own_buf
;
2715 packet
+= strlen ("QTDV:");
2717 packet
= unpack_varlen_hex (packet
, &num
);
2718 ++packet
; /* skip a colon */
2719 packet
= unpack_varlen_hex (packet
, &val
);
2720 ++packet
; /* skip a colon */
2721 packet
= unpack_varlen_hex (packet
, &builtin
);
2722 ++packet
; /* skip a colon */
2724 nbytes
= strlen (packet
) / 2;
2725 varname
= xmalloc (nbytes
+ 1);
2726 nbytes
= unhexify (varname
, packet
, nbytes
);
2727 varname
[nbytes
] = '\0';
2729 tsv
= create_trace_state_variable (num
, 1);
2730 tsv
->initial_value
= (LONGEST
) val
;
2731 tsv
->name
= varname
;
2733 set_trace_state_variable_value (num
, (LONGEST
) val
);
2739 cmd_qtenable_disable (char *own_buf
, int enable
)
2741 char *packet
= own_buf
;
2743 struct tracepoint
*tp
;
2745 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2746 packet
= unpack_varlen_hex (packet
, &num
);
2747 ++packet
; /* skip a colon */
2748 packet
= unpack_varlen_hex (packet
, &addr
);
2750 tp
= find_tracepoint (num
, addr
);
2754 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2756 trace_debug ("Tracepoint %d at 0x%s is already %s",
2757 (int) num
, paddress (addr
),
2758 enable
? "enabled" : "disabled");
2763 trace_debug ("%s tracepoint %d at 0x%s",
2764 enable
? "Enabling" : "Disabling",
2765 (int) num
, paddress (addr
));
2767 tp
->enabled
= enable
;
2769 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2772 int offset
= offsetof (struct tracepoint
, enabled
);
2773 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2775 ret
= prepare_to_access_memory ();
2778 trace_debug ("Failed to temporarily stop inferior threads");
2779 write_enn (own_buf
);
2783 ret
= write_inferior_integer (obj_addr
, enable
);
2784 done_accessing_memory ();
2788 trace_debug ("Cannot write enabled flag into "
2789 "inferior process memory");
2790 write_enn (own_buf
);
2799 trace_debug ("Tracepoint %d at 0x%s not found",
2800 (int) num
, paddress (addr
));
2801 write_enn (own_buf
);
2806 cmd_qtv (char *own_buf
)
2811 char *packet
= own_buf
;
2813 packet
+= strlen ("qTV:");
2814 unpack_varlen_hex (packet
, &num
);
2816 if (current_traceframe
>= 0)
2818 err
= traceframe_read_tsv ((int) num
, &val
);
2821 strcpy (own_buf
, "U");
2825 /* Only make tsv's be undefined before the first trace run. After a
2826 trace run is over, the user might want to see the last value of
2827 the tsv, and it might not be available in a traceframe. */
2828 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2830 strcpy (own_buf
, "U");
2834 val
= get_trace_state_variable_value (num
);
2836 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2839 /* Clear out the list of readonly regions. */
2842 clear_readonly_regions (void)
2844 struct readonly_region
*roreg
;
2846 while (readonly_regions
)
2848 roreg
= readonly_regions
;
2849 readonly_regions
= readonly_regions
->next
;
2854 /* Parse the collection of address ranges whose contents GDB believes
2855 to be unchanging and so can be read directly from target memory
2856 even while looking at a traceframe. */
2859 cmd_qtro (char *own_buf
)
2861 ULONGEST start
, end
;
2862 struct readonly_region
*roreg
;
2863 char *packet
= own_buf
;
2865 trace_debug ("Want to mark readonly regions");
2867 clear_readonly_regions ();
2869 packet
+= strlen ("QTro");
2871 while (*packet
== ':')
2873 ++packet
; /* skip a colon */
2874 packet
= unpack_varlen_hex (packet
, &start
);
2875 ++packet
; /* skip a comma */
2876 packet
= unpack_varlen_hex (packet
, &end
);
2877 roreg
= xmalloc (sizeof (struct readonly_region
));
2878 roreg
->start
= start
;
2880 roreg
->next
= readonly_regions
;
2881 readonly_regions
= roreg
;
2882 trace_debug ("Added readonly region from 0x%s to 0x%s",
2883 paddress (roreg
->start
), paddress (roreg
->end
));
2889 /* Test to see if the given range is in our list of readonly ranges.
2890 We only test for being entirely within a range, GDB is not going to
2891 send a single memory packet that spans multiple regions. */
2894 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2896 struct readonly_region
*roreg
;
2898 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2899 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2905 /* The maximum size of a jump pad entry. */
2906 static const int max_jump_pad_size
= 0x100;
2908 static CORE_ADDR gdb_jump_pad_head
;
2910 /* Return the address of the next free jump space. */
2913 get_jump_space_head (void)
2915 if (gdb_jump_pad_head
== 0)
2917 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2918 &gdb_jump_pad_head
))
2919 fatal ("error extracting jump_pad_buffer");
2922 return gdb_jump_pad_head
;
2925 /* Reserve USED bytes from the jump space. */
2928 claim_jump_space (ULONGEST used
)
2930 trace_debug ("claim_jump_space reserves %s bytes at %s",
2931 pulongest (used
), paddress (gdb_jump_pad_head
));
2932 gdb_jump_pad_head
+= used
;
2935 static CORE_ADDR trampoline_buffer_head
= 0;
2936 static CORE_ADDR trampoline_buffer_tail
;
2938 /* Reserve USED bytes from the trampoline buffer and return the
2939 address of the start of the reserved space in TRAMPOLINE. Returns
2940 non-zero if the space is successfully claimed. */
2943 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2945 if (!trampoline_buffer_head
)
2947 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2948 &trampoline_buffer_tail
))
2950 fatal ("error extracting trampoline_buffer");
2954 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2955 &trampoline_buffer_head
))
2957 fatal ("error extracting trampoline_buffer_end");
2962 /* Start claiming space from the top of the trampoline space. If
2963 the space is located at the bottom of the virtual address space,
2964 this reduces the possibility that corruption will occur if a null
2965 pointer is used to write to memory. */
2966 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2968 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2973 trampoline_buffer_head
-= used
;
2975 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2976 pulongest (used
), paddress (trampoline_buffer_head
));
2978 *trampoline
= trampoline_buffer_head
;
2982 /* Returns non-zero if there is space allocated for use in trampolines
2983 for fast tracepoints. */
2986 have_fast_tracepoint_trampoline_buffer (char *buf
)
2988 CORE_ADDR trampoline_end
, errbuf
;
2990 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2993 fatal ("error extracting trampoline_buffer_end");
3000 strcpy (buf
, "was claiming");
3001 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
3004 fatal ("error extracting errbuf");
3008 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
3011 return trampoline_end
!= 0;
3014 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3015 the command fails, or 0 otherwise. If the command ran
3016 successfully, but probing the marker failed, ERROUT will be filled
3017 with the error to reply to GDB, and -1 is also returned. This
3018 allows directly passing IPA errors to GDB. */
3021 probe_marker_at (CORE_ADDR address
, char *errout
)
3023 char cmd
[IPA_CMD_BUF_SIZE
];
3026 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
3027 err
= run_inferior_command (cmd
, strlen (cmd
) + 1);
3033 strcpy (errout
, cmd
);
3042 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
3044 to
->jump_pad
= from
->jump_pad
;
3045 to
->jump_pad_end
= from
->jump_pad_end
;
3046 to
->trampoline
= from
->trampoline
;
3047 to
->trampoline_end
= from
->trampoline_end
;
3048 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
3049 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
3050 to
->handle
= from
->handle
;
3052 gdb_assert (from
->handle
);
3053 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
3056 #define MAX_JUMP_SIZE 20
3058 /* Install fast tracepoint. Return 0 if successful, otherwise return
3062 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
3064 CORE_ADDR jentry
, jump_entry
;
3065 CORE_ADDR trampoline
;
3066 ULONGEST trampoline_size
;
3068 /* The jump to the jump pad of the last fast tracepoint
3070 unsigned char fjump
[MAX_JUMP_SIZE
];
3071 ULONGEST fjump_size
;
3073 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
3075 trace_debug ("Requested a fast tracepoint on an instruction "
3076 "that is of less than the minimum length.");
3080 jentry
= jump_entry
= get_jump_space_head ();
3083 trampoline_size
= 0;
3085 /* Install the jump pad. */
3086 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3088 ipa_sym_addrs
.addr_gdb_collect
,
3089 ipa_sym_addrs
.addr_collecting
,
3092 &trampoline
, &trampoline_size
,
3094 &tpoint
->adjusted_insn_addr
,
3095 &tpoint
->adjusted_insn_addr_end
,
3102 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3105 if (tpoint
->handle
!= NULL
)
3107 tpoint
->jump_pad
= jump_entry
;
3108 tpoint
->jump_pad_end
= jentry
;
3109 tpoint
->trampoline
= trampoline
;
3110 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3112 /* Pad to 8-byte alignment. */
3113 jentry
= ((jentry
+ 7) & ~0x7);
3114 claim_jump_space (jentry
- jump_entry
);
3121 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3124 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3126 tpoint
->handle
= NULL
;
3129 if (tpoint
->type
== trap_tracepoint
)
3131 /* Tracepoints are installed as memory breakpoints. Just go
3132 ahead and install the trap. The breakpoints module
3133 handles duplicated breakpoints, and the memory read
3134 routine handles un-patching traps from memory reads. */
3135 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3136 tracepoint_handler
);
3138 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3140 if (!agent_loaded_p ())
3142 trace_debug ("Requested a %s tracepoint, but fast "
3143 "tracepoints aren't supported.",
3144 tpoint
->type
== static_tracepoint
? "static" : "fast");
3145 write_e_ipa_not_loaded (own_buf
);
3148 if (tpoint
->type
== static_tracepoint
3149 && !in_process_agent_supports_ust ())
3151 trace_debug ("Requested a static tracepoint, but static "
3152 "tracepoints are not supported.");
3153 write_e_ust_not_loaded (own_buf
);
3157 if (tpoint
->type
== fast_tracepoint
)
3158 install_fast_tracepoint (tpoint
, own_buf
);
3161 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3162 tpoint
->handle
= (void *) -1;
3167 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3169 if (tpoint
->handle
== NULL
)
3171 if (*own_buf
== '\0')
3172 write_enn (own_buf
);
3178 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3181 cmd_qtstart (char *packet
)
3183 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3184 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3186 trace_debug ("Starting the trace");
3188 /* Pause all threads temporarily while we patch tracepoints. */
3191 /* Get threads out of jump pads. Safe to do here, since this is a
3192 top level command. And, required to do here, since we're
3193 deleting/rewriting jump pads. */
3195 stabilize_threads ();
3197 /* Freeze threads. */
3200 /* Sync the fast tracepoints list in the inferior ftlib. */
3201 if (agent_loaded_p ())
3202 download_trace_state_variables ();
3204 /* No previous fast tpoint yet. */
3205 prev_ftpoint
= NULL
;
3207 /* No previous static tpoint yet. */
3208 prev_stpoint
= NULL
;
3212 /* Start out empty. */
3213 if (agent_loaded_p ())
3214 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
3216 /* Download and install tracepoints. */
3217 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3219 /* Ensure all the hit counts start at zero. */
3220 tpoint
->hit_count
= 0;
3221 tpoint
->traceframe_usage
= 0;
3223 if (tpoint
->type
== trap_tracepoint
)
3225 /* Tracepoints are installed as memory breakpoints. Just go
3226 ahead and install the trap. The breakpoints module
3227 handles duplicated breakpoints, and the memory read
3228 routine handles un-patching traps from memory reads. */
3229 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3230 tracepoint_handler
);
3232 else if (tpoint
->type
== fast_tracepoint
3233 || tpoint
->type
== static_tracepoint
)
3235 if (maybe_write_ipa_not_loaded (packet
))
3237 trace_debug ("Requested a %s tracepoint, but fast "
3238 "tracepoints aren't supported.",
3239 tpoint
->type
== static_tracepoint
3240 ? "static" : "fast");
3244 if (tpoint
->type
== fast_tracepoint
)
3247 = use_agent
&& agent_capability_check (AGENT_CAPA_FAST_TRACE
);
3249 if (prev_ftpoint
!= NULL
3250 && prev_ftpoint
->address
== tpoint
->address
)
3253 tracepoint_send_agent (tpoint
);
3255 download_tracepoint_1 (tpoint
);
3257 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3261 /* Tracepoint is installed successfully? */
3264 /* Download and install fast tracepoint by agent. */
3266 installed
= !tracepoint_send_agent (tpoint
);
3269 download_tracepoint_1 (tpoint
);
3270 installed
= !install_fast_tracepoint (tpoint
, packet
);
3274 prev_ftpoint
= tpoint
;
3279 if (!in_process_agent_supports_ust ())
3281 trace_debug ("Requested a static tracepoint, but static "
3282 "tracepoints are not supported.");
3286 download_tracepoint_1 (tpoint
);
3287 /* Can only probe a given marker once. */
3288 if (prev_stpoint
!= NULL
3289 && prev_stpoint
->address
== tpoint
->address
)
3290 tpoint
->handle
= (void *) -1;
3293 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3295 tpoint
->handle
= (void *) -1;
3297 /* So that we can handle multiple static tracepoints
3298 at the same address easily. */
3299 prev_stpoint
= tpoint
;
3305 tpptr
= tpoint
->obj_addr_on_target
;
3307 if (tpoint
== tracepoints
)
3308 /* First object in list, set the head pointer in the
3310 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3312 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
3317 /* Any failure in the inner loop is sufficient cause to give
3319 if (tpoint
->handle
== NULL
)
3323 /* Any error in tracepoint insertion is unacceptable; better to
3324 address the problem now, than end up with a useless or misleading
3328 clear_installed_tracepoints ();
3329 if (*packet
== '\0')
3335 stopping_tracepoint
= NULL
;
3336 trace_buffer_is_full
= 0;
3337 expr_eval_result
= expr_eval_no_error
;
3338 error_tracepoint
= NULL
;
3339 tracing_start_time
= get_timestamp ();
3341 /* Tracing is now active, hits will now start being logged. */
3344 if (agent_loaded_p ())
3346 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3347 fatal ("Error setting tracing variable in lib");
3349 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3351 fatal ("Error clearing stopping_tracepoint variable in lib");
3353 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3354 fatal ("Error clearing trace_buffer_is_full variable in lib");
3356 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3357 stop_tracing_handler
);
3358 if (stop_tracing_bkpt
== NULL
)
3359 error ("Error setting stop_tracing breakpoint");
3361 flush_trace_buffer_bkpt
3362 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3363 flush_trace_buffer_handler
);
3364 if (flush_trace_buffer_bkpt
== NULL
)
3365 error ("Error setting flush_trace_buffer breakpoint");
3373 /* End a tracing run, filling in a stop reason to report back to GDB,
3374 and removing the tracepoints from the code. */
3381 trace_debug ("Tracing is already off, ignoring");
3385 trace_debug ("Stopping the trace");
3387 /* Pause all threads before removing fast jumps from memory,
3388 breakpoints, and touching IPA state variables (inferior memory).
3389 Some thread may hit the internal tracing breakpoints, or be
3390 collecting this moment, but that's ok, we don't release the
3391 tpoint object's memory or the jump pads here (we only do that
3392 when we're sure we can move all threads out of the jump pads).
3393 We can't now, since we may be getting here due to the inferior
3394 agent calling us. */
3396 /* Since we're removing breakpoints, cancel breakpoint hits,
3397 possibly related to the breakpoints we're about to delete. */
3398 cancel_breakpoints ();
3400 /* Stop logging. Tracepoints can still be hit, but they will not be
3403 if (agent_loaded_p ())
3405 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3406 fatal ("Error clearing tracing variable in lib");
3409 tracing_stop_time
= get_timestamp ();
3410 tracing_stop_reason
= "t???";
3411 tracing_stop_tpnum
= 0;
3412 if (stopping_tracepoint
)
3414 trace_debug ("Stopping the trace because "
3415 "tracepoint %d was hit %" PRIu64
" times",
3416 stopping_tracepoint
->number
,
3417 stopping_tracepoint
->pass_count
);
3418 tracing_stop_reason
= "tpasscount";
3419 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3421 else if (trace_buffer_is_full
)
3423 trace_debug ("Stopping the trace because the trace buffer is full");
3424 tracing_stop_reason
= "tfull";
3426 else if (expr_eval_result
!= expr_eval_no_error
)
3428 trace_debug ("Stopping the trace because of an expression eval error");
3429 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3430 tracing_stop_tpnum
= error_tracepoint
->number
;
3432 #ifndef IN_PROCESS_AGENT
3433 else if (!gdb_connected ())
3435 trace_debug ("Stopping the trace because GDB disconnected");
3436 tracing_stop_reason
= "tdisconnected";
3441 trace_debug ("Stopping the trace because of a tstop command");
3442 tracing_stop_reason
= "tstop";
3445 stopping_tracepoint
= NULL
;
3446 error_tracepoint
= NULL
;
3448 /* Clear out the tracepoints. */
3449 clear_installed_tracepoints ();
3451 if (agent_loaded_p ())
3453 /* Pull in fast tracepoint trace frames from the inferior lib
3454 buffer into our buffer, even if our buffer is already full,
3455 because we want to present the full number of created frames
3456 in addition to what fit in the trace buffer. */
3457 upload_fast_traceframes ();
3460 if (stop_tracing_bkpt
!= NULL
)
3462 delete_breakpoint (stop_tracing_bkpt
);
3463 stop_tracing_bkpt
= NULL
;
3466 if (flush_trace_buffer_bkpt
!= NULL
)
3468 delete_breakpoint (flush_trace_buffer_bkpt
);
3469 flush_trace_buffer_bkpt
= NULL
;
3476 stop_tracing_handler (CORE_ADDR addr
)
3478 trace_debug ("lib hit stop_tracing");
3480 /* Don't actually handle it here. When we stop tracing we remove
3481 breakpoints from the inferior, and that is not allowed in a
3482 breakpoint handler (as the caller is walking the breakpoint
3488 flush_trace_buffer_handler (CORE_ADDR addr
)
3490 trace_debug ("lib hit flush_trace_buffer");
3495 cmd_qtstop (char *packet
)
3502 cmd_qtdisconnected (char *own_buf
)
3505 char *packet
= own_buf
;
3507 packet
+= strlen ("QTDisconnected:");
3509 unpack_varlen_hex (packet
, &setting
);
3513 disconnected_tracing
= setting
;
3517 cmd_qtframe (char *own_buf
)
3519 ULONGEST frame
, pc
, lo
, hi
, num
;
3521 struct traceframe
*tframe
;
3522 char *packet
= own_buf
;
3524 packet
+= strlen ("QTFrame:");
3526 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3528 packet
+= strlen ("pc:");
3529 unpack_varlen_hex (packet
, &pc
);
3530 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3531 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3533 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3535 packet
+= strlen ("range:");
3536 packet
= unpack_varlen_hex (packet
, &lo
);
3538 unpack_varlen_hex (packet
, &hi
);
3539 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3540 paddress (lo
), paddress (hi
));
3541 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3543 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3545 packet
+= strlen ("outside:");
3546 packet
= unpack_varlen_hex (packet
, &lo
);
3548 unpack_varlen_hex (packet
, &hi
);
3549 trace_debug ("Want to find next traceframe "
3550 "outside the range 0x%s to 0x%s",
3551 paddress (lo
), paddress (hi
));
3552 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3554 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3556 packet
+= strlen ("tdp:");
3557 unpack_varlen_hex (packet
, &num
);
3559 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3560 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3564 unpack_varlen_hex (packet
, &frame
);
3565 tfnum
= (int) frame
;
3568 trace_debug ("Want to stop looking at traceframes");
3569 current_traceframe
= -1;
3573 trace_debug ("Want to look at traceframe %d", tfnum
);
3574 tframe
= find_traceframe (tfnum
);
3579 current_traceframe
= tfnum
;
3580 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3583 sprintf (own_buf
, "F-1");
3587 cmd_qtstatus (char *packet
)
3589 char *stop_reason_rsp
= NULL
;
3590 char *buf1
, *buf2
, *buf3
, *str
;
3593 /* Translate the plain text of the notes back into hex for
3596 str
= (tracing_user_name
? tracing_user_name
: "");
3597 slen
= strlen (str
);
3598 buf1
= (char *) alloca (slen
* 2 + 1);
3599 hexify (buf1
, str
, slen
);
3601 str
= (tracing_notes
? tracing_notes
: "");
3602 slen
= strlen (str
);
3603 buf2
= (char *) alloca (slen
* 2 + 1);
3604 hexify (buf2
, str
, slen
);
3606 str
= (tracing_stop_note
? tracing_stop_note
: "");
3607 slen
= strlen (str
);
3608 buf3
= (char *) alloca (slen
* 2 + 1);
3609 hexify (buf3
, str
, slen
);
3611 trace_debug ("Returning trace status as %d, stop reason %s",
3612 tracing
, tracing_stop_reason
);
3614 if (agent_loaded_p ())
3618 upload_fast_traceframes ();
3623 stop_reason_rsp
= (char *) tracing_stop_reason
;
3625 /* The user visible error string in terror needs to be hex encoded.
3626 We leave it as plain string in `tracing_stop_reason' to ease
3628 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3630 const char *result_name
;
3634 result_name
= stop_reason_rsp
+ strlen ("terror:");
3635 hexstr_len
= strlen (result_name
) * 2;
3636 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3637 strcpy (p
, "terror:");
3639 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3642 /* If this was a forced stop, include any stop note that was supplied. */
3643 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3645 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3646 strcpy (stop_reason_rsp
, "tstop:");
3647 strcat (stop_reason_rsp
, buf3
);
3653 "tframes:%x;tcreated:%x;"
3654 "tfree:%x;tsize:%s;"
3657 "starttime:%s;stoptime:%s;"
3658 "username:%s:;notes:%s:",
3660 stop_reason_rsp
, tracing_stop_tpnum
,
3661 traceframe_count
, traceframes_created
,
3662 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3663 circular_trace_buffer
,
3664 disconnected_tracing
,
3665 plongest (tracing_start_time
), plongest (tracing_stop_time
),
3670 cmd_qtp (char *own_buf
)
3673 struct tracepoint
*tpoint
;
3674 char *packet
= own_buf
;
3676 packet
+= strlen ("qTP:");
3678 packet
= unpack_varlen_hex (packet
, &num
);
3679 ++packet
; /* skip a colon */
3680 packet
= unpack_varlen_hex (packet
, &addr
);
3682 /* See if we already have this tracepoint. */
3683 tpoint
= find_tracepoint (num
, addr
);
3687 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3688 (int) num
, paddress (addr
));
3689 write_enn (own_buf
);
3693 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3694 tpoint
->traceframe_usage
);
3697 /* State variables to help return all the tracepoint bits. */
3698 static struct tracepoint
*cur_tpoint
;
3699 static unsigned int cur_action
;
3700 static unsigned int cur_step_action
;
3701 static struct source_string
*cur_source_string
;
3702 static struct trace_state_variable
*cur_tsv
;
3704 /* Compose a response that is an imitation of the syntax by which the
3705 tracepoint was originally downloaded. */
3708 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3712 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3713 paddress (tpoint
->address
),
3714 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3715 tpoint
->pass_count
);
3716 if (tpoint
->type
== fast_tracepoint
)
3717 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3718 else if (tpoint
->type
== static_tracepoint
)
3719 sprintf (packet
+ strlen (packet
), ":S");
3723 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3724 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3725 tpoint
->cond
->length
, buf
);
3730 /* Compose a response that is an imitation of the syntax by which the
3731 tracepoint action was originally downloaded (with the difference
3732 that due to the way we store the actions, this will output a packet
3733 per action, while GDB could have combined more than one action
3737 response_action (char *packet
, struct tracepoint
*tpoint
,
3738 char *taction
, int step
)
3740 sprintf (packet
, "%c%x:%s:%s",
3741 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3745 /* Compose a response that is an imitation of the syntax by which the
3746 tracepoint source piece was originally downloaded. */
3749 response_source (char *packet
,
3750 struct tracepoint
*tpoint
, struct source_string
*src
)
3755 len
= strlen (src
->str
);
3756 buf
= alloca (len
* 2 + 1);
3757 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3759 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3760 tpoint
->number
, paddress (tpoint
->address
),
3761 src
->type
, 0, len
, buf
);
3764 /* Return the first piece of tracepoint definition, and initialize the
3765 state machine that will iterate through all the tracepoint
3769 cmd_qtfp (char *packet
)
3771 trace_debug ("Returning first tracepoint definition piece");
3773 cur_tpoint
= tracepoints
;
3774 cur_action
= cur_step_action
= 0;
3775 cur_source_string
= NULL
;
3778 response_tracepoint (packet
, cur_tpoint
);
3780 strcpy (packet
, "l");
3783 /* Return additional pieces of tracepoint definition. Each action and
3784 stepping action must go into its own packet, because of packet size
3785 limits, and so we use state variables to deliver one piece at a
3789 cmd_qtsp (char *packet
)
3791 trace_debug ("Returning subsequent tracepoint definition piece");
3795 /* This case would normally never occur, but be prepared for
3797 strcpy (packet
, "l");
3799 else if (cur_action
< cur_tpoint
->numactions
)
3801 response_action (packet
, cur_tpoint
,
3802 cur_tpoint
->actions_str
[cur_action
], 0);
3805 else if (cur_step_action
< cur_tpoint
->num_step_actions
)
3807 response_action (packet
, cur_tpoint
,
3808 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3811 else if ((cur_source_string
3812 ? cur_source_string
->next
3813 : cur_tpoint
->source_strings
))
3815 if (cur_source_string
)
3816 cur_source_string
= cur_source_string
->next
;
3818 cur_source_string
= cur_tpoint
->source_strings
;
3819 response_source (packet
, cur_tpoint
, cur_source_string
);
3823 cur_tpoint
= cur_tpoint
->next
;
3824 cur_action
= cur_step_action
= 0;
3825 cur_source_string
= NULL
;
3827 response_tracepoint (packet
, cur_tpoint
);
3829 strcpy (packet
, "l");
3833 /* Compose a response that is an imitation of the syntax by which the
3834 trace state variable was originally downloaded. */
3837 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3839 char *buf
= (char *) "";
3844 namelen
= strlen (tsv
->name
);
3845 buf
= alloca (namelen
* 2 + 1);
3846 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3849 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3850 tsv
->getter
? 1 : 0, buf
);
3853 /* Return the first trace state variable definition, and initialize
3854 the state machine that will iterate through all the tsv bits. */
3857 cmd_qtfv (char *packet
)
3859 trace_debug ("Returning first trace state variable definition");
3861 cur_tsv
= trace_state_variables
;
3864 response_tsv (packet
, cur_tsv
);
3866 strcpy (packet
, "l");
3869 /* Return additional trace state variable definitions. */
3872 cmd_qtsv (char *packet
)
3874 trace_debug ("Returning additional trace state variable definition");
3878 cur_tsv
= cur_tsv
->next
;
3880 response_tsv (packet
, cur_tsv
);
3882 strcpy (packet
, "l");
3885 strcpy (packet
, "l");
3888 /* Return the first static tracepoint marker, and initialize the state
3889 machine that will iterate through all the static tracepoints
3893 cmd_qtfstm (char *packet
)
3895 if (!maybe_write_ipa_ust_not_loaded (packet
))
3896 run_inferior_command (packet
, strlen (packet
) + 1);
3899 /* Return additional static tracepoints markers. */
3902 cmd_qtsstm (char *packet
)
3904 if (!maybe_write_ipa_ust_not_loaded (packet
))
3905 run_inferior_command (packet
, strlen (packet
) + 1);
3908 /* Return the definition of the static tracepoint at a given address.
3909 Result packet is the same as qTsST's. */
3912 cmd_qtstmat (char *packet
)
3914 if (!maybe_write_ipa_ust_not_loaded (packet
))
3915 run_inferior_command (packet
, strlen (packet
) + 1);
3918 /* Sent the agent a command to close it. */
3921 gdb_agent_about_to_close (int pid
)
3923 char buf
[IPA_CMD_BUF_SIZE
];
3925 if (!maybe_write_ipa_not_loaded (buf
))
3927 struct thread_info
*save_inferior
;
3928 struct inferior_list_entry
*inf
= all_threads
.head
;
3930 save_inferior
= current_inferior
;
3932 /* Find a certain thread which belongs to process PID. */
3935 if (ptid_get_pid (inf
->id
) == pid
)
3940 current_inferior
= (struct thread_info
*) inf
;
3942 strcpy (buf
, "close");
3944 run_inferior_command (buf
, strlen (buf
) + 1);
3946 current_inferior
= save_inferior
;
3950 /* Return the minimum instruction size needed for fast tracepoints as a
3951 hexadecimal number. */
3954 cmd_qtminftpilen (char *packet
)
3956 if (current_inferior
== NULL
)
3958 /* Indicate that the minimum length is currently unknown. */
3959 strcpy (packet
, "0");
3963 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3966 /* Respond to qTBuffer packet with a block of raw data from the trace
3967 buffer. GDB may ask for a lot, but we are allowed to reply with
3968 only as much as will fit within packet limits or whatever. */
3971 cmd_qtbuffer (char *own_buf
)
3973 ULONGEST offset
, num
, tot
;
3975 char *packet
= own_buf
;
3977 packet
+= strlen ("qTBuffer:");
3979 packet
= unpack_varlen_hex (packet
, &offset
);
3980 ++packet
; /* skip a comma */
3981 unpack_varlen_hex (packet
, &num
);
3983 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3984 (int) num
, phex_nz (offset
, 0));
3986 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3988 /* If we're right at the end, reply specially that we're done. */
3991 strcpy (own_buf
, "l");
3995 /* Object to any other out-of-bounds request. */
3998 write_enn (own_buf
);
4002 /* Compute the pointer corresponding to the given offset, accounting
4004 tbp
= trace_buffer_start
+ offset
;
4005 if (tbp
>= trace_buffer_wrap
)
4006 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
4008 /* Trim to the remaining bytes if we're close to the end. */
4009 if (num
> tot
- offset
)
4012 /* Trim to available packet size. */
4013 if (num
>= (PBUFSIZ
- 16) / 2 )
4014 num
= (PBUFSIZ
- 16) / 2;
4016 convert_int_to_ascii (tbp
, own_buf
, num
);
4020 cmd_bigqtbuffer_circular (char *own_buf
)
4023 char *packet
= own_buf
;
4025 packet
+= strlen ("QTBuffer:circular:");
4027 unpack_varlen_hex (packet
, &val
);
4028 circular_trace_buffer
= val
;
4029 trace_debug ("Trace buffer is now %s",
4030 circular_trace_buffer
? "circular" : "linear");
4035 cmd_bigqtbuffer_size (char *own_buf
)
4039 char *packet
= own_buf
;
4041 /* Can't change the size during a tracing run. */
4044 write_enn (own_buf
);
4048 packet
+= strlen ("QTBuffer:size:");
4050 /* -1 is sent as literal "-1". */
4051 if (strcmp (packet
, "-1") == 0)
4052 sval
= DEFAULT_TRACE_BUFFER_SIZE
;
4055 unpack_varlen_hex (packet
, &val
);
4056 sval
= (LONGEST
) val
;
4059 init_trace_buffer (sval
);
4060 trace_debug ("Trace buffer is now %s bytes",
4061 plongest (trace_buffer_size
));
4066 cmd_qtnotes (char *own_buf
)
4069 char *saved
, *user
, *notes
, *stopnote
;
4070 char *packet
= own_buf
;
4072 packet
+= strlen ("QTNotes:");
4076 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
4078 packet
+= strlen ("user:");
4080 packet
= strchr (packet
, ';');
4081 nbytes
= (packet
- saved
) / 2;
4082 user
= xmalloc (nbytes
+ 1);
4083 nbytes
= unhexify (user
, saved
, nbytes
);
4084 user
[nbytes
] = '\0';
4085 ++packet
; /* skip the semicolon */
4086 trace_debug ("User is '%s'", user
);
4087 xfree (tracing_user_name
);
4088 tracing_user_name
= user
;
4090 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
4092 packet
+= strlen ("notes:");
4094 packet
= strchr (packet
, ';');
4095 nbytes
= (packet
- saved
) / 2;
4096 notes
= xmalloc (nbytes
+ 1);
4097 nbytes
= unhexify (notes
, saved
, nbytes
);
4098 notes
[nbytes
] = '\0';
4099 ++packet
; /* skip the semicolon */
4100 trace_debug ("Notes is '%s'", notes
);
4101 xfree (tracing_notes
);
4102 tracing_notes
= notes
;
4104 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
4106 packet
+= strlen ("tstop:");
4108 packet
= strchr (packet
, ';');
4109 nbytes
= (packet
- saved
) / 2;
4110 stopnote
= xmalloc (nbytes
+ 1);
4111 nbytes
= unhexify (stopnote
, saved
, nbytes
);
4112 stopnote
[nbytes
] = '\0';
4113 ++packet
; /* skip the semicolon */
4114 trace_debug ("tstop note is '%s'", stopnote
);
4115 xfree (tracing_stop_note
);
4116 tracing_stop_note
= stopnote
;
4126 handle_tracepoint_general_set (char *packet
)
4128 if (strcmp ("QTinit", packet
) == 0)
4130 cmd_qtinit (packet
);
4133 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
4138 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
4140 cmd_qtdpsrc (packet
);
4143 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
4145 cmd_qtenable_disable (packet
, 1);
4148 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
4150 cmd_qtenable_disable (packet
, 0);
4153 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
4158 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
4163 else if (strcmp ("QTStart", packet
) == 0)
4165 cmd_qtstart (packet
);
4168 else if (strcmp ("QTStop", packet
) == 0)
4170 cmd_qtstop (packet
);
4173 else if (strncmp ("QTDisconnected:", packet
,
4174 strlen ("QTDisconnected:")) == 0)
4176 cmd_qtdisconnected (packet
);
4179 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
4181 cmd_qtframe (packet
);
4184 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
4186 cmd_bigqtbuffer_circular (packet
);
4189 else if (strncmp ("QTBuffer:size:", packet
, strlen ("QTBuffer:size:")) == 0)
4191 cmd_bigqtbuffer_size (packet
);
4194 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
4196 cmd_qtnotes (packet
);
4204 handle_tracepoint_query (char *packet
)
4206 if (strcmp ("qTStatus", packet
) == 0)
4208 cmd_qtstatus (packet
);
4211 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4216 else if (strcmp ("qTfP", packet
) == 0)
4221 else if (strcmp ("qTsP", packet
) == 0)
4226 else if (strcmp ("qTfV", packet
) == 0)
4231 else if (strcmp ("qTsV", packet
) == 0)
4236 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4241 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4243 cmd_qtbuffer (packet
);
4246 else if (strcmp ("qTfSTM", packet
) == 0)
4248 cmd_qtfstm (packet
);
4251 else if (strcmp ("qTsSTM", packet
) == 0)
4253 cmd_qtsstm (packet
);
4256 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4258 cmd_qtstmat (packet
);
4261 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4263 cmd_qtminftpilen (packet
);
4271 #ifndef IN_PROCESS_AGENT
4273 /* Call this when thread TINFO has hit the tracepoint defined by
4274 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4275 action. This adds a while-stepping collecting state item to the
4276 threads' collecting state list, so that we can keep track of
4277 multiple simultaneous while-stepping actions being collected by the
4278 same thread. This can happen in cases like:
4280 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4282 ff0003 INSN3 <-- TP2, collect $regs
4283 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4286 Notice that when instruction INSN5 is reached, the while-stepping
4287 actions of both TP1 and TP3 are still being collected, and that TP2
4288 had been collected meanwhile. The whole range of ff0001-ff0005
4289 should be single-stepped, due to at least TP1's while-stepping
4290 action covering the whole range. */
4293 add_while_stepping_state (struct thread_info
*tinfo
,
4294 int tp_number
, CORE_ADDR tp_address
)
4296 struct wstep_state
*wstep
;
4298 wstep
= xmalloc (sizeof (*wstep
));
4299 wstep
->next
= tinfo
->while_stepping
;
4301 wstep
->tp_number
= tp_number
;
4302 wstep
->tp_address
= tp_address
;
4303 wstep
->current_step
= 0;
4305 tinfo
->while_stepping
= wstep
;
4308 /* Release the while-stepping collecting state WSTEP. */
4311 release_while_stepping_state (struct wstep_state
*wstep
)
4316 /* Release all while-stepping collecting states currently associated
4317 with thread TINFO. */
4320 release_while_stepping_state_list (struct thread_info
*tinfo
)
4322 struct wstep_state
*head
;
4324 while (tinfo
->while_stepping
)
4326 head
= tinfo
->while_stepping
;
4327 tinfo
->while_stepping
= head
->next
;
4328 release_while_stepping_state (head
);
4332 /* If TINFO was handling a 'while-stepping' action, the step has
4333 finished, so collect any step data needed, and check if any more
4334 steps are required. Return true if the thread was indeed
4335 collecting tracepoint data, false otherwise. */
4338 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4340 struct tracepoint
*tpoint
;
4341 struct wstep_state
*wstep
;
4342 struct wstep_state
**wstep_link
;
4343 struct trap_tracepoint_ctx ctx
;
4345 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4347 if (agent_loaded_p ())
4348 upload_fast_traceframes ();
4350 /* Check if we were indeed collecting data for one of more
4351 tracepoints with a 'while-stepping' count. */
4352 if (tinfo
->while_stepping
== NULL
)
4357 /* We're not even tracing anymore. Stop this thread from
4359 release_while_stepping_state_list (tinfo
);
4361 /* The thread had stopped due to a single-step request indeed
4362 explained by a tracepoint. */
4366 wstep
= tinfo
->while_stepping
;
4367 wstep_link
= &tinfo
->while_stepping
;
4369 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4370 target_pid_to_str (tinfo
->entry
.id
),
4371 wstep
->tp_number
, paddress (wstep
->tp_address
));
4373 ctx
.base
.type
= trap_tracepoint
;
4374 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4376 while (wstep
!= NULL
)
4378 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4381 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4382 wstep
->tp_number
, paddress (wstep
->tp_address
),
4383 target_pid_to_str (tinfo
->entry
.id
));
4386 *wstep_link
= wstep
->next
;
4387 release_while_stepping_state (wstep
);
4388 wstep
= *wstep_link
;
4392 /* We've just finished one step. */
4393 ++wstep
->current_step
;
4396 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4397 stop_pc
, tpoint
, wstep
->current_step
);
4399 if (wstep
->current_step
>= tpoint
->step_count
)
4401 /* The requested numbers of steps have occurred. */
4402 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4403 target_pid_to_str (tinfo
->entry
.id
),
4404 wstep
->tp_number
, paddress (wstep
->tp_address
));
4406 /* Unlink the wstep. */
4407 *wstep_link
= wstep
->next
;
4408 release_while_stepping_state (wstep
);
4409 wstep
= *wstep_link
;
4411 /* Only check the hit count now, which ensure that we do all
4412 our stepping before stopping the run. */
4413 if (tpoint
->pass_count
> 0
4414 && tpoint
->hit_count
>= tpoint
->pass_count
4415 && stopping_tracepoint
== NULL
)
4416 stopping_tracepoint
= tpoint
;
4420 /* Keep single-stepping until the requested numbers of steps
4422 wstep_link
= &wstep
->next
;
4423 wstep
= *wstep_link
;
4426 if (stopping_tracepoint
4427 || trace_buffer_is_full
4428 || expr_eval_result
!= expr_eval_no_error
)
4438 /* Handle any internal tracing control breakpoint hits. That means,
4439 pull traceframes from the IPA to our buffer, and syncing both
4440 tracing agents when the IPA's tracing stops for some reason. */
4443 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4445 /* Pull in fast tracepoint trace frames from the inferior in-process
4446 agent's buffer into our buffer. */
4448 if (!agent_loaded_p ())
4451 upload_fast_traceframes ();
4453 /* Check if the in-process agent had decided we should stop
4455 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4457 int ipa_trace_buffer_is_full
;
4458 CORE_ADDR ipa_stopping_tracepoint
;
4459 int ipa_expr_eval_result
;
4460 CORE_ADDR ipa_error_tracepoint
;
4462 trace_debug ("lib stopped at stop_tracing");
4464 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4465 &ipa_trace_buffer_is_full
);
4467 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4468 &ipa_stopping_tracepoint
);
4469 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4471 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4472 &ipa_error_tracepoint
);
4473 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4475 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4476 &ipa_expr_eval_result
);
4477 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4479 trace_debug ("lib: trace_buffer_is_full: %d, "
4480 "stopping_tracepoint: %s, "
4481 "ipa_expr_eval_result: %d, "
4482 "error_tracepoint: %s, ",
4483 ipa_trace_buffer_is_full
,
4484 paddress (ipa_stopping_tracepoint
),
4485 ipa_expr_eval_result
,
4486 paddress (ipa_error_tracepoint
));
4490 if (ipa_trace_buffer_is_full
)
4491 trace_debug ("lib stopped due to full buffer.");
4492 if (ipa_stopping_tracepoint
)
4493 trace_debug ("lib stopped due to tpoint");
4494 if (ipa_stopping_tracepoint
)
4495 trace_debug ("lib stopped due to error");
4498 if (ipa_stopping_tracepoint
!= 0)
4501 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4503 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4505 expr_eval_result
= ipa_expr_eval_result
;
4507 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4512 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4514 trace_debug ("lib stopped at flush_trace_buffer");
4521 /* Return true if TINFO just hit a tracepoint. Collect data if
4525 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4527 struct tracepoint
*tpoint
;
4529 struct trap_tracepoint_ctx ctx
;
4531 /* Not tracing, don't handle. */
4535 ctx
.base
.type
= trap_tracepoint
;
4536 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4538 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4540 /* Note that we collect fast tracepoints here as well. We'll
4541 step over the fast tracepoint jump later, which avoids the
4542 double collect. However, we don't collect for static
4543 tracepoints here, because UST markers are compiled in program,
4544 and probes will be executed in program. So static tracepoints
4545 are collected there. */
4546 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4547 && tpoint
->type
!= static_tracepoint
)
4549 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4550 target_pid_to_str (tinfo
->entry
.id
),
4551 tpoint
->number
, paddress (tpoint
->address
));
4553 /* Test the condition if present, and collect if true. */
4555 || (condition_true_at_tracepoint
4556 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4557 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4560 if (stopping_tracepoint
4561 || trace_buffer_is_full
4562 || expr_eval_result
!= expr_eval_no_error
)
4566 /* If the tracepoint had a 'while-stepping' action, then set
4567 the thread to collect this tracepoint on the following
4569 else if (tpoint
->step_count
> 0)
4571 add_while_stepping_state (tinfo
,
4572 tpoint
->number
, tpoint
->address
);
4584 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4585 struct ust_marker_data
;
4586 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4587 struct traceframe
*tframe
);
4590 /* Create a trace frame for the hit of the given tracepoint in the
4594 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4595 struct tracepoint
*tpoint
)
4597 struct traceframe
*tframe
;
4600 /* Only count it as a hit when we actually collect data. */
4601 tpoint
->hit_count
++;
4603 /* If we've exceeded a defined pass count, record the event for
4604 later, and finish the collection for this hit. This test is only
4605 for nonstepping tracepoints, stepping tracepoints test at the end
4606 of their while-stepping loop. */
4607 if (tpoint
->pass_count
> 0
4608 && tpoint
->hit_count
>= tpoint
->pass_count
4609 && tpoint
->step_count
== 0
4610 && stopping_tracepoint
== NULL
)
4611 stopping_tracepoint
= tpoint
;
4613 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4614 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4616 tframe
= add_traceframe (tpoint
);
4620 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4622 #ifndef IN_PROCESS_AGENT
4623 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4624 tpoint
->number
, paddress (tpoint
->address
),
4625 tpoint
->actions_str
[acti
]);
4628 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4629 tpoint
->actions
[acti
]);
4632 finish_traceframe (tframe
);
4635 if (tframe
== NULL
&& tracing
)
4636 trace_buffer_is_full
= 1;
4639 #ifndef IN_PROCESS_AGENT
4642 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4644 struct tracepoint
*tpoint
, int current_step
)
4646 struct traceframe
*tframe
;
4649 trace_debug ("Making new step traceframe for "
4650 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4651 tpoint
->number
, paddress (tpoint
->address
),
4652 current_step
, tpoint
->step_count
,
4655 tframe
= add_traceframe (tpoint
);
4659 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4661 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4662 tpoint
->number
, paddress (tpoint
->address
),
4663 tpoint
->step_actions_str
[acti
]);
4665 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4666 tpoint
->step_actions
[acti
]);
4669 finish_traceframe (tframe
);
4672 if (tframe
== NULL
&& tracing
)
4673 trace_buffer_is_full
= 1;
4678 static struct regcache
*
4679 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4681 struct regcache
*regcache
= NULL
;
4683 #ifdef IN_PROCESS_AGENT
4684 if (ctx
->type
== fast_tracepoint
)
4686 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4687 if (!fctx
->regcache_initted
)
4689 fctx
->regcache_initted
= 1;
4690 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4691 supply_regblock (&fctx
->regcache
, NULL
);
4692 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4694 regcache
= &fctx
->regcache
;
4697 if (ctx
->type
== static_tracepoint
)
4699 struct static_tracepoint_ctx
*sctx
4700 = (struct static_tracepoint_ctx
*) ctx
;
4702 if (!sctx
->regcache_initted
)
4704 sctx
->regcache_initted
= 1;
4705 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4706 supply_regblock (&sctx
->regcache
, NULL
);
4707 /* Pass down the tracepoint address, because REGS doesn't
4708 include the PC, but we know what it must have been. */
4709 supply_static_tracepoint_registers (&sctx
->regcache
,
4710 (const unsigned char *)
4712 sctx
->tpoint
->address
);
4714 regcache
= &sctx
->regcache
;
4718 if (ctx
->type
== trap_tracepoint
)
4720 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4721 regcache
= tctx
->regcache
;
4725 gdb_assert (regcache
!= NULL
);
4731 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4733 struct tracepoint
*tpoint
,
4734 struct traceframe
*tframe
,
4735 struct tracepoint_action
*taction
)
4737 enum eval_result_type err
;
4739 switch (taction
->type
)
4743 struct collect_memory_action
*maction
;
4744 struct eval_agent_expr_context ax_ctx
;
4746 maction
= (struct collect_memory_action
*) taction
;
4747 ax_ctx
.regcache
= NULL
;
4748 ax_ctx
.tframe
= tframe
;
4749 ax_ctx
.tpoint
= tpoint
;
4751 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4752 pulongest (maction
->len
),
4753 paddress (maction
->addr
), maction
->basereg
);
4754 /* (should use basereg) */
4755 agent_mem_read (&ax_ctx
, NULL
, (CORE_ADDR
) maction
->addr
,
4761 unsigned char *regspace
;
4762 struct regcache tregcache
;
4763 struct regcache
*context_regcache
;
4766 trace_debug ("Want to collect registers");
4768 /* Collect all registers for now. */
4769 regspace
= add_traceframe_block (tframe
, tpoint
,
4770 1 + register_cache_size ());
4771 if (regspace
== NULL
)
4773 trace_debug ("Trace buffer block allocation failed, skipping");
4776 /* Identify a register block. */
4779 context_regcache
= get_context_regcache (ctx
);
4781 /* Wrap the regblock in a register cache (in the stack, we
4782 don't want to malloc here). */
4783 init_register_cache (&tregcache
, regspace
+ 1);
4785 /* Copy the register data to the regblock. */
4786 regcache_cpy (&tregcache
, context_regcache
);
4788 #ifndef IN_PROCESS_AGENT
4789 /* On some platforms, trap-based tracepoints will have the PC
4790 pointing to the next instruction after the trap, but we
4791 don't want the user or GDB trying to guess whether the
4792 saved PC needs adjusting; so always record the adjusted
4793 stop_pc. Note that we can't use tpoint->address instead,
4794 since it will be wrong for while-stepping actions. This
4795 adjustment is a nop for fast tracepoints collected from the
4796 in-process lib (but not if GDBserver is collecting one
4797 preemptively), since the PC had already been adjusted to
4798 contain the tracepoint's address by the jump pad. */
4799 trace_debug ("Storing stop pc (0x%s) in regblock",
4800 paddress (stop_pc
));
4802 /* This changes the regblock, not the thread's
4804 regcache_write_pc (&tregcache
, stop_pc
);
4810 struct eval_expr_action
*eaction
;
4811 struct eval_agent_expr_context ax_ctx
;
4813 eaction
= (struct eval_expr_action
*) taction
;
4814 ax_ctx
.regcache
= get_context_regcache (ctx
);
4815 ax_ctx
.tframe
= tframe
;
4816 ax_ctx
.tpoint
= tpoint
;
4818 trace_debug ("Want to evaluate expression");
4820 err
= gdb_eval_agent_expr (&ax_ctx
, eaction
->expr
, NULL
);
4822 if (err
!= expr_eval_no_error
)
4824 record_tracepoint_error (tpoint
, "action expression", err
);
4831 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4832 trace_debug ("Want to collect static trace data");
4833 collect_ust_data_at_tracepoint (ctx
, tframe
);
4835 trace_debug ("warning: collecting static trace data, "
4836 "but static tracepoints are not supported");
4841 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4847 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4848 struct tracepoint
*tpoint
)
4851 enum eval_result_type err
;
4853 /* Presently, gdbserver doesn't run compiled conditions, only the
4854 IPA does. If the program stops at a fast tracepoint's address
4855 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4856 gdbserver preemptively collect the fast tracepoint. Later, on
4857 resume, gdbserver steps over the fast tracepoint like it steps
4858 over breakpoints, so that the IPA doesn't see that fast
4859 tracepoint. This avoids double collects of fast tracepoints in
4860 that stopping scenario. Having gdbserver itself handle the fast
4861 tracepoint gives the user a consistent view of when fast or trap
4862 tracepoints are collected, compared to an alternative where only
4863 trap tracepoints are collected on stop, and fast tracepoints on
4864 resume. When a fast tracepoint is being processed by gdbserver,
4865 it is always the non-compiled condition expression that is
4867 #ifdef IN_PROCESS_AGENT
4868 if (tpoint
->compiled_cond
)
4869 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4873 struct eval_agent_expr_context ax_ctx
;
4875 ax_ctx
.regcache
= get_context_regcache (ctx
);
4876 ax_ctx
.tframe
= NULL
;
4877 ax_ctx
.tpoint
= tpoint
;
4879 err
= gdb_eval_agent_expr (&ax_ctx
, tpoint
->cond
, &value
);
4881 if (err
!= expr_eval_no_error
)
4883 record_tracepoint_error (tpoint
, "condition", err
);
4884 /* The error case must return false. */
4888 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4889 tpoint
->number
, paddress (tpoint
->address
),
4891 return (value
? 1 : 0);
4894 /* Do memory copies for bytecodes. */
4895 /* Do the recording of memory blocks for actions and bytecodes. */
4898 agent_mem_read (struct eval_agent_expr_context
*ctx
,
4899 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4901 unsigned char *mspace
;
4902 ULONGEST remaining
= len
;
4903 unsigned short blocklen
;
4905 /* If a 'to' buffer is specified, use it. */
4908 read_inferior_memory (from
, to
, len
);
4912 /* Otherwise, create a new memory block in the trace buffer. */
4913 while (remaining
> 0)
4917 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4918 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4919 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4922 /* Identify block as a memory block. */
4925 /* Record address and size. */
4926 memcpy (mspace
, &from
, sizeof (from
));
4927 mspace
+= sizeof (from
);
4928 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4929 mspace
+= sizeof (blocklen
);
4930 /* Record the memory block proper. */
4931 read_inferior_memory (from
, mspace
, blocklen
);
4932 trace_debug ("%d bytes recorded", blocklen
);
4933 remaining
-= blocklen
;
4940 agent_mem_read_string (struct eval_agent_expr_context
*ctx
,
4941 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4943 unsigned char *buf
, *mspace
;
4944 ULONGEST remaining
= len
;
4945 unsigned short blocklen
, i
;
4947 /* To save a bit of space, block lengths are 16-bit, so break large
4948 requests into multiple blocks. Bordering on overkill for strings,
4949 but it could happen that someone specifies a large max length. */
4950 while (remaining
> 0)
4954 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4955 /* We want working space to accumulate nonzero bytes, since
4956 traceframes must have a predecided size (otherwise it gets
4957 harder to wrap correctly for the circular case, etc). */
4958 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4959 for (i
= 0; i
< blocklen
; ++i
)
4961 /* Read the string one byte at a time, in case the string is
4962 at the end of a valid memory area - we don't want a
4963 correctly-terminated string to engender segvio
4965 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4970 /* Make sure outer loop stops now too. */
4971 remaining
= blocklen
;
4975 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4976 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4982 /* Identify block as a memory block. */
4985 /* Record address and size. */
4986 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4987 mspace
+= sizeof (from
);
4988 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4989 mspace
+= sizeof (blocklen
);
4990 /* Copy the string contents. */
4991 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
4992 remaining
-= blocklen
;
4999 /* Record the value of a trace state variable. */
5002 agent_tsv_read (struct eval_agent_expr_context
*ctx
, int n
)
5004 unsigned char *vspace
;
5007 vspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
,
5008 1 + sizeof (n
) + sizeof (LONGEST
));
5011 /* Identify block as a variable. */
5013 /* Record variable's number and value. */
5014 memcpy (vspace
+ 1, &n
, sizeof (n
));
5015 val
= get_trace_state_variable_value (n
);
5016 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
5017 trace_debug ("Variable %d recorded", n
);
5021 #ifndef IN_PROCESS_AGENT
5023 /* Callback for traceframe_walk_blocks, used to find a given block
5024 type in a traceframe. */
5027 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
5029 char *wantedp
= data
;
5031 if (*wantedp
== blocktype
)
5037 /* Walk over all traceframe blocks of the traceframe buffer starting
5038 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5039 block found, passing in DATA unmodified. If CALLBACK returns true,
5040 this returns a pointer to where the block is found. Returns NULL
5041 if no callback call returned true, indicating that all blocks have
5044 static unsigned char *
5045 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
5047 int (*callback
) (char blocktype
,
5048 unsigned char *dataptr
,
5052 unsigned char *dataptr
;
5056 trace_debug ("traceframe %d has no data", tfnum
);
5060 /* Iterate through a traceframe's blocks, looking for a block of the
5062 for (dataptr
= database
;
5063 dataptr
< database
+ datasize
;
5067 unsigned short mlen
;
5069 if (dataptr
== trace_buffer_wrap
)
5071 /* Adjust to reflect wrapping part of the frame around to
5073 datasize
= dataptr
- database
;
5074 dataptr
= database
= trace_buffer_lo
;
5077 blocktype
= *dataptr
++;
5079 if ((*callback
) (blocktype
, dataptr
, data
))
5085 /* Skip over the registers block. */
5086 dataptr
+= register_cache_size ();
5089 /* Skip over the memory block. */
5090 dataptr
+= sizeof (CORE_ADDR
);
5091 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5092 dataptr
+= (sizeof (mlen
) + mlen
);
5095 /* Skip over the TSV block. */
5096 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5099 /* Skip over the static trace data block. */
5100 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5101 dataptr
+= (sizeof (mlen
) + mlen
);
5104 trace_debug ("traceframe %d has unknown block type 0x%x",
5113 /* Look for the block of type TYPE_WANTED in the trameframe starting
5114 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5117 static unsigned char *
5118 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5119 int tfnum
, char type_wanted
)
5121 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5122 match_blocktype
, &type_wanted
);
5125 static unsigned char *
5126 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5128 unsigned char *regblock
;
5130 regblock
= traceframe_find_block_type (tframe
->data
,
5134 if (regblock
== NULL
)
5135 trace_debug ("traceframe %d has no register data", tfnum
);
5140 /* Get registers from a traceframe. */
5143 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5145 unsigned char *dataptr
;
5146 struct tracepoint
*tpoint
;
5147 struct traceframe
*tframe
;
5149 tframe
= find_traceframe (tfnum
);
5153 trace_debug ("traceframe %d not found", tfnum
);
5157 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5158 if (dataptr
== NULL
)
5160 /* Mark registers unavailable. */
5161 supply_regblock (regcache
, NULL
);
5163 /* We can generally guess at a PC, although this will be
5164 misleading for while-stepping frames and multi-location
5166 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5168 regcache_write_pc (regcache
, tpoint
->address
);
5171 supply_regblock (regcache
, dataptr
);
5177 traceframe_get_pc (struct traceframe
*tframe
)
5179 struct regcache regcache
;
5180 unsigned char *dataptr
;
5182 dataptr
= traceframe_find_regblock (tframe
, -1);
5183 if (dataptr
== NULL
)
5186 init_register_cache (®cache
, dataptr
);
5187 return regcache_read_pc (®cache
);
5190 /* Read a requested block of memory from a trace frame. */
5193 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5194 unsigned char *buf
, ULONGEST length
,
5197 struct traceframe
*tframe
;
5198 unsigned char *database
, *dataptr
;
5199 unsigned int datasize
;
5201 unsigned short mlen
;
5203 trace_debug ("traceframe_read_mem");
5205 tframe
= find_traceframe (tfnum
);
5209 trace_debug ("traceframe %d not found", tfnum
);
5213 datasize
= tframe
->data_size
;
5214 database
= dataptr
= &tframe
->data
[0];
5216 /* Iterate through a traceframe's blocks, looking for memory. */
5217 while ((dataptr
= traceframe_find_block_type (dataptr
,
5219 - (dataptr
- database
),
5220 tfnum
, 'M')) != NULL
)
5222 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5223 dataptr
+= sizeof (maddr
);
5224 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5225 dataptr
+= sizeof (mlen
);
5226 trace_debug ("traceframe %d has %d bytes at %s",
5227 tfnum
, mlen
, paddress (maddr
));
5229 /* If the block includes the first part of the desired range,
5230 return as much it has; GDB will re-request the remainder,
5231 which might be in a different block of this trace frame. */
5232 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5234 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5238 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5243 /* Skip over this block. */
5247 trace_debug ("traceframe %d has no memory data for the desired region",
5255 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5258 struct traceframe
*tframe
;
5259 unsigned char *database
, *dataptr
;
5260 unsigned int datasize
;
5264 trace_debug ("traceframe_read_tsv");
5266 tfnum
= current_traceframe
;
5270 trace_debug ("no current traceframe");
5274 tframe
= find_traceframe (tfnum
);
5278 trace_debug ("traceframe %d not found", tfnum
);
5282 datasize
= tframe
->data_size
;
5283 database
= dataptr
= &tframe
->data
[0];
5285 /* Iterate through a traceframe's blocks, looking for the last
5287 while ((dataptr
= traceframe_find_block_type (dataptr
,
5289 - (dataptr
- database
),
5290 tfnum
, 'V')) != NULL
)
5292 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5293 dataptr
+= sizeof (vnum
);
5295 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5297 /* Check that this is the variable we want. */
5300 memcpy (val
, dataptr
, sizeof (*val
));
5304 /* Skip over this block. */
5305 dataptr
+= sizeof (LONGEST
);
5309 trace_debug ("traceframe %d has no data for variable %d",
5314 /* Read a requested block of static tracepoint data from a trace
5318 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5319 unsigned char *buf
, ULONGEST length
,
5322 struct traceframe
*tframe
;
5323 unsigned char *database
, *dataptr
;
5324 unsigned int datasize
;
5325 unsigned short mlen
;
5327 trace_debug ("traceframe_read_sdata");
5329 tframe
= find_traceframe (tfnum
);
5333 trace_debug ("traceframe %d not found", tfnum
);
5337 datasize
= tframe
->data_size
;
5338 database
= &tframe
->data
[0];
5340 /* Iterate through a traceframe's blocks, looking for static
5342 dataptr
= traceframe_find_block_type (database
, datasize
,
5344 if (dataptr
!= NULL
)
5346 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5347 dataptr
+= sizeof (mlen
);
5350 if (offset
+ length
> mlen
)
5351 length
= mlen
- offset
;
5353 memcpy (buf
, dataptr
, length
);
5361 trace_debug ("traceframe %d has no static trace data", tfnum
);
5367 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5368 object. DATA is pointer to a struct buffer holding the
5369 traceframe-info object being built. */
5372 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5374 struct buffer
*buffer
= data
;
5380 unsigned short mlen
;
5383 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5384 dataptr
+= sizeof (maddr
);
5385 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5386 dataptr
+= sizeof (mlen
);
5387 buffer_xml_printf (buffer
,
5388 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5389 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5399 warning ("Unhandled trace block type (%d) '%c ' "
5400 "while building trace frame info.",
5401 blocktype
, blocktype
);
5408 /* Build a traceframe-info object for traceframe number TFNUM into
5412 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5414 struct traceframe
*tframe
;
5416 trace_debug ("traceframe_read_info");
5418 tframe
= find_traceframe (tfnum
);
5422 trace_debug ("traceframe %d not found", tfnum
);
5426 buffer_grow_str (buffer
, "<traceframe-info>\n");
5427 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5428 tfnum
, build_traceframe_info_xml
, buffer
);
5429 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5433 /* Return the first fast tracepoint whose jump pad contains PC. */
5435 static struct tracepoint
*
5436 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5438 struct tracepoint
*tpoint
;
5440 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5441 if (tpoint
->type
== fast_tracepoint
)
5442 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5448 /* Return the first fast tracepoint whose trampoline contains PC. */
5450 static struct tracepoint
*
5451 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5453 struct tracepoint
*tpoint
;
5455 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5457 if (tpoint
->type
== fast_tracepoint
5458 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5465 /* Return GDBserver's tracepoint that matches the IP Agent's
5466 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5469 static struct tracepoint
*
5470 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5472 struct tracepoint
*tpoint
;
5474 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5475 if (tpoint
->type
== fast_tracepoint
)
5476 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5484 /* The type of the object that is used to synchronize fast tracepoint
5487 typedef struct collecting_t
5489 /* The fast tracepoint number currently collecting. */
5492 /* A number that GDBserver can use to identify the thread that is
5493 presently holding the collect lock. This need not (and usually
5494 is not) the thread id, as getting the current thread ID usually
5495 requires a system call, which we want to avoid like the plague.
5496 Usually this is thread's TCB, found in the TLS (pseudo-)
5497 register, which is readable with a single insn on several
5499 uintptr_t thread_area
;
5502 #ifndef IN_PROCESS_AGENT
5505 force_unlock_trace_buffer (void)
5507 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5510 /* Check if the thread identified by THREAD_AREA which is stopped at
5511 STOP_PC, is presently locking the fast tracepoint collection, and
5512 if so, gather some status of said collection. Returns 0 if the
5513 thread isn't collecting or in the jump pad at all. 1, if in the
5514 jump pad (or within gdb_collect) and hasn't executed the adjusted
5515 original insn yet (can set a breakpoint there and run to it). 2,
5516 if presently executing the adjusted original insn --- in which
5517 case, if we want to move the thread out of the jump pad, we need to
5518 single-step it until this function returns 0. */
5521 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5523 struct fast_tpoint_collect_status
*status
)
5525 CORE_ADDR ipa_collecting
;
5526 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5527 CORE_ADDR ipa_gdb_trampoline_buffer
;
5528 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5529 struct tracepoint
*tpoint
;
5530 int needs_breakpoint
;
5532 /* The thread THREAD_AREA is either:
5534 0. not collecting at all, not within the jump pad, or within
5535 gdb_collect or one of its callees.
5537 1. in the jump pad and haven't reached gdb_collect
5539 2. within gdb_collect (out of the jump pad) (collect is set)
5541 3. we're in the jump pad, after gdb_collect having returned,
5542 possibly executing the adjusted insns.
5544 For cases 1 and 3, `collecting' may or not be set. The jump pad
5545 doesn't have any complicated jump logic, so we can tell if the
5546 thread is executing the adjust original insn or not by just
5547 matching STOP_PC with known jump pad addresses. If we it isn't
5548 yet executing the original insn, set a breakpoint there, and let
5549 the thread run to it, so to quickly step over a possible (many
5550 insns) gdb_collect call. Otherwise, or when the breakpoint is
5551 hit, only a few (small number of) insns are left to be executed
5552 in the jump pad. Single-step the thread until it leaves the
5557 needs_breakpoint
= 0;
5558 trace_debug ("fast_tracepoint_collecting");
5560 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5561 &ipa_gdb_jump_pad_buffer
))
5562 fatal ("error extracting `gdb_jump_pad_buffer'");
5563 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5564 &ipa_gdb_jump_pad_buffer_end
))
5565 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5567 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5568 &ipa_gdb_trampoline_buffer
))
5569 fatal ("error extracting `gdb_trampoline_buffer'");
5570 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5571 &ipa_gdb_trampoline_buffer_end
))
5572 fatal ("error extracting `gdb_trampoline_buffer_end'");
5574 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5575 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5577 /* We can tell which tracepoint(s) the thread is collecting by
5578 matching the jump pad address back to the tracepoint. */
5579 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5582 warning ("in jump pad, but no matching tpoint?");
5587 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5589 tpoint
->number
, paddress (tpoint
->address
),
5590 paddress (tpoint
->jump_pad
),
5591 paddress (tpoint
->jump_pad_end
),
5592 paddress (tpoint
->adjusted_insn_addr
),
5593 paddress (tpoint
->adjusted_insn_addr_end
));
5596 /* Definitely in the jump pad. May or may not need
5597 fast-exit-jump-pad breakpoint. */
5598 if (tpoint
->jump_pad
<= stop_pc
5599 && stop_pc
< tpoint
->adjusted_insn_addr
)
5600 needs_breakpoint
= 1;
5602 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5603 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5605 /* We can tell which tracepoint(s) the thread is collecting by
5606 matching the trampoline address back to the tracepoint. */
5607 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5610 warning ("in trampoline, but no matching tpoint?");
5615 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5616 tpoint
->number
, paddress (tpoint
->address
),
5617 paddress (tpoint
->trampoline
),
5618 paddress (tpoint
->trampoline_end
));
5621 /* Have not reached jump pad yet, but treat the trampoline as a
5622 part of the jump pad that is before the adjusted original
5624 needs_breakpoint
= 1;
5628 collecting_t ipa_collecting_obj
;
5630 /* If `collecting' is set/locked, then the THREAD_AREA thread
5631 may or not be the one holding the lock. We have to read the
5632 lock to find out. */
5634 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5637 trace_debug ("fast_tracepoint_collecting:"
5638 " failed reading 'collecting' in the inferior");
5642 if (!ipa_collecting
)
5644 trace_debug ("fast_tracepoint_collecting: not collecting"
5645 " (and nobody is).");
5649 /* Some thread is collecting. Check which. */
5650 if (read_inferior_memory (ipa_collecting
,
5651 (unsigned char *) &ipa_collecting_obj
,
5652 sizeof (ipa_collecting_obj
)) != 0)
5655 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5657 trace_debug ("fast_tracepoint_collecting: not collecting "
5658 "(another thread is)");
5663 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5666 warning ("fast_tracepoint_collecting: collecting, "
5667 "but tpoint %s not found?",
5668 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5672 /* The thread is within `gdb_collect', skip over the rest of
5673 fast tracepoint collection quickly using a breakpoint. */
5674 needs_breakpoint
= 1;
5677 /* The caller wants a bit of status detail. */
5680 status
->tpoint_num
= tpoint
->number
;
5681 status
->tpoint_addr
= tpoint
->address
;
5682 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5683 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5686 if (needs_breakpoint
)
5688 /* Hasn't executed the original instruction yet. Set breakpoint
5689 there, and wait till it's hit, then single-step until exiting
5693 fast_tracepoint_collecting, returning continue-until-break at %s",
5694 paddress (tpoint
->adjusted_insn_addr
));
5696 return 1; /* continue */
5700 /* Just single-step until exiting the jump pad. */
5702 trace_debug ("fast_tracepoint_collecting, returning "
5703 "need-single-step (%s-%s)",
5704 paddress (tpoint
->adjusted_insn_addr
),
5705 paddress (tpoint
->adjusted_insn_addr_end
));
5707 return 2; /* single-step */
5713 #ifdef IN_PROCESS_AGENT
5715 /* The global fast tracepoint collect lock. Points to a collecting_t
5716 object built on the stack by the jump pad, if presently locked;
5717 NULL if it isn't locked. Note that this lock *must* be set while
5718 executing any *function other than the jump pad. See
5719 fast_tracepoint_collecting. */
5720 static collecting_t
* ATTR_USED collecting
;
5722 /* This routine, called from the jump pad (in asm) is designed to be
5723 called from the jump pads of fast tracepoints, thus it is on the
5726 IP_AGENT_EXPORT
void ATTR_USED
5727 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5729 struct fast_tracepoint_ctx ctx
;
5731 /* Don't do anything until the trace run is completely set up. */
5735 ctx
.base
.type
= fast_tracepoint
;
5737 ctx
.regcache_initted
= 0;
5738 /* Wrap the regblock in a register cache (in the stack, we don't
5739 want to malloc here). */
5740 ctx
.regspace
= alloca (register_cache_size ());
5741 if (ctx
.regspace
== NULL
)
5743 trace_debug ("Trace buffer block allocation failed, skipping");
5747 for (ctx
.tpoint
= tpoint
;
5748 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5749 ctx
.tpoint
= ctx
.tpoint
->next
)
5751 if (!ctx
.tpoint
->enabled
)
5754 /* Multiple tracepoints of different types, such as fast tracepoint and
5755 static tracepoint, can be set at the same address. */
5756 if (ctx
.tpoint
->type
!= tpoint
->type
)
5759 /* Test the condition if present, and collect if true. */
5760 if (ctx
.tpoint
->cond
== NULL
5761 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5764 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5765 ctx
.tpoint
->address
, ctx
.tpoint
);
5767 /* Note that this will cause original insns to be written back
5768 to where we jumped from, but that's OK because we're jumping
5769 back to the next whole instruction. This will go badly if
5770 instruction restoration is not atomic though. */
5771 if (stopping_tracepoint
5772 || trace_buffer_is_full
5773 || expr_eval_result
!= expr_eval_no_error
)
5781 /* If there was a condition and it evaluated to false, the only
5782 way we would stop tracing is if there was an error during
5783 condition expression evaluation. */
5784 if (expr_eval_result
!= expr_eval_no_error
)
5795 #ifndef IN_PROCESS_AGENT
5798 get_raw_reg_func_addr (void)
5800 return ipa_sym_addrs
.addr_get_raw_reg
;
5804 get_get_tsv_func_addr (void)
5806 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5810 get_set_tsv_func_addr (void)
5812 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5816 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5817 CORE_ADDR
*jump_entry
)
5819 CORE_ADDR entry_point
= *jump_entry
;
5820 enum eval_result_type err
;
5822 trace_debug ("Starting condition compilation for tracepoint %d\n",
5825 /* Initialize the global pointer to the code being built. */
5826 current_insn_ptr
= *jump_entry
;
5830 err
= compile_bytecodes (tpoint
->cond
);
5832 if (err
== expr_eval_no_error
)
5836 /* Record the beginning of the compiled code. */
5837 tpoint
->compiled_cond
= entry_point
;
5839 trace_debug ("Condition compilation for tracepoint %d complete\n",
5844 /* Leave the unfinished code in situ, but don't point to it. */
5846 tpoint
->compiled_cond
= 0;
5848 trace_debug ("Condition compilation for tracepoint %d failed, "
5850 tpoint
->number
, err
);
5853 /* Update the code pointer passed in. Note that we do this even if
5854 the compile fails, so that we can look at the partial results
5855 instead of letting them be overwritten. */
5856 *jump_entry
= current_insn_ptr
;
5858 /* Leave a gap, to aid dump decipherment. */
5862 /* We'll need to adjust these when we consider bi-arch setups, and big
5866 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5868 return write_inferior_memory (where
,
5869 (unsigned char *) &ptr
, sizeof (void *));
5872 /* The base pointer of the IPA's heap. This is the only memory the
5873 IPA is allowed to use. The IPA should _not_ call the inferior's
5874 `malloc' during operation. That'd be slow, and, most importantly,
5875 it may not be safe. We may be collecting a tracepoint in a signal
5876 handler, for example. */
5877 static CORE_ADDR target_tp_heap
;
5879 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5883 target_malloc (ULONGEST size
)
5887 if (target_tp_heap
== 0)
5889 /* We have the pointer *address*, need what it points to. */
5890 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5892 fatal ("could get target heap head pointer");
5895 ptr
= target_tp_heap
;
5896 target_tp_heap
+= size
;
5898 /* Pad to 8-byte alignment. */
5899 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5905 download_agent_expr (struct agent_expr
*expr
)
5907 CORE_ADDR expr_addr
;
5908 CORE_ADDR expr_bytes
;
5910 expr_addr
= target_malloc (sizeof (*expr
));
5911 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5913 expr_bytes
= target_malloc (expr
->length
);
5914 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5916 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5921 /* Align V up to N bits. */
5922 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5924 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5927 download_tracepoint_1 (struct tracepoint
*tpoint
)
5929 struct tracepoint target_tracepoint
;
5930 CORE_ADDR tpptr
= 0;
5932 gdb_assert (tpoint
->type
== fast_tracepoint
5933 || tpoint
->type
== static_tracepoint
);
5935 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5937 CORE_ADDR jentry
, jump_entry
;
5939 jentry
= jump_entry
= get_jump_space_head ();
5941 if (tpoint
->cond
!= NULL
)
5943 /* Pad to 8-byte alignment. (needed?) */
5944 /* Actually this should be left for the target to
5946 jentry
= UALIGN (jentry
, 8);
5948 compile_tracepoint_condition (tpoint
, &jentry
);
5951 /* Pad to 8-byte alignment. */
5952 jentry
= UALIGN (jentry
, 8);
5953 claim_jump_space (jentry
- jump_entry
);
5956 target_tracepoint
= *tpoint
;
5958 tpptr
= target_malloc (sizeof (*tpoint
));
5959 tpoint
->obj_addr_on_target
= tpptr
;
5961 /* Write the whole object. We'll fix up its pointers in a bit.
5962 Assume no next for now. This is fixed up above on the next
5963 iteration, if there's any. */
5964 target_tracepoint
.next
= NULL
;
5965 /* Need to clear this here too, since we're downloading the
5966 tracepoints before clearing our own copy. */
5967 target_tracepoint
.hit_count
= 0;
5969 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5970 sizeof (target_tracepoint
));
5973 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5975 download_agent_expr (tpoint
->cond
));
5977 if (tpoint
->numactions
)
5980 CORE_ADDR actions_array
;
5982 /* The pointers array. */
5984 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
5985 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5989 /* Now for each pointer, download the action. */
5990 for (i
= 0; i
< tpoint
->numactions
; i
++)
5992 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5993 CORE_ADDR ipa_action
= action
->ops
->download (action
);
5995 if (ipa_action
!= 0)
5996 write_inferior_data_ptr
5997 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
6003 #define IPA_PROTO_FAST_TRACE_FLAG 0
6004 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
6005 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
6006 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
6007 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
6009 /* Send a command to agent to download and install tracepoint TPOINT. */
6012 tracepoint_send_agent (struct tracepoint
*tpoint
)
6014 char buf
[IPA_CMD_BUF_SIZE
];
6019 strcpy (p
, "FastTrace:");
6022 COPY_FIELD_TO_BUF (p
, tpoint
, number
);
6023 COPY_FIELD_TO_BUF (p
, tpoint
, address
);
6024 COPY_FIELD_TO_BUF (p
, tpoint
, type
);
6025 COPY_FIELD_TO_BUF (p
, tpoint
, enabled
);
6026 COPY_FIELD_TO_BUF (p
, tpoint
, step_count
);
6027 COPY_FIELD_TO_BUF (p
, tpoint
, pass_count
);
6028 COPY_FIELD_TO_BUF (p
, tpoint
, numactions
);
6029 COPY_FIELD_TO_BUF (p
, tpoint
, hit_count
);
6030 COPY_FIELD_TO_BUF (p
, tpoint
, traceframe_usage
);
6031 COPY_FIELD_TO_BUF (p
, tpoint
, compiled_cond
);
6032 COPY_FIELD_TO_BUF (p
, tpoint
, orig_size
);
6035 p
= agent_expr_send (p
, tpoint
->cond
);
6037 /* tracepoint_action */
6038 for (i
= 0; i
< tpoint
->numactions
; i
++)
6040 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6042 p
[0] = action
->type
;
6043 p
= action
->ops
->send (&p
[1], action
);
6046 get_jump_space_head ();
6047 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6048 agent can use jump pad from it. */
6049 if (tpoint
->type
== fast_tracepoint
)
6051 memcpy (p
, &gdb_jump_pad_head
, 8);
6055 ret
= run_inferior_command (buf
, (int) (ptrdiff_t) (p
- buf
));
6059 if (strncmp (buf
, "OK", 2) != 0)
6062 /* The value of tracepoint's target address is stored in BUF. */
6063 memcpy (&tpoint
->obj_addr_on_target
,
6064 &buf
[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET
], 8);
6066 if (tpoint
->type
== fast_tracepoint
)
6069 = (unsigned char *) &buf
[IPA_PROTO_FAST_TRACE_FJUMP_INSN
];
6072 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6073 (unsigned int) tpoint
->obj_addr_on_target
,
6074 (unsigned int) gdb_jump_pad_head
);
6076 memcpy (&gdb_jump_pad_head
, &buf
[IPA_PROTO_FAST_TRACE_JUMP_PAD
], 8);
6078 /* This has been done in agent. We should also set up record for it. */
6079 memcpy (&fjump_size
, &buf
[IPA_PROTO_FAST_TRACE_FJUMP_SIZE
], 4);
6082 = set_fast_tracepoint_jump (tpoint
->address
, insn
, fjump_size
);
6089 download_tracepoint (struct tracepoint
*tpoint
)
6091 struct tracepoint
*tp
, *tp_prev
;
6093 if (tpoint
->type
!= fast_tracepoint
6094 && tpoint
->type
!= static_tracepoint
)
6097 download_tracepoint_1 (tpoint
);
6099 /* Find the previous entry of TPOINT, which is fast tracepoint or
6100 static tracepoint. */
6102 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6104 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6110 CORE_ADDR tp_prev_target_next_addr
;
6112 /* Insert TPOINT after TP_PREV in IPA. */
6113 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6114 + offsetof (struct tracepoint
, next
),
6115 &tp_prev_target_next_addr
))
6116 fatal ("error reading `tp_prev->next'");
6118 /* tpoint->next = tp_prev->next */
6119 write_inferior_data_ptr (tpoint
->obj_addr_on_target
6120 + offsetof (struct tracepoint
, next
),
6121 tp_prev_target_next_addr
);
6122 /* tp_prev->next = tpoint */
6123 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
6124 + offsetof (struct tracepoint
, next
),
6125 tpoint
->obj_addr_on_target
);
6128 /* First object in list, set the head pointer in the
6130 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
6131 tpoint
->obj_addr_on_target
);
6136 download_trace_state_variables (void)
6138 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6139 struct trace_state_variable
*tsv
;
6141 /* Start out empty. */
6142 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6144 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6146 struct trace_state_variable target_tsv
;
6148 /* TSV's with a getter have been initialized equally in both the
6149 inferior and GDBserver. Skip them. */
6150 if (tsv
->getter
!= NULL
)
6156 ptr
= target_malloc (sizeof (*tsv
));
6158 if (tsv
== trace_state_variables
)
6160 /* First object in list, set the head pointer in the
6163 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6168 write_inferior_data_ptr (prev_ptr
6169 + offsetof (struct trace_state_variable
,
6174 /* Write the whole object. We'll fix up its pointers in a bit.
6175 Assume no next, fixup when needed. */
6176 target_tsv
.next
= NULL
;
6178 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6179 sizeof (target_tsv
));
6181 if (tsv
->name
!= NULL
)
6183 size_t size
= strlen (tsv
->name
) + 1;
6184 CORE_ADDR name_addr
= target_malloc (size
);
6185 write_inferior_memory (name_addr
,
6186 (unsigned char *) tsv
->name
, size
);
6187 write_inferior_data_ptr (ptr
6188 + offsetof (struct trace_state_variable
,
6193 if (tsv
->getter
!= NULL
)
6195 fatal ("what to do with these?");
6201 /* Fixup the next pointer in the last item in the list. */
6202 write_inferior_data_ptr (prev_ptr
6203 + offsetof (struct trace_state_variable
,
6208 /* Upload complete trace frames out of the IP Agent's trace buffer
6209 into GDBserver's trace buffer. This always uploads either all or
6210 no trace frames. This is the counter part of
6211 `trace_alloc_trace_buffer'. See its description of the atomic
6212 synching mechanism. */
6215 upload_fast_traceframes (void)
6217 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6218 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6220 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6221 unsigned int curr_tbctrl_idx
;
6222 unsigned int ipa_trace_buffer_ctrl_curr
;
6223 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6224 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6225 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6226 CORE_ADDR ipa_trace_buffer_lo
;
6227 CORE_ADDR ipa_trace_buffer_hi
;
6229 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6230 &ipa_traceframe_read_count_racy
))
6232 /* This will happen in most targets if the current thread is
6237 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6238 &ipa_traceframe_write_count_racy
))
6241 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6242 ipa_traceframe_write_count_racy
6243 - ipa_traceframe_read_count_racy
,
6244 ipa_traceframe_write_count_racy
,
6245 ipa_traceframe_read_count_racy
);
6247 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6250 about_to_request_buffer_space_bkpt
6251 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6254 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6255 &ipa_trace_buffer_ctrl_curr
))
6258 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6260 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6263 unsigned int prev
, counter
;
6265 /* Update the token, with new counters, and the GDBserver stamp
6266 bit. Alway reuse the current TBC index. */
6267 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6268 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6270 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6276 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6277 ipa_trace_buffer_ctrl_curr
))
6280 trace_debug ("Lib: Committed %08x -> %08x",
6281 ipa_trace_buffer_ctrl_curr_old
,
6282 ipa_trace_buffer_ctrl_curr
);
6284 /* Re-read these, now that we've installed the
6285 `about_to_request_buffer_space' breakpoint/lock. A thread could
6286 have finished a traceframe between the last read of these
6287 counters and setting the breakpoint above. If we start
6288 uploading, we never want to leave this function with
6289 traceframe_read_count != 0, otherwise, GDBserver could end up
6290 incrementing the counter tokens more than once (due to event loop
6291 nesting), which would break the IP agent's "effective" detection
6292 (see trace_alloc_trace_buffer). */
6293 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6294 &ipa_traceframe_read_count
))
6296 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6297 &ipa_traceframe_write_count
))
6302 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6303 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6304 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6306 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6307 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6308 trace_debug ("note that ipa_traceframe_count's parts changed");
6311 /* Get the address of the current TBC object (the IP agent has an
6312 array of 3 such objects). The index is stored in the TBC
6314 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6315 ipa_trace_buffer_ctrl_addr
6316 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6318 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6319 (unsigned char *) &ipa_trace_buffer_ctrl
,
6320 sizeof (struct ipa_trace_buffer_control
)))
6323 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6324 &ipa_trace_buffer_lo
))
6326 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6327 &ipa_trace_buffer_hi
))
6330 /* Offsets are easier to grok for debugging than raw addresses,
6331 especially for the small trace buffer sizes that are useful for
6333 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6334 "endfree=%d wrap=%d hi=%d",
6336 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6337 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6338 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6339 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6340 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6342 /* Note that the IPA's buffer is always circular. */
6344 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6346 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6347 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6349 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6350 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6351 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6352 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6355 tf
= IPA_FIRST_TRACEFRAME ();
6357 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6359 struct tracepoint
*tpoint
;
6360 struct traceframe
*tframe
;
6361 unsigned char *block
;
6362 struct traceframe ipa_tframe
;
6364 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6365 offsetof (struct traceframe
, data
)))
6366 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6368 if (ipa_tframe
.tpnum
== 0)
6369 fatal ("Uploading: No (more) fast traceframes, but "
6370 "ipa_traceframe_count == %u??\n",
6371 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6373 /* Note that this will be incorrect for multi-location
6375 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6377 tframe
= add_traceframe (tpoint
);
6380 trace_buffer_is_full
= 1;
6381 trace_debug ("Uploading: trace buffer is full");
6385 /* Copy the whole set of blocks in one go for now. FIXME:
6386 split this in smaller blocks. */
6387 block
= add_traceframe_block (tframe
, tpoint
,
6388 ipa_tframe
.data_size
);
6391 if (read_inferior_memory (tf
6392 + offsetof (struct traceframe
, data
),
6393 block
, ipa_tframe
.data_size
))
6394 error ("Uploading: Couldn't read traceframe data at %s\n",
6395 paddress (tf
+ offsetof (struct traceframe
, data
)));
6398 trace_debug ("Uploading: traceframe didn't fit");
6399 finish_traceframe (tframe
);
6402 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6404 /* If we freed the traceframe that wrapped around, go back
6405 to the non-wrap case. */
6406 if (tf
< ipa_trace_buffer_ctrl
.start
)
6408 trace_debug ("Lib: Discarding past the wraparound");
6409 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6411 ipa_trace_buffer_ctrl
.start
= tf
;
6412 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6413 ++ipa_traceframe_read_count
;
6415 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6416 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6418 trace_debug ("Lib: buffer is fully empty. "
6419 "Trace buffer [%d] start=%d free=%d endfree=%d",
6421 (int) (ipa_trace_buffer_ctrl
.start
6422 - ipa_trace_buffer_lo
),
6423 (int) (ipa_trace_buffer_ctrl
.free
6424 - ipa_trace_buffer_lo
),
6425 (int) (ipa_trace_buffer_ctrl
.end_free
6426 - ipa_trace_buffer_lo
));
6428 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6429 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6430 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6431 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6434 trace_debug ("Uploaded a traceframe\n"
6435 "Lib: Trace buffer [%d] start=%d free=%d "
6436 "endfree=%d wrap=%d hi=%d",
6438 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6439 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6440 (int) (ipa_trace_buffer_ctrl
.end_free
6441 - ipa_trace_buffer_lo
),
6442 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6443 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6446 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6447 (unsigned char *) &ipa_trace_buffer_ctrl
,
6448 sizeof (struct ipa_trace_buffer_control
)))
6451 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6452 ipa_traceframe_read_count
);
6454 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6457 cancel_breakpoints ();
6459 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6460 about_to_request_buffer_space_bkpt
= NULL
;
6464 if (trace_buffer_is_full
)
6469 #ifdef IN_PROCESS_AGENT
6471 IP_AGENT_EXPORT
int ust_loaded
;
6472 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6476 /* Static tracepoints. */
6478 /* UST puts a "struct tracepoint" in the global namespace, which
6479 conflicts with our tracepoint. Arguably, being a library, it
6480 shouldn't take ownership of such a generic name. We work around it
6482 #define tracepoint ust_tracepoint
6483 #include <ust/ust.h>
6486 extern int serialize_to_text (char *outbuf
, int bufsize
,
6487 const char *fmt
, va_list ap
);
6489 #define GDB_PROBE_NAME "gdb"
6491 /* We dynamically search for the UST symbols instead of linking them
6492 in. This lets the user decide if the application uses static
6493 tracepoints, instead of always pulling libust.so in. This vector
6494 holds pointers to all functions we care about. */
6498 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6499 const char *fmt
, va_list ap
);
6501 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6502 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6504 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6506 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6509 void (*marker_iter_start
) (struct marker_iter
*iter
);
6510 void (*marker_iter_next
) (struct marker_iter
*iter
);
6511 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6512 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6517 /* Cast through typeof to catch incompatible API changes. Since UST
6518 only builds with gcc, we can freely use gcc extensions here
6520 #define GET_UST_SYM(SYM) \
6523 if (ust_ops.SYM == NULL) \
6524 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6525 if (ust_ops.SYM == NULL) \
6529 #define USTF(SYM) ust_ops.SYM
6531 /* Get pointers to all libust.so functions we care about. */
6536 GET_UST_SYM (serialize_to_text
);
6538 GET_UST_SYM (ltt_probe_register
);
6539 GET_UST_SYM (ltt_probe_unregister
);
6540 GET_UST_SYM (ltt_marker_connect
);
6541 GET_UST_SYM (ltt_marker_disconnect
);
6543 GET_UST_SYM (marker_iter_start
);
6544 GET_UST_SYM (marker_iter_next
);
6545 GET_UST_SYM (marker_iter_stop
);
6546 GET_UST_SYM (marker_iter_reset
);
6552 /* Given an UST marker, return the matching gdb static tracepoint.
6553 The match is done by address. */
6555 static struct tracepoint
*
6556 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6558 struct tracepoint
*tpoint
;
6560 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6562 if (tpoint
->type
!= static_tracepoint
)
6565 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6572 /* The probe function we install on lttng/ust markers. Whenever a
6573 probed ust marker is hit, this function is called. This is similar
6574 to gdb_collect, only for static tracepoints, instead of fast
6578 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6579 struct registers
*regs
, void *call_private
,
6580 const char *fmt
, va_list *args
)
6582 struct tracepoint
*tpoint
;
6583 struct static_tracepoint_ctx ctx
;
6585 /* Don't do anything until the trace run is completely set up. */
6588 trace_debug ("gdb_probe: not tracing\n");
6592 ctx
.base
.type
= static_tracepoint
;
6593 ctx
.regcache_initted
= 0;
6598 /* Wrap the regblock in a register cache (in the stack, we don't
6599 want to malloc here). */
6600 ctx
.regspace
= alloca (register_cache_size ());
6601 if (ctx
.regspace
== NULL
)
6603 trace_debug ("Trace buffer block allocation failed, skipping");
6607 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6610 trace_debug ("gdb_probe: marker not known: "
6611 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6612 mdata
->location
, mdata
->channel
,
6613 mdata
->name
, mdata
->format
);
6617 if (!tpoint
->enabled
)
6619 trace_debug ("gdb_probe: tracepoint disabled");
6623 ctx
.tpoint
= tpoint
;
6625 trace_debug ("gdb_probe: collecting marker: "
6626 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6627 mdata
->location
, mdata
->channel
,
6628 mdata
->name
, mdata
->format
);
6630 /* Test the condition if present, and collect if true. */
6631 if (tpoint
->cond
== NULL
6632 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6635 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6636 tpoint
->address
, tpoint
);
6638 if (stopping_tracepoint
6639 || trace_buffer_is_full
6640 || expr_eval_result
!= expr_eval_no_error
)
6645 /* If there was a condition and it evaluated to false, the only
6646 way we would stop tracing is if there was an error during
6647 condition expression evaluation. */
6648 if (expr_eval_result
!= expr_eval_no_error
)
6653 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6654 static tracepoint string data. This is a string passed to the
6655 tracing library by the user, at the time of the tracepoint marker
6656 call. E.g., in the UST marker call:
6658 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6660 the collected data is "str FOOBAZ".
6664 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6665 struct traceframe
*tframe
)
6667 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6668 unsigned char *bufspace
;
6671 unsigned short blocklen
;
6675 trace_debug ("Wanted to collect static trace data, "
6676 "but there's no static trace data");
6680 va_copy (copy
, *umd
->args
);
6681 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6684 trace_debug ("Want to collect ust data");
6686 /* 'S' + size + string */
6687 bufspace
= add_traceframe_block (tframe
, umd
->tpoint
,
6688 1 + sizeof (blocklen
) + size
+ 1);
6689 if (bufspace
== NULL
)
6691 trace_debug ("Trace buffer block allocation failed, skipping");
6695 /* Identify a static trace data block. */
6698 blocklen
= size
+ 1;
6699 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6701 va_copy (copy
, *umd
->args
);
6702 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6703 size
+ 1, umd
->fmt
, copy
);
6706 trace_debug ("Storing static tracepoint data in regblock: %s",
6707 bufspace
+ 1 + sizeof (blocklen
));
6710 /* The probe to register with lttng/ust. */
6711 static struct ltt_available_probe gdb_ust_probe
=
6718 #endif /* HAVE_UST */
6719 #endif /* IN_PROCESS_AGENT */
6721 #ifndef IN_PROCESS_AGENT
6723 /* Ask the in-process agent to run a command. Since we don't want to
6724 have to handle the IPA hitting breakpoints while running the
6725 command, we pause all threads, remove all breakpoints, and then set
6726 the helper thread re-running. We communicate with the helper
6727 thread by means of direct memory xfering, and a socket for
6731 run_inferior_command (char *cmd
, int len
)
6734 int pid
= ptid_get_pid (current_ptid
);
6736 trace_debug ("run_inferior_command: running: %s", cmd
);
6739 uninsert_all_breakpoints ();
6741 err
= agent_run_command (pid
, (const char *) cmd
, len
);
6743 reinsert_all_breakpoints ();
6749 #else /* !IN_PROCESS_AGENT */
6751 #include <sys/socket.h>
6754 #ifndef UNIX_PATH_MAX
6755 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6758 /* Where we put the socked used for synchronization. */
6759 #define SOCK_DIR P_tmpdir
6761 /* Thread ID of the helper thread. GDBserver reads this to know which
6762 is the help thread. This is an LWP id on Linux. */
6763 int helper_thread_id
;
6766 init_named_socket (const char *name
)
6769 struct sockaddr_un addr
;
6771 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6774 warning ("socket creation failed: %s", strerror (errno
));
6778 addr
.sun_family
= AF_UNIX
;
6780 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6781 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6783 result
= access (name
, F_OK
);
6787 result
= unlink (name
);
6790 warning ("unlink failed: %s", strerror (errno
));
6794 warning ("socket %s already exists; overwriting", name
);
6797 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6800 warning ("bind failed: %s", strerror (errno
));
6805 result
= listen (fd
, 1);
6808 warning ("listen: %s", strerror (errno
));
6816 static char agent_socket_name
[UNIX_PATH_MAX
];
6819 gdb_agent_socket_init (void)
6823 result
= xsnprintf (agent_socket_name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6824 SOCK_DIR
, getpid ());
6825 if (result
>= UNIX_PATH_MAX
)
6827 trace_debug ("string overflow allocating socket name");
6831 fd
= init_named_socket (agent_socket_name
);
6833 warning ("Error initializing named socket (%s) for communication with the "
6834 "ust helper thread. Check that directory exists and that it "
6835 "is writable.", agent_socket_name
);
6842 /* The next marker to be returned on a qTsSTM command. */
6843 static const struct marker
*next_st
;
6845 /* Returns the first known marker. */
6850 struct marker_iter iter
;
6852 USTF(marker_iter_reset
) (&iter
);
6853 USTF(marker_iter_start
) (&iter
);
6858 /* Returns the marker following M. */
6860 const struct marker
*
6861 next_marker (const struct marker
*m
)
6863 struct marker_iter iter
;
6865 USTF(marker_iter_reset
) (&iter
);
6866 USTF(marker_iter_start
) (&iter
);
6868 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6870 if (iter
.marker
== m
)
6872 USTF(marker_iter_next
) (&iter
);
6880 /* Return an hexstr version of the STR C string, fit for sending to
6884 cstr_to_hexstr (const char *str
)
6886 int len
= strlen (str
);
6887 char *hexstr
= xmalloc (len
* 2 + 1);
6888 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6892 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6896 response_ust_marker (char *packet
, const struct marker
*st
)
6898 char *strid
, *format
, *tmp
;
6900 next_st
= next_marker (st
);
6902 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6903 strlen (st
->name
) + 1);
6904 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6906 strid
= cstr_to_hexstr (tmp
);
6909 format
= cstr_to_hexstr (st
->format
);
6911 sprintf (packet
, "m%s:%s:%s",
6912 paddress ((uintptr_t) st
->location
),
6920 /* Return the first static tracepoint, and initialize the state
6921 machine that will iterate through all the static tracepoints. */
6924 cmd_qtfstm (char *packet
)
6926 trace_debug ("Returning first trace state variable definition");
6928 if (first_marker ())
6929 response_ust_marker (packet
, first_marker ());
6931 strcpy (packet
, "l");
6934 /* Return additional trace state variable definitions. */
6937 cmd_qtsstm (char *packet
)
6939 trace_debug ("Returning static tracepoint");
6942 response_ust_marker (packet
, next_st
);
6944 strcpy (packet
, "l");
6947 /* Disconnect the GDB probe from a marker at a given address. */
6950 unprobe_marker_at (char *packet
)
6954 struct marker_iter iter
;
6956 p
+= sizeof ("unprobe_marker_at:") - 1;
6958 p
= unpack_varlen_hex (p
, &address
);
6960 USTF(marker_iter_reset
) (&iter
);
6961 USTF(marker_iter_start
) (&iter
);
6962 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6963 if ((uintptr_t ) iter
.marker
->location
== address
)
6967 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
6971 warning ("could not disable marker %s/%s",
6972 iter
.marker
->channel
, iter
.marker
->name
);
6977 /* Connect the GDB probe to a marker at a given address. */
6980 probe_marker_at (char *packet
)
6984 struct marker_iter iter
;
6987 p
+= sizeof ("probe_marker_at:") - 1;
6989 p
= unpack_varlen_hex (p
, &address
);
6991 USTF(marker_iter_reset
) (&iter
);
6993 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6995 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6996 if ((uintptr_t ) m
->location
== address
)
7000 trace_debug ("found marker for address. "
7001 "ltt_marker_connect (marker = %s/%s)",
7002 m
->channel
, m
->name
);
7004 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7006 if (result
&& result
!= -EEXIST
)
7007 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7008 m
->channel
, m
->name
, -result
);
7012 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7013 m
->channel
, m
->name
);
7017 strcpy (packet
, "OK");
7021 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7026 cmd_qtstmat (char *packet
)
7030 struct marker_iter iter
;
7033 p
+= sizeof ("qTSTMat:") - 1;
7035 p
= unpack_varlen_hex (p
, &address
);
7037 USTF(marker_iter_reset
) (&iter
);
7039 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7041 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7042 if ((uintptr_t ) m
->location
== address
)
7044 response_ust_marker (packet
, m
);
7048 strcpy (packet
, "l");
7058 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7061 #endif /* HAVE_UST */
7063 #include <sys/syscall.h>
7067 gdb_agent_remove_socket (void)
7069 unlink (agent_socket_name
);
7072 /* Helper thread of agent. */
7075 gdb_agent_helper_thread (void *arg
)
7079 atexit (gdb_agent_remove_socket
);
7083 listen_fd
= gdb_agent_socket_init ();
7085 if (helper_thread_id
== 0)
7086 helper_thread_id
= syscall (SYS_gettid
);
7088 if (listen_fd
== -1)
7090 warning ("could not create sync socket\n");
7097 struct sockaddr_un sockaddr
;
7103 tmp
= sizeof (sockaddr
);
7107 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7109 /* It seems an ERESTARTSYS can escape out of accept. */
7110 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7114 warning ("Accept returned %d, error: %s\n",
7115 fd
, strerror (errno
));
7121 ret
= read (fd
, buf
, 1);
7122 } while (ret
== -1 && errno
== EINTR
);
7126 warning ("reading socket (fd=%d) failed with %s",
7127 fd
, strerror (errno
));
7134 if (strncmp ("close", cmd_buf
, 5) == 0)
7139 else if (strcmp ("qTfSTM", cmd_buf
) == 0)
7141 cmd_qtfstm (cmd_buf
);
7143 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7145 cmd_qtsstm (cmd_buf
);
7147 else if (strncmp ("unprobe_marker_at:",
7149 sizeof ("unprobe_marker_at:") - 1) == 0)
7151 unprobe_marker_at (cmd_buf
);
7153 else if (strncmp ("probe_marker_at:",
7155 sizeof ("probe_marker_at:") - 1) == 0)
7157 probe_marker_at (cmd_buf
);
7159 else if (strncmp ("qTSTMat:",
7161 sizeof ("qTSTMat:") - 1) == 0)
7163 cmd_qtstmat (cmd_buf
);
7165 #endif /* HAVE_UST */
7168 /* Fix compiler's warning: ignoring return value of 'write'. */
7169 ret
= write (fd
, buf
, 1);
7175 unlink (agent_socket_name
);
7177 /* Sleep endlessly to wait the whole inferior stops. This
7178 thread can not exit because GDB or GDBserver may still need
7179 'current_inferior' (representing this thread) to access
7180 inferior memory. Otherwise, this thread exits earlier than
7181 other threads, and 'current_inferior' is set to NULL. */
7192 #include <pthread.h>
7194 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
7197 gdb_agent_init (void)
7204 /* We want the helper thread to be as transparent as possible, so
7205 have it inherit an all-signals-blocked mask. */
7207 sigfillset (&new_mask
);
7208 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7210 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7212 res
= pthread_create (&thread
,
7214 gdb_agent_helper_thread
,
7217 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7219 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7221 while (helper_thread_id
== 0)
7229 #include <sys/mman.h>
7232 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7233 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7234 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7235 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
7236 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
7237 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
7239 /* Record the result of getting buffer space for fast tracepoint
7240 trampolines. Any error message is copied, since caller may not be
7241 using persistent storage. */
7244 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7246 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7247 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7249 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7251 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7254 static void __attribute__ ((constructor
))
7255 initialize_tracepoint_ftlib (void)
7257 initialize_tracepoint ();
7262 #endif /* IN_PROCESS_AGENT */
7264 /* Return a timestamp, expressed as microseconds of the usual Unix
7265 time. (As the result is a 64-bit number, it will not overflow any
7269 get_timestamp (void)
7273 if (gettimeofday (&tv
, 0) != 0)
7276 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7280 initialize_tracepoint (void)
7282 /* Start with the default size. */
7283 init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE
);
7285 /* Wire trace state variable 1 to be the timestamp. This will be
7286 uploaded to GDB upon connection and become one of its trace state
7287 variables. (In case you're wondering, if GDB already has a trace
7288 variable numbered 1, it will be renumbered.) */
7289 create_trace_state_variable (1, 0);
7290 set_trace_state_variable_name (1, "trace_timestamp");
7291 set_trace_state_variable_getter (1, get_timestamp
);
7293 #ifdef IN_PROCESS_AGENT
7298 pagesize
= sysconf (_SC_PAGE_SIZE
);
7302 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7304 #define SCRATCH_BUFFER_NPAGES 20
7306 /* Allocate scratch buffer aligned on a page boundary, at a low
7307 address (close to the main executable's code). */
7308 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7310 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7311 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7312 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7314 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7320 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7323 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7326 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7328 /* It's not a fatal error for something to go wrong with trampoline
7329 buffer setup, but it can be mysterious, so create a channel to
7330 report back on what went wrong, using a fixed size since we may
7331 not be able to allocate space later when the problem occurs. */
7332 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7334 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7336 initialize_low_tracepoint ();