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