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