1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2012 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 /* This file is built for both GDBserver, and the in-process
33 agent (IPA), a shared library that includes a tracing agent that is
34 loaded by the inferior to support fast tracepoints. Fast
35 tracepoints (or more accurately, jump based tracepoints) are
36 implemented by patching the tracepoint location with a jump into a
37 small trampoline function whose job is to save the register state,
38 call the in-process tracing agent, and then execute the original
39 instruction that was under the tracepoint jump (possibly adjusted,
40 if PC-relative, or some such).
42 The current synchronization design is pull based. That means,
43 GDBserver does most of the work, by peeking/poking at the inferior
44 agent's memory directly for downloading tracepoint and associated
45 objects, and for uploading trace frames. Whenever the IPA needs
46 something from GDBserver (trace buffer is full, tracing stopped for
47 some reason, etc.) the IPA calls a corresponding hook function
48 where GDBserver has placed a breakpoint.
50 Each of the agents has its own trace buffer. When browsing the
51 trace frames built from slow and fast tracepoints from GDB (tfind
52 mode), there's no guarantee the user is seeing the trace frames in
53 strict chronological creation order, although, GDBserver tries to
54 keep the order relatively reasonable, by syncing the trace buffers
59 static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf
, 1, 2);
62 trace_vdebug (const char *fmt
, ...)
68 vsprintf (buf
, fmt
, ap
);
69 fprintf (stderr
, PROG
"/tracepoint: %s\n", buf
);
73 #define trace_debug_1(level, fmt, args...) \
75 if (level <= debug_threads) \
76 trace_vdebug ((fmt), ##args); \
79 #define trace_debug(FMT, args...) \
80 trace_debug_1 (1, FMT, ##args)
83 # define ATTR_USED __attribute__((used))
84 # define ATTR_NOINLINE __attribute__((noinline))
85 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
88 # define ATTR_NOINLINE
89 # define ATTR_CONSTRUCTOR
92 /* Make sure the functions the IPA needs to export (symbols GDBserver
93 needs to query GDB about) are exported. */
95 #ifdef IN_PROCESS_AGENT
96 # if defined _WIN32 || defined __CYGWIN__
97 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
100 # define IP_AGENT_EXPORT \
101 __attribute__ ((visibility("default"))) ATTR_USED
103 # define IP_AGENT_EXPORT ATTR_USED
107 # define IP_AGENT_EXPORT
110 /* Prefix exported symbols, for good citizenship. All the symbols
111 that need exporting are defined in this module. */
112 #ifdef IN_PROCESS_AGENT
113 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
114 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
115 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
116 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
117 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
118 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
119 # define collecting gdb_agent_collecting
120 # define gdb_collect gdb_agent_gdb_collect
121 # define stop_tracing gdb_agent_stop_tracing
122 # define flush_trace_buffer gdb_agent_flush_trace_buffer
123 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
124 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
125 # define stopping_tracepoint gdb_agent_stopping_tracepoint
126 # define expr_eval_result gdb_agent_expr_eval_result
127 # define error_tracepoint gdb_agent_error_tracepoint
128 # define tracepoints gdb_agent_tracepoints
129 # define tracing gdb_agent_tracing
130 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
131 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
132 # define trace_buffer_lo gdb_agent_trace_buffer_lo
133 # define trace_buffer_hi gdb_agent_trace_buffer_hi
134 # define traceframe_read_count gdb_agent_traceframe_read_count
135 # define traceframe_write_count gdb_agent_traceframe_write_count
136 # define traceframes_created gdb_agent_traceframes_created
137 # define trace_state_variables gdb_agent_trace_state_variables
138 # define get_raw_reg gdb_agent_get_raw_reg
139 # define get_trace_state_variable_value \
140 gdb_agent_get_trace_state_variable_value
141 # define set_trace_state_variable_value \
142 gdb_agent_set_trace_state_variable_value
143 # define ust_loaded gdb_agent_ust_loaded
144 # define helper_thread_id gdb_agent_helper_thread_id
145 # define cmd_buf gdb_agent_cmd_buf
148 #ifndef IN_PROCESS_AGENT
150 /* Addresses of in-process agent's symbols GDBserver cares about. */
152 struct ipa_sym_addresses
154 CORE_ADDR addr_gdb_tp_heap_buffer
;
155 CORE_ADDR addr_gdb_jump_pad_buffer
;
156 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
157 CORE_ADDR addr_gdb_trampoline_buffer
;
158 CORE_ADDR addr_gdb_trampoline_buffer_end
;
159 CORE_ADDR addr_gdb_trampoline_buffer_error
;
160 CORE_ADDR addr_collecting
;
161 CORE_ADDR addr_gdb_collect
;
162 CORE_ADDR addr_stop_tracing
;
163 CORE_ADDR addr_flush_trace_buffer
;
164 CORE_ADDR addr_about_to_request_buffer_space
;
165 CORE_ADDR addr_trace_buffer_is_full
;
166 CORE_ADDR addr_stopping_tracepoint
;
167 CORE_ADDR addr_expr_eval_result
;
168 CORE_ADDR addr_error_tracepoint
;
169 CORE_ADDR addr_tracepoints
;
170 CORE_ADDR addr_tracing
;
171 CORE_ADDR addr_trace_buffer_ctrl
;
172 CORE_ADDR addr_trace_buffer_ctrl_curr
;
173 CORE_ADDR addr_trace_buffer_lo
;
174 CORE_ADDR addr_trace_buffer_hi
;
175 CORE_ADDR addr_traceframe_read_count
;
176 CORE_ADDR addr_traceframe_write_count
;
177 CORE_ADDR addr_traceframes_created
;
178 CORE_ADDR addr_trace_state_variables
;
179 CORE_ADDR addr_get_raw_reg
;
180 CORE_ADDR addr_get_trace_state_variable_value
;
181 CORE_ADDR addr_set_trace_state_variable_value
;
182 CORE_ADDR addr_ust_loaded
;
191 IPA_SYM(gdb_tp_heap_buffer
),
192 IPA_SYM(gdb_jump_pad_buffer
),
193 IPA_SYM(gdb_jump_pad_buffer_end
),
194 IPA_SYM(gdb_trampoline_buffer
),
195 IPA_SYM(gdb_trampoline_buffer_end
),
196 IPA_SYM(gdb_trampoline_buffer_error
),
198 IPA_SYM(gdb_collect
),
199 IPA_SYM(stop_tracing
),
200 IPA_SYM(flush_trace_buffer
),
201 IPA_SYM(about_to_request_buffer_space
),
202 IPA_SYM(trace_buffer_is_full
),
203 IPA_SYM(stopping_tracepoint
),
204 IPA_SYM(expr_eval_result
),
205 IPA_SYM(error_tracepoint
),
206 IPA_SYM(tracepoints
),
208 IPA_SYM(trace_buffer_ctrl
),
209 IPA_SYM(trace_buffer_ctrl_curr
),
210 IPA_SYM(trace_buffer_lo
),
211 IPA_SYM(trace_buffer_hi
),
212 IPA_SYM(traceframe_read_count
),
213 IPA_SYM(traceframe_write_count
),
214 IPA_SYM(traceframes_created
),
215 IPA_SYM(trace_state_variables
),
216 IPA_SYM(get_raw_reg
),
217 IPA_SYM(get_trace_state_variable_value
),
218 IPA_SYM(set_trace_state_variable_value
),
222 static struct ipa_sym_addresses ipa_sym_addrs
;
224 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
226 /* Returns true if both the in-process agent library and the static
227 tracepoints libraries are loaded in the inferior, and agent has
228 capability on static tracepoints. */
231 in_process_agent_supports_ust (void)
235 if (!agent_loaded_p ())
237 warning ("In-process agent not loaded");
241 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE
))
243 /* Agent understands static tracepoint, then check whether UST is in
244 fact loaded in the inferior. */
245 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
247 warning ("Error reading ust_loaded in lib");
258 write_e_ipa_not_loaded (char *buffer
)
261 "E.In-process agent library not loaded in process. "
262 "Fast and static tracepoints unavailable.");
265 /* Write an error to BUFFER indicating that UST isn't loaded in the
269 write_e_ust_not_loaded (char *buffer
)
273 "E.UST library not loaded in process. "
274 "Static tracepoints unavailable.");
276 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
280 /* If the in-process agent library isn't loaded in the inferior, write
281 an error to BUFFER, and return 1. Otherwise, return 0. */
284 maybe_write_ipa_not_loaded (char *buffer
)
286 if (!agent_loaded_p ())
288 write_e_ipa_not_loaded (buffer
);
294 /* If the in-process agent library and the ust (static tracepoints)
295 library aren't loaded in the inferior, write an error to BUFFER,
296 and return 1. Otherwise, return 0. */
299 maybe_write_ipa_ust_not_loaded (char *buffer
)
301 if (!agent_loaded_p ())
303 write_e_ipa_not_loaded (buffer
);
306 else if (!in_process_agent_supports_ust ())
308 write_e_ust_not_loaded (buffer
);
314 /* Cache all future symbols that the tracepoints module might request.
315 We can not request symbols at arbitrary states in the remote
316 protocol, only when the client tells us that new symbols are
317 available. So when we load the in-process library, make sure to
318 check the entire list. */
321 tracepoint_look_up_symbols (void)
325 if (agent_loaded_p ())
328 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
331 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
333 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
336 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
341 agent_look_up_symbols (NULL
);
346 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
347 of the "stop_tracing" function. When this breakpoint is hit,
348 tracing stopped in the IPA for some reason. E.g., due to
349 tracepoint reaching the pass count, hitting conditional expression
350 evaluation error, etc.
352 The IPA's trace buffer is never in circular tracing mode: instead,
353 GDBserver's is, and whenever the in-process buffer fills, it calls
354 "flush_trace_buffer", which triggers an internal breakpoint.
355 GDBserver reacts to this breakpoint by pulling the meanwhile
356 collected data. Old frames discarding is always handled on the
359 #ifdef IN_PROCESS_AGENT
361 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
363 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
367 /* Call this in the functions where GDBserver places a breakpoint, so
368 that the compiler doesn't try to be clever and skip calling the
369 function at all. This is necessary, even if we tell the compiler
370 to not inline said functions. */
372 #if defined(__GNUC__)
373 # define UNKNOWN_SIDE_EFFECTS() asm ("")
375 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
378 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
381 /* GDBserver places breakpoint here. */
382 UNKNOWN_SIDE_EFFECTS();
385 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
386 flush_trace_buffer (void)
388 /* GDBserver places breakpoint here. */
389 UNKNOWN_SIDE_EFFECTS();
394 #ifndef IN_PROCESS_AGENT
396 tracepoint_handler (CORE_ADDR address
)
398 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
403 /* Breakpoint at "stop_tracing" in the inferior lib. */
404 struct breakpoint
*stop_tracing_bkpt
;
405 static int stop_tracing_handler (CORE_ADDR
);
407 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
408 struct breakpoint
*flush_trace_buffer_bkpt
;
409 static int flush_trace_buffer_handler (CORE_ADDR
);
411 static void download_tracepoints (void);
412 static void download_trace_state_variables (void);
413 static void upload_fast_traceframes (void);
415 static int run_inferior_command (char *cmd
);
418 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
420 return read_inferior_memory (symaddr
, (unsigned char *) val
,
425 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
427 return read_inferior_memory (symaddr
, (unsigned char *) val
,
432 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
434 void *pval
= (void *) (uintptr_t) val
;
437 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
438 *val
= (uintptr_t) pval
;
443 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
445 void *pval
= (void *) (uintptr_t) val
;
446 return write_inferior_memory (symaddr
,
447 (unsigned char *) &pval
, sizeof (pval
));
451 write_inferior_integer (CORE_ADDR symaddr
, int val
)
453 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
457 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
459 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
462 static CORE_ADDR
target_malloc (ULONGEST size
);
463 static int write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
);
466 /* Operations on various types of tracepoint actions. */
468 struct tracepoint_action
;
470 struct tracepoint_action_ops
472 /* Download tracepoint action ACTION to IPA. Return the address of action
474 CORE_ADDR (*download
) (const struct tracepoint_action
*action
);
477 /* Base action. Concrete actions inherit this. */
479 struct tracepoint_action
481 #ifndef IN_PROCESS_AGENT
482 const struct tracepoint_action_ops
*ops
;
487 /* An 'M' (collect memory) action. */
488 struct collect_memory_action
490 struct tracepoint_action base
;
497 /* An 'R' (collect registers) action. */
499 struct collect_registers_action
501 struct tracepoint_action base
;
504 /* An 'X' (evaluate expression) action. */
506 struct eval_expr_action
508 struct tracepoint_action base
;
510 struct agent_expr
*expr
;
513 /* An 'L' (collect static trace data) action. */
514 struct collect_static_trace_data_action
516 struct tracepoint_action base
;
519 #ifndef IN_PROCESS_AGENT
521 m_tracepoint_action_download (const struct tracepoint_action
*action
)
523 int size_in_ipa
= (sizeof (struct collect_memory_action
)
524 - offsetof (struct tracepoint_action
, type
));
525 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
527 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
533 static const struct tracepoint_action_ops m_tracepoint_action_ops
=
535 m_tracepoint_action_download
,
539 r_tracepoint_action_download (const struct tracepoint_action
*action
)
541 int size_in_ipa
= (sizeof (struct collect_registers_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 static const struct tracepoint_action_ops r_tracepoint_action_ops
=
553 r_tracepoint_action_download
,
556 static CORE_ADDR
download_agent_expr (struct agent_expr
*expr
);
559 x_tracepoint_action_download (const struct tracepoint_action
*action
)
561 int size_in_ipa
= (sizeof (struct eval_expr_action
)
562 - offsetof (struct tracepoint_action
, type
));
563 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
566 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
568 expr
= download_agent_expr (((struct eval_expr_action
*)action
)->expr
);
569 write_inferior_data_ptr (ipa_action
+ offsetof (struct eval_expr_action
, expr
)
570 - offsetof (struct tracepoint_action
, type
),
576 static const struct tracepoint_action_ops x_tracepoint_action_ops
=
578 x_tracepoint_action_download
,
582 l_tracepoint_action_download (const struct tracepoint_action
*action
)
584 int size_in_ipa
= (sizeof (struct collect_static_trace_data_action
)
585 - offsetof (struct tracepoint_action
, type
));
586 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
588 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
594 static const struct tracepoint_action_ops l_tracepoint_action_ops
=
596 l_tracepoint_action_download
,
600 /* This structure describes a piece of the source-level definition of
601 the tracepoint. The contents are not interpreted by the target,
602 but preserved verbatim for uploading upon reconnection. */
606 /* The type of string, such as "cond" for a conditional. */
609 /* The source-level string itself. For the sake of target
610 debugging, we store it in plaintext, even though it is always
611 transmitted in hex. */
614 /* Link to the next one in the list. We link them in the order
615 received, in case some make up an ordered list of commands or
617 struct source_string
*next
;
622 /* Trap based tracepoint. */
625 /* A fast tracepoint implemented with a jump instead of a trap. */
628 /* A static tracepoint, implemented by a program call into a tracing
633 struct tracepoint_hit_ctx
;
635 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
638 /* The definition of a tracepoint. */
640 /* Tracepoints may have multiple locations, each at a different
641 address. This can occur with optimizations, template
642 instantiation, etc. Since the locations may be in different
643 scopes, the conditions and actions may be different for each
644 location. Our target version of tracepoints is more like GDB's
645 notion of "breakpoint locations", but we have almost nothing that
646 is not per-location, so we bother having two kinds of objects. The
647 key consequence is that numbers are not unique, and that it takes
648 both number and address to identify a tracepoint uniquely. */
652 /* The number of the tracepoint, as specified by GDB. Several
653 tracepoint objects here may share a number. */
656 /* Address at which the tracepoint is supposed to trigger. Several
657 tracepoints may share an address. */
660 /* Tracepoint type. */
661 enum tracepoint_type type
;
663 /* True if the tracepoint is currently enabled. */
666 /* The number of single steps that will be performed after each
670 /* The number of times the tracepoint may be hit before it will
671 terminate the entire tracing run. */
674 /* Pointer to the agent expression that is the tracepoint's
675 conditional, or NULL if the tracepoint is unconditional. */
676 struct agent_expr
*cond
;
678 /* The list of actions to take when the tracepoint triggers. */
680 struct tracepoint_action
**actions
;
682 /* Count of the times we've hit this tracepoint during the run.
683 Note that while-stepping steps are not counted as "hits". */
686 /* Cached sum of the sizes of traceframes created by this point. */
687 uint64_t traceframe_usage
;
689 CORE_ADDR compiled_cond
;
691 /* Link to the next tracepoint in the list. */
692 struct tracepoint
*next
;
694 #ifndef IN_PROCESS_AGENT
695 /* The list of actions to take when the tracepoint triggers, in
696 string/packet form. */
699 /* The collection of strings that describe the tracepoint as it was
700 entered into GDB. These are not used by the target, but are
701 reported back to GDB upon reconnection. */
702 struct source_string
*source_strings
;
704 /* The number of bytes displaced by fast tracepoints. It may subsume
705 multiple instructions, for multi-byte fast tracepoints. This
706 field is only valid for fast tracepoints. */
709 /* Only for fast tracepoints. */
710 CORE_ADDR obj_addr_on_target
;
712 /* Address range where the original instruction under a fast
713 tracepoint was relocated to. (_end is actually one byte past
715 CORE_ADDR adjusted_insn_addr
;
716 CORE_ADDR adjusted_insn_addr_end
;
718 /* The address range of the piece of the jump pad buffer that was
719 assigned to this fast tracepoint. (_end is actually one byte
722 CORE_ADDR jump_pad_end
;
724 /* The address range of the piece of the trampoline buffer that was
725 assigned to this fast tracepoint. (_end is actually one byte
727 CORE_ADDR trampoline
;
728 CORE_ADDR trampoline_end
;
730 /* The list of actions to take while in a stepping loop. These
731 fields are only valid for patch-based tracepoints. */
732 int num_step_actions
;
733 struct tracepoint_action
**step_actions
;
734 /* Same, but in string/packet form. */
735 char **step_actions_str
;
737 /* Handle returned by the breakpoint or tracepoint module when we
738 inserted the trap or jump, or hooked into a static tracepoint.
739 NULL if we haven't inserted it yet. */
745 #ifndef IN_PROCESS_AGENT
747 /* Given `while-stepping', a thread may be collecting data for more
748 than one tracepoint simultaneously. On the other hand, the same
749 tracepoint with a while-stepping action may be hit by more than one
750 thread simultaneously (but not quite, each thread could be handling
751 a different step). Each thread holds a list of these objects,
752 representing the current step of each while-stepping action being
757 struct wstep_state
*next
;
759 /* The tracepoint number. */
761 /* The tracepoint's address. */
762 CORE_ADDR tp_address
;
764 /* The number of the current step in this 'while-stepping'
771 /* The linked list of all tracepoints. Marked explicitly as used as
772 the in-process library doesn't use it for the fast tracepoints
774 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
776 #ifndef IN_PROCESS_AGENT
778 /* Pointer to the last tracepoint in the list, new tracepoints are
779 linked in at the end. */
781 static struct tracepoint
*last_tracepoint
;
784 /* The first tracepoint to exceed its pass count. */
786 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
788 /* True if the trace buffer is full or otherwise no longer usable. */
790 IP_AGENT_EXPORT
int trace_buffer_is_full
;
792 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
794 #ifndef IN_PROCESS_AGENT
796 static const char *eval_result_names
[] =
798 "terror:in the attic", /* this should never be reported */
799 "terror:empty expression",
800 "terror:empty stack",
801 "terror:stack overflow",
802 "terror:stack underflow",
803 "terror:unhandled opcode",
804 "terror:unrecognized opcode",
805 "terror:divide by zero"
810 /* The tracepoint in which the error occurred. */
812 static struct tracepoint
*error_tracepoint
;
814 struct trace_state_variable
816 /* This is the name of the variable as used in GDB. The target
817 doesn't use the name, but needs to have it for saving and
818 reconnection purposes. */
821 /* This number identifies the variable uniquely. Numbers may be
822 assigned either by the target (in the case of builtin variables),
823 or by GDB, and are presumed unique during the course of a trace
827 /* The variable's initial value, a 64-bit signed integer always. */
828 LONGEST initial_value
;
830 /* The variable's value, a 64-bit signed integer always. */
833 /* Pointer to a getter function, used to supply computed values. */
834 LONGEST (*getter
) (void);
836 /* Link to the next variable. */
837 struct trace_state_variable
*next
;
840 /* Linked list of all trace state variables. */
842 #ifdef IN_PROCESS_AGENT
843 struct trace_state_variable
*alloced_trace_state_variables
;
846 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
848 /* The results of tracing go into a fixed-size space known as the
849 "trace buffer". Because usage follows a limited number of
850 patterns, we manage it ourselves rather than with malloc. Basic
851 rules are that we create only one trace frame at a time, each is
852 variable in size, they are never moved once created, and we only
853 discard if we are doing a circular buffer, and then only the oldest
854 ones. Each trace frame includes its own size, so we don't need to
855 link them together, and the trace frame number is relative to the
856 first one, so we don't need to record numbers. A trace frame also
857 records the number of the tracepoint that created it. The data
858 itself is a series of blocks, each introduced by a single character
859 and with a defined format. Each type of block has enough
860 type/length info to allow scanners to jump quickly from one block
861 to the next without reading each byte in the block. */
863 /* Trace buffer management would be simple - advance a free pointer
864 from beginning to end, then stop - were it not for the circular
865 buffer option, which is a useful way to prevent a trace run from
866 stopping prematurely because the buffer filled up. In the circular
867 case, the location of the first trace frame (trace_buffer_start)
868 moves as old trace frames are discarded. Also, since we grow trace
869 frames incrementally as actions are performed, we wrap around to
870 the beginning of the trace buffer. This is per-block, so each
871 block within a trace frame remains contiguous. Things get messy
872 when the wrapped-around trace frame is the one being discarded; the
873 free space ends up in two parts at opposite ends of the buffer. */
876 # if defined(__GNUC__)
877 # define ATTR_PACKED __attribute__ ((packed))
879 # define ATTR_PACKED /* nothing */
883 /* The data collected at a tracepoint hit. This object should be as
884 small as possible, since there may be a great many of them. We do
885 not need to keep a frame number, because they are all sequential
886 and there are no deletions; so the Nth frame in the buffer is
887 always frame number N. */
891 /* Number of the tracepoint that collected this traceframe. A value
892 of 0 indicates the current end of the trace buffer. We make this
893 a 16-bit field because it's never going to happen that GDB's
894 numbering of tracepoints reaches 32,000. */
897 /* The size of the data in this trace frame. We limit this to 32
898 bits, even on a 64-bit target, because it's just implausible that
899 one is validly going to collect 4 gigabytes of data at a single
901 unsigned int data_size
: 32;
903 /* The base of the trace data, which is contiguous from this point. */
904 unsigned char data
[0];
908 /* The traceframe to be used as the source of data to send back to
909 GDB. A value of -1 means to get data from the live program. */
911 int current_traceframe
= -1;
913 /* This flag is true if the trace buffer is circular, meaning that
914 when it fills, the oldest trace frames are discarded in order to
917 #ifndef IN_PROCESS_AGENT
918 static int circular_trace_buffer
;
921 /* Pointer to the block of memory that traceframes all go into. */
923 static unsigned char *trace_buffer_lo
;
925 /* Pointer to the end of the trace buffer, more precisely to the byte
926 after the end of the buffer. */
928 static unsigned char *trace_buffer_hi
;
930 /* Control structure holding the read/write/etc. pointers into the
931 trace buffer. We need more than one of these to implement a
932 transaction-like mechanism to garantees that both GDBserver and the
933 in-process agent can try to change the trace buffer
936 struct trace_buffer_control
938 /* Pointer to the first trace frame in the buffer. In the
939 non-circular case, this is equal to trace_buffer_lo, otherwise it
940 moves around in the buffer. */
941 unsigned char *start
;
943 /* Pointer to the free part of the trace buffer. Note that we clear
944 several bytes at and after this pointer, so that traceframe
945 scans/searches terminate properly. */
948 /* Pointer to the byte after the end of the free part. Note that
949 this may be smaller than trace_buffer_free in the circular case,
950 and means that the free part is in two pieces. Initially it is
951 equal to trace_buffer_hi, then is generally equivalent to
952 trace_buffer_start. */
953 unsigned char *end_free
;
955 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
956 this is the point at which the trace data breaks, and resumes at
961 /* Same as above, to be used by GDBserver when updating the in-process
963 struct ipa_trace_buffer_control
972 /* We have possibly both GDBserver and an inferior thread accessing
973 the same IPA trace buffer memory. The IPA is the producer (tries
974 to put new frames in the buffer), while GDBserver occasionally
975 consumes them, that is, flushes the IPA's buffer into its own
976 buffer. Both sides need to update the trace buffer control
977 pointers (current head, tail, etc.). We can't use a global lock to
978 synchronize the accesses, as otherwise we could deadlock GDBserver
979 (if the thread holding the lock stops for a signal, say). So
980 instead of that, we use a transaction scheme where GDBserver writes
981 always prevail over the IPAs writes, and, we have the IPA detect
982 the commit failure/overwrite, and retry the whole attempt. This is
983 mainly implemented by having a global token object that represents
984 who wrote last to the buffer control structure. We need to freeze
985 any inferior writing to the buffer while GDBserver touches memory,
986 so that the inferior can correctly detect that GDBserver had been
987 there, otherwise, it could mistakingly think its commit was
988 successful; that's implemented by simply having GDBserver set a
989 breakpoint the inferior hits if it is the critical region.
991 There are three cycling trace buffer control structure copies
992 (buffer head, tail, etc.), with the token object including an index
993 indicating which is current live copy. The IPA tentatively builds
994 an updated copy in a non-current control structure, while GDBserver
995 always clobbers the current version directly. The IPA then tries
996 to atomically "commit" its version; if GDBserver clobbered the
997 structure meanwhile, that will fail, and the IPA restarts the
1000 Listing the step in further detail, we have:
1002 In-process agent (producer):
1004 - passes by `about_to_request_buffer_space' breakpoint/lock
1006 - reads current token, extracts current trace buffer control index,
1007 and starts tentatively updating the rightmost one (0->1, 1->2,
1008 2->0). Note that only one inferior thread is executing this code
1009 at any given time, due to an outer lock in the jump pads.
1011 - updates counters, and tries to commit the token.
1013 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1014 leaving the sync region.
1016 - checks if the update was effective.
1018 - if trace buffer was found full, hits flush_trace_buffer
1019 breakpoint, and restarts later afterwards.
1021 GDBserver (consumer):
1023 - sets `about_to_request_buffer_space' breakpoint/lock.
1025 - updates the token unconditionally, using the current buffer
1026 control index, since it knows that the IP agent always writes to
1027 the rightmost, and due to the breakpoint, at most one IP thread
1028 can try to update the trace buffer concurrently to GDBserver, so
1029 there will be no danger of trace buffer control index wrap making
1030 the IPA write to the same index as GDBserver.
1032 - flushes the IP agent's trace buffer completely, and updates the
1033 current trace buffer control structure. GDBserver *always* wins.
1035 - removes the `about_to_request_buffer_space' breakpoint.
1037 The token is stored in the `trace_buffer_ctrl_curr' variable.
1038 Internally, it's bits are defined as:
1040 |-------------+-----+-------------+--------+-------------+--------------|
1041 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1042 |-------------+-----+-------------+--------+-------------+--------------|
1043 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1044 |-------------+-----+-------------+--------+-------------+--------------|
1046 GSB - GDBserver Stamp Bit
1047 PC - Previous Counter
1048 CC - Current Counter
1049 TBCI - Trace Buffer Control Index
1052 An IPA update of `trace_buffer_ctrl_curr' does:
1054 - read CC from the current token, save as PC.
1056 - atomically tries to write PC+1,CC
1058 A GDBserver update of `trace_buffer_ctrl_curr' does:
1060 - reads PC and CC from the current token.
1065 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1066 for the counters described below. The cleared bits are used to
1067 hold the index of the items of the `trace_buffer_ctrl' array that
1069 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1071 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1072 counter, and the `current' counter. */
1074 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1075 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1077 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1078 always stamps this bit as set. */
1079 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1081 #ifdef IN_PROCESS_AGENT
1082 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1083 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1085 # define TRACE_BUFFER_CTRL_CURR \
1086 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1090 /* The GDBserver side agent only needs one instance of this object, as
1091 it doesn't need to sync with itself. Define it as array anyway so
1092 that the rest of the code base doesn't need to care for the
1094 struct trace_buffer_control trace_buffer_ctrl
[1];
1095 # define TRACE_BUFFER_CTRL_CURR 0
1098 /* These are convenience macros used to access the current trace
1099 buffer control in effect. */
1100 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1101 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1102 #define trace_buffer_end_free \
1103 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1104 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1107 /* Macro that returns a pointer to the first traceframe in the buffer. */
1109 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1111 /* Macro that returns a pointer to the next traceframe in the buffer.
1112 If the computed location is beyond the wraparound point, subtract
1113 the offset of the wraparound. */
1115 #define NEXT_TRACEFRAME_1(TF) \
1116 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1118 #define NEXT_TRACEFRAME(TF) \
1119 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1120 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1121 ? (trace_buffer_wrap - trace_buffer_lo) \
1124 /* The difference between these counters represents the total number
1125 of complete traceframes present in the trace buffer. The IP agent
1126 writes to the write count, GDBserver writes to read count. */
1128 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1129 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1131 /* Convenience macro. */
1133 #define traceframe_count \
1134 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1136 /* The count of all traceframes created in the current run, including
1137 ones that were discarded to make room. */
1139 IP_AGENT_EXPORT
int traceframes_created
;
1141 #ifndef IN_PROCESS_AGENT
1143 /* Read-only regions are address ranges whose contents don't change,
1144 and so can be read from target memory even while looking at a trace
1145 frame. Without these, disassembly for instance will likely fail,
1146 because the program code is not usually collected into a trace
1147 frame. This data structure does not need to be very complicated or
1148 particularly efficient, it's only going to be used occasionally,
1149 and only by some commands. */
1151 struct readonly_region
1153 /* The bounds of the region. */
1154 CORE_ADDR start
, end
;
1156 /* Link to the next one. */
1157 struct readonly_region
*next
;
1160 /* Linked list of readonly regions. This list stays in effect from
1161 one tstart to the next. */
1163 static struct readonly_region
*readonly_regions
;
1167 /* The global that controls tracing overall. */
1169 IP_AGENT_EXPORT
int tracing
;
1171 #ifndef IN_PROCESS_AGENT
1173 /* Controls whether tracing should continue after GDB disconnects. */
1175 int disconnected_tracing
;
1177 /* The reason for the last tracing run to have stopped. We initialize
1178 to a distinct string so that GDB can distinguish between "stopped
1179 after running" and "stopped because never run" cases. */
1181 static const char *tracing_stop_reason
= "tnotrun";
1183 static int tracing_stop_tpnum
;
1185 /* 64-bit timestamps for the trace run's start and finish, expressed
1186 in microseconds from the Unix epoch. */
1188 LONGEST tracing_start_time
;
1189 LONGEST tracing_stop_time
;
1191 /* The (optional) user-supplied name of the user that started the run.
1192 This is an arbitrary string, and may be NULL. */
1194 char *tracing_user_name
;
1196 /* Optional user-supplied text describing the run. This is
1197 an arbitrary string, and may be NULL. */
1199 char *tracing_notes
;
1201 /* Optional user-supplied text explaining a tstop command. This is an
1202 arbitrary string, and may be NULL. */
1204 char *tracing_stop_note
;
1208 /* Functions local to this file. */
1210 /* Base "class" for tracepoint type specific data to be passed down to
1211 collect_data_at_tracepoint. */
1212 struct tracepoint_hit_ctx
1214 enum tracepoint_type type
;
1217 #ifdef IN_PROCESS_AGENT
1219 /* Fast/jump tracepoint specific data to be passed down to
1220 collect_data_at_tracepoint. */
1221 struct fast_tracepoint_ctx
1223 struct tracepoint_hit_ctx base
;
1225 struct regcache regcache
;
1226 int regcache_initted
;
1227 unsigned char *regspace
;
1229 unsigned char *regs
;
1230 struct tracepoint
*tpoint
;
1233 /* Static tracepoint specific data to be passed down to
1234 collect_data_at_tracepoint. */
1235 struct static_tracepoint_ctx
1237 struct tracepoint_hit_ctx base
;
1239 /* The regcache corresponding to the registers state at the time of
1240 the tracepoint hit. Initialized lazily, from REGS. */
1241 struct regcache regcache
;
1242 int regcache_initted
;
1244 /* The buffer space REGCACHE above uses. We use a separate buffer
1245 instead of letting the regcache malloc for both signal safety and
1246 performance reasons; this is allocated on the stack instead. */
1247 unsigned char *regspace
;
1249 /* The register buffer as passed on by lttng/ust. */
1250 struct registers
*regs
;
1252 /* The "printf" formatter and the args the user passed to the marker
1253 call. We use this to be able to collect "static trace data"
1258 /* The GDB tracepoint matching the probed marker that was "hit". */
1259 struct tracepoint
*tpoint
;
1264 /* Static tracepoint specific data to be passed down to
1265 collect_data_at_tracepoint. */
1266 struct trap_tracepoint_ctx
1268 struct tracepoint_hit_ctx base
;
1270 struct regcache
*regcache
;
1275 static enum eval_result_type
1276 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1277 struct traceframe
*tframe
,
1278 struct agent_expr
*aexpr
,
1281 #ifndef IN_PROCESS_AGENT
1282 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1283 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1286 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1287 struct tracepoint
*tpoint
);
1289 #ifndef IN_PROCESS_AGENT
1290 static void clear_readonly_regions (void);
1291 static void clear_installed_tracepoints (void);
1294 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1296 struct tracepoint
*tpoint
);
1297 #ifndef IN_PROCESS_AGENT
1298 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1300 struct tracepoint
*tpoint
, int current_step
);
1301 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1302 CORE_ADDR
*jump_entry
);
1304 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1306 struct tracepoint
*tpoint
,
1307 struct traceframe
*tframe
,
1308 struct tracepoint_action
*taction
);
1310 #ifndef IN_PROCESS_AGENT
1311 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1313 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1314 static void download_tracepoint (struct tracepoint
*);
1315 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1316 static void clone_fast_tracepoint (struct tracepoint
*to
,
1317 const struct tracepoint
*from
);
1320 static LONGEST
get_timestamp (void);
1322 #if defined(__GNUC__)
1323 # define memory_barrier() asm volatile ("" : : : "memory")
1325 # define memory_barrier() do {} while (0)
1328 /* We only build the IPA if this builtin is supported, and there are
1329 no uses of this in GDBserver itself, so we're safe in defining this
1331 #define cmpxchg(mem, oldval, newval) \
1332 __sync_val_compare_and_swap (mem, oldval, newval)
1334 /* Record that an error occurred during expression evaluation. */
1337 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1338 enum eval_result_type rtype
)
1340 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1341 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1343 #ifdef IN_PROCESS_AGENT
1344 /* Only record the first error we get. */
1345 if (cmpxchg (&expr_eval_result
,
1347 rtype
) != expr_eval_no_error
)
1350 if (expr_eval_result
!= expr_eval_no_error
)
1354 error_tracepoint
= tpoint
;
1357 /* Trace buffer management. */
1360 clear_trace_buffer (void)
1362 trace_buffer_start
= trace_buffer_lo
;
1363 trace_buffer_free
= trace_buffer_lo
;
1364 trace_buffer_end_free
= trace_buffer_hi
;
1365 trace_buffer_wrap
= trace_buffer_hi
;
1366 /* A traceframe with zeroed fields marks the end of trace data. */
1367 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1368 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1369 traceframe_read_count
= traceframe_write_count
= 0;
1370 traceframes_created
= 0;
1373 #ifndef IN_PROCESS_AGENT
1376 clear_inferior_trace_buffer (void)
1378 CORE_ADDR ipa_trace_buffer_lo
;
1379 CORE_ADDR ipa_trace_buffer_hi
;
1380 struct traceframe ipa_traceframe
= { 0 };
1381 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1383 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1384 &ipa_trace_buffer_lo
);
1385 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1386 &ipa_trace_buffer_hi
);
1388 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1389 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1390 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1391 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1393 /* A traceframe with zeroed fields marks the end of trace data. */
1394 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1395 (unsigned char *) &ipa_trace_buffer_ctrl
,
1396 sizeof (ipa_trace_buffer_ctrl
));
1398 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1400 /* A traceframe with zeroed fields marks the end of trace data. */
1401 write_inferior_memory (ipa_trace_buffer_lo
,
1402 (unsigned char *) &ipa_traceframe
,
1403 sizeof (ipa_traceframe
));
1405 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1406 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1407 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1413 init_trace_buffer (unsigned char *buf
, int bufsize
)
1415 trace_buffer_lo
= buf
;
1416 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1418 clear_trace_buffer ();
1421 #ifdef IN_PROCESS_AGENT
1423 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1424 about_to_request_buffer_space (void)
1426 /* GDBserver places breakpoint here while it goes about to flush
1427 data at random times. */
1428 UNKNOWN_SIDE_EFFECTS();
1433 /* Carve out a piece of the trace buffer, returning NULL in case of
1437 trace_buffer_alloc (size_t amt
)
1439 unsigned char *rslt
;
1440 struct trace_buffer_control
*tbctrl
;
1442 #ifdef IN_PROCESS_AGENT
1443 unsigned int prev
, prev_filtered
;
1444 unsigned int commit_count
;
1445 unsigned int commit
;
1446 unsigned int readout
;
1448 struct traceframe
*oldest
;
1449 unsigned char *new_start
;
1452 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1453 (long) amt
, (long) sizeof (struct traceframe
));
1455 /* Account for the EOB marker. */
1456 amt
+= sizeof (struct traceframe
);
1458 #ifdef IN_PROCESS_AGENT
1462 /* Read the current token and extract the index to try to write to,
1463 storing it in CURR. */
1464 prev
= trace_buffer_ctrl_curr
;
1465 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1466 curr
= prev_filtered
+ 1;
1470 about_to_request_buffer_space ();
1472 /* Start out with a copy of the current state. GDBserver may be
1473 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1474 be able to commit anyway if that happens. */
1475 trace_buffer_ctrl
[curr
]
1476 = trace_buffer_ctrl
[prev_filtered
];
1477 trace_debug ("trying curr=%u", curr
);
1479 /* The GDBserver's agent doesn't need all that syncing, and always
1480 updates TCB 0 (there's only one, mind you). */
1483 tbctrl
= &trace_buffer_ctrl
[curr
];
1485 /* Offsets are easier to grok for debugging than raw addresses,
1486 especially for the small trace buffer sizes that are useful for
1488 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1490 (int) (tbctrl
->start
- trace_buffer_lo
),
1491 (int) (tbctrl
->free
- trace_buffer_lo
),
1492 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1493 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1494 (int) (trace_buffer_hi
- trace_buffer_lo
));
1496 /* The algorithm here is to keep trying to get a contiguous block of
1497 the requested size, possibly discarding older traceframes to free
1498 up space. Since free space might come in one or two pieces,
1499 depending on whether discarded traceframes wrapped around at the
1500 high end of the buffer, we test both pieces after each
1504 /* First, if we have two free parts, try the upper one first. */
1505 if (tbctrl
->end_free
< tbctrl
->free
)
1507 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1508 /* We have enough in the upper part. */
1512 /* Our high part of free space wasn't enough. Give up
1513 on it for now, set wraparound. We will recover the
1514 space later, if/when the wrapped-around traceframe is
1516 trace_debug ("Upper part too small, setting wraparound");
1517 tbctrl
->wrap
= tbctrl
->free
;
1518 tbctrl
->free
= trace_buffer_lo
;
1522 /* The normal case. */
1523 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1526 #ifdef IN_PROCESS_AGENT
1527 /* The IP Agent's buffer is always circular. It isn't used
1528 currently, but `circular_trace_buffer' could represent
1529 GDBserver's mode. If we didn't find space, ask GDBserver to
1532 flush_trace_buffer ();
1536 trace_debug ("gdbserver flushed buffer, retrying");
1540 /* GDBserver cancelled the tracing. Bail out as well. */
1543 /* If we're here, then neither part is big enough, and
1544 non-circular trace buffers are now full. */
1545 if (!circular_trace_buffer
)
1547 trace_debug ("Not enough space in the trace buffer");
1551 trace_debug ("Need more space in the trace buffer");
1553 /* If we have a circular buffer, we can try discarding the
1554 oldest traceframe and see if that helps. */
1555 oldest
= FIRST_TRACEFRAME ();
1556 if (oldest
->tpnum
== 0)
1558 /* Not good; we have no traceframes to free. Perhaps we're
1559 asking for a block that is larger than the buffer? In
1560 any case, give up. */
1561 trace_debug ("No traceframes to discard");
1565 /* We don't run this code in the in-process agent currently.
1566 E.g., we could leave the in-process agent in autonomous
1567 circular mode if we only have fast tracepoints. If we do
1568 that, then this bit becomes racy with GDBserver, which also
1569 writes to this counter. */
1570 --traceframe_write_count
;
1572 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1573 /* If we freed the traceframe that wrapped around, go back
1574 to the non-wrap case. */
1575 if (new_start
< tbctrl
->start
)
1577 trace_debug ("Discarding past the wraparound");
1578 tbctrl
->wrap
= trace_buffer_hi
;
1580 tbctrl
->start
= new_start
;
1581 tbctrl
->end_free
= tbctrl
->start
;
1583 trace_debug ("Discarded a traceframe\n"
1584 "Trace buffer [%d], start=%d free=%d "
1585 "endfree=%d wrap=%d hi=%d",
1587 (int) (tbctrl
->start
- trace_buffer_lo
),
1588 (int) (tbctrl
->free
- trace_buffer_lo
),
1589 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1590 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1591 (int) (trace_buffer_hi
- trace_buffer_lo
));
1593 /* Now go back around the loop. The discard might have resulted
1594 in either one or two pieces of free space, so we want to try
1595 both before freeing any more traceframes. */
1599 /* If we get here, we know we can provide the asked-for space. */
1601 rslt
= tbctrl
->free
;
1603 /* Adjust the request back down, now that we know we have space for
1604 the marker, but don't commit to AMT yet, we may still need to
1605 restart the operation if GDBserver touches the trace buffer
1606 (obviously only important in the in-process agent's version). */
1607 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1609 /* Or not. If GDBserver changed the trace buffer behind our back,
1610 we get to restart a new allocation attempt. */
1612 #ifdef IN_PROCESS_AGENT
1613 /* Build the tentative token. */
1614 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1615 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1616 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1620 /* Try to commit it. */
1621 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1622 if (readout
!= prev
)
1624 trace_debug ("GDBserver has touched the trace buffer, restarting."
1625 " (prev=%08x, commit=%08x, readout=%08x)",
1626 prev
, commit
, readout
);
1630 /* Hold your horses here. Even if that change was committed,
1631 GDBserver could come in, and clobber it. We need to hold to be
1632 able to tell if GDBserver clobbers before or after we committed
1633 the change. Whenever GDBserver goes about touching the IPA
1634 buffer, it sets a breakpoint in this routine, so we have a sync
1636 about_to_request_buffer_space ();
1638 /* Check if the change has been effective, even if GDBserver stopped
1639 us at the breakpoint. */
1642 unsigned int refetch
;
1646 refetch
= trace_buffer_ctrl_curr
;
1648 if (refetch
== commit
1649 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1652 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1653 "readout=%08x, refetch=%08x)",
1654 prev
, commit
, readout
, refetch
);
1658 trace_debug ("GDBserver has touched the trace buffer, not effective."
1659 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1660 prev
, commit
, readout
, refetch
);
1666 /* We have a new piece of the trace buffer. Hurray! */
1668 /* Add an EOB marker just past this allocation. */
1669 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1670 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1672 /* Adjust the request back down, now that we know we have space for
1674 amt
-= sizeof (struct traceframe
);
1678 trace_debug ("Allocated %d bytes", (int) amt
);
1679 trace_debug ("Trace buffer [%d] start=%d free=%d "
1680 "endfree=%d wrap=%d hi=%d",
1682 (int) (tbctrl
->start
- trace_buffer_lo
),
1683 (int) (tbctrl
->free
- trace_buffer_lo
),
1684 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1685 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1686 (int) (trace_buffer_hi
- trace_buffer_lo
));
1692 #ifndef IN_PROCESS_AGENT
1694 /* Return the total free space. This is not necessarily the largest
1695 block we can allocate, because of the two-part case. */
1700 if (trace_buffer_free
<= trace_buffer_end_free
)
1701 return trace_buffer_end_free
- trace_buffer_free
;
1703 return ((trace_buffer_end_free
- trace_buffer_lo
)
1704 + (trace_buffer_hi
- trace_buffer_free
));
1707 /* An 'S' in continuation packets indicates remainder are for
1710 static int seen_step_action_flag
;
1712 /* Create a tracepoint (location) with given number and address. Add this
1713 new tracepoint to list and sort this list. */
1715 static struct tracepoint
*
1716 add_tracepoint (int num
, CORE_ADDR addr
)
1718 struct tracepoint
*tpoint
, **tp_next
;
1720 tpoint
= xmalloc (sizeof (struct tracepoint
));
1721 tpoint
->number
= num
;
1722 tpoint
->address
= addr
;
1723 tpoint
->numactions
= 0;
1724 tpoint
->actions
= NULL
;
1725 tpoint
->actions_str
= NULL
;
1726 tpoint
->cond
= NULL
;
1727 tpoint
->num_step_actions
= 0;
1728 tpoint
->step_actions
= NULL
;
1729 tpoint
->step_actions_str
= NULL
;
1730 /* Start all off as regular (slow) tracepoints. */
1731 tpoint
->type
= trap_tracepoint
;
1732 tpoint
->orig_size
= -1;
1733 tpoint
->source_strings
= NULL
;
1734 tpoint
->compiled_cond
= 0;
1735 tpoint
->handle
= NULL
;
1736 tpoint
->next
= NULL
;
1738 /* Find a place to insert this tracepoint into list in order to keep
1739 the tracepoint list still in the ascending order. There may be
1740 multiple tracepoints at the same address as TPOINT's, and this
1741 guarantees TPOINT is inserted after all the tracepoints which are
1742 set at the same address. For example, fast tracepoints A, B, C are
1743 set at the same address, and D is to be insert at the same place as
1746 -->| A |--> | B |-->| C |->...
1748 One jump pad was created for tracepoint A, B, and C, and the target
1749 address of A is referenced/used in jump pad. So jump pad will let
1750 inferior jump to A. If D is inserted in front of A, like this,
1752 -->| D |-->| A |--> | B |-->| C |->...
1754 without updating jump pad, D is not reachable during collect, which
1755 is wrong. As we can see, the order of B, C and D doesn't matter, but
1756 A should always be the `first' one. */
1757 for (tp_next
= &tracepoints
;
1758 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1759 tp_next
= &(*tp_next
)->next
)
1761 tpoint
->next
= *tp_next
;
1763 last_tracepoint
= tpoint
;
1765 seen_step_action_flag
= 0;
1770 #ifndef IN_PROCESS_AGENT
1772 /* Return the tracepoint with the given number and address, or NULL. */
1774 static struct tracepoint
*
1775 find_tracepoint (int id
, CORE_ADDR addr
)
1777 struct tracepoint
*tpoint
;
1779 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1780 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1786 /* Remove TPOINT from global list. */
1789 remove_tracepoint (struct tracepoint
*tpoint
)
1791 struct tracepoint
*tp
, *tp_prev
;
1793 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1794 tp_prev
= tp
, tp
= tp
->next
)
1800 tp_prev
->next
= tp
->next
;
1802 tracepoints
= tp
->next
;
1808 /* There may be several tracepoints with the same number (because they
1809 are "locations", in GDB parlance); return the next one after the
1810 given tracepoint, or search from the beginning of the list if the
1811 first argument is NULL. */
1813 static struct tracepoint
*
1814 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1816 struct tracepoint
*tpoint
;
1819 tpoint
= prev_tp
->next
;
1821 tpoint
= tracepoints
;
1822 for (; tpoint
; tpoint
= tpoint
->next
)
1823 if (tpoint
->number
== num
)
1832 save_string (const char *str
, size_t len
)
1836 s
= xmalloc (len
+ 1);
1837 memcpy (s
, str
, len
);
1843 /* Append another action to perform when the tracepoint triggers. */
1846 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1852 seen_step_action_flag
= 1;
1860 char *act_start
= act
;
1861 struct tracepoint_action
*action
= NULL
;
1867 struct collect_memory_action
*maction
;
1871 maction
= xmalloc (sizeof *maction
);
1872 maction
->base
.type
= *act
;
1873 maction
->base
.ops
= &m_tracepoint_action_ops
;
1874 action
= &maction
->base
;
1877 is_neg
= (*act
== '-');
1880 act
= unpack_varlen_hex (act
, &basereg
);
1882 act
= unpack_varlen_hex (act
, &maction
->addr
);
1884 act
= unpack_varlen_hex (act
, &maction
->len
);
1885 maction
->basereg
= (is_neg
1888 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1889 pulongest (maction
->len
),
1890 paddress (maction
->addr
), maction
->basereg
);
1895 struct collect_registers_action
*raction
;
1897 raction
= xmalloc (sizeof *raction
);
1898 raction
->base
.type
= *act
;
1899 raction
->base
.ops
= &r_tracepoint_action_ops
;
1900 action
= &raction
->base
;
1902 trace_debug ("Want to collect registers");
1904 /* skip past hex digits of mask for now */
1905 while (isxdigit(*act
))
1911 struct collect_static_trace_data_action
*raction
;
1913 raction
= xmalloc (sizeof *raction
);
1914 raction
->base
.type
= *act
;
1915 raction
->base
.ops
= &l_tracepoint_action_ops
;
1916 action
= &raction
->base
;
1918 trace_debug ("Want to collect static trace data");
1923 trace_debug ("Unexpected step action, ignoring");
1928 struct eval_expr_action
*xaction
;
1930 xaction
= xmalloc (sizeof (*xaction
));
1931 xaction
->base
.type
= *act
;
1932 xaction
->base
.ops
= &x_tracepoint_action_ops
;
1933 action
= &xaction
->base
;
1935 trace_debug ("Want to evaluate expression");
1936 xaction
->expr
= gdb_parse_agent_expr (&act
);
1940 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1949 if (seen_step_action_flag
)
1951 tpoint
->num_step_actions
++;
1953 tpoint
->step_actions
1954 = xrealloc (tpoint
->step_actions
,
1955 (sizeof (*tpoint
->step_actions
)
1956 * tpoint
->num_step_actions
));
1957 tpoint
->step_actions_str
1958 = xrealloc (tpoint
->step_actions_str
,
1959 (sizeof (*tpoint
->step_actions_str
)
1960 * tpoint
->num_step_actions
));
1961 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1962 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1963 = save_string (act_start
, act
- act_start
);
1967 tpoint
->numactions
++;
1969 = xrealloc (tpoint
->actions
,
1970 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1972 = xrealloc (tpoint
->actions_str
,
1973 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1974 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1975 tpoint
->actions_str
[tpoint
->numactions
- 1]
1976 = save_string (act_start
, act
- act_start
);
1983 /* Find or create a trace state variable with the given number. */
1985 static struct trace_state_variable
*
1986 get_trace_state_variable (int num
)
1988 struct trace_state_variable
*tsv
;
1990 #ifdef IN_PROCESS_AGENT
1991 /* Search for an existing variable. */
1992 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1993 if (tsv
->number
== num
)
1997 /* Search for an existing variable. */
1998 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1999 if (tsv
->number
== num
)
2005 /* Find or create a trace state variable with the given number. */
2007 static struct trace_state_variable
*
2008 create_trace_state_variable (int num
, int gdb
)
2010 struct trace_state_variable
*tsv
;
2012 tsv
= get_trace_state_variable (num
);
2016 /* Create a new variable. */
2017 tsv
= xmalloc (sizeof (struct trace_state_variable
));
2019 tsv
->initial_value
= 0;
2023 #ifdef IN_PROCESS_AGENT
2026 tsv
->next
= alloced_trace_state_variables
;
2027 alloced_trace_state_variables
= tsv
;
2032 tsv
->next
= trace_state_variables
;
2033 trace_state_variables
= tsv
;
2038 IP_AGENT_EXPORT LONGEST
2039 get_trace_state_variable_value (int num
)
2041 struct trace_state_variable
*tsv
;
2043 tsv
= get_trace_state_variable (num
);
2047 trace_debug ("No trace state variable %d, skipping value get", num
);
2051 /* Call a getter function if we have one. While it's tempting to
2052 set up something to only call the getter once per tracepoint hit,
2053 it could run afoul of thread races. Better to let the getter
2054 handle it directly, if necessary to worry about it. */
2056 tsv
->value
= (tsv
->getter
) ();
2058 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2059 num
, plongest (tsv
->value
));
2064 IP_AGENT_EXPORT
void
2065 set_trace_state_variable_value (int num
, LONGEST val
)
2067 struct trace_state_variable
*tsv
;
2069 tsv
= get_trace_state_variable (num
);
2073 trace_debug ("No trace state variable %d, skipping value set", num
);
2081 agent_get_trace_state_variable_value (int num
)
2083 return get_trace_state_variable_value (num
);
2087 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2089 set_trace_state_variable_value (num
, val
);
2093 set_trace_state_variable_name (int num
, const char *name
)
2095 struct trace_state_variable
*tsv
;
2097 tsv
= get_trace_state_variable (num
);
2101 trace_debug ("No trace state variable %d, skipping name set", num
);
2105 tsv
->name
= (char *) name
;
2109 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2111 struct trace_state_variable
*tsv
;
2113 tsv
= get_trace_state_variable (num
);
2117 trace_debug ("No trace state variable %d, skipping getter set", num
);
2121 tsv
->getter
= getter
;
2124 /* Add a raw traceframe for the given tracepoint. */
2126 static struct traceframe
*
2127 add_traceframe (struct tracepoint
*tpoint
)
2129 struct traceframe
*tframe
;
2131 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2136 tframe
->tpnum
= tpoint
->number
;
2137 tframe
->data_size
= 0;
2142 /* Add a block to the traceframe currently being worked on. */
2144 static unsigned char *
2145 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2147 unsigned char *block
;
2152 block
= trace_buffer_alloc (amt
);
2157 tframe
->data_size
+= amt
;
2162 /* Flag that the current traceframe is finished. */
2165 finish_traceframe (struct traceframe
*tframe
)
2167 ++traceframe_write_count
;
2168 ++traceframes_created
;
2171 #ifndef IN_PROCESS_AGENT
2173 /* Given a traceframe number NUM, find the NUMth traceframe in the
2176 static struct traceframe
*
2177 find_traceframe (int num
)
2179 struct traceframe
*tframe
;
2182 for (tframe
= FIRST_TRACEFRAME ();
2184 tframe
= NEXT_TRACEFRAME (tframe
))
2195 get_traceframe_address (struct traceframe
*tframe
)
2198 struct tracepoint
*tpoint
;
2200 addr
= traceframe_get_pc (tframe
);
2205 /* Fallback strategy, will be incorrect for while-stepping frames
2206 and multi-location tracepoints. */
2207 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2208 return tpoint
->address
;
2211 /* Search for the next traceframe whose address is inside or outside
2214 static struct traceframe
*
2215 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2218 struct traceframe
*tframe
;
2221 *tfnump
= current_traceframe
+ 1;
2222 tframe
= find_traceframe (*tfnump
);
2223 /* The search is not supposed to wrap around. */
2230 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2232 tfaddr
= get_traceframe_address (tframe
);
2234 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2235 : (lo
> tfaddr
|| tfaddr
> hi
))
2244 /* Search for the next traceframe recorded by the given tracepoint.
2245 Note that for multi-location tracepoints, this will find whatever
2246 location appears first. */
2248 static struct traceframe
*
2249 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2251 struct traceframe
*tframe
;
2253 *tfnump
= current_traceframe
+ 1;
2254 tframe
= find_traceframe (*tfnump
);
2255 /* The search is not supposed to wrap around. */
2262 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2264 if (tframe
->tpnum
== num
)
2275 #ifndef IN_PROCESS_AGENT
2277 /* Clear all past trace state. */
2280 cmd_qtinit (char *packet
)
2282 struct trace_state_variable
*tsv
, *prev
, *next
;
2284 /* Make sure we don't try to read from a trace frame. */
2285 current_traceframe
= -1;
2287 trace_debug ("Initializing the trace");
2289 clear_installed_tracepoints ();
2290 clear_readonly_regions ();
2293 last_tracepoint
= NULL
;
2295 /* Clear out any leftover trace state variables. Ones with target
2296 defined getters should be kept however. */
2298 tsv
= trace_state_variables
;
2301 trace_debug ("Looking at var %d", tsv
->number
);
2302 if (tsv
->getter
== NULL
)
2308 trace_state_variables
= next
;
2309 trace_debug ("Deleting var %d", tsv
->number
);
2320 clear_trace_buffer ();
2321 clear_inferior_trace_buffer ();
2326 /* Unprobe the UST marker at ADDRESS. */
2329 unprobe_marker_at (CORE_ADDR address
)
2331 char cmd
[IPA_CMD_BUF_SIZE
];
2333 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2334 run_inferior_command (cmd
);
2337 /* Restore the program to its pre-tracing state. This routine may be called
2338 in error situations, so it needs to be careful about only restoring
2339 from known-valid bits. */
2342 clear_installed_tracepoints (void)
2344 struct tracepoint
*tpoint
;
2345 struct tracepoint
*prev_stpoint
;
2348 cancel_breakpoints ();
2350 prev_stpoint
= NULL
;
2352 /* Restore any bytes overwritten by tracepoints. */
2353 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2355 /* Catch the case where we might try to remove a tracepoint that
2356 was never actually installed. */
2357 if (tpoint
->handle
== NULL
)
2359 trace_debug ("Tracepoint %d at 0x%s was "
2360 "never installed, nothing to clear",
2361 tpoint
->number
, paddress (tpoint
->address
));
2365 switch (tpoint
->type
)
2367 case trap_tracepoint
:
2368 delete_breakpoint (tpoint
->handle
);
2370 case fast_tracepoint
:
2371 delete_fast_tracepoint_jump (tpoint
->handle
);
2373 case static_tracepoint
:
2374 if (prev_stpoint
!= NULL
2375 && prev_stpoint
->address
== tpoint
->address
)
2376 /* Nothing to do. We already unprobed a tracepoint set at
2377 this marker address (and there can only be one probe
2382 unprobe_marker_at (tpoint
->address
);
2383 prev_stpoint
= tpoint
;
2388 tpoint
->handle
= NULL
;
2394 /* Parse a packet that defines a tracepoint. */
2397 cmd_qtdp (char *own_buf
)
2400 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2401 int trail_hyphen
= 0;
2405 struct tracepoint
*tpoint
;
2407 char *packet
= own_buf
;
2409 packet
+= strlen ("QTDP:");
2411 /* A hyphen at the beginning marks a packet specifying actions for a
2412 tracepoint already supplied. */
2419 packet
= unpack_varlen_hex (packet
, &num
);
2420 ++packet
; /* skip a colon */
2421 packet
= unpack_varlen_hex (packet
, &addr
);
2422 ++packet
; /* skip a colon */
2424 /* See if we already have this tracepoint. */
2425 tpoint
= find_tracepoint (num
, addr
);
2429 /* Duplicate tracepoints are never allowed. */
2432 trace_debug ("Tracepoint error: tracepoint %d"
2433 " at 0x%s already exists",
2434 (int) num
, paddress (addr
));
2435 write_enn (own_buf
);
2439 tpoint
= add_tracepoint (num
, addr
);
2441 tpoint
->enabled
= (*packet
== 'E');
2442 ++packet
; /* skip 'E' */
2443 ++packet
; /* skip a colon */
2444 packet
= unpack_varlen_hex (packet
, &count
);
2445 tpoint
->step_count
= count
;
2446 ++packet
; /* skip a colon */
2447 packet
= unpack_varlen_hex (packet
, &count
);
2448 tpoint
->pass_count
= count
;
2449 /* See if we have any of the additional optional fields. */
2450 while (*packet
== ':')
2455 tpoint
->type
= fast_tracepoint
;
2457 packet
= unpack_varlen_hex (packet
, &count
);
2458 tpoint
->orig_size
= count
;
2460 else if (*packet
== 'S')
2462 tpoint
->type
= static_tracepoint
;
2465 else if (*packet
== 'X')
2467 actparm
= (char *) packet
;
2468 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2471 else if (*packet
== '-')
2473 else if (*packet
== '\0')
2476 trace_debug ("Unknown optional tracepoint field");
2481 trace_debug ("Also has actions\n");
2484 trace_debug ("Defined %stracepoint %d at 0x%s, "
2485 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2486 tpoint
->type
== fast_tracepoint
? "fast "
2487 : tpoint
->type
== static_tracepoint
? "static " : "",
2488 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2489 tpoint
->step_count
, tpoint
->pass_count
);
2492 add_tracepoint_action (tpoint
, packet
);
2495 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2496 (int) num
, paddress (addr
));
2497 write_enn (own_buf
);
2501 /* Install tracepoint during tracing only once for each tracepoint location.
2502 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2503 determine the last QTDP packet for one tracepoint location by checking
2504 trailing hyphen in QTDP packet. */
2505 if (tracing
&& !trail_hyphen
)
2507 struct tracepoint
*tp
= NULL
;
2509 /* Pause all threads temporarily while we patch tracepoints. */
2512 /* download_tracepoint will update global `tracepoints'
2513 list, so it is unsafe to leave threads in jump pad. */
2514 stabilize_threads ();
2516 /* Freeze threads. */
2520 if (tpoint
->type
!= trap_tracepoint
)
2522 /* Find another fast or static tracepoint at the same address. */
2523 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2525 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2526 && tp
->number
!= tpoint
->number
)
2530 /* TPOINT is installed at the same address as TP. */
2533 if (tpoint
->type
== fast_tracepoint
)
2534 clone_fast_tracepoint (tpoint
, tp
);
2535 else if (tpoint
->type
== static_tracepoint
)
2536 tpoint
->handle
= (void *) -1;
2540 download_tracepoint (tpoint
);
2542 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2544 install_tracepoint (tpoint
, own_buf
);
2545 if (strcmp (own_buf
, "OK") != 0)
2546 remove_tracepoint (tpoint
);
2559 cmd_qtdpsrc (char *own_buf
)
2561 ULONGEST num
, addr
, start
, slen
;
2562 struct tracepoint
*tpoint
;
2563 char *packet
= own_buf
;
2564 char *saved
, *srctype
, *src
;
2566 struct source_string
*last
, *newlast
;
2568 packet
+= strlen ("QTDPsrc:");
2570 packet
= unpack_varlen_hex (packet
, &num
);
2571 ++packet
; /* skip a colon */
2572 packet
= unpack_varlen_hex (packet
, &addr
);
2573 ++packet
; /* skip a colon */
2575 /* See if we already have this tracepoint. */
2576 tpoint
= find_tracepoint (num
, addr
);
2580 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2581 (int) num
, paddress (addr
));
2582 write_enn (own_buf
);
2587 packet
= strchr (packet
, ':');
2588 srctype
= xmalloc (packet
- saved
+ 1);
2589 memcpy (srctype
, saved
, packet
- saved
);
2590 srctype
[packet
- saved
] = '\0';
2592 packet
= unpack_varlen_hex (packet
, &start
);
2593 ++packet
; /* skip a colon */
2594 packet
= unpack_varlen_hex (packet
, &slen
);
2595 ++packet
; /* skip a colon */
2596 src
= xmalloc (slen
+ 1);
2597 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2600 newlast
= xmalloc (sizeof (struct source_string
));
2601 newlast
->type
= srctype
;
2603 newlast
->next
= NULL
;
2604 /* Always add a source string to the end of the list;
2605 this keeps sequences of actions/commands in the right
2607 if (tpoint
->source_strings
)
2609 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2611 last
->next
= newlast
;
2614 tpoint
->source_strings
= newlast
;
2620 cmd_qtdv (char *own_buf
)
2622 ULONGEST num
, val
, builtin
;
2625 struct trace_state_variable
*tsv
;
2626 char *packet
= own_buf
;
2628 packet
+= strlen ("QTDV:");
2630 packet
= unpack_varlen_hex (packet
, &num
);
2631 ++packet
; /* skip a colon */
2632 packet
= unpack_varlen_hex (packet
, &val
);
2633 ++packet
; /* skip a colon */
2634 packet
= unpack_varlen_hex (packet
, &builtin
);
2635 ++packet
; /* skip a colon */
2637 nbytes
= strlen (packet
) / 2;
2638 varname
= xmalloc (nbytes
+ 1);
2639 nbytes
= unhexify (varname
, packet
, nbytes
);
2640 varname
[nbytes
] = '\0';
2642 tsv
= create_trace_state_variable (num
, 1);
2643 tsv
->initial_value
= (LONGEST
) val
;
2644 tsv
->name
= varname
;
2646 set_trace_state_variable_value (num
, (LONGEST
) val
);
2652 cmd_qtenable_disable (char *own_buf
, int enable
)
2654 char *packet
= own_buf
;
2656 struct tracepoint
*tp
;
2658 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2659 packet
= unpack_varlen_hex (packet
, &num
);
2660 ++packet
; /* skip a colon */
2661 packet
= unpack_varlen_hex (packet
, &addr
);
2663 tp
= find_tracepoint (num
, addr
);
2667 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2669 trace_debug ("Tracepoint %d at 0x%s is already %s",
2670 (int) num
, paddress (addr
),
2671 enable
? "enabled" : "disabled");
2676 trace_debug ("%s tracepoint %d at 0x%s",
2677 enable
? "Enabling" : "Disabling",
2678 (int) num
, paddress (addr
));
2680 tp
->enabled
= enable
;
2682 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2685 int offset
= offsetof (struct tracepoint
, enabled
);
2686 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2688 ret
= prepare_to_access_memory ();
2691 trace_debug ("Failed to temporarily stop inferior threads");
2692 write_enn (own_buf
);
2696 ret
= write_inferior_integer (obj_addr
, enable
);
2697 done_accessing_memory ();
2701 trace_debug ("Cannot write enabled flag into "
2702 "inferior process memory");
2703 write_enn (own_buf
);
2712 trace_debug ("Tracepoint %d at 0x%s not found",
2713 (int) num
, paddress (addr
));
2714 write_enn (own_buf
);
2719 cmd_qtv (char *own_buf
)
2724 char *packet
= own_buf
;
2726 packet
+= strlen ("qTV:");
2727 unpack_varlen_hex (packet
, &num
);
2729 if (current_traceframe
>= 0)
2731 err
= traceframe_read_tsv ((int) num
, &val
);
2734 strcpy (own_buf
, "U");
2738 /* Only make tsv's be undefined before the first trace run. After a
2739 trace run is over, the user might want to see the last value of
2740 the tsv, and it might not be available in a traceframe. */
2741 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2743 strcpy (own_buf
, "U");
2747 val
= get_trace_state_variable_value (num
);
2749 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2752 /* Clear out the list of readonly regions. */
2755 clear_readonly_regions (void)
2757 struct readonly_region
*roreg
;
2759 while (readonly_regions
)
2761 roreg
= readonly_regions
;
2762 readonly_regions
= readonly_regions
->next
;
2767 /* Parse the collection of address ranges whose contents GDB believes
2768 to be unchanging and so can be read directly from target memory
2769 even while looking at a traceframe. */
2772 cmd_qtro (char *own_buf
)
2774 ULONGEST start
, end
;
2775 struct readonly_region
*roreg
;
2776 char *packet
= own_buf
;
2778 trace_debug ("Want to mark readonly regions");
2780 clear_readonly_regions ();
2782 packet
+= strlen ("QTro");
2784 while (*packet
== ':')
2786 ++packet
; /* skip a colon */
2787 packet
= unpack_varlen_hex (packet
, &start
);
2788 ++packet
; /* skip a comma */
2789 packet
= unpack_varlen_hex (packet
, &end
);
2790 roreg
= xmalloc (sizeof (struct readonly_region
));
2791 roreg
->start
= start
;
2793 roreg
->next
= readonly_regions
;
2794 readonly_regions
= roreg
;
2795 trace_debug ("Added readonly region from 0x%s to 0x%s",
2796 paddress (roreg
->start
), paddress (roreg
->end
));
2802 /* Test to see if the given range is in our list of readonly ranges.
2803 We only test for being entirely within a range, GDB is not going to
2804 send a single memory packet that spans multiple regions. */
2807 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2809 struct readonly_region
*roreg
;
2811 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2812 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2818 /* The maximum size of a jump pad entry. */
2819 static const int max_jump_pad_size
= 0x100;
2821 static CORE_ADDR gdb_jump_pad_head
;
2823 /* Return the address of the next free jump space. */
2826 get_jump_space_head (void)
2828 if (gdb_jump_pad_head
== 0)
2830 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2831 &gdb_jump_pad_head
))
2832 fatal ("error extracting jump_pad_buffer");
2835 return gdb_jump_pad_head
;
2838 /* Reserve USED bytes from the jump space. */
2841 claim_jump_space (ULONGEST used
)
2843 trace_debug ("claim_jump_space reserves %s bytes at %s",
2844 pulongest (used
), paddress (gdb_jump_pad_head
));
2845 gdb_jump_pad_head
+= used
;
2848 static CORE_ADDR trampoline_buffer_head
= 0;
2849 static CORE_ADDR trampoline_buffer_tail
;
2851 /* Reserve USED bytes from the trampoline buffer and return the
2852 address of the start of the reserved space in TRAMPOLINE. Returns
2853 non-zero if the space is successfully claimed. */
2856 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2858 if (!trampoline_buffer_head
)
2860 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2861 &trampoline_buffer_tail
))
2863 fatal ("error extracting trampoline_buffer");
2867 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2868 &trampoline_buffer_head
))
2870 fatal ("error extracting trampoline_buffer_end");
2875 /* Start claiming space from the top of the trampoline space. If
2876 the space is located at the bottom of the virtual address space,
2877 this reduces the possibility that corruption will occur if a null
2878 pointer is used to write to memory. */
2879 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2881 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2886 trampoline_buffer_head
-= used
;
2888 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2889 pulongest (used
), paddress (trampoline_buffer_head
));
2891 *trampoline
= trampoline_buffer_head
;
2895 /* Returns non-zero if there is space allocated for use in trampolines
2896 for fast tracepoints. */
2899 have_fast_tracepoint_trampoline_buffer (char *buf
)
2901 CORE_ADDR trampoline_end
, errbuf
;
2903 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2906 fatal ("error extracting trampoline_buffer_end");
2913 strcpy (buf
, "was claiming");
2914 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
2917 fatal ("error extracting errbuf");
2921 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
2924 return trampoline_end
!= 0;
2927 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2928 the command fails, or 0 otherwise. If the command ran
2929 successfully, but probing the marker failed, ERROUT will be filled
2930 with the error to reply to GDB, and -1 is also returned. This
2931 allows directly passing IPA errors to GDB. */
2934 probe_marker_at (CORE_ADDR address
, char *errout
)
2936 char cmd
[IPA_CMD_BUF_SIZE
];
2939 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
2940 err
= run_inferior_command (cmd
);
2946 strcpy (errout
, cmd
);
2955 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
2957 to
->jump_pad
= from
->jump_pad
;
2958 to
->jump_pad_end
= from
->jump_pad_end
;
2959 to
->trampoline
= from
->trampoline
;
2960 to
->trampoline_end
= from
->trampoline_end
;
2961 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
2962 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
2963 to
->handle
= from
->handle
;
2965 gdb_assert (from
->handle
);
2966 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
2969 #define MAX_JUMP_SIZE 20
2971 /* Install fast tracepoint. Return 0 if successful, otherwise return
2975 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
2977 CORE_ADDR jentry
, jump_entry
;
2978 CORE_ADDR trampoline
;
2979 ULONGEST trampoline_size
;
2981 /* The jump to the jump pad of the last fast tracepoint
2983 unsigned char fjump
[MAX_JUMP_SIZE
];
2984 ULONGEST fjump_size
;
2986 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
2988 trace_debug ("Requested a fast tracepoint on an instruction "
2989 "that is of less than the minimum length.");
2993 jentry
= jump_entry
= get_jump_space_head ();
2996 trampoline_size
= 0;
2998 /* Install the jump pad. */
2999 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3001 ipa_sym_addrs
.addr_gdb_collect
,
3002 ipa_sym_addrs
.addr_collecting
,
3005 &trampoline
, &trampoline_size
,
3007 &tpoint
->adjusted_insn_addr
,
3008 &tpoint
->adjusted_insn_addr_end
,
3015 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3018 if (tpoint
->handle
!= NULL
)
3020 tpoint
->jump_pad
= jump_entry
;
3021 tpoint
->jump_pad_end
= jentry
;
3022 tpoint
->trampoline
= trampoline
;
3023 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3025 /* Pad to 8-byte alignment. */
3026 jentry
= ((jentry
+ 7) & ~0x7);
3027 claim_jump_space (jentry
- jump_entry
);
3034 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3037 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3039 tpoint
->handle
= NULL
;
3042 if (tpoint
->type
== trap_tracepoint
)
3044 /* Tracepoints are installed as memory breakpoints. Just go
3045 ahead and install the trap. The breakpoints module
3046 handles duplicated breakpoints, and the memory read
3047 routine handles un-patching traps from memory reads. */
3048 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3049 tracepoint_handler
);
3051 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3053 if (!agent_loaded_p ())
3055 trace_debug ("Requested a %s tracepoint, but fast "
3056 "tracepoints aren't supported.",
3057 tpoint
->type
== static_tracepoint
? "static" : "fast");
3058 write_e_ipa_not_loaded (own_buf
);
3061 if (tpoint
->type
== static_tracepoint
3062 && !in_process_agent_supports_ust ())
3064 trace_debug ("Requested a static tracepoint, but static "
3065 "tracepoints are not supported.");
3066 write_e_ust_not_loaded (own_buf
);
3070 if (tpoint
->type
== fast_tracepoint
)
3071 install_fast_tracepoint (tpoint
, own_buf
);
3074 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3075 tpoint
->handle
= (void *) -1;
3080 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3082 if (tpoint
->handle
== NULL
)
3084 if (*own_buf
== '\0')
3085 write_enn (own_buf
);
3092 cmd_qtstart (char *packet
)
3094 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3096 trace_debug ("Starting the trace");
3098 /* Pause all threads temporarily while we patch tracepoints. */
3101 /* Get threads out of jump pads. Safe to do here, since this is a
3102 top level command. And, required to do here, since we're
3103 deleting/rewriting jump pads. */
3105 stabilize_threads ();
3107 /* Freeze threads. */
3110 /* Sync the fast tracepoints list in the inferior ftlib. */
3111 if (agent_loaded_p ())
3113 download_tracepoints ();
3114 download_trace_state_variables ();
3117 /* No previous fast tpoint yet. */
3118 prev_ftpoint
= NULL
;
3120 /* No previous static tpoint yet. */
3121 prev_stpoint
= NULL
;
3125 /* Install tracepoints. */
3126 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3128 /* Ensure all the hit counts start at zero. */
3129 tpoint
->hit_count
= 0;
3130 tpoint
->traceframe_usage
= 0;
3132 if (tpoint
->type
== trap_tracepoint
)
3134 /* Tracepoints are installed as memory breakpoints. Just go
3135 ahead and install the trap. The breakpoints module
3136 handles duplicated breakpoints, and the memory read
3137 routine handles un-patching traps from memory reads. */
3138 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3139 tracepoint_handler
);
3141 else if (tpoint
->type
== fast_tracepoint
)
3143 if (maybe_write_ipa_not_loaded (packet
))
3145 trace_debug ("Requested a fast tracepoint, but fast "
3146 "tracepoints aren't supported.");
3150 if (prev_ftpoint
!= NULL
&& prev_ftpoint
->address
== tpoint
->address
)
3151 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3154 if (install_fast_tracepoint (tpoint
, packet
) == 0)
3155 prev_ftpoint
= tpoint
;
3158 else if (tpoint
->type
== static_tracepoint
)
3160 if (maybe_write_ipa_ust_not_loaded (packet
))
3162 trace_debug ("Requested a static tracepoint, but static "
3163 "tracepoints are not supported.");
3167 /* Can only probe a given marker once. */
3168 if (prev_stpoint
!= NULL
&& prev_stpoint
->address
== tpoint
->address
)
3170 tpoint
->handle
= (void *) -1;
3174 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3176 tpoint
->handle
= (void *) -1;
3178 /* So that we can handle multiple static tracepoints
3179 at the same address easily. */
3180 prev_stpoint
= tpoint
;
3185 /* Any failure in the inner loop is sufficient cause to give
3187 if (tpoint
->handle
== NULL
)
3191 /* Any error in tracepoint insertion is unacceptable; better to
3192 address the problem now, than end up with a useless or misleading
3196 clear_installed_tracepoints ();
3197 if (*packet
== '\0')
3203 stopping_tracepoint
= NULL
;
3204 trace_buffer_is_full
= 0;
3205 expr_eval_result
= expr_eval_no_error
;
3206 error_tracepoint
= NULL
;
3207 tracing_start_time
= get_timestamp ();
3209 /* Tracing is now active, hits will now start being logged. */
3212 if (agent_loaded_p ())
3214 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3215 fatal ("Error setting tracing variable in lib");
3217 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3219 fatal ("Error clearing stopping_tracepoint variable in lib");
3221 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3222 fatal ("Error clearing trace_buffer_is_full variable in lib");
3224 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3225 stop_tracing_handler
);
3226 if (stop_tracing_bkpt
== NULL
)
3227 error ("Error setting stop_tracing breakpoint");
3229 flush_trace_buffer_bkpt
3230 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3231 flush_trace_buffer_handler
);
3232 if (flush_trace_buffer_bkpt
== NULL
)
3233 error ("Error setting flush_trace_buffer breakpoint");
3241 /* End a tracing run, filling in a stop reason to report back to GDB,
3242 and removing the tracepoints from the code. */
3249 trace_debug ("Tracing is already off, ignoring");
3253 trace_debug ("Stopping the trace");
3255 /* Pause all threads before removing fast jumps from memory,
3256 breakpoints, and touching IPA state variables (inferior memory).
3257 Some thread may hit the internal tracing breakpoints, or be
3258 collecting this moment, but that's ok, we don't release the
3259 tpoint object's memory or the jump pads here (we only do that
3260 when we're sure we can move all threads out of the jump pads).
3261 We can't now, since we may be getting here due to the inferior
3262 agent calling us. */
3264 /* Since we're removing breakpoints, cancel breakpoint hits,
3265 possibly related to the breakpoints we're about to delete. */
3266 cancel_breakpoints ();
3268 /* Stop logging. Tracepoints can still be hit, but they will not be
3271 if (agent_loaded_p ())
3273 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3274 fatal ("Error clearing tracing variable in lib");
3277 tracing_stop_time
= get_timestamp ();
3278 tracing_stop_reason
= "t???";
3279 tracing_stop_tpnum
= 0;
3280 if (stopping_tracepoint
)
3282 trace_debug ("Stopping the trace because "
3283 "tracepoint %d was hit %" PRIu64
" times",
3284 stopping_tracepoint
->number
,
3285 stopping_tracepoint
->pass_count
);
3286 tracing_stop_reason
= "tpasscount";
3287 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3289 else if (trace_buffer_is_full
)
3291 trace_debug ("Stopping the trace because the trace buffer is full");
3292 tracing_stop_reason
= "tfull";
3294 else if (expr_eval_result
!= expr_eval_no_error
)
3296 trace_debug ("Stopping the trace because of an expression eval error");
3297 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3298 tracing_stop_tpnum
= error_tracepoint
->number
;
3300 #ifndef IN_PROCESS_AGENT
3301 else if (!gdb_connected ())
3303 trace_debug ("Stopping the trace because GDB disconnected");
3304 tracing_stop_reason
= "tdisconnected";
3309 trace_debug ("Stopping the trace because of a tstop command");
3310 tracing_stop_reason
= "tstop";
3313 stopping_tracepoint
= NULL
;
3314 error_tracepoint
= NULL
;
3316 /* Clear out the tracepoints. */
3317 clear_installed_tracepoints ();
3319 if (agent_loaded_p ())
3321 /* Pull in fast tracepoint trace frames from the inferior lib
3322 buffer into our buffer, even if our buffer is already full,
3323 because we want to present the full number of created frames
3324 in addition to what fit in the trace buffer. */
3325 upload_fast_traceframes ();
3328 if (stop_tracing_bkpt
!= NULL
)
3330 delete_breakpoint (stop_tracing_bkpt
);
3331 stop_tracing_bkpt
= NULL
;
3334 if (flush_trace_buffer_bkpt
!= NULL
)
3336 delete_breakpoint (flush_trace_buffer_bkpt
);
3337 flush_trace_buffer_bkpt
= NULL
;
3344 stop_tracing_handler (CORE_ADDR addr
)
3346 trace_debug ("lib hit stop_tracing");
3348 /* Don't actually handle it here. When we stop tracing we remove
3349 breakpoints from the inferior, and that is not allowed in a
3350 breakpoint handler (as the caller is walking the breakpoint
3356 flush_trace_buffer_handler (CORE_ADDR addr
)
3358 trace_debug ("lib hit flush_trace_buffer");
3363 cmd_qtstop (char *packet
)
3370 cmd_qtdisconnected (char *own_buf
)
3373 char *packet
= own_buf
;
3375 packet
+= strlen ("QTDisconnected:");
3377 unpack_varlen_hex (packet
, &setting
);
3381 disconnected_tracing
= setting
;
3385 cmd_qtframe (char *own_buf
)
3387 ULONGEST frame
, pc
, lo
, hi
, num
;
3389 struct traceframe
*tframe
;
3390 char *packet
= own_buf
;
3392 packet
+= strlen ("QTFrame:");
3394 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3396 packet
+= strlen ("pc:");
3397 unpack_varlen_hex (packet
, &pc
);
3398 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3399 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3401 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3403 packet
+= strlen ("range:");
3404 packet
= unpack_varlen_hex (packet
, &lo
);
3406 unpack_varlen_hex (packet
, &hi
);
3407 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3408 paddress (lo
), paddress (hi
));
3409 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3411 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3413 packet
+= strlen ("outside:");
3414 packet
= unpack_varlen_hex (packet
, &lo
);
3416 unpack_varlen_hex (packet
, &hi
);
3417 trace_debug ("Want to find next traceframe "
3418 "outside the range 0x%s to 0x%s",
3419 paddress (lo
), paddress (hi
));
3420 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3422 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3424 packet
+= strlen ("tdp:");
3425 unpack_varlen_hex (packet
, &num
);
3427 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3428 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3432 unpack_varlen_hex (packet
, &frame
);
3433 tfnum
= (int) frame
;
3436 trace_debug ("Want to stop looking at traceframes");
3437 current_traceframe
= -1;
3441 trace_debug ("Want to look at traceframe %d", tfnum
);
3442 tframe
= find_traceframe (tfnum
);
3447 current_traceframe
= tfnum
;
3448 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3451 sprintf (own_buf
, "F-1");
3455 cmd_qtstatus (char *packet
)
3457 char *stop_reason_rsp
= NULL
;
3458 char *buf1
, *buf2
, *buf3
, *str
;
3461 /* Translate the plain text of the notes back into hex for
3464 str
= (tracing_user_name
? tracing_user_name
: "");
3465 slen
= strlen (str
);
3466 buf1
= (char *) alloca (slen
* 2 + 1);
3467 hexify (buf1
, str
, slen
);
3469 str
= (tracing_notes
? tracing_notes
: "");
3470 slen
= strlen (str
);
3471 buf2
= (char *) alloca (slen
* 2 + 1);
3472 hexify (buf2
, str
, slen
);
3474 str
= (tracing_stop_note
? tracing_stop_note
: "");
3475 slen
= strlen (str
);
3476 buf3
= (char *) alloca (slen
* 2 + 1);
3477 hexify (buf3
, str
, slen
);
3479 trace_debug ("Returning trace status as %d, stop reason %s",
3480 tracing
, tracing_stop_reason
);
3482 if (agent_loaded_p ())
3486 upload_fast_traceframes ();
3491 stop_reason_rsp
= (char *) tracing_stop_reason
;
3493 /* The user visible error string in terror needs to be hex encoded.
3494 We leave it as plain string in `tracing_stop_reason' to ease
3496 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3498 const char *result_name
;
3502 result_name
= stop_reason_rsp
+ strlen ("terror:");
3503 hexstr_len
= strlen (result_name
) * 2;
3504 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3505 strcpy (p
, "terror:");
3507 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3510 /* If this was a forced stop, include any stop note that was supplied. */
3511 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3513 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3514 strcpy (stop_reason_rsp
, "tstop:");
3515 strcat (stop_reason_rsp
, buf3
);
3521 "tframes:%x;tcreated:%x;"
3522 "tfree:%x;tsize:%s;"
3525 "starttime:%s;stoptime:%s;"
3526 "username:%s:;notes:%s:",
3528 stop_reason_rsp
, tracing_stop_tpnum
,
3529 traceframe_count
, traceframes_created
,
3530 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3531 circular_trace_buffer
,
3532 disconnected_tracing
,
3533 plongest (tracing_start_time
), plongest (tracing_stop_time
),
3538 cmd_qtp (char *own_buf
)
3541 struct tracepoint
*tpoint
;
3542 char *packet
= own_buf
;
3544 packet
+= strlen ("qTP:");
3546 packet
= unpack_varlen_hex (packet
, &num
);
3547 ++packet
; /* skip a colon */
3548 packet
= unpack_varlen_hex (packet
, &addr
);
3550 /* See if we already have this tracepoint. */
3551 tpoint
= find_tracepoint (num
, addr
);
3555 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3556 (int) num
, paddress (addr
));
3557 write_enn (own_buf
);
3561 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3562 tpoint
->traceframe_usage
);
3565 /* State variables to help return all the tracepoint bits. */
3566 static struct tracepoint
*cur_tpoint
;
3567 static int cur_action
;
3568 static int cur_step_action
;
3569 static struct source_string
*cur_source_string
;
3570 static struct trace_state_variable
*cur_tsv
;
3572 /* Compose a response that is an imitation of the syntax by which the
3573 tracepoint was originally downloaded. */
3576 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3580 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3581 paddress (tpoint
->address
),
3582 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3583 tpoint
->pass_count
);
3584 if (tpoint
->type
== fast_tracepoint
)
3585 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3586 else if (tpoint
->type
== static_tracepoint
)
3587 sprintf (packet
+ strlen (packet
), ":S");
3591 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3592 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3593 tpoint
->cond
->length
, buf
);
3598 /* Compose a response that is an imitation of the syntax by which the
3599 tracepoint action was originally downloaded (with the difference
3600 that due to the way we store the actions, this will output a packet
3601 per action, while GDB could have combined more than one action
3605 response_action (char *packet
, struct tracepoint
*tpoint
,
3606 char *taction
, int step
)
3608 sprintf (packet
, "%c%x:%s:%s",
3609 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3613 /* Compose a response that is an imitation of the syntax by which the
3614 tracepoint source piece was originally downloaded. */
3617 response_source (char *packet
,
3618 struct tracepoint
*tpoint
, struct source_string
*src
)
3623 len
= strlen (src
->str
);
3624 buf
= alloca (len
* 2 + 1);
3625 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3627 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3628 tpoint
->number
, paddress (tpoint
->address
),
3629 src
->type
, 0, len
, buf
);
3632 /* Return the first piece of tracepoint definition, and initialize the
3633 state machine that will iterate through all the tracepoint
3637 cmd_qtfp (char *packet
)
3639 trace_debug ("Returning first tracepoint definition piece");
3641 cur_tpoint
= tracepoints
;
3642 cur_action
= cur_step_action
= -1;
3643 cur_source_string
= NULL
;
3646 response_tracepoint (packet
, cur_tpoint
);
3648 strcpy (packet
, "l");
3651 /* Return additional pieces of tracepoint definition. Each action and
3652 stepping action must go into its own packet, because of packet size
3653 limits, and so we use state variables to deliver one piece at a
3657 cmd_qtsp (char *packet
)
3659 trace_debug ("Returning subsequent tracepoint definition piece");
3663 /* This case would normally never occur, but be prepared for
3665 strcpy (packet
, "l");
3667 else if (cur_action
< cur_tpoint
->numactions
- 1)
3670 response_action (packet
, cur_tpoint
,
3671 cur_tpoint
->actions_str
[cur_action
], 0);
3673 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3676 response_action (packet
, cur_tpoint
,
3677 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3679 else if ((cur_source_string
3680 ? cur_source_string
->next
3681 : cur_tpoint
->source_strings
))
3683 if (cur_source_string
)
3684 cur_source_string
= cur_source_string
->next
;
3686 cur_source_string
= cur_tpoint
->source_strings
;
3687 response_source (packet
, cur_tpoint
, cur_source_string
);
3691 cur_tpoint
= cur_tpoint
->next
;
3692 cur_action
= cur_step_action
= -1;
3693 cur_source_string
= NULL
;
3695 response_tracepoint (packet
, cur_tpoint
);
3697 strcpy (packet
, "l");
3701 /* Compose a response that is an imitation of the syntax by which the
3702 trace state variable was originally downloaded. */
3705 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3707 char *buf
= (char *) "";
3712 namelen
= strlen (tsv
->name
);
3713 buf
= alloca (namelen
* 2 + 1);
3714 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3717 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3718 tsv
->getter
? 1 : 0, buf
);
3721 /* Return the first trace state variable definition, and initialize
3722 the state machine that will iterate through all the tsv bits. */
3725 cmd_qtfv (char *packet
)
3727 trace_debug ("Returning first trace state variable definition");
3729 cur_tsv
= trace_state_variables
;
3732 response_tsv (packet
, cur_tsv
);
3734 strcpy (packet
, "l");
3737 /* Return additional trace state variable definitions. */
3740 cmd_qtsv (char *packet
)
3742 trace_debug ("Returning first trace state variable definition");
3746 /* This case would normally never occur, but be prepared for
3748 strcpy (packet
, "l");
3752 cur_tsv
= cur_tsv
->next
;
3754 response_tsv (packet
, cur_tsv
);
3756 strcpy (packet
, "l");
3759 strcpy (packet
, "l");
3762 /* Return the first static tracepoint marker, and initialize the state
3763 machine that will iterate through all the static tracepoints
3767 cmd_qtfstm (char *packet
)
3769 if (!maybe_write_ipa_ust_not_loaded (packet
))
3770 run_inferior_command (packet
);
3773 /* Return additional static tracepoints markers. */
3776 cmd_qtsstm (char *packet
)
3778 if (!maybe_write_ipa_ust_not_loaded (packet
))
3779 run_inferior_command (packet
);
3782 /* Return the definition of the static tracepoint at a given address.
3783 Result packet is the same as qTsST's. */
3786 cmd_qtstmat (char *packet
)
3788 if (!maybe_write_ipa_ust_not_loaded (packet
))
3789 run_inferior_command (packet
);
3792 /* Return the minimum instruction size needed for fast tracepoints as a
3793 hexadecimal number. */
3796 cmd_qtminftpilen (char *packet
)
3798 if (current_inferior
== NULL
)
3800 /* Indicate that the minimum length is currently unknown. */
3801 strcpy (packet
, "0");
3805 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3808 /* Respond to qTBuffer packet with a block of raw data from the trace
3809 buffer. GDB may ask for a lot, but we are allowed to reply with
3810 only as much as will fit within packet limits or whatever. */
3813 cmd_qtbuffer (char *own_buf
)
3815 ULONGEST offset
, num
, tot
;
3817 char *packet
= own_buf
;
3819 packet
+= strlen ("qTBuffer:");
3821 packet
= unpack_varlen_hex (packet
, &offset
);
3822 ++packet
; /* skip a comma */
3823 unpack_varlen_hex (packet
, &num
);
3825 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3826 (int) num
, pulongest (offset
));
3828 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3830 /* If we're right at the end, reply specially that we're done. */
3833 strcpy (own_buf
, "l");
3837 /* Object to any other out-of-bounds request. */
3840 write_enn (own_buf
);
3844 /* Compute the pointer corresponding to the given offset, accounting
3846 tbp
= trace_buffer_start
+ offset
;
3847 if (tbp
>= trace_buffer_wrap
)
3848 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3850 /* Trim to the remaining bytes if we're close to the end. */
3851 if (num
> tot
- offset
)
3854 /* Trim to available packet size. */
3855 if (num
>= (PBUFSIZ
- 16) / 2 )
3856 num
= (PBUFSIZ
- 16) / 2;
3858 convert_int_to_ascii (tbp
, own_buf
, num
);
3859 own_buf
[num
] = '\0';
3863 cmd_bigqtbuffer_circular (char *own_buf
)
3866 char *packet
= own_buf
;
3868 packet
+= strlen ("QTBuffer:circular:");
3870 unpack_varlen_hex (packet
, &val
);
3871 circular_trace_buffer
= val
;
3872 trace_debug ("Trace buffer is now %s",
3873 circular_trace_buffer
? "circular" : "linear");
3878 cmd_qtnotes (char *own_buf
)
3881 char *saved
, *user
, *notes
, *stopnote
;
3882 char *packet
= own_buf
;
3884 packet
+= strlen ("QTNotes:");
3888 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
3890 packet
+= strlen ("user:");
3892 packet
= strchr (packet
, ';');
3893 nbytes
= (packet
- saved
) / 2;
3894 user
= xmalloc (nbytes
+ 1);
3895 nbytes
= unhexify (user
, saved
, nbytes
);
3896 user
[nbytes
] = '\0';
3897 ++packet
; /* skip the semicolon */
3898 trace_debug ("User is '%s'", user
);
3899 tracing_user_name
= user
;
3901 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
3903 packet
+= strlen ("notes:");
3905 packet
= strchr (packet
, ';');
3906 nbytes
= (packet
- saved
) / 2;
3907 notes
= xmalloc (nbytes
+ 1);
3908 nbytes
= unhexify (notes
, saved
, nbytes
);
3909 notes
[nbytes
] = '\0';
3910 ++packet
; /* skip the semicolon */
3911 trace_debug ("Notes is '%s'", notes
);
3912 tracing_notes
= notes
;
3914 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
3916 packet
+= strlen ("tstop:");
3918 packet
= strchr (packet
, ';');
3919 nbytes
= (packet
- saved
) / 2;
3920 stopnote
= xmalloc (nbytes
+ 1);
3921 nbytes
= unhexify (stopnote
, saved
, nbytes
);
3922 stopnote
[nbytes
] = '\0';
3923 ++packet
; /* skip the semicolon */
3924 trace_debug ("tstop note is '%s'", stopnote
);
3925 tracing_stop_note
= stopnote
;
3935 handle_tracepoint_general_set (char *packet
)
3937 if (strcmp ("QTinit", packet
) == 0)
3939 cmd_qtinit (packet
);
3942 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3947 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3949 cmd_qtdpsrc (packet
);
3952 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
3954 cmd_qtenable_disable (packet
, 1);
3957 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
3959 cmd_qtenable_disable (packet
, 0);
3962 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3967 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3972 else if (strcmp ("QTStart", packet
) == 0)
3974 cmd_qtstart (packet
);
3977 else if (strcmp ("QTStop", packet
) == 0)
3979 cmd_qtstop (packet
);
3982 else if (strncmp ("QTDisconnected:", packet
,
3983 strlen ("QTDisconnected:")) == 0)
3985 cmd_qtdisconnected (packet
);
3988 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3990 cmd_qtframe (packet
);
3993 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
3995 cmd_bigqtbuffer_circular (packet
);
3998 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
4000 cmd_qtnotes (packet
);
4008 handle_tracepoint_query (char *packet
)
4010 if (strcmp ("qTStatus", packet
) == 0)
4012 cmd_qtstatus (packet
);
4015 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4020 else if (strcmp ("qTfP", packet
) == 0)
4025 else if (strcmp ("qTsP", packet
) == 0)
4030 else if (strcmp ("qTfV", packet
) == 0)
4035 else if (strcmp ("qTsV", packet
) == 0)
4040 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4045 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4047 cmd_qtbuffer (packet
);
4050 else if (strcmp ("qTfSTM", packet
) == 0)
4052 cmd_qtfstm (packet
);
4055 else if (strcmp ("qTsSTM", packet
) == 0)
4057 cmd_qtsstm (packet
);
4060 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4062 cmd_qtstmat (packet
);
4065 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4067 cmd_qtminftpilen (packet
);
4075 #ifndef IN_PROCESS_AGENT
4077 /* Call this when thread TINFO has hit the tracepoint defined by
4078 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4079 action. This adds a while-stepping collecting state item to the
4080 threads' collecting state list, so that we can keep track of
4081 multiple simultaneous while-stepping actions being collected by the
4082 same thread. This can happen in cases like:
4084 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4086 ff0003 INSN3 <-- TP2, collect $regs
4087 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4090 Notice that when instruction INSN5 is reached, the while-stepping
4091 actions of both TP1 and TP3 are still being collected, and that TP2
4092 had been collected meanwhile. The whole range of ff0001-ff0005
4093 should be single-stepped, due to at least TP1's while-stepping
4094 action covering the whole range. */
4097 add_while_stepping_state (struct thread_info
*tinfo
,
4098 int tp_number
, CORE_ADDR tp_address
)
4100 struct wstep_state
*wstep
;
4102 wstep
= xmalloc (sizeof (*wstep
));
4103 wstep
->next
= tinfo
->while_stepping
;
4105 wstep
->tp_number
= tp_number
;
4106 wstep
->tp_address
= tp_address
;
4107 wstep
->current_step
= 0;
4109 tinfo
->while_stepping
= wstep
;
4112 /* Release the while-stepping collecting state WSTEP. */
4115 release_while_stepping_state (struct wstep_state
*wstep
)
4120 /* Release all while-stepping collecting states currently associated
4121 with thread TINFO. */
4124 release_while_stepping_state_list (struct thread_info
*tinfo
)
4126 struct wstep_state
*head
;
4128 while (tinfo
->while_stepping
)
4130 head
= tinfo
->while_stepping
;
4131 tinfo
->while_stepping
= head
->next
;
4132 release_while_stepping_state (head
);
4136 /* If TINFO was handling a 'while-stepping' action, the step has
4137 finished, so collect any step data needed, and check if any more
4138 steps are required. Return true if the thread was indeed
4139 collecting tracepoint data, false otherwise. */
4142 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4144 struct tracepoint
*tpoint
;
4145 struct wstep_state
*wstep
;
4146 struct wstep_state
**wstep_link
;
4147 struct trap_tracepoint_ctx ctx
;
4149 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4151 if (agent_loaded_p ())
4152 upload_fast_traceframes ();
4154 /* Check if we were indeed collecting data for one of more
4155 tracepoints with a 'while-stepping' count. */
4156 if (tinfo
->while_stepping
== NULL
)
4161 /* We're not even tracing anymore. Stop this thread from
4163 release_while_stepping_state_list (tinfo
);
4165 /* The thread had stopped due to a single-step request indeed
4166 explained by a tracepoint. */
4170 wstep
= tinfo
->while_stepping
;
4171 wstep_link
= &tinfo
->while_stepping
;
4173 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4174 target_pid_to_str (tinfo
->entry
.id
),
4175 wstep
->tp_number
, paddress (wstep
->tp_address
));
4177 ctx
.base
.type
= trap_tracepoint
;
4178 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4180 while (wstep
!= NULL
)
4182 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4185 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4186 wstep
->tp_number
, paddress (wstep
->tp_address
),
4187 target_pid_to_str (tinfo
->entry
.id
));
4190 *wstep_link
= wstep
->next
;
4191 release_while_stepping_state (wstep
);
4192 wstep
= *wstep_link
;
4196 /* We've just finished one step. */
4197 ++wstep
->current_step
;
4200 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4201 stop_pc
, tpoint
, wstep
->current_step
);
4203 if (wstep
->current_step
>= tpoint
->step_count
)
4205 /* The requested numbers of steps have occurred. */
4206 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4207 target_pid_to_str (tinfo
->entry
.id
),
4208 wstep
->tp_number
, paddress (wstep
->tp_address
));
4210 /* Unlink the wstep. */
4211 *wstep_link
= wstep
->next
;
4212 release_while_stepping_state (wstep
);
4213 wstep
= *wstep_link
;
4215 /* Only check the hit count now, which ensure that we do all
4216 our stepping before stopping the run. */
4217 if (tpoint
->pass_count
> 0
4218 && tpoint
->hit_count
>= tpoint
->pass_count
4219 && stopping_tracepoint
== NULL
)
4220 stopping_tracepoint
= tpoint
;
4224 /* Keep single-stepping until the requested numbers of steps
4226 wstep_link
= &wstep
->next
;
4227 wstep
= *wstep_link
;
4230 if (stopping_tracepoint
4231 || trace_buffer_is_full
4232 || expr_eval_result
!= expr_eval_no_error
)
4242 /* Handle any internal tracing control breakpoint hits. That means,
4243 pull traceframes from the IPA to our buffer, and syncing both
4244 tracing agents when the IPA's tracing stops for some reason. */
4247 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4249 /* Pull in fast tracepoint trace frames from the inferior in-process
4250 agent's buffer into our buffer. */
4252 if (!agent_loaded_p ())
4255 upload_fast_traceframes ();
4257 /* Check if the in-process agent had decided we should stop
4259 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4261 int ipa_trace_buffer_is_full
;
4262 CORE_ADDR ipa_stopping_tracepoint
;
4263 int ipa_expr_eval_result
;
4264 CORE_ADDR ipa_error_tracepoint
;
4266 trace_debug ("lib stopped at stop_tracing");
4268 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4269 &ipa_trace_buffer_is_full
);
4271 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4272 &ipa_stopping_tracepoint
);
4273 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4275 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4276 &ipa_error_tracepoint
);
4277 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4279 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4280 &ipa_expr_eval_result
);
4281 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4283 trace_debug ("lib: trace_buffer_is_full: %d, "
4284 "stopping_tracepoint: %s, "
4285 "ipa_expr_eval_result: %d, "
4286 "error_tracepoint: %s, ",
4287 ipa_trace_buffer_is_full
,
4288 paddress (ipa_stopping_tracepoint
),
4289 ipa_expr_eval_result
,
4290 paddress (ipa_error_tracepoint
));
4294 if (ipa_trace_buffer_is_full
)
4295 trace_debug ("lib stopped due to full buffer.");
4296 if (ipa_stopping_tracepoint
)
4297 trace_debug ("lib stopped due to tpoint");
4298 if (ipa_stopping_tracepoint
)
4299 trace_debug ("lib stopped due to error");
4302 if (ipa_stopping_tracepoint
!= 0)
4305 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4307 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4309 expr_eval_result
= ipa_expr_eval_result
;
4311 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4316 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4318 trace_debug ("lib stopped at flush_trace_buffer");
4325 /* Return true if TINFO just hit a tracepoint. Collect data if
4329 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4331 struct tracepoint
*tpoint
;
4333 struct trap_tracepoint_ctx ctx
;
4335 /* Not tracing, don't handle. */
4339 ctx
.base
.type
= trap_tracepoint
;
4340 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4342 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4344 /* Note that we collect fast tracepoints here as well. We'll
4345 step over the fast tracepoint jump later, which avoids the
4346 double collect. However, we don't collect for static
4347 tracepoints here, because UST markers are compiled in program,
4348 and probes will be executed in program. So static tracepoints
4349 are collected there. */
4350 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4351 && tpoint
->type
!= static_tracepoint
)
4353 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4354 target_pid_to_str (tinfo
->entry
.id
),
4355 tpoint
->number
, paddress (tpoint
->address
));
4357 /* Test the condition if present, and collect if true. */
4359 || (condition_true_at_tracepoint
4360 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4361 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4364 if (stopping_tracepoint
4365 || trace_buffer_is_full
4366 || expr_eval_result
!= expr_eval_no_error
)
4370 /* If the tracepoint had a 'while-stepping' action, then set
4371 the thread to collect this tracepoint on the following
4373 else if (tpoint
->step_count
> 0)
4375 add_while_stepping_state (tinfo
,
4376 tpoint
->number
, tpoint
->address
);
4388 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4389 struct ust_marker_data
;
4390 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4391 struct traceframe
*tframe
);
4394 /* Create a trace frame for the hit of the given tracepoint in the
4398 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4399 struct tracepoint
*tpoint
)
4401 struct traceframe
*tframe
;
4404 /* Only count it as a hit when we actually collect data. */
4405 tpoint
->hit_count
++;
4407 /* If we've exceeded a defined pass count, record the event for
4408 later, and finish the collection for this hit. This test is only
4409 for nonstepping tracepoints, stepping tracepoints test at the end
4410 of their while-stepping loop. */
4411 if (tpoint
->pass_count
> 0
4412 && tpoint
->hit_count
>= tpoint
->pass_count
4413 && tpoint
->step_count
== 0
4414 && stopping_tracepoint
== NULL
)
4415 stopping_tracepoint
= tpoint
;
4417 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4418 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4420 tframe
= add_traceframe (tpoint
);
4424 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4426 #ifndef IN_PROCESS_AGENT
4427 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4428 tpoint
->number
, paddress (tpoint
->address
),
4429 tpoint
->actions_str
[acti
]);
4432 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4433 tpoint
->actions
[acti
]);
4436 finish_traceframe (tframe
);
4439 if (tframe
== NULL
&& tracing
)
4440 trace_buffer_is_full
= 1;
4443 #ifndef IN_PROCESS_AGENT
4446 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4448 struct tracepoint
*tpoint
, int current_step
)
4450 struct traceframe
*tframe
;
4453 trace_debug ("Making new step traceframe for "
4454 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4455 tpoint
->number
, paddress (tpoint
->address
),
4456 current_step
, tpoint
->step_count
,
4459 tframe
= add_traceframe (tpoint
);
4463 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4465 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4466 tpoint
->number
, paddress (tpoint
->address
),
4467 tpoint
->step_actions_str
[acti
]);
4469 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4470 tpoint
->step_actions
[acti
]);
4473 finish_traceframe (tframe
);
4476 if (tframe
== NULL
&& tracing
)
4477 trace_buffer_is_full
= 1;
4482 static struct regcache
*
4483 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4485 struct regcache
*regcache
= NULL
;
4487 #ifdef IN_PROCESS_AGENT
4488 if (ctx
->type
== fast_tracepoint
)
4490 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4491 if (!fctx
->regcache_initted
)
4493 fctx
->regcache_initted
= 1;
4494 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4495 supply_regblock (&fctx
->regcache
, NULL
);
4496 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4498 regcache
= &fctx
->regcache
;
4501 if (ctx
->type
== static_tracepoint
)
4503 struct static_tracepoint_ctx
*sctx
4504 = (struct static_tracepoint_ctx
*) ctx
;
4506 if (!sctx
->regcache_initted
)
4508 sctx
->regcache_initted
= 1;
4509 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4510 supply_regblock (&sctx
->regcache
, NULL
);
4511 /* Pass down the tracepoint address, because REGS doesn't
4512 include the PC, but we know what it must have been. */
4513 supply_static_tracepoint_registers (&sctx
->regcache
,
4514 (const unsigned char *)
4516 sctx
->tpoint
->address
);
4518 regcache
= &sctx
->regcache
;
4522 if (ctx
->type
== trap_tracepoint
)
4524 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4525 regcache
= tctx
->regcache
;
4529 gdb_assert (regcache
!= NULL
);
4535 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4537 struct tracepoint
*tpoint
,
4538 struct traceframe
*tframe
,
4539 struct tracepoint_action
*taction
)
4541 enum eval_result_type err
;
4543 switch (taction
->type
)
4547 struct collect_memory_action
*maction
;
4549 maction
= (struct collect_memory_action
*) taction
;
4551 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4552 pulongest (maction
->len
),
4553 paddress (maction
->addr
), maction
->basereg
);
4554 /* (should use basereg) */
4555 agent_mem_read (tframe
, NULL
,
4556 (CORE_ADDR
) maction
->addr
, maction
->len
);
4561 unsigned char *regspace
;
4562 struct regcache tregcache
;
4563 struct regcache
*context_regcache
;
4566 trace_debug ("Want to collect registers");
4568 /* Collect all registers for now. */
4569 regspace
= add_traceframe_block (tframe
,
4570 1 + register_cache_size ());
4571 if (regspace
== NULL
)
4573 trace_debug ("Trace buffer block allocation failed, skipping");
4576 /* Identify a register block. */
4579 context_regcache
= get_context_regcache (ctx
);
4581 /* Wrap the regblock in a register cache (in the stack, we
4582 don't want to malloc here). */
4583 init_register_cache (&tregcache
, regspace
+ 1);
4585 /* Copy the register data to the regblock. */
4586 regcache_cpy (&tregcache
, context_regcache
);
4588 #ifndef IN_PROCESS_AGENT
4589 /* On some platforms, trap-based tracepoints will have the PC
4590 pointing to the next instruction after the trap, but we
4591 don't want the user or GDB trying to guess whether the
4592 saved PC needs adjusting; so always record the adjusted
4593 stop_pc. Note that we can't use tpoint->address instead,
4594 since it will be wrong for while-stepping actions. This
4595 adjustment is a nop for fast tracepoints collected from the
4596 in-process lib (but not if GDBserver is collecting one
4597 preemptively), since the PC had already been adjusted to
4598 contain the tracepoint's address by the jump pad. */
4599 trace_debug ("Storing stop pc (0x%s) in regblock",
4600 paddress (stop_pc
));
4602 /* This changes the regblock, not the thread's
4604 regcache_write_pc (&tregcache
, stop_pc
);
4610 struct eval_expr_action
*eaction
;
4612 eaction
= (struct eval_expr_action
*) taction
;
4614 trace_debug ("Want to evaluate expression");
4616 err
= eval_tracepoint_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4618 if (err
!= expr_eval_no_error
)
4620 record_tracepoint_error (tpoint
, "action expression", err
);
4627 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4628 trace_debug ("Want to collect static trace data");
4629 collect_ust_data_at_tracepoint (ctx
, tframe
);
4631 trace_debug ("warning: collecting static trace data, "
4632 "but static tracepoints are not supported");
4637 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4643 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4644 struct tracepoint
*tpoint
)
4647 enum eval_result_type err
;
4649 /* Presently, gdbserver doesn't run compiled conditions, only the
4650 IPA does. If the program stops at a fast tracepoint's address
4651 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4652 gdbserver preemptively collect the fast tracepoint. Later, on
4653 resume, gdbserver steps over the fast tracepoint like it steps
4654 over breakpoints, so that the IPA doesn't see that fast
4655 tracepoint. This avoids double collects of fast tracepoints in
4656 that stopping scenario. Having gdbserver itself handle the fast
4657 tracepoint gives the user a consistent view of when fast or trap
4658 tracepoints are collected, compared to an alternative where only
4659 trap tracepoints are collected on stop, and fast tracepoints on
4660 resume. When a fast tracepoint is being processed by gdbserver,
4661 it is always the non-compiled condition expression that is
4663 #ifdef IN_PROCESS_AGENT
4664 if (tpoint
->compiled_cond
)
4665 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4668 err
= eval_tracepoint_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4670 if (err
!= expr_eval_no_error
)
4672 record_tracepoint_error (tpoint
, "condition", err
);
4673 /* The error case must return false. */
4677 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4678 tpoint
->number
, paddress (tpoint
->address
),
4680 return (value
? 1 : 0);
4683 /* Evaluates a tracepoint agent expression with context CTX,
4684 traceframe TFRAME, agent expression AEXPR and store the
4687 static enum eval_result_type
4688 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4689 struct traceframe
*tframe
,
4690 struct agent_expr
*aexpr
,
4693 struct regcache
*regcache
;
4694 regcache
= get_context_regcache (ctx
);
4696 return gdb_eval_agent_expr (regcache
, tframe
, aexpr
, rslt
);
4699 /* Do memory copies for bytecodes. */
4700 /* Do the recording of memory blocks for actions and bytecodes. */
4703 agent_mem_read (struct traceframe
*tframe
,
4704 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4706 unsigned char *mspace
;
4707 ULONGEST remaining
= len
;
4708 unsigned short blocklen
;
4710 /* If a 'to' buffer is specified, use it. */
4713 read_inferior_memory (from
, to
, len
);
4717 /* Otherwise, create a new memory block in the trace buffer. */
4718 while (remaining
> 0)
4722 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4723 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4724 mspace
= add_traceframe_block (tframe
, sp
);
4727 /* Identify block as a memory block. */
4730 /* Record address and size. */
4731 memcpy (mspace
, &from
, sizeof (from
));
4732 mspace
+= sizeof (from
);
4733 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4734 mspace
+= sizeof (blocklen
);
4735 /* Record the memory block proper. */
4736 read_inferior_memory (from
, mspace
, blocklen
);
4737 trace_debug ("%d bytes recorded", blocklen
);
4738 remaining
-= blocklen
;
4745 agent_mem_read_string (struct traceframe
*tframe
,
4746 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4748 unsigned char *buf
, *mspace
;
4749 ULONGEST remaining
= len
;
4750 unsigned short blocklen
, i
;
4752 /* To save a bit of space, block lengths are 16-bit, so break large
4753 requests into multiple blocks. Bordering on overkill for strings,
4754 but it could happen that someone specifies a large max length. */
4755 while (remaining
> 0)
4759 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4760 /* We want working space to accumulate nonzero bytes, since
4761 traceframes must have a predecided size (otherwise it gets
4762 harder to wrap correctly for the circular case, etc). */
4763 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4764 for (i
= 0; i
< blocklen
; ++i
)
4766 /* Read the string one byte at a time, in case the string is
4767 at the end of a valid memory area - we don't want a
4768 correctly-terminated string to engender segvio
4770 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4775 /* Make sure outer loop stops now too. */
4776 remaining
= blocklen
;
4780 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4781 mspace
= add_traceframe_block (tframe
, sp
);
4787 /* Identify block as a memory block. */
4790 /* Record address and size. */
4791 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4792 mspace
+= sizeof (from
);
4793 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4794 mspace
+= sizeof (blocklen
);
4795 /* Copy the string contents. */
4796 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
4797 remaining
-= blocklen
;
4804 /* Record the value of a trace state variable. */
4807 agent_tsv_read (struct traceframe
*tframe
, int n
)
4809 unsigned char *vspace
;
4812 vspace
= add_traceframe_block (tframe
,
4813 1 + sizeof (n
) + sizeof (LONGEST
));
4816 /* Identify block as a variable. */
4818 /* Record variable's number and value. */
4819 memcpy (vspace
+ 1, &n
, sizeof (n
));
4820 val
= get_trace_state_variable_value (n
);
4821 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4822 trace_debug ("Variable %d recorded", n
);
4826 #ifndef IN_PROCESS_AGENT
4828 /* Callback for traceframe_walk_blocks, used to find a given block
4829 type in a traceframe. */
4832 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
4834 char *wantedp
= data
;
4836 if (*wantedp
== blocktype
)
4842 /* Walk over all traceframe blocks of the traceframe buffer starting
4843 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4844 block found, passing in DATA unmodified. If CALLBACK returns true,
4845 this returns a pointer to where the block is found. Returns NULL
4846 if no callback call returned true, indicating that all blocks have
4849 static unsigned char *
4850 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
4852 int (*callback
) (char blocktype
,
4853 unsigned char *dataptr
,
4857 unsigned char *dataptr
;
4861 trace_debug ("traceframe %d has no data", tfnum
);
4865 /* Iterate through a traceframe's blocks, looking for a block of the
4867 for (dataptr
= database
;
4868 dataptr
< database
+ datasize
;
4872 unsigned short mlen
;
4874 if (dataptr
== trace_buffer_wrap
)
4876 /* Adjust to reflect wrapping part of the frame around to
4878 datasize
= dataptr
- database
;
4879 dataptr
= database
= trace_buffer_lo
;
4882 blocktype
= *dataptr
++;
4884 if ((*callback
) (blocktype
, dataptr
, data
))
4890 /* Skip over the registers block. */
4891 dataptr
+= register_cache_size ();
4894 /* Skip over the memory block. */
4895 dataptr
+= sizeof (CORE_ADDR
);
4896 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4897 dataptr
+= (sizeof (mlen
) + mlen
);
4900 /* Skip over the TSV block. */
4901 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4904 /* Skip over the static trace data block. */
4905 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4906 dataptr
+= (sizeof (mlen
) + mlen
);
4909 trace_debug ("traceframe %d has unknown block type 0x%x",
4918 /* Look for the block of type TYPE_WANTED in the trameframe starting
4919 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4922 static unsigned char *
4923 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4924 int tfnum
, char type_wanted
)
4926 return traceframe_walk_blocks (database
, datasize
, tfnum
,
4927 match_blocktype
, &type_wanted
);
4930 static unsigned char *
4931 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4933 unsigned char *regblock
;
4935 regblock
= traceframe_find_block_type (tframe
->data
,
4939 if (regblock
== NULL
)
4940 trace_debug ("traceframe %d has no register data", tfnum
);
4945 /* Get registers from a traceframe. */
4948 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4950 unsigned char *dataptr
;
4951 struct tracepoint
*tpoint
;
4952 struct traceframe
*tframe
;
4954 tframe
= find_traceframe (tfnum
);
4958 trace_debug ("traceframe %d not found", tfnum
);
4962 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4963 if (dataptr
== NULL
)
4965 /* Mark registers unavailable. */
4966 supply_regblock (regcache
, NULL
);
4968 /* We can generally guess at a PC, although this will be
4969 misleading for while-stepping frames and multi-location
4971 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4973 regcache_write_pc (regcache
, tpoint
->address
);
4976 supply_regblock (regcache
, dataptr
);
4982 traceframe_get_pc (struct traceframe
*tframe
)
4984 struct regcache regcache
;
4985 unsigned char *dataptr
;
4987 dataptr
= traceframe_find_regblock (tframe
, -1);
4988 if (dataptr
== NULL
)
4991 init_register_cache (®cache
, dataptr
);
4992 return regcache_read_pc (®cache
);
4995 /* Read a requested block of memory from a trace frame. */
4998 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
4999 unsigned char *buf
, ULONGEST length
,
5002 struct traceframe
*tframe
;
5003 unsigned char *database
, *dataptr
;
5004 unsigned int datasize
;
5006 unsigned short mlen
;
5008 trace_debug ("traceframe_read_mem");
5010 tframe
= find_traceframe (tfnum
);
5014 trace_debug ("traceframe %d not found", tfnum
);
5018 datasize
= tframe
->data_size
;
5019 database
= dataptr
= &tframe
->data
[0];
5021 /* Iterate through a traceframe's blocks, looking for memory. */
5022 while ((dataptr
= traceframe_find_block_type (dataptr
,
5024 - (dataptr
- database
),
5025 tfnum
, 'M')) != NULL
)
5027 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5028 dataptr
+= sizeof (maddr
);
5029 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5030 dataptr
+= sizeof (mlen
);
5031 trace_debug ("traceframe %d has %d bytes at %s",
5032 tfnum
, mlen
, paddress (maddr
));
5034 /* If the block includes the first part of the desired range,
5035 return as much it has; GDB will re-request the remainder,
5036 which might be in a different block of this trace frame. */
5037 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5039 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5043 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5048 /* Skip over this block. */
5052 trace_debug ("traceframe %d has no memory data for the desired region",
5060 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5063 struct traceframe
*tframe
;
5064 unsigned char *database
, *dataptr
;
5065 unsigned int datasize
;
5068 trace_debug ("traceframe_read_tsv");
5070 tfnum
= current_traceframe
;
5074 trace_debug ("no current traceframe");
5078 tframe
= find_traceframe (tfnum
);
5082 trace_debug ("traceframe %d not found", tfnum
);
5086 datasize
= tframe
->data_size
;
5087 database
= dataptr
= &tframe
->data
[0];
5089 /* Iterate through a traceframe's blocks, looking for the tsv. */
5090 while ((dataptr
= traceframe_find_block_type (dataptr
,
5092 - (dataptr
- database
),
5093 tfnum
, 'V')) != NULL
)
5095 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5096 dataptr
+= sizeof (vnum
);
5098 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5100 /* Check that this is the variable we want. */
5103 memcpy (val
, dataptr
, sizeof (*val
));
5107 /* Skip over this block. */
5108 dataptr
+= sizeof (LONGEST
);
5111 trace_debug ("traceframe %d has no data for variable %d",
5116 /* Read a requested block of static tracepoint data from a trace
5120 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5121 unsigned char *buf
, ULONGEST length
,
5124 struct traceframe
*tframe
;
5125 unsigned char *database
, *dataptr
;
5126 unsigned int datasize
;
5127 unsigned short mlen
;
5129 trace_debug ("traceframe_read_sdata");
5131 tframe
= find_traceframe (tfnum
);
5135 trace_debug ("traceframe %d not found", tfnum
);
5139 datasize
= tframe
->data_size
;
5140 database
= &tframe
->data
[0];
5142 /* Iterate through a traceframe's blocks, looking for static
5144 dataptr
= traceframe_find_block_type (database
, datasize
,
5146 if (dataptr
!= NULL
)
5148 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5149 dataptr
+= sizeof (mlen
);
5152 if (offset
+ length
> mlen
)
5153 length
= mlen
- offset
;
5155 memcpy (buf
, dataptr
, length
);
5163 trace_debug ("traceframe %d has no static trace data", tfnum
);
5169 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5170 object. DATA is pointer to a struct buffer holding the
5171 traceframe-info object being built. */
5174 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5176 struct buffer
*buffer
= data
;
5182 unsigned short mlen
;
5185 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5186 dataptr
+= sizeof (maddr
);
5187 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5188 dataptr
+= sizeof (mlen
);
5189 buffer_xml_printf (buffer
,
5190 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5191 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5201 warning ("Unhandled trace block type (%d) '%c ' "
5202 "while building trace frame info.",
5203 blocktype
, blocktype
);
5210 /* Build a traceframe-info object for traceframe number TFNUM into
5214 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5216 struct traceframe
*tframe
;
5218 trace_debug ("traceframe_read_info");
5220 tframe
= find_traceframe (tfnum
);
5224 trace_debug ("traceframe %d not found", tfnum
);
5228 buffer_grow_str (buffer
, "<traceframe-info>\n");
5229 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5230 tfnum
, build_traceframe_info_xml
, buffer
);
5231 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5235 /* Return the first fast tracepoint whose jump pad contains PC. */
5237 static struct tracepoint
*
5238 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5240 struct tracepoint
*tpoint
;
5242 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5243 if (tpoint
->type
== fast_tracepoint
)
5244 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5250 /* Return the first fast tracepoint whose trampoline contains PC. */
5252 static struct tracepoint
*
5253 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5255 struct tracepoint
*tpoint
;
5257 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5259 if (tpoint
->type
== fast_tracepoint
5260 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5267 /* Return GDBserver's tracepoint that matches the IP Agent's
5268 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5271 static struct tracepoint
*
5272 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5274 struct tracepoint
*tpoint
;
5276 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5277 if (tpoint
->type
== fast_tracepoint
)
5278 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5286 /* The type of the object that is used to synchronize fast tracepoint
5289 typedef struct collecting_t
5291 /* The fast tracepoint number currently collecting. */
5294 /* A number that GDBserver can use to identify the thread that is
5295 presently holding the collect lock. This need not (and usually
5296 is not) the thread id, as getting the current thread ID usually
5297 requires a system call, which we want to avoid like the plague.
5298 Usually this is thread's TCB, found in the TLS (pseudo-)
5299 register, which is readable with a single insn on several
5301 uintptr_t thread_area
;
5304 #ifndef IN_PROCESS_AGENT
5307 force_unlock_trace_buffer (void)
5309 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5312 /* Check if the thread identified by THREAD_AREA which is stopped at
5313 STOP_PC, is presently locking the fast tracepoint collection, and
5314 if so, gather some status of said collection. Returns 0 if the
5315 thread isn't collecting or in the jump pad at all. 1, if in the
5316 jump pad (or within gdb_collect) and hasn't executed the adjusted
5317 original insn yet (can set a breakpoint there and run to it). 2,
5318 if presently executing the adjusted original insn --- in which
5319 case, if we want to move the thread out of the jump pad, we need to
5320 single-step it until this function returns 0. */
5323 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5325 struct fast_tpoint_collect_status
*status
)
5327 CORE_ADDR ipa_collecting
;
5328 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5329 CORE_ADDR ipa_gdb_trampoline_buffer
;
5330 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5331 struct tracepoint
*tpoint
;
5332 int needs_breakpoint
;
5334 /* The thread THREAD_AREA is either:
5336 0. not collecting at all, not within the jump pad, or within
5337 gdb_collect or one of its callees.
5339 1. in the jump pad and haven't reached gdb_collect
5341 2. within gdb_collect (out of the jump pad) (collect is set)
5343 3. we're in the jump pad, after gdb_collect having returned,
5344 possibly executing the adjusted insns.
5346 For cases 1 and 3, `collecting' may or not be set. The jump pad
5347 doesn't have any complicated jump logic, so we can tell if the
5348 thread is executing the adjust original insn or not by just
5349 matching STOP_PC with known jump pad addresses. If we it isn't
5350 yet executing the original insn, set a breakpoint there, and let
5351 the thread run to it, so to quickly step over a possible (many
5352 insns) gdb_collect call. Otherwise, or when the breakpoint is
5353 hit, only a few (small number of) insns are left to be executed
5354 in the jump pad. Single-step the thread until it leaves the
5359 needs_breakpoint
= 0;
5360 trace_debug ("fast_tracepoint_collecting");
5362 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5363 &ipa_gdb_jump_pad_buffer
))
5364 fatal ("error extracting `gdb_jump_pad_buffer'");
5365 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5366 &ipa_gdb_jump_pad_buffer_end
))
5367 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5369 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5370 &ipa_gdb_trampoline_buffer
))
5371 fatal ("error extracting `gdb_trampoline_buffer'");
5372 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5373 &ipa_gdb_trampoline_buffer_end
))
5374 fatal ("error extracting `gdb_trampoline_buffer_end'");
5376 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5377 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5379 /* We can tell which tracepoint(s) the thread is collecting by
5380 matching the jump pad address back to the tracepoint. */
5381 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5384 warning ("in jump pad, but no matching tpoint?");
5389 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5391 tpoint
->number
, paddress (tpoint
->address
),
5392 paddress (tpoint
->jump_pad
),
5393 paddress (tpoint
->jump_pad_end
),
5394 paddress (tpoint
->adjusted_insn_addr
),
5395 paddress (tpoint
->adjusted_insn_addr_end
));
5398 /* Definitely in the jump pad. May or may not need
5399 fast-exit-jump-pad breakpoint. */
5400 if (tpoint
->jump_pad
<= stop_pc
5401 && stop_pc
< tpoint
->adjusted_insn_addr
)
5402 needs_breakpoint
= 1;
5404 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5405 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5407 /* We can tell which tracepoint(s) the thread is collecting by
5408 matching the trampoline address back to the tracepoint. */
5409 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5412 warning ("in trampoline, but no matching tpoint?");
5417 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5418 tpoint
->number
, paddress (tpoint
->address
),
5419 paddress (tpoint
->trampoline
),
5420 paddress (tpoint
->trampoline_end
));
5423 /* Have not reached jump pad yet, but treat the trampoline as a
5424 part of the jump pad that is before the adjusted original
5426 needs_breakpoint
= 1;
5430 collecting_t ipa_collecting_obj
;
5432 /* If `collecting' is set/locked, then the THREAD_AREA thread
5433 may or not be the one holding the lock. We have to read the
5434 lock to find out. */
5436 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5439 trace_debug ("fast_tracepoint_collecting:"
5440 " failed reading 'collecting' in the inferior");
5444 if (!ipa_collecting
)
5446 trace_debug ("fast_tracepoint_collecting: not collecting"
5447 " (and nobody is).");
5451 /* Some thread is collecting. Check which. */
5452 if (read_inferior_memory (ipa_collecting
,
5453 (unsigned char *) &ipa_collecting_obj
,
5454 sizeof (ipa_collecting_obj
)) != 0)
5457 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5459 trace_debug ("fast_tracepoint_collecting: not collecting "
5460 "(another thread is)");
5465 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5468 warning ("fast_tracepoint_collecting: collecting, "
5469 "but tpoint %s not found?",
5470 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5474 /* The thread is within `gdb_collect', skip over the rest of
5475 fast tracepoint collection quickly using a breakpoint. */
5476 needs_breakpoint
= 1;
5479 /* The caller wants a bit of status detail. */
5482 status
->tpoint_num
= tpoint
->number
;
5483 status
->tpoint_addr
= tpoint
->address
;
5484 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5485 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5488 if (needs_breakpoint
)
5490 /* Hasn't executed the original instruction yet. Set breakpoint
5491 there, and wait till it's hit, then single-step until exiting
5495 fast_tracepoint_collecting, returning continue-until-break at %s",
5496 paddress (tpoint
->adjusted_insn_addr
));
5498 return 1; /* continue */
5502 /* Just single-step until exiting the jump pad. */
5504 trace_debug ("fast_tracepoint_collecting, returning "
5505 "need-single-step (%s-%s)",
5506 paddress (tpoint
->adjusted_insn_addr
),
5507 paddress (tpoint
->adjusted_insn_addr_end
));
5509 return 2; /* single-step */
5515 #ifdef IN_PROCESS_AGENT
5517 /* The global fast tracepoint collect lock. Points to a collecting_t
5518 object built on the stack by the jump pad, if presently locked;
5519 NULL if it isn't locked. Note that this lock *must* be set while
5520 executing any *function other than the jump pad. See
5521 fast_tracepoint_collecting. */
5522 static collecting_t
* ATTR_USED collecting
;
5524 /* This routine, called from the jump pad (in asm) is designed to be
5525 called from the jump pads of fast tracepoints, thus it is on the
5528 IP_AGENT_EXPORT
void ATTR_USED
5529 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5531 struct fast_tracepoint_ctx ctx
;
5533 /* Don't do anything until the trace run is completely set up. */
5537 ctx
.base
.type
= fast_tracepoint
;
5539 ctx
.regcache_initted
= 0;
5540 /* Wrap the regblock in a register cache (in the stack, we don't
5541 want to malloc here). */
5542 ctx
.regspace
= alloca (register_cache_size ());
5543 if (ctx
.regspace
== NULL
)
5545 trace_debug ("Trace buffer block allocation failed, skipping");
5549 for (ctx
.tpoint
= tpoint
;
5550 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5551 ctx
.tpoint
= ctx
.tpoint
->next
)
5553 if (!ctx
.tpoint
->enabled
)
5556 /* Multiple tracepoints of different types, such as fast tracepoint and
5557 static tracepoint, can be set at the same address. */
5558 if (ctx
.tpoint
->type
!= tpoint
->type
)
5561 /* Test the condition if present, and collect if true. */
5562 if (ctx
.tpoint
->cond
== NULL
5563 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5566 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5567 ctx
.tpoint
->address
, ctx
.tpoint
);
5569 /* Note that this will cause original insns to be written back
5570 to where we jumped from, but that's OK because we're jumping
5571 back to the next whole instruction. This will go badly if
5572 instruction restoration is not atomic though. */
5573 if (stopping_tracepoint
5574 || trace_buffer_is_full
5575 || expr_eval_result
!= expr_eval_no_error
)
5583 /* If there was a condition and it evaluated to false, the only
5584 way we would stop tracing is if there was an error during
5585 condition expression evaluation. */
5586 if (expr_eval_result
!= expr_eval_no_error
)
5597 #ifndef IN_PROCESS_AGENT
5600 get_raw_reg_func_addr (void)
5602 return ipa_sym_addrs
.addr_get_raw_reg
;
5606 get_get_tsv_func_addr (void)
5608 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5612 get_set_tsv_func_addr (void)
5614 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5618 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5619 CORE_ADDR
*jump_entry
)
5621 CORE_ADDR entry_point
= *jump_entry
;
5622 enum eval_result_type err
;
5624 trace_debug ("Starting condition compilation for tracepoint %d\n",
5627 /* Initialize the global pointer to the code being built. */
5628 current_insn_ptr
= *jump_entry
;
5632 err
= compile_bytecodes (tpoint
->cond
);
5634 if (err
== expr_eval_no_error
)
5638 /* Record the beginning of the compiled code. */
5639 tpoint
->compiled_cond
= entry_point
;
5641 trace_debug ("Condition compilation for tracepoint %d complete\n",
5646 /* Leave the unfinished code in situ, but don't point to it. */
5648 tpoint
->compiled_cond
= 0;
5650 trace_debug ("Condition compilation for tracepoint %d failed, "
5652 tpoint
->number
, err
);
5655 /* Update the code pointer passed in. Note that we do this even if
5656 the compile fails, so that we can look at the partial results
5657 instead of letting them be overwritten. */
5658 *jump_entry
= current_insn_ptr
;
5660 /* Leave a gap, to aid dump decipherment. */
5664 /* We'll need to adjust these when we consider bi-arch setups, and big
5668 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5670 return write_inferior_memory (where
,
5671 (unsigned char *) &ptr
, sizeof (void *));
5674 /* The base pointer of the IPA's heap. This is the only memory the
5675 IPA is allowed to use. The IPA should _not_ call the inferior's
5676 `malloc' during operation. That'd be slow, and, most importantly,
5677 it may not be safe. We may be collecting a tracepoint in a signal
5678 handler, for example. */
5679 static CORE_ADDR target_tp_heap
;
5681 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5685 target_malloc (ULONGEST size
)
5689 if (target_tp_heap
== 0)
5691 /* We have the pointer *address*, need what it points to. */
5692 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5694 fatal ("could get target heap head pointer");
5697 ptr
= target_tp_heap
;
5698 target_tp_heap
+= size
;
5700 /* Pad to 8-byte alignment. */
5701 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5707 download_agent_expr (struct agent_expr
*expr
)
5709 CORE_ADDR expr_addr
;
5710 CORE_ADDR expr_bytes
;
5712 expr_addr
= target_malloc (sizeof (*expr
));
5713 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5715 expr_bytes
= target_malloc (expr
->length
);
5716 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5718 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5723 /* Align V up to N bits. */
5724 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5726 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5729 download_tracepoint_1 (struct tracepoint
*tpoint
)
5731 struct tracepoint target_tracepoint
;
5732 CORE_ADDR tpptr
= 0;
5734 gdb_assert (tpoint
->type
== fast_tracepoint
5735 || tpoint
->type
== static_tracepoint
);
5737 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5739 CORE_ADDR jentry
, jump_entry
;
5741 jentry
= jump_entry
= get_jump_space_head ();
5743 if (tpoint
->cond
!= NULL
)
5745 /* Pad to 8-byte alignment. (needed?) */
5746 /* Actually this should be left for the target to
5748 jentry
= UALIGN (jentry
, 8);
5750 compile_tracepoint_condition (tpoint
, &jentry
);
5753 /* Pad to 8-byte alignment. */
5754 jentry
= UALIGN (jentry
, 8);
5755 claim_jump_space (jentry
- jump_entry
);
5758 target_tracepoint
= *tpoint
;
5760 tpptr
= target_malloc (sizeof (*tpoint
));
5761 tpoint
->obj_addr_on_target
= tpptr
;
5763 /* Write the whole object. We'll fix up its pointers in a bit.
5764 Assume no next for now. This is fixed up above on the next
5765 iteration, if there's any. */
5766 target_tracepoint
.next
= NULL
;
5767 /* Need to clear this here too, since we're downloading the
5768 tracepoints before clearing our own copy. */
5769 target_tracepoint
.hit_count
= 0;
5771 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5772 sizeof (target_tracepoint
));
5775 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5777 download_agent_expr (tpoint
->cond
));
5779 if (tpoint
->numactions
)
5782 CORE_ADDR actions_array
;
5784 /* The pointers array. */
5786 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
5787 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5791 /* Now for each pointer, download the action. */
5792 for (i
= 0; i
< tpoint
->numactions
; i
++)
5794 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5795 CORE_ADDR ipa_action
= action
->ops
->download (action
);
5797 if (ipa_action
!= 0)
5798 write_inferior_data_ptr
5799 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
5806 download_tracepoint (struct tracepoint
*tpoint
)
5808 struct tracepoint
*tp
, *tp_prev
;
5810 if (tpoint
->type
!= fast_tracepoint
5811 && tpoint
->type
!= static_tracepoint
)
5814 download_tracepoint_1 (tpoint
);
5816 /* Find the previous entry of TPOINT, which is fast tracepoint or
5817 static tracepoint. */
5819 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
5821 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
5827 CORE_ADDR tp_prev_target_next_addr
;
5829 /* Insert TPOINT after TP_PREV in IPA. */
5830 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
5831 + offsetof (struct tracepoint
, next
),
5832 &tp_prev_target_next_addr
))
5833 fatal ("error reading `tp_prev->next'");
5835 /* tpoint->next = tp_prev->next */
5836 write_inferior_data_ptr (tpoint
->obj_addr_on_target
5837 + offsetof (struct tracepoint
, next
),
5838 tp_prev_target_next_addr
);
5839 /* tp_prev->next = tpoint */
5840 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
5841 + offsetof (struct tracepoint
, next
),
5842 tpoint
->obj_addr_on_target
);
5845 /* First object in list, set the head pointer in the
5847 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
5848 tpoint
->obj_addr_on_target
);
5853 download_tracepoints (void)
5855 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
5856 struct tracepoint
*tpoint
;
5858 /* Start out empty. */
5859 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
5861 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5863 if (tpoint
->type
!= fast_tracepoint
5864 && tpoint
->type
!= static_tracepoint
)
5869 download_tracepoint_1 (tpoint
);
5871 tpptr
= tpoint
->obj_addr_on_target
;
5873 if (tpoint
== tracepoints
)
5875 /* First object in list, set the head pointer in the
5877 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
5881 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
5889 download_trace_state_variables (void)
5891 CORE_ADDR ptr
= 0, prev_ptr
= 0;
5892 struct trace_state_variable
*tsv
;
5894 /* Start out empty. */
5895 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
5897 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
5899 struct trace_state_variable target_tsv
;
5901 /* TSV's with a getter have been initialized equally in both the
5902 inferior and GDBserver. Skip them. */
5903 if (tsv
->getter
!= NULL
)
5909 ptr
= target_malloc (sizeof (*tsv
));
5911 if (tsv
== trace_state_variables
)
5913 /* First object in list, set the head pointer in the
5916 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
5921 write_inferior_data_ptr (prev_ptr
5922 + offsetof (struct trace_state_variable
,
5927 /* Write the whole object. We'll fix up its pointers in a bit.
5928 Assume no next, fixup when needed. */
5929 target_tsv
.next
= NULL
;
5931 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
5932 sizeof (target_tsv
));
5934 if (tsv
->name
!= NULL
)
5936 size_t size
= strlen (tsv
->name
) + 1;
5937 CORE_ADDR name_addr
= target_malloc (size
);
5938 write_inferior_memory (name_addr
,
5939 (unsigned char *) tsv
->name
, size
);
5940 write_inferior_data_ptr (ptr
5941 + offsetof (struct trace_state_variable
,
5946 if (tsv
->getter
!= NULL
)
5948 fatal ("what to do with these?");
5954 /* Fixup the next pointer in the last item in the list. */
5955 write_inferior_data_ptr (prev_ptr
5956 + offsetof (struct trace_state_variable
,
5961 /* Upload complete trace frames out of the IP Agent's trace buffer
5962 into GDBserver's trace buffer. This always uploads either all or
5963 no trace frames. This is the counter part of
5964 `trace_alloc_trace_buffer'. See its description of the atomic
5965 synching mechanism. */
5968 upload_fast_traceframes (void)
5970 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
5971 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
5973 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
5974 unsigned int curr_tbctrl_idx
;
5975 unsigned int ipa_trace_buffer_ctrl_curr
;
5976 unsigned int ipa_trace_buffer_ctrl_curr_old
;
5977 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
5978 struct breakpoint
*about_to_request_buffer_space_bkpt
;
5979 CORE_ADDR ipa_trace_buffer_lo
;
5980 CORE_ADDR ipa_trace_buffer_hi
;
5982 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
5983 &ipa_traceframe_read_count_racy
))
5985 /* This will happen in most targets if the current thread is
5990 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
5991 &ipa_traceframe_write_count_racy
))
5994 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
5995 ipa_traceframe_write_count_racy
5996 - ipa_traceframe_read_count_racy
,
5997 ipa_traceframe_write_count_racy
,
5998 ipa_traceframe_read_count_racy
);
6000 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6003 about_to_request_buffer_space_bkpt
6004 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6007 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6008 &ipa_trace_buffer_ctrl_curr
))
6011 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6013 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6016 unsigned int prev
, counter
;
6018 /* Update the token, with new counters, and the GDBserver stamp
6019 bit. Alway reuse the current TBC index. */
6020 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6021 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6023 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6029 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6030 ipa_trace_buffer_ctrl_curr
))
6033 trace_debug ("Lib: Committed %08x -> %08x",
6034 ipa_trace_buffer_ctrl_curr_old
,
6035 ipa_trace_buffer_ctrl_curr
);
6037 /* Re-read these, now that we've installed the
6038 `about_to_request_buffer_space' breakpoint/lock. A thread could
6039 have finished a traceframe between the last read of these
6040 counters and setting the breakpoint above. If we start
6041 uploading, we never want to leave this function with
6042 traceframe_read_count != 0, otherwise, GDBserver could end up
6043 incrementing the counter tokens more than once (due to event loop
6044 nesting), which would break the IP agent's "effective" detection
6045 (see trace_alloc_trace_buffer). */
6046 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6047 &ipa_traceframe_read_count
))
6049 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6050 &ipa_traceframe_write_count
))
6055 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6056 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6057 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6059 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6060 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6061 trace_debug ("note that ipa_traceframe_count's parts changed");
6064 /* Get the address of the current TBC object (the IP agent has an
6065 array of 3 such objects). The index is stored in the TBC
6067 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6068 ipa_trace_buffer_ctrl_addr
6069 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6071 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6072 (unsigned char *) &ipa_trace_buffer_ctrl
,
6073 sizeof (struct ipa_trace_buffer_control
)))
6076 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6077 &ipa_trace_buffer_lo
))
6079 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6080 &ipa_trace_buffer_hi
))
6083 /* Offsets are easier to grok for debugging than raw addresses,
6084 especially for the small trace buffer sizes that are useful for
6086 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6087 "endfree=%d wrap=%d hi=%d",
6089 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6090 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6091 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6092 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6093 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6095 /* Note that the IPA's buffer is always circular. */
6097 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6099 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6100 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6102 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6103 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6104 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6105 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6108 tf
= IPA_FIRST_TRACEFRAME ();
6110 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6112 struct tracepoint
*tpoint
;
6113 struct traceframe
*tframe
;
6114 unsigned char *block
;
6115 struct traceframe ipa_tframe
;
6117 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6118 offsetof (struct traceframe
, data
)))
6119 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6121 if (ipa_tframe
.tpnum
== 0)
6122 fatal ("Uploading: No (more) fast traceframes, but "
6123 "ipa_traceframe_count == %u??\n",
6124 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6126 /* Note that this will be incorrect for multi-location
6128 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6130 tframe
= add_traceframe (tpoint
);
6133 trace_buffer_is_full
= 1;
6134 trace_debug ("Uploading: trace buffer is full");
6138 /* Copy the whole set of blocks in one go for now. FIXME:
6139 split this in smaller blocks. */
6140 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6143 if (read_inferior_memory (tf
6144 + offsetof (struct traceframe
, data
),
6145 block
, ipa_tframe
.data_size
))
6146 error ("Uploading: Couldn't read traceframe data at %s\n",
6147 paddress (tf
+ offsetof (struct traceframe
, data
)));
6150 trace_debug ("Uploading: traceframe didn't fit");
6151 finish_traceframe (tframe
);
6154 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6156 /* If we freed the traceframe that wrapped around, go back
6157 to the non-wrap case. */
6158 if (tf
< ipa_trace_buffer_ctrl
.start
)
6160 trace_debug ("Lib: Discarding past the wraparound");
6161 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6163 ipa_trace_buffer_ctrl
.start
= tf
;
6164 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6165 ++ipa_traceframe_read_count
;
6167 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6168 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6170 trace_debug ("Lib: buffer is fully empty. "
6171 "Trace buffer [%d] start=%d free=%d endfree=%d",
6173 (int) (ipa_trace_buffer_ctrl
.start
6174 - ipa_trace_buffer_lo
),
6175 (int) (ipa_trace_buffer_ctrl
.free
6176 - ipa_trace_buffer_lo
),
6177 (int) (ipa_trace_buffer_ctrl
.end_free
6178 - ipa_trace_buffer_lo
));
6180 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6181 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6182 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6183 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6186 trace_debug ("Uploaded a traceframe\n"
6187 "Lib: Trace buffer [%d] start=%d free=%d "
6188 "endfree=%d wrap=%d hi=%d",
6190 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6191 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6192 (int) (ipa_trace_buffer_ctrl
.end_free
6193 - ipa_trace_buffer_lo
),
6194 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6195 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6198 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6199 (unsigned char *) &ipa_trace_buffer_ctrl
,
6200 sizeof (struct ipa_trace_buffer_control
)))
6203 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6204 ipa_traceframe_read_count
);
6206 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6209 cancel_breakpoints ();
6211 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6212 about_to_request_buffer_space_bkpt
= NULL
;
6216 if (trace_buffer_is_full
)
6221 #ifdef IN_PROCESS_AGENT
6223 IP_AGENT_EXPORT
int ust_loaded
;
6224 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6228 /* Static tracepoints. */
6230 /* UST puts a "struct tracepoint" in the global namespace, which
6231 conflicts with our tracepoint. Arguably, being a library, it
6232 shouldn't take ownership of such a generic name. We work around it
6234 #define tracepoint ust_tracepoint
6235 #include <ust/ust.h>
6238 extern int serialize_to_text (char *outbuf
, int bufsize
,
6239 const char *fmt
, va_list ap
);
6241 #define GDB_PROBE_NAME "gdb"
6243 /* We dynamically search for the UST symbols instead of linking them
6244 in. This lets the user decide if the application uses static
6245 tracepoints, instead of always pulling libust.so in. This vector
6246 holds pointers to all functions we care about. */
6250 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6251 const char *fmt
, va_list ap
);
6253 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6254 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6256 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6258 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6261 void (*marker_iter_start
) (struct marker_iter
*iter
);
6262 void (*marker_iter_next
) (struct marker_iter
*iter
);
6263 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6264 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6269 /* Cast through typeof to catch incompatible API changes. Since UST
6270 only builds with gcc, we can freely use gcc extensions here
6272 #define GET_UST_SYM(SYM) \
6275 if (ust_ops.SYM == NULL) \
6276 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6277 if (ust_ops.SYM == NULL) \
6281 #define USTF(SYM) ust_ops.SYM
6283 /* Get pointers to all libust.so functions we care about. */
6288 GET_UST_SYM (serialize_to_text
);
6290 GET_UST_SYM (ltt_probe_register
);
6291 GET_UST_SYM (ltt_probe_unregister
);
6292 GET_UST_SYM (ltt_marker_connect
);
6293 GET_UST_SYM (ltt_marker_disconnect
);
6295 GET_UST_SYM (marker_iter_start
);
6296 GET_UST_SYM (marker_iter_next
);
6297 GET_UST_SYM (marker_iter_stop
);
6298 GET_UST_SYM (marker_iter_reset
);
6304 /* Given an UST marker, return the matching gdb static tracepoint.
6305 The match is done by address. */
6307 static struct tracepoint
*
6308 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6310 struct tracepoint
*tpoint
;
6312 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6314 if (tpoint
->type
!= static_tracepoint
)
6317 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6324 /* The probe function we install on lttng/ust markers. Whenever a
6325 probed ust marker is hit, this function is called. This is similar
6326 to gdb_collect, only for static tracepoints, instead of fast
6330 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6331 struct registers
*regs
, void *call_private
,
6332 const char *fmt
, va_list *args
)
6334 struct tracepoint
*tpoint
;
6335 struct static_tracepoint_ctx ctx
;
6337 /* Don't do anything until the trace run is completely set up. */
6340 trace_debug ("gdb_probe: not tracing\n");
6344 ctx
.base
.type
= static_tracepoint
;
6345 ctx
.regcache_initted
= 0;
6350 /* Wrap the regblock in a register cache (in the stack, we don't
6351 want to malloc here). */
6352 ctx
.regspace
= alloca (register_cache_size ());
6353 if (ctx
.regspace
== NULL
)
6355 trace_debug ("Trace buffer block allocation failed, skipping");
6359 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6362 trace_debug ("gdb_probe: marker not known: "
6363 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6364 mdata
->location
, mdata
->channel
,
6365 mdata
->name
, mdata
->format
);
6369 if (!tpoint
->enabled
)
6371 trace_debug ("gdb_probe: tracepoint disabled");
6375 ctx
.tpoint
= tpoint
;
6377 trace_debug ("gdb_probe: collecting marker: "
6378 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6379 mdata
->location
, mdata
->channel
,
6380 mdata
->name
, mdata
->format
);
6382 /* Test the condition if present, and collect if true. */
6383 if (tpoint
->cond
== NULL
6384 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6387 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6388 tpoint
->address
, tpoint
);
6390 if (stopping_tracepoint
6391 || trace_buffer_is_full
6392 || expr_eval_result
!= expr_eval_no_error
)
6397 /* If there was a condition and it evaluated to false, the only
6398 way we would stop tracing is if there was an error during
6399 condition expression evaluation. */
6400 if (expr_eval_result
!= expr_eval_no_error
)
6405 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6406 static tracepoint string data. This is a string passed to the
6407 tracing library by the user, at the time of the tracepoint marker
6408 call. E.g., in the UST marker call:
6410 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6412 the collected data is "str FOOBAZ".
6416 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6417 struct traceframe
*tframe
)
6419 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6420 unsigned char *bufspace
;
6423 unsigned short blocklen
;
6427 trace_debug ("Wanted to collect static trace data, "
6428 "but there's no static trace data");
6432 va_copy (copy
, *umd
->args
);
6433 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6436 trace_debug ("Want to collect ust data");
6438 /* 'S' + size + string */
6439 bufspace
= add_traceframe_block (tframe
,
6440 1 + sizeof (blocklen
) + size
+ 1);
6441 if (bufspace
== NULL
)
6443 trace_debug ("Trace buffer block allocation failed, skipping");
6447 /* Identify a static trace data block. */
6450 blocklen
= size
+ 1;
6451 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6453 va_copy (copy
, *umd
->args
);
6454 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6455 size
+ 1, umd
->fmt
, copy
);
6458 trace_debug ("Storing static tracepoint data in regblock: %s",
6459 bufspace
+ 1 + sizeof (blocklen
));
6462 /* The probe to register with lttng/ust. */
6463 static struct ltt_available_probe gdb_ust_probe
=
6470 #endif /* HAVE_UST */
6471 #endif /* IN_PROCESS_AGENT */
6473 #ifndef IN_PROCESS_AGENT
6475 /* Ask the in-process agent to run a command. Since we don't want to
6476 have to handle the IPA hitting breakpoints while running the
6477 command, we pause all threads, remove all breakpoints, and then set
6478 the helper thread re-running. We communicate with the helper
6479 thread by means of direct memory xfering, and a socket for
6483 run_inferior_command (char *cmd
)
6486 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
6488 trace_debug ("run_inferior_command: running: %s", cmd
);
6491 uninsert_all_breakpoints ();
6493 err
= agent_run_command (pid
, (const char *) cmd
);
6495 reinsert_all_breakpoints ();
6501 #else /* !IN_PROCESS_AGENT */
6503 #include <sys/socket.h>
6506 #ifndef UNIX_PATH_MAX
6507 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6510 /* Where we put the socked used for synchronization. */
6511 #define SOCK_DIR P_tmpdir
6513 /* Thread ID of the helper thread. GDBserver reads this to know which
6514 is the help thread. This is an LWP id on Linux. */
6515 int helper_thread_id
;
6518 init_named_socket (const char *name
)
6521 struct sockaddr_un addr
;
6523 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6526 warning ("socket creation failed: %s", strerror (errno
));
6530 addr
.sun_family
= AF_UNIX
;
6532 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6533 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6535 result
= access (name
, F_OK
);
6539 result
= unlink (name
);
6542 warning ("unlink failed: %s", strerror (errno
));
6546 warning ("socket %s already exists; overwriting", name
);
6549 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6552 warning ("bind failed: %s", strerror (errno
));
6557 result
= listen (fd
, 1);
6560 warning ("listen: %s", strerror (errno
));
6569 gdb_agent_socket_init (void)
6572 char name
[UNIX_PATH_MAX
];
6574 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6575 SOCK_DIR
, getpid ());
6576 if (result
>= UNIX_PATH_MAX
)
6578 trace_debug ("string overflow allocating socket name");
6582 fd
= init_named_socket (name
);
6584 warning ("Error initializing named socket (%s) for communication with the "
6585 "ust helper thread. Check that directory exists and that it "
6586 "is writable.", name
);
6593 /* The next marker to be returned on a qTsSTM command. */
6594 static const struct marker
*next_st
;
6596 /* Returns the first known marker. */
6601 struct marker_iter iter
;
6603 USTF(marker_iter_reset
) (&iter
);
6604 USTF(marker_iter_start
) (&iter
);
6609 /* Returns the marker following M. */
6611 const struct marker
*
6612 next_marker (const struct marker
*m
)
6614 struct marker_iter iter
;
6616 USTF(marker_iter_reset
) (&iter
);
6617 USTF(marker_iter_start
) (&iter
);
6619 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6621 if (iter
.marker
== m
)
6623 USTF(marker_iter_next
) (&iter
);
6631 /* Return an hexstr version of the STR C string, fit for sending to
6635 cstr_to_hexstr (const char *str
)
6637 int len
= strlen (str
);
6638 char *hexstr
= xmalloc (len
* 2 + 1);
6639 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6643 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6647 response_ust_marker (char *packet
, const struct marker
*st
)
6649 char *strid
, *format
, *tmp
;
6651 next_st
= next_marker (st
);
6653 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6654 strlen (st
->name
) + 1);
6655 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6657 strid
= cstr_to_hexstr (tmp
);
6660 format
= cstr_to_hexstr (st
->format
);
6662 sprintf (packet
, "m%s:%s:%s",
6663 paddress ((uintptr_t) st
->location
),
6671 /* Return the first static tracepoint, and initialize the state
6672 machine that will iterate through all the static tracepoints. */
6675 cmd_qtfstm (char *packet
)
6677 trace_debug ("Returning first trace state variable definition");
6679 if (first_marker ())
6680 response_ust_marker (packet
, first_marker ());
6682 strcpy (packet
, "l");
6685 /* Return additional trace state variable definitions. */
6688 cmd_qtsstm (char *packet
)
6690 trace_debug ("Returning static tracepoint");
6693 response_ust_marker (packet
, next_st
);
6695 strcpy (packet
, "l");
6698 /* Disconnect the GDB probe from a marker at a given address. */
6701 unprobe_marker_at (char *packet
)
6705 struct marker_iter iter
;
6707 p
+= sizeof ("unprobe_marker_at:") - 1;
6709 p
= unpack_varlen_hex (p
, &address
);
6711 USTF(marker_iter_reset
) (&iter
);
6712 USTF(marker_iter_start
) (&iter
);
6713 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6714 if ((uintptr_t ) iter
.marker
->location
== address
)
6718 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
6722 warning ("could not disable marker %s/%s",
6723 iter
.marker
->channel
, iter
.marker
->name
);
6728 /* Connect the GDB probe to a marker at a given address. */
6731 probe_marker_at (char *packet
)
6735 struct marker_iter iter
;
6738 p
+= sizeof ("probe_marker_at:") - 1;
6740 p
= unpack_varlen_hex (p
, &address
);
6742 USTF(marker_iter_reset
) (&iter
);
6744 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6746 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6747 if ((uintptr_t ) m
->location
== address
)
6751 trace_debug ("found marker for address. "
6752 "ltt_marker_connect (marker = %s/%s)",
6753 m
->channel
, m
->name
);
6755 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
6757 if (result
&& result
!= -EEXIST
)
6758 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
6759 m
->channel
, m
->name
, -result
);
6763 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
6764 m
->channel
, m
->name
);
6768 strcpy (packet
, "OK");
6772 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
6777 cmd_qtstmat (char *packet
)
6781 struct marker_iter iter
;
6784 p
+= sizeof ("qTSTMat:") - 1;
6786 p
= unpack_varlen_hex (p
, &address
);
6788 USTF(marker_iter_reset
) (&iter
);
6790 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6792 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6793 if ((uintptr_t ) m
->location
== address
)
6795 response_ust_marker (packet
, m
);
6799 strcpy (packet
, "l");
6809 USTF(ltt_probe_register
) (&gdb_ust_probe
);
6812 #endif /* HAVE_UST */
6814 #include <sys/syscall.h>
6816 /* Helper thread of agent. */
6819 gdb_agent_helper_thread (void *arg
)
6825 listen_fd
= gdb_agent_socket_init ();
6827 if (helper_thread_id
== 0)
6828 helper_thread_id
= syscall (SYS_gettid
);
6830 if (listen_fd
== -1)
6832 warning ("could not create sync socket\n");
6839 struct sockaddr_un sockaddr
;
6844 tmp
= sizeof (sockaddr
);
6848 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
6850 /* It seems an ERESTARTSYS can escape out of accept. */
6851 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
6855 warning ("Accept returned %d, error: %s\n",
6856 fd
, strerror (errno
));
6862 ret
= read (fd
, buf
, 1);
6863 } while (ret
== -1 && errno
== EINTR
);
6867 warning ("reading socket (fd=%d) failed with %s",
6868 fd
, strerror (errno
));
6876 if (strcmp ("qTfSTM", cmd_buf
) == 0)
6878 cmd_qtfstm (cmd_buf
);
6880 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
6882 cmd_qtsstm (cmd_buf
);
6884 else if (strncmp ("unprobe_marker_at:",
6886 sizeof ("unprobe_marker_at:") - 1) == 0)
6888 unprobe_marker_at (cmd_buf
);
6890 else if (strncmp ("probe_marker_at:",
6892 sizeof ("probe_marker_at:") - 1) == 0)
6894 probe_marker_at (cmd_buf
);
6896 else if (strncmp ("qTSTMat:",
6898 sizeof ("qTSTMat:") - 1) == 0)
6900 cmd_qtstmat (cmd_buf
);
6902 #endif /* HAVE_UST */
6905 /* Fix compiler's warning: ignoring return value of 'write'. */
6906 ret
= write (fd
, buf
, 1);
6915 #include <pthread.h>
6917 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
6920 gdb_agent_init (void)
6927 /* We want the helper thread to be as transparent as possible, so
6928 have it inherit an all-signals-blocked mask. */
6930 sigfillset (&new_mask
);
6931 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
6933 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
6935 res
= pthread_create (&thread
,
6937 gdb_agent_helper_thread
,
6940 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
6942 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
6944 while (helper_thread_id
== 0)
6952 #include <sys/mman.h>
6955 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
6956 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
6957 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
6958 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
6959 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
6960 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
6962 /* Record the result of getting buffer space for fast tracepoint
6963 trampolines. Any error message is copied, since caller may not be
6964 using persistent storage. */
6967 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
6969 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
6970 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
6972 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
6974 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
6977 static void __attribute__ ((constructor
))
6978 initialize_tracepoint_ftlib (void)
6980 initialize_tracepoint ();
6985 #endif /* IN_PROCESS_AGENT */
6987 /* Return a timestamp, expressed as microseconds of the usual Unix
6988 time. (As the result is a 64-bit number, it will not overflow any
6992 get_timestamp (void)
6996 if (gettimeofday (&tv
, 0) != 0)
6999 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7003 initialize_tracepoint (void)
7005 /* There currently no way to change the buffer size. */
7006 const int sizeOfBuffer
= 5 * 1024 * 1024;
7007 unsigned char *buf
= xmalloc (sizeOfBuffer
);
7008 init_trace_buffer (buf
, sizeOfBuffer
);
7010 /* Wire trace state variable 1 to be the timestamp. This will be
7011 uploaded to GDB upon connection and become one of its trace state
7012 variables. (In case you're wondering, if GDB already has a trace
7013 variable numbered 1, it will be renumbered.) */
7014 create_trace_state_variable (1, 0);
7015 set_trace_state_variable_name (1, "trace_timestamp");
7016 set_trace_state_variable_getter (1, get_timestamp
);
7018 #ifdef IN_PROCESS_AGENT
7023 pagesize
= sysconf (_SC_PAGE_SIZE
);
7027 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7029 #define SCRATCH_BUFFER_NPAGES 20
7031 /* Allocate scratch buffer aligned on a page boundary, at a low
7032 address (close to the main executable's code). */
7033 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7035 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7036 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7037 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7039 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7045 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7048 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7051 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7053 /* It's not a fatal error for something to go wrong with trampoline
7054 buffer setup, but it can be mysterious, so create a channel to
7055 report back on what went wrong, using a fixed size since we may
7056 not be able to allocate space later when the problem occurs. */
7057 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7059 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7061 initialize_low_tracepoint ();