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