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/>. */
30 /* This file is built for both GDBserver, and the in-process
31 agent (IPA), a shared library that includes a tracing agent that is
32 loaded by the inferior to support fast tracepoints. Fast
33 tracepoints (or more accurately, jump based tracepoints) are
34 implemented by patching the tracepoint location with a jump into a
35 small trampoline function whose job is to save the register state,
36 call the in-process tracing agent, and then execute the original
37 instruction that was under the tracepoint jump (possibly adjusted,
38 if PC-relative, or some such).
40 The current synchronization design is pull based. That means,
41 GDBserver does most of the work, by peeking/poking at the inferior
42 agent's memory directly for downloading tracepoint and associated
43 objects, and for uploading trace frames. Whenever the IPA needs
44 something from GDBserver (trace buffer is full, tracing stopped for
45 some reason, etc.) the IPA calls a corresponding hook function
46 where GDBserver has placed a breakpoint.
48 Each of the agents has its own trace buffer. When browsing the
49 trace frames built from slow and fast tracepoints from GDB (tfind
50 mode), there's no guarantee the user is seeing the trace frames in
51 strict chronological creation order, although, GDBserver tries to
52 keep the order relatively reasonable, by syncing the trace buffers
57 static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf
, 1, 2);
60 trace_vdebug (const char *fmt
, ...)
66 vsprintf (buf
, fmt
, ap
);
67 fprintf (stderr
, PROG
"/tracepoint: %s\n", buf
);
71 #define trace_debug_1(level, fmt, args...) \
73 if (level <= debug_threads) \
74 trace_vdebug ((fmt), ##args); \
77 #define trace_debug(FMT, args...) \
78 trace_debug_1 (1, FMT, ##args)
81 # define ATTR_USED __attribute__((used))
82 # define ATTR_NOINLINE __attribute__((noinline))
83 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
86 # define ATTR_NOINLINE
87 # define ATTR_CONSTRUCTOR
90 /* Make sure the functions the IPA needs to export (symbols GDBserver
91 needs to query GDB about) are exported. */
93 #ifdef IN_PROCESS_AGENT
94 # if defined _WIN32 || defined __CYGWIN__
95 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
98 # define IP_AGENT_EXPORT \
99 __attribute__ ((visibility("default"))) ATTR_USED
101 # define IP_AGENT_EXPORT ATTR_USED
105 # define IP_AGENT_EXPORT
108 /* Prefix exported symbols, for good citizenship. All the symbols
109 that need exporting are defined in this module. */
110 #ifdef IN_PROCESS_AGENT
111 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
112 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
113 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
114 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
115 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
116 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
117 # define collecting gdb_agent_collecting
118 # define gdb_collect gdb_agent_gdb_collect
119 # define stop_tracing gdb_agent_stop_tracing
120 # define flush_trace_buffer gdb_agent_flush_trace_buffer
121 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
122 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
123 # define stopping_tracepoint gdb_agent_stopping_tracepoint
124 # define expr_eval_result gdb_agent_expr_eval_result
125 # define error_tracepoint gdb_agent_error_tracepoint
126 # define tracepoints gdb_agent_tracepoints
127 # define tracing gdb_agent_tracing
128 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
129 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
130 # define trace_buffer_lo gdb_agent_trace_buffer_lo
131 # define trace_buffer_hi gdb_agent_trace_buffer_hi
132 # define traceframe_read_count gdb_agent_traceframe_read_count
133 # define traceframe_write_count gdb_agent_traceframe_write_count
134 # define traceframes_created gdb_agent_traceframes_created
135 # define trace_state_variables gdb_agent_trace_state_variables
136 # define get_raw_reg gdb_agent_get_raw_reg
137 # define get_trace_state_variable_value \
138 gdb_agent_get_trace_state_variable_value
139 # define set_trace_state_variable_value \
140 gdb_agent_set_trace_state_variable_value
141 # define ust_loaded gdb_agent_ust_loaded
142 # define helper_thread_id gdb_agent_helper_thread_id
143 # define cmd_buf gdb_agent_cmd_buf
146 #ifndef IN_PROCESS_AGENT
148 /* Addresses of in-process agent's symbols GDBserver cares about. */
150 struct ipa_sym_addresses
152 CORE_ADDR addr_gdb_tp_heap_buffer
;
153 CORE_ADDR addr_gdb_jump_pad_buffer
;
154 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
155 CORE_ADDR addr_gdb_trampoline_buffer
;
156 CORE_ADDR addr_gdb_trampoline_buffer_end
;
157 CORE_ADDR addr_gdb_trampoline_buffer_error
;
158 CORE_ADDR addr_collecting
;
159 CORE_ADDR addr_gdb_collect
;
160 CORE_ADDR addr_stop_tracing
;
161 CORE_ADDR addr_flush_trace_buffer
;
162 CORE_ADDR addr_about_to_request_buffer_space
;
163 CORE_ADDR addr_trace_buffer_is_full
;
164 CORE_ADDR addr_stopping_tracepoint
;
165 CORE_ADDR addr_expr_eval_result
;
166 CORE_ADDR addr_error_tracepoint
;
167 CORE_ADDR addr_tracepoints
;
168 CORE_ADDR addr_tracing
;
169 CORE_ADDR addr_trace_buffer_ctrl
;
170 CORE_ADDR addr_trace_buffer_ctrl_curr
;
171 CORE_ADDR addr_trace_buffer_lo
;
172 CORE_ADDR addr_trace_buffer_hi
;
173 CORE_ADDR addr_traceframe_read_count
;
174 CORE_ADDR addr_traceframe_write_count
;
175 CORE_ADDR addr_traceframes_created
;
176 CORE_ADDR addr_trace_state_variables
;
177 CORE_ADDR addr_get_raw_reg
;
178 CORE_ADDR addr_get_trace_state_variable_value
;
179 CORE_ADDR addr_set_trace_state_variable_value
;
180 CORE_ADDR addr_ust_loaded
;
181 CORE_ADDR addr_helper_thread_id
;
182 CORE_ADDR addr_cmd_buf
;
185 #define STRINGIZE_1(STR) #STR
186 #define STRINGIZE(STR) STRINGIZE_1(STR)
187 #define IPA_SYM(SYM) \
189 STRINGIZE (gdb_agent_ ## SYM), \
190 offsetof (struct ipa_sym_addresses, addr_ ## SYM) \
199 IPA_SYM(gdb_tp_heap_buffer
),
200 IPA_SYM(gdb_jump_pad_buffer
),
201 IPA_SYM(gdb_jump_pad_buffer_end
),
202 IPA_SYM(gdb_trampoline_buffer
),
203 IPA_SYM(gdb_trampoline_buffer_end
),
204 IPA_SYM(gdb_trampoline_buffer_error
),
206 IPA_SYM(gdb_collect
),
207 IPA_SYM(stop_tracing
),
208 IPA_SYM(flush_trace_buffer
),
209 IPA_SYM(about_to_request_buffer_space
),
210 IPA_SYM(trace_buffer_is_full
),
211 IPA_SYM(stopping_tracepoint
),
212 IPA_SYM(expr_eval_result
),
213 IPA_SYM(error_tracepoint
),
214 IPA_SYM(tracepoints
),
216 IPA_SYM(trace_buffer_ctrl
),
217 IPA_SYM(trace_buffer_ctrl_curr
),
218 IPA_SYM(trace_buffer_lo
),
219 IPA_SYM(trace_buffer_hi
),
220 IPA_SYM(traceframe_read_count
),
221 IPA_SYM(traceframe_write_count
),
222 IPA_SYM(traceframes_created
),
223 IPA_SYM(trace_state_variables
),
224 IPA_SYM(get_raw_reg
),
225 IPA_SYM(get_trace_state_variable_value
),
226 IPA_SYM(set_trace_state_variable_value
),
228 IPA_SYM(helper_thread_id
),
232 struct ipa_sym_addresses ipa_sym_addrs
;
234 int all_tracepoint_symbols_looked_up
;
237 in_process_agent_loaded (void)
239 return all_tracepoint_symbols_looked_up
;
242 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
244 /* Returns true if both the in-process agent library and the static
245 tracepoints libraries are loaded in the inferior. */
248 in_process_agent_loaded_ust (void)
252 if (!in_process_agent_loaded ())
254 warning ("In-process agent not loaded");
258 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
260 warning ("Error reading ust_loaded in lib");
268 write_e_ipa_not_loaded (char *buffer
)
271 "E.In-process agent library not loaded in process. "
272 "Fast and static tracepoints unavailable.");
275 /* Write an error to BUFFER indicating that UST isn't loaded in the
279 write_e_ust_not_loaded (char *buffer
)
283 "E.UST library not loaded in process. "
284 "Static tracepoints unavailable.");
286 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
290 /* If the in-process agent library isn't loaded in the inferior, write
291 an error to BUFFER, and return 1. Otherwise, return 0. */
294 maybe_write_ipa_not_loaded (char *buffer
)
296 if (!in_process_agent_loaded ())
298 write_e_ipa_not_loaded (buffer
);
304 /* If the in-process agent library and the ust (static tracepoints)
305 library aren't loaded in the inferior, write an error to BUFFER,
306 and return 1. Otherwise, return 0. */
309 maybe_write_ipa_ust_not_loaded (char *buffer
)
311 if (!in_process_agent_loaded ())
313 write_e_ipa_not_loaded (buffer
);
316 else if (!in_process_agent_loaded_ust ())
318 write_e_ust_not_loaded (buffer
);
324 /* Cache all future symbols that the tracepoints module might request.
325 We can not request symbols at arbitrary states in the remote
326 protocol, only when the client tells us that new symbols are
327 available. So when we load the in-process library, make sure to
328 check the entire list. */
331 tracepoint_look_up_symbols (void)
335 if (all_tracepoint_symbols_looked_up
)
338 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
341 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
343 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
346 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
351 all_tracepoint_symbols_looked_up
= 1;
356 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
357 of the "stop_tracing" function. When this breakpoint is hit,
358 tracing stopped in the IPA for some reason. E.g., due to
359 tracepoint reaching the pass count, hitting conditional expression
360 evaluation error, etc.
362 The IPA's trace buffer is never in circular tracing mode: instead,
363 GDBserver's is, and whenever the in-process buffer fills, it calls
364 "flush_trace_buffer", which triggers an internal breakpoint.
365 GDBserver reacts to this breakpoint by pulling the meanwhile
366 collected data. Old frames discarding is always handled on the
369 #ifdef IN_PROCESS_AGENT
371 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
373 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
377 /* Call this in the functions where GDBserver places a breakpoint, so
378 that the compiler doesn't try to be clever and skip calling the
379 function at all. This is necessary, even if we tell the compiler
380 to not inline said functions. */
382 #if defined(__GNUC__)
383 # define UNKNOWN_SIDE_EFFECTS() asm ("")
385 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
388 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
391 /* GDBserver places breakpoint here. */
392 UNKNOWN_SIDE_EFFECTS();
395 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
396 flush_trace_buffer (void)
398 /* GDBserver places breakpoint here. */
399 UNKNOWN_SIDE_EFFECTS();
404 #ifndef IN_PROCESS_AGENT
406 tracepoint_handler (CORE_ADDR address
)
408 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
413 /* Breakpoint at "stop_tracing" in the inferior lib. */
414 struct breakpoint
*stop_tracing_bkpt
;
415 static int stop_tracing_handler (CORE_ADDR
);
417 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
418 struct breakpoint
*flush_trace_buffer_bkpt
;
419 static int flush_trace_buffer_handler (CORE_ADDR
);
421 static void download_tracepoints (void);
422 static void download_trace_state_variables (void);
423 static void upload_fast_traceframes (void);
425 static int run_inferior_command (char *cmd
);
428 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
430 return read_inferior_memory (symaddr
, (unsigned char *) val
,
435 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
437 return read_inferior_memory (symaddr
, (unsigned char *) val
,
442 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
444 void *pval
= (void *) (uintptr_t) val
;
447 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
448 *val
= (uintptr_t) pval
;
453 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
455 void *pval
= (void *) (uintptr_t) val
;
456 return write_inferior_memory (symaddr
,
457 (unsigned char *) &pval
, sizeof (pval
));
461 write_inferior_integer (CORE_ADDR symaddr
, int val
)
463 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
467 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
469 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
474 /* Base action. Concrete actions inherit this. */
476 struct tracepoint_action
481 /* An 'M' (collect memory) action. */
482 struct collect_memory_action
484 struct tracepoint_action base
;
491 /* An 'R' (collect registers) action. */
493 struct collect_registers_action
495 struct tracepoint_action base
;
498 /* An 'X' (evaluate expression) action. */
500 struct eval_expr_action
502 struct tracepoint_action base
;
504 struct agent_expr
*expr
;
507 /* An 'L' (collect static trace data) action. */
508 struct collect_static_trace_data_action
510 struct tracepoint_action base
;
513 /* This structure describes a piece of the source-level definition of
514 the tracepoint. The contents are not interpreted by the target,
515 but preserved verbatim for uploading upon reconnection. */
519 /* The type of string, such as "cond" for a conditional. */
522 /* The source-level string itself. For the sake of target
523 debugging, we store it in plaintext, even though it is always
524 transmitted in hex. */
527 /* Link to the next one in the list. We link them in the order
528 received, in case some make up an ordered list of commands or
530 struct source_string
*next
;
535 /* Trap based tracepoint. */
538 /* A fast tracepoint implemented with a jump instead of a trap. */
541 /* A static tracepoint, implemented by a program call into a tracing
546 struct tracepoint_hit_ctx
;
548 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
551 /* The definition of a tracepoint. */
553 /* Tracepoints may have multiple locations, each at a different
554 address. This can occur with optimizations, template
555 instantiation, etc. Since the locations may be in different
556 scopes, the conditions and actions may be different for each
557 location. Our target version of tracepoints is more like GDB's
558 notion of "breakpoint locations", but we have almost nothing that
559 is not per-location, so we bother having two kinds of objects. The
560 key consequence is that numbers are not unique, and that it takes
561 both number and address to identify a tracepoint uniquely. */
565 /* The number of the tracepoint, as specified by GDB. Several
566 tracepoint objects here may share a number. */
569 /* Address at which the tracepoint is supposed to trigger. Several
570 tracepoints may share an address. */
573 /* Tracepoint type. */
574 enum tracepoint_type type
;
576 /* True if the tracepoint is currently enabled. */
579 /* The number of single steps that will be performed after each
583 /* The number of times the tracepoint may be hit before it will
584 terminate the entire tracing run. */
587 /* Pointer to the agent expression that is the tracepoint's
588 conditional, or NULL if the tracepoint is unconditional. */
589 struct agent_expr
*cond
;
591 /* The list of actions to take when the tracepoint triggers. */
593 struct tracepoint_action
**actions
;
595 /* Count of the times we've hit this tracepoint during the run.
596 Note that while-stepping steps are not counted as "hits". */
599 /* Cached sum of the sizes of traceframes created by this point. */
600 long traceframe_usage
;
602 CORE_ADDR compiled_cond
;
604 /* Link to the next tracepoint in the list. */
605 struct tracepoint
*next
;
607 #ifndef IN_PROCESS_AGENT
608 /* The list of actions to take when the tracepoint triggers, in
609 string/packet form. */
612 /* The collection of strings that describe the tracepoint as it was
613 entered into GDB. These are not used by the target, but are
614 reported back to GDB upon reconnection. */
615 struct source_string
*source_strings
;
617 /* The number of bytes displaced by fast tracepoints. It may subsume
618 multiple instructions, for multi-byte fast tracepoints. This
619 field is only valid for fast tracepoints. */
622 /* Only for fast tracepoints. */
623 CORE_ADDR obj_addr_on_target
;
625 /* Address range where the original instruction under a fast
626 tracepoint was relocated to. (_end is actually one byte past
628 CORE_ADDR adjusted_insn_addr
;
629 CORE_ADDR adjusted_insn_addr_end
;
631 /* The address range of the piece of the jump pad buffer that was
632 assigned to this fast tracepoint. (_end is actually one byte
635 CORE_ADDR jump_pad_end
;
637 /* The address range of the piece of the trampoline buffer that was
638 assigned to this fast tracepoint. (_end is actually one byte
640 CORE_ADDR trampoline
;
641 CORE_ADDR trampoline_end
;
643 /* The list of actions to take while in a stepping loop. These
644 fields are only valid for patch-based tracepoints. */
645 int num_step_actions
;
646 struct tracepoint_action
**step_actions
;
647 /* Same, but in string/packet form. */
648 char **step_actions_str
;
650 /* Handle returned by the breakpoint or tracepoint module when we
651 inserted the trap or jump, or hooked into a static tracepoint.
652 NULL if we haven't inserted it yet. */
658 #ifndef IN_PROCESS_AGENT
660 /* Given `while-stepping', a thread may be collecting data for more
661 than one tracepoint simultaneously. On the other hand, the same
662 tracepoint with a while-stepping action may be hit by more than one
663 thread simultaneously (but not quite, each thread could be handling
664 a different step). Each thread holds a list of these objects,
665 representing the current step of each while-stepping action being
670 struct wstep_state
*next
;
672 /* The tracepoint number. */
674 /* The tracepoint's address. */
675 CORE_ADDR tp_address
;
677 /* The number of the current step in this 'while-stepping'
684 /* The linked list of all tracepoints. Marked explicitly as used as
685 the in-process library doesn't use it for the fast tracepoints
687 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
689 #ifndef IN_PROCESS_AGENT
691 /* Pointer to the last tracepoint in the list, new tracepoints are
692 linked in at the end. */
694 static struct tracepoint
*last_tracepoint
;
697 /* The first tracepoint to exceed its pass count. */
699 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
701 /* True if the trace buffer is full or otherwise no longer usable. */
703 IP_AGENT_EXPORT
int trace_buffer_is_full
;
705 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
707 #ifndef IN_PROCESS_AGENT
709 static const char *eval_result_names
[] =
711 "terror:in the attic", /* this should never be reported */
712 "terror:empty expression",
713 "terror:empty stack",
714 "terror:stack overflow",
715 "terror:stack underflow",
716 "terror:unhandled opcode",
717 "terror:unrecognized opcode",
718 "terror:divide by zero"
723 /* The tracepoint in which the error occurred. */
725 static struct tracepoint
*error_tracepoint
;
727 struct trace_state_variable
729 /* This is the name of the variable as used in GDB. The target
730 doesn't use the name, but needs to have it for saving and
731 reconnection purposes. */
734 /* This number identifies the variable uniquely. Numbers may be
735 assigned either by the target (in the case of builtin variables),
736 or by GDB, and are presumed unique during the course of a trace
740 /* The variable's initial value, a 64-bit signed integer always. */
741 LONGEST initial_value
;
743 /* The variable's value, a 64-bit signed integer always. */
746 /* Pointer to a getter function, used to supply computed values. */
747 LONGEST (*getter
) (void);
749 /* Link to the next variable. */
750 struct trace_state_variable
*next
;
753 /* Linked list of all trace state variables. */
755 #ifdef IN_PROCESS_AGENT
756 struct trace_state_variable
*alloced_trace_state_variables
;
759 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
761 /* The results of tracing go into a fixed-size space known as the
762 "trace buffer". Because usage follows a limited number of
763 patterns, we manage it ourselves rather than with malloc. Basic
764 rules are that we create only one trace frame at a time, each is
765 variable in size, they are never moved once created, and we only
766 discard if we are doing a circular buffer, and then only the oldest
767 ones. Each trace frame includes its own size, so we don't need to
768 link them together, and the trace frame number is relative to the
769 first one, so we don't need to record numbers. A trace frame also
770 records the number of the tracepoint that created it. The data
771 itself is a series of blocks, each introduced by a single character
772 and with a defined format. Each type of block has enough
773 type/length info to allow scanners to jump quickly from one block
774 to the next without reading each byte in the block. */
776 /* Trace buffer management would be simple - advance a free pointer
777 from beginning to end, then stop - were it not for the circular
778 buffer option, which is a useful way to prevent a trace run from
779 stopping prematurely because the buffer filled up. In the circular
780 case, the location of the first trace frame (trace_buffer_start)
781 moves as old trace frames are discarded. Also, since we grow trace
782 frames incrementally as actions are performed, we wrap around to
783 the beginning of the trace buffer. This is per-block, so each
784 block within a trace frame remains contiguous. Things get messy
785 when the wrapped-around trace frame is the one being discarded; the
786 free space ends up in two parts at opposite ends of the buffer. */
789 # if defined(__GNUC__)
790 # define ATTR_PACKED __attribute__ ((packed))
792 # define ATTR_PACKED /* nothing */
796 /* The data collected at a tracepoint hit. This object should be as
797 small as possible, since there may be a great many of them. We do
798 not need to keep a frame number, because they are all sequential
799 and there are no deletions; so the Nth frame in the buffer is
800 always frame number N. */
804 /* Number of the tracepoint that collected this traceframe. A value
805 of 0 indicates the current end of the trace buffer. We make this
806 a 16-bit field because it's never going to happen that GDB's
807 numbering of tracepoints reaches 32,000. */
810 /* The size of the data in this trace frame. We limit this to 32
811 bits, even on a 64-bit target, because it's just implausible that
812 one is validly going to collect 4 gigabytes of data at a single
814 unsigned int data_size
: 32;
816 /* The base of the trace data, which is contiguous from this point. */
817 unsigned char data
[0];
821 /* The traceframe to be used as the source of data to send back to
822 GDB. A value of -1 means to get data from the live program. */
824 int current_traceframe
= -1;
826 /* This flag is true if the trace buffer is circular, meaning that
827 when it fills, the oldest trace frames are discarded in order to
830 #ifndef IN_PROCESS_AGENT
831 static int circular_trace_buffer
;
834 /* Pointer to the block of memory that traceframes all go into. */
836 static unsigned char *trace_buffer_lo
;
838 /* Pointer to the end of the trace buffer, more precisely to the byte
839 after the end of the buffer. */
841 static unsigned char *trace_buffer_hi
;
843 /* Control structure holding the read/write/etc. pointers into the
844 trace buffer. We need more than one of these to implement a
845 transaction-like mechanism to garantees that both GDBserver and the
846 in-process agent can try to change the trace buffer
849 struct trace_buffer_control
851 /* Pointer to the first trace frame in the buffer. In the
852 non-circular case, this is equal to trace_buffer_lo, otherwise it
853 moves around in the buffer. */
854 unsigned char *start
;
856 /* Pointer to the free part of the trace buffer. Note that we clear
857 several bytes at and after this pointer, so that traceframe
858 scans/searches terminate properly. */
861 /* Pointer to the byte after the end of the free part. Note that
862 this may be smaller than trace_buffer_free in the circular case,
863 and means that the free part is in two pieces. Initially it is
864 equal to trace_buffer_hi, then is generally equivalent to
865 trace_buffer_start. */
866 unsigned char *end_free
;
868 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
869 this is the point at which the trace data breaks, and resumes at
874 /* Same as above, to be used by GDBserver when updating the in-process
876 struct ipa_trace_buffer_control
885 /* We have possibly both GDBserver and an inferior thread accessing
886 the same IPA trace buffer memory. The IPA is the producer (tries
887 to put new frames in the buffer), while GDBserver occasionally
888 consumes them, that is, flushes the IPA's buffer into its own
889 buffer. Both sides need to update the trace buffer control
890 pointers (current head, tail, etc.). We can't use a global lock to
891 synchronize the accesses, as otherwise we could deadlock GDBserver
892 (if the thread holding the lock stops for a signal, say). So
893 instead of that, we use a transaction scheme where GDBserver writes
894 always prevail over the IPAs writes, and, we have the IPA detect
895 the commit failure/overwrite, and retry the whole attempt. This is
896 mainly implemented by having a global token object that represents
897 who wrote last to the buffer control structure. We need to freeze
898 any inferior writing to the buffer while GDBserver touches memory,
899 so that the inferior can correctly detect that GDBserver had been
900 there, otherwise, it could mistakingly think its commit was
901 successful; that's implemented by simply having GDBserver set a
902 breakpoint the inferior hits if it is the critical region.
904 There are three cycling trace buffer control structure copies
905 (buffer head, tail, etc.), with the token object including an index
906 indicating which is current live copy. The IPA tentatively builds
907 an updated copy in a non-current control structure, while GDBserver
908 always clobbers the current version directly. The IPA then tries
909 to atomically "commit" its version; if GDBserver clobbered the
910 structure meanwhile, that will fail, and the IPA restarts the
913 Listing the step in further detail, we have:
915 In-process agent (producer):
917 - passes by `about_to_request_buffer_space' breakpoint/lock
919 - reads current token, extracts current trace buffer control index,
920 and starts tentatively updating the rightmost one (0->1, 1->2,
921 2->0). Note that only one inferior thread is executing this code
922 at any given time, due to an outer lock in the jump pads.
924 - updates counters, and tries to commit the token.
926 - passes by second `about_to_request_buffer_space' breakpoint/lock,
927 leaving the sync region.
929 - checks if the update was effective.
931 - if trace buffer was found full, hits flush_trace_buffer
932 breakpoint, and restarts later afterwards.
934 GDBserver (consumer):
936 - sets `about_to_request_buffer_space' breakpoint/lock.
938 - updates the token unconditionally, using the current buffer
939 control index, since it knows that the IP agent always writes to
940 the rightmost, and due to the breakpoint, at most one IP thread
941 can try to update the trace buffer concurrently to GDBserver, so
942 there will be no danger of trace buffer control index wrap making
943 the IPA write to the same index as GDBserver.
945 - flushes the IP agent's trace buffer completely, and updates the
946 current trace buffer control structure. GDBserver *always* wins.
948 - removes the `about_to_request_buffer_space' breakpoint.
950 The token is stored in the `trace_buffer_ctrl_curr' variable.
951 Internally, it's bits are defined as:
953 |-------------+-----+-------------+--------+-------------+--------------|
954 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
955 |-------------+-----+-------------+--------+-------------+--------------|
956 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
957 |-------------+-----+-------------+--------+-------------+--------------|
959 GSB - GDBserver Stamp Bit
960 PC - Previous Counter
962 TBCI - Trace Buffer Control Index
965 An IPA update of `trace_buffer_ctrl_curr' does:
967 - read CC from the current token, save as PC.
969 - atomically tries to write PC+1,CC
971 A GDBserver update of `trace_buffer_ctrl_curr' does:
973 - reads PC and CC from the current token.
978 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
979 for the counters described below. The cleared bits are used to
980 hold the index of the items of the `trace_buffer_ctrl' array that
982 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
984 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
985 counter, and the `current' counter. */
987 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
988 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
990 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
991 always stamps this bit as set. */
992 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
994 #ifdef IN_PROCESS_AGENT
995 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
996 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
998 # define TRACE_BUFFER_CTRL_CURR \
999 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1003 /* The GDBserver side agent only needs one instance of this object, as
1004 it doesn't need to sync with itself. Define it as array anyway so
1005 that the rest of the code base doesn't need to care for the
1007 struct trace_buffer_control trace_buffer_ctrl
[1];
1008 # define TRACE_BUFFER_CTRL_CURR 0
1011 /* These are convenience macros used to access the current trace
1012 buffer control in effect. */
1013 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1014 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1015 #define trace_buffer_end_free \
1016 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1017 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1020 /* Macro that returns a pointer to the first traceframe in the buffer. */
1022 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1024 /* Macro that returns a pointer to the next traceframe in the buffer.
1025 If the computed location is beyond the wraparound point, subtract
1026 the offset of the wraparound. */
1028 #define NEXT_TRACEFRAME_1(TF) \
1029 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1031 #define NEXT_TRACEFRAME(TF) \
1032 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1033 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1034 ? (trace_buffer_wrap - trace_buffer_lo) \
1037 /* The difference between these counters represents the total number
1038 of complete traceframes present in the trace buffer. The IP agent
1039 writes to the write count, GDBserver writes to read count. */
1041 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1042 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1044 /* Convenience macro. */
1046 #define traceframe_count \
1047 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1049 /* The count of all traceframes created in the current run, including
1050 ones that were discarded to make room. */
1052 IP_AGENT_EXPORT
int traceframes_created
;
1054 #ifndef IN_PROCESS_AGENT
1056 /* Read-only regions are address ranges whose contents don't change,
1057 and so can be read from target memory even while looking at a trace
1058 frame. Without these, disassembly for instance will likely fail,
1059 because the program code is not usually collected into a trace
1060 frame. This data structure does not need to be very complicated or
1061 particularly efficient, it's only going to be used occasionally,
1062 and only by some commands. */
1064 struct readonly_region
1066 /* The bounds of the region. */
1067 CORE_ADDR start
, end
;
1069 /* Link to the next one. */
1070 struct readonly_region
*next
;
1073 /* Linked list of readonly regions. This list stays in effect from
1074 one tstart to the next. */
1076 static struct readonly_region
*readonly_regions
;
1080 /* The global that controls tracing overall. */
1082 IP_AGENT_EXPORT
int tracing
;
1084 #ifndef IN_PROCESS_AGENT
1086 /* Controls whether tracing should continue after GDB disconnects. */
1088 int disconnected_tracing
;
1090 /* The reason for the last tracing run to have stopped. We initialize
1091 to a distinct string so that GDB can distinguish between "stopped
1092 after running" and "stopped because never run" cases. */
1094 static const char *tracing_stop_reason
= "tnotrun";
1096 static int tracing_stop_tpnum
;
1098 /* 64-bit timestamps for the trace run's start and finish, expressed
1099 in microseconds from the Unix epoch. */
1101 LONGEST tracing_start_time
;
1102 LONGEST tracing_stop_time
;
1104 /* The (optional) user-supplied name of the user that started the run.
1105 This is an arbitrary string, and may be NULL. */
1107 char *tracing_user_name
;
1109 /* Optional user-supplied text describing the run. This is
1110 an arbitrary string, and may be NULL. */
1112 char *tracing_notes
;
1114 /* Optional user-supplied text explaining a tstop command. This is an
1115 arbitrary string, and may be NULL. */
1117 char *tracing_stop_note
;
1121 /* Functions local to this file. */
1123 /* Base "class" for tracepoint type specific data to be passed down to
1124 collect_data_at_tracepoint. */
1125 struct tracepoint_hit_ctx
1127 enum tracepoint_type type
;
1130 #ifdef IN_PROCESS_AGENT
1132 /* Fast/jump tracepoint specific data to be passed down to
1133 collect_data_at_tracepoint. */
1134 struct fast_tracepoint_ctx
1136 struct tracepoint_hit_ctx base
;
1138 struct regcache regcache
;
1139 int regcache_initted
;
1140 unsigned char *regspace
;
1142 unsigned char *regs
;
1143 struct tracepoint
*tpoint
;
1146 /* Static tracepoint specific data to be passed down to
1147 collect_data_at_tracepoint. */
1148 struct static_tracepoint_ctx
1150 struct tracepoint_hit_ctx base
;
1152 /* The regcache corresponding to the registers state at the time of
1153 the tracepoint hit. Initialized lazily, from REGS. */
1154 struct regcache regcache
;
1155 int regcache_initted
;
1157 /* The buffer space REGCACHE above uses. We use a separate buffer
1158 instead of letting the regcache malloc for both signal safety and
1159 performance reasons; this is allocated on the stack instead. */
1160 unsigned char *regspace
;
1162 /* The register buffer as passed on by lttng/ust. */
1163 struct registers
*regs
;
1165 /* The "printf" formatter and the args the user passed to the marker
1166 call. We use this to be able to collect "static trace data"
1171 /* The GDB tracepoint matching the probed marker that was "hit". */
1172 struct tracepoint
*tpoint
;
1177 /* Static tracepoint specific data to be passed down to
1178 collect_data_at_tracepoint. */
1179 struct trap_tracepoint_ctx
1181 struct tracepoint_hit_ctx base
;
1183 struct regcache
*regcache
;
1188 static enum eval_result_type
1189 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1190 struct traceframe
*tframe
,
1191 struct agent_expr
*aexpr
,
1194 #ifndef IN_PROCESS_AGENT
1195 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1196 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1199 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1200 struct tracepoint
*tpoint
);
1202 #ifndef IN_PROCESS_AGENT
1203 static void clear_readonly_regions (void);
1204 static void clear_installed_tracepoints (void);
1207 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1209 struct tracepoint
*tpoint
);
1210 #ifndef IN_PROCESS_AGENT
1211 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1213 struct tracepoint
*tpoint
, int current_step
);
1214 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1215 CORE_ADDR
*jump_entry
);
1217 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1219 struct tracepoint
*tpoint
,
1220 struct traceframe
*tframe
,
1221 struct tracepoint_action
*taction
);
1223 #ifndef IN_PROCESS_AGENT
1224 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1226 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1227 static void download_tracepoint (struct tracepoint
*);
1228 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1231 static LONGEST
get_timestamp (void);
1233 #if defined(__GNUC__)
1234 # define memory_barrier() asm volatile ("" : : : "memory")
1236 # define memory_barrier() do {} while (0)
1239 /* We only build the IPA if this builtin is supported, and there are
1240 no uses of this in GDBserver itself, so we're safe in defining this
1242 #define cmpxchg(mem, oldval, newval) \
1243 __sync_val_compare_and_swap (mem, oldval, newval)
1245 /* The size in bytes of the buffer used to talk to the IPA helper
1247 #define CMD_BUF_SIZE 1024
1249 /* Record that an error occurred during expression evaluation. */
1252 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1253 enum eval_result_type rtype
)
1255 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1256 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1258 #ifdef IN_PROCESS_AGENT
1259 /* Only record the first error we get. */
1260 if (cmpxchg (&expr_eval_result
,
1262 rtype
) != expr_eval_no_error
)
1265 if (expr_eval_result
!= expr_eval_no_error
)
1269 error_tracepoint
= tpoint
;
1272 /* Trace buffer management. */
1275 clear_trace_buffer (void)
1277 trace_buffer_start
= trace_buffer_lo
;
1278 trace_buffer_free
= trace_buffer_lo
;
1279 trace_buffer_end_free
= trace_buffer_hi
;
1280 trace_buffer_wrap
= trace_buffer_hi
;
1281 /* A traceframe with zeroed fields marks the end of trace data. */
1282 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1283 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1284 traceframe_read_count
= traceframe_write_count
= 0;
1285 traceframes_created
= 0;
1288 #ifndef IN_PROCESS_AGENT
1291 clear_inferior_trace_buffer (void)
1293 CORE_ADDR ipa_trace_buffer_lo
;
1294 CORE_ADDR ipa_trace_buffer_hi
;
1295 struct traceframe ipa_traceframe
= { 0 };
1296 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1298 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1299 &ipa_trace_buffer_lo
);
1300 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1301 &ipa_trace_buffer_hi
);
1303 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1304 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1305 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1306 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1308 /* A traceframe with zeroed fields marks the end of trace data. */
1309 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1310 (unsigned char *) &ipa_trace_buffer_ctrl
,
1311 sizeof (ipa_trace_buffer_ctrl
));
1313 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1315 /* A traceframe with zeroed fields marks the end of trace data. */
1316 write_inferior_memory (ipa_trace_buffer_lo
,
1317 (unsigned char *) &ipa_traceframe
,
1318 sizeof (ipa_traceframe
));
1320 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1321 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1322 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1328 init_trace_buffer (unsigned char *buf
, int bufsize
)
1330 trace_buffer_lo
= buf
;
1331 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1333 clear_trace_buffer ();
1336 #ifdef IN_PROCESS_AGENT
1338 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1339 about_to_request_buffer_space (void)
1341 /* GDBserver places breakpoint here while it goes about to flush
1342 data at random times. */
1343 UNKNOWN_SIDE_EFFECTS();
1348 /* Carve out a piece of the trace buffer, returning NULL in case of
1352 trace_buffer_alloc (size_t amt
)
1354 unsigned char *rslt
;
1355 struct trace_buffer_control
*tbctrl
;
1357 #ifdef IN_PROCESS_AGENT
1358 unsigned int prev
, prev_filtered
;
1359 unsigned int commit_count
;
1360 unsigned int commit
;
1361 unsigned int readout
;
1363 struct traceframe
*oldest
;
1364 unsigned char *new_start
;
1367 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1368 (long) amt
, (long) sizeof (struct traceframe
));
1370 /* Account for the EOB marker. */
1371 amt
+= sizeof (struct traceframe
);
1373 #ifdef IN_PROCESS_AGENT
1377 /* Read the current token and extract the index to try to write to,
1378 storing it in CURR. */
1379 prev
= trace_buffer_ctrl_curr
;
1380 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1381 curr
= prev_filtered
+ 1;
1385 about_to_request_buffer_space ();
1387 /* Start out with a copy of the current state. GDBserver may be
1388 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1389 be able to commit anyway if that happens. */
1390 trace_buffer_ctrl
[curr
]
1391 = trace_buffer_ctrl
[prev_filtered
];
1392 trace_debug ("trying curr=%u", curr
);
1394 /* The GDBserver's agent doesn't need all that syncing, and always
1395 updates TCB 0 (there's only one, mind you). */
1398 tbctrl
= &trace_buffer_ctrl
[curr
];
1400 /* Offsets are easier to grok for debugging than raw addresses,
1401 especially for the small trace buffer sizes that are useful for
1403 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1405 (int) (tbctrl
->start
- trace_buffer_lo
),
1406 (int) (tbctrl
->free
- trace_buffer_lo
),
1407 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1408 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1409 (int) (trace_buffer_hi
- trace_buffer_lo
));
1411 /* The algorithm here is to keep trying to get a contiguous block of
1412 the requested size, possibly discarding older traceframes to free
1413 up space. Since free space might come in one or two pieces,
1414 depending on whether discarded traceframes wrapped around at the
1415 high end of the buffer, we test both pieces after each
1419 /* First, if we have two free parts, try the upper one first. */
1420 if (tbctrl
->end_free
< tbctrl
->free
)
1422 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1423 /* We have enough in the upper part. */
1427 /* Our high part of free space wasn't enough. Give up
1428 on it for now, set wraparound. We will recover the
1429 space later, if/when the wrapped-around traceframe is
1431 trace_debug ("Upper part too small, setting wraparound");
1432 tbctrl
->wrap
= tbctrl
->free
;
1433 tbctrl
->free
= trace_buffer_lo
;
1437 /* The normal case. */
1438 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1441 #ifdef IN_PROCESS_AGENT
1442 /* The IP Agent's buffer is always circular. It isn't used
1443 currently, but `circular_trace_buffer' could represent
1444 GDBserver's mode. If we didn't find space, ask GDBserver to
1447 flush_trace_buffer ();
1451 trace_debug ("gdbserver flushed buffer, retrying");
1455 /* GDBserver cancelled the tracing. Bail out as well. */
1458 /* If we're here, then neither part is big enough, and
1459 non-circular trace buffers are now full. */
1460 if (!circular_trace_buffer
)
1462 trace_debug ("Not enough space in the trace buffer");
1466 trace_debug ("Need more space in the trace buffer");
1468 /* If we have a circular buffer, we can try discarding the
1469 oldest traceframe and see if that helps. */
1470 oldest
= FIRST_TRACEFRAME ();
1471 if (oldest
->tpnum
== 0)
1473 /* Not good; we have no traceframes to free. Perhaps we're
1474 asking for a block that is larger than the buffer? In
1475 any case, give up. */
1476 trace_debug ("No traceframes to discard");
1480 /* We don't run this code in the in-process agent currently.
1481 E.g., we could leave the in-process agent in autonomous
1482 circular mode if we only have fast tracepoints. If we do
1483 that, then this bit becomes racy with GDBserver, which also
1484 writes to this counter. */
1485 --traceframe_write_count
;
1487 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1488 /* If we freed the traceframe that wrapped around, go back
1489 to the non-wrap case. */
1490 if (new_start
< tbctrl
->start
)
1492 trace_debug ("Discarding past the wraparound");
1493 tbctrl
->wrap
= trace_buffer_hi
;
1495 tbctrl
->start
= new_start
;
1496 tbctrl
->end_free
= tbctrl
->start
;
1498 trace_debug ("Discarded a traceframe\n"
1499 "Trace buffer [%d], start=%d free=%d "
1500 "endfree=%d wrap=%d hi=%d",
1502 (int) (tbctrl
->start
- trace_buffer_lo
),
1503 (int) (tbctrl
->free
- trace_buffer_lo
),
1504 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1505 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1506 (int) (trace_buffer_hi
- trace_buffer_lo
));
1508 /* Now go back around the loop. The discard might have resulted
1509 in either one or two pieces of free space, so we want to try
1510 both before freeing any more traceframes. */
1514 /* If we get here, we know we can provide the asked-for space. */
1516 rslt
= tbctrl
->free
;
1518 /* Adjust the request back down, now that we know we have space for
1519 the marker, but don't commit to AMT yet, we may still need to
1520 restart the operation if GDBserver touches the trace buffer
1521 (obviously only important in the in-process agent's version). */
1522 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1524 /* Or not. If GDBserver changed the trace buffer behind our back,
1525 we get to restart a new allocation attempt. */
1527 #ifdef IN_PROCESS_AGENT
1528 /* Build the tentative token. */
1529 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1530 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1531 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1535 /* Try to commit it. */
1536 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1537 if (readout
!= prev
)
1539 trace_debug ("GDBserver has touched the trace buffer, restarting."
1540 " (prev=%08x, commit=%08x, readout=%08x)",
1541 prev
, commit
, readout
);
1545 /* Hold your horses here. Even if that change was committed,
1546 GDBserver could come in, and clobber it. We need to hold to be
1547 able to tell if GDBserver clobbers before or after we committed
1548 the change. Whenever GDBserver goes about touching the IPA
1549 buffer, it sets a breakpoint in this routine, so we have a sync
1551 about_to_request_buffer_space ();
1553 /* Check if the change has been effective, even if GDBserver stopped
1554 us at the breakpoint. */
1557 unsigned int refetch
;
1561 refetch
= trace_buffer_ctrl_curr
;
1563 if (refetch
== commit
1564 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1567 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1568 "readout=%08x, refetch=%08x)",
1569 prev
, commit
, readout
, refetch
);
1573 trace_debug ("GDBserver has touched the trace buffer, not effective."
1574 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1575 prev
, commit
, readout
, refetch
);
1581 /* We have a new piece of the trace buffer. Hurray! */
1583 /* Add an EOB marker just past this allocation. */
1584 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1585 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1587 /* Adjust the request back down, now that we know we have space for
1589 amt
-= sizeof (struct traceframe
);
1593 trace_debug ("Allocated %d bytes", (int) amt
);
1594 trace_debug ("Trace buffer [%d] start=%d free=%d "
1595 "endfree=%d wrap=%d hi=%d",
1597 (int) (tbctrl
->start
- trace_buffer_lo
),
1598 (int) (tbctrl
->free
- trace_buffer_lo
),
1599 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1600 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1601 (int) (trace_buffer_hi
- trace_buffer_lo
));
1607 #ifndef IN_PROCESS_AGENT
1609 /* Return the total free space. This is not necessarily the largest
1610 block we can allocate, because of the two-part case. */
1615 if (trace_buffer_free
<= trace_buffer_end_free
)
1616 return trace_buffer_end_free
- trace_buffer_free
;
1618 return ((trace_buffer_end_free
- trace_buffer_lo
)
1619 + (trace_buffer_hi
- trace_buffer_free
));
1622 /* An 'S' in continuation packets indicates remainder are for
1625 static int seen_step_action_flag
;
1627 /* Create a tracepoint (location) with given number and address. Add this
1628 new tracepoint to list and sort this list. */
1630 static struct tracepoint
*
1631 add_tracepoint (int num
, CORE_ADDR addr
)
1633 struct tracepoint
*tpoint
, **tp_next
;
1635 tpoint
= xmalloc (sizeof (struct tracepoint
));
1636 tpoint
->number
= num
;
1637 tpoint
->address
= addr
;
1638 tpoint
->numactions
= 0;
1639 tpoint
->actions
= NULL
;
1640 tpoint
->actions_str
= NULL
;
1641 tpoint
->cond
= NULL
;
1642 tpoint
->num_step_actions
= 0;
1643 tpoint
->step_actions
= NULL
;
1644 tpoint
->step_actions_str
= NULL
;
1645 /* Start all off as regular (slow) tracepoints. */
1646 tpoint
->type
= trap_tracepoint
;
1647 tpoint
->orig_size
= -1;
1648 tpoint
->source_strings
= NULL
;
1649 tpoint
->compiled_cond
= 0;
1650 tpoint
->handle
= NULL
;
1651 tpoint
->next
= NULL
;
1653 /* Find a place to insert this tracepoint into list in order to keep
1654 the tracepoint list still in the ascending order. There may be
1655 multiple tracepoints at the same address as TPOINT's, and this
1656 guarantees TPOINT is inserted after all the tracepoints which are
1657 set at the same address. For example, fast tracepoints A, B, C are
1658 set at the same address, and D is to be insert at the same place as
1661 -->| A |--> | B |-->| C |->...
1663 One jump pad was created for tracepoint A, B, and C, and the target
1664 address of A is referenced/used in jump pad. So jump pad will let
1665 inferior jump to A. If D is inserted in front of A, like this,
1667 -->| D |-->| A |--> | B |-->| C |->...
1669 without updating jump pad, D is not reachable during collect, which
1670 is wrong. As we can see, the order of B, C and D doesn't matter, but
1671 A should always be the `first' one. */
1672 for (tp_next
= &tracepoints
;
1673 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1674 tp_next
= &(*tp_next
)->next
)
1676 tpoint
->next
= *tp_next
;
1678 last_tracepoint
= tpoint
;
1680 seen_step_action_flag
= 0;
1685 #ifndef IN_PROCESS_AGENT
1687 /* Return the tracepoint with the given number and address, or NULL. */
1689 static struct tracepoint
*
1690 find_tracepoint (int id
, CORE_ADDR addr
)
1692 struct tracepoint
*tpoint
;
1694 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1695 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1701 /* There may be several tracepoints with the same number (because they
1702 are "locations", in GDB parlance); return the next one after the
1703 given tracepoint, or search from the beginning of the list if the
1704 first argument is NULL. */
1706 static struct tracepoint
*
1707 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1709 struct tracepoint
*tpoint
;
1712 tpoint
= prev_tp
->next
;
1714 tpoint
= tracepoints
;
1715 for (; tpoint
; tpoint
= tpoint
->next
)
1716 if (tpoint
->number
== num
)
1725 save_string (const char *str
, size_t len
)
1729 s
= xmalloc (len
+ 1);
1730 memcpy (s
, str
, len
);
1736 /* Append another action to perform when the tracepoint triggers. */
1739 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1745 seen_step_action_flag
= 1;
1753 char *act_start
= act
;
1754 struct tracepoint_action
*action
= NULL
;
1760 struct collect_memory_action
*maction
;
1764 maction
= xmalloc (sizeof *maction
);
1765 maction
->base
.type
= *act
;
1766 action
= &maction
->base
;
1769 is_neg
= (*act
== '-');
1772 act
= unpack_varlen_hex (act
, &basereg
);
1774 act
= unpack_varlen_hex (act
, &maction
->addr
);
1776 act
= unpack_varlen_hex (act
, &maction
->len
);
1777 maction
->basereg
= (is_neg
1780 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1781 pulongest (maction
->len
),
1782 paddress (maction
->addr
), maction
->basereg
);
1787 struct collect_registers_action
*raction
;
1789 raction
= xmalloc (sizeof *raction
);
1790 raction
->base
.type
= *act
;
1791 action
= &raction
->base
;
1793 trace_debug ("Want to collect registers");
1795 /* skip past hex digits of mask for now */
1796 while (isxdigit(*act
))
1802 struct collect_static_trace_data_action
*raction
;
1804 raction
= xmalloc (sizeof *raction
);
1805 raction
->base
.type
= *act
;
1806 action
= &raction
->base
;
1808 trace_debug ("Want to collect static trace data");
1813 trace_debug ("Unexpected step action, ignoring");
1818 struct eval_expr_action
*xaction
;
1820 xaction
= xmalloc (sizeof (*xaction
));
1821 xaction
->base
.type
= *act
;
1822 action
= &xaction
->base
;
1824 trace_debug ("Want to evaluate expression");
1825 xaction
->expr
= gdb_parse_agent_expr (&act
);
1829 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1838 if (seen_step_action_flag
)
1840 tpoint
->num_step_actions
++;
1842 tpoint
->step_actions
1843 = xrealloc (tpoint
->step_actions
,
1844 (sizeof (*tpoint
->step_actions
)
1845 * tpoint
->num_step_actions
));
1846 tpoint
->step_actions_str
1847 = xrealloc (tpoint
->step_actions_str
,
1848 (sizeof (*tpoint
->step_actions_str
)
1849 * tpoint
->num_step_actions
));
1850 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1851 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1852 = save_string (act_start
, act
- act_start
);
1856 tpoint
->numactions
++;
1858 = xrealloc (tpoint
->actions
,
1859 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1861 = xrealloc (tpoint
->actions_str
,
1862 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1863 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1864 tpoint
->actions_str
[tpoint
->numactions
- 1]
1865 = save_string (act_start
, act
- act_start
);
1872 /* Find or create a trace state variable with the given number. */
1874 static struct trace_state_variable
*
1875 get_trace_state_variable (int num
)
1877 struct trace_state_variable
*tsv
;
1879 #ifdef IN_PROCESS_AGENT
1880 /* Search for an existing variable. */
1881 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1882 if (tsv
->number
== num
)
1886 /* Search for an existing variable. */
1887 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1888 if (tsv
->number
== num
)
1894 /* Find or create a trace state variable with the given number. */
1896 static struct trace_state_variable
*
1897 create_trace_state_variable (int num
, int gdb
)
1899 struct trace_state_variable
*tsv
;
1901 tsv
= get_trace_state_variable (num
);
1905 /* Create a new variable. */
1906 tsv
= xmalloc (sizeof (struct trace_state_variable
));
1908 tsv
->initial_value
= 0;
1912 #ifdef IN_PROCESS_AGENT
1915 tsv
->next
= alloced_trace_state_variables
;
1916 alloced_trace_state_variables
= tsv
;
1921 tsv
->next
= trace_state_variables
;
1922 trace_state_variables
= tsv
;
1927 IP_AGENT_EXPORT LONGEST
1928 get_trace_state_variable_value (int num
)
1930 struct trace_state_variable
*tsv
;
1932 tsv
= get_trace_state_variable (num
);
1936 trace_debug ("No trace state variable %d, skipping value get", num
);
1940 /* Call a getter function if we have one. While it's tempting to
1941 set up something to only call the getter once per tracepoint hit,
1942 it could run afoul of thread races. Better to let the getter
1943 handle it directly, if necessary to worry about it. */
1945 tsv
->value
= (tsv
->getter
) ();
1947 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1948 num
, plongest (tsv
->value
));
1953 IP_AGENT_EXPORT
void
1954 set_trace_state_variable_value (int num
, LONGEST val
)
1956 struct trace_state_variable
*tsv
;
1958 tsv
= get_trace_state_variable (num
);
1962 trace_debug ("No trace state variable %d, skipping value set", num
);
1970 agent_get_trace_state_variable_value (int num
)
1972 return get_trace_state_variable_value (num
);
1976 agent_set_trace_state_variable_value (int num
, LONGEST val
)
1978 set_trace_state_variable_value (num
, val
);
1982 set_trace_state_variable_name (int num
, const char *name
)
1984 struct trace_state_variable
*tsv
;
1986 tsv
= get_trace_state_variable (num
);
1990 trace_debug ("No trace state variable %d, skipping name set", num
);
1994 tsv
->name
= (char *) name
;
1998 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2000 struct trace_state_variable
*tsv
;
2002 tsv
= get_trace_state_variable (num
);
2006 trace_debug ("No trace state variable %d, skipping getter set", num
);
2010 tsv
->getter
= getter
;
2013 /* Add a raw traceframe for the given tracepoint. */
2015 static struct traceframe
*
2016 add_traceframe (struct tracepoint
*tpoint
)
2018 struct traceframe
*tframe
;
2020 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2025 tframe
->tpnum
= tpoint
->number
;
2026 tframe
->data_size
= 0;
2031 /* Add a block to the traceframe currently being worked on. */
2033 static unsigned char *
2034 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2036 unsigned char *block
;
2041 block
= trace_buffer_alloc (amt
);
2046 tframe
->data_size
+= amt
;
2051 /* Flag that the current traceframe is finished. */
2054 finish_traceframe (struct traceframe
*tframe
)
2056 ++traceframe_write_count
;
2057 ++traceframes_created
;
2060 #ifndef IN_PROCESS_AGENT
2062 /* Given a traceframe number NUM, find the NUMth traceframe in the
2065 static struct traceframe
*
2066 find_traceframe (int num
)
2068 struct traceframe
*tframe
;
2071 for (tframe
= FIRST_TRACEFRAME ();
2073 tframe
= NEXT_TRACEFRAME (tframe
))
2084 get_traceframe_address (struct traceframe
*tframe
)
2087 struct tracepoint
*tpoint
;
2089 addr
= traceframe_get_pc (tframe
);
2094 /* Fallback strategy, will be incorrect for while-stepping frames
2095 and multi-location tracepoints. */
2096 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2097 return tpoint
->address
;
2100 /* Search for the next traceframe whose address is inside or outside
2103 static struct traceframe
*
2104 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2107 struct traceframe
*tframe
;
2110 *tfnump
= current_traceframe
+ 1;
2111 tframe
= find_traceframe (*tfnump
);
2112 /* The search is not supposed to wrap around. */
2119 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2121 tfaddr
= get_traceframe_address (tframe
);
2123 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2124 : (lo
> tfaddr
|| tfaddr
> hi
))
2133 /* Search for the next traceframe recorded by the given tracepoint.
2134 Note that for multi-location tracepoints, this will find whatever
2135 location appears first. */
2137 static struct traceframe
*
2138 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2140 struct traceframe
*tframe
;
2142 *tfnump
= current_traceframe
+ 1;
2143 tframe
= find_traceframe (*tfnump
);
2144 /* The search is not supposed to wrap around. */
2151 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2153 if (tframe
->tpnum
== num
)
2164 #ifndef IN_PROCESS_AGENT
2166 /* Clear all past trace state. */
2169 cmd_qtinit (char *packet
)
2171 struct trace_state_variable
*tsv
, *prev
, *next
;
2173 /* Make sure we don't try to read from a trace frame. */
2174 current_traceframe
= -1;
2176 trace_debug ("Initializing the trace");
2178 clear_installed_tracepoints ();
2179 clear_readonly_regions ();
2182 last_tracepoint
= NULL
;
2184 /* Clear out any leftover trace state variables. Ones with target
2185 defined getters should be kept however. */
2187 tsv
= trace_state_variables
;
2190 trace_debug ("Looking at var %d", tsv
->number
);
2191 if (tsv
->getter
== NULL
)
2197 trace_state_variables
= next
;
2198 trace_debug ("Deleting var %d", tsv
->number
);
2209 clear_trace_buffer ();
2210 clear_inferior_trace_buffer ();
2215 /* Unprobe the UST marker at ADDRESS. */
2218 unprobe_marker_at (CORE_ADDR address
)
2220 char cmd
[CMD_BUF_SIZE
];
2222 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2223 run_inferior_command (cmd
);
2226 /* Restore the program to its pre-tracing state. This routine may be called
2227 in error situations, so it needs to be careful about only restoring
2228 from known-valid bits. */
2231 clear_installed_tracepoints (void)
2233 struct tracepoint
*tpoint
;
2234 struct tracepoint
*prev_stpoint
;
2237 cancel_breakpoints ();
2239 prev_stpoint
= NULL
;
2241 /* Restore any bytes overwritten by tracepoints. */
2242 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2244 /* Catch the case where we might try to remove a tracepoint that
2245 was never actually installed. */
2246 if (tpoint
->handle
== NULL
)
2248 trace_debug ("Tracepoint %d at 0x%s was "
2249 "never installed, nothing to clear",
2250 tpoint
->number
, paddress (tpoint
->address
));
2254 switch (tpoint
->type
)
2256 case trap_tracepoint
:
2257 delete_breakpoint (tpoint
->handle
);
2259 case fast_tracepoint
:
2260 delete_fast_tracepoint_jump (tpoint
->handle
);
2262 case static_tracepoint
:
2263 if (prev_stpoint
!= NULL
2264 && prev_stpoint
->address
== tpoint
->address
)
2265 /* Nothing to do. We already unprobed a tracepoint set at
2266 this marker address (and there can only be one probe
2271 unprobe_marker_at (tpoint
->address
);
2272 prev_stpoint
= tpoint
;
2277 tpoint
->handle
= NULL
;
2283 /* Parse a packet that defines a tracepoint. */
2286 cmd_qtdp (char *own_buf
)
2289 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2290 int trail_hyphen
= 0;
2294 struct tracepoint
*tpoint
;
2296 char *packet
= own_buf
;
2298 packet
+= strlen ("QTDP:");
2300 /* A hyphen at the beginning marks a packet specifying actions for a
2301 tracepoint already supplied. */
2308 packet
= unpack_varlen_hex (packet
, &num
);
2309 ++packet
; /* skip a colon */
2310 packet
= unpack_varlen_hex (packet
, &addr
);
2311 ++packet
; /* skip a colon */
2313 /* See if we already have this tracepoint. */
2314 tpoint
= find_tracepoint (num
, addr
);
2318 /* Duplicate tracepoints are never allowed. */
2321 trace_debug ("Tracepoint error: tracepoint %d"
2322 " at 0x%s already exists",
2323 (int) num
, paddress (addr
));
2324 write_enn (own_buf
);
2328 tpoint
= add_tracepoint (num
, addr
);
2330 tpoint
->enabled
= (*packet
== 'E');
2331 ++packet
; /* skip 'E' */
2332 ++packet
; /* skip a colon */
2333 packet
= unpack_varlen_hex (packet
, &count
);
2334 tpoint
->step_count
= count
;
2335 ++packet
; /* skip a colon */
2336 packet
= unpack_varlen_hex (packet
, &count
);
2337 tpoint
->pass_count
= count
;
2338 /* See if we have any of the additional optional fields. */
2339 while (*packet
== ':')
2344 tpoint
->type
= fast_tracepoint
;
2346 packet
= unpack_varlen_hex (packet
, &count
);
2347 tpoint
->orig_size
= count
;
2349 else if (*packet
== 'S')
2351 tpoint
->type
= static_tracepoint
;
2354 else if (*packet
== 'X')
2356 actparm
= (char *) packet
;
2357 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2360 else if (*packet
== '-')
2362 else if (*packet
== '\0')
2365 trace_debug ("Unknown optional tracepoint field");
2370 trace_debug ("Also has actions\n");
2373 trace_debug ("Defined %stracepoint %d at 0x%s, "
2374 "enabled %d step %ld pass %ld",
2375 tpoint
->type
== fast_tracepoint
? "fast "
2376 : tpoint
->type
== static_tracepoint
? "static " : "",
2377 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2378 tpoint
->step_count
, tpoint
->pass_count
);
2381 add_tracepoint_action (tpoint
, packet
);
2384 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2385 (int) num
, paddress (addr
));
2386 write_enn (own_buf
);
2390 /* Install tracepoint during tracing only once for each tracepoint location.
2391 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2392 determine the last QTDP packet for one tracepoint location by checking
2393 trailing hyphen in QTDP packet. */
2394 if (tracing
&& !trail_hyphen
)
2396 /* Pause all threads temporarily while we patch tracepoints. */
2399 /* download_tracepoint will update global `tracepoints'
2400 list, so it is unsafe to leave threads in jump pad. */
2401 stabilize_threads ();
2403 /* Freeze threads. */
2406 download_tracepoint (tpoint
);
2407 install_tracepoint (tpoint
, own_buf
);
2417 cmd_qtdpsrc (char *own_buf
)
2419 ULONGEST num
, addr
, start
, slen
;
2420 struct tracepoint
*tpoint
;
2421 char *packet
= own_buf
;
2422 char *saved
, *srctype
, *src
;
2424 struct source_string
*last
, *newlast
;
2426 packet
+= strlen ("QTDPsrc:");
2428 packet
= unpack_varlen_hex (packet
, &num
);
2429 ++packet
; /* skip a colon */
2430 packet
= unpack_varlen_hex (packet
, &addr
);
2431 ++packet
; /* skip a colon */
2433 /* See if we already have this tracepoint. */
2434 tpoint
= find_tracepoint (num
, addr
);
2438 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2439 (int) num
, paddress (addr
));
2440 write_enn (own_buf
);
2445 packet
= strchr (packet
, ':');
2446 srctype
= xmalloc (packet
- saved
+ 1);
2447 memcpy (srctype
, saved
, packet
- saved
);
2448 srctype
[packet
- saved
] = '\0';
2450 packet
= unpack_varlen_hex (packet
, &start
);
2451 ++packet
; /* skip a colon */
2452 packet
= unpack_varlen_hex (packet
, &slen
);
2453 ++packet
; /* skip a colon */
2454 src
= xmalloc (slen
+ 1);
2455 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2458 newlast
= xmalloc (sizeof (struct source_string
));
2459 newlast
->type
= srctype
;
2461 newlast
->next
= NULL
;
2462 /* Always add a source string to the end of the list;
2463 this keeps sequences of actions/commands in the right
2465 if (tpoint
->source_strings
)
2467 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2469 last
->next
= newlast
;
2472 tpoint
->source_strings
= newlast
;
2478 cmd_qtdv (char *own_buf
)
2480 ULONGEST num
, val
, builtin
;
2483 struct trace_state_variable
*tsv
;
2484 char *packet
= own_buf
;
2486 packet
+= strlen ("QTDV:");
2488 packet
= unpack_varlen_hex (packet
, &num
);
2489 ++packet
; /* skip a colon */
2490 packet
= unpack_varlen_hex (packet
, &val
);
2491 ++packet
; /* skip a colon */
2492 packet
= unpack_varlen_hex (packet
, &builtin
);
2493 ++packet
; /* skip a colon */
2495 nbytes
= strlen (packet
) / 2;
2496 varname
= xmalloc (nbytes
+ 1);
2497 nbytes
= unhexify (varname
, packet
, nbytes
);
2498 varname
[nbytes
] = '\0';
2500 tsv
= create_trace_state_variable (num
, 1);
2501 tsv
->initial_value
= (LONGEST
) val
;
2502 tsv
->name
= varname
;
2504 set_trace_state_variable_value (num
, (LONGEST
) val
);
2510 cmd_qtenable_disable (char *own_buf
, int enable
)
2512 char *packet
= own_buf
;
2514 struct tracepoint
*tp
;
2516 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2517 packet
= unpack_varlen_hex (packet
, &num
);
2518 ++packet
; /* skip a colon */
2519 packet
= unpack_varlen_hex (packet
, &addr
);
2521 tp
= find_tracepoint (num
, addr
);
2525 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2527 trace_debug ("Tracepoint %d at 0x%s is already %s",
2528 (int) num
, paddress (addr
),
2529 enable
? "enabled" : "disabled");
2534 trace_debug ("%s tracepoint %d at 0x%s",
2535 enable
? "Enabling" : "Disabling",
2536 (int) num
, paddress (addr
));
2538 tp
->enabled
= enable
;
2540 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2543 int offset
= offsetof (struct tracepoint
, enabled
);
2544 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2546 ret
= prepare_to_access_memory ();
2549 trace_debug ("Failed to temporarily stop inferior threads");
2550 write_enn (own_buf
);
2554 ret
= write_inferior_integer (obj_addr
, enable
);
2555 done_accessing_memory ();
2559 trace_debug ("Cannot write enabled flag into "
2560 "inferior process memory");
2561 write_enn (own_buf
);
2570 trace_debug ("Tracepoint %d at 0x%s not found",
2571 (int) num
, paddress (addr
));
2572 write_enn (own_buf
);
2577 cmd_qtv (char *own_buf
)
2582 char *packet
= own_buf
;
2584 packet
+= strlen ("qTV:");
2585 unpack_varlen_hex (packet
, &num
);
2587 if (current_traceframe
>= 0)
2589 err
= traceframe_read_tsv ((int) num
, &val
);
2592 strcpy (own_buf
, "U");
2596 /* Only make tsv's be undefined before the first trace run. After a
2597 trace run is over, the user might want to see the last value of
2598 the tsv, and it might not be available in a traceframe. */
2599 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2601 strcpy (own_buf
, "U");
2605 val
= get_trace_state_variable_value (num
);
2607 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2610 /* Clear out the list of readonly regions. */
2613 clear_readonly_regions (void)
2615 struct readonly_region
*roreg
;
2617 while (readonly_regions
)
2619 roreg
= readonly_regions
;
2620 readonly_regions
= readonly_regions
->next
;
2625 /* Parse the collection of address ranges whose contents GDB believes
2626 to be unchanging and so can be read directly from target memory
2627 even while looking at a traceframe. */
2630 cmd_qtro (char *own_buf
)
2632 ULONGEST start
, end
;
2633 struct readonly_region
*roreg
;
2634 char *packet
= own_buf
;
2636 trace_debug ("Want to mark readonly regions");
2638 clear_readonly_regions ();
2640 packet
+= strlen ("QTro");
2642 while (*packet
== ':')
2644 ++packet
; /* skip a colon */
2645 packet
= unpack_varlen_hex (packet
, &start
);
2646 ++packet
; /* skip a comma */
2647 packet
= unpack_varlen_hex (packet
, &end
);
2648 roreg
= xmalloc (sizeof (struct readonly_region
));
2649 roreg
->start
= start
;
2651 roreg
->next
= readonly_regions
;
2652 readonly_regions
= roreg
;
2653 trace_debug ("Added readonly region from 0x%s to 0x%s",
2654 paddress (roreg
->start
), paddress (roreg
->end
));
2660 /* Test to see if the given range is in our list of readonly ranges.
2661 We only test for being entirely within a range, GDB is not going to
2662 send a single memory packet that spans multiple regions. */
2665 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2667 struct readonly_region
*roreg
;
2669 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2670 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2676 /* The maximum size of a jump pad entry. */
2677 static const int max_jump_pad_size
= 0x100;
2679 static CORE_ADDR gdb_jump_pad_head
;
2681 /* Return the address of the next free jump space. */
2684 get_jump_space_head (void)
2686 if (gdb_jump_pad_head
== 0)
2688 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2689 &gdb_jump_pad_head
))
2690 fatal ("error extracting jump_pad_buffer");
2693 return gdb_jump_pad_head
;
2696 /* Reserve USED bytes from the jump space. */
2699 claim_jump_space (ULONGEST used
)
2701 trace_debug ("claim_jump_space reserves %s bytes at %s",
2702 pulongest (used
), paddress (gdb_jump_pad_head
));
2703 gdb_jump_pad_head
+= used
;
2706 static CORE_ADDR trampoline_buffer_head
= 0;
2707 static CORE_ADDR trampoline_buffer_tail
;
2709 /* Reserve USED bytes from the trampoline buffer and return the
2710 address of the start of the reserved space in TRAMPOLINE. Returns
2711 non-zero if the space is successfully claimed. */
2714 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2716 if (!trampoline_buffer_head
)
2718 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2719 &trampoline_buffer_tail
))
2721 fatal ("error extracting trampoline_buffer");
2725 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2726 &trampoline_buffer_head
))
2728 fatal ("error extracting trampoline_buffer_end");
2733 /* Start claiming space from the top of the trampoline space. If
2734 the space is located at the bottom of the virtual address space,
2735 this reduces the possibility that corruption will occur if a null
2736 pointer is used to write to memory. */
2737 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2739 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2744 trampoline_buffer_head
-= used
;
2746 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2747 pulongest (used
), paddress (trampoline_buffer_head
));
2749 *trampoline
= trampoline_buffer_head
;
2753 /* Returns non-zero if there is space allocated for use in trampolines
2754 for fast tracepoints. */
2757 have_fast_tracepoint_trampoline_buffer (char *buf
)
2759 CORE_ADDR trampoline_end
, errbuf
;
2761 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2764 fatal ("error extracting trampoline_buffer_end");
2771 strcpy (buf
, "was claiming");
2772 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
2775 fatal ("error extracting errbuf");
2779 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
2782 return trampoline_end
!= 0;
2785 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2786 the command fails, or 0 otherwise. If the command ran
2787 successfully, but probing the marker failed, ERROUT will be filled
2788 with the error to reply to GDB, and -1 is also returned. This
2789 allows directly passing IPA errors to GDB. */
2792 probe_marker_at (CORE_ADDR address
, char *errout
)
2794 char cmd
[CMD_BUF_SIZE
];
2797 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
2798 err
= run_inferior_command (cmd
);
2804 strcpy (errout
, cmd
);
2813 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
2815 to
->jump_pad
= from
->jump_pad
;
2816 to
->jump_pad_end
= from
->jump_pad_end
;
2817 to
->trampoline
= from
->trampoline
;
2818 to
->trampoline_end
= from
->trampoline_end
;
2819 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
2820 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
2821 to
->handle
= from
->handle
;
2823 gdb_assert (from
->handle
);
2824 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
2827 #define MAX_JUMP_SIZE 20
2829 /* Install fast tracepoint. Return 0 if successful, otherwise return
2833 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
2835 CORE_ADDR jentry
, jump_entry
;
2836 CORE_ADDR trampoline
;
2837 ULONGEST trampoline_size
;
2839 /* The jump to the jump pad of the last fast tracepoint
2841 unsigned char fjump
[MAX_JUMP_SIZE
];
2842 ULONGEST fjump_size
;
2844 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
2846 trace_debug ("Requested a fast tracepoint on an instruction "
2847 "that is of less than the minimum length.");
2851 jentry
= jump_entry
= get_jump_space_head ();
2854 trampoline_size
= 0;
2856 /* Install the jump pad. */
2857 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
2859 ipa_sym_addrs
.addr_gdb_collect
,
2860 ipa_sym_addrs
.addr_collecting
,
2863 &trampoline
, &trampoline_size
,
2865 &tpoint
->adjusted_insn_addr
,
2866 &tpoint
->adjusted_insn_addr_end
,
2873 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
2876 if (tpoint
->handle
!= NULL
)
2878 tpoint
->jump_pad
= jump_entry
;
2879 tpoint
->jump_pad_end
= jentry
;
2880 tpoint
->trampoline
= trampoline
;
2881 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
2883 /* Pad to 8-byte alignment. */
2884 jentry
= ((jentry
+ 7) & ~0x7);
2885 claim_jump_space (jentry
- jump_entry
);
2892 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
2895 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
2897 tpoint
->handle
= NULL
;
2900 if (tpoint
->type
== trap_tracepoint
)
2902 /* Tracepoints are installed as memory breakpoints. Just go
2903 ahead and install the trap. The breakpoints module
2904 handles duplicated breakpoints, and the memory read
2905 routine handles un-patching traps from memory reads. */
2906 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
2907 tracepoint_handler
);
2909 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
2911 struct tracepoint
*tp
;
2913 if (!in_process_agent_loaded ())
2915 trace_debug ("Requested a %s tracepoint, but fast "
2916 "tracepoints aren't supported.",
2917 tpoint
->type
== static_tracepoint
? "static" : "fast");
2918 write_e_ipa_not_loaded (own_buf
);
2921 if (tpoint
->type
== static_tracepoint
&& !in_process_agent_loaded_ust ())
2923 trace_debug ("Requested a static tracepoint, but static "
2924 "tracepoints are not supported.");
2925 write_e_ust_not_loaded (own_buf
);
2929 /* Find another fast or static tracepoint at the same address. */
2930 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2932 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2933 && tp
->number
!= tpoint
->number
)
2937 if (tpoint
->type
== fast_tracepoint
)
2939 if (tp
) /* TPOINT is installed at the same address as TP. */
2940 clone_fast_tracepoint (tpoint
, tp
);
2942 install_fast_tracepoint (tpoint
, own_buf
);
2947 tpoint
->handle
= (void *) -1;
2950 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
2951 tpoint
->handle
= (void *) -1;
2957 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
2959 if (tpoint
->handle
== NULL
)
2961 if (*own_buf
== '\0')
2962 write_enn (own_buf
);
2969 cmd_qtstart (char *packet
)
2971 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
2973 trace_debug ("Starting the trace");
2975 /* Pause all threads temporarily while we patch tracepoints. */
2978 /* Get threads out of jump pads. Safe to do here, since this is a
2979 top level command. And, required to do here, since we're
2980 deleting/rewriting jump pads. */
2982 stabilize_threads ();
2984 /* Freeze threads. */
2987 /* Sync the fast tracepoints list in the inferior ftlib. */
2988 if (in_process_agent_loaded ())
2990 download_tracepoints ();
2991 download_trace_state_variables ();
2994 /* No previous fast tpoint yet. */
2995 prev_ftpoint
= NULL
;
2997 /* No previous static tpoint yet. */
2998 prev_stpoint
= NULL
;
3002 /* Install tracepoints. */
3003 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3005 /* Ensure all the hit counts start at zero. */
3006 tpoint
->hit_count
= 0;
3007 tpoint
->traceframe_usage
= 0;
3009 if (tpoint
->type
== trap_tracepoint
)
3011 /* Tracepoints are installed as memory breakpoints. Just go
3012 ahead and install the trap. The breakpoints module
3013 handles duplicated breakpoints, and the memory read
3014 routine handles un-patching traps from memory reads. */
3015 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3016 tracepoint_handler
);
3018 else if (tpoint
->type
== fast_tracepoint
)
3020 if (maybe_write_ipa_not_loaded (packet
))
3022 trace_debug ("Requested a fast tracepoint, but fast "
3023 "tracepoints aren't supported.");
3027 if (prev_ftpoint
!= NULL
&& prev_ftpoint
->address
== tpoint
->address
)
3028 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3031 if (install_fast_tracepoint (tpoint
, packet
) == 0)
3032 prev_ftpoint
= tpoint
;
3035 else if (tpoint
->type
== static_tracepoint
)
3037 if (maybe_write_ipa_ust_not_loaded (packet
))
3039 trace_debug ("Requested a static tracepoint, but static "
3040 "tracepoints are not supported.");
3044 /* Can only probe a given marker once. */
3045 if (prev_stpoint
!= NULL
&& prev_stpoint
->address
== tpoint
->address
)
3047 tpoint
->handle
= (void *) -1;
3051 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3053 tpoint
->handle
= (void *) -1;
3055 /* So that we can handle multiple static tracepoints
3056 at the same address easily. */
3057 prev_stpoint
= tpoint
;
3062 /* Any failure in the inner loop is sufficient cause to give
3064 if (tpoint
->handle
== NULL
)
3068 /* Any error in tracepoint insertion is unacceptable; better to
3069 address the problem now, than end up with a useless or misleading
3073 clear_installed_tracepoints ();
3074 if (*packet
== '\0')
3080 stopping_tracepoint
= NULL
;
3081 trace_buffer_is_full
= 0;
3082 expr_eval_result
= expr_eval_no_error
;
3083 error_tracepoint
= NULL
;
3084 tracing_start_time
= get_timestamp ();
3086 /* Tracing is now active, hits will now start being logged. */
3089 if (in_process_agent_loaded ())
3091 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3092 fatal ("Error setting tracing variable in lib");
3094 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3096 fatal ("Error clearing stopping_tracepoint variable in lib");
3098 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3099 fatal ("Error clearing trace_buffer_is_full variable in lib");
3101 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3102 stop_tracing_handler
);
3103 if (stop_tracing_bkpt
== NULL
)
3104 error ("Error setting stop_tracing breakpoint");
3106 flush_trace_buffer_bkpt
3107 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3108 flush_trace_buffer_handler
);
3109 if (flush_trace_buffer_bkpt
== NULL
)
3110 error ("Error setting flush_trace_buffer breakpoint");
3118 /* End a tracing run, filling in a stop reason to report back to GDB,
3119 and removing the tracepoints from the code. */
3126 trace_debug ("Tracing is already off, ignoring");
3130 trace_debug ("Stopping the trace");
3132 /* Pause all threads before removing fast jumps from memory,
3133 breakpoints, and touching IPA state variables (inferior memory).
3134 Some thread may hit the internal tracing breakpoints, or be
3135 collecting this moment, but that's ok, we don't release the
3136 tpoint object's memory or the jump pads here (we only do that
3137 when we're sure we can move all threads out of the jump pads).
3138 We can't now, since we may be getting here due to the inferior
3139 agent calling us. */
3141 /* Since we're removing breakpoints, cancel breakpoint hits,
3142 possibly related to the breakpoints we're about to delete. */
3143 cancel_breakpoints ();
3145 /* Stop logging. Tracepoints can still be hit, but they will not be
3148 if (in_process_agent_loaded ())
3150 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3151 fatal ("Error clearing tracing variable in lib");
3154 tracing_stop_time
= get_timestamp ();
3155 tracing_stop_reason
= "t???";
3156 tracing_stop_tpnum
= 0;
3157 if (stopping_tracepoint
)
3159 trace_debug ("Stopping the trace because "
3160 "tracepoint %d was hit %ld times",
3161 stopping_tracepoint
->number
,
3162 stopping_tracepoint
->pass_count
);
3163 tracing_stop_reason
= "tpasscount";
3164 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3166 else if (trace_buffer_is_full
)
3168 trace_debug ("Stopping the trace because the trace buffer is full");
3169 tracing_stop_reason
= "tfull";
3171 else if (expr_eval_result
!= expr_eval_no_error
)
3173 trace_debug ("Stopping the trace because of an expression eval error");
3174 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3175 tracing_stop_tpnum
= error_tracepoint
->number
;
3177 #ifndef IN_PROCESS_AGENT
3178 else if (!gdb_connected ())
3180 trace_debug ("Stopping the trace because GDB disconnected");
3181 tracing_stop_reason
= "tdisconnected";
3186 trace_debug ("Stopping the trace because of a tstop command");
3187 tracing_stop_reason
= "tstop";
3190 stopping_tracepoint
= NULL
;
3191 error_tracepoint
= NULL
;
3193 /* Clear out the tracepoints. */
3194 clear_installed_tracepoints ();
3196 if (in_process_agent_loaded ())
3198 /* Pull in fast tracepoint trace frames from the inferior lib
3199 buffer into our buffer, even if our buffer is already full,
3200 because we want to present the full number of created frames
3201 in addition to what fit in the trace buffer. */
3202 upload_fast_traceframes ();
3205 if (stop_tracing_bkpt
!= NULL
)
3207 delete_breakpoint (stop_tracing_bkpt
);
3208 stop_tracing_bkpt
= NULL
;
3211 if (flush_trace_buffer_bkpt
!= NULL
)
3213 delete_breakpoint (flush_trace_buffer_bkpt
);
3214 flush_trace_buffer_bkpt
= NULL
;
3221 stop_tracing_handler (CORE_ADDR addr
)
3223 trace_debug ("lib hit stop_tracing");
3225 /* Don't actually handle it here. When we stop tracing we remove
3226 breakpoints from the inferior, and that is not allowed in a
3227 breakpoint handler (as the caller is walking the breakpoint
3233 flush_trace_buffer_handler (CORE_ADDR addr
)
3235 trace_debug ("lib hit flush_trace_buffer");
3240 cmd_qtstop (char *packet
)
3247 cmd_qtdisconnected (char *own_buf
)
3250 char *packet
= own_buf
;
3252 packet
+= strlen ("QTDisconnected:");
3254 unpack_varlen_hex (packet
, &setting
);
3258 disconnected_tracing
= setting
;
3262 cmd_qtframe (char *own_buf
)
3264 ULONGEST frame
, pc
, lo
, hi
, num
;
3266 struct traceframe
*tframe
;
3267 char *packet
= own_buf
;
3269 packet
+= strlen ("QTFrame:");
3271 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3273 packet
+= strlen ("pc:");
3274 unpack_varlen_hex (packet
, &pc
);
3275 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3276 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3278 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3280 packet
+= strlen ("range:");
3281 packet
= unpack_varlen_hex (packet
, &lo
);
3283 unpack_varlen_hex (packet
, &hi
);
3284 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3285 paddress (lo
), paddress (hi
));
3286 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3288 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3290 packet
+= strlen ("outside:");
3291 packet
= unpack_varlen_hex (packet
, &lo
);
3293 unpack_varlen_hex (packet
, &hi
);
3294 trace_debug ("Want to find next traceframe "
3295 "outside the range 0x%s to 0x%s",
3296 paddress (lo
), paddress (hi
));
3297 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3299 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3301 packet
+= strlen ("tdp:");
3302 unpack_varlen_hex (packet
, &num
);
3304 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3305 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3309 unpack_varlen_hex (packet
, &frame
);
3310 tfnum
= (int) frame
;
3313 trace_debug ("Want to stop looking at traceframes");
3314 current_traceframe
= -1;
3318 trace_debug ("Want to look at traceframe %d", tfnum
);
3319 tframe
= find_traceframe (tfnum
);
3324 current_traceframe
= tfnum
;
3325 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3328 sprintf (own_buf
, "F-1");
3332 cmd_qtstatus (char *packet
)
3334 char *stop_reason_rsp
= NULL
;
3335 char *buf1
, *buf2
, *buf3
, *str
;
3338 /* Translate the plain text of the notes back into hex for
3341 str
= (tracing_user_name
? tracing_user_name
: "");
3342 slen
= strlen (str
);
3343 buf1
= (char *) alloca (slen
* 2 + 1);
3344 hexify (buf1
, str
, slen
);
3346 str
= (tracing_notes
? tracing_notes
: "");
3347 slen
= strlen (str
);
3348 buf2
= (char *) alloca (slen
* 2 + 1);
3349 hexify (buf2
, str
, slen
);
3351 str
= (tracing_stop_note
? tracing_stop_note
: "");
3352 slen
= strlen (str
);
3353 buf3
= (char *) alloca (slen
* 2 + 1);
3354 hexify (buf3
, str
, slen
);
3356 trace_debug ("Returning trace status as %d, stop reason %s",
3357 tracing
, tracing_stop_reason
);
3359 if (in_process_agent_loaded ())
3363 upload_fast_traceframes ();
3368 stop_reason_rsp
= (char *) tracing_stop_reason
;
3370 /* The user visible error string in terror needs to be hex encoded.
3371 We leave it as plain string in `tracing_stop_reason' to ease
3373 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3375 const char *result_name
;
3379 result_name
= stop_reason_rsp
+ strlen ("terror:");
3380 hexstr_len
= strlen (result_name
) * 2;
3381 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3382 strcpy (p
, "terror:");
3384 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3387 /* If this was a forced stop, include any stop note that was supplied. */
3388 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3390 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3391 strcpy (stop_reason_rsp
, "tstop:");
3392 strcat (stop_reason_rsp
, buf3
);
3398 "tframes:%x;tcreated:%x;"
3399 "tfree:%x;tsize:%s;"
3402 "starttime:%s;stoptime:%s;"
3403 "username:%s:;notes:%s:",
3405 stop_reason_rsp
, tracing_stop_tpnum
,
3406 traceframe_count
, traceframes_created
,
3407 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3408 circular_trace_buffer
,
3409 disconnected_tracing
,
3410 plongest (tracing_start_time
), plongest (tracing_stop_time
),
3415 cmd_qtp (char *own_buf
)
3418 struct tracepoint
*tpoint
;
3419 char *packet
= own_buf
;
3421 packet
+= strlen ("qTP:");
3423 packet
= unpack_varlen_hex (packet
, &num
);
3424 ++packet
; /* skip a colon */
3425 packet
= unpack_varlen_hex (packet
, &addr
);
3427 /* See if we already have this tracepoint. */
3428 tpoint
= find_tracepoint (num
, addr
);
3432 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3433 (int) num
, paddress (addr
));
3434 write_enn (own_buf
);
3438 sprintf (own_buf
, "V%lx:%lx", tpoint
->hit_count
, tpoint
->traceframe_usage
);
3441 /* State variables to help return all the tracepoint bits. */
3442 static struct tracepoint
*cur_tpoint
;
3443 static int cur_action
;
3444 static int cur_step_action
;
3445 static struct source_string
*cur_source_string
;
3446 static struct trace_state_variable
*cur_tsv
;
3448 /* Compose a response that is an imitation of the syntax by which the
3449 tracepoint was originally downloaded. */
3452 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3456 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
3457 paddress (tpoint
->address
),
3458 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3459 tpoint
->pass_count
);
3460 if (tpoint
->type
== fast_tracepoint
)
3461 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3462 else if (tpoint
->type
== static_tracepoint
)
3463 sprintf (packet
+ strlen (packet
), ":S");
3467 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3468 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3469 tpoint
->cond
->length
, buf
);
3474 /* Compose a response that is an imitation of the syntax by which the
3475 tracepoint action was originally downloaded (with the difference
3476 that due to the way we store the actions, this will output a packet
3477 per action, while GDB could have combined more than one action
3481 response_action (char *packet
, struct tracepoint
*tpoint
,
3482 char *taction
, int step
)
3484 sprintf (packet
, "%c%x:%s:%s",
3485 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3489 /* Compose a response that is an imitation of the syntax by which the
3490 tracepoint source piece was originally downloaded. */
3493 response_source (char *packet
,
3494 struct tracepoint
*tpoint
, struct source_string
*src
)
3499 len
= strlen (src
->str
);
3500 buf
= alloca (len
* 2 + 1);
3501 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3503 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3504 tpoint
->number
, paddress (tpoint
->address
),
3505 src
->type
, 0, len
, buf
);
3508 /* Return the first piece of tracepoint definition, and initialize the
3509 state machine that will iterate through all the tracepoint
3513 cmd_qtfp (char *packet
)
3515 trace_debug ("Returning first tracepoint definition piece");
3517 cur_tpoint
= tracepoints
;
3518 cur_action
= cur_step_action
= -1;
3519 cur_source_string
= NULL
;
3522 response_tracepoint (packet
, cur_tpoint
);
3524 strcpy (packet
, "l");
3527 /* Return additional pieces of tracepoint definition. Each action and
3528 stepping action must go into its own packet, because of packet size
3529 limits, and so we use state variables to deliver one piece at a
3533 cmd_qtsp (char *packet
)
3535 trace_debug ("Returning subsequent tracepoint definition piece");
3539 /* This case would normally never occur, but be prepared for
3541 strcpy (packet
, "l");
3543 else if (cur_action
< cur_tpoint
->numactions
- 1)
3546 response_action (packet
, cur_tpoint
,
3547 cur_tpoint
->actions_str
[cur_action
], 0);
3549 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3552 response_action (packet
, cur_tpoint
,
3553 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3555 else if ((cur_source_string
3556 ? cur_source_string
->next
3557 : cur_tpoint
->source_strings
))
3559 if (cur_source_string
)
3560 cur_source_string
= cur_source_string
->next
;
3562 cur_source_string
= cur_tpoint
->source_strings
;
3563 response_source (packet
, cur_tpoint
, cur_source_string
);
3567 cur_tpoint
= cur_tpoint
->next
;
3568 cur_action
= cur_step_action
= -1;
3569 cur_source_string
= NULL
;
3571 response_tracepoint (packet
, cur_tpoint
);
3573 strcpy (packet
, "l");
3577 /* Compose a response that is an imitation of the syntax by which the
3578 trace state variable was originally downloaded. */
3581 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3583 char *buf
= (char *) "";
3588 namelen
= strlen (tsv
->name
);
3589 buf
= alloca (namelen
* 2 + 1);
3590 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3593 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3594 tsv
->getter
? 1 : 0, buf
);
3597 /* Return the first trace state variable definition, and initialize
3598 the state machine that will iterate through all the tsv bits. */
3601 cmd_qtfv (char *packet
)
3603 trace_debug ("Returning first trace state variable definition");
3605 cur_tsv
= trace_state_variables
;
3608 response_tsv (packet
, cur_tsv
);
3610 strcpy (packet
, "l");
3613 /* Return additional trace state variable definitions. */
3616 cmd_qtsv (char *packet
)
3618 trace_debug ("Returning first trace state variable definition");
3622 /* This case would normally never occur, but be prepared for
3624 strcpy (packet
, "l");
3628 cur_tsv
= cur_tsv
->next
;
3630 response_tsv (packet
, cur_tsv
);
3632 strcpy (packet
, "l");
3635 strcpy (packet
, "l");
3638 /* Return the first static tracepoint marker, and initialize the state
3639 machine that will iterate through all the static tracepoints
3643 cmd_qtfstm (char *packet
)
3645 if (!maybe_write_ipa_ust_not_loaded (packet
))
3646 run_inferior_command (packet
);
3649 /* Return additional static tracepoints markers. */
3652 cmd_qtsstm (char *packet
)
3654 if (!maybe_write_ipa_ust_not_loaded (packet
))
3655 run_inferior_command (packet
);
3658 /* Return the definition of the static tracepoint at a given address.
3659 Result packet is the same as qTsST's. */
3662 cmd_qtstmat (char *packet
)
3664 if (!maybe_write_ipa_ust_not_loaded (packet
))
3665 run_inferior_command (packet
);
3668 /* Return the minimum instruction size needed for fast tracepoints as a
3669 hexadecimal number. */
3672 cmd_qtminftpilen (char *packet
)
3674 if (current_inferior
== NULL
)
3676 /* Indicate that the minimum length is currently unknown. */
3677 strcpy (packet
, "0");
3681 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3684 /* Respond to qTBuffer packet with a block of raw data from the trace
3685 buffer. GDB may ask for a lot, but we are allowed to reply with
3686 only as much as will fit within packet limits or whatever. */
3689 cmd_qtbuffer (char *own_buf
)
3691 ULONGEST offset
, num
, tot
;
3693 char *packet
= own_buf
;
3695 packet
+= strlen ("qTBuffer:");
3697 packet
= unpack_varlen_hex (packet
, &offset
);
3698 ++packet
; /* skip a comma */
3699 unpack_varlen_hex (packet
, &num
);
3701 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3702 (int) num
, pulongest (offset
));
3704 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3706 /* If we're right at the end, reply specially that we're done. */
3709 strcpy (own_buf
, "l");
3713 /* Object to any other out-of-bounds request. */
3716 write_enn (own_buf
);
3720 /* Compute the pointer corresponding to the given offset, accounting
3722 tbp
= trace_buffer_start
+ offset
;
3723 if (tbp
>= trace_buffer_wrap
)
3724 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3726 /* Trim to the remaining bytes if we're close to the end. */
3727 if (num
> tot
- offset
)
3730 /* Trim to available packet size. */
3731 if (num
>= (PBUFSIZ
- 16) / 2 )
3732 num
= (PBUFSIZ
- 16) / 2;
3734 convert_int_to_ascii (tbp
, own_buf
, num
);
3735 own_buf
[num
] = '\0';
3739 cmd_bigqtbuffer_circular (char *own_buf
)
3742 char *packet
= own_buf
;
3744 packet
+= strlen ("QTBuffer:circular:");
3746 unpack_varlen_hex (packet
, &val
);
3747 circular_trace_buffer
= val
;
3748 trace_debug ("Trace buffer is now %s",
3749 circular_trace_buffer
? "circular" : "linear");
3754 cmd_qtnotes (char *own_buf
)
3757 char *saved
, *user
, *notes
, *stopnote
;
3758 char *packet
= own_buf
;
3760 packet
+= strlen ("QTNotes:");
3764 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
3766 packet
+= strlen ("user:");
3768 packet
= strchr (packet
, ';');
3769 nbytes
= (packet
- saved
) / 2;
3770 user
= xmalloc (nbytes
+ 1);
3771 nbytes
= unhexify (user
, saved
, nbytes
);
3772 user
[nbytes
] = '\0';
3773 ++packet
; /* skip the semicolon */
3774 trace_debug ("User is '%s'", user
);
3775 tracing_user_name
= user
;
3777 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
3779 packet
+= strlen ("notes:");
3781 packet
= strchr (packet
, ';');
3782 nbytes
= (packet
- saved
) / 2;
3783 notes
= xmalloc (nbytes
+ 1);
3784 nbytes
= unhexify (notes
, saved
, nbytes
);
3785 notes
[nbytes
] = '\0';
3786 ++packet
; /* skip the semicolon */
3787 trace_debug ("Notes is '%s'", notes
);
3788 tracing_notes
= notes
;
3790 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
3792 packet
+= strlen ("tstop:");
3794 packet
= strchr (packet
, ';');
3795 nbytes
= (packet
- saved
) / 2;
3796 stopnote
= xmalloc (nbytes
+ 1);
3797 nbytes
= unhexify (stopnote
, saved
, nbytes
);
3798 stopnote
[nbytes
] = '\0';
3799 ++packet
; /* skip the semicolon */
3800 trace_debug ("tstop note is '%s'", stopnote
);
3801 tracing_stop_note
= stopnote
;
3811 handle_tracepoint_general_set (char *packet
)
3813 if (strcmp ("QTinit", packet
) == 0)
3815 cmd_qtinit (packet
);
3818 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3823 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3825 cmd_qtdpsrc (packet
);
3828 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
3830 cmd_qtenable_disable (packet
, 1);
3833 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
3835 cmd_qtenable_disable (packet
, 0);
3838 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3843 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3848 else if (strcmp ("QTStart", packet
) == 0)
3850 cmd_qtstart (packet
);
3853 else if (strcmp ("QTStop", packet
) == 0)
3855 cmd_qtstop (packet
);
3858 else if (strncmp ("QTDisconnected:", packet
,
3859 strlen ("QTDisconnected:")) == 0)
3861 cmd_qtdisconnected (packet
);
3864 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3866 cmd_qtframe (packet
);
3869 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
3871 cmd_bigqtbuffer_circular (packet
);
3874 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
3876 cmd_qtnotes (packet
);
3884 handle_tracepoint_query (char *packet
)
3886 if (strcmp ("qTStatus", packet
) == 0)
3888 cmd_qtstatus (packet
);
3891 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
3896 else if (strcmp ("qTfP", packet
) == 0)
3901 else if (strcmp ("qTsP", packet
) == 0)
3906 else if (strcmp ("qTfV", packet
) == 0)
3911 else if (strcmp ("qTsV", packet
) == 0)
3916 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
3921 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
3923 cmd_qtbuffer (packet
);
3926 else if (strcmp ("qTfSTM", packet
) == 0)
3928 cmd_qtfstm (packet
);
3931 else if (strcmp ("qTsSTM", packet
) == 0)
3933 cmd_qtsstm (packet
);
3936 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
3938 cmd_qtstmat (packet
);
3941 else if (strcmp ("qTMinFTPILen", packet
) == 0)
3943 cmd_qtminftpilen (packet
);
3951 #ifndef IN_PROCESS_AGENT
3953 /* Call this when thread TINFO has hit the tracepoint defined by
3954 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3955 action. This adds a while-stepping collecting state item to the
3956 threads' collecting state list, so that we can keep track of
3957 multiple simultaneous while-stepping actions being collected by the
3958 same thread. This can happen in cases like:
3960 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3962 ff0003 INSN3 <-- TP2, collect $regs
3963 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3966 Notice that when instruction INSN5 is reached, the while-stepping
3967 actions of both TP1 and TP3 are still being collected, and that TP2
3968 had been collected meanwhile. The whole range of ff0001-ff0005
3969 should be single-stepped, due to at least TP1's while-stepping
3970 action covering the whole range. */
3973 add_while_stepping_state (struct thread_info
*tinfo
,
3974 int tp_number
, CORE_ADDR tp_address
)
3976 struct wstep_state
*wstep
;
3978 wstep
= xmalloc (sizeof (*wstep
));
3979 wstep
->next
= tinfo
->while_stepping
;
3981 wstep
->tp_number
= tp_number
;
3982 wstep
->tp_address
= tp_address
;
3983 wstep
->current_step
= 0;
3985 tinfo
->while_stepping
= wstep
;
3988 /* Release the while-stepping collecting state WSTEP. */
3991 release_while_stepping_state (struct wstep_state
*wstep
)
3996 /* Release all while-stepping collecting states currently associated
3997 with thread TINFO. */
4000 release_while_stepping_state_list (struct thread_info
*tinfo
)
4002 struct wstep_state
*head
;
4004 while (tinfo
->while_stepping
)
4006 head
= tinfo
->while_stepping
;
4007 tinfo
->while_stepping
= head
->next
;
4008 release_while_stepping_state (head
);
4012 /* If TINFO was handling a 'while-stepping' action, the step has
4013 finished, so collect any step data needed, and check if any more
4014 steps are required. Return true if the thread was indeed
4015 collecting tracepoint data, false otherwise. */
4018 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4020 struct tracepoint
*tpoint
;
4021 struct wstep_state
*wstep
;
4022 struct wstep_state
**wstep_link
;
4023 struct trap_tracepoint_ctx ctx
;
4025 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4027 if (in_process_agent_loaded ())
4028 upload_fast_traceframes ();
4030 /* Check if we were indeed collecting data for one of more
4031 tracepoints with a 'while-stepping' count. */
4032 if (tinfo
->while_stepping
== NULL
)
4037 /* We're not even tracing anymore. Stop this thread from
4039 release_while_stepping_state_list (tinfo
);
4041 /* The thread had stopped due to a single-step request indeed
4042 explained by a tracepoint. */
4046 wstep
= tinfo
->while_stepping
;
4047 wstep_link
= &tinfo
->while_stepping
;
4049 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4050 target_pid_to_str (tinfo
->entry
.id
),
4051 wstep
->tp_number
, paddress (wstep
->tp_address
));
4053 ctx
.base
.type
= trap_tracepoint
;
4054 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4056 while (wstep
!= NULL
)
4058 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4061 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4062 wstep
->tp_number
, paddress (wstep
->tp_address
),
4063 target_pid_to_str (tinfo
->entry
.id
));
4066 *wstep_link
= wstep
->next
;
4067 release_while_stepping_state (wstep
);
4068 wstep
= *wstep_link
;
4072 /* We've just finished one step. */
4073 ++wstep
->current_step
;
4076 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4077 stop_pc
, tpoint
, wstep
->current_step
);
4079 if (wstep
->current_step
>= tpoint
->step_count
)
4081 /* The requested numbers of steps have occurred. */
4082 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4083 target_pid_to_str (tinfo
->entry
.id
),
4084 wstep
->tp_number
, paddress (wstep
->tp_address
));
4086 /* Unlink the wstep. */
4087 *wstep_link
= wstep
->next
;
4088 release_while_stepping_state (wstep
);
4089 wstep
= *wstep_link
;
4091 /* Only check the hit count now, which ensure that we do all
4092 our stepping before stopping the run. */
4093 if (tpoint
->pass_count
> 0
4094 && tpoint
->hit_count
>= tpoint
->pass_count
4095 && stopping_tracepoint
== NULL
)
4096 stopping_tracepoint
= tpoint
;
4100 /* Keep single-stepping until the requested numbers of steps
4102 wstep_link
= &wstep
->next
;
4103 wstep
= *wstep_link
;
4106 if (stopping_tracepoint
4107 || trace_buffer_is_full
4108 || expr_eval_result
!= expr_eval_no_error
)
4118 /* Handle any internal tracing control breakpoint hits. That means,
4119 pull traceframes from the IPA to our buffer, and syncing both
4120 tracing agents when the IPA's tracing stops for some reason. */
4123 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4125 /* Pull in fast tracepoint trace frames from the inferior in-process
4126 agent's buffer into our buffer. */
4128 if (!in_process_agent_loaded ())
4131 upload_fast_traceframes ();
4133 /* Check if the in-process agent had decided we should stop
4135 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4137 int ipa_trace_buffer_is_full
;
4138 CORE_ADDR ipa_stopping_tracepoint
;
4139 int ipa_expr_eval_result
;
4140 CORE_ADDR ipa_error_tracepoint
;
4142 trace_debug ("lib stopped at stop_tracing");
4144 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4145 &ipa_trace_buffer_is_full
);
4147 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4148 &ipa_stopping_tracepoint
);
4149 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4151 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4152 &ipa_error_tracepoint
);
4153 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4155 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4156 &ipa_expr_eval_result
);
4157 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4159 trace_debug ("lib: trace_buffer_is_full: %d, "
4160 "stopping_tracepoint: %s, "
4161 "ipa_expr_eval_result: %d, "
4162 "error_tracepoint: %s, ",
4163 ipa_trace_buffer_is_full
,
4164 paddress (ipa_stopping_tracepoint
),
4165 ipa_expr_eval_result
,
4166 paddress (ipa_error_tracepoint
));
4170 if (ipa_trace_buffer_is_full
)
4171 trace_debug ("lib stopped due to full buffer.");
4172 if (ipa_stopping_tracepoint
)
4173 trace_debug ("lib stopped due to tpoint");
4174 if (ipa_stopping_tracepoint
)
4175 trace_debug ("lib stopped due to error");
4178 if (ipa_stopping_tracepoint
!= 0)
4181 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4183 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4185 expr_eval_result
= ipa_expr_eval_result
;
4187 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4192 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4194 trace_debug ("lib stopped at flush_trace_buffer");
4201 /* Return true if TINFO just hit a tracepoint. Collect data if
4205 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4207 struct tracepoint
*tpoint
;
4209 struct trap_tracepoint_ctx ctx
;
4211 /* Not tracing, don't handle. */
4215 ctx
.base
.type
= trap_tracepoint
;
4216 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4218 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4220 /* Note that we collect fast tracepoints here as well. We'll
4221 step over the fast tracepoint jump later, which avoids the
4222 double collect. However, we don't collect for static
4223 tracepoints here, because UST markers are compiled in program,
4224 and probes will be executed in program. So static tracepoints
4225 are collected there. */
4226 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4227 && tpoint
->type
!= static_tracepoint
)
4229 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4230 target_pid_to_str (tinfo
->entry
.id
),
4231 tpoint
->number
, paddress (tpoint
->address
));
4233 /* Test the condition if present, and collect if true. */
4235 || (condition_true_at_tracepoint
4236 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4237 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4240 if (stopping_tracepoint
4241 || trace_buffer_is_full
4242 || expr_eval_result
!= expr_eval_no_error
)
4246 /* If the tracepoint had a 'while-stepping' action, then set
4247 the thread to collect this tracepoint on the following
4249 else if (tpoint
->step_count
> 0)
4251 add_while_stepping_state (tinfo
,
4252 tpoint
->number
, tpoint
->address
);
4264 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4265 struct ust_marker_data
;
4266 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4267 struct traceframe
*tframe
);
4270 /* Create a trace frame for the hit of the given tracepoint in the
4274 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4275 struct tracepoint
*tpoint
)
4277 struct traceframe
*tframe
;
4280 /* Only count it as a hit when we actually collect data. */
4281 tpoint
->hit_count
++;
4283 /* If we've exceeded a defined pass count, record the event for
4284 later, and finish the collection for this hit. This test is only
4285 for nonstepping tracepoints, stepping tracepoints test at the end
4286 of their while-stepping loop. */
4287 if (tpoint
->pass_count
> 0
4288 && tpoint
->hit_count
>= tpoint
->pass_count
4289 && tpoint
->step_count
== 0
4290 && stopping_tracepoint
== NULL
)
4291 stopping_tracepoint
= tpoint
;
4293 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
4294 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4296 tframe
= add_traceframe (tpoint
);
4300 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4302 #ifndef IN_PROCESS_AGENT
4303 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4304 tpoint
->number
, paddress (tpoint
->address
),
4305 tpoint
->actions_str
[acti
]);
4308 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4309 tpoint
->actions
[acti
]);
4312 finish_traceframe (tframe
);
4315 if (tframe
== NULL
&& tracing
)
4316 trace_buffer_is_full
= 1;
4319 #ifndef IN_PROCESS_AGENT
4322 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4324 struct tracepoint
*tpoint
, int current_step
)
4326 struct traceframe
*tframe
;
4329 trace_debug ("Making new step traceframe for "
4330 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4331 tpoint
->number
, paddress (tpoint
->address
),
4332 current_step
, tpoint
->step_count
,
4335 tframe
= add_traceframe (tpoint
);
4339 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4341 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4342 tpoint
->number
, paddress (tpoint
->address
),
4343 tpoint
->step_actions_str
[acti
]);
4345 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4346 tpoint
->step_actions
[acti
]);
4349 finish_traceframe (tframe
);
4352 if (tframe
== NULL
&& tracing
)
4353 trace_buffer_is_full
= 1;
4358 static struct regcache
*
4359 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4361 struct regcache
*regcache
= NULL
;
4363 #ifdef IN_PROCESS_AGENT
4364 if (ctx
->type
== fast_tracepoint
)
4366 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4367 if (!fctx
->regcache_initted
)
4369 fctx
->regcache_initted
= 1;
4370 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4371 supply_regblock (&fctx
->regcache
, NULL
);
4372 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4374 regcache
= &fctx
->regcache
;
4377 if (ctx
->type
== static_tracepoint
)
4379 struct static_tracepoint_ctx
*sctx
4380 = (struct static_tracepoint_ctx
*) ctx
;
4382 if (!sctx
->regcache_initted
)
4384 sctx
->regcache_initted
= 1;
4385 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4386 supply_regblock (&sctx
->regcache
, NULL
);
4387 /* Pass down the tracepoint address, because REGS doesn't
4388 include the PC, but we know what it must have been. */
4389 supply_static_tracepoint_registers (&sctx
->regcache
,
4390 (const unsigned char *)
4392 sctx
->tpoint
->address
);
4394 regcache
= &sctx
->regcache
;
4398 if (ctx
->type
== trap_tracepoint
)
4400 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4401 regcache
= tctx
->regcache
;
4405 gdb_assert (regcache
!= NULL
);
4411 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4413 struct tracepoint
*tpoint
,
4414 struct traceframe
*tframe
,
4415 struct tracepoint_action
*taction
)
4417 enum eval_result_type err
;
4419 switch (taction
->type
)
4423 struct collect_memory_action
*maction
;
4425 maction
= (struct collect_memory_action
*) taction
;
4427 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4428 pulongest (maction
->len
),
4429 paddress (maction
->addr
), maction
->basereg
);
4430 /* (should use basereg) */
4431 agent_mem_read (tframe
, NULL
,
4432 (CORE_ADDR
) maction
->addr
, maction
->len
);
4437 unsigned char *regspace
;
4438 struct regcache tregcache
;
4439 struct regcache
*context_regcache
;
4442 trace_debug ("Want to collect registers");
4444 /* Collect all registers for now. */
4445 regspace
= add_traceframe_block (tframe
,
4446 1 + register_cache_size ());
4447 if (regspace
== NULL
)
4449 trace_debug ("Trace buffer block allocation failed, skipping");
4452 /* Identify a register block. */
4455 context_regcache
= get_context_regcache (ctx
);
4457 /* Wrap the regblock in a register cache (in the stack, we
4458 don't want to malloc here). */
4459 init_register_cache (&tregcache
, regspace
+ 1);
4461 /* Copy the register data to the regblock. */
4462 regcache_cpy (&tregcache
, context_regcache
);
4464 #ifndef IN_PROCESS_AGENT
4465 /* On some platforms, trap-based tracepoints will have the PC
4466 pointing to the next instruction after the trap, but we
4467 don't want the user or GDB trying to guess whether the
4468 saved PC needs adjusting; so always record the adjusted
4469 stop_pc. Note that we can't use tpoint->address instead,
4470 since it will be wrong for while-stepping actions. This
4471 adjustment is a nop for fast tracepoints collected from the
4472 in-process lib (but not if GDBserver is collecting one
4473 preemptively), since the PC had already been adjusted to
4474 contain the tracepoint's address by the jump pad. */
4475 trace_debug ("Storing stop pc (0x%s) in regblock",
4476 paddress (stop_pc
));
4478 /* This changes the regblock, not the thread's
4480 regcache_write_pc (&tregcache
, stop_pc
);
4486 struct eval_expr_action
*eaction
;
4488 eaction
= (struct eval_expr_action
*) taction
;
4490 trace_debug ("Want to evaluate expression");
4492 err
= eval_tracepoint_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4494 if (err
!= expr_eval_no_error
)
4496 record_tracepoint_error (tpoint
, "action expression", err
);
4503 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4504 trace_debug ("Want to collect static trace data");
4505 collect_ust_data_at_tracepoint (ctx
, tframe
);
4507 trace_debug ("warning: collecting static trace data, "
4508 "but static tracepoints are not supported");
4513 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4519 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4520 struct tracepoint
*tpoint
)
4523 enum eval_result_type err
;
4525 /* Presently, gdbserver doesn't run compiled conditions, only the
4526 IPA does. If the program stops at a fast tracepoint's address
4527 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4528 gdbserver preemptively collect the fast tracepoint. Later, on
4529 resume, gdbserver steps over the fast tracepoint like it steps
4530 over breakpoints, so that the IPA doesn't see that fast
4531 tracepoint. This avoids double collects of fast tracepoints in
4532 that stopping scenario. Having gdbserver itself handle the fast
4533 tracepoint gives the user a consistent view of when fast or trap
4534 tracepoints are collected, compared to an alternative where only
4535 trap tracepoints are collected on stop, and fast tracepoints on
4536 resume. When a fast tracepoint is being processed by gdbserver,
4537 it is always the non-compiled condition expression that is
4539 #ifdef IN_PROCESS_AGENT
4540 if (tpoint
->compiled_cond
)
4541 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4544 err
= eval_tracepoint_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4546 if (err
!= expr_eval_no_error
)
4548 record_tracepoint_error (tpoint
, "condition", err
);
4549 /* The error case must return false. */
4553 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4554 tpoint
->number
, paddress (tpoint
->address
),
4556 return (value
? 1 : 0);
4559 /* Evaluates a tracepoint agent expression with context CTX,
4560 traceframe TFRAME, agent expression AEXPR and store the
4563 static enum eval_result_type
4564 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4565 struct traceframe
*tframe
,
4566 struct agent_expr
*aexpr
,
4569 struct regcache
*regcache
;
4570 regcache
= get_context_regcache (ctx
);
4572 return gdb_eval_agent_expr (regcache
, tframe
, aexpr
, rslt
);
4575 /* Do memory copies for bytecodes. */
4576 /* Do the recording of memory blocks for actions and bytecodes. */
4579 agent_mem_read (struct traceframe
*tframe
,
4580 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4582 unsigned char *mspace
;
4583 ULONGEST remaining
= len
;
4584 unsigned short blocklen
;
4586 /* If a 'to' buffer is specified, use it. */
4589 read_inferior_memory (from
, to
, len
);
4593 /* Otherwise, create a new memory block in the trace buffer. */
4594 while (remaining
> 0)
4598 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4599 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4600 mspace
= add_traceframe_block (tframe
, sp
);
4603 /* Identify block as a memory block. */
4606 /* Record address and size. */
4607 memcpy (mspace
, &from
, sizeof (from
));
4608 mspace
+= sizeof (from
);
4609 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4610 mspace
+= sizeof (blocklen
);
4611 /* Record the memory block proper. */
4612 read_inferior_memory (from
, mspace
, blocklen
);
4613 trace_debug ("%d bytes recorded", blocklen
);
4614 remaining
-= blocklen
;
4621 agent_mem_read_string (struct traceframe
*tframe
,
4622 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4624 unsigned char *buf
, *mspace
;
4625 ULONGEST remaining
= len
;
4626 unsigned short blocklen
, i
;
4628 /* To save a bit of space, block lengths are 16-bit, so break large
4629 requests into multiple blocks. Bordering on overkill for strings,
4630 but it could happen that someone specifies a large max length. */
4631 while (remaining
> 0)
4635 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4636 /* We want working space to accumulate nonzero bytes, since
4637 traceframes must have a predecided size (otherwise it gets
4638 harder to wrap correctly for the circular case, etc). */
4639 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4640 for (i
= 0; i
< blocklen
; ++i
)
4642 /* Read the string one byte at a time, in case the string is
4643 at the end of a valid memory area - we don't want a
4644 correctly-terminated string to engender segvio
4646 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4651 /* Make sure outer loop stops now too. */
4652 remaining
= blocklen
;
4656 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4657 mspace
= add_traceframe_block (tframe
, sp
);
4663 /* Identify block as a memory block. */
4666 /* Record address and size. */
4667 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4668 mspace
+= sizeof (from
);
4669 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4670 mspace
+= sizeof (blocklen
);
4671 /* Copy the string contents. */
4672 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
4673 remaining
-= blocklen
;
4680 /* Record the value of a trace state variable. */
4683 agent_tsv_read (struct traceframe
*tframe
, int n
)
4685 unsigned char *vspace
;
4688 vspace
= add_traceframe_block (tframe
,
4689 1 + sizeof (n
) + sizeof (LONGEST
));
4692 /* Identify block as a variable. */
4694 /* Record variable's number and value. */
4695 memcpy (vspace
+ 1, &n
, sizeof (n
));
4696 val
= get_trace_state_variable_value (n
);
4697 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4698 trace_debug ("Variable %d recorded", n
);
4702 #ifndef IN_PROCESS_AGENT
4704 /* Callback for traceframe_walk_blocks, used to find a given block
4705 type in a traceframe. */
4708 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
4710 char *wantedp
= data
;
4712 if (*wantedp
== blocktype
)
4718 /* Walk over all traceframe blocks of the traceframe buffer starting
4719 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4720 block found, passing in DATA unmodified. If CALLBACK returns true,
4721 this returns a pointer to where the block is found. Returns NULL
4722 if no callback call returned true, indicating that all blocks have
4725 static unsigned char *
4726 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
4728 int (*callback
) (char blocktype
,
4729 unsigned char *dataptr
,
4733 unsigned char *dataptr
;
4737 trace_debug ("traceframe %d has no data", tfnum
);
4741 /* Iterate through a traceframe's blocks, looking for a block of the
4743 for (dataptr
= database
;
4744 dataptr
< database
+ datasize
;
4748 unsigned short mlen
;
4750 if (dataptr
== trace_buffer_wrap
)
4752 /* Adjust to reflect wrapping part of the frame around to
4754 datasize
= dataptr
- database
;
4755 dataptr
= database
= trace_buffer_lo
;
4758 blocktype
= *dataptr
++;
4760 if ((*callback
) (blocktype
, dataptr
, data
))
4766 /* Skip over the registers block. */
4767 dataptr
+= register_cache_size ();
4770 /* Skip over the memory block. */
4771 dataptr
+= sizeof (CORE_ADDR
);
4772 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4773 dataptr
+= (sizeof (mlen
) + mlen
);
4776 /* Skip over the TSV block. */
4777 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4780 /* Skip over the static trace data block. */
4781 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4782 dataptr
+= (sizeof (mlen
) + mlen
);
4785 trace_debug ("traceframe %d has unknown block type 0x%x",
4794 /* Look for the block of type TYPE_WANTED in the trameframe starting
4795 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4798 static unsigned char *
4799 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4800 int tfnum
, char type_wanted
)
4802 return traceframe_walk_blocks (database
, datasize
, tfnum
,
4803 match_blocktype
, &type_wanted
);
4806 static unsigned char *
4807 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4809 unsigned char *regblock
;
4811 regblock
= traceframe_find_block_type (tframe
->data
,
4815 if (regblock
== NULL
)
4816 trace_debug ("traceframe %d has no register data", tfnum
);
4821 /* Get registers from a traceframe. */
4824 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4826 unsigned char *dataptr
;
4827 struct tracepoint
*tpoint
;
4828 struct traceframe
*tframe
;
4830 tframe
= find_traceframe (tfnum
);
4834 trace_debug ("traceframe %d not found", tfnum
);
4838 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4839 if (dataptr
== NULL
)
4841 /* Mark registers unavailable. */
4842 supply_regblock (regcache
, NULL
);
4844 /* We can generally guess at a PC, although this will be
4845 misleading for while-stepping frames and multi-location
4847 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4849 regcache_write_pc (regcache
, tpoint
->address
);
4852 supply_regblock (regcache
, dataptr
);
4858 traceframe_get_pc (struct traceframe
*tframe
)
4860 struct regcache regcache
;
4861 unsigned char *dataptr
;
4863 dataptr
= traceframe_find_regblock (tframe
, -1);
4864 if (dataptr
== NULL
)
4867 init_register_cache (®cache
, dataptr
);
4868 return regcache_read_pc (®cache
);
4871 /* Read a requested block of memory from a trace frame. */
4874 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
4875 unsigned char *buf
, ULONGEST length
,
4878 struct traceframe
*tframe
;
4879 unsigned char *database
, *dataptr
;
4880 unsigned int datasize
;
4882 unsigned short mlen
;
4884 trace_debug ("traceframe_read_mem");
4886 tframe
= find_traceframe (tfnum
);
4890 trace_debug ("traceframe %d not found", tfnum
);
4894 datasize
= tframe
->data_size
;
4895 database
= dataptr
= &tframe
->data
[0];
4897 /* Iterate through a traceframe's blocks, looking for memory. */
4898 while ((dataptr
= traceframe_find_block_type (dataptr
,
4900 - (dataptr
- database
),
4901 tfnum
, 'M')) != NULL
)
4903 memcpy (&maddr
, dataptr
, sizeof (maddr
));
4904 dataptr
+= sizeof (maddr
);
4905 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4906 dataptr
+= sizeof (mlen
);
4907 trace_debug ("traceframe %d has %d bytes at %s",
4908 tfnum
, mlen
, paddress (maddr
));
4910 /* If the block includes the first part of the desired range,
4911 return as much it has; GDB will re-request the remainder,
4912 which might be in a different block of this trace frame. */
4913 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
4915 ULONGEST amt
= (maddr
+ mlen
) - addr
;
4919 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
4924 /* Skip over this block. */
4928 trace_debug ("traceframe %d has no memory data for the desired region",
4936 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
4939 struct traceframe
*tframe
;
4940 unsigned char *database
, *dataptr
;
4941 unsigned int datasize
;
4944 trace_debug ("traceframe_read_tsv");
4946 tfnum
= current_traceframe
;
4950 trace_debug ("no current traceframe");
4954 tframe
= find_traceframe (tfnum
);
4958 trace_debug ("traceframe %d not found", tfnum
);
4962 datasize
= tframe
->data_size
;
4963 database
= dataptr
= &tframe
->data
[0];
4965 /* Iterate through a traceframe's blocks, looking for the tsv. */
4966 while ((dataptr
= traceframe_find_block_type (dataptr
,
4968 - (dataptr
- database
),
4969 tfnum
, 'V')) != NULL
)
4971 memcpy (&vnum
, dataptr
, sizeof (vnum
));
4972 dataptr
+= sizeof (vnum
);
4974 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
4976 /* Check that this is the variable we want. */
4979 memcpy (val
, dataptr
, sizeof (*val
));
4983 /* Skip over this block. */
4984 dataptr
+= sizeof (LONGEST
);
4987 trace_debug ("traceframe %d has no data for variable %d",
4992 /* Read a requested block of static tracepoint data from a trace
4996 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
4997 unsigned char *buf
, ULONGEST length
,
5000 struct traceframe
*tframe
;
5001 unsigned char *database
, *dataptr
;
5002 unsigned int datasize
;
5003 unsigned short mlen
;
5005 trace_debug ("traceframe_read_sdata");
5007 tframe
= find_traceframe (tfnum
);
5011 trace_debug ("traceframe %d not found", tfnum
);
5015 datasize
= tframe
->data_size
;
5016 database
= &tframe
->data
[0];
5018 /* Iterate through a traceframe's blocks, looking for static
5020 dataptr
= traceframe_find_block_type (database
, datasize
,
5022 if (dataptr
!= NULL
)
5024 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5025 dataptr
+= sizeof (mlen
);
5028 if (offset
+ length
> mlen
)
5029 length
= mlen
- offset
;
5031 memcpy (buf
, dataptr
, length
);
5039 trace_debug ("traceframe %d has no static trace data", tfnum
);
5045 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5046 object. DATA is pointer to a struct buffer holding the
5047 traceframe-info object being built. */
5050 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5052 struct buffer
*buffer
= data
;
5058 unsigned short mlen
;
5061 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5062 dataptr
+= sizeof (maddr
);
5063 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5064 dataptr
+= sizeof (mlen
);
5065 buffer_xml_printf (buffer
,
5066 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5067 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5077 warning ("Unhandled trace block type (%d) '%c ' "
5078 "while building trace frame info.",
5079 blocktype
, blocktype
);
5086 /* Build a traceframe-info object for traceframe number TFNUM into
5090 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5092 struct traceframe
*tframe
;
5094 trace_debug ("traceframe_read_info");
5096 tframe
= find_traceframe (tfnum
);
5100 trace_debug ("traceframe %d not found", tfnum
);
5104 buffer_grow_str (buffer
, "<traceframe-info>\n");
5105 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5106 tfnum
, build_traceframe_info_xml
, buffer
);
5107 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5111 /* Return the first fast tracepoint whose jump pad contains PC. */
5113 static struct tracepoint
*
5114 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5116 struct tracepoint
*tpoint
;
5118 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5119 if (tpoint
->type
== fast_tracepoint
)
5120 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5126 /* Return the first fast tracepoint whose trampoline contains PC. */
5128 static struct tracepoint
*
5129 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5131 struct tracepoint
*tpoint
;
5133 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5135 if (tpoint
->type
== fast_tracepoint
5136 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5143 /* Return GDBserver's tracepoint that matches the IP Agent's
5144 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5147 static struct tracepoint
*
5148 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5150 struct tracepoint
*tpoint
;
5152 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5153 if (tpoint
->type
== fast_tracepoint
)
5154 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5162 /* The type of the object that is used to synchronize fast tracepoint
5165 typedef struct collecting_t
5167 /* The fast tracepoint number currently collecting. */
5170 /* A number that GDBserver can use to identify the thread that is
5171 presently holding the collect lock. This need not (and usually
5172 is not) the thread id, as getting the current thread ID usually
5173 requires a system call, which we want to avoid like the plague.
5174 Usually this is thread's TCB, found in the TLS (pseudo-)
5175 register, which is readable with a single insn on several
5177 uintptr_t thread_area
;
5180 #ifndef IN_PROCESS_AGENT
5183 force_unlock_trace_buffer (void)
5185 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5188 /* Check if the thread identified by THREAD_AREA which is stopped at
5189 STOP_PC, is presently locking the fast tracepoint collection, and
5190 if so, gather some status of said collection. Returns 0 if the
5191 thread isn't collecting or in the jump pad at all. 1, if in the
5192 jump pad (or within gdb_collect) and hasn't executed the adjusted
5193 original insn yet (can set a breakpoint there and run to it). 2,
5194 if presently executing the adjusted original insn --- in which
5195 case, if we want to move the thread out of the jump pad, we need to
5196 single-step it until this function returns 0. */
5199 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5201 struct fast_tpoint_collect_status
*status
)
5203 CORE_ADDR ipa_collecting
;
5204 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5205 CORE_ADDR ipa_gdb_trampoline_buffer
;
5206 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5207 struct tracepoint
*tpoint
;
5208 int needs_breakpoint
;
5210 /* The thread THREAD_AREA is either:
5212 0. not collecting at all, not within the jump pad, or within
5213 gdb_collect or one of its callees.
5215 1. in the jump pad and haven't reached gdb_collect
5217 2. within gdb_collect (out of the jump pad) (collect is set)
5219 3. we're in the jump pad, after gdb_collect having returned,
5220 possibly executing the adjusted insns.
5222 For cases 1 and 3, `collecting' may or not be set. The jump pad
5223 doesn't have any complicated jump logic, so we can tell if the
5224 thread is executing the adjust original insn or not by just
5225 matching STOP_PC with known jump pad addresses. If we it isn't
5226 yet executing the original insn, set a breakpoint there, and let
5227 the thread run to it, so to quickly step over a possible (many
5228 insns) gdb_collect call. Otherwise, or when the breakpoint is
5229 hit, only a few (small number of) insns are left to be executed
5230 in the jump pad. Single-step the thread until it leaves the
5235 needs_breakpoint
= 0;
5236 trace_debug ("fast_tracepoint_collecting");
5238 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5239 &ipa_gdb_jump_pad_buffer
))
5240 fatal ("error extracting `gdb_jump_pad_buffer'");
5241 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5242 &ipa_gdb_jump_pad_buffer_end
))
5243 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5245 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5246 &ipa_gdb_trampoline_buffer
))
5247 fatal ("error extracting `gdb_trampoline_buffer'");
5248 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5249 &ipa_gdb_trampoline_buffer_end
))
5250 fatal ("error extracting `gdb_trampoline_buffer_end'");
5252 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5253 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5255 /* We can tell which tracepoint(s) the thread is collecting by
5256 matching the jump pad address back to the tracepoint. */
5257 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5260 warning ("in jump pad, but no matching tpoint?");
5265 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5267 tpoint
->number
, paddress (tpoint
->address
),
5268 paddress (tpoint
->jump_pad
),
5269 paddress (tpoint
->jump_pad_end
),
5270 paddress (tpoint
->adjusted_insn_addr
),
5271 paddress (tpoint
->adjusted_insn_addr_end
));
5274 /* Definitely in the jump pad. May or may not need
5275 fast-exit-jump-pad breakpoint. */
5276 if (tpoint
->jump_pad
<= stop_pc
5277 && stop_pc
< tpoint
->adjusted_insn_addr
)
5278 needs_breakpoint
= 1;
5280 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5281 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5283 /* We can tell which tracepoint(s) the thread is collecting by
5284 matching the trampoline address back to the tracepoint. */
5285 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5288 warning ("in trampoline, but no matching tpoint?");
5293 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5294 tpoint
->number
, paddress (tpoint
->address
),
5295 paddress (tpoint
->trampoline
),
5296 paddress (tpoint
->trampoline_end
));
5299 /* Have not reached jump pad yet, but treat the trampoline as a
5300 part of the jump pad that is before the adjusted original
5302 needs_breakpoint
= 1;
5306 collecting_t ipa_collecting_obj
;
5308 /* If `collecting' is set/locked, then the THREAD_AREA thread
5309 may or not be the one holding the lock. We have to read the
5310 lock to find out. */
5312 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5315 trace_debug ("fast_tracepoint_collecting:"
5316 " failed reading 'collecting' in the inferior");
5320 if (!ipa_collecting
)
5322 trace_debug ("fast_tracepoint_collecting: not collecting"
5323 " (and nobody is).");
5327 /* Some thread is collecting. Check which. */
5328 if (read_inferior_memory (ipa_collecting
,
5329 (unsigned char *) &ipa_collecting_obj
,
5330 sizeof (ipa_collecting_obj
)) != 0)
5333 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5335 trace_debug ("fast_tracepoint_collecting: not collecting "
5336 "(another thread is)");
5341 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5344 warning ("fast_tracepoint_collecting: collecting, "
5345 "but tpoint %s not found?",
5346 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5350 /* The thread is within `gdb_collect', skip over the rest of
5351 fast tracepoint collection quickly using a breakpoint. */
5352 needs_breakpoint
= 1;
5355 /* The caller wants a bit of status detail. */
5358 status
->tpoint_num
= tpoint
->number
;
5359 status
->tpoint_addr
= tpoint
->address
;
5360 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5361 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5364 if (needs_breakpoint
)
5366 /* Hasn't executed the original instruction yet. Set breakpoint
5367 there, and wait till it's hit, then single-step until exiting
5371 fast_tracepoint_collecting, returning continue-until-break at %s",
5372 paddress (tpoint
->adjusted_insn_addr
));
5374 return 1; /* continue */
5378 /* Just single-step until exiting the jump pad. */
5380 trace_debug ("fast_tracepoint_collecting, returning "
5381 "need-single-step (%s-%s)",
5382 paddress (tpoint
->adjusted_insn_addr
),
5383 paddress (tpoint
->adjusted_insn_addr_end
));
5385 return 2; /* single-step */
5391 #ifdef IN_PROCESS_AGENT
5393 /* The global fast tracepoint collect lock. Points to a collecting_t
5394 object built on the stack by the jump pad, if presently locked;
5395 NULL if it isn't locked. Note that this lock *must* be set while
5396 executing any *function other than the jump pad. See
5397 fast_tracepoint_collecting. */
5398 static collecting_t
* ATTR_USED collecting
;
5400 /* This routine, called from the jump pad (in asm) is designed to be
5401 called from the jump pads of fast tracepoints, thus it is on the
5404 IP_AGENT_EXPORT
void ATTR_USED
5405 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5407 struct fast_tracepoint_ctx ctx
;
5409 /* Don't do anything until the trace run is completely set up. */
5413 ctx
.base
.type
= fast_tracepoint
;
5415 ctx
.regcache_initted
= 0;
5416 /* Wrap the regblock in a register cache (in the stack, we don't
5417 want to malloc here). */
5418 ctx
.regspace
= alloca (register_cache_size ());
5419 if (ctx
.regspace
== NULL
)
5421 trace_debug ("Trace buffer block allocation failed, skipping");
5425 for (ctx
.tpoint
= tpoint
;
5426 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5427 ctx
.tpoint
= ctx
.tpoint
->next
)
5429 if (!ctx
.tpoint
->enabled
)
5432 /* Multiple tracepoints of different types, such as fast tracepoint and
5433 static tracepoint, can be set at the same address. */
5434 if (ctx
.tpoint
->type
!= tpoint
->type
)
5437 /* Test the condition if present, and collect if true. */
5438 if (ctx
.tpoint
->cond
== NULL
5439 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5442 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5443 ctx
.tpoint
->address
, ctx
.tpoint
);
5445 /* Note that this will cause original insns to be written back
5446 to where we jumped from, but that's OK because we're jumping
5447 back to the next whole instruction. This will go badly if
5448 instruction restoration is not atomic though. */
5449 if (stopping_tracepoint
5450 || trace_buffer_is_full
5451 || expr_eval_result
!= expr_eval_no_error
)
5459 /* If there was a condition and it evaluated to false, the only
5460 way we would stop tracing is if there was an error during
5461 condition expression evaluation. */
5462 if (expr_eval_result
!= expr_eval_no_error
)
5473 #ifndef IN_PROCESS_AGENT
5476 get_raw_reg_func_addr (void)
5478 return ipa_sym_addrs
.addr_get_raw_reg
;
5482 get_get_tsv_func_addr (void)
5484 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5488 get_set_tsv_func_addr (void)
5490 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5494 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5495 CORE_ADDR
*jump_entry
)
5497 CORE_ADDR entry_point
= *jump_entry
;
5498 enum eval_result_type err
;
5500 trace_debug ("Starting condition compilation for tracepoint %d\n",
5503 /* Initialize the global pointer to the code being built. */
5504 current_insn_ptr
= *jump_entry
;
5508 err
= compile_bytecodes (tpoint
->cond
);
5510 if (err
== expr_eval_no_error
)
5514 /* Record the beginning of the compiled code. */
5515 tpoint
->compiled_cond
= entry_point
;
5517 trace_debug ("Condition compilation for tracepoint %d complete\n",
5522 /* Leave the unfinished code in situ, but don't point to it. */
5524 tpoint
->compiled_cond
= 0;
5526 trace_debug ("Condition compilation for tracepoint %d failed, "
5528 tpoint
->number
, err
);
5531 /* Update the code pointer passed in. Note that we do this even if
5532 the compile fails, so that we can look at the partial results
5533 instead of letting them be overwritten. */
5534 *jump_entry
= current_insn_ptr
;
5536 /* Leave a gap, to aid dump decipherment. */
5540 /* We'll need to adjust these when we consider bi-arch setups, and big
5544 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5546 return write_inferior_memory (where
,
5547 (unsigned char *) &ptr
, sizeof (void *));
5550 /* The base pointer of the IPA's heap. This is the only memory the
5551 IPA is allowed to use. The IPA should _not_ call the inferior's
5552 `malloc' during operation. That'd be slow, and, most importantly,
5553 it may not be safe. We may be collecting a tracepoint in a signal
5554 handler, for example. */
5555 static CORE_ADDR target_tp_heap
;
5557 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5561 target_malloc (ULONGEST size
)
5565 if (target_tp_heap
== 0)
5567 /* We have the pointer *address*, need what it points to. */
5568 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5570 fatal ("could get target heap head pointer");
5573 ptr
= target_tp_heap
;
5574 target_tp_heap
+= size
;
5576 /* Pad to 8-byte alignment. */
5577 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5583 download_agent_expr (struct agent_expr
*expr
)
5585 CORE_ADDR expr_addr
;
5586 CORE_ADDR expr_bytes
;
5588 expr_addr
= target_malloc (sizeof (*expr
));
5589 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5591 expr_bytes
= target_malloc (expr
->length
);
5592 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5594 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5599 /* Align V up to N bits. */
5600 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5602 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5605 download_tracepoint_1 (struct tracepoint
*tpoint
)
5607 struct tracepoint target_tracepoint
;
5608 CORE_ADDR tpptr
= 0;
5610 gdb_assert (tpoint
->type
== fast_tracepoint
5611 || tpoint
->type
== static_tracepoint
);
5613 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5615 CORE_ADDR jentry
, jump_entry
;
5617 jentry
= jump_entry
= get_jump_space_head ();
5619 if (tpoint
->cond
!= NULL
)
5621 /* Pad to 8-byte alignment. (needed?) */
5622 /* Actually this should be left for the target to
5624 jentry
= UALIGN (jentry
, 8);
5626 compile_tracepoint_condition (tpoint
, &jentry
);
5629 /* Pad to 8-byte alignment. */
5630 jentry
= UALIGN (jentry
, 8);
5631 claim_jump_space (jentry
- jump_entry
);
5634 target_tracepoint
= *tpoint
;
5636 tpptr
= target_malloc (sizeof (*tpoint
));
5637 tpoint
->obj_addr_on_target
= tpptr
;
5639 /* Write the whole object. We'll fix up its pointers in a bit.
5640 Assume no next for now. This is fixed up above on the next
5641 iteration, if there's any. */
5642 target_tracepoint
.next
= NULL
;
5643 /* Need to clear this here too, since we're downloading the
5644 tracepoints before clearing our own copy. */
5645 target_tracepoint
.hit_count
= 0;
5647 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5648 sizeof (target_tracepoint
));
5651 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5653 download_agent_expr (tpoint
->cond
));
5655 if (tpoint
->numactions
)
5658 CORE_ADDR actions_array
;
5660 /* The pointers array. */
5662 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
5663 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5667 /* Now for each pointer, download the action. */
5668 for (i
= 0; i
< tpoint
->numactions
; i
++)
5670 CORE_ADDR ipa_action
= 0;
5671 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5673 switch (action
->type
)
5677 = target_malloc (sizeof (struct collect_memory_action
));
5678 write_inferior_memory (ipa_action
,
5679 (unsigned char *) action
,
5680 sizeof (struct collect_memory_action
));
5684 = target_malloc (sizeof (struct collect_registers_action
));
5685 write_inferior_memory (ipa_action
,
5686 (unsigned char *) action
,
5687 sizeof (struct collect_registers_action
));
5692 struct eval_expr_action
*eaction
5693 = (struct eval_expr_action
*) action
;
5695 ipa_action
= target_malloc (sizeof (*eaction
));
5696 write_inferior_memory (ipa_action
,
5697 (unsigned char *) eaction
,
5700 expr
= download_agent_expr (eaction
->expr
);
5701 write_inferior_data_ptr
5702 (ipa_action
+ offsetof (struct eval_expr_action
, expr
),
5707 ipa_action
= target_malloc
5708 (sizeof (struct collect_static_trace_data_action
));
5709 write_inferior_memory
5711 (unsigned char *) action
,
5712 sizeof (struct collect_static_trace_data_action
));
5715 trace_debug ("unknown trace action '%c', ignoring",
5720 if (ipa_action
!= 0)
5721 write_inferior_data_ptr
5722 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
5729 download_tracepoint (struct tracepoint
*tpoint
)
5731 struct tracepoint
*tp
, *tp_prev
;
5733 if (tpoint
->type
!= fast_tracepoint
5734 && tpoint
->type
!= static_tracepoint
)
5737 download_tracepoint_1 (tpoint
);
5739 /* Find the previous entry of TPOINT, which is fast tracepoint or
5740 static tracepoint. */
5742 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
5744 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
5750 CORE_ADDR tp_prev_target_next_addr
;
5752 /* Insert TPOINT after TP_PREV in IPA. */
5753 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
5754 + offsetof (struct tracepoint
, next
),
5755 &tp_prev_target_next_addr
))
5756 fatal ("error reading `tp_prev->next'");
5758 /* tpoint->next = tp_prev->next */
5759 write_inferior_data_ptr (tpoint
->obj_addr_on_target
5760 + offsetof (struct tracepoint
, next
),
5761 tp_prev_target_next_addr
);
5762 /* tp_prev->next = tpoint */
5763 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
5764 + offsetof (struct tracepoint
, next
),
5765 tpoint
->obj_addr_on_target
);
5768 /* First object in list, set the head pointer in the
5770 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
5771 tpoint
->obj_addr_on_target
);
5776 download_tracepoints (void)
5778 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
5779 struct tracepoint
*tpoint
;
5781 /* Start out empty. */
5782 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
5784 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5786 if (tpoint
->type
!= fast_tracepoint
5787 && tpoint
->type
!= static_tracepoint
)
5792 download_tracepoint_1 (tpoint
);
5794 tpptr
= tpoint
->obj_addr_on_target
;
5796 if (tpoint
== tracepoints
)
5798 /* First object in list, set the head pointer in the
5800 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
5804 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
5812 download_trace_state_variables (void)
5814 CORE_ADDR ptr
= 0, prev_ptr
= 0;
5815 struct trace_state_variable
*tsv
;
5817 /* Start out empty. */
5818 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
5820 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
5822 struct trace_state_variable target_tsv
;
5824 /* TSV's with a getter have been initialized equally in both the
5825 inferior and GDBserver. Skip them. */
5826 if (tsv
->getter
!= NULL
)
5832 ptr
= target_malloc (sizeof (*tsv
));
5834 if (tsv
== trace_state_variables
)
5836 /* First object in list, set the head pointer in the
5839 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
5844 write_inferior_data_ptr (prev_ptr
5845 + offsetof (struct trace_state_variable
,
5850 /* Write the whole object. We'll fix up its pointers in a bit.
5851 Assume no next, fixup when needed. */
5852 target_tsv
.next
= NULL
;
5854 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
5855 sizeof (target_tsv
));
5857 if (tsv
->name
!= NULL
)
5859 size_t size
= strlen (tsv
->name
) + 1;
5860 CORE_ADDR name_addr
= target_malloc (size
);
5861 write_inferior_memory (name_addr
,
5862 (unsigned char *) tsv
->name
, size
);
5863 write_inferior_data_ptr (ptr
5864 + offsetof (struct trace_state_variable
,
5869 if (tsv
->getter
!= NULL
)
5871 fatal ("what to do with these?");
5877 /* Fixup the next pointer in the last item in the list. */
5878 write_inferior_data_ptr (prev_ptr
5879 + offsetof (struct trace_state_variable
,
5884 /* Upload complete trace frames out of the IP Agent's trace buffer
5885 into GDBserver's trace buffer. This always uploads either all or
5886 no trace frames. This is the counter part of
5887 `trace_alloc_trace_buffer'. See its description of the atomic
5888 synching mechanism. */
5891 upload_fast_traceframes (void)
5893 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
5894 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
5896 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
5897 unsigned int curr_tbctrl_idx
;
5898 unsigned int ipa_trace_buffer_ctrl_curr
;
5899 unsigned int ipa_trace_buffer_ctrl_curr_old
;
5900 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
5901 struct breakpoint
*about_to_request_buffer_space_bkpt
;
5902 CORE_ADDR ipa_trace_buffer_lo
;
5903 CORE_ADDR ipa_trace_buffer_hi
;
5905 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
5906 &ipa_traceframe_read_count_racy
))
5908 /* This will happen in most targets if the current thread is
5913 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
5914 &ipa_traceframe_write_count_racy
))
5917 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
5918 ipa_traceframe_write_count_racy
5919 - ipa_traceframe_read_count_racy
,
5920 ipa_traceframe_write_count_racy
,
5921 ipa_traceframe_read_count_racy
);
5923 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
5926 about_to_request_buffer_space_bkpt
5927 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
5930 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
5931 &ipa_trace_buffer_ctrl_curr
))
5934 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
5936 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
5939 unsigned int prev
, counter
;
5941 /* Update the token, with new counters, and the GDBserver stamp
5942 bit. Alway reuse the current TBC index. */
5943 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
5944 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
5946 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
5952 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
5953 ipa_trace_buffer_ctrl_curr
))
5956 trace_debug ("Lib: Committed %08x -> %08x",
5957 ipa_trace_buffer_ctrl_curr_old
,
5958 ipa_trace_buffer_ctrl_curr
);
5960 /* Re-read these, now that we've installed the
5961 `about_to_request_buffer_space' breakpoint/lock. A thread could
5962 have finished a traceframe between the last read of these
5963 counters and setting the breakpoint above. If we start
5964 uploading, we never want to leave this function with
5965 traceframe_read_count != 0, otherwise, GDBserver could end up
5966 incrementing the counter tokens more than once (due to event loop
5967 nesting), which would break the IP agent's "effective" detection
5968 (see trace_alloc_trace_buffer). */
5969 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
5970 &ipa_traceframe_read_count
))
5972 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
5973 &ipa_traceframe_write_count
))
5978 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
5979 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
5980 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
5982 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
5983 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
5984 trace_debug ("note that ipa_traceframe_count's parts changed");
5987 /* Get the address of the current TBC object (the IP agent has an
5988 array of 3 such objects). The index is stored in the TBC
5990 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
5991 ipa_trace_buffer_ctrl_addr
5992 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
5994 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
5995 (unsigned char *) &ipa_trace_buffer_ctrl
,
5996 sizeof (struct ipa_trace_buffer_control
)))
5999 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6000 &ipa_trace_buffer_lo
))
6002 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6003 &ipa_trace_buffer_hi
))
6006 /* Offsets are easier to grok for debugging than raw addresses,
6007 especially for the small trace buffer sizes that are useful for
6009 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6010 "endfree=%d wrap=%d hi=%d",
6012 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6013 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6014 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6015 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6016 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6018 /* Note that the IPA's buffer is always circular. */
6020 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6022 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6023 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6025 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6026 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6027 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6028 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6031 tf
= IPA_FIRST_TRACEFRAME ();
6033 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6035 struct tracepoint
*tpoint
;
6036 struct traceframe
*tframe
;
6037 unsigned char *block
;
6038 struct traceframe ipa_tframe
;
6040 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6041 offsetof (struct traceframe
, data
)))
6042 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6044 if (ipa_tframe
.tpnum
== 0)
6045 fatal ("Uploading: No (more) fast traceframes, but "
6046 "ipa_traceframe_count == %u??\n",
6047 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6049 /* Note that this will be incorrect for multi-location
6051 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6053 tframe
= add_traceframe (tpoint
);
6056 trace_buffer_is_full
= 1;
6057 trace_debug ("Uploading: trace buffer is full");
6061 /* Copy the whole set of blocks in one go for now. FIXME:
6062 split this in smaller blocks. */
6063 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6066 if (read_inferior_memory (tf
6067 + offsetof (struct traceframe
, data
),
6068 block
, ipa_tframe
.data_size
))
6069 error ("Uploading: Couldn't read traceframe data at %s\n",
6070 paddress (tf
+ offsetof (struct traceframe
, data
)));
6073 trace_debug ("Uploading: traceframe didn't fit");
6074 finish_traceframe (tframe
);
6077 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6079 /* If we freed the traceframe that wrapped around, go back
6080 to the non-wrap case. */
6081 if (tf
< ipa_trace_buffer_ctrl
.start
)
6083 trace_debug ("Lib: Discarding past the wraparound");
6084 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6086 ipa_trace_buffer_ctrl
.start
= tf
;
6087 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6088 ++ipa_traceframe_read_count
;
6090 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6091 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6093 trace_debug ("Lib: buffer is fully empty. "
6094 "Trace buffer [%d] start=%d free=%d endfree=%d",
6096 (int) (ipa_trace_buffer_ctrl
.start
6097 - ipa_trace_buffer_lo
),
6098 (int) (ipa_trace_buffer_ctrl
.free
6099 - ipa_trace_buffer_lo
),
6100 (int) (ipa_trace_buffer_ctrl
.end_free
6101 - ipa_trace_buffer_lo
));
6103 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6104 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6105 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6106 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6109 trace_debug ("Uploaded a traceframe\n"
6110 "Lib: Trace buffer [%d] start=%d free=%d "
6111 "endfree=%d wrap=%d hi=%d",
6113 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6114 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6115 (int) (ipa_trace_buffer_ctrl
.end_free
6116 - ipa_trace_buffer_lo
),
6117 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6118 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6121 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6122 (unsigned char *) &ipa_trace_buffer_ctrl
,
6123 sizeof (struct ipa_trace_buffer_control
)))
6126 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6127 ipa_traceframe_read_count
);
6129 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6132 cancel_breakpoints ();
6134 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6135 about_to_request_buffer_space_bkpt
= NULL
;
6139 if (trace_buffer_is_full
)
6144 #ifdef IN_PROCESS_AGENT
6146 IP_AGENT_EXPORT
int ust_loaded
;
6147 IP_AGENT_EXPORT
char cmd_buf
[CMD_BUF_SIZE
];
6151 /* Static tracepoints. */
6153 /* UST puts a "struct tracepoint" in the global namespace, which
6154 conflicts with our tracepoint. Arguably, being a library, it
6155 shouldn't take ownership of such a generic name. We work around it
6157 #define tracepoint ust_tracepoint
6158 #include <ust/ust.h>
6161 extern int serialize_to_text (char *outbuf
, int bufsize
,
6162 const char *fmt
, va_list ap
);
6164 #define GDB_PROBE_NAME "gdb"
6166 /* We dynamically search for the UST symbols instead of linking them
6167 in. This lets the user decide if the application uses static
6168 tracepoints, instead of always pulling libust.so in. This vector
6169 holds pointers to all functions we care about. */
6173 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6174 const char *fmt
, va_list ap
);
6176 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6177 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6179 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6181 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6184 void (*marker_iter_start
) (struct marker_iter
*iter
);
6185 void (*marker_iter_next
) (struct marker_iter
*iter
);
6186 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6187 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6192 /* Cast through typeof to catch incompatible API changes. Since UST
6193 only builds with gcc, we can freely use gcc extensions here
6195 #define GET_UST_SYM(SYM) \
6198 if (ust_ops.SYM == NULL) \
6199 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6200 if (ust_ops.SYM == NULL) \
6204 #define USTF(SYM) ust_ops.SYM
6206 /* Get pointers to all libust.so functions we care about. */
6211 GET_UST_SYM (serialize_to_text
);
6213 GET_UST_SYM (ltt_probe_register
);
6214 GET_UST_SYM (ltt_probe_unregister
);
6215 GET_UST_SYM (ltt_marker_connect
);
6216 GET_UST_SYM (ltt_marker_disconnect
);
6218 GET_UST_SYM (marker_iter_start
);
6219 GET_UST_SYM (marker_iter_next
);
6220 GET_UST_SYM (marker_iter_stop
);
6221 GET_UST_SYM (marker_iter_reset
);
6227 /* Given an UST marker, return the matching gdb static tracepoint.
6228 The match is done by address. */
6230 static struct tracepoint
*
6231 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6233 struct tracepoint
*tpoint
;
6235 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6237 if (tpoint
->type
!= static_tracepoint
)
6240 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6247 /* The probe function we install on lttng/ust markers. Whenever a
6248 probed ust marker is hit, this function is called. This is similar
6249 to gdb_collect, only for static tracepoints, instead of fast
6253 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6254 struct registers
*regs
, void *call_private
,
6255 const char *fmt
, va_list *args
)
6257 struct tracepoint
*tpoint
;
6258 struct static_tracepoint_ctx ctx
;
6260 /* Don't do anything until the trace run is completely set up. */
6263 trace_debug ("gdb_probe: not tracing\n");
6267 ctx
.base
.type
= static_tracepoint
;
6268 ctx
.regcache_initted
= 0;
6273 /* Wrap the regblock in a register cache (in the stack, we don't
6274 want to malloc here). */
6275 ctx
.regspace
= alloca (register_cache_size ());
6276 if (ctx
.regspace
== NULL
)
6278 trace_debug ("Trace buffer block allocation failed, skipping");
6282 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6285 trace_debug ("gdb_probe: marker not known: "
6286 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6287 mdata
->location
, mdata
->channel
,
6288 mdata
->name
, mdata
->format
);
6292 if (!tpoint
->enabled
)
6294 trace_debug ("gdb_probe: tracepoint disabled");
6298 ctx
.tpoint
= tpoint
;
6300 trace_debug ("gdb_probe: collecting marker: "
6301 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6302 mdata
->location
, mdata
->channel
,
6303 mdata
->name
, mdata
->format
);
6305 /* Test the condition if present, and collect if true. */
6306 if (tpoint
->cond
== NULL
6307 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6310 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6311 tpoint
->address
, tpoint
);
6313 if (stopping_tracepoint
6314 || trace_buffer_is_full
6315 || expr_eval_result
!= expr_eval_no_error
)
6320 /* If there was a condition and it evaluated to false, the only
6321 way we would stop tracing is if there was an error during
6322 condition expression evaluation. */
6323 if (expr_eval_result
!= expr_eval_no_error
)
6328 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6329 static tracepoint string data. This is a string passed to the
6330 tracing library by the user, at the time of the tracepoint marker
6331 call. E.g., in the UST marker call:
6333 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6335 the collected data is "str FOOBAZ".
6339 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6340 struct traceframe
*tframe
)
6342 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6343 unsigned char *bufspace
;
6346 unsigned short blocklen
;
6350 trace_debug ("Wanted to collect static trace data, "
6351 "but there's no static trace data");
6355 va_copy (copy
, *umd
->args
);
6356 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6359 trace_debug ("Want to collect ust data");
6361 /* 'S' + size + string */
6362 bufspace
= add_traceframe_block (tframe
,
6363 1 + sizeof (blocklen
) + size
+ 1);
6364 if (bufspace
== NULL
)
6366 trace_debug ("Trace buffer block allocation failed, skipping");
6370 /* Identify a static trace data block. */
6373 blocklen
= size
+ 1;
6374 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6376 va_copy (copy
, *umd
->args
);
6377 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6378 size
+ 1, umd
->fmt
, copy
);
6381 trace_debug ("Storing static tracepoint data in regblock: %s",
6382 bufspace
+ 1 + sizeof (blocklen
));
6385 /* The probe to register with lttng/ust. */
6386 static struct ltt_available_probe gdb_ust_probe
=
6393 #endif /* HAVE_UST */
6394 #endif /* IN_PROCESS_AGENT */
6398 #include <sys/socket.h>
6401 #ifndef UNIX_PATH_MAX
6402 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6405 /* Where we put the socked used for synchronization. */
6406 #define SOCK_DIR P_tmpdir
6408 #endif /* HAVE_UST */
6410 #ifndef IN_PROCESS_AGENT
6415 gdb_ust_connect_sync_socket (int pid
)
6417 struct sockaddr_un addr
;
6419 char path
[UNIX_PATH_MAX
];
6421 res
= xsnprintf (path
, UNIX_PATH_MAX
, "%s/gdb_ust%d", SOCK_DIR
, pid
);
6422 if (res
>= UNIX_PATH_MAX
)
6424 trace_debug ("string overflow allocating socket name");
6428 res
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6431 warning ("error opening sync socket: %s\n", strerror (errno
));
6435 addr
.sun_family
= AF_UNIX
;
6437 res
= xsnprintf (addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
6438 if (res
>= UNIX_PATH_MAX
)
6440 warning ("string overflow allocating socket name\n");
6445 res
= connect (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6448 warning ("error connecting sync socket (%s): %s. "
6449 "Make sure the directory exists and that it is writable.",
6450 path
, strerror (errno
));
6458 /* Resume thread PTID. */
6461 resume_thread (ptid_t ptid
)
6463 struct thread_resume resume_info
;
6465 resume_info
.thread
= ptid
;
6466 resume_info
.kind
= resume_continue
;
6467 resume_info
.sig
= TARGET_SIGNAL_0
;
6468 (*the_target
->resume
) (&resume_info
, 1);
6471 /* Stop thread PTID. */
6474 stop_thread (ptid_t ptid
)
6476 struct thread_resume resume_info
;
6478 resume_info
.thread
= ptid
;
6479 resume_info
.kind
= resume_stop
;
6480 resume_info
.sig
= TARGET_SIGNAL_0
;
6481 (*the_target
->resume
) (&resume_info
, 1);
6484 /* Ask the in-process agent to run a command. Since we don't want to
6485 have to handle the IPA hitting breakpoints while running the
6486 command, we pause all threads, remove all breakpoints, and then set
6487 the helper thread re-running. We communicate with the helper
6488 thread by means of direct memory xfering, and a socket for
6492 run_inferior_command (char *cmd
)
6496 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
6498 ptid_t ptid
= null_ptid
;
6500 trace_debug ("run_inferior_command: running: %s", cmd
);
6503 uninsert_all_breakpoints ();
6505 if (read_inferior_integer (ipa_sym_addrs
.addr_helper_thread_id
, &tid
))
6507 warning ("Error reading helper thread's id in lib");
6513 warning ("helper thread not initialized yet");
6517 if (write_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
6518 (unsigned char *) cmd
, strlen (cmd
) + 1))
6520 warning ("Error writing command");
6524 ptid
= ptid_build (pid
, tid
, 0);
6526 resume_thread (ptid
);
6528 fd
= gdb_ust_connect_sync_socket (pid
);
6534 trace_debug ("signalling helper thread");
6538 ret
= write (fd
, buf
, 1);
6539 } while (ret
== -1 && errno
== EINTR
);
6541 trace_debug ("waiting for helper thread's response");
6545 ret
= read (fd
, buf
, 1);
6546 } while (ret
== -1 && errno
== EINTR
);
6550 trace_debug ("helper thread's response received");
6555 /* Need to read response with the inferior stopped. */
6556 if (!ptid_equal (ptid
, null_ptid
))
6558 int was_non_stop
= non_stop
;
6559 struct target_waitstatus status
;
6563 mywait (ptid
, &status
, 0, 0);
6564 non_stop
= was_non_stop
;
6569 if (read_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
6570 (unsigned char *) cmd
, CMD_BUF_SIZE
))
6572 warning ("Error reading command response");
6577 trace_debug ("run_inferior_command: response: %s", cmd
);
6581 reinsert_all_breakpoints ();
6587 #else /* HAVE_UST */
6590 run_inferior_command (char *cmd
)
6595 #endif /* HAVE_UST */
6597 #else /* !IN_PROCESS_AGENT */
6599 /* Thread ID of the helper thread. GDBserver reads this to know which
6600 is the help thread. This is an LWP id on Linux. */
6601 int helper_thread_id
;
6606 init_named_socket (const char *name
)
6609 struct sockaddr_un addr
;
6611 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6614 warning ("socket creation failed: %s", strerror (errno
));
6618 addr
.sun_family
= AF_UNIX
;
6620 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6621 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6623 result
= access (name
, F_OK
);
6627 result
= unlink (name
);
6630 warning ("unlink failed: %s", strerror (errno
));
6634 warning ("socket %s already exists; overwriting", name
);
6637 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6640 warning ("bind failed: %s", strerror (errno
));
6645 result
= listen (fd
, 1);
6648 warning ("listen: %s", strerror (errno
));
6657 gdb_ust_socket_init (void)
6660 char name
[UNIX_PATH_MAX
];
6662 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6663 SOCK_DIR
, getpid ());
6664 if (result
>= UNIX_PATH_MAX
)
6666 trace_debug ("string overflow allocating socket name");
6670 fd
= init_named_socket (name
);
6672 warning ("Error initializing named socket (%s) for communication with the "
6673 "ust helper thread. Check that directory exists and that it "
6674 "is writable.", name
);
6679 /* Return an hexstr version of the STR C string, fit for sending to
6683 cstr_to_hexstr (const char *str
)
6685 int len
= strlen (str
);
6686 char *hexstr
= xmalloc (len
* 2 + 1);
6687 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6691 /* The next marker to be returned on a qTsSTM command. */
6692 static const struct marker
*next_st
;
6694 /* Returns the first known marker. */
6699 struct marker_iter iter
;
6701 USTF(marker_iter_reset
) (&iter
);
6702 USTF(marker_iter_start
) (&iter
);
6707 /* Returns the marker following M. */
6709 const struct marker
*
6710 next_marker (const struct marker
*m
)
6712 struct marker_iter iter
;
6714 USTF(marker_iter_reset
) (&iter
);
6715 USTF(marker_iter_start
) (&iter
);
6717 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6719 if (iter
.marker
== m
)
6721 USTF(marker_iter_next
) (&iter
);
6729 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6733 response_ust_marker (char *packet
, const struct marker
*st
)
6735 char *strid
, *format
, *tmp
;
6737 next_st
= next_marker (st
);
6739 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6740 strlen (st
->name
) + 1);
6741 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6743 strid
= cstr_to_hexstr (tmp
);
6746 format
= cstr_to_hexstr (st
->format
);
6748 sprintf (packet
, "m%s:%s:%s",
6749 paddress ((uintptr_t) st
->location
),
6757 /* Return the first static tracepoint, and initialize the state
6758 machine that will iterate through all the static tracepoints. */
6761 cmd_qtfstm (char *packet
)
6763 trace_debug ("Returning first trace state variable definition");
6765 if (first_marker ())
6766 response_ust_marker (packet
, first_marker ());
6768 strcpy (packet
, "l");
6771 /* Return additional trace state variable definitions. */
6774 cmd_qtsstm (char *packet
)
6776 trace_debug ("Returning static tracepoint");
6779 response_ust_marker (packet
, next_st
);
6781 strcpy (packet
, "l");
6784 /* Disconnect the GDB probe from a marker at a given address. */
6787 unprobe_marker_at (char *packet
)
6791 struct marker_iter iter
;
6793 p
+= sizeof ("unprobe_marker_at:") - 1;
6795 p
= unpack_varlen_hex (p
, &address
);
6797 USTF(marker_iter_reset
) (&iter
);
6798 USTF(marker_iter_start
) (&iter
);
6799 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6800 if ((uintptr_t ) iter
.marker
->location
== address
)
6804 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
6808 warning ("could not disable marker %s/%s",
6809 iter
.marker
->channel
, iter
.marker
->name
);
6814 /* Connect the GDB probe to a marker at a given address. */
6817 probe_marker_at (char *packet
)
6821 struct marker_iter iter
;
6824 p
+= sizeof ("probe_marker_at:") - 1;
6826 p
= unpack_varlen_hex (p
, &address
);
6828 USTF(marker_iter_reset
) (&iter
);
6830 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6832 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6833 if ((uintptr_t ) m
->location
== address
)
6837 trace_debug ("found marker for address. "
6838 "ltt_marker_connect (marker = %s/%s)",
6839 m
->channel
, m
->name
);
6841 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
6843 if (result
&& result
!= -EEXIST
)
6844 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
6845 m
->channel
, m
->name
, -result
);
6849 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
6850 m
->channel
, m
->name
);
6854 strcpy (packet
, "OK");
6858 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
6863 cmd_qtstmat (char *packet
)
6867 struct marker_iter iter
;
6870 p
+= sizeof ("qTSTMat:") - 1;
6872 p
= unpack_varlen_hex (p
, &address
);
6874 USTF(marker_iter_reset
) (&iter
);
6876 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6878 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6879 if ((uintptr_t ) m
->location
== address
)
6881 response_ust_marker (packet
, m
);
6885 strcpy (packet
, "l");
6889 #include <sys/syscall.h>
6892 gdb_ust_thread (void *arg
)
6898 listen_fd
= gdb_ust_socket_init ();
6900 if (helper_thread_id
== 0)
6901 helper_thread_id
= syscall (SYS_gettid
);
6903 if (listen_fd
== -1)
6905 warning ("could not create sync socket\n");
6912 struct sockaddr_un sockaddr
;
6917 tmp
= sizeof (sockaddr
);
6921 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
6923 /* It seems an ERESTARTSYS can escape out of accept. */
6924 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
6928 warning ("Accept returned %d, error: %s\n",
6929 fd
, strerror (errno
));
6935 ret
= read (fd
, buf
, 1);
6936 } while (ret
== -1 && errno
== EINTR
);
6940 warning ("reading socket (fd=%d) failed with %s",
6941 fd
, strerror (errno
));
6948 if (strcmp ("qTfSTM", cmd_buf
) == 0)
6950 cmd_qtfstm (cmd_buf
);
6952 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
6954 cmd_qtsstm (cmd_buf
);
6956 else if (strncmp ("unprobe_marker_at:",
6958 sizeof ("unprobe_marker_at:") - 1) == 0)
6960 unprobe_marker_at (cmd_buf
);
6962 else if (strncmp ("probe_marker_at:",
6964 sizeof ("probe_marker_at:") - 1) == 0)
6966 probe_marker_at (cmd_buf
);
6968 else if (strncmp ("qTSTMat:",
6970 sizeof ("qTSTMat:") - 1) == 0)
6972 cmd_qtstmat (cmd_buf
);
6974 else if (strcmp (cmd_buf
, "help") == 0)
6976 strcpy (cmd_buf
, "for help, press F1\n");
6979 strcpy (cmd_buf
, "");
6982 /* Fix compiler's warning: ignoring return value of 'write'. */
6983 ret
= write (fd
, buf
, 1);
7004 /* We want the helper thread to be as transparent as possible, so
7005 have it inherit an all-signals-blocked mask. */
7007 sigfillset (&new_mask
);
7008 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7010 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7012 res
= pthread_create (&thread
,
7017 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7019 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7021 while (helper_thread_id
== 0)
7024 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7027 #endif /* HAVE_UST */
7029 #include <sys/mman.h>
7032 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7033 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7034 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7035 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
7036 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
7037 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
7039 /* Record the result of getting buffer space for fast tracepoint
7040 trampolines. Any error message is copied, since caller may not be
7041 using persistent storage. */
7044 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7046 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7047 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7049 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7051 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7054 static void __attribute__ ((constructor
))
7055 initialize_tracepoint_ftlib (void)
7057 initialize_tracepoint ();
7064 #endif /* IN_PROCESS_AGENT */
7066 /* Return a timestamp, expressed as microseconds of the usual Unix
7067 time. (As the result is a 64-bit number, it will not overflow any
7071 get_timestamp (void)
7075 if (gettimeofday (&tv
, 0) != 0)
7078 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7082 initialize_tracepoint (void)
7084 /* There currently no way to change the buffer size. */
7085 const int sizeOfBuffer
= 5 * 1024 * 1024;
7086 unsigned char *buf
= xmalloc (sizeOfBuffer
);
7087 init_trace_buffer (buf
, sizeOfBuffer
);
7089 /* Wire trace state variable 1 to be the timestamp. This will be
7090 uploaded to GDB upon connection and become one of its trace state
7091 variables. (In case you're wondering, if GDB already has a trace
7092 variable numbered 1, it will be renumbered.) */
7093 create_trace_state_variable (1, 0);
7094 set_trace_state_variable_name (1, "trace_timestamp");
7095 set_trace_state_variable_getter (1, get_timestamp
);
7097 #ifdef IN_PROCESS_AGENT
7102 pagesize
= sysconf (_SC_PAGE_SIZE
);
7106 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7108 #define SCRATCH_BUFFER_NPAGES 20
7110 /* Allocate scratch buffer aligned on a page boundary, at a low
7111 address (close to the main executable's code). */
7112 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7114 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7115 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7116 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7118 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7124 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7127 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7130 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7132 /* It's not a fatal error for something to go wrong with trampoline
7133 buffer setup, but it can be mysterious, so create a channel to
7134 report back on what went wrong, using a fixed size since we may
7135 not be able to allocate space later when the problem occurs. */
7136 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7138 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7140 initialize_low_tracepoint ();