1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2012 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/>. */
31 /* This file is built for both GDBserver, and the in-process
32 agent (IPA), a shared library that includes a tracing agent that is
33 loaded by the inferior to support fast tracepoints. Fast
34 tracepoints (or more accurately, jump based tracepoints) are
35 implemented by patching the tracepoint location with a jump into a
36 small trampoline function whose job is to save the register state,
37 call the in-process tracing agent, and then execute the original
38 instruction that was under the tracepoint jump (possibly adjusted,
39 if PC-relative, or some such).
41 The current synchronization design is pull based. That means,
42 GDBserver does most of the work, by peeking/poking at the inferior
43 agent's memory directly for downloading tracepoint and associated
44 objects, and for uploading trace frames. Whenever the IPA needs
45 something from GDBserver (trace buffer is full, tracing stopped for
46 some reason, etc.) the IPA calls a corresponding hook function
47 where GDBserver has placed a breakpoint.
49 Each of the agents has its own trace buffer. When browsing the
50 trace frames built from slow and fast tracepoints from GDB (tfind
51 mode), there's no guarantee the user is seeing the trace frames in
52 strict chronological creation order, although, GDBserver tries to
53 keep the order relatively reasonable, by syncing the trace buffers
58 static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf
, 1, 2);
61 trace_vdebug (const char *fmt
, ...)
67 vsprintf (buf
, fmt
, ap
);
68 fprintf (stderr
, PROG
"/tracepoint: %s\n", buf
);
72 #define trace_debug_1(level, fmt, args...) \
74 if (level <= debug_threads) \
75 trace_vdebug ((fmt), ##args); \
78 #define trace_debug(FMT, args...) \
79 trace_debug_1 (1, FMT, ##args)
82 # define ATTR_USED __attribute__((used))
83 # define ATTR_NOINLINE __attribute__((noinline))
84 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
87 # define ATTR_NOINLINE
88 # define ATTR_CONSTRUCTOR
91 /* Make sure the functions the IPA needs to export (symbols GDBserver
92 needs to query GDB about) are exported. */
94 #ifdef IN_PROCESS_AGENT
95 # if defined _WIN32 || defined __CYGWIN__
96 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
99 # define IP_AGENT_EXPORT \
100 __attribute__ ((visibility("default"))) ATTR_USED
102 # define IP_AGENT_EXPORT ATTR_USED
106 # define IP_AGENT_EXPORT
109 /* Prefix exported symbols, for good citizenship. All the symbols
110 that need exporting are defined in this module. */
111 #ifdef IN_PROCESS_AGENT
112 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
113 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
114 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
115 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
116 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
117 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
118 # define collecting gdb_agent_collecting
119 # define gdb_collect gdb_agent_gdb_collect
120 # define stop_tracing gdb_agent_stop_tracing
121 # define flush_trace_buffer gdb_agent_flush_trace_buffer
122 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
123 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
124 # define stopping_tracepoint gdb_agent_stopping_tracepoint
125 # define expr_eval_result gdb_agent_expr_eval_result
126 # define error_tracepoint gdb_agent_error_tracepoint
127 # define tracepoints gdb_agent_tracepoints
128 # define tracing gdb_agent_tracing
129 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
130 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
131 # define trace_buffer_lo gdb_agent_trace_buffer_lo
132 # define trace_buffer_hi gdb_agent_trace_buffer_hi
133 # define traceframe_read_count gdb_agent_traceframe_read_count
134 # define traceframe_write_count gdb_agent_traceframe_write_count
135 # define traceframes_created gdb_agent_traceframes_created
136 # define trace_state_variables gdb_agent_trace_state_variables
137 # define get_raw_reg gdb_agent_get_raw_reg
138 # define get_trace_state_variable_value \
139 gdb_agent_get_trace_state_variable_value
140 # define set_trace_state_variable_value \
141 gdb_agent_set_trace_state_variable_value
142 # define ust_loaded gdb_agent_ust_loaded
143 # define helper_thread_id gdb_agent_helper_thread_id
144 # define cmd_buf gdb_agent_cmd_buf
147 #ifndef IN_PROCESS_AGENT
149 /* Addresses of in-process agent's symbols GDBserver cares about. */
151 struct ipa_sym_addresses
153 CORE_ADDR addr_gdb_tp_heap_buffer
;
154 CORE_ADDR addr_gdb_jump_pad_buffer
;
155 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
156 CORE_ADDR addr_gdb_trampoline_buffer
;
157 CORE_ADDR addr_gdb_trampoline_buffer_end
;
158 CORE_ADDR addr_gdb_trampoline_buffer_error
;
159 CORE_ADDR addr_collecting
;
160 CORE_ADDR addr_gdb_collect
;
161 CORE_ADDR addr_stop_tracing
;
162 CORE_ADDR addr_flush_trace_buffer
;
163 CORE_ADDR addr_about_to_request_buffer_space
;
164 CORE_ADDR addr_trace_buffer_is_full
;
165 CORE_ADDR addr_stopping_tracepoint
;
166 CORE_ADDR addr_expr_eval_result
;
167 CORE_ADDR addr_error_tracepoint
;
168 CORE_ADDR addr_tracepoints
;
169 CORE_ADDR addr_tracing
;
170 CORE_ADDR addr_trace_buffer_ctrl
;
171 CORE_ADDR addr_trace_buffer_ctrl_curr
;
172 CORE_ADDR addr_trace_buffer_lo
;
173 CORE_ADDR addr_trace_buffer_hi
;
174 CORE_ADDR addr_traceframe_read_count
;
175 CORE_ADDR addr_traceframe_write_count
;
176 CORE_ADDR addr_traceframes_created
;
177 CORE_ADDR addr_trace_state_variables
;
178 CORE_ADDR addr_get_raw_reg
;
179 CORE_ADDR addr_get_trace_state_variable_value
;
180 CORE_ADDR addr_set_trace_state_variable_value
;
181 CORE_ADDR addr_ust_loaded
;
190 IPA_SYM(gdb_tp_heap_buffer
),
191 IPA_SYM(gdb_jump_pad_buffer
),
192 IPA_SYM(gdb_jump_pad_buffer_end
),
193 IPA_SYM(gdb_trampoline_buffer
),
194 IPA_SYM(gdb_trampoline_buffer_end
),
195 IPA_SYM(gdb_trampoline_buffer_error
),
197 IPA_SYM(gdb_collect
),
198 IPA_SYM(stop_tracing
),
199 IPA_SYM(flush_trace_buffer
),
200 IPA_SYM(about_to_request_buffer_space
),
201 IPA_SYM(trace_buffer_is_full
),
202 IPA_SYM(stopping_tracepoint
),
203 IPA_SYM(expr_eval_result
),
204 IPA_SYM(error_tracepoint
),
205 IPA_SYM(tracepoints
),
207 IPA_SYM(trace_buffer_ctrl
),
208 IPA_SYM(trace_buffer_ctrl_curr
),
209 IPA_SYM(trace_buffer_lo
),
210 IPA_SYM(trace_buffer_hi
),
211 IPA_SYM(traceframe_read_count
),
212 IPA_SYM(traceframe_write_count
),
213 IPA_SYM(traceframes_created
),
214 IPA_SYM(trace_state_variables
),
215 IPA_SYM(get_raw_reg
),
216 IPA_SYM(get_trace_state_variable_value
),
217 IPA_SYM(set_trace_state_variable_value
),
221 static struct ipa_sym_addresses ipa_sym_addrs
;
223 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
225 /* Returns true if both the in-process agent library and the static
226 tracepoints libraries are loaded in the inferior, and agent has
227 capability on static tracepoints. */
230 in_process_agent_supports_ust (void)
234 if (!agent_loaded_p ())
236 warning ("In-process agent not loaded");
240 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE
))
242 /* Agent understands static tracepoint, then check whether UST is in
243 fact loaded in the inferior. */
244 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
246 warning ("Error reading ust_loaded in lib");
257 write_e_ipa_not_loaded (char *buffer
)
260 "E.In-process agent library not loaded in process. "
261 "Fast and static tracepoints unavailable.");
264 /* Write an error to BUFFER indicating that UST isn't loaded in the
268 write_e_ust_not_loaded (char *buffer
)
272 "E.UST library not loaded in process. "
273 "Static tracepoints unavailable.");
275 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
279 /* If the in-process agent library isn't loaded in the inferior, write
280 an error to BUFFER, and return 1. Otherwise, return 0. */
283 maybe_write_ipa_not_loaded (char *buffer
)
285 if (!agent_loaded_p ())
287 write_e_ipa_not_loaded (buffer
);
293 /* If the in-process agent library and the ust (static tracepoints)
294 library aren't loaded in the inferior, write an error to BUFFER,
295 and return 1. Otherwise, return 0. */
298 maybe_write_ipa_ust_not_loaded (char *buffer
)
300 if (!agent_loaded_p ())
302 write_e_ipa_not_loaded (buffer
);
305 else if (!in_process_agent_supports_ust ())
307 write_e_ust_not_loaded (buffer
);
313 /* Cache all future symbols that the tracepoints module might request.
314 We can not request symbols at arbitrary states in the remote
315 protocol, only when the client tells us that new symbols are
316 available. So when we load the in-process library, make sure to
317 check the entire list. */
320 tracepoint_look_up_symbols (void)
324 if (agent_loaded_p ())
327 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
330 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
332 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
335 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
340 agent_look_up_symbols (NULL
);
345 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
346 of the "stop_tracing" function. When this breakpoint is hit,
347 tracing stopped in the IPA for some reason. E.g., due to
348 tracepoint reaching the pass count, hitting conditional expression
349 evaluation error, etc.
351 The IPA's trace buffer is never in circular tracing mode: instead,
352 GDBserver's is, and whenever the in-process buffer fills, it calls
353 "flush_trace_buffer", which triggers an internal breakpoint.
354 GDBserver reacts to this breakpoint by pulling the meanwhile
355 collected data. Old frames discarding is always handled on the
358 #ifdef IN_PROCESS_AGENT
360 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
362 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
366 /* Call this in the functions where GDBserver places a breakpoint, so
367 that the compiler doesn't try to be clever and skip calling the
368 function at all. This is necessary, even if we tell the compiler
369 to not inline said functions. */
371 #if defined(__GNUC__)
372 # define UNKNOWN_SIDE_EFFECTS() asm ("")
374 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
377 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
380 /* GDBserver places breakpoint here. */
381 UNKNOWN_SIDE_EFFECTS();
384 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
385 flush_trace_buffer (void)
387 /* GDBserver places breakpoint here. */
388 UNKNOWN_SIDE_EFFECTS();
393 #ifndef IN_PROCESS_AGENT
395 tracepoint_handler (CORE_ADDR address
)
397 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
402 /* Breakpoint at "stop_tracing" in the inferior lib. */
403 struct breakpoint
*stop_tracing_bkpt
;
404 static int stop_tracing_handler (CORE_ADDR
);
406 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
407 struct breakpoint
*flush_trace_buffer_bkpt
;
408 static int flush_trace_buffer_handler (CORE_ADDR
);
410 static void download_tracepoints (void);
411 static void download_trace_state_variables (void);
412 static void upload_fast_traceframes (void);
414 static int run_inferior_command (char *cmd
);
417 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
419 return read_inferior_memory (symaddr
, (unsigned char *) val
,
424 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
426 return read_inferior_memory (symaddr
, (unsigned char *) val
,
431 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
433 void *pval
= (void *) (uintptr_t) val
;
436 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
437 *val
= (uintptr_t) pval
;
442 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
444 void *pval
= (void *) (uintptr_t) val
;
445 return write_inferior_memory (symaddr
,
446 (unsigned char *) &pval
, sizeof (pval
));
450 write_inferior_integer (CORE_ADDR symaddr
, int val
)
452 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
456 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
458 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
461 static CORE_ADDR
target_malloc (ULONGEST size
);
462 static int write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
);
465 /* Operations on various types of tracepoint actions. */
467 struct tracepoint_action
;
469 struct tracepoint_action_ops
471 /* Download tracepoint action ACTION to IPA. Return the address of action
473 CORE_ADDR (*download
) (const struct tracepoint_action
*action
);
476 /* Base action. Concrete actions inherit this. */
478 struct tracepoint_action
480 #ifndef IN_PROCESS_AGENT
481 const struct tracepoint_action_ops
*ops
;
486 /* An 'M' (collect memory) action. */
487 struct collect_memory_action
489 struct tracepoint_action base
;
496 /* An 'R' (collect registers) action. */
498 struct collect_registers_action
500 struct tracepoint_action base
;
503 /* An 'X' (evaluate expression) action. */
505 struct eval_expr_action
507 struct tracepoint_action base
;
509 struct agent_expr
*expr
;
512 /* An 'L' (collect static trace data) action. */
513 struct collect_static_trace_data_action
515 struct tracepoint_action base
;
518 #ifndef IN_PROCESS_AGENT
520 m_tracepoint_action_download (const struct tracepoint_action
*action
)
522 int size_in_ipa
= (sizeof (struct collect_memory_action
)
523 - offsetof (struct tracepoint_action
, type
));
524 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
526 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
532 static const struct tracepoint_action_ops m_tracepoint_action_ops
=
534 m_tracepoint_action_download
,
538 r_tracepoint_action_download (const struct tracepoint_action
*action
)
540 int size_in_ipa
= (sizeof (struct collect_registers_action
)
541 - offsetof (struct tracepoint_action
, type
));
542 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
544 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
550 static const struct tracepoint_action_ops r_tracepoint_action_ops
=
552 r_tracepoint_action_download
,
555 static CORE_ADDR
download_agent_expr (struct agent_expr
*expr
);
558 x_tracepoint_action_download (const struct tracepoint_action
*action
)
560 int size_in_ipa
= (sizeof (struct eval_expr_action
)
561 - offsetof (struct tracepoint_action
, type
));
562 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
565 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
567 expr
= download_agent_expr (((struct eval_expr_action
*)action
)->expr
);
568 write_inferior_data_ptr (ipa_action
+ offsetof (struct eval_expr_action
, expr
)
569 - offsetof (struct tracepoint_action
, type
),
575 static const struct tracepoint_action_ops x_tracepoint_action_ops
=
577 x_tracepoint_action_download
,
581 l_tracepoint_action_download (const struct tracepoint_action
*action
)
583 int size_in_ipa
= (sizeof (struct collect_static_trace_data_action
)
584 - offsetof (struct tracepoint_action
, type
));
585 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
587 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
593 static const struct tracepoint_action_ops l_tracepoint_action_ops
=
595 l_tracepoint_action_download
,
599 /* This structure describes a piece of the source-level definition of
600 the tracepoint. The contents are not interpreted by the target,
601 but preserved verbatim for uploading upon reconnection. */
605 /* The type of string, such as "cond" for a conditional. */
608 /* The source-level string itself. For the sake of target
609 debugging, we store it in plaintext, even though it is always
610 transmitted in hex. */
613 /* Link to the next one in the list. We link them in the order
614 received, in case some make up an ordered list of commands or
616 struct source_string
*next
;
621 /* Trap based tracepoint. */
624 /* A fast tracepoint implemented with a jump instead of a trap. */
627 /* A static tracepoint, implemented by a program call into a tracing
632 struct tracepoint_hit_ctx
;
634 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
637 /* The definition of a tracepoint. */
639 /* Tracepoints may have multiple locations, each at a different
640 address. This can occur with optimizations, template
641 instantiation, etc. Since the locations may be in different
642 scopes, the conditions and actions may be different for each
643 location. Our target version of tracepoints is more like GDB's
644 notion of "breakpoint locations", but we have almost nothing that
645 is not per-location, so we bother having two kinds of objects. The
646 key consequence is that numbers are not unique, and that it takes
647 both number and address to identify a tracepoint uniquely. */
651 /* The number of the tracepoint, as specified by GDB. Several
652 tracepoint objects here may share a number. */
655 /* Address at which the tracepoint is supposed to trigger. Several
656 tracepoints may share an address. */
659 /* Tracepoint type. */
660 enum tracepoint_type type
;
662 /* True if the tracepoint is currently enabled. */
665 /* The number of single steps that will be performed after each
669 /* The number of times the tracepoint may be hit before it will
670 terminate the entire tracing run. */
673 /* Pointer to the agent expression that is the tracepoint's
674 conditional, or NULL if the tracepoint is unconditional. */
675 struct agent_expr
*cond
;
677 /* The list of actions to take when the tracepoint triggers. */
679 struct tracepoint_action
**actions
;
681 /* Count of the times we've hit this tracepoint during the run.
682 Note that while-stepping steps are not counted as "hits". */
685 /* Cached sum of the sizes of traceframes created by this point. */
686 long traceframe_usage
;
688 CORE_ADDR compiled_cond
;
690 /* Link to the next tracepoint in the list. */
691 struct tracepoint
*next
;
693 #ifndef IN_PROCESS_AGENT
694 /* The list of actions to take when the tracepoint triggers, in
695 string/packet form. */
698 /* The collection of strings that describe the tracepoint as it was
699 entered into GDB. These are not used by the target, but are
700 reported back to GDB upon reconnection. */
701 struct source_string
*source_strings
;
703 /* The number of bytes displaced by fast tracepoints. It may subsume
704 multiple instructions, for multi-byte fast tracepoints. This
705 field is only valid for fast tracepoints. */
708 /* Only for fast tracepoints. */
709 CORE_ADDR obj_addr_on_target
;
711 /* Address range where the original instruction under a fast
712 tracepoint was relocated to. (_end is actually one byte past
714 CORE_ADDR adjusted_insn_addr
;
715 CORE_ADDR adjusted_insn_addr_end
;
717 /* The address range of the piece of the jump pad buffer that was
718 assigned to this fast tracepoint. (_end is actually one byte
721 CORE_ADDR jump_pad_end
;
723 /* The address range of the piece of the trampoline buffer that was
724 assigned to this fast tracepoint. (_end is actually one byte
726 CORE_ADDR trampoline
;
727 CORE_ADDR trampoline_end
;
729 /* The list of actions to take while in a stepping loop. These
730 fields are only valid for patch-based tracepoints. */
731 int num_step_actions
;
732 struct tracepoint_action
**step_actions
;
733 /* Same, but in string/packet form. */
734 char **step_actions_str
;
736 /* Handle returned by the breakpoint or tracepoint module when we
737 inserted the trap or jump, or hooked into a static tracepoint.
738 NULL if we haven't inserted it yet. */
744 #ifndef IN_PROCESS_AGENT
746 /* Given `while-stepping', a thread may be collecting data for more
747 than one tracepoint simultaneously. On the other hand, the same
748 tracepoint with a while-stepping action may be hit by more than one
749 thread simultaneously (but not quite, each thread could be handling
750 a different step). Each thread holds a list of these objects,
751 representing the current step of each while-stepping action being
756 struct wstep_state
*next
;
758 /* The tracepoint number. */
760 /* The tracepoint's address. */
761 CORE_ADDR tp_address
;
763 /* The number of the current step in this 'while-stepping'
770 /* The linked list of all tracepoints. Marked explicitly as used as
771 the in-process library doesn't use it for the fast tracepoints
773 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
775 #ifndef IN_PROCESS_AGENT
777 /* Pointer to the last tracepoint in the list, new tracepoints are
778 linked in at the end. */
780 static struct tracepoint
*last_tracepoint
;
783 /* The first tracepoint to exceed its pass count. */
785 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
787 /* True if the trace buffer is full or otherwise no longer usable. */
789 IP_AGENT_EXPORT
int trace_buffer_is_full
;
791 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
793 #ifndef IN_PROCESS_AGENT
795 static const char *eval_result_names
[] =
797 "terror:in the attic", /* this should never be reported */
798 "terror:empty expression",
799 "terror:empty stack",
800 "terror:stack overflow",
801 "terror:stack underflow",
802 "terror:unhandled opcode",
803 "terror:unrecognized opcode",
804 "terror:divide by zero"
809 /* The tracepoint in which the error occurred. */
811 static struct tracepoint
*error_tracepoint
;
813 struct trace_state_variable
815 /* This is the name of the variable as used in GDB. The target
816 doesn't use the name, but needs to have it for saving and
817 reconnection purposes. */
820 /* This number identifies the variable uniquely. Numbers may be
821 assigned either by the target (in the case of builtin variables),
822 or by GDB, and are presumed unique during the course of a trace
826 /* The variable's initial value, a 64-bit signed integer always. */
827 LONGEST initial_value
;
829 /* The variable's value, a 64-bit signed integer always. */
832 /* Pointer to a getter function, used to supply computed values. */
833 LONGEST (*getter
) (void);
835 /* Link to the next variable. */
836 struct trace_state_variable
*next
;
839 /* Linked list of all trace state variables. */
841 #ifdef IN_PROCESS_AGENT
842 struct trace_state_variable
*alloced_trace_state_variables
;
845 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
847 /* The results of tracing go into a fixed-size space known as the
848 "trace buffer". Because usage follows a limited number of
849 patterns, we manage it ourselves rather than with malloc. Basic
850 rules are that we create only one trace frame at a time, each is
851 variable in size, they are never moved once created, and we only
852 discard if we are doing a circular buffer, and then only the oldest
853 ones. Each trace frame includes its own size, so we don't need to
854 link them together, and the trace frame number is relative to the
855 first one, so we don't need to record numbers. A trace frame also
856 records the number of the tracepoint that created it. The data
857 itself is a series of blocks, each introduced by a single character
858 and with a defined format. Each type of block has enough
859 type/length info to allow scanners to jump quickly from one block
860 to the next without reading each byte in the block. */
862 /* Trace buffer management would be simple - advance a free pointer
863 from beginning to end, then stop - were it not for the circular
864 buffer option, which is a useful way to prevent a trace run from
865 stopping prematurely because the buffer filled up. In the circular
866 case, the location of the first trace frame (trace_buffer_start)
867 moves as old trace frames are discarded. Also, since we grow trace
868 frames incrementally as actions are performed, we wrap around to
869 the beginning of the trace buffer. This is per-block, so each
870 block within a trace frame remains contiguous. Things get messy
871 when the wrapped-around trace frame is the one being discarded; the
872 free space ends up in two parts at opposite ends of the buffer. */
875 # if defined(__GNUC__)
876 # define ATTR_PACKED __attribute__ ((packed))
878 # define ATTR_PACKED /* nothing */
882 /* The data collected at a tracepoint hit. This object should be as
883 small as possible, since there may be a great many of them. We do
884 not need to keep a frame number, because they are all sequential
885 and there are no deletions; so the Nth frame in the buffer is
886 always frame number N. */
890 /* Number of the tracepoint that collected this traceframe. A value
891 of 0 indicates the current end of the trace buffer. We make this
892 a 16-bit field because it's never going to happen that GDB's
893 numbering of tracepoints reaches 32,000. */
896 /* The size of the data in this trace frame. We limit this to 32
897 bits, even on a 64-bit target, because it's just implausible that
898 one is validly going to collect 4 gigabytes of data at a single
900 unsigned int data_size
: 32;
902 /* The base of the trace data, which is contiguous from this point. */
903 unsigned char data
[0];
907 /* The traceframe to be used as the source of data to send back to
908 GDB. A value of -1 means to get data from the live program. */
910 int current_traceframe
= -1;
912 /* This flag is true if the trace buffer is circular, meaning that
913 when it fills, the oldest trace frames are discarded in order to
916 #ifndef IN_PROCESS_AGENT
917 static int circular_trace_buffer
;
920 /* Pointer to the block of memory that traceframes all go into. */
922 static unsigned char *trace_buffer_lo
;
924 /* Pointer to the end of the trace buffer, more precisely to the byte
925 after the end of the buffer. */
927 static unsigned char *trace_buffer_hi
;
929 /* Control structure holding the read/write/etc. pointers into the
930 trace buffer. We need more than one of these to implement a
931 transaction-like mechanism to garantees that both GDBserver and the
932 in-process agent can try to change the trace buffer
935 struct trace_buffer_control
937 /* Pointer to the first trace frame in the buffer. In the
938 non-circular case, this is equal to trace_buffer_lo, otherwise it
939 moves around in the buffer. */
940 unsigned char *start
;
942 /* Pointer to the free part of the trace buffer. Note that we clear
943 several bytes at and after this pointer, so that traceframe
944 scans/searches terminate properly. */
947 /* Pointer to the byte after the end of the free part. Note that
948 this may be smaller than trace_buffer_free in the circular case,
949 and means that the free part is in two pieces. Initially it is
950 equal to trace_buffer_hi, then is generally equivalent to
951 trace_buffer_start. */
952 unsigned char *end_free
;
954 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
955 this is the point at which the trace data breaks, and resumes at
960 /* Same as above, to be used by GDBserver when updating the in-process
962 struct ipa_trace_buffer_control
971 /* We have possibly both GDBserver and an inferior thread accessing
972 the same IPA trace buffer memory. The IPA is the producer (tries
973 to put new frames in the buffer), while GDBserver occasionally
974 consumes them, that is, flushes the IPA's buffer into its own
975 buffer. Both sides need to update the trace buffer control
976 pointers (current head, tail, etc.). We can't use a global lock to
977 synchronize the accesses, as otherwise we could deadlock GDBserver
978 (if the thread holding the lock stops for a signal, say). So
979 instead of that, we use a transaction scheme where GDBserver writes
980 always prevail over the IPAs writes, and, we have the IPA detect
981 the commit failure/overwrite, and retry the whole attempt. This is
982 mainly implemented by having a global token object that represents
983 who wrote last to the buffer control structure. We need to freeze
984 any inferior writing to the buffer while GDBserver touches memory,
985 so that the inferior can correctly detect that GDBserver had been
986 there, otherwise, it could mistakingly think its commit was
987 successful; that's implemented by simply having GDBserver set a
988 breakpoint the inferior hits if it is the critical region.
990 There are three cycling trace buffer control structure copies
991 (buffer head, tail, etc.), with the token object including an index
992 indicating which is current live copy. The IPA tentatively builds
993 an updated copy in a non-current control structure, while GDBserver
994 always clobbers the current version directly. The IPA then tries
995 to atomically "commit" its version; if GDBserver clobbered the
996 structure meanwhile, that will fail, and the IPA restarts the
999 Listing the step in further detail, we have:
1001 In-process agent (producer):
1003 - passes by `about_to_request_buffer_space' breakpoint/lock
1005 - reads current token, extracts current trace buffer control index,
1006 and starts tentatively updating the rightmost one (0->1, 1->2,
1007 2->0). Note that only one inferior thread is executing this code
1008 at any given time, due to an outer lock in the jump pads.
1010 - updates counters, and tries to commit the token.
1012 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1013 leaving the sync region.
1015 - checks if the update was effective.
1017 - if trace buffer was found full, hits flush_trace_buffer
1018 breakpoint, and restarts later afterwards.
1020 GDBserver (consumer):
1022 - sets `about_to_request_buffer_space' breakpoint/lock.
1024 - updates the token unconditionally, using the current buffer
1025 control index, since it knows that the IP agent always writes to
1026 the rightmost, and due to the breakpoint, at most one IP thread
1027 can try to update the trace buffer concurrently to GDBserver, so
1028 there will be no danger of trace buffer control index wrap making
1029 the IPA write to the same index as GDBserver.
1031 - flushes the IP agent's trace buffer completely, and updates the
1032 current trace buffer control structure. GDBserver *always* wins.
1034 - removes the `about_to_request_buffer_space' breakpoint.
1036 The token is stored in the `trace_buffer_ctrl_curr' variable.
1037 Internally, it's bits are defined as:
1039 |-------------+-----+-------------+--------+-------------+--------------|
1040 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1041 |-------------+-----+-------------+--------+-------------+--------------|
1042 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1043 |-------------+-----+-------------+--------+-------------+--------------|
1045 GSB - GDBserver Stamp Bit
1046 PC - Previous Counter
1047 CC - Current Counter
1048 TBCI - Trace Buffer Control Index
1051 An IPA update of `trace_buffer_ctrl_curr' does:
1053 - read CC from the current token, save as PC.
1055 - atomically tries to write PC+1,CC
1057 A GDBserver update of `trace_buffer_ctrl_curr' does:
1059 - reads PC and CC from the current token.
1064 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1065 for the counters described below. The cleared bits are used to
1066 hold the index of the items of the `trace_buffer_ctrl' array that
1068 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1070 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1071 counter, and the `current' counter. */
1073 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1074 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1076 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1077 always stamps this bit as set. */
1078 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1080 #ifdef IN_PROCESS_AGENT
1081 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1082 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1084 # define TRACE_BUFFER_CTRL_CURR \
1085 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1089 /* The GDBserver side agent only needs one instance of this object, as
1090 it doesn't need to sync with itself. Define it as array anyway so
1091 that the rest of the code base doesn't need to care for the
1093 struct trace_buffer_control trace_buffer_ctrl
[1];
1094 # define TRACE_BUFFER_CTRL_CURR 0
1097 /* These are convenience macros used to access the current trace
1098 buffer control in effect. */
1099 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1100 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1101 #define trace_buffer_end_free \
1102 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1103 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1106 /* Macro that returns a pointer to the first traceframe in the buffer. */
1108 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1110 /* Macro that returns a pointer to the next traceframe in the buffer.
1111 If the computed location is beyond the wraparound point, subtract
1112 the offset of the wraparound. */
1114 #define NEXT_TRACEFRAME_1(TF) \
1115 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1117 #define NEXT_TRACEFRAME(TF) \
1118 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1119 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1120 ? (trace_buffer_wrap - trace_buffer_lo) \
1123 /* The difference between these counters represents the total number
1124 of complete traceframes present in the trace buffer. The IP agent
1125 writes to the write count, GDBserver writes to read count. */
1127 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1128 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1130 /* Convenience macro. */
1132 #define traceframe_count \
1133 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1135 /* The count of all traceframes created in the current run, including
1136 ones that were discarded to make room. */
1138 IP_AGENT_EXPORT
int traceframes_created
;
1140 #ifndef IN_PROCESS_AGENT
1142 /* Read-only regions are address ranges whose contents don't change,
1143 and so can be read from target memory even while looking at a trace
1144 frame. Without these, disassembly for instance will likely fail,
1145 because the program code is not usually collected into a trace
1146 frame. This data structure does not need to be very complicated or
1147 particularly efficient, it's only going to be used occasionally,
1148 and only by some commands. */
1150 struct readonly_region
1152 /* The bounds of the region. */
1153 CORE_ADDR start
, end
;
1155 /* Link to the next one. */
1156 struct readonly_region
*next
;
1159 /* Linked list of readonly regions. This list stays in effect from
1160 one tstart to the next. */
1162 static struct readonly_region
*readonly_regions
;
1166 /* The global that controls tracing overall. */
1168 IP_AGENT_EXPORT
int tracing
;
1170 #ifndef IN_PROCESS_AGENT
1172 /* Controls whether tracing should continue after GDB disconnects. */
1174 int disconnected_tracing
;
1176 /* The reason for the last tracing run to have stopped. We initialize
1177 to a distinct string so that GDB can distinguish between "stopped
1178 after running" and "stopped because never run" cases. */
1180 static const char *tracing_stop_reason
= "tnotrun";
1182 static int tracing_stop_tpnum
;
1184 /* 64-bit timestamps for the trace run's start and finish, expressed
1185 in microseconds from the Unix epoch. */
1187 LONGEST tracing_start_time
;
1188 LONGEST tracing_stop_time
;
1190 /* The (optional) user-supplied name of the user that started the run.
1191 This is an arbitrary string, and may be NULL. */
1193 char *tracing_user_name
;
1195 /* Optional user-supplied text describing the run. This is
1196 an arbitrary string, and may be NULL. */
1198 char *tracing_notes
;
1200 /* Optional user-supplied text explaining a tstop command. This is an
1201 arbitrary string, and may be NULL. */
1203 char *tracing_stop_note
;
1207 /* Functions local to this file. */
1209 /* Base "class" for tracepoint type specific data to be passed down to
1210 collect_data_at_tracepoint. */
1211 struct tracepoint_hit_ctx
1213 enum tracepoint_type type
;
1216 #ifdef IN_PROCESS_AGENT
1218 /* Fast/jump tracepoint specific data to be passed down to
1219 collect_data_at_tracepoint. */
1220 struct fast_tracepoint_ctx
1222 struct tracepoint_hit_ctx base
;
1224 struct regcache regcache
;
1225 int regcache_initted
;
1226 unsigned char *regspace
;
1228 unsigned char *regs
;
1229 struct tracepoint
*tpoint
;
1232 /* Static tracepoint specific data to be passed down to
1233 collect_data_at_tracepoint. */
1234 struct static_tracepoint_ctx
1236 struct tracepoint_hit_ctx base
;
1238 /* The regcache corresponding to the registers state at the time of
1239 the tracepoint hit. Initialized lazily, from REGS. */
1240 struct regcache regcache
;
1241 int regcache_initted
;
1243 /* The buffer space REGCACHE above uses. We use a separate buffer
1244 instead of letting the regcache malloc for both signal safety and
1245 performance reasons; this is allocated on the stack instead. */
1246 unsigned char *regspace
;
1248 /* The register buffer as passed on by lttng/ust. */
1249 struct registers
*regs
;
1251 /* The "printf" formatter and the args the user passed to the marker
1252 call. We use this to be able to collect "static trace data"
1257 /* The GDB tracepoint matching the probed marker that was "hit". */
1258 struct tracepoint
*tpoint
;
1263 /* Static tracepoint specific data to be passed down to
1264 collect_data_at_tracepoint. */
1265 struct trap_tracepoint_ctx
1267 struct tracepoint_hit_ctx base
;
1269 struct regcache
*regcache
;
1274 static enum eval_result_type
1275 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1276 struct traceframe
*tframe
,
1277 struct agent_expr
*aexpr
,
1280 #ifndef IN_PROCESS_AGENT
1281 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1282 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1285 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1286 struct tracepoint
*tpoint
);
1288 #ifndef IN_PROCESS_AGENT
1289 static void clear_readonly_regions (void);
1290 static void clear_installed_tracepoints (void);
1293 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1295 struct tracepoint
*tpoint
);
1296 #ifndef IN_PROCESS_AGENT
1297 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1299 struct tracepoint
*tpoint
, int current_step
);
1300 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1301 CORE_ADDR
*jump_entry
);
1303 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1305 struct tracepoint
*tpoint
,
1306 struct traceframe
*tframe
,
1307 struct tracepoint_action
*taction
);
1309 #ifndef IN_PROCESS_AGENT
1310 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1312 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1313 static void download_tracepoint (struct tracepoint
*);
1314 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1315 static void clone_fast_tracepoint (struct tracepoint
*to
,
1316 const struct tracepoint
*from
);
1319 static LONGEST
get_timestamp (void);
1321 #if defined(__GNUC__)
1322 # define memory_barrier() asm volatile ("" : : : "memory")
1324 # define memory_barrier() do {} while (0)
1327 /* We only build the IPA if this builtin is supported, and there are
1328 no uses of this in GDBserver itself, so we're safe in defining this
1330 #define cmpxchg(mem, oldval, newval) \
1331 __sync_val_compare_and_swap (mem, oldval, newval)
1333 /* Record that an error occurred during expression evaluation. */
1336 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1337 enum eval_result_type rtype
)
1339 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1340 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1342 #ifdef IN_PROCESS_AGENT
1343 /* Only record the first error we get. */
1344 if (cmpxchg (&expr_eval_result
,
1346 rtype
) != expr_eval_no_error
)
1349 if (expr_eval_result
!= expr_eval_no_error
)
1353 error_tracepoint
= tpoint
;
1356 /* Trace buffer management. */
1359 clear_trace_buffer (void)
1361 trace_buffer_start
= trace_buffer_lo
;
1362 trace_buffer_free
= trace_buffer_lo
;
1363 trace_buffer_end_free
= trace_buffer_hi
;
1364 trace_buffer_wrap
= trace_buffer_hi
;
1365 /* A traceframe with zeroed fields marks the end of trace data. */
1366 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1367 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1368 traceframe_read_count
= traceframe_write_count
= 0;
1369 traceframes_created
= 0;
1372 #ifndef IN_PROCESS_AGENT
1375 clear_inferior_trace_buffer (void)
1377 CORE_ADDR ipa_trace_buffer_lo
;
1378 CORE_ADDR ipa_trace_buffer_hi
;
1379 struct traceframe ipa_traceframe
= { 0 };
1380 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1382 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1383 &ipa_trace_buffer_lo
);
1384 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1385 &ipa_trace_buffer_hi
);
1387 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1388 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1389 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1390 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1392 /* A traceframe with zeroed fields marks the end of trace data. */
1393 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1394 (unsigned char *) &ipa_trace_buffer_ctrl
,
1395 sizeof (ipa_trace_buffer_ctrl
));
1397 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1399 /* A traceframe with zeroed fields marks the end of trace data. */
1400 write_inferior_memory (ipa_trace_buffer_lo
,
1401 (unsigned char *) &ipa_traceframe
,
1402 sizeof (ipa_traceframe
));
1404 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1405 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1406 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1412 init_trace_buffer (unsigned char *buf
, int bufsize
)
1414 trace_buffer_lo
= buf
;
1415 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1417 clear_trace_buffer ();
1420 #ifdef IN_PROCESS_AGENT
1422 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1423 about_to_request_buffer_space (void)
1425 /* GDBserver places breakpoint here while it goes about to flush
1426 data at random times. */
1427 UNKNOWN_SIDE_EFFECTS();
1432 /* Carve out a piece of the trace buffer, returning NULL in case of
1436 trace_buffer_alloc (size_t amt
)
1438 unsigned char *rslt
;
1439 struct trace_buffer_control
*tbctrl
;
1441 #ifdef IN_PROCESS_AGENT
1442 unsigned int prev
, prev_filtered
;
1443 unsigned int commit_count
;
1444 unsigned int commit
;
1445 unsigned int readout
;
1447 struct traceframe
*oldest
;
1448 unsigned char *new_start
;
1451 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1452 (long) amt
, (long) sizeof (struct traceframe
));
1454 /* Account for the EOB marker. */
1455 amt
+= sizeof (struct traceframe
);
1457 #ifdef IN_PROCESS_AGENT
1461 /* Read the current token and extract the index to try to write to,
1462 storing it in CURR. */
1463 prev
= trace_buffer_ctrl_curr
;
1464 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1465 curr
= prev_filtered
+ 1;
1469 about_to_request_buffer_space ();
1471 /* Start out with a copy of the current state. GDBserver may be
1472 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1473 be able to commit anyway if that happens. */
1474 trace_buffer_ctrl
[curr
]
1475 = trace_buffer_ctrl
[prev_filtered
];
1476 trace_debug ("trying curr=%u", curr
);
1478 /* The GDBserver's agent doesn't need all that syncing, and always
1479 updates TCB 0 (there's only one, mind you). */
1482 tbctrl
= &trace_buffer_ctrl
[curr
];
1484 /* Offsets are easier to grok for debugging than raw addresses,
1485 especially for the small trace buffer sizes that are useful for
1487 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1489 (int) (tbctrl
->start
- trace_buffer_lo
),
1490 (int) (tbctrl
->free
- trace_buffer_lo
),
1491 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1492 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1493 (int) (trace_buffer_hi
- trace_buffer_lo
));
1495 /* The algorithm here is to keep trying to get a contiguous block of
1496 the requested size, possibly discarding older traceframes to free
1497 up space. Since free space might come in one or two pieces,
1498 depending on whether discarded traceframes wrapped around at the
1499 high end of the buffer, we test both pieces after each
1503 /* First, if we have two free parts, try the upper one first. */
1504 if (tbctrl
->end_free
< tbctrl
->free
)
1506 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1507 /* We have enough in the upper part. */
1511 /* Our high part of free space wasn't enough. Give up
1512 on it for now, set wraparound. We will recover the
1513 space later, if/when the wrapped-around traceframe is
1515 trace_debug ("Upper part too small, setting wraparound");
1516 tbctrl
->wrap
= tbctrl
->free
;
1517 tbctrl
->free
= trace_buffer_lo
;
1521 /* The normal case. */
1522 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1525 #ifdef IN_PROCESS_AGENT
1526 /* The IP Agent's buffer is always circular. It isn't used
1527 currently, but `circular_trace_buffer' could represent
1528 GDBserver's mode. If we didn't find space, ask GDBserver to
1531 flush_trace_buffer ();
1535 trace_debug ("gdbserver flushed buffer, retrying");
1539 /* GDBserver cancelled the tracing. Bail out as well. */
1542 /* If we're here, then neither part is big enough, and
1543 non-circular trace buffers are now full. */
1544 if (!circular_trace_buffer
)
1546 trace_debug ("Not enough space in the trace buffer");
1550 trace_debug ("Need more space in the trace buffer");
1552 /* If we have a circular buffer, we can try discarding the
1553 oldest traceframe and see if that helps. */
1554 oldest
= FIRST_TRACEFRAME ();
1555 if (oldest
->tpnum
== 0)
1557 /* Not good; we have no traceframes to free. Perhaps we're
1558 asking for a block that is larger than the buffer? In
1559 any case, give up. */
1560 trace_debug ("No traceframes to discard");
1564 /* We don't run this code in the in-process agent currently.
1565 E.g., we could leave the in-process agent in autonomous
1566 circular mode if we only have fast tracepoints. If we do
1567 that, then this bit becomes racy with GDBserver, which also
1568 writes to this counter. */
1569 --traceframe_write_count
;
1571 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1572 /* If we freed the traceframe that wrapped around, go back
1573 to the non-wrap case. */
1574 if (new_start
< tbctrl
->start
)
1576 trace_debug ("Discarding past the wraparound");
1577 tbctrl
->wrap
= trace_buffer_hi
;
1579 tbctrl
->start
= new_start
;
1580 tbctrl
->end_free
= tbctrl
->start
;
1582 trace_debug ("Discarded a traceframe\n"
1583 "Trace buffer [%d], start=%d free=%d "
1584 "endfree=%d wrap=%d hi=%d",
1586 (int) (tbctrl
->start
- trace_buffer_lo
),
1587 (int) (tbctrl
->free
- trace_buffer_lo
),
1588 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1589 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1590 (int) (trace_buffer_hi
- trace_buffer_lo
));
1592 /* Now go back around the loop. The discard might have resulted
1593 in either one or two pieces of free space, so we want to try
1594 both before freeing any more traceframes. */
1598 /* If we get here, we know we can provide the asked-for space. */
1600 rslt
= tbctrl
->free
;
1602 /* Adjust the request back down, now that we know we have space for
1603 the marker, but don't commit to AMT yet, we may still need to
1604 restart the operation if GDBserver touches the trace buffer
1605 (obviously only important in the in-process agent's version). */
1606 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1608 /* Or not. If GDBserver changed the trace buffer behind our back,
1609 we get to restart a new allocation attempt. */
1611 #ifdef IN_PROCESS_AGENT
1612 /* Build the tentative token. */
1613 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1614 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1615 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1619 /* Try to commit it. */
1620 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1621 if (readout
!= prev
)
1623 trace_debug ("GDBserver has touched the trace buffer, restarting."
1624 " (prev=%08x, commit=%08x, readout=%08x)",
1625 prev
, commit
, readout
);
1629 /* Hold your horses here. Even if that change was committed,
1630 GDBserver could come in, and clobber it. We need to hold to be
1631 able to tell if GDBserver clobbers before or after we committed
1632 the change. Whenever GDBserver goes about touching the IPA
1633 buffer, it sets a breakpoint in this routine, so we have a sync
1635 about_to_request_buffer_space ();
1637 /* Check if the change has been effective, even if GDBserver stopped
1638 us at the breakpoint. */
1641 unsigned int refetch
;
1645 refetch
= trace_buffer_ctrl_curr
;
1647 if (refetch
== commit
1648 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1651 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1652 "readout=%08x, refetch=%08x)",
1653 prev
, commit
, readout
, refetch
);
1657 trace_debug ("GDBserver has touched the trace buffer, not effective."
1658 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1659 prev
, commit
, readout
, refetch
);
1665 /* We have a new piece of the trace buffer. Hurray! */
1667 /* Add an EOB marker just past this allocation. */
1668 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1669 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1671 /* Adjust the request back down, now that we know we have space for
1673 amt
-= sizeof (struct traceframe
);
1677 trace_debug ("Allocated %d bytes", (int) amt
);
1678 trace_debug ("Trace buffer [%d] start=%d free=%d "
1679 "endfree=%d wrap=%d hi=%d",
1681 (int) (tbctrl
->start
- trace_buffer_lo
),
1682 (int) (tbctrl
->free
- trace_buffer_lo
),
1683 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1684 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1685 (int) (trace_buffer_hi
- trace_buffer_lo
));
1691 #ifndef IN_PROCESS_AGENT
1693 /* Return the total free space. This is not necessarily the largest
1694 block we can allocate, because of the two-part case. */
1699 if (trace_buffer_free
<= trace_buffer_end_free
)
1700 return trace_buffer_end_free
- trace_buffer_free
;
1702 return ((trace_buffer_end_free
- trace_buffer_lo
)
1703 + (trace_buffer_hi
- trace_buffer_free
));
1706 /* An 'S' in continuation packets indicates remainder are for
1709 static int seen_step_action_flag
;
1711 /* Create a tracepoint (location) with given number and address. Add this
1712 new tracepoint to list and sort this list. */
1714 static struct tracepoint
*
1715 add_tracepoint (int num
, CORE_ADDR addr
)
1717 struct tracepoint
*tpoint
, **tp_next
;
1719 tpoint
= xmalloc (sizeof (struct tracepoint
));
1720 tpoint
->number
= num
;
1721 tpoint
->address
= addr
;
1722 tpoint
->numactions
= 0;
1723 tpoint
->actions
= NULL
;
1724 tpoint
->actions_str
= NULL
;
1725 tpoint
->cond
= NULL
;
1726 tpoint
->num_step_actions
= 0;
1727 tpoint
->step_actions
= NULL
;
1728 tpoint
->step_actions_str
= NULL
;
1729 /* Start all off as regular (slow) tracepoints. */
1730 tpoint
->type
= trap_tracepoint
;
1731 tpoint
->orig_size
= -1;
1732 tpoint
->source_strings
= NULL
;
1733 tpoint
->compiled_cond
= 0;
1734 tpoint
->handle
= NULL
;
1735 tpoint
->next
= NULL
;
1737 /* Find a place to insert this tracepoint into list in order to keep
1738 the tracepoint list still in the ascending order. There may be
1739 multiple tracepoints at the same address as TPOINT's, and this
1740 guarantees TPOINT is inserted after all the tracepoints which are
1741 set at the same address. For example, fast tracepoints A, B, C are
1742 set at the same address, and D is to be insert at the same place as
1745 -->| A |--> | B |-->| C |->...
1747 One jump pad was created for tracepoint A, B, and C, and the target
1748 address of A is referenced/used in jump pad. So jump pad will let
1749 inferior jump to A. If D is inserted in front of A, like this,
1751 -->| D |-->| A |--> | B |-->| C |->...
1753 without updating jump pad, D is not reachable during collect, which
1754 is wrong. As we can see, the order of B, C and D doesn't matter, but
1755 A should always be the `first' one. */
1756 for (tp_next
= &tracepoints
;
1757 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1758 tp_next
= &(*tp_next
)->next
)
1760 tpoint
->next
= *tp_next
;
1762 last_tracepoint
= tpoint
;
1764 seen_step_action_flag
= 0;
1769 #ifndef IN_PROCESS_AGENT
1771 /* Return the tracepoint with the given number and address, or NULL. */
1773 static struct tracepoint
*
1774 find_tracepoint (int id
, CORE_ADDR addr
)
1776 struct tracepoint
*tpoint
;
1778 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1779 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1785 /* Remove TPOINT from global list. */
1788 remove_tracepoint (struct tracepoint
*tpoint
)
1790 struct tracepoint
*tp
, *tp_prev
;
1792 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1793 tp_prev
= tp
, tp
= tp
->next
)
1799 tp_prev
->next
= tp
->next
;
1801 tracepoints
= tp
->next
;
1807 /* There may be several tracepoints with the same number (because they
1808 are "locations", in GDB parlance); return the next one after the
1809 given tracepoint, or search from the beginning of the list if the
1810 first argument is NULL. */
1812 static struct tracepoint
*
1813 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1815 struct tracepoint
*tpoint
;
1818 tpoint
= prev_tp
->next
;
1820 tpoint
= tracepoints
;
1821 for (; tpoint
; tpoint
= tpoint
->next
)
1822 if (tpoint
->number
== num
)
1831 save_string (const char *str
, size_t len
)
1835 s
= xmalloc (len
+ 1);
1836 memcpy (s
, str
, len
);
1842 /* Append another action to perform when the tracepoint triggers. */
1845 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1851 seen_step_action_flag
= 1;
1859 char *act_start
= act
;
1860 struct tracepoint_action
*action
= NULL
;
1866 struct collect_memory_action
*maction
;
1870 maction
= xmalloc (sizeof *maction
);
1871 maction
->base
.type
= *act
;
1872 maction
->base
.ops
= &m_tracepoint_action_ops
;
1873 action
= &maction
->base
;
1876 is_neg
= (*act
== '-');
1879 act
= unpack_varlen_hex (act
, &basereg
);
1881 act
= unpack_varlen_hex (act
, &maction
->addr
);
1883 act
= unpack_varlen_hex (act
, &maction
->len
);
1884 maction
->basereg
= (is_neg
1887 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1888 pulongest (maction
->len
),
1889 paddress (maction
->addr
), maction
->basereg
);
1894 struct collect_registers_action
*raction
;
1896 raction
= xmalloc (sizeof *raction
);
1897 raction
->base
.type
= *act
;
1898 raction
->base
.ops
= &r_tracepoint_action_ops
;
1899 action
= &raction
->base
;
1901 trace_debug ("Want to collect registers");
1903 /* skip past hex digits of mask for now */
1904 while (isxdigit(*act
))
1910 struct collect_static_trace_data_action
*raction
;
1912 raction
= xmalloc (sizeof *raction
);
1913 raction
->base
.type
= *act
;
1914 raction
->base
.ops
= &l_tracepoint_action_ops
;
1915 action
= &raction
->base
;
1917 trace_debug ("Want to collect static trace data");
1922 trace_debug ("Unexpected step action, ignoring");
1927 struct eval_expr_action
*xaction
;
1929 xaction
= xmalloc (sizeof (*xaction
));
1930 xaction
->base
.type
= *act
;
1931 xaction
->base
.ops
= &x_tracepoint_action_ops
;
1932 action
= &xaction
->base
;
1934 trace_debug ("Want to evaluate expression");
1935 xaction
->expr
= gdb_parse_agent_expr (&act
);
1939 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1948 if (seen_step_action_flag
)
1950 tpoint
->num_step_actions
++;
1952 tpoint
->step_actions
1953 = xrealloc (tpoint
->step_actions
,
1954 (sizeof (*tpoint
->step_actions
)
1955 * tpoint
->num_step_actions
));
1956 tpoint
->step_actions_str
1957 = xrealloc (tpoint
->step_actions_str
,
1958 (sizeof (*tpoint
->step_actions_str
)
1959 * tpoint
->num_step_actions
));
1960 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1961 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1962 = save_string (act_start
, act
- act_start
);
1966 tpoint
->numactions
++;
1968 = xrealloc (tpoint
->actions
,
1969 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1971 = xrealloc (tpoint
->actions_str
,
1972 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1973 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1974 tpoint
->actions_str
[tpoint
->numactions
- 1]
1975 = save_string (act_start
, act
- act_start
);
1982 /* Find or create a trace state variable with the given number. */
1984 static struct trace_state_variable
*
1985 get_trace_state_variable (int num
)
1987 struct trace_state_variable
*tsv
;
1989 #ifdef IN_PROCESS_AGENT
1990 /* Search for an existing variable. */
1991 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1992 if (tsv
->number
== num
)
1996 /* Search for an existing variable. */
1997 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1998 if (tsv
->number
== num
)
2004 /* Find or create a trace state variable with the given number. */
2006 static struct trace_state_variable
*
2007 create_trace_state_variable (int num
, int gdb
)
2009 struct trace_state_variable
*tsv
;
2011 tsv
= get_trace_state_variable (num
);
2015 /* Create a new variable. */
2016 tsv
= xmalloc (sizeof (struct trace_state_variable
));
2018 tsv
->initial_value
= 0;
2022 #ifdef IN_PROCESS_AGENT
2025 tsv
->next
= alloced_trace_state_variables
;
2026 alloced_trace_state_variables
= tsv
;
2031 tsv
->next
= trace_state_variables
;
2032 trace_state_variables
= tsv
;
2037 IP_AGENT_EXPORT LONGEST
2038 get_trace_state_variable_value (int num
)
2040 struct trace_state_variable
*tsv
;
2042 tsv
= get_trace_state_variable (num
);
2046 trace_debug ("No trace state variable %d, skipping value get", num
);
2050 /* Call a getter function if we have one. While it's tempting to
2051 set up something to only call the getter once per tracepoint hit,
2052 it could run afoul of thread races. Better to let the getter
2053 handle it directly, if necessary to worry about it. */
2055 tsv
->value
= (tsv
->getter
) ();
2057 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2058 num
, plongest (tsv
->value
));
2063 IP_AGENT_EXPORT
void
2064 set_trace_state_variable_value (int num
, LONGEST val
)
2066 struct trace_state_variable
*tsv
;
2068 tsv
= get_trace_state_variable (num
);
2072 trace_debug ("No trace state variable %d, skipping value set", num
);
2080 agent_get_trace_state_variable_value (int num
)
2082 return get_trace_state_variable_value (num
);
2086 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2088 set_trace_state_variable_value (num
, val
);
2092 set_trace_state_variable_name (int num
, const char *name
)
2094 struct trace_state_variable
*tsv
;
2096 tsv
= get_trace_state_variable (num
);
2100 trace_debug ("No trace state variable %d, skipping name set", num
);
2104 tsv
->name
= (char *) name
;
2108 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2110 struct trace_state_variable
*tsv
;
2112 tsv
= get_trace_state_variable (num
);
2116 trace_debug ("No trace state variable %d, skipping getter set", num
);
2120 tsv
->getter
= getter
;
2123 /* Add a raw traceframe for the given tracepoint. */
2125 static struct traceframe
*
2126 add_traceframe (struct tracepoint
*tpoint
)
2128 struct traceframe
*tframe
;
2130 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2135 tframe
->tpnum
= tpoint
->number
;
2136 tframe
->data_size
= 0;
2141 /* Add a block to the traceframe currently being worked on. */
2143 static unsigned char *
2144 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2146 unsigned char *block
;
2151 block
= trace_buffer_alloc (amt
);
2156 tframe
->data_size
+= amt
;
2161 /* Flag that the current traceframe is finished. */
2164 finish_traceframe (struct traceframe
*tframe
)
2166 ++traceframe_write_count
;
2167 ++traceframes_created
;
2170 #ifndef IN_PROCESS_AGENT
2172 /* Given a traceframe number NUM, find the NUMth traceframe in the
2175 static struct traceframe
*
2176 find_traceframe (int num
)
2178 struct traceframe
*tframe
;
2181 for (tframe
= FIRST_TRACEFRAME ();
2183 tframe
= NEXT_TRACEFRAME (tframe
))
2194 get_traceframe_address (struct traceframe
*tframe
)
2197 struct tracepoint
*tpoint
;
2199 addr
= traceframe_get_pc (tframe
);
2204 /* Fallback strategy, will be incorrect for while-stepping frames
2205 and multi-location tracepoints. */
2206 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2207 return tpoint
->address
;
2210 /* Search for the next traceframe whose address is inside or outside
2213 static struct traceframe
*
2214 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2217 struct traceframe
*tframe
;
2220 *tfnump
= current_traceframe
+ 1;
2221 tframe
= find_traceframe (*tfnump
);
2222 /* The search is not supposed to wrap around. */
2229 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2231 tfaddr
= get_traceframe_address (tframe
);
2233 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2234 : (lo
> tfaddr
|| tfaddr
> hi
))
2243 /* Search for the next traceframe recorded by the given tracepoint.
2244 Note that for multi-location tracepoints, this will find whatever
2245 location appears first. */
2247 static struct traceframe
*
2248 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2250 struct traceframe
*tframe
;
2252 *tfnump
= current_traceframe
+ 1;
2253 tframe
= find_traceframe (*tfnump
);
2254 /* The search is not supposed to wrap around. */
2261 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2263 if (tframe
->tpnum
== num
)
2274 #ifndef IN_PROCESS_AGENT
2276 /* Clear all past trace state. */
2279 cmd_qtinit (char *packet
)
2281 struct trace_state_variable
*tsv
, *prev
, *next
;
2283 /* Make sure we don't try to read from a trace frame. */
2284 current_traceframe
= -1;
2286 trace_debug ("Initializing the trace");
2288 clear_installed_tracepoints ();
2289 clear_readonly_regions ();
2292 last_tracepoint
= NULL
;
2294 /* Clear out any leftover trace state variables. Ones with target
2295 defined getters should be kept however. */
2297 tsv
= trace_state_variables
;
2300 trace_debug ("Looking at var %d", tsv
->number
);
2301 if (tsv
->getter
== NULL
)
2307 trace_state_variables
= next
;
2308 trace_debug ("Deleting var %d", tsv
->number
);
2319 clear_trace_buffer ();
2320 clear_inferior_trace_buffer ();
2325 /* Unprobe the UST marker at ADDRESS. */
2328 unprobe_marker_at (CORE_ADDR address
)
2330 char cmd
[IPA_CMD_BUF_SIZE
];
2332 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2333 run_inferior_command (cmd
);
2336 /* Restore the program to its pre-tracing state. This routine may be called
2337 in error situations, so it needs to be careful about only restoring
2338 from known-valid bits. */
2341 clear_installed_tracepoints (void)
2343 struct tracepoint
*tpoint
;
2344 struct tracepoint
*prev_stpoint
;
2347 cancel_breakpoints ();
2349 prev_stpoint
= NULL
;
2351 /* Restore any bytes overwritten by tracepoints. */
2352 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2354 /* Catch the case where we might try to remove a tracepoint that
2355 was never actually installed. */
2356 if (tpoint
->handle
== NULL
)
2358 trace_debug ("Tracepoint %d at 0x%s was "
2359 "never installed, nothing to clear",
2360 tpoint
->number
, paddress (tpoint
->address
));
2364 switch (tpoint
->type
)
2366 case trap_tracepoint
:
2367 delete_breakpoint (tpoint
->handle
);
2369 case fast_tracepoint
:
2370 delete_fast_tracepoint_jump (tpoint
->handle
);
2372 case static_tracepoint
:
2373 if (prev_stpoint
!= NULL
2374 && prev_stpoint
->address
== tpoint
->address
)
2375 /* Nothing to do. We already unprobed a tracepoint set at
2376 this marker address (and there can only be one probe
2381 unprobe_marker_at (tpoint
->address
);
2382 prev_stpoint
= tpoint
;
2387 tpoint
->handle
= NULL
;
2393 /* Parse a packet that defines a tracepoint. */
2396 cmd_qtdp (char *own_buf
)
2399 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2400 int trail_hyphen
= 0;
2404 struct tracepoint
*tpoint
;
2406 char *packet
= own_buf
;
2408 packet
+= strlen ("QTDP:");
2410 /* A hyphen at the beginning marks a packet specifying actions for a
2411 tracepoint already supplied. */
2418 packet
= unpack_varlen_hex (packet
, &num
);
2419 ++packet
; /* skip a colon */
2420 packet
= unpack_varlen_hex (packet
, &addr
);
2421 ++packet
; /* skip a colon */
2423 /* See if we already have this tracepoint. */
2424 tpoint
= find_tracepoint (num
, addr
);
2428 /* Duplicate tracepoints are never allowed. */
2431 trace_debug ("Tracepoint error: tracepoint %d"
2432 " at 0x%s already exists",
2433 (int) num
, paddress (addr
));
2434 write_enn (own_buf
);
2438 tpoint
= add_tracepoint (num
, addr
);
2440 tpoint
->enabled
= (*packet
== 'E');
2441 ++packet
; /* skip 'E' */
2442 ++packet
; /* skip a colon */
2443 packet
= unpack_varlen_hex (packet
, &count
);
2444 tpoint
->step_count
= count
;
2445 ++packet
; /* skip a colon */
2446 packet
= unpack_varlen_hex (packet
, &count
);
2447 tpoint
->pass_count
= count
;
2448 /* See if we have any of the additional optional fields. */
2449 while (*packet
== ':')
2454 tpoint
->type
= fast_tracepoint
;
2456 packet
= unpack_varlen_hex (packet
, &count
);
2457 tpoint
->orig_size
= count
;
2459 else if (*packet
== 'S')
2461 tpoint
->type
= static_tracepoint
;
2464 else if (*packet
== 'X')
2466 actparm
= (char *) packet
;
2467 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2470 else if (*packet
== '-')
2472 else if (*packet
== '\0')
2475 trace_debug ("Unknown optional tracepoint field");
2480 trace_debug ("Also has actions\n");
2483 trace_debug ("Defined %stracepoint %d at 0x%s, "
2484 "enabled %d step %ld pass %ld",
2485 tpoint
->type
== fast_tracepoint
? "fast "
2486 : tpoint
->type
== static_tracepoint
? "static " : "",
2487 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2488 tpoint
->step_count
, tpoint
->pass_count
);
2491 add_tracepoint_action (tpoint
, packet
);
2494 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2495 (int) num
, paddress (addr
));
2496 write_enn (own_buf
);
2500 /* Install tracepoint during tracing only once for each tracepoint location.
2501 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2502 determine the last QTDP packet for one tracepoint location by checking
2503 trailing hyphen in QTDP packet. */
2504 if (tracing
&& !trail_hyphen
)
2506 struct tracepoint
*tp
= NULL
;
2508 /* Pause all threads temporarily while we patch tracepoints. */
2511 /* download_tracepoint will update global `tracepoints'
2512 list, so it is unsafe to leave threads in jump pad. */
2513 stabilize_threads ();
2515 /* Freeze threads. */
2519 if (tpoint
->type
!= trap_tracepoint
)
2521 /* Find another fast or static tracepoint at the same address. */
2522 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2524 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2525 && tp
->number
!= tpoint
->number
)
2529 /* TPOINT is installed at the same address as TP. */
2532 if (tpoint
->type
== fast_tracepoint
)
2533 clone_fast_tracepoint (tpoint
, tp
);
2534 else if (tpoint
->type
== static_tracepoint
)
2535 tpoint
->handle
= (void *) -1;
2539 download_tracepoint (tpoint
);
2541 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2543 install_tracepoint (tpoint
, own_buf
);
2544 if (strcmp (own_buf
, "OK") != 0)
2545 remove_tracepoint (tpoint
);
2558 cmd_qtdpsrc (char *own_buf
)
2560 ULONGEST num
, addr
, start
, slen
;
2561 struct tracepoint
*tpoint
;
2562 char *packet
= own_buf
;
2563 char *saved
, *srctype
, *src
;
2565 struct source_string
*last
, *newlast
;
2567 packet
+= strlen ("QTDPsrc:");
2569 packet
= unpack_varlen_hex (packet
, &num
);
2570 ++packet
; /* skip a colon */
2571 packet
= unpack_varlen_hex (packet
, &addr
);
2572 ++packet
; /* skip a colon */
2574 /* See if we already have this tracepoint. */
2575 tpoint
= find_tracepoint (num
, addr
);
2579 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2580 (int) num
, paddress (addr
));
2581 write_enn (own_buf
);
2586 packet
= strchr (packet
, ':');
2587 srctype
= xmalloc (packet
- saved
+ 1);
2588 memcpy (srctype
, saved
, packet
- saved
);
2589 srctype
[packet
- saved
] = '\0';
2591 packet
= unpack_varlen_hex (packet
, &start
);
2592 ++packet
; /* skip a colon */
2593 packet
= unpack_varlen_hex (packet
, &slen
);
2594 ++packet
; /* skip a colon */
2595 src
= xmalloc (slen
+ 1);
2596 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2599 newlast
= xmalloc (sizeof (struct source_string
));
2600 newlast
->type
= srctype
;
2602 newlast
->next
= NULL
;
2603 /* Always add a source string to the end of the list;
2604 this keeps sequences of actions/commands in the right
2606 if (tpoint
->source_strings
)
2608 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2610 last
->next
= newlast
;
2613 tpoint
->source_strings
= newlast
;
2619 cmd_qtdv (char *own_buf
)
2621 ULONGEST num
, val
, builtin
;
2624 struct trace_state_variable
*tsv
;
2625 char *packet
= own_buf
;
2627 packet
+= strlen ("QTDV:");
2629 packet
= unpack_varlen_hex (packet
, &num
);
2630 ++packet
; /* skip a colon */
2631 packet
= unpack_varlen_hex (packet
, &val
);
2632 ++packet
; /* skip a colon */
2633 packet
= unpack_varlen_hex (packet
, &builtin
);
2634 ++packet
; /* skip a colon */
2636 nbytes
= strlen (packet
) / 2;
2637 varname
= xmalloc (nbytes
+ 1);
2638 nbytes
= unhexify (varname
, packet
, nbytes
);
2639 varname
[nbytes
] = '\0';
2641 tsv
= create_trace_state_variable (num
, 1);
2642 tsv
->initial_value
= (LONGEST
) val
;
2643 tsv
->name
= varname
;
2645 set_trace_state_variable_value (num
, (LONGEST
) val
);
2651 cmd_qtenable_disable (char *own_buf
, int enable
)
2653 char *packet
= own_buf
;
2655 struct tracepoint
*tp
;
2657 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2658 packet
= unpack_varlen_hex (packet
, &num
);
2659 ++packet
; /* skip a colon */
2660 packet
= unpack_varlen_hex (packet
, &addr
);
2662 tp
= find_tracepoint (num
, addr
);
2666 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2668 trace_debug ("Tracepoint %d at 0x%s is already %s",
2669 (int) num
, paddress (addr
),
2670 enable
? "enabled" : "disabled");
2675 trace_debug ("%s tracepoint %d at 0x%s",
2676 enable
? "Enabling" : "Disabling",
2677 (int) num
, paddress (addr
));
2679 tp
->enabled
= enable
;
2681 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2684 int offset
= offsetof (struct tracepoint
, enabled
);
2685 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2687 ret
= prepare_to_access_memory ();
2690 trace_debug ("Failed to temporarily stop inferior threads");
2691 write_enn (own_buf
);
2695 ret
= write_inferior_integer (obj_addr
, enable
);
2696 done_accessing_memory ();
2700 trace_debug ("Cannot write enabled flag into "
2701 "inferior process memory");
2702 write_enn (own_buf
);
2711 trace_debug ("Tracepoint %d at 0x%s not found",
2712 (int) num
, paddress (addr
));
2713 write_enn (own_buf
);
2718 cmd_qtv (char *own_buf
)
2723 char *packet
= own_buf
;
2725 packet
+= strlen ("qTV:");
2726 unpack_varlen_hex (packet
, &num
);
2728 if (current_traceframe
>= 0)
2730 err
= traceframe_read_tsv ((int) num
, &val
);
2733 strcpy (own_buf
, "U");
2737 /* Only make tsv's be undefined before the first trace run. After a
2738 trace run is over, the user might want to see the last value of
2739 the tsv, and it might not be available in a traceframe. */
2740 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2742 strcpy (own_buf
, "U");
2746 val
= get_trace_state_variable_value (num
);
2748 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2751 /* Clear out the list of readonly regions. */
2754 clear_readonly_regions (void)
2756 struct readonly_region
*roreg
;
2758 while (readonly_regions
)
2760 roreg
= readonly_regions
;
2761 readonly_regions
= readonly_regions
->next
;
2766 /* Parse the collection of address ranges whose contents GDB believes
2767 to be unchanging and so can be read directly from target memory
2768 even while looking at a traceframe. */
2771 cmd_qtro (char *own_buf
)
2773 ULONGEST start
, end
;
2774 struct readonly_region
*roreg
;
2775 char *packet
= own_buf
;
2777 trace_debug ("Want to mark readonly regions");
2779 clear_readonly_regions ();
2781 packet
+= strlen ("QTro");
2783 while (*packet
== ':')
2785 ++packet
; /* skip a colon */
2786 packet
= unpack_varlen_hex (packet
, &start
);
2787 ++packet
; /* skip a comma */
2788 packet
= unpack_varlen_hex (packet
, &end
);
2789 roreg
= xmalloc (sizeof (struct readonly_region
));
2790 roreg
->start
= start
;
2792 roreg
->next
= readonly_regions
;
2793 readonly_regions
= roreg
;
2794 trace_debug ("Added readonly region from 0x%s to 0x%s",
2795 paddress (roreg
->start
), paddress (roreg
->end
));
2801 /* Test to see if the given range is in our list of readonly ranges.
2802 We only test for being entirely within a range, GDB is not going to
2803 send a single memory packet that spans multiple regions. */
2806 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2808 struct readonly_region
*roreg
;
2810 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2811 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2817 /* The maximum size of a jump pad entry. */
2818 static const int max_jump_pad_size
= 0x100;
2820 static CORE_ADDR gdb_jump_pad_head
;
2822 /* Return the address of the next free jump space. */
2825 get_jump_space_head (void)
2827 if (gdb_jump_pad_head
== 0)
2829 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2830 &gdb_jump_pad_head
))
2831 fatal ("error extracting jump_pad_buffer");
2834 return gdb_jump_pad_head
;
2837 /* Reserve USED bytes from the jump space. */
2840 claim_jump_space (ULONGEST used
)
2842 trace_debug ("claim_jump_space reserves %s bytes at %s",
2843 pulongest (used
), paddress (gdb_jump_pad_head
));
2844 gdb_jump_pad_head
+= used
;
2847 static CORE_ADDR trampoline_buffer_head
= 0;
2848 static CORE_ADDR trampoline_buffer_tail
;
2850 /* Reserve USED bytes from the trampoline buffer and return the
2851 address of the start of the reserved space in TRAMPOLINE. Returns
2852 non-zero if the space is successfully claimed. */
2855 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2857 if (!trampoline_buffer_head
)
2859 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2860 &trampoline_buffer_tail
))
2862 fatal ("error extracting trampoline_buffer");
2866 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2867 &trampoline_buffer_head
))
2869 fatal ("error extracting trampoline_buffer_end");
2874 /* Start claiming space from the top of the trampoline space. If
2875 the space is located at the bottom of the virtual address space,
2876 this reduces the possibility that corruption will occur if a null
2877 pointer is used to write to memory. */
2878 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2880 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2885 trampoline_buffer_head
-= used
;
2887 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2888 pulongest (used
), paddress (trampoline_buffer_head
));
2890 *trampoline
= trampoline_buffer_head
;
2894 /* Returns non-zero if there is space allocated for use in trampolines
2895 for fast tracepoints. */
2898 have_fast_tracepoint_trampoline_buffer (char *buf
)
2900 CORE_ADDR trampoline_end
, errbuf
;
2902 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2905 fatal ("error extracting trampoline_buffer_end");
2912 strcpy (buf
, "was claiming");
2913 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
2916 fatal ("error extracting errbuf");
2920 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
2923 return trampoline_end
!= 0;
2926 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2927 the command fails, or 0 otherwise. If the command ran
2928 successfully, but probing the marker failed, ERROUT will be filled
2929 with the error to reply to GDB, and -1 is also returned. This
2930 allows directly passing IPA errors to GDB. */
2933 probe_marker_at (CORE_ADDR address
, char *errout
)
2935 char cmd
[IPA_CMD_BUF_SIZE
];
2938 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
2939 err
= run_inferior_command (cmd
);
2945 strcpy (errout
, cmd
);
2954 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
2956 to
->jump_pad
= from
->jump_pad
;
2957 to
->jump_pad_end
= from
->jump_pad_end
;
2958 to
->trampoline
= from
->trampoline
;
2959 to
->trampoline_end
= from
->trampoline_end
;
2960 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
2961 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
2962 to
->handle
= from
->handle
;
2964 gdb_assert (from
->handle
);
2965 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
2968 #define MAX_JUMP_SIZE 20
2970 /* Install fast tracepoint. Return 0 if successful, otherwise return
2974 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
2976 CORE_ADDR jentry
, jump_entry
;
2977 CORE_ADDR trampoline
;
2978 ULONGEST trampoline_size
;
2980 /* The jump to the jump pad of the last fast tracepoint
2982 unsigned char fjump
[MAX_JUMP_SIZE
];
2983 ULONGEST fjump_size
;
2985 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
2987 trace_debug ("Requested a fast tracepoint on an instruction "
2988 "that is of less than the minimum length.");
2992 jentry
= jump_entry
= get_jump_space_head ();
2995 trampoline_size
= 0;
2997 /* Install the jump pad. */
2998 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3000 ipa_sym_addrs
.addr_gdb_collect
,
3001 ipa_sym_addrs
.addr_collecting
,
3004 &trampoline
, &trampoline_size
,
3006 &tpoint
->adjusted_insn_addr
,
3007 &tpoint
->adjusted_insn_addr_end
,
3014 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3017 if (tpoint
->handle
!= NULL
)
3019 tpoint
->jump_pad
= jump_entry
;
3020 tpoint
->jump_pad_end
= jentry
;
3021 tpoint
->trampoline
= trampoline
;
3022 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3024 /* Pad to 8-byte alignment. */
3025 jentry
= ((jentry
+ 7) & ~0x7);
3026 claim_jump_space (jentry
- jump_entry
);
3033 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3036 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3038 tpoint
->handle
= NULL
;
3041 if (tpoint
->type
== trap_tracepoint
)
3043 /* Tracepoints are installed as memory breakpoints. Just go
3044 ahead and install the trap. The breakpoints module
3045 handles duplicated breakpoints, and the memory read
3046 routine handles un-patching traps from memory reads. */
3047 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3048 tracepoint_handler
);
3050 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3052 if (!agent_loaded_p ())
3054 trace_debug ("Requested a %s tracepoint, but fast "
3055 "tracepoints aren't supported.",
3056 tpoint
->type
== static_tracepoint
? "static" : "fast");
3057 write_e_ipa_not_loaded (own_buf
);
3060 if (tpoint
->type
== static_tracepoint
3061 && !in_process_agent_supports_ust ())
3063 trace_debug ("Requested a static tracepoint, but static "
3064 "tracepoints are not supported.");
3065 write_e_ust_not_loaded (own_buf
);
3069 if (tpoint
->type
== fast_tracepoint
)
3070 install_fast_tracepoint (tpoint
, own_buf
);
3073 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3074 tpoint
->handle
= (void *) -1;
3079 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3081 if (tpoint
->handle
== NULL
)
3083 if (*own_buf
== '\0')
3084 write_enn (own_buf
);
3091 cmd_qtstart (char *packet
)
3093 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3095 trace_debug ("Starting the trace");
3097 /* Pause all threads temporarily while we patch tracepoints. */
3100 /* Get threads out of jump pads. Safe to do here, since this is a
3101 top level command. And, required to do here, since we're
3102 deleting/rewriting jump pads. */
3104 stabilize_threads ();
3106 /* Freeze threads. */
3109 /* Sync the fast tracepoints list in the inferior ftlib. */
3110 if (agent_loaded_p ())
3112 download_tracepoints ();
3113 download_trace_state_variables ();
3116 /* No previous fast tpoint yet. */
3117 prev_ftpoint
= NULL
;
3119 /* No previous static tpoint yet. */
3120 prev_stpoint
= NULL
;
3124 /* Install tracepoints. */
3125 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3127 /* Ensure all the hit counts start at zero. */
3128 tpoint
->hit_count
= 0;
3129 tpoint
->traceframe_usage
= 0;
3131 if (tpoint
->type
== trap_tracepoint
)
3133 /* Tracepoints are installed as memory breakpoints. Just go
3134 ahead and install the trap. The breakpoints module
3135 handles duplicated breakpoints, and the memory read
3136 routine handles un-patching traps from memory reads. */
3137 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3138 tracepoint_handler
);
3140 else if (tpoint
->type
== fast_tracepoint
)
3142 if (maybe_write_ipa_not_loaded (packet
))
3144 trace_debug ("Requested a fast tracepoint, but fast "
3145 "tracepoints aren't supported.");
3149 if (prev_ftpoint
!= NULL
&& prev_ftpoint
->address
== tpoint
->address
)
3150 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3153 if (install_fast_tracepoint (tpoint
, packet
) == 0)
3154 prev_ftpoint
= tpoint
;
3157 else if (tpoint
->type
== static_tracepoint
)
3159 if (maybe_write_ipa_ust_not_loaded (packet
))
3161 trace_debug ("Requested a static tracepoint, but static "
3162 "tracepoints are not supported.");
3166 /* Can only probe a given marker once. */
3167 if (prev_stpoint
!= NULL
&& prev_stpoint
->address
== tpoint
->address
)
3169 tpoint
->handle
= (void *) -1;
3173 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3175 tpoint
->handle
= (void *) -1;
3177 /* So that we can handle multiple static tracepoints
3178 at the same address easily. */
3179 prev_stpoint
= tpoint
;
3184 /* Any failure in the inner loop is sufficient cause to give
3186 if (tpoint
->handle
== NULL
)
3190 /* Any error in tracepoint insertion is unacceptable; better to
3191 address the problem now, than end up with a useless or misleading
3195 clear_installed_tracepoints ();
3196 if (*packet
== '\0')
3202 stopping_tracepoint
= NULL
;
3203 trace_buffer_is_full
= 0;
3204 expr_eval_result
= expr_eval_no_error
;
3205 error_tracepoint
= NULL
;
3206 tracing_start_time
= get_timestamp ();
3208 /* Tracing is now active, hits will now start being logged. */
3211 if (agent_loaded_p ())
3213 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3214 fatal ("Error setting tracing variable in lib");
3216 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3218 fatal ("Error clearing stopping_tracepoint variable in lib");
3220 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3221 fatal ("Error clearing trace_buffer_is_full variable in lib");
3223 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3224 stop_tracing_handler
);
3225 if (stop_tracing_bkpt
== NULL
)
3226 error ("Error setting stop_tracing breakpoint");
3228 flush_trace_buffer_bkpt
3229 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3230 flush_trace_buffer_handler
);
3231 if (flush_trace_buffer_bkpt
== NULL
)
3232 error ("Error setting flush_trace_buffer breakpoint");
3240 /* End a tracing run, filling in a stop reason to report back to GDB,
3241 and removing the tracepoints from the code. */
3248 trace_debug ("Tracing is already off, ignoring");
3252 trace_debug ("Stopping the trace");
3254 /* Pause all threads before removing fast jumps from memory,
3255 breakpoints, and touching IPA state variables (inferior memory).
3256 Some thread may hit the internal tracing breakpoints, or be
3257 collecting this moment, but that's ok, we don't release the
3258 tpoint object's memory or the jump pads here (we only do that
3259 when we're sure we can move all threads out of the jump pads).
3260 We can't now, since we may be getting here due to the inferior
3261 agent calling us. */
3263 /* Since we're removing breakpoints, cancel breakpoint hits,
3264 possibly related to the breakpoints we're about to delete. */
3265 cancel_breakpoints ();
3267 /* Stop logging. Tracepoints can still be hit, but they will not be
3270 if (agent_loaded_p ())
3272 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3273 fatal ("Error clearing tracing variable in lib");
3276 tracing_stop_time
= get_timestamp ();
3277 tracing_stop_reason
= "t???";
3278 tracing_stop_tpnum
= 0;
3279 if (stopping_tracepoint
)
3281 trace_debug ("Stopping the trace because "
3282 "tracepoint %d was hit %ld times",
3283 stopping_tracepoint
->number
,
3284 stopping_tracepoint
->pass_count
);
3285 tracing_stop_reason
= "tpasscount";
3286 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3288 else if (trace_buffer_is_full
)
3290 trace_debug ("Stopping the trace because the trace buffer is full");
3291 tracing_stop_reason
= "tfull";
3293 else if (expr_eval_result
!= expr_eval_no_error
)
3295 trace_debug ("Stopping the trace because of an expression eval error");
3296 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3297 tracing_stop_tpnum
= error_tracepoint
->number
;
3299 #ifndef IN_PROCESS_AGENT
3300 else if (!gdb_connected ())
3302 trace_debug ("Stopping the trace because GDB disconnected");
3303 tracing_stop_reason
= "tdisconnected";
3308 trace_debug ("Stopping the trace because of a tstop command");
3309 tracing_stop_reason
= "tstop";
3312 stopping_tracepoint
= NULL
;
3313 error_tracepoint
= NULL
;
3315 /* Clear out the tracepoints. */
3316 clear_installed_tracepoints ();
3318 if (agent_loaded_p ())
3320 /* Pull in fast tracepoint trace frames from the inferior lib
3321 buffer into our buffer, even if our buffer is already full,
3322 because we want to present the full number of created frames
3323 in addition to what fit in the trace buffer. */
3324 upload_fast_traceframes ();
3327 if (stop_tracing_bkpt
!= NULL
)
3329 delete_breakpoint (stop_tracing_bkpt
);
3330 stop_tracing_bkpt
= NULL
;
3333 if (flush_trace_buffer_bkpt
!= NULL
)
3335 delete_breakpoint (flush_trace_buffer_bkpt
);
3336 flush_trace_buffer_bkpt
= NULL
;
3343 stop_tracing_handler (CORE_ADDR addr
)
3345 trace_debug ("lib hit stop_tracing");
3347 /* Don't actually handle it here. When we stop tracing we remove
3348 breakpoints from the inferior, and that is not allowed in a
3349 breakpoint handler (as the caller is walking the breakpoint
3355 flush_trace_buffer_handler (CORE_ADDR addr
)
3357 trace_debug ("lib hit flush_trace_buffer");
3362 cmd_qtstop (char *packet
)
3369 cmd_qtdisconnected (char *own_buf
)
3372 char *packet
= own_buf
;
3374 packet
+= strlen ("QTDisconnected:");
3376 unpack_varlen_hex (packet
, &setting
);
3380 disconnected_tracing
= setting
;
3384 cmd_qtframe (char *own_buf
)
3386 ULONGEST frame
, pc
, lo
, hi
, num
;
3388 struct traceframe
*tframe
;
3389 char *packet
= own_buf
;
3391 packet
+= strlen ("QTFrame:");
3393 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3395 packet
+= strlen ("pc:");
3396 unpack_varlen_hex (packet
, &pc
);
3397 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3398 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3400 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3402 packet
+= strlen ("range:");
3403 packet
= unpack_varlen_hex (packet
, &lo
);
3405 unpack_varlen_hex (packet
, &hi
);
3406 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3407 paddress (lo
), paddress (hi
));
3408 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3410 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3412 packet
+= strlen ("outside:");
3413 packet
= unpack_varlen_hex (packet
, &lo
);
3415 unpack_varlen_hex (packet
, &hi
);
3416 trace_debug ("Want to find next traceframe "
3417 "outside the range 0x%s to 0x%s",
3418 paddress (lo
), paddress (hi
));
3419 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3421 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3423 packet
+= strlen ("tdp:");
3424 unpack_varlen_hex (packet
, &num
);
3426 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3427 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3431 unpack_varlen_hex (packet
, &frame
);
3432 tfnum
= (int) frame
;
3435 trace_debug ("Want to stop looking at traceframes");
3436 current_traceframe
= -1;
3440 trace_debug ("Want to look at traceframe %d", tfnum
);
3441 tframe
= find_traceframe (tfnum
);
3446 current_traceframe
= tfnum
;
3447 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3450 sprintf (own_buf
, "F-1");
3454 cmd_qtstatus (char *packet
)
3456 char *stop_reason_rsp
= NULL
;
3457 char *buf1
, *buf2
, *buf3
, *str
;
3460 /* Translate the plain text of the notes back into hex for
3463 str
= (tracing_user_name
? tracing_user_name
: "");
3464 slen
= strlen (str
);
3465 buf1
= (char *) alloca (slen
* 2 + 1);
3466 hexify (buf1
, str
, slen
);
3468 str
= (tracing_notes
? tracing_notes
: "");
3469 slen
= strlen (str
);
3470 buf2
= (char *) alloca (slen
* 2 + 1);
3471 hexify (buf2
, str
, slen
);
3473 str
= (tracing_stop_note
? tracing_stop_note
: "");
3474 slen
= strlen (str
);
3475 buf3
= (char *) alloca (slen
* 2 + 1);
3476 hexify (buf3
, str
, slen
);
3478 trace_debug ("Returning trace status as %d, stop reason %s",
3479 tracing
, tracing_stop_reason
);
3481 if (agent_loaded_p ())
3485 upload_fast_traceframes ();
3490 stop_reason_rsp
= (char *) tracing_stop_reason
;
3492 /* The user visible error string in terror needs to be hex encoded.
3493 We leave it as plain string in `tracing_stop_reason' to ease
3495 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3497 const char *result_name
;
3501 result_name
= stop_reason_rsp
+ strlen ("terror:");
3502 hexstr_len
= strlen (result_name
) * 2;
3503 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3504 strcpy (p
, "terror:");
3506 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3509 /* If this was a forced stop, include any stop note that was supplied. */
3510 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3512 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3513 strcpy (stop_reason_rsp
, "tstop:");
3514 strcat (stop_reason_rsp
, buf3
);
3520 "tframes:%x;tcreated:%x;"
3521 "tfree:%x;tsize:%s;"
3524 "starttime:%s;stoptime:%s;"
3525 "username:%s:;notes:%s:",
3527 stop_reason_rsp
, tracing_stop_tpnum
,
3528 traceframe_count
, traceframes_created
,
3529 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3530 circular_trace_buffer
,
3531 disconnected_tracing
,
3532 plongest (tracing_start_time
), plongest (tracing_stop_time
),
3537 cmd_qtp (char *own_buf
)
3540 struct tracepoint
*tpoint
;
3541 char *packet
= own_buf
;
3543 packet
+= strlen ("qTP:");
3545 packet
= unpack_varlen_hex (packet
, &num
);
3546 ++packet
; /* skip a colon */
3547 packet
= unpack_varlen_hex (packet
, &addr
);
3549 /* See if we already have this tracepoint. */
3550 tpoint
= find_tracepoint (num
, addr
);
3554 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3555 (int) num
, paddress (addr
));
3556 write_enn (own_buf
);
3560 sprintf (own_buf
, "V%lx:%lx", tpoint
->hit_count
, tpoint
->traceframe_usage
);
3563 /* State variables to help return all the tracepoint bits. */
3564 static struct tracepoint
*cur_tpoint
;
3565 static int cur_action
;
3566 static int cur_step_action
;
3567 static struct source_string
*cur_source_string
;
3568 static struct trace_state_variable
*cur_tsv
;
3570 /* Compose a response that is an imitation of the syntax by which the
3571 tracepoint was originally downloaded. */
3574 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3578 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
3579 paddress (tpoint
->address
),
3580 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3581 tpoint
->pass_count
);
3582 if (tpoint
->type
== fast_tracepoint
)
3583 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3584 else if (tpoint
->type
== static_tracepoint
)
3585 sprintf (packet
+ strlen (packet
), ":S");
3589 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3590 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3591 tpoint
->cond
->length
, buf
);
3596 /* Compose a response that is an imitation of the syntax by which the
3597 tracepoint action was originally downloaded (with the difference
3598 that due to the way we store the actions, this will output a packet
3599 per action, while GDB could have combined more than one action
3603 response_action (char *packet
, struct tracepoint
*tpoint
,
3604 char *taction
, int step
)
3606 sprintf (packet
, "%c%x:%s:%s",
3607 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3611 /* Compose a response that is an imitation of the syntax by which the
3612 tracepoint source piece was originally downloaded. */
3615 response_source (char *packet
,
3616 struct tracepoint
*tpoint
, struct source_string
*src
)
3621 len
= strlen (src
->str
);
3622 buf
= alloca (len
* 2 + 1);
3623 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3625 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3626 tpoint
->number
, paddress (tpoint
->address
),
3627 src
->type
, 0, len
, buf
);
3630 /* Return the first piece of tracepoint definition, and initialize the
3631 state machine that will iterate through all the tracepoint
3635 cmd_qtfp (char *packet
)
3637 trace_debug ("Returning first tracepoint definition piece");
3639 cur_tpoint
= tracepoints
;
3640 cur_action
= cur_step_action
= -1;
3641 cur_source_string
= NULL
;
3644 response_tracepoint (packet
, cur_tpoint
);
3646 strcpy (packet
, "l");
3649 /* Return additional pieces of tracepoint definition. Each action and
3650 stepping action must go into its own packet, because of packet size
3651 limits, and so we use state variables to deliver one piece at a
3655 cmd_qtsp (char *packet
)
3657 trace_debug ("Returning subsequent tracepoint definition piece");
3661 /* This case would normally never occur, but be prepared for
3663 strcpy (packet
, "l");
3665 else if (cur_action
< cur_tpoint
->numactions
- 1)
3668 response_action (packet
, cur_tpoint
,
3669 cur_tpoint
->actions_str
[cur_action
], 0);
3671 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3674 response_action (packet
, cur_tpoint
,
3675 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3677 else if ((cur_source_string
3678 ? cur_source_string
->next
3679 : cur_tpoint
->source_strings
))
3681 if (cur_source_string
)
3682 cur_source_string
= cur_source_string
->next
;
3684 cur_source_string
= cur_tpoint
->source_strings
;
3685 response_source (packet
, cur_tpoint
, cur_source_string
);
3689 cur_tpoint
= cur_tpoint
->next
;
3690 cur_action
= cur_step_action
= -1;
3691 cur_source_string
= NULL
;
3693 response_tracepoint (packet
, cur_tpoint
);
3695 strcpy (packet
, "l");
3699 /* Compose a response that is an imitation of the syntax by which the
3700 trace state variable was originally downloaded. */
3703 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3705 char *buf
= (char *) "";
3710 namelen
= strlen (tsv
->name
);
3711 buf
= alloca (namelen
* 2 + 1);
3712 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3715 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3716 tsv
->getter
? 1 : 0, buf
);
3719 /* Return the first trace state variable definition, and initialize
3720 the state machine that will iterate through all the tsv bits. */
3723 cmd_qtfv (char *packet
)
3725 trace_debug ("Returning first trace state variable definition");
3727 cur_tsv
= trace_state_variables
;
3730 response_tsv (packet
, cur_tsv
);
3732 strcpy (packet
, "l");
3735 /* Return additional trace state variable definitions. */
3738 cmd_qtsv (char *packet
)
3740 trace_debug ("Returning first trace state variable definition");
3744 /* This case would normally never occur, but be prepared for
3746 strcpy (packet
, "l");
3750 cur_tsv
= cur_tsv
->next
;
3752 response_tsv (packet
, cur_tsv
);
3754 strcpy (packet
, "l");
3757 strcpy (packet
, "l");
3760 /* Return the first static tracepoint marker, and initialize the state
3761 machine that will iterate through all the static tracepoints
3765 cmd_qtfstm (char *packet
)
3767 if (!maybe_write_ipa_ust_not_loaded (packet
))
3768 run_inferior_command (packet
);
3771 /* Return additional static tracepoints markers. */
3774 cmd_qtsstm (char *packet
)
3776 if (!maybe_write_ipa_ust_not_loaded (packet
))
3777 run_inferior_command (packet
);
3780 /* Return the definition of the static tracepoint at a given address.
3781 Result packet is the same as qTsST's. */
3784 cmd_qtstmat (char *packet
)
3786 if (!maybe_write_ipa_ust_not_loaded (packet
))
3787 run_inferior_command (packet
);
3790 /* Return the minimum instruction size needed for fast tracepoints as a
3791 hexadecimal number. */
3794 cmd_qtminftpilen (char *packet
)
3796 if (current_inferior
== NULL
)
3798 /* Indicate that the minimum length is currently unknown. */
3799 strcpy (packet
, "0");
3803 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3806 /* Respond to qTBuffer packet with a block of raw data from the trace
3807 buffer. GDB may ask for a lot, but we are allowed to reply with
3808 only as much as will fit within packet limits or whatever. */
3811 cmd_qtbuffer (char *own_buf
)
3813 ULONGEST offset
, num
, tot
;
3815 char *packet
= own_buf
;
3817 packet
+= strlen ("qTBuffer:");
3819 packet
= unpack_varlen_hex (packet
, &offset
);
3820 ++packet
; /* skip a comma */
3821 unpack_varlen_hex (packet
, &num
);
3823 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3824 (int) num
, pulongest (offset
));
3826 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3828 /* If we're right at the end, reply specially that we're done. */
3831 strcpy (own_buf
, "l");
3835 /* Object to any other out-of-bounds request. */
3838 write_enn (own_buf
);
3842 /* Compute the pointer corresponding to the given offset, accounting
3844 tbp
= trace_buffer_start
+ offset
;
3845 if (tbp
>= trace_buffer_wrap
)
3846 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3848 /* Trim to the remaining bytes if we're close to the end. */
3849 if (num
> tot
- offset
)
3852 /* Trim to available packet size. */
3853 if (num
>= (PBUFSIZ
- 16) / 2 )
3854 num
= (PBUFSIZ
- 16) / 2;
3856 convert_int_to_ascii (tbp
, own_buf
, num
);
3857 own_buf
[num
] = '\0';
3861 cmd_bigqtbuffer_circular (char *own_buf
)
3864 char *packet
= own_buf
;
3866 packet
+= strlen ("QTBuffer:circular:");
3868 unpack_varlen_hex (packet
, &val
);
3869 circular_trace_buffer
= val
;
3870 trace_debug ("Trace buffer is now %s",
3871 circular_trace_buffer
? "circular" : "linear");
3876 cmd_qtnotes (char *own_buf
)
3879 char *saved
, *user
, *notes
, *stopnote
;
3880 char *packet
= own_buf
;
3882 packet
+= strlen ("QTNotes:");
3886 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
3888 packet
+= strlen ("user:");
3890 packet
= strchr (packet
, ';');
3891 nbytes
= (packet
- saved
) / 2;
3892 user
= xmalloc (nbytes
+ 1);
3893 nbytes
= unhexify (user
, saved
, nbytes
);
3894 user
[nbytes
] = '\0';
3895 ++packet
; /* skip the semicolon */
3896 trace_debug ("User is '%s'", user
);
3897 tracing_user_name
= user
;
3899 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
3901 packet
+= strlen ("notes:");
3903 packet
= strchr (packet
, ';');
3904 nbytes
= (packet
- saved
) / 2;
3905 notes
= xmalloc (nbytes
+ 1);
3906 nbytes
= unhexify (notes
, saved
, nbytes
);
3907 notes
[nbytes
] = '\0';
3908 ++packet
; /* skip the semicolon */
3909 trace_debug ("Notes is '%s'", notes
);
3910 tracing_notes
= notes
;
3912 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
3914 packet
+= strlen ("tstop:");
3916 packet
= strchr (packet
, ';');
3917 nbytes
= (packet
- saved
) / 2;
3918 stopnote
= xmalloc (nbytes
+ 1);
3919 nbytes
= unhexify (stopnote
, saved
, nbytes
);
3920 stopnote
[nbytes
] = '\0';
3921 ++packet
; /* skip the semicolon */
3922 trace_debug ("tstop note is '%s'", stopnote
);
3923 tracing_stop_note
= stopnote
;
3933 handle_tracepoint_general_set (char *packet
)
3935 if (strcmp ("QTinit", packet
) == 0)
3937 cmd_qtinit (packet
);
3940 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3945 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3947 cmd_qtdpsrc (packet
);
3950 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
3952 cmd_qtenable_disable (packet
, 1);
3955 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
3957 cmd_qtenable_disable (packet
, 0);
3960 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3965 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3970 else if (strcmp ("QTStart", packet
) == 0)
3972 cmd_qtstart (packet
);
3975 else if (strcmp ("QTStop", packet
) == 0)
3977 cmd_qtstop (packet
);
3980 else if (strncmp ("QTDisconnected:", packet
,
3981 strlen ("QTDisconnected:")) == 0)
3983 cmd_qtdisconnected (packet
);
3986 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3988 cmd_qtframe (packet
);
3991 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
3993 cmd_bigqtbuffer_circular (packet
);
3996 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
3998 cmd_qtnotes (packet
);
4006 handle_tracepoint_query (char *packet
)
4008 if (strcmp ("qTStatus", packet
) == 0)
4010 cmd_qtstatus (packet
);
4013 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4018 else if (strcmp ("qTfP", packet
) == 0)
4023 else if (strcmp ("qTsP", packet
) == 0)
4028 else if (strcmp ("qTfV", packet
) == 0)
4033 else if (strcmp ("qTsV", packet
) == 0)
4038 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4043 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4045 cmd_qtbuffer (packet
);
4048 else if (strcmp ("qTfSTM", packet
) == 0)
4050 cmd_qtfstm (packet
);
4053 else if (strcmp ("qTsSTM", packet
) == 0)
4055 cmd_qtsstm (packet
);
4058 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4060 cmd_qtstmat (packet
);
4063 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4065 cmd_qtminftpilen (packet
);
4073 #ifndef IN_PROCESS_AGENT
4075 /* Call this when thread TINFO has hit the tracepoint defined by
4076 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4077 action. This adds a while-stepping collecting state item to the
4078 threads' collecting state list, so that we can keep track of
4079 multiple simultaneous while-stepping actions being collected by the
4080 same thread. This can happen in cases like:
4082 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4084 ff0003 INSN3 <-- TP2, collect $regs
4085 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4088 Notice that when instruction INSN5 is reached, the while-stepping
4089 actions of both TP1 and TP3 are still being collected, and that TP2
4090 had been collected meanwhile. The whole range of ff0001-ff0005
4091 should be single-stepped, due to at least TP1's while-stepping
4092 action covering the whole range. */
4095 add_while_stepping_state (struct thread_info
*tinfo
,
4096 int tp_number
, CORE_ADDR tp_address
)
4098 struct wstep_state
*wstep
;
4100 wstep
= xmalloc (sizeof (*wstep
));
4101 wstep
->next
= tinfo
->while_stepping
;
4103 wstep
->tp_number
= tp_number
;
4104 wstep
->tp_address
= tp_address
;
4105 wstep
->current_step
= 0;
4107 tinfo
->while_stepping
= wstep
;
4110 /* Release the while-stepping collecting state WSTEP. */
4113 release_while_stepping_state (struct wstep_state
*wstep
)
4118 /* Release all while-stepping collecting states currently associated
4119 with thread TINFO. */
4122 release_while_stepping_state_list (struct thread_info
*tinfo
)
4124 struct wstep_state
*head
;
4126 while (tinfo
->while_stepping
)
4128 head
= tinfo
->while_stepping
;
4129 tinfo
->while_stepping
= head
->next
;
4130 release_while_stepping_state (head
);
4134 /* If TINFO was handling a 'while-stepping' action, the step has
4135 finished, so collect any step data needed, and check if any more
4136 steps are required. Return true if the thread was indeed
4137 collecting tracepoint data, false otherwise. */
4140 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4142 struct tracepoint
*tpoint
;
4143 struct wstep_state
*wstep
;
4144 struct wstep_state
**wstep_link
;
4145 struct trap_tracepoint_ctx ctx
;
4147 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4149 if (agent_loaded_p ())
4150 upload_fast_traceframes ();
4152 /* Check if we were indeed collecting data for one of more
4153 tracepoints with a 'while-stepping' count. */
4154 if (tinfo
->while_stepping
== NULL
)
4159 /* We're not even tracing anymore. Stop this thread from
4161 release_while_stepping_state_list (tinfo
);
4163 /* The thread had stopped due to a single-step request indeed
4164 explained by a tracepoint. */
4168 wstep
= tinfo
->while_stepping
;
4169 wstep_link
= &tinfo
->while_stepping
;
4171 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4172 target_pid_to_str (tinfo
->entry
.id
),
4173 wstep
->tp_number
, paddress (wstep
->tp_address
));
4175 ctx
.base
.type
= trap_tracepoint
;
4176 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4178 while (wstep
!= NULL
)
4180 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4183 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4184 wstep
->tp_number
, paddress (wstep
->tp_address
),
4185 target_pid_to_str (tinfo
->entry
.id
));
4188 *wstep_link
= wstep
->next
;
4189 release_while_stepping_state (wstep
);
4190 wstep
= *wstep_link
;
4194 /* We've just finished one step. */
4195 ++wstep
->current_step
;
4198 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4199 stop_pc
, tpoint
, wstep
->current_step
);
4201 if (wstep
->current_step
>= tpoint
->step_count
)
4203 /* The requested numbers of steps have occurred. */
4204 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4205 target_pid_to_str (tinfo
->entry
.id
),
4206 wstep
->tp_number
, paddress (wstep
->tp_address
));
4208 /* Unlink the wstep. */
4209 *wstep_link
= wstep
->next
;
4210 release_while_stepping_state (wstep
);
4211 wstep
= *wstep_link
;
4213 /* Only check the hit count now, which ensure that we do all
4214 our stepping before stopping the run. */
4215 if (tpoint
->pass_count
> 0
4216 && tpoint
->hit_count
>= tpoint
->pass_count
4217 && stopping_tracepoint
== NULL
)
4218 stopping_tracepoint
= tpoint
;
4222 /* Keep single-stepping until the requested numbers of steps
4224 wstep_link
= &wstep
->next
;
4225 wstep
= *wstep_link
;
4228 if (stopping_tracepoint
4229 || trace_buffer_is_full
4230 || expr_eval_result
!= expr_eval_no_error
)
4240 /* Handle any internal tracing control breakpoint hits. That means,
4241 pull traceframes from the IPA to our buffer, and syncing both
4242 tracing agents when the IPA's tracing stops for some reason. */
4245 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4247 /* Pull in fast tracepoint trace frames from the inferior in-process
4248 agent's buffer into our buffer. */
4250 if (!agent_loaded_p ())
4253 upload_fast_traceframes ();
4255 /* Check if the in-process agent had decided we should stop
4257 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4259 int ipa_trace_buffer_is_full
;
4260 CORE_ADDR ipa_stopping_tracepoint
;
4261 int ipa_expr_eval_result
;
4262 CORE_ADDR ipa_error_tracepoint
;
4264 trace_debug ("lib stopped at stop_tracing");
4266 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4267 &ipa_trace_buffer_is_full
);
4269 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4270 &ipa_stopping_tracepoint
);
4271 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4273 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4274 &ipa_error_tracepoint
);
4275 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4277 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4278 &ipa_expr_eval_result
);
4279 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4281 trace_debug ("lib: trace_buffer_is_full: %d, "
4282 "stopping_tracepoint: %s, "
4283 "ipa_expr_eval_result: %d, "
4284 "error_tracepoint: %s, ",
4285 ipa_trace_buffer_is_full
,
4286 paddress (ipa_stopping_tracepoint
),
4287 ipa_expr_eval_result
,
4288 paddress (ipa_error_tracepoint
));
4292 if (ipa_trace_buffer_is_full
)
4293 trace_debug ("lib stopped due to full buffer.");
4294 if (ipa_stopping_tracepoint
)
4295 trace_debug ("lib stopped due to tpoint");
4296 if (ipa_stopping_tracepoint
)
4297 trace_debug ("lib stopped due to error");
4300 if (ipa_stopping_tracepoint
!= 0)
4303 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4305 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4307 expr_eval_result
= ipa_expr_eval_result
;
4309 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4314 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4316 trace_debug ("lib stopped at flush_trace_buffer");
4323 /* Return true if TINFO just hit a tracepoint. Collect data if
4327 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4329 struct tracepoint
*tpoint
;
4331 struct trap_tracepoint_ctx ctx
;
4333 /* Not tracing, don't handle. */
4337 ctx
.base
.type
= trap_tracepoint
;
4338 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4340 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4342 /* Note that we collect fast tracepoints here as well. We'll
4343 step over the fast tracepoint jump later, which avoids the
4344 double collect. However, we don't collect for static
4345 tracepoints here, because UST markers are compiled in program,
4346 and probes will be executed in program. So static tracepoints
4347 are collected there. */
4348 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4349 && tpoint
->type
!= static_tracepoint
)
4351 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4352 target_pid_to_str (tinfo
->entry
.id
),
4353 tpoint
->number
, paddress (tpoint
->address
));
4355 /* Test the condition if present, and collect if true. */
4357 || (condition_true_at_tracepoint
4358 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4359 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4362 if (stopping_tracepoint
4363 || trace_buffer_is_full
4364 || expr_eval_result
!= expr_eval_no_error
)
4368 /* If the tracepoint had a 'while-stepping' action, then set
4369 the thread to collect this tracepoint on the following
4371 else if (tpoint
->step_count
> 0)
4373 add_while_stepping_state (tinfo
,
4374 tpoint
->number
, tpoint
->address
);
4386 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4387 struct ust_marker_data
;
4388 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4389 struct traceframe
*tframe
);
4392 /* Create a trace frame for the hit of the given tracepoint in the
4396 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4397 struct tracepoint
*tpoint
)
4399 struct traceframe
*tframe
;
4402 /* Only count it as a hit when we actually collect data. */
4403 tpoint
->hit_count
++;
4405 /* If we've exceeded a defined pass count, record the event for
4406 later, and finish the collection for this hit. This test is only
4407 for nonstepping tracepoints, stepping tracepoints test at the end
4408 of their while-stepping loop. */
4409 if (tpoint
->pass_count
> 0
4410 && tpoint
->hit_count
>= tpoint
->pass_count
4411 && tpoint
->step_count
== 0
4412 && stopping_tracepoint
== NULL
)
4413 stopping_tracepoint
= tpoint
;
4415 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
4416 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4418 tframe
= add_traceframe (tpoint
);
4422 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4424 #ifndef IN_PROCESS_AGENT
4425 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4426 tpoint
->number
, paddress (tpoint
->address
),
4427 tpoint
->actions_str
[acti
]);
4430 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4431 tpoint
->actions
[acti
]);
4434 finish_traceframe (tframe
);
4437 if (tframe
== NULL
&& tracing
)
4438 trace_buffer_is_full
= 1;
4441 #ifndef IN_PROCESS_AGENT
4444 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4446 struct tracepoint
*tpoint
, int current_step
)
4448 struct traceframe
*tframe
;
4451 trace_debug ("Making new step traceframe for "
4452 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4453 tpoint
->number
, paddress (tpoint
->address
),
4454 current_step
, tpoint
->step_count
,
4457 tframe
= add_traceframe (tpoint
);
4461 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4463 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4464 tpoint
->number
, paddress (tpoint
->address
),
4465 tpoint
->step_actions_str
[acti
]);
4467 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4468 tpoint
->step_actions
[acti
]);
4471 finish_traceframe (tframe
);
4474 if (tframe
== NULL
&& tracing
)
4475 trace_buffer_is_full
= 1;
4480 static struct regcache
*
4481 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4483 struct regcache
*regcache
= NULL
;
4485 #ifdef IN_PROCESS_AGENT
4486 if (ctx
->type
== fast_tracepoint
)
4488 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4489 if (!fctx
->regcache_initted
)
4491 fctx
->regcache_initted
= 1;
4492 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4493 supply_regblock (&fctx
->regcache
, NULL
);
4494 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4496 regcache
= &fctx
->regcache
;
4499 if (ctx
->type
== static_tracepoint
)
4501 struct static_tracepoint_ctx
*sctx
4502 = (struct static_tracepoint_ctx
*) ctx
;
4504 if (!sctx
->regcache_initted
)
4506 sctx
->regcache_initted
= 1;
4507 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4508 supply_regblock (&sctx
->regcache
, NULL
);
4509 /* Pass down the tracepoint address, because REGS doesn't
4510 include the PC, but we know what it must have been. */
4511 supply_static_tracepoint_registers (&sctx
->regcache
,
4512 (const unsigned char *)
4514 sctx
->tpoint
->address
);
4516 regcache
= &sctx
->regcache
;
4520 if (ctx
->type
== trap_tracepoint
)
4522 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4523 regcache
= tctx
->regcache
;
4527 gdb_assert (regcache
!= NULL
);
4533 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4535 struct tracepoint
*tpoint
,
4536 struct traceframe
*tframe
,
4537 struct tracepoint_action
*taction
)
4539 enum eval_result_type err
;
4541 switch (taction
->type
)
4545 struct collect_memory_action
*maction
;
4547 maction
= (struct collect_memory_action
*) taction
;
4549 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4550 pulongest (maction
->len
),
4551 paddress (maction
->addr
), maction
->basereg
);
4552 /* (should use basereg) */
4553 agent_mem_read (tframe
, NULL
,
4554 (CORE_ADDR
) maction
->addr
, maction
->len
);
4559 unsigned char *regspace
;
4560 struct regcache tregcache
;
4561 struct regcache
*context_regcache
;
4564 trace_debug ("Want to collect registers");
4566 /* Collect all registers for now. */
4567 regspace
= add_traceframe_block (tframe
,
4568 1 + register_cache_size ());
4569 if (regspace
== NULL
)
4571 trace_debug ("Trace buffer block allocation failed, skipping");
4574 /* Identify a register block. */
4577 context_regcache
= get_context_regcache (ctx
);
4579 /* Wrap the regblock in a register cache (in the stack, we
4580 don't want to malloc here). */
4581 init_register_cache (&tregcache
, regspace
+ 1);
4583 /* Copy the register data to the regblock. */
4584 regcache_cpy (&tregcache
, context_regcache
);
4586 #ifndef IN_PROCESS_AGENT
4587 /* On some platforms, trap-based tracepoints will have the PC
4588 pointing to the next instruction after the trap, but we
4589 don't want the user or GDB trying to guess whether the
4590 saved PC needs adjusting; so always record the adjusted
4591 stop_pc. Note that we can't use tpoint->address instead,
4592 since it will be wrong for while-stepping actions. This
4593 adjustment is a nop for fast tracepoints collected from the
4594 in-process lib (but not if GDBserver is collecting one
4595 preemptively), since the PC had already been adjusted to
4596 contain the tracepoint's address by the jump pad. */
4597 trace_debug ("Storing stop pc (0x%s) in regblock",
4598 paddress (stop_pc
));
4600 /* This changes the regblock, not the thread's
4602 regcache_write_pc (&tregcache
, stop_pc
);
4608 struct eval_expr_action
*eaction
;
4610 eaction
= (struct eval_expr_action
*) taction
;
4612 trace_debug ("Want to evaluate expression");
4614 err
= eval_tracepoint_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4616 if (err
!= expr_eval_no_error
)
4618 record_tracepoint_error (tpoint
, "action expression", err
);
4625 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4626 trace_debug ("Want to collect static trace data");
4627 collect_ust_data_at_tracepoint (ctx
, tframe
);
4629 trace_debug ("warning: collecting static trace data, "
4630 "but static tracepoints are not supported");
4635 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4641 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4642 struct tracepoint
*tpoint
)
4645 enum eval_result_type err
;
4647 /* Presently, gdbserver doesn't run compiled conditions, only the
4648 IPA does. If the program stops at a fast tracepoint's address
4649 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4650 gdbserver preemptively collect the fast tracepoint. Later, on
4651 resume, gdbserver steps over the fast tracepoint like it steps
4652 over breakpoints, so that the IPA doesn't see that fast
4653 tracepoint. This avoids double collects of fast tracepoints in
4654 that stopping scenario. Having gdbserver itself handle the fast
4655 tracepoint gives the user a consistent view of when fast or trap
4656 tracepoints are collected, compared to an alternative where only
4657 trap tracepoints are collected on stop, and fast tracepoints on
4658 resume. When a fast tracepoint is being processed by gdbserver,
4659 it is always the non-compiled condition expression that is
4661 #ifdef IN_PROCESS_AGENT
4662 if (tpoint
->compiled_cond
)
4663 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4666 err
= eval_tracepoint_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4668 if (err
!= expr_eval_no_error
)
4670 record_tracepoint_error (tpoint
, "condition", err
);
4671 /* The error case must return false. */
4675 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4676 tpoint
->number
, paddress (tpoint
->address
),
4678 return (value
? 1 : 0);
4681 /* Evaluates a tracepoint agent expression with context CTX,
4682 traceframe TFRAME, agent expression AEXPR and store the
4685 static enum eval_result_type
4686 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4687 struct traceframe
*tframe
,
4688 struct agent_expr
*aexpr
,
4691 struct regcache
*regcache
;
4692 regcache
= get_context_regcache (ctx
);
4694 return gdb_eval_agent_expr (regcache
, tframe
, aexpr
, rslt
);
4697 /* Do memory copies for bytecodes. */
4698 /* Do the recording of memory blocks for actions and bytecodes. */
4701 agent_mem_read (struct traceframe
*tframe
,
4702 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4704 unsigned char *mspace
;
4705 ULONGEST remaining
= len
;
4706 unsigned short blocklen
;
4708 /* If a 'to' buffer is specified, use it. */
4711 read_inferior_memory (from
, to
, len
);
4715 /* Otherwise, create a new memory block in the trace buffer. */
4716 while (remaining
> 0)
4720 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4721 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4722 mspace
= add_traceframe_block (tframe
, sp
);
4725 /* Identify block as a memory block. */
4728 /* Record address and size. */
4729 memcpy (mspace
, &from
, sizeof (from
));
4730 mspace
+= sizeof (from
);
4731 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4732 mspace
+= sizeof (blocklen
);
4733 /* Record the memory block proper. */
4734 read_inferior_memory (from
, mspace
, blocklen
);
4735 trace_debug ("%d bytes recorded", blocklen
);
4736 remaining
-= blocklen
;
4743 agent_mem_read_string (struct traceframe
*tframe
,
4744 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4746 unsigned char *buf
, *mspace
;
4747 ULONGEST remaining
= len
;
4748 unsigned short blocklen
, i
;
4750 /* To save a bit of space, block lengths are 16-bit, so break large
4751 requests into multiple blocks. Bordering on overkill for strings,
4752 but it could happen that someone specifies a large max length. */
4753 while (remaining
> 0)
4757 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4758 /* We want working space to accumulate nonzero bytes, since
4759 traceframes must have a predecided size (otherwise it gets
4760 harder to wrap correctly for the circular case, etc). */
4761 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4762 for (i
= 0; i
< blocklen
; ++i
)
4764 /* Read the string one byte at a time, in case the string is
4765 at the end of a valid memory area - we don't want a
4766 correctly-terminated string to engender segvio
4768 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4773 /* Make sure outer loop stops now too. */
4774 remaining
= blocklen
;
4778 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4779 mspace
= add_traceframe_block (tframe
, sp
);
4785 /* Identify block as a memory block. */
4788 /* Record address and size. */
4789 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4790 mspace
+= sizeof (from
);
4791 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4792 mspace
+= sizeof (blocklen
);
4793 /* Copy the string contents. */
4794 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
4795 remaining
-= blocklen
;
4802 /* Record the value of a trace state variable. */
4805 agent_tsv_read (struct traceframe
*tframe
, int n
)
4807 unsigned char *vspace
;
4810 vspace
= add_traceframe_block (tframe
,
4811 1 + sizeof (n
) + sizeof (LONGEST
));
4814 /* Identify block as a variable. */
4816 /* Record variable's number and value. */
4817 memcpy (vspace
+ 1, &n
, sizeof (n
));
4818 val
= get_trace_state_variable_value (n
);
4819 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4820 trace_debug ("Variable %d recorded", n
);
4824 #ifndef IN_PROCESS_AGENT
4826 /* Callback for traceframe_walk_blocks, used to find a given block
4827 type in a traceframe. */
4830 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
4832 char *wantedp
= data
;
4834 if (*wantedp
== blocktype
)
4840 /* Walk over all traceframe blocks of the traceframe buffer starting
4841 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4842 block found, passing in DATA unmodified. If CALLBACK returns true,
4843 this returns a pointer to where the block is found. Returns NULL
4844 if no callback call returned true, indicating that all blocks have
4847 static unsigned char *
4848 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
4850 int (*callback
) (char blocktype
,
4851 unsigned char *dataptr
,
4855 unsigned char *dataptr
;
4859 trace_debug ("traceframe %d has no data", tfnum
);
4863 /* Iterate through a traceframe's blocks, looking for a block of the
4865 for (dataptr
= database
;
4866 dataptr
< database
+ datasize
;
4870 unsigned short mlen
;
4872 if (dataptr
== trace_buffer_wrap
)
4874 /* Adjust to reflect wrapping part of the frame around to
4876 datasize
= dataptr
- database
;
4877 dataptr
= database
= trace_buffer_lo
;
4880 blocktype
= *dataptr
++;
4882 if ((*callback
) (blocktype
, dataptr
, data
))
4888 /* Skip over the registers block. */
4889 dataptr
+= register_cache_size ();
4892 /* Skip over the memory block. */
4893 dataptr
+= sizeof (CORE_ADDR
);
4894 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4895 dataptr
+= (sizeof (mlen
) + mlen
);
4898 /* Skip over the TSV block. */
4899 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4902 /* Skip over the static trace data block. */
4903 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4904 dataptr
+= (sizeof (mlen
) + mlen
);
4907 trace_debug ("traceframe %d has unknown block type 0x%x",
4916 /* Look for the block of type TYPE_WANTED in the trameframe starting
4917 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4920 static unsigned char *
4921 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4922 int tfnum
, char type_wanted
)
4924 return traceframe_walk_blocks (database
, datasize
, tfnum
,
4925 match_blocktype
, &type_wanted
);
4928 static unsigned char *
4929 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4931 unsigned char *regblock
;
4933 regblock
= traceframe_find_block_type (tframe
->data
,
4937 if (regblock
== NULL
)
4938 trace_debug ("traceframe %d has no register data", tfnum
);
4943 /* Get registers from a traceframe. */
4946 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4948 unsigned char *dataptr
;
4949 struct tracepoint
*tpoint
;
4950 struct traceframe
*tframe
;
4952 tframe
= find_traceframe (tfnum
);
4956 trace_debug ("traceframe %d not found", tfnum
);
4960 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4961 if (dataptr
== NULL
)
4963 /* Mark registers unavailable. */
4964 supply_regblock (regcache
, NULL
);
4966 /* We can generally guess at a PC, although this will be
4967 misleading for while-stepping frames and multi-location
4969 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4971 regcache_write_pc (regcache
, tpoint
->address
);
4974 supply_regblock (regcache
, dataptr
);
4980 traceframe_get_pc (struct traceframe
*tframe
)
4982 struct regcache regcache
;
4983 unsigned char *dataptr
;
4985 dataptr
= traceframe_find_regblock (tframe
, -1);
4986 if (dataptr
== NULL
)
4989 init_register_cache (®cache
, dataptr
);
4990 return regcache_read_pc (®cache
);
4993 /* Read a requested block of memory from a trace frame. */
4996 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
4997 unsigned char *buf
, ULONGEST length
,
5000 struct traceframe
*tframe
;
5001 unsigned char *database
, *dataptr
;
5002 unsigned int datasize
;
5004 unsigned short mlen
;
5006 trace_debug ("traceframe_read_mem");
5008 tframe
= find_traceframe (tfnum
);
5012 trace_debug ("traceframe %d not found", tfnum
);
5016 datasize
= tframe
->data_size
;
5017 database
= dataptr
= &tframe
->data
[0];
5019 /* Iterate through a traceframe's blocks, looking for memory. */
5020 while ((dataptr
= traceframe_find_block_type (dataptr
,
5022 - (dataptr
- database
),
5023 tfnum
, 'M')) != NULL
)
5025 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5026 dataptr
+= sizeof (maddr
);
5027 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5028 dataptr
+= sizeof (mlen
);
5029 trace_debug ("traceframe %d has %d bytes at %s",
5030 tfnum
, mlen
, paddress (maddr
));
5032 /* If the block includes the first part of the desired range,
5033 return as much it has; GDB will re-request the remainder,
5034 which might be in a different block of this trace frame. */
5035 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5037 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5041 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5046 /* Skip over this block. */
5050 trace_debug ("traceframe %d has no memory data for the desired region",
5058 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5061 struct traceframe
*tframe
;
5062 unsigned char *database
, *dataptr
;
5063 unsigned int datasize
;
5066 trace_debug ("traceframe_read_tsv");
5068 tfnum
= current_traceframe
;
5072 trace_debug ("no current traceframe");
5076 tframe
= find_traceframe (tfnum
);
5080 trace_debug ("traceframe %d not found", tfnum
);
5084 datasize
= tframe
->data_size
;
5085 database
= dataptr
= &tframe
->data
[0];
5087 /* Iterate through a traceframe's blocks, looking for the tsv. */
5088 while ((dataptr
= traceframe_find_block_type (dataptr
,
5090 - (dataptr
- database
),
5091 tfnum
, 'V')) != NULL
)
5093 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5094 dataptr
+= sizeof (vnum
);
5096 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5098 /* Check that this is the variable we want. */
5101 memcpy (val
, dataptr
, sizeof (*val
));
5105 /* Skip over this block. */
5106 dataptr
+= sizeof (LONGEST
);
5109 trace_debug ("traceframe %d has no data for variable %d",
5114 /* Read a requested block of static tracepoint data from a trace
5118 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5119 unsigned char *buf
, ULONGEST length
,
5122 struct traceframe
*tframe
;
5123 unsigned char *database
, *dataptr
;
5124 unsigned int datasize
;
5125 unsigned short mlen
;
5127 trace_debug ("traceframe_read_sdata");
5129 tframe
= find_traceframe (tfnum
);
5133 trace_debug ("traceframe %d not found", tfnum
);
5137 datasize
= tframe
->data_size
;
5138 database
= &tframe
->data
[0];
5140 /* Iterate through a traceframe's blocks, looking for static
5142 dataptr
= traceframe_find_block_type (database
, datasize
,
5144 if (dataptr
!= NULL
)
5146 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5147 dataptr
+= sizeof (mlen
);
5150 if (offset
+ length
> mlen
)
5151 length
= mlen
- offset
;
5153 memcpy (buf
, dataptr
, length
);
5161 trace_debug ("traceframe %d has no static trace data", tfnum
);
5167 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5168 object. DATA is pointer to a struct buffer holding the
5169 traceframe-info object being built. */
5172 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5174 struct buffer
*buffer
= data
;
5180 unsigned short mlen
;
5183 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5184 dataptr
+= sizeof (maddr
);
5185 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5186 dataptr
+= sizeof (mlen
);
5187 buffer_xml_printf (buffer
,
5188 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5189 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5199 warning ("Unhandled trace block type (%d) '%c ' "
5200 "while building trace frame info.",
5201 blocktype
, blocktype
);
5208 /* Build a traceframe-info object for traceframe number TFNUM into
5212 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5214 struct traceframe
*tframe
;
5216 trace_debug ("traceframe_read_info");
5218 tframe
= find_traceframe (tfnum
);
5222 trace_debug ("traceframe %d not found", tfnum
);
5226 buffer_grow_str (buffer
, "<traceframe-info>\n");
5227 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5228 tfnum
, build_traceframe_info_xml
, buffer
);
5229 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5233 /* Return the first fast tracepoint whose jump pad contains PC. */
5235 static struct tracepoint
*
5236 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5238 struct tracepoint
*tpoint
;
5240 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5241 if (tpoint
->type
== fast_tracepoint
)
5242 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5248 /* Return the first fast tracepoint whose trampoline contains PC. */
5250 static struct tracepoint
*
5251 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5253 struct tracepoint
*tpoint
;
5255 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5257 if (tpoint
->type
== fast_tracepoint
5258 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5265 /* Return GDBserver's tracepoint that matches the IP Agent's
5266 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5269 static struct tracepoint
*
5270 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5272 struct tracepoint
*tpoint
;
5274 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5275 if (tpoint
->type
== fast_tracepoint
)
5276 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5284 /* The type of the object that is used to synchronize fast tracepoint
5287 typedef struct collecting_t
5289 /* The fast tracepoint number currently collecting. */
5292 /* A number that GDBserver can use to identify the thread that is
5293 presently holding the collect lock. This need not (and usually
5294 is not) the thread id, as getting the current thread ID usually
5295 requires a system call, which we want to avoid like the plague.
5296 Usually this is thread's TCB, found in the TLS (pseudo-)
5297 register, which is readable with a single insn on several
5299 uintptr_t thread_area
;
5302 #ifndef IN_PROCESS_AGENT
5305 force_unlock_trace_buffer (void)
5307 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5310 /* Check if the thread identified by THREAD_AREA which is stopped at
5311 STOP_PC, is presently locking the fast tracepoint collection, and
5312 if so, gather some status of said collection. Returns 0 if the
5313 thread isn't collecting or in the jump pad at all. 1, if in the
5314 jump pad (or within gdb_collect) and hasn't executed the adjusted
5315 original insn yet (can set a breakpoint there and run to it). 2,
5316 if presently executing the adjusted original insn --- in which
5317 case, if we want to move the thread out of the jump pad, we need to
5318 single-step it until this function returns 0. */
5321 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5323 struct fast_tpoint_collect_status
*status
)
5325 CORE_ADDR ipa_collecting
;
5326 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5327 CORE_ADDR ipa_gdb_trampoline_buffer
;
5328 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5329 struct tracepoint
*tpoint
;
5330 int needs_breakpoint
;
5332 /* The thread THREAD_AREA is either:
5334 0. not collecting at all, not within the jump pad, or within
5335 gdb_collect or one of its callees.
5337 1. in the jump pad and haven't reached gdb_collect
5339 2. within gdb_collect (out of the jump pad) (collect is set)
5341 3. we're in the jump pad, after gdb_collect having returned,
5342 possibly executing the adjusted insns.
5344 For cases 1 and 3, `collecting' may or not be set. The jump pad
5345 doesn't have any complicated jump logic, so we can tell if the
5346 thread is executing the adjust original insn or not by just
5347 matching STOP_PC with known jump pad addresses. If we it isn't
5348 yet executing the original insn, set a breakpoint there, and let
5349 the thread run to it, so to quickly step over a possible (many
5350 insns) gdb_collect call. Otherwise, or when the breakpoint is
5351 hit, only a few (small number of) insns are left to be executed
5352 in the jump pad. Single-step the thread until it leaves the
5357 needs_breakpoint
= 0;
5358 trace_debug ("fast_tracepoint_collecting");
5360 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5361 &ipa_gdb_jump_pad_buffer
))
5362 fatal ("error extracting `gdb_jump_pad_buffer'");
5363 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5364 &ipa_gdb_jump_pad_buffer_end
))
5365 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5367 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5368 &ipa_gdb_trampoline_buffer
))
5369 fatal ("error extracting `gdb_trampoline_buffer'");
5370 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5371 &ipa_gdb_trampoline_buffer_end
))
5372 fatal ("error extracting `gdb_trampoline_buffer_end'");
5374 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5375 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5377 /* We can tell which tracepoint(s) the thread is collecting by
5378 matching the jump pad address back to the tracepoint. */
5379 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5382 warning ("in jump pad, but no matching tpoint?");
5387 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5389 tpoint
->number
, paddress (tpoint
->address
),
5390 paddress (tpoint
->jump_pad
),
5391 paddress (tpoint
->jump_pad_end
),
5392 paddress (tpoint
->adjusted_insn_addr
),
5393 paddress (tpoint
->adjusted_insn_addr_end
));
5396 /* Definitely in the jump pad. May or may not need
5397 fast-exit-jump-pad breakpoint. */
5398 if (tpoint
->jump_pad
<= stop_pc
5399 && stop_pc
< tpoint
->adjusted_insn_addr
)
5400 needs_breakpoint
= 1;
5402 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5403 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5405 /* We can tell which tracepoint(s) the thread is collecting by
5406 matching the trampoline address back to the tracepoint. */
5407 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5410 warning ("in trampoline, but no matching tpoint?");
5415 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5416 tpoint
->number
, paddress (tpoint
->address
),
5417 paddress (tpoint
->trampoline
),
5418 paddress (tpoint
->trampoline_end
));
5421 /* Have not reached jump pad yet, but treat the trampoline as a
5422 part of the jump pad that is before the adjusted original
5424 needs_breakpoint
= 1;
5428 collecting_t ipa_collecting_obj
;
5430 /* If `collecting' is set/locked, then the THREAD_AREA thread
5431 may or not be the one holding the lock. We have to read the
5432 lock to find out. */
5434 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5437 trace_debug ("fast_tracepoint_collecting:"
5438 " failed reading 'collecting' in the inferior");
5442 if (!ipa_collecting
)
5444 trace_debug ("fast_tracepoint_collecting: not collecting"
5445 " (and nobody is).");
5449 /* Some thread is collecting. Check which. */
5450 if (read_inferior_memory (ipa_collecting
,
5451 (unsigned char *) &ipa_collecting_obj
,
5452 sizeof (ipa_collecting_obj
)) != 0)
5455 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5457 trace_debug ("fast_tracepoint_collecting: not collecting "
5458 "(another thread is)");
5463 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5466 warning ("fast_tracepoint_collecting: collecting, "
5467 "but tpoint %s not found?",
5468 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5472 /* The thread is within `gdb_collect', skip over the rest of
5473 fast tracepoint collection quickly using a breakpoint. */
5474 needs_breakpoint
= 1;
5477 /* The caller wants a bit of status detail. */
5480 status
->tpoint_num
= tpoint
->number
;
5481 status
->tpoint_addr
= tpoint
->address
;
5482 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5483 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5486 if (needs_breakpoint
)
5488 /* Hasn't executed the original instruction yet. Set breakpoint
5489 there, and wait till it's hit, then single-step until exiting
5493 fast_tracepoint_collecting, returning continue-until-break at %s",
5494 paddress (tpoint
->adjusted_insn_addr
));
5496 return 1; /* continue */
5500 /* Just single-step until exiting the jump pad. */
5502 trace_debug ("fast_tracepoint_collecting, returning "
5503 "need-single-step (%s-%s)",
5504 paddress (tpoint
->adjusted_insn_addr
),
5505 paddress (tpoint
->adjusted_insn_addr_end
));
5507 return 2; /* single-step */
5513 #ifdef IN_PROCESS_AGENT
5515 /* The global fast tracepoint collect lock. Points to a collecting_t
5516 object built on the stack by the jump pad, if presently locked;
5517 NULL if it isn't locked. Note that this lock *must* be set while
5518 executing any *function other than the jump pad. See
5519 fast_tracepoint_collecting. */
5520 static collecting_t
* ATTR_USED collecting
;
5522 /* This routine, called from the jump pad (in asm) is designed to be
5523 called from the jump pads of fast tracepoints, thus it is on the
5526 IP_AGENT_EXPORT
void ATTR_USED
5527 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5529 struct fast_tracepoint_ctx ctx
;
5531 /* Don't do anything until the trace run is completely set up. */
5535 ctx
.base
.type
= fast_tracepoint
;
5537 ctx
.regcache_initted
= 0;
5538 /* Wrap the regblock in a register cache (in the stack, we don't
5539 want to malloc here). */
5540 ctx
.regspace
= alloca (register_cache_size ());
5541 if (ctx
.regspace
== NULL
)
5543 trace_debug ("Trace buffer block allocation failed, skipping");
5547 for (ctx
.tpoint
= tpoint
;
5548 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5549 ctx
.tpoint
= ctx
.tpoint
->next
)
5551 if (!ctx
.tpoint
->enabled
)
5554 /* Multiple tracepoints of different types, such as fast tracepoint and
5555 static tracepoint, can be set at the same address. */
5556 if (ctx
.tpoint
->type
!= tpoint
->type
)
5559 /* Test the condition if present, and collect if true. */
5560 if (ctx
.tpoint
->cond
== NULL
5561 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5564 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5565 ctx
.tpoint
->address
, ctx
.tpoint
);
5567 /* Note that this will cause original insns to be written back
5568 to where we jumped from, but that's OK because we're jumping
5569 back to the next whole instruction. This will go badly if
5570 instruction restoration is not atomic though. */
5571 if (stopping_tracepoint
5572 || trace_buffer_is_full
5573 || expr_eval_result
!= expr_eval_no_error
)
5581 /* If there was a condition and it evaluated to false, the only
5582 way we would stop tracing is if there was an error during
5583 condition expression evaluation. */
5584 if (expr_eval_result
!= expr_eval_no_error
)
5595 #ifndef IN_PROCESS_AGENT
5598 get_raw_reg_func_addr (void)
5600 return ipa_sym_addrs
.addr_get_raw_reg
;
5604 get_get_tsv_func_addr (void)
5606 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5610 get_set_tsv_func_addr (void)
5612 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5616 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5617 CORE_ADDR
*jump_entry
)
5619 CORE_ADDR entry_point
= *jump_entry
;
5620 enum eval_result_type err
;
5622 trace_debug ("Starting condition compilation for tracepoint %d\n",
5625 /* Initialize the global pointer to the code being built. */
5626 current_insn_ptr
= *jump_entry
;
5630 err
= compile_bytecodes (tpoint
->cond
);
5632 if (err
== expr_eval_no_error
)
5636 /* Record the beginning of the compiled code. */
5637 tpoint
->compiled_cond
= entry_point
;
5639 trace_debug ("Condition compilation for tracepoint %d complete\n",
5644 /* Leave the unfinished code in situ, but don't point to it. */
5646 tpoint
->compiled_cond
= 0;
5648 trace_debug ("Condition compilation for tracepoint %d failed, "
5650 tpoint
->number
, err
);
5653 /* Update the code pointer passed in. Note that we do this even if
5654 the compile fails, so that we can look at the partial results
5655 instead of letting them be overwritten. */
5656 *jump_entry
= current_insn_ptr
;
5658 /* Leave a gap, to aid dump decipherment. */
5662 /* We'll need to adjust these when we consider bi-arch setups, and big
5666 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5668 return write_inferior_memory (where
,
5669 (unsigned char *) &ptr
, sizeof (void *));
5672 /* The base pointer of the IPA's heap. This is the only memory the
5673 IPA is allowed to use. The IPA should _not_ call the inferior's
5674 `malloc' during operation. That'd be slow, and, most importantly,
5675 it may not be safe. We may be collecting a tracepoint in a signal
5676 handler, for example. */
5677 static CORE_ADDR target_tp_heap
;
5679 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5683 target_malloc (ULONGEST size
)
5687 if (target_tp_heap
== 0)
5689 /* We have the pointer *address*, need what it points to. */
5690 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5692 fatal ("could get target heap head pointer");
5695 ptr
= target_tp_heap
;
5696 target_tp_heap
+= size
;
5698 /* Pad to 8-byte alignment. */
5699 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5705 download_agent_expr (struct agent_expr
*expr
)
5707 CORE_ADDR expr_addr
;
5708 CORE_ADDR expr_bytes
;
5710 expr_addr
= target_malloc (sizeof (*expr
));
5711 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5713 expr_bytes
= target_malloc (expr
->length
);
5714 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5716 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5721 /* Align V up to N bits. */
5722 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5724 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5727 download_tracepoint_1 (struct tracepoint
*tpoint
)
5729 struct tracepoint target_tracepoint
;
5730 CORE_ADDR tpptr
= 0;
5732 gdb_assert (tpoint
->type
== fast_tracepoint
5733 || tpoint
->type
== static_tracepoint
);
5735 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5737 CORE_ADDR jentry
, jump_entry
;
5739 jentry
= jump_entry
= get_jump_space_head ();
5741 if (tpoint
->cond
!= NULL
)
5743 /* Pad to 8-byte alignment. (needed?) */
5744 /* Actually this should be left for the target to
5746 jentry
= UALIGN (jentry
, 8);
5748 compile_tracepoint_condition (tpoint
, &jentry
);
5751 /* Pad to 8-byte alignment. */
5752 jentry
= UALIGN (jentry
, 8);
5753 claim_jump_space (jentry
- jump_entry
);
5756 target_tracepoint
= *tpoint
;
5758 tpptr
= target_malloc (sizeof (*tpoint
));
5759 tpoint
->obj_addr_on_target
= tpptr
;
5761 /* Write the whole object. We'll fix up its pointers in a bit.
5762 Assume no next for now. This is fixed up above on the next
5763 iteration, if there's any. */
5764 target_tracepoint
.next
= NULL
;
5765 /* Need to clear this here too, since we're downloading the
5766 tracepoints before clearing our own copy. */
5767 target_tracepoint
.hit_count
= 0;
5769 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5770 sizeof (target_tracepoint
));
5773 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5775 download_agent_expr (tpoint
->cond
));
5777 if (tpoint
->numactions
)
5780 CORE_ADDR actions_array
;
5782 /* The pointers array. */
5784 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
5785 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5789 /* Now for each pointer, download the action. */
5790 for (i
= 0; i
< tpoint
->numactions
; i
++)
5792 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5793 CORE_ADDR ipa_action
= action
->ops
->download (action
);
5795 if (ipa_action
!= 0)
5796 write_inferior_data_ptr
5797 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
5804 download_tracepoint (struct tracepoint
*tpoint
)
5806 struct tracepoint
*tp
, *tp_prev
;
5808 if (tpoint
->type
!= fast_tracepoint
5809 && tpoint
->type
!= static_tracepoint
)
5812 download_tracepoint_1 (tpoint
);
5814 /* Find the previous entry of TPOINT, which is fast tracepoint or
5815 static tracepoint. */
5817 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
5819 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
5825 CORE_ADDR tp_prev_target_next_addr
;
5827 /* Insert TPOINT after TP_PREV in IPA. */
5828 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
5829 + offsetof (struct tracepoint
, next
),
5830 &tp_prev_target_next_addr
))
5831 fatal ("error reading `tp_prev->next'");
5833 /* tpoint->next = tp_prev->next */
5834 write_inferior_data_ptr (tpoint
->obj_addr_on_target
5835 + offsetof (struct tracepoint
, next
),
5836 tp_prev_target_next_addr
);
5837 /* tp_prev->next = tpoint */
5838 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
5839 + offsetof (struct tracepoint
, next
),
5840 tpoint
->obj_addr_on_target
);
5843 /* First object in list, set the head pointer in the
5845 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
5846 tpoint
->obj_addr_on_target
);
5851 download_tracepoints (void)
5853 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
5854 struct tracepoint
*tpoint
;
5856 /* Start out empty. */
5857 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
5859 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5861 if (tpoint
->type
!= fast_tracepoint
5862 && tpoint
->type
!= static_tracepoint
)
5867 download_tracepoint_1 (tpoint
);
5869 tpptr
= tpoint
->obj_addr_on_target
;
5871 if (tpoint
== tracepoints
)
5873 /* First object in list, set the head pointer in the
5875 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
5879 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
5887 download_trace_state_variables (void)
5889 CORE_ADDR ptr
= 0, prev_ptr
= 0;
5890 struct trace_state_variable
*tsv
;
5892 /* Start out empty. */
5893 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
5895 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
5897 struct trace_state_variable target_tsv
;
5899 /* TSV's with a getter have been initialized equally in both the
5900 inferior and GDBserver. Skip them. */
5901 if (tsv
->getter
!= NULL
)
5907 ptr
= target_malloc (sizeof (*tsv
));
5909 if (tsv
== trace_state_variables
)
5911 /* First object in list, set the head pointer in the
5914 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
5919 write_inferior_data_ptr (prev_ptr
5920 + offsetof (struct trace_state_variable
,
5925 /* Write the whole object. We'll fix up its pointers in a bit.
5926 Assume no next, fixup when needed. */
5927 target_tsv
.next
= NULL
;
5929 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
5930 sizeof (target_tsv
));
5932 if (tsv
->name
!= NULL
)
5934 size_t size
= strlen (tsv
->name
) + 1;
5935 CORE_ADDR name_addr
= target_malloc (size
);
5936 write_inferior_memory (name_addr
,
5937 (unsigned char *) tsv
->name
, size
);
5938 write_inferior_data_ptr (ptr
5939 + offsetof (struct trace_state_variable
,
5944 if (tsv
->getter
!= NULL
)
5946 fatal ("what to do with these?");
5952 /* Fixup the next pointer in the last item in the list. */
5953 write_inferior_data_ptr (prev_ptr
5954 + offsetof (struct trace_state_variable
,
5959 /* Upload complete trace frames out of the IP Agent's trace buffer
5960 into GDBserver's trace buffer. This always uploads either all or
5961 no trace frames. This is the counter part of
5962 `trace_alloc_trace_buffer'. See its description of the atomic
5963 synching mechanism. */
5966 upload_fast_traceframes (void)
5968 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
5969 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
5971 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
5972 unsigned int curr_tbctrl_idx
;
5973 unsigned int ipa_trace_buffer_ctrl_curr
;
5974 unsigned int ipa_trace_buffer_ctrl_curr_old
;
5975 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
5976 struct breakpoint
*about_to_request_buffer_space_bkpt
;
5977 CORE_ADDR ipa_trace_buffer_lo
;
5978 CORE_ADDR ipa_trace_buffer_hi
;
5980 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
5981 &ipa_traceframe_read_count_racy
))
5983 /* This will happen in most targets if the current thread is
5988 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
5989 &ipa_traceframe_write_count_racy
))
5992 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
5993 ipa_traceframe_write_count_racy
5994 - ipa_traceframe_read_count_racy
,
5995 ipa_traceframe_write_count_racy
,
5996 ipa_traceframe_read_count_racy
);
5998 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6001 about_to_request_buffer_space_bkpt
6002 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6005 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6006 &ipa_trace_buffer_ctrl_curr
))
6009 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6011 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6014 unsigned int prev
, counter
;
6016 /* Update the token, with new counters, and the GDBserver stamp
6017 bit. Alway reuse the current TBC index. */
6018 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6019 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6021 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6027 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6028 ipa_trace_buffer_ctrl_curr
))
6031 trace_debug ("Lib: Committed %08x -> %08x",
6032 ipa_trace_buffer_ctrl_curr_old
,
6033 ipa_trace_buffer_ctrl_curr
);
6035 /* Re-read these, now that we've installed the
6036 `about_to_request_buffer_space' breakpoint/lock. A thread could
6037 have finished a traceframe between the last read of these
6038 counters and setting the breakpoint above. If we start
6039 uploading, we never want to leave this function with
6040 traceframe_read_count != 0, otherwise, GDBserver could end up
6041 incrementing the counter tokens more than once (due to event loop
6042 nesting), which would break the IP agent's "effective" detection
6043 (see trace_alloc_trace_buffer). */
6044 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6045 &ipa_traceframe_read_count
))
6047 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6048 &ipa_traceframe_write_count
))
6053 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6054 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6055 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6057 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6058 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6059 trace_debug ("note that ipa_traceframe_count's parts changed");
6062 /* Get the address of the current TBC object (the IP agent has an
6063 array of 3 such objects). The index is stored in the TBC
6065 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6066 ipa_trace_buffer_ctrl_addr
6067 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6069 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6070 (unsigned char *) &ipa_trace_buffer_ctrl
,
6071 sizeof (struct ipa_trace_buffer_control
)))
6074 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6075 &ipa_trace_buffer_lo
))
6077 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6078 &ipa_trace_buffer_hi
))
6081 /* Offsets are easier to grok for debugging than raw addresses,
6082 especially for the small trace buffer sizes that are useful for
6084 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6085 "endfree=%d wrap=%d hi=%d",
6087 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6088 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6089 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6090 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6091 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6093 /* Note that the IPA's buffer is always circular. */
6095 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6097 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6098 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6100 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6101 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6102 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6103 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6106 tf
= IPA_FIRST_TRACEFRAME ();
6108 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6110 struct tracepoint
*tpoint
;
6111 struct traceframe
*tframe
;
6112 unsigned char *block
;
6113 struct traceframe ipa_tframe
;
6115 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6116 offsetof (struct traceframe
, data
)))
6117 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6119 if (ipa_tframe
.tpnum
== 0)
6120 fatal ("Uploading: No (more) fast traceframes, but "
6121 "ipa_traceframe_count == %u??\n",
6122 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6124 /* Note that this will be incorrect for multi-location
6126 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6128 tframe
= add_traceframe (tpoint
);
6131 trace_buffer_is_full
= 1;
6132 trace_debug ("Uploading: trace buffer is full");
6136 /* Copy the whole set of blocks in one go for now. FIXME:
6137 split this in smaller blocks. */
6138 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6141 if (read_inferior_memory (tf
6142 + offsetof (struct traceframe
, data
),
6143 block
, ipa_tframe
.data_size
))
6144 error ("Uploading: Couldn't read traceframe data at %s\n",
6145 paddress (tf
+ offsetof (struct traceframe
, data
)));
6148 trace_debug ("Uploading: traceframe didn't fit");
6149 finish_traceframe (tframe
);
6152 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6154 /* If we freed the traceframe that wrapped around, go back
6155 to the non-wrap case. */
6156 if (tf
< ipa_trace_buffer_ctrl
.start
)
6158 trace_debug ("Lib: Discarding past the wraparound");
6159 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6161 ipa_trace_buffer_ctrl
.start
= tf
;
6162 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6163 ++ipa_traceframe_read_count
;
6165 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6166 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6168 trace_debug ("Lib: buffer is fully empty. "
6169 "Trace buffer [%d] start=%d free=%d endfree=%d",
6171 (int) (ipa_trace_buffer_ctrl
.start
6172 - ipa_trace_buffer_lo
),
6173 (int) (ipa_trace_buffer_ctrl
.free
6174 - ipa_trace_buffer_lo
),
6175 (int) (ipa_trace_buffer_ctrl
.end_free
6176 - ipa_trace_buffer_lo
));
6178 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6179 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6180 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6181 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6184 trace_debug ("Uploaded a traceframe\n"
6185 "Lib: Trace buffer [%d] start=%d free=%d "
6186 "endfree=%d wrap=%d hi=%d",
6188 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6189 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6190 (int) (ipa_trace_buffer_ctrl
.end_free
6191 - ipa_trace_buffer_lo
),
6192 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6193 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6196 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6197 (unsigned char *) &ipa_trace_buffer_ctrl
,
6198 sizeof (struct ipa_trace_buffer_control
)))
6201 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6202 ipa_traceframe_read_count
);
6204 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6207 cancel_breakpoints ();
6209 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6210 about_to_request_buffer_space_bkpt
= NULL
;
6214 if (trace_buffer_is_full
)
6219 #ifdef IN_PROCESS_AGENT
6221 IP_AGENT_EXPORT
int ust_loaded
;
6222 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6226 /* Static tracepoints. */
6228 /* UST puts a "struct tracepoint" in the global namespace, which
6229 conflicts with our tracepoint. Arguably, being a library, it
6230 shouldn't take ownership of such a generic name. We work around it
6232 #define tracepoint ust_tracepoint
6233 #include <ust/ust.h>
6236 extern int serialize_to_text (char *outbuf
, int bufsize
,
6237 const char *fmt
, va_list ap
);
6239 #define GDB_PROBE_NAME "gdb"
6241 /* We dynamically search for the UST symbols instead of linking them
6242 in. This lets the user decide if the application uses static
6243 tracepoints, instead of always pulling libust.so in. This vector
6244 holds pointers to all functions we care about. */
6248 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6249 const char *fmt
, va_list ap
);
6251 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6252 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6254 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6256 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6259 void (*marker_iter_start
) (struct marker_iter
*iter
);
6260 void (*marker_iter_next
) (struct marker_iter
*iter
);
6261 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6262 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6267 /* Cast through typeof to catch incompatible API changes. Since UST
6268 only builds with gcc, we can freely use gcc extensions here
6270 #define GET_UST_SYM(SYM) \
6273 if (ust_ops.SYM == NULL) \
6274 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6275 if (ust_ops.SYM == NULL) \
6279 #define USTF(SYM) ust_ops.SYM
6281 /* Get pointers to all libust.so functions we care about. */
6286 GET_UST_SYM (serialize_to_text
);
6288 GET_UST_SYM (ltt_probe_register
);
6289 GET_UST_SYM (ltt_probe_unregister
);
6290 GET_UST_SYM (ltt_marker_connect
);
6291 GET_UST_SYM (ltt_marker_disconnect
);
6293 GET_UST_SYM (marker_iter_start
);
6294 GET_UST_SYM (marker_iter_next
);
6295 GET_UST_SYM (marker_iter_stop
);
6296 GET_UST_SYM (marker_iter_reset
);
6302 /* Given an UST marker, return the matching gdb static tracepoint.
6303 The match is done by address. */
6305 static struct tracepoint
*
6306 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6308 struct tracepoint
*tpoint
;
6310 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6312 if (tpoint
->type
!= static_tracepoint
)
6315 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6322 /* The probe function we install on lttng/ust markers. Whenever a
6323 probed ust marker is hit, this function is called. This is similar
6324 to gdb_collect, only for static tracepoints, instead of fast
6328 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6329 struct registers
*regs
, void *call_private
,
6330 const char *fmt
, va_list *args
)
6332 struct tracepoint
*tpoint
;
6333 struct static_tracepoint_ctx ctx
;
6335 /* Don't do anything until the trace run is completely set up. */
6338 trace_debug ("gdb_probe: not tracing\n");
6342 ctx
.base
.type
= static_tracepoint
;
6343 ctx
.regcache_initted
= 0;
6348 /* Wrap the regblock in a register cache (in the stack, we don't
6349 want to malloc here). */
6350 ctx
.regspace
= alloca (register_cache_size ());
6351 if (ctx
.regspace
== NULL
)
6353 trace_debug ("Trace buffer block allocation failed, skipping");
6357 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6360 trace_debug ("gdb_probe: marker not known: "
6361 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6362 mdata
->location
, mdata
->channel
,
6363 mdata
->name
, mdata
->format
);
6367 if (!tpoint
->enabled
)
6369 trace_debug ("gdb_probe: tracepoint disabled");
6373 ctx
.tpoint
= tpoint
;
6375 trace_debug ("gdb_probe: collecting marker: "
6376 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6377 mdata
->location
, mdata
->channel
,
6378 mdata
->name
, mdata
->format
);
6380 /* Test the condition if present, and collect if true. */
6381 if (tpoint
->cond
== NULL
6382 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6385 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6386 tpoint
->address
, tpoint
);
6388 if (stopping_tracepoint
6389 || trace_buffer_is_full
6390 || expr_eval_result
!= expr_eval_no_error
)
6395 /* If there was a condition and it evaluated to false, the only
6396 way we would stop tracing is if there was an error during
6397 condition expression evaluation. */
6398 if (expr_eval_result
!= expr_eval_no_error
)
6403 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6404 static tracepoint string data. This is a string passed to the
6405 tracing library by the user, at the time of the tracepoint marker
6406 call. E.g., in the UST marker call:
6408 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6410 the collected data is "str FOOBAZ".
6414 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6415 struct traceframe
*tframe
)
6417 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6418 unsigned char *bufspace
;
6421 unsigned short blocklen
;
6425 trace_debug ("Wanted to collect static trace data, "
6426 "but there's no static trace data");
6430 va_copy (copy
, *umd
->args
);
6431 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6434 trace_debug ("Want to collect ust data");
6436 /* 'S' + size + string */
6437 bufspace
= add_traceframe_block (tframe
,
6438 1 + sizeof (blocklen
) + size
+ 1);
6439 if (bufspace
== NULL
)
6441 trace_debug ("Trace buffer block allocation failed, skipping");
6445 /* Identify a static trace data block. */
6448 blocklen
= size
+ 1;
6449 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6451 va_copy (copy
, *umd
->args
);
6452 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6453 size
+ 1, umd
->fmt
, copy
);
6456 trace_debug ("Storing static tracepoint data in regblock: %s",
6457 bufspace
+ 1 + sizeof (blocklen
));
6460 /* The probe to register with lttng/ust. */
6461 static struct ltt_available_probe gdb_ust_probe
=
6468 #endif /* HAVE_UST */
6469 #endif /* IN_PROCESS_AGENT */
6471 #ifndef IN_PROCESS_AGENT
6473 /* Ask the in-process agent to run a command. Since we don't want to
6474 have to handle the IPA hitting breakpoints while running the
6475 command, we pause all threads, remove all breakpoints, and then set
6476 the helper thread re-running. We communicate with the helper
6477 thread by means of direct memory xfering, and a socket for
6481 run_inferior_command (char *cmd
)
6484 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
6486 trace_debug ("run_inferior_command: running: %s", cmd
);
6489 uninsert_all_breakpoints ();
6491 err
= agent_run_command (pid
, (const char *) cmd
);
6493 reinsert_all_breakpoints ();
6499 #else /* !IN_PROCESS_AGENT */
6501 #include <sys/socket.h>
6504 #ifndef UNIX_PATH_MAX
6505 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6508 /* Where we put the socked used for synchronization. */
6509 #define SOCK_DIR P_tmpdir
6511 /* Thread ID of the helper thread. GDBserver reads this to know which
6512 is the help thread. This is an LWP id on Linux. */
6513 int helper_thread_id
;
6516 init_named_socket (const char *name
)
6519 struct sockaddr_un addr
;
6521 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6524 warning ("socket creation failed: %s", strerror (errno
));
6528 addr
.sun_family
= AF_UNIX
;
6530 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6531 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6533 result
= access (name
, F_OK
);
6537 result
= unlink (name
);
6540 warning ("unlink failed: %s", strerror (errno
));
6544 warning ("socket %s already exists; overwriting", name
);
6547 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6550 warning ("bind failed: %s", strerror (errno
));
6555 result
= listen (fd
, 1);
6558 warning ("listen: %s", strerror (errno
));
6567 gdb_agent_socket_init (void)
6570 char name
[UNIX_PATH_MAX
];
6572 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6573 SOCK_DIR
, getpid ());
6574 if (result
>= UNIX_PATH_MAX
)
6576 trace_debug ("string overflow allocating socket name");
6580 fd
= init_named_socket (name
);
6582 warning ("Error initializing named socket (%s) for communication with the "
6583 "ust helper thread. Check that directory exists and that it "
6584 "is writable.", name
);
6591 /* The next marker to be returned on a qTsSTM command. */
6592 static const struct marker
*next_st
;
6594 /* Returns the first known marker. */
6599 struct marker_iter iter
;
6601 USTF(marker_iter_reset
) (&iter
);
6602 USTF(marker_iter_start
) (&iter
);
6607 /* Returns the marker following M. */
6609 const struct marker
*
6610 next_marker (const struct marker
*m
)
6612 struct marker_iter iter
;
6614 USTF(marker_iter_reset
) (&iter
);
6615 USTF(marker_iter_start
) (&iter
);
6617 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6619 if (iter
.marker
== m
)
6621 USTF(marker_iter_next
) (&iter
);
6629 /* Return an hexstr version of the STR C string, fit for sending to
6633 cstr_to_hexstr (const char *str
)
6635 int len
= strlen (str
);
6636 char *hexstr
= xmalloc (len
* 2 + 1);
6637 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6641 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6645 response_ust_marker (char *packet
, const struct marker
*st
)
6647 char *strid
, *format
, *tmp
;
6649 next_st
= next_marker (st
);
6651 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6652 strlen (st
->name
) + 1);
6653 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6655 strid
= cstr_to_hexstr (tmp
);
6658 format
= cstr_to_hexstr (st
->format
);
6660 sprintf (packet
, "m%s:%s:%s",
6661 paddress ((uintptr_t) st
->location
),
6669 /* Return the first static tracepoint, and initialize the state
6670 machine that will iterate through all the static tracepoints. */
6673 cmd_qtfstm (char *packet
)
6675 trace_debug ("Returning first trace state variable definition");
6677 if (first_marker ())
6678 response_ust_marker (packet
, first_marker ());
6680 strcpy (packet
, "l");
6683 /* Return additional trace state variable definitions. */
6686 cmd_qtsstm (char *packet
)
6688 trace_debug ("Returning static tracepoint");
6691 response_ust_marker (packet
, next_st
);
6693 strcpy (packet
, "l");
6696 /* Disconnect the GDB probe from a marker at a given address. */
6699 unprobe_marker_at (char *packet
)
6703 struct marker_iter iter
;
6705 p
+= sizeof ("unprobe_marker_at:") - 1;
6707 p
= unpack_varlen_hex (p
, &address
);
6709 USTF(marker_iter_reset
) (&iter
);
6710 USTF(marker_iter_start
) (&iter
);
6711 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6712 if ((uintptr_t ) iter
.marker
->location
== address
)
6716 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
6720 warning ("could not disable marker %s/%s",
6721 iter
.marker
->channel
, iter
.marker
->name
);
6726 /* Connect the GDB probe to a marker at a given address. */
6729 probe_marker_at (char *packet
)
6733 struct marker_iter iter
;
6736 p
+= sizeof ("probe_marker_at:") - 1;
6738 p
= unpack_varlen_hex (p
, &address
);
6740 USTF(marker_iter_reset
) (&iter
);
6742 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6744 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6745 if ((uintptr_t ) m
->location
== address
)
6749 trace_debug ("found marker for address. "
6750 "ltt_marker_connect (marker = %s/%s)",
6751 m
->channel
, m
->name
);
6753 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
6755 if (result
&& result
!= -EEXIST
)
6756 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
6757 m
->channel
, m
->name
, -result
);
6761 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
6762 m
->channel
, m
->name
);
6766 strcpy (packet
, "OK");
6770 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
6775 cmd_qtstmat (char *packet
)
6779 struct marker_iter iter
;
6782 p
+= sizeof ("qTSTMat:") - 1;
6784 p
= unpack_varlen_hex (p
, &address
);
6786 USTF(marker_iter_reset
) (&iter
);
6788 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6790 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6791 if ((uintptr_t ) m
->location
== address
)
6793 response_ust_marker (packet
, m
);
6797 strcpy (packet
, "l");
6807 USTF(ltt_probe_register
) (&gdb_ust_probe
);
6810 #endif /* HAVE_UST */
6812 #include <sys/syscall.h>
6814 /* Helper thread of agent. */
6817 gdb_agent_helper_thread (void *arg
)
6823 listen_fd
= gdb_agent_socket_init ();
6825 if (helper_thread_id
== 0)
6826 helper_thread_id
= syscall (SYS_gettid
);
6828 if (listen_fd
== -1)
6830 warning ("could not create sync socket\n");
6837 struct sockaddr_un sockaddr
;
6842 tmp
= sizeof (sockaddr
);
6846 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
6848 /* It seems an ERESTARTSYS can escape out of accept. */
6849 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
6853 warning ("Accept returned %d, error: %s\n",
6854 fd
, strerror (errno
));
6860 ret
= read (fd
, buf
, 1);
6861 } while (ret
== -1 && errno
== EINTR
);
6865 warning ("reading socket (fd=%d) failed with %s",
6866 fd
, strerror (errno
));
6874 if (strcmp ("qTfSTM", cmd_buf
) == 0)
6876 cmd_qtfstm (cmd_buf
);
6878 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
6880 cmd_qtsstm (cmd_buf
);
6882 else if (strncmp ("unprobe_marker_at:",
6884 sizeof ("unprobe_marker_at:") - 1) == 0)
6886 unprobe_marker_at (cmd_buf
);
6888 else if (strncmp ("probe_marker_at:",
6890 sizeof ("probe_marker_at:") - 1) == 0)
6892 probe_marker_at (cmd_buf
);
6894 else if (strncmp ("qTSTMat:",
6896 sizeof ("qTSTMat:") - 1) == 0)
6898 cmd_qtstmat (cmd_buf
);
6900 #endif /* HAVE_UST */
6903 /* Fix compiler's warning: ignoring return value of 'write'. */
6904 ret
= write (fd
, buf
, 1);
6913 #include <pthread.h>
6915 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
6918 gdb_agent_init (void)
6925 /* We want the helper thread to be as transparent as possible, so
6926 have it inherit an all-signals-blocked mask. */
6928 sigfillset (&new_mask
);
6929 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
6931 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
6933 res
= pthread_create (&thread
,
6935 gdb_agent_helper_thread
,
6938 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
6940 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
6942 while (helper_thread_id
== 0)
6950 #include <sys/mman.h>
6953 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
6954 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
6955 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
6956 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
6957 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
6958 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
6960 /* Record the result of getting buffer space for fast tracepoint
6961 trampolines. Any error message is copied, since caller may not be
6962 using persistent storage. */
6965 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
6967 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
6968 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
6970 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
6972 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
6975 static void __attribute__ ((constructor
))
6976 initialize_tracepoint_ftlib (void)
6978 initialize_tracepoint ();
6983 #endif /* IN_PROCESS_AGENT */
6985 /* Return a timestamp, expressed as microseconds of the usual Unix
6986 time. (As the result is a 64-bit number, it will not overflow any
6990 get_timestamp (void)
6994 if (gettimeofday (&tv
, 0) != 0)
6997 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7001 initialize_tracepoint (void)
7003 /* There currently no way to change the buffer size. */
7004 const int sizeOfBuffer
= 5 * 1024 * 1024;
7005 unsigned char *buf
= xmalloc (sizeOfBuffer
);
7006 init_trace_buffer (buf
, sizeOfBuffer
);
7008 /* Wire trace state variable 1 to be the timestamp. This will be
7009 uploaded to GDB upon connection and become one of its trace state
7010 variables. (In case you're wondering, if GDB already has a trace
7011 variable numbered 1, it will be renumbered.) */
7012 create_trace_state_variable (1, 0);
7013 set_trace_state_variable_name (1, "trace_timestamp");
7014 set_trace_state_variable_getter (1, get_timestamp
);
7016 #ifdef IN_PROCESS_AGENT
7021 pagesize
= sysconf (_SC_PAGE_SIZE
);
7025 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7027 #define SCRATCH_BUFFER_NPAGES 20
7029 /* Allocate scratch buffer aligned on a page boundary, at a low
7030 address (close to the main executable's code). */
7031 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7033 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7034 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7035 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7037 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7043 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7046 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7049 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7051 /* It's not a fatal error for something to go wrong with trampoline
7052 buffer setup, but it can be mysterious, so create a channel to
7053 report back on what went wrong, using a fixed size since we may
7054 not be able to allocate space later when the problem occurs. */
7055 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7057 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7059 initialize_low_tracepoint ();