gdb:
[deliverable/binutils-gdb.git] / gdb / gdbserver / tracepoint.c
CommitLineData
219f2f23 1/* Tracepoint code for remote server for GDB.
0b302171 2 Copyright (C) 2009-2012 Free Software Foundation, Inc.
219f2f23
PA
3
4 This file is part of GDB.
5
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.
10
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.
15
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/>. */
18
19#include "server.h"
2fa291ac
YQ
20#include "agent.h"
21
219f2f23
PA
22#include <ctype.h>
23#include <fcntl.h>
24#include <unistd.h>
25#include <sys/time.h>
fa593d66 26#include <stddef.h>
fa593d66
PA
27#if HAVE_STDINT_H
28#include <stdint.h>
29#endif
5e1dc496 30#include "ax.h"
219f2f23 31
61adf464 32/* This file is built for both GDBserver, and the in-process
fa593d66
PA
33 agent (IPA), a shared library that includes a tracing agent that is
34 loaded by the inferior to support fast tracepoints. Fast
35 tracepoints (or more accurately, jump based tracepoints) are
36 implemented by patching the tracepoint location with a jump into a
37 small trampoline function whose job is to save the register state,
38 call the in-process tracing agent, and then execute the original
39 instruction that was under the tracepoint jump (possibly adjusted,
40 if PC-relative, or some such).
41
42 The current synchronization design is pull based. That means,
43 GDBserver does most of the work, by peeking/poking at the inferior
44 agent's memory directly for downloading tracepoint and associated
45 objects, and for uploading trace frames. Whenever the IPA needs
46 something from GDBserver (trace buffer is full, tracing stopped for
47 some reason, etc.) the IPA calls a corresponding hook function
48 where GDBserver has placed a breakpoint.
49
50 Each of the agents has its own trace buffer. When browsing the
51 trace frames built from slow and fast tracepoints from GDB (tfind
52 mode), there's no guarantee the user is seeing the trace frames in
53 strict chronological creation order, although, GDBserver tries to
54 keep the order relatively reasonable, by syncing the trace buffers
55 at appropriate times.
56
57*/
58
59static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf, 1, 2);
219f2f23
PA
60
61static void
fa593d66 62trace_vdebug (const char *fmt, ...)
219f2f23
PA
63{
64 char buf[1024];
65 va_list ap;
66
67 va_start (ap, fmt);
68 vsprintf (buf, fmt, ap);
5e1dc496 69 fprintf (stderr, PROG "/tracepoint: %s\n", buf);
219f2f23
PA
70 va_end (ap);
71}
72
fa593d66 73#define trace_debug_1(level, fmt, args...) \
219f2f23 74 do { \
32d92999 75 if (level <= debug_threads) \
fa593d66 76 trace_vdebug ((fmt), ##args); \
219f2f23
PA
77 } while (0)
78
fa593d66
PA
79#define trace_debug(FMT, args...) \
80 trace_debug_1 (1, FMT, ##args)
81
82#if defined(__GNUC__)
83# define ATTR_USED __attribute__((used))
84# define ATTR_NOINLINE __attribute__((noinline))
85# define ATTR_CONSTRUCTOR __attribute__ ((constructor))
86#else
87# define ATTR_USED
88# define ATTR_NOINLINE
89# define ATTR_CONSTRUCTOR
90#endif
91
92/* Make sure the functions the IPA needs to export (symbols GDBserver
93 needs to query GDB about) are exported. */
94
95#ifdef IN_PROCESS_AGENT
96# if defined _WIN32 || defined __CYGWIN__
97# define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
98# else
99# if __GNUC__ >= 4
100# define IP_AGENT_EXPORT \
101 __attribute__ ((visibility("default"))) ATTR_USED
102# else
103# define IP_AGENT_EXPORT ATTR_USED
104# endif
105# endif
106#else
107# define IP_AGENT_EXPORT
108#endif
109
110/* Prefix exported symbols, for good citizenship. All the symbols
111 that need exporting are defined in this module. */
112#ifdef IN_PROCESS_AGENT
113# define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
114# define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
115# define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
405f8e94
SS
116# define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
117# define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
118# define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
fa593d66
PA
119# define collecting gdb_agent_collecting
120# define gdb_collect gdb_agent_gdb_collect
121# define stop_tracing gdb_agent_stop_tracing
122# define flush_trace_buffer gdb_agent_flush_trace_buffer
123# define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
124# define trace_buffer_is_full gdb_agent_trace_buffer_is_full
125# define stopping_tracepoint gdb_agent_stopping_tracepoint
126# define expr_eval_result gdb_agent_expr_eval_result
127# define error_tracepoint gdb_agent_error_tracepoint
128# define tracepoints gdb_agent_tracepoints
129# define tracing gdb_agent_tracing
130# define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
131# define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
132# define trace_buffer_lo gdb_agent_trace_buffer_lo
133# define trace_buffer_hi gdb_agent_trace_buffer_hi
134# define traceframe_read_count gdb_agent_traceframe_read_count
135# define traceframe_write_count gdb_agent_traceframe_write_count
136# define traceframes_created gdb_agent_traceframes_created
137# define trace_state_variables gdb_agent_trace_state_variables
6a271cae 138# define get_raw_reg gdb_agent_get_raw_reg
493e2a69
MS
139# define get_trace_state_variable_value \
140 gdb_agent_get_trace_state_variable_value
141# define set_trace_state_variable_value \
142 gdb_agent_set_trace_state_variable_value
0fb4aa4b
PA
143# define ust_loaded gdb_agent_ust_loaded
144# define helper_thread_id gdb_agent_helper_thread_id
145# define cmd_buf gdb_agent_cmd_buf
fa593d66
PA
146#endif
147
148#ifndef IN_PROCESS_AGENT
149
150/* Addresses of in-process agent's symbols GDBserver cares about. */
151
152struct ipa_sym_addresses
153{
154 CORE_ADDR addr_gdb_tp_heap_buffer;
155 CORE_ADDR addr_gdb_jump_pad_buffer;
156 CORE_ADDR addr_gdb_jump_pad_buffer_end;
405f8e94
SS
157 CORE_ADDR addr_gdb_trampoline_buffer;
158 CORE_ADDR addr_gdb_trampoline_buffer_end;
159 CORE_ADDR addr_gdb_trampoline_buffer_error;
fa593d66
PA
160 CORE_ADDR addr_collecting;
161 CORE_ADDR addr_gdb_collect;
162 CORE_ADDR addr_stop_tracing;
163 CORE_ADDR addr_flush_trace_buffer;
164 CORE_ADDR addr_about_to_request_buffer_space;
165 CORE_ADDR addr_trace_buffer_is_full;
166 CORE_ADDR addr_stopping_tracepoint;
167 CORE_ADDR addr_expr_eval_result;
168 CORE_ADDR addr_error_tracepoint;
169 CORE_ADDR addr_tracepoints;
170 CORE_ADDR addr_tracing;
171 CORE_ADDR addr_trace_buffer_ctrl;
172 CORE_ADDR addr_trace_buffer_ctrl_curr;
173 CORE_ADDR addr_trace_buffer_lo;
174 CORE_ADDR addr_trace_buffer_hi;
175 CORE_ADDR addr_traceframe_read_count;
176 CORE_ADDR addr_traceframe_write_count;
177 CORE_ADDR addr_traceframes_created;
178 CORE_ADDR addr_trace_state_variables;
6a271cae
PA
179 CORE_ADDR addr_get_raw_reg;
180 CORE_ADDR addr_get_trace_state_variable_value;
181 CORE_ADDR addr_set_trace_state_variable_value;
0fb4aa4b 182 CORE_ADDR addr_ust_loaded;
fa593d66
PA
183};
184
fa593d66
PA
185static struct
186{
187 const char *name;
188 int offset;
189 int required;
190} symbol_list[] = {
191 IPA_SYM(gdb_tp_heap_buffer),
192 IPA_SYM(gdb_jump_pad_buffer),
193 IPA_SYM(gdb_jump_pad_buffer_end),
405f8e94
SS
194 IPA_SYM(gdb_trampoline_buffer),
195 IPA_SYM(gdb_trampoline_buffer_end),
196 IPA_SYM(gdb_trampoline_buffer_error),
fa593d66
PA
197 IPA_SYM(collecting),
198 IPA_SYM(gdb_collect),
199 IPA_SYM(stop_tracing),
200 IPA_SYM(flush_trace_buffer),
201 IPA_SYM(about_to_request_buffer_space),
202 IPA_SYM(trace_buffer_is_full),
203 IPA_SYM(stopping_tracepoint),
204 IPA_SYM(expr_eval_result),
205 IPA_SYM(error_tracepoint),
206 IPA_SYM(tracepoints),
207 IPA_SYM(tracing),
208 IPA_SYM(trace_buffer_ctrl),
209 IPA_SYM(trace_buffer_ctrl_curr),
210 IPA_SYM(trace_buffer_lo),
211 IPA_SYM(trace_buffer_hi),
212 IPA_SYM(traceframe_read_count),
213 IPA_SYM(traceframe_write_count),
214 IPA_SYM(traceframes_created),
215 IPA_SYM(trace_state_variables),
6a271cae
PA
216 IPA_SYM(get_raw_reg),
217 IPA_SYM(get_trace_state_variable_value),
218 IPA_SYM(set_trace_state_variable_value),
0fb4aa4b 219 IPA_SYM(ust_loaded),
fa593d66
PA
220};
221
2fa291ac 222static struct ipa_sym_addresses ipa_sym_addrs;
fa593d66
PA
223
224int all_tracepoint_symbols_looked_up;
225
226int
227in_process_agent_loaded (void)
228{
229 return all_tracepoint_symbols_looked_up;
230}
231
232static int read_inferior_integer (CORE_ADDR symaddr, int *val);
233
0fb4aa4b 234/* Returns true if both the in-process agent library and the static
8ffcbaaf
YQ
235 tracepoints libraries are loaded in the inferior, and agent has
236 capability on static tracepoints. */
0fb4aa4b
PA
237
238static int
8ffcbaaf 239in_process_agent_supports_ust (void)
0fb4aa4b
PA
240{
241 int loaded = 0;
242
243 if (!in_process_agent_loaded ())
244 {
245 warning ("In-process agent not loaded");
246 return 0;
247 }
248
8ffcbaaf 249 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE))
0fb4aa4b 250 {
8ffcbaaf
YQ
251 /* Agent understands static tracepoint, then check whether UST is in
252 fact loaded in the inferior. */
253 if (read_inferior_integer (ipa_sym_addrs.addr_ust_loaded, &loaded))
254 {
255 warning ("Error reading ust_loaded in lib");
256 return 0;
257 }
0fb4aa4b 258
8ffcbaaf
YQ
259 return loaded;
260 }
261 else
262 return 0;
0fb4aa4b
PA
263}
264
fa593d66
PA
265static void
266write_e_ipa_not_loaded (char *buffer)
267{
268 sprintf (buffer,
269 "E.In-process agent library not loaded in process. "
0fb4aa4b
PA
270 "Fast and static tracepoints unavailable.");
271}
272
273/* Write an error to BUFFER indicating that UST isn't loaded in the
274 inferior. */
275
276static void
277write_e_ust_not_loaded (char *buffer)
278{
279#ifdef HAVE_UST
280 sprintf (buffer,
281 "E.UST library not loaded in process. "
282 "Static tracepoints unavailable.");
283#else
284 sprintf (buffer, "E.GDBserver was built without static tracepoints support");
285#endif
fa593d66
PA
286}
287
0fb4aa4b
PA
288/* If the in-process agent library isn't loaded in the inferior, write
289 an error to BUFFER, and return 1. Otherwise, return 0. */
290
fa593d66
PA
291static int
292maybe_write_ipa_not_loaded (char *buffer)
293{
294 if (!in_process_agent_loaded ())
295 {
296 write_e_ipa_not_loaded (buffer);
297 return 1;
298 }
299 return 0;
300}
301
0fb4aa4b
PA
302/* If the in-process agent library and the ust (static tracepoints)
303 library aren't loaded in the inferior, write an error to BUFFER,
304 and return 1. Otherwise, return 0. */
305
306static int
307maybe_write_ipa_ust_not_loaded (char *buffer)
308{
309 if (!in_process_agent_loaded ())
310 {
311 write_e_ipa_not_loaded (buffer);
312 return 1;
313 }
8ffcbaaf 314 else if (!in_process_agent_supports_ust ())
0fb4aa4b
PA
315 {
316 write_e_ust_not_loaded (buffer);
317 return 1;
318 }
319 return 0;
320}
321
fa593d66
PA
322/* Cache all future symbols that the tracepoints module might request.
323 We can not request symbols at arbitrary states in the remote
324 protocol, only when the client tells us that new symbols are
325 available. So when we load the in-process library, make sure to
326 check the entire list. */
327
328void
329tracepoint_look_up_symbols (void)
330{
fa593d66
PA
331 int i;
332
333 if (all_tracepoint_symbols_looked_up)
334 return;
335
fa593d66
PA
336 for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
337 {
338 CORE_ADDR *addrp =
339 (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
340
341 if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
342 {
343 if (debug_threads)
344 fprintf (stderr, "symbol `%s' not found\n", symbol_list[i].name);
2275a1a7 345 return;
fa593d66
PA
346 }
347 }
348
2fa291ac
YQ
349 if (agent_look_up_symbols () != 0)
350 return;
351
2275a1a7 352 all_tracepoint_symbols_looked_up = 1;
fa593d66
PA
353}
354
355#endif
356
357/* GDBserver places a breakpoint on the IPA's version (which is a nop)
358 of the "stop_tracing" function. When this breakpoint is hit,
359 tracing stopped in the IPA for some reason. E.g., due to
360 tracepoint reaching the pass count, hitting conditional expression
361 evaluation error, etc.
362
363 The IPA's trace buffer is never in circular tracing mode: instead,
364 GDBserver's is, and whenever the in-process buffer fills, it calls
365 "flush_trace_buffer", which triggers an internal breakpoint.
366 GDBserver reacts to this breakpoint by pulling the meanwhile
367 collected data. Old frames discarding is always handled on the
368 GDBserver side. */
369
370#ifdef IN_PROCESS_AGENT
fa593d66
PA
371int
372read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
373{
374 memcpy (myaddr, (void *) (uintptr_t) memaddr, len);
375 return 0;
376}
377
378/* Call this in the functions where GDBserver places a breakpoint, so
379 that the compiler doesn't try to be clever and skip calling the
380 function at all. This is necessary, even if we tell the compiler
381 to not inline said functions. */
382
383#if defined(__GNUC__)
384# define UNKNOWN_SIDE_EFFECTS() asm ("")
385#else
386# define UNKNOWN_SIDE_EFFECTS() do {} while (0)
387#endif
388
389IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
390stop_tracing (void)
391{
392 /* GDBserver places breakpoint here. */
393 UNKNOWN_SIDE_EFFECTS();
394}
395
396IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
397flush_trace_buffer (void)
398{
399 /* GDBserver places breakpoint here. */
400 UNKNOWN_SIDE_EFFECTS();
401}
402
403#endif
404
405#ifndef IN_PROCESS_AGENT
219f2f23
PA
406static int
407tracepoint_handler (CORE_ADDR address)
408{
409 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
410 paddress (address));
411 return 0;
412}
413
fa593d66
PA
414/* Breakpoint at "stop_tracing" in the inferior lib. */
415struct breakpoint *stop_tracing_bkpt;
416static int stop_tracing_handler (CORE_ADDR);
417
418/* Breakpoint at "flush_trace_buffer" in the inferior lib. */
419struct breakpoint *flush_trace_buffer_bkpt;
420static int flush_trace_buffer_handler (CORE_ADDR);
421
422static void download_tracepoints (void);
423static void download_trace_state_variables (void);
424static void upload_fast_traceframes (void);
425
0fb4aa4b
PA
426static int run_inferior_command (char *cmd);
427
fa593d66
PA
428static int
429read_inferior_integer (CORE_ADDR symaddr, int *val)
430{
431 return read_inferior_memory (symaddr, (unsigned char *) val,
432 sizeof (*val));
433}
434
435static int
436read_inferior_uinteger (CORE_ADDR symaddr, unsigned int *val)
437{
438 return read_inferior_memory (symaddr, (unsigned char *) val,
439 sizeof (*val));
440}
441
442static int
443read_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR *val)
444{
445 void *pval = (void *) (uintptr_t) val;
446 int ret;
447
448 ret = read_inferior_memory (symaddr, (unsigned char *) &pval, sizeof (pval));
449 *val = (uintptr_t) pval;
450 return ret;
451}
452
453static int
454write_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR val)
455{
456 void *pval = (void *) (uintptr_t) val;
457 return write_inferior_memory (symaddr,
458 (unsigned char *) &pval, sizeof (pval));
459}
460
461static int
462write_inferior_integer (CORE_ADDR symaddr, int val)
463{
464 return write_inferior_memory (symaddr, (unsigned char *) &val, sizeof (val));
465}
466
467static int
468write_inferior_uinteger (CORE_ADDR symaddr, unsigned int val)
469{
470 return write_inferior_memory (symaddr, (unsigned char *) &val, sizeof (val));
471}
472
473#endif
474
219f2f23
PA
475/* Base action. Concrete actions inherit this. */
476
477struct tracepoint_action
478{
479 char type;
480};
481
482/* An 'M' (collect memory) action. */
483struct collect_memory_action
484{
485 struct tracepoint_action base;
486
487 ULONGEST addr;
488 ULONGEST len;
489 int basereg;
490};
491
492/* An 'R' (collect registers) action. */
493
494struct collect_registers_action
495{
496 struct tracepoint_action base;
497};
498
499/* An 'X' (evaluate expression) action. */
500
501struct eval_expr_action
502{
503 struct tracepoint_action base;
504
505 struct agent_expr *expr;
506};
507
0fb4aa4b
PA
508/* An 'L' (collect static trace data) action. */
509struct collect_static_trace_data_action
510{
511 struct tracepoint_action base;
512};
513
219f2f23
PA
514/* This structure describes a piece of the source-level definition of
515 the tracepoint. The contents are not interpreted by the target,
516 but preserved verbatim for uploading upon reconnection. */
517
518struct source_string
519{
520 /* The type of string, such as "cond" for a conditional. */
521 char *type;
522
523 /* The source-level string itself. For the sake of target
524 debugging, we store it in plaintext, even though it is always
525 transmitted in hex. */
526 char *str;
527
528 /* Link to the next one in the list. We link them in the order
529 received, in case some make up an ordered list of commands or
530 some such. */
531 struct source_string *next;
532};
533
fa593d66
PA
534enum tracepoint_type
535{
536 /* Trap based tracepoint. */
537 trap_tracepoint,
538
539 /* A fast tracepoint implemented with a jump instead of a trap. */
540 fast_tracepoint,
0fb4aa4b
PA
541
542 /* A static tracepoint, implemented by a program call into a tracing
543 library. */
544 static_tracepoint
fa593d66
PA
545};
546
219f2f23
PA
547struct tracepoint_hit_ctx;
548
6a271cae
PA
549typedef enum eval_result_type (*condfn) (struct tracepoint_hit_ctx *,
550 ULONGEST *);
551
219f2f23
PA
552/* The definition of a tracepoint. */
553
554/* Tracepoints may have multiple locations, each at a different
555 address. This can occur with optimizations, template
556 instantiation, etc. Since the locations may be in different
557 scopes, the conditions and actions may be different for each
558 location. Our target version of tracepoints is more like GDB's
559 notion of "breakpoint locations", but we have almost nothing that
560 is not per-location, so we bother having two kinds of objects. The
561 key consequence is that numbers are not unique, and that it takes
562 both number and address to identify a tracepoint uniquely. */
563
564struct tracepoint
565{
566 /* The number of the tracepoint, as specified by GDB. Several
567 tracepoint objects here may share a number. */
568 int number;
569
570 /* Address at which the tracepoint is supposed to trigger. Several
571 tracepoints may share an address. */
572 CORE_ADDR address;
573
fa593d66
PA
574 /* Tracepoint type. */
575 enum tracepoint_type type;
576
219f2f23
PA
577 /* True if the tracepoint is currently enabled. */
578 int enabled;
579
580 /* The number of single steps that will be performed after each
581 tracepoint hit. */
582 long step_count;
583
584 /* The number of times the tracepoint may be hit before it will
585 terminate the entire tracing run. */
586 long pass_count;
587
588 /* Pointer to the agent expression that is the tracepoint's
589 conditional, or NULL if the tracepoint is unconditional. */
590 struct agent_expr *cond;
591
592 /* The list of actions to take when the tracepoint triggers. */
593 int numactions;
594 struct tracepoint_action **actions;
219f2f23
PA
595
596 /* Count of the times we've hit this tracepoint during the run.
597 Note that while-stepping steps are not counted as "hits". */
598 long hit_count;
599
f196051f
SS
600 /* Cached sum of the sizes of traceframes created by this point. */
601 long traceframe_usage;
602
6a271cae
PA
603 CORE_ADDR compiled_cond;
604
fa593d66
PA
605 /* Link to the next tracepoint in the list. */
606 struct tracepoint *next;
607
608#ifndef IN_PROCESS_AGENT
609 /* The list of actions to take when the tracepoint triggers, in
610 string/packet form. */
611 char **actions_str;
612
219f2f23
PA
613 /* The collection of strings that describe the tracepoint as it was
614 entered into GDB. These are not used by the target, but are
615 reported back to GDB upon reconnection. */
616 struct source_string *source_strings;
617
fa593d66
PA
618 /* The number of bytes displaced by fast tracepoints. It may subsume
619 multiple instructions, for multi-byte fast tracepoints. This
620 field is only valid for fast tracepoints. */
621 int orig_size;
622
623 /* Only for fast tracepoints. */
624 CORE_ADDR obj_addr_on_target;
625
626 /* Address range where the original instruction under a fast
627 tracepoint was relocated to. (_end is actually one byte past
628 the end). */
629 CORE_ADDR adjusted_insn_addr;
630 CORE_ADDR adjusted_insn_addr_end;
631
632 /* The address range of the piece of the jump pad buffer that was
633 assigned to this fast tracepoint. (_end is actually one byte
634 past the end).*/
635 CORE_ADDR jump_pad;
636 CORE_ADDR jump_pad_end;
637
405f8e94
SS
638 /* The address range of the piece of the trampoline buffer that was
639 assigned to this fast tracepoint. (_end is actually one byte
640 past the end). */
641 CORE_ADDR trampoline;
642 CORE_ADDR trampoline_end;
643
fa593d66
PA
644 /* The list of actions to take while in a stepping loop. These
645 fields are only valid for patch-based tracepoints. */
646 int num_step_actions;
647 struct tracepoint_action **step_actions;
648 /* Same, but in string/packet form. */
649 char **step_actions_str;
650
651 /* Handle returned by the breakpoint or tracepoint module when we
0fb4aa4b
PA
652 inserted the trap or jump, or hooked into a static tracepoint.
653 NULL if we haven't inserted it yet. */
219f2f23 654 void *handle;
fa593d66 655#endif
219f2f23 656
219f2f23
PA
657};
658
fa593d66
PA
659#ifndef IN_PROCESS_AGENT
660
219f2f23
PA
661/* Given `while-stepping', a thread may be collecting data for more
662 than one tracepoint simultaneously. On the other hand, the same
663 tracepoint with a while-stepping action may be hit by more than one
664 thread simultaneously (but not quite, each thread could be handling
665 a different step). Each thread holds a list of these objects,
666 representing the current step of each while-stepping action being
667 collected. */
668
669struct wstep_state
670{
671 struct wstep_state *next;
672
673 /* The tracepoint number. */
674 int tp_number;
675 /* The tracepoint's address. */
676 CORE_ADDR tp_address;
677
678 /* The number of the current step in this 'while-stepping'
679 action. */
680 long current_step;
681};
682
fa593d66
PA
683#endif
684
685/* The linked list of all tracepoints. Marked explicitly as used as
686 the in-process library doesn't use it for the fast tracepoints
687 support. */
688IP_AGENT_EXPORT struct tracepoint *tracepoints ATTR_USED;
219f2f23 689
fa593d66 690#ifndef IN_PROCESS_AGENT
219f2f23
PA
691
692/* Pointer to the last tracepoint in the list, new tracepoints are
693 linked in at the end. */
694
695static struct tracepoint *last_tracepoint;
fa593d66 696#endif
219f2f23
PA
697
698/* The first tracepoint to exceed its pass count. */
699
fa593d66 700IP_AGENT_EXPORT struct tracepoint *stopping_tracepoint;
219f2f23
PA
701
702/* True if the trace buffer is full or otherwise no longer usable. */
703
fa593d66 704IP_AGENT_EXPORT int trace_buffer_is_full;
219f2f23 705
219f2f23
PA
706static enum eval_result_type expr_eval_result = expr_eval_no_error;
707
fa593d66
PA
708#ifndef IN_PROCESS_AGENT
709
219f2f23
PA
710static const char *eval_result_names[] =
711 {
712 "terror:in the attic", /* this should never be reported */
713 "terror:empty expression",
714 "terror:empty stack",
715 "terror:stack overflow",
716 "terror:stack underflow",
717 "terror:unhandled opcode",
718 "terror:unrecognized opcode",
719 "terror:divide by zero"
720 };
721
fa593d66
PA
722#endif
723
219f2f23
PA
724/* The tracepoint in which the error occurred. */
725
726static struct tracepoint *error_tracepoint;
727
728struct trace_state_variable
729{
730 /* This is the name of the variable as used in GDB. The target
731 doesn't use the name, but needs to have it for saving and
732 reconnection purposes. */
733 char *name;
734
735 /* This number identifies the variable uniquely. Numbers may be
736 assigned either by the target (in the case of builtin variables),
737 or by GDB, and are presumed unique during the course of a trace
738 experiment. */
739 int number;
740
741 /* The variable's initial value, a 64-bit signed integer always. */
742 LONGEST initial_value;
743
744 /* The variable's value, a 64-bit signed integer always. */
745 LONGEST value;
746
747 /* Pointer to a getter function, used to supply computed values. */
748 LONGEST (*getter) (void);
749
750 /* Link to the next variable. */
751 struct trace_state_variable *next;
752};
753
754/* Linked list of all trace state variables. */
755
fa593d66
PA
756#ifdef IN_PROCESS_AGENT
757struct trace_state_variable *alloced_trace_state_variables;
758#endif
759
760IP_AGENT_EXPORT struct trace_state_variable *trace_state_variables;
219f2f23
PA
761
762/* The results of tracing go into a fixed-size space known as the
763 "trace buffer". Because usage follows a limited number of
764 patterns, we manage it ourselves rather than with malloc. Basic
765 rules are that we create only one trace frame at a time, each is
766 variable in size, they are never moved once created, and we only
767 discard if we are doing a circular buffer, and then only the oldest
768 ones. Each trace frame includes its own size, so we don't need to
769 link them together, and the trace frame number is relative to the
770 first one, so we don't need to record numbers. A trace frame also
771 records the number of the tracepoint that created it. The data
772 itself is a series of blocks, each introduced by a single character
773 and with a defined format. Each type of block has enough
774 type/length info to allow scanners to jump quickly from one block
775 to the next without reading each byte in the block. */
776
777/* Trace buffer management would be simple - advance a free pointer
778 from beginning to end, then stop - were it not for the circular
779 buffer option, which is a useful way to prevent a trace run from
780 stopping prematurely because the buffer filled up. In the circular
781 case, the location of the first trace frame (trace_buffer_start)
782 moves as old trace frames are discarded. Also, since we grow trace
783 frames incrementally as actions are performed, we wrap around to
784 the beginning of the trace buffer. This is per-block, so each
785 block within a trace frame remains contiguous. Things get messy
786 when the wrapped-around trace frame is the one being discarded; the
787 free space ends up in two parts at opposite ends of the buffer. */
788
789#ifndef ATTR_PACKED
790# if defined(__GNUC__)
791# define ATTR_PACKED __attribute__ ((packed))
792# else
793# define ATTR_PACKED /* nothing */
794# endif
795#endif
796
797/* The data collected at a tracepoint hit. This object should be as
798 small as possible, since there may be a great many of them. We do
799 not need to keep a frame number, because they are all sequential
800 and there are no deletions; so the Nth frame in the buffer is
801 always frame number N. */
802
803struct traceframe
804{
805 /* Number of the tracepoint that collected this traceframe. A value
806 of 0 indicates the current end of the trace buffer. We make this
807 a 16-bit field because it's never going to happen that GDB's
808 numbering of tracepoints reaches 32,000. */
809 int tpnum : 16;
810
811 /* The size of the data in this trace frame. We limit this to 32
812 bits, even on a 64-bit target, because it's just implausible that
813 one is validly going to collect 4 gigabytes of data at a single
814 tracepoint hit. */
815 unsigned int data_size : 32;
816
817 /* The base of the trace data, which is contiguous from this point. */
818 unsigned char data[0];
819
fa593d66 820} ATTR_PACKED;
219f2f23
PA
821
822/* The traceframe to be used as the source of data to send back to
823 GDB. A value of -1 means to get data from the live program. */
824
825int current_traceframe = -1;
826
827/* This flag is true if the trace buffer is circular, meaning that
828 when it fills, the oldest trace frames are discarded in order to
829 make room. */
830
fa593d66 831#ifndef IN_PROCESS_AGENT
219f2f23 832static int circular_trace_buffer;
fa593d66 833#endif
219f2f23
PA
834
835/* Pointer to the block of memory that traceframes all go into. */
836
837static unsigned char *trace_buffer_lo;
838
839/* Pointer to the end of the trace buffer, more precisely to the byte
840 after the end of the buffer. */
841
842static unsigned char *trace_buffer_hi;
843
fa593d66
PA
844/* Control structure holding the read/write/etc. pointers into the
845 trace buffer. We need more than one of these to implement a
846 transaction-like mechanism to garantees that both GDBserver and the
847 in-process agent can try to change the trace buffer
848 simultaneously. */
849
850struct trace_buffer_control
851{
852 /* Pointer to the first trace frame in the buffer. In the
853 non-circular case, this is equal to trace_buffer_lo, otherwise it
854 moves around in the buffer. */
855 unsigned char *start;
856
857 /* Pointer to the free part of the trace buffer. Note that we clear
858 several bytes at and after this pointer, so that traceframe
859 scans/searches terminate properly. */
860 unsigned char *free;
861
862 /* Pointer to the byte after the end of the free part. Note that
863 this may be smaller than trace_buffer_free in the circular case,
864 and means that the free part is in two pieces. Initially it is
865 equal to trace_buffer_hi, then is generally equivalent to
866 trace_buffer_start. */
867 unsigned char *end_free;
868
869 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
870 this is the point at which the trace data breaks, and resumes at
871 trace_buffer_lo. */
872 unsigned char *wrap;
873};
874
875/* Same as above, to be used by GDBserver when updating the in-process
876 agent. */
877struct ipa_trace_buffer_control
878{
879 uintptr_t start;
880 uintptr_t free;
881 uintptr_t end_free;
882 uintptr_t wrap;
883};
884
885
886/* We have possibly both GDBserver and an inferior thread accessing
887 the same IPA trace buffer memory. The IPA is the producer (tries
888 to put new frames in the buffer), while GDBserver occasionally
889 consumes them, that is, flushes the IPA's buffer into its own
890 buffer. Both sides need to update the trace buffer control
891 pointers (current head, tail, etc.). We can't use a global lock to
892 synchronize the accesses, as otherwise we could deadlock GDBserver
893 (if the thread holding the lock stops for a signal, say). So
894 instead of that, we use a transaction scheme where GDBserver writes
895 always prevail over the IPAs writes, and, we have the IPA detect
896 the commit failure/overwrite, and retry the whole attempt. This is
897 mainly implemented by having a global token object that represents
898 who wrote last to the buffer control structure. We need to freeze
899 any inferior writing to the buffer while GDBserver touches memory,
900 so that the inferior can correctly detect that GDBserver had been
901 there, otherwise, it could mistakingly think its commit was
902 successful; that's implemented by simply having GDBserver set a
903 breakpoint the inferior hits if it is the critical region.
904
905 There are three cycling trace buffer control structure copies
906 (buffer head, tail, etc.), with the token object including an index
907 indicating which is current live copy. The IPA tentatively builds
908 an updated copy in a non-current control structure, while GDBserver
909 always clobbers the current version directly. The IPA then tries
910 to atomically "commit" its version; if GDBserver clobbered the
911 structure meanwhile, that will fail, and the IPA restarts the
912 allocation process.
913
914 Listing the step in further detail, we have:
915
916 In-process agent (producer):
917
918 - passes by `about_to_request_buffer_space' breakpoint/lock
919
920 - reads current token, extracts current trace buffer control index,
921 and starts tentatively updating the rightmost one (0->1, 1->2,
922 2->0). Note that only one inferior thread is executing this code
923 at any given time, due to an outer lock in the jump pads.
219f2f23 924
fa593d66 925 - updates counters, and tries to commit the token.
219f2f23 926
fa593d66
PA
927 - passes by second `about_to_request_buffer_space' breakpoint/lock,
928 leaving the sync region.
219f2f23 929
fa593d66 930 - checks if the update was effective.
219f2f23 931
fa593d66
PA
932 - if trace buffer was found full, hits flush_trace_buffer
933 breakpoint, and restarts later afterwards.
219f2f23 934
fa593d66 935 GDBserver (consumer):
219f2f23 936
fa593d66
PA
937 - sets `about_to_request_buffer_space' breakpoint/lock.
938
939 - updates the token unconditionally, using the current buffer
940 control index, since it knows that the IP agent always writes to
941 the rightmost, and due to the breakpoint, at most one IP thread
942 can try to update the trace buffer concurrently to GDBserver, so
943 there will be no danger of trace buffer control index wrap making
944 the IPA write to the same index as GDBserver.
945
946 - flushes the IP agent's trace buffer completely, and updates the
947 current trace buffer control structure. GDBserver *always* wins.
948
949 - removes the `about_to_request_buffer_space' breakpoint.
950
951The token is stored in the `trace_buffer_ctrl_curr' variable.
952Internally, it's bits are defined as:
953
954 |-------------+-----+-------------+--------+-------------+--------------|
955 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
956 |-------------+-----+-------------+--------+-------------+--------------|
957 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
958 |-------------+-----+-------------+--------+-------------+--------------|
959
960 GSB - GDBserver Stamp Bit
961 PC - Previous Counter
962 CC - Current Counter
963 TBCI - Trace Buffer Control Index
964
965
966An IPA update of `trace_buffer_ctrl_curr' does:
967
968 - read CC from the current token, save as PC.
969 - updates pointers
970 - atomically tries to write PC+1,CC
971
972A GDBserver update of `trace_buffer_ctrl_curr' does:
973
974 - reads PC and CC from the current token.
975 - updates pointers
976 - writes GSB,PC,CC
977*/
978
979/* These are the bits of `trace_buffer_ctrl_curr' that are reserved
980 for the counters described below. The cleared bits are used to
981 hold the index of the items of the `trace_buffer_ctrl' array that
982 is "current". */
983#define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
984
985/* `trace_buffer_ctrl_curr' contains two counters. The `previous'
986 counter, and the `current' counter. */
987
988#define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
989#define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
990
991/* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
992 always stamps this bit as set. */
993#define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
994
995#ifdef IN_PROCESS_AGENT
996IP_AGENT_EXPORT struct trace_buffer_control trace_buffer_ctrl[3];
997IP_AGENT_EXPORT unsigned int trace_buffer_ctrl_curr;
998
999# define TRACE_BUFFER_CTRL_CURR \
1000 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1001
1002#else
1003
1004/* The GDBserver side agent only needs one instance of this object, as
1005 it doesn't need to sync with itself. Define it as array anyway so
1006 that the rest of the code base doesn't need to care for the
1007 difference. */
1008struct trace_buffer_control trace_buffer_ctrl[1];
1009# define TRACE_BUFFER_CTRL_CURR 0
1010#endif
1011
1012/* These are convenience macros used to access the current trace
1013 buffer control in effect. */
1014#define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1015#define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1016#define trace_buffer_end_free \
1017 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1018#define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
219f2f23 1019
219f2f23
PA
1020
1021/* Macro that returns a pointer to the first traceframe in the buffer. */
1022
1023#define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1024
1025/* Macro that returns a pointer to the next traceframe in the buffer.
1026 If the computed location is beyond the wraparound point, subtract
1027 the offset of the wraparound. */
1028
1029#define NEXT_TRACEFRAME_1(TF) \
1030 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1031
1032#define NEXT_TRACEFRAME(TF) \
1033 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1034 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1035 ? (trace_buffer_wrap - trace_buffer_lo) \
1036 : 0)))
1037
1038/* The difference between these counters represents the total number
fa593d66
PA
1039 of complete traceframes present in the trace buffer. The IP agent
1040 writes to the write count, GDBserver writes to read count. */
219f2f23 1041
fa593d66
PA
1042IP_AGENT_EXPORT unsigned int traceframe_write_count;
1043IP_AGENT_EXPORT unsigned int traceframe_read_count;
219f2f23
PA
1044
1045/* Convenience macro. */
1046
1047#define traceframe_count \
1048 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1049
1050/* The count of all traceframes created in the current run, including
1051 ones that were discarded to make room. */
1052
fa593d66
PA
1053IP_AGENT_EXPORT int traceframes_created;
1054
1055#ifndef IN_PROCESS_AGENT
219f2f23
PA
1056
1057/* Read-only regions are address ranges whose contents don't change,
1058 and so can be read from target memory even while looking at a trace
1059 frame. Without these, disassembly for instance will likely fail,
1060 because the program code is not usually collected into a trace
1061 frame. This data structure does not need to be very complicated or
1062 particularly efficient, it's only going to be used occasionally,
1063 and only by some commands. */
1064
1065struct readonly_region
1066{
1067 /* The bounds of the region. */
1068 CORE_ADDR start, end;
1069
1070 /* Link to the next one. */
1071 struct readonly_region *next;
1072};
1073
1074/* Linked list of readonly regions. This list stays in effect from
1075 one tstart to the next. */
1076
1077static struct readonly_region *readonly_regions;
1078
fa593d66
PA
1079#endif
1080
219f2f23
PA
1081/* The global that controls tracing overall. */
1082
fa593d66
PA
1083IP_AGENT_EXPORT int tracing;
1084
1085#ifndef IN_PROCESS_AGENT
8336d594
PA
1086
1087/* Controls whether tracing should continue after GDB disconnects. */
1088
1089int disconnected_tracing;
219f2f23
PA
1090
1091/* The reason for the last tracing run to have stopped. We initialize
1092 to a distinct string so that GDB can distinguish between "stopped
1093 after running" and "stopped because never run" cases. */
1094
1095static const char *tracing_stop_reason = "tnotrun";
1096
1097static int tracing_stop_tpnum;
1098
f196051f
SS
1099/* 64-bit timestamps for the trace run's start and finish, expressed
1100 in microseconds from the Unix epoch. */
1101
1102LONGEST tracing_start_time;
1103LONGEST tracing_stop_time;
1104
1105/* The (optional) user-supplied name of the user that started the run.
1106 This is an arbitrary string, and may be NULL. */
1107
1108char *tracing_user_name;
1109
1110/* Optional user-supplied text describing the run. This is
1111 an arbitrary string, and may be NULL. */
1112
1113char *tracing_notes;
1114
1115/* Optional user-supplied text explaining a tstop command. This is an
1116 arbitrary string, and may be NULL. */
1117
1118char *tracing_stop_note;
1119
fa593d66
PA
1120#endif
1121
219f2f23
PA
1122/* Functions local to this file. */
1123
1124/* Base "class" for tracepoint type specific data to be passed down to
fa593d66 1125 collect_data_at_tracepoint. */
219f2f23
PA
1126struct tracepoint_hit_ctx
1127{
fa593d66 1128 enum tracepoint_type type;
219f2f23
PA
1129};
1130
fa593d66
PA
1131#ifdef IN_PROCESS_AGENT
1132
1133/* Fast/jump tracepoint specific data to be passed down to
219f2f23 1134 collect_data_at_tracepoint. */
fa593d66
PA
1135struct fast_tracepoint_ctx
1136{
1137 struct tracepoint_hit_ctx base;
1138
1139 struct regcache regcache;
1140 int regcache_initted;
1141 unsigned char *regspace;
1142
1143 unsigned char *regs;
1144 struct tracepoint *tpoint;
1145};
219f2f23 1146
0fb4aa4b
PA
1147/* Static tracepoint specific data to be passed down to
1148 collect_data_at_tracepoint. */
1149struct static_tracepoint_ctx
1150{
1151 struct tracepoint_hit_ctx base;
1152
1153 /* The regcache corresponding to the registers state at the time of
1154 the tracepoint hit. Initialized lazily, from REGS. */
1155 struct regcache regcache;
1156 int regcache_initted;
1157
1158 /* The buffer space REGCACHE above uses. We use a separate buffer
1159 instead of letting the regcache malloc for both signal safety and
1160 performance reasons; this is allocated on the stack instead. */
1161 unsigned char *regspace;
1162
1163 /* The register buffer as passed on by lttng/ust. */
1164 struct registers *regs;
1165
1166 /* The "printf" formatter and the args the user passed to the marker
1167 call. We use this to be able to collect "static trace data"
1168 ($_sdata). */
1169 const char *fmt;
1170 va_list *args;
1171
1172 /* The GDB tracepoint matching the probed marker that was "hit". */
1173 struct tracepoint *tpoint;
1174};
1175
fa593d66
PA
1176#else
1177
1178/* Static tracepoint specific data to be passed down to
1179 collect_data_at_tracepoint. */
219f2f23
PA
1180struct trap_tracepoint_ctx
1181{
1182 struct tracepoint_hit_ctx base;
1183
1184 struct regcache *regcache;
1185};
1186
fa593d66
PA
1187#endif
1188
5e1dc496
LM
1189static enum eval_result_type
1190eval_tracepoint_agent_expr (struct tracepoint_hit_ctx *ctx,
1191 struct traceframe *tframe,
1192 struct agent_expr *aexpr,
1193 ULONGEST *rslt);
219f2f23 1194
fa593d66 1195#ifndef IN_PROCESS_AGENT
219f2f23
PA
1196static CORE_ADDR traceframe_get_pc (struct traceframe *tframe);
1197static int traceframe_read_tsv (int num, LONGEST *val);
fa593d66 1198#endif
219f2f23
PA
1199
1200static int condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1201 struct tracepoint *tpoint);
1202
fa593d66 1203#ifndef IN_PROCESS_AGENT
219f2f23
PA
1204static void clear_readonly_regions (void);
1205static void clear_installed_tracepoints (void);
fa593d66 1206#endif
219f2f23
PA
1207
1208static void collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1209 CORE_ADDR stop_pc,
1210 struct tracepoint *tpoint);
fa593d66 1211#ifndef IN_PROCESS_AGENT
219f2f23
PA
1212static void collect_data_at_step (struct tracepoint_hit_ctx *ctx,
1213 CORE_ADDR stop_pc,
1214 struct tracepoint *tpoint, int current_step);
6a271cae
PA
1215static void compile_tracepoint_condition (struct tracepoint *tpoint,
1216 CORE_ADDR *jump_entry);
fa593d66 1217#endif
219f2f23
PA
1218static void do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1219 CORE_ADDR stop_pc,
1220 struct tracepoint *tpoint,
1221 struct traceframe *tframe,
1222 struct tracepoint_action *taction);
1223
fa593d66
PA
1224#ifndef IN_PROCESS_AGENT
1225static struct tracepoint *fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR);
1e4d1764
YQ
1226
1227static void install_tracepoint (struct tracepoint *, char *own_buf);
1228static void download_tracepoint (struct tracepoint *);
405f8e94 1229static int install_fast_tracepoint (struct tracepoint *, char *errbuf);
fa593d66
PA
1230#endif
1231
f196051f
SS
1232static LONGEST get_timestamp (void);
1233
fa593d66
PA
1234#if defined(__GNUC__)
1235# define memory_barrier() asm volatile ("" : : : "memory")
1236#else
1237# define memory_barrier() do {} while (0)
1238#endif
1239
1240/* We only build the IPA if this builtin is supported, and there are
1241 no uses of this in GDBserver itself, so we're safe in defining this
1242 unconditionally. */
1243#define cmpxchg(mem, oldval, newval) \
1244 __sync_val_compare_and_swap (mem, oldval, newval)
1245
219f2f23
PA
1246/* Record that an error occurred during expression evaluation. */
1247
1248static void
1249record_tracepoint_error (struct tracepoint *tpoint, const char *which,
1250 enum eval_result_type rtype)
1251{
1252 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1253 tpoint->number, paddress (tpoint->address), which, rtype);
1254
fa593d66
PA
1255#ifdef IN_PROCESS_AGENT
1256 /* Only record the first error we get. */
1257 if (cmpxchg (&expr_eval_result,
1258 expr_eval_no_error,
1259 rtype) != expr_eval_no_error)
1260 return;
1261#else
1262 if (expr_eval_result != expr_eval_no_error)
1263 return;
1264#endif
1265
219f2f23
PA
1266 error_tracepoint = tpoint;
1267}
1268
1269/* Trace buffer management. */
1270
1271static void
1272clear_trace_buffer (void)
1273{
1274 trace_buffer_start = trace_buffer_lo;
1275 trace_buffer_free = trace_buffer_lo;
1276 trace_buffer_end_free = trace_buffer_hi;
1277 trace_buffer_wrap = trace_buffer_hi;
1278 /* A traceframe with zeroed fields marks the end of trace data. */
1279 ((struct traceframe *) trace_buffer_free)->tpnum = 0;
1280 ((struct traceframe *) trace_buffer_free)->data_size = 0;
1281 traceframe_read_count = traceframe_write_count = 0;
1282 traceframes_created = 0;
1283}
1284
fa593d66
PA
1285#ifndef IN_PROCESS_AGENT
1286
1287static void
1288clear_inferior_trace_buffer (void)
1289{
1290 CORE_ADDR ipa_trace_buffer_lo;
1291 CORE_ADDR ipa_trace_buffer_hi;
1292 struct traceframe ipa_traceframe = { 0 };
1293 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
1294
1295 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
1296 &ipa_trace_buffer_lo);
1297 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
1298 &ipa_trace_buffer_hi);
1299
1300 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
1301 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
1302 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
1303 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
1304
1305 /* A traceframe with zeroed fields marks the end of trace data. */
1306 write_inferior_memory (ipa_sym_addrs.addr_trace_buffer_ctrl,
1307 (unsigned char *) &ipa_trace_buffer_ctrl,
1308 sizeof (ipa_trace_buffer_ctrl));
1309
1310 write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr, 0);
1311
1312 /* A traceframe with zeroed fields marks the end of trace data. */
1313 write_inferior_memory (ipa_trace_buffer_lo,
1314 (unsigned char *) &ipa_traceframe,
1315 sizeof (ipa_traceframe));
1316
1317 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count, 0);
1318 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count, 0);
1319 write_inferior_integer (ipa_sym_addrs.addr_traceframes_created, 0);
1320}
1321
1322#endif
1323
219f2f23
PA
1324static void
1325init_trace_buffer (unsigned char *buf, int bufsize)
1326{
1327 trace_buffer_lo = buf;
1328 trace_buffer_hi = trace_buffer_lo + bufsize;
1329
1330 clear_trace_buffer ();
1331}
1332
fa593d66
PA
1333#ifdef IN_PROCESS_AGENT
1334
1335IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
1336about_to_request_buffer_space (void)
1337{
1338 /* GDBserver places breakpoint here while it goes about to flush
1339 data at random times. */
1340 UNKNOWN_SIDE_EFFECTS();
1341}
1342
1343#endif
1344
219f2f23
PA
1345/* Carve out a piece of the trace buffer, returning NULL in case of
1346 failure. */
1347
1348static void *
1349trace_buffer_alloc (size_t amt)
1350{
1351 unsigned char *rslt;
fa593d66
PA
1352 struct trace_buffer_control *tbctrl;
1353 unsigned int curr;
1354#ifdef IN_PROCESS_AGENT
1355 unsigned int prev, prev_filtered;
1356 unsigned int commit_count;
1357 unsigned int commit;
1358 unsigned int readout;
1359#else
219f2f23
PA
1360 struct traceframe *oldest;
1361 unsigned char *new_start;
fa593d66 1362#endif
219f2f23
PA
1363
1364 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1365 (long) amt, (long) sizeof (struct traceframe));
1366
1367 /* Account for the EOB marker. */
1368 amt += sizeof (struct traceframe);
1369
fa593d66
PA
1370#ifdef IN_PROCESS_AGENT
1371 again:
1372 memory_barrier ();
1373
1374 /* Read the current token and extract the index to try to write to,
1375 storing it in CURR. */
1376 prev = trace_buffer_ctrl_curr;
1377 prev_filtered = prev & ~GDBSERVER_FLUSH_COUNT_MASK;
1378 curr = prev_filtered + 1;
1379 if (curr > 2)
1380 curr = 0;
1381
1382 about_to_request_buffer_space ();
1383
1384 /* Start out with a copy of the current state. GDBserver may be
1385 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1386 be able to commit anyway if that happens. */
1387 trace_buffer_ctrl[curr]
1388 = trace_buffer_ctrl[prev_filtered];
1389 trace_debug ("trying curr=%u", curr);
1390#else
1391 /* The GDBserver's agent doesn't need all that syncing, and always
1392 updates TCB 0 (there's only one, mind you). */
1393 curr = 0;
1394#endif
1395 tbctrl = &trace_buffer_ctrl[curr];
1396
219f2f23
PA
1397 /* Offsets are easier to grok for debugging than raw addresses,
1398 especially for the small trace buffer sizes that are useful for
1399 testing. */
fa593d66
PA
1400 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1401 curr,
1402 (int) (tbctrl->start - trace_buffer_lo),
1403 (int) (tbctrl->free - trace_buffer_lo),
1404 (int) (tbctrl->end_free - trace_buffer_lo),
1405 (int) (tbctrl->wrap - trace_buffer_lo),
219f2f23
PA
1406 (int) (trace_buffer_hi - trace_buffer_lo));
1407
1408 /* The algorithm here is to keep trying to get a contiguous block of
1409 the requested size, possibly discarding older traceframes to free
1410 up space. Since free space might come in one or two pieces,
1411 depending on whether discarded traceframes wrapped around at the
1412 high end of the buffer, we test both pieces after each
1413 discard. */
1414 while (1)
1415 {
1416 /* First, if we have two free parts, try the upper one first. */
fa593d66 1417 if (tbctrl->end_free < tbctrl->free)
219f2f23 1418 {
fa593d66 1419 if (tbctrl->free + amt <= trace_buffer_hi)
219f2f23
PA
1420 /* We have enough in the upper part. */
1421 break;
1422 else
1423 {
1424 /* Our high part of free space wasn't enough. Give up
1425 on it for now, set wraparound. We will recover the
1426 space later, if/when the wrapped-around traceframe is
1427 discarded. */
1428 trace_debug ("Upper part too small, setting wraparound");
fa593d66
PA
1429 tbctrl->wrap = tbctrl->free;
1430 tbctrl->free = trace_buffer_lo;
219f2f23
PA
1431 }
1432 }
1433
1434 /* The normal case. */
fa593d66 1435 if (tbctrl->free + amt <= tbctrl->end_free)
219f2f23
PA
1436 break;
1437
fa593d66
PA
1438#ifdef IN_PROCESS_AGENT
1439 /* The IP Agent's buffer is always circular. It isn't used
1440 currently, but `circular_trace_buffer' could represent
1441 GDBserver's mode. If we didn't find space, ask GDBserver to
1442 flush. */
1443
1444 flush_trace_buffer ();
1445 memory_barrier ();
1446 if (tracing)
1447 {
1448 trace_debug ("gdbserver flushed buffer, retrying");
1449 goto again;
1450 }
1451
1452 /* GDBserver cancelled the tracing. Bail out as well. */
1453 return NULL;
1454#else
219f2f23
PA
1455 /* If we're here, then neither part is big enough, and
1456 non-circular trace buffers are now full. */
1457 if (!circular_trace_buffer)
1458 {
1459 trace_debug ("Not enough space in the trace buffer");
1460 return NULL;
1461 }
1462
1463 trace_debug ("Need more space in the trace buffer");
1464
1465 /* If we have a circular buffer, we can try discarding the
1466 oldest traceframe and see if that helps. */
1467 oldest = FIRST_TRACEFRAME ();
1468 if (oldest->tpnum == 0)
1469 {
1470 /* Not good; we have no traceframes to free. Perhaps we're
1471 asking for a block that is larger than the buffer? In
1472 any case, give up. */
1473 trace_debug ("No traceframes to discard");
1474 return NULL;
1475 }
1476
fa593d66
PA
1477 /* We don't run this code in the in-process agent currently.
1478 E.g., we could leave the in-process agent in autonomous
1479 circular mode if we only have fast tracepoints. If we do
1480 that, then this bit becomes racy with GDBserver, which also
1481 writes to this counter. */
219f2f23
PA
1482 --traceframe_write_count;
1483
1484 new_start = (unsigned char *) NEXT_TRACEFRAME (oldest);
1485 /* If we freed the traceframe that wrapped around, go back
1486 to the non-wrap case. */
fa593d66 1487 if (new_start < tbctrl->start)
219f2f23
PA
1488 {
1489 trace_debug ("Discarding past the wraparound");
fa593d66 1490 tbctrl->wrap = trace_buffer_hi;
219f2f23 1491 }
fa593d66
PA
1492 tbctrl->start = new_start;
1493 tbctrl->end_free = tbctrl->start;
219f2f23
PA
1494
1495 trace_debug ("Discarded a traceframe\n"
fa593d66
PA
1496 "Trace buffer [%d], start=%d free=%d "
1497 "endfree=%d wrap=%d hi=%d",
1498 curr,
1499 (int) (tbctrl->start - trace_buffer_lo),
1500 (int) (tbctrl->free - trace_buffer_lo),
1501 (int) (tbctrl->end_free - trace_buffer_lo),
1502 (int) (tbctrl->wrap - trace_buffer_lo),
219f2f23
PA
1503 (int) (trace_buffer_hi - trace_buffer_lo));
1504
1505 /* Now go back around the loop. The discard might have resulted
1506 in either one or two pieces of free space, so we want to try
1507 both before freeing any more traceframes. */
fa593d66 1508#endif
219f2f23
PA
1509 }
1510
1511 /* If we get here, we know we can provide the asked-for space. */
1512
fa593d66 1513 rslt = tbctrl->free;
219f2f23
PA
1514
1515 /* Adjust the request back down, now that we know we have space for
fa593d66
PA
1516 the marker, but don't commit to AMT yet, we may still need to
1517 restart the operation if GDBserver touches the trace buffer
1518 (obviously only important in the in-process agent's version). */
1519 tbctrl->free += (amt - sizeof (struct traceframe));
1520
1521 /* Or not. If GDBserver changed the trace buffer behind our back,
1522 we get to restart a new allocation attempt. */
1523
1524#ifdef IN_PROCESS_AGENT
1525 /* Build the tentative token. */
2ece8244
YQ
1526 commit_count = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) + 0x100)
1527 & GDBSERVER_FLUSH_COUNT_MASK_CURR);
1528 commit = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) << 12)
fa593d66
PA
1529 | commit_count
1530 | curr);
1531
1532 /* Try to commit it. */
1533 readout = cmpxchg (&trace_buffer_ctrl_curr, prev, commit);
1534 if (readout != prev)
1535 {
1536 trace_debug ("GDBserver has touched the trace buffer, restarting."
1537 " (prev=%08x, commit=%08x, readout=%08x)",
1538 prev, commit, readout);
1539 goto again;
1540 }
219f2f23 1541
fa593d66
PA
1542 /* Hold your horses here. Even if that change was committed,
1543 GDBserver could come in, and clobber it. We need to hold to be
1544 able to tell if GDBserver clobbers before or after we committed
1545 the change. Whenever GDBserver goes about touching the IPA
1546 buffer, it sets a breakpoint in this routine, so we have a sync
1547 point here. */
1548 about_to_request_buffer_space ();
219f2f23 1549
fa593d66
PA
1550 /* Check if the change has been effective, even if GDBserver stopped
1551 us at the breakpoint. */
219f2f23 1552
fa593d66
PA
1553 {
1554 unsigned int refetch;
219f2f23 1555
fa593d66
PA
1556 memory_barrier ();
1557
1558 refetch = trace_buffer_ctrl_curr;
1559
2ece8244
YQ
1560 if (refetch == commit
1561 || ((refetch & GDBSERVER_FLUSH_COUNT_MASK_PREV) >> 12) == commit_count)
fa593d66
PA
1562 {
1563 /* effective */
1564 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1565 "readout=%08x, refetch=%08x)",
1566 prev, commit, readout, refetch);
1567 }
1568 else
1569 {
1570 trace_debug ("GDBserver has touched the trace buffer, not effective."
1571 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1572 prev, commit, readout, refetch);
1573 goto again;
1574 }
1575 }
1576#endif
1577
1578 /* We have a new piece of the trace buffer. Hurray! */
1579
1580 /* Add an EOB marker just past this allocation. */
1581 ((struct traceframe *) tbctrl->free)->tpnum = 0;
1582 ((struct traceframe *) tbctrl->free)->data_size = 0;
1583
1584 /* Adjust the request back down, now that we know we have space for
1585 the marker. */
1586 amt -= sizeof (struct traceframe);
1587
1588 if (debug_threads)
1589 {
219f2f23 1590 trace_debug ("Allocated %d bytes", (int) amt);
fa593d66
PA
1591 trace_debug ("Trace buffer [%d] start=%d free=%d "
1592 "endfree=%d wrap=%d hi=%d",
1593 curr,
1594 (int) (tbctrl->start - trace_buffer_lo),
1595 (int) (tbctrl->free - trace_buffer_lo),
1596 (int) (tbctrl->end_free - trace_buffer_lo),
1597 (int) (tbctrl->wrap - trace_buffer_lo),
219f2f23
PA
1598 (int) (trace_buffer_hi - trace_buffer_lo));
1599 }
1600
1601 return rslt;
1602}
1603
fa593d66
PA
1604#ifndef IN_PROCESS_AGENT
1605
219f2f23
PA
1606/* Return the total free space. This is not necessarily the largest
1607 block we can allocate, because of the two-part case. */
1608
1609static int
1610free_space (void)
1611{
1612 if (trace_buffer_free <= trace_buffer_end_free)
1613 return trace_buffer_end_free - trace_buffer_free;
1614 else
1615 return ((trace_buffer_end_free - trace_buffer_lo)
1616 + (trace_buffer_hi - trace_buffer_free));
1617}
1618
1619/* An 'S' in continuation packets indicates remainder are for
1620 while-stepping. */
1621
1622static int seen_step_action_flag;
1623
1e4d1764
YQ
1624/* Create a tracepoint (location) with given number and address. Add this
1625 new tracepoint to list and sort this list. */
219f2f23
PA
1626
1627static struct tracepoint *
1628add_tracepoint (int num, CORE_ADDR addr)
1629{
1e4d1764 1630 struct tracepoint *tpoint, **tp_next;
219f2f23
PA
1631
1632 tpoint = xmalloc (sizeof (struct tracepoint));
1633 tpoint->number = num;
1634 tpoint->address = addr;
1635 tpoint->numactions = 0;
1636 tpoint->actions = NULL;
1637 tpoint->actions_str = NULL;
1638 tpoint->cond = NULL;
1639 tpoint->num_step_actions = 0;
1640 tpoint->step_actions = NULL;
1641 tpoint->step_actions_str = NULL;
fa593d66
PA
1642 /* Start all off as regular (slow) tracepoints. */
1643 tpoint->type = trap_tracepoint;
1644 tpoint->orig_size = -1;
219f2f23 1645 tpoint->source_strings = NULL;
6a271cae 1646 tpoint->compiled_cond = 0;
219f2f23
PA
1647 tpoint->handle = NULL;
1648 tpoint->next = NULL;
1649
1e4d1764
YQ
1650 /* Find a place to insert this tracepoint into list in order to keep
1651 the tracepoint list still in the ascending order. There may be
1652 multiple tracepoints at the same address as TPOINT's, and this
1653 guarantees TPOINT is inserted after all the tracepoints which are
1654 set at the same address. For example, fast tracepoints A, B, C are
1655 set at the same address, and D is to be insert at the same place as
1656 well,
1657
1658 -->| A |--> | B |-->| C |->...
1659
1660 One jump pad was created for tracepoint A, B, and C, and the target
1661 address of A is referenced/used in jump pad. So jump pad will let
1662 inferior jump to A. If D is inserted in front of A, like this,
1663
1664 -->| D |-->| A |--> | B |-->| C |->...
1665
1666 without updating jump pad, D is not reachable during collect, which
1667 is wrong. As we can see, the order of B, C and D doesn't matter, but
1668 A should always be the `first' one. */
1669 for (tp_next = &tracepoints;
1670 (*tp_next) != NULL && (*tp_next)->address <= tpoint->address;
1671 tp_next = &(*tp_next)->next)
1672 ;
1673 tpoint->next = *tp_next;
1674 *tp_next = tpoint;
219f2f23
PA
1675 last_tracepoint = tpoint;
1676
1677 seen_step_action_flag = 0;
1678
1679 return tpoint;
1680}
1681
fa593d66
PA
1682#ifndef IN_PROCESS_AGENT
1683
219f2f23
PA
1684/* Return the tracepoint with the given number and address, or NULL. */
1685
1686static struct tracepoint *
1687find_tracepoint (int id, CORE_ADDR addr)
1688{
1689 struct tracepoint *tpoint;
1690
1691 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
1692 if (tpoint->number == id && tpoint->address == addr)
1693 return tpoint;
1694
1695 return NULL;
1696}
1697
1698/* There may be several tracepoints with the same number (because they
1699 are "locations", in GDB parlance); return the next one after the
1700 given tracepoint, or search from the beginning of the list if the
1701 first argument is NULL. */
1702
1703static struct tracepoint *
1704find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num)
1705{
1706 struct tracepoint *tpoint;
1707
1708 if (prev_tp)
1709 tpoint = prev_tp->next;
1710 else
1711 tpoint = tracepoints;
1712 for (; tpoint; tpoint = tpoint->next)
1713 if (tpoint->number == num)
1714 return tpoint;
1715
1716 return NULL;
1717}
1718
fa593d66
PA
1719#endif
1720
219f2f23
PA
1721static char *
1722save_string (const char *str, size_t len)
1723{
1724 char *s;
1725
1726 s = xmalloc (len + 1);
1727 memcpy (s, str, len);
1728 s[len] = '\0';
1729
1730 return s;
1731}
1732
1733/* Append another action to perform when the tracepoint triggers. */
1734
1735static void
1736add_tracepoint_action (struct tracepoint *tpoint, char *packet)
1737{
1738 char *act;
1739
1740 if (*packet == 'S')
1741 {
1742 seen_step_action_flag = 1;
1743 ++packet;
1744 }
1745
1746 act = packet;
1747
1748 while (*act)
1749 {
1750 char *act_start = act;
1751 struct tracepoint_action *action = NULL;
1752
1753 switch (*act)
1754 {
1755 case 'M':
1756 {
1757 struct collect_memory_action *maction;
1758 ULONGEST basereg;
1759 int is_neg;
1760
1761 maction = xmalloc (sizeof *maction);
1762 maction->base.type = *act;
1763 action = &maction->base;
1764
1765 ++act;
1766 is_neg = (*act == '-');
1767 if (*act == '-')
1768 ++act;
1769 act = unpack_varlen_hex (act, &basereg);
1770 ++act;
1771 act = unpack_varlen_hex (act, &maction->addr);
1772 ++act;
1773 act = unpack_varlen_hex (act, &maction->len);
1774 maction->basereg = (is_neg
1775 ? - (int) basereg
1776 : (int) basereg);
1777 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1778 pulongest (maction->len),
1779 paddress (maction->addr), maction->basereg);
1780 break;
1781 }
1782 case 'R':
1783 {
1784 struct collect_registers_action *raction;
1785
1786 raction = xmalloc (sizeof *raction);
1787 raction->base.type = *act;
1788 action = &raction->base;
1789
1790 trace_debug ("Want to collect registers");
1791 ++act;
1792 /* skip past hex digits of mask for now */
1793 while (isxdigit(*act))
1794 ++act;
1795 break;
1796 }
0fb4aa4b
PA
1797 case 'L':
1798 {
1799 struct collect_static_trace_data_action *raction;
1800
1801 raction = xmalloc (sizeof *raction);
1802 raction->base.type = *act;
1803 action = &raction->base;
1804
1805 trace_debug ("Want to collect static trace data");
1806 ++act;
1807 break;
1808 }
219f2f23
PA
1809 case 'S':
1810 trace_debug ("Unexpected step action, ignoring");
1811 ++act;
1812 break;
1813 case 'X':
1814 {
1815 struct eval_expr_action *xaction;
1816
1817 xaction = xmalloc (sizeof (*xaction));
1818 xaction->base.type = *act;
1819 action = &xaction->base;
1820
1821 trace_debug ("Want to evaluate expression");
5e1dc496 1822 xaction->expr = gdb_parse_agent_expr (&act);
219f2f23
PA
1823 break;
1824 }
1825 default:
1826 trace_debug ("unknown trace action '%c', ignoring...", *act);
1827 break;
1828 case '-':
1829 break;
1830 }
1831
1832 if (action == NULL)
1833 break;
1834
1835 if (seen_step_action_flag)
1836 {
1837 tpoint->num_step_actions++;
1838
1839 tpoint->step_actions
1840 = xrealloc (tpoint->step_actions,
1841 (sizeof (*tpoint->step_actions)
1842 * tpoint->num_step_actions));
1843 tpoint->step_actions_str
1844 = xrealloc (tpoint->step_actions_str,
1845 (sizeof (*tpoint->step_actions_str)
1846 * tpoint->num_step_actions));
1847 tpoint->step_actions[tpoint->num_step_actions - 1] = action;
1848 tpoint->step_actions_str[tpoint->num_step_actions - 1]
1849 = save_string (act_start, act - act_start);
1850 }
1851 else
1852 {
1853 tpoint->numactions++;
1854 tpoint->actions
1855 = xrealloc (tpoint->actions,
1856 sizeof (*tpoint->actions) * tpoint->numactions);
1857 tpoint->actions_str
1858 = xrealloc (tpoint->actions_str,
1859 sizeof (*tpoint->actions_str) * tpoint->numactions);
1860 tpoint->actions[tpoint->numactions - 1] = action;
1861 tpoint->actions_str[tpoint->numactions - 1]
1862 = save_string (act_start, act - act_start);
1863 }
1864 }
1865}
1866
fa593d66
PA
1867#endif
1868
219f2f23
PA
1869/* Find or create a trace state variable with the given number. */
1870
1871static struct trace_state_variable *
1872get_trace_state_variable (int num)
1873{
1874 struct trace_state_variable *tsv;
1875
fa593d66
PA
1876#ifdef IN_PROCESS_AGENT
1877 /* Search for an existing variable. */
1878 for (tsv = alloced_trace_state_variables; tsv; tsv = tsv->next)
1879 if (tsv->number == num)
1880 return tsv;
1881#endif
1882
219f2f23
PA
1883 /* Search for an existing variable. */
1884 for (tsv = trace_state_variables; tsv; tsv = tsv->next)
1885 if (tsv->number == num)
1886 return tsv;
1887
1888 return NULL;
1889}
1890
1891/* Find or create a trace state variable with the given number. */
1892
1893static struct trace_state_variable *
fa593d66 1894create_trace_state_variable (int num, int gdb)
219f2f23
PA
1895{
1896 struct trace_state_variable *tsv;
1897
1898 tsv = get_trace_state_variable (num);
1899 if (tsv != NULL)
1900 return tsv;
1901
1902 /* Create a new variable. */
1903 tsv = xmalloc (sizeof (struct trace_state_variable));
1904 tsv->number = num;
1905 tsv->initial_value = 0;
1906 tsv->value = 0;
1907 tsv->getter = NULL;
1908 tsv->name = NULL;
fa593d66
PA
1909#ifdef IN_PROCESS_AGENT
1910 if (!gdb)
1911 {
1912 tsv->next = alloced_trace_state_variables;
1913 alloced_trace_state_variables = tsv;
1914 }
1915 else
1916#endif
1917 {
1918 tsv->next = trace_state_variables;
1919 trace_state_variables = tsv;
1920 }
219f2f23
PA
1921 return tsv;
1922}
1923
6a271cae 1924IP_AGENT_EXPORT LONGEST
219f2f23
PA
1925get_trace_state_variable_value (int num)
1926{
1927 struct trace_state_variable *tsv;
1928
1929 tsv = get_trace_state_variable (num);
1930
1931 if (!tsv)
1932 {
1933 trace_debug ("No trace state variable %d, skipping value get", num);
1934 return 0;
1935 }
1936
1937 /* Call a getter function if we have one. While it's tempting to
1938 set up something to only call the getter once per tracepoint hit,
1939 it could run afoul of thread races. Better to let the getter
1940 handle it directly, if necessary to worry about it. */
1941 if (tsv->getter)
1942 tsv->value = (tsv->getter) ();
1943
1944 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1945 num, plongest (tsv->value));
1946
1947 return tsv->value;
1948}
1949
6a271cae 1950IP_AGENT_EXPORT void
219f2f23
PA
1951set_trace_state_variable_value (int num, LONGEST val)
1952{
1953 struct trace_state_variable *tsv;
1954
1955 tsv = get_trace_state_variable (num);
1956
1957 if (!tsv)
1958 {
1959 trace_debug ("No trace state variable %d, skipping value set", num);
1960 return;
1961 }
1962
1963 tsv->value = val;
1964}
1965
5e1dc496
LM
1966LONGEST
1967agent_get_trace_state_variable_value (int num)
1968{
1969 return get_trace_state_variable_value (num);
1970}
1971
1972void
1973agent_set_trace_state_variable_value (int num, LONGEST val)
1974{
1975 set_trace_state_variable_value (num, val);
1976}
1977
219f2f23
PA
1978static void
1979set_trace_state_variable_name (int num, const char *name)
1980{
1981 struct trace_state_variable *tsv;
1982
1983 tsv = get_trace_state_variable (num);
1984
1985 if (!tsv)
1986 {
1987 trace_debug ("No trace state variable %d, skipping name set", num);
1988 return;
1989 }
1990
1991 tsv->name = (char *) name;
1992}
1993
1994static void
1995set_trace_state_variable_getter (int num, LONGEST (*getter) (void))
1996{
1997 struct trace_state_variable *tsv;
1998
1999 tsv = get_trace_state_variable (num);
2000
2001 if (!tsv)
2002 {
2003 trace_debug ("No trace state variable %d, skipping getter set", num);
2004 return;
2005 }
2006
2007 tsv->getter = getter;
2008}
2009
2010/* Add a raw traceframe for the given tracepoint. */
2011
2012static struct traceframe *
2013add_traceframe (struct tracepoint *tpoint)
2014{
2015 struct traceframe *tframe;
2016
2017 tframe = trace_buffer_alloc (sizeof (struct traceframe));
2018
2019 if (tframe == NULL)
2020 return NULL;
2021
2022 tframe->tpnum = tpoint->number;
2023 tframe->data_size = 0;
2024
2025 return tframe;
2026}
2027
2028/* Add a block to the traceframe currently being worked on. */
2029
2030static unsigned char *
2031add_traceframe_block (struct traceframe *tframe, int amt)
2032{
2033 unsigned char *block;
2034
2035 if (!tframe)
2036 return NULL;
2037
2038 block = trace_buffer_alloc (amt);
2039
2040 if (!block)
2041 return NULL;
2042
2043 tframe->data_size += amt;
2044
2045 return block;
2046}
2047
2048/* Flag that the current traceframe is finished. */
2049
2050static void
2051finish_traceframe (struct traceframe *tframe)
2052{
2053 ++traceframe_write_count;
2054 ++traceframes_created;
2055}
2056
fa593d66
PA
2057#ifndef IN_PROCESS_AGENT
2058
219f2f23
PA
2059/* Given a traceframe number NUM, find the NUMth traceframe in the
2060 buffer. */
2061
2062static struct traceframe *
2063find_traceframe (int num)
2064{
2065 struct traceframe *tframe;
2066 int tfnum = 0;
2067
2068 for (tframe = FIRST_TRACEFRAME ();
2069 tframe->tpnum != 0;
2070 tframe = NEXT_TRACEFRAME (tframe))
2071 {
2072 if (tfnum == num)
2073 return tframe;
2074 ++tfnum;
2075 }
2076
2077 return NULL;
2078}
2079
2080static CORE_ADDR
2081get_traceframe_address (struct traceframe *tframe)
2082{
2083 CORE_ADDR addr;
2084 struct tracepoint *tpoint;
2085
2086 addr = traceframe_get_pc (tframe);
2087
2088 if (addr)
2089 return addr;
2090
2091 /* Fallback strategy, will be incorrect for while-stepping frames
2092 and multi-location tracepoints. */
2093 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
2094 return tpoint->address;
2095}
2096
2097/* Search for the next traceframe whose address is inside or outside
2098 the given range. */
2099
2100static struct traceframe *
2101find_next_traceframe_in_range (CORE_ADDR lo, CORE_ADDR hi, int inside_p,
2102 int *tfnump)
2103{
2104 struct traceframe *tframe;
2105 CORE_ADDR tfaddr;
2106
2107 *tfnump = current_traceframe + 1;
2108 tframe = find_traceframe (*tfnump);
2109 /* The search is not supposed to wrap around. */
2110 if (!tframe)
2111 {
2112 *tfnump = -1;
2113 return NULL;
2114 }
2115
2116 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2117 {
2118 tfaddr = get_traceframe_address (tframe);
2119 if (inside_p
2120 ? (lo <= tfaddr && tfaddr <= hi)
2121 : (lo > tfaddr || tfaddr > hi))
2122 return tframe;
2123 ++*tfnump;
2124 }
2125
2126 *tfnump = -1;
2127 return NULL;
2128}
2129
2130/* Search for the next traceframe recorded by the given tracepoint.
2131 Note that for multi-location tracepoints, this will find whatever
2132 location appears first. */
2133
2134static struct traceframe *
2135find_next_traceframe_by_tracepoint (int num, int *tfnump)
2136{
2137 struct traceframe *tframe;
2138
2139 *tfnump = current_traceframe + 1;
2140 tframe = find_traceframe (*tfnump);
2141 /* The search is not supposed to wrap around. */
2142 if (!tframe)
2143 {
2144 *tfnump = -1;
2145 return NULL;
2146 }
2147
2148 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2149 {
2150 if (tframe->tpnum == num)
2151 return tframe;
2152 ++*tfnump;
2153 }
2154
2155 *tfnump = -1;
2156 return NULL;
2157}
2158
fa593d66
PA
2159#endif
2160
2161#ifndef IN_PROCESS_AGENT
2162
219f2f23
PA
2163/* Clear all past trace state. */
2164
2165static void
2166cmd_qtinit (char *packet)
2167{
2168 struct trace_state_variable *tsv, *prev, *next;
2169
2170 /* Make sure we don't try to read from a trace frame. */
2171 current_traceframe = -1;
2172
2173 trace_debug ("Initializing the trace");
2174
2175 clear_installed_tracepoints ();
2176 clear_readonly_regions ();
2177
2178 tracepoints = NULL;
2179 last_tracepoint = NULL;
2180
2181 /* Clear out any leftover trace state variables. Ones with target
2182 defined getters should be kept however. */
2183 prev = NULL;
2184 tsv = trace_state_variables;
2185 while (tsv)
2186 {
2187 trace_debug ("Looking at var %d", tsv->number);
2188 if (tsv->getter == NULL)
2189 {
2190 next = tsv->next;
2191 if (prev)
2192 prev->next = next;
2193 else
2194 trace_state_variables = next;
2195 trace_debug ("Deleting var %d", tsv->number);
2196 free (tsv);
2197 tsv = next;
2198 }
2199 else
2200 {
2201 prev = tsv;
2202 tsv = tsv->next;
2203 }
2204 }
2205
2206 clear_trace_buffer ();
fa593d66 2207 clear_inferior_trace_buffer ();
219f2f23
PA
2208
2209 write_ok (packet);
2210}
2211
0fb4aa4b
PA
2212/* Unprobe the UST marker at ADDRESS. */
2213
2214static void
2215unprobe_marker_at (CORE_ADDR address)
2216{
2fa291ac 2217 char cmd[IPA_CMD_BUF_SIZE];
0fb4aa4b
PA
2218
2219 sprintf (cmd, "unprobe_marker_at:%s", paddress (address));
2220 run_inferior_command (cmd);
2221}
2222
219f2f23
PA
2223/* Restore the program to its pre-tracing state. This routine may be called
2224 in error situations, so it needs to be careful about only restoring
2225 from known-valid bits. */
2226
2227static void
2228clear_installed_tracepoints (void)
2229{
2230 struct tracepoint *tpoint;
2231 struct tracepoint *prev_stpoint;
2232
7984d532
PA
2233 pause_all (1);
2234 cancel_breakpoints ();
2235
219f2f23
PA
2236 prev_stpoint = NULL;
2237
2238 /* Restore any bytes overwritten by tracepoints. */
2239 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2240 {
219f2f23
PA
2241 /* Catch the case where we might try to remove a tracepoint that
2242 was never actually installed. */
2243 if (tpoint->handle == NULL)
2244 {
2245 trace_debug ("Tracepoint %d at 0x%s was "
2246 "never installed, nothing to clear",
2247 tpoint->number, paddress (tpoint->address));
2248 continue;
2249 }
2250
fa593d66
PA
2251 switch (tpoint->type)
2252 {
2253 case trap_tracepoint:
2254 delete_breakpoint (tpoint->handle);
2255 break;
2256 case fast_tracepoint:
2257 delete_fast_tracepoint_jump (tpoint->handle);
2258 break;
0fb4aa4b
PA
2259 case static_tracepoint:
2260 if (prev_stpoint != NULL
2261 && prev_stpoint->address == tpoint->address)
2262 /* Nothing to do. We already unprobed a tracepoint set at
2263 this marker address (and there can only be one probe
2264 per marker). */
2265 ;
2266 else
2267 {
2268 unprobe_marker_at (tpoint->address);
2269 prev_stpoint = tpoint;
2270 }
2271 break;
fa593d66
PA
2272 }
2273
219f2f23
PA
2274 tpoint->handle = NULL;
2275 }
7984d532
PA
2276
2277 unpause_all (1);
219f2f23
PA
2278}
2279
2280/* Parse a packet that defines a tracepoint. */
2281
2282static void
2283cmd_qtdp (char *own_buf)
2284{
2285 int tppacket;
1e4d1764
YQ
2286 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2287 int trail_hyphen = 0;
219f2f23
PA
2288 ULONGEST num;
2289 ULONGEST addr;
2290 ULONGEST count;
2291 struct tracepoint *tpoint;
2292 char *actparm;
2293 char *packet = own_buf;
2294
2295 packet += strlen ("QTDP:");
2296
2297 /* A hyphen at the beginning marks a packet specifying actions for a
2298 tracepoint already supplied. */
2299 tppacket = 1;
2300 if (*packet == '-')
2301 {
2302 tppacket = 0;
2303 ++packet;
2304 }
2305 packet = unpack_varlen_hex (packet, &num);
2306 ++packet; /* skip a colon */
2307 packet = unpack_varlen_hex (packet, &addr);
2308 ++packet; /* skip a colon */
2309
2310 /* See if we already have this tracepoint. */
2311 tpoint = find_tracepoint (num, addr);
2312
2313 if (tppacket)
2314 {
2315 /* Duplicate tracepoints are never allowed. */
2316 if (tpoint)
2317 {
2318 trace_debug ("Tracepoint error: tracepoint %d"
2319 " at 0x%s already exists",
2320 (int) num, paddress (addr));
2321 write_enn (own_buf);
2322 return;
2323 }
2324
2325 tpoint = add_tracepoint (num, addr);
2326
2327 tpoint->enabled = (*packet == 'E');
2328 ++packet; /* skip 'E' */
2329 ++packet; /* skip a colon */
2330 packet = unpack_varlen_hex (packet, &count);
2331 tpoint->step_count = count;
2332 ++packet; /* skip a colon */
2333 packet = unpack_varlen_hex (packet, &count);
2334 tpoint->pass_count = count;
2335 /* See if we have any of the additional optional fields. */
2336 while (*packet == ':')
2337 {
2338 ++packet;
fa593d66
PA
2339 if (*packet == 'F')
2340 {
2341 tpoint->type = fast_tracepoint;
2342 ++packet;
2343 packet = unpack_varlen_hex (packet, &count);
2344 tpoint->orig_size = count;
2345 }
0fb4aa4b
PA
2346 else if (*packet == 'S')
2347 {
2348 tpoint->type = static_tracepoint;
2349 ++packet;
2350 }
fa593d66 2351 else if (*packet == 'X')
219f2f23
PA
2352 {
2353 actparm = (char *) packet;
5e1dc496 2354 tpoint->cond = gdb_parse_agent_expr (&actparm);
219f2f23
PA
2355 packet = actparm;
2356 }
2357 else if (*packet == '-')
2358 break;
2359 else if (*packet == '\0')
2360 break;
2361 else
2362 trace_debug ("Unknown optional tracepoint field");
2363 }
2364 if (*packet == '-')
1e4d1764
YQ
2365 {
2366 trail_hyphen = 1;
2367 trace_debug ("Also has actions\n");
2368 }
219f2f23 2369
fa593d66 2370 trace_debug ("Defined %stracepoint %d at 0x%s, "
219f2f23 2371 "enabled %d step %ld pass %ld",
fa593d66 2372 tpoint->type == fast_tracepoint ? "fast "
5e0a92a9 2373 : tpoint->type == static_tracepoint ? "static " : "",
fa593d66 2374 tpoint->number, paddress (tpoint->address), tpoint->enabled,
219f2f23
PA
2375 tpoint->step_count, tpoint->pass_count);
2376 }
2377 else if (tpoint)
2378 add_tracepoint_action (tpoint, packet);
2379 else
2380 {
2381 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2382 (int) num, paddress (addr));
2383 write_enn (own_buf);
2384 return;
2385 }
2386
1e4d1764
YQ
2387 /* Install tracepoint during tracing only once for each tracepoint location.
2388 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2389 determine the last QTDP packet for one tracepoint location by checking
2390 trailing hyphen in QTDP packet. */
2391 if (tracing && !trail_hyphen)
2392 {
2393 /* Pause all threads temporarily while we patch tracepoints. */
2394 pause_all (0);
2395
2396 /* download_tracepoint will update global `tracepoints'
2397 list, so it is unsafe to leave threads in jump pad. */
2398 stabilize_threads ();
2399
2400 /* Freeze threads. */
2401 pause_all (1);
2402
2403 download_tracepoint (tpoint);
2404 install_tracepoint (tpoint, own_buf);
2405
2406 unpause_all (1);
2407 return;
2408 }
2409
219f2f23
PA
2410 write_ok (own_buf);
2411}
2412
2413static void
2414cmd_qtdpsrc (char *own_buf)
2415{
2416 ULONGEST num, addr, start, slen;
2417 struct tracepoint *tpoint;
2418 char *packet = own_buf;
2419 char *saved, *srctype, *src;
2420 size_t nbytes;
2421 struct source_string *last, *newlast;
2422
2423 packet += strlen ("QTDPsrc:");
2424
2425 packet = unpack_varlen_hex (packet, &num);
2426 ++packet; /* skip a colon */
2427 packet = unpack_varlen_hex (packet, &addr);
2428 ++packet; /* skip a colon */
2429
2430 /* See if we already have this tracepoint. */
2431 tpoint = find_tracepoint (num, addr);
2432
2433 if (!tpoint)
2434 {
2435 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2436 (int) num, paddress (addr));
2437 write_enn (own_buf);
2438 return;
2439 }
2440
2441 saved = packet;
2442 packet = strchr (packet, ':');
2443 srctype = xmalloc (packet - saved + 1);
2444 memcpy (srctype, saved, packet - saved);
2445 srctype[packet - saved] = '\0';
2446 ++packet;
2447 packet = unpack_varlen_hex (packet, &start);
2448 ++packet; /* skip a colon */
2449 packet = unpack_varlen_hex (packet, &slen);
2450 ++packet; /* skip a colon */
2451 src = xmalloc (slen + 1);
2452 nbytes = unhexify (src, packet, strlen (packet) / 2);
2453 src[nbytes] = '\0';
2454
2455 newlast = xmalloc (sizeof (struct source_string));
2456 newlast->type = srctype;
2457 newlast->str = src;
2458 newlast->next = NULL;
2459 /* Always add a source string to the end of the list;
2460 this keeps sequences of actions/commands in the right
2461 order. */
2462 if (tpoint->source_strings)
2463 {
2464 for (last = tpoint->source_strings; last->next; last = last->next)
2465 ;
2466 last->next = newlast;
2467 }
2468 else
2469 tpoint->source_strings = newlast;
2470
2471 write_ok (own_buf);
2472}
2473
2474static void
2475cmd_qtdv (char *own_buf)
2476{
2477 ULONGEST num, val, builtin;
2478 char *varname;
2479 size_t nbytes;
2480 struct trace_state_variable *tsv;
2481 char *packet = own_buf;
2482
2483 packet += strlen ("QTDV:");
2484
2485 packet = unpack_varlen_hex (packet, &num);
2486 ++packet; /* skip a colon */
2487 packet = unpack_varlen_hex (packet, &val);
2488 ++packet; /* skip a colon */
2489 packet = unpack_varlen_hex (packet, &builtin);
2490 ++packet; /* skip a colon */
2491
2492 nbytes = strlen (packet) / 2;
2493 varname = xmalloc (nbytes + 1);
2494 nbytes = unhexify (varname, packet, nbytes);
2495 varname[nbytes] = '\0';
2496
fa593d66 2497 tsv = create_trace_state_variable (num, 1);
219f2f23
PA
2498 tsv->initial_value = (LONGEST) val;
2499 tsv->name = varname;
2500
2501 set_trace_state_variable_value (num, (LONGEST) val);
2502
2503 write_ok (own_buf);
2504}
2505
d248b706
KY
2506static void
2507cmd_qtenable_disable (char *own_buf, int enable)
2508{
2509 char *packet = own_buf;
2510 ULONGEST num, addr;
2511 struct tracepoint *tp;
2512
2513 packet += strlen (enable ? "QTEnable:" : "QTDisable:");
2514 packet = unpack_varlen_hex (packet, &num);
2515 ++packet; /* skip a colon */
2516 packet = unpack_varlen_hex (packet, &addr);
2517
2518 tp = find_tracepoint (num, addr);
2519
2520 if (tp)
2521 {
2522 if ((enable && tp->enabled) || (!enable && !tp->enabled))
2523 {
2524 trace_debug ("Tracepoint %d at 0x%s is already %s",
2525 (int) num, paddress (addr),
2526 enable ? "enabled" : "disabled");
2527 write_ok (own_buf);
2528 return;
2529 }
2530
2531 trace_debug ("%s tracepoint %d at 0x%s",
2532 enable ? "Enabling" : "Disabling",
2533 (int) num, paddress (addr));
2534
2535 tp->enabled = enable;
2536
2537 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
2538 {
2539 int ret;
2540 int offset = offsetof (struct tracepoint, enabled);
2541 CORE_ADDR obj_addr = tp->obj_addr_on_target + offset;
2542
2543 ret = prepare_to_access_memory ();
2544 if (ret)
2545 {
2546 trace_debug ("Failed to temporarily stop inferior threads");
2547 write_enn (own_buf);
2548 return;
2549 }
2550
2551 ret = write_inferior_integer (obj_addr, enable);
2552 done_accessing_memory ();
2553
2554 if (ret)
2555 {
2556 trace_debug ("Cannot write enabled flag into "
2557 "inferior process memory");
2558 write_enn (own_buf);
2559 return;
2560 }
2561 }
2562
2563 write_ok (own_buf);
2564 }
2565 else
2566 {
2567 trace_debug ("Tracepoint %d at 0x%s not found",
2568 (int) num, paddress (addr));
2569 write_enn (own_buf);
2570 }
2571}
2572
219f2f23
PA
2573static void
2574cmd_qtv (char *own_buf)
2575{
2576 ULONGEST num;
2577 LONGEST val;
2578 int err;
2579 char *packet = own_buf;
2580
2581 packet += strlen ("qTV:");
f8f67713 2582 unpack_varlen_hex (packet, &num);
219f2f23
PA
2583
2584 if (current_traceframe >= 0)
2585 {
2586 err = traceframe_read_tsv ((int) num, &val);
2587 if (err)
2588 {
2589 strcpy (own_buf, "U");
2590 return;
2591 }
2592 }
2593 /* Only make tsv's be undefined before the first trace run. After a
2594 trace run is over, the user might want to see the last value of
2595 the tsv, and it might not be available in a traceframe. */
2596 else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0)
2597 {
2598 strcpy (own_buf, "U");
2599 return;
2600 }
2601 else
2602 val = get_trace_state_variable_value (num);
2603
2604 sprintf (own_buf, "V%s", phex_nz (val, 0));
2605}
2606
2607/* Clear out the list of readonly regions. */
2608
2609static void
2610clear_readonly_regions (void)
2611{
2612 struct readonly_region *roreg;
2613
2614 while (readonly_regions)
2615 {
2616 roreg = readonly_regions;
2617 readonly_regions = readonly_regions->next;
2618 free (roreg);
2619 }
2620}
2621
2622/* Parse the collection of address ranges whose contents GDB believes
2623 to be unchanging and so can be read directly from target memory
2624 even while looking at a traceframe. */
2625
2626static void
2627cmd_qtro (char *own_buf)
2628{
2629 ULONGEST start, end;
2630 struct readonly_region *roreg;
2631 char *packet = own_buf;
2632
2633 trace_debug ("Want to mark readonly regions");
2634
2635 clear_readonly_regions ();
2636
2637 packet += strlen ("QTro");
2638
2639 while (*packet == ':')
2640 {
2641 ++packet; /* skip a colon */
2642 packet = unpack_varlen_hex (packet, &start);
2643 ++packet; /* skip a comma */
2644 packet = unpack_varlen_hex (packet, &end);
2645 roreg = xmalloc (sizeof (struct readonly_region));
2646 roreg->start = start;
2647 roreg->end = end;
2648 roreg->next = readonly_regions;
2649 readonly_regions = roreg;
2650 trace_debug ("Added readonly region from 0x%s to 0x%s",
2651 paddress (roreg->start), paddress (roreg->end));
2652 }
2653
2654 write_ok (own_buf);
2655}
2656
2657/* Test to see if the given range is in our list of readonly ranges.
2658 We only test for being entirely within a range, GDB is not going to
2659 send a single memory packet that spans multiple regions. */
2660
2661int
2662in_readonly_region (CORE_ADDR addr, ULONGEST length)
2663{
2664 struct readonly_region *roreg;
2665
2666 for (roreg = readonly_regions; roreg; roreg = roreg->next)
2667 if (roreg->start <= addr && (addr + length - 1) <= roreg->end)
2668 return 1;
2669
2670 return 0;
2671}
2672
fa593d66
PA
2673/* The maximum size of a jump pad entry. */
2674static const int max_jump_pad_size = 0x100;
2675
2676static CORE_ADDR gdb_jump_pad_head;
2677
2678/* Return the address of the next free jump space. */
2679
2680static CORE_ADDR
2681get_jump_space_head (void)
2682{
2683 if (gdb_jump_pad_head == 0)
2684 {
2685 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
2686 &gdb_jump_pad_head))
2687 fatal ("error extracting jump_pad_buffer");
2688 }
2689
2690 return gdb_jump_pad_head;
2691}
2692
2693/* Reserve USED bytes from the jump space. */
2694
2695static void
2696claim_jump_space (ULONGEST used)
2697{
2698 trace_debug ("claim_jump_space reserves %s bytes at %s",
2699 pulongest (used), paddress (gdb_jump_pad_head));
2700 gdb_jump_pad_head += used;
2701}
2702
405f8e94
SS
2703static CORE_ADDR trampoline_buffer_head = 0;
2704static CORE_ADDR trampoline_buffer_tail;
2705
2706/* Reserve USED bytes from the trampoline buffer and return the
2707 address of the start of the reserved space in TRAMPOLINE. Returns
2708 non-zero if the space is successfully claimed. */
2709
2710int
2711claim_trampoline_space (ULONGEST used, CORE_ADDR *trampoline)
2712{
2713 if (!trampoline_buffer_head)
2714 {
2715 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
2716 &trampoline_buffer_tail))
2717 {
2718 fatal ("error extracting trampoline_buffer");
2719 return 0;
2720 }
2721
2722 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2723 &trampoline_buffer_head))
2724 {
2725 fatal ("error extracting trampoline_buffer_end");
2726 return 0;
2727 }
2728 }
2729
2730 /* Start claiming space from the top of the trampoline space. If
2731 the space is located at the bottom of the virtual address space,
2732 this reduces the possibility that corruption will occur if a null
2733 pointer is used to write to memory. */
2734 if (trampoline_buffer_head - trampoline_buffer_tail < used)
2735 {
2736 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2737 pulongest (used));
2738 return 0;
2739 }
2740
2741 trampoline_buffer_head -= used;
2742
2743 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2744 pulongest (used), paddress (trampoline_buffer_head));
2745
2746 *trampoline = trampoline_buffer_head;
2747 return 1;
2748}
2749
2750/* Returns non-zero if there is space allocated for use in trampolines
2751 for fast tracepoints. */
2752
2753int
2754have_fast_tracepoint_trampoline_buffer (char *buf)
2755{
2756 CORE_ADDR trampoline_end, errbuf;
2757
2758 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2759 &trampoline_end))
2760 {
2761 fatal ("error extracting trampoline_buffer_end");
2762 return 0;
2763 }
2764
2765 if (buf)
2766 {
2767 buf[0] = '\0';
2768 strcpy (buf, "was claiming");
2769 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_error,
2770 &errbuf))
2771 {
2772 fatal ("error extracting errbuf");
2773 return 0;
2774 }
2775
2776 read_inferior_memory (errbuf, (unsigned char *) buf, 100);
2777 }
2778
2779 return trampoline_end != 0;
2780}
2781
0fb4aa4b
PA
2782/* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2783 the command fails, or 0 otherwise. If the command ran
2784 successfully, but probing the marker failed, ERROUT will be filled
2785 with the error to reply to GDB, and -1 is also returned. This
2786 allows directly passing IPA errors to GDB. */
2787
2788static int
2789probe_marker_at (CORE_ADDR address, char *errout)
2790{
2fa291ac 2791 char cmd[IPA_CMD_BUF_SIZE];
0fb4aa4b
PA
2792 int err;
2793
2794 sprintf (cmd, "probe_marker_at:%s", paddress (address));
2795 err = run_inferior_command (cmd);
2796
2797 if (err == 0)
2798 {
2799 if (*cmd == 'E')
2800 {
2801 strcpy (errout, cmd);
2802 return -1;
2803 }
2804 }
2805
2806 return err;
2807}
2808
219f2f23 2809static void
5c73ff4e 2810clone_fast_tracepoint (struct tracepoint *to, const struct tracepoint *from)
219f2f23 2811{
5c73ff4e
YQ
2812 to->jump_pad = from->jump_pad;
2813 to->jump_pad_end = from->jump_pad_end;
405f8e94
SS
2814 to->trampoline = from->trampoline;
2815 to->trampoline_end = from->trampoline_end;
5c73ff4e
YQ
2816 to->adjusted_insn_addr = from->adjusted_insn_addr;
2817 to->adjusted_insn_addr_end = from->adjusted_insn_addr_end;
2818 to->handle = from->handle;
fa593d66 2819
5c73ff4e
YQ
2820 gdb_assert (from->handle);
2821 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump *) from->handle);
2822}
2823
2824#define MAX_JUMP_SIZE 20
2825
2826/* Install fast tracepoint. Return 0 if successful, otherwise return
2827 non-zero. */
2828
2829static int
405f8e94 2830install_fast_tracepoint (struct tracepoint *tpoint, char *errbuf)
5c73ff4e
YQ
2831{
2832 CORE_ADDR jentry, jump_entry;
405f8e94
SS
2833 CORE_ADDR trampoline;
2834 ULONGEST trampoline_size;
5c73ff4e 2835 int err = 0;
fa593d66
PA
2836 /* The jump to the jump pad of the last fast tracepoint
2837 installed. */
2838 unsigned char fjump[MAX_JUMP_SIZE];
2839 ULONGEST fjump_size;
219f2f23 2840
405f8e94
SS
2841 if (tpoint->orig_size < target_get_min_fast_tracepoint_insn_len ())
2842 {
2843 trace_debug ("Requested a fast tracepoint on an instruction "
2844 "that is of less than the minimum length.");
2845 return 0;
2846 }
2847
5c73ff4e
YQ
2848 jentry = jump_entry = get_jump_space_head ();
2849
405f8e94
SS
2850 trampoline = 0;
2851 trampoline_size = 0;
2852
5c73ff4e
YQ
2853 /* Install the jump pad. */
2854 err = install_fast_tracepoint_jump_pad (tpoint->obj_addr_on_target,
2855 tpoint->address,
2856 ipa_sym_addrs.addr_gdb_collect,
2857 ipa_sym_addrs.addr_collecting,
2858 tpoint->orig_size,
405f8e94
SS
2859 &jentry,
2860 &trampoline, &trampoline_size,
2861 fjump, &fjump_size,
5c73ff4e 2862 &tpoint->adjusted_insn_addr,
405f8e94
SS
2863 &tpoint->adjusted_insn_addr_end,
2864 errbuf);
5c73ff4e
YQ
2865
2866 if (err)
2867 return 1;
2868
2869 /* Wire it in. */
2870 tpoint->handle = set_fast_tracepoint_jump (tpoint->address, fjump,
2871 fjump_size);
2872
2873 if (tpoint->handle != NULL)
2874 {
2875 tpoint->jump_pad = jump_entry;
2876 tpoint->jump_pad_end = jentry;
405f8e94
SS
2877 tpoint->trampoline = trampoline;
2878 tpoint->trampoline_end = trampoline + trampoline_size;
5c73ff4e
YQ
2879
2880 /* Pad to 8-byte alignment. */
2881 jentry = ((jentry + 7) & ~0x7);
2882 claim_jump_space (jentry - jump_entry);
2883 }
2884
2885 return 0;
2886}
2887
1e4d1764
YQ
2888
2889/* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
2890
2891static void
2892install_tracepoint (struct tracepoint *tpoint, char *own_buf)
2893{
2894 tpoint->handle = NULL;
2895 *own_buf = '\0';
2896
2897 if (tpoint->type == trap_tracepoint)
2898 {
2899 /* Tracepoints are installed as memory breakpoints. Just go
2900 ahead and install the trap. The breakpoints module
2901 handles duplicated breakpoints, and the memory read
2902 routine handles un-patching traps from memory reads. */
2903 tpoint->handle = set_breakpoint_at (tpoint->address,
2904 tracepoint_handler);
2905 }
2906 else if (tpoint->type == fast_tracepoint || tpoint->type == static_tracepoint)
2907 {
2908 struct tracepoint *tp;
2909
2910 if (!in_process_agent_loaded ())
2911 {
2912 trace_debug ("Requested a %s tracepoint, but fast "
2913 "tracepoints aren't supported.",
2914 tpoint->type == static_tracepoint ? "static" : "fast");
2915 write_e_ipa_not_loaded (own_buf);
2916 return;
2917 }
8ffcbaaf
YQ
2918 if (tpoint->type == static_tracepoint
2919 && !in_process_agent_supports_ust ())
1e4d1764
YQ
2920 {
2921 trace_debug ("Requested a static tracepoint, but static "
2922 "tracepoints are not supported.");
2923 write_e_ust_not_loaded (own_buf);
2924 return;
2925 }
2926
2927 /* Find another fast or static tracepoint at the same address. */
2928 for (tp = tracepoints; tp; tp = tp->next)
2929 {
2930 if (tp->address == tpoint->address && tp->type == tpoint->type
2931 && tp->number != tpoint->number)
2932 break;
2933 }
2934
2935 if (tpoint->type == fast_tracepoint)
2936 {
2937 if (tp) /* TPOINT is installed at the same address as TP. */
2938 clone_fast_tracepoint (tpoint, tp);
2939 else
405f8e94 2940 install_fast_tracepoint (tpoint, own_buf);
1e4d1764
YQ
2941 }
2942 else
2943 {
2944 if (tp)
2945 tpoint->handle = (void *) -1;
2946 else
2947 {
2948 if (probe_marker_at (tpoint->address, own_buf) == 0)
2949 tpoint->handle = (void *) -1;
2950 }
2951 }
2952
2953 }
2954 else
2955 internal_error (__FILE__, __LINE__, "Unknown tracepoint type");
2956
2957 if (tpoint->handle == NULL)
2958 {
2959 if (*own_buf == '\0')
2960 write_enn (own_buf);
2961 }
2962 else
2963 write_ok (own_buf);
2964}
2965
5c73ff4e
YQ
2966static void
2967cmd_qtstart (char *packet)
2968{
2969 struct tracepoint *tpoint, *prev_ftpoint, *prev_stpoint;
2970
219f2f23
PA
2971 trace_debug ("Starting the trace");
2972
7984d532 2973 /* Pause all threads temporarily while we patch tracepoints. */
fa593d66
PA
2974 pause_all (0);
2975
2976 /* Get threads out of jump pads. Safe to do here, since this is a
2977 top level command. And, required to do here, since we're
2978 deleting/rewriting jump pads. */
2979
2980 stabilize_threads ();
2981
2982 /* Freeze threads. */
7984d532
PA
2983 pause_all (1);
2984
fa593d66
PA
2985 /* Sync the fast tracepoints list in the inferior ftlib. */
2986 if (in_process_agent_loaded ())
2987 {
2988 download_tracepoints ();
2989 download_trace_state_variables ();
2990 }
2991
2992 /* No previous fast tpoint yet. */
2993 prev_ftpoint = NULL;
2994
0fb4aa4b
PA
2995 /* No previous static tpoint yet. */
2996 prev_stpoint = NULL;
2997
fa593d66
PA
2998 *packet = '\0';
2999
219f2f23
PA
3000 /* Install tracepoints. */
3001 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
3002 {
3003 /* Ensure all the hit counts start at zero. */
3004 tpoint->hit_count = 0;
f196051f 3005 tpoint->traceframe_usage = 0;
219f2f23 3006
fa593d66
PA
3007 if (tpoint->type == trap_tracepoint)
3008 {
fa593d66
PA
3009 /* Tracepoints are installed as memory breakpoints. Just go
3010 ahead and install the trap. The breakpoints module
3011 handles duplicated breakpoints, and the memory read
3012 routine handles un-patching traps from memory reads. */
3013 tpoint->handle = set_breakpoint_at (tpoint->address,
3014 tracepoint_handler);
3015 }
3016 else if (tpoint->type == fast_tracepoint)
3017 {
fa593d66
PA
3018 if (maybe_write_ipa_not_loaded (packet))
3019 {
3020 trace_debug ("Requested a fast tracepoint, but fast "
3021 "tracepoints aren't supported.");
3022 break;
3023 }
3024
3025 if (prev_ftpoint != NULL && prev_ftpoint->address == tpoint->address)
5c73ff4e 3026 clone_fast_tracepoint (tpoint, prev_ftpoint);
fa593d66
PA
3027 else
3028 {
405f8e94 3029 if (install_fast_tracepoint (tpoint, packet) == 0)
5c73ff4e 3030 prev_ftpoint = tpoint;
fa593d66
PA
3031 }
3032 }
0fb4aa4b
PA
3033 else if (tpoint->type == static_tracepoint)
3034 {
3035 if (maybe_write_ipa_ust_not_loaded (packet))
3036 {
3037 trace_debug ("Requested a static tracepoint, but static "
3038 "tracepoints are not supported.");
3039 break;
3040 }
3041
3042 /* Can only probe a given marker once. */
3043 if (prev_stpoint != NULL && prev_stpoint->address == tpoint->address)
3044 {
3045 tpoint->handle = (void *) -1;
3046 }
3047 else
3048 {
3049 if (probe_marker_at (tpoint->address, packet) == 0)
3050 {
3051 tpoint->handle = (void *) -1;
3052
3053 /* So that we can handle multiple static tracepoints
3054 at the same address easily. */
3055 prev_stpoint = tpoint;
3056 }
3057 }
3058 }
fa593d66
PA
3059
3060 /* Any failure in the inner loop is sufficient cause to give
3061 up. */
219f2f23
PA
3062 if (tpoint->handle == NULL)
3063 break;
3064 }
3065
3066 /* Any error in tracepoint insertion is unacceptable; better to
3067 address the problem now, than end up with a useless or misleading
3068 trace run. */
3069 if (tpoint != NULL)
3070 {
3071 clear_installed_tracepoints ();
3072 if (*packet == '\0')
3073 write_enn (packet);
7984d532 3074 unpause_all (1);
219f2f23
PA
3075 return;
3076 }
3077
3078 stopping_tracepoint = NULL;
3079 trace_buffer_is_full = 0;
3080 expr_eval_result = expr_eval_no_error;
3081 error_tracepoint = NULL;
f196051f 3082 tracing_start_time = get_timestamp ();
219f2f23
PA
3083
3084 /* Tracing is now active, hits will now start being logged. */
3085 tracing = 1;
3086
fa593d66
PA
3087 if (in_process_agent_loaded ())
3088 {
3089 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 1))
3090 fatal ("Error setting tracing variable in lib");
3091
3092 if (write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
3093 0))
3094 fatal ("Error clearing stopping_tracepoint variable in lib");
3095
3096 if (write_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 0))
3097 fatal ("Error clearing trace_buffer_is_full variable in lib");
3098
3099 stop_tracing_bkpt = set_breakpoint_at (ipa_sym_addrs.addr_stop_tracing,
3100 stop_tracing_handler);
3101 if (stop_tracing_bkpt == NULL)
3102 error ("Error setting stop_tracing breakpoint");
3103
3104 flush_trace_buffer_bkpt
3105 = set_breakpoint_at (ipa_sym_addrs.addr_flush_trace_buffer,
3106 flush_trace_buffer_handler);
3107 if (flush_trace_buffer_bkpt == NULL)
3108 error ("Error setting flush_trace_buffer breakpoint");
3109 }
3110
7984d532
PA
3111 unpause_all (1);
3112
219f2f23
PA
3113 write_ok (packet);
3114}
3115
3116/* End a tracing run, filling in a stop reason to report back to GDB,
3117 and removing the tracepoints from the code. */
3118
8336d594 3119void
219f2f23
PA
3120stop_tracing (void)
3121{
3122 if (!tracing)
3123 {
3124 trace_debug ("Tracing is already off, ignoring");
3125 return;
3126 }
3127
3128 trace_debug ("Stopping the trace");
3129
fa593d66
PA
3130 /* Pause all threads before removing fast jumps from memory,
3131 breakpoints, and touching IPA state variables (inferior memory).
3132 Some thread may hit the internal tracing breakpoints, or be
3133 collecting this moment, but that's ok, we don't release the
3134 tpoint object's memory or the jump pads here (we only do that
3135 when we're sure we can move all threads out of the jump pads).
3136 We can't now, since we may be getting here due to the inferior
3137 agent calling us. */
7984d532
PA
3138 pause_all (1);
3139 /* Since we're removing breakpoints, cancel breakpoint hits,
3140 possibly related to the breakpoints we're about to delete. */
3141 cancel_breakpoints ();
3142
219f2f23
PA
3143 /* Stop logging. Tracepoints can still be hit, but they will not be
3144 recorded. */
3145 tracing = 0;
fa593d66
PA
3146 if (in_process_agent_loaded ())
3147 {
3148 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 0))
3149 fatal ("Error clearing tracing variable in lib");
3150 }
219f2f23 3151
f196051f 3152 tracing_stop_time = get_timestamp ();
219f2f23
PA
3153 tracing_stop_reason = "t???";
3154 tracing_stop_tpnum = 0;
3155 if (stopping_tracepoint)
3156 {
3157 trace_debug ("Stopping the trace because "
3158 "tracepoint %d was hit %ld times",
3159 stopping_tracepoint->number,
3160 stopping_tracepoint->pass_count);
3161 tracing_stop_reason = "tpasscount";
3162 tracing_stop_tpnum = stopping_tracepoint->number;
3163 }
3164 else if (trace_buffer_is_full)
3165 {
3166 trace_debug ("Stopping the trace because the trace buffer is full");
3167 tracing_stop_reason = "tfull";
3168 }
3169 else if (expr_eval_result != expr_eval_no_error)
3170 {
3171 trace_debug ("Stopping the trace because of an expression eval error");
3172 tracing_stop_reason = eval_result_names[expr_eval_result];
3173 tracing_stop_tpnum = error_tracepoint->number;
3174 }
fa593d66 3175#ifndef IN_PROCESS_AGENT
8336d594
PA
3176 else if (!gdb_connected ())
3177 {
3178 trace_debug ("Stopping the trace because GDB disconnected");
3179 tracing_stop_reason = "tdisconnected";
3180 }
fa593d66 3181#endif
219f2f23
PA
3182 else
3183 {
3184 trace_debug ("Stopping the trace because of a tstop command");
3185 tracing_stop_reason = "tstop";
3186 }
3187
3188 stopping_tracepoint = NULL;
3189 error_tracepoint = NULL;
3190
3191 /* Clear out the tracepoints. */
3192 clear_installed_tracepoints ();
7984d532 3193
fa593d66
PA
3194 if (in_process_agent_loaded ())
3195 {
3196 /* Pull in fast tracepoint trace frames from the inferior lib
3197 buffer into our buffer, even if our buffer is already full,
3198 because we want to present the full number of created frames
3199 in addition to what fit in the trace buffer. */
3200 upload_fast_traceframes ();
3201 }
3202
3203 if (stop_tracing_bkpt != NULL)
3204 {
3205 delete_breakpoint (stop_tracing_bkpt);
3206 stop_tracing_bkpt = NULL;
3207 }
3208
3209 if (flush_trace_buffer_bkpt != NULL)
3210 {
3211 delete_breakpoint (flush_trace_buffer_bkpt);
3212 flush_trace_buffer_bkpt = NULL;
3213 }
3214
7984d532 3215 unpause_all (1);
219f2f23
PA
3216}
3217
fa593d66
PA
3218static int
3219stop_tracing_handler (CORE_ADDR addr)
3220{
3221 trace_debug ("lib hit stop_tracing");
3222
3223 /* Don't actually handle it here. When we stop tracing we remove
3224 breakpoints from the inferior, and that is not allowed in a
3225 breakpoint handler (as the caller is walking the breakpoint
3226 list). */
3227 return 0;
3228}
3229
3230static int
3231flush_trace_buffer_handler (CORE_ADDR addr)
3232{
3233 trace_debug ("lib hit flush_trace_buffer");
3234 return 0;
3235}
3236
219f2f23
PA
3237static void
3238cmd_qtstop (char *packet)
3239{
3240 stop_tracing ();
3241 write_ok (packet);
3242}
3243
8336d594
PA
3244static void
3245cmd_qtdisconnected (char *own_buf)
3246{
3247 ULONGEST setting;
3248 char *packet = own_buf;
3249
3250 packet += strlen ("QTDisconnected:");
3251
3252 unpack_varlen_hex (packet, &setting);
3253
3254 write_ok (own_buf);
3255
3256 disconnected_tracing = setting;
3257}
3258
219f2f23
PA
3259static void
3260cmd_qtframe (char *own_buf)
3261{
3262 ULONGEST frame, pc, lo, hi, num;
3263 int tfnum, tpnum;
3264 struct traceframe *tframe;
3265 char *packet = own_buf;
3266
3267 packet += strlen ("QTFrame:");
3268
3269 if (strncmp (packet, "pc:", strlen ("pc:")) == 0)
3270 {
3271 packet += strlen ("pc:");
f8f67713 3272 unpack_varlen_hex (packet, &pc);
219f2f23
PA
3273 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc));
3274 tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum);
3275 }
3276 else if (strncmp (packet, "range:", strlen ("range:")) == 0)
3277 {
3278 packet += strlen ("range:");
3279 packet = unpack_varlen_hex (packet, &lo);
3280 ++packet;
f8f67713 3281 unpack_varlen_hex (packet, &hi);
219f2f23
PA
3282 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3283 paddress (lo), paddress (hi));
3284 tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum);
3285 }
3286 else if (strncmp (packet, "outside:", strlen ("outside:")) == 0)
3287 {
3288 packet += strlen ("outside:");
3289 packet = unpack_varlen_hex (packet, &lo);
3290 ++packet;
f8f67713 3291 unpack_varlen_hex (packet, &hi);
219f2f23
PA
3292 trace_debug ("Want to find next traceframe "
3293 "outside the range 0x%s to 0x%s",
3294 paddress (lo), paddress (hi));
3295 tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum);
3296 }
3297 else if (strncmp (packet, "tdp:", strlen ("tdp:")) == 0)
3298 {
3299 packet += strlen ("tdp:");
f8f67713 3300 unpack_varlen_hex (packet, &num);
219f2f23
PA
3301 tpnum = (int) num;
3302 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum);
3303 tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum);
3304 }
3305 else
3306 {
3307 unpack_varlen_hex (packet, &frame);
3308 tfnum = (int) frame;
3309 if (tfnum == -1)
3310 {
3311 trace_debug ("Want to stop looking at traceframes");
3312 current_traceframe = -1;
3313 write_ok (own_buf);
3314 return;
3315 }
3316 trace_debug ("Want to look at traceframe %d", tfnum);
3317 tframe = find_traceframe (tfnum);
3318 }
3319
3320 if (tframe)
3321 {
3322 current_traceframe = tfnum;
3323 sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum);
3324 }
3325 else
3326 sprintf (own_buf, "F-1");
3327}
3328
3329static void
3330cmd_qtstatus (char *packet)
3331{
3332 char *stop_reason_rsp = NULL;
f196051f
SS
3333 char *buf1, *buf2, *buf3, *str;
3334 int slen;
3335
3336 /* Translate the plain text of the notes back into hex for
3337 transmission. */
3338
3339 str = (tracing_user_name ? tracing_user_name : "");
3340 slen = strlen (str);
3341 buf1 = (char *) alloca (slen * 2 + 1);
3342 hexify (buf1, str, slen);
3343
3344 str = (tracing_notes ? tracing_notes : "");
3345 slen = strlen (str);
3346 buf2 = (char *) alloca (slen * 2 + 1);
3347 hexify (buf2, str, slen);
3348
3349 str = (tracing_stop_note ? tracing_stop_note : "");
3350 slen = strlen (str);
3351 buf3 = (char *) alloca (slen * 2 + 1);
3352 hexify (buf3, str, slen);
219f2f23
PA
3353
3354 trace_debug ("Returning trace status as %d, stop reason %s",
3355 tracing, tracing_stop_reason);
3356
fa593d66
PA
3357 if (in_process_agent_loaded ())
3358 {
3359 pause_all (1);
3360
3361 upload_fast_traceframes ();
3362
3363 unpause_all (1);
3364 }
3365
219f2f23
PA
3366 stop_reason_rsp = (char *) tracing_stop_reason;
3367
3368 /* The user visible error string in terror needs to be hex encoded.
f196051f 3369 We leave it as plain string in `tracing_stop_reason' to ease
219f2f23
PA
3370 debugging. */
3371 if (strncmp (stop_reason_rsp, "terror:", strlen ("terror:")) == 0)
3372 {
3373 const char *result_name;
3374 int hexstr_len;
3375 char *p;
3376
3377 result_name = stop_reason_rsp + strlen ("terror:");
3378 hexstr_len = strlen (result_name) * 2;
3379 p = stop_reason_rsp = alloca (strlen ("terror:") + hexstr_len + 1);
3380 strcpy (p, "terror:");
3381 p += strlen (p);
3382 convert_int_to_ascii ((gdb_byte *) result_name, p, strlen (result_name));
3383 }
3384
f196051f
SS
3385 /* If this was a forced stop, include any stop note that was supplied. */
3386 if (strcmp (stop_reason_rsp, "tstop") == 0)
3387 {
3388 stop_reason_rsp = alloca (strlen ("tstop:") + strlen (buf3) + 1);
3389 strcpy (stop_reason_rsp, "tstop:");
3390 strcat (stop_reason_rsp, buf3);
3391 }
3392
8336d594
PA
3393 sprintf (packet,
3394 "T%d;"
3395 "%s:%x;"
3396 "tframes:%x;tcreated:%x;"
3397 "tfree:%x;tsize:%s;"
3398 "circular:%d;"
f196051f 3399 "disconn:%d;"
242f5f1c 3400 "starttime:%s;stoptime:%s;"
f196051f 3401 "username:%s:;notes:%s:",
623ccd72 3402 tracing ? 1 : 0,
219f2f23
PA
3403 stop_reason_rsp, tracing_stop_tpnum,
3404 traceframe_count, traceframes_created,
8336d594
PA
3405 free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
3406 circular_trace_buffer,
f196051f 3407 disconnected_tracing,
242f5f1c 3408 plongest (tracing_start_time), plongest (tracing_stop_time),
f196051f
SS
3409 buf1, buf2);
3410}
3411
3412static void
3413cmd_qtp (char *own_buf)
3414{
3415 ULONGEST num, addr;
3416 struct tracepoint *tpoint;
3417 char *packet = own_buf;
3418
3419 packet += strlen ("qTP:");
3420
3421 packet = unpack_varlen_hex (packet, &num);
3422 ++packet; /* skip a colon */
3423 packet = unpack_varlen_hex (packet, &addr);
3424
3425 /* See if we already have this tracepoint. */
3426 tpoint = find_tracepoint (num, addr);
3427
3428 if (!tpoint)
3429 {
3430 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3431 (int) num, paddress (addr));
3432 write_enn (own_buf);
3433 return;
3434 }
3435
3436 sprintf (own_buf, "V%lx:%lx", tpoint->hit_count, tpoint->traceframe_usage);
219f2f23
PA
3437}
3438
3439/* State variables to help return all the tracepoint bits. */
3440static struct tracepoint *cur_tpoint;
3441static int cur_action;
3442static int cur_step_action;
3443static struct source_string *cur_source_string;
3444static struct trace_state_variable *cur_tsv;
3445
3446/* Compose a response that is an imitation of the syntax by which the
3447 tracepoint was originally downloaded. */
3448
3449static void
3450response_tracepoint (char *packet, struct tracepoint *tpoint)
3451{
3452 char *buf;
3453
3454 sprintf (packet, "T%x:%s:%c:%lx:%lx", tpoint->number,
3455 paddress (tpoint->address),
3456 (tpoint->enabled ? 'E' : 'D'), tpoint->step_count,
3457 tpoint->pass_count);
fa593d66
PA
3458 if (tpoint->type == fast_tracepoint)
3459 sprintf (packet + strlen (packet), ":F%x", tpoint->orig_size);
0fb4aa4b
PA
3460 else if (tpoint->type == static_tracepoint)
3461 sprintf (packet + strlen (packet), ":S");
219f2f23
PA
3462
3463 if (tpoint->cond)
3464 {
5e1dc496 3465 buf = gdb_unparse_agent_expr (tpoint->cond);
219f2f23
PA
3466 sprintf (packet + strlen (packet), ":X%x,%s",
3467 tpoint->cond->length, buf);
3468 free (buf);
3469 }
3470}
3471
3472/* Compose a response that is an imitation of the syntax by which the
3473 tracepoint action was originally downloaded (with the difference
3474 that due to the way we store the actions, this will output a packet
3475 per action, while GDB could have combined more than one action
3476 per-packet. */
3477
3478static void
3479response_action (char *packet, struct tracepoint *tpoint,
3480 char *taction, int step)
3481{
3482 sprintf (packet, "%c%x:%s:%s",
3483 (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address),
3484 taction);
3485}
3486
3487/* Compose a response that is an imitation of the syntax by which the
3488 tracepoint source piece was originally downloaded. */
3489
3490static void
3491response_source (char *packet,
3492 struct tracepoint *tpoint, struct source_string *src)
3493{
3494 char *buf;
3495 int len;
3496
3497 len = strlen (src->str);
3498 buf = alloca (len * 2 + 1);
3499 convert_int_to_ascii ((gdb_byte *) src->str, buf, len);
3500
3501 sprintf (packet, "Z%x:%s:%s:%x:%x:%s",
3502 tpoint->number, paddress (tpoint->address),
3503 src->type, 0, len, buf);
3504}
3505
3506/* Return the first piece of tracepoint definition, and initialize the
3507 state machine that will iterate through all the tracepoint
3508 bits. */
3509
3510static void
3511cmd_qtfp (char *packet)
3512{
3513 trace_debug ("Returning first tracepoint definition piece");
3514
3515 cur_tpoint = tracepoints;
3516 cur_action = cur_step_action = -1;
3517 cur_source_string = NULL;
3518
3519 if (cur_tpoint)
3520 response_tracepoint (packet, cur_tpoint);
3521 else
3522 strcpy (packet, "l");
3523}
3524
3525/* Return additional pieces of tracepoint definition. Each action and
3526 stepping action must go into its own packet, because of packet size
3527 limits, and so we use state variables to deliver one piece at a
3528 time. */
3529
3530static void
3531cmd_qtsp (char *packet)
3532{
3533 trace_debug ("Returning subsequent tracepoint definition piece");
3534
3535 if (!cur_tpoint)
3536 {
3537 /* This case would normally never occur, but be prepared for
3538 GDB misbehavior. */
3539 strcpy (packet, "l");
3540 }
3541 else if (cur_action < cur_tpoint->numactions - 1)
3542 {
3543 ++cur_action;
3544 response_action (packet, cur_tpoint,
3545 cur_tpoint->actions_str[cur_action], 0);
3546 }
3547 else if (cur_step_action < cur_tpoint->num_step_actions - 1)
3548 {
3549 ++cur_step_action;
3550 response_action (packet, cur_tpoint,
3551 cur_tpoint->step_actions_str[cur_step_action], 1);
3552 }
3553 else if ((cur_source_string
3554 ? cur_source_string->next
3555 : cur_tpoint->source_strings))
3556 {
3557 if (cur_source_string)
3558 cur_source_string = cur_source_string->next;
3559 else
3560 cur_source_string = cur_tpoint->source_strings;
3561 response_source (packet, cur_tpoint, cur_source_string);
3562 }
3563 else
3564 {
3565 cur_tpoint = cur_tpoint->next;
3566 cur_action = cur_step_action = -1;
3567 cur_source_string = NULL;
3568 if (cur_tpoint)
3569 response_tracepoint (packet, cur_tpoint);
3570 else
3571 strcpy (packet, "l");
3572 }
3573}
3574
3575/* Compose a response that is an imitation of the syntax by which the
3576 trace state variable was originally downloaded. */
3577
3578static void
3579response_tsv (char *packet, struct trace_state_variable *tsv)
3580{
3581 char *buf = (char *) "";
3582 int namelen;
3583
3584 if (tsv->name)
3585 {
3586 namelen = strlen (tsv->name);
3587 buf = alloca (namelen * 2 + 1);
3588 convert_int_to_ascii ((gdb_byte *) tsv->name, buf, namelen);
3589 }
3590
3591 sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0),
3592 tsv->getter ? 1 : 0, buf);
3593}
3594
3595/* Return the first trace state variable definition, and initialize
3596 the state machine that will iterate through all the tsv bits. */
3597
3598static void
3599cmd_qtfv (char *packet)
3600{
3601 trace_debug ("Returning first trace state variable definition");
3602
3603 cur_tsv = trace_state_variables;
3604
3605 if (cur_tsv)
3606 response_tsv (packet, cur_tsv);
3607 else
3608 strcpy (packet, "l");
3609}
3610
3611/* Return additional trace state variable definitions. */
3612
3613static void
3614cmd_qtsv (char *packet)
3615{
3616 trace_debug ("Returning first trace state variable definition");
3617
3618 if (!cur_tpoint)
3619 {
3620 /* This case would normally never occur, but be prepared for
3621 GDB misbehavior. */
3622 strcpy (packet, "l");
3623 }
3624 else if (cur_tsv)
3625 {
3626 cur_tsv = cur_tsv->next;
3627 if (cur_tsv)
3628 response_tsv (packet, cur_tsv);
3629 else
3630 strcpy (packet, "l");
3631 }
3632 else
3633 strcpy (packet, "l");
3634}
3635
0fb4aa4b
PA
3636/* Return the first static tracepoint marker, and initialize the state
3637 machine that will iterate through all the static tracepoints
3638 markers. */
3639
3640static void
3641cmd_qtfstm (char *packet)
3642{
3643 if (!maybe_write_ipa_ust_not_loaded (packet))
3644 run_inferior_command (packet);
3645}
3646
3647/* Return additional static tracepoints markers. */
3648
3649static void
3650cmd_qtsstm (char *packet)
3651{
3652 if (!maybe_write_ipa_ust_not_loaded (packet))
3653 run_inferior_command (packet);
3654}
3655
3656/* Return the definition of the static tracepoint at a given address.
3657 Result packet is the same as qTsST's. */
3658
3659static void
3660cmd_qtstmat (char *packet)
3661{
3662 if (!maybe_write_ipa_ust_not_loaded (packet))
3663 run_inferior_command (packet);
3664}
3665
405f8e94
SS
3666/* Return the minimum instruction size needed for fast tracepoints as a
3667 hexadecimal number. */
3668
3669static void
3670cmd_qtminftpilen (char *packet)
3671{
e886a173
PA
3672 if (current_inferior == NULL)
3673 {
3674 /* Indicate that the minimum length is currently unknown. */
3675 strcpy (packet, "0");
3676 return;
3677 }
3678
405f8e94
SS
3679 sprintf (packet, "%x", target_get_min_fast_tracepoint_insn_len ());
3680}
3681
219f2f23
PA
3682/* Respond to qTBuffer packet with a block of raw data from the trace
3683 buffer. GDB may ask for a lot, but we are allowed to reply with
3684 only as much as will fit within packet limits or whatever. */
3685
3686static void
3687cmd_qtbuffer (char *own_buf)
3688{
3689 ULONGEST offset, num, tot;
3690 unsigned char *tbp;
3691 char *packet = own_buf;
3692
3693 packet += strlen ("qTBuffer:");
3694
3695 packet = unpack_varlen_hex (packet, &offset);
3696 ++packet; /* skip a comma */
f8f67713 3697 unpack_varlen_hex (packet, &num);
219f2f23
PA
3698
3699 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3700 (int) num, pulongest (offset));
3701
3702 tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
3703
3704 /* If we're right at the end, reply specially that we're done. */
3705 if (offset == tot)
3706 {
3707 strcpy (own_buf, "l");
3708 return;
3709 }
3710
3711 /* Object to any other out-of-bounds request. */
3712 if (offset > tot)
3713 {
3714 write_enn (own_buf);
3715 return;
3716 }
3717
3718 /* Compute the pointer corresponding to the given offset, accounting
3719 for wraparound. */
3720 tbp = trace_buffer_start + offset;
3721 if (tbp >= trace_buffer_wrap)
3722 tbp -= (trace_buffer_wrap - trace_buffer_lo);
3723
3724 /* Trim to the remaining bytes if we're close to the end. */
3725 if (num > tot - offset)
3726 num = tot - offset;
3727
3728 /* Trim to available packet size. */
3729 if (num >= (PBUFSIZ - 16) / 2 )
3730 num = (PBUFSIZ - 16) / 2;
3731
3732 convert_int_to_ascii (tbp, own_buf, num);
3733 own_buf[num] = '\0';
3734}
3735
3736static void
277e4e52 3737cmd_bigqtbuffer_circular (char *own_buf)
219f2f23
PA
3738{
3739 ULONGEST val;
3740 char *packet = own_buf;
3741
277e4e52 3742 packet += strlen ("QTBuffer:circular:");
219f2f23 3743
277e4e52
PA
3744 unpack_varlen_hex (packet, &val);
3745 circular_trace_buffer = val;
3746 trace_debug ("Trace buffer is now %s",
3747 circular_trace_buffer ? "circular" : "linear");
3748 write_ok (own_buf);
219f2f23
PA
3749}
3750
f196051f
SS
3751static void
3752cmd_qtnotes (char *own_buf)
3753{
3754 size_t nbytes;
3755 char *saved, *user, *notes, *stopnote;
3756 char *packet = own_buf;
3757
3758 packet += strlen ("QTNotes:");
3759
3760 while (*packet)
3761 {
3762 if (strncmp ("user:", packet, strlen ("user:")) == 0)
3763 {
3764 packet += strlen ("user:");
3765 saved = packet;
3766 packet = strchr (packet, ';');
3767 nbytes = (packet - saved) / 2;
3768 user = xmalloc (nbytes + 1);
3769 nbytes = unhexify (user, saved, nbytes);
3770 user[nbytes] = '\0';
3771 ++packet; /* skip the semicolon */
3772 trace_debug ("User is '%s'", user);
3773 tracing_user_name = user;
3774 }
3775 else if (strncmp ("notes:", packet, strlen ("notes:")) == 0)
3776 {
3777 packet += strlen ("notes:");
3778 saved = packet;
3779 packet = strchr (packet, ';');
3780 nbytes = (packet - saved) / 2;
3781 notes = xmalloc (nbytes + 1);
3782 nbytes = unhexify (notes, saved, nbytes);
3783 notes[nbytes] = '\0';
3784 ++packet; /* skip the semicolon */
3785 trace_debug ("Notes is '%s'", notes);
3786 tracing_notes = notes;
3787 }
3788 else if (strncmp ("tstop:", packet, strlen ("tstop:")) == 0)
3789 {
3790 packet += strlen ("tstop:");
3791 saved = packet;
3792 packet = strchr (packet, ';');
3793 nbytes = (packet - saved) / 2;
3794 stopnote = xmalloc (nbytes + 1);
3795 nbytes = unhexify (stopnote, saved, nbytes);
3796 stopnote[nbytes] = '\0';
3797 ++packet; /* skip the semicolon */
3798 trace_debug ("tstop note is '%s'", stopnote);
3799 tracing_stop_note = stopnote;
3800 }
3801 else
3802 break;
3803 }
3804
3805 write_ok (own_buf);
3806}
3807
219f2f23
PA
3808int
3809handle_tracepoint_general_set (char *packet)
3810{
3811 if (strcmp ("QTinit", packet) == 0)
3812 {
3813 cmd_qtinit (packet);
3814 return 1;
3815 }
3816 else if (strncmp ("QTDP:", packet, strlen ("QTDP:")) == 0)
3817 {
3818 cmd_qtdp (packet);
3819 return 1;
3820 }
3821 else if (strncmp ("QTDPsrc:", packet, strlen ("QTDPsrc:")) == 0)
3822 {
3823 cmd_qtdpsrc (packet);
3824 return 1;
3825 }
d248b706
KY
3826 else if (strncmp ("QTEnable:", packet, strlen ("QTEnable:")) == 0)
3827 {
3828 cmd_qtenable_disable (packet, 1);
3829 return 1;
3830 }
3831 else if (strncmp ("QTDisable:", packet, strlen ("QTDisable:")) == 0)
3832 {
3833 cmd_qtenable_disable (packet, 0);
3834 return 1;
3835 }
219f2f23
PA
3836 else if (strncmp ("QTDV:", packet, strlen ("QTDV:")) == 0)
3837 {
3838 cmd_qtdv (packet);
3839 return 1;
3840 }
3841 else if (strncmp ("QTro:", packet, strlen ("QTro:")) == 0)
3842 {
3843 cmd_qtro (packet);
3844 return 1;
3845 }
3846 else if (strcmp ("QTStart", packet) == 0)
3847 {
3848 cmd_qtstart (packet);
3849 return 1;
3850 }
3851 else if (strcmp ("QTStop", packet) == 0)
3852 {
3853 cmd_qtstop (packet);
3854 return 1;
3855 }
8336d594
PA
3856 else if (strncmp ("QTDisconnected:", packet,
3857 strlen ("QTDisconnected:")) == 0)
3858 {
3859 cmd_qtdisconnected (packet);
3860 return 1;
3861 }
219f2f23
PA
3862 else if (strncmp ("QTFrame:", packet, strlen ("QTFrame:")) == 0)
3863 {
3864 cmd_qtframe (packet);
3865 return 1;
3866 }
277e4e52 3867 else if (strncmp ("QTBuffer:circular:", packet, strlen ("QTBuffer:circular:")) == 0)
219f2f23 3868 {
277e4e52 3869 cmd_bigqtbuffer_circular (packet);
219f2f23
PA
3870 return 1;
3871 }
f196051f
SS
3872 else if (strncmp ("QTNotes:", packet, strlen ("QTNotes:")) == 0)
3873 {
3874 cmd_qtnotes (packet);
3875 return 1;
3876 }
219f2f23
PA
3877
3878 return 0;
3879}
3880
3881int
3882handle_tracepoint_query (char *packet)
3883{
3884 if (strcmp ("qTStatus", packet) == 0)
3885 {
3886 cmd_qtstatus (packet);
3887 return 1;
3888 }
f196051f
SS
3889 else if (strncmp ("qTP:", packet, strlen ("qTP:")) == 0)
3890 {
3891 cmd_qtp (packet);
3892 return 1;
3893 }
219f2f23
PA
3894 else if (strcmp ("qTfP", packet) == 0)
3895 {
3896 cmd_qtfp (packet);
3897 return 1;
3898 }
3899 else if (strcmp ("qTsP", packet) == 0)
3900 {
3901 cmd_qtsp (packet);
3902 return 1;
3903 }
3904 else if (strcmp ("qTfV", packet) == 0)
3905 {
3906 cmd_qtfv (packet);
3907 return 1;
3908 }
3909 else if (strcmp ("qTsV", packet) == 0)
3910 {
3911 cmd_qtsv (packet);
3912 return 1;
3913 }
3914 else if (strncmp ("qTV:", packet, strlen ("qTV:")) == 0)
3915 {
3916 cmd_qtv (packet);
3917 return 1;
3918 }
3919 else if (strncmp ("qTBuffer:", packet, strlen ("qTBuffer:")) == 0)
3920 {
3921 cmd_qtbuffer (packet);
3922 return 1;
3923 }
0fb4aa4b
PA
3924 else if (strcmp ("qTfSTM", packet) == 0)
3925 {
3926 cmd_qtfstm (packet);
3927 return 1;
3928 }
3929 else if (strcmp ("qTsSTM", packet) == 0)
3930 {
3931 cmd_qtsstm (packet);
3932 return 1;
3933 }
3934 else if (strncmp ("qTSTMat:", packet, strlen ("qTSTMat:")) == 0)
3935 {
3936 cmd_qtstmat (packet);
3937 return 1;
3938 }
405f8e94
SS
3939 else if (strcmp ("qTMinFTPILen", packet) == 0)
3940 {
3941 cmd_qtminftpilen (packet);
3942 return 1;
3943 }
219f2f23
PA
3944
3945 return 0;
3946}
3947
fa593d66
PA
3948#endif
3949#ifndef IN_PROCESS_AGENT
3950
219f2f23
PA
3951/* Call this when thread TINFO has hit the tracepoint defined by
3952 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3953 action. This adds a while-stepping collecting state item to the
3954 threads' collecting state list, so that we can keep track of
3955 multiple simultaneous while-stepping actions being collected by the
3956 same thread. This can happen in cases like:
3957
3958 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3959 ff0002 INSN2
3960 ff0003 INSN3 <-- TP2, collect $regs
3961 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3962 ff0005 INSN5
3963
3964 Notice that when instruction INSN5 is reached, the while-stepping
3965 actions of both TP1 and TP3 are still being collected, and that TP2
3966 had been collected meanwhile. The whole range of ff0001-ff0005
3967 should be single-stepped, due to at least TP1's while-stepping
3968 action covering the whole range. */
3969
3970static void
3971add_while_stepping_state (struct thread_info *tinfo,
3972 int tp_number, CORE_ADDR tp_address)
3973{
3974 struct wstep_state *wstep;
3975
3976 wstep = xmalloc (sizeof (*wstep));
3977 wstep->next = tinfo->while_stepping;
3978
3979 wstep->tp_number = tp_number;
3980 wstep->tp_address = tp_address;
3981 wstep->current_step = 0;
3982
3983 tinfo->while_stepping = wstep;
3984}
3985
3986/* Release the while-stepping collecting state WSTEP. */
3987
3988static void
3989release_while_stepping_state (struct wstep_state *wstep)
3990{
3991 free (wstep);
3992}
3993
3994/* Release all while-stepping collecting states currently associated
3995 with thread TINFO. */
3996
3997void
3998release_while_stepping_state_list (struct thread_info *tinfo)
3999{
4000 struct wstep_state *head;
4001
4002 while (tinfo->while_stepping)
4003 {
4004 head = tinfo->while_stepping;
4005 tinfo->while_stepping = head->next;
4006 release_while_stepping_state (head);
4007 }
4008}
4009
4010/* If TINFO was handling a 'while-stepping' action, the step has
4011 finished, so collect any step data needed, and check if any more
4012 steps are required. Return true if the thread was indeed
4013 collecting tracepoint data, false otherwise. */
4014
4015int
4016tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
4017{
4018 struct tracepoint *tpoint;
4019 struct wstep_state *wstep;
4020 struct wstep_state **wstep_link;
4021 struct trap_tracepoint_ctx ctx;
4022
fa593d66
PA
4023 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4024 our buffer. */
4025 if (in_process_agent_loaded ())
4026 upload_fast_traceframes ();
4027
219f2f23
PA
4028 /* Check if we were indeed collecting data for one of more
4029 tracepoints with a 'while-stepping' count. */
4030 if (tinfo->while_stepping == NULL)
4031 return 0;
4032
4033 if (!tracing)
4034 {
4035 /* We're not even tracing anymore. Stop this thread from
4036 collecting. */
4037 release_while_stepping_state_list (tinfo);
4038
4039 /* The thread had stopped due to a single-step request indeed
4040 explained by a tracepoint. */
4041 return 1;
4042 }
4043
4044 wstep = tinfo->while_stepping;
4045 wstep_link = &tinfo->while_stepping;
4046
4047 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4048 target_pid_to_str (tinfo->entry.id),
4049 wstep->tp_number, paddress (wstep->tp_address));
4050
fa593d66 4051 ctx.base.type = trap_tracepoint;
219f2f23
PA
4052 ctx.regcache = get_thread_regcache (tinfo, 1);
4053
4054 while (wstep != NULL)
4055 {
4056 tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
4057 if (tpoint == NULL)
4058 {
4059 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4060 wstep->tp_number, paddress (wstep->tp_address),
4061 target_pid_to_str (tinfo->entry.id));
4062
4063 /* Unlink. */
4064 *wstep_link = wstep->next;
4065 release_while_stepping_state (wstep);
4f269b12 4066 wstep = *wstep_link;
219f2f23
PA
4067 continue;
4068 }
4069
4070 /* We've just finished one step. */
4071 ++wstep->current_step;
4072
4073 /* Collect data. */
4074 collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
4075 stop_pc, tpoint, wstep->current_step);
4076
4077 if (wstep->current_step >= tpoint->step_count)
4078 {
4079 /* The requested numbers of steps have occurred. */
4080 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4081 target_pid_to_str (tinfo->entry.id),
4082 wstep->tp_number, paddress (wstep->tp_address));
4083
4084 /* Unlink the wstep. */
4085 *wstep_link = wstep->next;
4086 release_while_stepping_state (wstep);
4087 wstep = *wstep_link;
4088
4089 /* Only check the hit count now, which ensure that we do all
4090 our stepping before stopping the run. */
4091 if (tpoint->pass_count > 0
4092 && tpoint->hit_count >= tpoint->pass_count
4093 && stopping_tracepoint == NULL)
4094 stopping_tracepoint = tpoint;
4095 }
4096 else
4097 {
4098 /* Keep single-stepping until the requested numbers of steps
4099 have occurred. */
4100 wstep_link = &wstep->next;
4101 wstep = *wstep_link;
4102 }
4103
4104 if (stopping_tracepoint
4105 || trace_buffer_is_full
4106 || expr_eval_result != expr_eval_no_error)
4107 {
4108 stop_tracing ();
4109 break;
4110 }
4111 }
4112
4113 return 1;
4114}
4115
fa593d66
PA
4116/* Handle any internal tracing control breakpoint hits. That means,
4117 pull traceframes from the IPA to our buffer, and syncing both
4118 tracing agents when the IPA's tracing stops for some reason. */
4119
4120int
4121handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
4122{
4123 /* Pull in fast tracepoint trace frames from the inferior in-process
4124 agent's buffer into our buffer. */
4125
4126 if (!in_process_agent_loaded ())
4127 return 0;
4128
4129 upload_fast_traceframes ();
4130
4131 /* Check if the in-process agent had decided we should stop
4132 tracing. */
4133 if (stop_pc == ipa_sym_addrs.addr_stop_tracing)
4134 {
4135 int ipa_trace_buffer_is_full;
4136 CORE_ADDR ipa_stopping_tracepoint;
4137 int ipa_expr_eval_result;
4138 CORE_ADDR ipa_error_tracepoint;
4139
4140 trace_debug ("lib stopped at stop_tracing");
4141
4142 read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full,
4143 &ipa_trace_buffer_is_full);
4144
4145 read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
4146 &ipa_stopping_tracepoint);
4147 write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0);
4148
4149 read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint,
4150 &ipa_error_tracepoint);
4151 write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0);
4152
4153 read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result,
4154 &ipa_expr_eval_result);
4155 write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0);
4156
4157 trace_debug ("lib: trace_buffer_is_full: %d, "
4158 "stopping_tracepoint: %s, "
4159 "ipa_expr_eval_result: %d, "
4160 "error_tracepoint: %s, ",
4161 ipa_trace_buffer_is_full,
4162 paddress (ipa_stopping_tracepoint),
4163 ipa_expr_eval_result,
4164 paddress (ipa_error_tracepoint));
4165
4166 if (debug_threads)
4167 {
4168 if (ipa_trace_buffer_is_full)
4169 trace_debug ("lib stopped due to full buffer.");
4170 if (ipa_stopping_tracepoint)
4171 trace_debug ("lib stopped due to tpoint");
4172 if (ipa_stopping_tracepoint)
4173 trace_debug ("lib stopped due to error");
4174 }
4175
4176 if (ipa_stopping_tracepoint != 0)
4177 {
4178 stopping_tracepoint
4179 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint);
4180 }
4181 else if (ipa_expr_eval_result != expr_eval_no_error)
4182 {
4183 expr_eval_result = ipa_expr_eval_result;
4184 error_tracepoint
4185 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint);
4186 }
4187 stop_tracing ();
4188 return 1;
4189 }
4190 else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer)
4191 {
4192 trace_debug ("lib stopped at flush_trace_buffer");
4193 return 1;
4194 }
4195
4196 return 0;
4197}
4198
219f2f23
PA
4199/* Return true if TINFO just hit a tracepoint. Collect data if
4200 so. */
4201
4202int
4203tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
4204{
4205 struct tracepoint *tpoint;
4206 int ret = 0;
4207 struct trap_tracepoint_ctx ctx;
4208
4209 /* Not tracing, don't handle. */
4210 if (!tracing)
4211 return 0;
4212
fa593d66 4213 ctx.base.type = trap_tracepoint;
219f2f23
PA
4214 ctx.regcache = get_thread_regcache (tinfo, 1);
4215
4216 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
4217 {
fa593d66
PA
4218 /* Note that we collect fast tracepoints here as well. We'll
4219 step over the fast tracepoint jump later, which avoids the
f72429c5
YQ
4220 double collect. However, we don't collect for static
4221 tracepoints here, because UST markers are compiled in program,
4222 and probes will be executed in program. So static tracepoints
4223 are collected there. */
4224 if (tpoint->enabled && stop_pc == tpoint->address
4225 && tpoint->type != static_tracepoint)
219f2f23
PA
4226 {
4227 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4228 target_pid_to_str (tinfo->entry.id),
4229 tpoint->number, paddress (tpoint->address));
4230
4231 /* Test the condition if present, and collect if true. */
4232 if (!tpoint->cond
4233 || (condition_true_at_tracepoint
4234 ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
4235 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
4236 stop_pc, tpoint);
4237
4238 if (stopping_tracepoint
4239 || trace_buffer_is_full
4240 || expr_eval_result != expr_eval_no_error)
4241 {
4242 stop_tracing ();
4243 }
4244 /* If the tracepoint had a 'while-stepping' action, then set
4245 the thread to collect this tracepoint on the following
4246 single-steps. */
4247 else if (tpoint->step_count > 0)
4248 {
4249 add_while_stepping_state (tinfo,
4250 tpoint->number, tpoint->address);
4251 }
4252
4253 ret = 1;
4254 }
4255 }
4256
4257 return ret;
4258}
4259
fa593d66
PA
4260#endif
4261
0fb4aa4b
PA
4262#if defined IN_PROCESS_AGENT && defined HAVE_UST
4263struct ust_marker_data;
4264static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
0fb4aa4b
PA
4265 struct traceframe *tframe);
4266#endif
4267
219f2f23
PA
4268/* Create a trace frame for the hit of the given tracepoint in the
4269 given thread. */
4270
4271static void
4272collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
4273 struct tracepoint *tpoint)
4274{
4275 struct traceframe *tframe;
4276 int acti;
4277
4278 /* Only count it as a hit when we actually collect data. */
4279 tpoint->hit_count++;
4280
4281 /* If we've exceeded a defined pass count, record the event for
4282 later, and finish the collection for this hit. This test is only
4283 for nonstepping tracepoints, stepping tracepoints test at the end
4284 of their while-stepping loop. */
4285 if (tpoint->pass_count > 0
4286 && tpoint->hit_count >= tpoint->pass_count
4287 && tpoint->step_count == 0
4288 && stopping_tracepoint == NULL)
4289 stopping_tracepoint = tpoint;
4290
4291 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
4292 tpoint->number, paddress (tpoint->address), tpoint->hit_count);
4293
4294 tframe = add_traceframe (tpoint);
4295
4296 if (tframe)
4297 {
4298 for (acti = 0; acti < tpoint->numactions; ++acti)
4299 {
fa593d66 4300#ifndef IN_PROCESS_AGENT
219f2f23
PA
4301 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4302 tpoint->number, paddress (tpoint->address),
4303 tpoint->actions_str[acti]);
fa593d66 4304#endif
219f2f23
PA
4305
4306 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4307 tpoint->actions[acti]);
4308 }
4309
4310 finish_traceframe (tframe);
4311 }
4312
4313 if (tframe == NULL && tracing)
4314 trace_buffer_is_full = 1;
4315}
4316
fa593d66
PA
4317#ifndef IN_PROCESS_AGENT
4318
219f2f23
PA
4319static void
4320collect_data_at_step (struct tracepoint_hit_ctx *ctx,
4321 CORE_ADDR stop_pc,
4322 struct tracepoint *tpoint, int current_step)
4323{
4324 struct traceframe *tframe;
4325 int acti;
4326
4327 trace_debug ("Making new step traceframe for "
4328 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4329 tpoint->number, paddress (tpoint->address),
4330 current_step, tpoint->step_count,
4331 tpoint->hit_count);
4332
4333 tframe = add_traceframe (tpoint);
4334
4335 if (tframe)
4336 {
4337 for (acti = 0; acti < tpoint->num_step_actions; ++acti)
4338 {
4339 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4340 tpoint->number, paddress (tpoint->address),
4341 tpoint->step_actions_str[acti]);
4342
4343 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4344 tpoint->step_actions[acti]);
4345 }
4346
4347 finish_traceframe (tframe);
4348 }
4349
4350 if (tframe == NULL && tracing)
4351 trace_buffer_is_full = 1;
4352}
4353
fa593d66
PA
4354#endif
4355
219f2f23
PA
4356static struct regcache *
4357get_context_regcache (struct tracepoint_hit_ctx *ctx)
4358{
fa593d66
PA
4359 struct regcache *regcache = NULL;
4360
4361#ifdef IN_PROCESS_AGENT
4362 if (ctx->type == fast_tracepoint)
4363 {
4364 struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
4365 if (!fctx->regcache_initted)
4366 {
4367 fctx->regcache_initted = 1;
4368 init_register_cache (&fctx->regcache, fctx->regspace);
4369 supply_regblock (&fctx->regcache, NULL);
4370 supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
4371 }
4372 regcache = &fctx->regcache;
4373 }
0fb4aa4b
PA
4374#ifdef HAVE_UST
4375 if (ctx->type == static_tracepoint)
4376 {
493e2a69
MS
4377 struct static_tracepoint_ctx *sctx
4378 = (struct static_tracepoint_ctx *) ctx;
4379
0fb4aa4b
PA
4380 if (!sctx->regcache_initted)
4381 {
4382 sctx->regcache_initted = 1;
4383 init_register_cache (&sctx->regcache, sctx->regspace);
4384 supply_regblock (&sctx->regcache, NULL);
4385 /* Pass down the tracepoint address, because REGS doesn't
4386 include the PC, but we know what it must have been. */
4387 supply_static_tracepoint_registers (&sctx->regcache,
4388 (const unsigned char *)
4389 sctx->regs,
4390 sctx->tpoint->address);
4391 }
4392 regcache = &sctx->regcache;
4393 }
4394#endif
fa593d66
PA
4395#else
4396 if (ctx->type == trap_tracepoint)
4397 {
4398 struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
4399 regcache = tctx->regcache;
4400 }
4401#endif
219f2f23
PA
4402
4403 gdb_assert (regcache != NULL);
4404
4405 return regcache;
4406}
4407
4408static void
4409do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4410 CORE_ADDR stop_pc,
4411 struct tracepoint *tpoint,
4412 struct traceframe *tframe,
4413 struct tracepoint_action *taction)
4414{
4415 enum eval_result_type err;
4416
4417 switch (taction->type)
4418 {
4419 case 'M':
4420 {
4421 struct collect_memory_action *maction;
4422
4423 maction = (struct collect_memory_action *) taction;
4424
4425 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4426 pulongest (maction->len),
4427 paddress (maction->addr), maction->basereg);
4428 /* (should use basereg) */
4429 agent_mem_read (tframe, NULL,
4430 (CORE_ADDR) maction->addr, maction->len);
4431 break;
4432 }
4433 case 'R':
4434 {
219f2f23
PA
4435 unsigned char *regspace;
4436 struct regcache tregcache;
4437 struct regcache *context_regcache;
4438
219f2f23
PA
4439
4440 trace_debug ("Want to collect registers");
4441
4442 /* Collect all registers for now. */
4443 regspace = add_traceframe_block (tframe,
4444 1 + register_cache_size ());
4445 if (regspace == NULL)
4446 {
4447 trace_debug ("Trace buffer block allocation failed, skipping");
4448 break;
4449 }
4450 /* Identify a register block. */
4451 *regspace = 'R';
4452
4453 context_regcache = get_context_regcache (ctx);
4454
4455 /* Wrap the regblock in a register cache (in the stack, we
4456 don't want to malloc here). */
4457 init_register_cache (&tregcache, regspace + 1);
4458
4459 /* Copy the register data to the regblock. */
4460 regcache_cpy (&tregcache, context_regcache);
4461
fa593d66 4462#ifndef IN_PROCESS_AGENT
219f2f23
PA
4463 /* On some platforms, trap-based tracepoints will have the PC
4464 pointing to the next instruction after the trap, but we
4465 don't want the user or GDB trying to guess whether the
4466 saved PC needs adjusting; so always record the adjusted
4467 stop_pc. Note that we can't use tpoint->address instead,
fa593d66
PA
4468 since it will be wrong for while-stepping actions. This
4469 adjustment is a nop for fast tracepoints collected from the
4470 in-process lib (but not if GDBserver is collecting one
4471 preemptively), since the PC had already been adjusted to
4472 contain the tracepoint's address by the jump pad. */
219f2f23 4473 trace_debug ("Storing stop pc (0x%s) in regblock",
8d00225b 4474 paddress (stop_pc));
219f2f23
PA
4475
4476 /* This changes the regblock, not the thread's
4477 regcache. */
4478 regcache_write_pc (&tregcache, stop_pc);
fa593d66 4479#endif
219f2f23
PA
4480 }
4481 break;
4482 case 'X':
4483 {
4484 struct eval_expr_action *eaction;
4485
4486 eaction = (struct eval_expr_action *) taction;
4487
4488 trace_debug ("Want to evaluate expression");
4489
5e1dc496 4490 err = eval_tracepoint_agent_expr (ctx, tframe, eaction->expr, NULL);
219f2f23
PA
4491
4492 if (err != expr_eval_no_error)
4493 {
4494 record_tracepoint_error (tpoint, "action expression", err);
4495 return;
4496 }
4497 }
4498 break;
0fb4aa4b
PA
4499 case 'L':
4500 {
4501#if defined IN_PROCESS_AGENT && defined HAVE_UST
4502 trace_debug ("Want to collect static trace data");
19560ba5 4503 collect_ust_data_at_tracepoint (ctx, tframe);
0fb4aa4b
PA
4504#else
4505 trace_debug ("warning: collecting static trace data, "
4506 "but static tracepoints are not supported");
4507#endif
4508 }
4509 break;
219f2f23
PA
4510 default:
4511 trace_debug ("unknown trace action '%c', ignoring", taction->type);
4512 break;
4513 }
4514}
4515
4516static int
4517condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4518 struct tracepoint *tpoint)
4519{
4520 ULONGEST value = 0;
4521 enum eval_result_type err;
4522
c6beb2cb
PA
4523 /* Presently, gdbserver doesn't run compiled conditions, only the
4524 IPA does. If the program stops at a fast tracepoint's address
4525 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4526 gdbserver preemptively collect the fast tracepoint. Later, on
4527 resume, gdbserver steps over the fast tracepoint like it steps
4528 over breakpoints, so that the IPA doesn't see that fast
4529 tracepoint. This avoids double collects of fast tracepoints in
4530 that stopping scenario. Having gdbserver itself handle the fast
4531 tracepoint gives the user a consistent view of when fast or trap
4532 tracepoints are collected, compared to an alternative where only
4533 trap tracepoints are collected on stop, and fast tracepoints on
4534 resume. When a fast tracepoint is being processed by gdbserver,
4535 it is always the non-compiled condition expression that is
4536 used. */
4537#ifdef IN_PROCESS_AGENT
6a271cae
PA
4538 if (tpoint->compiled_cond)
4539 err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (ctx, &value);
4540 else
c6beb2cb 4541#endif
5e1dc496 4542 err = eval_tracepoint_agent_expr (ctx, NULL, tpoint->cond, &value);
219f2f23
PA
4543
4544 if (err != expr_eval_no_error)
4545 {
4546 record_tracepoint_error (tpoint, "condition", err);
4547 /* The error case must return false. */
4548 return 0;
4549 }
4550
4551 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4552 tpoint->number, paddress (tpoint->address),
4553 pulongest (value));
4554 return (value ? 1 : 0);
4555}
4556
5e1dc496
LM
4557/* Evaluates a tracepoint agent expression with context CTX,
4558 traceframe TFRAME, agent expression AEXPR and store the
4559 result in RSLT. */
219f2f23
PA
4560
4561static enum eval_result_type
5e1dc496
LM
4562eval_tracepoint_agent_expr (struct tracepoint_hit_ctx *ctx,
4563 struct traceframe *tframe,
4564 struct agent_expr *aexpr,
4565 ULONGEST *rslt)
4566{
4567 struct regcache *regcache;
4568 regcache = get_context_regcache (ctx);
219f2f23 4569
5e1dc496 4570 return gdb_eval_agent_expr (regcache, tframe, aexpr, rslt);
219f2f23
PA
4571}
4572
4573/* Do memory copies for bytecodes. */
4574/* Do the recording of memory blocks for actions and bytecodes. */
4575
5e1dc496 4576int
219f2f23
PA
4577agent_mem_read (struct traceframe *tframe,
4578 unsigned char *to, CORE_ADDR from, ULONGEST len)
4579{
4580 unsigned char *mspace;
4581 ULONGEST remaining = len;
4582 unsigned short blocklen;
4583
4584 /* If a 'to' buffer is specified, use it. */
4585 if (to != NULL)
4586 {
4587 read_inferior_memory (from, to, len);
4588 return 0;
4589 }
4590
4591 /* Otherwise, create a new memory block in the trace buffer. */
4592 while (remaining > 0)
4593 {
4594 size_t sp;
4595
4596 blocklen = (remaining > 65535 ? 65535 : remaining);
4597 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4598 mspace = add_traceframe_block (tframe, sp);
4599 if (mspace == NULL)
4600 return 1;
4601 /* Identify block as a memory block. */
4602 *mspace = 'M';
4603 ++mspace;
4604 /* Record address and size. */
4605 memcpy (mspace, &from, sizeof (from));
4606 mspace += sizeof (from);
4607 memcpy (mspace, &blocklen, sizeof (blocklen));
4608 mspace += sizeof (blocklen);
4609 /* Record the memory block proper. */
4610 read_inferior_memory (from, mspace, blocklen);
4611 trace_debug ("%d bytes recorded", blocklen);
4612 remaining -= blocklen;
4613 from += blocklen;
4614 }
4615 return 0;
4616}
4617
5e1dc496 4618int
3065dfb6
SS
4619agent_mem_read_string (struct traceframe *tframe,
4620 unsigned char *to, CORE_ADDR from, ULONGEST len)
4621{
4622 unsigned char *buf, *mspace;
4623 ULONGEST remaining = len;
4624 unsigned short blocklen, i;
4625
4626 /* To save a bit of space, block lengths are 16-bit, so break large
4627 requests into multiple blocks. Bordering on overkill for strings,
4628 but it could happen that someone specifies a large max length. */
4629 while (remaining > 0)
4630 {
4631 size_t sp;
4632
4633 blocklen = (remaining > 65535 ? 65535 : remaining);
4634 /* We want working space to accumulate nonzero bytes, since
4635 traceframes must have a predecided size (otherwise it gets
4636 harder to wrap correctly for the circular case, etc). */
4637 buf = (unsigned char *) xmalloc (blocklen + 1);
4638 for (i = 0; i < blocklen; ++i)
4639 {
4640 /* Read the string one byte at a time, in case the string is
4641 at the end of a valid memory area - we don't want a
4642 correctly-terminated string to engender segvio
4643 complaints. */
4644 read_inferior_memory (from + i, buf + i, 1);
4645
4646 if (buf[i] == '\0')
4647 {
4648 blocklen = i + 1;
4649 /* Make sure outer loop stops now too. */
4650 remaining = blocklen;
4651 break;
4652 }
4653 }
4654 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4655 mspace = add_traceframe_block (tframe, sp);
4656 if (mspace == NULL)
4657 {
4658 xfree (buf);
4659 return 1;
4660 }
4661 /* Identify block as a memory block. */
4662 *mspace = 'M';
4663 ++mspace;
4664 /* Record address and size. */
4665 memcpy ((void *) mspace, (void *) &from, sizeof (from));
4666 mspace += sizeof (from);
4667 memcpy ((void *) mspace, (void *) &blocklen, sizeof (blocklen));
4668 mspace += sizeof (blocklen);
4669 /* Copy the string contents. */
4670 memcpy ((void *) mspace, (void *) buf, blocklen);
4671 remaining -= blocklen;
4672 from += blocklen;
4673 xfree (buf);
4674 }
4675 return 0;
4676}
4677
219f2f23
PA
4678/* Record the value of a trace state variable. */
4679
5e1dc496 4680int
219f2f23
PA
4681agent_tsv_read (struct traceframe *tframe, int n)
4682{
4683 unsigned char *vspace;
4684 LONGEST val;
4685
4686 vspace = add_traceframe_block (tframe,
4687 1 + sizeof (n) + sizeof (LONGEST));
4688 if (vspace == NULL)
4689 return 1;
4690 /* Identify block as a variable. */
4691 *vspace = 'V';
4692 /* Record variable's number and value. */
4693 memcpy (vspace + 1, &n, sizeof (n));
4694 val = get_trace_state_variable_value (n);
4695 memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
4696 trace_debug ("Variable %d recorded", n);
4697 return 0;
4698}
4699
fa593d66
PA
4700#ifndef IN_PROCESS_AGENT
4701
b3b9301e
PA
4702/* Callback for traceframe_walk_blocks, used to find a given block
4703 type in a traceframe. */
4704
4705static int
4706match_blocktype (char blocktype, unsigned char *dataptr, void *data)
4707{
4708 char *wantedp = data;
4709
4710 if (*wantedp == blocktype)
4711 return 1;
4712
4713 return 0;
4714}
4715
4716/* Walk over all traceframe blocks of the traceframe buffer starting
4717 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4718 block found, passing in DATA unmodified. If CALLBACK returns true,
4719 this returns a pointer to where the block is found. Returns NULL
4720 if no callback call returned true, indicating that all blocks have
4721 been walked. */
4722
219f2f23 4723static unsigned char *
b3b9301e
PA
4724traceframe_walk_blocks (unsigned char *database, unsigned int datasize,
4725 int tfnum,
4726 int (*callback) (char blocktype,
4727 unsigned char *dataptr,
4728 void *data),
4729 void *data)
219f2f23
PA
4730{
4731 unsigned char *dataptr;
4732
4733 if (datasize == 0)
4734 {
4735 trace_debug ("traceframe %d has no data", tfnum);
4736 return NULL;
4737 }
4738
4739 /* Iterate through a traceframe's blocks, looking for a block of the
4740 requested type. */
4741 for (dataptr = database;
4742 dataptr < database + datasize;
4743 /* nothing */)
4744 {
4745 char blocktype;
4746 unsigned short mlen;
4747
4748 if (dataptr == trace_buffer_wrap)
4749 {
4750 /* Adjust to reflect wrapping part of the frame around to
4751 the beginning. */
4752 datasize = dataptr - database;
4753 dataptr = database = trace_buffer_lo;
4754 }
b3b9301e 4755
219f2f23
PA
4756 blocktype = *dataptr++;
4757
b3b9301e 4758 if ((*callback) (blocktype, dataptr, data))
219f2f23
PA
4759 return dataptr;
4760
4761 switch (blocktype)
4762 {
4763 case 'R':
4764 /* Skip over the registers block. */
4765 dataptr += register_cache_size ();
4766 break;
4767 case 'M':
4768 /* Skip over the memory block. */
4769 dataptr += sizeof (CORE_ADDR);
4770 memcpy (&mlen, dataptr, sizeof (mlen));
4771 dataptr += (sizeof (mlen) + mlen);
4772 break;
219f2f23
PA
4773 case 'V':
4774 /* Skip over the TSV block. */
4775 dataptr += (sizeof (int) + sizeof (LONGEST));
4776 break;
0fb4aa4b
PA
4777 case 'S':
4778 /* Skip over the static trace data block. */
4779 memcpy (&mlen, dataptr, sizeof (mlen));
4780 dataptr += (sizeof (mlen) + mlen);
4781 break;
219f2f23
PA
4782 default:
4783 trace_debug ("traceframe %d has unknown block type 0x%x",
4784 tfnum, blocktype);
4785 return NULL;
4786 }
4787 }
4788
4789 return NULL;
4790}
4791
b3b9301e
PA
4792/* Look for the block of type TYPE_WANTED in the trameframe starting
4793 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4794 number. */
4795
4796static unsigned char *
4797traceframe_find_block_type (unsigned char *database, unsigned int datasize,
4798 int tfnum, char type_wanted)
4799{
4800 return traceframe_walk_blocks (database, datasize, tfnum,
4801 match_blocktype, &type_wanted);
4802}
4803
219f2f23
PA
4804static unsigned char *
4805traceframe_find_regblock (struct traceframe *tframe, int tfnum)
4806{
4807 unsigned char *regblock;
4808
4809 regblock = traceframe_find_block_type (tframe->data,
4810 tframe->data_size,
4811 tfnum, 'R');
4812
4813 if (regblock == NULL)
4814 trace_debug ("traceframe %d has no register data", tfnum);
4815
4816 return regblock;
4817}
4818
4819/* Get registers from a traceframe. */
4820
4821int
4822fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
4823{
4824 unsigned char *dataptr;
4825 struct tracepoint *tpoint;
4826 struct traceframe *tframe;
4827
4828 tframe = find_traceframe (tfnum);
4829
4830 if (tframe == NULL)
4831 {
4832 trace_debug ("traceframe %d not found", tfnum);
4833 return 1;
4834 }
4835
4836 dataptr = traceframe_find_regblock (tframe, tfnum);
4837 if (dataptr == NULL)
4838 {
1c79eb8a 4839 /* Mark registers unavailable. */
219f2f23
PA
4840 supply_regblock (regcache, NULL);
4841
4842 /* We can generally guess at a PC, although this will be
4843 misleading for while-stepping frames and multi-location
4844 tracepoints. */
4845 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
4846 if (tpoint != NULL)
4847 regcache_write_pc (regcache, tpoint->address);
4848 }
4849 else
4850 supply_regblock (regcache, dataptr);
4851
4852 return 0;
4853}
4854
4855static CORE_ADDR
4856traceframe_get_pc (struct traceframe *tframe)
4857{
4858 struct regcache regcache;
4859 unsigned char *dataptr;
4860
4861 dataptr = traceframe_find_regblock (tframe, -1);
4862 if (dataptr == NULL)
4863 return 0;
4864
4865 init_register_cache (&regcache, dataptr);
4866 return regcache_read_pc (&regcache);
4867}
4868
4869/* Read a requested block of memory from a trace frame. */
4870
4871int
4872traceframe_read_mem (int tfnum, CORE_ADDR addr,
4873 unsigned char *buf, ULONGEST length,
4874 ULONGEST *nbytes)
4875{
4876 struct traceframe *tframe;
4877 unsigned char *database, *dataptr;
4878 unsigned int datasize;
4879 CORE_ADDR maddr;
4880 unsigned short mlen;
4881
4882 trace_debug ("traceframe_read_mem");
4883
4884 tframe = find_traceframe (tfnum);
4885
4886 if (!tframe)
4887 {
4888 trace_debug ("traceframe %d not found", tfnum);
4889 return 1;
4890 }
4891
4892 datasize = tframe->data_size;
4893 database = dataptr = &tframe->data[0];
4894
4895 /* Iterate through a traceframe's blocks, looking for memory. */
4896 while ((dataptr = traceframe_find_block_type (dataptr,
493e2a69
MS
4897 datasize
4898 - (dataptr - database),
219f2f23
PA
4899 tfnum, 'M')) != NULL)
4900 {
4901 memcpy (&maddr, dataptr, sizeof (maddr));
4902 dataptr += sizeof (maddr);
4903 memcpy (&mlen, dataptr, sizeof (mlen));
4904 dataptr += sizeof (mlen);
4905 trace_debug ("traceframe %d has %d bytes at %s",
4906 tfnum, mlen, paddress (maddr));
4907
764880b7
PA
4908 /* If the block includes the first part of the desired range,
4909 return as much it has; GDB will re-request the remainder,
4910 which might be in a different block of this trace frame. */
4911 if (maddr <= addr && addr < (maddr + mlen))
219f2f23 4912 {
764880b7
PA
4913 ULONGEST amt = (maddr + mlen) - addr;
4914 if (amt > length)
4915 amt = length;
4916
4917 memcpy (buf, dataptr + (addr - maddr), amt);
4918 *nbytes = amt;
219f2f23
PA
4919 return 0;
4920 }
4921
4922 /* Skip over this block. */
4923 dataptr += mlen;
4924 }
4925
4926 trace_debug ("traceframe %d has no memory data for the desired region",
4927 tfnum);
4928
4929 *nbytes = 0;
4930 return 0;
4931}
4932
4933static int
4934traceframe_read_tsv (int tsvnum, LONGEST *val)
4935{
4936 int tfnum;
4937 struct traceframe *tframe;
4938 unsigned char *database, *dataptr;
4939 unsigned int datasize;
4940 int vnum;
4941
4942 trace_debug ("traceframe_read_tsv");
4943
4944 tfnum = current_traceframe;
4945
4946 if (tfnum < 0)
4947 {
4948 trace_debug ("no current traceframe");
4949 return 1;
4950 }
4951
4952 tframe = find_traceframe (tfnum);
4953
4954 if (tframe == NULL)
4955 {
4956 trace_debug ("traceframe %d not found", tfnum);
4957 return 1;
4958 }
4959
4960 datasize = tframe->data_size;
4961 database = dataptr = &tframe->data[0];
4962
4963 /* Iterate through a traceframe's blocks, looking for the tsv. */
4964 while ((dataptr = traceframe_find_block_type (dataptr,
493e2a69
MS
4965 datasize
4966 - (dataptr - database),
219f2f23
PA
4967 tfnum, 'V')) != NULL)
4968 {
4969 memcpy (&vnum, dataptr, sizeof (vnum));
4970 dataptr += sizeof (vnum);
4971
4972 trace_debug ("traceframe %d has variable %d", tfnum, vnum);
4973
4974 /* Check that this is the variable we want. */
4975 if (tsvnum == vnum)
4976 {
4977 memcpy (val, dataptr, sizeof (*val));
4978 return 0;
4979 }
4980
4981 /* Skip over this block. */
4982 dataptr += sizeof (LONGEST);
4983 }
4984
4985 trace_debug ("traceframe %d has no data for variable %d",
4986 tfnum, tsvnum);
4987 return 1;
4988}
4989
0fb4aa4b
PA
4990/* Read a requested block of static tracepoint data from a trace
4991 frame. */
4992
4993int
4994traceframe_read_sdata (int tfnum, ULONGEST offset,
4995 unsigned char *buf, ULONGEST length,
4996 ULONGEST *nbytes)
4997{
4998 struct traceframe *tframe;
4999 unsigned char *database, *dataptr;
5000 unsigned int datasize;
5001 unsigned short mlen;
5002
5003 trace_debug ("traceframe_read_sdata");
5004
5005 tframe = find_traceframe (tfnum);
5006
5007 if (!tframe)
5008 {
5009 trace_debug ("traceframe %d not found", tfnum);
5010 return 1;
5011 }
5012
5013 datasize = tframe->data_size;
5014 database = &tframe->data[0];
5015
5016 /* Iterate through a traceframe's blocks, looking for static
5017 tracepoint data. */
5018 dataptr = traceframe_find_block_type (database, datasize,
5019 tfnum, 'S');
5020 if (dataptr != NULL)
5021 {
5022 memcpy (&mlen, dataptr, sizeof (mlen));
5023 dataptr += sizeof (mlen);
5024 if (offset < mlen)
5025 {
5026 if (offset + length > mlen)
5027 length = mlen - offset;
5028
5029 memcpy (buf, dataptr, length);
5030 *nbytes = length;
5031 }
5032 else
5033 *nbytes = 0;
5034 return 0;
5035 }
5036
5037 trace_debug ("traceframe %d has no static trace data", tfnum);
5038
5039 *nbytes = 0;
5040 return 0;
5041}
5042
b3b9301e
PA
5043/* Callback for traceframe_walk_blocks. Builds a traceframe-info
5044 object. DATA is pointer to a struct buffer holding the
5045 traceframe-info object being built. */
5046
5047static int
5048build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
5049{
5050 struct buffer *buffer = data;
5051
5052 switch (blocktype)
5053 {
5054 case 'M':
5055 {
5056 unsigned short mlen;
5057 CORE_ADDR maddr;
5058
5059 memcpy (&maddr, dataptr, sizeof (maddr));
5060 dataptr += sizeof (maddr);
5061 memcpy (&mlen, dataptr, sizeof (mlen));
5062 dataptr += sizeof (mlen);
5063 buffer_xml_printf (buffer,
5064 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5065 paddress (maddr), phex_nz (mlen, sizeof (mlen)));
5066 break;
5067 }
5068 case 'V':
5069 case 'R':
5070 case 'S':
5071 {
5072 break;
5073 }
5074 default:
5075 warning ("Unhandled trace block type (%d) '%c ' "
5076 "while building trace frame info.",
5077 blocktype, blocktype);
5078 break;
5079 }
5080
5081 return 0;
5082}
5083
5084/* Build a traceframe-info object for traceframe number TFNUM into
5085 BUFFER. */
5086
5087int
5088traceframe_read_info (int tfnum, struct buffer *buffer)
5089{
5090 struct traceframe *tframe;
5091
5092 trace_debug ("traceframe_read_info");
5093
5094 tframe = find_traceframe (tfnum);
5095
5096 if (!tframe)
5097 {
5098 trace_debug ("traceframe %d not found", tfnum);
5099 return 1;
5100 }
5101
5102 buffer_grow_str (buffer, "<traceframe-info>\n");
5103 traceframe_walk_blocks (tframe->data, tframe->data_size,
5104 tfnum, build_traceframe_info_xml, buffer);
5105 buffer_grow_str0 (buffer, "</traceframe-info>\n");
5106 return 0;
5107}
5108
fa593d66
PA
5109/* Return the first fast tracepoint whose jump pad contains PC. */
5110
5111static struct tracepoint *
5112fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
5113{
5114 struct tracepoint *tpoint;
5115
5116 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5117 if (tpoint->type == fast_tracepoint)
5118 if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
5119 return tpoint;
5120
5121 return NULL;
5122}
5123
405f8e94
SS
5124/* Return the first fast tracepoint whose trampoline contains PC. */
5125
5126static struct tracepoint *
5127fast_tracepoint_from_trampoline_address (CORE_ADDR pc)
5128{
5129 struct tracepoint *tpoint;
5130
5131 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5132 {
5133 if (tpoint->type == fast_tracepoint
5134 && tpoint->trampoline <= pc && pc < tpoint->trampoline_end)
5135 return tpoint;
5136 }
5137
5138 return NULL;
5139}
5140
fa593d66
PA
5141/* Return GDBserver's tracepoint that matches the IP Agent's
5142 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5143 address space. */
5144
5145static struct tracepoint *
5146fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
5147{
5148 struct tracepoint *tpoint;
5149
5150 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5151 if (tpoint->type == fast_tracepoint)
5152 if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
5153 return tpoint;
5154
5155 return NULL;
5156}
5157
5158#endif
5159
5160/* The type of the object that is used to synchronize fast tracepoint
5161 collection. */
5162
5163typedef struct collecting_t
5164{
5165 /* The fast tracepoint number currently collecting. */
5166 uintptr_t tpoint;
5167
5168 /* A number that GDBserver can use to identify the thread that is
5169 presently holding the collect lock. This need not (and usually
5170 is not) the thread id, as getting the current thread ID usually
5171 requires a system call, which we want to avoid like the plague.
5172 Usually this is thread's TCB, found in the TLS (pseudo-)
5173 register, which is readable with a single insn on several
5174 architectures. */
5175 uintptr_t thread_area;
5176} collecting_t;
5177
5178#ifndef IN_PROCESS_AGENT
5179
5180void
5181force_unlock_trace_buffer (void)
5182{
5183 write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
5184}
5185
5186/* Check if the thread identified by THREAD_AREA which is stopped at
5187 STOP_PC, is presently locking the fast tracepoint collection, and
5188 if so, gather some status of said collection. Returns 0 if the
5189 thread isn't collecting or in the jump pad at all. 1, if in the
5190 jump pad (or within gdb_collect) and hasn't executed the adjusted
5191 original insn yet (can set a breakpoint there and run to it). 2,
5192 if presently executing the adjusted original insn --- in which
5193 case, if we want to move the thread out of the jump pad, we need to
5194 single-step it until this function returns 0. */
5195
5196int
5197fast_tracepoint_collecting (CORE_ADDR thread_area,
5198 CORE_ADDR stop_pc,
5199 struct fast_tpoint_collect_status *status)
5200{
5201 CORE_ADDR ipa_collecting;
5202 CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
405f8e94
SS
5203 CORE_ADDR ipa_gdb_trampoline_buffer;
5204 CORE_ADDR ipa_gdb_trampoline_buffer_end;
fa593d66
PA
5205 struct tracepoint *tpoint;
5206 int needs_breakpoint;
5207
5208 /* The thread THREAD_AREA is either:
5209
5210 0. not collecting at all, not within the jump pad, or within
5211 gdb_collect or one of its callees.
5212
5213 1. in the jump pad and haven't reached gdb_collect
5214
5215 2. within gdb_collect (out of the jump pad) (collect is set)
5216
5217 3. we're in the jump pad, after gdb_collect having returned,
5218 possibly executing the adjusted insns.
5219
5220 For cases 1 and 3, `collecting' may or not be set. The jump pad
5221 doesn't have any complicated jump logic, so we can tell if the
5222 thread is executing the adjust original insn or not by just
5223 matching STOP_PC with known jump pad addresses. If we it isn't
5224 yet executing the original insn, set a breakpoint there, and let
5225 the thread run to it, so to quickly step over a possible (many
5226 insns) gdb_collect call. Otherwise, or when the breakpoint is
5227 hit, only a few (small number of) insns are left to be executed
5228 in the jump pad. Single-step the thread until it leaves the
5229 jump pad. */
5230
5231 again:
5232 tpoint = NULL;
5233 needs_breakpoint = 0;
5234 trace_debug ("fast_tracepoint_collecting");
5235
5236 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
5237 &ipa_gdb_jump_pad_buffer))
5238 fatal ("error extracting `gdb_jump_pad_buffer'");
5239 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
5240 &ipa_gdb_jump_pad_buffer_end))
5241 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5242
405f8e94
SS
5243 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
5244 &ipa_gdb_trampoline_buffer))
5245 fatal ("error extracting `gdb_trampoline_buffer'");
5246 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
5247 &ipa_gdb_trampoline_buffer_end))
5248 fatal ("error extracting `gdb_trampoline_buffer_end'");
5249
493e2a69
MS
5250 if (ipa_gdb_jump_pad_buffer <= stop_pc
5251 && stop_pc < ipa_gdb_jump_pad_buffer_end)
fa593d66
PA
5252 {
5253 /* We can tell which tracepoint(s) the thread is collecting by
5254 matching the jump pad address back to the tracepoint. */
5255 tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
5256 if (tpoint == NULL)
5257 {
5258 warning ("in jump pad, but no matching tpoint?");
5259 return 0;
5260 }
5261 else
5262 {
5263 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5264 "adj_insn(%s, %s)",
5265 tpoint->number, paddress (tpoint->address),
5266 paddress (tpoint->jump_pad),
5267 paddress (tpoint->jump_pad_end),
5268 paddress (tpoint->adjusted_insn_addr),
5269 paddress (tpoint->adjusted_insn_addr_end));
5270 }
5271
5272 /* Definitely in the jump pad. May or may not need
5273 fast-exit-jump-pad breakpoint. */
5274 if (tpoint->jump_pad <= stop_pc
5275 && stop_pc < tpoint->adjusted_insn_addr)
5276 needs_breakpoint = 1;
5277 }
405f8e94
SS
5278 else if (ipa_gdb_trampoline_buffer <= stop_pc
5279 && stop_pc < ipa_gdb_trampoline_buffer_end)
5280 {
5281 /* We can tell which tracepoint(s) the thread is collecting by
5282 matching the trampoline address back to the tracepoint. */
5283 tpoint = fast_tracepoint_from_trampoline_address (stop_pc);
5284 if (tpoint == NULL)
5285 {
5286 warning ("in trampoline, but no matching tpoint?");
5287 return 0;
5288 }
5289 else
5290 {
5291 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5292 tpoint->number, paddress (tpoint->address),
5293 paddress (tpoint->trampoline),
5294 paddress (tpoint->trampoline_end));
5295 }
5296
5297 /* Have not reached jump pad yet, but treat the trampoline as a
5298 part of the jump pad that is before the adjusted original
5299 instruction. */
5300 needs_breakpoint = 1;
5301 }
fa593d66
PA
5302 else
5303 {
5304 collecting_t ipa_collecting_obj;
5305
5306 /* If `collecting' is set/locked, then the THREAD_AREA thread
5307 may or not be the one holding the lock. We have to read the
5308 lock to find out. */
5309
5310 if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
5311 &ipa_collecting))
5312 {
5313 trace_debug ("fast_tracepoint_collecting:"
5314 " failed reading 'collecting' in the inferior");
5315 return 0;
5316 }
5317
5318 if (!ipa_collecting)
5319 {
5320 trace_debug ("fast_tracepoint_collecting: not collecting"
5321 " (and nobody is).");
5322 return 0;
5323 }
5324
5325 /* Some thread is collecting. Check which. */
5326 if (read_inferior_memory (ipa_collecting,
5327 (unsigned char *) &ipa_collecting_obj,
5328 sizeof (ipa_collecting_obj)) != 0)
5329 goto again;
5330
5331 if (ipa_collecting_obj.thread_area != thread_area)
5332 {
5333 trace_debug ("fast_tracepoint_collecting: not collecting "
5334 "(another thread is)");
5335 return 0;
5336 }
5337
5338 tpoint
5339 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
5340 if (tpoint == NULL)
5341 {
5342 warning ("fast_tracepoint_collecting: collecting, "
5343 "but tpoint %s not found?",
5344 paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
5345 return 0;
5346 }
5347
5348 /* The thread is within `gdb_collect', skip over the rest of
5349 fast tracepoint collection quickly using a breakpoint. */
5350 needs_breakpoint = 1;
5351 }
5352
5353 /* The caller wants a bit of status detail. */
5354 if (status != NULL)
5355 {
5356 status->tpoint_num = tpoint->number;
5357 status->tpoint_addr = tpoint->address;
5358 status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
5359 status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
5360 }
5361
5362 if (needs_breakpoint)
5363 {
5364 /* Hasn't executed the original instruction yet. Set breakpoint
5365 there, and wait till it's hit, then single-step until exiting
5366 the jump pad. */
5367
5368 trace_debug ("\
5369fast_tracepoint_collecting, returning continue-until-break at %s",
5370 paddress (tpoint->adjusted_insn_addr));
5371
5372 return 1; /* continue */
5373 }
5374 else
5375 {
5376 /* Just single-step until exiting the jump pad. */
5377
5378 trace_debug ("fast_tracepoint_collecting, returning "
5379 "need-single-step (%s-%s)",
5380 paddress (tpoint->adjusted_insn_addr),
5381 paddress (tpoint->adjusted_insn_addr_end));
5382
5383 return 2; /* single-step */
5384 }
5385}
5386
5387#endif
5388
5389#ifdef IN_PROCESS_AGENT
5390
5391/* The global fast tracepoint collect lock. Points to a collecting_t
5392 object built on the stack by the jump pad, if presently locked;
5393 NULL if it isn't locked. Note that this lock *must* be set while
5394 executing any *function other than the jump pad. See
5395 fast_tracepoint_collecting. */
5396static collecting_t * ATTR_USED collecting;
5397
5398/* This routine, called from the jump pad (in asm) is designed to be
5399 called from the jump pads of fast tracepoints, thus it is on the
5400 critical path. */
5401
5402IP_AGENT_EXPORT void ATTR_USED
5403gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
5404{
5405 struct fast_tracepoint_ctx ctx;
5406
5407 /* Don't do anything until the trace run is completely set up. */
5408 if (!tracing)
5409 return;
5410
5411 ctx.base.type = fast_tracepoint;
5412 ctx.regs = regs;
5413 ctx.regcache_initted = 0;
fa593d66
PA
5414 /* Wrap the regblock in a register cache (in the stack, we don't
5415 want to malloc here). */
5416 ctx.regspace = alloca (register_cache_size ());
5417 if (ctx.regspace == NULL)
5418 {
5419 trace_debug ("Trace buffer block allocation failed, skipping");
5420 return;
5421 }
5422
a59306a3
YQ
5423 for (ctx.tpoint = tpoint;
5424 ctx.tpoint != NULL && ctx.tpoint->address == tpoint->address;
5425 ctx.tpoint = ctx.tpoint->next)
fa593d66 5426 {
a59306a3
YQ
5427 if (!ctx.tpoint->enabled)
5428 continue;
fa593d66 5429
a59306a3
YQ
5430 /* Multiple tracepoints of different types, such as fast tracepoint and
5431 static tracepoint, can be set at the same address. */
5432 if (ctx.tpoint->type != tpoint->type)
5433 continue;
5434
5435 /* Test the condition if present, and collect if true. */
5436 if (ctx.tpoint->cond == NULL
5437 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5438 ctx.tpoint))
5439 {
5440 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5441 ctx.tpoint->address, ctx.tpoint);
5442
5443 /* Note that this will cause original insns to be written back
5444 to where we jumped from, but that's OK because we're jumping
5445 back to the next whole instruction. This will go badly if
5446 instruction restoration is not atomic though. */
5447 if (stopping_tracepoint
5448 || trace_buffer_is_full
5449 || expr_eval_result != expr_eval_no_error)
5450 {
5451 stop_tracing ();
5452 break;
5453 }
5454 }
5455 else
5456 {
5457 /* If there was a condition and it evaluated to false, the only
5458 way we would stop tracing is if there was an error during
5459 condition expression evaluation. */
5460 if (expr_eval_result != expr_eval_no_error)
5461 {
5462 stop_tracing ();
5463 break;
5464 }
5465 }
fa593d66
PA
5466 }
5467}
5468
5469#endif
5470
5471#ifndef IN_PROCESS_AGENT
5472
6a271cae
PA
5473CORE_ADDR
5474get_raw_reg_func_addr (void)
5475{
5476 return ipa_sym_addrs.addr_get_raw_reg;
5477}
5478
5e1dc496
LM
5479CORE_ADDR
5480get_get_tsv_func_addr (void)
6b9801d4 5481{
5e1dc496 5482 return ipa_sym_addrs.addr_get_trace_state_variable_value;
6b9801d4
SS
5483}
5484
5e1dc496
LM
5485CORE_ADDR
5486get_set_tsv_func_addr (void)
6b9801d4 5487{
5e1dc496 5488 return ipa_sym_addrs.addr_set_trace_state_variable_value;
6b9801d4
SS
5489}
5490
6a271cae 5491static void
493e2a69
MS
5492compile_tracepoint_condition (struct tracepoint *tpoint,
5493 CORE_ADDR *jump_entry)
6a271cae
PA
5494{
5495 CORE_ADDR entry_point = *jump_entry;
5496 enum eval_result_type err;
5497
5498 trace_debug ("Starting condition compilation for tracepoint %d\n",
5499 tpoint->number);
5500
5501 /* Initialize the global pointer to the code being built. */
5502 current_insn_ptr = *jump_entry;
5503
5504 emit_prologue ();
5505
5506 err = compile_bytecodes (tpoint->cond);
5507
5508 if (err == expr_eval_no_error)
5509 {
5510 emit_epilogue ();
5511
5512 /* Record the beginning of the compiled code. */
5513 tpoint->compiled_cond = entry_point;
5514
5515 trace_debug ("Condition compilation for tracepoint %d complete\n",
5516 tpoint->number);
5517 }
5518 else
5519 {
5520 /* Leave the unfinished code in situ, but don't point to it. */
5521
5522 tpoint->compiled_cond = 0;
5523
5524 trace_debug ("Condition compilation for tracepoint %d failed, "
5525 "error code %d",
5526 tpoint->number, err);
5527 }
5528
5529 /* Update the code pointer passed in. Note that we do this even if
5530 the compile fails, so that we can look at the partial results
5531 instead of letting them be overwritten. */
5532 *jump_entry = current_insn_ptr;
5533
5534 /* Leave a gap, to aid dump decipherment. */
5535 *jump_entry += 16;
5536}
5537
fa593d66
PA
5538/* We'll need to adjust these when we consider bi-arch setups, and big
5539 endian machines. */
5540
5541static int
5542write_inferior_data_ptr (CORE_ADDR where, CORE_ADDR ptr)
5543{
5544 return write_inferior_memory (where,
5545 (unsigned char *) &ptr, sizeof (void *));
5546}
5547
5548/* The base pointer of the IPA's heap. This is the only memory the
5549 IPA is allowed to use. The IPA should _not_ call the inferior's
5550 `malloc' during operation. That'd be slow, and, most importantly,
5551 it may not be safe. We may be collecting a tracepoint in a signal
5552 handler, for example. */
5553static CORE_ADDR target_tp_heap;
5554
5555/* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5556 to 8 bytes. */
5557
5558static CORE_ADDR
5559target_malloc (ULONGEST size)
5560{
5561 CORE_ADDR ptr;
5562
5563 if (target_tp_heap == 0)
5564 {
5565 /* We have the pointer *address*, need what it points to. */
5566 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
5567 &target_tp_heap))
5568 fatal ("could get target heap head pointer");
5569 }
5570
5571 ptr = target_tp_heap;
5572 target_tp_heap += size;
5573
5574 /* Pad to 8-byte alignment. */
5575 target_tp_heap = ((target_tp_heap + 7) & ~0x7);
5576
5577 return ptr;
5578}
5579
5580static CORE_ADDR
5581download_agent_expr (struct agent_expr *expr)
5582{
5583 CORE_ADDR expr_addr;
5584 CORE_ADDR expr_bytes;
5585
5586 expr_addr = target_malloc (sizeof (*expr));
5587 write_inferior_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
5588
5589 expr_bytes = target_malloc (expr->length);
5590 write_inferior_data_ptr (expr_addr + offsetof (struct agent_expr, bytes),
5591 expr_bytes);
5592 write_inferior_memory (expr_bytes, expr->bytes, expr->length);
5593
5594 return expr_addr;
5595}
5596
5597/* Align V up to N bits. */
5598#define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5599
5c73ff4e
YQ
5600/* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5601
fa593d66 5602static void
5c73ff4e 5603download_tracepoint_1 (struct tracepoint *tpoint)
fa593d66 5604{
5c73ff4e
YQ
5605 struct tracepoint target_tracepoint;
5606 CORE_ADDR tpptr = 0;
fa593d66 5607
5c73ff4e
YQ
5608 gdb_assert (tpoint->type == fast_tracepoint
5609 || tpoint->type == static_tracepoint);
fa593d66 5610
5c73ff4e 5611 if (tpoint->cond != NULL && target_emit_ops () != NULL)
fa593d66 5612 {
5c73ff4e 5613 CORE_ADDR jentry, jump_entry;
fa593d66 5614
5c73ff4e 5615 jentry = jump_entry = get_jump_space_head ();
fa593d66 5616
5c73ff4e 5617 if (tpoint->cond != NULL)
6a271cae 5618 {
5c73ff4e
YQ
5619 /* Pad to 8-byte alignment. (needed?) */
5620 /* Actually this should be left for the target to
5621 decide. */
5622 jentry = UALIGN (jentry, 8);
5623
5624 compile_tracepoint_condition (tpoint, &jentry);
5625 }
5626
5627 /* Pad to 8-byte alignment. */
5628 jentry = UALIGN (jentry, 8);
5629 claim_jump_space (jentry - jump_entry);
5630 }
6a271cae 5631
5c73ff4e 5632 target_tracepoint = *tpoint;
6a271cae 5633
5c73ff4e
YQ
5634 tpptr = target_malloc (sizeof (*tpoint));
5635 tpoint->obj_addr_on_target = tpptr;
5636
5637 /* Write the whole object. We'll fix up its pointers in a bit.
5638 Assume no next for now. This is fixed up above on the next
5639 iteration, if there's any. */
5640 target_tracepoint.next = NULL;
5641 /* Need to clear this here too, since we're downloading the
5642 tracepoints before clearing our own copy. */
5643 target_tracepoint.hit_count = 0;
5644
5645 write_inferior_memory (tpptr, (unsigned char *) &target_tracepoint,
5646 sizeof (target_tracepoint));
5647
5648 if (tpoint->cond)
5649 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
5650 cond),
5651 download_agent_expr (tpoint->cond));
5652
5653 if (tpoint->numactions)
5654 {
5655 int i;
5656 CORE_ADDR actions_array;
5657
5658 /* The pointers array. */
5659 actions_array
5660 = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
5661 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
5662 actions),
5663 actions_array);
5664
5665 /* Now for each pointer, download the action. */
5666 for (i = 0; i < tpoint->numactions; i++)
5667 {
5668 CORE_ADDR ipa_action = 0;
5669 struct tracepoint_action *action = tpoint->actions[i];
5670
5671 switch (action->type)
6a271cae 5672 {
5c73ff4e
YQ
5673 case 'M':
5674 ipa_action
5675 = target_malloc (sizeof (struct collect_memory_action));
5676 write_inferior_memory (ipa_action,
5677 (unsigned char *) action,
5678 sizeof (struct collect_memory_action));
5679 break;
5680 case 'R':
5681 ipa_action
5682 = target_malloc (sizeof (struct collect_registers_action));
5683 write_inferior_memory (ipa_action,
5684 (unsigned char *) action,
5685 sizeof (struct collect_registers_action));
5686 break;
5687 case 'X':
5688 {
5689 CORE_ADDR expr;
5690 struct eval_expr_action *eaction
5691 = (struct eval_expr_action *) action;
6a271cae 5692
5c73ff4e
YQ
5693 ipa_action = target_malloc (sizeof (*eaction));
5694 write_inferior_memory (ipa_action,
5695 (unsigned char *) eaction,
5696 sizeof (*eaction));
5697
5698 expr = download_agent_expr (eaction->expr);
5699 write_inferior_data_ptr
5700 (ipa_action + offsetof (struct eval_expr_action, expr),
5701 expr);
5702 break;
5703 }
5704 case 'L':
5705 ipa_action = target_malloc
5706 (sizeof (struct collect_static_trace_data_action));
5707 write_inferior_memory
5708 (ipa_action,
5709 (unsigned char *) action,
5710 sizeof (struct collect_static_trace_data_action));
5711 break;
5712 default:
5713 trace_debug ("unknown trace action '%c', ignoring",
5714 action->type);
5715 break;
6a271cae
PA
5716 }
5717
5c73ff4e
YQ
5718 if (ipa_action != 0)
5719 write_inferior_data_ptr
5720 (actions_array + i * sizeof (sizeof (*tpoint->actions)),
5721 ipa_action);
6a271cae 5722 }
5c73ff4e
YQ
5723 }
5724}
6a271cae 5725
1e4d1764
YQ
5726static void
5727download_tracepoint (struct tracepoint *tpoint)
5728{
5729 struct tracepoint *tp, *tp_prev;
5730
5731 if (tpoint->type != fast_tracepoint
5732 && tpoint->type != static_tracepoint)
5733 return;
5734
5735 download_tracepoint_1 (tpoint);
5736
5737 /* Find the previous entry of TPOINT, which is fast tracepoint or
5738 static tracepoint. */
5739 tp_prev = NULL;
5740 for (tp = tracepoints; tp != tpoint; tp = tp->next)
5741 {
5742 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
5743 tp_prev = tp;
5744 }
5745
5746 if (tp_prev)
5747 {
5748 CORE_ADDR tp_prev_target_next_addr;
5749
5750 /* Insert TPOINT after TP_PREV in IPA. */
5751 if (read_inferior_data_pointer (tp_prev->obj_addr_on_target
5752 + offsetof (struct tracepoint, next),
5753 &tp_prev_target_next_addr))
5754 fatal ("error reading `tp_prev->next'");
5755
5756 /* tpoint->next = tp_prev->next */
5757 write_inferior_data_ptr (tpoint->obj_addr_on_target
5758 + offsetof (struct tracepoint, next),
5759 tp_prev_target_next_addr);
5760 /* tp_prev->next = tpoint */
5761 write_inferior_data_ptr (tp_prev->obj_addr_on_target
5762 + offsetof (struct tracepoint, next),
5763 tpoint->obj_addr_on_target);
5764 }
5765 else
5766 /* First object in list, set the head pointer in the
5767 inferior. */
5768 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints,
5769 tpoint->obj_addr_on_target);
5770
5771}
5772
5c73ff4e
YQ
5773static void
5774download_tracepoints (void)
5775{
5776 CORE_ADDR tpptr = 0, prev_tpptr = 0;
5777 struct tracepoint *tpoint;
5778
5779 /* Start out empty. */
5780 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, 0);
5781
5782 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5783 {
5784 if (tpoint->type != fast_tracepoint
5785 && tpoint->type != static_tracepoint)
5786 continue;
fa593d66
PA
5787
5788 prev_tpptr = tpptr;
5c73ff4e
YQ
5789
5790 download_tracepoint_1 (tpoint);
5791
5792 tpptr = tpoint->obj_addr_on_target;
fa593d66
PA
5793
5794 if (tpoint == tracepoints)
5795 {
5796 /* First object in list, set the head pointer in the
5797 inferior. */
5798 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, tpptr);
5799 }
5800 else
5801 {
5802 write_inferior_data_ptr (prev_tpptr + offsetof (struct tracepoint,
5803 next),
5804 tpptr);
5805 }
fa593d66
PA
5806 }
5807}
5808
5809static void
5810download_trace_state_variables (void)
5811{
5812 CORE_ADDR ptr = 0, prev_ptr = 0;
5813 struct trace_state_variable *tsv;
5814
5815 /* Start out empty. */
5816 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables, 0);
5817
5818 for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
5819 {
5820 struct trace_state_variable target_tsv;
5821
5822 /* TSV's with a getter have been initialized equally in both the
5823 inferior and GDBserver. Skip them. */
5824 if (tsv->getter != NULL)
5825 continue;
5826
5827 target_tsv = *tsv;
5828
5829 prev_ptr = ptr;
5830 ptr = target_malloc (sizeof (*tsv));
5831
5832 if (tsv == trace_state_variables)
5833 {
5834 /* First object in list, set the head pointer in the
5835 inferior. */
5836
5837 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables,
5838 ptr);
5839 }
5840 else
5841 {
5842 write_inferior_data_ptr (prev_ptr
5843 + offsetof (struct trace_state_variable,
5844 next),
5845 ptr);
5846 }
5847
5848 /* Write the whole object. We'll fix up its pointers in a bit.
5849 Assume no next, fixup when needed. */
5850 target_tsv.next = NULL;
5851
5852 write_inferior_memory (ptr, (unsigned char *) &target_tsv,
5853 sizeof (target_tsv));
5854
5855 if (tsv->name != NULL)
5856 {
5857 size_t size = strlen (tsv->name) + 1;
5858 CORE_ADDR name_addr = target_malloc (size);
5859 write_inferior_memory (name_addr,
5860 (unsigned char *) tsv->name, size);
5861 write_inferior_data_ptr (ptr
5862 + offsetof (struct trace_state_variable,
5863 name),
5864 name_addr);
5865 }
5866
5867 if (tsv->getter != NULL)
5868 {
5869 fatal ("what to do with these?");
5870 }
5871 }
5872
5873 if (prev_ptr != 0)
5874 {
5875 /* Fixup the next pointer in the last item in the list. */
493e2a69
MS
5876 write_inferior_data_ptr (prev_ptr
5877 + offsetof (struct trace_state_variable,
5878 next), 0);
fa593d66
PA
5879 }
5880}
5881
5882/* Upload complete trace frames out of the IP Agent's trace buffer
5883 into GDBserver's trace buffer. This always uploads either all or
5884 no trace frames. This is the counter part of
5885 `trace_alloc_trace_buffer'. See its description of the atomic
5886 synching mechanism. */
5887
5888static void
5889upload_fast_traceframes (void)
5890{
5891 unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
5892 unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
5893 CORE_ADDR tf;
5894 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
5895 unsigned int curr_tbctrl_idx;
5896 unsigned int ipa_trace_buffer_ctrl_curr;
5897 unsigned int ipa_trace_buffer_ctrl_curr_old;
5898 CORE_ADDR ipa_trace_buffer_ctrl_addr;
5899 struct breakpoint *about_to_request_buffer_space_bkpt;
5900 CORE_ADDR ipa_trace_buffer_lo;
5901 CORE_ADDR ipa_trace_buffer_hi;
5902
5903 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
5904 &ipa_traceframe_read_count_racy))
5905 {
5906 /* This will happen in most targets if the current thread is
5907 running. */
5908 return;
5909 }
5910
5911 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
5912 &ipa_traceframe_write_count_racy))
5913 return;
5914
5915 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
493e2a69
MS
5916 ipa_traceframe_write_count_racy
5917 - ipa_traceframe_read_count_racy,
5918 ipa_traceframe_write_count_racy,
5919 ipa_traceframe_read_count_racy);
fa593d66
PA
5920
5921 if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
5922 return;
5923
5924 about_to_request_buffer_space_bkpt
5925 = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
5926 NULL);
5927
5928 if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
5929 &ipa_trace_buffer_ctrl_curr))
5930 return;
5931
5932 ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
5933
5934 curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
5935
5936 {
5937 unsigned int prev, counter;
5938
5939 /* Update the token, with new counters, and the GDBserver stamp
5940 bit. Alway reuse the current TBC index. */
2ece8244
YQ
5941 prev = ipa_trace_buffer_ctrl_curr & GDBSERVER_FLUSH_COUNT_MASK_CURR;
5942 counter = (prev + 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR;
fa593d66 5943
2ece8244 5944 ipa_trace_buffer_ctrl_curr = (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
fa593d66
PA
5945 | (prev << 12)
5946 | counter
5947 | curr_tbctrl_idx);
5948 }
5949
5950 if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
5951 ipa_trace_buffer_ctrl_curr))
5952 return;
5953
5954 trace_debug ("Lib: Committed %08x -> %08x",
5955 ipa_trace_buffer_ctrl_curr_old,
5956 ipa_trace_buffer_ctrl_curr);
5957
5958 /* Re-read these, now that we've installed the
5959 `about_to_request_buffer_space' breakpoint/lock. A thread could
5960 have finished a traceframe between the last read of these
5961 counters and setting the breakpoint above. If we start
5962 uploading, we never want to leave this function with
5963 traceframe_read_count != 0, otherwise, GDBserver could end up
5964 incrementing the counter tokens more than once (due to event loop
5965 nesting), which would break the IP agent's "effective" detection
5966 (see trace_alloc_trace_buffer). */
5967 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
5968 &ipa_traceframe_read_count))
5969 return;
5970 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
5971 &ipa_traceframe_write_count))
5972 return;
5973
5974 if (debug_threads)
5975 {
5976 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
5977 ipa_traceframe_write_count - ipa_traceframe_read_count,
5978 ipa_traceframe_write_count, ipa_traceframe_read_count);
5979
5980 if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
5981 || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
5982 trace_debug ("note that ipa_traceframe_count's parts changed");
5983 }
5984
5985 /* Get the address of the current TBC object (the IP agent has an
5986 array of 3 such objects). The index is stored in the TBC
5987 token. */
5988 ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
5989 ipa_trace_buffer_ctrl_addr
5990 += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
5991
5992 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
5993 (unsigned char *) &ipa_trace_buffer_ctrl,
5994 sizeof (struct ipa_trace_buffer_control)))
5995 return;
5996
5997 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
5998 &ipa_trace_buffer_lo))
5999 return;
6000 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
6001 &ipa_trace_buffer_hi))
6002 return;
6003
6004 /* Offsets are easier to grok for debugging than raw addresses,
6005 especially for the small trace buffer sizes that are useful for
6006 testing. */
6007 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6008 "endfree=%d wrap=%d hi=%d",
6009 curr_tbctrl_idx,
6010 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6011 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6012 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
6013 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6014 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6015
6016 /* Note that the IPA's buffer is always circular. */
6017
6018#define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6019
6020#define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6021 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6022
6023#define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6024 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6025 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6026 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6027 : 0))
6028
6029 tf = IPA_FIRST_TRACEFRAME ();
6030
6031 while (ipa_traceframe_write_count - ipa_traceframe_read_count)
6032 {
6033 struct tracepoint *tpoint;
6034 struct traceframe *tframe;
6035 unsigned char *block;
6036 struct traceframe ipa_tframe;
6037
6038 if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
6039 offsetof (struct traceframe, data)))
6040 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
6041
6042 if (ipa_tframe.tpnum == 0)
6043 fatal ("Uploading: No (more) fast traceframes, but "
6044 "ipa_traceframe_count == %u??\n",
6045 ipa_traceframe_write_count - ipa_traceframe_read_count);
6046
6047 /* Note that this will be incorrect for multi-location
6048 tracepoints... */
6049 tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
6050
6051 tframe = add_traceframe (tpoint);
6052 if (tframe == NULL)
6053 {
6054 trace_buffer_is_full = 1;
6055 trace_debug ("Uploading: trace buffer is full");
6056 }
6057 else
6058 {
6059 /* Copy the whole set of blocks in one go for now. FIXME:
6060 split this in smaller blocks. */
6061 block = add_traceframe_block (tframe, ipa_tframe.data_size);
6062 if (block != NULL)
6063 {
493e2a69
MS
6064 if (read_inferior_memory (tf
6065 + offsetof (struct traceframe, data),
fa593d66
PA
6066 block, ipa_tframe.data_size))
6067 error ("Uploading: Couldn't read traceframe data at %s\n",
6068 paddress (tf + offsetof (struct traceframe, data)));
6069 }
6070
6071 trace_debug ("Uploading: traceframe didn't fit");
6072 finish_traceframe (tframe);
6073 }
6074
6075 tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
6076
6077 /* If we freed the traceframe that wrapped around, go back
6078 to the non-wrap case. */
6079 if (tf < ipa_trace_buffer_ctrl.start)
6080 {
6081 trace_debug ("Lib: Discarding past the wraparound");
6082 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6083 }
6084 ipa_trace_buffer_ctrl.start = tf;
6085 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
6086 ++ipa_traceframe_read_count;
6087
6088 if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
6089 && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
6090 {
6091 trace_debug ("Lib: buffer is fully empty. "
6092 "Trace buffer [%d] start=%d free=%d endfree=%d",
6093 curr_tbctrl_idx,
6094 (int) (ipa_trace_buffer_ctrl.start
6095 - ipa_trace_buffer_lo),
6096 (int) (ipa_trace_buffer_ctrl.free
6097 - ipa_trace_buffer_lo),
6098 (int) (ipa_trace_buffer_ctrl.end_free
6099 - ipa_trace_buffer_lo));
6100
6101 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
6102 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
6103 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
6104 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6105 }
6106
6107 trace_debug ("Uploaded a traceframe\n"
6108 "Lib: Trace buffer [%d] start=%d free=%d "
6109 "endfree=%d wrap=%d hi=%d",
6110 curr_tbctrl_idx,
6111 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6112 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
493e2a69
MS
6113 (int) (ipa_trace_buffer_ctrl.end_free
6114 - ipa_trace_buffer_lo),
fa593d66
PA
6115 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6116 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6117 }
6118
6119 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr,
6120 (unsigned char *) &ipa_trace_buffer_ctrl,
6121 sizeof (struct ipa_trace_buffer_control)))
6122 return;
6123
6124 write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
6125 ipa_traceframe_read_count);
6126
6127 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
6128
6129 pause_all (1);
6130 cancel_breakpoints ();
6131
6132 delete_breakpoint (about_to_request_buffer_space_bkpt);
6133 about_to_request_buffer_space_bkpt = NULL;
6134
6135 unpause_all (1);
6136
6137 if (trace_buffer_is_full)
6138 stop_tracing ();
6139}
6140#endif
6141
6142#ifdef IN_PROCESS_AGENT
6143
0fb4aa4b 6144IP_AGENT_EXPORT int ust_loaded;
2fa291ac 6145IP_AGENT_EXPORT char cmd_buf[IPA_CMD_BUF_SIZE];
fa593d66 6146
0fb4aa4b 6147#ifdef HAVE_UST
fa593d66 6148
0fb4aa4b
PA
6149/* Static tracepoints. */
6150
6151/* UST puts a "struct tracepoint" in the global namespace, which
6152 conflicts with our tracepoint. Arguably, being a library, it
6153 shouldn't take ownership of such a generic name. We work around it
6154 here. */
6155#define tracepoint ust_tracepoint
6156#include <ust/ust.h>
6157#undef tracepoint
6158
6159extern int serialize_to_text (char *outbuf, int bufsize,
6160 const char *fmt, va_list ap);
6161
6162#define GDB_PROBE_NAME "gdb"
6163
6164/* We dynamically search for the UST symbols instead of linking them
6165 in. This lets the user decide if the application uses static
6166 tracepoints, instead of always pulling libust.so in. This vector
6167 holds pointers to all functions we care about. */
6168
6169static struct
fa593d66 6170{
0fb4aa4b
PA
6171 int (*serialize_to_text) (char *outbuf, int bufsize,
6172 const char *fmt, va_list ap);
6173
6174 int (*ltt_probe_register) (struct ltt_available_probe *pdata);
6175 int (*ltt_probe_unregister) (struct ltt_available_probe *pdata);
6176
6177 int (*ltt_marker_connect) (const char *channel, const char *mname,
6178 const char *pname);
6179 int (*ltt_marker_disconnect) (const char *channel, const char *mname,
6180 const char *pname);
6181
6182 void (*marker_iter_start) (struct marker_iter *iter);
6183 void (*marker_iter_next) (struct marker_iter *iter);
6184 void (*marker_iter_stop) (struct marker_iter *iter);
6185 void (*marker_iter_reset) (struct marker_iter *iter);
6186} ust_ops;
6187
6188#include <dlfcn.h>
6189
6190/* Cast through typeof to catch incompatible API changes. Since UST
6191 only builds with gcc, we can freely use gcc extensions here
6192 too. */
6193#define GET_UST_SYM(SYM) \
6194 do \
6195 { \
6196 if (ust_ops.SYM == NULL) \
6197 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6198 if (ust_ops.SYM == NULL) \
6199 return 0; \
6200 } while (0)
6201
6202#define USTF(SYM) ust_ops.SYM
6203
6204/* Get pointers to all libust.so functions we care about. */
6205
6206static int
6207dlsym_ust (void)
6208{
6209 GET_UST_SYM (serialize_to_text);
6210
6211 GET_UST_SYM (ltt_probe_register);
6212 GET_UST_SYM (ltt_probe_unregister);
6213 GET_UST_SYM (ltt_marker_connect);
6214 GET_UST_SYM (ltt_marker_disconnect);
6215
6216 GET_UST_SYM (marker_iter_start);
6217 GET_UST_SYM (marker_iter_next);
6218 GET_UST_SYM (marker_iter_stop);
6219 GET_UST_SYM (marker_iter_reset);
6220
6221 ust_loaded = 1;
6222 return 1;
fa593d66
PA
6223}
6224
0fb4aa4b
PA
6225/* Given an UST marker, return the matching gdb static tracepoint.
6226 The match is done by address. */
6227
6228static struct tracepoint *
6229ust_marker_to_static_tracepoint (const struct marker *mdata)
6230{
6231 struct tracepoint *tpoint;
6232
6233 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
6234 {
d248b706 6235 if (tpoint->type != static_tracepoint)
0fb4aa4b
PA
6236 continue;
6237
6238 if (tpoint->address == (uintptr_t) mdata->location)
6239 return tpoint;
6240 }
6241
6242 return NULL;
6243}
6244
6245/* The probe function we install on lttng/ust markers. Whenever a
6246 probed ust marker is hit, this function is called. This is similar
6247 to gdb_collect, only for static tracepoints, instead of fast
6248 tracepoints. */
6249
6250static void
6251gdb_probe (const struct marker *mdata, void *probe_private,
6252 struct registers *regs, void *call_private,
6253 const char *fmt, va_list *args)
6254{
6255 struct tracepoint *tpoint;
6256 struct static_tracepoint_ctx ctx;
6257
6258 /* Don't do anything until the trace run is completely set up. */
6259 if (!tracing)
6260 {
6261 trace_debug ("gdb_probe: not tracing\n");
6262 return;
6263 }
6264
6265 ctx.base.type = static_tracepoint;
6266 ctx.regcache_initted = 0;
6267 ctx.regs = regs;
6268 ctx.fmt = fmt;
6269 ctx.args = args;
6270
6271 /* Wrap the regblock in a register cache (in the stack, we don't
6272 want to malloc here). */
6273 ctx.regspace = alloca (register_cache_size ());
6274 if (ctx.regspace == NULL)
6275 {
6276 trace_debug ("Trace buffer block allocation failed, skipping");
6277 return;
6278 }
6279
6280 tpoint = ust_marker_to_static_tracepoint (mdata);
6281 if (tpoint == NULL)
6282 {
6283 trace_debug ("gdb_probe: marker not known: "
6284 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6285 mdata->location, mdata->channel,
6286 mdata->name, mdata->format);
6287 return;
6288 }
6289
d248b706
KY
6290 if (!tpoint->enabled)
6291 {
6292 trace_debug ("gdb_probe: tracepoint disabled");
6293 return;
6294 }
6295
0fb4aa4b
PA
6296 ctx.tpoint = tpoint;
6297
6298 trace_debug ("gdb_probe: collecting marker: "
6299 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6300 mdata->location, mdata->channel,
6301 mdata->name, mdata->format);
6302
6303 /* Test the condition if present, and collect if true. */
6304 if (tpoint->cond == NULL
6305 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6306 tpoint))
6307 {
6308 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6309 tpoint->address, tpoint);
6310
6311 if (stopping_tracepoint
6312 || trace_buffer_is_full
6313 || expr_eval_result != expr_eval_no_error)
6314 stop_tracing ();
6315 }
6316 else
6317 {
6318 /* If there was a condition and it evaluated to false, the only
6319 way we would stop tracing is if there was an error during
6320 condition expression evaluation. */
6321 if (expr_eval_result != expr_eval_no_error)
6322 stop_tracing ();
6323 }
6324}
6325
6326/* Called if the gdb static tracepoint requested collecting "$_sdata",
6327 static tracepoint string data. This is a string passed to the
6328 tracing library by the user, at the time of the tracepoint marker
6329 call. E.g., in the UST marker call:
6330
6331 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6332
6333 the collected data is "str FOOBAZ".
6334*/
6335
6336static void
6337collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
0fb4aa4b
PA
6338 struct traceframe *tframe)
6339{
6340 struct static_tracepoint_ctx *umd = (struct static_tracepoint_ctx *) ctx;
6341 unsigned char *bufspace;
6342 int size;
6343 va_list copy;
6344 unsigned short blocklen;
6345
6346 if (umd == NULL)
6347 {
6348 trace_debug ("Wanted to collect static trace data, "
6349 "but there's no static trace data");
6350 return;
6351 }
6352
6353 va_copy (copy, *umd->args);
6354 size = USTF(serialize_to_text) (NULL, 0, umd->fmt, copy);
6355 va_end (copy);
6356
6357 trace_debug ("Want to collect ust data");
6358
6359 /* 'S' + size + string */
6360 bufspace = add_traceframe_block (tframe,
6361 1 + sizeof (blocklen) + size + 1);
6362 if (bufspace == NULL)
6363 {
6364 trace_debug ("Trace buffer block allocation failed, skipping");
6365 return;
6366 }
6367
6368 /* Identify a static trace data block. */
6369 *bufspace = 'S';
6370
6371 blocklen = size + 1;
6372 memcpy (bufspace + 1, &blocklen, sizeof (blocklen));
6373
6374 va_copy (copy, *umd->args);
6375 USTF(serialize_to_text) ((char *) bufspace + 1 + sizeof (blocklen),
6376 size + 1, umd->fmt, copy);
6377 va_end (copy);
6378
6379 trace_debug ("Storing static tracepoint data in regblock: %s",
6380 bufspace + 1 + sizeof (blocklen));
6381}
6382
6383/* The probe to register with lttng/ust. */
6384static struct ltt_available_probe gdb_ust_probe =
6385 {
6386 GDB_PROBE_NAME,
6387 NULL,
6388 gdb_probe,
6389 };
6390
6391#endif /* HAVE_UST */
6392#endif /* IN_PROCESS_AGENT */
6393
0fb4aa4b
PA
6394#ifndef IN_PROCESS_AGENT
6395
0fb4aa4b
PA
6396/* Ask the in-process agent to run a command. Since we don't want to
6397 have to handle the IPA hitting breakpoints while running the
6398 command, we pause all threads, remove all breakpoints, and then set
6399 the helper thread re-running. We communicate with the helper
6400 thread by means of direct memory xfering, and a socket for
6401 synchronization. */
6402
6403static int
6404run_inferior_command (char *cmd)
6405{
6406 int err = -1;
0fb4aa4b 6407 int pid = ptid_get_pid (current_inferior->entry.id);
0fb4aa4b
PA
6408
6409 trace_debug ("run_inferior_command: running: %s", cmd);
6410
6411 pause_all (0);
6412 uninsert_all_breakpoints ();
6413
2fa291ac 6414 err = agent_run_command (pid, (const char *) cmd);
0fb4aa4b
PA
6415
6416 reinsert_all_breakpoints ();
6417 unpause_all (0);
6418
6419 return err;
6420}
6421
2fa291ac 6422#else /* !IN_PROCESS_AGENT */
0fb4aa4b 6423
2fa291ac
YQ
6424#include <sys/socket.h>
6425#include <sys/un.h>
0fb4aa4b 6426
2fa291ac
YQ
6427#ifndef UNIX_PATH_MAX
6428#define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6429#endif
0fb4aa4b 6430
2fa291ac
YQ
6431/* Where we put the socked used for synchronization. */
6432#define SOCK_DIR P_tmpdir
0fb4aa4b
PA
6433
6434/* Thread ID of the helper thread. GDBserver reads this to know which
6435 is the help thread. This is an LWP id on Linux. */
6436int helper_thread_id;
6437
0fb4aa4b
PA
6438static int
6439init_named_socket (const char *name)
6440{
6441 int result, fd;
6442 struct sockaddr_un addr;
6443
6444 result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
6445 if (result == -1)
6446 {
6447 warning ("socket creation failed: %s", strerror (errno));
6448 return -1;
6449 }
6450
6451 addr.sun_family = AF_UNIX;
6452
6453 strncpy (addr.sun_path, name, UNIX_PATH_MAX);
6454 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
6455
6456 result = access (name, F_OK);
6457 if (result == 0)
6458 {
6459 /* File exists. */
6460 result = unlink (name);
6461 if (result == -1)
6462 {
6463 warning ("unlink failed: %s", strerror (errno));
6464 close (fd);
6465 return -1;
6466 }
6467 warning ("socket %s already exists; overwriting", name);
6468 }
6469
6470 result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
6471 if (result == -1)
6472 {
6473 warning ("bind failed: %s", strerror (errno));
6474 close (fd);
6475 return -1;
6476 }
6477
6478 result = listen (fd, 1);
6479 if (result == -1)
6480 {
6481 warning ("listen: %s", strerror (errno));
6482 close (fd);
6483 return -1;
6484 }
6485
6486 return fd;
6487}
6488
6489static int
2fa291ac 6490gdb_agent_socket_init (void)
0fb4aa4b
PA
6491{
6492 int result, fd;
6493 char name[UNIX_PATH_MAX];
6494
6cebaf6e 6495 result = xsnprintf (name, UNIX_PATH_MAX, "%s/gdb_ust%d",
6496 SOCK_DIR, getpid ());
0fb4aa4b
PA
6497 if (result >= UNIX_PATH_MAX)
6498 {
6499 trace_debug ("string overflow allocating socket name");
6500 return -1;
6501 }
6502
6503 fd = init_named_socket (name);
6504 if (fd < 0)
6505 warning ("Error initializing named socket (%s) for communication with the "
6506 "ust helper thread. Check that directory exists and that it "
6507 "is writable.", name);
6508
6509 return fd;
6510}
6511
2fa291ac 6512#ifdef HAVE_UST
0fb4aa4b
PA
6513
6514/* The next marker to be returned on a qTsSTM command. */
6515static const struct marker *next_st;
6516
6517/* Returns the first known marker. */
6518
6519struct marker *
6520first_marker (void)
6521{
6522 struct marker_iter iter;
6523
6524 USTF(marker_iter_reset) (&iter);
6525 USTF(marker_iter_start) (&iter);
6526
6527 return iter.marker;
6528}
6529
6530/* Returns the marker following M. */
6531
6532const struct marker *
6533next_marker (const struct marker *m)
6534{
6535 struct marker_iter iter;
6536
6537 USTF(marker_iter_reset) (&iter);
6538 USTF(marker_iter_start) (&iter);
6539
6540 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
6541 {
6542 if (iter.marker == m)
6543 {
6544 USTF(marker_iter_next) (&iter);
6545 return iter.marker;
6546 }
6547 }
6548
6549 return NULL;
6550}
6551
2fa291ac
YQ
6552/* Return an hexstr version of the STR C string, fit for sending to
6553 GDB. */
6554
6555static char *
6556cstr_to_hexstr (const char *str)
6557{
6558 int len = strlen (str);
6559 char *hexstr = xmalloc (len * 2 + 1);
6560 convert_int_to_ascii ((gdb_byte *) str, hexstr, len);
6561 return hexstr;
6562}
6563
0fb4aa4b
PA
6564/* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6565 packets. */
6566
6567static void
6568response_ust_marker (char *packet, const struct marker *st)
6569{
6570 char *strid, *format, *tmp;
6571
6572 next_st = next_marker (st);
6573
6574 tmp = xmalloc (strlen (st->channel) + 1 +
6575 strlen (st->name) + 1);
6576 sprintf (tmp, "%s/%s", st->channel, st->name);
6577
6578 strid = cstr_to_hexstr (tmp);
6579 free (tmp);
6580
6581 format = cstr_to_hexstr (st->format);
6582
6583 sprintf (packet, "m%s:%s:%s",
6584 paddress ((uintptr_t) st->location),
6585 strid,
6586 format);
6587
6588 free (strid);
6589 free (format);
6590}
6591
6592/* Return the first static tracepoint, and initialize the state
6593 machine that will iterate through all the static tracepoints. */
6594
6595static void
6596cmd_qtfstm (char *packet)
6597{
6598 trace_debug ("Returning first trace state variable definition");
6599
6600 if (first_marker ())
6601 response_ust_marker (packet, first_marker ());
6602 else
6603 strcpy (packet, "l");
6604}
6605
6606/* Return additional trace state variable definitions. */
6607
6608static void
6609cmd_qtsstm (char *packet)
6610{
6611 trace_debug ("Returning static tracepoint");
6612
6613 if (next_st)
6614 response_ust_marker (packet, next_st);
6615 else
6616 strcpy (packet, "l");
6617}
6618
6619/* Disconnect the GDB probe from a marker at a given address. */
6620
6621static void
6622unprobe_marker_at (char *packet)
6623{
6624 char *p = packet;
6625 ULONGEST address;
6626 struct marker_iter iter;
6627
6628 p += sizeof ("unprobe_marker_at:") - 1;
6629
6630 p = unpack_varlen_hex (p, &address);
6631
6632 USTF(marker_iter_reset) (&iter);
6633 USTF(marker_iter_start) (&iter);
6634 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
6635 if ((uintptr_t ) iter.marker->location == address)
6636 {
6637 int result;
6638
6639 result = USTF(ltt_marker_disconnect) (iter.marker->channel,
6640 iter.marker->name,
6641 GDB_PROBE_NAME);
6642 if (result < 0)
6643 warning ("could not disable marker %s/%s",
6644 iter.marker->channel, iter.marker->name);
6645 break;
6646 }
6647}
6648
6649/* Connect the GDB probe to a marker at a given address. */
6650
6651static int
6652probe_marker_at (char *packet)
6653{
6654 char *p = packet;
6655 ULONGEST address;
6656 struct marker_iter iter;
6657 struct marker *m;
6658
6659 p += sizeof ("probe_marker_at:") - 1;
6660
6661 p = unpack_varlen_hex (p, &address);
6662
6663 USTF(marker_iter_reset) (&iter);
6664
6665 for (USTF(marker_iter_start) (&iter), m = iter.marker;
6666 m != NULL;
6667 USTF(marker_iter_next) (&iter), m = iter.marker)
6668 if ((uintptr_t ) m->location == address)
6669 {
6670 int result;
6671
6672 trace_debug ("found marker for address. "
6673 "ltt_marker_connect (marker = %s/%s)",
6674 m->channel, m->name);
6675
493e2a69
MS
6676 result = USTF(ltt_marker_connect) (m->channel, m->name,
6677 GDB_PROBE_NAME);
0fb4aa4b
PA
6678 if (result && result != -EEXIST)
6679 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
6680 m->channel, m->name, -result);
6681
6682 if (result < 0)
6683 {
6684 sprintf (packet, "E.could not connect marker: channel=%s, name=%s",
6685 m->channel, m->name);
6686 return -1;
6687 }
6688
6689 strcpy (packet, "OK");
6690 return 0;
6691 }
6692
6693 sprintf (packet, "E.no marker found at 0x%s", paddress (address));
6694 return -1;
6695}
6696
6697static int
6698cmd_qtstmat (char *packet)
6699{
6700 char *p = packet;
6701 ULONGEST address;
6702 struct marker_iter iter;
6703 struct marker *m;
6704
6705 p += sizeof ("qTSTMat:") - 1;
6706
6707 p = unpack_varlen_hex (p, &address);
6708
6709 USTF(marker_iter_reset) (&iter);
6710
6711 for (USTF(marker_iter_start) (&iter), m = iter.marker;
6712 m != NULL;
6713 USTF(marker_iter_next) (&iter), m = iter.marker)
6714 if ((uintptr_t ) m->location == address)
6715 {
6716 response_ust_marker (packet, m);
6717 return 0;
6718 }
6719
6720 strcpy (packet, "l");
6721 return -1;
6722}
6723
2fa291ac
YQ
6724static void
6725gdb_ust_init (void)
6726{
6727 if (!dlsym_ust ())
6728 return;
6729
6730 USTF(ltt_probe_register) (&gdb_ust_probe);
6731}
6732
6733#endif /* HAVE_UST */
6734
82067193
YQ
6735#include <sys/syscall.h>
6736
2fa291ac
YQ
6737/* Helper thread of agent. */
6738
0fb4aa4b 6739static void *
2fa291ac 6740gdb_agent_helper_thread (void *arg)
0fb4aa4b
PA
6741{
6742 int listen_fd;
6743
6744 while (1)
6745 {
2fa291ac 6746 listen_fd = gdb_agent_socket_init ();
0fb4aa4b 6747
0fb4aa4b
PA
6748 if (helper_thread_id == 0)
6749 helper_thread_id = syscall (SYS_gettid);
0fb4aa4b
PA
6750
6751 if (listen_fd == -1)
6752 {
6753 warning ("could not create sync socket\n");
6754 break;
6755 }
6756
6757 while (1)
6758 {
6759 socklen_t tmp;
6760 struct sockaddr_un sockaddr;
6761 int fd;
6762 char buf[1];
6763 int ret;
6764
6765 tmp = sizeof (sockaddr);
6766
6767 do
6768 {
6769 fd = accept (listen_fd, &sockaddr, &tmp);
6770 }
6771 /* It seems an ERESTARTSYS can escape out of accept. */
6772 while (fd == -512 || (fd == -1 && errno == EINTR));
6773
6774 if (fd < 0)
6775 {
6776 warning ("Accept returned %d, error: %s\n",
6777 fd, strerror (errno));
6778 break;
6779 }
6780
6781 do
6782 {
6783 ret = read (fd, buf, 1);
6784 } while (ret == -1 && errno == EINTR);
6785
6786 if (ret == -1)
6787 {
6788 warning ("reading socket (fd=%d) failed with %s",
6789 fd, strerror (errno));
6790 close (fd);
6791 break;
6792 }
6793
6794 if (cmd_buf[0])
6795 {
2fa291ac 6796#ifdef HAVE_UST
0fb4aa4b
PA
6797 if (strcmp ("qTfSTM", cmd_buf) == 0)
6798 {
6799 cmd_qtfstm (cmd_buf);
6800 }
6801 else if (strcmp ("qTsSTM", cmd_buf) == 0)
6802 {
6803 cmd_qtsstm (cmd_buf);
6804 }
6805 else if (strncmp ("unprobe_marker_at:",
6806 cmd_buf,
6807 sizeof ("unprobe_marker_at:") - 1) == 0)
6808 {
6809 unprobe_marker_at (cmd_buf);
6810 }
6811 else if (strncmp ("probe_marker_at:",
6812 cmd_buf,
6813 sizeof ("probe_marker_at:") - 1) == 0)
6814 {
6815 probe_marker_at (cmd_buf);
6816 }
6817 else if (strncmp ("qTSTMat:",
6818 cmd_buf,
6819 sizeof ("qTSTMat:") - 1) == 0)
6820 {
6821 cmd_qtstmat (cmd_buf);
6822 }
2fa291ac 6823#endif /* HAVE_UST */
0fb4aa4b
PA
6824 }
6825
712c6575
YQ
6826 /* Fix compiler's warning: ignoring return value of 'write'. */
6827 ret = write (fd, buf, 1);
0fb4aa4b
PA
6828 close (fd);
6829 }
6830 }
6831
6832 return NULL;
6833}
6834
6835#include <signal.h>
2fa291ac 6836#include <pthread.h>
0fb4aa4b 6837
8ffcbaaf
YQ
6838IP_AGENT_EXPORT int gdb_agent_capability = AGENT_CAPA_STATIC_TRACE;
6839
0fb4aa4b 6840static void
2fa291ac 6841gdb_agent_init (void)
0fb4aa4b
PA
6842{
6843 int res;
6844 pthread_t thread;
6845 sigset_t new_mask;
6846 sigset_t orig_mask;
6847
0fb4aa4b
PA
6848 /* We want the helper thread to be as transparent as possible, so
6849 have it inherit an all-signals-blocked mask. */
6850
6851 sigfillset (&new_mask);
6852 res = pthread_sigmask (SIG_SETMASK, &new_mask, &orig_mask);
6853 if (res)
6854 fatal ("pthread_sigmask (1) failed: %s", strerror (res));
6855
6856 res = pthread_create (&thread,
6857 NULL,
2fa291ac 6858 gdb_agent_helper_thread,
0fb4aa4b
PA
6859 NULL);
6860
6861 res = pthread_sigmask (SIG_SETMASK, &orig_mask, NULL);
6862 if (res)
6863 fatal ("pthread_sigmask (2) failed: %s", strerror (res));
6864
6865 while (helper_thread_id == 0)
6866 usleep (1);
6867
2fa291ac
YQ
6868#ifdef HAVE_UST
6869 gdb_ust_init ();
6870#endif
0fb4aa4b
PA
6871}
6872
0fb4aa4b
PA
6873#include <sys/mman.h>
6874#include <fcntl.h>
6875
6876IP_AGENT_EXPORT char *gdb_tp_heap_buffer;
6877IP_AGENT_EXPORT char *gdb_jump_pad_buffer;
6878IP_AGENT_EXPORT char *gdb_jump_pad_buffer_end;
405f8e94
SS
6879IP_AGENT_EXPORT char *gdb_trampoline_buffer;
6880IP_AGENT_EXPORT char *gdb_trampoline_buffer_end;
6881IP_AGENT_EXPORT char *gdb_trampoline_buffer_error;
6882
6883/* Record the result of getting buffer space for fast tracepoint
6884 trampolines. Any error message is copied, since caller may not be
6885 using persistent storage. */
6886
6887void
6888set_trampoline_buffer_space (CORE_ADDR begin, CORE_ADDR end, char *errmsg)
6889{
6890 gdb_trampoline_buffer = (char *) (uintptr_t) begin;
6891 gdb_trampoline_buffer_end = (char *) (uintptr_t) end;
6892 if (errmsg)
6893 strncpy (gdb_trampoline_buffer_error, errmsg, 99);
6894 else
6895 strcpy (gdb_trampoline_buffer_error, "no buffer passed");
6896}
0fb4aa4b
PA
6897
6898static void __attribute__ ((constructor))
6899initialize_tracepoint_ftlib (void)
6900{
6901 initialize_tracepoint ();
6902
2fa291ac 6903 gdb_agent_init ();
0fb4aa4b
PA
6904}
6905
6906#endif /* IN_PROCESS_AGENT */
fa593d66 6907
f196051f
SS
6908/* Return a timestamp, expressed as microseconds of the usual Unix
6909 time. (As the result is a 64-bit number, it will not overflow any
6910 time soon.) */
6911
219f2f23 6912static LONGEST
f196051f 6913get_timestamp (void)
219f2f23
PA
6914{
6915 struct timeval tv;
6916
6917 if (gettimeofday (&tv, 0) != 0)
6918 return -1;
6919 else
6920 return (LONGEST) tv.tv_sec * 1000000 + tv.tv_usec;
6921}
6922
6923void
6924initialize_tracepoint (void)
6925{
6926 /* There currently no way to change the buffer size. */
6927 const int sizeOfBuffer = 5 * 1024 * 1024;
6928 unsigned char *buf = xmalloc (sizeOfBuffer);
6929 init_trace_buffer (buf, sizeOfBuffer);
6930
6931 /* Wire trace state variable 1 to be the timestamp. This will be
6932 uploaded to GDB upon connection and become one of its trace state
6933 variables. (In case you're wondering, if GDB already has a trace
6934 variable numbered 1, it will be renumbered.) */
fa593d66 6935 create_trace_state_variable (1, 0);
219f2f23 6936 set_trace_state_variable_name (1, "trace_timestamp");
f196051f 6937 set_trace_state_variable_getter (1, get_timestamp);
fa593d66
PA
6938
6939#ifdef IN_PROCESS_AGENT
6940 {
fc1ab1a0 6941 uintptr_t addr;
fa593d66 6942 int pagesize;
fc1ab1a0 6943
fa593d66
PA
6944 pagesize = sysconf (_SC_PAGE_SIZE);
6945 if (pagesize == -1)
6946 fatal ("sysconf");
6947
6948 gdb_tp_heap_buffer = xmalloc (5 * 1024 * 1024);
6949
fc1ab1a0
PA
6950#define SCRATCH_BUFFER_NPAGES 20
6951
6952 /* Allocate scratch buffer aligned on a page boundary, at a low
6953 address (close to the main executable's code). */
6954 for (addr = pagesize; addr != 0; addr += pagesize)
6955 {
6956 gdb_jump_pad_buffer = mmap ((void *) addr, pagesize * SCRATCH_BUFFER_NPAGES,
6957 PROT_READ | PROT_WRITE | PROT_EXEC,
6958 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
6959 -1, 0);
6960 if (gdb_jump_pad_buffer != MAP_FAILED)
6961 break;
6962 }
fa593d66 6963
fc1ab1a0 6964 if (addr == 0)
fa593d66 6965 fatal ("\
fc1ab1a0
PA
6966initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
6967 strerror (errno));
6968
6969 gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + pagesize * SCRATCH_BUFFER_NPAGES;
fa593d66
PA
6970 }
6971
405f8e94
SS
6972 gdb_trampoline_buffer = gdb_trampoline_buffer_end = 0;
6973
6974 /* It's not a fatal error for something to go wrong with trampoline
6975 buffer setup, but it can be mysterious, so create a channel to
6976 report back on what went wrong, using a fixed size since we may
6977 not be able to allocate space later when the problem occurs. */
6978 gdb_trampoline_buffer_error = xmalloc (IPA_BUFSIZ);
6979
6980 strcpy (gdb_trampoline_buffer_error, "No errors reported");
6981
fa593d66
PA
6982 initialize_low_tracepoint ();
6983#endif
219f2f23 6984}
This page took 0.666504 seconds and 4 git commands to generate.