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