1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "tracepoint.h"
21 #include "gdbthread.h"
35 #define DEFAULT_TRACE_BUFFER_SIZE 5242880 /* 5*1024*1024 */
37 /* This file is built for both GDBserver, and the in-process
38 agent (IPA), a shared library that includes a tracing agent that is
39 loaded by the inferior to support fast tracepoints. Fast
40 tracepoints (or more accurately, jump based tracepoints) are
41 implemented by patching the tracepoint location with a jump into a
42 small trampoline function whose job is to save the register state,
43 call the in-process tracing agent, and then execute the original
44 instruction that was under the tracepoint jump (possibly adjusted,
45 if PC-relative, or some such).
47 The current synchronization design is pull based. That means,
48 GDBserver does most of the work, by peeking/poking at the inferior
49 agent's memory directly for downloading tracepoint and associated
50 objects, and for uploading trace frames. Whenever the IPA needs
51 something from GDBserver (trace buffer is full, tracing stopped for
52 some reason, etc.) the IPA calls a corresponding hook function
53 where GDBserver has placed a breakpoint.
55 Each of the agents has its own trace buffer. When browsing the
56 trace frames built from slow and fast tracepoints from GDB (tfind
57 mode), there's no guarantee the user is seeing the trace frames in
58 strict chronological creation order, although, GDBserver tries to
59 keep the order relatively reasonable, by syncing the trace buffers
64 static void trace_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
67 trace_vdebug (const char *fmt
, ...)
73 vsprintf (buf
, fmt
, ap
);
74 fprintf (stderr
, PROG
"/tracepoint: %s\n", buf
);
78 #define trace_debug_1(level, fmt, args...) \
80 if (level <= debug_threads) \
81 trace_vdebug ((fmt), ##args); \
84 #define trace_debug(FMT, args...) \
85 trace_debug_1 (1, FMT, ##args)
88 # define ATTR_USED __attribute__((used))
89 # define ATTR_NOINLINE __attribute__((noinline))
90 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
93 # define ATTR_NOINLINE
94 # define ATTR_CONSTRUCTOR
97 /* Make sure the functions the IPA needs to export (symbols GDBserver
98 needs to query GDB about) are exported. */
100 #ifdef IN_PROCESS_AGENT
101 # if defined _WIN32 || defined __CYGWIN__
102 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
105 # define IP_AGENT_EXPORT \
106 __attribute__ ((visibility("default"))) ATTR_USED
108 # define IP_AGENT_EXPORT ATTR_USED
112 # define IP_AGENT_EXPORT
115 /* Prefix exported symbols, for good citizenship. All the symbols
116 that need exporting are defined in this module. */
117 #ifdef IN_PROCESS_AGENT
118 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
119 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
120 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
121 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
122 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
123 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
124 # define collecting gdb_agent_collecting
125 # define gdb_collect gdb_agent_gdb_collect
126 # define stop_tracing gdb_agent_stop_tracing
127 # define flush_trace_buffer gdb_agent_flush_trace_buffer
128 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
129 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
130 # define stopping_tracepoint gdb_agent_stopping_tracepoint
131 # define expr_eval_result gdb_agent_expr_eval_result
132 # define error_tracepoint gdb_agent_error_tracepoint
133 # define tracepoints gdb_agent_tracepoints
134 # define tracing gdb_agent_tracing
135 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
136 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
137 # define trace_buffer_lo gdb_agent_trace_buffer_lo
138 # define trace_buffer_hi gdb_agent_trace_buffer_hi
139 # define traceframe_read_count gdb_agent_traceframe_read_count
140 # define traceframe_write_count gdb_agent_traceframe_write_count
141 # define traceframes_created gdb_agent_traceframes_created
142 # define trace_state_variables gdb_agent_trace_state_variables
143 # define get_raw_reg gdb_agent_get_raw_reg
144 # define get_trace_state_variable_value \
145 gdb_agent_get_trace_state_variable_value
146 # define set_trace_state_variable_value \
147 gdb_agent_set_trace_state_variable_value
148 # define ust_loaded gdb_agent_ust_loaded
149 # define helper_thread_id gdb_agent_helper_thread_id
150 # define cmd_buf gdb_agent_cmd_buf
153 #ifndef IN_PROCESS_AGENT
155 /* Addresses of in-process agent's symbols GDBserver cares about. */
157 struct ipa_sym_addresses
159 CORE_ADDR addr_gdb_tp_heap_buffer
;
160 CORE_ADDR addr_gdb_jump_pad_buffer
;
161 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
162 CORE_ADDR addr_gdb_trampoline_buffer
;
163 CORE_ADDR addr_gdb_trampoline_buffer_end
;
164 CORE_ADDR addr_gdb_trampoline_buffer_error
;
165 CORE_ADDR addr_collecting
;
166 CORE_ADDR addr_gdb_collect
;
167 CORE_ADDR addr_stop_tracing
;
168 CORE_ADDR addr_flush_trace_buffer
;
169 CORE_ADDR addr_about_to_request_buffer_space
;
170 CORE_ADDR addr_trace_buffer_is_full
;
171 CORE_ADDR addr_stopping_tracepoint
;
172 CORE_ADDR addr_expr_eval_result
;
173 CORE_ADDR addr_error_tracepoint
;
174 CORE_ADDR addr_tracepoints
;
175 CORE_ADDR addr_tracing
;
176 CORE_ADDR addr_trace_buffer_ctrl
;
177 CORE_ADDR addr_trace_buffer_ctrl_curr
;
178 CORE_ADDR addr_trace_buffer_lo
;
179 CORE_ADDR addr_trace_buffer_hi
;
180 CORE_ADDR addr_traceframe_read_count
;
181 CORE_ADDR addr_traceframe_write_count
;
182 CORE_ADDR addr_traceframes_created
;
183 CORE_ADDR addr_trace_state_variables
;
184 CORE_ADDR addr_get_raw_reg
;
185 CORE_ADDR addr_get_trace_state_variable_value
;
186 CORE_ADDR addr_set_trace_state_variable_value
;
187 CORE_ADDR addr_ust_loaded
;
196 IPA_SYM(gdb_tp_heap_buffer
),
197 IPA_SYM(gdb_jump_pad_buffer
),
198 IPA_SYM(gdb_jump_pad_buffer_end
),
199 IPA_SYM(gdb_trampoline_buffer
),
200 IPA_SYM(gdb_trampoline_buffer_end
),
201 IPA_SYM(gdb_trampoline_buffer_error
),
203 IPA_SYM(gdb_collect
),
204 IPA_SYM(stop_tracing
),
205 IPA_SYM(flush_trace_buffer
),
206 IPA_SYM(about_to_request_buffer_space
),
207 IPA_SYM(trace_buffer_is_full
),
208 IPA_SYM(stopping_tracepoint
),
209 IPA_SYM(expr_eval_result
),
210 IPA_SYM(error_tracepoint
),
211 IPA_SYM(tracepoints
),
213 IPA_SYM(trace_buffer_ctrl
),
214 IPA_SYM(trace_buffer_ctrl_curr
),
215 IPA_SYM(trace_buffer_lo
),
216 IPA_SYM(trace_buffer_hi
),
217 IPA_SYM(traceframe_read_count
),
218 IPA_SYM(traceframe_write_count
),
219 IPA_SYM(traceframes_created
),
220 IPA_SYM(trace_state_variables
),
221 IPA_SYM(get_raw_reg
),
222 IPA_SYM(get_trace_state_variable_value
),
223 IPA_SYM(set_trace_state_variable_value
),
227 static struct ipa_sym_addresses ipa_sym_addrs
;
229 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
231 /* Returns true if both the in-process agent library and the static
232 tracepoints libraries are loaded in the inferior, and agent has
233 capability on static tracepoints. */
236 in_process_agent_supports_ust (void)
240 if (!agent_loaded_p ())
242 warning ("In-process agent not loaded");
246 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE
))
248 /* Agent understands static tracepoint, then check whether UST is in
249 fact loaded in the inferior. */
250 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
252 warning ("Error reading ust_loaded in lib");
263 write_e_ipa_not_loaded (char *buffer
)
266 "E.In-process agent library not loaded in process. "
267 "Fast and static tracepoints unavailable.");
270 /* Write an error to BUFFER indicating that UST isn't loaded in the
274 write_e_ust_not_loaded (char *buffer
)
278 "E.UST library not loaded in process. "
279 "Static tracepoints unavailable.");
281 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
285 /* If the in-process agent library isn't loaded in the inferior, write
286 an error to BUFFER, and return 1. Otherwise, return 0. */
289 maybe_write_ipa_not_loaded (char *buffer
)
291 if (!agent_loaded_p ())
293 write_e_ipa_not_loaded (buffer
);
299 /* If the in-process agent library and the ust (static tracepoints)
300 library aren't loaded in the inferior, write an error to BUFFER,
301 and return 1. Otherwise, return 0. */
304 maybe_write_ipa_ust_not_loaded (char *buffer
)
306 if (!agent_loaded_p ())
308 write_e_ipa_not_loaded (buffer
);
311 else if (!in_process_agent_supports_ust ())
313 write_e_ust_not_loaded (buffer
);
319 /* Cache all future symbols that the tracepoints module might request.
320 We can not request symbols at arbitrary states in the remote
321 protocol, only when the client tells us that new symbols are
322 available. So when we load the in-process library, make sure to
323 check the entire list. */
326 tracepoint_look_up_symbols (void)
330 if (agent_loaded_p ())
333 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
336 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
338 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
341 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
346 agent_look_up_symbols (NULL
);
351 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
352 of the "stop_tracing" function. When this breakpoint is hit,
353 tracing stopped in the IPA for some reason. E.g., due to
354 tracepoint reaching the pass count, hitting conditional expression
355 evaluation error, etc.
357 The IPA's trace buffer is never in circular tracing mode: instead,
358 GDBserver's is, and whenever the in-process buffer fills, it calls
359 "flush_trace_buffer", which triggers an internal breakpoint.
360 GDBserver reacts to this breakpoint by pulling the meanwhile
361 collected data. Old frames discarding is always handled on the
364 #ifdef IN_PROCESS_AGENT
366 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
368 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
372 /* Call this in the functions where GDBserver places a breakpoint, so
373 that the compiler doesn't try to be clever and skip calling the
374 function at all. This is necessary, even if we tell the compiler
375 to not inline said functions. */
377 #if defined(__GNUC__)
378 # define UNKNOWN_SIDE_EFFECTS() asm ("")
380 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
383 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
386 /* GDBserver places breakpoint here. */
387 UNKNOWN_SIDE_EFFECTS();
390 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
391 flush_trace_buffer (void)
393 /* GDBserver places breakpoint here. */
394 UNKNOWN_SIDE_EFFECTS();
399 #ifndef IN_PROCESS_AGENT
401 tracepoint_handler (CORE_ADDR address
)
403 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
408 /* Breakpoint at "stop_tracing" in the inferior lib. */
409 struct breakpoint
*stop_tracing_bkpt
;
410 static int stop_tracing_handler (CORE_ADDR
);
412 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
413 struct breakpoint
*flush_trace_buffer_bkpt
;
414 static int flush_trace_buffer_handler (CORE_ADDR
);
416 static void download_trace_state_variables (void);
417 static void upload_fast_traceframes (void);
419 static int run_inferior_command (char *cmd
, int len
);
422 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
424 return read_inferior_memory (symaddr
, (unsigned char *) val
,
429 static int tracepoint_send_agent (struct tracepoint
*tpoint
);
432 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
434 return read_inferior_memory (symaddr
, (unsigned char *) val
,
439 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
441 void *pval
= (void *) (uintptr_t) val
;
444 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
445 *val
= (uintptr_t) pval
;
450 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
452 void *pval
= (void *) (uintptr_t) val
;
453 return write_inferior_memory (symaddr
,
454 (unsigned char *) &pval
, sizeof (pval
));
458 write_inferior_integer (CORE_ADDR symaddr
, int val
)
460 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
464 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
466 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
469 static CORE_ADDR
target_malloc (ULONGEST size
);
470 static int write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
);
472 #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD) \
474 memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \
475 BUF += sizeof ((OBJ)->FIELD); \
480 /* Operations on various types of tracepoint actions. */
482 struct tracepoint_action
;
484 struct tracepoint_action_ops
486 /* Download tracepoint action ACTION to IPA. Return the address of action
488 CORE_ADDR (*download
) (const struct tracepoint_action
*action
);
490 /* Send ACTION to agent via command buffer started from BUFFER. Return
491 updated head of command buffer. */
492 char* (*send
) (char *buffer
, const struct tracepoint_action
*action
);
495 /* Base action. Concrete actions inherit this. */
497 struct tracepoint_action
499 #ifndef IN_PROCESS_AGENT
500 const struct tracepoint_action_ops
*ops
;
505 /* An 'M' (collect memory) action. */
506 struct collect_memory_action
508 struct tracepoint_action base
;
515 /* An 'R' (collect registers) action. */
517 struct collect_registers_action
519 struct tracepoint_action base
;
522 /* An 'X' (evaluate expression) action. */
524 struct eval_expr_action
526 struct tracepoint_action base
;
528 struct agent_expr
*expr
;
531 /* An 'L' (collect static trace data) action. */
532 struct collect_static_trace_data_action
534 struct tracepoint_action base
;
537 #ifndef IN_PROCESS_AGENT
539 m_tracepoint_action_download (const struct tracepoint_action
*action
)
541 int size_in_ipa
= (sizeof (struct collect_memory_action
)
542 - offsetof (struct tracepoint_action
, type
));
543 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
545 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
551 m_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
553 struct collect_memory_action
*maction
554 = (struct collect_memory_action
*) action
;
556 COPY_FIELD_TO_BUF (buffer
, maction
, addr
);
557 COPY_FIELD_TO_BUF (buffer
, maction
, len
);
558 COPY_FIELD_TO_BUF (buffer
, maction
, basereg
);
563 static const struct tracepoint_action_ops m_tracepoint_action_ops
=
565 m_tracepoint_action_download
,
566 m_tracepoint_action_send
,
570 r_tracepoint_action_download (const struct tracepoint_action
*action
)
572 int size_in_ipa
= (sizeof (struct collect_registers_action
)
573 - offsetof (struct tracepoint_action
, type
));
574 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
576 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
583 r_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
588 static const struct tracepoint_action_ops r_tracepoint_action_ops
=
590 r_tracepoint_action_download
,
591 r_tracepoint_action_send
,
594 static CORE_ADDR
download_agent_expr (struct agent_expr
*expr
);
597 x_tracepoint_action_download (const struct tracepoint_action
*action
)
599 int size_in_ipa
= (sizeof (struct eval_expr_action
)
600 - offsetof (struct tracepoint_action
, type
));
601 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
604 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
606 expr
= download_agent_expr (((struct eval_expr_action
*)action
)->expr
);
607 write_inferior_data_ptr (ipa_action
+ offsetof (struct eval_expr_action
, expr
)
608 - offsetof (struct tracepoint_action
, type
),
614 /* Copy agent expression AEXPR to buffer pointed by P. If AEXPR is NULL,
615 copy 0 to P. Return updated header of buffer. */
618 agent_expr_send (char *p
, const struct agent_expr
*aexpr
)
620 /* Copy the length of condition first, and then copy its
629 memcpy (p
, &aexpr
->length
, 4);
632 memcpy (p
, aexpr
->bytes
, aexpr
->length
);
639 x_tracepoint_action_send ( char *buffer
, const struct tracepoint_action
*action
)
641 struct eval_expr_action
*eaction
= (struct eval_expr_action
*) action
;
643 return agent_expr_send (buffer
, eaction
->expr
);
646 static const struct tracepoint_action_ops x_tracepoint_action_ops
=
648 x_tracepoint_action_download
,
649 x_tracepoint_action_send
,
653 l_tracepoint_action_download (const struct tracepoint_action
*action
)
655 int size_in_ipa
= (sizeof (struct collect_static_trace_data_action
)
656 - offsetof (struct tracepoint_action
, type
));
657 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
659 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
666 l_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
671 static const struct tracepoint_action_ops l_tracepoint_action_ops
=
673 l_tracepoint_action_download
,
674 l_tracepoint_action_send
,
678 /* This structure describes a piece of the source-level definition of
679 the tracepoint. The contents are not interpreted by the target,
680 but preserved verbatim for uploading upon reconnection. */
684 /* The type of string, such as "cond" for a conditional. */
687 /* The source-level string itself. For the sake of target
688 debugging, we store it in plaintext, even though it is always
689 transmitted in hex. */
692 /* Link to the next one in the list. We link them in the order
693 received, in case some make up an ordered list of commands or
695 struct source_string
*next
;
700 /* Trap based tracepoint. */
703 /* A fast tracepoint implemented with a jump instead of a trap. */
706 /* A static tracepoint, implemented by a program call into a tracing
711 struct tracepoint_hit_ctx
;
713 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
716 /* The definition of a tracepoint. */
718 /* Tracepoints may have multiple locations, each at a different
719 address. This can occur with optimizations, template
720 instantiation, etc. Since the locations may be in different
721 scopes, the conditions and actions may be different for each
722 location. Our target version of tracepoints is more like GDB's
723 notion of "breakpoint locations", but we have almost nothing that
724 is not per-location, so we bother having two kinds of objects. The
725 key consequence is that numbers are not unique, and that it takes
726 both number and address to identify a tracepoint uniquely. */
730 /* The number of the tracepoint, as specified by GDB. Several
731 tracepoint objects here may share a number. */
734 /* Address at which the tracepoint is supposed to trigger. Several
735 tracepoints may share an address. */
738 /* Tracepoint type. */
739 enum tracepoint_type type
;
741 /* True if the tracepoint is currently enabled. */
744 /* The number of single steps that will be performed after each
748 /* The number of times the tracepoint may be hit before it will
749 terminate the entire tracing run. */
752 /* Pointer to the agent expression that is the tracepoint's
753 conditional, or NULL if the tracepoint is unconditional. */
754 struct agent_expr
*cond
;
756 /* The list of actions to take when the tracepoint triggers. */
758 struct tracepoint_action
**actions
;
760 /* Count of the times we've hit this tracepoint during the run.
761 Note that while-stepping steps are not counted as "hits". */
764 /* Cached sum of the sizes of traceframes created by this point. */
765 uint64_t traceframe_usage
;
767 CORE_ADDR compiled_cond
;
769 /* Link to the next tracepoint in the list. */
770 struct tracepoint
*next
;
772 #ifndef IN_PROCESS_AGENT
773 /* The list of actions to take when the tracepoint triggers, in
774 string/packet form. */
777 /* The collection of strings that describe the tracepoint as it was
778 entered into GDB. These are not used by the target, but are
779 reported back to GDB upon reconnection. */
780 struct source_string
*source_strings
;
782 /* The number of bytes displaced by fast tracepoints. It may subsume
783 multiple instructions, for multi-byte fast tracepoints. This
784 field is only valid for fast tracepoints. */
787 /* Only for fast tracepoints. */
788 CORE_ADDR obj_addr_on_target
;
790 /* Address range where the original instruction under a fast
791 tracepoint was relocated to. (_end is actually one byte past
793 CORE_ADDR adjusted_insn_addr
;
794 CORE_ADDR adjusted_insn_addr_end
;
796 /* The address range of the piece of the jump pad buffer that was
797 assigned to this fast tracepoint. (_end is actually one byte
800 CORE_ADDR jump_pad_end
;
802 /* The address range of the piece of the trampoline buffer that was
803 assigned to this fast tracepoint. (_end is actually one byte
805 CORE_ADDR trampoline
;
806 CORE_ADDR trampoline_end
;
808 /* The list of actions to take while in a stepping loop. These
809 fields are only valid for patch-based tracepoints. */
810 int num_step_actions
;
811 struct tracepoint_action
**step_actions
;
812 /* Same, but in string/packet form. */
813 char **step_actions_str
;
815 /* Handle returned by the breakpoint or tracepoint module when we
816 inserted the trap or jump, or hooked into a static tracepoint.
817 NULL if we haven't inserted it yet. */
823 #ifndef IN_PROCESS_AGENT
825 /* Given `while-stepping', a thread may be collecting data for more
826 than one tracepoint simultaneously. On the other hand, the same
827 tracepoint with a while-stepping action may be hit by more than one
828 thread simultaneously (but not quite, each thread could be handling
829 a different step). Each thread holds a list of these objects,
830 representing the current step of each while-stepping action being
835 struct wstep_state
*next
;
837 /* The tracepoint number. */
839 /* The tracepoint's address. */
840 CORE_ADDR tp_address
;
842 /* The number of the current step in this 'while-stepping'
849 /* The linked list of all tracepoints. Marked explicitly as used as
850 the in-process library doesn't use it for the fast tracepoints
852 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
854 #ifndef IN_PROCESS_AGENT
856 /* Pointer to the last tracepoint in the list, new tracepoints are
857 linked in at the end. */
859 static struct tracepoint
*last_tracepoint
;
862 /* The first tracepoint to exceed its pass count. */
864 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
866 /* True if the trace buffer is full or otherwise no longer usable. */
868 IP_AGENT_EXPORT
int trace_buffer_is_full
;
870 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
872 #ifndef IN_PROCESS_AGENT
874 static const char *eval_result_names
[] =
876 "terror:in the attic", /* this should never be reported */
877 "terror:empty expression",
878 "terror:empty stack",
879 "terror:stack overflow",
880 "terror:stack underflow",
881 "terror:unhandled opcode",
882 "terror:unrecognized opcode",
883 "terror:divide by zero"
888 /* The tracepoint in which the error occurred. */
890 static struct tracepoint
*error_tracepoint
;
892 struct trace_state_variable
894 /* This is the name of the variable as used in GDB. The target
895 doesn't use the name, but needs to have it for saving and
896 reconnection purposes. */
899 /* This number identifies the variable uniquely. Numbers may be
900 assigned either by the target (in the case of builtin variables),
901 or by GDB, and are presumed unique during the course of a trace
905 /* The variable's initial value, a 64-bit signed integer always. */
906 LONGEST initial_value
;
908 /* The variable's value, a 64-bit signed integer always. */
911 /* Pointer to a getter function, used to supply computed values. */
912 LONGEST (*getter
) (void);
914 /* Link to the next variable. */
915 struct trace_state_variable
*next
;
918 /* Linked list of all trace state variables. */
920 #ifdef IN_PROCESS_AGENT
921 struct trace_state_variable
*alloced_trace_state_variables
;
924 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
926 /* The results of tracing go into a fixed-size space known as the
927 "trace buffer". Because usage follows a limited number of
928 patterns, we manage it ourselves rather than with malloc. Basic
929 rules are that we create only one trace frame at a time, each is
930 variable in size, they are never moved once created, and we only
931 discard if we are doing a circular buffer, and then only the oldest
932 ones. Each trace frame includes its own size, so we don't need to
933 link them together, and the trace frame number is relative to the
934 first one, so we don't need to record numbers. A trace frame also
935 records the number of the tracepoint that created it. The data
936 itself is a series of blocks, each introduced by a single character
937 and with a defined format. Each type of block has enough
938 type/length info to allow scanners to jump quickly from one block
939 to the next without reading each byte in the block. */
941 /* Trace buffer management would be simple - advance a free pointer
942 from beginning to end, then stop - were it not for the circular
943 buffer option, which is a useful way to prevent a trace run from
944 stopping prematurely because the buffer filled up. In the circular
945 case, the location of the first trace frame (trace_buffer_start)
946 moves as old trace frames are discarded. Also, since we grow trace
947 frames incrementally as actions are performed, we wrap around to
948 the beginning of the trace buffer. This is per-block, so each
949 block within a trace frame remains contiguous. Things get messy
950 when the wrapped-around trace frame is the one being discarded; the
951 free space ends up in two parts at opposite ends of the buffer. */
954 # if defined(__GNUC__)
955 # define ATTR_PACKED __attribute__ ((packed))
957 # define ATTR_PACKED /* nothing */
961 /* The data collected at a tracepoint hit. This object should be as
962 small as possible, since there may be a great many of them. We do
963 not need to keep a frame number, because they are all sequential
964 and there are no deletions; so the Nth frame in the buffer is
965 always frame number N. */
969 /* Number of the tracepoint that collected this traceframe. A value
970 of 0 indicates the current end of the trace buffer. We make this
971 a 16-bit field because it's never going to happen that GDB's
972 numbering of tracepoints reaches 32,000. */
975 /* The size of the data in this trace frame. We limit this to 32
976 bits, even on a 64-bit target, because it's just implausible that
977 one is validly going to collect 4 gigabytes of data at a single
979 unsigned int data_size
: 32;
981 /* The base of the trace data, which is contiguous from this point. */
982 unsigned char data
[0];
986 /* The size of the EOB marker, in bytes. A traceframe with zeroed
987 fields (and no data) marks the end of trace data. */
988 #define TRACEFRAME_EOB_MARKER_SIZE offsetof (struct traceframe, data)
990 /* The traceframe to be used as the source of data to send back to
991 GDB. A value of -1 means to get data from the live program. */
993 int current_traceframe
= -1;
995 /* This flag is true if the trace buffer is circular, meaning that
996 when it fills, the oldest trace frames are discarded in order to
999 #ifndef IN_PROCESS_AGENT
1000 static int circular_trace_buffer
;
1003 /* Size of the trace buffer. */
1005 static LONGEST trace_buffer_size
;
1007 /* Pointer to the block of memory that traceframes all go into. */
1009 static unsigned char *trace_buffer_lo
;
1011 /* Pointer to the end of the trace buffer, more precisely to the byte
1012 after the end of the buffer. */
1014 static unsigned char *trace_buffer_hi
;
1016 /* Control structure holding the read/write/etc. pointers into the
1017 trace buffer. We need more than one of these to implement a
1018 transaction-like mechanism to garantees that both GDBserver and the
1019 in-process agent can try to change the trace buffer
1022 struct trace_buffer_control
1024 /* Pointer to the first trace frame in the buffer. In the
1025 non-circular case, this is equal to trace_buffer_lo, otherwise it
1026 moves around in the buffer. */
1027 unsigned char *start
;
1029 /* Pointer to the free part of the trace buffer. Note that we clear
1030 several bytes at and after this pointer, so that traceframe
1031 scans/searches terminate properly. */
1032 unsigned char *free
;
1034 /* Pointer to the byte after the end of the free part. Note that
1035 this may be smaller than trace_buffer_free in the circular case,
1036 and means that the free part is in two pieces. Initially it is
1037 equal to trace_buffer_hi, then is generally equivalent to
1038 trace_buffer_start. */
1039 unsigned char *end_free
;
1041 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
1042 this is the point at which the trace data breaks, and resumes at
1044 unsigned char *wrap
;
1047 /* Same as above, to be used by GDBserver when updating the in-process
1049 struct ipa_trace_buffer_control
1058 /* We have possibly both GDBserver and an inferior thread accessing
1059 the same IPA trace buffer memory. The IPA is the producer (tries
1060 to put new frames in the buffer), while GDBserver occasionally
1061 consumes them, that is, flushes the IPA's buffer into its own
1062 buffer. Both sides need to update the trace buffer control
1063 pointers (current head, tail, etc.). We can't use a global lock to
1064 synchronize the accesses, as otherwise we could deadlock GDBserver
1065 (if the thread holding the lock stops for a signal, say). So
1066 instead of that, we use a transaction scheme where GDBserver writes
1067 always prevail over the IPAs writes, and, we have the IPA detect
1068 the commit failure/overwrite, and retry the whole attempt. This is
1069 mainly implemented by having a global token object that represents
1070 who wrote last to the buffer control structure. We need to freeze
1071 any inferior writing to the buffer while GDBserver touches memory,
1072 so that the inferior can correctly detect that GDBserver had been
1073 there, otherwise, it could mistakingly think its commit was
1074 successful; that's implemented by simply having GDBserver set a
1075 breakpoint the inferior hits if it is the critical region.
1077 There are three cycling trace buffer control structure copies
1078 (buffer head, tail, etc.), with the token object including an index
1079 indicating which is current live copy. The IPA tentatively builds
1080 an updated copy in a non-current control structure, while GDBserver
1081 always clobbers the current version directly. The IPA then tries
1082 to atomically "commit" its version; if GDBserver clobbered the
1083 structure meanwhile, that will fail, and the IPA restarts the
1086 Listing the step in further detail, we have:
1088 In-process agent (producer):
1090 - passes by `about_to_request_buffer_space' breakpoint/lock
1092 - reads current token, extracts current trace buffer control index,
1093 and starts tentatively updating the rightmost one (0->1, 1->2,
1094 2->0). Note that only one inferior thread is executing this code
1095 at any given time, due to an outer lock in the jump pads.
1097 - updates counters, and tries to commit the token.
1099 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1100 leaving the sync region.
1102 - checks if the update was effective.
1104 - if trace buffer was found full, hits flush_trace_buffer
1105 breakpoint, and restarts later afterwards.
1107 GDBserver (consumer):
1109 - sets `about_to_request_buffer_space' breakpoint/lock.
1111 - updates the token unconditionally, using the current buffer
1112 control index, since it knows that the IP agent always writes to
1113 the rightmost, and due to the breakpoint, at most one IP thread
1114 can try to update the trace buffer concurrently to GDBserver, so
1115 there will be no danger of trace buffer control index wrap making
1116 the IPA write to the same index as GDBserver.
1118 - flushes the IP agent's trace buffer completely, and updates the
1119 current trace buffer control structure. GDBserver *always* wins.
1121 - removes the `about_to_request_buffer_space' breakpoint.
1123 The token is stored in the `trace_buffer_ctrl_curr' variable.
1124 Internally, it's bits are defined as:
1126 |-------------+-----+-------------+--------+-------------+--------------|
1127 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1128 |-------------+-----+-------------+--------+-------------+--------------|
1129 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1130 |-------------+-----+-------------+--------+-------------+--------------|
1132 GSB - GDBserver Stamp Bit
1133 PC - Previous Counter
1134 CC - Current Counter
1135 TBCI - Trace Buffer Control Index
1138 An IPA update of `trace_buffer_ctrl_curr' does:
1140 - read CC from the current token, save as PC.
1142 - atomically tries to write PC+1,CC
1144 A GDBserver update of `trace_buffer_ctrl_curr' does:
1146 - reads PC and CC from the current token.
1151 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1152 for the counters described below. The cleared bits are used to
1153 hold the index of the items of the `trace_buffer_ctrl' array that
1155 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1157 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1158 counter, and the `current' counter. */
1160 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1161 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1163 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1164 always stamps this bit as set. */
1165 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1167 #ifdef IN_PROCESS_AGENT
1168 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1169 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1171 # define TRACE_BUFFER_CTRL_CURR \
1172 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1176 /* The GDBserver side agent only needs one instance of this object, as
1177 it doesn't need to sync with itself. Define it as array anyway so
1178 that the rest of the code base doesn't need to care for the
1180 struct trace_buffer_control trace_buffer_ctrl
[1];
1181 # define TRACE_BUFFER_CTRL_CURR 0
1184 /* These are convenience macros used to access the current trace
1185 buffer control in effect. */
1186 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1187 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1188 #define trace_buffer_end_free \
1189 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1190 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1193 /* Macro that returns a pointer to the first traceframe in the buffer. */
1195 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1197 /* Macro that returns a pointer to the next traceframe in the buffer.
1198 If the computed location is beyond the wraparound point, subtract
1199 the offset of the wraparound. */
1201 #define NEXT_TRACEFRAME_1(TF) \
1202 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1204 #define NEXT_TRACEFRAME(TF) \
1205 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1206 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1207 ? (trace_buffer_wrap - trace_buffer_lo) \
1210 /* The difference between these counters represents the total number
1211 of complete traceframes present in the trace buffer. The IP agent
1212 writes to the write count, GDBserver writes to read count. */
1214 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1215 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1217 /* Convenience macro. */
1219 #define traceframe_count \
1220 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1222 /* The count of all traceframes created in the current run, including
1223 ones that were discarded to make room. */
1225 IP_AGENT_EXPORT
int traceframes_created
;
1227 #ifndef IN_PROCESS_AGENT
1229 /* Read-only regions are address ranges whose contents don't change,
1230 and so can be read from target memory even while looking at a trace
1231 frame. Without these, disassembly for instance will likely fail,
1232 because the program code is not usually collected into a trace
1233 frame. This data structure does not need to be very complicated or
1234 particularly efficient, it's only going to be used occasionally,
1235 and only by some commands. */
1237 struct readonly_region
1239 /* The bounds of the region. */
1240 CORE_ADDR start
, end
;
1242 /* Link to the next one. */
1243 struct readonly_region
*next
;
1246 /* Linked list of readonly regions. This list stays in effect from
1247 one tstart to the next. */
1249 static struct readonly_region
*readonly_regions
;
1253 /* The global that controls tracing overall. */
1255 IP_AGENT_EXPORT
int tracing
;
1257 #ifndef IN_PROCESS_AGENT
1259 /* Controls whether tracing should continue after GDB disconnects. */
1261 int disconnected_tracing
;
1263 /* The reason for the last tracing run to have stopped. We initialize
1264 to a distinct string so that GDB can distinguish between "stopped
1265 after running" and "stopped because never run" cases. */
1267 static const char *tracing_stop_reason
= "tnotrun";
1269 static int tracing_stop_tpnum
;
1271 /* 64-bit timestamps for the trace run's start and finish, expressed
1272 in microseconds from the Unix epoch. */
1274 LONGEST tracing_start_time
;
1275 LONGEST tracing_stop_time
;
1277 /* The (optional) user-supplied name of the user that started the run.
1278 This is an arbitrary string, and may be NULL. */
1280 char *tracing_user_name
;
1282 /* Optional user-supplied text describing the run. This is
1283 an arbitrary string, and may be NULL. */
1285 char *tracing_notes
;
1287 /* Optional user-supplied text explaining a tstop command. This is an
1288 arbitrary string, and may be NULL. */
1290 char *tracing_stop_note
;
1294 /* Functions local to this file. */
1296 /* Base "class" for tracepoint type specific data to be passed down to
1297 collect_data_at_tracepoint. */
1298 struct tracepoint_hit_ctx
1300 enum tracepoint_type type
;
1303 #ifdef IN_PROCESS_AGENT
1305 /* Fast/jump tracepoint specific data to be passed down to
1306 collect_data_at_tracepoint. */
1307 struct fast_tracepoint_ctx
1309 struct tracepoint_hit_ctx base
;
1311 struct regcache regcache
;
1312 int regcache_initted
;
1313 unsigned char *regspace
;
1315 unsigned char *regs
;
1316 struct tracepoint
*tpoint
;
1319 /* Static tracepoint specific data to be passed down to
1320 collect_data_at_tracepoint. */
1321 struct static_tracepoint_ctx
1323 struct tracepoint_hit_ctx base
;
1325 /* The regcache corresponding to the registers state at the time of
1326 the tracepoint hit. Initialized lazily, from REGS. */
1327 struct regcache regcache
;
1328 int regcache_initted
;
1330 /* The buffer space REGCACHE above uses. We use a separate buffer
1331 instead of letting the regcache malloc for both signal safety and
1332 performance reasons; this is allocated on the stack instead. */
1333 unsigned char *regspace
;
1335 /* The register buffer as passed on by lttng/ust. */
1336 struct registers
*regs
;
1338 /* The "printf" formatter and the args the user passed to the marker
1339 call. We use this to be able to collect "static trace data"
1344 /* The GDB tracepoint matching the probed marker that was "hit". */
1345 struct tracepoint
*tpoint
;
1350 /* Static tracepoint specific data to be passed down to
1351 collect_data_at_tracepoint. */
1352 struct trap_tracepoint_ctx
1354 struct tracepoint_hit_ctx base
;
1356 struct regcache
*regcache
;
1361 #ifndef IN_PROCESS_AGENT
1362 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1363 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1366 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1367 struct tracepoint
*tpoint
);
1369 #ifndef IN_PROCESS_AGENT
1370 static void clear_readonly_regions (void);
1371 static void clear_installed_tracepoints (void);
1374 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1376 struct tracepoint
*tpoint
);
1377 #ifndef IN_PROCESS_AGENT
1378 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1380 struct tracepoint
*tpoint
, int current_step
);
1381 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1382 CORE_ADDR
*jump_entry
);
1384 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1386 struct tracepoint
*tpoint
,
1387 struct traceframe
*tframe
,
1388 struct tracepoint_action
*taction
);
1390 #ifndef IN_PROCESS_AGENT
1391 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1393 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1394 static void download_tracepoint (struct tracepoint
*);
1395 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1396 static void clone_fast_tracepoint (struct tracepoint
*to
,
1397 const struct tracepoint
*from
);
1400 static LONGEST
get_timestamp (void);
1402 #if defined(__GNUC__)
1403 # define memory_barrier() asm volatile ("" : : : "memory")
1405 # define memory_barrier() do {} while (0)
1408 /* We only build the IPA if this builtin is supported, and there are
1409 no uses of this in GDBserver itself, so we're safe in defining this
1411 #define cmpxchg(mem, oldval, newval) \
1412 __sync_val_compare_and_swap (mem, oldval, newval)
1414 /* Record that an error occurred during expression evaluation. */
1417 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1418 enum eval_result_type rtype
)
1420 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1421 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1423 #ifdef IN_PROCESS_AGENT
1424 /* Only record the first error we get. */
1425 if (cmpxchg (&expr_eval_result
,
1427 rtype
) != expr_eval_no_error
)
1430 if (expr_eval_result
!= expr_eval_no_error
)
1434 error_tracepoint
= tpoint
;
1437 /* Trace buffer management. */
1440 clear_trace_buffer (void)
1442 trace_buffer_start
= trace_buffer_lo
;
1443 trace_buffer_free
= trace_buffer_lo
;
1444 trace_buffer_end_free
= trace_buffer_hi
;
1445 trace_buffer_wrap
= trace_buffer_hi
;
1446 /* A traceframe with zeroed fields marks the end of trace data. */
1447 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1448 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1449 traceframe_read_count
= traceframe_write_count
= 0;
1450 traceframes_created
= 0;
1453 #ifndef IN_PROCESS_AGENT
1456 clear_inferior_trace_buffer (void)
1458 CORE_ADDR ipa_trace_buffer_lo
;
1459 CORE_ADDR ipa_trace_buffer_hi
;
1460 struct traceframe ipa_traceframe
= { 0 };
1461 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1463 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1464 &ipa_trace_buffer_lo
);
1465 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1466 &ipa_trace_buffer_hi
);
1468 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1469 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1470 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1471 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1473 /* A traceframe with zeroed fields marks the end of trace data. */
1474 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1475 (unsigned char *) &ipa_trace_buffer_ctrl
,
1476 sizeof (ipa_trace_buffer_ctrl
));
1478 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1480 /* A traceframe with zeroed fields marks the end of trace data. */
1481 write_inferior_memory (ipa_trace_buffer_lo
,
1482 (unsigned char *) &ipa_traceframe
,
1483 sizeof (ipa_traceframe
));
1485 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1486 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1487 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1493 init_trace_buffer (LONGEST bufsize
)
1497 trace_buffer_size
= bufsize
;
1499 /* Make sure to internally allocate at least space for the EOB
1501 alloc_size
= (bufsize
< TRACEFRAME_EOB_MARKER_SIZE
1502 ? TRACEFRAME_EOB_MARKER_SIZE
: bufsize
);
1503 trace_buffer_lo
= xrealloc (trace_buffer_lo
, alloc_size
);
1505 trace_buffer_hi
= trace_buffer_lo
+ trace_buffer_size
;
1507 clear_trace_buffer ();
1510 #ifdef IN_PROCESS_AGENT
1512 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1513 about_to_request_buffer_space (void)
1515 /* GDBserver places breakpoint here while it goes about to flush
1516 data at random times. */
1517 UNKNOWN_SIDE_EFFECTS();
1522 /* Carve out a piece of the trace buffer, returning NULL in case of
1526 trace_buffer_alloc (size_t amt
)
1528 unsigned char *rslt
;
1529 struct trace_buffer_control
*tbctrl
;
1531 #ifdef IN_PROCESS_AGENT
1532 unsigned int prev
, prev_filtered
;
1533 unsigned int commit_count
;
1534 unsigned int commit
;
1535 unsigned int readout
;
1537 struct traceframe
*oldest
;
1538 unsigned char *new_start
;
1541 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1542 (long) amt
, (long) sizeof (struct traceframe
));
1544 /* Account for the EOB marker. */
1545 amt
+= TRACEFRAME_EOB_MARKER_SIZE
;
1547 #ifdef IN_PROCESS_AGENT
1551 /* Read the current token and extract the index to try to write to,
1552 storing it in CURR. */
1553 prev
= trace_buffer_ctrl_curr
;
1554 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1555 curr
= prev_filtered
+ 1;
1559 about_to_request_buffer_space ();
1561 /* Start out with a copy of the current state. GDBserver may be
1562 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1563 be able to commit anyway if that happens. */
1564 trace_buffer_ctrl
[curr
]
1565 = trace_buffer_ctrl
[prev_filtered
];
1566 trace_debug ("trying curr=%u", curr
);
1568 /* The GDBserver's agent doesn't need all that syncing, and always
1569 updates TCB 0 (there's only one, mind you). */
1572 tbctrl
= &trace_buffer_ctrl
[curr
];
1574 /* Offsets are easier to grok for debugging than raw addresses,
1575 especially for the small trace buffer sizes that are useful for
1577 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1579 (int) (tbctrl
->start
- trace_buffer_lo
),
1580 (int) (tbctrl
->free
- trace_buffer_lo
),
1581 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1582 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1583 (int) (trace_buffer_hi
- trace_buffer_lo
));
1585 /* The algorithm here is to keep trying to get a contiguous block of
1586 the requested size, possibly discarding older traceframes to free
1587 up space. Since free space might come in one or two pieces,
1588 depending on whether discarded traceframes wrapped around at the
1589 high end of the buffer, we test both pieces after each
1593 /* First, if we have two free parts, try the upper one first. */
1594 if (tbctrl
->end_free
< tbctrl
->free
)
1596 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1597 /* We have enough in the upper part. */
1601 /* Our high part of free space wasn't enough. Give up
1602 on it for now, set wraparound. We will recover the
1603 space later, if/when the wrapped-around traceframe is
1605 trace_debug ("Upper part too small, setting wraparound");
1606 tbctrl
->wrap
= tbctrl
->free
;
1607 tbctrl
->free
= trace_buffer_lo
;
1611 /* The normal case. */
1612 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1615 #ifdef IN_PROCESS_AGENT
1616 /* The IP Agent's buffer is always circular. It isn't used
1617 currently, but `circular_trace_buffer' could represent
1618 GDBserver's mode. If we didn't find space, ask GDBserver to
1621 flush_trace_buffer ();
1625 trace_debug ("gdbserver flushed buffer, retrying");
1629 /* GDBserver cancelled the tracing. Bail out as well. */
1632 /* If we're here, then neither part is big enough, and
1633 non-circular trace buffers are now full. */
1634 if (!circular_trace_buffer
)
1636 trace_debug ("Not enough space in the trace buffer");
1640 trace_debug ("Need more space in the trace buffer");
1642 /* If we have a circular buffer, we can try discarding the
1643 oldest traceframe and see if that helps. */
1644 oldest
= FIRST_TRACEFRAME ();
1645 if (oldest
->tpnum
== 0)
1647 /* Not good; we have no traceframes to free. Perhaps we're
1648 asking for a block that is larger than the buffer? In
1649 any case, give up. */
1650 trace_debug ("No traceframes to discard");
1654 /* We don't run this code in the in-process agent currently.
1655 E.g., we could leave the in-process agent in autonomous
1656 circular mode if we only have fast tracepoints. If we do
1657 that, then this bit becomes racy with GDBserver, which also
1658 writes to this counter. */
1659 --traceframe_write_count
;
1661 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1662 /* If we freed the traceframe that wrapped around, go back
1663 to the non-wrap case. */
1664 if (new_start
< tbctrl
->start
)
1666 trace_debug ("Discarding past the wraparound");
1667 tbctrl
->wrap
= trace_buffer_hi
;
1669 tbctrl
->start
= new_start
;
1670 tbctrl
->end_free
= tbctrl
->start
;
1672 trace_debug ("Discarded a traceframe\n"
1673 "Trace buffer [%d], start=%d free=%d "
1674 "endfree=%d wrap=%d hi=%d",
1676 (int) (tbctrl
->start
- trace_buffer_lo
),
1677 (int) (tbctrl
->free
- trace_buffer_lo
),
1678 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1679 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1680 (int) (trace_buffer_hi
- trace_buffer_lo
));
1682 /* Now go back around the loop. The discard might have resulted
1683 in either one or two pieces of free space, so we want to try
1684 both before freeing any more traceframes. */
1688 /* If we get here, we know we can provide the asked-for space. */
1690 rslt
= tbctrl
->free
;
1692 /* Adjust the request back down, now that we know we have space for
1693 the marker, but don't commit to AMT yet, we may still need to
1694 restart the operation if GDBserver touches the trace buffer
1695 (obviously only important in the in-process agent's version). */
1696 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1698 /* Or not. If GDBserver changed the trace buffer behind our back,
1699 we get to restart a new allocation attempt. */
1701 #ifdef IN_PROCESS_AGENT
1702 /* Build the tentative token. */
1703 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1704 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1705 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1709 /* Try to commit it. */
1710 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1711 if (readout
!= prev
)
1713 trace_debug ("GDBserver has touched the trace buffer, restarting."
1714 " (prev=%08x, commit=%08x, readout=%08x)",
1715 prev
, commit
, readout
);
1719 /* Hold your horses here. Even if that change was committed,
1720 GDBserver could come in, and clobber it. We need to hold to be
1721 able to tell if GDBserver clobbers before or after we committed
1722 the change. Whenever GDBserver goes about touching the IPA
1723 buffer, it sets a breakpoint in this routine, so we have a sync
1725 about_to_request_buffer_space ();
1727 /* Check if the change has been effective, even if GDBserver stopped
1728 us at the breakpoint. */
1731 unsigned int refetch
;
1735 refetch
= trace_buffer_ctrl_curr
;
1737 if (refetch
== commit
1738 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1741 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1742 "readout=%08x, refetch=%08x)",
1743 prev
, commit
, readout
, refetch
);
1747 trace_debug ("GDBserver has touched the trace buffer, not effective."
1748 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1749 prev
, commit
, readout
, refetch
);
1755 /* We have a new piece of the trace buffer. Hurray! */
1757 /* Add an EOB marker just past this allocation. */
1758 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1759 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1761 /* Adjust the request back down, now that we know we have space for
1763 amt
-= sizeof (struct traceframe
);
1767 trace_debug ("Allocated %d bytes", (int) amt
);
1768 trace_debug ("Trace buffer [%d] start=%d free=%d "
1769 "endfree=%d wrap=%d hi=%d",
1771 (int) (tbctrl
->start
- trace_buffer_lo
),
1772 (int) (tbctrl
->free
- trace_buffer_lo
),
1773 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1774 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1775 (int) (trace_buffer_hi
- trace_buffer_lo
));
1781 #ifndef IN_PROCESS_AGENT
1783 /* Return the total free space. This is not necessarily the largest
1784 block we can allocate, because of the two-part case. */
1789 if (trace_buffer_free
<= trace_buffer_end_free
)
1790 return trace_buffer_end_free
- trace_buffer_free
;
1792 return ((trace_buffer_end_free
- trace_buffer_lo
)
1793 + (trace_buffer_hi
- trace_buffer_free
));
1796 /* An 'S' in continuation packets indicates remainder are for
1799 static int seen_step_action_flag
;
1801 /* Create a tracepoint (location) with given number and address. Add this
1802 new tracepoint to list and sort this list. */
1804 static struct tracepoint
*
1805 add_tracepoint (int num
, CORE_ADDR addr
)
1807 struct tracepoint
*tpoint
, **tp_next
;
1809 tpoint
= xmalloc (sizeof (struct tracepoint
));
1810 tpoint
->number
= num
;
1811 tpoint
->address
= addr
;
1812 tpoint
->numactions
= 0;
1813 tpoint
->actions
= NULL
;
1814 tpoint
->actions_str
= NULL
;
1815 tpoint
->cond
= NULL
;
1816 tpoint
->num_step_actions
= 0;
1817 tpoint
->step_actions
= NULL
;
1818 tpoint
->step_actions_str
= NULL
;
1819 /* Start all off as regular (slow) tracepoints. */
1820 tpoint
->type
= trap_tracepoint
;
1821 tpoint
->orig_size
= -1;
1822 tpoint
->source_strings
= NULL
;
1823 tpoint
->compiled_cond
= 0;
1824 tpoint
->handle
= NULL
;
1825 tpoint
->next
= NULL
;
1827 /* Find a place to insert this tracepoint into list in order to keep
1828 the tracepoint list still in the ascending order. There may be
1829 multiple tracepoints at the same address as TPOINT's, and this
1830 guarantees TPOINT is inserted after all the tracepoints which are
1831 set at the same address. For example, fast tracepoints A, B, C are
1832 set at the same address, and D is to be insert at the same place as
1835 -->| A |--> | B |-->| C |->...
1837 One jump pad was created for tracepoint A, B, and C, and the target
1838 address of A is referenced/used in jump pad. So jump pad will let
1839 inferior jump to A. If D is inserted in front of A, like this,
1841 -->| D |-->| A |--> | B |-->| C |->...
1843 without updating jump pad, D is not reachable during collect, which
1844 is wrong. As we can see, the order of B, C and D doesn't matter, but
1845 A should always be the `first' one. */
1846 for (tp_next
= &tracepoints
;
1847 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1848 tp_next
= &(*tp_next
)->next
)
1850 tpoint
->next
= *tp_next
;
1852 last_tracepoint
= tpoint
;
1854 seen_step_action_flag
= 0;
1859 #ifndef IN_PROCESS_AGENT
1861 /* Return the tracepoint with the given number and address, or NULL. */
1863 static struct tracepoint
*
1864 find_tracepoint (int id
, CORE_ADDR addr
)
1866 struct tracepoint
*tpoint
;
1868 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1869 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1875 /* Remove TPOINT from global list. */
1878 remove_tracepoint (struct tracepoint
*tpoint
)
1880 struct tracepoint
*tp
, *tp_prev
;
1882 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1883 tp_prev
= tp
, tp
= tp
->next
)
1889 tp_prev
->next
= tp
->next
;
1891 tracepoints
= tp
->next
;
1897 /* There may be several tracepoints with the same number (because they
1898 are "locations", in GDB parlance); return the next one after the
1899 given tracepoint, or search from the beginning of the list if the
1900 first argument is NULL. */
1902 static struct tracepoint
*
1903 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1905 struct tracepoint
*tpoint
;
1908 tpoint
= prev_tp
->next
;
1910 tpoint
= tracepoints
;
1911 for (; tpoint
; tpoint
= tpoint
->next
)
1912 if (tpoint
->number
== num
)
1920 /* Append another action to perform when the tracepoint triggers. */
1923 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1929 seen_step_action_flag
= 1;
1937 char *act_start
= act
;
1938 struct tracepoint_action
*action
= NULL
;
1944 struct collect_memory_action
*maction
;
1948 maction
= xmalloc (sizeof *maction
);
1949 maction
->base
.type
= *act
;
1950 maction
->base
.ops
= &m_tracepoint_action_ops
;
1951 action
= &maction
->base
;
1954 is_neg
= (*act
== '-');
1957 act
= unpack_varlen_hex (act
, &basereg
);
1959 act
= unpack_varlen_hex (act
, &maction
->addr
);
1961 act
= unpack_varlen_hex (act
, &maction
->len
);
1962 maction
->basereg
= (is_neg
1965 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1966 pulongest (maction
->len
),
1967 paddress (maction
->addr
), maction
->basereg
);
1972 struct collect_registers_action
*raction
;
1974 raction
= xmalloc (sizeof *raction
);
1975 raction
->base
.type
= *act
;
1976 raction
->base
.ops
= &r_tracepoint_action_ops
;
1977 action
= &raction
->base
;
1979 trace_debug ("Want to collect registers");
1981 /* skip past hex digits of mask for now */
1982 while (isxdigit(*act
))
1988 struct collect_static_trace_data_action
*raction
;
1990 raction
= xmalloc (sizeof *raction
);
1991 raction
->base
.type
= *act
;
1992 raction
->base
.ops
= &l_tracepoint_action_ops
;
1993 action
= &raction
->base
;
1995 trace_debug ("Want to collect static trace data");
2000 trace_debug ("Unexpected step action, ignoring");
2005 struct eval_expr_action
*xaction
;
2007 xaction
= xmalloc (sizeof (*xaction
));
2008 xaction
->base
.type
= *act
;
2009 xaction
->base
.ops
= &x_tracepoint_action_ops
;
2010 action
= &xaction
->base
;
2012 trace_debug ("Want to evaluate expression");
2013 xaction
->expr
= gdb_parse_agent_expr (&act
);
2017 trace_debug ("unknown trace action '%c', ignoring...", *act
);
2026 if (seen_step_action_flag
)
2028 tpoint
->num_step_actions
++;
2030 tpoint
->step_actions
2031 = xrealloc (tpoint
->step_actions
,
2032 (sizeof (*tpoint
->step_actions
)
2033 * tpoint
->num_step_actions
));
2034 tpoint
->step_actions_str
2035 = xrealloc (tpoint
->step_actions_str
,
2036 (sizeof (*tpoint
->step_actions_str
)
2037 * tpoint
->num_step_actions
));
2038 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
2039 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
2040 = savestring (act_start
, act
- act_start
);
2044 tpoint
->numactions
++;
2046 = xrealloc (tpoint
->actions
,
2047 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
2049 = xrealloc (tpoint
->actions_str
,
2050 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
2051 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
2052 tpoint
->actions_str
[tpoint
->numactions
- 1]
2053 = savestring (act_start
, act
- act_start
);
2060 /* Find or create a trace state variable with the given number. */
2062 static struct trace_state_variable
*
2063 get_trace_state_variable (int num
)
2065 struct trace_state_variable
*tsv
;
2067 #ifdef IN_PROCESS_AGENT
2068 /* Search for an existing variable. */
2069 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
2070 if (tsv
->number
== num
)
2074 /* Search for an existing variable. */
2075 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
2076 if (tsv
->number
== num
)
2082 /* Find or create a trace state variable with the given number. */
2084 static struct trace_state_variable
*
2085 create_trace_state_variable (int num
, int gdb
)
2087 struct trace_state_variable
*tsv
;
2089 tsv
= get_trace_state_variable (num
);
2093 /* Create a new variable. */
2094 tsv
= xmalloc (sizeof (struct trace_state_variable
));
2096 tsv
->initial_value
= 0;
2100 #ifdef IN_PROCESS_AGENT
2103 tsv
->next
= alloced_trace_state_variables
;
2104 alloced_trace_state_variables
= tsv
;
2109 tsv
->next
= trace_state_variables
;
2110 trace_state_variables
= tsv
;
2115 IP_AGENT_EXPORT LONGEST
2116 get_trace_state_variable_value (int num
)
2118 struct trace_state_variable
*tsv
;
2120 tsv
= get_trace_state_variable (num
);
2124 trace_debug ("No trace state variable %d, skipping value get", num
);
2128 /* Call a getter function if we have one. While it's tempting to
2129 set up something to only call the getter once per tracepoint hit,
2130 it could run afoul of thread races. Better to let the getter
2131 handle it directly, if necessary to worry about it. */
2133 tsv
->value
= (tsv
->getter
) ();
2135 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2136 num
, plongest (tsv
->value
));
2141 IP_AGENT_EXPORT
void
2142 set_trace_state_variable_value (int num
, LONGEST val
)
2144 struct trace_state_variable
*tsv
;
2146 tsv
= get_trace_state_variable (num
);
2150 trace_debug ("No trace state variable %d, skipping value set", num
);
2158 agent_get_trace_state_variable_value (int num
)
2160 return get_trace_state_variable_value (num
);
2164 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2166 set_trace_state_variable_value (num
, val
);
2170 set_trace_state_variable_name (int num
, const char *name
)
2172 struct trace_state_variable
*tsv
;
2174 tsv
= get_trace_state_variable (num
);
2178 trace_debug ("No trace state variable %d, skipping name set", num
);
2182 tsv
->name
= (char *) name
;
2186 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2188 struct trace_state_variable
*tsv
;
2190 tsv
= get_trace_state_variable (num
);
2194 trace_debug ("No trace state variable %d, skipping getter set", num
);
2198 tsv
->getter
= getter
;
2201 /* Add a raw traceframe for the given tracepoint. */
2203 static struct traceframe
*
2204 add_traceframe (struct tracepoint
*tpoint
)
2206 struct traceframe
*tframe
;
2208 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2213 tframe
->tpnum
= tpoint
->number
;
2214 tframe
->data_size
= 0;
2219 /* Add a block to the traceframe currently being worked on. */
2221 static unsigned char *
2222 add_traceframe_block (struct traceframe
*tframe
,
2223 struct tracepoint
*tpoint
, int amt
)
2225 unsigned char *block
;
2230 block
= trace_buffer_alloc (amt
);
2235 gdb_assert (tframe
->tpnum
== tpoint
->number
);
2237 tframe
->data_size
+= amt
;
2238 tpoint
->traceframe_usage
+= amt
;
2243 /* Flag that the current traceframe is finished. */
2246 finish_traceframe (struct traceframe
*tframe
)
2248 ++traceframe_write_count
;
2249 ++traceframes_created
;
2252 #ifndef IN_PROCESS_AGENT
2254 /* Given a traceframe number NUM, find the NUMth traceframe in the
2257 static struct traceframe
*
2258 find_traceframe (int num
)
2260 struct traceframe
*tframe
;
2263 for (tframe
= FIRST_TRACEFRAME ();
2265 tframe
= NEXT_TRACEFRAME (tframe
))
2276 get_traceframe_address (struct traceframe
*tframe
)
2279 struct tracepoint
*tpoint
;
2281 addr
= traceframe_get_pc (tframe
);
2286 /* Fallback strategy, will be incorrect for while-stepping frames
2287 and multi-location tracepoints. */
2288 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2289 return tpoint
->address
;
2292 /* Search for the next traceframe whose address is inside or outside
2295 static struct traceframe
*
2296 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2299 struct traceframe
*tframe
;
2302 *tfnump
= current_traceframe
+ 1;
2303 tframe
= find_traceframe (*tfnump
);
2304 /* The search is not supposed to wrap around. */
2311 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2313 tfaddr
= get_traceframe_address (tframe
);
2315 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2316 : (lo
> tfaddr
|| tfaddr
> hi
))
2325 /* Search for the next traceframe recorded by the given tracepoint.
2326 Note that for multi-location tracepoints, this will find whatever
2327 location appears first. */
2329 static struct traceframe
*
2330 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2332 struct traceframe
*tframe
;
2334 *tfnump
= current_traceframe
+ 1;
2335 tframe
= find_traceframe (*tfnump
);
2336 /* The search is not supposed to wrap around. */
2343 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2345 if (tframe
->tpnum
== num
)
2356 #ifndef IN_PROCESS_AGENT
2358 /* Clear all past trace state. */
2361 cmd_qtinit (char *packet
)
2363 struct trace_state_variable
*tsv
, *prev
, *next
;
2365 /* Make sure we don't try to read from a trace frame. */
2366 current_traceframe
= -1;
2370 trace_debug ("Initializing the trace");
2372 clear_installed_tracepoints ();
2373 clear_readonly_regions ();
2376 last_tracepoint
= NULL
;
2378 /* Clear out any leftover trace state variables. Ones with target
2379 defined getters should be kept however. */
2381 tsv
= trace_state_variables
;
2384 trace_debug ("Looking at var %d", tsv
->number
);
2385 if (tsv
->getter
== NULL
)
2391 trace_state_variables
= next
;
2392 trace_debug ("Deleting var %d", tsv
->number
);
2403 clear_trace_buffer ();
2404 clear_inferior_trace_buffer ();
2409 /* Unprobe the UST marker at ADDRESS. */
2412 unprobe_marker_at (CORE_ADDR address
)
2414 char cmd
[IPA_CMD_BUF_SIZE
];
2416 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2417 run_inferior_command (cmd
, strlen (cmd
) + 1);
2420 /* Restore the program to its pre-tracing state. This routine may be called
2421 in error situations, so it needs to be careful about only restoring
2422 from known-valid bits. */
2425 clear_installed_tracepoints (void)
2427 struct tracepoint
*tpoint
;
2428 struct tracepoint
*prev_stpoint
;
2431 cancel_breakpoints ();
2433 prev_stpoint
= NULL
;
2435 /* Restore any bytes overwritten by tracepoints. */
2436 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2438 /* Catch the case where we might try to remove a tracepoint that
2439 was never actually installed. */
2440 if (tpoint
->handle
== NULL
)
2442 trace_debug ("Tracepoint %d at 0x%s was "
2443 "never installed, nothing to clear",
2444 tpoint
->number
, paddress (tpoint
->address
));
2448 switch (tpoint
->type
)
2450 case trap_tracepoint
:
2451 delete_breakpoint (tpoint
->handle
);
2453 case fast_tracepoint
:
2454 delete_fast_tracepoint_jump (tpoint
->handle
);
2456 case static_tracepoint
:
2457 if (prev_stpoint
!= NULL
2458 && prev_stpoint
->address
== tpoint
->address
)
2459 /* Nothing to do. We already unprobed a tracepoint set at
2460 this marker address (and there can only be one probe
2465 unprobe_marker_at (tpoint
->address
);
2466 prev_stpoint
= tpoint
;
2471 tpoint
->handle
= NULL
;
2477 /* Parse a packet that defines a tracepoint. */
2480 cmd_qtdp (char *own_buf
)
2483 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2484 int trail_hyphen
= 0;
2488 struct tracepoint
*tpoint
;
2490 char *packet
= own_buf
;
2492 packet
+= strlen ("QTDP:");
2494 /* A hyphen at the beginning marks a packet specifying actions for a
2495 tracepoint already supplied. */
2502 packet
= unpack_varlen_hex (packet
, &num
);
2503 ++packet
; /* skip a colon */
2504 packet
= unpack_varlen_hex (packet
, &addr
);
2505 ++packet
; /* skip a colon */
2507 /* See if we already have this tracepoint. */
2508 tpoint
= find_tracepoint (num
, addr
);
2512 /* Duplicate tracepoints are never allowed. */
2515 trace_debug ("Tracepoint error: tracepoint %d"
2516 " at 0x%s already exists",
2517 (int) num
, paddress (addr
));
2518 write_enn (own_buf
);
2522 tpoint
= add_tracepoint (num
, addr
);
2524 tpoint
->enabled
= (*packet
== 'E');
2525 ++packet
; /* skip 'E' */
2526 ++packet
; /* skip a colon */
2527 packet
= unpack_varlen_hex (packet
, &count
);
2528 tpoint
->step_count
= count
;
2529 ++packet
; /* skip a colon */
2530 packet
= unpack_varlen_hex (packet
, &count
);
2531 tpoint
->pass_count
= count
;
2532 /* See if we have any of the additional optional fields. */
2533 while (*packet
== ':')
2538 tpoint
->type
= fast_tracepoint
;
2540 packet
= unpack_varlen_hex (packet
, &count
);
2541 tpoint
->orig_size
= count
;
2543 else if (*packet
== 'S')
2545 tpoint
->type
= static_tracepoint
;
2548 else if (*packet
== 'X')
2550 actparm
= (char *) packet
;
2551 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2554 else if (*packet
== '-')
2556 else if (*packet
== '\0')
2559 trace_debug ("Unknown optional tracepoint field");
2564 trace_debug ("Also has actions\n");
2567 trace_debug ("Defined %stracepoint %d at 0x%s, "
2568 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2569 tpoint
->type
== fast_tracepoint
? "fast "
2570 : tpoint
->type
== static_tracepoint
? "static " : "",
2571 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2572 tpoint
->step_count
, tpoint
->pass_count
);
2575 add_tracepoint_action (tpoint
, packet
);
2578 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2579 (int) num
, paddress (addr
));
2580 write_enn (own_buf
);
2584 /* Install tracepoint during tracing only once for each tracepoint location.
2585 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2586 determine the last QTDP packet for one tracepoint location by checking
2587 trailing hyphen in QTDP packet. */
2588 if (tracing
&& !trail_hyphen
)
2590 struct tracepoint
*tp
= NULL
;
2592 /* Pause all threads temporarily while we patch tracepoints. */
2595 /* download_tracepoint will update global `tracepoints'
2596 list, so it is unsafe to leave threads in jump pad. */
2597 stabilize_threads ();
2599 /* Freeze threads. */
2603 if (tpoint
->type
!= trap_tracepoint
)
2605 /* Find another fast or static tracepoint at the same address. */
2606 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2608 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2609 && tp
->number
!= tpoint
->number
)
2613 /* TPOINT is installed at the same address as TP. */
2616 if (tpoint
->type
== fast_tracepoint
)
2617 clone_fast_tracepoint (tpoint
, tp
);
2618 else if (tpoint
->type
== static_tracepoint
)
2619 tpoint
->handle
= (void *) -1;
2623 if (use_agent
&& tpoint
->type
== fast_tracepoint
2624 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
2626 /* Download and install fast tracepoint by agent. */
2627 if (tracepoint_send_agent (tpoint
) == 0)
2631 write_enn (own_buf
);
2632 remove_tracepoint (tpoint
);
2637 download_tracepoint (tpoint
);
2639 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2641 install_tracepoint (tpoint
, own_buf
);
2642 if (strcmp (own_buf
, "OK") != 0)
2643 remove_tracepoint (tpoint
);
2657 cmd_qtdpsrc (char *own_buf
)
2659 ULONGEST num
, addr
, start
, slen
;
2660 struct tracepoint
*tpoint
;
2661 char *packet
= own_buf
;
2662 char *saved
, *srctype
, *src
;
2664 struct source_string
*last
, *newlast
;
2666 packet
+= strlen ("QTDPsrc:");
2668 packet
= unpack_varlen_hex (packet
, &num
);
2669 ++packet
; /* skip a colon */
2670 packet
= unpack_varlen_hex (packet
, &addr
);
2671 ++packet
; /* skip a colon */
2673 /* See if we already have this tracepoint. */
2674 tpoint
= find_tracepoint (num
, addr
);
2678 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2679 (int) num
, paddress (addr
));
2680 write_enn (own_buf
);
2685 packet
= strchr (packet
, ':');
2686 srctype
= xmalloc (packet
- saved
+ 1);
2687 memcpy (srctype
, saved
, packet
- saved
);
2688 srctype
[packet
- saved
] = '\0';
2690 packet
= unpack_varlen_hex (packet
, &start
);
2691 ++packet
; /* skip a colon */
2692 packet
= unpack_varlen_hex (packet
, &slen
);
2693 ++packet
; /* skip a colon */
2694 src
= xmalloc (slen
+ 1);
2695 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2698 newlast
= xmalloc (sizeof (struct source_string
));
2699 newlast
->type
= srctype
;
2701 newlast
->next
= NULL
;
2702 /* Always add a source string to the end of the list;
2703 this keeps sequences of actions/commands in the right
2705 if (tpoint
->source_strings
)
2707 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2709 last
->next
= newlast
;
2712 tpoint
->source_strings
= newlast
;
2718 cmd_qtdv (char *own_buf
)
2720 ULONGEST num
, val
, builtin
;
2723 struct trace_state_variable
*tsv
;
2724 char *packet
= own_buf
;
2726 packet
+= strlen ("QTDV:");
2728 packet
= unpack_varlen_hex (packet
, &num
);
2729 ++packet
; /* skip a colon */
2730 packet
= unpack_varlen_hex (packet
, &val
);
2731 ++packet
; /* skip a colon */
2732 packet
= unpack_varlen_hex (packet
, &builtin
);
2733 ++packet
; /* skip a colon */
2735 nbytes
= strlen (packet
) / 2;
2736 varname
= xmalloc (nbytes
+ 1);
2737 nbytes
= unhexify (varname
, packet
, nbytes
);
2738 varname
[nbytes
] = '\0';
2740 tsv
= create_trace_state_variable (num
, 1);
2741 tsv
->initial_value
= (LONGEST
) val
;
2742 tsv
->name
= varname
;
2744 set_trace_state_variable_value (num
, (LONGEST
) val
);
2750 cmd_qtenable_disable (char *own_buf
, int enable
)
2752 char *packet
= own_buf
;
2754 struct tracepoint
*tp
;
2756 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2757 packet
= unpack_varlen_hex (packet
, &num
);
2758 ++packet
; /* skip a colon */
2759 packet
= unpack_varlen_hex (packet
, &addr
);
2761 tp
= find_tracepoint (num
, addr
);
2765 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2767 trace_debug ("Tracepoint %d at 0x%s is already %s",
2768 (int) num
, paddress (addr
),
2769 enable
? "enabled" : "disabled");
2774 trace_debug ("%s tracepoint %d at 0x%s",
2775 enable
? "Enabling" : "Disabling",
2776 (int) num
, paddress (addr
));
2778 tp
->enabled
= enable
;
2780 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2783 int offset
= offsetof (struct tracepoint
, enabled
);
2784 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2786 ret
= prepare_to_access_memory ();
2789 trace_debug ("Failed to temporarily stop inferior threads");
2790 write_enn (own_buf
);
2794 ret
= write_inferior_integer (obj_addr
, enable
);
2795 done_accessing_memory ();
2799 trace_debug ("Cannot write enabled flag into "
2800 "inferior process memory");
2801 write_enn (own_buf
);
2810 trace_debug ("Tracepoint %d at 0x%s not found",
2811 (int) num
, paddress (addr
));
2812 write_enn (own_buf
);
2817 cmd_qtv (char *own_buf
)
2822 char *packet
= own_buf
;
2824 packet
+= strlen ("qTV:");
2825 unpack_varlen_hex (packet
, &num
);
2827 if (current_traceframe
>= 0)
2829 err
= traceframe_read_tsv ((int) num
, &val
);
2832 strcpy (own_buf
, "U");
2836 /* Only make tsv's be undefined before the first trace run. After a
2837 trace run is over, the user might want to see the last value of
2838 the tsv, and it might not be available in a traceframe. */
2839 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2841 strcpy (own_buf
, "U");
2845 val
= get_trace_state_variable_value (num
);
2847 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2850 /* Clear out the list of readonly regions. */
2853 clear_readonly_regions (void)
2855 struct readonly_region
*roreg
;
2857 while (readonly_regions
)
2859 roreg
= readonly_regions
;
2860 readonly_regions
= readonly_regions
->next
;
2865 /* Parse the collection of address ranges whose contents GDB believes
2866 to be unchanging and so can be read directly from target memory
2867 even while looking at a traceframe. */
2870 cmd_qtro (char *own_buf
)
2872 ULONGEST start
, end
;
2873 struct readonly_region
*roreg
;
2874 char *packet
= own_buf
;
2876 trace_debug ("Want to mark readonly regions");
2878 clear_readonly_regions ();
2880 packet
+= strlen ("QTro");
2882 while (*packet
== ':')
2884 ++packet
; /* skip a colon */
2885 packet
= unpack_varlen_hex (packet
, &start
);
2886 ++packet
; /* skip a comma */
2887 packet
= unpack_varlen_hex (packet
, &end
);
2888 roreg
= xmalloc (sizeof (struct readonly_region
));
2889 roreg
->start
= start
;
2891 roreg
->next
= readonly_regions
;
2892 readonly_regions
= roreg
;
2893 trace_debug ("Added readonly region from 0x%s to 0x%s",
2894 paddress (roreg
->start
), paddress (roreg
->end
));
2900 /* Test to see if the given range is in our list of readonly ranges.
2901 We only test for being entirely within a range, GDB is not going to
2902 send a single memory packet that spans multiple regions. */
2905 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2907 struct readonly_region
*roreg
;
2909 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2910 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2916 /* The maximum size of a jump pad entry. */
2917 static const int max_jump_pad_size
= 0x100;
2919 static CORE_ADDR gdb_jump_pad_head
;
2921 /* Return the address of the next free jump space. */
2924 get_jump_space_head (void)
2926 if (gdb_jump_pad_head
== 0)
2928 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2929 &gdb_jump_pad_head
))
2930 fatal ("error extracting jump_pad_buffer");
2933 return gdb_jump_pad_head
;
2936 /* Reserve USED bytes from the jump space. */
2939 claim_jump_space (ULONGEST used
)
2941 trace_debug ("claim_jump_space reserves %s bytes at %s",
2942 pulongest (used
), paddress (gdb_jump_pad_head
));
2943 gdb_jump_pad_head
+= used
;
2946 static CORE_ADDR trampoline_buffer_head
= 0;
2947 static CORE_ADDR trampoline_buffer_tail
;
2949 /* Reserve USED bytes from the trampoline buffer and return the
2950 address of the start of the reserved space in TRAMPOLINE. Returns
2951 non-zero if the space is successfully claimed. */
2954 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2956 if (!trampoline_buffer_head
)
2958 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2959 &trampoline_buffer_tail
))
2961 fatal ("error extracting trampoline_buffer");
2965 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2966 &trampoline_buffer_head
))
2968 fatal ("error extracting trampoline_buffer_end");
2973 /* Start claiming space from the top of the trampoline space. If
2974 the space is located at the bottom of the virtual address space,
2975 this reduces the possibility that corruption will occur if a null
2976 pointer is used to write to memory. */
2977 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2979 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2984 trampoline_buffer_head
-= used
;
2986 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2987 pulongest (used
), paddress (trampoline_buffer_head
));
2989 *trampoline
= trampoline_buffer_head
;
2993 /* Returns non-zero if there is space allocated for use in trampolines
2994 for fast tracepoints. */
2997 have_fast_tracepoint_trampoline_buffer (char *buf
)
2999 CORE_ADDR trampoline_end
, errbuf
;
3001 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
3004 fatal ("error extracting trampoline_buffer_end");
3011 strcpy (buf
, "was claiming");
3012 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
3015 fatal ("error extracting errbuf");
3019 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
3022 return trampoline_end
!= 0;
3025 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3026 the command fails, or 0 otherwise. If the command ran
3027 successfully, but probing the marker failed, ERROUT will be filled
3028 with the error to reply to GDB, and -1 is also returned. This
3029 allows directly passing IPA errors to GDB. */
3032 probe_marker_at (CORE_ADDR address
, char *errout
)
3034 char cmd
[IPA_CMD_BUF_SIZE
];
3037 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
3038 err
= run_inferior_command (cmd
, strlen (cmd
) + 1);
3044 strcpy (errout
, cmd
);
3053 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
3055 to
->jump_pad
= from
->jump_pad
;
3056 to
->jump_pad_end
= from
->jump_pad_end
;
3057 to
->trampoline
= from
->trampoline
;
3058 to
->trampoline_end
= from
->trampoline_end
;
3059 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
3060 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
3061 to
->handle
= from
->handle
;
3063 gdb_assert (from
->handle
);
3064 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
3067 #define MAX_JUMP_SIZE 20
3069 /* Install fast tracepoint. Return 0 if successful, otherwise return
3073 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
3075 CORE_ADDR jentry
, jump_entry
;
3076 CORE_ADDR trampoline
;
3077 ULONGEST trampoline_size
;
3079 /* The jump to the jump pad of the last fast tracepoint
3081 unsigned char fjump
[MAX_JUMP_SIZE
];
3082 ULONGEST fjump_size
;
3084 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
3086 trace_debug ("Requested a fast tracepoint on an instruction "
3087 "that is of less than the minimum length.");
3091 jentry
= jump_entry
= get_jump_space_head ();
3094 trampoline_size
= 0;
3096 /* Install the jump pad. */
3097 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3099 ipa_sym_addrs
.addr_gdb_collect
,
3100 ipa_sym_addrs
.addr_collecting
,
3103 &trampoline
, &trampoline_size
,
3105 &tpoint
->adjusted_insn_addr
,
3106 &tpoint
->adjusted_insn_addr_end
,
3113 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3116 if (tpoint
->handle
!= NULL
)
3118 tpoint
->jump_pad
= jump_entry
;
3119 tpoint
->jump_pad_end
= jentry
;
3120 tpoint
->trampoline
= trampoline
;
3121 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3123 /* Pad to 8-byte alignment. */
3124 jentry
= ((jentry
+ 7) & ~0x7);
3125 claim_jump_space (jentry
- jump_entry
);
3132 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3135 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3137 tpoint
->handle
= NULL
;
3140 if (tpoint
->type
== trap_tracepoint
)
3142 /* Tracepoints are installed as memory breakpoints. Just go
3143 ahead and install the trap. The breakpoints module
3144 handles duplicated breakpoints, and the memory read
3145 routine handles un-patching traps from memory reads. */
3146 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3147 tracepoint_handler
);
3149 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3151 if (!agent_loaded_p ())
3153 trace_debug ("Requested a %s tracepoint, but fast "
3154 "tracepoints aren't supported.",
3155 tpoint
->type
== static_tracepoint
? "static" : "fast");
3156 write_e_ipa_not_loaded (own_buf
);
3159 if (tpoint
->type
== static_tracepoint
3160 && !in_process_agent_supports_ust ())
3162 trace_debug ("Requested a static tracepoint, but static "
3163 "tracepoints are not supported.");
3164 write_e_ust_not_loaded (own_buf
);
3168 if (tpoint
->type
== fast_tracepoint
)
3169 install_fast_tracepoint (tpoint
, own_buf
);
3172 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3173 tpoint
->handle
= (void *) -1;
3178 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3180 if (tpoint
->handle
== NULL
)
3182 if (*own_buf
== '\0')
3183 write_enn (own_buf
);
3189 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3192 cmd_qtstart (char *packet
)
3194 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3195 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3197 trace_debug ("Starting the trace");
3199 /* Pause all threads temporarily while we patch tracepoints. */
3202 /* Get threads out of jump pads. Safe to do here, since this is a
3203 top level command. And, required to do here, since we're
3204 deleting/rewriting jump pads. */
3206 stabilize_threads ();
3208 /* Freeze threads. */
3211 /* Sync the fast tracepoints list in the inferior ftlib. */
3212 if (agent_loaded_p ())
3213 download_trace_state_variables ();
3215 /* No previous fast tpoint yet. */
3216 prev_ftpoint
= NULL
;
3218 /* No previous static tpoint yet. */
3219 prev_stpoint
= NULL
;
3223 /* Start out empty. */
3224 if (agent_loaded_p ())
3225 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
3227 /* Download and install tracepoints. */
3228 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3230 /* Ensure all the hit counts start at zero. */
3231 tpoint
->hit_count
= 0;
3232 tpoint
->traceframe_usage
= 0;
3234 if (tpoint
->type
== trap_tracepoint
)
3236 /* Tracepoints are installed as memory breakpoints. Just go
3237 ahead and install the trap. The breakpoints module
3238 handles duplicated breakpoints, and the memory read
3239 routine handles un-patching traps from memory reads. */
3240 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3241 tracepoint_handler
);
3243 else if (tpoint
->type
== fast_tracepoint
3244 || tpoint
->type
== static_tracepoint
)
3246 if (maybe_write_ipa_not_loaded (packet
))
3248 trace_debug ("Requested a %s tracepoint, but fast "
3249 "tracepoints aren't supported.",
3250 tpoint
->type
== static_tracepoint
3251 ? "static" : "fast");
3255 if (tpoint
->type
== fast_tracepoint
)
3258 = use_agent
&& agent_capability_check (AGENT_CAPA_FAST_TRACE
);
3260 if (prev_ftpoint
!= NULL
3261 && prev_ftpoint
->address
== tpoint
->address
)
3264 tracepoint_send_agent (tpoint
);
3266 download_tracepoint_1 (tpoint
);
3268 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3272 /* Tracepoint is installed successfully? */
3275 /* Download and install fast tracepoint by agent. */
3277 installed
= !tracepoint_send_agent (tpoint
);
3280 download_tracepoint_1 (tpoint
);
3281 installed
= !install_fast_tracepoint (tpoint
, packet
);
3285 prev_ftpoint
= tpoint
;
3290 if (!in_process_agent_supports_ust ())
3292 trace_debug ("Requested a static tracepoint, but static "
3293 "tracepoints are not supported.");
3297 download_tracepoint_1 (tpoint
);
3298 /* Can only probe a given marker once. */
3299 if (prev_stpoint
!= NULL
3300 && prev_stpoint
->address
== tpoint
->address
)
3301 tpoint
->handle
= (void *) -1;
3304 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3306 tpoint
->handle
= (void *) -1;
3308 /* So that we can handle multiple static tracepoints
3309 at the same address easily. */
3310 prev_stpoint
= tpoint
;
3316 tpptr
= tpoint
->obj_addr_on_target
;
3318 if (tpoint
== tracepoints
)
3319 /* First object in list, set the head pointer in the
3321 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3323 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
3328 /* Any failure in the inner loop is sufficient cause to give
3330 if (tpoint
->handle
== NULL
)
3334 /* Any error in tracepoint insertion is unacceptable; better to
3335 address the problem now, than end up with a useless or misleading
3339 clear_installed_tracepoints ();
3340 if (*packet
== '\0')
3346 stopping_tracepoint
= NULL
;
3347 trace_buffer_is_full
= 0;
3348 expr_eval_result
= expr_eval_no_error
;
3349 error_tracepoint
= NULL
;
3350 tracing_start_time
= get_timestamp ();
3352 /* Tracing is now active, hits will now start being logged. */
3355 if (agent_loaded_p ())
3357 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3358 fatal ("Error setting tracing variable in lib");
3360 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3362 fatal ("Error clearing stopping_tracepoint variable in lib");
3364 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3365 fatal ("Error clearing trace_buffer_is_full variable in lib");
3367 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3368 stop_tracing_handler
);
3369 if (stop_tracing_bkpt
== NULL
)
3370 error ("Error setting stop_tracing breakpoint");
3372 flush_trace_buffer_bkpt
3373 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3374 flush_trace_buffer_handler
);
3375 if (flush_trace_buffer_bkpt
== NULL
)
3376 error ("Error setting flush_trace_buffer breakpoint");
3384 /* End a tracing run, filling in a stop reason to report back to GDB,
3385 and removing the tracepoints from the code. */
3392 trace_debug ("Tracing is already off, ignoring");
3396 trace_debug ("Stopping the trace");
3398 /* Pause all threads before removing fast jumps from memory,
3399 breakpoints, and touching IPA state variables (inferior memory).
3400 Some thread may hit the internal tracing breakpoints, or be
3401 collecting this moment, but that's ok, we don't release the
3402 tpoint object's memory or the jump pads here (we only do that
3403 when we're sure we can move all threads out of the jump pads).
3404 We can't now, since we may be getting here due to the inferior
3405 agent calling us. */
3407 /* Since we're removing breakpoints, cancel breakpoint hits,
3408 possibly related to the breakpoints we're about to delete. */
3409 cancel_breakpoints ();
3411 /* Stop logging. Tracepoints can still be hit, but they will not be
3414 if (agent_loaded_p ())
3416 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3417 fatal ("Error clearing tracing variable in lib");
3420 tracing_stop_time
= get_timestamp ();
3421 tracing_stop_reason
= "t???";
3422 tracing_stop_tpnum
= 0;
3423 if (stopping_tracepoint
)
3425 trace_debug ("Stopping the trace because "
3426 "tracepoint %d was hit %" PRIu64
" times",
3427 stopping_tracepoint
->number
,
3428 stopping_tracepoint
->pass_count
);
3429 tracing_stop_reason
= "tpasscount";
3430 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3432 else if (trace_buffer_is_full
)
3434 trace_debug ("Stopping the trace because the trace buffer is full");
3435 tracing_stop_reason
= "tfull";
3437 else if (expr_eval_result
!= expr_eval_no_error
)
3439 trace_debug ("Stopping the trace because of an expression eval error");
3440 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3441 tracing_stop_tpnum
= error_tracepoint
->number
;
3443 #ifndef IN_PROCESS_AGENT
3444 else if (!gdb_connected ())
3446 trace_debug ("Stopping the trace because GDB disconnected");
3447 tracing_stop_reason
= "tdisconnected";
3452 trace_debug ("Stopping the trace because of a tstop command");
3453 tracing_stop_reason
= "tstop";
3456 stopping_tracepoint
= NULL
;
3457 error_tracepoint
= NULL
;
3459 /* Clear out the tracepoints. */
3460 clear_installed_tracepoints ();
3462 if (agent_loaded_p ())
3464 /* Pull in fast tracepoint trace frames from the inferior lib
3465 buffer into our buffer, even if our buffer is already full,
3466 because we want to present the full number of created frames
3467 in addition to what fit in the trace buffer. */
3468 upload_fast_traceframes ();
3471 if (stop_tracing_bkpt
!= NULL
)
3473 delete_breakpoint (stop_tracing_bkpt
);
3474 stop_tracing_bkpt
= NULL
;
3477 if (flush_trace_buffer_bkpt
!= NULL
)
3479 delete_breakpoint (flush_trace_buffer_bkpt
);
3480 flush_trace_buffer_bkpt
= NULL
;
3487 stop_tracing_handler (CORE_ADDR addr
)
3489 trace_debug ("lib hit stop_tracing");
3491 /* Don't actually handle it here. When we stop tracing we remove
3492 breakpoints from the inferior, and that is not allowed in a
3493 breakpoint handler (as the caller is walking the breakpoint
3499 flush_trace_buffer_handler (CORE_ADDR addr
)
3501 trace_debug ("lib hit flush_trace_buffer");
3506 cmd_qtstop (char *packet
)
3513 cmd_qtdisconnected (char *own_buf
)
3516 char *packet
= own_buf
;
3518 packet
+= strlen ("QTDisconnected:");
3520 unpack_varlen_hex (packet
, &setting
);
3524 disconnected_tracing
= setting
;
3528 cmd_qtframe (char *own_buf
)
3530 ULONGEST frame
, pc
, lo
, hi
, num
;
3532 struct traceframe
*tframe
;
3533 char *packet
= own_buf
;
3535 packet
+= strlen ("QTFrame:");
3537 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3539 packet
+= strlen ("pc:");
3540 unpack_varlen_hex (packet
, &pc
);
3541 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3542 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3544 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3546 packet
+= strlen ("range:");
3547 packet
= unpack_varlen_hex (packet
, &lo
);
3549 unpack_varlen_hex (packet
, &hi
);
3550 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3551 paddress (lo
), paddress (hi
));
3552 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3554 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3556 packet
+= strlen ("outside:");
3557 packet
= unpack_varlen_hex (packet
, &lo
);
3559 unpack_varlen_hex (packet
, &hi
);
3560 trace_debug ("Want to find next traceframe "
3561 "outside the range 0x%s to 0x%s",
3562 paddress (lo
), paddress (hi
));
3563 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3565 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3567 packet
+= strlen ("tdp:");
3568 unpack_varlen_hex (packet
, &num
);
3570 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3571 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3575 unpack_varlen_hex (packet
, &frame
);
3576 tfnum
= (int) frame
;
3579 trace_debug ("Want to stop looking at traceframes");
3580 current_traceframe
= -1;
3584 trace_debug ("Want to look at traceframe %d", tfnum
);
3585 tframe
= find_traceframe (tfnum
);
3590 current_traceframe
= tfnum
;
3591 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3594 sprintf (own_buf
, "F-1");
3598 cmd_qtstatus (char *packet
)
3600 char *stop_reason_rsp
= NULL
;
3601 char *buf1
, *buf2
, *buf3
, *str
;
3604 /* Translate the plain text of the notes back into hex for
3607 str
= (tracing_user_name
? tracing_user_name
: "");
3608 slen
= strlen (str
);
3609 buf1
= (char *) alloca (slen
* 2 + 1);
3610 hexify (buf1
, str
, slen
);
3612 str
= (tracing_notes
? tracing_notes
: "");
3613 slen
= strlen (str
);
3614 buf2
= (char *) alloca (slen
* 2 + 1);
3615 hexify (buf2
, str
, slen
);
3617 str
= (tracing_stop_note
? tracing_stop_note
: "");
3618 slen
= strlen (str
);
3619 buf3
= (char *) alloca (slen
* 2 + 1);
3620 hexify (buf3
, str
, slen
);
3622 trace_debug ("Returning trace status as %d, stop reason %s",
3623 tracing
, tracing_stop_reason
);
3625 if (agent_loaded_p ())
3629 upload_fast_traceframes ();
3634 stop_reason_rsp
= (char *) tracing_stop_reason
;
3636 /* The user visible error string in terror needs to be hex encoded.
3637 We leave it as plain string in `tracing_stop_reason' to ease
3639 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3641 const char *result_name
;
3645 result_name
= stop_reason_rsp
+ strlen ("terror:");
3646 hexstr_len
= strlen (result_name
) * 2;
3647 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3648 strcpy (p
, "terror:");
3650 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3653 /* If this was a forced stop, include any stop note that was supplied. */
3654 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3656 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3657 strcpy (stop_reason_rsp
, "tstop:");
3658 strcat (stop_reason_rsp
, buf3
);
3664 "tframes:%x;tcreated:%x;"
3665 "tfree:%x;tsize:%s;"
3668 "starttime:%s;stoptime:%s;"
3669 "username:%s;notes:%s:",
3671 stop_reason_rsp
, tracing_stop_tpnum
,
3672 traceframe_count
, traceframes_created
,
3673 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3674 circular_trace_buffer
,
3675 disconnected_tracing
,
3676 phex_nz (tracing_start_time
, sizeof (tracing_start_time
)),
3677 phex_nz (tracing_stop_time
, sizeof (tracing_stop_time
)),
3682 cmd_qtp (char *own_buf
)
3685 struct tracepoint
*tpoint
;
3686 char *packet
= own_buf
;
3688 packet
+= strlen ("qTP:");
3690 packet
= unpack_varlen_hex (packet
, &num
);
3691 ++packet
; /* skip a colon */
3692 packet
= unpack_varlen_hex (packet
, &addr
);
3694 /* See if we already have this tracepoint. */
3695 tpoint
= find_tracepoint (num
, addr
);
3699 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3700 (int) num
, paddress (addr
));
3701 write_enn (own_buf
);
3705 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3706 tpoint
->traceframe_usage
);
3709 /* State variables to help return all the tracepoint bits. */
3710 static struct tracepoint
*cur_tpoint
;
3711 static unsigned int cur_action
;
3712 static unsigned int cur_step_action
;
3713 static struct source_string
*cur_source_string
;
3714 static struct trace_state_variable
*cur_tsv
;
3716 /* Compose a response that is an imitation of the syntax by which the
3717 tracepoint was originally downloaded. */
3720 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3724 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3725 paddress (tpoint
->address
),
3726 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3727 tpoint
->pass_count
);
3728 if (tpoint
->type
== fast_tracepoint
)
3729 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3730 else if (tpoint
->type
== static_tracepoint
)
3731 sprintf (packet
+ strlen (packet
), ":S");
3735 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3736 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3737 tpoint
->cond
->length
, buf
);
3742 /* Compose a response that is an imitation of the syntax by which the
3743 tracepoint action was originally downloaded (with the difference
3744 that due to the way we store the actions, this will output a packet
3745 per action, while GDB could have combined more than one action
3749 response_action (char *packet
, struct tracepoint
*tpoint
,
3750 char *taction
, int step
)
3752 sprintf (packet
, "%c%x:%s:%s",
3753 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3757 /* Compose a response that is an imitation of the syntax by which the
3758 tracepoint source piece was originally downloaded. */
3761 response_source (char *packet
,
3762 struct tracepoint
*tpoint
, struct source_string
*src
)
3767 len
= strlen (src
->str
);
3768 buf
= alloca (len
* 2 + 1);
3769 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3771 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3772 tpoint
->number
, paddress (tpoint
->address
),
3773 src
->type
, 0, len
, buf
);
3776 /* Return the first piece of tracepoint definition, and initialize the
3777 state machine that will iterate through all the tracepoint
3781 cmd_qtfp (char *packet
)
3783 trace_debug ("Returning first tracepoint definition piece");
3785 cur_tpoint
= tracepoints
;
3786 cur_action
= cur_step_action
= 0;
3787 cur_source_string
= NULL
;
3790 response_tracepoint (packet
, cur_tpoint
);
3792 strcpy (packet
, "l");
3795 /* Return additional pieces of tracepoint definition. Each action and
3796 stepping action must go into its own packet, because of packet size
3797 limits, and so we use state variables to deliver one piece at a
3801 cmd_qtsp (char *packet
)
3803 trace_debug ("Returning subsequent tracepoint definition piece");
3807 /* This case would normally never occur, but be prepared for
3809 strcpy (packet
, "l");
3811 else if (cur_action
< cur_tpoint
->numactions
)
3813 response_action (packet
, cur_tpoint
,
3814 cur_tpoint
->actions_str
[cur_action
], 0);
3817 else if (cur_step_action
< cur_tpoint
->num_step_actions
)
3819 response_action (packet
, cur_tpoint
,
3820 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3823 else if ((cur_source_string
3824 ? cur_source_string
->next
3825 : cur_tpoint
->source_strings
))
3827 if (cur_source_string
)
3828 cur_source_string
= cur_source_string
->next
;
3830 cur_source_string
= cur_tpoint
->source_strings
;
3831 response_source (packet
, cur_tpoint
, cur_source_string
);
3835 cur_tpoint
= cur_tpoint
->next
;
3836 cur_action
= cur_step_action
= 0;
3837 cur_source_string
= NULL
;
3839 response_tracepoint (packet
, cur_tpoint
);
3841 strcpy (packet
, "l");
3845 /* Compose a response that is an imitation of the syntax by which the
3846 trace state variable was originally downloaded. */
3849 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3851 char *buf
= (char *) "";
3856 namelen
= strlen (tsv
->name
);
3857 buf
= alloca (namelen
* 2 + 1);
3858 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3861 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3862 tsv
->getter
? 1 : 0, buf
);
3865 /* Return the first trace state variable definition, and initialize
3866 the state machine that will iterate through all the tsv bits. */
3869 cmd_qtfv (char *packet
)
3871 trace_debug ("Returning first trace state variable definition");
3873 cur_tsv
= trace_state_variables
;
3876 response_tsv (packet
, cur_tsv
);
3878 strcpy (packet
, "l");
3881 /* Return additional trace state variable definitions. */
3884 cmd_qtsv (char *packet
)
3886 trace_debug ("Returning additional trace state variable definition");
3890 cur_tsv
= cur_tsv
->next
;
3892 response_tsv (packet
, cur_tsv
);
3894 strcpy (packet
, "l");
3897 strcpy (packet
, "l");
3900 /* Return the first static tracepoint marker, and initialize the state
3901 machine that will iterate through all the static tracepoints
3905 cmd_qtfstm (char *packet
)
3907 if (!maybe_write_ipa_ust_not_loaded (packet
))
3908 run_inferior_command (packet
, strlen (packet
) + 1);
3911 /* Return additional static tracepoints markers. */
3914 cmd_qtsstm (char *packet
)
3916 if (!maybe_write_ipa_ust_not_loaded (packet
))
3917 run_inferior_command (packet
, strlen (packet
) + 1);
3920 /* Return the definition of the static tracepoint at a given address.
3921 Result packet is the same as qTsST's. */
3924 cmd_qtstmat (char *packet
)
3926 if (!maybe_write_ipa_ust_not_loaded (packet
))
3927 run_inferior_command (packet
, strlen (packet
) + 1);
3930 /* Sent the agent a command to close it. */
3933 gdb_agent_about_to_close (int pid
)
3935 char buf
[IPA_CMD_BUF_SIZE
];
3937 if (!maybe_write_ipa_not_loaded (buf
))
3939 struct thread_info
*save_inferior
;
3940 struct inferior_list_entry
*inf
= all_threads
.head
;
3942 save_inferior
= current_inferior
;
3944 /* Find a certain thread which belongs to process PID. */
3947 if (ptid_get_pid (inf
->id
) == pid
)
3952 current_inferior
= (struct thread_info
*) inf
;
3954 strcpy (buf
, "close");
3956 run_inferior_command (buf
, strlen (buf
) + 1);
3958 current_inferior
= save_inferior
;
3962 /* Return the minimum instruction size needed for fast tracepoints as a
3963 hexadecimal number. */
3966 cmd_qtminftpilen (char *packet
)
3968 if (current_inferior
== NULL
)
3970 /* Indicate that the minimum length is currently unknown. */
3971 strcpy (packet
, "0");
3975 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3978 /* Respond to qTBuffer packet with a block of raw data from the trace
3979 buffer. GDB may ask for a lot, but we are allowed to reply with
3980 only as much as will fit within packet limits or whatever. */
3983 cmd_qtbuffer (char *own_buf
)
3985 ULONGEST offset
, num
, tot
;
3987 char *packet
= own_buf
;
3989 packet
+= strlen ("qTBuffer:");
3991 packet
= unpack_varlen_hex (packet
, &offset
);
3992 ++packet
; /* skip a comma */
3993 unpack_varlen_hex (packet
, &num
);
3995 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3996 (int) num
, phex_nz (offset
, 0));
3998 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
4000 /* If we're right at the end, reply specially that we're done. */
4003 strcpy (own_buf
, "l");
4007 /* Object to any other out-of-bounds request. */
4010 write_enn (own_buf
);
4014 /* Compute the pointer corresponding to the given offset, accounting
4016 tbp
= trace_buffer_start
+ offset
;
4017 if (tbp
>= trace_buffer_wrap
)
4018 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
4020 /* Trim to the remaining bytes if we're close to the end. */
4021 if (num
> tot
- offset
)
4024 /* Trim to available packet size. */
4025 if (num
>= (PBUFSIZ
- 16) / 2 )
4026 num
= (PBUFSIZ
- 16) / 2;
4028 convert_int_to_ascii (tbp
, own_buf
, num
);
4032 cmd_bigqtbuffer_circular (char *own_buf
)
4035 char *packet
= own_buf
;
4037 packet
+= strlen ("QTBuffer:circular:");
4039 unpack_varlen_hex (packet
, &val
);
4040 circular_trace_buffer
= val
;
4041 trace_debug ("Trace buffer is now %s",
4042 circular_trace_buffer
? "circular" : "linear");
4047 cmd_bigqtbuffer_size (char *own_buf
)
4051 char *packet
= own_buf
;
4053 /* Can't change the size during a tracing run. */
4056 write_enn (own_buf
);
4060 packet
+= strlen ("QTBuffer:size:");
4062 /* -1 is sent as literal "-1". */
4063 if (strcmp (packet
, "-1") == 0)
4064 sval
= DEFAULT_TRACE_BUFFER_SIZE
;
4067 unpack_varlen_hex (packet
, &val
);
4068 sval
= (LONGEST
) val
;
4071 init_trace_buffer (sval
);
4072 trace_debug ("Trace buffer is now %s bytes",
4073 plongest (trace_buffer_size
));
4078 cmd_qtnotes (char *own_buf
)
4081 char *saved
, *user
, *notes
, *stopnote
;
4082 char *packet
= own_buf
;
4084 packet
+= strlen ("QTNotes:");
4088 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
4090 packet
+= strlen ("user:");
4092 packet
= strchr (packet
, ';');
4093 nbytes
= (packet
- saved
) / 2;
4094 user
= xmalloc (nbytes
+ 1);
4095 nbytes
= unhexify (user
, saved
, nbytes
);
4096 user
[nbytes
] = '\0';
4097 ++packet
; /* skip the semicolon */
4098 trace_debug ("User is '%s'", user
);
4099 xfree (tracing_user_name
);
4100 tracing_user_name
= user
;
4102 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
4104 packet
+= strlen ("notes:");
4106 packet
= strchr (packet
, ';');
4107 nbytes
= (packet
- saved
) / 2;
4108 notes
= xmalloc (nbytes
+ 1);
4109 nbytes
= unhexify (notes
, saved
, nbytes
);
4110 notes
[nbytes
] = '\0';
4111 ++packet
; /* skip the semicolon */
4112 trace_debug ("Notes is '%s'", notes
);
4113 xfree (tracing_notes
);
4114 tracing_notes
= notes
;
4116 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
4118 packet
+= strlen ("tstop:");
4120 packet
= strchr (packet
, ';');
4121 nbytes
= (packet
- saved
) / 2;
4122 stopnote
= xmalloc (nbytes
+ 1);
4123 nbytes
= unhexify (stopnote
, saved
, nbytes
);
4124 stopnote
[nbytes
] = '\0';
4125 ++packet
; /* skip the semicolon */
4126 trace_debug ("tstop note is '%s'", stopnote
);
4127 xfree (tracing_stop_note
);
4128 tracing_stop_note
= stopnote
;
4138 handle_tracepoint_general_set (char *packet
)
4140 if (strcmp ("QTinit", packet
) == 0)
4142 cmd_qtinit (packet
);
4145 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
4150 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
4152 cmd_qtdpsrc (packet
);
4155 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
4157 cmd_qtenable_disable (packet
, 1);
4160 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
4162 cmd_qtenable_disable (packet
, 0);
4165 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
4170 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
4175 else if (strcmp ("QTStart", packet
) == 0)
4177 cmd_qtstart (packet
);
4180 else if (strcmp ("QTStop", packet
) == 0)
4182 cmd_qtstop (packet
);
4185 else if (strncmp ("QTDisconnected:", packet
,
4186 strlen ("QTDisconnected:")) == 0)
4188 cmd_qtdisconnected (packet
);
4191 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
4193 cmd_qtframe (packet
);
4196 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
4198 cmd_bigqtbuffer_circular (packet
);
4201 else if (strncmp ("QTBuffer:size:", packet
, strlen ("QTBuffer:size:")) == 0)
4203 cmd_bigqtbuffer_size (packet
);
4206 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
4208 cmd_qtnotes (packet
);
4216 handle_tracepoint_query (char *packet
)
4218 if (strcmp ("qTStatus", packet
) == 0)
4220 cmd_qtstatus (packet
);
4223 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4228 else if (strcmp ("qTfP", packet
) == 0)
4233 else if (strcmp ("qTsP", packet
) == 0)
4238 else if (strcmp ("qTfV", packet
) == 0)
4243 else if (strcmp ("qTsV", packet
) == 0)
4248 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4253 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4255 cmd_qtbuffer (packet
);
4258 else if (strcmp ("qTfSTM", packet
) == 0)
4260 cmd_qtfstm (packet
);
4263 else if (strcmp ("qTsSTM", packet
) == 0)
4265 cmd_qtsstm (packet
);
4268 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4270 cmd_qtstmat (packet
);
4273 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4275 cmd_qtminftpilen (packet
);
4283 #ifndef IN_PROCESS_AGENT
4285 /* Call this when thread TINFO has hit the tracepoint defined by
4286 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4287 action. This adds a while-stepping collecting state item to the
4288 threads' collecting state list, so that we can keep track of
4289 multiple simultaneous while-stepping actions being collected by the
4290 same thread. This can happen in cases like:
4292 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4294 ff0003 INSN3 <-- TP2, collect $regs
4295 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4298 Notice that when instruction INSN5 is reached, the while-stepping
4299 actions of both TP1 and TP3 are still being collected, and that TP2
4300 had been collected meanwhile. The whole range of ff0001-ff0005
4301 should be single-stepped, due to at least TP1's while-stepping
4302 action covering the whole range. */
4305 add_while_stepping_state (struct thread_info
*tinfo
,
4306 int tp_number
, CORE_ADDR tp_address
)
4308 struct wstep_state
*wstep
;
4310 wstep
= xmalloc (sizeof (*wstep
));
4311 wstep
->next
= tinfo
->while_stepping
;
4313 wstep
->tp_number
= tp_number
;
4314 wstep
->tp_address
= tp_address
;
4315 wstep
->current_step
= 0;
4317 tinfo
->while_stepping
= wstep
;
4320 /* Release the while-stepping collecting state WSTEP. */
4323 release_while_stepping_state (struct wstep_state
*wstep
)
4328 /* Release all while-stepping collecting states currently associated
4329 with thread TINFO. */
4332 release_while_stepping_state_list (struct thread_info
*tinfo
)
4334 struct wstep_state
*head
;
4336 while (tinfo
->while_stepping
)
4338 head
= tinfo
->while_stepping
;
4339 tinfo
->while_stepping
= head
->next
;
4340 release_while_stepping_state (head
);
4344 /* If TINFO was handling a 'while-stepping' action, the step has
4345 finished, so collect any step data needed, and check if any more
4346 steps are required. Return true if the thread was indeed
4347 collecting tracepoint data, false otherwise. */
4350 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4352 struct tracepoint
*tpoint
;
4353 struct wstep_state
*wstep
;
4354 struct wstep_state
**wstep_link
;
4355 struct trap_tracepoint_ctx ctx
;
4357 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4359 if (agent_loaded_p ())
4360 upload_fast_traceframes ();
4362 /* Check if we were indeed collecting data for one of more
4363 tracepoints with a 'while-stepping' count. */
4364 if (tinfo
->while_stepping
== NULL
)
4369 /* We're not even tracing anymore. Stop this thread from
4371 release_while_stepping_state_list (tinfo
);
4373 /* The thread had stopped due to a single-step request indeed
4374 explained by a tracepoint. */
4378 wstep
= tinfo
->while_stepping
;
4379 wstep_link
= &tinfo
->while_stepping
;
4381 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4382 target_pid_to_str (tinfo
->entry
.id
),
4383 wstep
->tp_number
, paddress (wstep
->tp_address
));
4385 ctx
.base
.type
= trap_tracepoint
;
4386 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4388 while (wstep
!= NULL
)
4390 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4393 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4394 wstep
->tp_number
, paddress (wstep
->tp_address
),
4395 target_pid_to_str (tinfo
->entry
.id
));
4398 *wstep_link
= wstep
->next
;
4399 release_while_stepping_state (wstep
);
4400 wstep
= *wstep_link
;
4404 /* We've just finished one step. */
4405 ++wstep
->current_step
;
4408 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4409 stop_pc
, tpoint
, wstep
->current_step
);
4411 if (wstep
->current_step
>= tpoint
->step_count
)
4413 /* The requested numbers of steps have occurred. */
4414 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4415 target_pid_to_str (tinfo
->entry
.id
),
4416 wstep
->tp_number
, paddress (wstep
->tp_address
));
4418 /* Unlink the wstep. */
4419 *wstep_link
= wstep
->next
;
4420 release_while_stepping_state (wstep
);
4421 wstep
= *wstep_link
;
4423 /* Only check the hit count now, which ensure that we do all
4424 our stepping before stopping the run. */
4425 if (tpoint
->pass_count
> 0
4426 && tpoint
->hit_count
>= tpoint
->pass_count
4427 && stopping_tracepoint
== NULL
)
4428 stopping_tracepoint
= tpoint
;
4432 /* Keep single-stepping until the requested numbers of steps
4434 wstep_link
= &wstep
->next
;
4435 wstep
= *wstep_link
;
4438 if (stopping_tracepoint
4439 || trace_buffer_is_full
4440 || expr_eval_result
!= expr_eval_no_error
)
4450 /* Handle any internal tracing control breakpoint hits. That means,
4451 pull traceframes from the IPA to our buffer, and syncing both
4452 tracing agents when the IPA's tracing stops for some reason. */
4455 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4457 /* Pull in fast tracepoint trace frames from the inferior in-process
4458 agent's buffer into our buffer. */
4460 if (!agent_loaded_p ())
4463 upload_fast_traceframes ();
4465 /* Check if the in-process agent had decided we should stop
4467 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4469 int ipa_trace_buffer_is_full
;
4470 CORE_ADDR ipa_stopping_tracepoint
;
4471 int ipa_expr_eval_result
;
4472 CORE_ADDR ipa_error_tracepoint
;
4474 trace_debug ("lib stopped at stop_tracing");
4476 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4477 &ipa_trace_buffer_is_full
);
4479 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4480 &ipa_stopping_tracepoint
);
4481 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4483 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4484 &ipa_error_tracepoint
);
4485 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4487 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4488 &ipa_expr_eval_result
);
4489 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4491 trace_debug ("lib: trace_buffer_is_full: %d, "
4492 "stopping_tracepoint: %s, "
4493 "ipa_expr_eval_result: %d, "
4494 "error_tracepoint: %s, ",
4495 ipa_trace_buffer_is_full
,
4496 paddress (ipa_stopping_tracepoint
),
4497 ipa_expr_eval_result
,
4498 paddress (ipa_error_tracepoint
));
4502 if (ipa_trace_buffer_is_full
)
4503 trace_debug ("lib stopped due to full buffer.");
4504 if (ipa_stopping_tracepoint
)
4505 trace_debug ("lib stopped due to tpoint");
4506 if (ipa_stopping_tracepoint
)
4507 trace_debug ("lib stopped due to error");
4510 if (ipa_stopping_tracepoint
!= 0)
4513 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4515 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4517 expr_eval_result
= ipa_expr_eval_result
;
4519 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4524 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4526 trace_debug ("lib stopped at flush_trace_buffer");
4533 /* Return true if TINFO just hit a tracepoint. Collect data if
4537 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4539 struct tracepoint
*tpoint
;
4541 struct trap_tracepoint_ctx ctx
;
4543 /* Not tracing, don't handle. */
4547 ctx
.base
.type
= trap_tracepoint
;
4548 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4550 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4552 /* Note that we collect fast tracepoints here as well. We'll
4553 step over the fast tracepoint jump later, which avoids the
4554 double collect. However, we don't collect for static
4555 tracepoints here, because UST markers are compiled in program,
4556 and probes will be executed in program. So static tracepoints
4557 are collected there. */
4558 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4559 && tpoint
->type
!= static_tracepoint
)
4561 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4562 target_pid_to_str (tinfo
->entry
.id
),
4563 tpoint
->number
, paddress (tpoint
->address
));
4565 /* Test the condition if present, and collect if true. */
4567 || (condition_true_at_tracepoint
4568 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4569 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4572 if (stopping_tracepoint
4573 || trace_buffer_is_full
4574 || expr_eval_result
!= expr_eval_no_error
)
4578 /* If the tracepoint had a 'while-stepping' action, then set
4579 the thread to collect this tracepoint on the following
4581 else if (tpoint
->step_count
> 0)
4583 add_while_stepping_state (tinfo
,
4584 tpoint
->number
, tpoint
->address
);
4596 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4597 struct ust_marker_data
;
4598 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4599 struct traceframe
*tframe
);
4602 /* Create a trace frame for the hit of the given tracepoint in the
4606 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4607 struct tracepoint
*tpoint
)
4609 struct traceframe
*tframe
;
4612 /* Only count it as a hit when we actually collect data. */
4613 tpoint
->hit_count
++;
4615 /* If we've exceeded a defined pass count, record the event for
4616 later, and finish the collection for this hit. This test is only
4617 for nonstepping tracepoints, stepping tracepoints test at the end
4618 of their while-stepping loop. */
4619 if (tpoint
->pass_count
> 0
4620 && tpoint
->hit_count
>= tpoint
->pass_count
4621 && tpoint
->step_count
== 0
4622 && stopping_tracepoint
== NULL
)
4623 stopping_tracepoint
= tpoint
;
4625 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4626 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4628 tframe
= add_traceframe (tpoint
);
4632 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4634 #ifndef IN_PROCESS_AGENT
4635 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4636 tpoint
->number
, paddress (tpoint
->address
),
4637 tpoint
->actions_str
[acti
]);
4640 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4641 tpoint
->actions
[acti
]);
4644 finish_traceframe (tframe
);
4647 if (tframe
== NULL
&& tracing
)
4648 trace_buffer_is_full
= 1;
4651 #ifndef IN_PROCESS_AGENT
4654 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4656 struct tracepoint
*tpoint
, int current_step
)
4658 struct traceframe
*tframe
;
4661 trace_debug ("Making new step traceframe for "
4662 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4663 tpoint
->number
, paddress (tpoint
->address
),
4664 current_step
, tpoint
->step_count
,
4667 tframe
= add_traceframe (tpoint
);
4671 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4673 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4674 tpoint
->number
, paddress (tpoint
->address
),
4675 tpoint
->step_actions_str
[acti
]);
4677 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4678 tpoint
->step_actions
[acti
]);
4681 finish_traceframe (tframe
);
4684 if (tframe
== NULL
&& tracing
)
4685 trace_buffer_is_full
= 1;
4690 #ifdef IN_PROCESS_AGENT
4691 /* The target description used by the IPA. Given that the IPA library
4692 is built for a specific architecture that is loaded into the
4693 inferior, there only needs to be one such description per
4695 const struct target_desc
*ipa_tdesc
;
4698 static struct regcache
*
4699 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4701 struct regcache
*regcache
= NULL
;
4703 #ifdef IN_PROCESS_AGENT
4704 if (ctx
->type
== fast_tracepoint
)
4706 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4707 if (!fctx
->regcache_initted
)
4709 fctx
->regcache_initted
= 1;
4710 init_register_cache (&fctx
->regcache
, ipa_tdesc
, fctx
->regspace
);
4711 supply_regblock (&fctx
->regcache
, NULL
);
4712 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4714 regcache
= &fctx
->regcache
;
4717 if (ctx
->type
== static_tracepoint
)
4719 struct static_tracepoint_ctx
*sctx
4720 = (struct static_tracepoint_ctx
*) ctx
;
4722 if (!sctx
->regcache_initted
)
4724 sctx
->regcache_initted
= 1;
4725 init_register_cache (&sctx
->regcache
, ipa_tdesc
, sctx
->regspace
);
4726 supply_regblock (&sctx
->regcache
, NULL
);
4727 /* Pass down the tracepoint address, because REGS doesn't
4728 include the PC, but we know what it must have been. */
4729 supply_static_tracepoint_registers (&sctx
->regcache
,
4730 (const unsigned char *)
4732 sctx
->tpoint
->address
);
4734 regcache
= &sctx
->regcache
;
4738 if (ctx
->type
== trap_tracepoint
)
4740 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4741 regcache
= tctx
->regcache
;
4745 gdb_assert (regcache
!= NULL
);
4751 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4753 struct tracepoint
*tpoint
,
4754 struct traceframe
*tframe
,
4755 struct tracepoint_action
*taction
)
4757 enum eval_result_type err
;
4759 switch (taction
->type
)
4763 struct collect_memory_action
*maction
;
4764 struct eval_agent_expr_context ax_ctx
;
4766 maction
= (struct collect_memory_action
*) taction
;
4767 ax_ctx
.regcache
= NULL
;
4768 ax_ctx
.tframe
= tframe
;
4769 ax_ctx
.tpoint
= tpoint
;
4771 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4772 pulongest (maction
->len
),
4773 paddress (maction
->addr
), maction
->basereg
);
4774 /* (should use basereg) */
4775 agent_mem_read (&ax_ctx
, NULL
, (CORE_ADDR
) maction
->addr
,
4781 unsigned char *regspace
;
4782 struct regcache tregcache
;
4783 struct regcache
*context_regcache
;
4786 trace_debug ("Want to collect registers");
4788 context_regcache
= get_context_regcache (ctx
);
4789 regcache_size
= register_cache_size (context_regcache
->tdesc
);
4791 /* Collect all registers for now. */
4792 regspace
= add_traceframe_block (tframe
, tpoint
, 1 + regcache_size
);
4793 if (regspace
== NULL
)
4795 trace_debug ("Trace buffer block allocation failed, skipping");
4798 /* Identify a register block. */
4801 /* Wrap the regblock in a register cache (in the stack, we
4802 don't want to malloc here). */
4803 init_register_cache (&tregcache
, context_regcache
->tdesc
,
4806 /* Copy the register data to the regblock. */
4807 regcache_cpy (&tregcache
, context_regcache
);
4809 #ifndef IN_PROCESS_AGENT
4810 /* On some platforms, trap-based tracepoints will have the PC
4811 pointing to the next instruction after the trap, but we
4812 don't want the user or GDB trying to guess whether the
4813 saved PC needs adjusting; so always record the adjusted
4814 stop_pc. Note that we can't use tpoint->address instead,
4815 since it will be wrong for while-stepping actions. This
4816 adjustment is a nop for fast tracepoints collected from the
4817 in-process lib (but not if GDBserver is collecting one
4818 preemptively), since the PC had already been adjusted to
4819 contain the tracepoint's address by the jump pad. */
4820 trace_debug ("Storing stop pc (0x%s) in regblock",
4821 paddress (stop_pc
));
4823 /* This changes the regblock, not the thread's
4825 regcache_write_pc (&tregcache
, stop_pc
);
4831 struct eval_expr_action
*eaction
;
4832 struct eval_agent_expr_context ax_ctx
;
4834 eaction
= (struct eval_expr_action
*) taction
;
4835 ax_ctx
.regcache
= get_context_regcache (ctx
);
4836 ax_ctx
.tframe
= tframe
;
4837 ax_ctx
.tpoint
= tpoint
;
4839 trace_debug ("Want to evaluate expression");
4841 err
= gdb_eval_agent_expr (&ax_ctx
, eaction
->expr
, NULL
);
4843 if (err
!= expr_eval_no_error
)
4845 record_tracepoint_error (tpoint
, "action expression", err
);
4852 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4853 trace_debug ("Want to collect static trace data");
4854 collect_ust_data_at_tracepoint (ctx
, tframe
);
4856 trace_debug ("warning: collecting static trace data, "
4857 "but static tracepoints are not supported");
4862 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4868 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4869 struct tracepoint
*tpoint
)
4872 enum eval_result_type err
;
4874 /* Presently, gdbserver doesn't run compiled conditions, only the
4875 IPA does. If the program stops at a fast tracepoint's address
4876 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4877 gdbserver preemptively collect the fast tracepoint. Later, on
4878 resume, gdbserver steps over the fast tracepoint like it steps
4879 over breakpoints, so that the IPA doesn't see that fast
4880 tracepoint. This avoids double collects of fast tracepoints in
4881 that stopping scenario. Having gdbserver itself handle the fast
4882 tracepoint gives the user a consistent view of when fast or trap
4883 tracepoints are collected, compared to an alternative where only
4884 trap tracepoints are collected on stop, and fast tracepoints on
4885 resume. When a fast tracepoint is being processed by gdbserver,
4886 it is always the non-compiled condition expression that is
4888 #ifdef IN_PROCESS_AGENT
4889 if (tpoint
->compiled_cond
)
4890 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4894 struct eval_agent_expr_context ax_ctx
;
4896 ax_ctx
.regcache
= get_context_regcache (ctx
);
4897 ax_ctx
.tframe
= NULL
;
4898 ax_ctx
.tpoint
= tpoint
;
4900 err
= gdb_eval_agent_expr (&ax_ctx
, tpoint
->cond
, &value
);
4902 if (err
!= expr_eval_no_error
)
4904 record_tracepoint_error (tpoint
, "condition", err
);
4905 /* The error case must return false. */
4909 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4910 tpoint
->number
, paddress (tpoint
->address
),
4912 return (value
? 1 : 0);
4915 /* Do memory copies for bytecodes. */
4916 /* Do the recording of memory blocks for actions and bytecodes. */
4919 agent_mem_read (struct eval_agent_expr_context
*ctx
,
4920 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4922 unsigned char *mspace
;
4923 ULONGEST remaining
= len
;
4924 unsigned short blocklen
;
4926 /* If a 'to' buffer is specified, use it. */
4929 read_inferior_memory (from
, to
, len
);
4933 /* Otherwise, create a new memory block in the trace buffer. */
4934 while (remaining
> 0)
4938 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4939 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4940 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4943 /* Identify block as a memory block. */
4946 /* Record address and size. */
4947 memcpy (mspace
, &from
, sizeof (from
));
4948 mspace
+= sizeof (from
);
4949 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4950 mspace
+= sizeof (blocklen
);
4951 /* Record the memory block proper. */
4952 read_inferior_memory (from
, mspace
, blocklen
);
4953 trace_debug ("%d bytes recorded", blocklen
);
4954 remaining
-= blocklen
;
4961 agent_mem_read_string (struct eval_agent_expr_context
*ctx
,
4962 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4964 unsigned char *buf
, *mspace
;
4965 ULONGEST remaining
= len
;
4966 unsigned short blocklen
, i
;
4968 /* To save a bit of space, block lengths are 16-bit, so break large
4969 requests into multiple blocks. Bordering on overkill for strings,
4970 but it could happen that someone specifies a large max length. */
4971 while (remaining
> 0)
4975 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4976 /* We want working space to accumulate nonzero bytes, since
4977 traceframes must have a predecided size (otherwise it gets
4978 harder to wrap correctly for the circular case, etc). */
4979 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4980 for (i
= 0; i
< blocklen
; ++i
)
4982 /* Read the string one byte at a time, in case the string is
4983 at the end of a valid memory area - we don't want a
4984 correctly-terminated string to engender segvio
4986 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4991 /* Make sure outer loop stops now too. */
4992 remaining
= blocklen
;
4996 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4997 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
5003 /* Identify block as a memory block. */
5006 /* Record address and size. */
5007 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
5008 mspace
+= sizeof (from
);
5009 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
5010 mspace
+= sizeof (blocklen
);
5011 /* Copy the string contents. */
5012 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
5013 remaining
-= blocklen
;
5020 /* Record the value of a trace state variable. */
5023 agent_tsv_read (struct eval_agent_expr_context
*ctx
, int n
)
5025 unsigned char *vspace
;
5028 vspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
,
5029 1 + sizeof (n
) + sizeof (LONGEST
));
5032 /* Identify block as a variable. */
5034 /* Record variable's number and value. */
5035 memcpy (vspace
+ 1, &n
, sizeof (n
));
5036 val
= get_trace_state_variable_value (n
);
5037 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
5038 trace_debug ("Variable %d recorded", n
);
5042 #ifndef IN_PROCESS_AGENT
5044 /* Callback for traceframe_walk_blocks, used to find a given block
5045 type in a traceframe. */
5048 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
5050 char *wantedp
= data
;
5052 if (*wantedp
== blocktype
)
5058 /* Walk over all traceframe blocks of the traceframe buffer starting
5059 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5060 block found, passing in DATA unmodified. If CALLBACK returns true,
5061 this returns a pointer to where the block is found. Returns NULL
5062 if no callback call returned true, indicating that all blocks have
5065 static unsigned char *
5066 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
5068 int (*callback
) (char blocktype
,
5069 unsigned char *dataptr
,
5073 unsigned char *dataptr
;
5077 trace_debug ("traceframe %d has no data", tfnum
);
5081 /* Iterate through a traceframe's blocks, looking for a block of the
5083 for (dataptr
= database
;
5084 dataptr
< database
+ datasize
;
5088 unsigned short mlen
;
5090 if (dataptr
== trace_buffer_wrap
)
5092 /* Adjust to reflect wrapping part of the frame around to
5094 datasize
= dataptr
- database
;
5095 dataptr
= database
= trace_buffer_lo
;
5098 blocktype
= *dataptr
++;
5100 if ((*callback
) (blocktype
, dataptr
, data
))
5106 /* Skip over the registers block. */
5107 dataptr
+= current_target_desc ()->registers_size
;
5110 /* Skip over the memory block. */
5111 dataptr
+= sizeof (CORE_ADDR
);
5112 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5113 dataptr
+= (sizeof (mlen
) + mlen
);
5116 /* Skip over the TSV block. */
5117 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5120 /* Skip over the static trace data block. */
5121 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5122 dataptr
+= (sizeof (mlen
) + mlen
);
5125 trace_debug ("traceframe %d has unknown block type 0x%x",
5134 /* Look for the block of type TYPE_WANTED in the trameframe starting
5135 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5138 static unsigned char *
5139 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5140 int tfnum
, char type_wanted
)
5142 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5143 match_blocktype
, &type_wanted
);
5146 static unsigned char *
5147 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5149 unsigned char *regblock
;
5151 regblock
= traceframe_find_block_type (tframe
->data
,
5155 if (regblock
== NULL
)
5156 trace_debug ("traceframe %d has no register data", tfnum
);
5161 /* Get registers from a traceframe. */
5164 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5166 unsigned char *dataptr
;
5167 struct tracepoint
*tpoint
;
5168 struct traceframe
*tframe
;
5170 tframe
= find_traceframe (tfnum
);
5174 trace_debug ("traceframe %d not found", tfnum
);
5178 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5179 if (dataptr
== NULL
)
5181 /* Mark registers unavailable. */
5182 supply_regblock (regcache
, NULL
);
5184 /* We can generally guess at a PC, although this will be
5185 misleading for while-stepping frames and multi-location
5187 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5189 regcache_write_pc (regcache
, tpoint
->address
);
5192 supply_regblock (regcache
, dataptr
);
5198 traceframe_get_pc (struct traceframe
*tframe
)
5200 struct regcache regcache
;
5201 unsigned char *dataptr
;
5202 const struct target_desc
*tdesc
= current_target_desc ();
5204 dataptr
= traceframe_find_regblock (tframe
, -1);
5205 if (dataptr
== NULL
)
5208 init_register_cache (®cache
, tdesc
, dataptr
);
5209 return regcache_read_pc (®cache
);
5212 /* Read a requested block of memory from a trace frame. */
5215 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5216 unsigned char *buf
, ULONGEST length
,
5219 struct traceframe
*tframe
;
5220 unsigned char *database
, *dataptr
;
5221 unsigned int datasize
;
5223 unsigned short mlen
;
5225 trace_debug ("traceframe_read_mem");
5227 tframe
= find_traceframe (tfnum
);
5231 trace_debug ("traceframe %d not found", tfnum
);
5235 datasize
= tframe
->data_size
;
5236 database
= dataptr
= &tframe
->data
[0];
5238 /* Iterate through a traceframe's blocks, looking for memory. */
5239 while ((dataptr
= traceframe_find_block_type (dataptr
,
5241 - (dataptr
- database
),
5242 tfnum
, 'M')) != NULL
)
5244 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5245 dataptr
+= sizeof (maddr
);
5246 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5247 dataptr
+= sizeof (mlen
);
5248 trace_debug ("traceframe %d has %d bytes at %s",
5249 tfnum
, mlen
, paddress (maddr
));
5251 /* If the block includes the first part of the desired range,
5252 return as much it has; GDB will re-request the remainder,
5253 which might be in a different block of this trace frame. */
5254 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5256 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5260 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5265 /* Skip over this block. */
5269 trace_debug ("traceframe %d has no memory data for the desired region",
5277 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5280 struct traceframe
*tframe
;
5281 unsigned char *database
, *dataptr
;
5282 unsigned int datasize
;
5286 trace_debug ("traceframe_read_tsv");
5288 tfnum
= current_traceframe
;
5292 trace_debug ("no current traceframe");
5296 tframe
= find_traceframe (tfnum
);
5300 trace_debug ("traceframe %d not found", tfnum
);
5304 datasize
= tframe
->data_size
;
5305 database
= dataptr
= &tframe
->data
[0];
5307 /* Iterate through a traceframe's blocks, looking for the last
5309 while ((dataptr
= traceframe_find_block_type (dataptr
,
5311 - (dataptr
- database
),
5312 tfnum
, 'V')) != NULL
)
5314 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5315 dataptr
+= sizeof (vnum
);
5317 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5319 /* Check that this is the variable we want. */
5322 memcpy (val
, dataptr
, sizeof (*val
));
5326 /* Skip over this block. */
5327 dataptr
+= sizeof (LONGEST
);
5331 trace_debug ("traceframe %d has no data for variable %d",
5336 /* Read a requested block of static tracepoint data from a trace
5340 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5341 unsigned char *buf
, ULONGEST length
,
5344 struct traceframe
*tframe
;
5345 unsigned char *database
, *dataptr
;
5346 unsigned int datasize
;
5347 unsigned short mlen
;
5349 trace_debug ("traceframe_read_sdata");
5351 tframe
= find_traceframe (tfnum
);
5355 trace_debug ("traceframe %d not found", tfnum
);
5359 datasize
= tframe
->data_size
;
5360 database
= &tframe
->data
[0];
5362 /* Iterate through a traceframe's blocks, looking for static
5364 dataptr
= traceframe_find_block_type (database
, datasize
,
5366 if (dataptr
!= NULL
)
5368 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5369 dataptr
+= sizeof (mlen
);
5372 if (offset
+ length
> mlen
)
5373 length
= mlen
- offset
;
5375 memcpy (buf
, dataptr
, length
);
5383 trace_debug ("traceframe %d has no static trace data", tfnum
);
5389 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5390 object. DATA is pointer to a struct buffer holding the
5391 traceframe-info object being built. */
5394 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5396 struct buffer
*buffer
= data
;
5402 unsigned short mlen
;
5405 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5406 dataptr
+= sizeof (maddr
);
5407 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5408 dataptr
+= sizeof (mlen
);
5409 buffer_xml_printf (buffer
,
5410 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5411 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5418 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5419 buffer_xml_printf (buffer
, "<tvar id=\"%d\"/>\n", vnum
);
5428 warning ("Unhandled trace block type (%d) '%c ' "
5429 "while building trace frame info.",
5430 blocktype
, blocktype
);
5437 /* Build a traceframe-info object for traceframe number TFNUM into
5441 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5443 struct traceframe
*tframe
;
5445 trace_debug ("traceframe_read_info");
5447 tframe
= find_traceframe (tfnum
);
5451 trace_debug ("traceframe %d not found", tfnum
);
5455 buffer_grow_str (buffer
, "<traceframe-info>\n");
5456 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5457 tfnum
, build_traceframe_info_xml
, buffer
);
5458 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5462 /* Return the first fast tracepoint whose jump pad contains PC. */
5464 static struct tracepoint
*
5465 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5467 struct tracepoint
*tpoint
;
5469 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5470 if (tpoint
->type
== fast_tracepoint
)
5471 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5477 /* Return the first fast tracepoint whose trampoline contains PC. */
5479 static struct tracepoint
*
5480 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5482 struct tracepoint
*tpoint
;
5484 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5486 if (tpoint
->type
== fast_tracepoint
5487 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5494 /* Return GDBserver's tracepoint that matches the IP Agent's
5495 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5498 static struct tracepoint
*
5499 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5501 struct tracepoint
*tpoint
;
5503 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5504 if (tpoint
->type
== fast_tracepoint
)
5505 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5513 /* The type of the object that is used to synchronize fast tracepoint
5516 typedef struct collecting_t
5518 /* The fast tracepoint number currently collecting. */
5521 /* A number that GDBserver can use to identify the thread that is
5522 presently holding the collect lock. This need not (and usually
5523 is not) the thread id, as getting the current thread ID usually
5524 requires a system call, which we want to avoid like the plague.
5525 Usually this is thread's TCB, found in the TLS (pseudo-)
5526 register, which is readable with a single insn on several
5528 uintptr_t thread_area
;
5531 #ifndef IN_PROCESS_AGENT
5534 force_unlock_trace_buffer (void)
5536 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5539 /* Check if the thread identified by THREAD_AREA which is stopped at
5540 STOP_PC, is presently locking the fast tracepoint collection, and
5541 if so, gather some status of said collection. Returns 0 if the
5542 thread isn't collecting or in the jump pad at all. 1, if in the
5543 jump pad (or within gdb_collect) and hasn't executed the adjusted
5544 original insn yet (can set a breakpoint there and run to it). 2,
5545 if presently executing the adjusted original insn --- in which
5546 case, if we want to move the thread out of the jump pad, we need to
5547 single-step it until this function returns 0. */
5550 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5552 struct fast_tpoint_collect_status
*status
)
5554 CORE_ADDR ipa_collecting
;
5555 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5556 CORE_ADDR ipa_gdb_trampoline_buffer
;
5557 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5558 struct tracepoint
*tpoint
;
5559 int needs_breakpoint
;
5561 /* The thread THREAD_AREA is either:
5563 0. not collecting at all, not within the jump pad, or within
5564 gdb_collect or one of its callees.
5566 1. in the jump pad and haven't reached gdb_collect
5568 2. within gdb_collect (out of the jump pad) (collect is set)
5570 3. we're in the jump pad, after gdb_collect having returned,
5571 possibly executing the adjusted insns.
5573 For cases 1 and 3, `collecting' may or not be set. The jump pad
5574 doesn't have any complicated jump logic, so we can tell if the
5575 thread is executing the adjust original insn or not by just
5576 matching STOP_PC with known jump pad addresses. If we it isn't
5577 yet executing the original insn, set a breakpoint there, and let
5578 the thread run to it, so to quickly step over a possible (many
5579 insns) gdb_collect call. Otherwise, or when the breakpoint is
5580 hit, only a few (small number of) insns are left to be executed
5581 in the jump pad. Single-step the thread until it leaves the
5586 needs_breakpoint
= 0;
5587 trace_debug ("fast_tracepoint_collecting");
5589 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5590 &ipa_gdb_jump_pad_buffer
))
5591 fatal ("error extracting `gdb_jump_pad_buffer'");
5592 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5593 &ipa_gdb_jump_pad_buffer_end
))
5594 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5596 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5597 &ipa_gdb_trampoline_buffer
))
5598 fatal ("error extracting `gdb_trampoline_buffer'");
5599 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5600 &ipa_gdb_trampoline_buffer_end
))
5601 fatal ("error extracting `gdb_trampoline_buffer_end'");
5603 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5604 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5606 /* We can tell which tracepoint(s) the thread is collecting by
5607 matching the jump pad address back to the tracepoint. */
5608 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5611 warning ("in jump pad, but no matching tpoint?");
5616 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5618 tpoint
->number
, paddress (tpoint
->address
),
5619 paddress (tpoint
->jump_pad
),
5620 paddress (tpoint
->jump_pad_end
),
5621 paddress (tpoint
->adjusted_insn_addr
),
5622 paddress (tpoint
->adjusted_insn_addr_end
));
5625 /* Definitely in the jump pad. May or may not need
5626 fast-exit-jump-pad breakpoint. */
5627 if (tpoint
->jump_pad
<= stop_pc
5628 && stop_pc
< tpoint
->adjusted_insn_addr
)
5629 needs_breakpoint
= 1;
5631 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5632 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5634 /* We can tell which tracepoint(s) the thread is collecting by
5635 matching the trampoline address back to the tracepoint. */
5636 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5639 warning ("in trampoline, but no matching tpoint?");
5644 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5645 tpoint
->number
, paddress (tpoint
->address
),
5646 paddress (tpoint
->trampoline
),
5647 paddress (tpoint
->trampoline_end
));
5650 /* Have not reached jump pad yet, but treat the trampoline as a
5651 part of the jump pad that is before the adjusted original
5653 needs_breakpoint
= 1;
5657 collecting_t ipa_collecting_obj
;
5659 /* If `collecting' is set/locked, then the THREAD_AREA thread
5660 may or not be the one holding the lock. We have to read the
5661 lock to find out. */
5663 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5666 trace_debug ("fast_tracepoint_collecting:"
5667 " failed reading 'collecting' in the inferior");
5671 if (!ipa_collecting
)
5673 trace_debug ("fast_tracepoint_collecting: not collecting"
5674 " (and nobody is).");
5678 /* Some thread is collecting. Check which. */
5679 if (read_inferior_memory (ipa_collecting
,
5680 (unsigned char *) &ipa_collecting_obj
,
5681 sizeof (ipa_collecting_obj
)) != 0)
5684 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5686 trace_debug ("fast_tracepoint_collecting: not collecting "
5687 "(another thread is)");
5692 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5695 warning ("fast_tracepoint_collecting: collecting, "
5696 "but tpoint %s not found?",
5697 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5701 /* The thread is within `gdb_collect', skip over the rest of
5702 fast tracepoint collection quickly using a breakpoint. */
5703 needs_breakpoint
= 1;
5706 /* The caller wants a bit of status detail. */
5709 status
->tpoint_num
= tpoint
->number
;
5710 status
->tpoint_addr
= tpoint
->address
;
5711 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5712 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5715 if (needs_breakpoint
)
5717 /* Hasn't executed the original instruction yet. Set breakpoint
5718 there, and wait till it's hit, then single-step until exiting
5722 fast_tracepoint_collecting, returning continue-until-break at %s",
5723 paddress (tpoint
->adjusted_insn_addr
));
5725 return 1; /* continue */
5729 /* Just single-step until exiting the jump pad. */
5731 trace_debug ("fast_tracepoint_collecting, returning "
5732 "need-single-step (%s-%s)",
5733 paddress (tpoint
->adjusted_insn_addr
),
5734 paddress (tpoint
->adjusted_insn_addr_end
));
5736 return 2; /* single-step */
5742 #ifdef IN_PROCESS_AGENT
5744 /* The global fast tracepoint collect lock. Points to a collecting_t
5745 object built on the stack by the jump pad, if presently locked;
5746 NULL if it isn't locked. Note that this lock *must* be set while
5747 executing any *function other than the jump pad. See
5748 fast_tracepoint_collecting. */
5749 static collecting_t
* ATTR_USED collecting
;
5751 /* This routine, called from the jump pad (in asm) is designed to be
5752 called from the jump pads of fast tracepoints, thus it is on the
5755 IP_AGENT_EXPORT
void ATTR_USED
5756 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5758 struct fast_tracepoint_ctx ctx
;
5760 /* Don't do anything until the trace run is completely set up. */
5764 ctx
.base
.type
= fast_tracepoint
;
5766 ctx
.regcache_initted
= 0;
5767 /* Wrap the regblock in a register cache (in the stack, we don't
5768 want to malloc here). */
5769 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
5770 if (ctx
.regspace
== NULL
)
5772 trace_debug ("Trace buffer block allocation failed, skipping");
5776 for (ctx
.tpoint
= tpoint
;
5777 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5778 ctx
.tpoint
= ctx
.tpoint
->next
)
5780 if (!ctx
.tpoint
->enabled
)
5783 /* Multiple tracepoints of different types, such as fast tracepoint and
5784 static tracepoint, can be set at the same address. */
5785 if (ctx
.tpoint
->type
!= tpoint
->type
)
5788 /* Test the condition if present, and collect if true. */
5789 if (ctx
.tpoint
->cond
== NULL
5790 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5793 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5794 ctx
.tpoint
->address
, ctx
.tpoint
);
5796 /* Note that this will cause original insns to be written back
5797 to where we jumped from, but that's OK because we're jumping
5798 back to the next whole instruction. This will go badly if
5799 instruction restoration is not atomic though. */
5800 if (stopping_tracepoint
5801 || trace_buffer_is_full
5802 || expr_eval_result
!= expr_eval_no_error
)
5810 /* If there was a condition and it evaluated to false, the only
5811 way we would stop tracing is if there was an error during
5812 condition expression evaluation. */
5813 if (expr_eval_result
!= expr_eval_no_error
)
5824 #ifndef IN_PROCESS_AGENT
5827 get_raw_reg_func_addr (void)
5829 return ipa_sym_addrs
.addr_get_raw_reg
;
5833 get_get_tsv_func_addr (void)
5835 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5839 get_set_tsv_func_addr (void)
5841 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5845 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5846 CORE_ADDR
*jump_entry
)
5848 CORE_ADDR entry_point
= *jump_entry
;
5849 enum eval_result_type err
;
5851 trace_debug ("Starting condition compilation for tracepoint %d\n",
5854 /* Initialize the global pointer to the code being built. */
5855 current_insn_ptr
= *jump_entry
;
5859 err
= compile_bytecodes (tpoint
->cond
);
5861 if (err
== expr_eval_no_error
)
5865 /* Record the beginning of the compiled code. */
5866 tpoint
->compiled_cond
= entry_point
;
5868 trace_debug ("Condition compilation for tracepoint %d complete\n",
5873 /* Leave the unfinished code in situ, but don't point to it. */
5875 tpoint
->compiled_cond
= 0;
5877 trace_debug ("Condition compilation for tracepoint %d failed, "
5879 tpoint
->number
, err
);
5882 /* Update the code pointer passed in. Note that we do this even if
5883 the compile fails, so that we can look at the partial results
5884 instead of letting them be overwritten. */
5885 *jump_entry
= current_insn_ptr
;
5887 /* Leave a gap, to aid dump decipherment. */
5891 /* We'll need to adjust these when we consider bi-arch setups, and big
5895 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5897 return write_inferior_memory (where
,
5898 (unsigned char *) &ptr
, sizeof (void *));
5901 /* The base pointer of the IPA's heap. This is the only memory the
5902 IPA is allowed to use. The IPA should _not_ call the inferior's
5903 `malloc' during operation. That'd be slow, and, most importantly,
5904 it may not be safe. We may be collecting a tracepoint in a signal
5905 handler, for example. */
5906 static CORE_ADDR target_tp_heap
;
5908 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5912 target_malloc (ULONGEST size
)
5916 if (target_tp_heap
== 0)
5918 /* We have the pointer *address*, need what it points to. */
5919 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5921 fatal ("could get target heap head pointer");
5924 ptr
= target_tp_heap
;
5925 target_tp_heap
+= size
;
5927 /* Pad to 8-byte alignment. */
5928 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5934 download_agent_expr (struct agent_expr
*expr
)
5936 CORE_ADDR expr_addr
;
5937 CORE_ADDR expr_bytes
;
5939 expr_addr
= target_malloc (sizeof (*expr
));
5940 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5942 expr_bytes
= target_malloc (expr
->length
);
5943 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5945 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5950 /* Align V up to N bits. */
5951 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5953 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5956 download_tracepoint_1 (struct tracepoint
*tpoint
)
5958 struct tracepoint target_tracepoint
;
5959 CORE_ADDR tpptr
= 0;
5961 gdb_assert (tpoint
->type
== fast_tracepoint
5962 || tpoint
->type
== static_tracepoint
);
5964 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5966 CORE_ADDR jentry
, jump_entry
;
5968 jentry
= jump_entry
= get_jump_space_head ();
5970 if (tpoint
->cond
!= NULL
)
5972 /* Pad to 8-byte alignment. (needed?) */
5973 /* Actually this should be left for the target to
5975 jentry
= UALIGN (jentry
, 8);
5977 compile_tracepoint_condition (tpoint
, &jentry
);
5980 /* Pad to 8-byte alignment. */
5981 jentry
= UALIGN (jentry
, 8);
5982 claim_jump_space (jentry
- jump_entry
);
5985 target_tracepoint
= *tpoint
;
5987 tpptr
= target_malloc (sizeof (*tpoint
));
5988 tpoint
->obj_addr_on_target
= tpptr
;
5990 /* Write the whole object. We'll fix up its pointers in a bit.
5991 Assume no next for now. This is fixed up above on the next
5992 iteration, if there's any. */
5993 target_tracepoint
.next
= NULL
;
5994 /* Need to clear this here too, since we're downloading the
5995 tracepoints before clearing our own copy. */
5996 target_tracepoint
.hit_count
= 0;
5998 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5999 sizeof (target_tracepoint
));
6002 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6004 download_agent_expr (tpoint
->cond
));
6006 if (tpoint
->numactions
)
6009 CORE_ADDR actions_array
;
6011 /* The pointers array. */
6013 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6014 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6018 /* Now for each pointer, download the action. */
6019 for (i
= 0; i
< tpoint
->numactions
; i
++)
6021 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6022 CORE_ADDR ipa_action
= action
->ops
->download (action
);
6024 if (ipa_action
!= 0)
6025 write_inferior_data_ptr
6026 (actions_array
+ i
* sizeof (*tpoint
->actions
),
6032 #define IPA_PROTO_FAST_TRACE_FLAG 0
6033 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
6034 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
6035 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
6036 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
6038 /* Send a command to agent to download and install tracepoint TPOINT. */
6041 tracepoint_send_agent (struct tracepoint
*tpoint
)
6043 char buf
[IPA_CMD_BUF_SIZE
];
6048 strcpy (p
, "FastTrace:");
6051 COPY_FIELD_TO_BUF (p
, tpoint
, number
);
6052 COPY_FIELD_TO_BUF (p
, tpoint
, address
);
6053 COPY_FIELD_TO_BUF (p
, tpoint
, type
);
6054 COPY_FIELD_TO_BUF (p
, tpoint
, enabled
);
6055 COPY_FIELD_TO_BUF (p
, tpoint
, step_count
);
6056 COPY_FIELD_TO_BUF (p
, tpoint
, pass_count
);
6057 COPY_FIELD_TO_BUF (p
, tpoint
, numactions
);
6058 COPY_FIELD_TO_BUF (p
, tpoint
, hit_count
);
6059 COPY_FIELD_TO_BUF (p
, tpoint
, traceframe_usage
);
6060 COPY_FIELD_TO_BUF (p
, tpoint
, compiled_cond
);
6061 COPY_FIELD_TO_BUF (p
, tpoint
, orig_size
);
6064 p
= agent_expr_send (p
, tpoint
->cond
);
6066 /* tracepoint_action */
6067 for (i
= 0; i
< tpoint
->numactions
; i
++)
6069 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6071 p
[0] = action
->type
;
6072 p
= action
->ops
->send (&p
[1], action
);
6075 get_jump_space_head ();
6076 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6077 agent can use jump pad from it. */
6078 if (tpoint
->type
== fast_tracepoint
)
6080 memcpy (p
, &gdb_jump_pad_head
, 8);
6084 ret
= run_inferior_command (buf
, (int) (ptrdiff_t) (p
- buf
));
6088 if (strncmp (buf
, "OK", 2) != 0)
6091 /* The value of tracepoint's target address is stored in BUF. */
6092 memcpy (&tpoint
->obj_addr_on_target
,
6093 &buf
[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET
], 8);
6095 if (tpoint
->type
== fast_tracepoint
)
6098 = (unsigned char *) &buf
[IPA_PROTO_FAST_TRACE_FJUMP_INSN
];
6101 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6102 (unsigned int) tpoint
->obj_addr_on_target
,
6103 (unsigned int) gdb_jump_pad_head
);
6105 memcpy (&gdb_jump_pad_head
, &buf
[IPA_PROTO_FAST_TRACE_JUMP_PAD
], 8);
6107 /* This has been done in agent. We should also set up record for it. */
6108 memcpy (&fjump_size
, &buf
[IPA_PROTO_FAST_TRACE_FJUMP_SIZE
], 4);
6111 = set_fast_tracepoint_jump (tpoint
->address
, insn
, fjump_size
);
6118 download_tracepoint (struct tracepoint
*tpoint
)
6120 struct tracepoint
*tp
, *tp_prev
;
6122 if (tpoint
->type
!= fast_tracepoint
6123 && tpoint
->type
!= static_tracepoint
)
6126 download_tracepoint_1 (tpoint
);
6128 /* Find the previous entry of TPOINT, which is fast tracepoint or
6129 static tracepoint. */
6131 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6133 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6139 CORE_ADDR tp_prev_target_next_addr
;
6141 /* Insert TPOINT after TP_PREV in IPA. */
6142 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6143 + offsetof (struct tracepoint
, next
),
6144 &tp_prev_target_next_addr
))
6145 fatal ("error reading `tp_prev->next'");
6147 /* tpoint->next = tp_prev->next */
6148 write_inferior_data_ptr (tpoint
->obj_addr_on_target
6149 + offsetof (struct tracepoint
, next
),
6150 tp_prev_target_next_addr
);
6151 /* tp_prev->next = tpoint */
6152 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
6153 + offsetof (struct tracepoint
, next
),
6154 tpoint
->obj_addr_on_target
);
6157 /* First object in list, set the head pointer in the
6159 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
6160 tpoint
->obj_addr_on_target
);
6165 download_trace_state_variables (void)
6167 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6168 struct trace_state_variable
*tsv
;
6170 /* Start out empty. */
6171 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6173 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6175 struct trace_state_variable target_tsv
;
6177 /* TSV's with a getter have been initialized equally in both the
6178 inferior and GDBserver. Skip them. */
6179 if (tsv
->getter
!= NULL
)
6185 ptr
= target_malloc (sizeof (*tsv
));
6187 if (tsv
== trace_state_variables
)
6189 /* First object in list, set the head pointer in the
6192 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6197 write_inferior_data_ptr (prev_ptr
6198 + offsetof (struct trace_state_variable
,
6203 /* Write the whole object. We'll fix up its pointers in a bit.
6204 Assume no next, fixup when needed. */
6205 target_tsv
.next
= NULL
;
6207 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6208 sizeof (target_tsv
));
6210 if (tsv
->name
!= NULL
)
6212 size_t size
= strlen (tsv
->name
) + 1;
6213 CORE_ADDR name_addr
= target_malloc (size
);
6214 write_inferior_memory (name_addr
,
6215 (unsigned char *) tsv
->name
, size
);
6216 write_inferior_data_ptr (ptr
6217 + offsetof (struct trace_state_variable
,
6222 if (tsv
->getter
!= NULL
)
6224 fatal ("what to do with these?");
6230 /* Fixup the next pointer in the last item in the list. */
6231 write_inferior_data_ptr (prev_ptr
6232 + offsetof (struct trace_state_variable
,
6237 /* Upload complete trace frames out of the IP Agent's trace buffer
6238 into GDBserver's trace buffer. This always uploads either all or
6239 no trace frames. This is the counter part of
6240 `trace_alloc_trace_buffer'. See its description of the atomic
6241 synching mechanism. */
6244 upload_fast_traceframes (void)
6246 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6247 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6249 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6250 unsigned int curr_tbctrl_idx
;
6251 unsigned int ipa_trace_buffer_ctrl_curr
;
6252 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6253 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6254 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6255 CORE_ADDR ipa_trace_buffer_lo
;
6256 CORE_ADDR ipa_trace_buffer_hi
;
6258 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6259 &ipa_traceframe_read_count_racy
))
6261 /* This will happen in most targets if the current thread is
6266 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6267 &ipa_traceframe_write_count_racy
))
6270 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6271 ipa_traceframe_write_count_racy
6272 - ipa_traceframe_read_count_racy
,
6273 ipa_traceframe_write_count_racy
,
6274 ipa_traceframe_read_count_racy
);
6276 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6279 about_to_request_buffer_space_bkpt
6280 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6283 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6284 &ipa_trace_buffer_ctrl_curr
))
6287 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6289 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6292 unsigned int prev
, counter
;
6294 /* Update the token, with new counters, and the GDBserver stamp
6295 bit. Alway reuse the current TBC index. */
6296 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6297 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6299 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6305 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6306 ipa_trace_buffer_ctrl_curr
))
6309 trace_debug ("Lib: Committed %08x -> %08x",
6310 ipa_trace_buffer_ctrl_curr_old
,
6311 ipa_trace_buffer_ctrl_curr
);
6313 /* Re-read these, now that we've installed the
6314 `about_to_request_buffer_space' breakpoint/lock. A thread could
6315 have finished a traceframe between the last read of these
6316 counters and setting the breakpoint above. If we start
6317 uploading, we never want to leave this function with
6318 traceframe_read_count != 0, otherwise, GDBserver could end up
6319 incrementing the counter tokens more than once (due to event loop
6320 nesting), which would break the IP agent's "effective" detection
6321 (see trace_alloc_trace_buffer). */
6322 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6323 &ipa_traceframe_read_count
))
6325 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6326 &ipa_traceframe_write_count
))
6331 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6332 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6333 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6335 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6336 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6337 trace_debug ("note that ipa_traceframe_count's parts changed");
6340 /* Get the address of the current TBC object (the IP agent has an
6341 array of 3 such objects). The index is stored in the TBC
6343 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6344 ipa_trace_buffer_ctrl_addr
6345 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6347 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6348 (unsigned char *) &ipa_trace_buffer_ctrl
,
6349 sizeof (struct ipa_trace_buffer_control
)))
6352 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6353 &ipa_trace_buffer_lo
))
6355 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6356 &ipa_trace_buffer_hi
))
6359 /* Offsets are easier to grok for debugging than raw addresses,
6360 especially for the small trace buffer sizes that are useful for
6362 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6363 "endfree=%d wrap=%d hi=%d",
6365 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6366 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6367 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6368 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6369 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6371 /* Note that the IPA's buffer is always circular. */
6373 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6375 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6376 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6378 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6379 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6380 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6381 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6384 tf
= IPA_FIRST_TRACEFRAME ();
6386 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6388 struct tracepoint
*tpoint
;
6389 struct traceframe
*tframe
;
6390 unsigned char *block
;
6391 struct traceframe ipa_tframe
;
6393 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6394 offsetof (struct traceframe
, data
)))
6395 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6397 if (ipa_tframe
.tpnum
== 0)
6398 fatal ("Uploading: No (more) fast traceframes, but "
6399 "ipa_traceframe_count == %u??\n",
6400 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6402 /* Note that this will be incorrect for multi-location
6404 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6406 tframe
= add_traceframe (tpoint
);
6409 trace_buffer_is_full
= 1;
6410 trace_debug ("Uploading: trace buffer is full");
6414 /* Copy the whole set of blocks in one go for now. FIXME:
6415 split this in smaller blocks. */
6416 block
= add_traceframe_block (tframe
, tpoint
,
6417 ipa_tframe
.data_size
);
6420 if (read_inferior_memory (tf
6421 + offsetof (struct traceframe
, data
),
6422 block
, ipa_tframe
.data_size
))
6423 error ("Uploading: Couldn't read traceframe data at %s\n",
6424 paddress (tf
+ offsetof (struct traceframe
, data
)));
6427 trace_debug ("Uploading: traceframe didn't fit");
6428 finish_traceframe (tframe
);
6431 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6433 /* If we freed the traceframe that wrapped around, go back
6434 to the non-wrap case. */
6435 if (tf
< ipa_trace_buffer_ctrl
.start
)
6437 trace_debug ("Lib: Discarding past the wraparound");
6438 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6440 ipa_trace_buffer_ctrl
.start
= tf
;
6441 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6442 ++ipa_traceframe_read_count
;
6444 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6445 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6447 trace_debug ("Lib: buffer is fully empty. "
6448 "Trace buffer [%d] start=%d free=%d endfree=%d",
6450 (int) (ipa_trace_buffer_ctrl
.start
6451 - ipa_trace_buffer_lo
),
6452 (int) (ipa_trace_buffer_ctrl
.free
6453 - ipa_trace_buffer_lo
),
6454 (int) (ipa_trace_buffer_ctrl
.end_free
6455 - ipa_trace_buffer_lo
));
6457 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6458 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6459 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6460 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6463 trace_debug ("Uploaded a traceframe\n"
6464 "Lib: Trace buffer [%d] start=%d free=%d "
6465 "endfree=%d wrap=%d hi=%d",
6467 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6468 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6469 (int) (ipa_trace_buffer_ctrl
.end_free
6470 - ipa_trace_buffer_lo
),
6471 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6472 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6475 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6476 (unsigned char *) &ipa_trace_buffer_ctrl
,
6477 sizeof (struct ipa_trace_buffer_control
)))
6480 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6481 ipa_traceframe_read_count
);
6483 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6486 cancel_breakpoints ();
6488 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6489 about_to_request_buffer_space_bkpt
= NULL
;
6493 if (trace_buffer_is_full
)
6498 #ifdef IN_PROCESS_AGENT
6500 IP_AGENT_EXPORT
int ust_loaded
;
6501 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6505 /* Static tracepoints. */
6507 /* UST puts a "struct tracepoint" in the global namespace, which
6508 conflicts with our tracepoint. Arguably, being a library, it
6509 shouldn't take ownership of such a generic name. We work around it
6511 #define tracepoint ust_tracepoint
6512 #include <ust/ust.h>
6515 extern int serialize_to_text (char *outbuf
, int bufsize
,
6516 const char *fmt
, va_list ap
);
6518 #define GDB_PROBE_NAME "gdb"
6520 /* We dynamically search for the UST symbols instead of linking them
6521 in. This lets the user decide if the application uses static
6522 tracepoints, instead of always pulling libust.so in. This vector
6523 holds pointers to all functions we care about. */
6527 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6528 const char *fmt
, va_list ap
);
6530 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6531 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6533 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6535 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6538 void (*marker_iter_start
) (struct marker_iter
*iter
);
6539 void (*marker_iter_next
) (struct marker_iter
*iter
);
6540 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6541 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6546 /* Cast through typeof to catch incompatible API changes. Since UST
6547 only builds with gcc, we can freely use gcc extensions here
6549 #define GET_UST_SYM(SYM) \
6552 if (ust_ops.SYM == NULL) \
6553 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6554 if (ust_ops.SYM == NULL) \
6558 #define USTF(SYM) ust_ops.SYM
6560 /* Get pointers to all libust.so functions we care about. */
6565 GET_UST_SYM (serialize_to_text
);
6567 GET_UST_SYM (ltt_probe_register
);
6568 GET_UST_SYM (ltt_probe_unregister
);
6569 GET_UST_SYM (ltt_marker_connect
);
6570 GET_UST_SYM (ltt_marker_disconnect
);
6572 GET_UST_SYM (marker_iter_start
);
6573 GET_UST_SYM (marker_iter_next
);
6574 GET_UST_SYM (marker_iter_stop
);
6575 GET_UST_SYM (marker_iter_reset
);
6581 /* Given an UST marker, return the matching gdb static tracepoint.
6582 The match is done by address. */
6584 static struct tracepoint
*
6585 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6587 struct tracepoint
*tpoint
;
6589 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6591 if (tpoint
->type
!= static_tracepoint
)
6594 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6601 /* The probe function we install on lttng/ust markers. Whenever a
6602 probed ust marker is hit, this function is called. This is similar
6603 to gdb_collect, only for static tracepoints, instead of fast
6607 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6608 struct registers
*regs
, void *call_private
,
6609 const char *fmt
, va_list *args
)
6611 struct tracepoint
*tpoint
;
6612 struct static_tracepoint_ctx ctx
;
6614 /* Don't do anything until the trace run is completely set up. */
6617 trace_debug ("gdb_probe: not tracing\n");
6621 ctx
.base
.type
= static_tracepoint
;
6622 ctx
.regcache_initted
= 0;
6627 /* Wrap the regblock in a register cache (in the stack, we don't
6628 want to malloc here). */
6629 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
6630 if (ctx
.regspace
== NULL
)
6632 trace_debug ("Trace buffer block allocation failed, skipping");
6636 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6639 trace_debug ("gdb_probe: marker not known: "
6640 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6641 mdata
->location
, mdata
->channel
,
6642 mdata
->name
, mdata
->format
);
6646 if (!tpoint
->enabled
)
6648 trace_debug ("gdb_probe: tracepoint disabled");
6652 ctx
.tpoint
= tpoint
;
6654 trace_debug ("gdb_probe: collecting marker: "
6655 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6656 mdata
->location
, mdata
->channel
,
6657 mdata
->name
, mdata
->format
);
6659 /* Test the condition if present, and collect if true. */
6660 if (tpoint
->cond
== NULL
6661 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6664 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6665 tpoint
->address
, tpoint
);
6667 if (stopping_tracepoint
6668 || trace_buffer_is_full
6669 || expr_eval_result
!= expr_eval_no_error
)
6674 /* If there was a condition and it evaluated to false, the only
6675 way we would stop tracing is if there was an error during
6676 condition expression evaluation. */
6677 if (expr_eval_result
!= expr_eval_no_error
)
6682 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6683 static tracepoint string data. This is a string passed to the
6684 tracing library by the user, at the time of the tracepoint marker
6685 call. E.g., in the UST marker call:
6687 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6689 the collected data is "str FOOBAZ".
6693 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6694 struct traceframe
*tframe
)
6696 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6697 unsigned char *bufspace
;
6700 unsigned short blocklen
;
6704 trace_debug ("Wanted to collect static trace data, "
6705 "but there's no static trace data");
6709 va_copy (copy
, *umd
->args
);
6710 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6713 trace_debug ("Want to collect ust data");
6715 /* 'S' + size + string */
6716 bufspace
= add_traceframe_block (tframe
, umd
->tpoint
,
6717 1 + sizeof (blocklen
) + size
+ 1);
6718 if (bufspace
== NULL
)
6720 trace_debug ("Trace buffer block allocation failed, skipping");
6724 /* Identify a static trace data block. */
6727 blocklen
= size
+ 1;
6728 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6730 va_copy (copy
, *umd
->args
);
6731 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6732 size
+ 1, umd
->fmt
, copy
);
6735 trace_debug ("Storing static tracepoint data in regblock: %s",
6736 bufspace
+ 1 + sizeof (blocklen
));
6739 /* The probe to register with lttng/ust. */
6740 static struct ltt_available_probe gdb_ust_probe
=
6747 #endif /* HAVE_UST */
6748 #endif /* IN_PROCESS_AGENT */
6750 #ifndef IN_PROCESS_AGENT
6752 /* Ask the in-process agent to run a command. Since we don't want to
6753 have to handle the IPA hitting breakpoints while running the
6754 command, we pause all threads, remove all breakpoints, and then set
6755 the helper thread re-running. We communicate with the helper
6756 thread by means of direct memory xfering, and a socket for
6760 run_inferior_command (char *cmd
, int len
)
6763 int pid
= ptid_get_pid (current_ptid
);
6765 trace_debug ("run_inferior_command: running: %s", cmd
);
6768 uninsert_all_breakpoints ();
6770 err
= agent_run_command (pid
, (const char *) cmd
, len
);
6772 reinsert_all_breakpoints ();
6778 #else /* !IN_PROCESS_AGENT */
6780 #include <sys/socket.h>
6783 #ifndef UNIX_PATH_MAX
6784 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6787 /* Where we put the socked used for synchronization. */
6788 #define SOCK_DIR P_tmpdir
6790 /* Thread ID of the helper thread. GDBserver reads this to know which
6791 is the help thread. This is an LWP id on Linux. */
6792 int helper_thread_id
;
6795 init_named_socket (const char *name
)
6798 struct sockaddr_un addr
;
6800 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6803 warning ("socket creation failed: %s", strerror (errno
));
6807 addr
.sun_family
= AF_UNIX
;
6809 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6810 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6812 result
= access (name
, F_OK
);
6816 result
= unlink (name
);
6819 warning ("unlink failed: %s", strerror (errno
));
6823 warning ("socket %s already exists; overwriting", name
);
6826 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6829 warning ("bind failed: %s", strerror (errno
));
6834 result
= listen (fd
, 1);
6837 warning ("listen: %s", strerror (errno
));
6845 static char agent_socket_name
[UNIX_PATH_MAX
];
6848 gdb_agent_socket_init (void)
6852 result
= xsnprintf (agent_socket_name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6853 SOCK_DIR
, getpid ());
6854 if (result
>= UNIX_PATH_MAX
)
6856 trace_debug ("string overflow allocating socket name");
6860 fd
= init_named_socket (agent_socket_name
);
6862 warning ("Error initializing named socket (%s) for communication with the "
6863 "ust helper thread. Check that directory exists and that it "
6864 "is writable.", agent_socket_name
);
6871 /* The next marker to be returned on a qTsSTM command. */
6872 static const struct marker
*next_st
;
6874 /* Returns the first known marker. */
6879 struct marker_iter iter
;
6881 USTF(marker_iter_reset
) (&iter
);
6882 USTF(marker_iter_start
) (&iter
);
6887 /* Returns the marker following M. */
6889 const struct marker
*
6890 next_marker (const struct marker
*m
)
6892 struct marker_iter iter
;
6894 USTF(marker_iter_reset
) (&iter
);
6895 USTF(marker_iter_start
) (&iter
);
6897 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6899 if (iter
.marker
== m
)
6901 USTF(marker_iter_next
) (&iter
);
6909 /* Return an hexstr version of the STR C string, fit for sending to
6913 cstr_to_hexstr (const char *str
)
6915 int len
= strlen (str
);
6916 char *hexstr
= xmalloc (len
* 2 + 1);
6917 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6921 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6925 response_ust_marker (char *packet
, const struct marker
*st
)
6927 char *strid
, *format
, *tmp
;
6929 next_st
= next_marker (st
);
6931 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6932 strlen (st
->name
) + 1);
6933 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6935 strid
= cstr_to_hexstr (tmp
);
6938 format
= cstr_to_hexstr (st
->format
);
6940 sprintf (packet
, "m%s:%s:%s",
6941 paddress ((uintptr_t) st
->location
),
6949 /* Return the first static tracepoint, and initialize the state
6950 machine that will iterate through all the static tracepoints. */
6953 cmd_qtfstm (char *packet
)
6955 trace_debug ("Returning first trace state variable definition");
6957 if (first_marker ())
6958 response_ust_marker (packet
, first_marker ());
6960 strcpy (packet
, "l");
6963 /* Return additional trace state variable definitions. */
6966 cmd_qtsstm (char *packet
)
6968 trace_debug ("Returning static tracepoint");
6971 response_ust_marker (packet
, next_st
);
6973 strcpy (packet
, "l");
6976 /* Disconnect the GDB probe from a marker at a given address. */
6979 unprobe_marker_at (char *packet
)
6983 struct marker_iter iter
;
6985 p
+= sizeof ("unprobe_marker_at:") - 1;
6987 p
= unpack_varlen_hex (p
, &address
);
6989 USTF(marker_iter_reset
) (&iter
);
6990 USTF(marker_iter_start
) (&iter
);
6991 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6992 if ((uintptr_t ) iter
.marker
->location
== address
)
6996 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7000 warning ("could not disable marker %s/%s",
7001 iter
.marker
->channel
, iter
.marker
->name
);
7006 /* Connect the GDB probe to a marker at a given address. */
7009 probe_marker_at (char *packet
)
7013 struct marker_iter iter
;
7016 p
+= sizeof ("probe_marker_at:") - 1;
7018 p
= unpack_varlen_hex (p
, &address
);
7020 USTF(marker_iter_reset
) (&iter
);
7022 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7024 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7025 if ((uintptr_t ) m
->location
== address
)
7029 trace_debug ("found marker for address. "
7030 "ltt_marker_connect (marker = %s/%s)",
7031 m
->channel
, m
->name
);
7033 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7035 if (result
&& result
!= -EEXIST
)
7036 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7037 m
->channel
, m
->name
, -result
);
7041 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7042 m
->channel
, m
->name
);
7046 strcpy (packet
, "OK");
7050 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7055 cmd_qtstmat (char *packet
)
7059 struct marker_iter iter
;
7062 p
+= sizeof ("qTSTMat:") - 1;
7064 p
= unpack_varlen_hex (p
, &address
);
7066 USTF(marker_iter_reset
) (&iter
);
7068 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7070 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7071 if ((uintptr_t ) m
->location
== address
)
7073 response_ust_marker (packet
, m
);
7077 strcpy (packet
, "l");
7087 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7090 #endif /* HAVE_UST */
7092 #include <sys/syscall.h>
7096 gdb_agent_remove_socket (void)
7098 unlink (agent_socket_name
);
7101 /* Helper thread of agent. */
7104 gdb_agent_helper_thread (void *arg
)
7108 atexit (gdb_agent_remove_socket
);
7112 listen_fd
= gdb_agent_socket_init ();
7114 if (helper_thread_id
== 0)
7115 helper_thread_id
= syscall (SYS_gettid
);
7117 if (listen_fd
== -1)
7119 warning ("could not create sync socket\n");
7126 struct sockaddr_un sockaddr
;
7132 tmp
= sizeof (sockaddr
);
7136 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7138 /* It seems an ERESTARTSYS can escape out of accept. */
7139 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7143 warning ("Accept returned %d, error: %s\n",
7144 fd
, strerror (errno
));
7150 ret
= read (fd
, buf
, 1);
7151 } while (ret
== -1 && errno
== EINTR
);
7155 warning ("reading socket (fd=%d) failed with %s",
7156 fd
, strerror (errno
));
7163 if (strncmp ("close", cmd_buf
, 5) == 0)
7168 else if (strcmp ("qTfSTM", cmd_buf
) == 0)
7170 cmd_qtfstm (cmd_buf
);
7172 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7174 cmd_qtsstm (cmd_buf
);
7176 else if (strncmp ("unprobe_marker_at:",
7178 sizeof ("unprobe_marker_at:") - 1) == 0)
7180 unprobe_marker_at (cmd_buf
);
7182 else if (strncmp ("probe_marker_at:",
7184 sizeof ("probe_marker_at:") - 1) == 0)
7186 probe_marker_at (cmd_buf
);
7188 else if (strncmp ("qTSTMat:",
7190 sizeof ("qTSTMat:") - 1) == 0)
7192 cmd_qtstmat (cmd_buf
);
7194 #endif /* HAVE_UST */
7197 /* Fix compiler's warning: ignoring return value of 'write'. */
7198 ret
= write (fd
, buf
, 1);
7204 unlink (agent_socket_name
);
7206 /* Sleep endlessly to wait the whole inferior stops. This
7207 thread can not exit because GDB or GDBserver may still need
7208 'current_inferior' (representing this thread) to access
7209 inferior memory. Otherwise, this thread exits earlier than
7210 other threads, and 'current_inferior' is set to NULL. */
7221 #include <pthread.h>
7223 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
7226 gdb_agent_init (void)
7233 /* We want the helper thread to be as transparent as possible, so
7234 have it inherit an all-signals-blocked mask. */
7236 sigfillset (&new_mask
);
7237 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7239 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7241 res
= pthread_create (&thread
,
7243 gdb_agent_helper_thread
,
7246 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7248 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7250 while (helper_thread_id
== 0)
7258 #include <sys/mman.h>
7261 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7262 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7263 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7264 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
7265 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
7266 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
7268 /* Record the result of getting buffer space for fast tracepoint
7269 trampolines. Any error message is copied, since caller may not be
7270 using persistent storage. */
7273 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7275 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7276 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7278 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7280 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7283 static void __attribute__ ((constructor
))
7284 initialize_tracepoint_ftlib (void)
7286 initialize_tracepoint ();
7291 #endif /* IN_PROCESS_AGENT */
7293 /* Return a timestamp, expressed as microseconds of the usual Unix
7294 time. (As the result is a 64-bit number, it will not overflow any
7298 get_timestamp (void)
7302 if (gettimeofday (&tv
, 0) != 0)
7305 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7309 initialize_tracepoint (void)
7311 /* Start with the default size. */
7312 init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE
);
7314 /* Wire trace state variable 1 to be the timestamp. This will be
7315 uploaded to GDB upon connection and become one of its trace state
7316 variables. (In case you're wondering, if GDB already has a trace
7317 variable numbered 1, it will be renumbered.) */
7318 create_trace_state_variable (1, 0);
7319 set_trace_state_variable_name (1, "trace_timestamp");
7320 set_trace_state_variable_getter (1, get_timestamp
);
7322 #ifdef IN_PROCESS_AGENT
7327 pagesize
= sysconf (_SC_PAGE_SIZE
);
7331 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7333 #define SCRATCH_BUFFER_NPAGES 20
7335 /* Allocate scratch buffer aligned on a page boundary, at a low
7336 address (close to the main executable's code). */
7337 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7339 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7340 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7341 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7343 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7349 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7352 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7355 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7357 /* It's not a fatal error for something to go wrong with trampoline
7358 buffer setup, but it can be mysterious, so create a channel to
7359 report back on what went wrong, using a fixed size since we may
7360 not be able to allocate space later when the problem occurs. */
7361 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7363 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7365 initialize_low_tracepoint ();