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;
2357 trace_debug ("Initializing the trace");
2359 clear_installed_tracepoints ();
2360 clear_readonly_regions ();
2363 last_tracepoint
= NULL
;
2365 /* Clear out any leftover trace state variables. Ones with target
2366 defined getters should be kept however. */
2368 tsv
= trace_state_variables
;
2371 trace_debug ("Looking at var %d", tsv
->number
);
2372 if (tsv
->getter
== NULL
)
2378 trace_state_variables
= next
;
2379 trace_debug ("Deleting var %d", tsv
->number
);
2390 clear_trace_buffer ();
2391 clear_inferior_trace_buffer ();
2396 /* Unprobe the UST marker at ADDRESS. */
2399 unprobe_marker_at (CORE_ADDR address
)
2401 char cmd
[IPA_CMD_BUF_SIZE
];
2403 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2404 run_inferior_command (cmd
, strlen (cmd
) + 1);
2407 /* Restore the program to its pre-tracing state. This routine may be called
2408 in error situations, so it needs to be careful about only restoring
2409 from known-valid bits. */
2412 clear_installed_tracepoints (void)
2414 struct tracepoint
*tpoint
;
2415 struct tracepoint
*prev_stpoint
;
2418 cancel_breakpoints ();
2420 prev_stpoint
= NULL
;
2422 /* Restore any bytes overwritten by tracepoints. */
2423 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2425 /* Catch the case where we might try to remove a tracepoint that
2426 was never actually installed. */
2427 if (tpoint
->handle
== NULL
)
2429 trace_debug ("Tracepoint %d at 0x%s was "
2430 "never installed, nothing to clear",
2431 tpoint
->number
, paddress (tpoint
->address
));
2435 switch (tpoint
->type
)
2437 case trap_tracepoint
:
2438 delete_breakpoint (tpoint
->handle
);
2440 case fast_tracepoint
:
2441 delete_fast_tracepoint_jump (tpoint
->handle
);
2443 case static_tracepoint
:
2444 if (prev_stpoint
!= NULL
2445 && prev_stpoint
->address
== tpoint
->address
)
2446 /* Nothing to do. We already unprobed a tracepoint set at
2447 this marker address (and there can only be one probe
2452 unprobe_marker_at (tpoint
->address
);
2453 prev_stpoint
= tpoint
;
2458 tpoint
->handle
= NULL
;
2464 /* Parse a packet that defines a tracepoint. */
2467 cmd_qtdp (char *own_buf
)
2470 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2471 int trail_hyphen
= 0;
2475 struct tracepoint
*tpoint
;
2477 char *packet
= own_buf
;
2479 packet
+= strlen ("QTDP:");
2481 /* A hyphen at the beginning marks a packet specifying actions for a
2482 tracepoint already supplied. */
2489 packet
= unpack_varlen_hex (packet
, &num
);
2490 ++packet
; /* skip a colon */
2491 packet
= unpack_varlen_hex (packet
, &addr
);
2492 ++packet
; /* skip a colon */
2494 /* See if we already have this tracepoint. */
2495 tpoint
= find_tracepoint (num
, addr
);
2499 /* Duplicate tracepoints are never allowed. */
2502 trace_debug ("Tracepoint error: tracepoint %d"
2503 " at 0x%s already exists",
2504 (int) num
, paddress (addr
));
2505 write_enn (own_buf
);
2509 tpoint
= add_tracepoint (num
, addr
);
2511 tpoint
->enabled
= (*packet
== 'E');
2512 ++packet
; /* skip 'E' */
2513 ++packet
; /* skip a colon */
2514 packet
= unpack_varlen_hex (packet
, &count
);
2515 tpoint
->step_count
= count
;
2516 ++packet
; /* skip a colon */
2517 packet
= unpack_varlen_hex (packet
, &count
);
2518 tpoint
->pass_count
= count
;
2519 /* See if we have any of the additional optional fields. */
2520 while (*packet
== ':')
2525 tpoint
->type
= fast_tracepoint
;
2527 packet
= unpack_varlen_hex (packet
, &count
);
2528 tpoint
->orig_size
= count
;
2530 else if (*packet
== 'S')
2532 tpoint
->type
= static_tracepoint
;
2535 else if (*packet
== 'X')
2537 actparm
= (char *) packet
;
2538 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2541 else if (*packet
== '-')
2543 else if (*packet
== '\0')
2546 trace_debug ("Unknown optional tracepoint field");
2551 trace_debug ("Also has actions\n");
2554 trace_debug ("Defined %stracepoint %d at 0x%s, "
2555 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2556 tpoint
->type
== fast_tracepoint
? "fast "
2557 : tpoint
->type
== static_tracepoint
? "static " : "",
2558 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2559 tpoint
->step_count
, tpoint
->pass_count
);
2562 add_tracepoint_action (tpoint
, packet
);
2565 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2566 (int) num
, paddress (addr
));
2567 write_enn (own_buf
);
2571 /* Install tracepoint during tracing only once for each tracepoint location.
2572 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2573 determine the last QTDP packet for one tracepoint location by checking
2574 trailing hyphen in QTDP packet. */
2575 if (tracing
&& !trail_hyphen
)
2577 struct tracepoint
*tp
= NULL
;
2579 /* Pause all threads temporarily while we patch tracepoints. */
2582 /* download_tracepoint will update global `tracepoints'
2583 list, so it is unsafe to leave threads in jump pad. */
2584 stabilize_threads ();
2586 /* Freeze threads. */
2590 if (tpoint
->type
!= trap_tracepoint
)
2592 /* Find another fast or static tracepoint at the same address. */
2593 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2595 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2596 && tp
->number
!= tpoint
->number
)
2600 /* TPOINT is installed at the same address as TP. */
2603 if (tpoint
->type
== fast_tracepoint
)
2604 clone_fast_tracepoint (tpoint
, tp
);
2605 else if (tpoint
->type
== static_tracepoint
)
2606 tpoint
->handle
= (void *) -1;
2610 if (use_agent
&& tpoint
->type
== fast_tracepoint
2611 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
2613 /* Download and install fast tracepoint by agent. */
2614 if (tracepoint_send_agent (tpoint
) == 0)
2618 write_enn (own_buf
);
2619 remove_tracepoint (tpoint
);
2624 download_tracepoint (tpoint
);
2626 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2628 install_tracepoint (tpoint
, own_buf
);
2629 if (strcmp (own_buf
, "OK") != 0)
2630 remove_tracepoint (tpoint
);
2644 cmd_qtdpsrc (char *own_buf
)
2646 ULONGEST num
, addr
, start
, slen
;
2647 struct tracepoint
*tpoint
;
2648 char *packet
= own_buf
;
2649 char *saved
, *srctype
, *src
;
2651 struct source_string
*last
, *newlast
;
2653 packet
+= strlen ("QTDPsrc:");
2655 packet
= unpack_varlen_hex (packet
, &num
);
2656 ++packet
; /* skip a colon */
2657 packet
= unpack_varlen_hex (packet
, &addr
);
2658 ++packet
; /* skip a colon */
2660 /* See if we already have this tracepoint. */
2661 tpoint
= find_tracepoint (num
, addr
);
2665 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2666 (int) num
, paddress (addr
));
2667 write_enn (own_buf
);
2672 packet
= strchr (packet
, ':');
2673 srctype
= xmalloc (packet
- saved
+ 1);
2674 memcpy (srctype
, saved
, packet
- saved
);
2675 srctype
[packet
- saved
] = '\0';
2677 packet
= unpack_varlen_hex (packet
, &start
);
2678 ++packet
; /* skip a colon */
2679 packet
= unpack_varlen_hex (packet
, &slen
);
2680 ++packet
; /* skip a colon */
2681 src
= xmalloc (slen
+ 1);
2682 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2685 newlast
= xmalloc (sizeof (struct source_string
));
2686 newlast
->type
= srctype
;
2688 newlast
->next
= NULL
;
2689 /* Always add a source string to the end of the list;
2690 this keeps sequences of actions/commands in the right
2692 if (tpoint
->source_strings
)
2694 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2696 last
->next
= newlast
;
2699 tpoint
->source_strings
= newlast
;
2705 cmd_qtdv (char *own_buf
)
2707 ULONGEST num
, val
, builtin
;
2710 struct trace_state_variable
*tsv
;
2711 char *packet
= own_buf
;
2713 packet
+= strlen ("QTDV:");
2715 packet
= unpack_varlen_hex (packet
, &num
);
2716 ++packet
; /* skip a colon */
2717 packet
= unpack_varlen_hex (packet
, &val
);
2718 ++packet
; /* skip a colon */
2719 packet
= unpack_varlen_hex (packet
, &builtin
);
2720 ++packet
; /* skip a colon */
2722 nbytes
= strlen (packet
) / 2;
2723 varname
= xmalloc (nbytes
+ 1);
2724 nbytes
= unhexify (varname
, packet
, nbytes
);
2725 varname
[nbytes
] = '\0';
2727 tsv
= create_trace_state_variable (num
, 1);
2728 tsv
->initial_value
= (LONGEST
) val
;
2729 tsv
->name
= varname
;
2731 set_trace_state_variable_value (num
, (LONGEST
) val
);
2737 cmd_qtenable_disable (char *own_buf
, int enable
)
2739 char *packet
= own_buf
;
2741 struct tracepoint
*tp
;
2743 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2744 packet
= unpack_varlen_hex (packet
, &num
);
2745 ++packet
; /* skip a colon */
2746 packet
= unpack_varlen_hex (packet
, &addr
);
2748 tp
= find_tracepoint (num
, addr
);
2752 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2754 trace_debug ("Tracepoint %d at 0x%s is already %s",
2755 (int) num
, paddress (addr
),
2756 enable
? "enabled" : "disabled");
2761 trace_debug ("%s tracepoint %d at 0x%s",
2762 enable
? "Enabling" : "Disabling",
2763 (int) num
, paddress (addr
));
2765 tp
->enabled
= enable
;
2767 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2770 int offset
= offsetof (struct tracepoint
, enabled
);
2771 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2773 ret
= prepare_to_access_memory ();
2776 trace_debug ("Failed to temporarily stop inferior threads");
2777 write_enn (own_buf
);
2781 ret
= write_inferior_integer (obj_addr
, enable
);
2782 done_accessing_memory ();
2786 trace_debug ("Cannot write enabled flag into "
2787 "inferior process memory");
2788 write_enn (own_buf
);
2797 trace_debug ("Tracepoint %d at 0x%s not found",
2798 (int) num
, paddress (addr
));
2799 write_enn (own_buf
);
2804 cmd_qtv (char *own_buf
)
2809 char *packet
= own_buf
;
2811 packet
+= strlen ("qTV:");
2812 unpack_varlen_hex (packet
, &num
);
2814 if (current_traceframe
>= 0)
2816 err
= traceframe_read_tsv ((int) num
, &val
);
2819 strcpy (own_buf
, "U");
2823 /* Only make tsv's be undefined before the first trace run. After a
2824 trace run is over, the user might want to see the last value of
2825 the tsv, and it might not be available in a traceframe. */
2826 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2828 strcpy (own_buf
, "U");
2832 val
= get_trace_state_variable_value (num
);
2834 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2837 /* Clear out the list of readonly regions. */
2840 clear_readonly_regions (void)
2842 struct readonly_region
*roreg
;
2844 while (readonly_regions
)
2846 roreg
= readonly_regions
;
2847 readonly_regions
= readonly_regions
->next
;
2852 /* Parse the collection of address ranges whose contents GDB believes
2853 to be unchanging and so can be read directly from target memory
2854 even while looking at a traceframe. */
2857 cmd_qtro (char *own_buf
)
2859 ULONGEST start
, end
;
2860 struct readonly_region
*roreg
;
2861 char *packet
= own_buf
;
2863 trace_debug ("Want to mark readonly regions");
2865 clear_readonly_regions ();
2867 packet
+= strlen ("QTro");
2869 while (*packet
== ':')
2871 ++packet
; /* skip a colon */
2872 packet
= unpack_varlen_hex (packet
, &start
);
2873 ++packet
; /* skip a comma */
2874 packet
= unpack_varlen_hex (packet
, &end
);
2875 roreg
= xmalloc (sizeof (struct readonly_region
));
2876 roreg
->start
= start
;
2878 roreg
->next
= readonly_regions
;
2879 readonly_regions
= roreg
;
2880 trace_debug ("Added readonly region from 0x%s to 0x%s",
2881 paddress (roreg
->start
), paddress (roreg
->end
));
2887 /* Test to see if the given range is in our list of readonly ranges.
2888 We only test for being entirely within a range, GDB is not going to
2889 send a single memory packet that spans multiple regions. */
2892 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2894 struct readonly_region
*roreg
;
2896 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2897 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2903 /* The maximum size of a jump pad entry. */
2904 static const int max_jump_pad_size
= 0x100;
2906 static CORE_ADDR gdb_jump_pad_head
;
2908 /* Return the address of the next free jump space. */
2911 get_jump_space_head (void)
2913 if (gdb_jump_pad_head
== 0)
2915 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2916 &gdb_jump_pad_head
))
2917 fatal ("error extracting jump_pad_buffer");
2920 return gdb_jump_pad_head
;
2923 /* Reserve USED bytes from the jump space. */
2926 claim_jump_space (ULONGEST used
)
2928 trace_debug ("claim_jump_space reserves %s bytes at %s",
2929 pulongest (used
), paddress (gdb_jump_pad_head
));
2930 gdb_jump_pad_head
+= used
;
2933 static CORE_ADDR trampoline_buffer_head
= 0;
2934 static CORE_ADDR trampoline_buffer_tail
;
2936 /* Reserve USED bytes from the trampoline buffer and return the
2937 address of the start of the reserved space in TRAMPOLINE. Returns
2938 non-zero if the space is successfully claimed. */
2941 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2943 if (!trampoline_buffer_head
)
2945 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2946 &trampoline_buffer_tail
))
2948 fatal ("error extracting trampoline_buffer");
2952 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2953 &trampoline_buffer_head
))
2955 fatal ("error extracting trampoline_buffer_end");
2960 /* Start claiming space from the top of the trampoline space. If
2961 the space is located at the bottom of the virtual address space,
2962 this reduces the possibility that corruption will occur if a null
2963 pointer is used to write to memory. */
2964 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2966 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2971 trampoline_buffer_head
-= used
;
2973 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2974 pulongest (used
), paddress (trampoline_buffer_head
));
2976 *trampoline
= trampoline_buffer_head
;
2980 /* Returns non-zero if there is space allocated for use in trampolines
2981 for fast tracepoints. */
2984 have_fast_tracepoint_trampoline_buffer (char *buf
)
2986 CORE_ADDR trampoline_end
, errbuf
;
2988 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2991 fatal ("error extracting trampoline_buffer_end");
2998 strcpy (buf
, "was claiming");
2999 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
3002 fatal ("error extracting errbuf");
3006 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
3009 return trampoline_end
!= 0;
3012 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3013 the command fails, or 0 otherwise. If the command ran
3014 successfully, but probing the marker failed, ERROUT will be filled
3015 with the error to reply to GDB, and -1 is also returned. This
3016 allows directly passing IPA errors to GDB. */
3019 probe_marker_at (CORE_ADDR address
, char *errout
)
3021 char cmd
[IPA_CMD_BUF_SIZE
];
3024 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
3025 err
= run_inferior_command (cmd
, strlen (cmd
) + 1);
3031 strcpy (errout
, cmd
);
3040 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
3042 to
->jump_pad
= from
->jump_pad
;
3043 to
->jump_pad_end
= from
->jump_pad_end
;
3044 to
->trampoline
= from
->trampoline
;
3045 to
->trampoline_end
= from
->trampoline_end
;
3046 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
3047 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
3048 to
->handle
= from
->handle
;
3050 gdb_assert (from
->handle
);
3051 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
3054 #define MAX_JUMP_SIZE 20
3056 /* Install fast tracepoint. Return 0 if successful, otherwise return
3060 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
3062 CORE_ADDR jentry
, jump_entry
;
3063 CORE_ADDR trampoline
;
3064 ULONGEST trampoline_size
;
3066 /* The jump to the jump pad of the last fast tracepoint
3068 unsigned char fjump
[MAX_JUMP_SIZE
];
3069 ULONGEST fjump_size
;
3071 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
3073 trace_debug ("Requested a fast tracepoint on an instruction "
3074 "that is of less than the minimum length.");
3078 jentry
= jump_entry
= get_jump_space_head ();
3081 trampoline_size
= 0;
3083 /* Install the jump pad. */
3084 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3086 ipa_sym_addrs
.addr_gdb_collect
,
3087 ipa_sym_addrs
.addr_collecting
,
3090 &trampoline
, &trampoline_size
,
3092 &tpoint
->adjusted_insn_addr
,
3093 &tpoint
->adjusted_insn_addr_end
,
3100 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3103 if (tpoint
->handle
!= NULL
)
3105 tpoint
->jump_pad
= jump_entry
;
3106 tpoint
->jump_pad_end
= jentry
;
3107 tpoint
->trampoline
= trampoline
;
3108 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3110 /* Pad to 8-byte alignment. */
3111 jentry
= ((jentry
+ 7) & ~0x7);
3112 claim_jump_space (jentry
- jump_entry
);
3119 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3122 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3124 tpoint
->handle
= NULL
;
3127 if (tpoint
->type
== trap_tracepoint
)
3129 /* Tracepoints are installed as memory breakpoints. Just go
3130 ahead and install the trap. The breakpoints module
3131 handles duplicated breakpoints, and the memory read
3132 routine handles un-patching traps from memory reads. */
3133 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3134 tracepoint_handler
);
3136 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3138 if (!agent_loaded_p ())
3140 trace_debug ("Requested a %s tracepoint, but fast "
3141 "tracepoints aren't supported.",
3142 tpoint
->type
== static_tracepoint
? "static" : "fast");
3143 write_e_ipa_not_loaded (own_buf
);
3146 if (tpoint
->type
== static_tracepoint
3147 && !in_process_agent_supports_ust ())
3149 trace_debug ("Requested a static tracepoint, but static "
3150 "tracepoints are not supported.");
3151 write_e_ust_not_loaded (own_buf
);
3155 if (tpoint
->type
== fast_tracepoint
)
3156 install_fast_tracepoint (tpoint
, own_buf
);
3159 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3160 tpoint
->handle
= (void *) -1;
3165 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3167 if (tpoint
->handle
== NULL
)
3169 if (*own_buf
== '\0')
3170 write_enn (own_buf
);
3176 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3179 cmd_qtstart (char *packet
)
3181 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3182 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3184 trace_debug ("Starting the trace");
3186 /* Pause all threads temporarily while we patch tracepoints. */
3189 /* Get threads out of jump pads. Safe to do here, since this is a
3190 top level command. And, required to do here, since we're
3191 deleting/rewriting jump pads. */
3193 stabilize_threads ();
3195 /* Freeze threads. */
3198 /* Sync the fast tracepoints list in the inferior ftlib. */
3199 if (agent_loaded_p ())
3200 download_trace_state_variables ();
3202 /* No previous fast tpoint yet. */
3203 prev_ftpoint
= NULL
;
3205 /* No previous static tpoint yet. */
3206 prev_stpoint
= NULL
;
3210 /* Start out empty. */
3211 if (agent_loaded_p ())
3212 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
3214 /* Download and install tracepoints. */
3215 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3217 /* Ensure all the hit counts start at zero. */
3218 tpoint
->hit_count
= 0;
3219 tpoint
->traceframe_usage
= 0;
3221 if (tpoint
->type
== trap_tracepoint
)
3223 /* Tracepoints are installed as memory breakpoints. Just go
3224 ahead and install the trap. The breakpoints module
3225 handles duplicated breakpoints, and the memory read
3226 routine handles un-patching traps from memory reads. */
3227 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3228 tracepoint_handler
);
3230 else if (tpoint
->type
== fast_tracepoint
3231 || tpoint
->type
== static_tracepoint
)
3233 if (maybe_write_ipa_not_loaded (packet
))
3235 trace_debug ("Requested a %s tracepoint, but fast "
3236 "tracepoints aren't supported.",
3237 tpoint
->type
== static_tracepoint
3238 ? "static" : "fast");
3242 if (tpoint
->type
== fast_tracepoint
)
3245 = use_agent
&& agent_capability_check (AGENT_CAPA_FAST_TRACE
);
3247 if (prev_ftpoint
!= NULL
3248 && prev_ftpoint
->address
== tpoint
->address
)
3251 tracepoint_send_agent (tpoint
);
3253 download_tracepoint_1 (tpoint
);
3255 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3259 /* Tracepoint is installed successfully? */
3262 /* Download and install fast tracepoint by agent. */
3264 installed
= !tracepoint_send_agent (tpoint
);
3267 download_tracepoint_1 (tpoint
);
3268 installed
= !install_fast_tracepoint (tpoint
, packet
);
3272 prev_ftpoint
= tpoint
;
3277 if (!in_process_agent_supports_ust ())
3279 trace_debug ("Requested a static tracepoint, but static "
3280 "tracepoints are not supported.");
3284 download_tracepoint_1 (tpoint
);
3285 /* Can only probe a given marker once. */
3286 if (prev_stpoint
!= NULL
3287 && prev_stpoint
->address
== tpoint
->address
)
3288 tpoint
->handle
= (void *) -1;
3291 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3293 tpoint
->handle
= (void *) -1;
3295 /* So that we can handle multiple static tracepoints
3296 at the same address easily. */
3297 prev_stpoint
= tpoint
;
3303 tpptr
= tpoint
->obj_addr_on_target
;
3305 if (tpoint
== tracepoints
)
3306 /* First object in list, set the head pointer in the
3308 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3310 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
3315 /* Any failure in the inner loop is sufficient cause to give
3317 if (tpoint
->handle
== NULL
)
3321 /* Any error in tracepoint insertion is unacceptable; better to
3322 address the problem now, than end up with a useless or misleading
3326 clear_installed_tracepoints ();
3327 if (*packet
== '\0')
3333 stopping_tracepoint
= NULL
;
3334 trace_buffer_is_full
= 0;
3335 expr_eval_result
= expr_eval_no_error
;
3336 error_tracepoint
= NULL
;
3337 tracing_start_time
= get_timestamp ();
3339 /* Tracing is now active, hits will now start being logged. */
3342 if (agent_loaded_p ())
3344 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3345 fatal ("Error setting tracing variable in lib");
3347 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3349 fatal ("Error clearing stopping_tracepoint variable in lib");
3351 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3352 fatal ("Error clearing trace_buffer_is_full variable in lib");
3354 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3355 stop_tracing_handler
);
3356 if (stop_tracing_bkpt
== NULL
)
3357 error ("Error setting stop_tracing breakpoint");
3359 flush_trace_buffer_bkpt
3360 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3361 flush_trace_buffer_handler
);
3362 if (flush_trace_buffer_bkpt
== NULL
)
3363 error ("Error setting flush_trace_buffer breakpoint");
3371 /* End a tracing run, filling in a stop reason to report back to GDB,
3372 and removing the tracepoints from the code. */
3379 trace_debug ("Tracing is already off, ignoring");
3383 trace_debug ("Stopping the trace");
3385 /* Pause all threads before removing fast jumps from memory,
3386 breakpoints, and touching IPA state variables (inferior memory).
3387 Some thread may hit the internal tracing breakpoints, or be
3388 collecting this moment, but that's ok, we don't release the
3389 tpoint object's memory or the jump pads here (we only do that
3390 when we're sure we can move all threads out of the jump pads).
3391 We can't now, since we may be getting here due to the inferior
3392 agent calling us. */
3394 /* Since we're removing breakpoints, cancel breakpoint hits,
3395 possibly related to the breakpoints we're about to delete. */
3396 cancel_breakpoints ();
3398 /* Stop logging. Tracepoints can still be hit, but they will not be
3401 if (agent_loaded_p ())
3403 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3404 fatal ("Error clearing tracing variable in lib");
3407 tracing_stop_time
= get_timestamp ();
3408 tracing_stop_reason
= "t???";
3409 tracing_stop_tpnum
= 0;
3410 if (stopping_tracepoint
)
3412 trace_debug ("Stopping the trace because "
3413 "tracepoint %d was hit %" PRIu64
" times",
3414 stopping_tracepoint
->number
,
3415 stopping_tracepoint
->pass_count
);
3416 tracing_stop_reason
= "tpasscount";
3417 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3419 else if (trace_buffer_is_full
)
3421 trace_debug ("Stopping the trace because the trace buffer is full");
3422 tracing_stop_reason
= "tfull";
3424 else if (expr_eval_result
!= expr_eval_no_error
)
3426 trace_debug ("Stopping the trace because of an expression eval error");
3427 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3428 tracing_stop_tpnum
= error_tracepoint
->number
;
3430 #ifndef IN_PROCESS_AGENT
3431 else if (!gdb_connected ())
3433 trace_debug ("Stopping the trace because GDB disconnected");
3434 tracing_stop_reason
= "tdisconnected";
3439 trace_debug ("Stopping the trace because of a tstop command");
3440 tracing_stop_reason
= "tstop";
3443 stopping_tracepoint
= NULL
;
3444 error_tracepoint
= NULL
;
3446 /* Clear out the tracepoints. */
3447 clear_installed_tracepoints ();
3449 if (agent_loaded_p ())
3451 /* Pull in fast tracepoint trace frames from the inferior lib
3452 buffer into our buffer, even if our buffer is already full,
3453 because we want to present the full number of created frames
3454 in addition to what fit in the trace buffer. */
3455 upload_fast_traceframes ();
3458 if (stop_tracing_bkpt
!= NULL
)
3460 delete_breakpoint (stop_tracing_bkpt
);
3461 stop_tracing_bkpt
= NULL
;
3464 if (flush_trace_buffer_bkpt
!= NULL
)
3466 delete_breakpoint (flush_trace_buffer_bkpt
);
3467 flush_trace_buffer_bkpt
= NULL
;
3474 stop_tracing_handler (CORE_ADDR addr
)
3476 trace_debug ("lib hit stop_tracing");
3478 /* Don't actually handle it here. When we stop tracing we remove
3479 breakpoints from the inferior, and that is not allowed in a
3480 breakpoint handler (as the caller is walking the breakpoint
3486 flush_trace_buffer_handler (CORE_ADDR addr
)
3488 trace_debug ("lib hit flush_trace_buffer");
3493 cmd_qtstop (char *packet
)
3500 cmd_qtdisconnected (char *own_buf
)
3503 char *packet
= own_buf
;
3505 packet
+= strlen ("QTDisconnected:");
3507 unpack_varlen_hex (packet
, &setting
);
3511 disconnected_tracing
= setting
;
3515 cmd_qtframe (char *own_buf
)
3517 ULONGEST frame
, pc
, lo
, hi
, num
;
3519 struct traceframe
*tframe
;
3520 char *packet
= own_buf
;
3522 packet
+= strlen ("QTFrame:");
3524 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3526 packet
+= strlen ("pc:");
3527 unpack_varlen_hex (packet
, &pc
);
3528 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3529 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3531 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3533 packet
+= strlen ("range:");
3534 packet
= unpack_varlen_hex (packet
, &lo
);
3536 unpack_varlen_hex (packet
, &hi
);
3537 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3538 paddress (lo
), paddress (hi
));
3539 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3541 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3543 packet
+= strlen ("outside:");
3544 packet
= unpack_varlen_hex (packet
, &lo
);
3546 unpack_varlen_hex (packet
, &hi
);
3547 trace_debug ("Want to find next traceframe "
3548 "outside the range 0x%s to 0x%s",
3549 paddress (lo
), paddress (hi
));
3550 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3552 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3554 packet
+= strlen ("tdp:");
3555 unpack_varlen_hex (packet
, &num
);
3557 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3558 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3562 unpack_varlen_hex (packet
, &frame
);
3563 tfnum
= (int) frame
;
3566 trace_debug ("Want to stop looking at traceframes");
3567 current_traceframe
= -1;
3571 trace_debug ("Want to look at traceframe %d", tfnum
);
3572 tframe
= find_traceframe (tfnum
);
3577 current_traceframe
= tfnum
;
3578 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3581 sprintf (own_buf
, "F-1");
3585 cmd_qtstatus (char *packet
)
3587 char *stop_reason_rsp
= NULL
;
3588 char *buf1
, *buf2
, *buf3
, *str
;
3591 /* Translate the plain text of the notes back into hex for
3594 str
= (tracing_user_name
? tracing_user_name
: "");
3595 slen
= strlen (str
);
3596 buf1
= (char *) alloca (slen
* 2 + 1);
3597 hexify (buf1
, str
, slen
);
3599 str
= (tracing_notes
? tracing_notes
: "");
3600 slen
= strlen (str
);
3601 buf2
= (char *) alloca (slen
* 2 + 1);
3602 hexify (buf2
, str
, slen
);
3604 str
= (tracing_stop_note
? tracing_stop_note
: "");
3605 slen
= strlen (str
);
3606 buf3
= (char *) alloca (slen
* 2 + 1);
3607 hexify (buf3
, str
, slen
);
3609 trace_debug ("Returning trace status as %d, stop reason %s",
3610 tracing
, tracing_stop_reason
);
3612 if (agent_loaded_p ())
3616 upload_fast_traceframes ();
3621 stop_reason_rsp
= (char *) tracing_stop_reason
;
3623 /* The user visible error string in terror needs to be hex encoded.
3624 We leave it as plain string in `tracing_stop_reason' to ease
3626 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3628 const char *result_name
;
3632 result_name
= stop_reason_rsp
+ strlen ("terror:");
3633 hexstr_len
= strlen (result_name
) * 2;
3634 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3635 strcpy (p
, "terror:");
3637 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3640 /* If this was a forced stop, include any stop note that was supplied. */
3641 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3643 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3644 strcpy (stop_reason_rsp
, "tstop:");
3645 strcat (stop_reason_rsp
, buf3
);
3651 "tframes:%x;tcreated:%x;"
3652 "tfree:%x;tsize:%s;"
3655 "starttime:%s;stoptime:%s;"
3656 "username:%s:;notes:%s:",
3658 stop_reason_rsp
, tracing_stop_tpnum
,
3659 traceframe_count
, traceframes_created
,
3660 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3661 circular_trace_buffer
,
3662 disconnected_tracing
,
3663 plongest (tracing_start_time
), plongest (tracing_stop_time
),
3668 cmd_qtp (char *own_buf
)
3671 struct tracepoint
*tpoint
;
3672 char *packet
= own_buf
;
3674 packet
+= strlen ("qTP:");
3676 packet
= unpack_varlen_hex (packet
, &num
);
3677 ++packet
; /* skip a colon */
3678 packet
= unpack_varlen_hex (packet
, &addr
);
3680 /* See if we already have this tracepoint. */
3681 tpoint
= find_tracepoint (num
, addr
);
3685 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3686 (int) num
, paddress (addr
));
3687 write_enn (own_buf
);
3691 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3692 tpoint
->traceframe_usage
);
3695 /* State variables to help return all the tracepoint bits. */
3696 static struct tracepoint
*cur_tpoint
;
3697 static unsigned int cur_action
;
3698 static unsigned int cur_step_action
;
3699 static struct source_string
*cur_source_string
;
3700 static struct trace_state_variable
*cur_tsv
;
3702 /* Compose a response that is an imitation of the syntax by which the
3703 tracepoint was originally downloaded. */
3706 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3710 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3711 paddress (tpoint
->address
),
3712 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3713 tpoint
->pass_count
);
3714 if (tpoint
->type
== fast_tracepoint
)
3715 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3716 else if (tpoint
->type
== static_tracepoint
)
3717 sprintf (packet
+ strlen (packet
), ":S");
3721 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3722 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3723 tpoint
->cond
->length
, buf
);
3728 /* Compose a response that is an imitation of the syntax by which the
3729 tracepoint action was originally downloaded (with the difference
3730 that due to the way we store the actions, this will output a packet
3731 per action, while GDB could have combined more than one action
3735 response_action (char *packet
, struct tracepoint
*tpoint
,
3736 char *taction
, int step
)
3738 sprintf (packet
, "%c%x:%s:%s",
3739 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3743 /* Compose a response that is an imitation of the syntax by which the
3744 tracepoint source piece was originally downloaded. */
3747 response_source (char *packet
,
3748 struct tracepoint
*tpoint
, struct source_string
*src
)
3753 len
= strlen (src
->str
);
3754 buf
= alloca (len
* 2 + 1);
3755 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3757 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3758 tpoint
->number
, paddress (tpoint
->address
),
3759 src
->type
, 0, len
, buf
);
3762 /* Return the first piece of tracepoint definition, and initialize the
3763 state machine that will iterate through all the tracepoint
3767 cmd_qtfp (char *packet
)
3769 trace_debug ("Returning first tracepoint definition piece");
3771 cur_tpoint
= tracepoints
;
3772 cur_action
= cur_step_action
= 0;
3773 cur_source_string
= NULL
;
3776 response_tracepoint (packet
, cur_tpoint
);
3778 strcpy (packet
, "l");
3781 /* Return additional pieces of tracepoint definition. Each action and
3782 stepping action must go into its own packet, because of packet size
3783 limits, and so we use state variables to deliver one piece at a
3787 cmd_qtsp (char *packet
)
3789 trace_debug ("Returning subsequent tracepoint definition piece");
3793 /* This case would normally never occur, but be prepared for
3795 strcpy (packet
, "l");
3797 else if (cur_action
< cur_tpoint
->numactions
)
3799 response_action (packet
, cur_tpoint
,
3800 cur_tpoint
->actions_str
[cur_action
], 0);
3803 else if (cur_step_action
< cur_tpoint
->num_step_actions
)
3805 response_action (packet
, cur_tpoint
,
3806 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3809 else if ((cur_source_string
3810 ? cur_source_string
->next
3811 : cur_tpoint
->source_strings
))
3813 if (cur_source_string
)
3814 cur_source_string
= cur_source_string
->next
;
3816 cur_source_string
= cur_tpoint
->source_strings
;
3817 response_source (packet
, cur_tpoint
, cur_source_string
);
3821 cur_tpoint
= cur_tpoint
->next
;
3822 cur_action
= cur_step_action
= 0;
3823 cur_source_string
= NULL
;
3825 response_tracepoint (packet
, cur_tpoint
);
3827 strcpy (packet
, "l");
3831 /* Compose a response that is an imitation of the syntax by which the
3832 trace state variable was originally downloaded. */
3835 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3837 char *buf
= (char *) "";
3842 namelen
= strlen (tsv
->name
);
3843 buf
= alloca (namelen
* 2 + 1);
3844 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3847 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3848 tsv
->getter
? 1 : 0, buf
);
3851 /* Return the first trace state variable definition, and initialize
3852 the state machine that will iterate through all the tsv bits. */
3855 cmd_qtfv (char *packet
)
3857 trace_debug ("Returning first trace state variable definition");
3859 cur_tsv
= trace_state_variables
;
3862 response_tsv (packet
, cur_tsv
);
3864 strcpy (packet
, "l");
3867 /* Return additional trace state variable definitions. */
3870 cmd_qtsv (char *packet
)
3872 trace_debug ("Returning additional trace state variable definition");
3876 cur_tsv
= cur_tsv
->next
;
3878 response_tsv (packet
, cur_tsv
);
3880 strcpy (packet
, "l");
3883 strcpy (packet
, "l");
3886 /* Return the first static tracepoint marker, and initialize the state
3887 machine that will iterate through all the static tracepoints
3891 cmd_qtfstm (char *packet
)
3893 if (!maybe_write_ipa_ust_not_loaded (packet
))
3894 run_inferior_command (packet
, strlen (packet
) + 1);
3897 /* Return additional static tracepoints markers. */
3900 cmd_qtsstm (char *packet
)
3902 if (!maybe_write_ipa_ust_not_loaded (packet
))
3903 run_inferior_command (packet
, strlen (packet
) + 1);
3906 /* Return the definition of the static tracepoint at a given address.
3907 Result packet is the same as qTsST's. */
3910 cmd_qtstmat (char *packet
)
3912 if (!maybe_write_ipa_ust_not_loaded (packet
))
3913 run_inferior_command (packet
, strlen (packet
) + 1);
3916 /* Sent the agent a command to close it. */
3919 gdb_agent_about_to_close (int pid
)
3921 char buf
[IPA_CMD_BUF_SIZE
];
3923 if (!maybe_write_ipa_not_loaded (buf
))
3925 struct thread_info
*save_inferior
;
3926 struct inferior_list_entry
*inf
= all_threads
.head
;
3928 save_inferior
= current_inferior
;
3930 /* Find a certain thread which belongs to process PID. */
3933 if (ptid_get_pid (inf
->id
) == pid
)
3938 current_inferior
= (struct thread_info
*) inf
;
3940 strcpy (buf
, "close");
3942 run_inferior_command (buf
, strlen (buf
) + 1);
3944 current_inferior
= save_inferior
;
3948 /* Return the minimum instruction size needed for fast tracepoints as a
3949 hexadecimal number. */
3952 cmd_qtminftpilen (char *packet
)
3954 if (current_inferior
== NULL
)
3956 /* Indicate that the minimum length is currently unknown. */
3957 strcpy (packet
, "0");
3961 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3964 /* Respond to qTBuffer packet with a block of raw data from the trace
3965 buffer. GDB may ask for a lot, but we are allowed to reply with
3966 only as much as will fit within packet limits or whatever. */
3969 cmd_qtbuffer (char *own_buf
)
3971 ULONGEST offset
, num
, tot
;
3973 char *packet
= own_buf
;
3975 packet
+= strlen ("qTBuffer:");
3977 packet
= unpack_varlen_hex (packet
, &offset
);
3978 ++packet
; /* skip a comma */
3979 unpack_varlen_hex (packet
, &num
);
3981 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3982 (int) num
, phex_nz (offset
, 0));
3984 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3986 /* If we're right at the end, reply specially that we're done. */
3989 strcpy (own_buf
, "l");
3993 /* Object to any other out-of-bounds request. */
3996 write_enn (own_buf
);
4000 /* Compute the pointer corresponding to the given offset, accounting
4002 tbp
= trace_buffer_start
+ offset
;
4003 if (tbp
>= trace_buffer_wrap
)
4004 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
4006 /* Trim to the remaining bytes if we're close to the end. */
4007 if (num
> tot
- offset
)
4010 /* Trim to available packet size. */
4011 if (num
>= (PBUFSIZ
- 16) / 2 )
4012 num
= (PBUFSIZ
- 16) / 2;
4014 convert_int_to_ascii (tbp
, own_buf
, num
);
4018 cmd_bigqtbuffer_circular (char *own_buf
)
4021 char *packet
= own_buf
;
4023 packet
+= strlen ("QTBuffer:circular:");
4025 unpack_varlen_hex (packet
, &val
);
4026 circular_trace_buffer
= val
;
4027 trace_debug ("Trace buffer is now %s",
4028 circular_trace_buffer
? "circular" : "linear");
4033 cmd_bigqtbuffer_size (char *own_buf
)
4037 char *packet
= own_buf
;
4039 /* Can't change the size during a tracing run. */
4042 write_enn (own_buf
);
4046 packet
+= strlen ("QTBuffer:size:");
4048 /* -1 is sent as literal "-1". */
4049 if (strcmp (packet
, "-1") == 0)
4050 sval
= DEFAULT_TRACE_BUFFER_SIZE
;
4053 unpack_varlen_hex (packet
, &val
);
4054 sval
= (LONGEST
) val
;
4057 init_trace_buffer (sval
);
4058 trace_debug ("Trace buffer is now %s bytes",
4059 plongest (trace_buffer_size
));
4064 cmd_qtnotes (char *own_buf
)
4067 char *saved
, *user
, *notes
, *stopnote
;
4068 char *packet
= own_buf
;
4070 packet
+= strlen ("QTNotes:");
4074 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
4076 packet
+= strlen ("user:");
4078 packet
= strchr (packet
, ';');
4079 nbytes
= (packet
- saved
) / 2;
4080 user
= xmalloc (nbytes
+ 1);
4081 nbytes
= unhexify (user
, saved
, nbytes
);
4082 user
[nbytes
] = '\0';
4083 ++packet
; /* skip the semicolon */
4084 trace_debug ("User is '%s'", user
);
4085 xfree (tracing_user_name
);
4086 tracing_user_name
= user
;
4088 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
4090 packet
+= strlen ("notes:");
4092 packet
= strchr (packet
, ';');
4093 nbytes
= (packet
- saved
) / 2;
4094 notes
= xmalloc (nbytes
+ 1);
4095 nbytes
= unhexify (notes
, saved
, nbytes
);
4096 notes
[nbytes
] = '\0';
4097 ++packet
; /* skip the semicolon */
4098 trace_debug ("Notes is '%s'", notes
);
4099 xfree (tracing_notes
);
4100 tracing_notes
= notes
;
4102 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
4104 packet
+= strlen ("tstop:");
4106 packet
= strchr (packet
, ';');
4107 nbytes
= (packet
- saved
) / 2;
4108 stopnote
= xmalloc (nbytes
+ 1);
4109 nbytes
= unhexify (stopnote
, saved
, nbytes
);
4110 stopnote
[nbytes
] = '\0';
4111 ++packet
; /* skip the semicolon */
4112 trace_debug ("tstop note is '%s'", stopnote
);
4113 xfree (tracing_stop_note
);
4114 tracing_stop_note
= stopnote
;
4124 handle_tracepoint_general_set (char *packet
)
4126 if (strcmp ("QTinit", packet
) == 0)
4128 cmd_qtinit (packet
);
4131 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
4136 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
4138 cmd_qtdpsrc (packet
);
4141 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
4143 cmd_qtenable_disable (packet
, 1);
4146 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
4148 cmd_qtenable_disable (packet
, 0);
4151 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
4156 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
4161 else if (strcmp ("QTStart", packet
) == 0)
4163 cmd_qtstart (packet
);
4166 else if (strcmp ("QTStop", packet
) == 0)
4168 cmd_qtstop (packet
);
4171 else if (strncmp ("QTDisconnected:", packet
,
4172 strlen ("QTDisconnected:")) == 0)
4174 cmd_qtdisconnected (packet
);
4177 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
4179 cmd_qtframe (packet
);
4182 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
4184 cmd_bigqtbuffer_circular (packet
);
4187 else if (strncmp ("QTBuffer:size:", packet
, strlen ("QTBuffer:size:")) == 0)
4189 cmd_bigqtbuffer_size (packet
);
4192 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
4194 cmd_qtnotes (packet
);
4202 handle_tracepoint_query (char *packet
)
4204 if (strcmp ("qTStatus", packet
) == 0)
4206 cmd_qtstatus (packet
);
4209 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4214 else if (strcmp ("qTfP", packet
) == 0)
4219 else if (strcmp ("qTsP", packet
) == 0)
4224 else if (strcmp ("qTfV", packet
) == 0)
4229 else if (strcmp ("qTsV", packet
) == 0)
4234 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4239 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4241 cmd_qtbuffer (packet
);
4244 else if (strcmp ("qTfSTM", packet
) == 0)
4246 cmd_qtfstm (packet
);
4249 else if (strcmp ("qTsSTM", packet
) == 0)
4251 cmd_qtsstm (packet
);
4254 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4256 cmd_qtstmat (packet
);
4259 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4261 cmd_qtminftpilen (packet
);
4269 #ifndef IN_PROCESS_AGENT
4271 /* Call this when thread TINFO has hit the tracepoint defined by
4272 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4273 action. This adds a while-stepping collecting state item to the
4274 threads' collecting state list, so that we can keep track of
4275 multiple simultaneous while-stepping actions being collected by the
4276 same thread. This can happen in cases like:
4278 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4280 ff0003 INSN3 <-- TP2, collect $regs
4281 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4284 Notice that when instruction INSN5 is reached, the while-stepping
4285 actions of both TP1 and TP3 are still being collected, and that TP2
4286 had been collected meanwhile. The whole range of ff0001-ff0005
4287 should be single-stepped, due to at least TP1's while-stepping
4288 action covering the whole range. */
4291 add_while_stepping_state (struct thread_info
*tinfo
,
4292 int tp_number
, CORE_ADDR tp_address
)
4294 struct wstep_state
*wstep
;
4296 wstep
= xmalloc (sizeof (*wstep
));
4297 wstep
->next
= tinfo
->while_stepping
;
4299 wstep
->tp_number
= tp_number
;
4300 wstep
->tp_address
= tp_address
;
4301 wstep
->current_step
= 0;
4303 tinfo
->while_stepping
= wstep
;
4306 /* Release the while-stepping collecting state WSTEP. */
4309 release_while_stepping_state (struct wstep_state
*wstep
)
4314 /* Release all while-stepping collecting states currently associated
4315 with thread TINFO. */
4318 release_while_stepping_state_list (struct thread_info
*tinfo
)
4320 struct wstep_state
*head
;
4322 while (tinfo
->while_stepping
)
4324 head
= tinfo
->while_stepping
;
4325 tinfo
->while_stepping
= head
->next
;
4326 release_while_stepping_state (head
);
4330 /* If TINFO was handling a 'while-stepping' action, the step has
4331 finished, so collect any step data needed, and check if any more
4332 steps are required. Return true if the thread was indeed
4333 collecting tracepoint data, false otherwise. */
4336 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4338 struct tracepoint
*tpoint
;
4339 struct wstep_state
*wstep
;
4340 struct wstep_state
**wstep_link
;
4341 struct trap_tracepoint_ctx ctx
;
4343 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4345 if (agent_loaded_p ())
4346 upload_fast_traceframes ();
4348 /* Check if we were indeed collecting data for one of more
4349 tracepoints with a 'while-stepping' count. */
4350 if (tinfo
->while_stepping
== NULL
)
4355 /* We're not even tracing anymore. Stop this thread from
4357 release_while_stepping_state_list (tinfo
);
4359 /* The thread had stopped due to a single-step request indeed
4360 explained by a tracepoint. */
4364 wstep
= tinfo
->while_stepping
;
4365 wstep_link
= &tinfo
->while_stepping
;
4367 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4368 target_pid_to_str (tinfo
->entry
.id
),
4369 wstep
->tp_number
, paddress (wstep
->tp_address
));
4371 ctx
.base
.type
= trap_tracepoint
;
4372 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4374 while (wstep
!= NULL
)
4376 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4379 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4380 wstep
->tp_number
, paddress (wstep
->tp_address
),
4381 target_pid_to_str (tinfo
->entry
.id
));
4384 *wstep_link
= wstep
->next
;
4385 release_while_stepping_state (wstep
);
4386 wstep
= *wstep_link
;
4390 /* We've just finished one step. */
4391 ++wstep
->current_step
;
4394 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4395 stop_pc
, tpoint
, wstep
->current_step
);
4397 if (wstep
->current_step
>= tpoint
->step_count
)
4399 /* The requested numbers of steps have occurred. */
4400 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4401 target_pid_to_str (tinfo
->entry
.id
),
4402 wstep
->tp_number
, paddress (wstep
->tp_address
));
4404 /* Unlink the wstep. */
4405 *wstep_link
= wstep
->next
;
4406 release_while_stepping_state (wstep
);
4407 wstep
= *wstep_link
;
4409 /* Only check the hit count now, which ensure that we do all
4410 our stepping before stopping the run. */
4411 if (tpoint
->pass_count
> 0
4412 && tpoint
->hit_count
>= tpoint
->pass_count
4413 && stopping_tracepoint
== NULL
)
4414 stopping_tracepoint
= tpoint
;
4418 /* Keep single-stepping until the requested numbers of steps
4420 wstep_link
= &wstep
->next
;
4421 wstep
= *wstep_link
;
4424 if (stopping_tracepoint
4425 || trace_buffer_is_full
4426 || expr_eval_result
!= expr_eval_no_error
)
4436 /* Handle any internal tracing control breakpoint hits. That means,
4437 pull traceframes from the IPA to our buffer, and syncing both
4438 tracing agents when the IPA's tracing stops for some reason. */
4441 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4443 /* Pull in fast tracepoint trace frames from the inferior in-process
4444 agent's buffer into our buffer. */
4446 if (!agent_loaded_p ())
4449 upload_fast_traceframes ();
4451 /* Check if the in-process agent had decided we should stop
4453 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4455 int ipa_trace_buffer_is_full
;
4456 CORE_ADDR ipa_stopping_tracepoint
;
4457 int ipa_expr_eval_result
;
4458 CORE_ADDR ipa_error_tracepoint
;
4460 trace_debug ("lib stopped at stop_tracing");
4462 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4463 &ipa_trace_buffer_is_full
);
4465 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4466 &ipa_stopping_tracepoint
);
4467 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4469 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4470 &ipa_error_tracepoint
);
4471 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4473 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4474 &ipa_expr_eval_result
);
4475 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4477 trace_debug ("lib: trace_buffer_is_full: %d, "
4478 "stopping_tracepoint: %s, "
4479 "ipa_expr_eval_result: %d, "
4480 "error_tracepoint: %s, ",
4481 ipa_trace_buffer_is_full
,
4482 paddress (ipa_stopping_tracepoint
),
4483 ipa_expr_eval_result
,
4484 paddress (ipa_error_tracepoint
));
4488 if (ipa_trace_buffer_is_full
)
4489 trace_debug ("lib stopped due to full buffer.");
4490 if (ipa_stopping_tracepoint
)
4491 trace_debug ("lib stopped due to tpoint");
4492 if (ipa_stopping_tracepoint
)
4493 trace_debug ("lib stopped due to error");
4496 if (ipa_stopping_tracepoint
!= 0)
4499 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4501 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4503 expr_eval_result
= ipa_expr_eval_result
;
4505 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4510 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4512 trace_debug ("lib stopped at flush_trace_buffer");
4519 /* Return true if TINFO just hit a tracepoint. Collect data if
4523 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4525 struct tracepoint
*tpoint
;
4527 struct trap_tracepoint_ctx ctx
;
4529 /* Not tracing, don't handle. */
4533 ctx
.base
.type
= trap_tracepoint
;
4534 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4536 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4538 /* Note that we collect fast tracepoints here as well. We'll
4539 step over the fast tracepoint jump later, which avoids the
4540 double collect. However, we don't collect for static
4541 tracepoints here, because UST markers are compiled in program,
4542 and probes will be executed in program. So static tracepoints
4543 are collected there. */
4544 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4545 && tpoint
->type
!= static_tracepoint
)
4547 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4548 target_pid_to_str (tinfo
->entry
.id
),
4549 tpoint
->number
, paddress (tpoint
->address
));
4551 /* Test the condition if present, and collect if true. */
4553 || (condition_true_at_tracepoint
4554 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4555 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4558 if (stopping_tracepoint
4559 || trace_buffer_is_full
4560 || expr_eval_result
!= expr_eval_no_error
)
4564 /* If the tracepoint had a 'while-stepping' action, then set
4565 the thread to collect this tracepoint on the following
4567 else if (tpoint
->step_count
> 0)
4569 add_while_stepping_state (tinfo
,
4570 tpoint
->number
, tpoint
->address
);
4582 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4583 struct ust_marker_data
;
4584 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4585 struct traceframe
*tframe
);
4588 /* Create a trace frame for the hit of the given tracepoint in the
4592 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4593 struct tracepoint
*tpoint
)
4595 struct traceframe
*tframe
;
4598 /* Only count it as a hit when we actually collect data. */
4599 tpoint
->hit_count
++;
4601 /* If we've exceeded a defined pass count, record the event for
4602 later, and finish the collection for this hit. This test is only
4603 for nonstepping tracepoints, stepping tracepoints test at the end
4604 of their while-stepping loop. */
4605 if (tpoint
->pass_count
> 0
4606 && tpoint
->hit_count
>= tpoint
->pass_count
4607 && tpoint
->step_count
== 0
4608 && stopping_tracepoint
== NULL
)
4609 stopping_tracepoint
= tpoint
;
4611 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4612 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4614 tframe
= add_traceframe (tpoint
);
4618 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4620 #ifndef IN_PROCESS_AGENT
4621 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4622 tpoint
->number
, paddress (tpoint
->address
),
4623 tpoint
->actions_str
[acti
]);
4626 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4627 tpoint
->actions
[acti
]);
4630 finish_traceframe (tframe
);
4633 if (tframe
== NULL
&& tracing
)
4634 trace_buffer_is_full
= 1;
4637 #ifndef IN_PROCESS_AGENT
4640 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4642 struct tracepoint
*tpoint
, int current_step
)
4644 struct traceframe
*tframe
;
4647 trace_debug ("Making new step traceframe for "
4648 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4649 tpoint
->number
, paddress (tpoint
->address
),
4650 current_step
, tpoint
->step_count
,
4653 tframe
= add_traceframe (tpoint
);
4657 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4659 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4660 tpoint
->number
, paddress (tpoint
->address
),
4661 tpoint
->step_actions_str
[acti
]);
4663 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4664 tpoint
->step_actions
[acti
]);
4667 finish_traceframe (tframe
);
4670 if (tframe
== NULL
&& tracing
)
4671 trace_buffer_is_full
= 1;
4676 static struct regcache
*
4677 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4679 struct regcache
*regcache
= NULL
;
4681 #ifdef IN_PROCESS_AGENT
4682 if (ctx
->type
== fast_tracepoint
)
4684 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4685 if (!fctx
->regcache_initted
)
4687 fctx
->regcache_initted
= 1;
4688 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4689 supply_regblock (&fctx
->regcache
, NULL
);
4690 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4692 regcache
= &fctx
->regcache
;
4695 if (ctx
->type
== static_tracepoint
)
4697 struct static_tracepoint_ctx
*sctx
4698 = (struct static_tracepoint_ctx
*) ctx
;
4700 if (!sctx
->regcache_initted
)
4702 sctx
->regcache_initted
= 1;
4703 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4704 supply_regblock (&sctx
->regcache
, NULL
);
4705 /* Pass down the tracepoint address, because REGS doesn't
4706 include the PC, but we know what it must have been. */
4707 supply_static_tracepoint_registers (&sctx
->regcache
,
4708 (const unsigned char *)
4710 sctx
->tpoint
->address
);
4712 regcache
= &sctx
->regcache
;
4716 if (ctx
->type
== trap_tracepoint
)
4718 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4719 regcache
= tctx
->regcache
;
4723 gdb_assert (regcache
!= NULL
);
4729 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4731 struct tracepoint
*tpoint
,
4732 struct traceframe
*tframe
,
4733 struct tracepoint_action
*taction
)
4735 enum eval_result_type err
;
4737 switch (taction
->type
)
4741 struct collect_memory_action
*maction
;
4742 struct eval_agent_expr_context ax_ctx
;
4744 maction
= (struct collect_memory_action
*) taction
;
4745 ax_ctx
.regcache
= NULL
;
4746 ax_ctx
.tframe
= tframe
;
4747 ax_ctx
.tpoint
= tpoint
;
4749 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4750 pulongest (maction
->len
),
4751 paddress (maction
->addr
), maction
->basereg
);
4752 /* (should use basereg) */
4753 agent_mem_read (&ax_ctx
, NULL
, (CORE_ADDR
) maction
->addr
,
4759 unsigned char *regspace
;
4760 struct regcache tregcache
;
4761 struct regcache
*context_regcache
;
4764 trace_debug ("Want to collect registers");
4766 /* Collect all registers for now. */
4767 regspace
= add_traceframe_block (tframe
, tpoint
,
4768 1 + register_cache_size ());
4769 if (regspace
== NULL
)
4771 trace_debug ("Trace buffer block allocation failed, skipping");
4774 /* Identify a register block. */
4777 context_regcache
= get_context_regcache (ctx
);
4779 /* Wrap the regblock in a register cache (in the stack, we
4780 don't want to malloc here). */
4781 init_register_cache (&tregcache
, regspace
+ 1);
4783 /* Copy the register data to the regblock. */
4784 regcache_cpy (&tregcache
, context_regcache
);
4786 #ifndef IN_PROCESS_AGENT
4787 /* On some platforms, trap-based tracepoints will have the PC
4788 pointing to the next instruction after the trap, but we
4789 don't want the user or GDB trying to guess whether the
4790 saved PC needs adjusting; so always record the adjusted
4791 stop_pc. Note that we can't use tpoint->address instead,
4792 since it will be wrong for while-stepping actions. This
4793 adjustment is a nop for fast tracepoints collected from the
4794 in-process lib (but not if GDBserver is collecting one
4795 preemptively), since the PC had already been adjusted to
4796 contain the tracepoint's address by the jump pad. */
4797 trace_debug ("Storing stop pc (0x%s) in regblock",
4798 paddress (stop_pc
));
4800 /* This changes the regblock, not the thread's
4802 regcache_write_pc (&tregcache
, stop_pc
);
4808 struct eval_expr_action
*eaction
;
4809 struct eval_agent_expr_context ax_ctx
;
4811 eaction
= (struct eval_expr_action
*) taction
;
4812 ax_ctx
.regcache
= get_context_regcache (ctx
);
4813 ax_ctx
.tframe
= tframe
;
4814 ax_ctx
.tpoint
= tpoint
;
4816 trace_debug ("Want to evaluate expression");
4818 err
= gdb_eval_agent_expr (&ax_ctx
, eaction
->expr
, NULL
);
4820 if (err
!= expr_eval_no_error
)
4822 record_tracepoint_error (tpoint
, "action expression", err
);
4829 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4830 trace_debug ("Want to collect static trace data");
4831 collect_ust_data_at_tracepoint (ctx
, tframe
);
4833 trace_debug ("warning: collecting static trace data, "
4834 "but static tracepoints are not supported");
4839 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4845 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4846 struct tracepoint
*tpoint
)
4849 enum eval_result_type err
;
4851 /* Presently, gdbserver doesn't run compiled conditions, only the
4852 IPA does. If the program stops at a fast tracepoint's address
4853 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4854 gdbserver preemptively collect the fast tracepoint. Later, on
4855 resume, gdbserver steps over the fast tracepoint like it steps
4856 over breakpoints, so that the IPA doesn't see that fast
4857 tracepoint. This avoids double collects of fast tracepoints in
4858 that stopping scenario. Having gdbserver itself handle the fast
4859 tracepoint gives the user a consistent view of when fast or trap
4860 tracepoints are collected, compared to an alternative where only
4861 trap tracepoints are collected on stop, and fast tracepoints on
4862 resume. When a fast tracepoint is being processed by gdbserver,
4863 it is always the non-compiled condition expression that is
4865 #ifdef IN_PROCESS_AGENT
4866 if (tpoint
->compiled_cond
)
4867 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4871 struct eval_agent_expr_context ax_ctx
;
4873 ax_ctx
.regcache
= get_context_regcache (ctx
);
4874 ax_ctx
.tframe
= NULL
;
4875 ax_ctx
.tpoint
= tpoint
;
4877 err
= gdb_eval_agent_expr (&ax_ctx
, tpoint
->cond
, &value
);
4879 if (err
!= expr_eval_no_error
)
4881 record_tracepoint_error (tpoint
, "condition", err
);
4882 /* The error case must return false. */
4886 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4887 tpoint
->number
, paddress (tpoint
->address
),
4889 return (value
? 1 : 0);
4892 /* Do memory copies for bytecodes. */
4893 /* Do the recording of memory blocks for actions and bytecodes. */
4896 agent_mem_read (struct eval_agent_expr_context
*ctx
,
4897 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4899 unsigned char *mspace
;
4900 ULONGEST remaining
= len
;
4901 unsigned short blocklen
;
4903 /* If a 'to' buffer is specified, use it. */
4906 read_inferior_memory (from
, to
, len
);
4910 /* Otherwise, create a new memory block in the trace buffer. */
4911 while (remaining
> 0)
4915 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4916 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4917 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4920 /* Identify block as a memory block. */
4923 /* Record address and size. */
4924 memcpy (mspace
, &from
, sizeof (from
));
4925 mspace
+= sizeof (from
);
4926 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4927 mspace
+= sizeof (blocklen
);
4928 /* Record the memory block proper. */
4929 read_inferior_memory (from
, mspace
, blocklen
);
4930 trace_debug ("%d bytes recorded", blocklen
);
4931 remaining
-= blocklen
;
4938 agent_mem_read_string (struct eval_agent_expr_context
*ctx
,
4939 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4941 unsigned char *buf
, *mspace
;
4942 ULONGEST remaining
= len
;
4943 unsigned short blocklen
, i
;
4945 /* To save a bit of space, block lengths are 16-bit, so break large
4946 requests into multiple blocks. Bordering on overkill for strings,
4947 but it could happen that someone specifies a large max length. */
4948 while (remaining
> 0)
4952 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4953 /* We want working space to accumulate nonzero bytes, since
4954 traceframes must have a predecided size (otherwise it gets
4955 harder to wrap correctly for the circular case, etc). */
4956 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4957 for (i
= 0; i
< blocklen
; ++i
)
4959 /* Read the string one byte at a time, in case the string is
4960 at the end of a valid memory area - we don't want a
4961 correctly-terminated string to engender segvio
4963 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4968 /* Make sure outer loop stops now too. */
4969 remaining
= blocklen
;
4973 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4974 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4980 /* Identify block as a memory block. */
4983 /* Record address and size. */
4984 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4985 mspace
+= sizeof (from
);
4986 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4987 mspace
+= sizeof (blocklen
);
4988 /* Copy the string contents. */
4989 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
4990 remaining
-= blocklen
;
4997 /* Record the value of a trace state variable. */
5000 agent_tsv_read (struct eval_agent_expr_context
*ctx
, int n
)
5002 unsigned char *vspace
;
5005 vspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
,
5006 1 + sizeof (n
) + sizeof (LONGEST
));
5009 /* Identify block as a variable. */
5011 /* Record variable's number and value. */
5012 memcpy (vspace
+ 1, &n
, sizeof (n
));
5013 val
= get_trace_state_variable_value (n
);
5014 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
5015 trace_debug ("Variable %d recorded", n
);
5019 #ifndef IN_PROCESS_AGENT
5021 /* Callback for traceframe_walk_blocks, used to find a given block
5022 type in a traceframe. */
5025 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
5027 char *wantedp
= data
;
5029 if (*wantedp
== blocktype
)
5035 /* Walk over all traceframe blocks of the traceframe buffer starting
5036 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5037 block found, passing in DATA unmodified. If CALLBACK returns true,
5038 this returns a pointer to where the block is found. Returns NULL
5039 if no callback call returned true, indicating that all blocks have
5042 static unsigned char *
5043 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
5045 int (*callback
) (char blocktype
,
5046 unsigned char *dataptr
,
5050 unsigned char *dataptr
;
5054 trace_debug ("traceframe %d has no data", tfnum
);
5058 /* Iterate through a traceframe's blocks, looking for a block of the
5060 for (dataptr
= database
;
5061 dataptr
< database
+ datasize
;
5065 unsigned short mlen
;
5067 if (dataptr
== trace_buffer_wrap
)
5069 /* Adjust to reflect wrapping part of the frame around to
5071 datasize
= dataptr
- database
;
5072 dataptr
= database
= trace_buffer_lo
;
5075 blocktype
= *dataptr
++;
5077 if ((*callback
) (blocktype
, dataptr
, data
))
5083 /* Skip over the registers block. */
5084 dataptr
+= register_cache_size ();
5087 /* Skip over the memory block. */
5088 dataptr
+= sizeof (CORE_ADDR
);
5089 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5090 dataptr
+= (sizeof (mlen
) + mlen
);
5093 /* Skip over the TSV block. */
5094 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5097 /* Skip over the static trace data block. */
5098 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5099 dataptr
+= (sizeof (mlen
) + mlen
);
5102 trace_debug ("traceframe %d has unknown block type 0x%x",
5111 /* Look for the block of type TYPE_WANTED in the trameframe starting
5112 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5115 static unsigned char *
5116 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5117 int tfnum
, char type_wanted
)
5119 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5120 match_blocktype
, &type_wanted
);
5123 static unsigned char *
5124 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5126 unsigned char *regblock
;
5128 regblock
= traceframe_find_block_type (tframe
->data
,
5132 if (regblock
== NULL
)
5133 trace_debug ("traceframe %d has no register data", tfnum
);
5138 /* Get registers from a traceframe. */
5141 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5143 unsigned char *dataptr
;
5144 struct tracepoint
*tpoint
;
5145 struct traceframe
*tframe
;
5147 tframe
= find_traceframe (tfnum
);
5151 trace_debug ("traceframe %d not found", tfnum
);
5155 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5156 if (dataptr
== NULL
)
5158 /* Mark registers unavailable. */
5159 supply_regblock (regcache
, NULL
);
5161 /* We can generally guess at a PC, although this will be
5162 misleading for while-stepping frames and multi-location
5164 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5166 regcache_write_pc (regcache
, tpoint
->address
);
5169 supply_regblock (regcache
, dataptr
);
5175 traceframe_get_pc (struct traceframe
*tframe
)
5177 struct regcache regcache
;
5178 unsigned char *dataptr
;
5180 dataptr
= traceframe_find_regblock (tframe
, -1);
5181 if (dataptr
== NULL
)
5184 init_register_cache (®cache
, dataptr
);
5185 return regcache_read_pc (®cache
);
5188 /* Read a requested block of memory from a trace frame. */
5191 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5192 unsigned char *buf
, ULONGEST length
,
5195 struct traceframe
*tframe
;
5196 unsigned char *database
, *dataptr
;
5197 unsigned int datasize
;
5199 unsigned short mlen
;
5201 trace_debug ("traceframe_read_mem");
5203 tframe
= find_traceframe (tfnum
);
5207 trace_debug ("traceframe %d not found", tfnum
);
5211 datasize
= tframe
->data_size
;
5212 database
= dataptr
= &tframe
->data
[0];
5214 /* Iterate through a traceframe's blocks, looking for memory. */
5215 while ((dataptr
= traceframe_find_block_type (dataptr
,
5217 - (dataptr
- database
),
5218 tfnum
, 'M')) != NULL
)
5220 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5221 dataptr
+= sizeof (maddr
);
5222 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5223 dataptr
+= sizeof (mlen
);
5224 trace_debug ("traceframe %d has %d bytes at %s",
5225 tfnum
, mlen
, paddress (maddr
));
5227 /* If the block includes the first part of the desired range,
5228 return as much it has; GDB will re-request the remainder,
5229 which might be in a different block of this trace frame. */
5230 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5232 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5236 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5241 /* Skip over this block. */
5245 trace_debug ("traceframe %d has no memory data for the desired region",
5253 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5256 struct traceframe
*tframe
;
5257 unsigned char *database
, *dataptr
;
5258 unsigned int datasize
;
5261 trace_debug ("traceframe_read_tsv");
5263 tfnum
= current_traceframe
;
5267 trace_debug ("no current traceframe");
5271 tframe
= find_traceframe (tfnum
);
5275 trace_debug ("traceframe %d not found", tfnum
);
5279 datasize
= tframe
->data_size
;
5280 database
= dataptr
= &tframe
->data
[0];
5282 /* Iterate through a traceframe's blocks, looking for the tsv. */
5283 while ((dataptr
= traceframe_find_block_type (dataptr
,
5285 - (dataptr
- database
),
5286 tfnum
, 'V')) != NULL
)
5288 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5289 dataptr
+= sizeof (vnum
);
5291 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5293 /* Check that this is the variable we want. */
5296 memcpy (val
, dataptr
, sizeof (*val
));
5300 /* Skip over this block. */
5301 dataptr
+= sizeof (LONGEST
);
5304 trace_debug ("traceframe %d has no data for variable %d",
5309 /* Read a requested block of static tracepoint data from a trace
5313 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5314 unsigned char *buf
, ULONGEST length
,
5317 struct traceframe
*tframe
;
5318 unsigned char *database
, *dataptr
;
5319 unsigned int datasize
;
5320 unsigned short mlen
;
5322 trace_debug ("traceframe_read_sdata");
5324 tframe
= find_traceframe (tfnum
);
5328 trace_debug ("traceframe %d not found", tfnum
);
5332 datasize
= tframe
->data_size
;
5333 database
= &tframe
->data
[0];
5335 /* Iterate through a traceframe's blocks, looking for static
5337 dataptr
= traceframe_find_block_type (database
, datasize
,
5339 if (dataptr
!= NULL
)
5341 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5342 dataptr
+= sizeof (mlen
);
5345 if (offset
+ length
> mlen
)
5346 length
= mlen
- offset
;
5348 memcpy (buf
, dataptr
, length
);
5356 trace_debug ("traceframe %d has no static trace data", tfnum
);
5362 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5363 object. DATA is pointer to a struct buffer holding the
5364 traceframe-info object being built. */
5367 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5369 struct buffer
*buffer
= data
;
5375 unsigned short mlen
;
5378 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5379 dataptr
+= sizeof (maddr
);
5380 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5381 dataptr
+= sizeof (mlen
);
5382 buffer_xml_printf (buffer
,
5383 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5384 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5394 warning ("Unhandled trace block type (%d) '%c ' "
5395 "while building trace frame info.",
5396 blocktype
, blocktype
);
5403 /* Build a traceframe-info object for traceframe number TFNUM into
5407 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5409 struct traceframe
*tframe
;
5411 trace_debug ("traceframe_read_info");
5413 tframe
= find_traceframe (tfnum
);
5417 trace_debug ("traceframe %d not found", tfnum
);
5421 buffer_grow_str (buffer
, "<traceframe-info>\n");
5422 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5423 tfnum
, build_traceframe_info_xml
, buffer
);
5424 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5428 /* Return the first fast tracepoint whose jump pad contains PC. */
5430 static struct tracepoint
*
5431 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5433 struct tracepoint
*tpoint
;
5435 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5436 if (tpoint
->type
== fast_tracepoint
)
5437 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5443 /* Return the first fast tracepoint whose trampoline contains PC. */
5445 static struct tracepoint
*
5446 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5448 struct tracepoint
*tpoint
;
5450 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5452 if (tpoint
->type
== fast_tracepoint
5453 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5460 /* Return GDBserver's tracepoint that matches the IP Agent's
5461 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5464 static struct tracepoint
*
5465 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5467 struct tracepoint
*tpoint
;
5469 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5470 if (tpoint
->type
== fast_tracepoint
)
5471 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5479 /* The type of the object that is used to synchronize fast tracepoint
5482 typedef struct collecting_t
5484 /* The fast tracepoint number currently collecting. */
5487 /* A number that GDBserver can use to identify the thread that is
5488 presently holding the collect lock. This need not (and usually
5489 is not) the thread id, as getting the current thread ID usually
5490 requires a system call, which we want to avoid like the plague.
5491 Usually this is thread's TCB, found in the TLS (pseudo-)
5492 register, which is readable with a single insn on several
5494 uintptr_t thread_area
;
5497 #ifndef IN_PROCESS_AGENT
5500 force_unlock_trace_buffer (void)
5502 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5505 /* Check if the thread identified by THREAD_AREA which is stopped at
5506 STOP_PC, is presently locking the fast tracepoint collection, and
5507 if so, gather some status of said collection. Returns 0 if the
5508 thread isn't collecting or in the jump pad at all. 1, if in the
5509 jump pad (or within gdb_collect) and hasn't executed the adjusted
5510 original insn yet (can set a breakpoint there and run to it). 2,
5511 if presently executing the adjusted original insn --- in which
5512 case, if we want to move the thread out of the jump pad, we need to
5513 single-step it until this function returns 0. */
5516 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5518 struct fast_tpoint_collect_status
*status
)
5520 CORE_ADDR ipa_collecting
;
5521 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5522 CORE_ADDR ipa_gdb_trampoline_buffer
;
5523 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5524 struct tracepoint
*tpoint
;
5525 int needs_breakpoint
;
5527 /* The thread THREAD_AREA is either:
5529 0. not collecting at all, not within the jump pad, or within
5530 gdb_collect or one of its callees.
5532 1. in the jump pad and haven't reached gdb_collect
5534 2. within gdb_collect (out of the jump pad) (collect is set)
5536 3. we're in the jump pad, after gdb_collect having returned,
5537 possibly executing the adjusted insns.
5539 For cases 1 and 3, `collecting' may or not be set. The jump pad
5540 doesn't have any complicated jump logic, so we can tell if the
5541 thread is executing the adjust original insn or not by just
5542 matching STOP_PC with known jump pad addresses. If we it isn't
5543 yet executing the original insn, set a breakpoint there, and let
5544 the thread run to it, so to quickly step over a possible (many
5545 insns) gdb_collect call. Otherwise, or when the breakpoint is
5546 hit, only a few (small number of) insns are left to be executed
5547 in the jump pad. Single-step the thread until it leaves the
5552 needs_breakpoint
= 0;
5553 trace_debug ("fast_tracepoint_collecting");
5555 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5556 &ipa_gdb_jump_pad_buffer
))
5557 fatal ("error extracting `gdb_jump_pad_buffer'");
5558 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5559 &ipa_gdb_jump_pad_buffer_end
))
5560 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5562 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5563 &ipa_gdb_trampoline_buffer
))
5564 fatal ("error extracting `gdb_trampoline_buffer'");
5565 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5566 &ipa_gdb_trampoline_buffer_end
))
5567 fatal ("error extracting `gdb_trampoline_buffer_end'");
5569 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5570 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5572 /* We can tell which tracepoint(s) the thread is collecting by
5573 matching the jump pad address back to the tracepoint. */
5574 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5577 warning ("in jump pad, but no matching tpoint?");
5582 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5584 tpoint
->number
, paddress (tpoint
->address
),
5585 paddress (tpoint
->jump_pad
),
5586 paddress (tpoint
->jump_pad_end
),
5587 paddress (tpoint
->adjusted_insn_addr
),
5588 paddress (tpoint
->adjusted_insn_addr_end
));
5591 /* Definitely in the jump pad. May or may not need
5592 fast-exit-jump-pad breakpoint. */
5593 if (tpoint
->jump_pad
<= stop_pc
5594 && stop_pc
< tpoint
->adjusted_insn_addr
)
5595 needs_breakpoint
= 1;
5597 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5598 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5600 /* We can tell which tracepoint(s) the thread is collecting by
5601 matching the trampoline address back to the tracepoint. */
5602 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5605 warning ("in trampoline, but no matching tpoint?");
5610 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5611 tpoint
->number
, paddress (tpoint
->address
),
5612 paddress (tpoint
->trampoline
),
5613 paddress (tpoint
->trampoline_end
));
5616 /* Have not reached jump pad yet, but treat the trampoline as a
5617 part of the jump pad that is before the adjusted original
5619 needs_breakpoint
= 1;
5623 collecting_t ipa_collecting_obj
;
5625 /* If `collecting' is set/locked, then the THREAD_AREA thread
5626 may or not be the one holding the lock. We have to read the
5627 lock to find out. */
5629 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5632 trace_debug ("fast_tracepoint_collecting:"
5633 " failed reading 'collecting' in the inferior");
5637 if (!ipa_collecting
)
5639 trace_debug ("fast_tracepoint_collecting: not collecting"
5640 " (and nobody is).");
5644 /* Some thread is collecting. Check which. */
5645 if (read_inferior_memory (ipa_collecting
,
5646 (unsigned char *) &ipa_collecting_obj
,
5647 sizeof (ipa_collecting_obj
)) != 0)
5650 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5652 trace_debug ("fast_tracepoint_collecting: not collecting "
5653 "(another thread is)");
5658 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5661 warning ("fast_tracepoint_collecting: collecting, "
5662 "but tpoint %s not found?",
5663 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5667 /* The thread is within `gdb_collect', skip over the rest of
5668 fast tracepoint collection quickly using a breakpoint. */
5669 needs_breakpoint
= 1;
5672 /* The caller wants a bit of status detail. */
5675 status
->tpoint_num
= tpoint
->number
;
5676 status
->tpoint_addr
= tpoint
->address
;
5677 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5678 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5681 if (needs_breakpoint
)
5683 /* Hasn't executed the original instruction yet. Set breakpoint
5684 there, and wait till it's hit, then single-step until exiting
5688 fast_tracepoint_collecting, returning continue-until-break at %s",
5689 paddress (tpoint
->adjusted_insn_addr
));
5691 return 1; /* continue */
5695 /* Just single-step until exiting the jump pad. */
5697 trace_debug ("fast_tracepoint_collecting, returning "
5698 "need-single-step (%s-%s)",
5699 paddress (tpoint
->adjusted_insn_addr
),
5700 paddress (tpoint
->adjusted_insn_addr_end
));
5702 return 2; /* single-step */
5708 #ifdef IN_PROCESS_AGENT
5710 /* The global fast tracepoint collect lock. Points to a collecting_t
5711 object built on the stack by the jump pad, if presently locked;
5712 NULL if it isn't locked. Note that this lock *must* be set while
5713 executing any *function other than the jump pad. See
5714 fast_tracepoint_collecting. */
5715 static collecting_t
* ATTR_USED collecting
;
5717 /* This routine, called from the jump pad (in asm) is designed to be
5718 called from the jump pads of fast tracepoints, thus it is on the
5721 IP_AGENT_EXPORT
void ATTR_USED
5722 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5724 struct fast_tracepoint_ctx ctx
;
5726 /* Don't do anything until the trace run is completely set up. */
5730 ctx
.base
.type
= fast_tracepoint
;
5732 ctx
.regcache_initted
= 0;
5733 /* Wrap the regblock in a register cache (in the stack, we don't
5734 want to malloc here). */
5735 ctx
.regspace
= alloca (register_cache_size ());
5736 if (ctx
.regspace
== NULL
)
5738 trace_debug ("Trace buffer block allocation failed, skipping");
5742 for (ctx
.tpoint
= tpoint
;
5743 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5744 ctx
.tpoint
= ctx
.tpoint
->next
)
5746 if (!ctx
.tpoint
->enabled
)
5749 /* Multiple tracepoints of different types, such as fast tracepoint and
5750 static tracepoint, can be set at the same address. */
5751 if (ctx
.tpoint
->type
!= tpoint
->type
)
5754 /* Test the condition if present, and collect if true. */
5755 if (ctx
.tpoint
->cond
== NULL
5756 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5759 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5760 ctx
.tpoint
->address
, ctx
.tpoint
);
5762 /* Note that this will cause original insns to be written back
5763 to where we jumped from, but that's OK because we're jumping
5764 back to the next whole instruction. This will go badly if
5765 instruction restoration is not atomic though. */
5766 if (stopping_tracepoint
5767 || trace_buffer_is_full
5768 || expr_eval_result
!= expr_eval_no_error
)
5776 /* If there was a condition and it evaluated to false, the only
5777 way we would stop tracing is if there was an error during
5778 condition expression evaluation. */
5779 if (expr_eval_result
!= expr_eval_no_error
)
5790 #ifndef IN_PROCESS_AGENT
5793 get_raw_reg_func_addr (void)
5795 return ipa_sym_addrs
.addr_get_raw_reg
;
5799 get_get_tsv_func_addr (void)
5801 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5805 get_set_tsv_func_addr (void)
5807 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5811 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5812 CORE_ADDR
*jump_entry
)
5814 CORE_ADDR entry_point
= *jump_entry
;
5815 enum eval_result_type err
;
5817 trace_debug ("Starting condition compilation for tracepoint %d\n",
5820 /* Initialize the global pointer to the code being built. */
5821 current_insn_ptr
= *jump_entry
;
5825 err
= compile_bytecodes (tpoint
->cond
);
5827 if (err
== expr_eval_no_error
)
5831 /* Record the beginning of the compiled code. */
5832 tpoint
->compiled_cond
= entry_point
;
5834 trace_debug ("Condition compilation for tracepoint %d complete\n",
5839 /* Leave the unfinished code in situ, but don't point to it. */
5841 tpoint
->compiled_cond
= 0;
5843 trace_debug ("Condition compilation for tracepoint %d failed, "
5845 tpoint
->number
, err
);
5848 /* Update the code pointer passed in. Note that we do this even if
5849 the compile fails, so that we can look at the partial results
5850 instead of letting them be overwritten. */
5851 *jump_entry
= current_insn_ptr
;
5853 /* Leave a gap, to aid dump decipherment. */
5857 /* We'll need to adjust these when we consider bi-arch setups, and big
5861 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5863 return write_inferior_memory (where
,
5864 (unsigned char *) &ptr
, sizeof (void *));
5867 /* The base pointer of the IPA's heap. This is the only memory the
5868 IPA is allowed to use. The IPA should _not_ call the inferior's
5869 `malloc' during operation. That'd be slow, and, most importantly,
5870 it may not be safe. We may be collecting a tracepoint in a signal
5871 handler, for example. */
5872 static CORE_ADDR target_tp_heap
;
5874 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5878 target_malloc (ULONGEST size
)
5882 if (target_tp_heap
== 0)
5884 /* We have the pointer *address*, need what it points to. */
5885 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5887 fatal ("could get target heap head pointer");
5890 ptr
= target_tp_heap
;
5891 target_tp_heap
+= size
;
5893 /* Pad to 8-byte alignment. */
5894 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5900 download_agent_expr (struct agent_expr
*expr
)
5902 CORE_ADDR expr_addr
;
5903 CORE_ADDR expr_bytes
;
5905 expr_addr
= target_malloc (sizeof (*expr
));
5906 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5908 expr_bytes
= target_malloc (expr
->length
);
5909 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5911 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5916 /* Align V up to N bits. */
5917 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5919 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5922 download_tracepoint_1 (struct tracepoint
*tpoint
)
5924 struct tracepoint target_tracepoint
;
5925 CORE_ADDR tpptr
= 0;
5927 gdb_assert (tpoint
->type
== fast_tracepoint
5928 || tpoint
->type
== static_tracepoint
);
5930 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5932 CORE_ADDR jentry
, jump_entry
;
5934 jentry
= jump_entry
= get_jump_space_head ();
5936 if (tpoint
->cond
!= NULL
)
5938 /* Pad to 8-byte alignment. (needed?) */
5939 /* Actually this should be left for the target to
5941 jentry
= UALIGN (jentry
, 8);
5943 compile_tracepoint_condition (tpoint
, &jentry
);
5946 /* Pad to 8-byte alignment. */
5947 jentry
= UALIGN (jentry
, 8);
5948 claim_jump_space (jentry
- jump_entry
);
5951 target_tracepoint
= *tpoint
;
5953 tpptr
= target_malloc (sizeof (*tpoint
));
5954 tpoint
->obj_addr_on_target
= tpptr
;
5956 /* Write the whole object. We'll fix up its pointers in a bit.
5957 Assume no next for now. This is fixed up above on the next
5958 iteration, if there's any. */
5959 target_tracepoint
.next
= NULL
;
5960 /* Need to clear this here too, since we're downloading the
5961 tracepoints before clearing our own copy. */
5962 target_tracepoint
.hit_count
= 0;
5964 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5965 sizeof (target_tracepoint
));
5968 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5970 download_agent_expr (tpoint
->cond
));
5972 if (tpoint
->numactions
)
5975 CORE_ADDR actions_array
;
5977 /* The pointers array. */
5979 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
5980 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5984 /* Now for each pointer, download the action. */
5985 for (i
= 0; i
< tpoint
->numactions
; i
++)
5987 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5988 CORE_ADDR ipa_action
= action
->ops
->download (action
);
5990 if (ipa_action
!= 0)
5991 write_inferior_data_ptr
5992 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
5998 #define IPA_PROTO_FAST_TRACE_FLAG 0
5999 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
6000 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
6001 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
6002 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
6004 /* Send a command to agent to download and install tracepoint TPOINT. */
6007 tracepoint_send_agent (struct tracepoint
*tpoint
)
6009 char buf
[IPA_CMD_BUF_SIZE
];
6014 strcpy (p
, "FastTrace:");
6017 COPY_FIELD_TO_BUF (p
, tpoint
, number
);
6018 COPY_FIELD_TO_BUF (p
, tpoint
, address
);
6019 COPY_FIELD_TO_BUF (p
, tpoint
, type
);
6020 COPY_FIELD_TO_BUF (p
, tpoint
, enabled
);
6021 COPY_FIELD_TO_BUF (p
, tpoint
, step_count
);
6022 COPY_FIELD_TO_BUF (p
, tpoint
, pass_count
);
6023 COPY_FIELD_TO_BUF (p
, tpoint
, numactions
);
6024 COPY_FIELD_TO_BUF (p
, tpoint
, hit_count
);
6025 COPY_FIELD_TO_BUF (p
, tpoint
, traceframe_usage
);
6026 COPY_FIELD_TO_BUF (p
, tpoint
, compiled_cond
);
6027 COPY_FIELD_TO_BUF (p
, tpoint
, orig_size
);
6030 p
= agent_expr_send (p
, tpoint
->cond
);
6032 /* tracepoint_action */
6033 for (i
= 0; i
< tpoint
->numactions
; i
++)
6035 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6037 p
[0] = action
->type
;
6038 p
= action
->ops
->send (&p
[1], action
);
6041 get_jump_space_head ();
6042 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6043 agent can use jump pad from it. */
6044 if (tpoint
->type
== fast_tracepoint
)
6046 memcpy (p
, &gdb_jump_pad_head
, 8);
6050 ret
= run_inferior_command (buf
, (int) (ptrdiff_t) (p
- buf
));
6054 if (strncmp (buf
, "OK", 2) != 0)
6057 /* The value of tracepoint's target address is stored in BUF. */
6058 memcpy (&tpoint
->obj_addr_on_target
,
6059 &buf
[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET
], 8);
6061 if (tpoint
->type
== fast_tracepoint
)
6064 = (unsigned char *) &buf
[IPA_PROTO_FAST_TRACE_FJUMP_INSN
];
6067 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6068 (unsigned int) tpoint
->obj_addr_on_target
,
6069 (unsigned int) gdb_jump_pad_head
);
6071 memcpy (&gdb_jump_pad_head
, &buf
[IPA_PROTO_FAST_TRACE_JUMP_PAD
], 8);
6073 /* This has been done in agent. We should also set up record for it. */
6074 memcpy (&fjump_size
, &buf
[IPA_PROTO_FAST_TRACE_FJUMP_SIZE
], 4);
6077 = set_fast_tracepoint_jump (tpoint
->address
, insn
, fjump_size
);
6084 download_tracepoint (struct tracepoint
*tpoint
)
6086 struct tracepoint
*tp
, *tp_prev
;
6088 if (tpoint
->type
!= fast_tracepoint
6089 && tpoint
->type
!= static_tracepoint
)
6092 download_tracepoint_1 (tpoint
);
6094 /* Find the previous entry of TPOINT, which is fast tracepoint or
6095 static tracepoint. */
6097 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6099 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6105 CORE_ADDR tp_prev_target_next_addr
;
6107 /* Insert TPOINT after TP_PREV in IPA. */
6108 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6109 + offsetof (struct tracepoint
, next
),
6110 &tp_prev_target_next_addr
))
6111 fatal ("error reading `tp_prev->next'");
6113 /* tpoint->next = tp_prev->next */
6114 write_inferior_data_ptr (tpoint
->obj_addr_on_target
6115 + offsetof (struct tracepoint
, next
),
6116 tp_prev_target_next_addr
);
6117 /* tp_prev->next = tpoint */
6118 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
6119 + offsetof (struct tracepoint
, next
),
6120 tpoint
->obj_addr_on_target
);
6123 /* First object in list, set the head pointer in the
6125 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
6126 tpoint
->obj_addr_on_target
);
6131 download_trace_state_variables (void)
6133 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6134 struct trace_state_variable
*tsv
;
6136 /* Start out empty. */
6137 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6139 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6141 struct trace_state_variable target_tsv
;
6143 /* TSV's with a getter have been initialized equally in both the
6144 inferior and GDBserver. Skip them. */
6145 if (tsv
->getter
!= NULL
)
6151 ptr
= target_malloc (sizeof (*tsv
));
6153 if (tsv
== trace_state_variables
)
6155 /* First object in list, set the head pointer in the
6158 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6163 write_inferior_data_ptr (prev_ptr
6164 + offsetof (struct trace_state_variable
,
6169 /* Write the whole object. We'll fix up its pointers in a bit.
6170 Assume no next, fixup when needed. */
6171 target_tsv
.next
= NULL
;
6173 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6174 sizeof (target_tsv
));
6176 if (tsv
->name
!= NULL
)
6178 size_t size
= strlen (tsv
->name
) + 1;
6179 CORE_ADDR name_addr
= target_malloc (size
);
6180 write_inferior_memory (name_addr
,
6181 (unsigned char *) tsv
->name
, size
);
6182 write_inferior_data_ptr (ptr
6183 + offsetof (struct trace_state_variable
,
6188 if (tsv
->getter
!= NULL
)
6190 fatal ("what to do with these?");
6196 /* Fixup the next pointer in the last item in the list. */
6197 write_inferior_data_ptr (prev_ptr
6198 + offsetof (struct trace_state_variable
,
6203 /* Upload complete trace frames out of the IP Agent's trace buffer
6204 into GDBserver's trace buffer. This always uploads either all or
6205 no trace frames. This is the counter part of
6206 `trace_alloc_trace_buffer'. See its description of the atomic
6207 synching mechanism. */
6210 upload_fast_traceframes (void)
6212 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6213 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6215 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6216 unsigned int curr_tbctrl_idx
;
6217 unsigned int ipa_trace_buffer_ctrl_curr
;
6218 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6219 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6220 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6221 CORE_ADDR ipa_trace_buffer_lo
;
6222 CORE_ADDR ipa_trace_buffer_hi
;
6224 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6225 &ipa_traceframe_read_count_racy
))
6227 /* This will happen in most targets if the current thread is
6232 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6233 &ipa_traceframe_write_count_racy
))
6236 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6237 ipa_traceframe_write_count_racy
6238 - ipa_traceframe_read_count_racy
,
6239 ipa_traceframe_write_count_racy
,
6240 ipa_traceframe_read_count_racy
);
6242 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6245 about_to_request_buffer_space_bkpt
6246 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6249 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6250 &ipa_trace_buffer_ctrl_curr
))
6253 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6255 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6258 unsigned int prev
, counter
;
6260 /* Update the token, with new counters, and the GDBserver stamp
6261 bit. Alway reuse the current TBC index. */
6262 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6263 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6265 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6271 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6272 ipa_trace_buffer_ctrl_curr
))
6275 trace_debug ("Lib: Committed %08x -> %08x",
6276 ipa_trace_buffer_ctrl_curr_old
,
6277 ipa_trace_buffer_ctrl_curr
);
6279 /* Re-read these, now that we've installed the
6280 `about_to_request_buffer_space' breakpoint/lock. A thread could
6281 have finished a traceframe between the last read of these
6282 counters and setting the breakpoint above. If we start
6283 uploading, we never want to leave this function with
6284 traceframe_read_count != 0, otherwise, GDBserver could end up
6285 incrementing the counter tokens more than once (due to event loop
6286 nesting), which would break the IP agent's "effective" detection
6287 (see trace_alloc_trace_buffer). */
6288 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6289 &ipa_traceframe_read_count
))
6291 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6292 &ipa_traceframe_write_count
))
6297 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6298 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6299 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6301 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6302 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6303 trace_debug ("note that ipa_traceframe_count's parts changed");
6306 /* Get the address of the current TBC object (the IP agent has an
6307 array of 3 such objects). The index is stored in the TBC
6309 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6310 ipa_trace_buffer_ctrl_addr
6311 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6313 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6314 (unsigned char *) &ipa_trace_buffer_ctrl
,
6315 sizeof (struct ipa_trace_buffer_control
)))
6318 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6319 &ipa_trace_buffer_lo
))
6321 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6322 &ipa_trace_buffer_hi
))
6325 /* Offsets are easier to grok for debugging than raw addresses,
6326 especially for the small trace buffer sizes that are useful for
6328 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6329 "endfree=%d wrap=%d hi=%d",
6331 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6332 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6333 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6334 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6335 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6337 /* Note that the IPA's buffer is always circular. */
6339 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6341 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6342 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6344 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6345 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6346 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6347 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6350 tf
= IPA_FIRST_TRACEFRAME ();
6352 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6354 struct tracepoint
*tpoint
;
6355 struct traceframe
*tframe
;
6356 unsigned char *block
;
6357 struct traceframe ipa_tframe
;
6359 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6360 offsetof (struct traceframe
, data
)))
6361 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6363 if (ipa_tframe
.tpnum
== 0)
6364 fatal ("Uploading: No (more) fast traceframes, but "
6365 "ipa_traceframe_count == %u??\n",
6366 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6368 /* Note that this will be incorrect for multi-location
6370 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6372 tframe
= add_traceframe (tpoint
);
6375 trace_buffer_is_full
= 1;
6376 trace_debug ("Uploading: trace buffer is full");
6380 /* Copy the whole set of blocks in one go for now. FIXME:
6381 split this in smaller blocks. */
6382 block
= add_traceframe_block (tframe
, tpoint
,
6383 ipa_tframe
.data_size
);
6386 if (read_inferior_memory (tf
6387 + offsetof (struct traceframe
, data
),
6388 block
, ipa_tframe
.data_size
))
6389 error ("Uploading: Couldn't read traceframe data at %s\n",
6390 paddress (tf
+ offsetof (struct traceframe
, data
)));
6393 trace_debug ("Uploading: traceframe didn't fit");
6394 finish_traceframe (tframe
);
6397 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6399 /* If we freed the traceframe that wrapped around, go back
6400 to the non-wrap case. */
6401 if (tf
< ipa_trace_buffer_ctrl
.start
)
6403 trace_debug ("Lib: Discarding past the wraparound");
6404 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6406 ipa_trace_buffer_ctrl
.start
= tf
;
6407 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6408 ++ipa_traceframe_read_count
;
6410 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6411 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6413 trace_debug ("Lib: buffer is fully empty. "
6414 "Trace buffer [%d] start=%d free=%d endfree=%d",
6416 (int) (ipa_trace_buffer_ctrl
.start
6417 - ipa_trace_buffer_lo
),
6418 (int) (ipa_trace_buffer_ctrl
.free
6419 - ipa_trace_buffer_lo
),
6420 (int) (ipa_trace_buffer_ctrl
.end_free
6421 - ipa_trace_buffer_lo
));
6423 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6424 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6425 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6426 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6429 trace_debug ("Uploaded a traceframe\n"
6430 "Lib: Trace buffer [%d] start=%d free=%d "
6431 "endfree=%d wrap=%d hi=%d",
6433 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6434 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6435 (int) (ipa_trace_buffer_ctrl
.end_free
6436 - ipa_trace_buffer_lo
),
6437 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6438 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6441 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6442 (unsigned char *) &ipa_trace_buffer_ctrl
,
6443 sizeof (struct ipa_trace_buffer_control
)))
6446 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6447 ipa_traceframe_read_count
);
6449 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6452 cancel_breakpoints ();
6454 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6455 about_to_request_buffer_space_bkpt
= NULL
;
6459 if (trace_buffer_is_full
)
6464 #ifdef IN_PROCESS_AGENT
6466 IP_AGENT_EXPORT
int ust_loaded
;
6467 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6471 /* Static tracepoints. */
6473 /* UST puts a "struct tracepoint" in the global namespace, which
6474 conflicts with our tracepoint. Arguably, being a library, it
6475 shouldn't take ownership of such a generic name. We work around it
6477 #define tracepoint ust_tracepoint
6478 #include <ust/ust.h>
6481 extern int serialize_to_text (char *outbuf
, int bufsize
,
6482 const char *fmt
, va_list ap
);
6484 #define GDB_PROBE_NAME "gdb"
6486 /* We dynamically search for the UST symbols instead of linking them
6487 in. This lets the user decide if the application uses static
6488 tracepoints, instead of always pulling libust.so in. This vector
6489 holds pointers to all functions we care about. */
6493 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6494 const char *fmt
, va_list ap
);
6496 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6497 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6499 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6501 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6504 void (*marker_iter_start
) (struct marker_iter
*iter
);
6505 void (*marker_iter_next
) (struct marker_iter
*iter
);
6506 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6507 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6512 /* Cast through typeof to catch incompatible API changes. Since UST
6513 only builds with gcc, we can freely use gcc extensions here
6515 #define GET_UST_SYM(SYM) \
6518 if (ust_ops.SYM == NULL) \
6519 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6520 if (ust_ops.SYM == NULL) \
6524 #define USTF(SYM) ust_ops.SYM
6526 /* Get pointers to all libust.so functions we care about. */
6531 GET_UST_SYM (serialize_to_text
);
6533 GET_UST_SYM (ltt_probe_register
);
6534 GET_UST_SYM (ltt_probe_unregister
);
6535 GET_UST_SYM (ltt_marker_connect
);
6536 GET_UST_SYM (ltt_marker_disconnect
);
6538 GET_UST_SYM (marker_iter_start
);
6539 GET_UST_SYM (marker_iter_next
);
6540 GET_UST_SYM (marker_iter_stop
);
6541 GET_UST_SYM (marker_iter_reset
);
6547 /* Given an UST marker, return the matching gdb static tracepoint.
6548 The match is done by address. */
6550 static struct tracepoint
*
6551 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6553 struct tracepoint
*tpoint
;
6555 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6557 if (tpoint
->type
!= static_tracepoint
)
6560 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6567 /* The probe function we install on lttng/ust markers. Whenever a
6568 probed ust marker is hit, this function is called. This is similar
6569 to gdb_collect, only for static tracepoints, instead of fast
6573 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6574 struct registers
*regs
, void *call_private
,
6575 const char *fmt
, va_list *args
)
6577 struct tracepoint
*tpoint
;
6578 struct static_tracepoint_ctx ctx
;
6580 /* Don't do anything until the trace run is completely set up. */
6583 trace_debug ("gdb_probe: not tracing\n");
6587 ctx
.base
.type
= static_tracepoint
;
6588 ctx
.regcache_initted
= 0;
6593 /* Wrap the regblock in a register cache (in the stack, we don't
6594 want to malloc here). */
6595 ctx
.regspace
= alloca (register_cache_size ());
6596 if (ctx
.regspace
== NULL
)
6598 trace_debug ("Trace buffer block allocation failed, skipping");
6602 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6605 trace_debug ("gdb_probe: marker not known: "
6606 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6607 mdata
->location
, mdata
->channel
,
6608 mdata
->name
, mdata
->format
);
6612 if (!tpoint
->enabled
)
6614 trace_debug ("gdb_probe: tracepoint disabled");
6618 ctx
.tpoint
= tpoint
;
6620 trace_debug ("gdb_probe: collecting marker: "
6621 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6622 mdata
->location
, mdata
->channel
,
6623 mdata
->name
, mdata
->format
);
6625 /* Test the condition if present, and collect if true. */
6626 if (tpoint
->cond
== NULL
6627 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6630 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6631 tpoint
->address
, tpoint
);
6633 if (stopping_tracepoint
6634 || trace_buffer_is_full
6635 || expr_eval_result
!= expr_eval_no_error
)
6640 /* If there was a condition and it evaluated to false, the only
6641 way we would stop tracing is if there was an error during
6642 condition expression evaluation. */
6643 if (expr_eval_result
!= expr_eval_no_error
)
6648 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6649 static tracepoint string data. This is a string passed to the
6650 tracing library by the user, at the time of the tracepoint marker
6651 call. E.g., in the UST marker call:
6653 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6655 the collected data is "str FOOBAZ".
6659 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6660 struct traceframe
*tframe
)
6662 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6663 unsigned char *bufspace
;
6666 unsigned short blocklen
;
6670 trace_debug ("Wanted to collect static trace data, "
6671 "but there's no static trace data");
6675 va_copy (copy
, *umd
->args
);
6676 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6679 trace_debug ("Want to collect ust data");
6681 /* 'S' + size + string */
6682 bufspace
= add_traceframe_block (tframe
, umd
->tpoint
,
6683 1 + sizeof (blocklen
) + size
+ 1);
6684 if (bufspace
== NULL
)
6686 trace_debug ("Trace buffer block allocation failed, skipping");
6690 /* Identify a static trace data block. */
6693 blocklen
= size
+ 1;
6694 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6696 va_copy (copy
, *umd
->args
);
6697 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6698 size
+ 1, umd
->fmt
, copy
);
6701 trace_debug ("Storing static tracepoint data in regblock: %s",
6702 bufspace
+ 1 + sizeof (blocklen
));
6705 /* The probe to register with lttng/ust. */
6706 static struct ltt_available_probe gdb_ust_probe
=
6713 #endif /* HAVE_UST */
6714 #endif /* IN_PROCESS_AGENT */
6716 #ifndef IN_PROCESS_AGENT
6718 /* Ask the in-process agent to run a command. Since we don't want to
6719 have to handle the IPA hitting breakpoints while running the
6720 command, we pause all threads, remove all breakpoints, and then set
6721 the helper thread re-running. We communicate with the helper
6722 thread by means of direct memory xfering, and a socket for
6726 run_inferior_command (char *cmd
, int len
)
6729 int pid
= ptid_get_pid (current_ptid
);
6731 trace_debug ("run_inferior_command: running: %s", cmd
);
6734 uninsert_all_breakpoints ();
6736 err
= agent_run_command (pid
, (const char *) cmd
, len
);
6738 reinsert_all_breakpoints ();
6744 #else /* !IN_PROCESS_AGENT */
6746 #include <sys/socket.h>
6749 #ifndef UNIX_PATH_MAX
6750 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6753 /* Where we put the socked used for synchronization. */
6754 #define SOCK_DIR P_tmpdir
6756 /* Thread ID of the helper thread. GDBserver reads this to know which
6757 is the help thread. This is an LWP id on Linux. */
6758 int helper_thread_id
;
6761 init_named_socket (const char *name
)
6764 struct sockaddr_un addr
;
6766 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6769 warning ("socket creation failed: %s", strerror (errno
));
6773 addr
.sun_family
= AF_UNIX
;
6775 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6776 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6778 result
= access (name
, F_OK
);
6782 result
= unlink (name
);
6785 warning ("unlink failed: %s", strerror (errno
));
6789 warning ("socket %s already exists; overwriting", name
);
6792 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6795 warning ("bind failed: %s", strerror (errno
));
6800 result
= listen (fd
, 1);
6803 warning ("listen: %s", strerror (errno
));
6811 static char agent_socket_name
[UNIX_PATH_MAX
];
6814 gdb_agent_socket_init (void)
6818 result
= xsnprintf (agent_socket_name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6819 SOCK_DIR
, getpid ());
6820 if (result
>= UNIX_PATH_MAX
)
6822 trace_debug ("string overflow allocating socket name");
6826 fd
= init_named_socket (agent_socket_name
);
6828 warning ("Error initializing named socket (%s) for communication with the "
6829 "ust helper thread. Check that directory exists and that it "
6830 "is writable.", agent_socket_name
);
6837 /* The next marker to be returned on a qTsSTM command. */
6838 static const struct marker
*next_st
;
6840 /* Returns the first known marker. */
6845 struct marker_iter iter
;
6847 USTF(marker_iter_reset
) (&iter
);
6848 USTF(marker_iter_start
) (&iter
);
6853 /* Returns the marker following M. */
6855 const struct marker
*
6856 next_marker (const struct marker
*m
)
6858 struct marker_iter iter
;
6860 USTF(marker_iter_reset
) (&iter
);
6861 USTF(marker_iter_start
) (&iter
);
6863 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6865 if (iter
.marker
== m
)
6867 USTF(marker_iter_next
) (&iter
);
6875 /* Return an hexstr version of the STR C string, fit for sending to
6879 cstr_to_hexstr (const char *str
)
6881 int len
= strlen (str
);
6882 char *hexstr
= xmalloc (len
* 2 + 1);
6883 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6887 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6891 response_ust_marker (char *packet
, const struct marker
*st
)
6893 char *strid
, *format
, *tmp
;
6895 next_st
= next_marker (st
);
6897 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6898 strlen (st
->name
) + 1);
6899 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6901 strid
= cstr_to_hexstr (tmp
);
6904 format
= cstr_to_hexstr (st
->format
);
6906 sprintf (packet
, "m%s:%s:%s",
6907 paddress ((uintptr_t) st
->location
),
6915 /* Return the first static tracepoint, and initialize the state
6916 machine that will iterate through all the static tracepoints. */
6919 cmd_qtfstm (char *packet
)
6921 trace_debug ("Returning first trace state variable definition");
6923 if (first_marker ())
6924 response_ust_marker (packet
, first_marker ());
6926 strcpy (packet
, "l");
6929 /* Return additional trace state variable definitions. */
6932 cmd_qtsstm (char *packet
)
6934 trace_debug ("Returning static tracepoint");
6937 response_ust_marker (packet
, next_st
);
6939 strcpy (packet
, "l");
6942 /* Disconnect the GDB probe from a marker at a given address. */
6945 unprobe_marker_at (char *packet
)
6949 struct marker_iter iter
;
6951 p
+= sizeof ("unprobe_marker_at:") - 1;
6953 p
= unpack_varlen_hex (p
, &address
);
6955 USTF(marker_iter_reset
) (&iter
);
6956 USTF(marker_iter_start
) (&iter
);
6957 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6958 if ((uintptr_t ) iter
.marker
->location
== address
)
6962 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
6966 warning ("could not disable marker %s/%s",
6967 iter
.marker
->channel
, iter
.marker
->name
);
6972 /* Connect the GDB probe to a marker at a given address. */
6975 probe_marker_at (char *packet
)
6979 struct marker_iter iter
;
6982 p
+= sizeof ("probe_marker_at:") - 1;
6984 p
= unpack_varlen_hex (p
, &address
);
6986 USTF(marker_iter_reset
) (&iter
);
6988 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6990 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6991 if ((uintptr_t ) m
->location
== address
)
6995 trace_debug ("found marker for address. "
6996 "ltt_marker_connect (marker = %s/%s)",
6997 m
->channel
, m
->name
);
6999 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7001 if (result
&& result
!= -EEXIST
)
7002 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7003 m
->channel
, m
->name
, -result
);
7007 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7008 m
->channel
, m
->name
);
7012 strcpy (packet
, "OK");
7016 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7021 cmd_qtstmat (char *packet
)
7025 struct marker_iter iter
;
7028 p
+= sizeof ("qTSTMat:") - 1;
7030 p
= unpack_varlen_hex (p
, &address
);
7032 USTF(marker_iter_reset
) (&iter
);
7034 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7036 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7037 if ((uintptr_t ) m
->location
== address
)
7039 response_ust_marker (packet
, m
);
7043 strcpy (packet
, "l");
7053 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7056 #endif /* HAVE_UST */
7058 #include <sys/syscall.h>
7062 gdb_agent_remove_socket (void)
7064 unlink (agent_socket_name
);
7067 /* Helper thread of agent. */
7070 gdb_agent_helper_thread (void *arg
)
7074 atexit (gdb_agent_remove_socket
);
7078 listen_fd
= gdb_agent_socket_init ();
7080 if (helper_thread_id
== 0)
7081 helper_thread_id
= syscall (SYS_gettid
);
7083 if (listen_fd
== -1)
7085 warning ("could not create sync socket\n");
7092 struct sockaddr_un sockaddr
;
7098 tmp
= sizeof (sockaddr
);
7102 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7104 /* It seems an ERESTARTSYS can escape out of accept. */
7105 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7109 warning ("Accept returned %d, error: %s\n",
7110 fd
, strerror (errno
));
7116 ret
= read (fd
, buf
, 1);
7117 } while (ret
== -1 && errno
== EINTR
);
7121 warning ("reading socket (fd=%d) failed with %s",
7122 fd
, strerror (errno
));
7129 if (strncmp ("close", cmd_buf
, 5) == 0)
7134 else if (strcmp ("qTfSTM", cmd_buf
) == 0)
7136 cmd_qtfstm (cmd_buf
);
7138 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7140 cmd_qtsstm (cmd_buf
);
7142 else if (strncmp ("unprobe_marker_at:",
7144 sizeof ("unprobe_marker_at:") - 1) == 0)
7146 unprobe_marker_at (cmd_buf
);
7148 else if (strncmp ("probe_marker_at:",
7150 sizeof ("probe_marker_at:") - 1) == 0)
7152 probe_marker_at (cmd_buf
);
7154 else if (strncmp ("qTSTMat:",
7156 sizeof ("qTSTMat:") - 1) == 0)
7158 cmd_qtstmat (cmd_buf
);
7160 #endif /* HAVE_UST */
7163 /* Fix compiler's warning: ignoring return value of 'write'. */
7164 ret
= write (fd
, buf
, 1);
7170 unlink (agent_socket_name
);
7172 /* Sleep endlessly to wait the whole inferior stops. This
7173 thread can not exit because GDB or GDBserver may still need
7174 'current_inferior' (representing this thread) to access
7175 inferior memory. Otherwise, this thread exits earlier than
7176 other threads, and 'current_inferior' is set to NULL. */
7187 #include <pthread.h>
7189 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
7192 gdb_agent_init (void)
7199 /* We want the helper thread to be as transparent as possible, so
7200 have it inherit an all-signals-blocked mask. */
7202 sigfillset (&new_mask
);
7203 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7205 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7207 res
= pthread_create (&thread
,
7209 gdb_agent_helper_thread
,
7212 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7214 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7216 while (helper_thread_id
== 0)
7224 #include <sys/mman.h>
7227 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7228 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7229 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7230 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
7231 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
7232 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
7234 /* Record the result of getting buffer space for fast tracepoint
7235 trampolines. Any error message is copied, since caller may not be
7236 using persistent storage. */
7239 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7241 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7242 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7244 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7246 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7249 static void __attribute__ ((constructor
))
7250 initialize_tracepoint_ftlib (void)
7252 initialize_tracepoint ();
7257 #endif /* IN_PROCESS_AGENT */
7259 /* Return a timestamp, expressed as microseconds of the usual Unix
7260 time. (As the result is a 64-bit number, it will not overflow any
7264 get_timestamp (void)
7268 if (gettimeofday (&tv
, 0) != 0)
7271 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7275 initialize_tracepoint (void)
7277 /* Start with the default size. */
7278 init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE
);
7280 /* Wire trace state variable 1 to be the timestamp. This will be
7281 uploaded to GDB upon connection and become one of its trace state
7282 variables. (In case you're wondering, if GDB already has a trace
7283 variable numbered 1, it will be renumbered.) */
7284 create_trace_state_variable (1, 0);
7285 set_trace_state_variable_name (1, "trace_timestamp");
7286 set_trace_state_variable_getter (1, get_timestamp
);
7288 #ifdef IN_PROCESS_AGENT
7293 pagesize
= sysconf (_SC_PAGE_SIZE
);
7297 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7299 #define SCRATCH_BUFFER_NPAGES 20
7301 /* Allocate scratch buffer aligned on a page boundary, at a low
7302 address (close to the main executable's code). */
7303 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7305 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7306 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7307 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7309 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7315 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7318 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7321 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7323 /* It's not a fatal error for something to go wrong with trampoline
7324 buffer setup, but it can be mysterious, so create a channel to
7325 report back on what went wrong, using a fixed size since we may
7326 not be able to allocate space later when the problem occurs. */
7327 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7329 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7331 initialize_low_tracepoint ();