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
);
1318 static LONGEST
get_timestamp (void);
1320 #if defined(__GNUC__)
1321 # define memory_barrier() asm volatile ("" : : : "memory")
1323 # define memory_barrier() do {} while (0)
1326 /* We only build the IPA if this builtin is supported, and there are
1327 no uses of this in GDBserver itself, so we're safe in defining this
1329 #define cmpxchg(mem, oldval, newval) \
1330 __sync_val_compare_and_swap (mem, oldval, newval)
1332 /* Record that an error occurred during expression evaluation. */
1335 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1336 enum eval_result_type rtype
)
1338 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1339 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1341 #ifdef IN_PROCESS_AGENT
1342 /* Only record the first error we get. */
1343 if (cmpxchg (&expr_eval_result
,
1345 rtype
) != expr_eval_no_error
)
1348 if (expr_eval_result
!= expr_eval_no_error
)
1352 error_tracepoint
= tpoint
;
1355 /* Trace buffer management. */
1358 clear_trace_buffer (void)
1360 trace_buffer_start
= trace_buffer_lo
;
1361 trace_buffer_free
= trace_buffer_lo
;
1362 trace_buffer_end_free
= trace_buffer_hi
;
1363 trace_buffer_wrap
= trace_buffer_hi
;
1364 /* A traceframe with zeroed fields marks the end of trace data. */
1365 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1366 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1367 traceframe_read_count
= traceframe_write_count
= 0;
1368 traceframes_created
= 0;
1371 #ifndef IN_PROCESS_AGENT
1374 clear_inferior_trace_buffer (void)
1376 CORE_ADDR ipa_trace_buffer_lo
;
1377 CORE_ADDR ipa_trace_buffer_hi
;
1378 struct traceframe ipa_traceframe
= { 0 };
1379 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1381 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1382 &ipa_trace_buffer_lo
);
1383 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1384 &ipa_trace_buffer_hi
);
1386 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1387 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1388 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1389 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1391 /* A traceframe with zeroed fields marks the end of trace data. */
1392 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1393 (unsigned char *) &ipa_trace_buffer_ctrl
,
1394 sizeof (ipa_trace_buffer_ctrl
));
1396 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1398 /* A traceframe with zeroed fields marks the end of trace data. */
1399 write_inferior_memory (ipa_trace_buffer_lo
,
1400 (unsigned char *) &ipa_traceframe
,
1401 sizeof (ipa_traceframe
));
1403 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1404 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1405 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1411 init_trace_buffer (unsigned char *buf
, int bufsize
)
1413 trace_buffer_lo
= buf
;
1414 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1416 clear_trace_buffer ();
1419 #ifdef IN_PROCESS_AGENT
1421 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1422 about_to_request_buffer_space (void)
1424 /* GDBserver places breakpoint here while it goes about to flush
1425 data at random times. */
1426 UNKNOWN_SIDE_EFFECTS();
1431 /* Carve out a piece of the trace buffer, returning NULL in case of
1435 trace_buffer_alloc (size_t amt
)
1437 unsigned char *rslt
;
1438 struct trace_buffer_control
*tbctrl
;
1440 #ifdef IN_PROCESS_AGENT
1441 unsigned int prev
, prev_filtered
;
1442 unsigned int commit_count
;
1443 unsigned int commit
;
1444 unsigned int readout
;
1446 struct traceframe
*oldest
;
1447 unsigned char *new_start
;
1450 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1451 (long) amt
, (long) sizeof (struct traceframe
));
1453 /* Account for the EOB marker. */
1454 amt
+= sizeof (struct traceframe
);
1456 #ifdef IN_PROCESS_AGENT
1460 /* Read the current token and extract the index to try to write to,
1461 storing it in CURR. */
1462 prev
= trace_buffer_ctrl_curr
;
1463 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1464 curr
= prev_filtered
+ 1;
1468 about_to_request_buffer_space ();
1470 /* Start out with a copy of the current state. GDBserver may be
1471 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1472 be able to commit anyway if that happens. */
1473 trace_buffer_ctrl
[curr
]
1474 = trace_buffer_ctrl
[prev_filtered
];
1475 trace_debug ("trying curr=%u", curr
);
1477 /* The GDBserver's agent doesn't need all that syncing, and always
1478 updates TCB 0 (there's only one, mind you). */
1481 tbctrl
= &trace_buffer_ctrl
[curr
];
1483 /* Offsets are easier to grok for debugging than raw addresses,
1484 especially for the small trace buffer sizes that are useful for
1486 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1488 (int) (tbctrl
->start
- trace_buffer_lo
),
1489 (int) (tbctrl
->free
- trace_buffer_lo
),
1490 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1491 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1492 (int) (trace_buffer_hi
- trace_buffer_lo
));
1494 /* The algorithm here is to keep trying to get a contiguous block of
1495 the requested size, possibly discarding older traceframes to free
1496 up space. Since free space might come in one or two pieces,
1497 depending on whether discarded traceframes wrapped around at the
1498 high end of the buffer, we test both pieces after each
1502 /* First, if we have two free parts, try the upper one first. */
1503 if (tbctrl
->end_free
< tbctrl
->free
)
1505 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1506 /* We have enough in the upper part. */
1510 /* Our high part of free space wasn't enough. Give up
1511 on it for now, set wraparound. We will recover the
1512 space later, if/when the wrapped-around traceframe is
1514 trace_debug ("Upper part too small, setting wraparound");
1515 tbctrl
->wrap
= tbctrl
->free
;
1516 tbctrl
->free
= trace_buffer_lo
;
1520 /* The normal case. */
1521 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1524 #ifdef IN_PROCESS_AGENT
1525 /* The IP Agent's buffer is always circular. It isn't used
1526 currently, but `circular_trace_buffer' could represent
1527 GDBserver's mode. If we didn't find space, ask GDBserver to
1530 flush_trace_buffer ();
1534 trace_debug ("gdbserver flushed buffer, retrying");
1538 /* GDBserver cancelled the tracing. Bail out as well. */
1541 /* If we're here, then neither part is big enough, and
1542 non-circular trace buffers are now full. */
1543 if (!circular_trace_buffer
)
1545 trace_debug ("Not enough space in the trace buffer");
1549 trace_debug ("Need more space in the trace buffer");
1551 /* If we have a circular buffer, we can try discarding the
1552 oldest traceframe and see if that helps. */
1553 oldest
= FIRST_TRACEFRAME ();
1554 if (oldest
->tpnum
== 0)
1556 /* Not good; we have no traceframes to free. Perhaps we're
1557 asking for a block that is larger than the buffer? In
1558 any case, give up. */
1559 trace_debug ("No traceframes to discard");
1563 /* We don't run this code in the in-process agent currently.
1564 E.g., we could leave the in-process agent in autonomous
1565 circular mode if we only have fast tracepoints. If we do
1566 that, then this bit becomes racy with GDBserver, which also
1567 writes to this counter. */
1568 --traceframe_write_count
;
1570 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1571 /* If we freed the traceframe that wrapped around, go back
1572 to the non-wrap case. */
1573 if (new_start
< tbctrl
->start
)
1575 trace_debug ("Discarding past the wraparound");
1576 tbctrl
->wrap
= trace_buffer_hi
;
1578 tbctrl
->start
= new_start
;
1579 tbctrl
->end_free
= tbctrl
->start
;
1581 trace_debug ("Discarded a traceframe\n"
1582 "Trace buffer [%d], start=%d free=%d "
1583 "endfree=%d wrap=%d hi=%d",
1585 (int) (tbctrl
->start
- trace_buffer_lo
),
1586 (int) (tbctrl
->free
- trace_buffer_lo
),
1587 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1588 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1589 (int) (trace_buffer_hi
- trace_buffer_lo
));
1591 /* Now go back around the loop. The discard might have resulted
1592 in either one or two pieces of free space, so we want to try
1593 both before freeing any more traceframes. */
1597 /* If we get here, we know we can provide the asked-for space. */
1599 rslt
= tbctrl
->free
;
1601 /* Adjust the request back down, now that we know we have space for
1602 the marker, but don't commit to AMT yet, we may still need to
1603 restart the operation if GDBserver touches the trace buffer
1604 (obviously only important in the in-process agent's version). */
1605 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1607 /* Or not. If GDBserver changed the trace buffer behind our back,
1608 we get to restart a new allocation attempt. */
1610 #ifdef IN_PROCESS_AGENT
1611 /* Build the tentative token. */
1612 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1613 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1614 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1618 /* Try to commit it. */
1619 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1620 if (readout
!= prev
)
1622 trace_debug ("GDBserver has touched the trace buffer, restarting."
1623 " (prev=%08x, commit=%08x, readout=%08x)",
1624 prev
, commit
, readout
);
1628 /* Hold your horses here. Even if that change was committed,
1629 GDBserver could come in, and clobber it. We need to hold to be
1630 able to tell if GDBserver clobbers before or after we committed
1631 the change. Whenever GDBserver goes about touching the IPA
1632 buffer, it sets a breakpoint in this routine, so we have a sync
1634 about_to_request_buffer_space ();
1636 /* Check if the change has been effective, even if GDBserver stopped
1637 us at the breakpoint. */
1640 unsigned int refetch
;
1644 refetch
= trace_buffer_ctrl_curr
;
1646 if (refetch
== commit
1647 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1650 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1651 "readout=%08x, refetch=%08x)",
1652 prev
, commit
, readout
, refetch
);
1656 trace_debug ("GDBserver has touched the trace buffer, not effective."
1657 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1658 prev
, commit
, readout
, refetch
);
1664 /* We have a new piece of the trace buffer. Hurray! */
1666 /* Add an EOB marker just past this allocation. */
1667 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1668 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1670 /* Adjust the request back down, now that we know we have space for
1672 amt
-= sizeof (struct traceframe
);
1676 trace_debug ("Allocated %d bytes", (int) amt
);
1677 trace_debug ("Trace buffer [%d] start=%d free=%d "
1678 "endfree=%d wrap=%d hi=%d",
1680 (int) (tbctrl
->start
- trace_buffer_lo
),
1681 (int) (tbctrl
->free
- trace_buffer_lo
),
1682 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1683 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1684 (int) (trace_buffer_hi
- trace_buffer_lo
));
1690 #ifndef IN_PROCESS_AGENT
1692 /* Return the total free space. This is not necessarily the largest
1693 block we can allocate, because of the two-part case. */
1698 if (trace_buffer_free
<= trace_buffer_end_free
)
1699 return trace_buffer_end_free
- trace_buffer_free
;
1701 return ((trace_buffer_end_free
- trace_buffer_lo
)
1702 + (trace_buffer_hi
- trace_buffer_free
));
1705 /* An 'S' in continuation packets indicates remainder are for
1708 static int seen_step_action_flag
;
1710 /* Create a tracepoint (location) with given number and address. Add this
1711 new tracepoint to list and sort this list. */
1713 static struct tracepoint
*
1714 add_tracepoint (int num
, CORE_ADDR addr
)
1716 struct tracepoint
*tpoint
, **tp_next
;
1718 tpoint
= xmalloc (sizeof (struct tracepoint
));
1719 tpoint
->number
= num
;
1720 tpoint
->address
= addr
;
1721 tpoint
->numactions
= 0;
1722 tpoint
->actions
= NULL
;
1723 tpoint
->actions_str
= NULL
;
1724 tpoint
->cond
= NULL
;
1725 tpoint
->num_step_actions
= 0;
1726 tpoint
->step_actions
= NULL
;
1727 tpoint
->step_actions_str
= NULL
;
1728 /* Start all off as regular (slow) tracepoints. */
1729 tpoint
->type
= trap_tracepoint
;
1730 tpoint
->orig_size
= -1;
1731 tpoint
->source_strings
= NULL
;
1732 tpoint
->compiled_cond
= 0;
1733 tpoint
->handle
= NULL
;
1734 tpoint
->next
= NULL
;
1736 /* Find a place to insert this tracepoint into list in order to keep
1737 the tracepoint list still in the ascending order. There may be
1738 multiple tracepoints at the same address as TPOINT's, and this
1739 guarantees TPOINT is inserted after all the tracepoints which are
1740 set at the same address. For example, fast tracepoints A, B, C are
1741 set at the same address, and D is to be insert at the same place as
1744 -->| A |--> | B |-->| C |->...
1746 One jump pad was created for tracepoint A, B, and C, and the target
1747 address of A is referenced/used in jump pad. So jump pad will let
1748 inferior jump to A. If D is inserted in front of A, like this,
1750 -->| D |-->| A |--> | B |-->| C |->...
1752 without updating jump pad, D is not reachable during collect, which
1753 is wrong. As we can see, the order of B, C and D doesn't matter, but
1754 A should always be the `first' one. */
1755 for (tp_next
= &tracepoints
;
1756 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1757 tp_next
= &(*tp_next
)->next
)
1759 tpoint
->next
= *tp_next
;
1761 last_tracepoint
= tpoint
;
1763 seen_step_action_flag
= 0;
1768 #ifndef IN_PROCESS_AGENT
1770 /* Return the tracepoint with the given number and address, or NULL. */
1772 static struct tracepoint
*
1773 find_tracepoint (int id
, CORE_ADDR addr
)
1775 struct tracepoint
*tpoint
;
1777 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1778 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1784 /* Remove TPOINT from global list. */
1787 remove_tracepoint (struct tracepoint
*tpoint
)
1789 struct tracepoint
*tp
, *tp_prev
;
1791 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1792 tp_prev
= tp
, tp
= tp
->next
)
1798 tp_prev
->next
= tp
->next
;
1800 tracepoints
= tp
->next
;
1806 /* There may be several tracepoints with the same number (because they
1807 are "locations", in GDB parlance); return the next one after the
1808 given tracepoint, or search from the beginning of the list if the
1809 first argument is NULL. */
1811 static struct tracepoint
*
1812 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1814 struct tracepoint
*tpoint
;
1817 tpoint
= prev_tp
->next
;
1819 tpoint
= tracepoints
;
1820 for (; tpoint
; tpoint
= tpoint
->next
)
1821 if (tpoint
->number
== num
)
1830 save_string (const char *str
, size_t len
)
1834 s
= xmalloc (len
+ 1);
1835 memcpy (s
, str
, len
);
1841 /* Append another action to perform when the tracepoint triggers. */
1844 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1850 seen_step_action_flag
= 1;
1858 char *act_start
= act
;
1859 struct tracepoint_action
*action
= NULL
;
1865 struct collect_memory_action
*maction
;
1869 maction
= xmalloc (sizeof *maction
);
1870 maction
->base
.type
= *act
;
1871 maction
->base
.ops
= &m_tracepoint_action_ops
;
1872 action
= &maction
->base
;
1875 is_neg
= (*act
== '-');
1878 act
= unpack_varlen_hex (act
, &basereg
);
1880 act
= unpack_varlen_hex (act
, &maction
->addr
);
1882 act
= unpack_varlen_hex (act
, &maction
->len
);
1883 maction
->basereg
= (is_neg
1886 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1887 pulongest (maction
->len
),
1888 paddress (maction
->addr
), maction
->basereg
);
1893 struct collect_registers_action
*raction
;
1895 raction
= xmalloc (sizeof *raction
);
1896 raction
->base
.type
= *act
;
1897 raction
->base
.ops
= &r_tracepoint_action_ops
;
1898 action
= &raction
->base
;
1900 trace_debug ("Want to collect registers");
1902 /* skip past hex digits of mask for now */
1903 while (isxdigit(*act
))
1909 struct collect_static_trace_data_action
*raction
;
1911 raction
= xmalloc (sizeof *raction
);
1912 raction
->base
.type
= *act
;
1913 raction
->base
.ops
= &l_tracepoint_action_ops
;
1914 action
= &raction
->base
;
1916 trace_debug ("Want to collect static trace data");
1921 trace_debug ("Unexpected step action, ignoring");
1926 struct eval_expr_action
*xaction
;
1928 xaction
= xmalloc (sizeof (*xaction
));
1929 xaction
->base
.type
= *act
;
1930 xaction
->base
.ops
= &x_tracepoint_action_ops
;
1931 action
= &xaction
->base
;
1933 trace_debug ("Want to evaluate expression");
1934 xaction
->expr
= gdb_parse_agent_expr (&act
);
1938 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1947 if (seen_step_action_flag
)
1949 tpoint
->num_step_actions
++;
1951 tpoint
->step_actions
1952 = xrealloc (tpoint
->step_actions
,
1953 (sizeof (*tpoint
->step_actions
)
1954 * tpoint
->num_step_actions
));
1955 tpoint
->step_actions_str
1956 = xrealloc (tpoint
->step_actions_str
,
1957 (sizeof (*tpoint
->step_actions_str
)
1958 * tpoint
->num_step_actions
));
1959 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1960 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1961 = save_string (act_start
, act
- act_start
);
1965 tpoint
->numactions
++;
1967 = xrealloc (tpoint
->actions
,
1968 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1970 = xrealloc (tpoint
->actions_str
,
1971 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1972 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1973 tpoint
->actions_str
[tpoint
->numactions
- 1]
1974 = save_string (act_start
, act
- act_start
);
1981 /* Find or create a trace state variable with the given number. */
1983 static struct trace_state_variable
*
1984 get_trace_state_variable (int num
)
1986 struct trace_state_variable
*tsv
;
1988 #ifdef IN_PROCESS_AGENT
1989 /* Search for an existing variable. */
1990 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1991 if (tsv
->number
== num
)
1995 /* Search for an existing variable. */
1996 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1997 if (tsv
->number
== num
)
2003 /* Find or create a trace state variable with the given number. */
2005 static struct trace_state_variable
*
2006 create_trace_state_variable (int num
, int gdb
)
2008 struct trace_state_variable
*tsv
;
2010 tsv
= get_trace_state_variable (num
);
2014 /* Create a new variable. */
2015 tsv
= xmalloc (sizeof (struct trace_state_variable
));
2017 tsv
->initial_value
= 0;
2021 #ifdef IN_PROCESS_AGENT
2024 tsv
->next
= alloced_trace_state_variables
;
2025 alloced_trace_state_variables
= tsv
;
2030 tsv
->next
= trace_state_variables
;
2031 trace_state_variables
= tsv
;
2036 IP_AGENT_EXPORT LONGEST
2037 get_trace_state_variable_value (int num
)
2039 struct trace_state_variable
*tsv
;
2041 tsv
= get_trace_state_variable (num
);
2045 trace_debug ("No trace state variable %d, skipping value get", num
);
2049 /* Call a getter function if we have one. While it's tempting to
2050 set up something to only call the getter once per tracepoint hit,
2051 it could run afoul of thread races. Better to let the getter
2052 handle it directly, if necessary to worry about it. */
2054 tsv
->value
= (tsv
->getter
) ();
2056 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2057 num
, plongest (tsv
->value
));
2062 IP_AGENT_EXPORT
void
2063 set_trace_state_variable_value (int num
, LONGEST val
)
2065 struct trace_state_variable
*tsv
;
2067 tsv
= get_trace_state_variable (num
);
2071 trace_debug ("No trace state variable %d, skipping value set", num
);
2079 agent_get_trace_state_variable_value (int num
)
2081 return get_trace_state_variable_value (num
);
2085 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2087 set_trace_state_variable_value (num
, val
);
2091 set_trace_state_variable_name (int num
, const char *name
)
2093 struct trace_state_variable
*tsv
;
2095 tsv
= get_trace_state_variable (num
);
2099 trace_debug ("No trace state variable %d, skipping name set", num
);
2103 tsv
->name
= (char *) name
;
2107 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2109 struct trace_state_variable
*tsv
;
2111 tsv
= get_trace_state_variable (num
);
2115 trace_debug ("No trace state variable %d, skipping getter set", num
);
2119 tsv
->getter
= getter
;
2122 /* Add a raw traceframe for the given tracepoint. */
2124 static struct traceframe
*
2125 add_traceframe (struct tracepoint
*tpoint
)
2127 struct traceframe
*tframe
;
2129 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2134 tframe
->tpnum
= tpoint
->number
;
2135 tframe
->data_size
= 0;
2140 /* Add a block to the traceframe currently being worked on. */
2142 static unsigned char *
2143 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2145 unsigned char *block
;
2150 block
= trace_buffer_alloc (amt
);
2155 tframe
->data_size
+= amt
;
2160 /* Flag that the current traceframe is finished. */
2163 finish_traceframe (struct traceframe
*tframe
)
2165 ++traceframe_write_count
;
2166 ++traceframes_created
;
2169 #ifndef IN_PROCESS_AGENT
2171 /* Given a traceframe number NUM, find the NUMth traceframe in the
2174 static struct traceframe
*
2175 find_traceframe (int num
)
2177 struct traceframe
*tframe
;
2180 for (tframe
= FIRST_TRACEFRAME ();
2182 tframe
= NEXT_TRACEFRAME (tframe
))
2193 get_traceframe_address (struct traceframe
*tframe
)
2196 struct tracepoint
*tpoint
;
2198 addr
= traceframe_get_pc (tframe
);
2203 /* Fallback strategy, will be incorrect for while-stepping frames
2204 and multi-location tracepoints. */
2205 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2206 return tpoint
->address
;
2209 /* Search for the next traceframe whose address is inside or outside
2212 static struct traceframe
*
2213 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2216 struct traceframe
*tframe
;
2219 *tfnump
= current_traceframe
+ 1;
2220 tframe
= find_traceframe (*tfnump
);
2221 /* The search is not supposed to wrap around. */
2228 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2230 tfaddr
= get_traceframe_address (tframe
);
2232 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2233 : (lo
> tfaddr
|| tfaddr
> hi
))
2242 /* Search for the next traceframe recorded by the given tracepoint.
2243 Note that for multi-location tracepoints, this will find whatever
2244 location appears first. */
2246 static struct traceframe
*
2247 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2249 struct traceframe
*tframe
;
2251 *tfnump
= current_traceframe
+ 1;
2252 tframe
= find_traceframe (*tfnump
);
2253 /* The search is not supposed to wrap around. */
2260 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2262 if (tframe
->tpnum
== num
)
2273 #ifndef IN_PROCESS_AGENT
2275 /* Clear all past trace state. */
2278 cmd_qtinit (char *packet
)
2280 struct trace_state_variable
*tsv
, *prev
, *next
;
2282 /* Make sure we don't try to read from a trace frame. */
2283 current_traceframe
= -1;
2285 trace_debug ("Initializing the trace");
2287 clear_installed_tracepoints ();
2288 clear_readonly_regions ();
2291 last_tracepoint
= NULL
;
2293 /* Clear out any leftover trace state variables. Ones with target
2294 defined getters should be kept however. */
2296 tsv
= trace_state_variables
;
2299 trace_debug ("Looking at var %d", tsv
->number
);
2300 if (tsv
->getter
== NULL
)
2306 trace_state_variables
= next
;
2307 trace_debug ("Deleting var %d", tsv
->number
);
2318 clear_trace_buffer ();
2319 clear_inferior_trace_buffer ();
2324 /* Unprobe the UST marker at ADDRESS. */
2327 unprobe_marker_at (CORE_ADDR address
)
2329 char cmd
[IPA_CMD_BUF_SIZE
];
2331 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2332 run_inferior_command (cmd
);
2335 /* Restore the program to its pre-tracing state. This routine may be called
2336 in error situations, so it needs to be careful about only restoring
2337 from known-valid bits. */
2340 clear_installed_tracepoints (void)
2342 struct tracepoint
*tpoint
;
2343 struct tracepoint
*prev_stpoint
;
2346 cancel_breakpoints ();
2348 prev_stpoint
= NULL
;
2350 /* Restore any bytes overwritten by tracepoints. */
2351 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2353 /* Catch the case where we might try to remove a tracepoint that
2354 was never actually installed. */
2355 if (tpoint
->handle
== NULL
)
2357 trace_debug ("Tracepoint %d at 0x%s was "
2358 "never installed, nothing to clear",
2359 tpoint
->number
, paddress (tpoint
->address
));
2363 switch (tpoint
->type
)
2365 case trap_tracepoint
:
2366 delete_breakpoint (tpoint
->handle
);
2368 case fast_tracepoint
:
2369 delete_fast_tracepoint_jump (tpoint
->handle
);
2371 case static_tracepoint
:
2372 if (prev_stpoint
!= NULL
2373 && prev_stpoint
->address
== tpoint
->address
)
2374 /* Nothing to do. We already unprobed a tracepoint set at
2375 this marker address (and there can only be one probe
2380 unprobe_marker_at (tpoint
->address
);
2381 prev_stpoint
= tpoint
;
2386 tpoint
->handle
= NULL
;
2392 /* Parse a packet that defines a tracepoint. */
2395 cmd_qtdp (char *own_buf
)
2398 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2399 int trail_hyphen
= 0;
2403 struct tracepoint
*tpoint
;
2405 char *packet
= own_buf
;
2407 packet
+= strlen ("QTDP:");
2409 /* A hyphen at the beginning marks a packet specifying actions for a
2410 tracepoint already supplied. */
2417 packet
= unpack_varlen_hex (packet
, &num
);
2418 ++packet
; /* skip a colon */
2419 packet
= unpack_varlen_hex (packet
, &addr
);
2420 ++packet
; /* skip a colon */
2422 /* See if we already have this tracepoint. */
2423 tpoint
= find_tracepoint (num
, addr
);
2427 /* Duplicate tracepoints are never allowed. */
2430 trace_debug ("Tracepoint error: tracepoint %d"
2431 " at 0x%s already exists",
2432 (int) num
, paddress (addr
));
2433 write_enn (own_buf
);
2437 tpoint
= add_tracepoint (num
, addr
);
2439 tpoint
->enabled
= (*packet
== 'E');
2440 ++packet
; /* skip 'E' */
2441 ++packet
; /* skip a colon */
2442 packet
= unpack_varlen_hex (packet
, &count
);
2443 tpoint
->step_count
= count
;
2444 ++packet
; /* skip a colon */
2445 packet
= unpack_varlen_hex (packet
, &count
);
2446 tpoint
->pass_count
= count
;
2447 /* See if we have any of the additional optional fields. */
2448 while (*packet
== ':')
2453 tpoint
->type
= fast_tracepoint
;
2455 packet
= unpack_varlen_hex (packet
, &count
);
2456 tpoint
->orig_size
= count
;
2458 else if (*packet
== 'S')
2460 tpoint
->type
= static_tracepoint
;
2463 else if (*packet
== 'X')
2465 actparm
= (char *) packet
;
2466 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2469 else if (*packet
== '-')
2471 else if (*packet
== '\0')
2474 trace_debug ("Unknown optional tracepoint field");
2479 trace_debug ("Also has actions\n");
2482 trace_debug ("Defined %stracepoint %d at 0x%s, "
2483 "enabled %d step %ld pass %ld",
2484 tpoint
->type
== fast_tracepoint
? "fast "
2485 : tpoint
->type
== static_tracepoint
? "static " : "",
2486 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2487 tpoint
->step_count
, tpoint
->pass_count
);
2490 add_tracepoint_action (tpoint
, packet
);
2493 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2494 (int) num
, paddress (addr
));
2495 write_enn (own_buf
);
2499 /* Install tracepoint during tracing only once for each tracepoint location.
2500 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2501 determine the last QTDP packet for one tracepoint location by checking
2502 trailing hyphen in QTDP packet. */
2503 if (tracing
&& !trail_hyphen
)
2505 /* Pause all threads temporarily while we patch tracepoints. */
2508 /* download_tracepoint will update global `tracepoints'
2509 list, so it is unsafe to leave threads in jump pad. */
2510 stabilize_threads ();
2512 /* Freeze threads. */
2515 download_tracepoint (tpoint
);
2516 install_tracepoint (tpoint
, own_buf
);
2517 if (strcmp (own_buf
, "OK") != 0)
2518 remove_tracepoint (tpoint
);
2528 cmd_qtdpsrc (char *own_buf
)
2530 ULONGEST num
, addr
, start
, slen
;
2531 struct tracepoint
*tpoint
;
2532 char *packet
= own_buf
;
2533 char *saved
, *srctype
, *src
;
2535 struct source_string
*last
, *newlast
;
2537 packet
+= strlen ("QTDPsrc:");
2539 packet
= unpack_varlen_hex (packet
, &num
);
2540 ++packet
; /* skip a colon */
2541 packet
= unpack_varlen_hex (packet
, &addr
);
2542 ++packet
; /* skip a colon */
2544 /* See if we already have this tracepoint. */
2545 tpoint
= find_tracepoint (num
, addr
);
2549 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2550 (int) num
, paddress (addr
));
2551 write_enn (own_buf
);
2556 packet
= strchr (packet
, ':');
2557 srctype
= xmalloc (packet
- saved
+ 1);
2558 memcpy (srctype
, saved
, packet
- saved
);
2559 srctype
[packet
- saved
] = '\0';
2561 packet
= unpack_varlen_hex (packet
, &start
);
2562 ++packet
; /* skip a colon */
2563 packet
= unpack_varlen_hex (packet
, &slen
);
2564 ++packet
; /* skip a colon */
2565 src
= xmalloc (slen
+ 1);
2566 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2569 newlast
= xmalloc (sizeof (struct source_string
));
2570 newlast
->type
= srctype
;
2572 newlast
->next
= NULL
;
2573 /* Always add a source string to the end of the list;
2574 this keeps sequences of actions/commands in the right
2576 if (tpoint
->source_strings
)
2578 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2580 last
->next
= newlast
;
2583 tpoint
->source_strings
= newlast
;
2589 cmd_qtdv (char *own_buf
)
2591 ULONGEST num
, val
, builtin
;
2594 struct trace_state_variable
*tsv
;
2595 char *packet
= own_buf
;
2597 packet
+= strlen ("QTDV:");
2599 packet
= unpack_varlen_hex (packet
, &num
);
2600 ++packet
; /* skip a colon */
2601 packet
= unpack_varlen_hex (packet
, &val
);
2602 ++packet
; /* skip a colon */
2603 packet
= unpack_varlen_hex (packet
, &builtin
);
2604 ++packet
; /* skip a colon */
2606 nbytes
= strlen (packet
) / 2;
2607 varname
= xmalloc (nbytes
+ 1);
2608 nbytes
= unhexify (varname
, packet
, nbytes
);
2609 varname
[nbytes
] = '\0';
2611 tsv
= create_trace_state_variable (num
, 1);
2612 tsv
->initial_value
= (LONGEST
) val
;
2613 tsv
->name
= varname
;
2615 set_trace_state_variable_value (num
, (LONGEST
) val
);
2621 cmd_qtenable_disable (char *own_buf
, int enable
)
2623 char *packet
= own_buf
;
2625 struct tracepoint
*tp
;
2627 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2628 packet
= unpack_varlen_hex (packet
, &num
);
2629 ++packet
; /* skip a colon */
2630 packet
= unpack_varlen_hex (packet
, &addr
);
2632 tp
= find_tracepoint (num
, addr
);
2636 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2638 trace_debug ("Tracepoint %d at 0x%s is already %s",
2639 (int) num
, paddress (addr
),
2640 enable
? "enabled" : "disabled");
2645 trace_debug ("%s tracepoint %d at 0x%s",
2646 enable
? "Enabling" : "Disabling",
2647 (int) num
, paddress (addr
));
2649 tp
->enabled
= enable
;
2651 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2654 int offset
= offsetof (struct tracepoint
, enabled
);
2655 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2657 ret
= prepare_to_access_memory ();
2660 trace_debug ("Failed to temporarily stop inferior threads");
2661 write_enn (own_buf
);
2665 ret
= write_inferior_integer (obj_addr
, enable
);
2666 done_accessing_memory ();
2670 trace_debug ("Cannot write enabled flag into "
2671 "inferior process memory");
2672 write_enn (own_buf
);
2681 trace_debug ("Tracepoint %d at 0x%s not found",
2682 (int) num
, paddress (addr
));
2683 write_enn (own_buf
);
2688 cmd_qtv (char *own_buf
)
2693 char *packet
= own_buf
;
2695 packet
+= strlen ("qTV:");
2696 unpack_varlen_hex (packet
, &num
);
2698 if (current_traceframe
>= 0)
2700 err
= traceframe_read_tsv ((int) num
, &val
);
2703 strcpy (own_buf
, "U");
2707 /* Only make tsv's be undefined before the first trace run. After a
2708 trace run is over, the user might want to see the last value of
2709 the tsv, and it might not be available in a traceframe. */
2710 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2712 strcpy (own_buf
, "U");
2716 val
= get_trace_state_variable_value (num
);
2718 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2721 /* Clear out the list of readonly regions. */
2724 clear_readonly_regions (void)
2726 struct readonly_region
*roreg
;
2728 while (readonly_regions
)
2730 roreg
= readonly_regions
;
2731 readonly_regions
= readonly_regions
->next
;
2736 /* Parse the collection of address ranges whose contents GDB believes
2737 to be unchanging and so can be read directly from target memory
2738 even while looking at a traceframe. */
2741 cmd_qtro (char *own_buf
)
2743 ULONGEST start
, end
;
2744 struct readonly_region
*roreg
;
2745 char *packet
= own_buf
;
2747 trace_debug ("Want to mark readonly regions");
2749 clear_readonly_regions ();
2751 packet
+= strlen ("QTro");
2753 while (*packet
== ':')
2755 ++packet
; /* skip a colon */
2756 packet
= unpack_varlen_hex (packet
, &start
);
2757 ++packet
; /* skip a comma */
2758 packet
= unpack_varlen_hex (packet
, &end
);
2759 roreg
= xmalloc (sizeof (struct readonly_region
));
2760 roreg
->start
= start
;
2762 roreg
->next
= readonly_regions
;
2763 readonly_regions
= roreg
;
2764 trace_debug ("Added readonly region from 0x%s to 0x%s",
2765 paddress (roreg
->start
), paddress (roreg
->end
));
2771 /* Test to see if the given range is in our list of readonly ranges.
2772 We only test for being entirely within a range, GDB is not going to
2773 send a single memory packet that spans multiple regions. */
2776 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2778 struct readonly_region
*roreg
;
2780 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2781 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2787 /* The maximum size of a jump pad entry. */
2788 static const int max_jump_pad_size
= 0x100;
2790 static CORE_ADDR gdb_jump_pad_head
;
2792 /* Return the address of the next free jump space. */
2795 get_jump_space_head (void)
2797 if (gdb_jump_pad_head
== 0)
2799 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2800 &gdb_jump_pad_head
))
2801 fatal ("error extracting jump_pad_buffer");
2804 return gdb_jump_pad_head
;
2807 /* Reserve USED bytes from the jump space. */
2810 claim_jump_space (ULONGEST used
)
2812 trace_debug ("claim_jump_space reserves %s bytes at %s",
2813 pulongest (used
), paddress (gdb_jump_pad_head
));
2814 gdb_jump_pad_head
+= used
;
2817 static CORE_ADDR trampoline_buffer_head
= 0;
2818 static CORE_ADDR trampoline_buffer_tail
;
2820 /* Reserve USED bytes from the trampoline buffer and return the
2821 address of the start of the reserved space in TRAMPOLINE. Returns
2822 non-zero if the space is successfully claimed. */
2825 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2827 if (!trampoline_buffer_head
)
2829 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2830 &trampoline_buffer_tail
))
2832 fatal ("error extracting trampoline_buffer");
2836 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2837 &trampoline_buffer_head
))
2839 fatal ("error extracting trampoline_buffer_end");
2844 /* Start claiming space from the top of the trampoline space. If
2845 the space is located at the bottom of the virtual address space,
2846 this reduces the possibility that corruption will occur if a null
2847 pointer is used to write to memory. */
2848 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2850 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2855 trampoline_buffer_head
-= used
;
2857 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2858 pulongest (used
), paddress (trampoline_buffer_head
));
2860 *trampoline
= trampoline_buffer_head
;
2864 /* Returns non-zero if there is space allocated for use in trampolines
2865 for fast tracepoints. */
2868 have_fast_tracepoint_trampoline_buffer (char *buf
)
2870 CORE_ADDR trampoline_end
, errbuf
;
2872 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2875 fatal ("error extracting trampoline_buffer_end");
2882 strcpy (buf
, "was claiming");
2883 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
2886 fatal ("error extracting errbuf");
2890 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
2893 return trampoline_end
!= 0;
2896 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2897 the command fails, or 0 otherwise. If the command ran
2898 successfully, but probing the marker failed, ERROUT will be filled
2899 with the error to reply to GDB, and -1 is also returned. This
2900 allows directly passing IPA errors to GDB. */
2903 probe_marker_at (CORE_ADDR address
, char *errout
)
2905 char cmd
[IPA_CMD_BUF_SIZE
];
2908 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
2909 err
= run_inferior_command (cmd
);
2915 strcpy (errout
, cmd
);
2924 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
2926 to
->jump_pad
= from
->jump_pad
;
2927 to
->jump_pad_end
= from
->jump_pad_end
;
2928 to
->trampoline
= from
->trampoline
;
2929 to
->trampoline_end
= from
->trampoline_end
;
2930 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
2931 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
2932 to
->handle
= from
->handle
;
2934 gdb_assert (from
->handle
);
2935 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
2938 #define MAX_JUMP_SIZE 20
2940 /* Install fast tracepoint. Return 0 if successful, otherwise return
2944 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
2946 CORE_ADDR jentry
, jump_entry
;
2947 CORE_ADDR trampoline
;
2948 ULONGEST trampoline_size
;
2950 /* The jump to the jump pad of the last fast tracepoint
2952 unsigned char fjump
[MAX_JUMP_SIZE
];
2953 ULONGEST fjump_size
;
2955 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
2957 trace_debug ("Requested a fast tracepoint on an instruction "
2958 "that is of less than the minimum length.");
2962 jentry
= jump_entry
= get_jump_space_head ();
2965 trampoline_size
= 0;
2967 /* Install the jump pad. */
2968 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
2970 ipa_sym_addrs
.addr_gdb_collect
,
2971 ipa_sym_addrs
.addr_collecting
,
2974 &trampoline
, &trampoline_size
,
2976 &tpoint
->adjusted_insn_addr
,
2977 &tpoint
->adjusted_insn_addr_end
,
2984 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
2987 if (tpoint
->handle
!= NULL
)
2989 tpoint
->jump_pad
= jump_entry
;
2990 tpoint
->jump_pad_end
= jentry
;
2991 tpoint
->trampoline
= trampoline
;
2992 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
2994 /* Pad to 8-byte alignment. */
2995 jentry
= ((jentry
+ 7) & ~0x7);
2996 claim_jump_space (jentry
- jump_entry
);
3003 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3006 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3008 tpoint
->handle
= NULL
;
3011 if (tpoint
->type
== trap_tracepoint
)
3013 /* Tracepoints are installed as memory breakpoints. Just go
3014 ahead and install the trap. The breakpoints module
3015 handles duplicated breakpoints, and the memory read
3016 routine handles un-patching traps from memory reads. */
3017 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3018 tracepoint_handler
);
3020 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3022 struct tracepoint
*tp
;
3024 if (!agent_loaded_p ())
3026 trace_debug ("Requested a %s tracepoint, but fast "
3027 "tracepoints aren't supported.",
3028 tpoint
->type
== static_tracepoint
? "static" : "fast");
3029 write_e_ipa_not_loaded (own_buf
);
3032 if (tpoint
->type
== static_tracepoint
3033 && !in_process_agent_supports_ust ())
3035 trace_debug ("Requested a static tracepoint, but static "
3036 "tracepoints are not supported.");
3037 write_e_ust_not_loaded (own_buf
);
3041 /* Find another fast or static tracepoint at the same address. */
3042 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
3044 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
3045 && tp
->number
!= tpoint
->number
)
3049 if (tpoint
->type
== fast_tracepoint
)
3051 if (tp
) /* TPOINT is installed at the same address as TP. */
3052 clone_fast_tracepoint (tpoint
, tp
);
3054 install_fast_tracepoint (tpoint
, own_buf
);
3059 tpoint
->handle
= (void *) -1;
3062 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3063 tpoint
->handle
= (void *) -1;
3069 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3071 if (tpoint
->handle
== NULL
)
3073 if (*own_buf
== '\0')
3074 write_enn (own_buf
);
3081 cmd_qtstart (char *packet
)
3083 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3085 trace_debug ("Starting the trace");
3087 /* Pause all threads temporarily while we patch tracepoints. */
3090 /* Get threads out of jump pads. Safe to do here, since this is a
3091 top level command. And, required to do here, since we're
3092 deleting/rewriting jump pads. */
3094 stabilize_threads ();
3096 /* Freeze threads. */
3099 /* Sync the fast tracepoints list in the inferior ftlib. */
3100 if (agent_loaded_p ())
3102 download_tracepoints ();
3103 download_trace_state_variables ();
3106 /* No previous fast tpoint yet. */
3107 prev_ftpoint
= NULL
;
3109 /* No previous static tpoint yet. */
3110 prev_stpoint
= NULL
;
3114 /* Install tracepoints. */
3115 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3117 /* Ensure all the hit counts start at zero. */
3118 tpoint
->hit_count
= 0;
3119 tpoint
->traceframe_usage
= 0;
3121 if (tpoint
->type
== trap_tracepoint
)
3123 /* Tracepoints are installed as memory breakpoints. Just go
3124 ahead and install the trap. The breakpoints module
3125 handles duplicated breakpoints, and the memory read
3126 routine handles un-patching traps from memory reads. */
3127 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3128 tracepoint_handler
);
3130 else if (tpoint
->type
== fast_tracepoint
)
3132 if (maybe_write_ipa_not_loaded (packet
))
3134 trace_debug ("Requested a fast tracepoint, but fast "
3135 "tracepoints aren't supported.");
3139 if (prev_ftpoint
!= NULL
&& prev_ftpoint
->address
== tpoint
->address
)
3140 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3143 if (install_fast_tracepoint (tpoint
, packet
) == 0)
3144 prev_ftpoint
= tpoint
;
3147 else if (tpoint
->type
== static_tracepoint
)
3149 if (maybe_write_ipa_ust_not_loaded (packet
))
3151 trace_debug ("Requested a static tracepoint, but static "
3152 "tracepoints are not supported.");
3156 /* Can only probe a given marker once. */
3157 if (prev_stpoint
!= NULL
&& prev_stpoint
->address
== tpoint
->address
)
3159 tpoint
->handle
= (void *) -1;
3163 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3165 tpoint
->handle
= (void *) -1;
3167 /* So that we can handle multiple static tracepoints
3168 at the same address easily. */
3169 prev_stpoint
= tpoint
;
3174 /* Any failure in the inner loop is sufficient cause to give
3176 if (tpoint
->handle
== NULL
)
3180 /* Any error in tracepoint insertion is unacceptable; better to
3181 address the problem now, than end up with a useless or misleading
3185 clear_installed_tracepoints ();
3186 if (*packet
== '\0')
3192 stopping_tracepoint
= NULL
;
3193 trace_buffer_is_full
= 0;
3194 expr_eval_result
= expr_eval_no_error
;
3195 error_tracepoint
= NULL
;
3196 tracing_start_time
= get_timestamp ();
3198 /* Tracing is now active, hits will now start being logged. */
3201 if (agent_loaded_p ())
3203 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3204 fatal ("Error setting tracing variable in lib");
3206 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3208 fatal ("Error clearing stopping_tracepoint variable in lib");
3210 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3211 fatal ("Error clearing trace_buffer_is_full variable in lib");
3213 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3214 stop_tracing_handler
);
3215 if (stop_tracing_bkpt
== NULL
)
3216 error ("Error setting stop_tracing breakpoint");
3218 flush_trace_buffer_bkpt
3219 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3220 flush_trace_buffer_handler
);
3221 if (flush_trace_buffer_bkpt
== NULL
)
3222 error ("Error setting flush_trace_buffer breakpoint");
3230 /* End a tracing run, filling in a stop reason to report back to GDB,
3231 and removing the tracepoints from the code. */
3238 trace_debug ("Tracing is already off, ignoring");
3242 trace_debug ("Stopping the trace");
3244 /* Pause all threads before removing fast jumps from memory,
3245 breakpoints, and touching IPA state variables (inferior memory).
3246 Some thread may hit the internal tracing breakpoints, or be
3247 collecting this moment, but that's ok, we don't release the
3248 tpoint object's memory or the jump pads here (we only do that
3249 when we're sure we can move all threads out of the jump pads).
3250 We can't now, since we may be getting here due to the inferior
3251 agent calling us. */
3253 /* Since we're removing breakpoints, cancel breakpoint hits,
3254 possibly related to the breakpoints we're about to delete. */
3255 cancel_breakpoints ();
3257 /* Stop logging. Tracepoints can still be hit, but they will not be
3260 if (agent_loaded_p ())
3262 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3263 fatal ("Error clearing tracing variable in lib");
3266 tracing_stop_time
= get_timestamp ();
3267 tracing_stop_reason
= "t???";
3268 tracing_stop_tpnum
= 0;
3269 if (stopping_tracepoint
)
3271 trace_debug ("Stopping the trace because "
3272 "tracepoint %d was hit %ld times",
3273 stopping_tracepoint
->number
,
3274 stopping_tracepoint
->pass_count
);
3275 tracing_stop_reason
= "tpasscount";
3276 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3278 else if (trace_buffer_is_full
)
3280 trace_debug ("Stopping the trace because the trace buffer is full");
3281 tracing_stop_reason
= "tfull";
3283 else if (expr_eval_result
!= expr_eval_no_error
)
3285 trace_debug ("Stopping the trace because of an expression eval error");
3286 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3287 tracing_stop_tpnum
= error_tracepoint
->number
;
3289 #ifndef IN_PROCESS_AGENT
3290 else if (!gdb_connected ())
3292 trace_debug ("Stopping the trace because GDB disconnected");
3293 tracing_stop_reason
= "tdisconnected";
3298 trace_debug ("Stopping the trace because of a tstop command");
3299 tracing_stop_reason
= "tstop";
3302 stopping_tracepoint
= NULL
;
3303 error_tracepoint
= NULL
;
3305 /* Clear out the tracepoints. */
3306 clear_installed_tracepoints ();
3308 if (agent_loaded_p ())
3310 /* Pull in fast tracepoint trace frames from the inferior lib
3311 buffer into our buffer, even if our buffer is already full,
3312 because we want to present the full number of created frames
3313 in addition to what fit in the trace buffer. */
3314 upload_fast_traceframes ();
3317 if (stop_tracing_bkpt
!= NULL
)
3319 delete_breakpoint (stop_tracing_bkpt
);
3320 stop_tracing_bkpt
= NULL
;
3323 if (flush_trace_buffer_bkpt
!= NULL
)
3325 delete_breakpoint (flush_trace_buffer_bkpt
);
3326 flush_trace_buffer_bkpt
= NULL
;
3333 stop_tracing_handler (CORE_ADDR addr
)
3335 trace_debug ("lib hit stop_tracing");
3337 /* Don't actually handle it here. When we stop tracing we remove
3338 breakpoints from the inferior, and that is not allowed in a
3339 breakpoint handler (as the caller is walking the breakpoint
3345 flush_trace_buffer_handler (CORE_ADDR addr
)
3347 trace_debug ("lib hit flush_trace_buffer");
3352 cmd_qtstop (char *packet
)
3359 cmd_qtdisconnected (char *own_buf
)
3362 char *packet
= own_buf
;
3364 packet
+= strlen ("QTDisconnected:");
3366 unpack_varlen_hex (packet
, &setting
);
3370 disconnected_tracing
= setting
;
3374 cmd_qtframe (char *own_buf
)
3376 ULONGEST frame
, pc
, lo
, hi
, num
;
3378 struct traceframe
*tframe
;
3379 char *packet
= own_buf
;
3381 packet
+= strlen ("QTFrame:");
3383 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3385 packet
+= strlen ("pc:");
3386 unpack_varlen_hex (packet
, &pc
);
3387 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3388 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3390 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3392 packet
+= strlen ("range:");
3393 packet
= unpack_varlen_hex (packet
, &lo
);
3395 unpack_varlen_hex (packet
, &hi
);
3396 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3397 paddress (lo
), paddress (hi
));
3398 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3400 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3402 packet
+= strlen ("outside:");
3403 packet
= unpack_varlen_hex (packet
, &lo
);
3405 unpack_varlen_hex (packet
, &hi
);
3406 trace_debug ("Want to find next traceframe "
3407 "outside the range 0x%s to 0x%s",
3408 paddress (lo
), paddress (hi
));
3409 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3411 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3413 packet
+= strlen ("tdp:");
3414 unpack_varlen_hex (packet
, &num
);
3416 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3417 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3421 unpack_varlen_hex (packet
, &frame
);
3422 tfnum
= (int) frame
;
3425 trace_debug ("Want to stop looking at traceframes");
3426 current_traceframe
= -1;
3430 trace_debug ("Want to look at traceframe %d", tfnum
);
3431 tframe
= find_traceframe (tfnum
);
3436 current_traceframe
= tfnum
;
3437 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3440 sprintf (own_buf
, "F-1");
3444 cmd_qtstatus (char *packet
)
3446 char *stop_reason_rsp
= NULL
;
3447 char *buf1
, *buf2
, *buf3
, *str
;
3450 /* Translate the plain text of the notes back into hex for
3453 str
= (tracing_user_name
? tracing_user_name
: "");
3454 slen
= strlen (str
);
3455 buf1
= (char *) alloca (slen
* 2 + 1);
3456 hexify (buf1
, str
, slen
);
3458 str
= (tracing_notes
? tracing_notes
: "");
3459 slen
= strlen (str
);
3460 buf2
= (char *) alloca (slen
* 2 + 1);
3461 hexify (buf2
, str
, slen
);
3463 str
= (tracing_stop_note
? tracing_stop_note
: "");
3464 slen
= strlen (str
);
3465 buf3
= (char *) alloca (slen
* 2 + 1);
3466 hexify (buf3
, str
, slen
);
3468 trace_debug ("Returning trace status as %d, stop reason %s",
3469 tracing
, tracing_stop_reason
);
3471 if (agent_loaded_p ())
3475 upload_fast_traceframes ();
3480 stop_reason_rsp
= (char *) tracing_stop_reason
;
3482 /* The user visible error string in terror needs to be hex encoded.
3483 We leave it as plain string in `tracing_stop_reason' to ease
3485 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3487 const char *result_name
;
3491 result_name
= stop_reason_rsp
+ strlen ("terror:");
3492 hexstr_len
= strlen (result_name
) * 2;
3493 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3494 strcpy (p
, "terror:");
3496 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3499 /* If this was a forced stop, include any stop note that was supplied. */
3500 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3502 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3503 strcpy (stop_reason_rsp
, "tstop:");
3504 strcat (stop_reason_rsp
, buf3
);
3510 "tframes:%x;tcreated:%x;"
3511 "tfree:%x;tsize:%s;"
3514 "starttime:%s;stoptime:%s;"
3515 "username:%s:;notes:%s:",
3517 stop_reason_rsp
, tracing_stop_tpnum
,
3518 traceframe_count
, traceframes_created
,
3519 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3520 circular_trace_buffer
,
3521 disconnected_tracing
,
3522 plongest (tracing_start_time
), plongest (tracing_stop_time
),
3527 cmd_qtp (char *own_buf
)
3530 struct tracepoint
*tpoint
;
3531 char *packet
= own_buf
;
3533 packet
+= strlen ("qTP:");
3535 packet
= unpack_varlen_hex (packet
, &num
);
3536 ++packet
; /* skip a colon */
3537 packet
= unpack_varlen_hex (packet
, &addr
);
3539 /* See if we already have this tracepoint. */
3540 tpoint
= find_tracepoint (num
, addr
);
3544 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3545 (int) num
, paddress (addr
));
3546 write_enn (own_buf
);
3550 sprintf (own_buf
, "V%lx:%lx", tpoint
->hit_count
, tpoint
->traceframe_usage
);
3553 /* State variables to help return all the tracepoint bits. */
3554 static struct tracepoint
*cur_tpoint
;
3555 static int cur_action
;
3556 static int cur_step_action
;
3557 static struct source_string
*cur_source_string
;
3558 static struct trace_state_variable
*cur_tsv
;
3560 /* Compose a response that is an imitation of the syntax by which the
3561 tracepoint was originally downloaded. */
3564 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3568 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
3569 paddress (tpoint
->address
),
3570 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3571 tpoint
->pass_count
);
3572 if (tpoint
->type
== fast_tracepoint
)
3573 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3574 else if (tpoint
->type
== static_tracepoint
)
3575 sprintf (packet
+ strlen (packet
), ":S");
3579 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3580 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3581 tpoint
->cond
->length
, buf
);
3586 /* Compose a response that is an imitation of the syntax by which the
3587 tracepoint action was originally downloaded (with the difference
3588 that due to the way we store the actions, this will output a packet
3589 per action, while GDB could have combined more than one action
3593 response_action (char *packet
, struct tracepoint
*tpoint
,
3594 char *taction
, int step
)
3596 sprintf (packet
, "%c%x:%s:%s",
3597 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3601 /* Compose a response that is an imitation of the syntax by which the
3602 tracepoint source piece was originally downloaded. */
3605 response_source (char *packet
,
3606 struct tracepoint
*tpoint
, struct source_string
*src
)
3611 len
= strlen (src
->str
);
3612 buf
= alloca (len
* 2 + 1);
3613 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3615 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3616 tpoint
->number
, paddress (tpoint
->address
),
3617 src
->type
, 0, len
, buf
);
3620 /* Return the first piece of tracepoint definition, and initialize the
3621 state machine that will iterate through all the tracepoint
3625 cmd_qtfp (char *packet
)
3627 trace_debug ("Returning first tracepoint definition piece");
3629 cur_tpoint
= tracepoints
;
3630 cur_action
= cur_step_action
= -1;
3631 cur_source_string
= NULL
;
3634 response_tracepoint (packet
, cur_tpoint
);
3636 strcpy (packet
, "l");
3639 /* Return additional pieces of tracepoint definition. Each action and
3640 stepping action must go into its own packet, because of packet size
3641 limits, and so we use state variables to deliver one piece at a
3645 cmd_qtsp (char *packet
)
3647 trace_debug ("Returning subsequent tracepoint definition piece");
3651 /* This case would normally never occur, but be prepared for
3653 strcpy (packet
, "l");
3655 else if (cur_action
< cur_tpoint
->numactions
- 1)
3658 response_action (packet
, cur_tpoint
,
3659 cur_tpoint
->actions_str
[cur_action
], 0);
3661 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3664 response_action (packet
, cur_tpoint
,
3665 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3667 else if ((cur_source_string
3668 ? cur_source_string
->next
3669 : cur_tpoint
->source_strings
))
3671 if (cur_source_string
)
3672 cur_source_string
= cur_source_string
->next
;
3674 cur_source_string
= cur_tpoint
->source_strings
;
3675 response_source (packet
, cur_tpoint
, cur_source_string
);
3679 cur_tpoint
= cur_tpoint
->next
;
3680 cur_action
= cur_step_action
= -1;
3681 cur_source_string
= NULL
;
3683 response_tracepoint (packet
, cur_tpoint
);
3685 strcpy (packet
, "l");
3689 /* Compose a response that is an imitation of the syntax by which the
3690 trace state variable was originally downloaded. */
3693 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3695 char *buf
= (char *) "";
3700 namelen
= strlen (tsv
->name
);
3701 buf
= alloca (namelen
* 2 + 1);
3702 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3705 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3706 tsv
->getter
? 1 : 0, buf
);
3709 /* Return the first trace state variable definition, and initialize
3710 the state machine that will iterate through all the tsv bits. */
3713 cmd_qtfv (char *packet
)
3715 trace_debug ("Returning first trace state variable definition");
3717 cur_tsv
= trace_state_variables
;
3720 response_tsv (packet
, cur_tsv
);
3722 strcpy (packet
, "l");
3725 /* Return additional trace state variable definitions. */
3728 cmd_qtsv (char *packet
)
3730 trace_debug ("Returning first trace state variable definition");
3734 /* This case would normally never occur, but be prepared for
3736 strcpy (packet
, "l");
3740 cur_tsv
= cur_tsv
->next
;
3742 response_tsv (packet
, cur_tsv
);
3744 strcpy (packet
, "l");
3747 strcpy (packet
, "l");
3750 /* Return the first static tracepoint marker, and initialize the state
3751 machine that will iterate through all the static tracepoints
3755 cmd_qtfstm (char *packet
)
3757 if (!maybe_write_ipa_ust_not_loaded (packet
))
3758 run_inferior_command (packet
);
3761 /* Return additional static tracepoints markers. */
3764 cmd_qtsstm (char *packet
)
3766 if (!maybe_write_ipa_ust_not_loaded (packet
))
3767 run_inferior_command (packet
);
3770 /* Return the definition of the static tracepoint at a given address.
3771 Result packet is the same as qTsST's. */
3774 cmd_qtstmat (char *packet
)
3776 if (!maybe_write_ipa_ust_not_loaded (packet
))
3777 run_inferior_command (packet
);
3780 /* Return the minimum instruction size needed for fast tracepoints as a
3781 hexadecimal number. */
3784 cmd_qtminftpilen (char *packet
)
3786 if (current_inferior
== NULL
)
3788 /* Indicate that the minimum length is currently unknown. */
3789 strcpy (packet
, "0");
3793 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3796 /* Respond to qTBuffer packet with a block of raw data from the trace
3797 buffer. GDB may ask for a lot, but we are allowed to reply with
3798 only as much as will fit within packet limits or whatever. */
3801 cmd_qtbuffer (char *own_buf
)
3803 ULONGEST offset
, num
, tot
;
3805 char *packet
= own_buf
;
3807 packet
+= strlen ("qTBuffer:");
3809 packet
= unpack_varlen_hex (packet
, &offset
);
3810 ++packet
; /* skip a comma */
3811 unpack_varlen_hex (packet
, &num
);
3813 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3814 (int) num
, pulongest (offset
));
3816 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3818 /* If we're right at the end, reply specially that we're done. */
3821 strcpy (own_buf
, "l");
3825 /* Object to any other out-of-bounds request. */
3828 write_enn (own_buf
);
3832 /* Compute the pointer corresponding to the given offset, accounting
3834 tbp
= trace_buffer_start
+ offset
;
3835 if (tbp
>= trace_buffer_wrap
)
3836 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3838 /* Trim to the remaining bytes if we're close to the end. */
3839 if (num
> tot
- offset
)
3842 /* Trim to available packet size. */
3843 if (num
>= (PBUFSIZ
- 16) / 2 )
3844 num
= (PBUFSIZ
- 16) / 2;
3846 convert_int_to_ascii (tbp
, own_buf
, num
);
3847 own_buf
[num
] = '\0';
3851 cmd_bigqtbuffer_circular (char *own_buf
)
3854 char *packet
= own_buf
;
3856 packet
+= strlen ("QTBuffer:circular:");
3858 unpack_varlen_hex (packet
, &val
);
3859 circular_trace_buffer
= val
;
3860 trace_debug ("Trace buffer is now %s",
3861 circular_trace_buffer
? "circular" : "linear");
3866 cmd_qtnotes (char *own_buf
)
3869 char *saved
, *user
, *notes
, *stopnote
;
3870 char *packet
= own_buf
;
3872 packet
+= strlen ("QTNotes:");
3876 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
3878 packet
+= strlen ("user:");
3880 packet
= strchr (packet
, ';');
3881 nbytes
= (packet
- saved
) / 2;
3882 user
= xmalloc (nbytes
+ 1);
3883 nbytes
= unhexify (user
, saved
, nbytes
);
3884 user
[nbytes
] = '\0';
3885 ++packet
; /* skip the semicolon */
3886 trace_debug ("User is '%s'", user
);
3887 tracing_user_name
= user
;
3889 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
3891 packet
+= strlen ("notes:");
3893 packet
= strchr (packet
, ';');
3894 nbytes
= (packet
- saved
) / 2;
3895 notes
= xmalloc (nbytes
+ 1);
3896 nbytes
= unhexify (notes
, saved
, nbytes
);
3897 notes
[nbytes
] = '\0';
3898 ++packet
; /* skip the semicolon */
3899 trace_debug ("Notes is '%s'", notes
);
3900 tracing_notes
= notes
;
3902 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
3904 packet
+= strlen ("tstop:");
3906 packet
= strchr (packet
, ';');
3907 nbytes
= (packet
- saved
) / 2;
3908 stopnote
= xmalloc (nbytes
+ 1);
3909 nbytes
= unhexify (stopnote
, saved
, nbytes
);
3910 stopnote
[nbytes
] = '\0';
3911 ++packet
; /* skip the semicolon */
3912 trace_debug ("tstop note is '%s'", stopnote
);
3913 tracing_stop_note
= stopnote
;
3923 handle_tracepoint_general_set (char *packet
)
3925 if (strcmp ("QTinit", packet
) == 0)
3927 cmd_qtinit (packet
);
3930 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3935 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3937 cmd_qtdpsrc (packet
);
3940 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
3942 cmd_qtenable_disable (packet
, 1);
3945 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
3947 cmd_qtenable_disable (packet
, 0);
3950 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3955 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3960 else if (strcmp ("QTStart", packet
) == 0)
3962 cmd_qtstart (packet
);
3965 else if (strcmp ("QTStop", packet
) == 0)
3967 cmd_qtstop (packet
);
3970 else if (strncmp ("QTDisconnected:", packet
,
3971 strlen ("QTDisconnected:")) == 0)
3973 cmd_qtdisconnected (packet
);
3976 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3978 cmd_qtframe (packet
);
3981 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
3983 cmd_bigqtbuffer_circular (packet
);
3986 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
3988 cmd_qtnotes (packet
);
3996 handle_tracepoint_query (char *packet
)
3998 if (strcmp ("qTStatus", packet
) == 0)
4000 cmd_qtstatus (packet
);
4003 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4008 else if (strcmp ("qTfP", packet
) == 0)
4013 else if (strcmp ("qTsP", packet
) == 0)
4018 else if (strcmp ("qTfV", packet
) == 0)
4023 else if (strcmp ("qTsV", packet
) == 0)
4028 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4033 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4035 cmd_qtbuffer (packet
);
4038 else if (strcmp ("qTfSTM", packet
) == 0)
4040 cmd_qtfstm (packet
);
4043 else if (strcmp ("qTsSTM", packet
) == 0)
4045 cmd_qtsstm (packet
);
4048 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4050 cmd_qtstmat (packet
);
4053 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4055 cmd_qtminftpilen (packet
);
4063 #ifndef IN_PROCESS_AGENT
4065 /* Call this when thread TINFO has hit the tracepoint defined by
4066 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4067 action. This adds a while-stepping collecting state item to the
4068 threads' collecting state list, so that we can keep track of
4069 multiple simultaneous while-stepping actions being collected by the
4070 same thread. This can happen in cases like:
4072 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4074 ff0003 INSN3 <-- TP2, collect $regs
4075 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4078 Notice that when instruction INSN5 is reached, the while-stepping
4079 actions of both TP1 and TP3 are still being collected, and that TP2
4080 had been collected meanwhile. The whole range of ff0001-ff0005
4081 should be single-stepped, due to at least TP1's while-stepping
4082 action covering the whole range. */
4085 add_while_stepping_state (struct thread_info
*tinfo
,
4086 int tp_number
, CORE_ADDR tp_address
)
4088 struct wstep_state
*wstep
;
4090 wstep
= xmalloc (sizeof (*wstep
));
4091 wstep
->next
= tinfo
->while_stepping
;
4093 wstep
->tp_number
= tp_number
;
4094 wstep
->tp_address
= tp_address
;
4095 wstep
->current_step
= 0;
4097 tinfo
->while_stepping
= wstep
;
4100 /* Release the while-stepping collecting state WSTEP. */
4103 release_while_stepping_state (struct wstep_state
*wstep
)
4108 /* Release all while-stepping collecting states currently associated
4109 with thread TINFO. */
4112 release_while_stepping_state_list (struct thread_info
*tinfo
)
4114 struct wstep_state
*head
;
4116 while (tinfo
->while_stepping
)
4118 head
= tinfo
->while_stepping
;
4119 tinfo
->while_stepping
= head
->next
;
4120 release_while_stepping_state (head
);
4124 /* If TINFO was handling a 'while-stepping' action, the step has
4125 finished, so collect any step data needed, and check if any more
4126 steps are required. Return true if the thread was indeed
4127 collecting tracepoint data, false otherwise. */
4130 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4132 struct tracepoint
*tpoint
;
4133 struct wstep_state
*wstep
;
4134 struct wstep_state
**wstep_link
;
4135 struct trap_tracepoint_ctx ctx
;
4137 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4139 if (agent_loaded_p ())
4140 upload_fast_traceframes ();
4142 /* Check if we were indeed collecting data for one of more
4143 tracepoints with a 'while-stepping' count. */
4144 if (tinfo
->while_stepping
== NULL
)
4149 /* We're not even tracing anymore. Stop this thread from
4151 release_while_stepping_state_list (tinfo
);
4153 /* The thread had stopped due to a single-step request indeed
4154 explained by a tracepoint. */
4158 wstep
= tinfo
->while_stepping
;
4159 wstep_link
= &tinfo
->while_stepping
;
4161 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4162 target_pid_to_str (tinfo
->entry
.id
),
4163 wstep
->tp_number
, paddress (wstep
->tp_address
));
4165 ctx
.base
.type
= trap_tracepoint
;
4166 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4168 while (wstep
!= NULL
)
4170 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4173 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4174 wstep
->tp_number
, paddress (wstep
->tp_address
),
4175 target_pid_to_str (tinfo
->entry
.id
));
4178 *wstep_link
= wstep
->next
;
4179 release_while_stepping_state (wstep
);
4180 wstep
= *wstep_link
;
4184 /* We've just finished one step. */
4185 ++wstep
->current_step
;
4188 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4189 stop_pc
, tpoint
, wstep
->current_step
);
4191 if (wstep
->current_step
>= tpoint
->step_count
)
4193 /* The requested numbers of steps have occurred. */
4194 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4195 target_pid_to_str (tinfo
->entry
.id
),
4196 wstep
->tp_number
, paddress (wstep
->tp_address
));
4198 /* Unlink the wstep. */
4199 *wstep_link
= wstep
->next
;
4200 release_while_stepping_state (wstep
);
4201 wstep
= *wstep_link
;
4203 /* Only check the hit count now, which ensure that we do all
4204 our stepping before stopping the run. */
4205 if (tpoint
->pass_count
> 0
4206 && tpoint
->hit_count
>= tpoint
->pass_count
4207 && stopping_tracepoint
== NULL
)
4208 stopping_tracepoint
= tpoint
;
4212 /* Keep single-stepping until the requested numbers of steps
4214 wstep_link
= &wstep
->next
;
4215 wstep
= *wstep_link
;
4218 if (stopping_tracepoint
4219 || trace_buffer_is_full
4220 || expr_eval_result
!= expr_eval_no_error
)
4230 /* Handle any internal tracing control breakpoint hits. That means,
4231 pull traceframes from the IPA to our buffer, and syncing both
4232 tracing agents when the IPA's tracing stops for some reason. */
4235 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4237 /* Pull in fast tracepoint trace frames from the inferior in-process
4238 agent's buffer into our buffer. */
4240 if (!agent_loaded_p ())
4243 upload_fast_traceframes ();
4245 /* Check if the in-process agent had decided we should stop
4247 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4249 int ipa_trace_buffer_is_full
;
4250 CORE_ADDR ipa_stopping_tracepoint
;
4251 int ipa_expr_eval_result
;
4252 CORE_ADDR ipa_error_tracepoint
;
4254 trace_debug ("lib stopped at stop_tracing");
4256 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4257 &ipa_trace_buffer_is_full
);
4259 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4260 &ipa_stopping_tracepoint
);
4261 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4263 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4264 &ipa_error_tracepoint
);
4265 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4267 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4268 &ipa_expr_eval_result
);
4269 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4271 trace_debug ("lib: trace_buffer_is_full: %d, "
4272 "stopping_tracepoint: %s, "
4273 "ipa_expr_eval_result: %d, "
4274 "error_tracepoint: %s, ",
4275 ipa_trace_buffer_is_full
,
4276 paddress (ipa_stopping_tracepoint
),
4277 ipa_expr_eval_result
,
4278 paddress (ipa_error_tracepoint
));
4282 if (ipa_trace_buffer_is_full
)
4283 trace_debug ("lib stopped due to full buffer.");
4284 if (ipa_stopping_tracepoint
)
4285 trace_debug ("lib stopped due to tpoint");
4286 if (ipa_stopping_tracepoint
)
4287 trace_debug ("lib stopped due to error");
4290 if (ipa_stopping_tracepoint
!= 0)
4293 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4295 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4297 expr_eval_result
= ipa_expr_eval_result
;
4299 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4304 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4306 trace_debug ("lib stopped at flush_trace_buffer");
4313 /* Return true if TINFO just hit a tracepoint. Collect data if
4317 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4319 struct tracepoint
*tpoint
;
4321 struct trap_tracepoint_ctx ctx
;
4323 /* Not tracing, don't handle. */
4327 ctx
.base
.type
= trap_tracepoint
;
4328 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4330 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4332 /* Note that we collect fast tracepoints here as well. We'll
4333 step over the fast tracepoint jump later, which avoids the
4334 double collect. However, we don't collect for static
4335 tracepoints here, because UST markers are compiled in program,
4336 and probes will be executed in program. So static tracepoints
4337 are collected there. */
4338 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4339 && tpoint
->type
!= static_tracepoint
)
4341 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4342 target_pid_to_str (tinfo
->entry
.id
),
4343 tpoint
->number
, paddress (tpoint
->address
));
4345 /* Test the condition if present, and collect if true. */
4347 || (condition_true_at_tracepoint
4348 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4349 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4352 if (stopping_tracepoint
4353 || trace_buffer_is_full
4354 || expr_eval_result
!= expr_eval_no_error
)
4358 /* If the tracepoint had a 'while-stepping' action, then set
4359 the thread to collect this tracepoint on the following
4361 else if (tpoint
->step_count
> 0)
4363 add_while_stepping_state (tinfo
,
4364 tpoint
->number
, tpoint
->address
);
4376 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4377 struct ust_marker_data
;
4378 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4379 struct traceframe
*tframe
);
4382 /* Create a trace frame for the hit of the given tracepoint in the
4386 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4387 struct tracepoint
*tpoint
)
4389 struct traceframe
*tframe
;
4392 /* Only count it as a hit when we actually collect data. */
4393 tpoint
->hit_count
++;
4395 /* If we've exceeded a defined pass count, record the event for
4396 later, and finish the collection for this hit. This test is only
4397 for nonstepping tracepoints, stepping tracepoints test at the end
4398 of their while-stepping loop. */
4399 if (tpoint
->pass_count
> 0
4400 && tpoint
->hit_count
>= tpoint
->pass_count
4401 && tpoint
->step_count
== 0
4402 && stopping_tracepoint
== NULL
)
4403 stopping_tracepoint
= tpoint
;
4405 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
4406 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4408 tframe
= add_traceframe (tpoint
);
4412 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4414 #ifndef IN_PROCESS_AGENT
4415 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4416 tpoint
->number
, paddress (tpoint
->address
),
4417 tpoint
->actions_str
[acti
]);
4420 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4421 tpoint
->actions
[acti
]);
4424 finish_traceframe (tframe
);
4427 if (tframe
== NULL
&& tracing
)
4428 trace_buffer_is_full
= 1;
4431 #ifndef IN_PROCESS_AGENT
4434 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4436 struct tracepoint
*tpoint
, int current_step
)
4438 struct traceframe
*tframe
;
4441 trace_debug ("Making new step traceframe for "
4442 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4443 tpoint
->number
, paddress (tpoint
->address
),
4444 current_step
, tpoint
->step_count
,
4447 tframe
= add_traceframe (tpoint
);
4451 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4453 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4454 tpoint
->number
, paddress (tpoint
->address
),
4455 tpoint
->step_actions_str
[acti
]);
4457 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4458 tpoint
->step_actions
[acti
]);
4461 finish_traceframe (tframe
);
4464 if (tframe
== NULL
&& tracing
)
4465 trace_buffer_is_full
= 1;
4470 static struct regcache
*
4471 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4473 struct regcache
*regcache
= NULL
;
4475 #ifdef IN_PROCESS_AGENT
4476 if (ctx
->type
== fast_tracepoint
)
4478 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4479 if (!fctx
->regcache_initted
)
4481 fctx
->regcache_initted
= 1;
4482 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4483 supply_regblock (&fctx
->regcache
, NULL
);
4484 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4486 regcache
= &fctx
->regcache
;
4489 if (ctx
->type
== static_tracepoint
)
4491 struct static_tracepoint_ctx
*sctx
4492 = (struct static_tracepoint_ctx
*) ctx
;
4494 if (!sctx
->regcache_initted
)
4496 sctx
->regcache_initted
= 1;
4497 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4498 supply_regblock (&sctx
->regcache
, NULL
);
4499 /* Pass down the tracepoint address, because REGS doesn't
4500 include the PC, but we know what it must have been. */
4501 supply_static_tracepoint_registers (&sctx
->regcache
,
4502 (const unsigned char *)
4504 sctx
->tpoint
->address
);
4506 regcache
= &sctx
->regcache
;
4510 if (ctx
->type
== trap_tracepoint
)
4512 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4513 regcache
= tctx
->regcache
;
4517 gdb_assert (regcache
!= NULL
);
4523 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4525 struct tracepoint
*tpoint
,
4526 struct traceframe
*tframe
,
4527 struct tracepoint_action
*taction
)
4529 enum eval_result_type err
;
4531 switch (taction
->type
)
4535 struct collect_memory_action
*maction
;
4537 maction
= (struct collect_memory_action
*) taction
;
4539 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4540 pulongest (maction
->len
),
4541 paddress (maction
->addr
), maction
->basereg
);
4542 /* (should use basereg) */
4543 agent_mem_read (tframe
, NULL
,
4544 (CORE_ADDR
) maction
->addr
, maction
->len
);
4549 unsigned char *regspace
;
4550 struct regcache tregcache
;
4551 struct regcache
*context_regcache
;
4554 trace_debug ("Want to collect registers");
4556 /* Collect all registers for now. */
4557 regspace
= add_traceframe_block (tframe
,
4558 1 + register_cache_size ());
4559 if (regspace
== NULL
)
4561 trace_debug ("Trace buffer block allocation failed, skipping");
4564 /* Identify a register block. */
4567 context_regcache
= get_context_regcache (ctx
);
4569 /* Wrap the regblock in a register cache (in the stack, we
4570 don't want to malloc here). */
4571 init_register_cache (&tregcache
, regspace
+ 1);
4573 /* Copy the register data to the regblock. */
4574 regcache_cpy (&tregcache
, context_regcache
);
4576 #ifndef IN_PROCESS_AGENT
4577 /* On some platforms, trap-based tracepoints will have the PC
4578 pointing to the next instruction after the trap, but we
4579 don't want the user or GDB trying to guess whether the
4580 saved PC needs adjusting; so always record the adjusted
4581 stop_pc. Note that we can't use tpoint->address instead,
4582 since it will be wrong for while-stepping actions. This
4583 adjustment is a nop for fast tracepoints collected from the
4584 in-process lib (but not if GDBserver is collecting one
4585 preemptively), since the PC had already been adjusted to
4586 contain the tracepoint's address by the jump pad. */
4587 trace_debug ("Storing stop pc (0x%s) in regblock",
4588 paddress (stop_pc
));
4590 /* This changes the regblock, not the thread's
4592 regcache_write_pc (&tregcache
, stop_pc
);
4598 struct eval_expr_action
*eaction
;
4600 eaction
= (struct eval_expr_action
*) taction
;
4602 trace_debug ("Want to evaluate expression");
4604 err
= eval_tracepoint_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4606 if (err
!= expr_eval_no_error
)
4608 record_tracepoint_error (tpoint
, "action expression", err
);
4615 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4616 trace_debug ("Want to collect static trace data");
4617 collect_ust_data_at_tracepoint (ctx
, tframe
);
4619 trace_debug ("warning: collecting static trace data, "
4620 "but static tracepoints are not supported");
4625 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4631 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4632 struct tracepoint
*tpoint
)
4635 enum eval_result_type err
;
4637 /* Presently, gdbserver doesn't run compiled conditions, only the
4638 IPA does. If the program stops at a fast tracepoint's address
4639 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4640 gdbserver preemptively collect the fast tracepoint. Later, on
4641 resume, gdbserver steps over the fast tracepoint like it steps
4642 over breakpoints, so that the IPA doesn't see that fast
4643 tracepoint. This avoids double collects of fast tracepoints in
4644 that stopping scenario. Having gdbserver itself handle the fast
4645 tracepoint gives the user a consistent view of when fast or trap
4646 tracepoints are collected, compared to an alternative where only
4647 trap tracepoints are collected on stop, and fast tracepoints on
4648 resume. When a fast tracepoint is being processed by gdbserver,
4649 it is always the non-compiled condition expression that is
4651 #ifdef IN_PROCESS_AGENT
4652 if (tpoint
->compiled_cond
)
4653 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4656 err
= eval_tracepoint_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4658 if (err
!= expr_eval_no_error
)
4660 record_tracepoint_error (tpoint
, "condition", err
);
4661 /* The error case must return false. */
4665 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4666 tpoint
->number
, paddress (tpoint
->address
),
4668 return (value
? 1 : 0);
4671 /* Evaluates a tracepoint agent expression with context CTX,
4672 traceframe TFRAME, agent expression AEXPR and store the
4675 static enum eval_result_type
4676 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4677 struct traceframe
*tframe
,
4678 struct agent_expr
*aexpr
,
4681 struct regcache
*regcache
;
4682 regcache
= get_context_regcache (ctx
);
4684 return gdb_eval_agent_expr (regcache
, tframe
, aexpr
, rslt
);
4687 /* Do memory copies for bytecodes. */
4688 /* Do the recording of memory blocks for actions and bytecodes. */
4691 agent_mem_read (struct traceframe
*tframe
,
4692 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4694 unsigned char *mspace
;
4695 ULONGEST remaining
= len
;
4696 unsigned short blocklen
;
4698 /* If a 'to' buffer is specified, use it. */
4701 read_inferior_memory (from
, to
, len
);
4705 /* Otherwise, create a new memory block in the trace buffer. */
4706 while (remaining
> 0)
4710 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4711 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4712 mspace
= add_traceframe_block (tframe
, sp
);
4715 /* Identify block as a memory block. */
4718 /* Record address and size. */
4719 memcpy (mspace
, &from
, sizeof (from
));
4720 mspace
+= sizeof (from
);
4721 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4722 mspace
+= sizeof (blocklen
);
4723 /* Record the memory block proper. */
4724 read_inferior_memory (from
, mspace
, blocklen
);
4725 trace_debug ("%d bytes recorded", blocklen
);
4726 remaining
-= blocklen
;
4733 agent_mem_read_string (struct traceframe
*tframe
,
4734 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4736 unsigned char *buf
, *mspace
;
4737 ULONGEST remaining
= len
;
4738 unsigned short blocklen
, i
;
4740 /* To save a bit of space, block lengths are 16-bit, so break large
4741 requests into multiple blocks. Bordering on overkill for strings,
4742 but it could happen that someone specifies a large max length. */
4743 while (remaining
> 0)
4747 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4748 /* We want working space to accumulate nonzero bytes, since
4749 traceframes must have a predecided size (otherwise it gets
4750 harder to wrap correctly for the circular case, etc). */
4751 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4752 for (i
= 0; i
< blocklen
; ++i
)
4754 /* Read the string one byte at a time, in case the string is
4755 at the end of a valid memory area - we don't want a
4756 correctly-terminated string to engender segvio
4758 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4763 /* Make sure outer loop stops now too. */
4764 remaining
= blocklen
;
4768 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4769 mspace
= add_traceframe_block (tframe
, sp
);
4775 /* Identify block as a memory block. */
4778 /* Record address and size. */
4779 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4780 mspace
+= sizeof (from
);
4781 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4782 mspace
+= sizeof (blocklen
);
4783 /* Copy the string contents. */
4784 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
4785 remaining
-= blocklen
;
4792 /* Record the value of a trace state variable. */
4795 agent_tsv_read (struct traceframe
*tframe
, int n
)
4797 unsigned char *vspace
;
4800 vspace
= add_traceframe_block (tframe
,
4801 1 + sizeof (n
) + sizeof (LONGEST
));
4804 /* Identify block as a variable. */
4806 /* Record variable's number and value. */
4807 memcpy (vspace
+ 1, &n
, sizeof (n
));
4808 val
= get_trace_state_variable_value (n
);
4809 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4810 trace_debug ("Variable %d recorded", n
);
4814 #ifndef IN_PROCESS_AGENT
4816 /* Callback for traceframe_walk_blocks, used to find a given block
4817 type in a traceframe. */
4820 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
4822 char *wantedp
= data
;
4824 if (*wantedp
== blocktype
)
4830 /* Walk over all traceframe blocks of the traceframe buffer starting
4831 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4832 block found, passing in DATA unmodified. If CALLBACK returns true,
4833 this returns a pointer to where the block is found. Returns NULL
4834 if no callback call returned true, indicating that all blocks have
4837 static unsigned char *
4838 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
4840 int (*callback
) (char blocktype
,
4841 unsigned char *dataptr
,
4845 unsigned char *dataptr
;
4849 trace_debug ("traceframe %d has no data", tfnum
);
4853 /* Iterate through a traceframe's blocks, looking for a block of the
4855 for (dataptr
= database
;
4856 dataptr
< database
+ datasize
;
4860 unsigned short mlen
;
4862 if (dataptr
== trace_buffer_wrap
)
4864 /* Adjust to reflect wrapping part of the frame around to
4866 datasize
= dataptr
- database
;
4867 dataptr
= database
= trace_buffer_lo
;
4870 blocktype
= *dataptr
++;
4872 if ((*callback
) (blocktype
, dataptr
, data
))
4878 /* Skip over the registers block. */
4879 dataptr
+= register_cache_size ();
4882 /* Skip over the memory block. */
4883 dataptr
+= sizeof (CORE_ADDR
);
4884 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4885 dataptr
+= (sizeof (mlen
) + mlen
);
4888 /* Skip over the TSV block. */
4889 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4892 /* Skip over the static trace data block. */
4893 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4894 dataptr
+= (sizeof (mlen
) + mlen
);
4897 trace_debug ("traceframe %d has unknown block type 0x%x",
4906 /* Look for the block of type TYPE_WANTED in the trameframe starting
4907 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4910 static unsigned char *
4911 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4912 int tfnum
, char type_wanted
)
4914 return traceframe_walk_blocks (database
, datasize
, tfnum
,
4915 match_blocktype
, &type_wanted
);
4918 static unsigned char *
4919 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4921 unsigned char *regblock
;
4923 regblock
= traceframe_find_block_type (tframe
->data
,
4927 if (regblock
== NULL
)
4928 trace_debug ("traceframe %d has no register data", tfnum
);
4933 /* Get registers from a traceframe. */
4936 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4938 unsigned char *dataptr
;
4939 struct tracepoint
*tpoint
;
4940 struct traceframe
*tframe
;
4942 tframe
= find_traceframe (tfnum
);
4946 trace_debug ("traceframe %d not found", tfnum
);
4950 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4951 if (dataptr
== NULL
)
4953 /* Mark registers unavailable. */
4954 supply_regblock (regcache
, NULL
);
4956 /* We can generally guess at a PC, although this will be
4957 misleading for while-stepping frames and multi-location
4959 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4961 regcache_write_pc (regcache
, tpoint
->address
);
4964 supply_regblock (regcache
, dataptr
);
4970 traceframe_get_pc (struct traceframe
*tframe
)
4972 struct regcache regcache
;
4973 unsigned char *dataptr
;
4975 dataptr
= traceframe_find_regblock (tframe
, -1);
4976 if (dataptr
== NULL
)
4979 init_register_cache (®cache
, dataptr
);
4980 return regcache_read_pc (®cache
);
4983 /* Read a requested block of memory from a trace frame. */
4986 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
4987 unsigned char *buf
, ULONGEST length
,
4990 struct traceframe
*tframe
;
4991 unsigned char *database
, *dataptr
;
4992 unsigned int datasize
;
4994 unsigned short mlen
;
4996 trace_debug ("traceframe_read_mem");
4998 tframe
= find_traceframe (tfnum
);
5002 trace_debug ("traceframe %d not found", tfnum
);
5006 datasize
= tframe
->data_size
;
5007 database
= dataptr
= &tframe
->data
[0];
5009 /* Iterate through a traceframe's blocks, looking for memory. */
5010 while ((dataptr
= traceframe_find_block_type (dataptr
,
5012 - (dataptr
- database
),
5013 tfnum
, 'M')) != NULL
)
5015 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5016 dataptr
+= sizeof (maddr
);
5017 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5018 dataptr
+= sizeof (mlen
);
5019 trace_debug ("traceframe %d has %d bytes at %s",
5020 tfnum
, mlen
, paddress (maddr
));
5022 /* If the block includes the first part of the desired range,
5023 return as much it has; GDB will re-request the remainder,
5024 which might be in a different block of this trace frame. */
5025 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5027 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5031 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5036 /* Skip over this block. */
5040 trace_debug ("traceframe %d has no memory data for the desired region",
5048 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5051 struct traceframe
*tframe
;
5052 unsigned char *database
, *dataptr
;
5053 unsigned int datasize
;
5056 trace_debug ("traceframe_read_tsv");
5058 tfnum
= current_traceframe
;
5062 trace_debug ("no current traceframe");
5066 tframe
= find_traceframe (tfnum
);
5070 trace_debug ("traceframe %d not found", tfnum
);
5074 datasize
= tframe
->data_size
;
5075 database
= dataptr
= &tframe
->data
[0];
5077 /* Iterate through a traceframe's blocks, looking for the tsv. */
5078 while ((dataptr
= traceframe_find_block_type (dataptr
,
5080 - (dataptr
- database
),
5081 tfnum
, 'V')) != NULL
)
5083 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5084 dataptr
+= sizeof (vnum
);
5086 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5088 /* Check that this is the variable we want. */
5091 memcpy (val
, dataptr
, sizeof (*val
));
5095 /* Skip over this block. */
5096 dataptr
+= sizeof (LONGEST
);
5099 trace_debug ("traceframe %d has no data for variable %d",
5104 /* Read a requested block of static tracepoint data from a trace
5108 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5109 unsigned char *buf
, ULONGEST length
,
5112 struct traceframe
*tframe
;
5113 unsigned char *database
, *dataptr
;
5114 unsigned int datasize
;
5115 unsigned short mlen
;
5117 trace_debug ("traceframe_read_sdata");
5119 tframe
= find_traceframe (tfnum
);
5123 trace_debug ("traceframe %d not found", tfnum
);
5127 datasize
= tframe
->data_size
;
5128 database
= &tframe
->data
[0];
5130 /* Iterate through a traceframe's blocks, looking for static
5132 dataptr
= traceframe_find_block_type (database
, datasize
,
5134 if (dataptr
!= NULL
)
5136 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5137 dataptr
+= sizeof (mlen
);
5140 if (offset
+ length
> mlen
)
5141 length
= mlen
- offset
;
5143 memcpy (buf
, dataptr
, length
);
5151 trace_debug ("traceframe %d has no static trace data", tfnum
);
5157 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5158 object. DATA is pointer to a struct buffer holding the
5159 traceframe-info object being built. */
5162 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5164 struct buffer
*buffer
= data
;
5170 unsigned short mlen
;
5173 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5174 dataptr
+= sizeof (maddr
);
5175 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5176 dataptr
+= sizeof (mlen
);
5177 buffer_xml_printf (buffer
,
5178 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5179 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5189 warning ("Unhandled trace block type (%d) '%c ' "
5190 "while building trace frame info.",
5191 blocktype
, blocktype
);
5198 /* Build a traceframe-info object for traceframe number TFNUM into
5202 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5204 struct traceframe
*tframe
;
5206 trace_debug ("traceframe_read_info");
5208 tframe
= find_traceframe (tfnum
);
5212 trace_debug ("traceframe %d not found", tfnum
);
5216 buffer_grow_str (buffer
, "<traceframe-info>\n");
5217 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5218 tfnum
, build_traceframe_info_xml
, buffer
);
5219 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5223 /* Return the first fast tracepoint whose jump pad contains PC. */
5225 static struct tracepoint
*
5226 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5228 struct tracepoint
*tpoint
;
5230 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5231 if (tpoint
->type
== fast_tracepoint
)
5232 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5238 /* Return the first fast tracepoint whose trampoline contains PC. */
5240 static struct tracepoint
*
5241 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5243 struct tracepoint
*tpoint
;
5245 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5247 if (tpoint
->type
== fast_tracepoint
5248 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5255 /* Return GDBserver's tracepoint that matches the IP Agent's
5256 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5259 static struct tracepoint
*
5260 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5262 struct tracepoint
*tpoint
;
5264 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5265 if (tpoint
->type
== fast_tracepoint
)
5266 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5274 /* The type of the object that is used to synchronize fast tracepoint
5277 typedef struct collecting_t
5279 /* The fast tracepoint number currently collecting. */
5282 /* A number that GDBserver can use to identify the thread that is
5283 presently holding the collect lock. This need not (and usually
5284 is not) the thread id, as getting the current thread ID usually
5285 requires a system call, which we want to avoid like the plague.
5286 Usually this is thread's TCB, found in the TLS (pseudo-)
5287 register, which is readable with a single insn on several
5289 uintptr_t thread_area
;
5292 #ifndef IN_PROCESS_AGENT
5295 force_unlock_trace_buffer (void)
5297 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5300 /* Check if the thread identified by THREAD_AREA which is stopped at
5301 STOP_PC, is presently locking the fast tracepoint collection, and
5302 if so, gather some status of said collection. Returns 0 if the
5303 thread isn't collecting or in the jump pad at all. 1, if in the
5304 jump pad (or within gdb_collect) and hasn't executed the adjusted
5305 original insn yet (can set a breakpoint there and run to it). 2,
5306 if presently executing the adjusted original insn --- in which
5307 case, if we want to move the thread out of the jump pad, we need to
5308 single-step it until this function returns 0. */
5311 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5313 struct fast_tpoint_collect_status
*status
)
5315 CORE_ADDR ipa_collecting
;
5316 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5317 CORE_ADDR ipa_gdb_trampoline_buffer
;
5318 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5319 struct tracepoint
*tpoint
;
5320 int needs_breakpoint
;
5322 /* The thread THREAD_AREA is either:
5324 0. not collecting at all, not within the jump pad, or within
5325 gdb_collect or one of its callees.
5327 1. in the jump pad and haven't reached gdb_collect
5329 2. within gdb_collect (out of the jump pad) (collect is set)
5331 3. we're in the jump pad, after gdb_collect having returned,
5332 possibly executing the adjusted insns.
5334 For cases 1 and 3, `collecting' may or not be set. The jump pad
5335 doesn't have any complicated jump logic, so we can tell if the
5336 thread is executing the adjust original insn or not by just
5337 matching STOP_PC with known jump pad addresses. If we it isn't
5338 yet executing the original insn, set a breakpoint there, and let
5339 the thread run to it, so to quickly step over a possible (many
5340 insns) gdb_collect call. Otherwise, or when the breakpoint is
5341 hit, only a few (small number of) insns are left to be executed
5342 in the jump pad. Single-step the thread until it leaves the
5347 needs_breakpoint
= 0;
5348 trace_debug ("fast_tracepoint_collecting");
5350 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5351 &ipa_gdb_jump_pad_buffer
))
5352 fatal ("error extracting `gdb_jump_pad_buffer'");
5353 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5354 &ipa_gdb_jump_pad_buffer_end
))
5355 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5357 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5358 &ipa_gdb_trampoline_buffer
))
5359 fatal ("error extracting `gdb_trampoline_buffer'");
5360 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5361 &ipa_gdb_trampoline_buffer_end
))
5362 fatal ("error extracting `gdb_trampoline_buffer_end'");
5364 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5365 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5367 /* We can tell which tracepoint(s) the thread is collecting by
5368 matching the jump pad address back to the tracepoint. */
5369 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5372 warning ("in jump pad, but no matching tpoint?");
5377 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5379 tpoint
->number
, paddress (tpoint
->address
),
5380 paddress (tpoint
->jump_pad
),
5381 paddress (tpoint
->jump_pad_end
),
5382 paddress (tpoint
->adjusted_insn_addr
),
5383 paddress (tpoint
->adjusted_insn_addr_end
));
5386 /* Definitely in the jump pad. May or may not need
5387 fast-exit-jump-pad breakpoint. */
5388 if (tpoint
->jump_pad
<= stop_pc
5389 && stop_pc
< tpoint
->adjusted_insn_addr
)
5390 needs_breakpoint
= 1;
5392 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5393 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5395 /* We can tell which tracepoint(s) the thread is collecting by
5396 matching the trampoline address back to the tracepoint. */
5397 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5400 warning ("in trampoline, but no matching tpoint?");
5405 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5406 tpoint
->number
, paddress (tpoint
->address
),
5407 paddress (tpoint
->trampoline
),
5408 paddress (tpoint
->trampoline_end
));
5411 /* Have not reached jump pad yet, but treat the trampoline as a
5412 part of the jump pad that is before the adjusted original
5414 needs_breakpoint
= 1;
5418 collecting_t ipa_collecting_obj
;
5420 /* If `collecting' is set/locked, then the THREAD_AREA thread
5421 may or not be the one holding the lock. We have to read the
5422 lock to find out. */
5424 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5427 trace_debug ("fast_tracepoint_collecting:"
5428 " failed reading 'collecting' in the inferior");
5432 if (!ipa_collecting
)
5434 trace_debug ("fast_tracepoint_collecting: not collecting"
5435 " (and nobody is).");
5439 /* Some thread is collecting. Check which. */
5440 if (read_inferior_memory (ipa_collecting
,
5441 (unsigned char *) &ipa_collecting_obj
,
5442 sizeof (ipa_collecting_obj
)) != 0)
5445 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5447 trace_debug ("fast_tracepoint_collecting: not collecting "
5448 "(another thread is)");
5453 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5456 warning ("fast_tracepoint_collecting: collecting, "
5457 "but tpoint %s not found?",
5458 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5462 /* The thread is within `gdb_collect', skip over the rest of
5463 fast tracepoint collection quickly using a breakpoint. */
5464 needs_breakpoint
= 1;
5467 /* The caller wants a bit of status detail. */
5470 status
->tpoint_num
= tpoint
->number
;
5471 status
->tpoint_addr
= tpoint
->address
;
5472 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5473 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5476 if (needs_breakpoint
)
5478 /* Hasn't executed the original instruction yet. Set breakpoint
5479 there, and wait till it's hit, then single-step until exiting
5483 fast_tracepoint_collecting, returning continue-until-break at %s",
5484 paddress (tpoint
->adjusted_insn_addr
));
5486 return 1; /* continue */
5490 /* Just single-step until exiting the jump pad. */
5492 trace_debug ("fast_tracepoint_collecting, returning "
5493 "need-single-step (%s-%s)",
5494 paddress (tpoint
->adjusted_insn_addr
),
5495 paddress (tpoint
->adjusted_insn_addr_end
));
5497 return 2; /* single-step */
5503 #ifdef IN_PROCESS_AGENT
5505 /* The global fast tracepoint collect lock. Points to a collecting_t
5506 object built on the stack by the jump pad, if presently locked;
5507 NULL if it isn't locked. Note that this lock *must* be set while
5508 executing any *function other than the jump pad. See
5509 fast_tracepoint_collecting. */
5510 static collecting_t
* ATTR_USED collecting
;
5512 /* This routine, called from the jump pad (in asm) is designed to be
5513 called from the jump pads of fast tracepoints, thus it is on the
5516 IP_AGENT_EXPORT
void ATTR_USED
5517 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5519 struct fast_tracepoint_ctx ctx
;
5521 /* Don't do anything until the trace run is completely set up. */
5525 ctx
.base
.type
= fast_tracepoint
;
5527 ctx
.regcache_initted
= 0;
5528 /* Wrap the regblock in a register cache (in the stack, we don't
5529 want to malloc here). */
5530 ctx
.regspace
= alloca (register_cache_size ());
5531 if (ctx
.regspace
== NULL
)
5533 trace_debug ("Trace buffer block allocation failed, skipping");
5537 for (ctx
.tpoint
= tpoint
;
5538 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5539 ctx
.tpoint
= ctx
.tpoint
->next
)
5541 if (!ctx
.tpoint
->enabled
)
5544 /* Multiple tracepoints of different types, such as fast tracepoint and
5545 static tracepoint, can be set at the same address. */
5546 if (ctx
.tpoint
->type
!= tpoint
->type
)
5549 /* Test the condition if present, and collect if true. */
5550 if (ctx
.tpoint
->cond
== NULL
5551 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5554 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5555 ctx
.tpoint
->address
, ctx
.tpoint
);
5557 /* Note that this will cause original insns to be written back
5558 to where we jumped from, but that's OK because we're jumping
5559 back to the next whole instruction. This will go badly if
5560 instruction restoration is not atomic though. */
5561 if (stopping_tracepoint
5562 || trace_buffer_is_full
5563 || expr_eval_result
!= expr_eval_no_error
)
5571 /* If there was a condition and it evaluated to false, the only
5572 way we would stop tracing is if there was an error during
5573 condition expression evaluation. */
5574 if (expr_eval_result
!= expr_eval_no_error
)
5585 #ifndef IN_PROCESS_AGENT
5588 get_raw_reg_func_addr (void)
5590 return ipa_sym_addrs
.addr_get_raw_reg
;
5594 get_get_tsv_func_addr (void)
5596 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5600 get_set_tsv_func_addr (void)
5602 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5606 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5607 CORE_ADDR
*jump_entry
)
5609 CORE_ADDR entry_point
= *jump_entry
;
5610 enum eval_result_type err
;
5612 trace_debug ("Starting condition compilation for tracepoint %d\n",
5615 /* Initialize the global pointer to the code being built. */
5616 current_insn_ptr
= *jump_entry
;
5620 err
= compile_bytecodes (tpoint
->cond
);
5622 if (err
== expr_eval_no_error
)
5626 /* Record the beginning of the compiled code. */
5627 tpoint
->compiled_cond
= entry_point
;
5629 trace_debug ("Condition compilation for tracepoint %d complete\n",
5634 /* Leave the unfinished code in situ, but don't point to it. */
5636 tpoint
->compiled_cond
= 0;
5638 trace_debug ("Condition compilation for tracepoint %d failed, "
5640 tpoint
->number
, err
);
5643 /* Update the code pointer passed in. Note that we do this even if
5644 the compile fails, so that we can look at the partial results
5645 instead of letting them be overwritten. */
5646 *jump_entry
= current_insn_ptr
;
5648 /* Leave a gap, to aid dump decipherment. */
5652 /* We'll need to adjust these when we consider bi-arch setups, and big
5656 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5658 return write_inferior_memory (where
,
5659 (unsigned char *) &ptr
, sizeof (void *));
5662 /* The base pointer of the IPA's heap. This is the only memory the
5663 IPA is allowed to use. The IPA should _not_ call the inferior's
5664 `malloc' during operation. That'd be slow, and, most importantly,
5665 it may not be safe. We may be collecting a tracepoint in a signal
5666 handler, for example. */
5667 static CORE_ADDR target_tp_heap
;
5669 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5673 target_malloc (ULONGEST size
)
5677 if (target_tp_heap
== 0)
5679 /* We have the pointer *address*, need what it points to. */
5680 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5682 fatal ("could get target heap head pointer");
5685 ptr
= target_tp_heap
;
5686 target_tp_heap
+= size
;
5688 /* Pad to 8-byte alignment. */
5689 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5695 download_agent_expr (struct agent_expr
*expr
)
5697 CORE_ADDR expr_addr
;
5698 CORE_ADDR expr_bytes
;
5700 expr_addr
= target_malloc (sizeof (*expr
));
5701 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5703 expr_bytes
= target_malloc (expr
->length
);
5704 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5706 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5711 /* Align V up to N bits. */
5712 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5714 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5717 download_tracepoint_1 (struct tracepoint
*tpoint
)
5719 struct tracepoint target_tracepoint
;
5720 CORE_ADDR tpptr
= 0;
5722 gdb_assert (tpoint
->type
== fast_tracepoint
5723 || tpoint
->type
== static_tracepoint
);
5725 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5727 CORE_ADDR jentry
, jump_entry
;
5729 jentry
= jump_entry
= get_jump_space_head ();
5731 if (tpoint
->cond
!= NULL
)
5733 /* Pad to 8-byte alignment. (needed?) */
5734 /* Actually this should be left for the target to
5736 jentry
= UALIGN (jentry
, 8);
5738 compile_tracepoint_condition (tpoint
, &jentry
);
5741 /* Pad to 8-byte alignment. */
5742 jentry
= UALIGN (jentry
, 8);
5743 claim_jump_space (jentry
- jump_entry
);
5746 target_tracepoint
= *tpoint
;
5748 tpptr
= target_malloc (sizeof (*tpoint
));
5749 tpoint
->obj_addr_on_target
= tpptr
;
5751 /* Write the whole object. We'll fix up its pointers in a bit.
5752 Assume no next for now. This is fixed up above on the next
5753 iteration, if there's any. */
5754 target_tracepoint
.next
= NULL
;
5755 /* Need to clear this here too, since we're downloading the
5756 tracepoints before clearing our own copy. */
5757 target_tracepoint
.hit_count
= 0;
5759 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5760 sizeof (target_tracepoint
));
5763 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5765 download_agent_expr (tpoint
->cond
));
5767 if (tpoint
->numactions
)
5770 CORE_ADDR actions_array
;
5772 /* The pointers array. */
5774 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
5775 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5779 /* Now for each pointer, download the action. */
5780 for (i
= 0; i
< tpoint
->numactions
; i
++)
5782 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5783 CORE_ADDR ipa_action
= action
->ops
->download (action
);
5785 if (ipa_action
!= 0)
5786 write_inferior_data_ptr
5787 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
5794 download_tracepoint (struct tracepoint
*tpoint
)
5796 struct tracepoint
*tp
, *tp_prev
;
5798 if (tpoint
->type
!= fast_tracepoint
5799 && tpoint
->type
!= static_tracepoint
)
5802 download_tracepoint_1 (tpoint
);
5804 /* Find the previous entry of TPOINT, which is fast tracepoint or
5805 static tracepoint. */
5807 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
5809 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
5815 CORE_ADDR tp_prev_target_next_addr
;
5817 /* Insert TPOINT after TP_PREV in IPA. */
5818 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
5819 + offsetof (struct tracepoint
, next
),
5820 &tp_prev_target_next_addr
))
5821 fatal ("error reading `tp_prev->next'");
5823 /* tpoint->next = tp_prev->next */
5824 write_inferior_data_ptr (tpoint
->obj_addr_on_target
5825 + offsetof (struct tracepoint
, next
),
5826 tp_prev_target_next_addr
);
5827 /* tp_prev->next = tpoint */
5828 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
5829 + offsetof (struct tracepoint
, next
),
5830 tpoint
->obj_addr_on_target
);
5833 /* First object in list, set the head pointer in the
5835 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
5836 tpoint
->obj_addr_on_target
);
5841 download_tracepoints (void)
5843 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
5844 struct tracepoint
*tpoint
;
5846 /* Start out empty. */
5847 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
5849 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5851 if (tpoint
->type
!= fast_tracepoint
5852 && tpoint
->type
!= static_tracepoint
)
5857 download_tracepoint_1 (tpoint
);
5859 tpptr
= tpoint
->obj_addr_on_target
;
5861 if (tpoint
== tracepoints
)
5863 /* First object in list, set the head pointer in the
5865 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
5869 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
5877 download_trace_state_variables (void)
5879 CORE_ADDR ptr
= 0, prev_ptr
= 0;
5880 struct trace_state_variable
*tsv
;
5882 /* Start out empty. */
5883 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
5885 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
5887 struct trace_state_variable target_tsv
;
5889 /* TSV's with a getter have been initialized equally in both the
5890 inferior and GDBserver. Skip them. */
5891 if (tsv
->getter
!= NULL
)
5897 ptr
= target_malloc (sizeof (*tsv
));
5899 if (tsv
== trace_state_variables
)
5901 /* First object in list, set the head pointer in the
5904 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
5909 write_inferior_data_ptr (prev_ptr
5910 + offsetof (struct trace_state_variable
,
5915 /* Write the whole object. We'll fix up its pointers in a bit.
5916 Assume no next, fixup when needed. */
5917 target_tsv
.next
= NULL
;
5919 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
5920 sizeof (target_tsv
));
5922 if (tsv
->name
!= NULL
)
5924 size_t size
= strlen (tsv
->name
) + 1;
5925 CORE_ADDR name_addr
= target_malloc (size
);
5926 write_inferior_memory (name_addr
,
5927 (unsigned char *) tsv
->name
, size
);
5928 write_inferior_data_ptr (ptr
5929 + offsetof (struct trace_state_variable
,
5934 if (tsv
->getter
!= NULL
)
5936 fatal ("what to do with these?");
5942 /* Fixup the next pointer in the last item in the list. */
5943 write_inferior_data_ptr (prev_ptr
5944 + offsetof (struct trace_state_variable
,
5949 /* Upload complete trace frames out of the IP Agent's trace buffer
5950 into GDBserver's trace buffer. This always uploads either all or
5951 no trace frames. This is the counter part of
5952 `trace_alloc_trace_buffer'. See its description of the atomic
5953 synching mechanism. */
5956 upload_fast_traceframes (void)
5958 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
5959 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
5961 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
5962 unsigned int curr_tbctrl_idx
;
5963 unsigned int ipa_trace_buffer_ctrl_curr
;
5964 unsigned int ipa_trace_buffer_ctrl_curr_old
;
5965 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
5966 struct breakpoint
*about_to_request_buffer_space_bkpt
;
5967 CORE_ADDR ipa_trace_buffer_lo
;
5968 CORE_ADDR ipa_trace_buffer_hi
;
5970 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
5971 &ipa_traceframe_read_count_racy
))
5973 /* This will happen in most targets if the current thread is
5978 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
5979 &ipa_traceframe_write_count_racy
))
5982 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
5983 ipa_traceframe_write_count_racy
5984 - ipa_traceframe_read_count_racy
,
5985 ipa_traceframe_write_count_racy
,
5986 ipa_traceframe_read_count_racy
);
5988 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
5991 about_to_request_buffer_space_bkpt
5992 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
5995 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
5996 &ipa_trace_buffer_ctrl_curr
))
5999 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6001 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6004 unsigned int prev
, counter
;
6006 /* Update the token, with new counters, and the GDBserver stamp
6007 bit. Alway reuse the current TBC index. */
6008 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6009 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6011 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6017 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6018 ipa_trace_buffer_ctrl_curr
))
6021 trace_debug ("Lib: Committed %08x -> %08x",
6022 ipa_trace_buffer_ctrl_curr_old
,
6023 ipa_trace_buffer_ctrl_curr
);
6025 /* Re-read these, now that we've installed the
6026 `about_to_request_buffer_space' breakpoint/lock. A thread could
6027 have finished a traceframe between the last read of these
6028 counters and setting the breakpoint above. If we start
6029 uploading, we never want to leave this function with
6030 traceframe_read_count != 0, otherwise, GDBserver could end up
6031 incrementing the counter tokens more than once (due to event loop
6032 nesting), which would break the IP agent's "effective" detection
6033 (see trace_alloc_trace_buffer). */
6034 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6035 &ipa_traceframe_read_count
))
6037 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6038 &ipa_traceframe_write_count
))
6043 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6044 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6045 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6047 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6048 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6049 trace_debug ("note that ipa_traceframe_count's parts changed");
6052 /* Get the address of the current TBC object (the IP agent has an
6053 array of 3 such objects). The index is stored in the TBC
6055 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6056 ipa_trace_buffer_ctrl_addr
6057 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6059 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6060 (unsigned char *) &ipa_trace_buffer_ctrl
,
6061 sizeof (struct ipa_trace_buffer_control
)))
6064 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6065 &ipa_trace_buffer_lo
))
6067 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6068 &ipa_trace_buffer_hi
))
6071 /* Offsets are easier to grok for debugging than raw addresses,
6072 especially for the small trace buffer sizes that are useful for
6074 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6075 "endfree=%d wrap=%d hi=%d",
6077 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6078 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6079 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6080 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6081 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6083 /* Note that the IPA's buffer is always circular. */
6085 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6087 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6088 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6090 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6091 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6092 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6093 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6096 tf
= IPA_FIRST_TRACEFRAME ();
6098 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6100 struct tracepoint
*tpoint
;
6101 struct traceframe
*tframe
;
6102 unsigned char *block
;
6103 struct traceframe ipa_tframe
;
6105 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6106 offsetof (struct traceframe
, data
)))
6107 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6109 if (ipa_tframe
.tpnum
== 0)
6110 fatal ("Uploading: No (more) fast traceframes, but "
6111 "ipa_traceframe_count == %u??\n",
6112 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6114 /* Note that this will be incorrect for multi-location
6116 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6118 tframe
= add_traceframe (tpoint
);
6121 trace_buffer_is_full
= 1;
6122 trace_debug ("Uploading: trace buffer is full");
6126 /* Copy the whole set of blocks in one go for now. FIXME:
6127 split this in smaller blocks. */
6128 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6131 if (read_inferior_memory (tf
6132 + offsetof (struct traceframe
, data
),
6133 block
, ipa_tframe
.data_size
))
6134 error ("Uploading: Couldn't read traceframe data at %s\n",
6135 paddress (tf
+ offsetof (struct traceframe
, data
)));
6138 trace_debug ("Uploading: traceframe didn't fit");
6139 finish_traceframe (tframe
);
6142 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6144 /* If we freed the traceframe that wrapped around, go back
6145 to the non-wrap case. */
6146 if (tf
< ipa_trace_buffer_ctrl
.start
)
6148 trace_debug ("Lib: Discarding past the wraparound");
6149 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6151 ipa_trace_buffer_ctrl
.start
= tf
;
6152 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6153 ++ipa_traceframe_read_count
;
6155 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6156 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6158 trace_debug ("Lib: buffer is fully empty. "
6159 "Trace buffer [%d] start=%d free=%d endfree=%d",
6161 (int) (ipa_trace_buffer_ctrl
.start
6162 - ipa_trace_buffer_lo
),
6163 (int) (ipa_trace_buffer_ctrl
.free
6164 - ipa_trace_buffer_lo
),
6165 (int) (ipa_trace_buffer_ctrl
.end_free
6166 - ipa_trace_buffer_lo
));
6168 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6169 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6170 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6171 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6174 trace_debug ("Uploaded a traceframe\n"
6175 "Lib: Trace buffer [%d] start=%d free=%d "
6176 "endfree=%d wrap=%d hi=%d",
6178 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6179 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6180 (int) (ipa_trace_buffer_ctrl
.end_free
6181 - ipa_trace_buffer_lo
),
6182 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6183 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6186 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6187 (unsigned char *) &ipa_trace_buffer_ctrl
,
6188 sizeof (struct ipa_trace_buffer_control
)))
6191 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6192 ipa_traceframe_read_count
);
6194 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6197 cancel_breakpoints ();
6199 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6200 about_to_request_buffer_space_bkpt
= NULL
;
6204 if (trace_buffer_is_full
)
6209 #ifdef IN_PROCESS_AGENT
6211 IP_AGENT_EXPORT
int ust_loaded
;
6212 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6216 /* Static tracepoints. */
6218 /* UST puts a "struct tracepoint" in the global namespace, which
6219 conflicts with our tracepoint. Arguably, being a library, it
6220 shouldn't take ownership of such a generic name. We work around it
6222 #define tracepoint ust_tracepoint
6223 #include <ust/ust.h>
6226 extern int serialize_to_text (char *outbuf
, int bufsize
,
6227 const char *fmt
, va_list ap
);
6229 #define GDB_PROBE_NAME "gdb"
6231 /* We dynamically search for the UST symbols instead of linking them
6232 in. This lets the user decide if the application uses static
6233 tracepoints, instead of always pulling libust.so in. This vector
6234 holds pointers to all functions we care about. */
6238 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6239 const char *fmt
, va_list ap
);
6241 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6242 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6244 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6246 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6249 void (*marker_iter_start
) (struct marker_iter
*iter
);
6250 void (*marker_iter_next
) (struct marker_iter
*iter
);
6251 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6252 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6257 /* Cast through typeof to catch incompatible API changes. Since UST
6258 only builds with gcc, we can freely use gcc extensions here
6260 #define GET_UST_SYM(SYM) \
6263 if (ust_ops.SYM == NULL) \
6264 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6265 if (ust_ops.SYM == NULL) \
6269 #define USTF(SYM) ust_ops.SYM
6271 /* Get pointers to all libust.so functions we care about. */
6276 GET_UST_SYM (serialize_to_text
);
6278 GET_UST_SYM (ltt_probe_register
);
6279 GET_UST_SYM (ltt_probe_unregister
);
6280 GET_UST_SYM (ltt_marker_connect
);
6281 GET_UST_SYM (ltt_marker_disconnect
);
6283 GET_UST_SYM (marker_iter_start
);
6284 GET_UST_SYM (marker_iter_next
);
6285 GET_UST_SYM (marker_iter_stop
);
6286 GET_UST_SYM (marker_iter_reset
);
6292 /* Given an UST marker, return the matching gdb static tracepoint.
6293 The match is done by address. */
6295 static struct tracepoint
*
6296 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6298 struct tracepoint
*tpoint
;
6300 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6302 if (tpoint
->type
!= static_tracepoint
)
6305 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6312 /* The probe function we install on lttng/ust markers. Whenever a
6313 probed ust marker is hit, this function is called. This is similar
6314 to gdb_collect, only for static tracepoints, instead of fast
6318 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6319 struct registers
*regs
, void *call_private
,
6320 const char *fmt
, va_list *args
)
6322 struct tracepoint
*tpoint
;
6323 struct static_tracepoint_ctx ctx
;
6325 /* Don't do anything until the trace run is completely set up. */
6328 trace_debug ("gdb_probe: not tracing\n");
6332 ctx
.base
.type
= static_tracepoint
;
6333 ctx
.regcache_initted
= 0;
6338 /* Wrap the regblock in a register cache (in the stack, we don't
6339 want to malloc here). */
6340 ctx
.regspace
= alloca (register_cache_size ());
6341 if (ctx
.regspace
== NULL
)
6343 trace_debug ("Trace buffer block allocation failed, skipping");
6347 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6350 trace_debug ("gdb_probe: marker not known: "
6351 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6352 mdata
->location
, mdata
->channel
,
6353 mdata
->name
, mdata
->format
);
6357 if (!tpoint
->enabled
)
6359 trace_debug ("gdb_probe: tracepoint disabled");
6363 ctx
.tpoint
= tpoint
;
6365 trace_debug ("gdb_probe: collecting marker: "
6366 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6367 mdata
->location
, mdata
->channel
,
6368 mdata
->name
, mdata
->format
);
6370 /* Test the condition if present, and collect if true. */
6371 if (tpoint
->cond
== NULL
6372 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6375 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6376 tpoint
->address
, tpoint
);
6378 if (stopping_tracepoint
6379 || trace_buffer_is_full
6380 || expr_eval_result
!= expr_eval_no_error
)
6385 /* If there was a condition and it evaluated to false, the only
6386 way we would stop tracing is if there was an error during
6387 condition expression evaluation. */
6388 if (expr_eval_result
!= expr_eval_no_error
)
6393 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6394 static tracepoint string data. This is a string passed to the
6395 tracing library by the user, at the time of the tracepoint marker
6396 call. E.g., in the UST marker call:
6398 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6400 the collected data is "str FOOBAZ".
6404 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6405 struct traceframe
*tframe
)
6407 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6408 unsigned char *bufspace
;
6411 unsigned short blocklen
;
6415 trace_debug ("Wanted to collect static trace data, "
6416 "but there's no static trace data");
6420 va_copy (copy
, *umd
->args
);
6421 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6424 trace_debug ("Want to collect ust data");
6426 /* 'S' + size + string */
6427 bufspace
= add_traceframe_block (tframe
,
6428 1 + sizeof (blocklen
) + size
+ 1);
6429 if (bufspace
== NULL
)
6431 trace_debug ("Trace buffer block allocation failed, skipping");
6435 /* Identify a static trace data block. */
6438 blocklen
= size
+ 1;
6439 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6441 va_copy (copy
, *umd
->args
);
6442 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6443 size
+ 1, umd
->fmt
, copy
);
6446 trace_debug ("Storing static tracepoint data in regblock: %s",
6447 bufspace
+ 1 + sizeof (blocklen
));
6450 /* The probe to register with lttng/ust. */
6451 static struct ltt_available_probe gdb_ust_probe
=
6458 #endif /* HAVE_UST */
6459 #endif /* IN_PROCESS_AGENT */
6461 #ifndef IN_PROCESS_AGENT
6463 /* Ask the in-process agent to run a command. Since we don't want to
6464 have to handle the IPA hitting breakpoints while running the
6465 command, we pause all threads, remove all breakpoints, and then set
6466 the helper thread re-running. We communicate with the helper
6467 thread by means of direct memory xfering, and a socket for
6471 run_inferior_command (char *cmd
)
6474 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
6476 trace_debug ("run_inferior_command: running: %s", cmd
);
6479 uninsert_all_breakpoints ();
6481 err
= agent_run_command (pid
, (const char *) cmd
);
6483 reinsert_all_breakpoints ();
6489 #else /* !IN_PROCESS_AGENT */
6491 #include <sys/socket.h>
6494 #ifndef UNIX_PATH_MAX
6495 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6498 /* Where we put the socked used for synchronization. */
6499 #define SOCK_DIR P_tmpdir
6501 /* Thread ID of the helper thread. GDBserver reads this to know which
6502 is the help thread. This is an LWP id on Linux. */
6503 int helper_thread_id
;
6506 init_named_socket (const char *name
)
6509 struct sockaddr_un addr
;
6511 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6514 warning ("socket creation failed: %s", strerror (errno
));
6518 addr
.sun_family
= AF_UNIX
;
6520 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6521 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6523 result
= access (name
, F_OK
);
6527 result
= unlink (name
);
6530 warning ("unlink failed: %s", strerror (errno
));
6534 warning ("socket %s already exists; overwriting", name
);
6537 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6540 warning ("bind failed: %s", strerror (errno
));
6545 result
= listen (fd
, 1);
6548 warning ("listen: %s", strerror (errno
));
6557 gdb_agent_socket_init (void)
6560 char name
[UNIX_PATH_MAX
];
6562 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6563 SOCK_DIR
, getpid ());
6564 if (result
>= UNIX_PATH_MAX
)
6566 trace_debug ("string overflow allocating socket name");
6570 fd
= init_named_socket (name
);
6572 warning ("Error initializing named socket (%s) for communication with the "
6573 "ust helper thread. Check that directory exists and that it "
6574 "is writable.", name
);
6581 /* The next marker to be returned on a qTsSTM command. */
6582 static const struct marker
*next_st
;
6584 /* Returns the first known marker. */
6589 struct marker_iter iter
;
6591 USTF(marker_iter_reset
) (&iter
);
6592 USTF(marker_iter_start
) (&iter
);
6597 /* Returns the marker following M. */
6599 const struct marker
*
6600 next_marker (const struct marker
*m
)
6602 struct marker_iter iter
;
6604 USTF(marker_iter_reset
) (&iter
);
6605 USTF(marker_iter_start
) (&iter
);
6607 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6609 if (iter
.marker
== m
)
6611 USTF(marker_iter_next
) (&iter
);
6619 /* Return an hexstr version of the STR C string, fit for sending to
6623 cstr_to_hexstr (const char *str
)
6625 int len
= strlen (str
);
6626 char *hexstr
= xmalloc (len
* 2 + 1);
6627 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6631 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6635 response_ust_marker (char *packet
, const struct marker
*st
)
6637 char *strid
, *format
, *tmp
;
6639 next_st
= next_marker (st
);
6641 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6642 strlen (st
->name
) + 1);
6643 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6645 strid
= cstr_to_hexstr (tmp
);
6648 format
= cstr_to_hexstr (st
->format
);
6650 sprintf (packet
, "m%s:%s:%s",
6651 paddress ((uintptr_t) st
->location
),
6659 /* Return the first static tracepoint, and initialize the state
6660 machine that will iterate through all the static tracepoints. */
6663 cmd_qtfstm (char *packet
)
6665 trace_debug ("Returning first trace state variable definition");
6667 if (first_marker ())
6668 response_ust_marker (packet
, first_marker ());
6670 strcpy (packet
, "l");
6673 /* Return additional trace state variable definitions. */
6676 cmd_qtsstm (char *packet
)
6678 trace_debug ("Returning static tracepoint");
6681 response_ust_marker (packet
, next_st
);
6683 strcpy (packet
, "l");
6686 /* Disconnect the GDB probe from a marker at a given address. */
6689 unprobe_marker_at (char *packet
)
6693 struct marker_iter iter
;
6695 p
+= sizeof ("unprobe_marker_at:") - 1;
6697 p
= unpack_varlen_hex (p
, &address
);
6699 USTF(marker_iter_reset
) (&iter
);
6700 USTF(marker_iter_start
) (&iter
);
6701 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6702 if ((uintptr_t ) iter
.marker
->location
== address
)
6706 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
6710 warning ("could not disable marker %s/%s",
6711 iter
.marker
->channel
, iter
.marker
->name
);
6716 /* Connect the GDB probe to a marker at a given address. */
6719 probe_marker_at (char *packet
)
6723 struct marker_iter iter
;
6726 p
+= sizeof ("probe_marker_at:") - 1;
6728 p
= unpack_varlen_hex (p
, &address
);
6730 USTF(marker_iter_reset
) (&iter
);
6732 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6734 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6735 if ((uintptr_t ) m
->location
== address
)
6739 trace_debug ("found marker for address. "
6740 "ltt_marker_connect (marker = %s/%s)",
6741 m
->channel
, m
->name
);
6743 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
6745 if (result
&& result
!= -EEXIST
)
6746 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
6747 m
->channel
, m
->name
, -result
);
6751 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
6752 m
->channel
, m
->name
);
6756 strcpy (packet
, "OK");
6760 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
6765 cmd_qtstmat (char *packet
)
6769 struct marker_iter iter
;
6772 p
+= sizeof ("qTSTMat:") - 1;
6774 p
= unpack_varlen_hex (p
, &address
);
6776 USTF(marker_iter_reset
) (&iter
);
6778 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6780 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6781 if ((uintptr_t ) m
->location
== address
)
6783 response_ust_marker (packet
, m
);
6787 strcpy (packet
, "l");
6797 USTF(ltt_probe_register
) (&gdb_ust_probe
);
6800 #endif /* HAVE_UST */
6802 #include <sys/syscall.h>
6804 /* Helper thread of agent. */
6807 gdb_agent_helper_thread (void *arg
)
6813 listen_fd
= gdb_agent_socket_init ();
6815 if (helper_thread_id
== 0)
6816 helper_thread_id
= syscall (SYS_gettid
);
6818 if (listen_fd
== -1)
6820 warning ("could not create sync socket\n");
6827 struct sockaddr_un sockaddr
;
6832 tmp
= sizeof (sockaddr
);
6836 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
6838 /* It seems an ERESTARTSYS can escape out of accept. */
6839 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
6843 warning ("Accept returned %d, error: %s\n",
6844 fd
, strerror (errno
));
6850 ret
= read (fd
, buf
, 1);
6851 } while (ret
== -1 && errno
== EINTR
);
6855 warning ("reading socket (fd=%d) failed with %s",
6856 fd
, strerror (errno
));
6864 if (strcmp ("qTfSTM", cmd_buf
) == 0)
6866 cmd_qtfstm (cmd_buf
);
6868 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
6870 cmd_qtsstm (cmd_buf
);
6872 else if (strncmp ("unprobe_marker_at:",
6874 sizeof ("unprobe_marker_at:") - 1) == 0)
6876 unprobe_marker_at (cmd_buf
);
6878 else if (strncmp ("probe_marker_at:",
6880 sizeof ("probe_marker_at:") - 1) == 0)
6882 probe_marker_at (cmd_buf
);
6884 else if (strncmp ("qTSTMat:",
6886 sizeof ("qTSTMat:") - 1) == 0)
6888 cmd_qtstmat (cmd_buf
);
6890 #endif /* HAVE_UST */
6893 /* Fix compiler's warning: ignoring return value of 'write'. */
6894 ret
= write (fd
, buf
, 1);
6903 #include <pthread.h>
6905 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
6908 gdb_agent_init (void)
6915 /* We want the helper thread to be as transparent as possible, so
6916 have it inherit an all-signals-blocked mask. */
6918 sigfillset (&new_mask
);
6919 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
6921 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
6923 res
= pthread_create (&thread
,
6925 gdb_agent_helper_thread
,
6928 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
6930 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
6932 while (helper_thread_id
== 0)
6940 #include <sys/mman.h>
6943 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
6944 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
6945 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
6946 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
6947 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
6948 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
6950 /* Record the result of getting buffer space for fast tracepoint
6951 trampolines. Any error message is copied, since caller may not be
6952 using persistent storage. */
6955 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
6957 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
6958 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
6960 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
6962 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
6965 static void __attribute__ ((constructor
))
6966 initialize_tracepoint_ftlib (void)
6968 initialize_tracepoint ();
6973 #endif /* IN_PROCESS_AGENT */
6975 /* Return a timestamp, expressed as microseconds of the usual Unix
6976 time. (As the result is a 64-bit number, it will not overflow any
6980 get_timestamp (void)
6984 if (gettimeofday (&tv
, 0) != 0)
6987 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
6991 initialize_tracepoint (void)
6993 /* There currently no way to change the buffer size. */
6994 const int sizeOfBuffer
= 5 * 1024 * 1024;
6995 unsigned char *buf
= xmalloc (sizeOfBuffer
);
6996 init_trace_buffer (buf
, sizeOfBuffer
);
6998 /* Wire trace state variable 1 to be the timestamp. This will be
6999 uploaded to GDB upon connection and become one of its trace state
7000 variables. (In case you're wondering, if GDB already has a trace
7001 variable numbered 1, it will be renumbered.) */
7002 create_trace_state_variable (1, 0);
7003 set_trace_state_variable_name (1, "trace_timestamp");
7004 set_trace_state_variable_getter (1, get_timestamp
);
7006 #ifdef IN_PROCESS_AGENT
7011 pagesize
= sysconf (_SC_PAGE_SIZE
);
7015 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7017 #define SCRATCH_BUFFER_NPAGES 20
7019 /* Allocate scratch buffer aligned on a page boundary, at a low
7020 address (close to the main executable's code). */
7021 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7023 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7024 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7025 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7027 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7033 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7036 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7039 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7041 /* It's not a fatal error for something to go wrong with trampoline
7042 buffer setup, but it can be mysterious, so create a channel to
7043 report back on what went wrong, using a fixed size since we may
7044 not be able to allocate space later when the problem occurs. */
7045 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7047 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7049 initialize_low_tracepoint ();