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