1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2016 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "tracepoint.h"
21 #include "gdbthread.h"
28 #include "gdb_sys_time.h"
33 #define DEFAULT_TRACE_BUFFER_SIZE 5242880 /* 5*1024*1024 */
35 /* This file is built for both GDBserver, and the in-process
36 agent (IPA), a shared library that includes a tracing agent that is
37 loaded by the inferior to support fast tracepoints. Fast
38 tracepoints (or more accurately, jump based tracepoints) are
39 implemented by patching the tracepoint location with a jump into a
40 small trampoline function whose job is to save the register state,
41 call the in-process tracing agent, and then execute the original
42 instruction that was under the tracepoint jump (possibly adjusted,
43 if PC-relative, or some such).
45 The current synchronization design is pull based. That means,
46 GDBserver does most of the work, by peeking/poking at the inferior
47 agent's memory directly for downloading tracepoint and associated
48 objects, and for uploading trace frames. Whenever the IPA needs
49 something from GDBserver (trace buffer is full, tracing stopped for
50 some reason, etc.) the IPA calls a corresponding hook function
51 where GDBserver has placed a breakpoint.
53 Each of the agents has its own trace buffer. When browsing the
54 trace frames built from slow and fast tracepoints from GDB (tfind
55 mode), there's no guarantee the user is seeing the trace frames in
56 strict chronological creation order, although, GDBserver tries to
57 keep the order relatively reasonable, by syncing the trace buffers
62 #ifdef IN_PROCESS_AGENT
64 static void trace_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
67 trace_vdebug (const char *fmt
, ...)
73 vsprintf (buf
, fmt
, ap
);
74 fprintf (stderr
, PROG
"/tracepoint: %s\n", buf
);
78 #define trace_debug_1(level, fmt, args...) \
80 if (level <= debug_threads) \
81 trace_vdebug ((fmt), ##args); \
86 #define trace_debug_1(level, fmt, args...) \
88 if (level <= debug_threads) \
90 debug_printf ((fmt), ##args); \
91 debug_printf ("\n"); \
97 #define trace_debug(FMT, args...) \
98 trace_debug_1 (1, FMT, ##args)
100 /* Prefix exported symbols, for good citizenship. All the symbols
101 that need exporting are defined in this module. Note that all
102 these symbols must be tagged with IP_AGENT_EXPORT_*. */
103 #ifdef IN_PROCESS_AGENT
104 # define gdb_tp_heap_buffer IPA_SYM_EXPORTED_NAME (gdb_tp_heap_buffer)
105 # define gdb_jump_pad_buffer IPA_SYM_EXPORTED_NAME (gdb_jump_pad_buffer)
106 # define gdb_jump_pad_buffer_end IPA_SYM_EXPORTED_NAME (gdb_jump_pad_buffer_end)
107 # define gdb_trampoline_buffer IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer)
108 # define gdb_trampoline_buffer_end IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer_end)
109 # define gdb_trampoline_buffer_error IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer_error)
110 # define collecting IPA_SYM_EXPORTED_NAME (collecting)
111 # define gdb_collect IPA_SYM_EXPORTED_NAME (gdb_collect)
112 # define stop_tracing IPA_SYM_EXPORTED_NAME (stop_tracing)
113 # define flush_trace_buffer IPA_SYM_EXPORTED_NAME (flush_trace_buffer)
114 # define about_to_request_buffer_space IPA_SYM_EXPORTED_NAME (about_to_request_buffer_space)
115 # define trace_buffer_is_full IPA_SYM_EXPORTED_NAME (trace_buffer_is_full)
116 # define stopping_tracepoint IPA_SYM_EXPORTED_NAME (stopping_tracepoint)
117 # define expr_eval_result IPA_SYM_EXPORTED_NAME (expr_eval_result)
118 # define error_tracepoint IPA_SYM_EXPORTED_NAME (error_tracepoint)
119 # define tracepoints IPA_SYM_EXPORTED_NAME (tracepoints)
120 # define tracing IPA_SYM_EXPORTED_NAME (tracing)
121 # define trace_buffer_ctrl IPA_SYM_EXPORTED_NAME (trace_buffer_ctrl)
122 # define trace_buffer_ctrl_curr IPA_SYM_EXPORTED_NAME (trace_buffer_ctrl_curr)
123 # define trace_buffer_lo IPA_SYM_EXPORTED_NAME (trace_buffer_lo)
124 # define trace_buffer_hi IPA_SYM_EXPORTED_NAME (trace_buffer_hi)
125 # define traceframe_read_count IPA_SYM_EXPORTED_NAME (traceframe_read_count)
126 # define traceframe_write_count IPA_SYM_EXPORTED_NAME (traceframe_write_count)
127 # define traceframes_created IPA_SYM_EXPORTED_NAME (traceframes_created)
128 # define trace_state_variables IPA_SYM_EXPORTED_NAME (trace_state_variables)
129 # define get_raw_reg IPA_SYM_EXPORTED_NAME (get_raw_reg)
130 # define get_trace_state_variable_value \
131 IPA_SYM_EXPORTED_NAME (get_trace_state_variable_value)
132 # define set_trace_state_variable_value \
133 IPA_SYM_EXPORTED_NAME (set_trace_state_variable_value)
134 # define ust_loaded IPA_SYM_EXPORTED_NAME (ust_loaded)
135 # define helper_thread_id IPA_SYM_EXPORTED_NAME (helper_thread_id)
136 # define cmd_buf IPA_SYM_EXPORTED_NAME (cmd_buf)
137 # define ipa_tdesc_idx IPA_SYM_EXPORTED_NAME (ipa_tdesc_idx)
140 #ifndef IN_PROCESS_AGENT
142 /* Addresses of in-process agent's symbols GDBserver cares about. */
144 struct ipa_sym_addresses
146 CORE_ADDR addr_gdb_tp_heap_buffer
;
147 CORE_ADDR addr_gdb_jump_pad_buffer
;
148 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
149 CORE_ADDR addr_gdb_trampoline_buffer
;
150 CORE_ADDR addr_gdb_trampoline_buffer_end
;
151 CORE_ADDR addr_gdb_trampoline_buffer_error
;
152 CORE_ADDR addr_collecting
;
153 CORE_ADDR addr_gdb_collect
;
154 CORE_ADDR addr_stop_tracing
;
155 CORE_ADDR addr_flush_trace_buffer
;
156 CORE_ADDR addr_about_to_request_buffer_space
;
157 CORE_ADDR addr_trace_buffer_is_full
;
158 CORE_ADDR addr_stopping_tracepoint
;
159 CORE_ADDR addr_expr_eval_result
;
160 CORE_ADDR addr_error_tracepoint
;
161 CORE_ADDR addr_tracepoints
;
162 CORE_ADDR addr_tracing
;
163 CORE_ADDR addr_trace_buffer_ctrl
;
164 CORE_ADDR addr_trace_buffer_ctrl_curr
;
165 CORE_ADDR addr_trace_buffer_lo
;
166 CORE_ADDR addr_trace_buffer_hi
;
167 CORE_ADDR addr_traceframe_read_count
;
168 CORE_ADDR addr_traceframe_write_count
;
169 CORE_ADDR addr_traceframes_created
;
170 CORE_ADDR addr_trace_state_variables
;
171 CORE_ADDR addr_get_raw_reg
;
172 CORE_ADDR addr_get_trace_state_variable_value
;
173 CORE_ADDR addr_set_trace_state_variable_value
;
174 CORE_ADDR addr_ust_loaded
;
175 CORE_ADDR addr_ipa_tdesc_idx
;
183 IPA_SYM(gdb_tp_heap_buffer
),
184 IPA_SYM(gdb_jump_pad_buffer
),
185 IPA_SYM(gdb_jump_pad_buffer_end
),
186 IPA_SYM(gdb_trampoline_buffer
),
187 IPA_SYM(gdb_trampoline_buffer_end
),
188 IPA_SYM(gdb_trampoline_buffer_error
),
190 IPA_SYM(gdb_collect
),
191 IPA_SYM(stop_tracing
),
192 IPA_SYM(flush_trace_buffer
),
193 IPA_SYM(about_to_request_buffer_space
),
194 IPA_SYM(trace_buffer_is_full
),
195 IPA_SYM(stopping_tracepoint
),
196 IPA_SYM(expr_eval_result
),
197 IPA_SYM(error_tracepoint
),
198 IPA_SYM(tracepoints
),
200 IPA_SYM(trace_buffer_ctrl
),
201 IPA_SYM(trace_buffer_ctrl_curr
),
202 IPA_SYM(trace_buffer_lo
),
203 IPA_SYM(trace_buffer_hi
),
204 IPA_SYM(traceframe_read_count
),
205 IPA_SYM(traceframe_write_count
),
206 IPA_SYM(traceframes_created
),
207 IPA_SYM(trace_state_variables
),
208 IPA_SYM(get_raw_reg
),
209 IPA_SYM(get_trace_state_variable_value
),
210 IPA_SYM(set_trace_state_variable_value
),
212 IPA_SYM(ipa_tdesc_idx
),
215 static struct ipa_sym_addresses ipa_sym_addrs
;
217 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
219 /* Returns true if both the in-process agent library and the static
220 tracepoints libraries are loaded in the inferior, and agent has
221 capability on static tracepoints. */
224 in_process_agent_supports_ust (void)
228 if (!agent_loaded_p ())
230 warning ("In-process agent not loaded");
234 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE
))
236 /* Agent understands static tracepoint, then check whether UST is in
237 fact loaded in the inferior. */
238 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
240 warning ("Error reading ust_loaded in lib");
251 write_e_ipa_not_loaded (char *buffer
)
254 "E.In-process agent library not loaded in process. "
255 "Fast and static tracepoints unavailable.");
258 /* Write an error to BUFFER indicating that UST isn't loaded in the
262 write_e_ust_not_loaded (char *buffer
)
266 "E.UST library not loaded in process. "
267 "Static tracepoints unavailable.");
269 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
273 /* If the in-process agent library isn't loaded in the inferior, write
274 an error to BUFFER, and return 1. Otherwise, return 0. */
277 maybe_write_ipa_not_loaded (char *buffer
)
279 if (!agent_loaded_p ())
281 write_e_ipa_not_loaded (buffer
);
287 /* If the in-process agent library and the ust (static tracepoints)
288 library aren't loaded in the inferior, write an error to BUFFER,
289 and return 1. Otherwise, return 0. */
292 maybe_write_ipa_ust_not_loaded (char *buffer
)
294 if (!agent_loaded_p ())
296 write_e_ipa_not_loaded (buffer
);
299 else if (!in_process_agent_supports_ust ())
301 write_e_ust_not_loaded (buffer
);
307 /* Cache all future symbols that the tracepoints module might request.
308 We can not request symbols at arbitrary states in the remote
309 protocol, only when the client tells us that new symbols are
310 available. So when we load the in-process library, make sure to
311 check the entire list. */
314 tracepoint_look_up_symbols (void)
318 if (agent_loaded_p ())
321 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
324 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
326 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
329 debug_printf ("symbol `%s' not found\n", symbol_list
[i
].name
);
334 agent_look_up_symbols (NULL
);
339 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
340 of the "stop_tracing" function. When this breakpoint is hit,
341 tracing stopped in the IPA for some reason. E.g., due to
342 tracepoint reaching the pass count, hitting conditional expression
343 evaluation error, etc.
345 The IPA's trace buffer is never in circular tracing mode: instead,
346 GDBserver's is, and whenever the in-process buffer fills, it calls
347 "flush_trace_buffer", which triggers an internal breakpoint.
348 GDBserver reacts to this breakpoint by pulling the meanwhile
349 collected data. Old frames discarding is always handled on the
352 #ifdef IN_PROCESS_AGENT
354 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
356 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
360 /* Call this in the functions where GDBserver places a breakpoint, so
361 that the compiler doesn't try to be clever and skip calling the
362 function at all. This is necessary, even if we tell the compiler
363 to not inline said functions. */
365 #if defined(__GNUC__)
366 # define UNKNOWN_SIDE_EFFECTS() asm ("")
368 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
371 IP_AGENT_EXPORT_FUNC
void
374 /* GDBserver places breakpoint here. */
375 UNKNOWN_SIDE_EFFECTS();
378 IP_AGENT_EXPORT_FUNC
void
379 flush_trace_buffer (void)
381 /* GDBserver places breakpoint here. */
382 UNKNOWN_SIDE_EFFECTS();
387 #ifndef IN_PROCESS_AGENT
389 tracepoint_handler (CORE_ADDR address
)
391 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
396 /* Breakpoint at "stop_tracing" in the inferior lib. */
397 struct breakpoint
*stop_tracing_bkpt
;
398 static int stop_tracing_handler (CORE_ADDR
);
400 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
401 struct breakpoint
*flush_trace_buffer_bkpt
;
402 static int flush_trace_buffer_handler (CORE_ADDR
);
404 static void download_trace_state_variables (void);
405 static void upload_fast_traceframes (void);
407 static int run_inferior_command (char *cmd
, int len
);
410 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
412 return read_inferior_memory (symaddr
, (unsigned char *) val
,
417 static int tracepoint_send_agent (struct tracepoint
*tpoint
);
420 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
422 return read_inferior_memory (symaddr
, (unsigned char *) val
,
427 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
429 void *pval
= (void *) (uintptr_t) val
;
432 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
433 *val
= (uintptr_t) pval
;
438 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
440 void *pval
= (void *) (uintptr_t) val
;
441 return write_inferior_memory (symaddr
,
442 (unsigned char *) &pval
, sizeof (pval
));
446 write_inferior_integer (CORE_ADDR symaddr
, int val
)
448 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
452 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
454 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
457 static CORE_ADDR
target_malloc (ULONGEST size
);
459 #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD) \
461 memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \
462 BUF += sizeof ((OBJ)->FIELD); \
467 /* Base action. Concrete actions inherit this. */
469 struct tracepoint_action
474 /* An 'M' (collect memory) action. */
475 struct collect_memory_action
477 struct tracepoint_action base
;
484 /* An 'R' (collect registers) action. */
486 struct collect_registers_action
488 struct tracepoint_action base
;
491 /* An 'X' (evaluate expression) action. */
493 struct eval_expr_action
495 struct tracepoint_action base
;
497 struct agent_expr
*expr
;
500 /* An 'L' (collect static trace data) action. */
501 struct collect_static_trace_data_action
503 struct tracepoint_action base
;
506 #ifndef IN_PROCESS_AGENT
508 m_tracepoint_action_download (const struct tracepoint_action
*action
)
510 CORE_ADDR ipa_action
= target_malloc (sizeof (struct collect_memory_action
));
512 write_inferior_memory (ipa_action
, (unsigned char *) action
,
513 sizeof (struct collect_memory_action
));
518 m_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
520 struct collect_memory_action
*maction
521 = (struct collect_memory_action
*) action
;
523 COPY_FIELD_TO_BUF (buffer
, maction
, addr
);
524 COPY_FIELD_TO_BUF (buffer
, maction
, len
);
525 COPY_FIELD_TO_BUF (buffer
, maction
, basereg
);
531 r_tracepoint_action_download (const struct tracepoint_action
*action
)
533 CORE_ADDR ipa_action
= target_malloc (sizeof (struct collect_registers_action
));
535 write_inferior_memory (ipa_action
, (unsigned char *) action
,
536 sizeof (struct collect_registers_action
));
542 r_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
547 static CORE_ADDR
download_agent_expr (struct agent_expr
*expr
);
550 x_tracepoint_action_download (const struct tracepoint_action
*action
)
552 CORE_ADDR ipa_action
= target_malloc (sizeof (struct eval_expr_action
));
555 write_inferior_memory (ipa_action
, (unsigned char *) action
,
556 sizeof (struct eval_expr_action
));
557 expr
= download_agent_expr (((struct eval_expr_action
*) action
)->expr
);
558 write_inferior_data_pointer (ipa_action
559 + offsetof (struct eval_expr_action
, expr
),
565 /* Copy agent expression AEXPR to buffer pointed by P. If AEXPR is NULL,
566 copy 0 to P. Return updated header of buffer. */
569 agent_expr_send (char *p
, const struct agent_expr
*aexpr
)
571 /* Copy the length of condition first, and then copy its
580 memcpy (p
, &aexpr
->length
, 4);
583 memcpy (p
, aexpr
->bytes
, aexpr
->length
);
590 x_tracepoint_action_send ( char *buffer
, const struct tracepoint_action
*action
)
592 struct eval_expr_action
*eaction
= (struct eval_expr_action
*) action
;
594 return agent_expr_send (buffer
, eaction
->expr
);
598 l_tracepoint_action_download (const struct tracepoint_action
*action
)
601 = target_malloc (sizeof (struct collect_static_trace_data_action
));
603 write_inferior_memory (ipa_action
, (unsigned char *) action
,
604 sizeof (struct collect_static_trace_data_action
));
610 l_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
616 tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
618 switch (action
->type
)
621 return m_tracepoint_action_send (buffer
, action
);
623 return r_tracepoint_action_send (buffer
, action
);
625 return x_tracepoint_action_send (buffer
, action
);
627 return l_tracepoint_action_send (buffer
, action
);
629 error ("Unknown trace action '%c'.", action
->type
);
633 tracepoint_action_download (const struct tracepoint_action
*action
)
635 switch (action
->type
)
638 return m_tracepoint_action_download (action
);
640 return r_tracepoint_action_download (action
);
642 return x_tracepoint_action_download (action
);
644 return l_tracepoint_action_download (action
);
646 error ("Unknown trace action '%c'.", action
->type
);
650 /* This structure describes a piece of the source-level definition of
651 the tracepoint. The contents are not interpreted by the target,
652 but preserved verbatim for uploading upon reconnection. */
656 /* The type of string, such as "cond" for a conditional. */
659 /* The source-level string itself. For the sake of target
660 debugging, we store it in plaintext, even though it is always
661 transmitted in hex. */
664 /* Link to the next one in the list. We link them in the order
665 received, in case some make up an ordered list of commands or
667 struct source_string
*next
;
672 /* Trap based tracepoint. */
675 /* A fast tracepoint implemented with a jump instead of a trap. */
678 /* A static tracepoint, implemented by a program call into a tracing
683 struct tracepoint_hit_ctx
;
685 typedef enum eval_result_type (*condfn
) (unsigned char *,
688 /* The definition of a tracepoint. */
690 /* Tracepoints may have multiple locations, each at a different
691 address. This can occur with optimizations, template
692 instantiation, etc. Since the locations may be in different
693 scopes, the conditions and actions may be different for each
694 location. Our target version of tracepoints is more like GDB's
695 notion of "breakpoint locations", but we have almost nothing that
696 is not per-location, so we bother having two kinds of objects. The
697 key consequence is that numbers are not unique, and that it takes
698 both number and address to identify a tracepoint uniquely. */
702 /* The number of the tracepoint, as specified by GDB. Several
703 tracepoint objects here may share a number. */
706 /* Address at which the tracepoint is supposed to trigger. Several
707 tracepoints may share an address. */
710 /* Tracepoint type. */
711 enum tracepoint_type type
;
713 /* True if the tracepoint is currently enabled. */
716 /* The number of single steps that will be performed after each
720 /* The number of times the tracepoint may be hit before it will
721 terminate the entire tracing run. */
724 /* Pointer to the agent expression that is the tracepoint's
725 conditional, or NULL if the tracepoint is unconditional. */
726 struct agent_expr
*cond
;
728 /* The list of actions to take when the tracepoint triggers. */
730 struct tracepoint_action
**actions
;
732 /* Count of the times we've hit this tracepoint during the run.
733 Note that while-stepping steps are not counted as "hits". */
736 /* Cached sum of the sizes of traceframes created by this point. */
737 uint64_t traceframe_usage
;
739 CORE_ADDR compiled_cond
;
741 /* Link to the next tracepoint in the list. */
742 struct tracepoint
*next
;
744 #ifndef IN_PROCESS_AGENT
745 /* The list of actions to take when the tracepoint triggers, in
746 string/packet form. */
749 /* The collection of strings that describe the tracepoint as it was
750 entered into GDB. These are not used by the target, but are
751 reported back to GDB upon reconnection. */
752 struct source_string
*source_strings
;
754 /* The number of bytes displaced by fast tracepoints. It may subsume
755 multiple instructions, for multi-byte fast tracepoints. This
756 field is only valid for fast tracepoints. */
759 /* Only for fast tracepoints. */
760 CORE_ADDR obj_addr_on_target
;
762 /* Address range where the original instruction under a fast
763 tracepoint was relocated to. (_end is actually one byte past
765 CORE_ADDR adjusted_insn_addr
;
766 CORE_ADDR adjusted_insn_addr_end
;
768 /* The address range of the piece of the jump pad buffer that was
769 assigned to this fast tracepoint. (_end is actually one byte
772 CORE_ADDR jump_pad_end
;
774 /* The address range of the piece of the trampoline buffer that was
775 assigned to this fast tracepoint. (_end is actually one byte
777 CORE_ADDR trampoline
;
778 CORE_ADDR trampoline_end
;
780 /* The list of actions to take while in a stepping loop. These
781 fields are only valid for patch-based tracepoints. */
782 int num_step_actions
;
783 struct tracepoint_action
**step_actions
;
784 /* Same, but in string/packet form. */
785 char **step_actions_str
;
787 /* Handle returned by the breakpoint or tracepoint module when we
788 inserted the trap or jump, or hooked into a static tracepoint.
789 NULL if we haven't inserted it yet. */
795 #ifndef IN_PROCESS_AGENT
797 /* Given `while-stepping', a thread may be collecting data for more
798 than one tracepoint simultaneously. On the other hand, the same
799 tracepoint with a while-stepping action may be hit by more than one
800 thread simultaneously (but not quite, each thread could be handling
801 a different step). Each thread holds a list of these objects,
802 representing the current step of each while-stepping action being
807 struct wstep_state
*next
;
809 /* The tracepoint number. */
811 /* The tracepoint's address. */
812 CORE_ADDR tp_address
;
814 /* The number of the current step in this 'while-stepping'
823 /* The linked list of all tracepoints. Marked explicitly as used as
824 the in-process library doesn't use it for the fast tracepoints
826 IP_AGENT_EXPORT_VAR
struct tracepoint
*tracepoints
;
828 /* The first tracepoint to exceed its pass count. */
830 IP_AGENT_EXPORT_VAR
struct tracepoint
*stopping_tracepoint
;
832 /* True if the trace buffer is full or otherwise no longer usable. */
834 IP_AGENT_EXPORT_VAR
int trace_buffer_is_full
;
836 /* The first error that occurred during expression evaluation. */
838 /* Stored as an int to avoid the IPA ABI being dependent on whatever
839 the compiler decides to use for the enum's underlying type. Holds
840 enum eval_result_type values. */
841 IP_AGENT_EXPORT_VAR
int expr_eval_result
= expr_eval_no_error
;
845 #ifndef IN_PROCESS_AGENT
847 /* Pointer to the last tracepoint in the list, new tracepoints are
848 linked in at the end. */
850 static struct tracepoint
*last_tracepoint
;
852 static const char *eval_result_names
[] =
854 "terror:in the attic", /* this should never be reported */
855 "terror:empty expression",
856 "terror:empty stack",
857 "terror:stack overflow",
858 "terror:stack underflow",
859 "terror:unhandled opcode",
860 "terror:unrecognized opcode",
861 "terror:divide by zero"
866 /* The tracepoint in which the error occurred. */
869 IP_AGENT_EXPORT_VAR
struct tracepoint
*error_tracepoint
;
872 struct trace_state_variable
874 /* This is the name of the variable as used in GDB. The target
875 doesn't use the name, but needs to have it for saving and
876 reconnection purposes. */
879 /* This number identifies the variable uniquely. Numbers may be
880 assigned either by the target (in the case of builtin variables),
881 or by GDB, and are presumed unique during the course of a trace
885 /* The variable's initial value, a 64-bit signed integer always. */
886 LONGEST initial_value
;
888 /* The variable's value, a 64-bit signed integer always. */
891 /* Pointer to a getter function, used to supply computed values. */
892 LONGEST (*getter
) (void);
894 /* Link to the next variable. */
895 struct trace_state_variable
*next
;
898 /* Linked list of all trace state variables. */
900 #ifdef IN_PROCESS_AGENT
901 struct trace_state_variable
*alloced_trace_state_variables
;
904 IP_AGENT_EXPORT_VAR
struct trace_state_variable
*trace_state_variables
;
906 /* The results of tracing go into a fixed-size space known as the
907 "trace buffer". Because usage follows a limited number of
908 patterns, we manage it ourselves rather than with malloc. Basic
909 rules are that we create only one trace frame at a time, each is
910 variable in size, they are never moved once created, and we only
911 discard if we are doing a circular buffer, and then only the oldest
912 ones. Each trace frame includes its own size, so we don't need to
913 link them together, and the trace frame number is relative to the
914 first one, so we don't need to record numbers. A trace frame also
915 records the number of the tracepoint that created it. The data
916 itself is a series of blocks, each introduced by a single character
917 and with a defined format. Each type of block has enough
918 type/length info to allow scanners to jump quickly from one block
919 to the next without reading each byte in the block. */
921 /* Trace buffer management would be simple - advance a free pointer
922 from beginning to end, then stop - were it not for the circular
923 buffer option, which is a useful way to prevent a trace run from
924 stopping prematurely because the buffer filled up. In the circular
925 case, the location of the first trace frame (trace_buffer_start)
926 moves as old trace frames are discarded. Also, since we grow trace
927 frames incrementally as actions are performed, we wrap around to
928 the beginning of the trace buffer. This is per-block, so each
929 block within a trace frame remains contiguous. Things get messy
930 when the wrapped-around trace frame is the one being discarded; the
931 free space ends up in two parts at opposite ends of the buffer. */
934 # if defined(__GNUC__)
935 # define ATTR_PACKED __attribute__ ((packed))
937 # define ATTR_PACKED /* nothing */
941 /* The data collected at a tracepoint hit. This object should be as
942 small as possible, since there may be a great many of them. We do
943 not need to keep a frame number, because they are all sequential
944 and there are no deletions; so the Nth frame in the buffer is
945 always frame number N. */
949 /* Number of the tracepoint that collected this traceframe. A value
950 of 0 indicates the current end of the trace buffer. We make this
951 a 16-bit field because it's never going to happen that GDB's
952 numbering of tracepoints reaches 32,000. */
955 /* The size of the data in this trace frame. We limit this to 32
956 bits, even on a 64-bit target, because it's just implausible that
957 one is validly going to collect 4 gigabytes of data at a single
959 unsigned int data_size
: 32;
961 /* The base of the trace data, which is contiguous from this point. */
962 unsigned char data
[0];
966 /* The size of the EOB marker, in bytes. A traceframe with zeroed
967 fields (and no data) marks the end of trace data. */
968 #define TRACEFRAME_EOB_MARKER_SIZE offsetof (struct traceframe, data)
970 /* The traceframe to be used as the source of data to send back to
971 GDB. A value of -1 means to get data from the live program. */
973 int current_traceframe
= -1;
975 /* This flag is true if the trace buffer is circular, meaning that
976 when it fills, the oldest trace frames are discarded in order to
979 #ifndef IN_PROCESS_AGENT
980 static int circular_trace_buffer
;
983 /* Size of the trace buffer. */
985 static LONGEST trace_buffer_size
;
989 /* Pointer to the block of memory that traceframes all go into. */
991 IP_AGENT_EXPORT_VAR
unsigned char *trace_buffer_lo
;
993 /* Pointer to the end of the trace buffer, more precisely to the byte
994 after the end of the buffer. */
996 IP_AGENT_EXPORT_VAR
unsigned char *trace_buffer_hi
;
1000 /* Control structure holding the read/write/etc. pointers into the
1001 trace buffer. We need more than one of these to implement a
1002 transaction-like mechanism to garantees that both GDBserver and the
1003 in-process agent can try to change the trace buffer
1006 struct trace_buffer_control
1008 /* Pointer to the first trace frame in the buffer. In the
1009 non-circular case, this is equal to trace_buffer_lo, otherwise it
1010 moves around in the buffer. */
1011 unsigned char *start
;
1013 /* Pointer to the free part of the trace buffer. Note that we clear
1014 several bytes at and after this pointer, so that traceframe
1015 scans/searches terminate properly. */
1016 unsigned char *free
;
1018 /* Pointer to the byte after the end of the free part. Note that
1019 this may be smaller than trace_buffer_free in the circular case,
1020 and means that the free part is in two pieces. Initially it is
1021 equal to trace_buffer_hi, then is generally equivalent to
1022 trace_buffer_start. */
1023 unsigned char *end_free
;
1025 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
1026 this is the point at which the trace data breaks, and resumes at
1028 unsigned char *wrap
;
1031 /* Same as above, to be used by GDBserver when updating the in-process
1033 struct ipa_trace_buffer_control
1042 /* We have possibly both GDBserver and an inferior thread accessing
1043 the same IPA trace buffer memory. The IPA is the producer (tries
1044 to put new frames in the buffer), while GDBserver occasionally
1045 consumes them, that is, flushes the IPA's buffer into its own
1046 buffer. Both sides need to update the trace buffer control
1047 pointers (current head, tail, etc.). We can't use a global lock to
1048 synchronize the accesses, as otherwise we could deadlock GDBserver
1049 (if the thread holding the lock stops for a signal, say). So
1050 instead of that, we use a transaction scheme where GDBserver writes
1051 always prevail over the IPAs writes, and, we have the IPA detect
1052 the commit failure/overwrite, and retry the whole attempt. This is
1053 mainly implemented by having a global token object that represents
1054 who wrote last to the buffer control structure. We need to freeze
1055 any inferior writing to the buffer while GDBserver touches memory,
1056 so that the inferior can correctly detect that GDBserver had been
1057 there, otherwise, it could mistakingly think its commit was
1058 successful; that's implemented by simply having GDBserver set a
1059 breakpoint the inferior hits if it is the critical region.
1061 There are three cycling trace buffer control structure copies
1062 (buffer head, tail, etc.), with the token object including an index
1063 indicating which is current live copy. The IPA tentatively builds
1064 an updated copy in a non-current control structure, while GDBserver
1065 always clobbers the current version directly. The IPA then tries
1066 to atomically "commit" its version; if GDBserver clobbered the
1067 structure meanwhile, that will fail, and the IPA restarts the
1070 Listing the step in further detail, we have:
1072 In-process agent (producer):
1074 - passes by `about_to_request_buffer_space' breakpoint/lock
1076 - reads current token, extracts current trace buffer control index,
1077 and starts tentatively updating the rightmost one (0->1, 1->2,
1078 2->0). Note that only one inferior thread is executing this code
1079 at any given time, due to an outer lock in the jump pads.
1081 - updates counters, and tries to commit the token.
1083 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1084 leaving the sync region.
1086 - checks if the update was effective.
1088 - if trace buffer was found full, hits flush_trace_buffer
1089 breakpoint, and restarts later afterwards.
1091 GDBserver (consumer):
1093 - sets `about_to_request_buffer_space' breakpoint/lock.
1095 - updates the token unconditionally, using the current buffer
1096 control index, since it knows that the IP agent always writes to
1097 the rightmost, and due to the breakpoint, at most one IP thread
1098 can try to update the trace buffer concurrently to GDBserver, so
1099 there will be no danger of trace buffer control index wrap making
1100 the IPA write to the same index as GDBserver.
1102 - flushes the IP agent's trace buffer completely, and updates the
1103 current trace buffer control structure. GDBserver *always* wins.
1105 - removes the `about_to_request_buffer_space' breakpoint.
1107 The token is stored in the `trace_buffer_ctrl_curr' variable.
1108 Internally, it's bits are defined as:
1110 |-------------+-----+-------------+--------+-------------+--------------|
1111 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1112 |-------------+-----+-------------+--------+-------------+--------------|
1113 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1114 |-------------+-----+-------------+--------+-------------+--------------|
1116 GSB - GDBserver Stamp Bit
1117 PC - Previous Counter
1118 CC - Current Counter
1119 TBCI - Trace Buffer Control Index
1122 An IPA update of `trace_buffer_ctrl_curr' does:
1124 - read CC from the current token, save as PC.
1126 - atomically tries to write PC+1,CC
1128 A GDBserver update of `trace_buffer_ctrl_curr' does:
1130 - reads PC and CC from the current token.
1135 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1136 for the counters described below. The cleared bits are used to
1137 hold the index of the items of the `trace_buffer_ctrl' array that
1139 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1141 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1142 counter, and the `current' counter. */
1144 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1145 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1147 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1148 always stamps this bit as set. */
1149 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1151 #ifdef IN_PROCESS_AGENT
1152 IP_AGENT_EXPORT_VAR
struct trace_buffer_control trace_buffer_ctrl
[3];
1153 IP_AGENT_EXPORT_VAR
unsigned int trace_buffer_ctrl_curr
;
1155 # define TRACE_BUFFER_CTRL_CURR \
1156 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1160 /* The GDBserver side agent only needs one instance of this object, as
1161 it doesn't need to sync with itself. Define it as array anyway so
1162 that the rest of the code base doesn't need to care for the
1164 struct trace_buffer_control trace_buffer_ctrl
[1];
1165 # define TRACE_BUFFER_CTRL_CURR 0
1168 /* These are convenience macros used to access the current trace
1169 buffer control in effect. */
1170 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1171 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1172 #define trace_buffer_end_free \
1173 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1174 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1177 /* Macro that returns a pointer to the first traceframe in the buffer. */
1179 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1181 /* Macro that returns a pointer to the next traceframe in the buffer.
1182 If the computed location is beyond the wraparound point, subtract
1183 the offset of the wraparound. */
1185 #define NEXT_TRACEFRAME_1(TF) \
1186 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1188 #define NEXT_TRACEFRAME(TF) \
1189 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1190 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1191 ? (trace_buffer_wrap - trace_buffer_lo) \
1194 /* The difference between these counters represents the total number
1195 of complete traceframes present in the trace buffer. The IP agent
1196 writes to the write count, GDBserver writes to read count. */
1198 IP_AGENT_EXPORT_VAR
unsigned int traceframe_write_count
;
1199 IP_AGENT_EXPORT_VAR
unsigned int traceframe_read_count
;
1201 /* Convenience macro. */
1203 #define traceframe_count \
1204 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1206 /* The count of all traceframes created in the current run, including
1207 ones that were discarded to make room. */
1209 IP_AGENT_EXPORT_VAR
int traceframes_created
;
1211 #ifndef IN_PROCESS_AGENT
1213 /* Read-only regions are address ranges whose contents don't change,
1214 and so can be read from target memory even while looking at a trace
1215 frame. Without these, disassembly for instance will likely fail,
1216 because the program code is not usually collected into a trace
1217 frame. This data structure does not need to be very complicated or
1218 particularly efficient, it's only going to be used occasionally,
1219 and only by some commands. */
1221 struct readonly_region
1223 /* The bounds of the region. */
1224 CORE_ADDR start
, end
;
1226 /* Link to the next one. */
1227 struct readonly_region
*next
;
1230 /* Linked list of readonly regions. This list stays in effect from
1231 one tstart to the next. */
1233 static struct readonly_region
*readonly_regions
;
1237 /* The global that controls tracing overall. */
1239 IP_AGENT_EXPORT_VAR
int tracing
;
1241 #ifndef IN_PROCESS_AGENT
1243 /* Controls whether tracing should continue after GDB disconnects. */
1245 int disconnected_tracing
;
1247 /* The reason for the last tracing run to have stopped. We initialize
1248 to a distinct string so that GDB can distinguish between "stopped
1249 after running" and "stopped because never run" cases. */
1251 static const char *tracing_stop_reason
= "tnotrun";
1253 static int tracing_stop_tpnum
;
1255 /* 64-bit timestamps for the trace run's start and finish, expressed
1256 in microseconds from the Unix epoch. */
1258 LONGEST tracing_start_time
;
1259 LONGEST tracing_stop_time
;
1261 /* The (optional) user-supplied name of the user that started the run.
1262 This is an arbitrary string, and may be NULL. */
1264 char *tracing_user_name
;
1266 /* Optional user-supplied text describing the run. This is
1267 an arbitrary string, and may be NULL. */
1269 char *tracing_notes
;
1271 /* Optional user-supplied text explaining a tstop command. This is an
1272 arbitrary string, and may be NULL. */
1274 char *tracing_stop_note
;
1278 /* Functions local to this file. */
1280 /* Base "class" for tracepoint type specific data to be passed down to
1281 collect_data_at_tracepoint. */
1282 struct tracepoint_hit_ctx
1284 enum tracepoint_type type
;
1287 #ifdef IN_PROCESS_AGENT
1289 /* Fast/jump tracepoint specific data to be passed down to
1290 collect_data_at_tracepoint. */
1291 struct fast_tracepoint_ctx
1293 struct tracepoint_hit_ctx base
;
1295 struct regcache regcache
;
1296 int regcache_initted
;
1297 unsigned char *regspace
;
1299 unsigned char *regs
;
1300 struct tracepoint
*tpoint
;
1303 /* Static tracepoint specific data to be passed down to
1304 collect_data_at_tracepoint. */
1305 struct static_tracepoint_ctx
1307 struct tracepoint_hit_ctx base
;
1309 /* The regcache corresponding to the registers state at the time of
1310 the tracepoint hit. Initialized lazily, from REGS. */
1311 struct regcache regcache
;
1312 int regcache_initted
;
1314 /* The buffer space REGCACHE above uses. We use a separate buffer
1315 instead of letting the regcache malloc for both signal safety and
1316 performance reasons; this is allocated on the stack instead. */
1317 unsigned char *regspace
;
1319 /* The register buffer as passed on by lttng/ust. */
1320 struct registers
*regs
;
1322 /* The "printf" formatter and the args the user passed to the marker
1323 call. We use this to be able to collect "static trace data"
1328 /* The GDB tracepoint matching the probed marker that was "hit". */
1329 struct tracepoint
*tpoint
;
1334 /* Static tracepoint specific data to be passed down to
1335 collect_data_at_tracepoint. */
1336 struct trap_tracepoint_ctx
1338 struct tracepoint_hit_ctx base
;
1340 struct regcache
*regcache
;
1345 #ifndef IN_PROCESS_AGENT
1346 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1347 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1350 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1351 struct tracepoint
*tpoint
);
1353 #ifndef IN_PROCESS_AGENT
1354 static void clear_readonly_regions (void);
1355 static void clear_installed_tracepoints (void);
1358 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1360 struct tracepoint
*tpoint
);
1361 #ifndef IN_PROCESS_AGENT
1362 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1364 struct tracepoint
*tpoint
, int current_step
);
1365 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1366 CORE_ADDR
*jump_entry
);
1368 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1370 struct tracepoint
*tpoint
,
1371 struct traceframe
*tframe
,
1372 struct tracepoint_action
*taction
);
1374 #ifndef IN_PROCESS_AGENT
1375 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1377 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1378 static void download_tracepoint (struct tracepoint
*);
1379 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1380 static void clone_fast_tracepoint (struct tracepoint
*to
,
1381 const struct tracepoint
*from
);
1384 static LONGEST
get_timestamp (void);
1386 #if defined(__GNUC__)
1387 # define memory_barrier() asm volatile ("" : : : "memory")
1389 # define memory_barrier() do {} while (0)
1392 /* We only build the IPA if this builtin is supported, and there are
1393 no uses of this in GDBserver itself, so we're safe in defining this
1395 #define cmpxchg(mem, oldval, newval) \
1396 __sync_val_compare_and_swap (mem, oldval, newval)
1398 /* Record that an error occurred during expression evaluation. */
1401 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1402 enum eval_result_type rtype
)
1404 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1405 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1407 #ifdef IN_PROCESS_AGENT
1408 /* Only record the first error we get. */
1409 if (cmpxchg (&expr_eval_result
,
1411 rtype
) != expr_eval_no_error
)
1414 if (expr_eval_result
!= expr_eval_no_error
)
1418 error_tracepoint
= tpoint
;
1421 /* Trace buffer management. */
1424 clear_trace_buffer (void)
1426 trace_buffer_start
= trace_buffer_lo
;
1427 trace_buffer_free
= trace_buffer_lo
;
1428 trace_buffer_end_free
= trace_buffer_hi
;
1429 trace_buffer_wrap
= trace_buffer_hi
;
1430 /* A traceframe with zeroed fields marks the end of trace data. */
1431 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1432 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1433 traceframe_read_count
= traceframe_write_count
= 0;
1434 traceframes_created
= 0;
1437 #ifndef IN_PROCESS_AGENT
1440 clear_inferior_trace_buffer (void)
1442 CORE_ADDR ipa_trace_buffer_lo
;
1443 CORE_ADDR ipa_trace_buffer_hi
;
1444 struct traceframe ipa_traceframe
= { 0 };
1445 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1447 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1448 &ipa_trace_buffer_lo
);
1449 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1450 &ipa_trace_buffer_hi
);
1452 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1453 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1454 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1455 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1457 /* A traceframe with zeroed fields marks the end of trace data. */
1458 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1459 (unsigned char *) &ipa_trace_buffer_ctrl
,
1460 sizeof (ipa_trace_buffer_ctrl
));
1462 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1464 /* A traceframe with zeroed fields marks the end of trace data. */
1465 write_inferior_memory (ipa_trace_buffer_lo
,
1466 (unsigned char *) &ipa_traceframe
,
1467 sizeof (ipa_traceframe
));
1469 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1470 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1471 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1477 init_trace_buffer (LONGEST bufsize
)
1481 trace_buffer_size
= bufsize
;
1483 /* Make sure to internally allocate at least space for the EOB
1485 alloc_size
= (bufsize
< TRACEFRAME_EOB_MARKER_SIZE
1486 ? TRACEFRAME_EOB_MARKER_SIZE
: bufsize
);
1487 trace_buffer_lo
= (unsigned char *) xrealloc (trace_buffer_lo
, alloc_size
);
1489 trace_buffer_hi
= trace_buffer_lo
+ trace_buffer_size
;
1491 clear_trace_buffer ();
1494 #ifdef IN_PROCESS_AGENT
1496 IP_AGENT_EXPORT_FUNC
void
1497 about_to_request_buffer_space (void)
1499 /* GDBserver places breakpoint here while it goes about to flush
1500 data at random times. */
1501 UNKNOWN_SIDE_EFFECTS();
1506 /* Carve out a piece of the trace buffer, returning NULL in case of
1510 trace_buffer_alloc (size_t amt
)
1512 unsigned char *rslt
;
1513 struct trace_buffer_control
*tbctrl
;
1515 #ifdef IN_PROCESS_AGENT
1516 unsigned int prev
, prev_filtered
;
1517 unsigned int commit_count
;
1518 unsigned int commit
;
1519 unsigned int readout
;
1521 struct traceframe
*oldest
;
1522 unsigned char *new_start
;
1525 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1526 (long) amt
, (long) sizeof (struct traceframe
));
1528 /* Account for the EOB marker. */
1529 amt
+= TRACEFRAME_EOB_MARKER_SIZE
;
1531 #ifdef IN_PROCESS_AGENT
1535 /* Read the current token and extract the index to try to write to,
1536 storing it in CURR. */
1537 prev
= trace_buffer_ctrl_curr
;
1538 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1539 curr
= prev_filtered
+ 1;
1543 about_to_request_buffer_space ();
1545 /* Start out with a copy of the current state. GDBserver may be
1546 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1547 be able to commit anyway if that happens. */
1548 trace_buffer_ctrl
[curr
]
1549 = trace_buffer_ctrl
[prev_filtered
];
1550 trace_debug ("trying curr=%u", curr
);
1552 /* The GDBserver's agent doesn't need all that syncing, and always
1553 updates TCB 0 (there's only one, mind you). */
1556 tbctrl
= &trace_buffer_ctrl
[curr
];
1558 /* Offsets are easier to grok for debugging than raw addresses,
1559 especially for the small trace buffer sizes that are useful for
1561 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1563 (int) (tbctrl
->start
- trace_buffer_lo
),
1564 (int) (tbctrl
->free
- trace_buffer_lo
),
1565 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1566 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1567 (int) (trace_buffer_hi
- trace_buffer_lo
));
1569 /* The algorithm here is to keep trying to get a contiguous block of
1570 the requested size, possibly discarding older traceframes to free
1571 up space. Since free space might come in one or two pieces,
1572 depending on whether discarded traceframes wrapped around at the
1573 high end of the buffer, we test both pieces after each
1577 /* First, if we have two free parts, try the upper one first. */
1578 if (tbctrl
->end_free
< tbctrl
->free
)
1580 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1581 /* We have enough in the upper part. */
1585 /* Our high part of free space wasn't enough. Give up
1586 on it for now, set wraparound. We will recover the
1587 space later, if/when the wrapped-around traceframe is
1589 trace_debug ("Upper part too small, setting wraparound");
1590 tbctrl
->wrap
= tbctrl
->free
;
1591 tbctrl
->free
= trace_buffer_lo
;
1595 /* The normal case. */
1596 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1599 #ifdef IN_PROCESS_AGENT
1600 /* The IP Agent's buffer is always circular. It isn't used
1601 currently, but `circular_trace_buffer' could represent
1602 GDBserver's mode. If we didn't find space, ask GDBserver to
1605 flush_trace_buffer ();
1609 trace_debug ("gdbserver flushed buffer, retrying");
1613 /* GDBserver cancelled the tracing. Bail out as well. */
1616 /* If we're here, then neither part is big enough, and
1617 non-circular trace buffers are now full. */
1618 if (!circular_trace_buffer
)
1620 trace_debug ("Not enough space in the trace buffer");
1624 trace_debug ("Need more space in the trace buffer");
1626 /* If we have a circular buffer, we can try discarding the
1627 oldest traceframe and see if that helps. */
1628 oldest
= FIRST_TRACEFRAME ();
1629 if (oldest
->tpnum
== 0)
1631 /* Not good; we have no traceframes to free. Perhaps we're
1632 asking for a block that is larger than the buffer? In
1633 any case, give up. */
1634 trace_debug ("No traceframes to discard");
1638 /* We don't run this code in the in-process agent currently.
1639 E.g., we could leave the in-process agent in autonomous
1640 circular mode if we only have fast tracepoints. If we do
1641 that, then this bit becomes racy with GDBserver, which also
1642 writes to this counter. */
1643 --traceframe_write_count
;
1645 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1646 /* If we freed the traceframe that wrapped around, go back
1647 to the non-wrap case. */
1648 if (new_start
< tbctrl
->start
)
1650 trace_debug ("Discarding past the wraparound");
1651 tbctrl
->wrap
= trace_buffer_hi
;
1653 tbctrl
->start
= new_start
;
1654 tbctrl
->end_free
= tbctrl
->start
;
1656 trace_debug ("Discarded a traceframe\n"
1657 "Trace buffer [%d], start=%d free=%d "
1658 "endfree=%d wrap=%d hi=%d",
1660 (int) (tbctrl
->start
- trace_buffer_lo
),
1661 (int) (tbctrl
->free
- trace_buffer_lo
),
1662 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1663 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1664 (int) (trace_buffer_hi
- trace_buffer_lo
));
1666 /* Now go back around the loop. The discard might have resulted
1667 in either one or two pieces of free space, so we want to try
1668 both before freeing any more traceframes. */
1672 /* If we get here, we know we can provide the asked-for space. */
1674 rslt
= tbctrl
->free
;
1676 /* Adjust the request back down, now that we know we have space for
1677 the marker, but don't commit to AMT yet, we may still need to
1678 restart the operation if GDBserver touches the trace buffer
1679 (obviously only important in the in-process agent's version). */
1680 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1682 /* Or not. If GDBserver changed the trace buffer behind our back,
1683 we get to restart a new allocation attempt. */
1685 #ifdef IN_PROCESS_AGENT
1686 /* Build the tentative token. */
1687 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1688 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1689 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1693 /* Try to commit it. */
1694 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1695 if (readout
!= prev
)
1697 trace_debug ("GDBserver has touched the trace buffer, restarting."
1698 " (prev=%08x, commit=%08x, readout=%08x)",
1699 prev
, commit
, readout
);
1703 /* Hold your horses here. Even if that change was committed,
1704 GDBserver could come in, and clobber it. We need to hold to be
1705 able to tell if GDBserver clobbers before or after we committed
1706 the change. Whenever GDBserver goes about touching the IPA
1707 buffer, it sets a breakpoint in this routine, so we have a sync
1709 about_to_request_buffer_space ();
1711 /* Check if the change has been effective, even if GDBserver stopped
1712 us at the breakpoint. */
1715 unsigned int refetch
;
1719 refetch
= trace_buffer_ctrl_curr
;
1721 if (refetch
== commit
1722 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1725 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1726 "readout=%08x, refetch=%08x)",
1727 prev
, commit
, readout
, refetch
);
1731 trace_debug ("GDBserver has touched the trace buffer, not effective."
1732 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1733 prev
, commit
, readout
, refetch
);
1739 /* We have a new piece of the trace buffer. Hurray! */
1741 /* Add an EOB marker just past this allocation. */
1742 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1743 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1745 /* Adjust the request back down, now that we know we have space for
1747 amt
-= sizeof (struct traceframe
);
1751 trace_debug ("Allocated %d bytes", (int) amt
);
1752 trace_debug ("Trace buffer [%d] start=%d free=%d "
1753 "endfree=%d wrap=%d hi=%d",
1755 (int) (tbctrl
->start
- trace_buffer_lo
),
1756 (int) (tbctrl
->free
- trace_buffer_lo
),
1757 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1758 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1759 (int) (trace_buffer_hi
- trace_buffer_lo
));
1765 #ifndef IN_PROCESS_AGENT
1767 /* Return the total free space. This is not necessarily the largest
1768 block we can allocate, because of the two-part case. */
1773 if (trace_buffer_free
<= trace_buffer_end_free
)
1774 return trace_buffer_end_free
- trace_buffer_free
;
1776 return ((trace_buffer_end_free
- trace_buffer_lo
)
1777 + (trace_buffer_hi
- trace_buffer_free
));
1780 /* An 'S' in continuation packets indicates remainder are for
1783 static int seen_step_action_flag
;
1785 /* Create a tracepoint (location) with given number and address. Add this
1786 new tracepoint to list and sort this list. */
1788 static struct tracepoint
*
1789 add_tracepoint (int num
, CORE_ADDR addr
)
1791 struct tracepoint
*tpoint
, **tp_next
;
1793 tpoint
= XNEW (struct tracepoint
);
1794 tpoint
->number
= num
;
1795 tpoint
->address
= addr
;
1796 tpoint
->numactions
= 0;
1797 tpoint
->actions
= NULL
;
1798 tpoint
->actions_str
= NULL
;
1799 tpoint
->cond
= NULL
;
1800 tpoint
->num_step_actions
= 0;
1801 tpoint
->step_actions
= NULL
;
1802 tpoint
->step_actions_str
= NULL
;
1803 /* Start all off as regular (slow) tracepoints. */
1804 tpoint
->type
= trap_tracepoint
;
1805 tpoint
->orig_size
= -1;
1806 tpoint
->source_strings
= NULL
;
1807 tpoint
->compiled_cond
= 0;
1808 tpoint
->handle
= NULL
;
1809 tpoint
->next
= NULL
;
1811 /* Find a place to insert this tracepoint into list in order to keep
1812 the tracepoint list still in the ascending order. There may be
1813 multiple tracepoints at the same address as TPOINT's, and this
1814 guarantees TPOINT is inserted after all the tracepoints which are
1815 set at the same address. For example, fast tracepoints A, B, C are
1816 set at the same address, and D is to be insert at the same place as
1819 -->| A |--> | B |-->| C |->...
1821 One jump pad was created for tracepoint A, B, and C, and the target
1822 address of A is referenced/used in jump pad. So jump pad will let
1823 inferior jump to A. If D is inserted in front of A, like this,
1825 -->| D |-->| A |--> | B |-->| C |->...
1827 without updating jump pad, D is not reachable during collect, which
1828 is wrong. As we can see, the order of B, C and D doesn't matter, but
1829 A should always be the `first' one. */
1830 for (tp_next
= &tracepoints
;
1831 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1832 tp_next
= &(*tp_next
)->next
)
1834 tpoint
->next
= *tp_next
;
1836 last_tracepoint
= tpoint
;
1838 seen_step_action_flag
= 0;
1843 #ifndef IN_PROCESS_AGENT
1845 /* Return the tracepoint with the given number and address, or NULL. */
1847 static struct tracepoint
*
1848 find_tracepoint (int id
, CORE_ADDR addr
)
1850 struct tracepoint
*tpoint
;
1852 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1853 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1859 /* Remove TPOINT from global list. */
1862 remove_tracepoint (struct tracepoint
*tpoint
)
1864 struct tracepoint
*tp
, *tp_prev
;
1866 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1867 tp_prev
= tp
, tp
= tp
->next
)
1873 tp_prev
->next
= tp
->next
;
1875 tracepoints
= tp
->next
;
1881 /* There may be several tracepoints with the same number (because they
1882 are "locations", in GDB parlance); return the next one after the
1883 given tracepoint, or search from the beginning of the list if the
1884 first argument is NULL. */
1886 static struct tracepoint
*
1887 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1889 struct tracepoint
*tpoint
;
1892 tpoint
= prev_tp
->next
;
1894 tpoint
= tracepoints
;
1895 for (; tpoint
; tpoint
= tpoint
->next
)
1896 if (tpoint
->number
== num
)
1904 /* Append another action to perform when the tracepoint triggers. */
1907 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1913 seen_step_action_flag
= 1;
1921 char *act_start
= act
;
1922 struct tracepoint_action
*action
= NULL
;
1928 struct collect_memory_action
*maction
=
1929 XNEW (struct collect_memory_action
);
1933 maction
->base
.type
= *act
;
1934 action
= &maction
->base
;
1937 is_neg
= (*act
== '-');
1940 act
= unpack_varlen_hex (act
, &basereg
);
1942 act
= unpack_varlen_hex (act
, &maction
->addr
);
1944 act
= unpack_varlen_hex (act
, &maction
->len
);
1945 maction
->basereg
= (is_neg
1948 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1949 pulongest (maction
->len
),
1950 paddress (maction
->addr
), maction
->basereg
);
1955 struct collect_registers_action
*raction
=
1956 XNEW (struct collect_registers_action
);
1958 raction
->base
.type
= *act
;
1959 action
= &raction
->base
;
1961 trace_debug ("Want to collect registers");
1963 /* skip past hex digits of mask for now */
1964 while (isxdigit(*act
))
1970 struct collect_static_trace_data_action
*raction
=
1971 XNEW (struct collect_static_trace_data_action
);
1973 raction
->base
.type
= *act
;
1974 action
= &raction
->base
;
1976 trace_debug ("Want to collect static trace data");
1981 trace_debug ("Unexpected step action, ignoring");
1986 struct eval_expr_action
*xaction
= XNEW (struct eval_expr_action
);
1988 xaction
->base
.type
= *act
;
1989 action
= &xaction
->base
;
1991 trace_debug ("Want to evaluate expression");
1992 xaction
->expr
= gdb_parse_agent_expr (&act
);
1996 trace_debug ("unknown trace action '%c', ignoring...", *act
);
2005 if (seen_step_action_flag
)
2007 tpoint
->num_step_actions
++;
2009 tpoint
->step_actions
2010 = XRESIZEVEC (struct tracepoint_action
*, tpoint
->step_actions
,
2011 tpoint
->num_step_actions
);
2012 tpoint
->step_actions_str
2013 = XRESIZEVEC (char *, tpoint
->step_actions_str
,
2014 tpoint
->num_step_actions
);
2015 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
2016 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
2017 = savestring (act_start
, act
- act_start
);
2021 tpoint
->numactions
++;
2023 = XRESIZEVEC (struct tracepoint_action
*, tpoint
->actions
,
2024 tpoint
->numactions
);
2026 = XRESIZEVEC (char *, tpoint
->actions_str
, tpoint
->numactions
);
2027 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
2028 tpoint
->actions_str
[tpoint
->numactions
- 1]
2029 = savestring (act_start
, act
- act_start
);
2036 /* Find or create a trace state variable with the given number. */
2038 static struct trace_state_variable
*
2039 get_trace_state_variable (int num
)
2041 struct trace_state_variable
*tsv
;
2043 #ifdef IN_PROCESS_AGENT
2044 /* Search for an existing variable. */
2045 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
2046 if (tsv
->number
== num
)
2050 /* Search for an existing variable. */
2051 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
2052 if (tsv
->number
== num
)
2058 /* Find or create a trace state variable with the given number. */
2060 static struct trace_state_variable
*
2061 create_trace_state_variable (int num
, int gdb
)
2063 struct trace_state_variable
*tsv
;
2065 tsv
= get_trace_state_variable (num
);
2069 /* Create a new variable. */
2070 tsv
= XNEW (struct trace_state_variable
);
2072 tsv
->initial_value
= 0;
2076 #ifdef IN_PROCESS_AGENT
2079 tsv
->next
= alloced_trace_state_variables
;
2080 alloced_trace_state_variables
= tsv
;
2085 tsv
->next
= trace_state_variables
;
2086 trace_state_variables
= tsv
;
2091 IP_AGENT_EXPORT_FUNC LONGEST
2092 get_trace_state_variable_value (int num
)
2094 struct trace_state_variable
*tsv
;
2096 tsv
= get_trace_state_variable (num
);
2100 trace_debug ("No trace state variable %d, skipping value get", num
);
2104 /* Call a getter function if we have one. While it's tempting to
2105 set up something to only call the getter once per tracepoint hit,
2106 it could run afoul of thread races. Better to let the getter
2107 handle it directly, if necessary to worry about it. */
2109 tsv
->value
= (tsv
->getter
) ();
2111 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2112 num
, plongest (tsv
->value
));
2117 IP_AGENT_EXPORT_FUNC
void
2118 set_trace_state_variable_value (int num
, LONGEST val
)
2120 struct trace_state_variable
*tsv
;
2122 tsv
= get_trace_state_variable (num
);
2126 trace_debug ("No trace state variable %d, skipping value set", num
);
2134 agent_get_trace_state_variable_value (int num
)
2136 return get_trace_state_variable_value (num
);
2140 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2142 set_trace_state_variable_value (num
, val
);
2146 set_trace_state_variable_name (int num
, const char *name
)
2148 struct trace_state_variable
*tsv
;
2150 tsv
= get_trace_state_variable (num
);
2154 trace_debug ("No trace state variable %d, skipping name set", num
);
2158 tsv
->name
= (char *) name
;
2162 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2164 struct trace_state_variable
*tsv
;
2166 tsv
= get_trace_state_variable (num
);
2170 trace_debug ("No trace state variable %d, skipping getter set", num
);
2174 tsv
->getter
= getter
;
2177 /* Add a raw traceframe for the given tracepoint. */
2179 static struct traceframe
*
2180 add_traceframe (struct tracepoint
*tpoint
)
2182 struct traceframe
*tframe
;
2185 = (struct traceframe
*) trace_buffer_alloc (sizeof (struct traceframe
));
2190 tframe
->tpnum
= tpoint
->number
;
2191 tframe
->data_size
= 0;
2196 /* Add a block to the traceframe currently being worked on. */
2198 static unsigned char *
2199 add_traceframe_block (struct traceframe
*tframe
,
2200 struct tracepoint
*tpoint
, int amt
)
2202 unsigned char *block
;
2207 block
= (unsigned char *) trace_buffer_alloc (amt
);
2212 gdb_assert (tframe
->tpnum
== tpoint
->number
);
2214 tframe
->data_size
+= amt
;
2215 tpoint
->traceframe_usage
+= amt
;
2220 /* Flag that the current traceframe is finished. */
2223 finish_traceframe (struct traceframe
*tframe
)
2225 ++traceframe_write_count
;
2226 ++traceframes_created
;
2229 #ifndef IN_PROCESS_AGENT
2231 /* Given a traceframe number NUM, find the NUMth traceframe in the
2234 static struct traceframe
*
2235 find_traceframe (int num
)
2237 struct traceframe
*tframe
;
2240 for (tframe
= FIRST_TRACEFRAME ();
2242 tframe
= NEXT_TRACEFRAME (tframe
))
2253 get_traceframe_address (struct traceframe
*tframe
)
2256 struct tracepoint
*tpoint
;
2258 addr
= traceframe_get_pc (tframe
);
2263 /* Fallback strategy, will be incorrect for while-stepping frames
2264 and multi-location tracepoints. */
2265 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2266 return tpoint
->address
;
2269 /* Search for the next traceframe whose address is inside or outside
2272 static struct traceframe
*
2273 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2276 struct traceframe
*tframe
;
2279 *tfnump
= current_traceframe
+ 1;
2280 tframe
= find_traceframe (*tfnump
);
2281 /* The search is not supposed to wrap around. */
2288 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2290 tfaddr
= get_traceframe_address (tframe
);
2292 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2293 : (lo
> tfaddr
|| tfaddr
> hi
))
2302 /* Search for the next traceframe recorded by the given tracepoint.
2303 Note that for multi-location tracepoints, this will find whatever
2304 location appears first. */
2306 static struct traceframe
*
2307 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2309 struct traceframe
*tframe
;
2311 *tfnump
= current_traceframe
+ 1;
2312 tframe
= find_traceframe (*tfnump
);
2313 /* The search is not supposed to wrap around. */
2320 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2322 if (tframe
->tpnum
== num
)
2333 #ifndef IN_PROCESS_AGENT
2335 /* Clear all past trace state. */
2338 cmd_qtinit (char *packet
)
2340 struct trace_state_variable
*tsv
, *prev
, *next
;
2342 /* Can't do this command without a pid attached. */
2343 if (current_thread
== NULL
)
2349 /* Make sure we don't try to read from a trace frame. */
2350 current_traceframe
= -1;
2354 trace_debug ("Initializing the trace");
2356 clear_installed_tracepoints ();
2357 clear_readonly_regions ();
2360 last_tracepoint
= NULL
;
2362 /* Clear out any leftover trace state variables. Ones with target
2363 defined getters should be kept however. */
2365 tsv
= trace_state_variables
;
2368 trace_debug ("Looking at var %d", tsv
->number
);
2369 if (tsv
->getter
== NULL
)
2375 trace_state_variables
= next
;
2376 trace_debug ("Deleting var %d", tsv
->number
);
2387 clear_trace_buffer ();
2388 clear_inferior_trace_buffer ();
2393 /* Unprobe the UST marker at ADDRESS. */
2396 unprobe_marker_at (CORE_ADDR address
)
2398 char cmd
[IPA_CMD_BUF_SIZE
];
2400 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2401 run_inferior_command (cmd
, strlen (cmd
) + 1);
2404 /* Restore the program to its pre-tracing state. This routine may be called
2405 in error situations, so it needs to be careful about only restoring
2406 from known-valid bits. */
2409 clear_installed_tracepoints (void)
2411 struct tracepoint
*tpoint
;
2412 struct tracepoint
*prev_stpoint
;
2416 prev_stpoint
= NULL
;
2418 /* Restore any bytes overwritten by tracepoints. */
2419 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2421 /* Catch the case where we might try to remove a tracepoint that
2422 was never actually installed. */
2423 if (tpoint
->handle
== NULL
)
2425 trace_debug ("Tracepoint %d at 0x%s was "
2426 "never installed, nothing to clear",
2427 tpoint
->number
, paddress (tpoint
->address
));
2431 switch (tpoint
->type
)
2433 case trap_tracepoint
:
2435 struct breakpoint
*bp
2436 = (struct breakpoint
*) tpoint
->handle
;
2438 delete_breakpoint (bp
);
2441 case fast_tracepoint
:
2443 struct fast_tracepoint_jump
*jump
2444 = (struct fast_tracepoint_jump
*) tpoint
->handle
;
2446 delete_fast_tracepoint_jump (jump
);
2449 case static_tracepoint
:
2450 if (prev_stpoint
!= NULL
2451 && prev_stpoint
->address
== tpoint
->address
)
2452 /* Nothing to do. We already unprobed a tracepoint set at
2453 this marker address (and there can only be one probe
2458 unprobe_marker_at (tpoint
->address
);
2459 prev_stpoint
= tpoint
;
2464 tpoint
->handle
= NULL
;
2470 /* Parse a packet that defines a tracepoint. */
2473 cmd_qtdp (char *own_buf
)
2476 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2477 int trail_hyphen
= 0;
2481 struct tracepoint
*tpoint
;
2483 char *packet
= own_buf
;
2485 packet
+= strlen ("QTDP:");
2487 /* A hyphen at the beginning marks a packet specifying actions for a
2488 tracepoint already supplied. */
2495 packet
= unpack_varlen_hex (packet
, &num
);
2496 ++packet
; /* skip a colon */
2497 packet
= unpack_varlen_hex (packet
, &addr
);
2498 ++packet
; /* skip a colon */
2500 /* See if we already have this tracepoint. */
2501 tpoint
= find_tracepoint (num
, addr
);
2505 /* Duplicate tracepoints are never allowed. */
2508 trace_debug ("Tracepoint error: tracepoint %d"
2509 " at 0x%s already exists",
2510 (int) num
, paddress (addr
));
2511 write_enn (own_buf
);
2515 tpoint
= add_tracepoint (num
, addr
);
2517 tpoint
->enabled
= (*packet
== 'E');
2518 ++packet
; /* skip 'E' */
2519 ++packet
; /* skip a colon */
2520 packet
= unpack_varlen_hex (packet
, &count
);
2521 tpoint
->step_count
= count
;
2522 ++packet
; /* skip a colon */
2523 packet
= unpack_varlen_hex (packet
, &count
);
2524 tpoint
->pass_count
= count
;
2525 /* See if we have any of the additional optional fields. */
2526 while (*packet
== ':')
2531 tpoint
->type
= fast_tracepoint
;
2533 packet
= unpack_varlen_hex (packet
, &count
);
2534 tpoint
->orig_size
= count
;
2536 else if (*packet
== 'S')
2538 tpoint
->type
= static_tracepoint
;
2541 else if (*packet
== 'X')
2543 actparm
= (char *) packet
;
2544 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2547 else if (*packet
== '-')
2549 else if (*packet
== '\0')
2552 trace_debug ("Unknown optional tracepoint field");
2557 trace_debug ("Also has actions\n");
2560 trace_debug ("Defined %stracepoint %d at 0x%s, "
2561 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2562 tpoint
->type
== fast_tracepoint
? "fast "
2563 : tpoint
->type
== static_tracepoint
? "static " : "",
2564 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2565 tpoint
->step_count
, tpoint
->pass_count
);
2568 add_tracepoint_action (tpoint
, packet
);
2571 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2572 (int) num
, paddress (addr
));
2573 write_enn (own_buf
);
2577 /* Install tracepoint during tracing only once for each tracepoint location.
2578 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2579 determine the last QTDP packet for one tracepoint location by checking
2580 trailing hyphen in QTDP packet. */
2581 if (tracing
&& !trail_hyphen
)
2583 struct tracepoint
*tp
= NULL
;
2585 /* Pause all threads temporarily while we patch tracepoints. */
2588 /* download_tracepoint will update global `tracepoints'
2589 list, so it is unsafe to leave threads in jump pad. */
2590 stabilize_threads ();
2592 /* Freeze threads. */
2596 if (tpoint
->type
!= trap_tracepoint
)
2598 /* Find another fast or static tracepoint at the same address. */
2599 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2601 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2602 && tp
->number
!= tpoint
->number
)
2606 /* TPOINT is installed at the same address as TP. */
2609 if (tpoint
->type
== fast_tracepoint
)
2610 clone_fast_tracepoint (tpoint
, tp
);
2611 else if (tpoint
->type
== static_tracepoint
)
2612 tpoint
->handle
= (void *) -1;
2616 if (use_agent
&& tpoint
->type
== fast_tracepoint
2617 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
2619 /* Download and install fast tracepoint by agent. */
2620 if (tracepoint_send_agent (tpoint
) == 0)
2624 write_enn (own_buf
);
2625 remove_tracepoint (tpoint
);
2630 download_tracepoint (tpoint
);
2632 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2634 install_tracepoint (tpoint
, own_buf
);
2635 if (strcmp (own_buf
, "OK") != 0)
2636 remove_tracepoint (tpoint
);
2650 cmd_qtdpsrc (char *own_buf
)
2652 ULONGEST num
, addr
, start
, slen
;
2653 struct tracepoint
*tpoint
;
2654 char *packet
= own_buf
;
2655 char *saved
, *srctype
, *src
;
2657 struct source_string
*last
, *newlast
;
2659 packet
+= strlen ("QTDPsrc:");
2661 packet
= unpack_varlen_hex (packet
, &num
);
2662 ++packet
; /* skip a colon */
2663 packet
= unpack_varlen_hex (packet
, &addr
);
2664 ++packet
; /* skip a colon */
2666 /* See if we already have this tracepoint. */
2667 tpoint
= find_tracepoint (num
, addr
);
2671 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2672 (int) num
, paddress (addr
));
2673 write_enn (own_buf
);
2678 packet
= strchr (packet
, ':');
2679 srctype
= (char *) xmalloc (packet
- saved
+ 1);
2680 memcpy (srctype
, saved
, packet
- saved
);
2681 srctype
[packet
- saved
] = '\0';
2683 packet
= unpack_varlen_hex (packet
, &start
);
2684 ++packet
; /* skip a colon */
2685 packet
= unpack_varlen_hex (packet
, &slen
);
2686 ++packet
; /* skip a colon */
2687 src
= (char *) xmalloc (slen
+ 1);
2688 nbytes
= hex2bin (packet
, (gdb_byte
*) src
, strlen (packet
) / 2);
2691 newlast
= XNEW (struct source_string
);
2692 newlast
->type
= srctype
;
2694 newlast
->next
= NULL
;
2695 /* Always add a source string to the end of the list;
2696 this keeps sequences of actions/commands in the right
2698 if (tpoint
->source_strings
)
2700 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2702 last
->next
= newlast
;
2705 tpoint
->source_strings
= newlast
;
2711 cmd_qtdv (char *own_buf
)
2713 ULONGEST num
, val
, builtin
;
2716 struct trace_state_variable
*tsv
;
2717 char *packet
= own_buf
;
2719 packet
+= strlen ("QTDV:");
2721 packet
= unpack_varlen_hex (packet
, &num
);
2722 ++packet
; /* skip a colon */
2723 packet
= unpack_varlen_hex (packet
, &val
);
2724 ++packet
; /* skip a colon */
2725 packet
= unpack_varlen_hex (packet
, &builtin
);
2726 ++packet
; /* skip a colon */
2728 nbytes
= strlen (packet
) / 2;
2729 varname
= (char *) xmalloc (nbytes
+ 1);
2730 nbytes
= hex2bin (packet
, (gdb_byte
*) varname
, nbytes
);
2731 varname
[nbytes
] = '\0';
2733 tsv
= create_trace_state_variable (num
, 1);
2734 tsv
->initial_value
= (LONGEST
) val
;
2735 tsv
->name
= varname
;
2737 set_trace_state_variable_value (num
, (LONGEST
) val
);
2743 cmd_qtenable_disable (char *own_buf
, int enable
)
2745 char *packet
= own_buf
;
2747 struct tracepoint
*tp
;
2749 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2750 packet
= unpack_varlen_hex (packet
, &num
);
2751 ++packet
; /* skip a colon */
2752 packet
= unpack_varlen_hex (packet
, &addr
);
2754 tp
= find_tracepoint (num
, addr
);
2758 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2760 trace_debug ("Tracepoint %d at 0x%s is already %s",
2761 (int) num
, paddress (addr
),
2762 enable
? "enabled" : "disabled");
2767 trace_debug ("%s tracepoint %d at 0x%s",
2768 enable
? "Enabling" : "Disabling",
2769 (int) num
, paddress (addr
));
2771 tp
->enabled
= enable
;
2773 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2776 int offset
= offsetof (struct tracepoint
, enabled
);
2777 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2779 ret
= prepare_to_access_memory ();
2782 trace_debug ("Failed to temporarily stop inferior threads");
2783 write_enn (own_buf
);
2787 ret
= write_inferior_integer (obj_addr
, enable
);
2788 done_accessing_memory ();
2792 trace_debug ("Cannot write enabled flag into "
2793 "inferior process memory");
2794 write_enn (own_buf
);
2803 trace_debug ("Tracepoint %d at 0x%s not found",
2804 (int) num
, paddress (addr
));
2805 write_enn (own_buf
);
2810 cmd_qtv (char *own_buf
)
2815 char *packet
= own_buf
;
2817 packet
+= strlen ("qTV:");
2818 unpack_varlen_hex (packet
, &num
);
2820 if (current_traceframe
>= 0)
2822 err
= traceframe_read_tsv ((int) num
, &val
);
2825 strcpy (own_buf
, "U");
2829 /* Only make tsv's be undefined before the first trace run. After a
2830 trace run is over, the user might want to see the last value of
2831 the tsv, and it might not be available in a traceframe. */
2832 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2834 strcpy (own_buf
, "U");
2838 val
= get_trace_state_variable_value (num
);
2840 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2843 /* Clear out the list of readonly regions. */
2846 clear_readonly_regions (void)
2848 struct readonly_region
*roreg
;
2850 while (readonly_regions
)
2852 roreg
= readonly_regions
;
2853 readonly_regions
= readonly_regions
->next
;
2858 /* Parse the collection of address ranges whose contents GDB believes
2859 to be unchanging and so can be read directly from target memory
2860 even while looking at a traceframe. */
2863 cmd_qtro (char *own_buf
)
2865 ULONGEST start
, end
;
2866 struct readonly_region
*roreg
;
2867 char *packet
= own_buf
;
2869 trace_debug ("Want to mark readonly regions");
2871 clear_readonly_regions ();
2873 packet
+= strlen ("QTro");
2875 while (*packet
== ':')
2877 ++packet
; /* skip a colon */
2878 packet
= unpack_varlen_hex (packet
, &start
);
2879 ++packet
; /* skip a comma */
2880 packet
= unpack_varlen_hex (packet
, &end
);
2882 roreg
= XNEW (struct readonly_region
);
2883 roreg
->start
= start
;
2885 roreg
->next
= readonly_regions
;
2886 readonly_regions
= roreg
;
2887 trace_debug ("Added readonly region from 0x%s to 0x%s",
2888 paddress (roreg
->start
), paddress (roreg
->end
));
2894 /* Test to see if the given range is in our list of readonly ranges.
2895 We only test for being entirely within a range, GDB is not going to
2896 send a single memory packet that spans multiple regions. */
2899 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2901 struct readonly_region
*roreg
;
2903 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2904 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2910 static CORE_ADDR gdb_jump_pad_head
;
2912 /* Return the address of the next free jump space. */
2915 get_jump_space_head (void)
2917 if (gdb_jump_pad_head
== 0)
2919 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2920 &gdb_jump_pad_head
))
2922 internal_error (__FILE__
, __LINE__
,
2923 "error extracting jump_pad_buffer");
2927 return gdb_jump_pad_head
;
2930 /* Reserve USED bytes from the jump space. */
2933 claim_jump_space (ULONGEST used
)
2935 trace_debug ("claim_jump_space reserves %s bytes at %s",
2936 pulongest (used
), paddress (gdb_jump_pad_head
));
2937 gdb_jump_pad_head
+= used
;
2940 static CORE_ADDR trampoline_buffer_head
= 0;
2941 static CORE_ADDR trampoline_buffer_tail
;
2943 /* Reserve USED bytes from the trampoline buffer and return the
2944 address of the start of the reserved space in TRAMPOLINE. Returns
2945 non-zero if the space is successfully claimed. */
2948 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2950 if (!trampoline_buffer_head
)
2952 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2953 &trampoline_buffer_tail
))
2955 internal_error (__FILE__
, __LINE__
,
2956 "error extracting trampoline_buffer");
2959 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2960 &trampoline_buffer_head
))
2962 internal_error (__FILE__
, __LINE__
,
2963 "error extracting trampoline_buffer_end");
2967 /* Start claiming space from the top of the trampoline space. If
2968 the space is located at the bottom of the virtual address space,
2969 this reduces the possibility that corruption will occur if a null
2970 pointer is used to write to memory. */
2971 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2973 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2978 trampoline_buffer_head
-= used
;
2980 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2981 pulongest (used
), paddress (trampoline_buffer_head
));
2983 *trampoline
= trampoline_buffer_head
;
2987 /* Returns non-zero if there is space allocated for use in trampolines
2988 for fast tracepoints. */
2991 have_fast_tracepoint_trampoline_buffer (char *buf
)
2993 CORE_ADDR trampoline_end
, errbuf
;
2995 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2998 internal_error (__FILE__
, __LINE__
,
2999 "error extracting trampoline_buffer_end");
3005 strcpy (buf
, "was claiming");
3006 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
3009 internal_error (__FILE__
, __LINE__
,
3010 "error extracting errbuf");
3013 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
3016 return trampoline_end
!= 0;
3019 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3020 the command fails, or 0 otherwise. If the command ran
3021 successfully, but probing the marker failed, ERROUT will be filled
3022 with the error to reply to GDB, and -1 is also returned. This
3023 allows directly passing IPA errors to GDB. */
3026 probe_marker_at (CORE_ADDR address
, char *errout
)
3028 char cmd
[IPA_CMD_BUF_SIZE
];
3031 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
3032 err
= run_inferior_command (cmd
, strlen (cmd
) + 1);
3038 strcpy (errout
, cmd
);
3047 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
3049 to
->jump_pad
= from
->jump_pad
;
3050 to
->jump_pad_end
= from
->jump_pad_end
;
3051 to
->trampoline
= from
->trampoline
;
3052 to
->trampoline_end
= from
->trampoline_end
;
3053 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
3054 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
3055 to
->handle
= from
->handle
;
3057 gdb_assert (from
->handle
);
3058 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
3061 #define MAX_JUMP_SIZE 20
3063 /* Install fast tracepoint. Return 0 if successful, otherwise return
3067 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
3069 CORE_ADDR jentry
, jump_entry
;
3070 CORE_ADDR trampoline
;
3071 ULONGEST trampoline_size
;
3073 /* The jump to the jump pad of the last fast tracepoint
3075 unsigned char fjump
[MAX_JUMP_SIZE
];
3076 ULONGEST fjump_size
;
3078 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
3080 trace_debug ("Requested a fast tracepoint on an instruction "
3081 "that is of less than the minimum length.");
3085 jentry
= jump_entry
= get_jump_space_head ();
3088 trampoline_size
= 0;
3090 /* Install the jump pad. */
3091 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3093 ipa_sym_addrs
.addr_gdb_collect
,
3094 ipa_sym_addrs
.addr_collecting
,
3097 &trampoline
, &trampoline_size
,
3099 &tpoint
->adjusted_insn_addr
,
3100 &tpoint
->adjusted_insn_addr_end
,
3107 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3110 if (tpoint
->handle
!= NULL
)
3112 tpoint
->jump_pad
= jump_entry
;
3113 tpoint
->jump_pad_end
= jentry
;
3114 tpoint
->trampoline
= trampoline
;
3115 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3117 /* Pad to 8-byte alignment. */
3118 jentry
= ((jentry
+ 7) & ~0x7);
3119 claim_jump_space (jentry
- jump_entry
);
3126 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3129 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3131 tpoint
->handle
= NULL
;
3134 if (tpoint
->type
== trap_tracepoint
)
3136 /* Tracepoints are installed as memory breakpoints. Just go
3137 ahead and install the trap. The breakpoints module
3138 handles duplicated breakpoints, and the memory read
3139 routine handles un-patching traps from memory reads. */
3140 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3141 tracepoint_handler
);
3143 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3145 if (!agent_loaded_p ())
3147 trace_debug ("Requested a %s tracepoint, but fast "
3148 "tracepoints aren't supported.",
3149 tpoint
->type
== static_tracepoint
? "static" : "fast");
3150 write_e_ipa_not_loaded (own_buf
);
3153 if (tpoint
->type
== static_tracepoint
3154 && !in_process_agent_supports_ust ())
3156 trace_debug ("Requested a static tracepoint, but static "
3157 "tracepoints are not supported.");
3158 write_e_ust_not_loaded (own_buf
);
3162 if (tpoint
->type
== fast_tracepoint
)
3163 install_fast_tracepoint (tpoint
, own_buf
);
3166 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3167 tpoint
->handle
= (void *) -1;
3172 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3174 if (tpoint
->handle
== NULL
)
3176 if (*own_buf
== '\0')
3177 write_enn (own_buf
);
3183 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3186 cmd_qtstart (char *packet
)
3188 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3189 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3191 trace_debug ("Starting the trace");
3193 /* Pause all threads temporarily while we patch tracepoints. */
3196 /* Get threads out of jump pads. Safe to do here, since this is a
3197 top level command. And, required to do here, since we're
3198 deleting/rewriting jump pads. */
3200 stabilize_threads ();
3202 /* Freeze threads. */
3205 /* Sync the fast tracepoints list in the inferior ftlib. */
3206 if (agent_loaded_p ())
3207 download_trace_state_variables ();
3209 /* No previous fast tpoint yet. */
3210 prev_ftpoint
= NULL
;
3212 /* No previous static tpoint yet. */
3213 prev_stpoint
= NULL
;
3217 if (agent_loaded_p ())
3219 /* Tell IPA about the correct tdesc. */
3220 if (write_inferior_integer (ipa_sym_addrs
.addr_ipa_tdesc_idx
,
3221 target_get_ipa_tdesc_idx ()))
3222 error ("Error setting ipa_tdesc_idx variable in lib");
3225 /* Start out empty. */
3226 if (agent_loaded_p ())
3227 write_inferior_data_pointer (ipa_sym_addrs
.addr_tracepoints
, 0);
3229 /* Download and install tracepoints. */
3230 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3232 /* Ensure all the hit counts start at zero. */
3233 tpoint
->hit_count
= 0;
3234 tpoint
->traceframe_usage
= 0;
3236 if (tpoint
->type
== trap_tracepoint
)
3238 /* Tracepoints are installed as memory breakpoints. Just go
3239 ahead and install the trap. The breakpoints module
3240 handles duplicated breakpoints, and the memory read
3241 routine handles un-patching traps from memory reads. */
3242 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3243 tracepoint_handler
);
3245 else if (tpoint
->type
== fast_tracepoint
3246 || tpoint
->type
== static_tracepoint
)
3248 if (maybe_write_ipa_not_loaded (packet
))
3250 trace_debug ("Requested a %s tracepoint, but fast "
3251 "tracepoints aren't supported.",
3252 tpoint
->type
== static_tracepoint
3253 ? "static" : "fast");
3257 if (tpoint
->type
== fast_tracepoint
)
3260 = use_agent
&& agent_capability_check (AGENT_CAPA_FAST_TRACE
);
3262 if (prev_ftpoint
!= NULL
3263 && prev_ftpoint
->address
== tpoint
->address
)
3266 tracepoint_send_agent (tpoint
);
3268 download_tracepoint_1 (tpoint
);
3270 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3274 /* Tracepoint is installed successfully? */
3277 /* Download and install fast tracepoint by agent. */
3279 installed
= !tracepoint_send_agent (tpoint
);
3282 download_tracepoint_1 (tpoint
);
3283 installed
= !install_fast_tracepoint (tpoint
, packet
);
3287 prev_ftpoint
= tpoint
;
3292 if (!in_process_agent_supports_ust ())
3294 trace_debug ("Requested a static tracepoint, but static "
3295 "tracepoints are not supported.");
3299 download_tracepoint_1 (tpoint
);
3300 /* Can only probe a given marker once. */
3301 if (prev_stpoint
!= NULL
3302 && prev_stpoint
->address
== tpoint
->address
)
3303 tpoint
->handle
= (void *) -1;
3306 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3308 tpoint
->handle
= (void *) -1;
3310 /* So that we can handle multiple static tracepoints
3311 at the same address easily. */
3312 prev_stpoint
= tpoint
;
3318 tpptr
= tpoint
->obj_addr_on_target
;
3320 if (tpoint
== tracepoints
)
3321 /* First object in list, set the head pointer in the
3323 write_inferior_data_pointer (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3325 write_inferior_data_pointer (prev_tpptr
3326 + offsetof (struct tracepoint
, next
),
3330 /* Any failure in the inner loop is sufficient cause to give
3332 if (tpoint
->handle
== NULL
)
3336 /* Any error in tracepoint insertion is unacceptable; better to
3337 address the problem now, than end up with a useless or misleading
3341 clear_installed_tracepoints ();
3342 if (*packet
== '\0')
3348 stopping_tracepoint
= NULL
;
3349 trace_buffer_is_full
= 0;
3350 expr_eval_result
= expr_eval_no_error
;
3351 error_tracepoint
= NULL
;
3352 tracing_start_time
= get_timestamp ();
3354 /* Tracing is now active, hits will now start being logged. */
3357 if (agent_loaded_p ())
3359 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3361 internal_error (__FILE__
, __LINE__
,
3362 "Error setting tracing variable in lib");
3365 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3368 internal_error (__FILE__
, __LINE__
,
3369 "Error clearing stopping_tracepoint variable"
3373 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3375 internal_error (__FILE__
, __LINE__
,
3376 "Error clearing trace_buffer_is_full variable"
3380 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3381 stop_tracing_handler
);
3382 if (stop_tracing_bkpt
== NULL
)
3383 error ("Error setting stop_tracing breakpoint");
3385 flush_trace_buffer_bkpt
3386 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3387 flush_trace_buffer_handler
);
3388 if (flush_trace_buffer_bkpt
== NULL
)
3389 error ("Error setting flush_trace_buffer breakpoint");
3397 /* End a tracing run, filling in a stop reason to report back to GDB,
3398 and removing the tracepoints from the code. */
3405 trace_debug ("Tracing is already off, ignoring");
3409 trace_debug ("Stopping the trace");
3411 /* Pause all threads before removing fast jumps from memory,
3412 breakpoints, and touching IPA state variables (inferior memory).
3413 Some thread may hit the internal tracing breakpoints, or be
3414 collecting this moment, but that's ok, we don't release the
3415 tpoint object's memory or the jump pads here (we only do that
3416 when we're sure we can move all threads out of the jump pads).
3417 We can't now, since we may be getting here due to the inferior
3418 agent calling us. */
3421 /* Stop logging. Tracepoints can still be hit, but they will not be
3424 if (agent_loaded_p ())
3426 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3428 internal_error (__FILE__
, __LINE__
,
3429 "Error clearing tracing variable in lib");
3433 tracing_stop_time
= get_timestamp ();
3434 tracing_stop_reason
= "t???";
3435 tracing_stop_tpnum
= 0;
3436 if (stopping_tracepoint
)
3438 trace_debug ("Stopping the trace because "
3439 "tracepoint %d was hit %" PRIu64
" times",
3440 stopping_tracepoint
->number
,
3441 stopping_tracepoint
->pass_count
);
3442 tracing_stop_reason
= "tpasscount";
3443 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3445 else if (trace_buffer_is_full
)
3447 trace_debug ("Stopping the trace because the trace buffer is full");
3448 tracing_stop_reason
= "tfull";
3450 else if (expr_eval_result
!= expr_eval_no_error
)
3452 trace_debug ("Stopping the trace because of an expression eval error");
3453 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3454 tracing_stop_tpnum
= error_tracepoint
->number
;
3456 #ifndef IN_PROCESS_AGENT
3457 else if (!gdb_connected ())
3459 trace_debug ("Stopping the trace because GDB disconnected");
3460 tracing_stop_reason
= "tdisconnected";
3465 trace_debug ("Stopping the trace because of a tstop command");
3466 tracing_stop_reason
= "tstop";
3469 stopping_tracepoint
= NULL
;
3470 error_tracepoint
= NULL
;
3472 /* Clear out the tracepoints. */
3473 clear_installed_tracepoints ();
3475 if (agent_loaded_p ())
3477 /* Pull in fast tracepoint trace frames from the inferior lib
3478 buffer into our buffer, even if our buffer is already full,
3479 because we want to present the full number of created frames
3480 in addition to what fit in the trace buffer. */
3481 upload_fast_traceframes ();
3484 if (stop_tracing_bkpt
!= NULL
)
3486 delete_breakpoint (stop_tracing_bkpt
);
3487 stop_tracing_bkpt
= NULL
;
3490 if (flush_trace_buffer_bkpt
!= NULL
)
3492 delete_breakpoint (flush_trace_buffer_bkpt
);
3493 flush_trace_buffer_bkpt
= NULL
;
3500 stop_tracing_handler (CORE_ADDR addr
)
3502 trace_debug ("lib hit stop_tracing");
3504 /* Don't actually handle it here. When we stop tracing we remove
3505 breakpoints from the inferior, and that is not allowed in a
3506 breakpoint handler (as the caller is walking the breakpoint
3512 flush_trace_buffer_handler (CORE_ADDR addr
)
3514 trace_debug ("lib hit flush_trace_buffer");
3519 cmd_qtstop (char *packet
)
3526 cmd_qtdisconnected (char *own_buf
)
3529 char *packet
= own_buf
;
3531 packet
+= strlen ("QTDisconnected:");
3533 unpack_varlen_hex (packet
, &setting
);
3537 disconnected_tracing
= setting
;
3541 cmd_qtframe (char *own_buf
)
3543 ULONGEST frame
, pc
, lo
, hi
, num
;
3545 struct traceframe
*tframe
;
3546 char *packet
= own_buf
;
3548 packet
+= strlen ("QTFrame:");
3550 if (startswith (packet
, "pc:"))
3552 packet
+= strlen ("pc:");
3553 unpack_varlen_hex (packet
, &pc
);
3554 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3555 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3557 else if (startswith (packet
, "range:"))
3559 packet
+= strlen ("range:");
3560 packet
= unpack_varlen_hex (packet
, &lo
);
3562 unpack_varlen_hex (packet
, &hi
);
3563 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3564 paddress (lo
), paddress (hi
));
3565 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3567 else if (startswith (packet
, "outside:"))
3569 packet
+= strlen ("outside:");
3570 packet
= unpack_varlen_hex (packet
, &lo
);
3572 unpack_varlen_hex (packet
, &hi
);
3573 trace_debug ("Want to find next traceframe "
3574 "outside the range 0x%s to 0x%s",
3575 paddress (lo
), paddress (hi
));
3576 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3578 else if (startswith (packet
, "tdp:"))
3580 packet
+= strlen ("tdp:");
3581 unpack_varlen_hex (packet
, &num
);
3583 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3584 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3588 unpack_varlen_hex (packet
, &frame
);
3589 tfnum
= (int) frame
;
3592 trace_debug ("Want to stop looking at traceframes");
3593 current_traceframe
= -1;
3597 trace_debug ("Want to look at traceframe %d", tfnum
);
3598 tframe
= find_traceframe (tfnum
);
3603 current_traceframe
= tfnum
;
3604 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3607 sprintf (own_buf
, "F-1");
3611 cmd_qtstatus (char *packet
)
3613 char *stop_reason_rsp
= NULL
;
3614 char *buf1
, *buf2
, *buf3
;
3618 /* Translate the plain text of the notes back into hex for
3621 str
= (tracing_user_name
? tracing_user_name
: "");
3622 slen
= strlen (str
);
3623 buf1
= (char *) alloca (slen
* 2 + 1);
3624 bin2hex ((gdb_byte
*) str
, buf1
, slen
);
3626 str
= (tracing_notes
? tracing_notes
: "");
3627 slen
= strlen (str
);
3628 buf2
= (char *) alloca (slen
* 2 + 1);
3629 bin2hex ((gdb_byte
*) str
, buf2
, slen
);
3631 str
= (tracing_stop_note
? tracing_stop_note
: "");
3632 slen
= strlen (str
);
3633 buf3
= (char *) alloca (slen
* 2 + 1);
3634 bin2hex ((gdb_byte
*) str
, buf3
, slen
);
3636 trace_debug ("Returning trace status as %d, stop reason %s",
3637 tracing
, tracing_stop_reason
);
3639 if (agent_loaded_p ())
3643 upload_fast_traceframes ();
3648 stop_reason_rsp
= (char *) tracing_stop_reason
;
3650 /* The user visible error string in terror needs to be hex encoded.
3651 We leave it as plain string in `tracing_stop_reason' to ease
3653 if (startswith (stop_reason_rsp
, "terror:"))
3655 const char *result_name
;
3659 result_name
= stop_reason_rsp
+ strlen ("terror:");
3660 hexstr_len
= strlen (result_name
) * 2;
3662 = (char *) alloca (strlen ("terror:") + hexstr_len
+ 1);
3663 strcpy (p
, "terror:");
3665 bin2hex ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3668 /* If this was a forced stop, include any stop note that was supplied. */
3669 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3671 stop_reason_rsp
= (char *) alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3672 strcpy (stop_reason_rsp
, "tstop:");
3673 strcat (stop_reason_rsp
, buf3
);
3679 "tframes:%x;tcreated:%x;"
3680 "tfree:%x;tsize:%s;"
3683 "starttime:%s;stoptime:%s;"
3684 "username:%s;notes:%s:",
3686 stop_reason_rsp
, tracing_stop_tpnum
,
3687 traceframe_count
, traceframes_created
,
3688 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3689 circular_trace_buffer
,
3690 disconnected_tracing
,
3691 phex_nz (tracing_start_time
, sizeof (tracing_start_time
)),
3692 phex_nz (tracing_stop_time
, sizeof (tracing_stop_time
)),
3697 cmd_qtp (char *own_buf
)
3700 struct tracepoint
*tpoint
;
3701 char *packet
= own_buf
;
3703 packet
+= strlen ("qTP:");
3705 packet
= unpack_varlen_hex (packet
, &num
);
3706 ++packet
; /* skip a colon */
3707 packet
= unpack_varlen_hex (packet
, &addr
);
3709 /* See if we already have this tracepoint. */
3710 tpoint
= find_tracepoint (num
, addr
);
3714 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3715 (int) num
, paddress (addr
));
3716 write_enn (own_buf
);
3720 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3721 tpoint
->traceframe_usage
);
3724 /* State variables to help return all the tracepoint bits. */
3725 static struct tracepoint
*cur_tpoint
;
3726 static unsigned int cur_action
;
3727 static unsigned int cur_step_action
;
3728 static struct source_string
*cur_source_string
;
3729 static struct trace_state_variable
*cur_tsv
;
3731 /* Compose a response that is an imitation of the syntax by which the
3732 tracepoint was originally downloaded. */
3735 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3739 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3740 paddress (tpoint
->address
),
3741 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3742 tpoint
->pass_count
);
3743 if (tpoint
->type
== fast_tracepoint
)
3744 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3745 else if (tpoint
->type
== static_tracepoint
)
3746 sprintf (packet
+ strlen (packet
), ":S");
3750 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3751 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3752 tpoint
->cond
->length
, buf
);
3757 /* Compose a response that is an imitation of the syntax by which the
3758 tracepoint action was originally downloaded (with the difference
3759 that due to the way we store the actions, this will output a packet
3760 per action, while GDB could have combined more than one action
3764 response_action (char *packet
, struct tracepoint
*tpoint
,
3765 char *taction
, int step
)
3767 sprintf (packet
, "%c%x:%s:%s",
3768 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3772 /* Compose a response that is an imitation of the syntax by which the
3773 tracepoint source piece was originally downloaded. */
3776 response_source (char *packet
,
3777 struct tracepoint
*tpoint
, struct source_string
*src
)
3782 len
= strlen (src
->str
);
3783 buf
= (char *) alloca (len
* 2 + 1);
3784 bin2hex ((gdb_byte
*) src
->str
, buf
, len
);
3786 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3787 tpoint
->number
, paddress (tpoint
->address
),
3788 src
->type
, 0, len
, buf
);
3791 /* Return the first piece of tracepoint definition, and initialize the
3792 state machine that will iterate through all the tracepoint
3796 cmd_qtfp (char *packet
)
3798 trace_debug ("Returning first tracepoint definition piece");
3800 cur_tpoint
= tracepoints
;
3801 cur_action
= cur_step_action
= 0;
3802 cur_source_string
= NULL
;
3805 response_tracepoint (packet
, cur_tpoint
);
3807 strcpy (packet
, "l");
3810 /* Return additional pieces of tracepoint definition. Each action and
3811 stepping action must go into its own packet, because of packet size
3812 limits, and so we use state variables to deliver one piece at a
3816 cmd_qtsp (char *packet
)
3818 trace_debug ("Returning subsequent tracepoint definition piece");
3822 /* This case would normally never occur, but be prepared for
3824 strcpy (packet
, "l");
3826 else if (cur_action
< cur_tpoint
->numactions
)
3828 response_action (packet
, cur_tpoint
,
3829 cur_tpoint
->actions_str
[cur_action
], 0);
3832 else if (cur_step_action
< cur_tpoint
->num_step_actions
)
3834 response_action (packet
, cur_tpoint
,
3835 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3838 else if ((cur_source_string
3839 ? cur_source_string
->next
3840 : cur_tpoint
->source_strings
))
3842 if (cur_source_string
)
3843 cur_source_string
= cur_source_string
->next
;
3845 cur_source_string
= cur_tpoint
->source_strings
;
3846 response_source (packet
, cur_tpoint
, cur_source_string
);
3850 cur_tpoint
= cur_tpoint
->next
;
3851 cur_action
= cur_step_action
= 0;
3852 cur_source_string
= NULL
;
3854 response_tracepoint (packet
, cur_tpoint
);
3856 strcpy (packet
, "l");
3860 /* Compose a response that is an imitation of the syntax by which the
3861 trace state variable was originally downloaded. */
3864 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3866 char *buf
= (char *) "";
3871 namelen
= strlen (tsv
->name
);
3872 buf
= (char *) alloca (namelen
* 2 + 1);
3873 bin2hex ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3876 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3877 tsv
->getter
? 1 : 0, buf
);
3880 /* Return the first trace state variable definition, and initialize
3881 the state machine that will iterate through all the tsv bits. */
3884 cmd_qtfv (char *packet
)
3886 trace_debug ("Returning first trace state variable definition");
3888 cur_tsv
= trace_state_variables
;
3891 response_tsv (packet
, cur_tsv
);
3893 strcpy (packet
, "l");
3896 /* Return additional trace state variable definitions. */
3899 cmd_qtsv (char *packet
)
3901 trace_debug ("Returning additional trace state variable definition");
3905 cur_tsv
= cur_tsv
->next
;
3907 response_tsv (packet
, cur_tsv
);
3909 strcpy (packet
, "l");
3912 strcpy (packet
, "l");
3915 /* Return the first static tracepoint marker, and initialize the state
3916 machine that will iterate through all the static tracepoints
3920 cmd_qtfstm (char *packet
)
3922 if (!maybe_write_ipa_ust_not_loaded (packet
))
3923 run_inferior_command (packet
, strlen (packet
) + 1);
3926 /* Return additional static tracepoints markers. */
3929 cmd_qtsstm (char *packet
)
3931 if (!maybe_write_ipa_ust_not_loaded (packet
))
3932 run_inferior_command (packet
, strlen (packet
) + 1);
3935 /* Return the definition of the static tracepoint at a given address.
3936 Result packet is the same as qTsST's. */
3939 cmd_qtstmat (char *packet
)
3941 if (!maybe_write_ipa_ust_not_loaded (packet
))
3942 run_inferior_command (packet
, strlen (packet
) + 1);
3945 /* Helper for gdb_agent_about_to_close.
3946 Return non-zero if thread ENTRY is in the same process in DATA. */
3949 same_process_p (struct inferior_list_entry
*entry
, void *data
)
3951 int *pid
= (int *) data
;
3953 return ptid_get_pid (entry
->id
) == *pid
;
3956 /* Sent the agent a command to close it. */
3959 gdb_agent_about_to_close (int pid
)
3961 char buf
[IPA_CMD_BUF_SIZE
];
3963 if (!maybe_write_ipa_not_loaded (buf
))
3965 struct thread_info
*saved_thread
;
3967 saved_thread
= current_thread
;
3969 /* Find any thread which belongs to process PID. */
3970 current_thread
= (struct thread_info
*)
3971 find_inferior (&all_threads
, same_process_p
, &pid
);
3973 strcpy (buf
, "close");
3975 run_inferior_command (buf
, strlen (buf
) + 1);
3977 current_thread
= saved_thread
;
3981 /* Return the minimum instruction size needed for fast tracepoints as a
3982 hexadecimal number. */
3985 cmd_qtminftpilen (char *packet
)
3987 if (current_thread
== NULL
)
3989 /* Indicate that the minimum length is currently unknown. */
3990 strcpy (packet
, "0");
3994 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3997 /* Respond to qTBuffer packet with a block of raw data from the trace
3998 buffer. GDB may ask for a lot, but we are allowed to reply with
3999 only as much as will fit within packet limits or whatever. */
4002 cmd_qtbuffer (char *own_buf
)
4004 ULONGEST offset
, num
, tot
;
4006 char *packet
= own_buf
;
4008 packet
+= strlen ("qTBuffer:");
4010 packet
= unpack_varlen_hex (packet
, &offset
);
4011 ++packet
; /* skip a comma */
4012 unpack_varlen_hex (packet
, &num
);
4014 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
4015 (int) num
, phex_nz (offset
, 0));
4017 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
4019 /* If we're right at the end, reply specially that we're done. */
4022 strcpy (own_buf
, "l");
4026 /* Object to any other out-of-bounds request. */
4029 write_enn (own_buf
);
4033 /* Compute the pointer corresponding to the given offset, accounting
4035 tbp
= trace_buffer_start
+ offset
;
4036 if (tbp
>= trace_buffer_wrap
)
4037 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
4039 /* Trim to the remaining bytes if we're close to the end. */
4040 if (num
> tot
- offset
)
4043 /* Trim to available packet size. */
4044 if (num
>= (PBUFSIZ
- 16) / 2 )
4045 num
= (PBUFSIZ
- 16) / 2;
4047 bin2hex (tbp
, own_buf
, num
);
4051 cmd_bigqtbuffer_circular (char *own_buf
)
4054 char *packet
= own_buf
;
4056 packet
+= strlen ("QTBuffer:circular:");
4058 unpack_varlen_hex (packet
, &val
);
4059 circular_trace_buffer
= val
;
4060 trace_debug ("Trace buffer is now %s",
4061 circular_trace_buffer
? "circular" : "linear");
4066 cmd_bigqtbuffer_size (char *own_buf
)
4070 char *packet
= own_buf
;
4072 /* Can't change the size during a tracing run. */
4075 write_enn (own_buf
);
4079 packet
+= strlen ("QTBuffer:size:");
4081 /* -1 is sent as literal "-1". */
4082 if (strcmp (packet
, "-1") == 0)
4083 sval
= DEFAULT_TRACE_BUFFER_SIZE
;
4086 unpack_varlen_hex (packet
, &val
);
4087 sval
= (LONGEST
) val
;
4090 init_trace_buffer (sval
);
4091 trace_debug ("Trace buffer is now %s bytes",
4092 plongest (trace_buffer_size
));
4097 cmd_qtnotes (char *own_buf
)
4100 char *saved
, *user
, *notes
, *stopnote
;
4101 char *packet
= own_buf
;
4103 packet
+= strlen ("QTNotes:");
4107 if (startswith (packet
, "user:"))
4109 packet
+= strlen ("user:");
4111 packet
= strchr (packet
, ';');
4112 nbytes
= (packet
- saved
) / 2;
4113 user
= (char *) xmalloc (nbytes
+ 1);
4114 nbytes
= hex2bin (saved
, (gdb_byte
*) user
, nbytes
);
4115 user
[nbytes
] = '\0';
4116 ++packet
; /* skip the semicolon */
4117 trace_debug ("User is '%s'", user
);
4118 xfree (tracing_user_name
);
4119 tracing_user_name
= user
;
4121 else if (startswith (packet
, "notes:"))
4123 packet
+= strlen ("notes:");
4125 packet
= strchr (packet
, ';');
4126 nbytes
= (packet
- saved
) / 2;
4127 notes
= (char *) xmalloc (nbytes
+ 1);
4128 nbytes
= hex2bin (saved
, (gdb_byte
*) notes
, nbytes
);
4129 notes
[nbytes
] = '\0';
4130 ++packet
; /* skip the semicolon */
4131 trace_debug ("Notes is '%s'", notes
);
4132 xfree (tracing_notes
);
4133 tracing_notes
= notes
;
4135 else if (startswith (packet
, "tstop:"))
4137 packet
+= strlen ("tstop:");
4139 packet
= strchr (packet
, ';');
4140 nbytes
= (packet
- saved
) / 2;
4141 stopnote
= (char *) xmalloc (nbytes
+ 1);
4142 nbytes
= hex2bin (saved
, (gdb_byte
*) stopnote
, nbytes
);
4143 stopnote
[nbytes
] = '\0';
4144 ++packet
; /* skip the semicolon */
4145 trace_debug ("tstop note is '%s'", stopnote
);
4146 xfree (tracing_stop_note
);
4147 tracing_stop_note
= stopnote
;
4157 handle_tracepoint_general_set (char *packet
)
4159 if (strcmp ("QTinit", packet
) == 0)
4161 cmd_qtinit (packet
);
4164 else if (startswith (packet
, "QTDP:"))
4169 else if (startswith (packet
, "QTDPsrc:"))
4171 cmd_qtdpsrc (packet
);
4174 else if (startswith (packet
, "QTEnable:"))
4176 cmd_qtenable_disable (packet
, 1);
4179 else if (startswith (packet
, "QTDisable:"))
4181 cmd_qtenable_disable (packet
, 0);
4184 else if (startswith (packet
, "QTDV:"))
4189 else if (startswith (packet
, "QTro:"))
4194 else if (strcmp ("QTStart", packet
) == 0)
4196 cmd_qtstart (packet
);
4199 else if (strcmp ("QTStop", packet
) == 0)
4201 cmd_qtstop (packet
);
4204 else if (startswith (packet
, "QTDisconnected:"))
4206 cmd_qtdisconnected (packet
);
4209 else if (startswith (packet
, "QTFrame:"))
4211 cmd_qtframe (packet
);
4214 else if (startswith (packet
, "QTBuffer:circular:"))
4216 cmd_bigqtbuffer_circular (packet
);
4219 else if (startswith (packet
, "QTBuffer:size:"))
4221 cmd_bigqtbuffer_size (packet
);
4224 else if (startswith (packet
, "QTNotes:"))
4226 cmd_qtnotes (packet
);
4234 handle_tracepoint_query (char *packet
)
4236 if (strcmp ("qTStatus", packet
) == 0)
4238 cmd_qtstatus (packet
);
4241 else if (startswith (packet
, "qTP:"))
4246 else if (strcmp ("qTfP", packet
) == 0)
4251 else if (strcmp ("qTsP", packet
) == 0)
4256 else if (strcmp ("qTfV", packet
) == 0)
4261 else if (strcmp ("qTsV", packet
) == 0)
4266 else if (startswith (packet
, "qTV:"))
4271 else if (startswith (packet
, "qTBuffer:"))
4273 cmd_qtbuffer (packet
);
4276 else if (strcmp ("qTfSTM", packet
) == 0)
4278 cmd_qtfstm (packet
);
4281 else if (strcmp ("qTsSTM", packet
) == 0)
4283 cmd_qtsstm (packet
);
4286 else if (startswith (packet
, "qTSTMat:"))
4288 cmd_qtstmat (packet
);
4291 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4293 cmd_qtminftpilen (packet
);
4301 #ifndef IN_PROCESS_AGENT
4303 /* Call this when thread TINFO has hit the tracepoint defined by
4304 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4305 action. This adds a while-stepping collecting state item to the
4306 threads' collecting state list, so that we can keep track of
4307 multiple simultaneous while-stepping actions being collected by the
4308 same thread. This can happen in cases like:
4310 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4312 ff0003 INSN3 <-- TP2, collect $regs
4313 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4316 Notice that when instruction INSN5 is reached, the while-stepping
4317 actions of both TP1 and TP3 are still being collected, and that TP2
4318 had been collected meanwhile. The whole range of ff0001-ff0005
4319 should be single-stepped, due to at least TP1's while-stepping
4320 action covering the whole range. */
4323 add_while_stepping_state (struct thread_info
*tinfo
,
4324 int tp_number
, CORE_ADDR tp_address
)
4326 struct wstep_state
*wstep
= XNEW (struct wstep_state
);
4328 wstep
->next
= tinfo
->while_stepping
;
4330 wstep
->tp_number
= tp_number
;
4331 wstep
->tp_address
= tp_address
;
4332 wstep
->current_step
= 0;
4334 tinfo
->while_stepping
= wstep
;
4337 /* Release the while-stepping collecting state WSTEP. */
4340 release_while_stepping_state (struct wstep_state
*wstep
)
4345 /* Release all while-stepping collecting states currently associated
4346 with thread TINFO. */
4349 release_while_stepping_state_list (struct thread_info
*tinfo
)
4351 struct wstep_state
*head
;
4353 while (tinfo
->while_stepping
)
4355 head
= tinfo
->while_stepping
;
4356 tinfo
->while_stepping
= head
->next
;
4357 release_while_stepping_state (head
);
4361 /* If TINFO was handling a 'while-stepping' action, the step has
4362 finished, so collect any step data needed, and check if any more
4363 steps are required. Return true if the thread was indeed
4364 collecting tracepoint data, false otherwise. */
4367 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4369 struct tracepoint
*tpoint
;
4370 struct wstep_state
*wstep
;
4371 struct wstep_state
**wstep_link
;
4372 struct trap_tracepoint_ctx ctx
;
4374 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4376 if (agent_loaded_p ())
4377 upload_fast_traceframes ();
4379 /* Check if we were indeed collecting data for one of more
4380 tracepoints with a 'while-stepping' count. */
4381 if (tinfo
->while_stepping
== NULL
)
4386 /* We're not even tracing anymore. Stop this thread from
4388 release_while_stepping_state_list (tinfo
);
4390 /* The thread had stopped due to a single-step request indeed
4391 explained by a tracepoint. */
4395 wstep
= tinfo
->while_stepping
;
4396 wstep_link
= &tinfo
->while_stepping
;
4398 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4399 target_pid_to_str (tinfo
->entry
.id
),
4400 wstep
->tp_number
, paddress (wstep
->tp_address
));
4402 ctx
.base
.type
= trap_tracepoint
;
4403 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4405 while (wstep
!= NULL
)
4407 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4410 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4411 wstep
->tp_number
, paddress (wstep
->tp_address
),
4412 target_pid_to_str (tinfo
->entry
.id
));
4415 *wstep_link
= wstep
->next
;
4416 release_while_stepping_state (wstep
);
4417 wstep
= *wstep_link
;
4421 /* We've just finished one step. */
4422 ++wstep
->current_step
;
4425 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4426 stop_pc
, tpoint
, wstep
->current_step
);
4428 if (wstep
->current_step
>= tpoint
->step_count
)
4430 /* The requested numbers of steps have occurred. */
4431 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4432 target_pid_to_str (tinfo
->entry
.id
),
4433 wstep
->tp_number
, paddress (wstep
->tp_address
));
4435 /* Unlink the wstep. */
4436 *wstep_link
= wstep
->next
;
4437 release_while_stepping_state (wstep
);
4438 wstep
= *wstep_link
;
4440 /* Only check the hit count now, which ensure that we do all
4441 our stepping before stopping the run. */
4442 if (tpoint
->pass_count
> 0
4443 && tpoint
->hit_count
>= tpoint
->pass_count
4444 && stopping_tracepoint
== NULL
)
4445 stopping_tracepoint
= tpoint
;
4449 /* Keep single-stepping until the requested numbers of steps
4451 wstep_link
= &wstep
->next
;
4452 wstep
= *wstep_link
;
4455 if (stopping_tracepoint
4456 || trace_buffer_is_full
4457 || expr_eval_result
!= expr_eval_no_error
)
4467 /* Handle any internal tracing control breakpoint hits. That means,
4468 pull traceframes from the IPA to our buffer, and syncing both
4469 tracing agents when the IPA's tracing stops for some reason. */
4472 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4474 /* Pull in fast tracepoint trace frames from the inferior in-process
4475 agent's buffer into our buffer. */
4477 if (!agent_loaded_p ())
4480 upload_fast_traceframes ();
4482 /* Check if the in-process agent had decided we should stop
4484 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4486 int ipa_trace_buffer_is_full
;
4487 CORE_ADDR ipa_stopping_tracepoint
;
4488 int ipa_expr_eval_result
;
4489 CORE_ADDR ipa_error_tracepoint
;
4491 trace_debug ("lib stopped at stop_tracing");
4493 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4494 &ipa_trace_buffer_is_full
);
4496 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4497 &ipa_stopping_tracepoint
);
4498 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4500 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4501 &ipa_error_tracepoint
);
4502 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4504 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4505 &ipa_expr_eval_result
);
4506 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4508 trace_debug ("lib: trace_buffer_is_full: %d, "
4509 "stopping_tracepoint: %s, "
4510 "ipa_expr_eval_result: %d, "
4511 "error_tracepoint: %s, ",
4512 ipa_trace_buffer_is_full
,
4513 paddress (ipa_stopping_tracepoint
),
4514 ipa_expr_eval_result
,
4515 paddress (ipa_error_tracepoint
));
4519 if (ipa_trace_buffer_is_full
)
4520 trace_debug ("lib stopped due to full buffer.");
4521 if (ipa_stopping_tracepoint
)
4522 trace_debug ("lib stopped due to tpoint");
4523 if (ipa_stopping_tracepoint
)
4524 trace_debug ("lib stopped due to error");
4527 if (ipa_stopping_tracepoint
!= 0)
4530 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4532 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4534 expr_eval_result
= ipa_expr_eval_result
;
4536 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4541 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4543 trace_debug ("lib stopped at flush_trace_buffer");
4550 /* Return true if TINFO just hit a tracepoint. Collect data if
4554 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4556 struct tracepoint
*tpoint
;
4558 struct trap_tracepoint_ctx ctx
;
4560 /* Not tracing, don't handle. */
4564 ctx
.base
.type
= trap_tracepoint
;
4565 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4567 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4569 /* Note that we collect fast tracepoints here as well. We'll
4570 step over the fast tracepoint jump later, which avoids the
4571 double collect. However, we don't collect for static
4572 tracepoints here, because UST markers are compiled in program,
4573 and probes will be executed in program. So static tracepoints
4574 are collected there. */
4575 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4576 && tpoint
->type
!= static_tracepoint
)
4578 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4579 target_pid_to_str (tinfo
->entry
.id
),
4580 tpoint
->number
, paddress (tpoint
->address
));
4582 /* Test the condition if present, and collect if true. */
4584 || (condition_true_at_tracepoint
4585 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4586 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4589 if (stopping_tracepoint
4590 || trace_buffer_is_full
4591 || expr_eval_result
!= expr_eval_no_error
)
4595 /* If the tracepoint had a 'while-stepping' action, then set
4596 the thread to collect this tracepoint on the following
4598 else if (tpoint
->step_count
> 0)
4600 add_while_stepping_state (tinfo
,
4601 tpoint
->number
, tpoint
->address
);
4613 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4614 struct ust_marker_data
;
4615 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4616 struct traceframe
*tframe
);
4619 /* Create a trace frame for the hit of the given tracepoint in the
4623 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4624 struct tracepoint
*tpoint
)
4626 struct traceframe
*tframe
;
4629 /* Only count it as a hit when we actually collect data. */
4630 tpoint
->hit_count
++;
4632 /* If we've exceeded a defined pass count, record the event for
4633 later, and finish the collection for this hit. This test is only
4634 for nonstepping tracepoints, stepping tracepoints test at the end
4635 of their while-stepping loop. */
4636 if (tpoint
->pass_count
> 0
4637 && tpoint
->hit_count
>= tpoint
->pass_count
4638 && tpoint
->step_count
== 0
4639 && stopping_tracepoint
== NULL
)
4640 stopping_tracepoint
= tpoint
;
4642 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4643 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4645 tframe
= add_traceframe (tpoint
);
4649 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4651 #ifndef IN_PROCESS_AGENT
4652 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4653 tpoint
->number
, paddress (tpoint
->address
),
4654 tpoint
->actions_str
[acti
]);
4657 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4658 tpoint
->actions
[acti
]);
4661 finish_traceframe (tframe
);
4664 if (tframe
== NULL
&& tracing
)
4665 trace_buffer_is_full
= 1;
4668 #ifndef IN_PROCESS_AGENT
4671 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4673 struct tracepoint
*tpoint
, int current_step
)
4675 struct traceframe
*tframe
;
4678 trace_debug ("Making new step traceframe for "
4679 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4680 tpoint
->number
, paddress (tpoint
->address
),
4681 current_step
, tpoint
->step_count
,
4684 tframe
= add_traceframe (tpoint
);
4688 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4690 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4691 tpoint
->number
, paddress (tpoint
->address
),
4692 tpoint
->step_actions_str
[acti
]);
4694 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4695 tpoint
->step_actions
[acti
]);
4698 finish_traceframe (tframe
);
4701 if (tframe
== NULL
&& tracing
)
4702 trace_buffer_is_full
= 1;
4707 #ifdef IN_PROCESS_AGENT
4708 /* The target description index for IPA. Passed from gdbserver, used
4709 to select ipa_tdesc. */
4711 IP_AGENT_EXPORT_VAR
int ipa_tdesc_idx
;
4715 static struct regcache
*
4716 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4718 struct regcache
*regcache
= NULL
;
4719 #ifdef IN_PROCESS_AGENT
4720 const struct target_desc
*ipa_tdesc
= get_ipa_tdesc (ipa_tdesc_idx
);
4722 if (ctx
->type
== fast_tracepoint
)
4724 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4725 if (!fctx
->regcache_initted
)
4727 fctx
->regcache_initted
= 1;
4728 init_register_cache (&fctx
->regcache
, ipa_tdesc
, fctx
->regspace
);
4729 supply_regblock (&fctx
->regcache
, NULL
);
4730 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4732 regcache
= &fctx
->regcache
;
4735 if (ctx
->type
== static_tracepoint
)
4737 struct static_tracepoint_ctx
*sctx
4738 = (struct static_tracepoint_ctx
*) ctx
;
4740 if (!sctx
->regcache_initted
)
4742 sctx
->regcache_initted
= 1;
4743 init_register_cache (&sctx
->regcache
, ipa_tdesc
, sctx
->regspace
);
4744 supply_regblock (&sctx
->regcache
, NULL
);
4745 /* Pass down the tracepoint address, because REGS doesn't
4746 include the PC, but we know what it must have been. */
4747 supply_static_tracepoint_registers (&sctx
->regcache
,
4748 (const unsigned char *)
4750 sctx
->tpoint
->address
);
4752 regcache
= &sctx
->regcache
;
4756 if (ctx
->type
== trap_tracepoint
)
4758 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4759 regcache
= tctx
->regcache
;
4763 gdb_assert (regcache
!= NULL
);
4769 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4771 struct tracepoint
*tpoint
,
4772 struct traceframe
*tframe
,
4773 struct tracepoint_action
*taction
)
4775 enum eval_result_type err
;
4777 switch (taction
->type
)
4781 struct collect_memory_action
*maction
;
4782 struct eval_agent_expr_context ax_ctx
;
4784 maction
= (struct collect_memory_action
*) taction
;
4785 ax_ctx
.regcache
= NULL
;
4786 ax_ctx
.tframe
= tframe
;
4787 ax_ctx
.tpoint
= tpoint
;
4789 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4790 pulongest (maction
->len
),
4791 paddress (maction
->addr
), maction
->basereg
);
4792 /* (should use basereg) */
4793 agent_mem_read (&ax_ctx
, NULL
, (CORE_ADDR
) maction
->addr
,
4799 unsigned char *regspace
;
4800 struct regcache tregcache
;
4801 struct regcache
*context_regcache
;
4804 trace_debug ("Want to collect registers");
4806 context_regcache
= get_context_regcache (ctx
);
4807 regcache_size
= register_cache_size (context_regcache
->tdesc
);
4809 /* Collect all registers for now. */
4810 regspace
= add_traceframe_block (tframe
, tpoint
, 1 + regcache_size
);
4811 if (regspace
== NULL
)
4813 trace_debug ("Trace buffer block allocation failed, skipping");
4816 /* Identify a register block. */
4819 /* Wrap the regblock in a register cache (in the stack, we
4820 don't want to malloc here). */
4821 init_register_cache (&tregcache
, context_regcache
->tdesc
,
4824 /* Copy the register data to the regblock. */
4825 regcache_cpy (&tregcache
, context_regcache
);
4827 #ifndef IN_PROCESS_AGENT
4828 /* On some platforms, trap-based tracepoints will have the PC
4829 pointing to the next instruction after the trap, but we
4830 don't want the user or GDB trying to guess whether the
4831 saved PC needs adjusting; so always record the adjusted
4832 stop_pc. Note that we can't use tpoint->address instead,
4833 since it will be wrong for while-stepping actions. This
4834 adjustment is a nop for fast tracepoints collected from the
4835 in-process lib (but not if GDBserver is collecting one
4836 preemptively), since the PC had already been adjusted to
4837 contain the tracepoint's address by the jump pad. */
4838 trace_debug ("Storing stop pc (0x%s) in regblock",
4839 paddress (stop_pc
));
4841 /* This changes the regblock, not the thread's
4843 regcache_write_pc (&tregcache
, stop_pc
);
4849 struct eval_expr_action
*eaction
;
4850 struct eval_agent_expr_context ax_ctx
;
4852 eaction
= (struct eval_expr_action
*) taction
;
4853 ax_ctx
.regcache
= get_context_regcache (ctx
);
4854 ax_ctx
.tframe
= tframe
;
4855 ax_ctx
.tpoint
= tpoint
;
4857 trace_debug ("Want to evaluate expression");
4859 err
= gdb_eval_agent_expr (&ax_ctx
, eaction
->expr
, NULL
);
4861 if (err
!= expr_eval_no_error
)
4863 record_tracepoint_error (tpoint
, "action expression", err
);
4870 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4871 trace_debug ("Want to collect static trace data");
4872 collect_ust_data_at_tracepoint (ctx
, tframe
);
4874 trace_debug ("warning: collecting static trace data, "
4875 "but static tracepoints are not supported");
4880 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4886 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4887 struct tracepoint
*tpoint
)
4890 enum eval_result_type err
;
4892 /* Presently, gdbserver doesn't run compiled conditions, only the
4893 IPA does. If the program stops at a fast tracepoint's address
4894 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4895 gdbserver preemptively collect the fast tracepoint. Later, on
4896 resume, gdbserver steps over the fast tracepoint like it steps
4897 over breakpoints, so that the IPA doesn't see that fast
4898 tracepoint. This avoids double collects of fast tracepoints in
4899 that stopping scenario. Having gdbserver itself handle the fast
4900 tracepoint gives the user a consistent view of when fast or trap
4901 tracepoints are collected, compared to an alternative where only
4902 trap tracepoints are collected on stop, and fast tracepoints on
4903 resume. When a fast tracepoint is being processed by gdbserver,
4904 it is always the non-compiled condition expression that is
4906 #ifdef IN_PROCESS_AGENT
4907 if (tpoint
->compiled_cond
)
4909 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4910 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (fctx
->regs
, &value
);
4915 struct eval_agent_expr_context ax_ctx
;
4917 ax_ctx
.regcache
= get_context_regcache (ctx
);
4918 ax_ctx
.tframe
= NULL
;
4919 ax_ctx
.tpoint
= tpoint
;
4921 err
= gdb_eval_agent_expr (&ax_ctx
, tpoint
->cond
, &value
);
4923 if (err
!= expr_eval_no_error
)
4925 record_tracepoint_error (tpoint
, "condition", err
);
4926 /* The error case must return false. */
4930 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4931 tpoint
->number
, paddress (tpoint
->address
),
4933 return (value
? 1 : 0);
4936 /* Do memory copies for bytecodes. */
4937 /* Do the recording of memory blocks for actions and bytecodes. */
4940 agent_mem_read (struct eval_agent_expr_context
*ctx
,
4941 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4943 unsigned char *mspace
;
4944 ULONGEST remaining
= len
;
4945 unsigned short blocklen
;
4947 /* If a 'to' buffer is specified, use it. */
4950 read_inferior_memory (from
, to
, len
);
4954 /* Otherwise, create a new memory block in the trace buffer. */
4955 while (remaining
> 0)
4959 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4960 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4961 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4964 /* Identify block as a memory block. */
4967 /* Record address and size. */
4968 memcpy (mspace
, &from
, sizeof (from
));
4969 mspace
+= sizeof (from
);
4970 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4971 mspace
+= sizeof (blocklen
);
4972 /* Record the memory block proper. */
4973 read_inferior_memory (from
, mspace
, blocklen
);
4974 trace_debug ("%d bytes recorded", blocklen
);
4975 remaining
-= blocklen
;
4982 agent_mem_read_string (struct eval_agent_expr_context
*ctx
,
4983 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4985 unsigned char *buf
, *mspace
;
4986 ULONGEST remaining
= len
;
4987 unsigned short blocklen
, i
;
4989 /* To save a bit of space, block lengths are 16-bit, so break large
4990 requests into multiple blocks. Bordering on overkill for strings,
4991 but it could happen that someone specifies a large max length. */
4992 while (remaining
> 0)
4996 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4997 /* We want working space to accumulate nonzero bytes, since
4998 traceframes must have a predecided size (otherwise it gets
4999 harder to wrap correctly for the circular case, etc). */
5000 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
5001 for (i
= 0; i
< blocklen
; ++i
)
5003 /* Read the string one byte at a time, in case the string is
5004 at the end of a valid memory area - we don't want a
5005 correctly-terminated string to engender segvio
5007 read_inferior_memory (from
+ i
, buf
+ i
, 1);
5012 /* Make sure outer loop stops now too. */
5013 remaining
= blocklen
;
5017 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
5018 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
5024 /* Identify block as a memory block. */
5027 /* Record address and size. */
5028 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
5029 mspace
+= sizeof (from
);
5030 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
5031 mspace
+= sizeof (blocklen
);
5032 /* Copy the string contents. */
5033 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
5034 remaining
-= blocklen
;
5041 /* Record the value of a trace state variable. */
5044 agent_tsv_read (struct eval_agent_expr_context
*ctx
, int n
)
5046 unsigned char *vspace
;
5049 vspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
,
5050 1 + sizeof (n
) + sizeof (LONGEST
));
5053 /* Identify block as a variable. */
5055 /* Record variable's number and value. */
5056 memcpy (vspace
+ 1, &n
, sizeof (n
));
5057 val
= get_trace_state_variable_value (n
);
5058 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
5059 trace_debug ("Variable %d recorded", n
);
5063 #ifndef IN_PROCESS_AGENT
5065 /* Callback for traceframe_walk_blocks, used to find a given block
5066 type in a traceframe. */
5069 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
5071 char *wantedp
= (char *) data
;
5073 if (*wantedp
== blocktype
)
5079 /* Walk over all traceframe blocks of the traceframe buffer starting
5080 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5081 block found, passing in DATA unmodified. If CALLBACK returns true,
5082 this returns a pointer to where the block is found. Returns NULL
5083 if no callback call returned true, indicating that all blocks have
5086 static unsigned char *
5087 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
5089 int (*callback
) (char blocktype
,
5090 unsigned char *dataptr
,
5094 unsigned char *dataptr
;
5098 trace_debug ("traceframe %d has no data", tfnum
);
5102 /* Iterate through a traceframe's blocks, looking for a block of the
5104 for (dataptr
= database
;
5105 dataptr
< database
+ datasize
;
5109 unsigned short mlen
;
5111 if (dataptr
== trace_buffer_wrap
)
5113 /* Adjust to reflect wrapping part of the frame around to
5115 datasize
= dataptr
- database
;
5116 dataptr
= database
= trace_buffer_lo
;
5119 blocktype
= *dataptr
++;
5121 if ((*callback
) (blocktype
, dataptr
, data
))
5127 /* Skip over the registers block. */
5128 dataptr
+= current_target_desc ()->registers_size
;
5131 /* Skip over the memory block. */
5132 dataptr
+= sizeof (CORE_ADDR
);
5133 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5134 dataptr
+= (sizeof (mlen
) + mlen
);
5137 /* Skip over the TSV block. */
5138 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5141 /* Skip over the static trace data block. */
5142 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5143 dataptr
+= (sizeof (mlen
) + mlen
);
5146 trace_debug ("traceframe %d has unknown block type 0x%x",
5155 /* Look for the block of type TYPE_WANTED in the trameframe starting
5156 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5159 static unsigned char *
5160 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5161 int tfnum
, char type_wanted
)
5163 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5164 match_blocktype
, &type_wanted
);
5167 static unsigned char *
5168 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5170 unsigned char *regblock
;
5172 regblock
= traceframe_find_block_type (tframe
->data
,
5176 if (regblock
== NULL
)
5177 trace_debug ("traceframe %d has no register data", tfnum
);
5182 /* Get registers from a traceframe. */
5185 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5187 unsigned char *dataptr
;
5188 struct tracepoint
*tpoint
;
5189 struct traceframe
*tframe
;
5191 tframe
= find_traceframe (tfnum
);
5195 trace_debug ("traceframe %d not found", tfnum
);
5199 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5200 if (dataptr
== NULL
)
5202 /* Mark registers unavailable. */
5203 supply_regblock (regcache
, NULL
);
5205 /* We can generally guess at a PC, although this will be
5206 misleading for while-stepping frames and multi-location
5208 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5210 regcache_write_pc (regcache
, tpoint
->address
);
5213 supply_regblock (regcache
, dataptr
);
5219 traceframe_get_pc (struct traceframe
*tframe
)
5221 struct regcache regcache
;
5222 unsigned char *dataptr
;
5223 const struct target_desc
*tdesc
= current_target_desc ();
5225 dataptr
= traceframe_find_regblock (tframe
, -1);
5226 if (dataptr
== NULL
)
5229 init_register_cache (®cache
, tdesc
, dataptr
);
5230 return regcache_read_pc (®cache
);
5233 /* Read a requested block of memory from a trace frame. */
5236 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5237 unsigned char *buf
, ULONGEST length
,
5240 struct traceframe
*tframe
;
5241 unsigned char *database
, *dataptr
;
5242 unsigned int datasize
;
5244 unsigned short mlen
;
5246 trace_debug ("traceframe_read_mem");
5248 tframe
= find_traceframe (tfnum
);
5252 trace_debug ("traceframe %d not found", tfnum
);
5256 datasize
= tframe
->data_size
;
5257 database
= dataptr
= &tframe
->data
[0];
5259 /* Iterate through a traceframe's blocks, looking for memory. */
5260 while ((dataptr
= traceframe_find_block_type (dataptr
,
5262 - (dataptr
- database
),
5263 tfnum
, 'M')) != NULL
)
5265 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5266 dataptr
+= sizeof (maddr
);
5267 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5268 dataptr
+= sizeof (mlen
);
5269 trace_debug ("traceframe %d has %d bytes at %s",
5270 tfnum
, mlen
, paddress (maddr
));
5272 /* If the block includes the first part of the desired range,
5273 return as much it has; GDB will re-request the remainder,
5274 which might be in a different block of this trace frame. */
5275 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5277 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5281 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5286 /* Skip over this block. */
5290 trace_debug ("traceframe %d has no memory data for the desired region",
5298 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5301 struct traceframe
*tframe
;
5302 unsigned char *database
, *dataptr
;
5303 unsigned int datasize
;
5307 trace_debug ("traceframe_read_tsv");
5309 tfnum
= current_traceframe
;
5313 trace_debug ("no current traceframe");
5317 tframe
= find_traceframe (tfnum
);
5321 trace_debug ("traceframe %d not found", tfnum
);
5325 datasize
= tframe
->data_size
;
5326 database
= dataptr
= &tframe
->data
[0];
5328 /* Iterate through a traceframe's blocks, looking for the last
5330 while ((dataptr
= traceframe_find_block_type (dataptr
,
5332 - (dataptr
- database
),
5333 tfnum
, 'V')) != NULL
)
5335 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5336 dataptr
+= sizeof (vnum
);
5338 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5340 /* Check that this is the variable we want. */
5343 memcpy (val
, dataptr
, sizeof (*val
));
5347 /* Skip over this block. */
5348 dataptr
+= sizeof (LONGEST
);
5352 trace_debug ("traceframe %d has no data for variable %d",
5357 /* Read a requested block of static tracepoint data from a trace
5361 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5362 unsigned char *buf
, ULONGEST length
,
5365 struct traceframe
*tframe
;
5366 unsigned char *database
, *dataptr
;
5367 unsigned int datasize
;
5368 unsigned short mlen
;
5370 trace_debug ("traceframe_read_sdata");
5372 tframe
= find_traceframe (tfnum
);
5376 trace_debug ("traceframe %d not found", tfnum
);
5380 datasize
= tframe
->data_size
;
5381 database
= &tframe
->data
[0];
5383 /* Iterate through a traceframe's blocks, looking for static
5385 dataptr
= traceframe_find_block_type (database
, datasize
,
5387 if (dataptr
!= NULL
)
5389 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5390 dataptr
+= sizeof (mlen
);
5393 if (offset
+ length
> mlen
)
5394 length
= mlen
- offset
;
5396 memcpy (buf
, dataptr
, length
);
5404 trace_debug ("traceframe %d has no static trace data", tfnum
);
5410 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5411 object. DATA is pointer to a struct buffer holding the
5412 traceframe-info object being built. */
5415 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5417 struct buffer
*buffer
= (struct buffer
*) data
;
5423 unsigned short mlen
;
5426 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5427 dataptr
+= sizeof (maddr
);
5428 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5429 dataptr
+= sizeof (mlen
);
5430 buffer_xml_printf (buffer
,
5431 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5432 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5439 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5440 buffer_xml_printf (buffer
, "<tvar id=\"%d\"/>\n", vnum
);
5449 warning ("Unhandled trace block type (%d) '%c ' "
5450 "while building trace frame info.",
5451 blocktype
, blocktype
);
5458 /* Build a traceframe-info object for traceframe number TFNUM into
5462 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5464 struct traceframe
*tframe
;
5466 trace_debug ("traceframe_read_info");
5468 tframe
= find_traceframe (tfnum
);
5472 trace_debug ("traceframe %d not found", tfnum
);
5476 buffer_grow_str (buffer
, "<traceframe-info>\n");
5477 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5478 tfnum
, build_traceframe_info_xml
, buffer
);
5479 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5483 /* Return the first fast tracepoint whose jump pad contains PC. */
5485 static struct tracepoint
*
5486 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5488 struct tracepoint
*tpoint
;
5490 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5491 if (tpoint
->type
== fast_tracepoint
)
5492 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5498 /* Return the first fast tracepoint whose trampoline contains PC. */
5500 static struct tracepoint
*
5501 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5503 struct tracepoint
*tpoint
;
5505 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5507 if (tpoint
->type
== fast_tracepoint
5508 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5515 /* Return GDBserver's tracepoint that matches the IP Agent's
5516 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5519 static struct tracepoint
*
5520 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5522 struct tracepoint
*tpoint
;
5524 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5525 if (tpoint
->type
== fast_tracepoint
)
5526 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5534 /* The type of the object that is used to synchronize fast tracepoint
5537 typedef struct collecting_t
5539 /* The fast tracepoint number currently collecting. */
5542 /* A number that GDBserver can use to identify the thread that is
5543 presently holding the collect lock. This need not (and usually
5544 is not) the thread id, as getting the current thread ID usually
5545 requires a system call, which we want to avoid like the plague.
5546 Usually this is thread's TCB, found in the TLS (pseudo-)
5547 register, which is readable with a single insn on several
5549 uintptr_t thread_area
;
5552 #ifndef IN_PROCESS_AGENT
5555 force_unlock_trace_buffer (void)
5557 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5560 /* Check if the thread identified by THREAD_AREA which is stopped at
5561 STOP_PC, is presently locking the fast tracepoint collection, and
5562 if so, gather some status of said collection. Returns 0 if the
5563 thread isn't collecting or in the jump pad at all. 1, if in the
5564 jump pad (or within gdb_collect) and hasn't executed the adjusted
5565 original insn yet (can set a breakpoint there and run to it). 2,
5566 if presently executing the adjusted original insn --- in which
5567 case, if we want to move the thread out of the jump pad, we need to
5568 single-step it until this function returns 0. */
5571 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5573 struct fast_tpoint_collect_status
*status
)
5575 CORE_ADDR ipa_collecting
;
5576 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5577 CORE_ADDR ipa_gdb_trampoline_buffer
;
5578 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5579 struct tracepoint
*tpoint
;
5580 int needs_breakpoint
;
5582 /* The thread THREAD_AREA is either:
5584 0. not collecting at all, not within the jump pad, or within
5585 gdb_collect or one of its callees.
5587 1. in the jump pad and haven't reached gdb_collect
5589 2. within gdb_collect (out of the jump pad) (collect is set)
5591 3. we're in the jump pad, after gdb_collect having returned,
5592 possibly executing the adjusted insns.
5594 For cases 1 and 3, `collecting' may or not be set. The jump pad
5595 doesn't have any complicated jump logic, so we can tell if the
5596 thread is executing the adjust original insn or not by just
5597 matching STOP_PC with known jump pad addresses. If we it isn't
5598 yet executing the original insn, set a breakpoint there, and let
5599 the thread run to it, so to quickly step over a possible (many
5600 insns) gdb_collect call. Otherwise, or when the breakpoint is
5601 hit, only a few (small number of) insns are left to be executed
5602 in the jump pad. Single-step the thread until it leaves the
5607 needs_breakpoint
= 0;
5608 trace_debug ("fast_tracepoint_collecting");
5610 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5611 &ipa_gdb_jump_pad_buffer
))
5613 internal_error (__FILE__
, __LINE__
,
5614 "error extracting `gdb_jump_pad_buffer'");
5616 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5617 &ipa_gdb_jump_pad_buffer_end
))
5619 internal_error (__FILE__
, __LINE__
,
5620 "error extracting `gdb_jump_pad_buffer_end'");
5623 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5624 &ipa_gdb_trampoline_buffer
))
5626 internal_error (__FILE__
, __LINE__
,
5627 "error extracting `gdb_trampoline_buffer'");
5629 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5630 &ipa_gdb_trampoline_buffer_end
))
5632 internal_error (__FILE__
, __LINE__
,
5633 "error extracting `gdb_trampoline_buffer_end'");
5636 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5637 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5639 /* We can tell which tracepoint(s) the thread is collecting by
5640 matching the jump pad address back to the tracepoint. */
5641 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5644 warning ("in jump pad, but no matching tpoint?");
5649 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5651 tpoint
->number
, paddress (tpoint
->address
),
5652 paddress (tpoint
->jump_pad
),
5653 paddress (tpoint
->jump_pad_end
),
5654 paddress (tpoint
->adjusted_insn_addr
),
5655 paddress (tpoint
->adjusted_insn_addr_end
));
5658 /* Definitely in the jump pad. May or may not need
5659 fast-exit-jump-pad breakpoint. */
5660 if (tpoint
->jump_pad
<= stop_pc
5661 && stop_pc
< tpoint
->adjusted_insn_addr
)
5662 needs_breakpoint
= 1;
5664 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5665 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5667 /* We can tell which tracepoint(s) the thread is collecting by
5668 matching the trampoline address back to the tracepoint. */
5669 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5672 warning ("in trampoline, but no matching tpoint?");
5677 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5678 tpoint
->number
, paddress (tpoint
->address
),
5679 paddress (tpoint
->trampoline
),
5680 paddress (tpoint
->trampoline_end
));
5683 /* Have not reached jump pad yet, but treat the trampoline as a
5684 part of the jump pad that is before the adjusted original
5686 needs_breakpoint
= 1;
5690 collecting_t ipa_collecting_obj
;
5692 /* If `collecting' is set/locked, then the THREAD_AREA thread
5693 may or not be the one holding the lock. We have to read the
5694 lock to find out. */
5696 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5699 trace_debug ("fast_tracepoint_collecting:"
5700 " failed reading 'collecting' in the inferior");
5704 if (!ipa_collecting
)
5706 trace_debug ("fast_tracepoint_collecting: not collecting"
5707 " (and nobody is).");
5711 /* Some thread is collecting. Check which. */
5712 if (read_inferior_memory (ipa_collecting
,
5713 (unsigned char *) &ipa_collecting_obj
,
5714 sizeof (ipa_collecting_obj
)) != 0)
5717 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5719 trace_debug ("fast_tracepoint_collecting: not collecting "
5720 "(another thread is)");
5725 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5728 warning ("fast_tracepoint_collecting: collecting, "
5729 "but tpoint %s not found?",
5730 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5734 /* The thread is within `gdb_collect', skip over the rest of
5735 fast tracepoint collection quickly using a breakpoint. */
5736 needs_breakpoint
= 1;
5739 /* The caller wants a bit of status detail. */
5742 status
->tpoint_num
= tpoint
->number
;
5743 status
->tpoint_addr
= tpoint
->address
;
5744 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5745 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5748 if (needs_breakpoint
)
5750 /* Hasn't executed the original instruction yet. Set breakpoint
5751 there, and wait till it's hit, then single-step until exiting
5755 fast_tracepoint_collecting, returning continue-until-break at %s",
5756 paddress (tpoint
->adjusted_insn_addr
));
5758 return 1; /* continue */
5762 /* Just single-step until exiting the jump pad. */
5764 trace_debug ("fast_tracepoint_collecting, returning "
5765 "need-single-step (%s-%s)",
5766 paddress (tpoint
->adjusted_insn_addr
),
5767 paddress (tpoint
->adjusted_insn_addr_end
));
5769 return 2; /* single-step */
5775 #ifdef IN_PROCESS_AGENT
5777 /* The global fast tracepoint collect lock. Points to a collecting_t
5778 object built on the stack by the jump pad, if presently locked;
5779 NULL if it isn't locked. Note that this lock *must* be set while
5780 executing any *function other than the jump pad. See
5781 fast_tracepoint_collecting. */
5783 IP_AGENT_EXPORT_VAR collecting_t
*collecting
;
5786 /* This routine, called from the jump pad (in asm) is designed to be
5787 called from the jump pads of fast tracepoints, thus it is on the
5790 IP_AGENT_EXPORT_FUNC
void
5791 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5793 struct fast_tracepoint_ctx ctx
;
5794 const struct target_desc
*ipa_tdesc
;
5796 /* Don't do anything until the trace run is completely set up. */
5800 ipa_tdesc
= get_ipa_tdesc (ipa_tdesc_idx
);
5801 ctx
.base
.type
= fast_tracepoint
;
5803 ctx
.regcache_initted
= 0;
5804 /* Wrap the regblock in a register cache (in the stack, we don't
5805 want to malloc here). */
5806 ctx
.regspace
= (unsigned char *) alloca (ipa_tdesc
->registers_size
);
5807 if (ctx
.regspace
== NULL
)
5809 trace_debug ("Trace buffer block allocation failed, skipping");
5813 for (ctx
.tpoint
= tpoint
;
5814 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5815 ctx
.tpoint
= ctx
.tpoint
->next
)
5817 if (!ctx
.tpoint
->enabled
)
5820 /* Multiple tracepoints of different types, such as fast tracepoint and
5821 static tracepoint, can be set at the same address. */
5822 if (ctx
.tpoint
->type
!= tpoint
->type
)
5825 /* Test the condition if present, and collect if true. */
5826 if (ctx
.tpoint
->cond
== NULL
5827 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5830 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5831 ctx
.tpoint
->address
, ctx
.tpoint
);
5833 /* Note that this will cause original insns to be written back
5834 to where we jumped from, but that's OK because we're jumping
5835 back to the next whole instruction. This will go badly if
5836 instruction restoration is not atomic though. */
5837 if (stopping_tracepoint
5838 || trace_buffer_is_full
5839 || expr_eval_result
!= expr_eval_no_error
)
5847 /* If there was a condition and it evaluated to false, the only
5848 way we would stop tracing is if there was an error during
5849 condition expression evaluation. */
5850 if (expr_eval_result
!= expr_eval_no_error
)
5861 #ifndef IN_PROCESS_AGENT
5864 get_raw_reg_func_addr (void)
5866 return ipa_sym_addrs
.addr_get_raw_reg
;
5870 get_get_tsv_func_addr (void)
5872 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5876 get_set_tsv_func_addr (void)
5878 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5882 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5883 CORE_ADDR
*jump_entry
)
5885 CORE_ADDR entry_point
= *jump_entry
;
5886 enum eval_result_type err
;
5888 trace_debug ("Starting condition compilation for tracepoint %d\n",
5891 /* Initialize the global pointer to the code being built. */
5892 current_insn_ptr
= *jump_entry
;
5896 err
= compile_bytecodes (tpoint
->cond
);
5898 if (err
== expr_eval_no_error
)
5902 /* Record the beginning of the compiled code. */
5903 tpoint
->compiled_cond
= entry_point
;
5905 trace_debug ("Condition compilation for tracepoint %d complete\n",
5910 /* Leave the unfinished code in situ, but don't point to it. */
5912 tpoint
->compiled_cond
= 0;
5914 trace_debug ("Condition compilation for tracepoint %d failed, "
5916 tpoint
->number
, err
);
5919 /* Update the code pointer passed in. Note that we do this even if
5920 the compile fails, so that we can look at the partial results
5921 instead of letting them be overwritten. */
5922 *jump_entry
= current_insn_ptr
;
5924 /* Leave a gap, to aid dump decipherment. */
5928 /* The base pointer of the IPA's heap. This is the only memory the
5929 IPA is allowed to use. The IPA should _not_ call the inferior's
5930 `malloc' during operation. That'd be slow, and, most importantly,
5931 it may not be safe. We may be collecting a tracepoint in a signal
5932 handler, for example. */
5933 static CORE_ADDR target_tp_heap
;
5935 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5939 target_malloc (ULONGEST size
)
5943 if (target_tp_heap
== 0)
5945 /* We have the pointer *address*, need what it points to. */
5946 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5949 internal_error (__FILE__
, __LINE__
,
5950 "couldn't get target heap head pointer");
5954 ptr
= target_tp_heap
;
5955 target_tp_heap
+= size
;
5957 /* Pad to 8-byte alignment. */
5958 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5964 download_agent_expr (struct agent_expr
*expr
)
5966 CORE_ADDR expr_addr
;
5967 CORE_ADDR expr_bytes
;
5969 expr_addr
= target_malloc (sizeof (*expr
));
5970 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5972 expr_bytes
= target_malloc (expr
->length
);
5973 write_inferior_data_pointer (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5975 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5980 /* Align V up to N bits. */
5981 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5983 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5986 download_tracepoint_1 (struct tracepoint
*tpoint
)
5988 struct tracepoint target_tracepoint
;
5989 CORE_ADDR tpptr
= 0;
5991 gdb_assert (tpoint
->type
== fast_tracepoint
5992 || tpoint
->type
== static_tracepoint
);
5994 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5996 CORE_ADDR jentry
, jump_entry
;
5998 jentry
= jump_entry
= get_jump_space_head ();
6000 if (tpoint
->cond
!= NULL
)
6002 /* Pad to 8-byte alignment. (needed?) */
6003 /* Actually this should be left for the target to
6005 jentry
= UALIGN (jentry
, 8);
6007 compile_tracepoint_condition (tpoint
, &jentry
);
6010 /* Pad to 8-byte alignment. */
6011 jentry
= UALIGN (jentry
, 8);
6012 claim_jump_space (jentry
- jump_entry
);
6015 target_tracepoint
= *tpoint
;
6017 tpptr
= target_malloc (sizeof (*tpoint
));
6018 tpoint
->obj_addr_on_target
= tpptr
;
6020 /* Write the whole object. We'll fix up its pointers in a bit.
6021 Assume no next for now. This is fixed up above on the next
6022 iteration, if there's any. */
6023 target_tracepoint
.next
= NULL
;
6024 /* Need to clear this here too, since we're downloading the
6025 tracepoints before clearing our own copy. */
6026 target_tracepoint
.hit_count
= 0;
6028 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
6029 sizeof (target_tracepoint
));
6032 write_inferior_data_pointer (tpptr
6033 + offsetof (struct tracepoint
, cond
),
6034 download_agent_expr (tpoint
->cond
));
6036 if (tpoint
->numactions
)
6039 CORE_ADDR actions_array
;
6041 /* The pointers array. */
6043 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6044 write_inferior_data_pointer (tpptr
+ offsetof (struct tracepoint
,
6048 /* Now for each pointer, download the action. */
6049 for (i
= 0; i
< tpoint
->numactions
; i
++)
6051 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6052 CORE_ADDR ipa_action
= tracepoint_action_download (action
);
6054 if (ipa_action
!= 0)
6055 write_inferior_data_pointer (actions_array
6056 + i
* sizeof (*tpoint
->actions
),
6062 #define IPA_PROTO_FAST_TRACE_FLAG 0
6063 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
6064 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
6065 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
6066 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
6068 /* Send a command to agent to download and install tracepoint TPOINT. */
6071 tracepoint_send_agent (struct tracepoint
*tpoint
)
6073 char buf
[IPA_CMD_BUF_SIZE
];
6078 strcpy (p
, "FastTrace:");
6081 COPY_FIELD_TO_BUF (p
, tpoint
, number
);
6082 COPY_FIELD_TO_BUF (p
, tpoint
, address
);
6083 COPY_FIELD_TO_BUF (p
, tpoint
, type
);
6084 COPY_FIELD_TO_BUF (p
, tpoint
, enabled
);
6085 COPY_FIELD_TO_BUF (p
, tpoint
, step_count
);
6086 COPY_FIELD_TO_BUF (p
, tpoint
, pass_count
);
6087 COPY_FIELD_TO_BUF (p
, tpoint
, numactions
);
6088 COPY_FIELD_TO_BUF (p
, tpoint
, hit_count
);
6089 COPY_FIELD_TO_BUF (p
, tpoint
, traceframe_usage
);
6090 COPY_FIELD_TO_BUF (p
, tpoint
, compiled_cond
);
6091 COPY_FIELD_TO_BUF (p
, tpoint
, orig_size
);
6094 p
= agent_expr_send (p
, tpoint
->cond
);
6096 /* tracepoint_action */
6097 for (i
= 0; i
< tpoint
->numactions
; i
++)
6099 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6101 p
[0] = action
->type
;
6102 p
= tracepoint_action_send (&p
[1], action
);
6105 get_jump_space_head ();
6106 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6107 agent can use jump pad from it. */
6108 if (tpoint
->type
== fast_tracepoint
)
6110 memcpy (p
, &gdb_jump_pad_head
, 8);
6114 ret
= run_inferior_command (buf
, (int) (ptrdiff_t) (p
- buf
));
6118 if (!startswith (buf
, "OK"))
6121 /* The value of tracepoint's target address is stored in BUF. */
6122 memcpy (&tpoint
->obj_addr_on_target
,
6123 &buf
[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET
], 8);
6125 if (tpoint
->type
== fast_tracepoint
)
6128 = (unsigned char *) &buf
[IPA_PROTO_FAST_TRACE_FJUMP_INSN
];
6131 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6132 (unsigned int) tpoint
->obj_addr_on_target
,
6133 (unsigned int) gdb_jump_pad_head
);
6135 memcpy (&gdb_jump_pad_head
, &buf
[IPA_PROTO_FAST_TRACE_JUMP_PAD
], 8);
6137 /* This has been done in agent. We should also set up record for it. */
6138 memcpy (&fjump_size
, &buf
[IPA_PROTO_FAST_TRACE_FJUMP_SIZE
], 4);
6141 = set_fast_tracepoint_jump (tpoint
->address
, insn
, fjump_size
);
6148 download_tracepoint (struct tracepoint
*tpoint
)
6150 struct tracepoint
*tp
, *tp_prev
;
6152 if (tpoint
->type
!= fast_tracepoint
6153 && tpoint
->type
!= static_tracepoint
)
6156 download_tracepoint_1 (tpoint
);
6158 /* Find the previous entry of TPOINT, which is fast tracepoint or
6159 static tracepoint. */
6161 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6163 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6169 CORE_ADDR tp_prev_target_next_addr
;
6171 /* Insert TPOINT after TP_PREV in IPA. */
6172 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6173 + offsetof (struct tracepoint
, next
),
6174 &tp_prev_target_next_addr
))
6176 internal_error (__FILE__
, __LINE__
,
6177 "error reading `tp_prev->next'");
6180 /* tpoint->next = tp_prev->next */
6181 write_inferior_data_pointer (tpoint
->obj_addr_on_target
6182 + offsetof (struct tracepoint
, next
),
6183 tp_prev_target_next_addr
);
6184 /* tp_prev->next = tpoint */
6185 write_inferior_data_pointer (tp_prev
->obj_addr_on_target
6186 + offsetof (struct tracepoint
, next
),
6187 tpoint
->obj_addr_on_target
);
6190 /* First object in list, set the head pointer in the
6192 write_inferior_data_pointer (ipa_sym_addrs
.addr_tracepoints
,
6193 tpoint
->obj_addr_on_target
);
6198 download_trace_state_variables (void)
6200 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6201 struct trace_state_variable
*tsv
;
6203 /* Start out empty. */
6204 write_inferior_data_pointer (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6206 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6208 struct trace_state_variable target_tsv
;
6210 /* TSV's with a getter have been initialized equally in both the
6211 inferior and GDBserver. Skip them. */
6212 if (tsv
->getter
!= NULL
)
6218 ptr
= target_malloc (sizeof (*tsv
));
6220 if (tsv
== trace_state_variables
)
6222 /* First object in list, set the head pointer in the
6225 write_inferior_data_pointer (ipa_sym_addrs
.addr_trace_state_variables
,
6230 write_inferior_data_pointer (prev_ptr
6231 + offsetof (struct trace_state_variable
,
6236 /* Write the whole object. We'll fix up its pointers in a bit.
6237 Assume no next, fixup when needed. */
6238 target_tsv
.next
= NULL
;
6240 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6241 sizeof (target_tsv
));
6243 if (tsv
->name
!= NULL
)
6245 size_t size
= strlen (tsv
->name
) + 1;
6246 CORE_ADDR name_addr
= target_malloc (size
);
6247 write_inferior_memory (name_addr
,
6248 (unsigned char *) tsv
->name
, size
);
6249 write_inferior_data_pointer (ptr
6250 + offsetof (struct trace_state_variable
,
6255 gdb_assert (tsv
->getter
== NULL
);
6260 /* Fixup the next pointer in the last item in the list. */
6261 write_inferior_data_pointer (prev_ptr
6262 + offsetof (struct trace_state_variable
,
6267 /* Upload complete trace frames out of the IP Agent's trace buffer
6268 into GDBserver's trace buffer. This always uploads either all or
6269 no trace frames. This is the counter part of
6270 `trace_alloc_trace_buffer'. See its description of the atomic
6271 synching mechanism. */
6274 upload_fast_traceframes (void)
6276 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6277 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6279 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6280 unsigned int curr_tbctrl_idx
;
6281 unsigned int ipa_trace_buffer_ctrl_curr
;
6282 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6283 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6284 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6285 CORE_ADDR ipa_trace_buffer_lo
;
6286 CORE_ADDR ipa_trace_buffer_hi
;
6288 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6289 &ipa_traceframe_read_count_racy
))
6291 /* This will happen in most targets if the current thread is
6296 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6297 &ipa_traceframe_write_count_racy
))
6300 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6301 ipa_traceframe_write_count_racy
6302 - ipa_traceframe_read_count_racy
,
6303 ipa_traceframe_write_count_racy
,
6304 ipa_traceframe_read_count_racy
);
6306 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6309 about_to_request_buffer_space_bkpt
6310 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6313 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6314 &ipa_trace_buffer_ctrl_curr
))
6317 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6319 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6322 unsigned int prev
, counter
;
6324 /* Update the token, with new counters, and the GDBserver stamp
6325 bit. Alway reuse the current TBC index. */
6326 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6327 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6329 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6335 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6336 ipa_trace_buffer_ctrl_curr
))
6339 trace_debug ("Lib: Committed %08x -> %08x",
6340 ipa_trace_buffer_ctrl_curr_old
,
6341 ipa_trace_buffer_ctrl_curr
);
6343 /* Re-read these, now that we've installed the
6344 `about_to_request_buffer_space' breakpoint/lock. A thread could
6345 have finished a traceframe between the last read of these
6346 counters and setting the breakpoint above. If we start
6347 uploading, we never want to leave this function with
6348 traceframe_read_count != 0, otherwise, GDBserver could end up
6349 incrementing the counter tokens more than once (due to event loop
6350 nesting), which would break the IP agent's "effective" detection
6351 (see trace_alloc_trace_buffer). */
6352 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6353 &ipa_traceframe_read_count
))
6355 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6356 &ipa_traceframe_write_count
))
6361 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6362 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6363 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6365 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6366 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6367 trace_debug ("note that ipa_traceframe_count's parts changed");
6370 /* Get the address of the current TBC object (the IP agent has an
6371 array of 3 such objects). The index is stored in the TBC
6373 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6374 ipa_trace_buffer_ctrl_addr
6375 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6377 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6378 (unsigned char *) &ipa_trace_buffer_ctrl
,
6379 sizeof (struct ipa_trace_buffer_control
)))
6382 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6383 &ipa_trace_buffer_lo
))
6385 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6386 &ipa_trace_buffer_hi
))
6389 /* Offsets are easier to grok for debugging than raw addresses,
6390 especially for the small trace buffer sizes that are useful for
6392 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6393 "endfree=%d wrap=%d hi=%d",
6395 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6396 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6397 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6398 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6399 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6401 /* Note that the IPA's buffer is always circular. */
6403 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6405 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6406 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6408 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6409 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6410 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6411 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6414 tf
= IPA_FIRST_TRACEFRAME ();
6416 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6418 struct tracepoint
*tpoint
;
6419 struct traceframe
*tframe
;
6420 unsigned char *block
;
6421 struct traceframe ipa_tframe
;
6423 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6424 offsetof (struct traceframe
, data
)))
6425 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6427 if (ipa_tframe
.tpnum
== 0)
6429 internal_error (__FILE__
, __LINE__
,
6430 "Uploading: No (more) fast traceframes, but"
6431 " ipa_traceframe_count == %u??\n",
6432 ipa_traceframe_write_count
6433 - ipa_traceframe_read_count
);
6436 /* Note that this will be incorrect for multi-location
6438 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6440 tframe
= add_traceframe (tpoint
);
6443 trace_buffer_is_full
= 1;
6444 trace_debug ("Uploading: trace buffer is full");
6448 /* Copy the whole set of blocks in one go for now. FIXME:
6449 split this in smaller blocks. */
6450 block
= add_traceframe_block (tframe
, tpoint
,
6451 ipa_tframe
.data_size
);
6454 if (read_inferior_memory (tf
6455 + offsetof (struct traceframe
, data
),
6456 block
, ipa_tframe
.data_size
))
6457 error ("Uploading: Couldn't read traceframe data at %s\n",
6458 paddress (tf
+ offsetof (struct traceframe
, data
)));
6461 trace_debug ("Uploading: traceframe didn't fit");
6462 finish_traceframe (tframe
);
6465 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6467 /* If we freed the traceframe that wrapped around, go back
6468 to the non-wrap case. */
6469 if (tf
< ipa_trace_buffer_ctrl
.start
)
6471 trace_debug ("Lib: Discarding past the wraparound");
6472 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6474 ipa_trace_buffer_ctrl
.start
= tf
;
6475 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6476 ++ipa_traceframe_read_count
;
6478 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6479 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6481 trace_debug ("Lib: buffer is fully empty. "
6482 "Trace buffer [%d] start=%d free=%d endfree=%d",
6484 (int) (ipa_trace_buffer_ctrl
.start
6485 - ipa_trace_buffer_lo
),
6486 (int) (ipa_trace_buffer_ctrl
.free
6487 - ipa_trace_buffer_lo
),
6488 (int) (ipa_trace_buffer_ctrl
.end_free
6489 - ipa_trace_buffer_lo
));
6491 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6492 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6493 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6494 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6497 trace_debug ("Uploaded a traceframe\n"
6498 "Lib: Trace buffer [%d] start=%d free=%d "
6499 "endfree=%d wrap=%d hi=%d",
6501 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6502 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6503 (int) (ipa_trace_buffer_ctrl
.end_free
6504 - ipa_trace_buffer_lo
),
6505 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6506 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6509 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6510 (unsigned char *) &ipa_trace_buffer_ctrl
,
6511 sizeof (struct ipa_trace_buffer_control
)))
6514 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6515 ipa_traceframe_read_count
);
6517 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6521 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6522 about_to_request_buffer_space_bkpt
= NULL
;
6526 if (trace_buffer_is_full
)
6531 #ifdef IN_PROCESS_AGENT
6533 IP_AGENT_EXPORT_VAR
int ust_loaded
;
6534 IP_AGENT_EXPORT_VAR
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6538 /* Static tracepoints. */
6540 /* UST puts a "struct tracepoint" in the global namespace, which
6541 conflicts with our tracepoint. Arguably, being a library, it
6542 shouldn't take ownership of such a generic name. We work around it
6544 #define tracepoint ust_tracepoint
6545 #include <ust/ust.h>
6548 extern int serialize_to_text (char *outbuf
, int bufsize
,
6549 const char *fmt
, va_list ap
);
6551 #define GDB_PROBE_NAME "gdb"
6553 /* We dynamically search for the UST symbols instead of linking them
6554 in. This lets the user decide if the application uses static
6555 tracepoints, instead of always pulling libust.so in. This vector
6556 holds pointers to all functions we care about. */
6560 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6561 const char *fmt
, va_list ap
);
6563 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6564 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6566 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6568 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6571 void (*marker_iter_start
) (struct marker_iter
*iter
);
6572 void (*marker_iter_next
) (struct marker_iter
*iter
);
6573 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6574 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6579 /* Cast through typeof to catch incompatible API changes. Since UST
6580 only builds with gcc, we can freely use gcc extensions here
6582 #define GET_UST_SYM(SYM) \
6585 if (ust_ops.SYM == NULL) \
6586 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6587 if (ust_ops.SYM == NULL) \
6591 #define USTF(SYM) ust_ops.SYM
6593 /* Get pointers to all libust.so functions we care about. */
6598 GET_UST_SYM (serialize_to_text
);
6600 GET_UST_SYM (ltt_probe_register
);
6601 GET_UST_SYM (ltt_probe_unregister
);
6602 GET_UST_SYM (ltt_marker_connect
);
6603 GET_UST_SYM (ltt_marker_disconnect
);
6605 GET_UST_SYM (marker_iter_start
);
6606 GET_UST_SYM (marker_iter_next
);
6607 GET_UST_SYM (marker_iter_stop
);
6608 GET_UST_SYM (marker_iter_reset
);
6614 /* Given an UST marker, return the matching gdb static tracepoint.
6615 The match is done by address. */
6617 static struct tracepoint
*
6618 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6620 struct tracepoint
*tpoint
;
6622 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6624 if (tpoint
->type
!= static_tracepoint
)
6627 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6634 /* The probe function we install on lttng/ust markers. Whenever a
6635 probed ust marker is hit, this function is called. This is similar
6636 to gdb_collect, only for static tracepoints, instead of fast
6640 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6641 struct registers
*regs
, void *call_private
,
6642 const char *fmt
, va_list *args
)
6644 struct tracepoint
*tpoint
;
6645 struct static_tracepoint_ctx ctx
;
6646 const struct target_desc
*ipa_tdesc
;
6648 /* Don't do anything until the trace run is completely set up. */
6651 trace_debug ("gdb_probe: not tracing\n");
6655 ipa_tdesc
= get_ipa_tdesc (ipa_tdesc_idx
);
6656 ctx
.base
.type
= static_tracepoint
;
6657 ctx
.regcache_initted
= 0;
6662 /* Wrap the regblock in a register cache (in the stack, we don't
6663 want to malloc here). */
6664 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
6665 if (ctx
.regspace
== NULL
)
6667 trace_debug ("Trace buffer block allocation failed, skipping");
6671 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6674 trace_debug ("gdb_probe: marker not known: "
6675 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6676 mdata
->location
, mdata
->channel
,
6677 mdata
->name
, mdata
->format
);
6681 if (!tpoint
->enabled
)
6683 trace_debug ("gdb_probe: tracepoint disabled");
6687 ctx
.tpoint
= tpoint
;
6689 trace_debug ("gdb_probe: collecting marker: "
6690 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6691 mdata
->location
, mdata
->channel
,
6692 mdata
->name
, mdata
->format
);
6694 /* Test the condition if present, and collect if true. */
6695 if (tpoint
->cond
== NULL
6696 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6699 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6700 tpoint
->address
, tpoint
);
6702 if (stopping_tracepoint
6703 || trace_buffer_is_full
6704 || expr_eval_result
!= expr_eval_no_error
)
6709 /* If there was a condition and it evaluated to false, the only
6710 way we would stop tracing is if there was an error during
6711 condition expression evaluation. */
6712 if (expr_eval_result
!= expr_eval_no_error
)
6717 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6718 static tracepoint string data. This is a string passed to the
6719 tracing library by the user, at the time of the tracepoint marker
6720 call. E.g., in the UST marker call:
6722 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6724 the collected data is "str FOOBAZ".
6728 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6729 struct traceframe
*tframe
)
6731 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6732 unsigned char *bufspace
;
6735 unsigned short blocklen
;
6739 trace_debug ("Wanted to collect static trace data, "
6740 "but there's no static trace data");
6744 va_copy (copy
, *umd
->args
);
6745 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6748 trace_debug ("Want to collect ust data");
6750 /* 'S' + size + string */
6751 bufspace
= add_traceframe_block (tframe
, umd
->tpoint
,
6752 1 + sizeof (blocklen
) + size
+ 1);
6753 if (bufspace
== NULL
)
6755 trace_debug ("Trace buffer block allocation failed, skipping");
6759 /* Identify a static trace data block. */
6762 blocklen
= size
+ 1;
6763 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6765 va_copy (copy
, *umd
->args
);
6766 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6767 size
+ 1, umd
->fmt
, copy
);
6770 trace_debug ("Storing static tracepoint data in regblock: %s",
6771 bufspace
+ 1 + sizeof (blocklen
));
6774 /* The probe to register with lttng/ust. */
6775 static struct ltt_available_probe gdb_ust_probe
=
6782 #endif /* HAVE_UST */
6783 #endif /* IN_PROCESS_AGENT */
6785 #ifndef IN_PROCESS_AGENT
6787 /* Ask the in-process agent to run a command. Since we don't want to
6788 have to handle the IPA hitting breakpoints while running the
6789 command, we pause all threads, remove all breakpoints, and then set
6790 the helper thread re-running. We communicate with the helper
6791 thread by means of direct memory xfering, and a socket for
6795 run_inferior_command (char *cmd
, int len
)
6798 int pid
= ptid_get_pid (current_ptid
);
6800 trace_debug ("run_inferior_command: running: %s", cmd
);
6803 uninsert_all_breakpoints ();
6805 err
= agent_run_command (pid
, (const char *) cmd
, len
);
6807 reinsert_all_breakpoints ();
6813 #else /* !IN_PROCESS_AGENT */
6815 #include <sys/socket.h>
6818 #ifndef UNIX_PATH_MAX
6819 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6822 /* Where we put the socked used for synchronization. */
6823 #define SOCK_DIR P_tmpdir
6825 /* Thread ID of the helper thread. GDBserver reads this to know which
6826 is the help thread. This is an LWP id on Linux. */
6828 IP_AGENT_EXPORT_VAR
int helper_thread_id
;
6832 init_named_socket (const char *name
)
6835 struct sockaddr_un addr
;
6837 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6840 warning ("socket creation failed: %s", strerror (errno
));
6844 addr
.sun_family
= AF_UNIX
;
6846 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6847 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6849 result
= access (name
, F_OK
);
6853 result
= unlink (name
);
6856 warning ("unlink failed: %s", strerror (errno
));
6860 warning ("socket %s already exists; overwriting", name
);
6863 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6866 warning ("bind failed: %s", strerror (errno
));
6871 result
= listen (fd
, 1);
6874 warning ("listen: %s", strerror (errno
));
6882 static char agent_socket_name
[UNIX_PATH_MAX
];
6885 gdb_agent_socket_init (void)
6889 result
= xsnprintf (agent_socket_name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6890 SOCK_DIR
, getpid ());
6891 if (result
>= UNIX_PATH_MAX
)
6893 trace_debug ("string overflow allocating socket name");
6897 fd
= init_named_socket (agent_socket_name
);
6899 warning ("Error initializing named socket (%s) for communication with the "
6900 "ust helper thread. Check that directory exists and that it "
6901 "is writable.", agent_socket_name
);
6908 /* The next marker to be returned on a qTsSTM command. */
6909 static const struct marker
*next_st
;
6911 /* Returns the first known marker. */
6916 struct marker_iter iter
;
6918 USTF(marker_iter_reset
) (&iter
);
6919 USTF(marker_iter_start
) (&iter
);
6924 /* Returns the marker following M. */
6926 const struct marker
*
6927 next_marker (const struct marker
*m
)
6929 struct marker_iter iter
;
6931 USTF(marker_iter_reset
) (&iter
);
6932 USTF(marker_iter_start
) (&iter
);
6934 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6936 if (iter
.marker
== m
)
6938 USTF(marker_iter_next
) (&iter
);
6946 /* Return an hexstr version of the STR C string, fit for sending to
6950 cstr_to_hexstr (const char *str
)
6952 int len
= strlen (str
);
6953 char *hexstr
= xmalloc (len
* 2 + 1);
6954 bin2hex ((gdb_byte
*) str
, hexstr
, len
);
6958 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6962 response_ust_marker (char *packet
, const struct marker
*st
)
6964 char *strid
, *format
, *tmp
;
6966 next_st
= next_marker (st
);
6968 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6969 strlen (st
->name
) + 1);
6970 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6972 strid
= cstr_to_hexstr (tmp
);
6975 format
= cstr_to_hexstr (st
->format
);
6977 sprintf (packet
, "m%s:%s:%s",
6978 paddress ((uintptr_t) st
->location
),
6986 /* Return the first static tracepoint, and initialize the state
6987 machine that will iterate through all the static tracepoints. */
6990 cmd_qtfstm (char *packet
)
6992 trace_debug ("Returning first trace state variable definition");
6994 if (first_marker ())
6995 response_ust_marker (packet
, first_marker ());
6997 strcpy (packet
, "l");
7000 /* Return additional trace state variable definitions. */
7003 cmd_qtsstm (char *packet
)
7005 trace_debug ("Returning static tracepoint");
7008 response_ust_marker (packet
, next_st
);
7010 strcpy (packet
, "l");
7013 /* Disconnect the GDB probe from a marker at a given address. */
7016 unprobe_marker_at (char *packet
)
7020 struct marker_iter iter
;
7022 p
+= sizeof ("unprobe_marker_at:") - 1;
7024 p
= unpack_varlen_hex (p
, &address
);
7026 USTF(marker_iter_reset
) (&iter
);
7027 USTF(marker_iter_start
) (&iter
);
7028 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7029 if ((uintptr_t ) iter
.marker
->location
== address
)
7033 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7037 warning ("could not disable marker %s/%s",
7038 iter
.marker
->channel
, iter
.marker
->name
);
7043 /* Connect the GDB probe to a marker at a given address. */
7046 probe_marker_at (char *packet
)
7050 struct marker_iter iter
;
7053 p
+= sizeof ("probe_marker_at:") - 1;
7055 p
= unpack_varlen_hex (p
, &address
);
7057 USTF(marker_iter_reset
) (&iter
);
7059 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7061 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7062 if ((uintptr_t ) m
->location
== address
)
7066 trace_debug ("found marker for address. "
7067 "ltt_marker_connect (marker = %s/%s)",
7068 m
->channel
, m
->name
);
7070 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7072 if (result
&& result
!= -EEXIST
)
7073 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7074 m
->channel
, m
->name
, -result
);
7078 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7079 m
->channel
, m
->name
);
7083 strcpy (packet
, "OK");
7087 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7092 cmd_qtstmat (char *packet
)
7096 struct marker_iter iter
;
7099 p
+= sizeof ("qTSTMat:") - 1;
7101 p
= unpack_varlen_hex (p
, &address
);
7103 USTF(marker_iter_reset
) (&iter
);
7105 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7107 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7108 if ((uintptr_t ) m
->location
== address
)
7110 response_ust_marker (packet
, m
);
7114 strcpy (packet
, "l");
7124 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7127 #endif /* HAVE_UST */
7129 #include <sys/syscall.h>
7132 gdb_agent_remove_socket (void)
7134 unlink (agent_socket_name
);
7137 /* Helper thread of agent. */
7140 gdb_agent_helper_thread (void *arg
)
7144 atexit (gdb_agent_remove_socket
);
7148 listen_fd
= gdb_agent_socket_init ();
7150 if (helper_thread_id
== 0)
7151 helper_thread_id
= syscall (SYS_gettid
);
7153 if (listen_fd
== -1)
7155 warning ("could not create sync socket\n");
7162 struct sockaddr_un sockaddr
;
7168 tmp
= sizeof (sockaddr
);
7172 fd
= accept (listen_fd
, (struct sockaddr
*) &sockaddr
, &tmp
);
7174 /* It seems an ERESTARTSYS can escape out of accept. */
7175 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7179 warning ("Accept returned %d, error: %s\n",
7180 fd
, strerror (errno
));
7186 ret
= read (fd
, buf
, 1);
7187 } while (ret
== -1 && errno
== EINTR
);
7191 warning ("reading socket (fd=%d) failed with %s",
7192 fd
, strerror (errno
));
7199 if (startswith (cmd_buf
, "close"))
7204 else if (strcmp ("qTfSTM", cmd_buf
) == 0)
7206 cmd_qtfstm (cmd_buf
);
7208 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7210 cmd_qtsstm (cmd_buf
);
7212 else if (startswith (cmd_buf
, "unprobe_marker_at:"))
7214 unprobe_marker_at (cmd_buf
);
7216 else if (startswith (cmd_buf
, "probe_marker_at:"))
7218 probe_marker_at (cmd_buf
);
7220 else if (startswith (cmd_buf
, "qTSTMat:"))
7222 cmd_qtstmat (cmd_buf
);
7224 #endif /* HAVE_UST */
7227 /* Fix compiler's warning: ignoring return value of 'write'. */
7228 ret
= write (fd
, buf
, 1);
7234 unlink (agent_socket_name
);
7236 /* Sleep endlessly to wait the whole inferior stops. This
7237 thread can not exit because GDB or GDBserver may still need
7238 'current_thread' (representing this thread) to access
7239 inferior memory. Otherwise, this thread exits earlier than
7240 other threads, and 'current_thread' is set to NULL. */
7251 #include <pthread.h>
7254 IP_AGENT_EXPORT_VAR
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
7258 gdb_agent_init (void)
7265 /* We want the helper thread to be as transparent as possible, so
7266 have it inherit an all-signals-blocked mask. */
7268 sigfillset (&new_mask
);
7269 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7271 perror_with_name ("pthread_sigmask (1)");
7273 res
= pthread_create (&thread
,
7275 gdb_agent_helper_thread
,
7278 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7280 perror_with_name ("pthread_sigmask (2)");
7282 while (helper_thread_id
== 0)
7290 #include <sys/mman.h>
7293 IP_AGENT_EXPORT_VAR
char *gdb_tp_heap_buffer
;
7294 IP_AGENT_EXPORT_VAR
char *gdb_jump_pad_buffer
;
7295 IP_AGENT_EXPORT_VAR
char *gdb_jump_pad_buffer_end
;
7296 IP_AGENT_EXPORT_VAR
char *gdb_trampoline_buffer
;
7297 IP_AGENT_EXPORT_VAR
char *gdb_trampoline_buffer_end
;
7298 IP_AGENT_EXPORT_VAR
char *gdb_trampoline_buffer_error
;
7300 /* Record the result of getting buffer space for fast tracepoint
7301 trampolines. Any error message is copied, since caller may not be
7302 using persistent storage. */
7305 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7307 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7308 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7310 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7312 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7315 static void __attribute__ ((constructor
))
7316 initialize_tracepoint_ftlib (void)
7318 initialize_tracepoint ();
7323 #endif /* IN_PROCESS_AGENT */
7325 /* Return a timestamp, expressed as microseconds of the usual Unix
7326 time. (As the result is a 64-bit number, it will not overflow any
7330 get_timestamp (void)
7334 if (gettimeofday (&tv
, 0) != 0)
7337 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7341 initialize_tracepoint (void)
7343 /* Start with the default size. */
7344 init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE
);
7346 /* Wire trace state variable 1 to be the timestamp. This will be
7347 uploaded to GDB upon connection and become one of its trace state
7348 variables. (In case you're wondering, if GDB already has a trace
7349 variable numbered 1, it will be renumbered.) */
7350 create_trace_state_variable (1, 0);
7351 set_trace_state_variable_name (1, "trace_timestamp");
7352 set_trace_state_variable_getter (1, get_timestamp
);
7354 #ifdef IN_PROCESS_AGENT
7359 pagesize
= sysconf (_SC_PAGE_SIZE
);
7361 perror_with_name ("sysconf");
7363 gdb_tp_heap_buffer
= (char *) xmalloc (5 * 1024 * 1024);
7365 #define SCRATCH_BUFFER_NPAGES 20
7367 /* Allocate scratch buffer aligned on a page boundary, at a low
7368 address (close to the main executable's code). */
7369 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7372 = (char *) mmap ((void *) addr
,
7373 pagesize
* SCRATCH_BUFFER_NPAGES
,
7374 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7375 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7377 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7382 perror_with_name ("mmap");
7384 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7387 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7389 /* It's not a fatal error for something to go wrong with trampoline
7390 buffer setup, but it can be mysterious, so create a channel to
7391 report back on what went wrong, using a fixed size since we may
7392 not be able to allocate space later when the problem occurs. */
7393 gdb_trampoline_buffer_error
= (char *) xmalloc (IPA_BUFSIZ
);
7395 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7397 initialize_low_tracepoint ();