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 long 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 %ld pass %ld",
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 %ld 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%lx:%lx", tpoint
->hit_count
, tpoint
->traceframe_usage
);
3564 /* State variables to help return all the tracepoint bits. */
3565 static struct tracepoint
*cur_tpoint
;
3566 static int cur_action
;
3567 static int cur_step_action
;
3568 static struct source_string
*cur_source_string
;
3569 static struct trace_state_variable
*cur_tsv
;
3571 /* Compose a response that is an imitation of the syntax by which the
3572 tracepoint was originally downloaded. */
3575 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3579 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
3580 paddress (tpoint
->address
),
3581 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3582 tpoint
->pass_count
);
3583 if (tpoint
->type
== fast_tracepoint
)
3584 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3585 else if (tpoint
->type
== static_tracepoint
)
3586 sprintf (packet
+ strlen (packet
), ":S");
3590 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3591 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3592 tpoint
->cond
->length
, buf
);
3597 /* Compose a response that is an imitation of the syntax by which the
3598 tracepoint action was originally downloaded (with the difference
3599 that due to the way we store the actions, this will output a packet
3600 per action, while GDB could have combined more than one action
3604 response_action (char *packet
, struct tracepoint
*tpoint
,
3605 char *taction
, int step
)
3607 sprintf (packet
, "%c%x:%s:%s",
3608 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3612 /* Compose a response that is an imitation of the syntax by which the
3613 tracepoint source piece was originally downloaded. */
3616 response_source (char *packet
,
3617 struct tracepoint
*tpoint
, struct source_string
*src
)
3622 len
= strlen (src
->str
);
3623 buf
= alloca (len
* 2 + 1);
3624 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3626 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3627 tpoint
->number
, paddress (tpoint
->address
),
3628 src
->type
, 0, len
, buf
);
3631 /* Return the first piece of tracepoint definition, and initialize the
3632 state machine that will iterate through all the tracepoint
3636 cmd_qtfp (char *packet
)
3638 trace_debug ("Returning first tracepoint definition piece");
3640 cur_tpoint
= tracepoints
;
3641 cur_action
= cur_step_action
= -1;
3642 cur_source_string
= NULL
;
3645 response_tracepoint (packet
, cur_tpoint
);
3647 strcpy (packet
, "l");
3650 /* Return additional pieces of tracepoint definition. Each action and
3651 stepping action must go into its own packet, because of packet size
3652 limits, and so we use state variables to deliver one piece at a
3656 cmd_qtsp (char *packet
)
3658 trace_debug ("Returning subsequent tracepoint definition piece");
3662 /* This case would normally never occur, but be prepared for
3664 strcpy (packet
, "l");
3666 else if (cur_action
< cur_tpoint
->numactions
- 1)
3669 response_action (packet
, cur_tpoint
,
3670 cur_tpoint
->actions_str
[cur_action
], 0);
3672 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3675 response_action (packet
, cur_tpoint
,
3676 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3678 else if ((cur_source_string
3679 ? cur_source_string
->next
3680 : cur_tpoint
->source_strings
))
3682 if (cur_source_string
)
3683 cur_source_string
= cur_source_string
->next
;
3685 cur_source_string
= cur_tpoint
->source_strings
;
3686 response_source (packet
, cur_tpoint
, cur_source_string
);
3690 cur_tpoint
= cur_tpoint
->next
;
3691 cur_action
= cur_step_action
= -1;
3692 cur_source_string
= NULL
;
3694 response_tracepoint (packet
, cur_tpoint
);
3696 strcpy (packet
, "l");
3700 /* Compose a response that is an imitation of the syntax by which the
3701 trace state variable was originally downloaded. */
3704 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3706 char *buf
= (char *) "";
3711 namelen
= strlen (tsv
->name
);
3712 buf
= alloca (namelen
* 2 + 1);
3713 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3716 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3717 tsv
->getter
? 1 : 0, buf
);
3720 /* Return the first trace state variable definition, and initialize
3721 the state machine that will iterate through all the tsv bits. */
3724 cmd_qtfv (char *packet
)
3726 trace_debug ("Returning first trace state variable definition");
3728 cur_tsv
= trace_state_variables
;
3731 response_tsv (packet
, cur_tsv
);
3733 strcpy (packet
, "l");
3736 /* Return additional trace state variable definitions. */
3739 cmd_qtsv (char *packet
)
3741 trace_debug ("Returning first trace state variable definition");
3745 /* This case would normally never occur, but be prepared for
3747 strcpy (packet
, "l");
3751 cur_tsv
= cur_tsv
->next
;
3753 response_tsv (packet
, cur_tsv
);
3755 strcpy (packet
, "l");
3758 strcpy (packet
, "l");
3761 /* Return the first static tracepoint marker, and initialize the state
3762 machine that will iterate through all the static tracepoints
3766 cmd_qtfstm (char *packet
)
3768 if (!maybe_write_ipa_ust_not_loaded (packet
))
3769 run_inferior_command (packet
);
3772 /* Return additional static tracepoints markers. */
3775 cmd_qtsstm (char *packet
)
3777 if (!maybe_write_ipa_ust_not_loaded (packet
))
3778 run_inferior_command (packet
);
3781 /* Return the definition of the static tracepoint at a given address.
3782 Result packet is the same as qTsST's. */
3785 cmd_qtstmat (char *packet
)
3787 if (!maybe_write_ipa_ust_not_loaded (packet
))
3788 run_inferior_command (packet
);
3791 /* Return the minimum instruction size needed for fast tracepoints as a
3792 hexadecimal number. */
3795 cmd_qtminftpilen (char *packet
)
3797 if (current_inferior
== NULL
)
3799 /* Indicate that the minimum length is currently unknown. */
3800 strcpy (packet
, "0");
3804 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3807 /* Respond to qTBuffer packet with a block of raw data from the trace
3808 buffer. GDB may ask for a lot, but we are allowed to reply with
3809 only as much as will fit within packet limits or whatever. */
3812 cmd_qtbuffer (char *own_buf
)
3814 ULONGEST offset
, num
, tot
;
3816 char *packet
= own_buf
;
3818 packet
+= strlen ("qTBuffer:");
3820 packet
= unpack_varlen_hex (packet
, &offset
);
3821 ++packet
; /* skip a comma */
3822 unpack_varlen_hex (packet
, &num
);
3824 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3825 (int) num
, pulongest (offset
));
3827 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3829 /* If we're right at the end, reply specially that we're done. */
3832 strcpy (own_buf
, "l");
3836 /* Object to any other out-of-bounds request. */
3839 write_enn (own_buf
);
3843 /* Compute the pointer corresponding to the given offset, accounting
3845 tbp
= trace_buffer_start
+ offset
;
3846 if (tbp
>= trace_buffer_wrap
)
3847 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3849 /* Trim to the remaining bytes if we're close to the end. */
3850 if (num
> tot
- offset
)
3853 /* Trim to available packet size. */
3854 if (num
>= (PBUFSIZ
- 16) / 2 )
3855 num
= (PBUFSIZ
- 16) / 2;
3857 convert_int_to_ascii (tbp
, own_buf
, num
);
3858 own_buf
[num
] = '\0';
3862 cmd_bigqtbuffer_circular (char *own_buf
)
3865 char *packet
= own_buf
;
3867 packet
+= strlen ("QTBuffer:circular:");
3869 unpack_varlen_hex (packet
, &val
);
3870 circular_trace_buffer
= val
;
3871 trace_debug ("Trace buffer is now %s",
3872 circular_trace_buffer
? "circular" : "linear");
3877 cmd_qtnotes (char *own_buf
)
3880 char *saved
, *user
, *notes
, *stopnote
;
3881 char *packet
= own_buf
;
3883 packet
+= strlen ("QTNotes:");
3887 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
3889 packet
+= strlen ("user:");
3891 packet
= strchr (packet
, ';');
3892 nbytes
= (packet
- saved
) / 2;
3893 user
= xmalloc (nbytes
+ 1);
3894 nbytes
= unhexify (user
, saved
, nbytes
);
3895 user
[nbytes
] = '\0';
3896 ++packet
; /* skip the semicolon */
3897 trace_debug ("User is '%s'", user
);
3898 tracing_user_name
= user
;
3900 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
3902 packet
+= strlen ("notes:");
3904 packet
= strchr (packet
, ';');
3905 nbytes
= (packet
- saved
) / 2;
3906 notes
= xmalloc (nbytes
+ 1);
3907 nbytes
= unhexify (notes
, saved
, nbytes
);
3908 notes
[nbytes
] = '\0';
3909 ++packet
; /* skip the semicolon */
3910 trace_debug ("Notes is '%s'", notes
);
3911 tracing_notes
= notes
;
3913 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
3915 packet
+= strlen ("tstop:");
3917 packet
= strchr (packet
, ';');
3918 nbytes
= (packet
- saved
) / 2;
3919 stopnote
= xmalloc (nbytes
+ 1);
3920 nbytes
= unhexify (stopnote
, saved
, nbytes
);
3921 stopnote
[nbytes
] = '\0';
3922 ++packet
; /* skip the semicolon */
3923 trace_debug ("tstop note is '%s'", stopnote
);
3924 tracing_stop_note
= stopnote
;
3934 handle_tracepoint_general_set (char *packet
)
3936 if (strcmp ("QTinit", packet
) == 0)
3938 cmd_qtinit (packet
);
3941 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3946 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3948 cmd_qtdpsrc (packet
);
3951 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
3953 cmd_qtenable_disable (packet
, 1);
3956 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
3958 cmd_qtenable_disable (packet
, 0);
3961 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3966 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3971 else if (strcmp ("QTStart", packet
) == 0)
3973 cmd_qtstart (packet
);
3976 else if (strcmp ("QTStop", packet
) == 0)
3978 cmd_qtstop (packet
);
3981 else if (strncmp ("QTDisconnected:", packet
,
3982 strlen ("QTDisconnected:")) == 0)
3984 cmd_qtdisconnected (packet
);
3987 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3989 cmd_qtframe (packet
);
3992 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
3994 cmd_bigqtbuffer_circular (packet
);
3997 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
3999 cmd_qtnotes (packet
);
4007 handle_tracepoint_query (char *packet
)
4009 if (strcmp ("qTStatus", packet
) == 0)
4011 cmd_qtstatus (packet
);
4014 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4019 else if (strcmp ("qTfP", packet
) == 0)
4024 else if (strcmp ("qTsP", packet
) == 0)
4029 else if (strcmp ("qTfV", packet
) == 0)
4034 else if (strcmp ("qTsV", packet
) == 0)
4039 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4044 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4046 cmd_qtbuffer (packet
);
4049 else if (strcmp ("qTfSTM", packet
) == 0)
4051 cmd_qtfstm (packet
);
4054 else if (strcmp ("qTsSTM", packet
) == 0)
4056 cmd_qtsstm (packet
);
4059 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4061 cmd_qtstmat (packet
);
4064 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4066 cmd_qtminftpilen (packet
);
4074 #ifndef IN_PROCESS_AGENT
4076 /* Call this when thread TINFO has hit the tracepoint defined by
4077 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4078 action. This adds a while-stepping collecting state item to the
4079 threads' collecting state list, so that we can keep track of
4080 multiple simultaneous while-stepping actions being collected by the
4081 same thread. This can happen in cases like:
4083 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4085 ff0003 INSN3 <-- TP2, collect $regs
4086 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4089 Notice that when instruction INSN5 is reached, the while-stepping
4090 actions of both TP1 and TP3 are still being collected, and that TP2
4091 had been collected meanwhile. The whole range of ff0001-ff0005
4092 should be single-stepped, due to at least TP1's while-stepping
4093 action covering the whole range. */
4096 add_while_stepping_state (struct thread_info
*tinfo
,
4097 int tp_number
, CORE_ADDR tp_address
)
4099 struct wstep_state
*wstep
;
4101 wstep
= xmalloc (sizeof (*wstep
));
4102 wstep
->next
= tinfo
->while_stepping
;
4104 wstep
->tp_number
= tp_number
;
4105 wstep
->tp_address
= tp_address
;
4106 wstep
->current_step
= 0;
4108 tinfo
->while_stepping
= wstep
;
4111 /* Release the while-stepping collecting state WSTEP. */
4114 release_while_stepping_state (struct wstep_state
*wstep
)
4119 /* Release all while-stepping collecting states currently associated
4120 with thread TINFO. */
4123 release_while_stepping_state_list (struct thread_info
*tinfo
)
4125 struct wstep_state
*head
;
4127 while (tinfo
->while_stepping
)
4129 head
= tinfo
->while_stepping
;
4130 tinfo
->while_stepping
= head
->next
;
4131 release_while_stepping_state (head
);
4135 /* If TINFO was handling a 'while-stepping' action, the step has
4136 finished, so collect any step data needed, and check if any more
4137 steps are required. Return true if the thread was indeed
4138 collecting tracepoint data, false otherwise. */
4141 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4143 struct tracepoint
*tpoint
;
4144 struct wstep_state
*wstep
;
4145 struct wstep_state
**wstep_link
;
4146 struct trap_tracepoint_ctx ctx
;
4148 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4150 if (agent_loaded_p ())
4151 upload_fast_traceframes ();
4153 /* Check if we were indeed collecting data for one of more
4154 tracepoints with a 'while-stepping' count. */
4155 if (tinfo
->while_stepping
== NULL
)
4160 /* We're not even tracing anymore. Stop this thread from
4162 release_while_stepping_state_list (tinfo
);
4164 /* The thread had stopped due to a single-step request indeed
4165 explained by a tracepoint. */
4169 wstep
= tinfo
->while_stepping
;
4170 wstep_link
= &tinfo
->while_stepping
;
4172 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4173 target_pid_to_str (tinfo
->entry
.id
),
4174 wstep
->tp_number
, paddress (wstep
->tp_address
));
4176 ctx
.base
.type
= trap_tracepoint
;
4177 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4179 while (wstep
!= NULL
)
4181 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4184 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4185 wstep
->tp_number
, paddress (wstep
->tp_address
),
4186 target_pid_to_str (tinfo
->entry
.id
));
4189 *wstep_link
= wstep
->next
;
4190 release_while_stepping_state (wstep
);
4191 wstep
= *wstep_link
;
4195 /* We've just finished one step. */
4196 ++wstep
->current_step
;
4199 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4200 stop_pc
, tpoint
, wstep
->current_step
);
4202 if (wstep
->current_step
>= tpoint
->step_count
)
4204 /* The requested numbers of steps have occurred. */
4205 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4206 target_pid_to_str (tinfo
->entry
.id
),
4207 wstep
->tp_number
, paddress (wstep
->tp_address
));
4209 /* Unlink the wstep. */
4210 *wstep_link
= wstep
->next
;
4211 release_while_stepping_state (wstep
);
4212 wstep
= *wstep_link
;
4214 /* Only check the hit count now, which ensure that we do all
4215 our stepping before stopping the run. */
4216 if (tpoint
->pass_count
> 0
4217 && tpoint
->hit_count
>= tpoint
->pass_count
4218 && stopping_tracepoint
== NULL
)
4219 stopping_tracepoint
= tpoint
;
4223 /* Keep single-stepping until the requested numbers of steps
4225 wstep_link
= &wstep
->next
;
4226 wstep
= *wstep_link
;
4229 if (stopping_tracepoint
4230 || trace_buffer_is_full
4231 || expr_eval_result
!= expr_eval_no_error
)
4241 /* Handle any internal tracing control breakpoint hits. That means,
4242 pull traceframes from the IPA to our buffer, and syncing both
4243 tracing agents when the IPA's tracing stops for some reason. */
4246 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4248 /* Pull in fast tracepoint trace frames from the inferior in-process
4249 agent's buffer into our buffer. */
4251 if (!agent_loaded_p ())
4254 upload_fast_traceframes ();
4256 /* Check if the in-process agent had decided we should stop
4258 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4260 int ipa_trace_buffer_is_full
;
4261 CORE_ADDR ipa_stopping_tracepoint
;
4262 int ipa_expr_eval_result
;
4263 CORE_ADDR ipa_error_tracepoint
;
4265 trace_debug ("lib stopped at stop_tracing");
4267 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4268 &ipa_trace_buffer_is_full
);
4270 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4271 &ipa_stopping_tracepoint
);
4272 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4274 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4275 &ipa_error_tracepoint
);
4276 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4278 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4279 &ipa_expr_eval_result
);
4280 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4282 trace_debug ("lib: trace_buffer_is_full: %d, "
4283 "stopping_tracepoint: %s, "
4284 "ipa_expr_eval_result: %d, "
4285 "error_tracepoint: %s, ",
4286 ipa_trace_buffer_is_full
,
4287 paddress (ipa_stopping_tracepoint
),
4288 ipa_expr_eval_result
,
4289 paddress (ipa_error_tracepoint
));
4293 if (ipa_trace_buffer_is_full
)
4294 trace_debug ("lib stopped due to full buffer.");
4295 if (ipa_stopping_tracepoint
)
4296 trace_debug ("lib stopped due to tpoint");
4297 if (ipa_stopping_tracepoint
)
4298 trace_debug ("lib stopped due to error");
4301 if (ipa_stopping_tracepoint
!= 0)
4304 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4306 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4308 expr_eval_result
= ipa_expr_eval_result
;
4310 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4315 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4317 trace_debug ("lib stopped at flush_trace_buffer");
4324 /* Return true if TINFO just hit a tracepoint. Collect data if
4328 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4330 struct tracepoint
*tpoint
;
4332 struct trap_tracepoint_ctx ctx
;
4334 /* Not tracing, don't handle. */
4338 ctx
.base
.type
= trap_tracepoint
;
4339 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4341 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4343 /* Note that we collect fast tracepoints here as well. We'll
4344 step over the fast tracepoint jump later, which avoids the
4345 double collect. However, we don't collect for static
4346 tracepoints here, because UST markers are compiled in program,
4347 and probes will be executed in program. So static tracepoints
4348 are collected there. */
4349 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4350 && tpoint
->type
!= static_tracepoint
)
4352 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4353 target_pid_to_str (tinfo
->entry
.id
),
4354 tpoint
->number
, paddress (tpoint
->address
));
4356 /* Test the condition if present, and collect if true. */
4358 || (condition_true_at_tracepoint
4359 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4360 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4363 if (stopping_tracepoint
4364 || trace_buffer_is_full
4365 || expr_eval_result
!= expr_eval_no_error
)
4369 /* If the tracepoint had a 'while-stepping' action, then set
4370 the thread to collect this tracepoint on the following
4372 else if (tpoint
->step_count
> 0)
4374 add_while_stepping_state (tinfo
,
4375 tpoint
->number
, tpoint
->address
);
4387 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4388 struct ust_marker_data
;
4389 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4390 struct traceframe
*tframe
);
4393 /* Create a trace frame for the hit of the given tracepoint in the
4397 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4398 struct tracepoint
*tpoint
)
4400 struct traceframe
*tframe
;
4403 /* Only count it as a hit when we actually collect data. */
4404 tpoint
->hit_count
++;
4406 /* If we've exceeded a defined pass count, record the event for
4407 later, and finish the collection for this hit. This test is only
4408 for nonstepping tracepoints, stepping tracepoints test at the end
4409 of their while-stepping loop. */
4410 if (tpoint
->pass_count
> 0
4411 && tpoint
->hit_count
>= tpoint
->pass_count
4412 && tpoint
->step_count
== 0
4413 && stopping_tracepoint
== NULL
)
4414 stopping_tracepoint
= tpoint
;
4416 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
4417 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4419 tframe
= add_traceframe (tpoint
);
4423 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4425 #ifndef IN_PROCESS_AGENT
4426 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4427 tpoint
->number
, paddress (tpoint
->address
),
4428 tpoint
->actions_str
[acti
]);
4431 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4432 tpoint
->actions
[acti
]);
4435 finish_traceframe (tframe
);
4438 if (tframe
== NULL
&& tracing
)
4439 trace_buffer_is_full
= 1;
4442 #ifndef IN_PROCESS_AGENT
4445 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4447 struct tracepoint
*tpoint
, int current_step
)
4449 struct traceframe
*tframe
;
4452 trace_debug ("Making new step traceframe for "
4453 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4454 tpoint
->number
, paddress (tpoint
->address
),
4455 current_step
, tpoint
->step_count
,
4458 tframe
= add_traceframe (tpoint
);
4462 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4464 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4465 tpoint
->number
, paddress (tpoint
->address
),
4466 tpoint
->step_actions_str
[acti
]);
4468 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4469 tpoint
->step_actions
[acti
]);
4472 finish_traceframe (tframe
);
4475 if (tframe
== NULL
&& tracing
)
4476 trace_buffer_is_full
= 1;
4481 static struct regcache
*
4482 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4484 struct regcache
*regcache
= NULL
;
4486 #ifdef IN_PROCESS_AGENT
4487 if (ctx
->type
== fast_tracepoint
)
4489 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4490 if (!fctx
->regcache_initted
)
4492 fctx
->regcache_initted
= 1;
4493 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4494 supply_regblock (&fctx
->regcache
, NULL
);
4495 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4497 regcache
= &fctx
->regcache
;
4500 if (ctx
->type
== static_tracepoint
)
4502 struct static_tracepoint_ctx
*sctx
4503 = (struct static_tracepoint_ctx
*) ctx
;
4505 if (!sctx
->regcache_initted
)
4507 sctx
->regcache_initted
= 1;
4508 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4509 supply_regblock (&sctx
->regcache
, NULL
);
4510 /* Pass down the tracepoint address, because REGS doesn't
4511 include the PC, but we know what it must have been. */
4512 supply_static_tracepoint_registers (&sctx
->regcache
,
4513 (const unsigned char *)
4515 sctx
->tpoint
->address
);
4517 regcache
= &sctx
->regcache
;
4521 if (ctx
->type
== trap_tracepoint
)
4523 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4524 regcache
= tctx
->regcache
;
4528 gdb_assert (regcache
!= NULL
);
4534 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4536 struct tracepoint
*tpoint
,
4537 struct traceframe
*tframe
,
4538 struct tracepoint_action
*taction
)
4540 enum eval_result_type err
;
4542 switch (taction
->type
)
4546 struct collect_memory_action
*maction
;
4548 maction
= (struct collect_memory_action
*) taction
;
4550 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4551 pulongest (maction
->len
),
4552 paddress (maction
->addr
), maction
->basereg
);
4553 /* (should use basereg) */
4554 agent_mem_read (tframe
, NULL
,
4555 (CORE_ADDR
) maction
->addr
, maction
->len
);
4560 unsigned char *regspace
;
4561 struct regcache tregcache
;
4562 struct regcache
*context_regcache
;
4565 trace_debug ("Want to collect registers");
4567 /* Collect all registers for now. */
4568 regspace
= add_traceframe_block (tframe
,
4569 1 + register_cache_size ());
4570 if (regspace
== NULL
)
4572 trace_debug ("Trace buffer block allocation failed, skipping");
4575 /* Identify a register block. */
4578 context_regcache
= get_context_regcache (ctx
);
4580 /* Wrap the regblock in a register cache (in the stack, we
4581 don't want to malloc here). */
4582 init_register_cache (&tregcache
, regspace
+ 1);
4584 /* Copy the register data to the regblock. */
4585 regcache_cpy (&tregcache
, context_regcache
);
4587 #ifndef IN_PROCESS_AGENT
4588 /* On some platforms, trap-based tracepoints will have the PC
4589 pointing to the next instruction after the trap, but we
4590 don't want the user or GDB trying to guess whether the
4591 saved PC needs adjusting; so always record the adjusted
4592 stop_pc. Note that we can't use tpoint->address instead,
4593 since it will be wrong for while-stepping actions. This
4594 adjustment is a nop for fast tracepoints collected from the
4595 in-process lib (but not if GDBserver is collecting one
4596 preemptively), since the PC had already been adjusted to
4597 contain the tracepoint's address by the jump pad. */
4598 trace_debug ("Storing stop pc (0x%s) in regblock",
4599 paddress (stop_pc
));
4601 /* This changes the regblock, not the thread's
4603 regcache_write_pc (&tregcache
, stop_pc
);
4609 struct eval_expr_action
*eaction
;
4611 eaction
= (struct eval_expr_action
*) taction
;
4613 trace_debug ("Want to evaluate expression");
4615 err
= eval_tracepoint_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4617 if (err
!= expr_eval_no_error
)
4619 record_tracepoint_error (tpoint
, "action expression", err
);
4626 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4627 trace_debug ("Want to collect static trace data");
4628 collect_ust_data_at_tracepoint (ctx
, tframe
);
4630 trace_debug ("warning: collecting static trace data, "
4631 "but static tracepoints are not supported");
4636 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4642 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4643 struct tracepoint
*tpoint
)
4646 enum eval_result_type err
;
4648 /* Presently, gdbserver doesn't run compiled conditions, only the
4649 IPA does. If the program stops at a fast tracepoint's address
4650 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4651 gdbserver preemptively collect the fast tracepoint. Later, on
4652 resume, gdbserver steps over the fast tracepoint like it steps
4653 over breakpoints, so that the IPA doesn't see that fast
4654 tracepoint. This avoids double collects of fast tracepoints in
4655 that stopping scenario. Having gdbserver itself handle the fast
4656 tracepoint gives the user a consistent view of when fast or trap
4657 tracepoints are collected, compared to an alternative where only
4658 trap tracepoints are collected on stop, and fast tracepoints on
4659 resume. When a fast tracepoint is being processed by gdbserver,
4660 it is always the non-compiled condition expression that is
4662 #ifdef IN_PROCESS_AGENT
4663 if (tpoint
->compiled_cond
)
4664 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4667 err
= eval_tracepoint_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4669 if (err
!= expr_eval_no_error
)
4671 record_tracepoint_error (tpoint
, "condition", err
);
4672 /* The error case must return false. */
4676 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4677 tpoint
->number
, paddress (tpoint
->address
),
4679 return (value
? 1 : 0);
4682 /* Evaluates a tracepoint agent expression with context CTX,
4683 traceframe TFRAME, agent expression AEXPR and store the
4686 static enum eval_result_type
4687 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4688 struct traceframe
*tframe
,
4689 struct agent_expr
*aexpr
,
4692 struct regcache
*regcache
;
4693 regcache
= get_context_regcache (ctx
);
4695 return gdb_eval_agent_expr (regcache
, tframe
, aexpr
, rslt
);
4698 /* Do memory copies for bytecodes. */
4699 /* Do the recording of memory blocks for actions and bytecodes. */
4702 agent_mem_read (struct traceframe
*tframe
,
4703 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4705 unsigned char *mspace
;
4706 ULONGEST remaining
= len
;
4707 unsigned short blocklen
;
4709 /* If a 'to' buffer is specified, use it. */
4712 read_inferior_memory (from
, to
, len
);
4716 /* Otherwise, create a new memory block in the trace buffer. */
4717 while (remaining
> 0)
4721 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4722 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4723 mspace
= add_traceframe_block (tframe
, sp
);
4726 /* Identify block as a memory block. */
4729 /* Record address and size. */
4730 memcpy (mspace
, &from
, sizeof (from
));
4731 mspace
+= sizeof (from
);
4732 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4733 mspace
+= sizeof (blocklen
);
4734 /* Record the memory block proper. */
4735 read_inferior_memory (from
, mspace
, blocklen
);
4736 trace_debug ("%d bytes recorded", blocklen
);
4737 remaining
-= blocklen
;
4744 agent_mem_read_string (struct traceframe
*tframe
,
4745 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4747 unsigned char *buf
, *mspace
;
4748 ULONGEST remaining
= len
;
4749 unsigned short blocklen
, i
;
4751 /* To save a bit of space, block lengths are 16-bit, so break large
4752 requests into multiple blocks. Bordering on overkill for strings,
4753 but it could happen that someone specifies a large max length. */
4754 while (remaining
> 0)
4758 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4759 /* We want working space to accumulate nonzero bytes, since
4760 traceframes must have a predecided size (otherwise it gets
4761 harder to wrap correctly for the circular case, etc). */
4762 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4763 for (i
= 0; i
< blocklen
; ++i
)
4765 /* Read the string one byte at a time, in case the string is
4766 at the end of a valid memory area - we don't want a
4767 correctly-terminated string to engender segvio
4769 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4774 /* Make sure outer loop stops now too. */
4775 remaining
= blocklen
;
4779 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4780 mspace
= add_traceframe_block (tframe
, sp
);
4786 /* Identify block as a memory block. */
4789 /* Record address and size. */
4790 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4791 mspace
+= sizeof (from
);
4792 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4793 mspace
+= sizeof (blocklen
);
4794 /* Copy the string contents. */
4795 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
4796 remaining
-= blocklen
;
4803 /* Record the value of a trace state variable. */
4806 agent_tsv_read (struct traceframe
*tframe
, int n
)
4808 unsigned char *vspace
;
4811 vspace
= add_traceframe_block (tframe
,
4812 1 + sizeof (n
) + sizeof (LONGEST
));
4815 /* Identify block as a variable. */
4817 /* Record variable's number and value. */
4818 memcpy (vspace
+ 1, &n
, sizeof (n
));
4819 val
= get_trace_state_variable_value (n
);
4820 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4821 trace_debug ("Variable %d recorded", n
);
4825 #ifndef IN_PROCESS_AGENT
4827 /* Callback for traceframe_walk_blocks, used to find a given block
4828 type in a traceframe. */
4831 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
4833 char *wantedp
= data
;
4835 if (*wantedp
== blocktype
)
4841 /* Walk over all traceframe blocks of the traceframe buffer starting
4842 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4843 block found, passing in DATA unmodified. If CALLBACK returns true,
4844 this returns a pointer to where the block is found. Returns NULL
4845 if no callback call returned true, indicating that all blocks have
4848 static unsigned char *
4849 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
4851 int (*callback
) (char blocktype
,
4852 unsigned char *dataptr
,
4856 unsigned char *dataptr
;
4860 trace_debug ("traceframe %d has no data", tfnum
);
4864 /* Iterate through a traceframe's blocks, looking for a block of the
4866 for (dataptr
= database
;
4867 dataptr
< database
+ datasize
;
4871 unsigned short mlen
;
4873 if (dataptr
== trace_buffer_wrap
)
4875 /* Adjust to reflect wrapping part of the frame around to
4877 datasize
= dataptr
- database
;
4878 dataptr
= database
= trace_buffer_lo
;
4881 blocktype
= *dataptr
++;
4883 if ((*callback
) (blocktype
, dataptr
, data
))
4889 /* Skip over the registers block. */
4890 dataptr
+= register_cache_size ();
4893 /* Skip over the memory block. */
4894 dataptr
+= sizeof (CORE_ADDR
);
4895 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4896 dataptr
+= (sizeof (mlen
) + mlen
);
4899 /* Skip over the TSV block. */
4900 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4903 /* Skip over the static trace data block. */
4904 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4905 dataptr
+= (sizeof (mlen
) + mlen
);
4908 trace_debug ("traceframe %d has unknown block type 0x%x",
4917 /* Look for the block of type TYPE_WANTED in the trameframe starting
4918 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4921 static unsigned char *
4922 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4923 int tfnum
, char type_wanted
)
4925 return traceframe_walk_blocks (database
, datasize
, tfnum
,
4926 match_blocktype
, &type_wanted
);
4929 static unsigned char *
4930 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4932 unsigned char *regblock
;
4934 regblock
= traceframe_find_block_type (tframe
->data
,
4938 if (regblock
== NULL
)
4939 trace_debug ("traceframe %d has no register data", tfnum
);
4944 /* Get registers from a traceframe. */
4947 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4949 unsigned char *dataptr
;
4950 struct tracepoint
*tpoint
;
4951 struct traceframe
*tframe
;
4953 tframe
= find_traceframe (tfnum
);
4957 trace_debug ("traceframe %d not found", tfnum
);
4961 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4962 if (dataptr
== NULL
)
4964 /* Mark registers unavailable. */
4965 supply_regblock (regcache
, NULL
);
4967 /* We can generally guess at a PC, although this will be
4968 misleading for while-stepping frames and multi-location
4970 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4972 regcache_write_pc (regcache
, tpoint
->address
);
4975 supply_regblock (regcache
, dataptr
);
4981 traceframe_get_pc (struct traceframe
*tframe
)
4983 struct regcache regcache
;
4984 unsigned char *dataptr
;
4986 dataptr
= traceframe_find_regblock (tframe
, -1);
4987 if (dataptr
== NULL
)
4990 init_register_cache (®cache
, dataptr
);
4991 return regcache_read_pc (®cache
);
4994 /* Read a requested block of memory from a trace frame. */
4997 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
4998 unsigned char *buf
, ULONGEST length
,
5001 struct traceframe
*tframe
;
5002 unsigned char *database
, *dataptr
;
5003 unsigned int datasize
;
5005 unsigned short mlen
;
5007 trace_debug ("traceframe_read_mem");
5009 tframe
= find_traceframe (tfnum
);
5013 trace_debug ("traceframe %d not found", tfnum
);
5017 datasize
= tframe
->data_size
;
5018 database
= dataptr
= &tframe
->data
[0];
5020 /* Iterate through a traceframe's blocks, looking for memory. */
5021 while ((dataptr
= traceframe_find_block_type (dataptr
,
5023 - (dataptr
- database
),
5024 tfnum
, 'M')) != NULL
)
5026 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5027 dataptr
+= sizeof (maddr
);
5028 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5029 dataptr
+= sizeof (mlen
);
5030 trace_debug ("traceframe %d has %d bytes at %s",
5031 tfnum
, mlen
, paddress (maddr
));
5033 /* If the block includes the first part of the desired range,
5034 return as much it has; GDB will re-request the remainder,
5035 which might be in a different block of this trace frame. */
5036 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5038 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5042 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5047 /* Skip over this block. */
5051 trace_debug ("traceframe %d has no memory data for the desired region",
5059 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5062 struct traceframe
*tframe
;
5063 unsigned char *database
, *dataptr
;
5064 unsigned int datasize
;
5067 trace_debug ("traceframe_read_tsv");
5069 tfnum
= current_traceframe
;
5073 trace_debug ("no current traceframe");
5077 tframe
= find_traceframe (tfnum
);
5081 trace_debug ("traceframe %d not found", tfnum
);
5085 datasize
= tframe
->data_size
;
5086 database
= dataptr
= &tframe
->data
[0];
5088 /* Iterate through a traceframe's blocks, looking for the tsv. */
5089 while ((dataptr
= traceframe_find_block_type (dataptr
,
5091 - (dataptr
- database
),
5092 tfnum
, 'V')) != NULL
)
5094 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5095 dataptr
+= sizeof (vnum
);
5097 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5099 /* Check that this is the variable we want. */
5102 memcpy (val
, dataptr
, sizeof (*val
));
5106 /* Skip over this block. */
5107 dataptr
+= sizeof (LONGEST
);
5110 trace_debug ("traceframe %d has no data for variable %d",
5115 /* Read a requested block of static tracepoint data from a trace
5119 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5120 unsigned char *buf
, ULONGEST length
,
5123 struct traceframe
*tframe
;
5124 unsigned char *database
, *dataptr
;
5125 unsigned int datasize
;
5126 unsigned short mlen
;
5128 trace_debug ("traceframe_read_sdata");
5130 tframe
= find_traceframe (tfnum
);
5134 trace_debug ("traceframe %d not found", tfnum
);
5138 datasize
= tframe
->data_size
;
5139 database
= &tframe
->data
[0];
5141 /* Iterate through a traceframe's blocks, looking for static
5143 dataptr
= traceframe_find_block_type (database
, datasize
,
5145 if (dataptr
!= NULL
)
5147 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5148 dataptr
+= sizeof (mlen
);
5151 if (offset
+ length
> mlen
)
5152 length
= mlen
- offset
;
5154 memcpy (buf
, dataptr
, length
);
5162 trace_debug ("traceframe %d has no static trace data", tfnum
);
5168 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5169 object. DATA is pointer to a struct buffer holding the
5170 traceframe-info object being built. */
5173 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5175 struct buffer
*buffer
= data
;
5181 unsigned short mlen
;
5184 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5185 dataptr
+= sizeof (maddr
);
5186 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5187 dataptr
+= sizeof (mlen
);
5188 buffer_xml_printf (buffer
,
5189 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5190 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5200 warning ("Unhandled trace block type (%d) '%c ' "
5201 "while building trace frame info.",
5202 blocktype
, blocktype
);
5209 /* Build a traceframe-info object for traceframe number TFNUM into
5213 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5215 struct traceframe
*tframe
;
5217 trace_debug ("traceframe_read_info");
5219 tframe
= find_traceframe (tfnum
);
5223 trace_debug ("traceframe %d not found", tfnum
);
5227 buffer_grow_str (buffer
, "<traceframe-info>\n");
5228 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5229 tfnum
, build_traceframe_info_xml
, buffer
);
5230 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5234 /* Return the first fast tracepoint whose jump pad contains PC. */
5236 static struct tracepoint
*
5237 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5239 struct tracepoint
*tpoint
;
5241 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5242 if (tpoint
->type
== fast_tracepoint
)
5243 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5249 /* Return the first fast tracepoint whose trampoline contains PC. */
5251 static struct tracepoint
*
5252 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5254 struct tracepoint
*tpoint
;
5256 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5258 if (tpoint
->type
== fast_tracepoint
5259 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5266 /* Return GDBserver's tracepoint that matches the IP Agent's
5267 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5270 static struct tracepoint
*
5271 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5273 struct tracepoint
*tpoint
;
5275 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5276 if (tpoint
->type
== fast_tracepoint
)
5277 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5285 /* The type of the object that is used to synchronize fast tracepoint
5288 typedef struct collecting_t
5290 /* The fast tracepoint number currently collecting. */
5293 /* A number that GDBserver can use to identify the thread that is
5294 presently holding the collect lock. This need not (and usually
5295 is not) the thread id, as getting the current thread ID usually
5296 requires a system call, which we want to avoid like the plague.
5297 Usually this is thread's TCB, found in the TLS (pseudo-)
5298 register, which is readable with a single insn on several
5300 uintptr_t thread_area
;
5303 #ifndef IN_PROCESS_AGENT
5306 force_unlock_trace_buffer (void)
5308 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5311 /* Check if the thread identified by THREAD_AREA which is stopped at
5312 STOP_PC, is presently locking the fast tracepoint collection, and
5313 if so, gather some status of said collection. Returns 0 if the
5314 thread isn't collecting or in the jump pad at all. 1, if in the
5315 jump pad (or within gdb_collect) and hasn't executed the adjusted
5316 original insn yet (can set a breakpoint there and run to it). 2,
5317 if presently executing the adjusted original insn --- in which
5318 case, if we want to move the thread out of the jump pad, we need to
5319 single-step it until this function returns 0. */
5322 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5324 struct fast_tpoint_collect_status
*status
)
5326 CORE_ADDR ipa_collecting
;
5327 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5328 CORE_ADDR ipa_gdb_trampoline_buffer
;
5329 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5330 struct tracepoint
*tpoint
;
5331 int needs_breakpoint
;
5333 /* The thread THREAD_AREA is either:
5335 0. not collecting at all, not within the jump pad, or within
5336 gdb_collect or one of its callees.
5338 1. in the jump pad and haven't reached gdb_collect
5340 2. within gdb_collect (out of the jump pad) (collect is set)
5342 3. we're in the jump pad, after gdb_collect having returned,
5343 possibly executing the adjusted insns.
5345 For cases 1 and 3, `collecting' may or not be set. The jump pad
5346 doesn't have any complicated jump logic, so we can tell if the
5347 thread is executing the adjust original insn or not by just
5348 matching STOP_PC with known jump pad addresses. If we it isn't
5349 yet executing the original insn, set a breakpoint there, and let
5350 the thread run to it, so to quickly step over a possible (many
5351 insns) gdb_collect call. Otherwise, or when the breakpoint is
5352 hit, only a few (small number of) insns are left to be executed
5353 in the jump pad. Single-step the thread until it leaves the
5358 needs_breakpoint
= 0;
5359 trace_debug ("fast_tracepoint_collecting");
5361 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5362 &ipa_gdb_jump_pad_buffer
))
5363 fatal ("error extracting `gdb_jump_pad_buffer'");
5364 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5365 &ipa_gdb_jump_pad_buffer_end
))
5366 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5368 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5369 &ipa_gdb_trampoline_buffer
))
5370 fatal ("error extracting `gdb_trampoline_buffer'");
5371 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5372 &ipa_gdb_trampoline_buffer_end
))
5373 fatal ("error extracting `gdb_trampoline_buffer_end'");
5375 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5376 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5378 /* We can tell which tracepoint(s) the thread is collecting by
5379 matching the jump pad address back to the tracepoint. */
5380 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5383 warning ("in jump pad, but no matching tpoint?");
5388 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5390 tpoint
->number
, paddress (tpoint
->address
),
5391 paddress (tpoint
->jump_pad
),
5392 paddress (tpoint
->jump_pad_end
),
5393 paddress (tpoint
->adjusted_insn_addr
),
5394 paddress (tpoint
->adjusted_insn_addr_end
));
5397 /* Definitely in the jump pad. May or may not need
5398 fast-exit-jump-pad breakpoint. */
5399 if (tpoint
->jump_pad
<= stop_pc
5400 && stop_pc
< tpoint
->adjusted_insn_addr
)
5401 needs_breakpoint
= 1;
5403 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5404 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5406 /* We can tell which tracepoint(s) the thread is collecting by
5407 matching the trampoline address back to the tracepoint. */
5408 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5411 warning ("in trampoline, but no matching tpoint?");
5416 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5417 tpoint
->number
, paddress (tpoint
->address
),
5418 paddress (tpoint
->trampoline
),
5419 paddress (tpoint
->trampoline_end
));
5422 /* Have not reached jump pad yet, but treat the trampoline as a
5423 part of the jump pad that is before the adjusted original
5425 needs_breakpoint
= 1;
5429 collecting_t ipa_collecting_obj
;
5431 /* If `collecting' is set/locked, then the THREAD_AREA thread
5432 may or not be the one holding the lock. We have to read the
5433 lock to find out. */
5435 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5438 trace_debug ("fast_tracepoint_collecting:"
5439 " failed reading 'collecting' in the inferior");
5443 if (!ipa_collecting
)
5445 trace_debug ("fast_tracepoint_collecting: not collecting"
5446 " (and nobody is).");
5450 /* Some thread is collecting. Check which. */
5451 if (read_inferior_memory (ipa_collecting
,
5452 (unsigned char *) &ipa_collecting_obj
,
5453 sizeof (ipa_collecting_obj
)) != 0)
5456 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5458 trace_debug ("fast_tracepoint_collecting: not collecting "
5459 "(another thread is)");
5464 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5467 warning ("fast_tracepoint_collecting: collecting, "
5468 "but tpoint %s not found?",
5469 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5473 /* The thread is within `gdb_collect', skip over the rest of
5474 fast tracepoint collection quickly using a breakpoint. */
5475 needs_breakpoint
= 1;
5478 /* The caller wants a bit of status detail. */
5481 status
->tpoint_num
= tpoint
->number
;
5482 status
->tpoint_addr
= tpoint
->address
;
5483 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5484 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5487 if (needs_breakpoint
)
5489 /* Hasn't executed the original instruction yet. Set breakpoint
5490 there, and wait till it's hit, then single-step until exiting
5494 fast_tracepoint_collecting, returning continue-until-break at %s",
5495 paddress (tpoint
->adjusted_insn_addr
));
5497 return 1; /* continue */
5501 /* Just single-step until exiting the jump pad. */
5503 trace_debug ("fast_tracepoint_collecting, returning "
5504 "need-single-step (%s-%s)",
5505 paddress (tpoint
->adjusted_insn_addr
),
5506 paddress (tpoint
->adjusted_insn_addr_end
));
5508 return 2; /* single-step */
5514 #ifdef IN_PROCESS_AGENT
5516 /* The global fast tracepoint collect lock. Points to a collecting_t
5517 object built on the stack by the jump pad, if presently locked;
5518 NULL if it isn't locked. Note that this lock *must* be set while
5519 executing any *function other than the jump pad. See
5520 fast_tracepoint_collecting. */
5521 static collecting_t
* ATTR_USED collecting
;
5523 /* This routine, called from the jump pad (in asm) is designed to be
5524 called from the jump pads of fast tracepoints, thus it is on the
5527 IP_AGENT_EXPORT
void ATTR_USED
5528 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5530 struct fast_tracepoint_ctx ctx
;
5532 /* Don't do anything until the trace run is completely set up. */
5536 ctx
.base
.type
= fast_tracepoint
;
5538 ctx
.regcache_initted
= 0;
5539 /* Wrap the regblock in a register cache (in the stack, we don't
5540 want to malloc here). */
5541 ctx
.regspace
= alloca (register_cache_size ());
5542 if (ctx
.regspace
== NULL
)
5544 trace_debug ("Trace buffer block allocation failed, skipping");
5548 for (ctx
.tpoint
= tpoint
;
5549 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5550 ctx
.tpoint
= ctx
.tpoint
->next
)
5552 if (!ctx
.tpoint
->enabled
)
5555 /* Multiple tracepoints of different types, such as fast tracepoint and
5556 static tracepoint, can be set at the same address. */
5557 if (ctx
.tpoint
->type
!= tpoint
->type
)
5560 /* Test the condition if present, and collect if true. */
5561 if (ctx
.tpoint
->cond
== NULL
5562 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5565 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5566 ctx
.tpoint
->address
, ctx
.tpoint
);
5568 /* Note that this will cause original insns to be written back
5569 to where we jumped from, but that's OK because we're jumping
5570 back to the next whole instruction. This will go badly if
5571 instruction restoration is not atomic though. */
5572 if (stopping_tracepoint
5573 || trace_buffer_is_full
5574 || expr_eval_result
!= expr_eval_no_error
)
5582 /* If there was a condition and it evaluated to false, the only
5583 way we would stop tracing is if there was an error during
5584 condition expression evaluation. */
5585 if (expr_eval_result
!= expr_eval_no_error
)
5596 #ifndef IN_PROCESS_AGENT
5599 get_raw_reg_func_addr (void)
5601 return ipa_sym_addrs
.addr_get_raw_reg
;
5605 get_get_tsv_func_addr (void)
5607 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5611 get_set_tsv_func_addr (void)
5613 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5617 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5618 CORE_ADDR
*jump_entry
)
5620 CORE_ADDR entry_point
= *jump_entry
;
5621 enum eval_result_type err
;
5623 trace_debug ("Starting condition compilation for tracepoint %d\n",
5626 /* Initialize the global pointer to the code being built. */
5627 current_insn_ptr
= *jump_entry
;
5631 err
= compile_bytecodes (tpoint
->cond
);
5633 if (err
== expr_eval_no_error
)
5637 /* Record the beginning of the compiled code. */
5638 tpoint
->compiled_cond
= entry_point
;
5640 trace_debug ("Condition compilation for tracepoint %d complete\n",
5645 /* Leave the unfinished code in situ, but don't point to it. */
5647 tpoint
->compiled_cond
= 0;
5649 trace_debug ("Condition compilation for tracepoint %d failed, "
5651 tpoint
->number
, err
);
5654 /* Update the code pointer passed in. Note that we do this even if
5655 the compile fails, so that we can look at the partial results
5656 instead of letting them be overwritten. */
5657 *jump_entry
= current_insn_ptr
;
5659 /* Leave a gap, to aid dump decipherment. */
5663 /* We'll need to adjust these when we consider bi-arch setups, and big
5667 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5669 return write_inferior_memory (where
,
5670 (unsigned char *) &ptr
, sizeof (void *));
5673 /* The base pointer of the IPA's heap. This is the only memory the
5674 IPA is allowed to use. The IPA should _not_ call the inferior's
5675 `malloc' during operation. That'd be slow, and, most importantly,
5676 it may not be safe. We may be collecting a tracepoint in a signal
5677 handler, for example. */
5678 static CORE_ADDR target_tp_heap
;
5680 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5684 target_malloc (ULONGEST size
)
5688 if (target_tp_heap
== 0)
5690 /* We have the pointer *address*, need what it points to. */
5691 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5693 fatal ("could get target heap head pointer");
5696 ptr
= target_tp_heap
;
5697 target_tp_heap
+= size
;
5699 /* Pad to 8-byte alignment. */
5700 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5706 download_agent_expr (struct agent_expr
*expr
)
5708 CORE_ADDR expr_addr
;
5709 CORE_ADDR expr_bytes
;
5711 expr_addr
= target_malloc (sizeof (*expr
));
5712 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5714 expr_bytes
= target_malloc (expr
->length
);
5715 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5717 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5722 /* Align V up to N bits. */
5723 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5725 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5728 download_tracepoint_1 (struct tracepoint
*tpoint
)
5730 struct tracepoint target_tracepoint
;
5731 CORE_ADDR tpptr
= 0;
5733 gdb_assert (tpoint
->type
== fast_tracepoint
5734 || tpoint
->type
== static_tracepoint
);
5736 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5738 CORE_ADDR jentry
, jump_entry
;
5740 jentry
= jump_entry
= get_jump_space_head ();
5742 if (tpoint
->cond
!= NULL
)
5744 /* Pad to 8-byte alignment. (needed?) */
5745 /* Actually this should be left for the target to
5747 jentry
= UALIGN (jentry
, 8);
5749 compile_tracepoint_condition (tpoint
, &jentry
);
5752 /* Pad to 8-byte alignment. */
5753 jentry
= UALIGN (jentry
, 8);
5754 claim_jump_space (jentry
- jump_entry
);
5757 target_tracepoint
= *tpoint
;
5759 tpptr
= target_malloc (sizeof (*tpoint
));
5760 tpoint
->obj_addr_on_target
= tpptr
;
5762 /* Write the whole object. We'll fix up its pointers in a bit.
5763 Assume no next for now. This is fixed up above on the next
5764 iteration, if there's any. */
5765 target_tracepoint
.next
= NULL
;
5766 /* Need to clear this here too, since we're downloading the
5767 tracepoints before clearing our own copy. */
5768 target_tracepoint
.hit_count
= 0;
5770 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5771 sizeof (target_tracepoint
));
5774 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5776 download_agent_expr (tpoint
->cond
));
5778 if (tpoint
->numactions
)
5781 CORE_ADDR actions_array
;
5783 /* The pointers array. */
5785 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
5786 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5790 /* Now for each pointer, download the action. */
5791 for (i
= 0; i
< tpoint
->numactions
; i
++)
5793 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5794 CORE_ADDR ipa_action
= action
->ops
->download (action
);
5796 if (ipa_action
!= 0)
5797 write_inferior_data_ptr
5798 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
5805 download_tracepoint (struct tracepoint
*tpoint
)
5807 struct tracepoint
*tp
, *tp_prev
;
5809 if (tpoint
->type
!= fast_tracepoint
5810 && tpoint
->type
!= static_tracepoint
)
5813 download_tracepoint_1 (tpoint
);
5815 /* Find the previous entry of TPOINT, which is fast tracepoint or
5816 static tracepoint. */
5818 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
5820 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
5826 CORE_ADDR tp_prev_target_next_addr
;
5828 /* Insert TPOINT after TP_PREV in IPA. */
5829 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
5830 + offsetof (struct tracepoint
, next
),
5831 &tp_prev_target_next_addr
))
5832 fatal ("error reading `tp_prev->next'");
5834 /* tpoint->next = tp_prev->next */
5835 write_inferior_data_ptr (tpoint
->obj_addr_on_target
5836 + offsetof (struct tracepoint
, next
),
5837 tp_prev_target_next_addr
);
5838 /* tp_prev->next = tpoint */
5839 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
5840 + offsetof (struct tracepoint
, next
),
5841 tpoint
->obj_addr_on_target
);
5844 /* First object in list, set the head pointer in the
5846 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
5847 tpoint
->obj_addr_on_target
);
5852 download_tracepoints (void)
5854 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
5855 struct tracepoint
*tpoint
;
5857 /* Start out empty. */
5858 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
5860 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5862 if (tpoint
->type
!= fast_tracepoint
5863 && tpoint
->type
!= static_tracepoint
)
5868 download_tracepoint_1 (tpoint
);
5870 tpptr
= tpoint
->obj_addr_on_target
;
5872 if (tpoint
== tracepoints
)
5874 /* First object in list, set the head pointer in the
5876 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
5880 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
5888 download_trace_state_variables (void)
5890 CORE_ADDR ptr
= 0, prev_ptr
= 0;
5891 struct trace_state_variable
*tsv
;
5893 /* Start out empty. */
5894 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
5896 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
5898 struct trace_state_variable target_tsv
;
5900 /* TSV's with a getter have been initialized equally in both the
5901 inferior and GDBserver. Skip them. */
5902 if (tsv
->getter
!= NULL
)
5908 ptr
= target_malloc (sizeof (*tsv
));
5910 if (tsv
== trace_state_variables
)
5912 /* First object in list, set the head pointer in the
5915 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
5920 write_inferior_data_ptr (prev_ptr
5921 + offsetof (struct trace_state_variable
,
5926 /* Write the whole object. We'll fix up its pointers in a bit.
5927 Assume no next, fixup when needed. */
5928 target_tsv
.next
= NULL
;
5930 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
5931 sizeof (target_tsv
));
5933 if (tsv
->name
!= NULL
)
5935 size_t size
= strlen (tsv
->name
) + 1;
5936 CORE_ADDR name_addr
= target_malloc (size
);
5937 write_inferior_memory (name_addr
,
5938 (unsigned char *) tsv
->name
, size
);
5939 write_inferior_data_ptr (ptr
5940 + offsetof (struct trace_state_variable
,
5945 if (tsv
->getter
!= NULL
)
5947 fatal ("what to do with these?");
5953 /* Fixup the next pointer in the last item in the list. */
5954 write_inferior_data_ptr (prev_ptr
5955 + offsetof (struct trace_state_variable
,
5960 /* Upload complete trace frames out of the IP Agent's trace buffer
5961 into GDBserver's trace buffer. This always uploads either all or
5962 no trace frames. This is the counter part of
5963 `trace_alloc_trace_buffer'. See its description of the atomic
5964 synching mechanism. */
5967 upload_fast_traceframes (void)
5969 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
5970 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
5972 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
5973 unsigned int curr_tbctrl_idx
;
5974 unsigned int ipa_trace_buffer_ctrl_curr
;
5975 unsigned int ipa_trace_buffer_ctrl_curr_old
;
5976 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
5977 struct breakpoint
*about_to_request_buffer_space_bkpt
;
5978 CORE_ADDR ipa_trace_buffer_lo
;
5979 CORE_ADDR ipa_trace_buffer_hi
;
5981 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
5982 &ipa_traceframe_read_count_racy
))
5984 /* This will happen in most targets if the current thread is
5989 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
5990 &ipa_traceframe_write_count_racy
))
5993 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
5994 ipa_traceframe_write_count_racy
5995 - ipa_traceframe_read_count_racy
,
5996 ipa_traceframe_write_count_racy
,
5997 ipa_traceframe_read_count_racy
);
5999 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6002 about_to_request_buffer_space_bkpt
6003 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6006 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6007 &ipa_trace_buffer_ctrl_curr
))
6010 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6012 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6015 unsigned int prev
, counter
;
6017 /* Update the token, with new counters, and the GDBserver stamp
6018 bit. Alway reuse the current TBC index. */
6019 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6020 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6022 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6028 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6029 ipa_trace_buffer_ctrl_curr
))
6032 trace_debug ("Lib: Committed %08x -> %08x",
6033 ipa_trace_buffer_ctrl_curr_old
,
6034 ipa_trace_buffer_ctrl_curr
);
6036 /* Re-read these, now that we've installed the
6037 `about_to_request_buffer_space' breakpoint/lock. A thread could
6038 have finished a traceframe between the last read of these
6039 counters and setting the breakpoint above. If we start
6040 uploading, we never want to leave this function with
6041 traceframe_read_count != 0, otherwise, GDBserver could end up
6042 incrementing the counter tokens more than once (due to event loop
6043 nesting), which would break the IP agent's "effective" detection
6044 (see trace_alloc_trace_buffer). */
6045 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6046 &ipa_traceframe_read_count
))
6048 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6049 &ipa_traceframe_write_count
))
6054 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6055 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6056 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6058 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6059 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6060 trace_debug ("note that ipa_traceframe_count's parts changed");
6063 /* Get the address of the current TBC object (the IP agent has an
6064 array of 3 such objects). The index is stored in the TBC
6066 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6067 ipa_trace_buffer_ctrl_addr
6068 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6070 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6071 (unsigned char *) &ipa_trace_buffer_ctrl
,
6072 sizeof (struct ipa_trace_buffer_control
)))
6075 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6076 &ipa_trace_buffer_lo
))
6078 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6079 &ipa_trace_buffer_hi
))
6082 /* Offsets are easier to grok for debugging than raw addresses,
6083 especially for the small trace buffer sizes that are useful for
6085 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6086 "endfree=%d wrap=%d hi=%d",
6088 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6089 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6090 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6091 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6092 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6094 /* Note that the IPA's buffer is always circular. */
6096 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6098 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6099 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6101 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6102 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6103 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6104 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6107 tf
= IPA_FIRST_TRACEFRAME ();
6109 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6111 struct tracepoint
*tpoint
;
6112 struct traceframe
*tframe
;
6113 unsigned char *block
;
6114 struct traceframe ipa_tframe
;
6116 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6117 offsetof (struct traceframe
, data
)))
6118 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6120 if (ipa_tframe
.tpnum
== 0)
6121 fatal ("Uploading: No (more) fast traceframes, but "
6122 "ipa_traceframe_count == %u??\n",
6123 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6125 /* Note that this will be incorrect for multi-location
6127 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6129 tframe
= add_traceframe (tpoint
);
6132 trace_buffer_is_full
= 1;
6133 trace_debug ("Uploading: trace buffer is full");
6137 /* Copy the whole set of blocks in one go for now. FIXME:
6138 split this in smaller blocks. */
6139 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6142 if (read_inferior_memory (tf
6143 + offsetof (struct traceframe
, data
),
6144 block
, ipa_tframe
.data_size
))
6145 error ("Uploading: Couldn't read traceframe data at %s\n",
6146 paddress (tf
+ offsetof (struct traceframe
, data
)));
6149 trace_debug ("Uploading: traceframe didn't fit");
6150 finish_traceframe (tframe
);
6153 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6155 /* If we freed the traceframe that wrapped around, go back
6156 to the non-wrap case. */
6157 if (tf
< ipa_trace_buffer_ctrl
.start
)
6159 trace_debug ("Lib: Discarding past the wraparound");
6160 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6162 ipa_trace_buffer_ctrl
.start
= tf
;
6163 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6164 ++ipa_traceframe_read_count
;
6166 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6167 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6169 trace_debug ("Lib: buffer is fully empty. "
6170 "Trace buffer [%d] start=%d free=%d endfree=%d",
6172 (int) (ipa_trace_buffer_ctrl
.start
6173 - ipa_trace_buffer_lo
),
6174 (int) (ipa_trace_buffer_ctrl
.free
6175 - ipa_trace_buffer_lo
),
6176 (int) (ipa_trace_buffer_ctrl
.end_free
6177 - ipa_trace_buffer_lo
));
6179 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6180 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6181 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6182 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6185 trace_debug ("Uploaded a traceframe\n"
6186 "Lib: Trace buffer [%d] start=%d free=%d "
6187 "endfree=%d wrap=%d hi=%d",
6189 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6190 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6191 (int) (ipa_trace_buffer_ctrl
.end_free
6192 - ipa_trace_buffer_lo
),
6193 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6194 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6197 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6198 (unsigned char *) &ipa_trace_buffer_ctrl
,
6199 sizeof (struct ipa_trace_buffer_control
)))
6202 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6203 ipa_traceframe_read_count
);
6205 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6208 cancel_breakpoints ();
6210 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6211 about_to_request_buffer_space_bkpt
= NULL
;
6215 if (trace_buffer_is_full
)
6220 #ifdef IN_PROCESS_AGENT
6222 IP_AGENT_EXPORT
int ust_loaded
;
6223 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6227 /* Static tracepoints. */
6229 /* UST puts a "struct tracepoint" in the global namespace, which
6230 conflicts with our tracepoint. Arguably, being a library, it
6231 shouldn't take ownership of such a generic name. We work around it
6233 #define tracepoint ust_tracepoint
6234 #include <ust/ust.h>
6237 extern int serialize_to_text (char *outbuf
, int bufsize
,
6238 const char *fmt
, va_list ap
);
6240 #define GDB_PROBE_NAME "gdb"
6242 /* We dynamically search for the UST symbols instead of linking them
6243 in. This lets the user decide if the application uses static
6244 tracepoints, instead of always pulling libust.so in. This vector
6245 holds pointers to all functions we care about. */
6249 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6250 const char *fmt
, va_list ap
);
6252 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6253 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6255 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6257 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6260 void (*marker_iter_start
) (struct marker_iter
*iter
);
6261 void (*marker_iter_next
) (struct marker_iter
*iter
);
6262 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6263 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6268 /* Cast through typeof to catch incompatible API changes. Since UST
6269 only builds with gcc, we can freely use gcc extensions here
6271 #define GET_UST_SYM(SYM) \
6274 if (ust_ops.SYM == NULL) \
6275 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6276 if (ust_ops.SYM == NULL) \
6280 #define USTF(SYM) ust_ops.SYM
6282 /* Get pointers to all libust.so functions we care about. */
6287 GET_UST_SYM (serialize_to_text
);
6289 GET_UST_SYM (ltt_probe_register
);
6290 GET_UST_SYM (ltt_probe_unregister
);
6291 GET_UST_SYM (ltt_marker_connect
);
6292 GET_UST_SYM (ltt_marker_disconnect
);
6294 GET_UST_SYM (marker_iter_start
);
6295 GET_UST_SYM (marker_iter_next
);
6296 GET_UST_SYM (marker_iter_stop
);
6297 GET_UST_SYM (marker_iter_reset
);
6303 /* Given an UST marker, return the matching gdb static tracepoint.
6304 The match is done by address. */
6306 static struct tracepoint
*
6307 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6309 struct tracepoint
*tpoint
;
6311 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6313 if (tpoint
->type
!= static_tracepoint
)
6316 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6323 /* The probe function we install on lttng/ust markers. Whenever a
6324 probed ust marker is hit, this function is called. This is similar
6325 to gdb_collect, only for static tracepoints, instead of fast
6329 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6330 struct registers
*regs
, void *call_private
,
6331 const char *fmt
, va_list *args
)
6333 struct tracepoint
*tpoint
;
6334 struct static_tracepoint_ctx ctx
;
6336 /* Don't do anything until the trace run is completely set up. */
6339 trace_debug ("gdb_probe: not tracing\n");
6343 ctx
.base
.type
= static_tracepoint
;
6344 ctx
.regcache_initted
= 0;
6349 /* Wrap the regblock in a register cache (in the stack, we don't
6350 want to malloc here). */
6351 ctx
.regspace
= alloca (register_cache_size ());
6352 if (ctx
.regspace
== NULL
)
6354 trace_debug ("Trace buffer block allocation failed, skipping");
6358 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6361 trace_debug ("gdb_probe: marker not known: "
6362 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6363 mdata
->location
, mdata
->channel
,
6364 mdata
->name
, mdata
->format
);
6368 if (!tpoint
->enabled
)
6370 trace_debug ("gdb_probe: tracepoint disabled");
6374 ctx
.tpoint
= tpoint
;
6376 trace_debug ("gdb_probe: collecting marker: "
6377 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6378 mdata
->location
, mdata
->channel
,
6379 mdata
->name
, mdata
->format
);
6381 /* Test the condition if present, and collect if true. */
6382 if (tpoint
->cond
== NULL
6383 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6386 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6387 tpoint
->address
, tpoint
);
6389 if (stopping_tracepoint
6390 || trace_buffer_is_full
6391 || expr_eval_result
!= expr_eval_no_error
)
6396 /* If there was a condition and it evaluated to false, the only
6397 way we would stop tracing is if there was an error during
6398 condition expression evaluation. */
6399 if (expr_eval_result
!= expr_eval_no_error
)
6404 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6405 static tracepoint string data. This is a string passed to the
6406 tracing library by the user, at the time of the tracepoint marker
6407 call. E.g., in the UST marker call:
6409 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6411 the collected data is "str FOOBAZ".
6415 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6416 struct traceframe
*tframe
)
6418 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6419 unsigned char *bufspace
;
6422 unsigned short blocklen
;
6426 trace_debug ("Wanted to collect static trace data, "
6427 "but there's no static trace data");
6431 va_copy (copy
, *umd
->args
);
6432 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6435 trace_debug ("Want to collect ust data");
6437 /* 'S' + size + string */
6438 bufspace
= add_traceframe_block (tframe
,
6439 1 + sizeof (blocklen
) + size
+ 1);
6440 if (bufspace
== NULL
)
6442 trace_debug ("Trace buffer block allocation failed, skipping");
6446 /* Identify a static trace data block. */
6449 blocklen
= size
+ 1;
6450 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6452 va_copy (copy
, *umd
->args
);
6453 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6454 size
+ 1, umd
->fmt
, copy
);
6457 trace_debug ("Storing static tracepoint data in regblock: %s",
6458 bufspace
+ 1 + sizeof (blocklen
));
6461 /* The probe to register with lttng/ust. */
6462 static struct ltt_available_probe gdb_ust_probe
=
6469 #endif /* HAVE_UST */
6470 #endif /* IN_PROCESS_AGENT */
6472 #ifndef IN_PROCESS_AGENT
6474 /* Ask the in-process agent to run a command. Since we don't want to
6475 have to handle the IPA hitting breakpoints while running the
6476 command, we pause all threads, remove all breakpoints, and then set
6477 the helper thread re-running. We communicate with the helper
6478 thread by means of direct memory xfering, and a socket for
6482 run_inferior_command (char *cmd
)
6485 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
6487 trace_debug ("run_inferior_command: running: %s", cmd
);
6490 uninsert_all_breakpoints ();
6492 err
= agent_run_command (pid
, (const char *) cmd
);
6494 reinsert_all_breakpoints ();
6500 #else /* !IN_PROCESS_AGENT */
6502 #include <sys/socket.h>
6505 #ifndef UNIX_PATH_MAX
6506 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6509 /* Where we put the socked used for synchronization. */
6510 #define SOCK_DIR P_tmpdir
6512 /* Thread ID of the helper thread. GDBserver reads this to know which
6513 is the help thread. This is an LWP id on Linux. */
6514 int helper_thread_id
;
6517 init_named_socket (const char *name
)
6520 struct sockaddr_un addr
;
6522 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6525 warning ("socket creation failed: %s", strerror (errno
));
6529 addr
.sun_family
= AF_UNIX
;
6531 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6532 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6534 result
= access (name
, F_OK
);
6538 result
= unlink (name
);
6541 warning ("unlink failed: %s", strerror (errno
));
6545 warning ("socket %s already exists; overwriting", name
);
6548 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6551 warning ("bind failed: %s", strerror (errno
));
6556 result
= listen (fd
, 1);
6559 warning ("listen: %s", strerror (errno
));
6568 gdb_agent_socket_init (void)
6571 char name
[UNIX_PATH_MAX
];
6573 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6574 SOCK_DIR
, getpid ());
6575 if (result
>= UNIX_PATH_MAX
)
6577 trace_debug ("string overflow allocating socket name");
6581 fd
= init_named_socket (name
);
6583 warning ("Error initializing named socket (%s) for communication with the "
6584 "ust helper thread. Check that directory exists and that it "
6585 "is writable.", name
);
6592 /* The next marker to be returned on a qTsSTM command. */
6593 static const struct marker
*next_st
;
6595 /* Returns the first known marker. */
6600 struct marker_iter iter
;
6602 USTF(marker_iter_reset
) (&iter
);
6603 USTF(marker_iter_start
) (&iter
);
6608 /* Returns the marker following M. */
6610 const struct marker
*
6611 next_marker (const struct marker
*m
)
6613 struct marker_iter iter
;
6615 USTF(marker_iter_reset
) (&iter
);
6616 USTF(marker_iter_start
) (&iter
);
6618 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6620 if (iter
.marker
== m
)
6622 USTF(marker_iter_next
) (&iter
);
6630 /* Return an hexstr version of the STR C string, fit for sending to
6634 cstr_to_hexstr (const char *str
)
6636 int len
= strlen (str
);
6637 char *hexstr
= xmalloc (len
* 2 + 1);
6638 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6642 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6646 response_ust_marker (char *packet
, const struct marker
*st
)
6648 char *strid
, *format
, *tmp
;
6650 next_st
= next_marker (st
);
6652 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6653 strlen (st
->name
) + 1);
6654 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6656 strid
= cstr_to_hexstr (tmp
);
6659 format
= cstr_to_hexstr (st
->format
);
6661 sprintf (packet
, "m%s:%s:%s",
6662 paddress ((uintptr_t) st
->location
),
6670 /* Return the first static tracepoint, and initialize the state
6671 machine that will iterate through all the static tracepoints. */
6674 cmd_qtfstm (char *packet
)
6676 trace_debug ("Returning first trace state variable definition");
6678 if (first_marker ())
6679 response_ust_marker (packet
, first_marker ());
6681 strcpy (packet
, "l");
6684 /* Return additional trace state variable definitions. */
6687 cmd_qtsstm (char *packet
)
6689 trace_debug ("Returning static tracepoint");
6692 response_ust_marker (packet
, next_st
);
6694 strcpy (packet
, "l");
6697 /* Disconnect the GDB probe from a marker at a given address. */
6700 unprobe_marker_at (char *packet
)
6704 struct marker_iter iter
;
6706 p
+= sizeof ("unprobe_marker_at:") - 1;
6708 p
= unpack_varlen_hex (p
, &address
);
6710 USTF(marker_iter_reset
) (&iter
);
6711 USTF(marker_iter_start
) (&iter
);
6712 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6713 if ((uintptr_t ) iter
.marker
->location
== address
)
6717 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
6721 warning ("could not disable marker %s/%s",
6722 iter
.marker
->channel
, iter
.marker
->name
);
6727 /* Connect the GDB probe to a marker at a given address. */
6730 probe_marker_at (char *packet
)
6734 struct marker_iter iter
;
6737 p
+= sizeof ("probe_marker_at:") - 1;
6739 p
= unpack_varlen_hex (p
, &address
);
6741 USTF(marker_iter_reset
) (&iter
);
6743 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6745 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6746 if ((uintptr_t ) m
->location
== address
)
6750 trace_debug ("found marker for address. "
6751 "ltt_marker_connect (marker = %s/%s)",
6752 m
->channel
, m
->name
);
6754 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
6756 if (result
&& result
!= -EEXIST
)
6757 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
6758 m
->channel
, m
->name
, -result
);
6762 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
6763 m
->channel
, m
->name
);
6767 strcpy (packet
, "OK");
6771 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
6776 cmd_qtstmat (char *packet
)
6780 struct marker_iter iter
;
6783 p
+= sizeof ("qTSTMat:") - 1;
6785 p
= unpack_varlen_hex (p
, &address
);
6787 USTF(marker_iter_reset
) (&iter
);
6789 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6791 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6792 if ((uintptr_t ) m
->location
== address
)
6794 response_ust_marker (packet
, m
);
6798 strcpy (packet
, "l");
6808 USTF(ltt_probe_register
) (&gdb_ust_probe
);
6811 #endif /* HAVE_UST */
6813 #include <sys/syscall.h>
6815 /* Helper thread of agent. */
6818 gdb_agent_helper_thread (void *arg
)
6824 listen_fd
= gdb_agent_socket_init ();
6826 if (helper_thread_id
== 0)
6827 helper_thread_id
= syscall (SYS_gettid
);
6829 if (listen_fd
== -1)
6831 warning ("could not create sync socket\n");
6838 struct sockaddr_un sockaddr
;
6843 tmp
= sizeof (sockaddr
);
6847 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
6849 /* It seems an ERESTARTSYS can escape out of accept. */
6850 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
6854 warning ("Accept returned %d, error: %s\n",
6855 fd
, strerror (errno
));
6861 ret
= read (fd
, buf
, 1);
6862 } while (ret
== -1 && errno
== EINTR
);
6866 warning ("reading socket (fd=%d) failed with %s",
6867 fd
, strerror (errno
));
6875 if (strcmp ("qTfSTM", cmd_buf
) == 0)
6877 cmd_qtfstm (cmd_buf
);
6879 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
6881 cmd_qtsstm (cmd_buf
);
6883 else if (strncmp ("unprobe_marker_at:",
6885 sizeof ("unprobe_marker_at:") - 1) == 0)
6887 unprobe_marker_at (cmd_buf
);
6889 else if (strncmp ("probe_marker_at:",
6891 sizeof ("probe_marker_at:") - 1) == 0)
6893 probe_marker_at (cmd_buf
);
6895 else if (strncmp ("qTSTMat:",
6897 sizeof ("qTSTMat:") - 1) == 0)
6899 cmd_qtstmat (cmd_buf
);
6901 #endif /* HAVE_UST */
6904 /* Fix compiler's warning: ignoring return value of 'write'. */
6905 ret
= write (fd
, buf
, 1);
6914 #include <pthread.h>
6916 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
6919 gdb_agent_init (void)
6926 /* We want the helper thread to be as transparent as possible, so
6927 have it inherit an all-signals-blocked mask. */
6929 sigfillset (&new_mask
);
6930 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
6932 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
6934 res
= pthread_create (&thread
,
6936 gdb_agent_helper_thread
,
6939 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
6941 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
6943 while (helper_thread_id
== 0)
6951 #include <sys/mman.h>
6954 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
6955 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
6956 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
6957 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
6958 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
6959 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
6961 /* Record the result of getting buffer space for fast tracepoint
6962 trampolines. Any error message is copied, since caller may not be
6963 using persistent storage. */
6966 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
6968 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
6969 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
6971 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
6973 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
6976 static void __attribute__ ((constructor
))
6977 initialize_tracepoint_ftlib (void)
6979 initialize_tracepoint ();
6984 #endif /* IN_PROCESS_AGENT */
6986 /* Return a timestamp, expressed as microseconds of the usual Unix
6987 time. (As the result is a 64-bit number, it will not overflow any
6991 get_timestamp (void)
6995 if (gettimeofday (&tv
, 0) != 0)
6998 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7002 initialize_tracepoint (void)
7004 /* There currently no way to change the buffer size. */
7005 const int sizeOfBuffer
= 5 * 1024 * 1024;
7006 unsigned char *buf
= xmalloc (sizeOfBuffer
);
7007 init_trace_buffer (buf
, sizeOfBuffer
);
7009 /* Wire trace state variable 1 to be the timestamp. This will be
7010 uploaded to GDB upon connection and become one of its trace state
7011 variables. (In case you're wondering, if GDB already has a trace
7012 variable numbered 1, it will be renumbered.) */
7013 create_trace_state_variable (1, 0);
7014 set_trace_state_variable_name (1, "trace_timestamp");
7015 set_trace_state_variable_getter (1, get_timestamp
);
7017 #ifdef IN_PROCESS_AGENT
7022 pagesize
= sysconf (_SC_PAGE_SIZE
);
7026 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7028 #define SCRATCH_BUFFER_NPAGES 20
7030 /* Allocate scratch buffer aligned on a page boundary, at a low
7031 address (close to the main executable's code). */
7032 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7034 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7035 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7036 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7038 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7044 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7047 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7050 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7052 /* It's not a fatal error for something to go wrong with trampoline
7053 buffer setup, but it can be mysterious, so create a channel to
7054 report back on what went wrong, using a fixed size since we may
7055 not be able to allocate space later when the problem occurs. */
7056 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7058 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7060 initialize_low_tracepoint ();