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