]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/tracepoint.c
Add myself as write-after-approval GDB maintainer
[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
2380 /* Make sure we don't try to read from a trace frame. */
2381 current_traceframe = -1;
2382
1ebff1fd
HAQ
2383 stop_tracing ();
2384
219f2f23
PA
2385 trace_debug ("Initializing the trace");
2386
2387 clear_installed_tracepoints ();
2388 clear_readonly_regions ();
2389
2390 tracepoints = NULL;
2391 last_tracepoint = NULL;
2392
2393 /* Clear out any leftover trace state variables. Ones with target
2394 defined getters should be kept however. */
2395 prev = NULL;
2396 tsv = trace_state_variables;
2397 while (tsv)
2398 {
2399 trace_debug ("Looking at var %d", tsv->number);
2400 if (tsv->getter == NULL)
2401 {
2402 next = tsv->next;
2403 if (prev)
2404 prev->next = next;
2405 else
2406 trace_state_variables = next;
2407 trace_debug ("Deleting var %d", tsv->number);
2408 free (tsv);
2409 tsv = next;
2410 }
2411 else
2412 {
2413 prev = tsv;
2414 tsv = tsv->next;
2415 }
2416 }
2417
2418 clear_trace_buffer ();
fa593d66 2419 clear_inferior_trace_buffer ();
219f2f23
PA
2420
2421 write_ok (packet);
2422}
2423
0fb4aa4b
PA
2424/* Unprobe the UST marker at ADDRESS. */
2425
2426static void
2427unprobe_marker_at (CORE_ADDR address)
2428{
2fa291ac 2429 char cmd[IPA_CMD_BUF_SIZE];
0fb4aa4b
PA
2430
2431 sprintf (cmd, "unprobe_marker_at:%s", paddress (address));
42476b70 2432 run_inferior_command (cmd, strlen (cmd) + 1);
0fb4aa4b
PA
2433}
2434
219f2f23
PA
2435/* Restore the program to its pre-tracing state. This routine may be called
2436 in error situations, so it needs to be careful about only restoring
2437 from known-valid bits. */
2438
2439static void
2440clear_installed_tracepoints (void)
2441{
2442 struct tracepoint *tpoint;
2443 struct tracepoint *prev_stpoint;
2444
7984d532 2445 pause_all (1);
7984d532 2446
219f2f23
PA
2447 prev_stpoint = NULL;
2448
2449 /* Restore any bytes overwritten by tracepoints. */
2450 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2451 {
219f2f23
PA
2452 /* Catch the case where we might try to remove a tracepoint that
2453 was never actually installed. */
2454 if (tpoint->handle == NULL)
2455 {
2456 trace_debug ("Tracepoint %d at 0x%s was "
2457 "never installed, nothing to clear",
2458 tpoint->number, paddress (tpoint->address));
2459 continue;
2460 }
2461
fa593d66
PA
2462 switch (tpoint->type)
2463 {
2464 case trap_tracepoint:
2465 delete_breakpoint (tpoint->handle);
2466 break;
2467 case fast_tracepoint:
2468 delete_fast_tracepoint_jump (tpoint->handle);
2469 break;
0fb4aa4b
PA
2470 case static_tracepoint:
2471 if (prev_stpoint != NULL
2472 && prev_stpoint->address == tpoint->address)
2473 /* Nothing to do. We already unprobed a tracepoint set at
2474 this marker address (and there can only be one probe
2475 per marker). */
2476 ;
2477 else
2478 {
2479 unprobe_marker_at (tpoint->address);
2480 prev_stpoint = tpoint;
2481 }
2482 break;
fa593d66
PA
2483 }
2484
219f2f23
PA
2485 tpoint->handle = NULL;
2486 }
7984d532
PA
2487
2488 unpause_all (1);
219f2f23
PA
2489}
2490
2491/* Parse a packet that defines a tracepoint. */
2492
2493static void
2494cmd_qtdp (char *own_buf)
2495{
2496 int tppacket;
1e4d1764
YQ
2497 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2498 int trail_hyphen = 0;
219f2f23
PA
2499 ULONGEST num;
2500 ULONGEST addr;
2501 ULONGEST count;
2502 struct tracepoint *tpoint;
2503 char *actparm;
2504 char *packet = own_buf;
2505
2506 packet += strlen ("QTDP:");
2507
2508 /* A hyphen at the beginning marks a packet specifying actions for a
2509 tracepoint already supplied. */
2510 tppacket = 1;
2511 if (*packet == '-')
2512 {
2513 tppacket = 0;
2514 ++packet;
2515 }
2516 packet = unpack_varlen_hex (packet, &num);
2517 ++packet; /* skip a colon */
2518 packet = unpack_varlen_hex (packet, &addr);
2519 ++packet; /* skip a colon */
2520
2521 /* See if we already have this tracepoint. */
2522 tpoint = find_tracepoint (num, addr);
2523
2524 if (tppacket)
2525 {
2526 /* Duplicate tracepoints are never allowed. */
2527 if (tpoint)
2528 {
2529 trace_debug ("Tracepoint error: tracepoint %d"
2530 " at 0x%s already exists",
2531 (int) num, paddress (addr));
2532 write_enn (own_buf);
2533 return;
2534 }
2535
2536 tpoint = add_tracepoint (num, addr);
2537
2538 tpoint->enabled = (*packet == 'E');
2539 ++packet; /* skip 'E' */
2540 ++packet; /* skip a colon */
2541 packet = unpack_varlen_hex (packet, &count);
2542 tpoint->step_count = count;
2543 ++packet; /* skip a colon */
2544 packet = unpack_varlen_hex (packet, &count);
2545 tpoint->pass_count = count;
2546 /* See if we have any of the additional optional fields. */
2547 while (*packet == ':')
2548 {
2549 ++packet;
fa593d66
PA
2550 if (*packet == 'F')
2551 {
2552 tpoint->type = fast_tracepoint;
2553 ++packet;
2554 packet = unpack_varlen_hex (packet, &count);
2555 tpoint->orig_size = count;
2556 }
0fb4aa4b
PA
2557 else if (*packet == 'S')
2558 {
2559 tpoint->type = static_tracepoint;
2560 ++packet;
2561 }
fa593d66 2562 else if (*packet == 'X')
219f2f23
PA
2563 {
2564 actparm = (char *) packet;
5e1dc496 2565 tpoint->cond = gdb_parse_agent_expr (&actparm);
219f2f23
PA
2566 packet = actparm;
2567 }
2568 else if (*packet == '-')
2569 break;
2570 else if (*packet == '\0')
2571 break;
2572 else
2573 trace_debug ("Unknown optional tracepoint field");
2574 }
2575 if (*packet == '-')
1e4d1764
YQ
2576 {
2577 trail_hyphen = 1;
2578 trace_debug ("Also has actions\n");
2579 }
219f2f23 2580
fa593d66 2581 trace_debug ("Defined %stracepoint %d at 0x%s, "
5f18041e 2582 "enabled %d step %" PRIu64 " pass %" PRIu64,
fa593d66 2583 tpoint->type == fast_tracepoint ? "fast "
5e0a92a9 2584 : tpoint->type == static_tracepoint ? "static " : "",
fa593d66 2585 tpoint->number, paddress (tpoint->address), tpoint->enabled,
219f2f23
PA
2586 tpoint->step_count, tpoint->pass_count);
2587 }
2588 else if (tpoint)
2589 add_tracepoint_action (tpoint, packet);
2590 else
2591 {
2592 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2593 (int) num, paddress (addr));
2594 write_enn (own_buf);
2595 return;
2596 }
2597
1e4d1764
YQ
2598 /* Install tracepoint during tracing only once for each tracepoint location.
2599 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2600 determine the last QTDP packet for one tracepoint location by checking
2601 trailing hyphen in QTDP packet. */
2602 if (tracing && !trail_hyphen)
2603 {
fc3e5175
YQ
2604 struct tracepoint *tp = NULL;
2605
1e4d1764
YQ
2606 /* Pause all threads temporarily while we patch tracepoints. */
2607 pause_all (0);
2608
2609 /* download_tracepoint will update global `tracepoints'
2610 list, so it is unsafe to leave threads in jump pad. */
2611 stabilize_threads ();
2612
2613 /* Freeze threads. */
2614 pause_all (1);
2615
fc3e5175
YQ
2616
2617 if (tpoint->type != trap_tracepoint)
2618 {
2619 /* Find another fast or static tracepoint at the same address. */
2620 for (tp = tracepoints; tp; tp = tp->next)
2621 {
2622 if (tp->address == tpoint->address && tp->type == tpoint->type
2623 && tp->number != tpoint->number)
2624 break;
2625 }
2626
2627 /* TPOINT is installed at the same address as TP. */
2628 if (tp)
2629 {
2630 if (tpoint->type == fast_tracepoint)
2631 clone_fast_tracepoint (tpoint, tp);
2632 else if (tpoint->type == static_tracepoint)
2633 tpoint->handle = (void *) -1;
2634 }
2635 }
2636
42476b70
YQ
2637 if (use_agent && tpoint->type == fast_tracepoint
2638 && agent_capability_check (AGENT_CAPA_FAST_TRACE))
fc3e5175 2639 {
42476b70
YQ
2640 /* Download and install fast tracepoint by agent. */
2641 if (tracepoint_send_agent (tpoint) == 0)
2642 write_ok (own_buf);
2643 else
2644 {
2645 write_enn (own_buf);
2646 remove_tracepoint (tpoint);
2647 }
fc3e5175
YQ
2648 }
2649 else
42476b70
YQ
2650 {
2651 download_tracepoint (tpoint);
2652
2653 if (tpoint->type == trap_tracepoint || tp == NULL)
2654 {
2655 install_tracepoint (tpoint, own_buf);
2656 if (strcmp (own_buf, "OK") != 0)
2657 remove_tracepoint (tpoint);
2658 }
2659 else
2660 write_ok (own_buf);
2661 }
1e4d1764
YQ
2662
2663 unpause_all (1);
2664 return;
2665 }
2666
219f2f23
PA
2667 write_ok (own_buf);
2668}
2669
2670static void
2671cmd_qtdpsrc (char *own_buf)
2672{
2673 ULONGEST num, addr, start, slen;
2674 struct tracepoint *tpoint;
2675 char *packet = own_buf;
2676 char *saved, *srctype, *src;
2677 size_t nbytes;
2678 struct source_string *last, *newlast;
2679
2680 packet += strlen ("QTDPsrc:");
2681
2682 packet = unpack_varlen_hex (packet, &num);
2683 ++packet; /* skip a colon */
2684 packet = unpack_varlen_hex (packet, &addr);
2685 ++packet; /* skip a colon */
2686
2687 /* See if we already have this tracepoint. */
2688 tpoint = find_tracepoint (num, addr);
2689
2690 if (!tpoint)
2691 {
2692 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2693 (int) num, paddress (addr));
2694 write_enn (own_buf);
2695 return;
2696 }
2697
2698 saved = packet;
2699 packet = strchr (packet, ':');
2700 srctype = xmalloc (packet - saved + 1);
2701 memcpy (srctype, saved, packet - saved);
2702 srctype[packet - saved] = '\0';
2703 ++packet;
2704 packet = unpack_varlen_hex (packet, &start);
2705 ++packet; /* skip a colon */
2706 packet = unpack_varlen_hex (packet, &slen);
2707 ++packet; /* skip a colon */
2708 src = xmalloc (slen + 1);
ff0e980e 2709 nbytes = hex2bin (packet, (gdb_byte *) src, strlen (packet) / 2);
219f2f23
PA
2710 src[nbytes] = '\0';
2711
2712 newlast = xmalloc (sizeof (struct source_string));
2713 newlast->type = srctype;
2714 newlast->str = src;
2715 newlast->next = NULL;
2716 /* Always add a source string to the end of the list;
2717 this keeps sequences of actions/commands in the right
2718 order. */
2719 if (tpoint->source_strings)
2720 {
2721 for (last = tpoint->source_strings; last->next; last = last->next)
2722 ;
2723 last->next = newlast;
2724 }
2725 else
2726 tpoint->source_strings = newlast;
2727
2728 write_ok (own_buf);
2729}
2730
2731static void
2732cmd_qtdv (char *own_buf)
2733{
2734 ULONGEST num, val, builtin;
2735 char *varname;
2736 size_t nbytes;
2737 struct trace_state_variable *tsv;
2738 char *packet = own_buf;
2739
2740 packet += strlen ("QTDV:");
2741
2742 packet = unpack_varlen_hex (packet, &num);
2743 ++packet; /* skip a colon */
2744 packet = unpack_varlen_hex (packet, &val);
2745 ++packet; /* skip a colon */
2746 packet = unpack_varlen_hex (packet, &builtin);
2747 ++packet; /* skip a colon */
2748
2749 nbytes = strlen (packet) / 2;
2750 varname = xmalloc (nbytes + 1);
ff0e980e 2751 nbytes = hex2bin (packet, (gdb_byte *) varname, nbytes);
219f2f23
PA
2752 varname[nbytes] = '\0';
2753
fa593d66 2754 tsv = create_trace_state_variable (num, 1);
219f2f23
PA
2755 tsv->initial_value = (LONGEST) val;
2756 tsv->name = varname;
2757
2758 set_trace_state_variable_value (num, (LONGEST) val);
2759
2760 write_ok (own_buf);
2761}
2762
d248b706
KY
2763static void
2764cmd_qtenable_disable (char *own_buf, int enable)
2765{
2766 char *packet = own_buf;
2767 ULONGEST num, addr;
2768 struct tracepoint *tp;
2769
2770 packet += strlen (enable ? "QTEnable:" : "QTDisable:");
2771 packet = unpack_varlen_hex (packet, &num);
2772 ++packet; /* skip a colon */
2773 packet = unpack_varlen_hex (packet, &addr);
2774
2775 tp = find_tracepoint (num, addr);
2776
2777 if (tp)
2778 {
2779 if ((enable && tp->enabled) || (!enable && !tp->enabled))
2780 {
2781 trace_debug ("Tracepoint %d at 0x%s is already %s",
2782 (int) num, paddress (addr),
2783 enable ? "enabled" : "disabled");
2784 write_ok (own_buf);
2785 return;
2786 }
2787
2788 trace_debug ("%s tracepoint %d at 0x%s",
2789 enable ? "Enabling" : "Disabling",
2790 (int) num, paddress (addr));
2791
2792 tp->enabled = enable;
2793
2794 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
2795 {
2796 int ret;
2797 int offset = offsetof (struct tracepoint, enabled);
2798 CORE_ADDR obj_addr = tp->obj_addr_on_target + offset;
2799
2800 ret = prepare_to_access_memory ();
2801 if (ret)
2802 {
2803 trace_debug ("Failed to temporarily stop inferior threads");
2804 write_enn (own_buf);
2805 return;
2806 }
2807
2808 ret = write_inferior_integer (obj_addr, enable);
2809 done_accessing_memory ();
2810
2811 if (ret)
2812 {
2813 trace_debug ("Cannot write enabled flag into "
2814 "inferior process memory");
2815 write_enn (own_buf);
2816 return;
2817 }
2818 }
2819
2820 write_ok (own_buf);
2821 }
2822 else
2823 {
2824 trace_debug ("Tracepoint %d at 0x%s not found",
2825 (int) num, paddress (addr));
2826 write_enn (own_buf);
2827 }
2828}
2829
219f2f23
PA
2830static void
2831cmd_qtv (char *own_buf)
2832{
2833 ULONGEST num;
eeb56fa7 2834 LONGEST val = 0;
219f2f23
PA
2835 int err;
2836 char *packet = own_buf;
2837
2838 packet += strlen ("qTV:");
f8f67713 2839 unpack_varlen_hex (packet, &num);
219f2f23
PA
2840
2841 if (current_traceframe >= 0)
2842 {
2843 err = traceframe_read_tsv ((int) num, &val);
2844 if (err)
2845 {
2846 strcpy (own_buf, "U");
2847 return;
2848 }
2849 }
2850 /* Only make tsv's be undefined before the first trace run. After a
2851 trace run is over, the user might want to see the last value of
2852 the tsv, and it might not be available in a traceframe. */
2853 else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0)
2854 {
2855 strcpy (own_buf, "U");
2856 return;
2857 }
2858 else
2859 val = get_trace_state_variable_value (num);
2860
2861 sprintf (own_buf, "V%s", phex_nz (val, 0));
2862}
2863
2864/* Clear out the list of readonly regions. */
2865
2866static void
2867clear_readonly_regions (void)
2868{
2869 struct readonly_region *roreg;
2870
2871 while (readonly_regions)
2872 {
2873 roreg = readonly_regions;
2874 readonly_regions = readonly_regions->next;
2875 free (roreg);
2876 }
2877}
2878
2879/* Parse the collection of address ranges whose contents GDB believes
2880 to be unchanging and so can be read directly from target memory
2881 even while looking at a traceframe. */
2882
2883static void
2884cmd_qtro (char *own_buf)
2885{
2886 ULONGEST start, end;
2887 struct readonly_region *roreg;
2888 char *packet = own_buf;
2889
2890 trace_debug ("Want to mark readonly regions");
2891
2892 clear_readonly_regions ();
2893
2894 packet += strlen ("QTro");
2895
2896 while (*packet == ':')
2897 {
2898 ++packet; /* skip a colon */
2899 packet = unpack_varlen_hex (packet, &start);
2900 ++packet; /* skip a comma */
2901 packet = unpack_varlen_hex (packet, &end);
2902 roreg = xmalloc (sizeof (struct readonly_region));
2903 roreg->start = start;
2904 roreg->end = end;
2905 roreg->next = readonly_regions;
2906 readonly_regions = roreg;
2907 trace_debug ("Added readonly region from 0x%s to 0x%s",
2908 paddress (roreg->start), paddress (roreg->end));
2909 }
2910
2911 write_ok (own_buf);
2912}
2913
2914/* Test to see if the given range is in our list of readonly ranges.
2915 We only test for being entirely within a range, GDB is not going to
2916 send a single memory packet that spans multiple regions. */
2917
2918int
2919in_readonly_region (CORE_ADDR addr, ULONGEST length)
2920{
2921 struct readonly_region *roreg;
2922
2923 for (roreg = readonly_regions; roreg; roreg = roreg->next)
2924 if (roreg->start <= addr && (addr + length - 1) <= roreg->end)
2925 return 1;
2926
2927 return 0;
2928}
2929
fa593d66
PA
2930/* The maximum size of a jump pad entry. */
2931static const int max_jump_pad_size = 0x100;
2932
2933static CORE_ADDR gdb_jump_pad_head;
2934
2935/* Return the address of the next free jump space. */
2936
2937static CORE_ADDR
2938get_jump_space_head (void)
2939{
2940 if (gdb_jump_pad_head == 0)
2941 {
2942 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
2943 &gdb_jump_pad_head))
38e08fca
GB
2944 {
2945 internal_error (__FILE__, __LINE__,
2946 "error extracting jump_pad_buffer");
2947 }
fa593d66
PA
2948 }
2949
2950 return gdb_jump_pad_head;
2951}
2952
2953/* Reserve USED bytes from the jump space. */
2954
2955static void
2956claim_jump_space (ULONGEST used)
2957{
2958 trace_debug ("claim_jump_space reserves %s bytes at %s",
2959 pulongest (used), paddress (gdb_jump_pad_head));
2960 gdb_jump_pad_head += used;
2961}
2962
405f8e94
SS
2963static CORE_ADDR trampoline_buffer_head = 0;
2964static CORE_ADDR trampoline_buffer_tail;
2965
2966/* Reserve USED bytes from the trampoline buffer and return the
2967 address of the start of the reserved space in TRAMPOLINE. Returns
2968 non-zero if the space is successfully claimed. */
2969
2970int
2971claim_trampoline_space (ULONGEST used, CORE_ADDR *trampoline)
2972{
2973 if (!trampoline_buffer_head)
2974 {
2975 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
2976 &trampoline_buffer_tail))
2977 {
38e08fca
GB
2978 internal_error (__FILE__, __LINE__,
2979 "error extracting trampoline_buffer");
405f8e94
SS
2980 }
2981
2982 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2983 &trampoline_buffer_head))
2984 {
38e08fca
GB
2985 internal_error (__FILE__, __LINE__,
2986 "error extracting trampoline_buffer_end");
405f8e94
SS
2987 }
2988 }
2989
2990 /* Start claiming space from the top of the trampoline space. If
2991 the space is located at the bottom of the virtual address space,
2992 this reduces the possibility that corruption will occur if a null
2993 pointer is used to write to memory. */
2994 if (trampoline_buffer_head - trampoline_buffer_tail < used)
2995 {
2996 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2997 pulongest (used));
2998 return 0;
2999 }
3000
3001 trampoline_buffer_head -= used;
3002
3003 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
3004 pulongest (used), paddress (trampoline_buffer_head));
3005
3006 *trampoline = trampoline_buffer_head;
3007 return 1;
3008}
3009
3010/* Returns non-zero if there is space allocated for use in trampolines
3011 for fast tracepoints. */
3012
3013int
3014have_fast_tracepoint_trampoline_buffer (char *buf)
3015{
3016 CORE_ADDR trampoline_end, errbuf;
3017
3018 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
3019 &trampoline_end))
3020 {
38e08fca
GB
3021 internal_error (__FILE__, __LINE__,
3022 "error extracting trampoline_buffer_end");
405f8e94
SS
3023 }
3024
3025 if (buf)
3026 {
3027 buf[0] = '\0';
3028 strcpy (buf, "was claiming");
3029 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_error,
3030 &errbuf))
3031 {
38e08fca
GB
3032 internal_error (__FILE__, __LINE__,
3033 "error extracting errbuf");
405f8e94
SS
3034 }
3035
3036 read_inferior_memory (errbuf, (unsigned char *) buf, 100);
3037 }
3038
3039 return trampoline_end != 0;
3040}
3041
0fb4aa4b
PA
3042/* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3043 the command fails, or 0 otherwise. If the command ran
3044 successfully, but probing the marker failed, ERROUT will be filled
3045 with the error to reply to GDB, and -1 is also returned. This
3046 allows directly passing IPA errors to GDB. */
3047
3048static int
3049probe_marker_at (CORE_ADDR address, char *errout)
3050{
2fa291ac 3051 char cmd[IPA_CMD_BUF_SIZE];
0fb4aa4b
PA
3052 int err;
3053
3054 sprintf (cmd, "probe_marker_at:%s", paddress (address));
42476b70 3055 err = run_inferior_command (cmd, strlen (cmd) + 1);
0fb4aa4b
PA
3056
3057 if (err == 0)
3058 {
3059 if (*cmd == 'E')
3060 {
3061 strcpy (errout, cmd);
3062 return -1;
3063 }
3064 }
3065
3066 return err;
3067}
3068
219f2f23 3069static void
5c73ff4e 3070clone_fast_tracepoint (struct tracepoint *to, const struct tracepoint *from)
219f2f23 3071{
5c73ff4e
YQ
3072 to->jump_pad = from->jump_pad;
3073 to->jump_pad_end = from->jump_pad_end;
405f8e94
SS
3074 to->trampoline = from->trampoline;
3075 to->trampoline_end = from->trampoline_end;
5c73ff4e
YQ
3076 to->adjusted_insn_addr = from->adjusted_insn_addr;
3077 to->adjusted_insn_addr_end = from->adjusted_insn_addr_end;
3078 to->handle = from->handle;
fa593d66 3079
5c73ff4e
YQ
3080 gdb_assert (from->handle);
3081 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump *) from->handle);
3082}
3083
3084#define MAX_JUMP_SIZE 20
3085
3086/* Install fast tracepoint. Return 0 if successful, otherwise return
3087 non-zero. */
3088
3089static int
405f8e94 3090install_fast_tracepoint (struct tracepoint *tpoint, char *errbuf)
5c73ff4e
YQ
3091{
3092 CORE_ADDR jentry, jump_entry;
405f8e94
SS
3093 CORE_ADDR trampoline;
3094 ULONGEST trampoline_size;
5c73ff4e 3095 int err = 0;
fa593d66
PA
3096 /* The jump to the jump pad of the last fast tracepoint
3097 installed. */
3098 unsigned char fjump[MAX_JUMP_SIZE];
3099 ULONGEST fjump_size;
219f2f23 3100
405f8e94
SS
3101 if (tpoint->orig_size < target_get_min_fast_tracepoint_insn_len ())
3102 {
3103 trace_debug ("Requested a fast tracepoint on an instruction "
3104 "that is of less than the minimum length.");
3105 return 0;
3106 }
3107
5c73ff4e
YQ
3108 jentry = jump_entry = get_jump_space_head ();
3109
405f8e94
SS
3110 trampoline = 0;
3111 trampoline_size = 0;
3112
5c73ff4e
YQ
3113 /* Install the jump pad. */
3114 err = install_fast_tracepoint_jump_pad (tpoint->obj_addr_on_target,
3115 tpoint->address,
3116 ipa_sym_addrs.addr_gdb_collect,
3117 ipa_sym_addrs.addr_collecting,
3118 tpoint->orig_size,
405f8e94
SS
3119 &jentry,
3120 &trampoline, &trampoline_size,
3121 fjump, &fjump_size,
5c73ff4e 3122 &tpoint->adjusted_insn_addr,
405f8e94
SS
3123 &tpoint->adjusted_insn_addr_end,
3124 errbuf);
5c73ff4e
YQ
3125
3126 if (err)
3127 return 1;
3128
3129 /* Wire it in. */
3130 tpoint->handle = set_fast_tracepoint_jump (tpoint->address, fjump,
3131 fjump_size);
3132
3133 if (tpoint->handle != NULL)
3134 {
3135 tpoint->jump_pad = jump_entry;
3136 tpoint->jump_pad_end = jentry;
405f8e94
SS
3137 tpoint->trampoline = trampoline;
3138 tpoint->trampoline_end = trampoline + trampoline_size;
5c73ff4e
YQ
3139
3140 /* Pad to 8-byte alignment. */
3141 jentry = ((jentry + 7) & ~0x7);
3142 claim_jump_space (jentry - jump_entry);
3143 }
3144
3145 return 0;
3146}
3147
1e4d1764
YQ
3148
3149/* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3150
3151static void
3152install_tracepoint (struct tracepoint *tpoint, char *own_buf)
3153{
3154 tpoint->handle = NULL;
3155 *own_buf = '\0';
3156
3157 if (tpoint->type == trap_tracepoint)
3158 {
3159 /* Tracepoints are installed as memory breakpoints. Just go
3160 ahead and install the trap. The breakpoints module
3161 handles duplicated breakpoints, and the memory read
3162 routine handles un-patching traps from memory reads. */
3163 tpoint->handle = set_breakpoint_at (tpoint->address,
3164 tracepoint_handler);
3165 }
3166 else if (tpoint->type == fast_tracepoint || tpoint->type == static_tracepoint)
3167 {
58b4daa5 3168 if (!agent_loaded_p ())
1e4d1764
YQ
3169 {
3170 trace_debug ("Requested a %s tracepoint, but fast "
3171 "tracepoints aren't supported.",
3172 tpoint->type == static_tracepoint ? "static" : "fast");
3173 write_e_ipa_not_loaded (own_buf);
3174 return;
3175 }
8ffcbaaf
YQ
3176 if (tpoint->type == static_tracepoint
3177 && !in_process_agent_supports_ust ())
1e4d1764
YQ
3178 {
3179 trace_debug ("Requested a static tracepoint, but static "
3180 "tracepoints are not supported.");
3181 write_e_ust_not_loaded (own_buf);
3182 return;
3183 }
3184
1e4d1764 3185 if (tpoint->type == fast_tracepoint)
fc3e5175 3186 install_fast_tracepoint (tpoint, own_buf);
1e4d1764
YQ
3187 else
3188 {
fc3e5175 3189 if (probe_marker_at (tpoint->address, own_buf) == 0)
1e4d1764 3190 tpoint->handle = (void *) -1;
1e4d1764
YQ
3191 }
3192
3193 }
3194 else
3195 internal_error (__FILE__, __LINE__, "Unknown tracepoint type");
3196
3197 if (tpoint->handle == NULL)
3198 {
3199 if (*own_buf == '\0')
3200 write_enn (own_buf);
3201 }
3202 else
3203 write_ok (own_buf);
3204}
3205
7bc83639
YQ
3206static void download_tracepoint_1 (struct tracepoint *tpoint);
3207
5c73ff4e
YQ
3208static void
3209cmd_qtstart (char *packet)
3210{
3211 struct tracepoint *tpoint, *prev_ftpoint, *prev_stpoint;
7bc83639 3212 CORE_ADDR tpptr = 0, prev_tpptr = 0;
5c73ff4e 3213
219f2f23
PA
3214 trace_debug ("Starting the trace");
3215
7984d532 3216 /* Pause all threads temporarily while we patch tracepoints. */
fa593d66
PA
3217 pause_all (0);
3218
3219 /* Get threads out of jump pads. Safe to do here, since this is a
3220 top level command. And, required to do here, since we're
3221 deleting/rewriting jump pads. */
3222
3223 stabilize_threads ();
3224
3225 /* Freeze threads. */
7984d532
PA
3226 pause_all (1);
3227
fa593d66 3228 /* Sync the fast tracepoints list in the inferior ftlib. */
58b4daa5 3229 if (agent_loaded_p ())
7bc83639 3230 download_trace_state_variables ();
fa593d66
PA
3231
3232 /* No previous fast tpoint yet. */
3233 prev_ftpoint = NULL;
3234
0fb4aa4b
PA
3235 /* No previous static tpoint yet. */
3236 prev_stpoint = NULL;
3237
fa593d66
PA
3238 *packet = '\0';
3239
7bc83639
YQ
3240 /* Start out empty. */
3241 if (agent_loaded_p ())
3242 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, 0);
3243
3244 /* Download and install tracepoints. */
219f2f23
PA
3245 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
3246 {
3247 /* Ensure all the hit counts start at zero. */
3248 tpoint->hit_count = 0;
f196051f 3249 tpoint->traceframe_usage = 0;
219f2f23 3250
fa593d66
PA
3251 if (tpoint->type == trap_tracepoint)
3252 {
fa593d66
PA
3253 /* Tracepoints are installed as memory breakpoints. Just go
3254 ahead and install the trap. The breakpoints module
3255 handles duplicated breakpoints, and the memory read
3256 routine handles un-patching traps from memory reads. */
3257 tpoint->handle = set_breakpoint_at (tpoint->address,
3258 tracepoint_handler);
3259 }
7bc83639
YQ
3260 else if (tpoint->type == fast_tracepoint
3261 || tpoint->type == static_tracepoint)
fa593d66 3262 {
fa593d66
PA
3263 if (maybe_write_ipa_not_loaded (packet))
3264 {
7bc83639
YQ
3265 trace_debug ("Requested a %s tracepoint, but fast "
3266 "tracepoints aren't supported.",
3267 tpoint->type == static_tracepoint
3268 ? "static" : "fast");
fa593d66
PA
3269 break;
3270 }
3271
7bc83639 3272 if (tpoint->type == fast_tracepoint)
0fb4aa4b 3273 {
80d26939
YQ
3274 int use_agent_p
3275 = use_agent && agent_capability_check (AGENT_CAPA_FAST_TRACE);
3276
7bc83639
YQ
3277 if (prev_ftpoint != NULL
3278 && prev_ftpoint->address == tpoint->address)
80d26939
YQ
3279 {
3280 if (use_agent_p)
3281 tracepoint_send_agent (tpoint);
3282 else
3283 download_tracepoint_1 (tpoint);
3284
3285 clone_fast_tracepoint (tpoint, prev_ftpoint);
3286 }
7bc83639
YQ
3287 else
3288 {
42476b70
YQ
3289 /* Tracepoint is installed successfully? */
3290 int installed = 0;
3291
3292 /* Download and install fast tracepoint by agent. */
80d26939 3293 if (use_agent_p)
42476b70
YQ
3294 installed = !tracepoint_send_agent (tpoint);
3295 else
3296 {
3297 download_tracepoint_1 (tpoint);
3298 installed = !install_fast_tracepoint (tpoint, packet);
3299 }
3300
3301 if (installed)
7bc83639
YQ
3302 prev_ftpoint = tpoint;
3303 }
0fb4aa4b
PA
3304 }
3305 else
3306 {
7bc83639 3307 if (!in_process_agent_supports_ust ())
0fb4aa4b 3308 {
7bc83639
YQ
3309 trace_debug ("Requested a static tracepoint, but static "
3310 "tracepoints are not supported.");
3311 break;
3312 }
0fb4aa4b 3313
7bc83639
YQ
3314 download_tracepoint_1 (tpoint);
3315 /* Can only probe a given marker once. */
3316 if (prev_stpoint != NULL
3317 && prev_stpoint->address == tpoint->address)
3318 tpoint->handle = (void *) -1;
3319 else
3320 {
3321 if (probe_marker_at (tpoint->address, packet) == 0)
3322 {
3323 tpoint->handle = (void *) -1;
3324
3325 /* So that we can handle multiple static tracepoints
3326 at the same address easily. */
3327 prev_stpoint = tpoint;
3328 }
0fb4aa4b
PA
3329 }
3330 }
7bc83639
YQ
3331
3332 prev_tpptr = tpptr;
3333 tpptr = tpoint->obj_addr_on_target;
3334
3335 if (tpoint == tracepoints)
3336 /* First object in list, set the head pointer in the
3337 inferior. */
3338 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, tpptr);
3339 else
3340 write_inferior_data_ptr (prev_tpptr + offsetof (struct tracepoint,
3341 next),
3342 tpptr);
0fb4aa4b 3343 }
fa593d66
PA
3344
3345 /* Any failure in the inner loop is sufficient cause to give
3346 up. */
219f2f23
PA
3347 if (tpoint->handle == NULL)
3348 break;
3349 }
3350
3351 /* Any error in tracepoint insertion is unacceptable; better to
3352 address the problem now, than end up with a useless or misleading
3353 trace run. */
3354 if (tpoint != NULL)
3355 {
3356 clear_installed_tracepoints ();
3357 if (*packet == '\0')
3358 write_enn (packet);
7984d532 3359 unpause_all (1);
219f2f23
PA
3360 return;
3361 }
3362
3363 stopping_tracepoint = NULL;
3364 trace_buffer_is_full = 0;
3365 expr_eval_result = expr_eval_no_error;
3366 error_tracepoint = NULL;
f196051f 3367 tracing_start_time = get_timestamp ();
219f2f23
PA
3368
3369 /* Tracing is now active, hits will now start being logged. */
3370 tracing = 1;
3371
58b4daa5 3372 if (agent_loaded_p ())
fa593d66
PA
3373 {
3374 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 1))
38e08fca
GB
3375 {
3376 internal_error (__FILE__, __LINE__,
3377 "Error setting tracing variable in lib");
3378 }
fa593d66
PA
3379
3380 if (write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
3381 0))
38e08fca
GB
3382 {
3383 internal_error (__FILE__, __LINE__,
3384 "Error clearing stopping_tracepoint variable"
3385 " in lib");
3386 }
fa593d66
PA
3387
3388 if (write_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 0))
38e08fca
GB
3389 {
3390 internal_error (__FILE__, __LINE__,
3391 "Error clearing trace_buffer_is_full variable"
3392 " in lib");
3393 }
fa593d66
PA
3394
3395 stop_tracing_bkpt = set_breakpoint_at (ipa_sym_addrs.addr_stop_tracing,
3396 stop_tracing_handler);
3397 if (stop_tracing_bkpt == NULL)
3398 error ("Error setting stop_tracing breakpoint");
3399
3400 flush_trace_buffer_bkpt
3401 = set_breakpoint_at (ipa_sym_addrs.addr_flush_trace_buffer,
3402 flush_trace_buffer_handler);
3403 if (flush_trace_buffer_bkpt == NULL)
3404 error ("Error setting flush_trace_buffer breakpoint");
3405 }
3406
7984d532
PA
3407 unpause_all (1);
3408
219f2f23
PA
3409 write_ok (packet);
3410}
3411
3412/* End a tracing run, filling in a stop reason to report back to GDB,
3413 and removing the tracepoints from the code. */
3414
8336d594 3415void
219f2f23
PA
3416stop_tracing (void)
3417{
3418 if (!tracing)
3419 {
3420 trace_debug ("Tracing is already off, ignoring");
3421 return;
3422 }
3423
3424 trace_debug ("Stopping the trace");
3425
fa593d66
PA
3426 /* Pause all threads before removing fast jumps from memory,
3427 breakpoints, and touching IPA state variables (inferior memory).
3428 Some thread may hit the internal tracing breakpoints, or be
3429 collecting this moment, but that's ok, we don't release the
3430 tpoint object's memory or the jump pads here (we only do that
3431 when we're sure we can move all threads out of the jump pads).
3432 We can't now, since we may be getting here due to the inferior
3433 agent calling us. */
7984d532 3434 pause_all (1);
7984d532 3435
219f2f23
PA
3436 /* Stop logging. Tracepoints can still be hit, but they will not be
3437 recorded. */
3438 tracing = 0;
58b4daa5 3439 if (agent_loaded_p ())
fa593d66
PA
3440 {
3441 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 0))
38e08fca
GB
3442 {
3443 internal_error (__FILE__, __LINE__,
3444 "Error clearing tracing variable in lib");
3445 }
fa593d66 3446 }
219f2f23 3447
f196051f 3448 tracing_stop_time = get_timestamp ();
219f2f23
PA
3449 tracing_stop_reason = "t???";
3450 tracing_stop_tpnum = 0;
3451 if (stopping_tracepoint)
3452 {
3453 trace_debug ("Stopping the trace because "
5f18041e 3454 "tracepoint %d was hit %" PRIu64 " times",
219f2f23
PA
3455 stopping_tracepoint->number,
3456 stopping_tracepoint->pass_count);
3457 tracing_stop_reason = "tpasscount";
3458 tracing_stop_tpnum = stopping_tracepoint->number;
3459 }
3460 else if (trace_buffer_is_full)
3461 {
3462 trace_debug ("Stopping the trace because the trace buffer is full");
3463 tracing_stop_reason = "tfull";
3464 }
3465 else if (expr_eval_result != expr_eval_no_error)
3466 {
3467 trace_debug ("Stopping the trace because of an expression eval error");
3468 tracing_stop_reason = eval_result_names[expr_eval_result];
3469 tracing_stop_tpnum = error_tracepoint->number;
3470 }
fa593d66 3471#ifndef IN_PROCESS_AGENT
8336d594
PA
3472 else if (!gdb_connected ())
3473 {
3474 trace_debug ("Stopping the trace because GDB disconnected");
3475 tracing_stop_reason = "tdisconnected";
3476 }
fa593d66 3477#endif
219f2f23
PA
3478 else
3479 {
3480 trace_debug ("Stopping the trace because of a tstop command");
3481 tracing_stop_reason = "tstop";
3482 }
3483
3484 stopping_tracepoint = NULL;
3485 error_tracepoint = NULL;
3486
3487 /* Clear out the tracepoints. */
3488 clear_installed_tracepoints ();
7984d532 3489
58b4daa5 3490 if (agent_loaded_p ())
fa593d66
PA
3491 {
3492 /* Pull in fast tracepoint trace frames from the inferior lib
3493 buffer into our buffer, even if our buffer is already full,
3494 because we want to present the full number of created frames
3495 in addition to what fit in the trace buffer. */
3496 upload_fast_traceframes ();
3497 }
3498
3499 if (stop_tracing_bkpt != NULL)
3500 {
3501 delete_breakpoint (stop_tracing_bkpt);
3502 stop_tracing_bkpt = NULL;
3503 }
3504
3505 if (flush_trace_buffer_bkpt != NULL)
3506 {
3507 delete_breakpoint (flush_trace_buffer_bkpt);
3508 flush_trace_buffer_bkpt = NULL;
3509 }
3510
7984d532 3511 unpause_all (1);
219f2f23
PA
3512}
3513
fa593d66
PA
3514static int
3515stop_tracing_handler (CORE_ADDR addr)
3516{
3517 trace_debug ("lib hit stop_tracing");
3518
3519 /* Don't actually handle it here. When we stop tracing we remove
3520 breakpoints from the inferior, and that is not allowed in a
3521 breakpoint handler (as the caller is walking the breakpoint
3522 list). */
3523 return 0;
3524}
3525
3526static int
3527flush_trace_buffer_handler (CORE_ADDR addr)
3528{
3529 trace_debug ("lib hit flush_trace_buffer");
3530 return 0;
3531}
3532
219f2f23
PA
3533static void
3534cmd_qtstop (char *packet)
3535{
3536 stop_tracing ();
3537 write_ok (packet);
3538}
3539
8336d594
PA
3540static void
3541cmd_qtdisconnected (char *own_buf)
3542{
3543 ULONGEST setting;
3544 char *packet = own_buf;
3545
3546 packet += strlen ("QTDisconnected:");
3547
3548 unpack_varlen_hex (packet, &setting);
3549
3550 write_ok (own_buf);
3551
3552 disconnected_tracing = setting;
3553}
3554
219f2f23
PA
3555static void
3556cmd_qtframe (char *own_buf)
3557{
3558 ULONGEST frame, pc, lo, hi, num;
3559 int tfnum, tpnum;
3560 struct traceframe *tframe;
3561 char *packet = own_buf;
3562
3563 packet += strlen ("QTFrame:");
3564
3565 if (strncmp (packet, "pc:", strlen ("pc:")) == 0)
3566 {
3567 packet += strlen ("pc:");
f8f67713 3568 unpack_varlen_hex (packet, &pc);
219f2f23
PA
3569 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc));
3570 tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum);
3571 }
3572 else if (strncmp (packet, "range:", strlen ("range:")) == 0)
3573 {
3574 packet += strlen ("range:");
3575 packet = unpack_varlen_hex (packet, &lo);
3576 ++packet;
f8f67713 3577 unpack_varlen_hex (packet, &hi);
219f2f23
PA
3578 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3579 paddress (lo), paddress (hi));
3580 tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum);
3581 }
3582 else if (strncmp (packet, "outside:", strlen ("outside:")) == 0)
3583 {
3584 packet += strlen ("outside:");
3585 packet = unpack_varlen_hex (packet, &lo);
3586 ++packet;
f8f67713 3587 unpack_varlen_hex (packet, &hi);
219f2f23
PA
3588 trace_debug ("Want to find next traceframe "
3589 "outside the range 0x%s to 0x%s",
3590 paddress (lo), paddress (hi));
3591 tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum);
3592 }
3593 else if (strncmp (packet, "tdp:", strlen ("tdp:")) == 0)
3594 {
3595 packet += strlen ("tdp:");
f8f67713 3596 unpack_varlen_hex (packet, &num);
219f2f23
PA
3597 tpnum = (int) num;
3598 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum);
3599 tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum);
3600 }
3601 else
3602 {
3603 unpack_varlen_hex (packet, &frame);
3604 tfnum = (int) frame;
3605 if (tfnum == -1)
3606 {
3607 trace_debug ("Want to stop looking at traceframes");
3608 current_traceframe = -1;
3609 write_ok (own_buf);
3610 return;
3611 }
3612 trace_debug ("Want to look at traceframe %d", tfnum);
3613 tframe = find_traceframe (tfnum);
3614 }
3615
3616 if (tframe)
3617 {
3618 current_traceframe = tfnum;
3619 sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum);
3620 }
3621 else
3622 sprintf (own_buf, "F-1");
3623}
3624
3625static void
3626cmd_qtstatus (char *packet)
3627{
3628 char *stop_reason_rsp = NULL;
f196051f
SS
3629 char *buf1, *buf2, *buf3, *str;
3630 int slen;
3631
3632 /* Translate the plain text of the notes back into hex for
3633 transmission. */
3634
3635 str = (tracing_user_name ? tracing_user_name : "");
3636 slen = strlen (str);
3637 buf1 = (char *) alloca (slen * 2 + 1);
971dc0b8 3638 bin2hex ((gdb_byte *) str, buf1, slen);
f196051f
SS
3639
3640 str = (tracing_notes ? tracing_notes : "");
3641 slen = strlen (str);
3642 buf2 = (char *) alloca (slen * 2 + 1);
971dc0b8 3643 bin2hex ((gdb_byte *) str, buf2, slen);
f196051f
SS
3644
3645 str = (tracing_stop_note ? tracing_stop_note : "");
3646 slen = strlen (str);
3647 buf3 = (char *) alloca (slen * 2 + 1);
971dc0b8 3648 bin2hex ((gdb_byte *) str, buf3, slen);
219f2f23
PA
3649
3650 trace_debug ("Returning trace status as %d, stop reason %s",
3651 tracing, tracing_stop_reason);
3652
58b4daa5 3653 if (agent_loaded_p ())
fa593d66
PA
3654 {
3655 pause_all (1);
3656
3657 upload_fast_traceframes ();
3658
3659 unpause_all (1);
3660 }
3661
219f2f23
PA
3662 stop_reason_rsp = (char *) tracing_stop_reason;
3663
3664 /* The user visible error string in terror needs to be hex encoded.
f196051f 3665 We leave it as plain string in `tracing_stop_reason' to ease
219f2f23
PA
3666 debugging. */
3667 if (strncmp (stop_reason_rsp, "terror:", strlen ("terror:")) == 0)
3668 {
3669 const char *result_name;
3670 int hexstr_len;
3671 char *p;
3672
3673 result_name = stop_reason_rsp + strlen ("terror:");
3674 hexstr_len = strlen (result_name) * 2;
3675 p = stop_reason_rsp = alloca (strlen ("terror:") + hexstr_len + 1);
3676 strcpy (p, "terror:");
3677 p += strlen (p);
e9371aff 3678 bin2hex ((gdb_byte *) result_name, p, strlen (result_name));
219f2f23
PA
3679 }
3680
f196051f
SS
3681 /* If this was a forced stop, include any stop note that was supplied. */
3682 if (strcmp (stop_reason_rsp, "tstop") == 0)
3683 {
3684 stop_reason_rsp = alloca (strlen ("tstop:") + strlen (buf3) + 1);
3685 strcpy (stop_reason_rsp, "tstop:");
3686 strcat (stop_reason_rsp, buf3);
3687 }
3688
8336d594
PA
3689 sprintf (packet,
3690 "T%d;"
3691 "%s:%x;"
3692 "tframes:%x;tcreated:%x;"
3693 "tfree:%x;tsize:%s;"
3694 "circular:%d;"
f196051f 3695 "disconn:%d;"
242f5f1c 3696 "starttime:%s;stoptime:%s;"
86ebe149 3697 "username:%s;notes:%s:",
623ccd72 3698 tracing ? 1 : 0,
219f2f23
PA
3699 stop_reason_rsp, tracing_stop_tpnum,
3700 traceframe_count, traceframes_created,
8336d594
PA
3701 free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
3702 circular_trace_buffer,
f196051f 3703 disconnected_tracing,
f30aa5af
DK
3704 phex_nz (tracing_start_time, sizeof (tracing_start_time)),
3705 phex_nz (tracing_stop_time, sizeof (tracing_stop_time)),
f196051f
SS
3706 buf1, buf2);
3707}
3708
3709static void
3710cmd_qtp (char *own_buf)
3711{
3712 ULONGEST num, addr;
3713 struct tracepoint *tpoint;
3714 char *packet = own_buf;
3715
3716 packet += strlen ("qTP:");
3717
3718 packet = unpack_varlen_hex (packet, &num);
3719 ++packet; /* skip a colon */
3720 packet = unpack_varlen_hex (packet, &addr);
3721
3722 /* See if we already have this tracepoint. */
3723 tpoint = find_tracepoint (num, addr);
3724
3725 if (!tpoint)
3726 {
3727 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3728 (int) num, paddress (addr));
3729 write_enn (own_buf);
3730 return;
3731 }
3732
5f18041e
YQ
3733 sprintf (own_buf, "V%" PRIu64 ":%" PRIu64 "", tpoint->hit_count,
3734 tpoint->traceframe_usage);
219f2f23
PA
3735}
3736
3737/* State variables to help return all the tracepoint bits. */
3738static struct tracepoint *cur_tpoint;
e64f7499
YQ
3739static unsigned int cur_action;
3740static unsigned int cur_step_action;
219f2f23
PA
3741static struct source_string *cur_source_string;
3742static struct trace_state_variable *cur_tsv;
3743
3744/* Compose a response that is an imitation of the syntax by which the
3745 tracepoint was originally downloaded. */
3746
3747static void
3748response_tracepoint (char *packet, struct tracepoint *tpoint)
3749{
3750 char *buf;
3751
5f18041e 3752 sprintf (packet, "T%x:%s:%c:%" PRIx64 ":%" PRIx64, tpoint->number,
219f2f23
PA
3753 paddress (tpoint->address),
3754 (tpoint->enabled ? 'E' : 'D'), tpoint->step_count,
3755 tpoint->pass_count);
fa593d66
PA
3756 if (tpoint->type == fast_tracepoint)
3757 sprintf (packet + strlen (packet), ":F%x", tpoint->orig_size);
0fb4aa4b
PA
3758 else if (tpoint->type == static_tracepoint)
3759 sprintf (packet + strlen (packet), ":S");
219f2f23
PA
3760
3761 if (tpoint->cond)
3762 {
5e1dc496 3763 buf = gdb_unparse_agent_expr (tpoint->cond);
219f2f23
PA
3764 sprintf (packet + strlen (packet), ":X%x,%s",
3765 tpoint->cond->length, buf);
3766 free (buf);
3767 }
3768}
3769
3770/* Compose a response that is an imitation of the syntax by which the
3771 tracepoint action was originally downloaded (with the difference
3772 that due to the way we store the actions, this will output a packet
3773 per action, while GDB could have combined more than one action
3774 per-packet. */
3775
3776static void
3777response_action (char *packet, struct tracepoint *tpoint,
3778 char *taction, int step)
3779{
3780 sprintf (packet, "%c%x:%s:%s",
3781 (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address),
3782 taction);
3783}
3784
3785/* Compose a response that is an imitation of the syntax by which the
3786 tracepoint source piece was originally downloaded. */
3787
3788static void
3789response_source (char *packet,
3790 struct tracepoint *tpoint, struct source_string *src)
3791{
3792 char *buf;
3793 int len;
3794
3795 len = strlen (src->str);
3796 buf = alloca (len * 2 + 1);
e9371aff 3797 bin2hex ((gdb_byte *) src->str, buf, len);
219f2f23
PA
3798
3799 sprintf (packet, "Z%x:%s:%s:%x:%x:%s",
3800 tpoint->number, paddress (tpoint->address),
3801 src->type, 0, len, buf);
3802}
3803
3804/* Return the first piece of tracepoint definition, and initialize the
3805 state machine that will iterate through all the tracepoint
3806 bits. */
3807
3808static void
3809cmd_qtfp (char *packet)
3810{
3811 trace_debug ("Returning first tracepoint definition piece");
3812
3813 cur_tpoint = tracepoints;
e64f7499 3814 cur_action = cur_step_action = 0;
219f2f23
PA
3815 cur_source_string = NULL;
3816
3817 if (cur_tpoint)
3818 response_tracepoint (packet, cur_tpoint);
3819 else
3820 strcpy (packet, "l");
3821}
3822
3823/* Return additional pieces of tracepoint definition. Each action and
3824 stepping action must go into its own packet, because of packet size
3825 limits, and so we use state variables to deliver one piece at a
3826 time. */
3827
3828static void
3829cmd_qtsp (char *packet)
3830{
3831 trace_debug ("Returning subsequent tracepoint definition piece");
3832
3833 if (!cur_tpoint)
3834 {
3835 /* This case would normally never occur, but be prepared for
3836 GDB misbehavior. */
3837 strcpy (packet, "l");
3838 }
e64f7499 3839 else if (cur_action < cur_tpoint->numactions)
219f2f23 3840 {
219f2f23
PA
3841 response_action (packet, cur_tpoint,
3842 cur_tpoint->actions_str[cur_action], 0);
e64f7499 3843 ++cur_action;
219f2f23 3844 }
e64f7499 3845 else if (cur_step_action < cur_tpoint->num_step_actions)
219f2f23 3846 {
219f2f23
PA
3847 response_action (packet, cur_tpoint,
3848 cur_tpoint->step_actions_str[cur_step_action], 1);
e64f7499 3849 ++cur_step_action;
219f2f23
PA
3850 }
3851 else if ((cur_source_string
3852 ? cur_source_string->next
3853 : cur_tpoint->source_strings))
3854 {
3855 if (cur_source_string)
3856 cur_source_string = cur_source_string->next;
3857 else
3858 cur_source_string = cur_tpoint->source_strings;
3859 response_source (packet, cur_tpoint, cur_source_string);
3860 }
3861 else
3862 {
3863 cur_tpoint = cur_tpoint->next;
e64f7499 3864 cur_action = cur_step_action = 0;
219f2f23
PA
3865 cur_source_string = NULL;
3866 if (cur_tpoint)
3867 response_tracepoint (packet, cur_tpoint);
3868 else
3869 strcpy (packet, "l");
3870 }
3871}
3872
3873/* Compose a response that is an imitation of the syntax by which the
3874 trace state variable was originally downloaded. */
3875
3876static void
3877response_tsv (char *packet, struct trace_state_variable *tsv)
3878{
3879 char *buf = (char *) "";
3880 int namelen;
3881
3882 if (tsv->name)
3883 {
3884 namelen = strlen (tsv->name);
3885 buf = alloca (namelen * 2 + 1);
e9371aff 3886 bin2hex ((gdb_byte *) tsv->name, buf, namelen);
219f2f23
PA
3887 }
3888
3889 sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0),
3890 tsv->getter ? 1 : 0, buf);
3891}
3892
3893/* Return the first trace state variable definition, and initialize
3894 the state machine that will iterate through all the tsv bits. */
3895
3896static void
3897cmd_qtfv (char *packet)
3898{
3899 trace_debug ("Returning first trace state variable definition");
3900
3901 cur_tsv = trace_state_variables;
3902
3903 if (cur_tsv)
3904 response_tsv (packet, cur_tsv);
3905 else
3906 strcpy (packet, "l");
3907}
3908
3909/* Return additional trace state variable definitions. */
3910
3911static void
3912cmd_qtsv (char *packet)
3913{
918d227b 3914 trace_debug ("Returning additional trace state variable definition");
219f2f23 3915
918d227b 3916 if (cur_tsv)
219f2f23
PA
3917 {
3918 cur_tsv = cur_tsv->next;
3919 if (cur_tsv)
3920 response_tsv (packet, cur_tsv);
3921 else
3922 strcpy (packet, "l");
3923 }
3924 else
3925 strcpy (packet, "l");
3926}
3927
0fb4aa4b
PA
3928/* Return the first static tracepoint marker, and initialize the state
3929 machine that will iterate through all the static tracepoints
3930 markers. */
3931
3932static void
3933cmd_qtfstm (char *packet)
3934{
3935 if (!maybe_write_ipa_ust_not_loaded (packet))
42476b70 3936 run_inferior_command (packet, strlen (packet) + 1);
0fb4aa4b
PA
3937}
3938
3939/* Return additional static tracepoints markers. */
3940
3941static void
3942cmd_qtsstm (char *packet)
3943{
3944 if (!maybe_write_ipa_ust_not_loaded (packet))
42476b70 3945 run_inferior_command (packet, strlen (packet) + 1);
0fb4aa4b
PA
3946}
3947
3948/* Return the definition of the static tracepoint at a given address.
3949 Result packet is the same as qTsST's. */
3950
3951static void
3952cmd_qtstmat (char *packet)
3953{
3954 if (!maybe_write_ipa_ust_not_loaded (packet))
42476b70 3955 run_inferior_command (packet, strlen (packet) + 1);
0fb4aa4b
PA
3956}
3957
649ebbca
DE
3958/* Helper for gdb_agent_about_to_close.
3959 Return non-zero if thread ENTRY is in the same process in DATA. */
3960
3961static int
3962same_process_p (struct inferior_list_entry *entry, void *data)
3963{
3964 int *pid = data;
3965
3966 return ptid_get_pid (entry->id) == *pid;
3967}
3968
7255706c
YQ
3969/* Sent the agent a command to close it. */
3970
3971void
3972gdb_agent_about_to_close (int pid)
3973{
3974 char buf[IPA_CMD_BUF_SIZE];
3975
3976 if (!maybe_write_ipa_not_loaded (buf))
3977 {
0bfdf32f 3978 struct thread_info *saved_thread;
7255706c 3979
0bfdf32f 3980 saved_thread = current_thread;
7255706c 3981
649ebbca 3982 /* Find any thread which belongs to process PID. */
0bfdf32f 3983 current_thread = (struct thread_info *)
649ebbca 3984 find_inferior (&all_threads, same_process_p, &pid);
7255706c
YQ
3985
3986 strcpy (buf, "close");
3987
3988 run_inferior_command (buf, strlen (buf) + 1);
3989
0bfdf32f 3990 current_thread = saved_thread;
7255706c
YQ
3991 }
3992}
3993
405f8e94
SS
3994/* Return the minimum instruction size needed for fast tracepoints as a
3995 hexadecimal number. */
3996
3997static void
3998cmd_qtminftpilen (char *packet)
3999{
0bfdf32f 4000 if (current_thread == NULL)
e886a173
PA
4001 {
4002 /* Indicate that the minimum length is currently unknown. */
4003 strcpy (packet, "0");
4004 return;
4005 }
4006
405f8e94
SS
4007 sprintf (packet, "%x", target_get_min_fast_tracepoint_insn_len ());
4008}
4009
219f2f23
PA
4010/* Respond to qTBuffer packet with a block of raw data from the trace
4011 buffer. GDB may ask for a lot, but we are allowed to reply with
4012 only as much as will fit within packet limits or whatever. */
4013
4014static void
4015cmd_qtbuffer (char *own_buf)
4016{
4017 ULONGEST offset, num, tot;
4018 unsigned char *tbp;
4019 char *packet = own_buf;
4020
4021 packet += strlen ("qTBuffer:");
4022
4023 packet = unpack_varlen_hex (packet, &offset);
4024 ++packet; /* skip a comma */
f8f67713 4025 unpack_varlen_hex (packet, &num);
219f2f23
PA
4026
4027 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
736cd585 4028 (int) num, phex_nz (offset, 0));
219f2f23
PA
4029
4030 tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
4031
4032 /* If we're right at the end, reply specially that we're done. */
4033 if (offset == tot)
4034 {
4035 strcpy (own_buf, "l");
4036 return;
4037 }
4038
4039 /* Object to any other out-of-bounds request. */
4040 if (offset > tot)
4041 {
4042 write_enn (own_buf);
4043 return;
4044 }
4045
4046 /* Compute the pointer corresponding to the given offset, accounting
4047 for wraparound. */
4048 tbp = trace_buffer_start + offset;
4049 if (tbp >= trace_buffer_wrap)
4050 tbp -= (trace_buffer_wrap - trace_buffer_lo);
4051
4052 /* Trim to the remaining bytes if we're close to the end. */
4053 if (num > tot - offset)
4054 num = tot - offset;
4055
4056 /* Trim to available packet size. */
4057 if (num >= (PBUFSIZ - 16) / 2 )
4058 num = (PBUFSIZ - 16) / 2;
4059
e9371aff 4060 bin2hex (tbp, own_buf, num);
219f2f23
PA
4061}
4062
4063static void
277e4e52 4064cmd_bigqtbuffer_circular (char *own_buf)
219f2f23
PA
4065{
4066 ULONGEST val;
4067 char *packet = own_buf;
4068
277e4e52 4069 packet += strlen ("QTBuffer:circular:");
219f2f23 4070
277e4e52
PA
4071 unpack_varlen_hex (packet, &val);
4072 circular_trace_buffer = val;
4073 trace_debug ("Trace buffer is now %s",
4074 circular_trace_buffer ? "circular" : "linear");
4075 write_ok (own_buf);
219f2f23
PA
4076}
4077
f6f899bf
HAQ
4078static void
4079cmd_bigqtbuffer_size (char *own_buf)
4080{
4081 ULONGEST val;
4082 LONGEST sval;
4083 char *packet = own_buf;
4084
4085 /* Can't change the size during a tracing run. */
4086 if (tracing)
4087 {
4088 write_enn (own_buf);
4089 return;
4090 }
4091
4092 packet += strlen ("QTBuffer:size:");
4093
4094 /* -1 is sent as literal "-1". */
4095 if (strcmp (packet, "-1") == 0)
4096 sval = DEFAULT_TRACE_BUFFER_SIZE;
4097 else
4098 {
4099 unpack_varlen_hex (packet, &val);
4100 sval = (LONGEST) val;
4101 }
4102
4103 init_trace_buffer (sval);
4104 trace_debug ("Trace buffer is now %s bytes",
4105 plongest (trace_buffer_size));
4106 write_ok (own_buf);
4107}
4108
f196051f
SS
4109static void
4110cmd_qtnotes (char *own_buf)
4111{
4112 size_t nbytes;
4113 char *saved, *user, *notes, *stopnote;
4114 char *packet = own_buf;
4115
4116 packet += strlen ("QTNotes:");
4117
4118 while (*packet)
4119 {
4120 if (strncmp ("user:", packet, strlen ("user:")) == 0)
4121 {
4122 packet += strlen ("user:");
4123 saved = packet;
4124 packet = strchr (packet, ';');
4125 nbytes = (packet - saved) / 2;
4126 user = xmalloc (nbytes + 1);
ff0e980e 4127 nbytes = hex2bin (saved, (gdb_byte *) user, nbytes);
f196051f
SS
4128 user[nbytes] = '\0';
4129 ++packet; /* skip the semicolon */
4130 trace_debug ("User is '%s'", user);
8e1d55a3 4131 xfree (tracing_user_name);
f196051f
SS
4132 tracing_user_name = user;
4133 }
4134 else if (strncmp ("notes:", packet, strlen ("notes:")) == 0)
4135 {
4136 packet += strlen ("notes:");
4137 saved = packet;
4138 packet = strchr (packet, ';');
4139 nbytes = (packet - saved) / 2;
4140 notes = xmalloc (nbytes + 1);
ff0e980e 4141 nbytes = hex2bin (saved, (gdb_byte *) notes, nbytes);
f196051f
SS
4142 notes[nbytes] = '\0';
4143 ++packet; /* skip the semicolon */
4144 trace_debug ("Notes is '%s'", notes);
8e1d55a3 4145 xfree (tracing_notes);
f196051f
SS
4146 tracing_notes = notes;
4147 }
4148 else if (strncmp ("tstop:", packet, strlen ("tstop:")) == 0)
4149 {
4150 packet += strlen ("tstop:");
4151 saved = packet;
4152 packet = strchr (packet, ';');
4153 nbytes = (packet - saved) / 2;
4154 stopnote = xmalloc (nbytes + 1);
ff0e980e 4155 nbytes = hex2bin (saved, (gdb_byte *) stopnote, nbytes);
f196051f
SS
4156 stopnote[nbytes] = '\0';
4157 ++packet; /* skip the semicolon */
4158 trace_debug ("tstop note is '%s'", stopnote);
8e1d55a3 4159 xfree (tracing_stop_note);
f196051f
SS
4160 tracing_stop_note = stopnote;
4161 }
4162 else
4163 break;
4164 }
4165
4166 write_ok (own_buf);
4167}
4168
219f2f23
PA
4169int
4170handle_tracepoint_general_set (char *packet)
4171{
4172 if (strcmp ("QTinit", packet) == 0)
4173 {
4174 cmd_qtinit (packet);
4175 return 1;
4176 }
4177 else if (strncmp ("QTDP:", packet, strlen ("QTDP:")) == 0)
4178 {
4179 cmd_qtdp (packet);
4180 return 1;
4181 }
4182 else if (strncmp ("QTDPsrc:", packet, strlen ("QTDPsrc:")) == 0)
4183 {
4184 cmd_qtdpsrc (packet);
4185 return 1;
4186 }
d248b706
KY
4187 else if (strncmp ("QTEnable:", packet, strlen ("QTEnable:")) == 0)
4188 {
4189 cmd_qtenable_disable (packet, 1);
4190 return 1;
4191 }
4192 else if (strncmp ("QTDisable:", packet, strlen ("QTDisable:")) == 0)
4193 {
4194 cmd_qtenable_disable (packet, 0);
4195 return 1;
4196 }
219f2f23
PA
4197 else if (strncmp ("QTDV:", packet, strlen ("QTDV:")) == 0)
4198 {
4199 cmd_qtdv (packet);
4200 return 1;
4201 }
4202 else if (strncmp ("QTro:", packet, strlen ("QTro:")) == 0)
4203 {
4204 cmd_qtro (packet);
4205 return 1;
4206 }
4207 else if (strcmp ("QTStart", packet) == 0)
4208 {
4209 cmd_qtstart (packet);
4210 return 1;
4211 }
4212 else if (strcmp ("QTStop", packet) == 0)
4213 {
4214 cmd_qtstop (packet);
4215 return 1;
4216 }
8336d594
PA
4217 else if (strncmp ("QTDisconnected:", packet,
4218 strlen ("QTDisconnected:")) == 0)
4219 {
4220 cmd_qtdisconnected (packet);
4221 return 1;
4222 }
219f2f23
PA
4223 else if (strncmp ("QTFrame:", packet, strlen ("QTFrame:")) == 0)
4224 {
4225 cmd_qtframe (packet);
4226 return 1;
4227 }
277e4e52 4228 else if (strncmp ("QTBuffer:circular:", packet, strlen ("QTBuffer:circular:")) == 0)
219f2f23 4229 {
277e4e52 4230 cmd_bigqtbuffer_circular (packet);
219f2f23
PA
4231 return 1;
4232 }
f6f899bf
HAQ
4233 else if (strncmp ("QTBuffer:size:", packet, strlen ("QTBuffer:size:")) == 0)
4234 {
4235 cmd_bigqtbuffer_size (packet);
4236 return 1;
4237 }
f196051f
SS
4238 else if (strncmp ("QTNotes:", packet, strlen ("QTNotes:")) == 0)
4239 {
4240 cmd_qtnotes (packet);
4241 return 1;
4242 }
219f2f23
PA
4243
4244 return 0;
4245}
4246
4247int
4248handle_tracepoint_query (char *packet)
4249{
4250 if (strcmp ("qTStatus", packet) == 0)
4251 {
4252 cmd_qtstatus (packet);
4253 return 1;
4254 }
f196051f
SS
4255 else if (strncmp ("qTP:", packet, strlen ("qTP:")) == 0)
4256 {
4257 cmd_qtp (packet);
4258 return 1;
4259 }
219f2f23
PA
4260 else if (strcmp ("qTfP", packet) == 0)
4261 {
4262 cmd_qtfp (packet);
4263 return 1;
4264 }
4265 else if (strcmp ("qTsP", packet) == 0)
4266 {
4267 cmd_qtsp (packet);
4268 return 1;
4269 }
4270 else if (strcmp ("qTfV", packet) == 0)
4271 {
4272 cmd_qtfv (packet);
4273 return 1;
4274 }
4275 else if (strcmp ("qTsV", packet) == 0)
4276 {
4277 cmd_qtsv (packet);
4278 return 1;
4279 }
4280 else if (strncmp ("qTV:", packet, strlen ("qTV:")) == 0)
4281 {
4282 cmd_qtv (packet);
4283 return 1;
4284 }
4285 else if (strncmp ("qTBuffer:", packet, strlen ("qTBuffer:")) == 0)
4286 {
4287 cmd_qtbuffer (packet);
4288 return 1;
4289 }
0fb4aa4b
PA
4290 else if (strcmp ("qTfSTM", packet) == 0)
4291 {
4292 cmd_qtfstm (packet);
4293 return 1;
4294 }
4295 else if (strcmp ("qTsSTM", packet) == 0)
4296 {
4297 cmd_qtsstm (packet);
4298 return 1;
4299 }
4300 else if (strncmp ("qTSTMat:", packet, strlen ("qTSTMat:")) == 0)
4301 {
4302 cmd_qtstmat (packet);
4303 return 1;
4304 }
405f8e94
SS
4305 else if (strcmp ("qTMinFTPILen", packet) == 0)
4306 {
4307 cmd_qtminftpilen (packet);
4308 return 1;
4309 }
219f2f23
PA
4310
4311 return 0;
4312}
4313
fa593d66
PA
4314#endif
4315#ifndef IN_PROCESS_AGENT
4316
219f2f23
PA
4317/* Call this when thread TINFO has hit the tracepoint defined by
4318 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4319 action. This adds a while-stepping collecting state item to the
4320 threads' collecting state list, so that we can keep track of
4321 multiple simultaneous while-stepping actions being collected by the
4322 same thread. This can happen in cases like:
4323
4324 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4325 ff0002 INSN2
4326 ff0003 INSN3 <-- TP2, collect $regs
4327 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4328 ff0005 INSN5
4329
4330 Notice that when instruction INSN5 is reached, the while-stepping
4331 actions of both TP1 and TP3 are still being collected, and that TP2
4332 had been collected meanwhile. The whole range of ff0001-ff0005
4333 should be single-stepped, due to at least TP1's while-stepping
4334 action covering the whole range. */
4335
4336static void
4337add_while_stepping_state (struct thread_info *tinfo,
4338 int tp_number, CORE_ADDR tp_address)
4339{
4340 struct wstep_state *wstep;
4341
4342 wstep = xmalloc (sizeof (*wstep));
4343 wstep->next = tinfo->while_stepping;
4344
4345 wstep->tp_number = tp_number;
4346 wstep->tp_address = tp_address;
4347 wstep->current_step = 0;
4348
4349 tinfo->while_stepping = wstep;
4350}
4351
4352/* Release the while-stepping collecting state WSTEP. */
4353
4354static void
4355release_while_stepping_state (struct wstep_state *wstep)
4356{
4357 free (wstep);
4358}
4359
4360/* Release all while-stepping collecting states currently associated
4361 with thread TINFO. */
4362
4363void
4364release_while_stepping_state_list (struct thread_info *tinfo)
4365{
4366 struct wstep_state *head;
4367
4368 while (tinfo->while_stepping)
4369 {
4370 head = tinfo->while_stepping;
4371 tinfo->while_stepping = head->next;
4372 release_while_stepping_state (head);
4373 }
4374}
4375
4376/* If TINFO was handling a 'while-stepping' action, the step has
4377 finished, so collect any step data needed, and check if any more
4378 steps are required. Return true if the thread was indeed
4379 collecting tracepoint data, false otherwise. */
4380
4381int
4382tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
4383{
4384 struct tracepoint *tpoint;
4385 struct wstep_state *wstep;
4386 struct wstep_state **wstep_link;
4387 struct trap_tracepoint_ctx ctx;
4388
fa593d66
PA
4389 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4390 our buffer. */
58b4daa5 4391 if (agent_loaded_p ())
fa593d66
PA
4392 upload_fast_traceframes ();
4393
219f2f23
PA
4394 /* Check if we were indeed collecting data for one of more
4395 tracepoints with a 'while-stepping' count. */
4396 if (tinfo->while_stepping == NULL)
4397 return 0;
4398
4399 if (!tracing)
4400 {
4401 /* We're not even tracing anymore. Stop this thread from
4402 collecting. */
4403 release_while_stepping_state_list (tinfo);
4404
4405 /* The thread had stopped due to a single-step request indeed
4406 explained by a tracepoint. */
4407 return 1;
4408 }
4409
4410 wstep = tinfo->while_stepping;
4411 wstep_link = &tinfo->while_stepping;
4412
4413 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4414 target_pid_to_str (tinfo->entry.id),
4415 wstep->tp_number, paddress (wstep->tp_address));
4416
fa593d66 4417 ctx.base.type = trap_tracepoint;
219f2f23
PA
4418 ctx.regcache = get_thread_regcache (tinfo, 1);
4419
4420 while (wstep != NULL)
4421 {
4422 tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
4423 if (tpoint == NULL)
4424 {
4425 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4426 wstep->tp_number, paddress (wstep->tp_address),
4427 target_pid_to_str (tinfo->entry.id));
4428
4429 /* Unlink. */
4430 *wstep_link = wstep->next;
4431 release_while_stepping_state (wstep);
4f269b12 4432 wstep = *wstep_link;
219f2f23
PA
4433 continue;
4434 }
4435
4436 /* We've just finished one step. */
4437 ++wstep->current_step;
4438
4439 /* Collect data. */
4440 collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
4441 stop_pc, tpoint, wstep->current_step);
4442
4443 if (wstep->current_step >= tpoint->step_count)
4444 {
4445 /* The requested numbers of steps have occurred. */
4446 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4447 target_pid_to_str (tinfo->entry.id),
4448 wstep->tp_number, paddress (wstep->tp_address));
4449
4450 /* Unlink the wstep. */
4451 *wstep_link = wstep->next;
4452 release_while_stepping_state (wstep);
4453 wstep = *wstep_link;
4454
4455 /* Only check the hit count now, which ensure that we do all
4456 our stepping before stopping the run. */
4457 if (tpoint->pass_count > 0
4458 && tpoint->hit_count >= tpoint->pass_count
4459 && stopping_tracepoint == NULL)
4460 stopping_tracepoint = tpoint;
4461 }
4462 else
4463 {
4464 /* Keep single-stepping until the requested numbers of steps
4465 have occurred. */
4466 wstep_link = &wstep->next;
4467 wstep = *wstep_link;
4468 }
4469
4470 if (stopping_tracepoint
4471 || trace_buffer_is_full
4472 || expr_eval_result != expr_eval_no_error)
4473 {
4474 stop_tracing ();
4475 break;
4476 }
4477 }
4478
4479 return 1;
4480}
4481
fa593d66
PA
4482/* Handle any internal tracing control breakpoint hits. That means,
4483 pull traceframes from the IPA to our buffer, and syncing both
4484 tracing agents when the IPA's tracing stops for some reason. */
4485
4486int
4487handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
4488{
4489 /* Pull in fast tracepoint trace frames from the inferior in-process
4490 agent's buffer into our buffer. */
4491
58b4daa5 4492 if (!agent_loaded_p ())
fa593d66
PA
4493 return 0;
4494
4495 upload_fast_traceframes ();
4496
4497 /* Check if the in-process agent had decided we should stop
4498 tracing. */
4499 if (stop_pc == ipa_sym_addrs.addr_stop_tracing)
4500 {
4501 int ipa_trace_buffer_is_full;
4502 CORE_ADDR ipa_stopping_tracepoint;
4503 int ipa_expr_eval_result;
4504 CORE_ADDR ipa_error_tracepoint;
4505
4506 trace_debug ("lib stopped at stop_tracing");
4507
4508 read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full,
4509 &ipa_trace_buffer_is_full);
4510
4511 read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
4512 &ipa_stopping_tracepoint);
4513 write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0);
4514
4515 read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint,
4516 &ipa_error_tracepoint);
4517 write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0);
4518
4519 read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result,
4520 &ipa_expr_eval_result);
4521 write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0);
4522
4523 trace_debug ("lib: trace_buffer_is_full: %d, "
4524 "stopping_tracepoint: %s, "
4525 "ipa_expr_eval_result: %d, "
4526 "error_tracepoint: %s, ",
4527 ipa_trace_buffer_is_full,
4528 paddress (ipa_stopping_tracepoint),
4529 ipa_expr_eval_result,
4530 paddress (ipa_error_tracepoint));
4531
4532 if (debug_threads)
4533 {
4534 if (ipa_trace_buffer_is_full)
4535 trace_debug ("lib stopped due to full buffer.");
4536 if (ipa_stopping_tracepoint)
4537 trace_debug ("lib stopped due to tpoint");
4538 if (ipa_stopping_tracepoint)
4539 trace_debug ("lib stopped due to error");
4540 }
4541
4542 if (ipa_stopping_tracepoint != 0)
4543 {
4544 stopping_tracepoint
4545 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint);
4546 }
4547 else if (ipa_expr_eval_result != expr_eval_no_error)
4548 {
4549 expr_eval_result = ipa_expr_eval_result;
4550 error_tracepoint
4551 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint);
4552 }
4553 stop_tracing ();
4554 return 1;
4555 }
4556 else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer)
4557 {
4558 trace_debug ("lib stopped at flush_trace_buffer");
4559 return 1;
4560 }
4561
4562 return 0;
4563}
4564
219f2f23
PA
4565/* Return true if TINFO just hit a tracepoint. Collect data if
4566 so. */
4567
4568int
4569tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
4570{
4571 struct tracepoint *tpoint;
4572 int ret = 0;
4573 struct trap_tracepoint_ctx ctx;
4574
4575 /* Not tracing, don't handle. */
4576 if (!tracing)
4577 return 0;
4578
fa593d66 4579 ctx.base.type = trap_tracepoint;
219f2f23
PA
4580 ctx.regcache = get_thread_regcache (tinfo, 1);
4581
4582 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
4583 {
fa593d66
PA
4584 /* Note that we collect fast tracepoints here as well. We'll
4585 step over the fast tracepoint jump later, which avoids the
f72429c5
YQ
4586 double collect. However, we don't collect for static
4587 tracepoints here, because UST markers are compiled in program,
4588 and probes will be executed in program. So static tracepoints
4589 are collected there. */
4590 if (tpoint->enabled && stop_pc == tpoint->address
4591 && tpoint->type != static_tracepoint)
219f2f23
PA
4592 {
4593 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4594 target_pid_to_str (tinfo->entry.id),
4595 tpoint->number, paddress (tpoint->address));
4596
4597 /* Test the condition if present, and collect if true. */
4598 if (!tpoint->cond
4599 || (condition_true_at_tracepoint
4600 ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
4601 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
4602 stop_pc, tpoint);
4603
4604 if (stopping_tracepoint
4605 || trace_buffer_is_full
4606 || expr_eval_result != expr_eval_no_error)
4607 {
4608 stop_tracing ();
4609 }
4610 /* If the tracepoint had a 'while-stepping' action, then set
4611 the thread to collect this tracepoint on the following
4612 single-steps. */
4613 else if (tpoint->step_count > 0)
4614 {
4615 add_while_stepping_state (tinfo,
4616 tpoint->number, tpoint->address);
4617 }
4618
4619 ret = 1;
4620 }
4621 }
4622
4623 return ret;
4624}
4625
fa593d66
PA
4626#endif
4627
0fb4aa4b
PA
4628#if defined IN_PROCESS_AGENT && defined HAVE_UST
4629struct ust_marker_data;
4630static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
0fb4aa4b
PA
4631 struct traceframe *tframe);
4632#endif
4633
219f2f23
PA
4634/* Create a trace frame for the hit of the given tracepoint in the
4635 given thread. */
4636
4637static void
4638collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
4639 struct tracepoint *tpoint)
4640{
4641 struct traceframe *tframe;
4642 int acti;
4643
4644 /* Only count it as a hit when we actually collect data. */
4645 tpoint->hit_count++;
4646
4647 /* If we've exceeded a defined pass count, record the event for
4648 later, and finish the collection for this hit. This test is only
4649 for nonstepping tracepoints, stepping tracepoints test at the end
4650 of their while-stepping loop. */
4651 if (tpoint->pass_count > 0
4652 && tpoint->hit_count >= tpoint->pass_count
4653 && tpoint->step_count == 0
4654 && stopping_tracepoint == NULL)
4655 stopping_tracepoint = tpoint;
4656
5f18041e 4657 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64,
219f2f23
PA
4658 tpoint->number, paddress (tpoint->address), tpoint->hit_count);
4659
4660 tframe = add_traceframe (tpoint);
4661
4662 if (tframe)
4663 {
4664 for (acti = 0; acti < tpoint->numactions; ++acti)
4665 {
fa593d66 4666#ifndef IN_PROCESS_AGENT
219f2f23
PA
4667 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4668 tpoint->number, paddress (tpoint->address),
4669 tpoint->actions_str[acti]);
fa593d66 4670#endif
219f2f23
PA
4671
4672 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4673 tpoint->actions[acti]);
4674 }
4675
4676 finish_traceframe (tframe);
4677 }
4678
4679 if (tframe == NULL && tracing)
4680 trace_buffer_is_full = 1;
4681}
4682
fa593d66
PA
4683#ifndef IN_PROCESS_AGENT
4684
219f2f23
PA
4685static void
4686collect_data_at_step (struct tracepoint_hit_ctx *ctx,
4687 CORE_ADDR stop_pc,
4688 struct tracepoint *tpoint, int current_step)
4689{
4690 struct traceframe *tframe;
4691 int acti;
4692
4693 trace_debug ("Making new step traceframe for "
5f18041e 4694 "tracepoint %d at 0x%s, step %d of %" PRIu64 ", hit %" PRIu64,
219f2f23
PA
4695 tpoint->number, paddress (tpoint->address),
4696 current_step, tpoint->step_count,
4697 tpoint->hit_count);
4698
4699 tframe = add_traceframe (tpoint);
4700
4701 if (tframe)
4702 {
4703 for (acti = 0; acti < tpoint->num_step_actions; ++acti)
4704 {
4705 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4706 tpoint->number, paddress (tpoint->address),
4707 tpoint->step_actions_str[acti]);
4708
4709 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4710 tpoint->step_actions[acti]);
4711 }
4712
4713 finish_traceframe (tframe);
4714 }
4715
4716 if (tframe == NULL && tracing)
4717 trace_buffer_is_full = 1;
4718}
4719
fa593d66
PA
4720#endif
4721
3aee8918
PA
4722#ifdef IN_PROCESS_AGENT
4723/* The target description used by the IPA. Given that the IPA library
4724 is built for a specific architecture that is loaded into the
4725 inferior, there only needs to be one such description per
4726 build. */
4727const struct target_desc *ipa_tdesc;
4728#endif
4729
219f2f23
PA
4730static struct regcache *
4731get_context_regcache (struct tracepoint_hit_ctx *ctx)
4732{
fa593d66
PA
4733 struct regcache *regcache = NULL;
4734
4735#ifdef IN_PROCESS_AGENT
4736 if (ctx->type == fast_tracepoint)
4737 {
4738 struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
4739 if (!fctx->regcache_initted)
4740 {
4741 fctx->regcache_initted = 1;
3aee8918 4742 init_register_cache (&fctx->regcache, ipa_tdesc, fctx->regspace);
fa593d66
PA
4743 supply_regblock (&fctx->regcache, NULL);
4744 supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
4745 }
4746 regcache = &fctx->regcache;
4747 }
0fb4aa4b
PA
4748#ifdef HAVE_UST
4749 if (ctx->type == static_tracepoint)
4750 {
493e2a69
MS
4751 struct static_tracepoint_ctx *sctx
4752 = (struct static_tracepoint_ctx *) ctx;
4753
0fb4aa4b
PA
4754 if (!sctx->regcache_initted)
4755 {
4756 sctx->regcache_initted = 1;
3aee8918 4757 init_register_cache (&sctx->regcache, ipa_tdesc, sctx->regspace);
0fb4aa4b
PA
4758 supply_regblock (&sctx->regcache, NULL);
4759 /* Pass down the tracepoint address, because REGS doesn't
4760 include the PC, but we know what it must have been. */
4761 supply_static_tracepoint_registers (&sctx->regcache,
4762 (const unsigned char *)
4763 sctx->regs,
4764 sctx->tpoint->address);
4765 }
4766 regcache = &sctx->regcache;
4767 }
4768#endif
fa593d66
PA
4769#else
4770 if (ctx->type == trap_tracepoint)
4771 {
4772 struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
4773 regcache = tctx->regcache;
4774 }
4775#endif
219f2f23
PA
4776
4777 gdb_assert (regcache != NULL);
4778
4779 return regcache;
4780}
4781
4782static void
4783do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4784 CORE_ADDR stop_pc,
4785 struct tracepoint *tpoint,
4786 struct traceframe *tframe,
4787 struct tracepoint_action *taction)
4788{
4789 enum eval_result_type err;
4790
4791 switch (taction->type)
4792 {
4793 case 'M':
4794 {
4795 struct collect_memory_action *maction;
5ae4861a 4796 struct eval_agent_expr_context ax_ctx;
219f2f23
PA
4797
4798 maction = (struct collect_memory_action *) taction;
5ae4861a
YQ
4799 ax_ctx.regcache = NULL;
4800 ax_ctx.tframe = tframe;
4801 ax_ctx.tpoint = tpoint;
219f2f23
PA
4802
4803 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4804 pulongest (maction->len),
4805 paddress (maction->addr), maction->basereg);
4806 /* (should use basereg) */
5ae4861a
YQ
4807 agent_mem_read (&ax_ctx, NULL, (CORE_ADDR) maction->addr,
4808 maction->len);
219f2f23
PA
4809 break;
4810 }
4811 case 'R':
4812 {
219f2f23
PA
4813 unsigned char *regspace;
4814 struct regcache tregcache;
4815 struct regcache *context_regcache;
3aee8918 4816 int regcache_size;
219f2f23
PA
4817
4818 trace_debug ("Want to collect registers");
4819
3aee8918
PA
4820 context_regcache = get_context_regcache (ctx);
4821 regcache_size = register_cache_size (context_regcache->tdesc);
4822
219f2f23 4823 /* Collect all registers for now. */
3aee8918 4824 regspace = add_traceframe_block (tframe, tpoint, 1 + regcache_size);
219f2f23
PA
4825 if (regspace == NULL)
4826 {
4827 trace_debug ("Trace buffer block allocation failed, skipping");
4828 break;
4829 }
4830 /* Identify a register block. */
4831 *regspace = 'R';
4832
219f2f23
PA
4833 /* Wrap the regblock in a register cache (in the stack, we
4834 don't want to malloc here). */
3aee8918
PA
4835 init_register_cache (&tregcache, context_regcache->tdesc,
4836 regspace + 1);
219f2f23
PA
4837
4838 /* Copy the register data to the regblock. */
4839 regcache_cpy (&tregcache, context_regcache);
4840
fa593d66 4841#ifndef IN_PROCESS_AGENT
219f2f23
PA
4842 /* On some platforms, trap-based tracepoints will have the PC
4843 pointing to the next instruction after the trap, but we
4844 don't want the user or GDB trying to guess whether the
4845 saved PC needs adjusting; so always record the adjusted
4846 stop_pc. Note that we can't use tpoint->address instead,
fa593d66
PA
4847 since it will be wrong for while-stepping actions. This
4848 adjustment is a nop for fast tracepoints collected from the
4849 in-process lib (but not if GDBserver is collecting one
4850 preemptively), since the PC had already been adjusted to
4851 contain the tracepoint's address by the jump pad. */
219f2f23 4852 trace_debug ("Storing stop pc (0x%s) in regblock",
8d00225b 4853 paddress (stop_pc));
219f2f23
PA
4854
4855 /* This changes the regblock, not the thread's
4856 regcache. */
4857 regcache_write_pc (&tregcache, stop_pc);
fa593d66 4858#endif
219f2f23
PA
4859 }
4860 break;
4861 case 'X':
4862 {
4863 struct eval_expr_action *eaction;
5ae4861a 4864 struct eval_agent_expr_context ax_ctx;
219f2f23
PA
4865
4866 eaction = (struct eval_expr_action *) taction;
5ae4861a
YQ
4867 ax_ctx.regcache = get_context_regcache (ctx);
4868 ax_ctx.tframe = tframe;
4869 ax_ctx.tpoint = tpoint;
219f2f23
PA
4870
4871 trace_debug ("Want to evaluate expression");
4872
5ae4861a 4873 err = gdb_eval_agent_expr (&ax_ctx, eaction->expr, NULL);
219f2f23
PA
4874
4875 if (err != expr_eval_no_error)
4876 {
4877 record_tracepoint_error (tpoint, "action expression", err);
4878 return;
4879 }
4880 }
4881 break;
0fb4aa4b
PA
4882 case 'L':
4883 {
4884#if defined IN_PROCESS_AGENT && defined HAVE_UST
4885 trace_debug ("Want to collect static trace data");
19560ba5 4886 collect_ust_data_at_tracepoint (ctx, tframe);
0fb4aa4b
PA
4887#else
4888 trace_debug ("warning: collecting static trace data, "
4889 "but static tracepoints are not supported");
4890#endif
4891 }
4892 break;
219f2f23
PA
4893 default:
4894 trace_debug ("unknown trace action '%c', ignoring", taction->type);
4895 break;
4896 }
4897}
4898
4899static int
4900condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4901 struct tracepoint *tpoint)
4902{
4903 ULONGEST value = 0;
4904 enum eval_result_type err;
4905
c6beb2cb
PA
4906 /* Presently, gdbserver doesn't run compiled conditions, only the
4907 IPA does. If the program stops at a fast tracepoint's address
4908 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4909 gdbserver preemptively collect the fast tracepoint. Later, on
4910 resume, gdbserver steps over the fast tracepoint like it steps
4911 over breakpoints, so that the IPA doesn't see that fast
4912 tracepoint. This avoids double collects of fast tracepoints in
4913 that stopping scenario. Having gdbserver itself handle the fast
4914 tracepoint gives the user a consistent view of when fast or trap
4915 tracepoints are collected, compared to an alternative where only
4916 trap tracepoints are collected on stop, and fast tracepoints on
4917 resume. When a fast tracepoint is being processed by gdbserver,
4918 it is always the non-compiled condition expression that is
4919 used. */
4920#ifdef IN_PROCESS_AGENT
6a271cae
PA
4921 if (tpoint->compiled_cond)
4922 err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (ctx, &value);
4923 else
c6beb2cb 4924#endif
5ae4861a
YQ
4925 {
4926 struct eval_agent_expr_context ax_ctx;
219f2f23 4927
5ae4861a
YQ
4928 ax_ctx.regcache = get_context_regcache (ctx);
4929 ax_ctx.tframe = NULL;
4930 ax_ctx.tpoint = tpoint;
4931
4932 err = gdb_eval_agent_expr (&ax_ctx, tpoint->cond, &value);
4933 }
219f2f23
PA
4934 if (err != expr_eval_no_error)
4935 {
4936 record_tracepoint_error (tpoint, "condition", err);
4937 /* The error case must return false. */
4938 return 0;
4939 }
4940
4941 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4942 tpoint->number, paddress (tpoint->address),
4943 pulongest (value));
4944 return (value ? 1 : 0);
4945}
4946
219f2f23
PA
4947/* Do memory copies for bytecodes. */
4948/* Do the recording of memory blocks for actions and bytecodes. */
4949
5e1dc496 4950int
5ae4861a 4951agent_mem_read (struct eval_agent_expr_context *ctx,
219f2f23
PA
4952 unsigned char *to, CORE_ADDR from, ULONGEST len)
4953{
4954 unsigned char *mspace;
4955 ULONGEST remaining = len;
4956 unsigned short blocklen;
4957
4958 /* If a 'to' buffer is specified, use it. */
4959 if (to != NULL)
4960 {
4961 read_inferior_memory (from, to, len);
4962 return 0;
4963 }
4964
4965 /* Otherwise, create a new memory block in the trace buffer. */
4966 while (remaining > 0)
4967 {
4968 size_t sp;
4969
4970 blocklen = (remaining > 65535 ? 65535 : remaining);
4971 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
5ae4861a 4972 mspace = add_traceframe_block (ctx->tframe, ctx->tpoint, sp);
219f2f23
PA
4973 if (mspace == NULL)
4974 return 1;
4975 /* Identify block as a memory block. */
4976 *mspace = 'M';
4977 ++mspace;
4978 /* Record address and size. */
4979 memcpy (mspace, &from, sizeof (from));
4980 mspace += sizeof (from);
4981 memcpy (mspace, &blocklen, sizeof (blocklen));
4982 mspace += sizeof (blocklen);
4983 /* Record the memory block proper. */
4984 read_inferior_memory (from, mspace, blocklen);
4985 trace_debug ("%d bytes recorded", blocklen);
4986 remaining -= blocklen;
4987 from += blocklen;
4988 }
4989 return 0;
4990}
4991
5e1dc496 4992int
5ae4861a 4993agent_mem_read_string (struct eval_agent_expr_context *ctx,
3065dfb6
SS
4994 unsigned char *to, CORE_ADDR from, ULONGEST len)
4995{
4996 unsigned char *buf, *mspace;
4997 ULONGEST remaining = len;
4998 unsigned short blocklen, i;
4999
5000 /* To save a bit of space, block lengths are 16-bit, so break large
5001 requests into multiple blocks. Bordering on overkill for strings,
5002 but it could happen that someone specifies a large max length. */
5003 while (remaining > 0)
5004 {
5005 size_t sp;
5006
5007 blocklen = (remaining > 65535 ? 65535 : remaining);
5008 /* We want working space to accumulate nonzero bytes, since
5009 traceframes must have a predecided size (otherwise it gets
5010 harder to wrap correctly for the circular case, etc). */
5011 buf = (unsigned char *) xmalloc (blocklen + 1);
5012 for (i = 0; i < blocklen; ++i)
5013 {
5014 /* Read the string one byte at a time, in case the string is
5015 at the end of a valid memory area - we don't want a
5016 correctly-terminated string to engender segvio
5017 complaints. */
5018 read_inferior_memory (from + i, buf + i, 1);
5019
5020 if (buf[i] == '\0')
5021 {
5022 blocklen = i + 1;
5023 /* Make sure outer loop stops now too. */
5024 remaining = blocklen;
5025 break;
5026 }
5027 }
5028 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
5ae4861a 5029 mspace = add_traceframe_block (ctx->tframe, ctx->tpoint, sp);
3065dfb6
SS
5030 if (mspace == NULL)
5031 {
5032 xfree (buf);
5033 return 1;
5034 }
5035 /* Identify block as a memory block. */
5036 *mspace = 'M';
5037 ++mspace;
5038 /* Record address and size. */
5039 memcpy ((void *) mspace, (void *) &from, sizeof (from));
5040 mspace += sizeof (from);
5041 memcpy ((void *) mspace, (void *) &blocklen, sizeof (blocklen));
5042 mspace += sizeof (blocklen);
5043 /* Copy the string contents. */
5044 memcpy ((void *) mspace, (void *) buf, blocklen);
5045 remaining -= blocklen;
5046 from += blocklen;
5047 xfree (buf);
5048 }
5049 return 0;
5050}
5051
219f2f23
PA
5052/* Record the value of a trace state variable. */
5053
5e1dc496 5054int
5ae4861a 5055agent_tsv_read (struct eval_agent_expr_context *ctx, int n)
219f2f23
PA
5056{
5057 unsigned char *vspace;
5058 LONGEST val;
5059
5ae4861a 5060 vspace = add_traceframe_block (ctx->tframe, ctx->tpoint,
219f2f23
PA
5061 1 + sizeof (n) + sizeof (LONGEST));
5062 if (vspace == NULL)
5063 return 1;
5064 /* Identify block as a variable. */
5065 *vspace = 'V';
5066 /* Record variable's number and value. */
5067 memcpy (vspace + 1, &n, sizeof (n));
5068 val = get_trace_state_variable_value (n);
5069 memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
5070 trace_debug ("Variable %d recorded", n);
5071 return 0;
5072}
5073
fa593d66
PA
5074#ifndef IN_PROCESS_AGENT
5075
b3b9301e
PA
5076/* Callback for traceframe_walk_blocks, used to find a given block
5077 type in a traceframe. */
5078
5079static int
5080match_blocktype (char blocktype, unsigned char *dataptr, void *data)
5081{
5082 char *wantedp = data;
5083
5084 if (*wantedp == blocktype)
5085 return 1;
5086
5087 return 0;
5088}
5089
5090/* Walk over all traceframe blocks of the traceframe buffer starting
5091 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5092 block found, passing in DATA unmodified. If CALLBACK returns true,
5093 this returns a pointer to where the block is found. Returns NULL
5094 if no callback call returned true, indicating that all blocks have
5095 been walked. */
5096
219f2f23 5097static unsigned char *
b3b9301e
PA
5098traceframe_walk_blocks (unsigned char *database, unsigned int datasize,
5099 int tfnum,
5100 int (*callback) (char blocktype,
5101 unsigned char *dataptr,
5102 void *data),
5103 void *data)
219f2f23
PA
5104{
5105 unsigned char *dataptr;
5106
5107 if (datasize == 0)
5108 {
5109 trace_debug ("traceframe %d has no data", tfnum);
5110 return NULL;
5111 }
5112
5113 /* Iterate through a traceframe's blocks, looking for a block of the
5114 requested type. */
5115 for (dataptr = database;
5116 dataptr < database + datasize;
5117 /* nothing */)
5118 {
5119 char blocktype;
5120 unsigned short mlen;
5121
5122 if (dataptr == trace_buffer_wrap)
5123 {
5124 /* Adjust to reflect wrapping part of the frame around to
5125 the beginning. */
5126 datasize = dataptr - database;
5127 dataptr = database = trace_buffer_lo;
5128 }
b3b9301e 5129
219f2f23
PA
5130 blocktype = *dataptr++;
5131
b3b9301e 5132 if ((*callback) (blocktype, dataptr, data))
219f2f23
PA
5133 return dataptr;
5134
5135 switch (blocktype)
5136 {
5137 case 'R':
5138 /* Skip over the registers block. */
3aee8918 5139 dataptr += current_target_desc ()->registers_size;
219f2f23
PA
5140 break;
5141 case 'M':
5142 /* Skip over the memory block. */
5143 dataptr += sizeof (CORE_ADDR);
5144 memcpy (&mlen, dataptr, sizeof (mlen));
5145 dataptr += (sizeof (mlen) + mlen);
5146 break;
219f2f23
PA
5147 case 'V':
5148 /* Skip over the TSV block. */
5149 dataptr += (sizeof (int) + sizeof (LONGEST));
5150 break;
0fb4aa4b
PA
5151 case 'S':
5152 /* Skip over the static trace data block. */
5153 memcpy (&mlen, dataptr, sizeof (mlen));
5154 dataptr += (sizeof (mlen) + mlen);
5155 break;
219f2f23
PA
5156 default:
5157 trace_debug ("traceframe %d has unknown block type 0x%x",
5158 tfnum, blocktype);
5159 return NULL;
5160 }
5161 }
5162
5163 return NULL;
5164}
5165
b3b9301e
PA
5166/* Look for the block of type TYPE_WANTED in the trameframe starting
5167 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5168 number. */
5169
5170static unsigned char *
5171traceframe_find_block_type (unsigned char *database, unsigned int datasize,
5172 int tfnum, char type_wanted)
5173{
5174 return traceframe_walk_blocks (database, datasize, tfnum,
5175 match_blocktype, &type_wanted);
5176}
5177
219f2f23
PA
5178static unsigned char *
5179traceframe_find_regblock (struct traceframe *tframe, int tfnum)
5180{
5181 unsigned char *regblock;
5182
5183 regblock = traceframe_find_block_type (tframe->data,
5184 tframe->data_size,
5185 tfnum, 'R');
5186
5187 if (regblock == NULL)
5188 trace_debug ("traceframe %d has no register data", tfnum);
5189
5190 return regblock;
5191}
5192
5193/* Get registers from a traceframe. */
5194
5195int
5196fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
5197{
5198 unsigned char *dataptr;
5199 struct tracepoint *tpoint;
5200 struct traceframe *tframe;
5201
5202 tframe = find_traceframe (tfnum);
5203
5204 if (tframe == NULL)
5205 {
5206 trace_debug ("traceframe %d not found", tfnum);
5207 return 1;
5208 }
5209
5210 dataptr = traceframe_find_regblock (tframe, tfnum);
5211 if (dataptr == NULL)
5212 {
1c79eb8a 5213 /* Mark registers unavailable. */
219f2f23
PA
5214 supply_regblock (regcache, NULL);
5215
5216 /* We can generally guess at a PC, although this will be
5217 misleading for while-stepping frames and multi-location
5218 tracepoints. */
5219 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
5220 if (tpoint != NULL)
5221 regcache_write_pc (regcache, tpoint->address);
5222 }
5223 else
5224 supply_regblock (regcache, dataptr);
5225
5226 return 0;
5227}
5228
5229static CORE_ADDR
5230traceframe_get_pc (struct traceframe *tframe)
5231{
5232 struct regcache regcache;
5233 unsigned char *dataptr;
3aee8918 5234 const struct target_desc *tdesc = current_target_desc ();
219f2f23
PA
5235
5236 dataptr = traceframe_find_regblock (tframe, -1);
5237 if (dataptr == NULL)
5238 return 0;
5239
3aee8918 5240 init_register_cache (&regcache, tdesc, dataptr);
219f2f23
PA
5241 return regcache_read_pc (&regcache);
5242}
5243
5244/* Read a requested block of memory from a trace frame. */
5245
5246int
5247traceframe_read_mem (int tfnum, CORE_ADDR addr,
5248 unsigned char *buf, ULONGEST length,
5249 ULONGEST *nbytes)
5250{
5251 struct traceframe *tframe;
5252 unsigned char *database, *dataptr;
5253 unsigned int datasize;
5254 CORE_ADDR maddr;
5255 unsigned short mlen;
5256
5257 trace_debug ("traceframe_read_mem");
5258
5259 tframe = find_traceframe (tfnum);
5260
5261 if (!tframe)
5262 {
5263 trace_debug ("traceframe %d not found", tfnum);
5264 return 1;
5265 }
5266
5267 datasize = tframe->data_size;
5268 database = dataptr = &tframe->data[0];
5269
5270 /* Iterate through a traceframe's blocks, looking for memory. */
5271 while ((dataptr = traceframe_find_block_type (dataptr,
493e2a69
MS
5272 datasize
5273 - (dataptr - database),
219f2f23
PA
5274 tfnum, 'M')) != NULL)
5275 {
5276 memcpy (&maddr, dataptr, sizeof (maddr));
5277 dataptr += sizeof (maddr);
5278 memcpy (&mlen, dataptr, sizeof (mlen));
5279 dataptr += sizeof (mlen);
5280 trace_debug ("traceframe %d has %d bytes at %s",
5281 tfnum, mlen, paddress (maddr));
5282
764880b7
PA
5283 /* If the block includes the first part of the desired range,
5284 return as much it has; GDB will re-request the remainder,
5285 which might be in a different block of this trace frame. */
5286 if (maddr <= addr && addr < (maddr + mlen))
219f2f23 5287 {
764880b7
PA
5288 ULONGEST amt = (maddr + mlen) - addr;
5289 if (amt > length)
5290 amt = length;
5291
5292 memcpy (buf, dataptr + (addr - maddr), amt);
5293 *nbytes = amt;
219f2f23
PA
5294 return 0;
5295 }
5296
5297 /* Skip over this block. */
5298 dataptr += mlen;
5299 }
5300
5301 trace_debug ("traceframe %d has no memory data for the desired region",
5302 tfnum);
5303
5304 *nbytes = 0;
5305 return 0;
5306}
5307
5308static int
5309traceframe_read_tsv (int tsvnum, LONGEST *val)
5310{
5311 int tfnum;
5312 struct traceframe *tframe;
5313 unsigned char *database, *dataptr;
5314 unsigned int datasize;
5315 int vnum;
8ddb1965 5316 int found = 0;
219f2f23
PA
5317
5318 trace_debug ("traceframe_read_tsv");
5319
5320 tfnum = current_traceframe;
5321
5322 if (tfnum < 0)
5323 {
5324 trace_debug ("no current traceframe");
5325 return 1;
5326 }
5327
5328 tframe = find_traceframe (tfnum);
5329
5330 if (tframe == NULL)
5331 {
5332 trace_debug ("traceframe %d not found", tfnum);
5333 return 1;
5334 }
5335
5336 datasize = tframe->data_size;
5337 database = dataptr = &tframe->data[0];
5338
8ddb1965
YQ
5339 /* Iterate through a traceframe's blocks, looking for the last
5340 matched tsv. */
219f2f23 5341 while ((dataptr = traceframe_find_block_type (dataptr,
493e2a69
MS
5342 datasize
5343 - (dataptr - database),
219f2f23
PA
5344 tfnum, 'V')) != NULL)
5345 {
5346 memcpy (&vnum, dataptr, sizeof (vnum));
5347 dataptr += sizeof (vnum);
5348
5349 trace_debug ("traceframe %d has variable %d", tfnum, vnum);
5350
5351 /* Check that this is the variable we want. */
5352 if (tsvnum == vnum)
5353 {
5354 memcpy (val, dataptr, sizeof (*val));
8ddb1965 5355 found = 1;
219f2f23
PA
5356 }
5357
5358 /* Skip over this block. */
5359 dataptr += sizeof (LONGEST);
5360 }
5361
8ddb1965
YQ
5362 if (!found)
5363 trace_debug ("traceframe %d has no data for variable %d",
5364 tfnum, tsvnum);
5365 return !found;
219f2f23
PA
5366}
5367
0fb4aa4b
PA
5368/* Read a requested block of static tracepoint data from a trace
5369 frame. */
5370
5371int
5372traceframe_read_sdata (int tfnum, ULONGEST offset,
5373 unsigned char *buf, ULONGEST length,
5374 ULONGEST *nbytes)
5375{
5376 struct traceframe *tframe;
5377 unsigned char *database, *dataptr;
5378 unsigned int datasize;
5379 unsigned short mlen;
5380
5381 trace_debug ("traceframe_read_sdata");
5382
5383 tframe = find_traceframe (tfnum);
5384
5385 if (!tframe)
5386 {
5387 trace_debug ("traceframe %d not found", tfnum);
5388 return 1;
5389 }
5390
5391 datasize = tframe->data_size;
5392 database = &tframe->data[0];
5393
5394 /* Iterate through a traceframe's blocks, looking for static
5395 tracepoint data. */
5396 dataptr = traceframe_find_block_type (database, datasize,
5397 tfnum, 'S');
5398 if (dataptr != NULL)
5399 {
5400 memcpy (&mlen, dataptr, sizeof (mlen));
5401 dataptr += sizeof (mlen);
5402 if (offset < mlen)
5403 {
5404 if (offset + length > mlen)
5405 length = mlen - offset;
5406
5407 memcpy (buf, dataptr, length);
5408 *nbytes = length;
5409 }
5410 else
5411 *nbytes = 0;
5412 return 0;
5413 }
5414
5415 trace_debug ("traceframe %d has no static trace data", tfnum);
5416
5417 *nbytes = 0;
5418 return 0;
5419}
5420
b3b9301e
PA
5421/* Callback for traceframe_walk_blocks. Builds a traceframe-info
5422 object. DATA is pointer to a struct buffer holding the
5423 traceframe-info object being built. */
5424
5425static int
5426build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
5427{
5428 struct buffer *buffer = data;
5429
5430 switch (blocktype)
5431 {
5432 case 'M':
5433 {
5434 unsigned short mlen;
5435 CORE_ADDR maddr;
5436
5437 memcpy (&maddr, dataptr, sizeof (maddr));
5438 dataptr += sizeof (maddr);
5439 memcpy (&mlen, dataptr, sizeof (mlen));
5440 dataptr += sizeof (mlen);
5441 buffer_xml_printf (buffer,
5442 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5443 paddress (maddr), phex_nz (mlen, sizeof (mlen)));
5444 break;
5445 }
5446 case 'V':
28a93511
YQ
5447 {
5448 int vnum;
5449
5450 memcpy (&vnum, dataptr, sizeof (vnum));
5451 buffer_xml_printf (buffer, "<tvar id=\"%d\"/>\n", vnum);
5452 break;
5453 }
b3b9301e
PA
5454 case 'R':
5455 case 'S':
5456 {
5457 break;
5458 }
5459 default:
5460 warning ("Unhandled trace block type (%d) '%c ' "
5461 "while building trace frame info.",
5462 blocktype, blocktype);
5463 break;
5464 }
5465
5466 return 0;
5467}
5468
5469/* Build a traceframe-info object for traceframe number TFNUM into
5470 BUFFER. */
5471
5472int
5473traceframe_read_info (int tfnum, struct buffer *buffer)
5474{
5475 struct traceframe *tframe;
5476
5477 trace_debug ("traceframe_read_info");
5478
5479 tframe = find_traceframe (tfnum);
5480
5481 if (!tframe)
5482 {
5483 trace_debug ("traceframe %d not found", tfnum);
5484 return 1;
5485 }
5486
5487 buffer_grow_str (buffer, "<traceframe-info>\n");
5488 traceframe_walk_blocks (tframe->data, tframe->data_size,
5489 tfnum, build_traceframe_info_xml, buffer);
5490 buffer_grow_str0 (buffer, "</traceframe-info>\n");
5491 return 0;
5492}
5493
fa593d66
PA
5494/* Return the first fast tracepoint whose jump pad contains PC. */
5495
5496static struct tracepoint *
5497fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
5498{
5499 struct tracepoint *tpoint;
5500
5501 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5502 if (tpoint->type == fast_tracepoint)
5503 if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
5504 return tpoint;
5505
5506 return NULL;
5507}
5508
405f8e94
SS
5509/* Return the first fast tracepoint whose trampoline contains PC. */
5510
5511static struct tracepoint *
5512fast_tracepoint_from_trampoline_address (CORE_ADDR pc)
5513{
5514 struct tracepoint *tpoint;
5515
5516 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5517 {
5518 if (tpoint->type == fast_tracepoint
5519 && tpoint->trampoline <= pc && pc < tpoint->trampoline_end)
5520 return tpoint;
5521 }
5522
5523 return NULL;
5524}
5525
fa593d66
PA
5526/* Return GDBserver's tracepoint that matches the IP Agent's
5527 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5528 address space. */
5529
5530static struct tracepoint *
5531fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
5532{
5533 struct tracepoint *tpoint;
5534
5535 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5536 if (tpoint->type == fast_tracepoint)
5537 if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
5538 return tpoint;
5539
5540 return NULL;
5541}
5542
5543#endif
5544
5545/* The type of the object that is used to synchronize fast tracepoint
5546 collection. */
5547
5548typedef struct collecting_t
5549{
5550 /* The fast tracepoint number currently collecting. */
5551 uintptr_t tpoint;
5552
5553 /* A number that GDBserver can use to identify the thread that is
5554 presently holding the collect lock. This need not (and usually
5555 is not) the thread id, as getting the current thread ID usually
5556 requires a system call, which we want to avoid like the plague.
5557 Usually this is thread's TCB, found in the TLS (pseudo-)
5558 register, which is readable with a single insn on several
5559 architectures. */
5560 uintptr_t thread_area;
5561} collecting_t;
5562
5563#ifndef IN_PROCESS_AGENT
5564
5565void
5566force_unlock_trace_buffer (void)
5567{
5568 write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
5569}
5570
5571/* Check if the thread identified by THREAD_AREA which is stopped at
5572 STOP_PC, is presently locking the fast tracepoint collection, and
5573 if so, gather some status of said collection. Returns 0 if the
5574 thread isn't collecting or in the jump pad at all. 1, if in the
5575 jump pad (or within gdb_collect) and hasn't executed the adjusted
5576 original insn yet (can set a breakpoint there and run to it). 2,
5577 if presently executing the adjusted original insn --- in which
5578 case, if we want to move the thread out of the jump pad, we need to
5579 single-step it until this function returns 0. */
5580
5581int
5582fast_tracepoint_collecting (CORE_ADDR thread_area,
5583 CORE_ADDR stop_pc,
5584 struct fast_tpoint_collect_status *status)
5585{
5586 CORE_ADDR ipa_collecting;
5587 CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
405f8e94
SS
5588 CORE_ADDR ipa_gdb_trampoline_buffer;
5589 CORE_ADDR ipa_gdb_trampoline_buffer_end;
fa593d66
PA
5590 struct tracepoint *tpoint;
5591 int needs_breakpoint;
5592
5593 /* The thread THREAD_AREA is either:
5594
5595 0. not collecting at all, not within the jump pad, or within
5596 gdb_collect or one of its callees.
5597
5598 1. in the jump pad and haven't reached gdb_collect
5599
5600 2. within gdb_collect (out of the jump pad) (collect is set)
5601
5602 3. we're in the jump pad, after gdb_collect having returned,
5603 possibly executing the adjusted insns.
5604
5605 For cases 1 and 3, `collecting' may or not be set. The jump pad
5606 doesn't have any complicated jump logic, so we can tell if the
5607 thread is executing the adjust original insn or not by just
5608 matching STOP_PC with known jump pad addresses. If we it isn't
5609 yet executing the original insn, set a breakpoint there, and let
5610 the thread run to it, so to quickly step over a possible (many
5611 insns) gdb_collect call. Otherwise, or when the breakpoint is
5612 hit, only a few (small number of) insns are left to be executed
5613 in the jump pad. Single-step the thread until it leaves the
5614 jump pad. */
5615
5616 again:
5617 tpoint = NULL;
5618 needs_breakpoint = 0;
5619 trace_debug ("fast_tracepoint_collecting");
5620
5621 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
5622 &ipa_gdb_jump_pad_buffer))
38e08fca
GB
5623 {
5624 internal_error (__FILE__, __LINE__,
5625 "error extracting `gdb_jump_pad_buffer'");
5626 }
fa593d66
PA
5627 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
5628 &ipa_gdb_jump_pad_buffer_end))
38e08fca
GB
5629 {
5630 internal_error (__FILE__, __LINE__,
5631 "error extracting `gdb_jump_pad_buffer_end'");
5632 }
fa593d66 5633
405f8e94
SS
5634 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
5635 &ipa_gdb_trampoline_buffer))
38e08fca
GB
5636 {
5637 internal_error (__FILE__, __LINE__,
5638 "error extracting `gdb_trampoline_buffer'");
5639 }
405f8e94
SS
5640 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
5641 &ipa_gdb_trampoline_buffer_end))
38e08fca
GB
5642 {
5643 internal_error (__FILE__, __LINE__,
5644 "error extracting `gdb_trampoline_buffer_end'");
5645 }
405f8e94 5646
493e2a69
MS
5647 if (ipa_gdb_jump_pad_buffer <= stop_pc
5648 && stop_pc < ipa_gdb_jump_pad_buffer_end)
fa593d66
PA
5649 {
5650 /* We can tell which tracepoint(s) the thread is collecting by
5651 matching the jump pad address back to the tracepoint. */
5652 tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
5653 if (tpoint == NULL)
5654 {
5655 warning ("in jump pad, but no matching tpoint?");
5656 return 0;
5657 }
5658 else
5659 {
5660 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5661 "adj_insn(%s, %s)",
5662 tpoint->number, paddress (tpoint->address),
5663 paddress (tpoint->jump_pad),
5664 paddress (tpoint->jump_pad_end),
5665 paddress (tpoint->adjusted_insn_addr),
5666 paddress (tpoint->adjusted_insn_addr_end));
5667 }
5668
5669 /* Definitely in the jump pad. May or may not need
5670 fast-exit-jump-pad breakpoint. */
5671 if (tpoint->jump_pad <= stop_pc
5672 && stop_pc < tpoint->adjusted_insn_addr)
5673 needs_breakpoint = 1;
5674 }
405f8e94
SS
5675 else if (ipa_gdb_trampoline_buffer <= stop_pc
5676 && stop_pc < ipa_gdb_trampoline_buffer_end)
5677 {
5678 /* We can tell which tracepoint(s) the thread is collecting by
5679 matching the trampoline address back to the tracepoint. */
5680 tpoint = fast_tracepoint_from_trampoline_address (stop_pc);
5681 if (tpoint == NULL)
5682 {
5683 warning ("in trampoline, but no matching tpoint?");
5684 return 0;
5685 }
5686 else
5687 {
5688 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5689 tpoint->number, paddress (tpoint->address),
5690 paddress (tpoint->trampoline),
5691 paddress (tpoint->trampoline_end));
5692 }
5693
5694 /* Have not reached jump pad yet, but treat the trampoline as a
5695 part of the jump pad that is before the adjusted original
5696 instruction. */
5697 needs_breakpoint = 1;
5698 }
fa593d66
PA
5699 else
5700 {
5701 collecting_t ipa_collecting_obj;
5702
5703 /* If `collecting' is set/locked, then the THREAD_AREA thread
5704 may or not be the one holding the lock. We have to read the
5705 lock to find out. */
5706
5707 if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
5708 &ipa_collecting))
5709 {
5710 trace_debug ("fast_tracepoint_collecting:"
5711 " failed reading 'collecting' in the inferior");
5712 return 0;
5713 }
5714
5715 if (!ipa_collecting)
5716 {
5717 trace_debug ("fast_tracepoint_collecting: not collecting"
5718 " (and nobody is).");
5719 return 0;
5720 }
5721
5722 /* Some thread is collecting. Check which. */
5723 if (read_inferior_memory (ipa_collecting,
5724 (unsigned char *) &ipa_collecting_obj,
5725 sizeof (ipa_collecting_obj)) != 0)
5726 goto again;
5727
5728 if (ipa_collecting_obj.thread_area != thread_area)
5729 {
5730 trace_debug ("fast_tracepoint_collecting: not collecting "
5731 "(another thread is)");
5732 return 0;
5733 }
5734
5735 tpoint
5736 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
5737 if (tpoint == NULL)
5738 {
5739 warning ("fast_tracepoint_collecting: collecting, "
5740 "but tpoint %s not found?",
5741 paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
5742 return 0;
5743 }
5744
5745 /* The thread is within `gdb_collect', skip over the rest of
5746 fast tracepoint collection quickly using a breakpoint. */
5747 needs_breakpoint = 1;
5748 }
5749
5750 /* The caller wants a bit of status detail. */
5751 if (status != NULL)
5752 {
5753 status->tpoint_num = tpoint->number;
5754 status->tpoint_addr = tpoint->address;
5755 status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
5756 status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
5757 }
5758
5759 if (needs_breakpoint)
5760 {
5761 /* Hasn't executed the original instruction yet. Set breakpoint
5762 there, and wait till it's hit, then single-step until exiting
5763 the jump pad. */
5764
5765 trace_debug ("\
5766fast_tracepoint_collecting, returning continue-until-break at %s",
5767 paddress (tpoint->adjusted_insn_addr));
5768
5769 return 1; /* continue */
5770 }
5771 else
5772 {
5773 /* Just single-step until exiting the jump pad. */
5774
5775 trace_debug ("fast_tracepoint_collecting, returning "
5776 "need-single-step (%s-%s)",
5777 paddress (tpoint->adjusted_insn_addr),
5778 paddress (tpoint->adjusted_insn_addr_end));
5779
5780 return 2; /* single-step */
5781 }
5782}
5783
5784#endif
5785
5786#ifdef IN_PROCESS_AGENT
5787
5788/* The global fast tracepoint collect lock. Points to a collecting_t
5789 object built on the stack by the jump pad, if presently locked;
5790 NULL if it isn't locked. Note that this lock *must* be set while
5791 executing any *function other than the jump pad. See
5792 fast_tracepoint_collecting. */
5793static collecting_t * ATTR_USED collecting;
5794
5795/* This routine, called from the jump pad (in asm) is designed to be
5796 called from the jump pads of fast tracepoints, thus it is on the
5797 critical path. */
5798
5799IP_AGENT_EXPORT void ATTR_USED
5800gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
5801{
5802 struct fast_tracepoint_ctx ctx;
5803
5804 /* Don't do anything until the trace run is completely set up. */
5805 if (!tracing)
5806 return;
5807
5808 ctx.base.type = fast_tracepoint;
5809 ctx.regs = regs;
5810 ctx.regcache_initted = 0;
fa593d66
PA
5811 /* Wrap the regblock in a register cache (in the stack, we don't
5812 want to malloc here). */
3aee8918 5813 ctx.regspace = alloca (ipa_tdesc->registers_size);
fa593d66
PA
5814 if (ctx.regspace == NULL)
5815 {
5816 trace_debug ("Trace buffer block allocation failed, skipping");
5817 return;
5818 }
5819
a59306a3
YQ
5820 for (ctx.tpoint = tpoint;
5821 ctx.tpoint != NULL && ctx.tpoint->address == tpoint->address;
5822 ctx.tpoint = ctx.tpoint->next)
fa593d66 5823 {
a59306a3
YQ
5824 if (!ctx.tpoint->enabled)
5825 continue;
fa593d66 5826
a59306a3
YQ
5827 /* Multiple tracepoints of different types, such as fast tracepoint and
5828 static tracepoint, can be set at the same address. */
5829 if (ctx.tpoint->type != tpoint->type)
5830 continue;
5831
5832 /* Test the condition if present, and collect if true. */
5833 if (ctx.tpoint->cond == NULL
5834 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5835 ctx.tpoint))
5836 {
5837 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5838 ctx.tpoint->address, ctx.tpoint);
5839
5840 /* Note that this will cause original insns to be written back
5841 to where we jumped from, but that's OK because we're jumping
5842 back to the next whole instruction. This will go badly if
5843 instruction restoration is not atomic though. */
5844 if (stopping_tracepoint
5845 || trace_buffer_is_full
5846 || expr_eval_result != expr_eval_no_error)
5847 {
5848 stop_tracing ();
5849 break;
5850 }
5851 }
5852 else
5853 {
5854 /* If there was a condition and it evaluated to false, the only
5855 way we would stop tracing is if there was an error during
5856 condition expression evaluation. */
5857 if (expr_eval_result != expr_eval_no_error)
5858 {
5859 stop_tracing ();
5860 break;
5861 }
5862 }
fa593d66
PA
5863 }
5864}
5865
5866#endif
5867
5868#ifndef IN_PROCESS_AGENT
5869
6a271cae
PA
5870CORE_ADDR
5871get_raw_reg_func_addr (void)
5872{
5873 return ipa_sym_addrs.addr_get_raw_reg;
5874}
5875
5e1dc496
LM
5876CORE_ADDR
5877get_get_tsv_func_addr (void)
6b9801d4 5878{
5e1dc496 5879 return ipa_sym_addrs.addr_get_trace_state_variable_value;
6b9801d4
SS
5880}
5881
5e1dc496
LM
5882CORE_ADDR
5883get_set_tsv_func_addr (void)
6b9801d4 5884{
5e1dc496 5885 return ipa_sym_addrs.addr_set_trace_state_variable_value;
6b9801d4
SS
5886}
5887
6a271cae 5888static void
493e2a69
MS
5889compile_tracepoint_condition (struct tracepoint *tpoint,
5890 CORE_ADDR *jump_entry)
6a271cae
PA
5891{
5892 CORE_ADDR entry_point = *jump_entry;
5893 enum eval_result_type err;
5894
5895 trace_debug ("Starting condition compilation for tracepoint %d\n",
5896 tpoint->number);
5897
5898 /* Initialize the global pointer to the code being built. */
5899 current_insn_ptr = *jump_entry;
5900
5901 emit_prologue ();
5902
5903 err = compile_bytecodes (tpoint->cond);
5904
5905 if (err == expr_eval_no_error)
5906 {
5907 emit_epilogue ();
5908
5909 /* Record the beginning of the compiled code. */
5910 tpoint->compiled_cond = entry_point;
5911
5912 trace_debug ("Condition compilation for tracepoint %d complete\n",
5913 tpoint->number);
5914 }
5915 else
5916 {
5917 /* Leave the unfinished code in situ, but don't point to it. */
5918
5919 tpoint->compiled_cond = 0;
5920
5921 trace_debug ("Condition compilation for tracepoint %d failed, "
5922 "error code %d",
5923 tpoint->number, err);
5924 }
5925
5926 /* Update the code pointer passed in. Note that we do this even if
5927 the compile fails, so that we can look at the partial results
5928 instead of letting them be overwritten. */
5929 *jump_entry = current_insn_ptr;
5930
5931 /* Leave a gap, to aid dump decipherment. */
5932 *jump_entry += 16;
5933}
5934
fa593d66
PA
5935/* We'll need to adjust these when we consider bi-arch setups, and big
5936 endian machines. */
5937
5938static int
5939write_inferior_data_ptr (CORE_ADDR where, CORE_ADDR ptr)
5940{
5941 return write_inferior_memory (where,
5942 (unsigned char *) &ptr, sizeof (void *));
5943}
5944
5945/* The base pointer of the IPA's heap. This is the only memory the
5946 IPA is allowed to use. The IPA should _not_ call the inferior's
5947 `malloc' during operation. That'd be slow, and, most importantly,
5948 it may not be safe. We may be collecting a tracepoint in a signal
5949 handler, for example. */
5950static CORE_ADDR target_tp_heap;
5951
5952/* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5953 to 8 bytes. */
5954
5955static CORE_ADDR
5956target_malloc (ULONGEST size)
5957{
5958 CORE_ADDR ptr;
5959
5960 if (target_tp_heap == 0)
5961 {
5962 /* We have the pointer *address*, need what it points to. */
5963 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
5964 &target_tp_heap))
38e08fca
GB
5965 {
5966 internal_error (__FILE__, __LINE__,
5967 "couldn't get target heap head pointer");
5968 }
fa593d66
PA
5969 }
5970
5971 ptr = target_tp_heap;
5972 target_tp_heap += size;
5973
5974 /* Pad to 8-byte alignment. */
5975 target_tp_heap = ((target_tp_heap + 7) & ~0x7);
5976
5977 return ptr;
5978}
5979
5980static CORE_ADDR
5981download_agent_expr (struct agent_expr *expr)
5982{
5983 CORE_ADDR expr_addr;
5984 CORE_ADDR expr_bytes;
5985
5986 expr_addr = target_malloc (sizeof (*expr));
5987 write_inferior_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
5988
5989 expr_bytes = target_malloc (expr->length);
5990 write_inferior_data_ptr (expr_addr + offsetof (struct agent_expr, bytes),
5991 expr_bytes);
5992 write_inferior_memory (expr_bytes, expr->bytes, expr->length);
5993
5994 return expr_addr;
5995}
5996
5997/* Align V up to N bits. */
5998#define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5999
5c73ff4e
YQ
6000/* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
6001
fa593d66 6002static void
5c73ff4e 6003download_tracepoint_1 (struct tracepoint *tpoint)
fa593d66 6004{
5c73ff4e
YQ
6005 struct tracepoint target_tracepoint;
6006 CORE_ADDR tpptr = 0;
fa593d66 6007
5c73ff4e
YQ
6008 gdb_assert (tpoint->type == fast_tracepoint
6009 || tpoint->type == static_tracepoint);
fa593d66 6010
5c73ff4e 6011 if (tpoint->cond != NULL && target_emit_ops () != NULL)
fa593d66 6012 {
5c73ff4e 6013 CORE_ADDR jentry, jump_entry;
fa593d66 6014
5c73ff4e 6015 jentry = jump_entry = get_jump_space_head ();
fa593d66 6016
5c73ff4e 6017 if (tpoint->cond != NULL)
6a271cae 6018 {
5c73ff4e
YQ
6019 /* Pad to 8-byte alignment. (needed?) */
6020 /* Actually this should be left for the target to
6021 decide. */
6022 jentry = UALIGN (jentry, 8);
6023
6024 compile_tracepoint_condition (tpoint, &jentry);
6025 }
6026
6027 /* Pad to 8-byte alignment. */
6028 jentry = UALIGN (jentry, 8);
6029 claim_jump_space (jentry - jump_entry);
6030 }
6a271cae 6031
5c73ff4e 6032 target_tracepoint = *tpoint;
6a271cae 6033
5c73ff4e
YQ
6034 tpptr = target_malloc (sizeof (*tpoint));
6035 tpoint->obj_addr_on_target = tpptr;
6036
6037 /* Write the whole object. We'll fix up its pointers in a bit.
6038 Assume no next for now. This is fixed up above on the next
6039 iteration, if there's any. */
6040 target_tracepoint.next = NULL;
6041 /* Need to clear this here too, since we're downloading the
6042 tracepoints before clearing our own copy. */
6043 target_tracepoint.hit_count = 0;
6044
6045 write_inferior_memory (tpptr, (unsigned char *) &target_tracepoint,
6046 sizeof (target_tracepoint));
6047
6048 if (tpoint->cond)
6049 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
6050 cond),
6051 download_agent_expr (tpoint->cond));
6052
6053 if (tpoint->numactions)
6054 {
6055 int i;
6056 CORE_ADDR actions_array;
6057
6058 /* The pointers array. */
6059 actions_array
6060 = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
6061 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
6062 actions),
6063 actions_array);
6064
6065 /* Now for each pointer, download the action. */
6066 for (i = 0; i < tpoint->numactions; i++)
6067 {
5c73ff4e 6068 struct tracepoint_action *action = tpoint->actions[i];
8d0d92cd 6069 CORE_ADDR ipa_action = action->ops->download (action);
6a271cae 6070
5c73ff4e
YQ
6071 if (ipa_action != 0)
6072 write_inferior_data_ptr
fd0a4d76 6073 (actions_array + i * sizeof (*tpoint->actions),
5c73ff4e 6074 ipa_action);
6a271cae 6075 }
5c73ff4e
YQ
6076 }
6077}
6a271cae 6078
42476b70
YQ
6079#define IPA_PROTO_FAST_TRACE_FLAG 0
6080#define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
6081#define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
6082#define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
6083#define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
6084
6085/* Send a command to agent to download and install tracepoint TPOINT. */
6086
6087static int
6088tracepoint_send_agent (struct tracepoint *tpoint)
6089{
6090 char buf[IPA_CMD_BUF_SIZE];
6091 char *p;
6092 int i, ret;
6093
6094 p = buf;
6095 strcpy (p, "FastTrace:");
6096 p += 10;
6097
6098 COPY_FIELD_TO_BUF (p, tpoint, number);
6099 COPY_FIELD_TO_BUF (p, tpoint, address);
6100 COPY_FIELD_TO_BUF (p, tpoint, type);
6101 COPY_FIELD_TO_BUF (p, tpoint, enabled);
6102 COPY_FIELD_TO_BUF (p, tpoint, step_count);
6103 COPY_FIELD_TO_BUF (p, tpoint, pass_count);
6104 COPY_FIELD_TO_BUF (p, tpoint, numactions);
6105 COPY_FIELD_TO_BUF (p, tpoint, hit_count);
6106 COPY_FIELD_TO_BUF (p, tpoint, traceframe_usage);
6107 COPY_FIELD_TO_BUF (p, tpoint, compiled_cond);
6108 COPY_FIELD_TO_BUF (p, tpoint, orig_size);
6109
6110 /* condition */
6111 p = agent_expr_send (p, tpoint->cond);
6112
6113 /* tracepoint_action */
6114 for (i = 0; i < tpoint->numactions; i++)
6115 {
6116 struct tracepoint_action *action = tpoint->actions[i];
6117
6118 p[0] = action->type;
6119 p = action->ops->send (&p[1], action);
6120 }
6121
6122 get_jump_space_head ();
6123 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6124 agent can use jump pad from it. */
6125 if (tpoint->type == fast_tracepoint)
6126 {
6127 memcpy (p, &gdb_jump_pad_head, 8);
6128 p += 8;
6129 }
6130
6131 ret = run_inferior_command (buf, (int) (ptrdiff_t) (p - buf));
6132 if (ret)
6133 return ret;
6134
6135 if (strncmp (buf, "OK", 2) != 0)
6136 return 1;
6137
6138 /* The value of tracepoint's target address is stored in BUF. */
6139 memcpy (&tpoint->obj_addr_on_target,
6140 &buf[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET], 8);
6141
6142 if (tpoint->type == fast_tracepoint)
6143 {
6144 unsigned char *insn
6145 = (unsigned char *) &buf[IPA_PROTO_FAST_TRACE_FJUMP_INSN];
6146 int fjump_size;
6147
6148 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6149 (unsigned int) tpoint->obj_addr_on_target,
6150 (unsigned int) gdb_jump_pad_head);
6151
6152 memcpy (&gdb_jump_pad_head, &buf[IPA_PROTO_FAST_TRACE_JUMP_PAD], 8);
6153
6154 /* This has been done in agent. We should also set up record for it. */
6155 memcpy (&fjump_size, &buf[IPA_PROTO_FAST_TRACE_FJUMP_SIZE], 4);
6156 /* Wire it in. */
6157 tpoint->handle
6158 = set_fast_tracepoint_jump (tpoint->address, insn, fjump_size);
6159 }
6160
6161 return 0;
6162}
6163
1e4d1764
YQ
6164static void
6165download_tracepoint (struct tracepoint *tpoint)
6166{
6167 struct tracepoint *tp, *tp_prev;
6168
6169 if (tpoint->type != fast_tracepoint
6170 && tpoint->type != static_tracepoint)
6171 return;
6172
6173 download_tracepoint_1 (tpoint);
6174
6175 /* Find the previous entry of TPOINT, which is fast tracepoint or
6176 static tracepoint. */
6177 tp_prev = NULL;
6178 for (tp = tracepoints; tp != tpoint; tp = tp->next)
6179 {
6180 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
6181 tp_prev = tp;
6182 }
6183
6184 if (tp_prev)
6185 {
6186 CORE_ADDR tp_prev_target_next_addr;
6187
6188 /* Insert TPOINT after TP_PREV in IPA. */
6189 if (read_inferior_data_pointer (tp_prev->obj_addr_on_target
6190 + offsetof (struct tracepoint, next),
6191 &tp_prev_target_next_addr))
38e08fca
GB
6192 {
6193 internal_error (__FILE__, __LINE__,
6194 "error reading `tp_prev->next'");
6195 }
1e4d1764
YQ
6196
6197 /* tpoint->next = tp_prev->next */
6198 write_inferior_data_ptr (tpoint->obj_addr_on_target
6199 + offsetof (struct tracepoint, next),
6200 tp_prev_target_next_addr);
6201 /* tp_prev->next = tpoint */
6202 write_inferior_data_ptr (tp_prev->obj_addr_on_target
6203 + offsetof (struct tracepoint, next),
6204 tpoint->obj_addr_on_target);
6205 }
6206 else
6207 /* First object in list, set the head pointer in the
6208 inferior. */
6209 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints,
6210 tpoint->obj_addr_on_target);
6211
6212}
6213
fa593d66
PA
6214static void
6215download_trace_state_variables (void)
6216{
6217 CORE_ADDR ptr = 0, prev_ptr = 0;
6218 struct trace_state_variable *tsv;
6219
6220 /* Start out empty. */
6221 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables, 0);
6222
6223 for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
6224 {
6225 struct trace_state_variable target_tsv;
6226
6227 /* TSV's with a getter have been initialized equally in both the
6228 inferior and GDBserver. Skip them. */
6229 if (tsv->getter != NULL)
6230 continue;
6231
6232 target_tsv = *tsv;
6233
6234 prev_ptr = ptr;
6235 ptr = target_malloc (sizeof (*tsv));
6236
6237 if (tsv == trace_state_variables)
6238 {
6239 /* First object in list, set the head pointer in the
6240 inferior. */
6241
6242 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables,
6243 ptr);
6244 }
6245 else
6246 {
6247 write_inferior_data_ptr (prev_ptr
6248 + offsetof (struct trace_state_variable,
6249 next),
6250 ptr);
6251 }
6252
6253 /* Write the whole object. We'll fix up its pointers in a bit.
6254 Assume no next, fixup when needed. */
6255 target_tsv.next = NULL;
6256
6257 write_inferior_memory (ptr, (unsigned char *) &target_tsv,
6258 sizeof (target_tsv));
6259
6260 if (tsv->name != NULL)
6261 {
6262 size_t size = strlen (tsv->name) + 1;
6263 CORE_ADDR name_addr = target_malloc (size);
6264 write_inferior_memory (name_addr,
6265 (unsigned char *) tsv->name, size);
6266 write_inferior_data_ptr (ptr
6267 + offsetof (struct trace_state_variable,
6268 name),
6269 name_addr);
6270 }
6271
38e08fca 6272 gdb_assert (tsv->getter == NULL);
fa593d66
PA
6273 }
6274
6275 if (prev_ptr != 0)
6276 {
6277 /* Fixup the next pointer in the last item in the list. */
493e2a69
MS
6278 write_inferior_data_ptr (prev_ptr
6279 + offsetof (struct trace_state_variable,
6280 next), 0);
fa593d66
PA
6281 }
6282}
6283
6284/* Upload complete trace frames out of the IP Agent's trace buffer
6285 into GDBserver's trace buffer. This always uploads either all or
6286 no trace frames. This is the counter part of
6287 `trace_alloc_trace_buffer'. See its description of the atomic
6288 synching mechanism. */
6289
6290static void
6291upload_fast_traceframes (void)
6292{
6293 unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
6294 unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
6295 CORE_ADDR tf;
6296 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
6297 unsigned int curr_tbctrl_idx;
6298 unsigned int ipa_trace_buffer_ctrl_curr;
6299 unsigned int ipa_trace_buffer_ctrl_curr_old;
6300 CORE_ADDR ipa_trace_buffer_ctrl_addr;
6301 struct breakpoint *about_to_request_buffer_space_bkpt;
6302 CORE_ADDR ipa_trace_buffer_lo;
6303 CORE_ADDR ipa_trace_buffer_hi;
6304
6305 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
6306 &ipa_traceframe_read_count_racy))
6307 {
6308 /* This will happen in most targets if the current thread is
6309 running. */
6310 return;
6311 }
6312
6313 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
6314 &ipa_traceframe_write_count_racy))
6315 return;
6316
6317 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
493e2a69
MS
6318 ipa_traceframe_write_count_racy
6319 - ipa_traceframe_read_count_racy,
6320 ipa_traceframe_write_count_racy,
6321 ipa_traceframe_read_count_racy);
fa593d66
PA
6322
6323 if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
6324 return;
6325
6326 about_to_request_buffer_space_bkpt
6327 = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
6328 NULL);
6329
6330 if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
6331 &ipa_trace_buffer_ctrl_curr))
6332 return;
6333
6334 ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
6335
6336 curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
6337
6338 {
6339 unsigned int prev, counter;
6340
6341 /* Update the token, with new counters, and the GDBserver stamp
6342 bit. Alway reuse the current TBC index. */
2ece8244
YQ
6343 prev = ipa_trace_buffer_ctrl_curr & GDBSERVER_FLUSH_COUNT_MASK_CURR;
6344 counter = (prev + 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR;
fa593d66 6345
2ece8244 6346 ipa_trace_buffer_ctrl_curr = (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
fa593d66
PA
6347 | (prev << 12)
6348 | counter
6349 | curr_tbctrl_idx);
6350 }
6351
6352 if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
6353 ipa_trace_buffer_ctrl_curr))
6354 return;
6355
6356 trace_debug ("Lib: Committed %08x -> %08x",
6357 ipa_trace_buffer_ctrl_curr_old,
6358 ipa_trace_buffer_ctrl_curr);
6359
6360 /* Re-read these, now that we've installed the
6361 `about_to_request_buffer_space' breakpoint/lock. A thread could
6362 have finished a traceframe between the last read of these
6363 counters and setting the breakpoint above. If we start
6364 uploading, we never want to leave this function with
6365 traceframe_read_count != 0, otherwise, GDBserver could end up
6366 incrementing the counter tokens more than once (due to event loop
6367 nesting), which would break the IP agent's "effective" detection
6368 (see trace_alloc_trace_buffer). */
6369 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
6370 &ipa_traceframe_read_count))
6371 return;
6372 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
6373 &ipa_traceframe_write_count))
6374 return;
6375
6376 if (debug_threads)
6377 {
6378 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6379 ipa_traceframe_write_count - ipa_traceframe_read_count,
6380 ipa_traceframe_write_count, ipa_traceframe_read_count);
6381
6382 if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
6383 || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
6384 trace_debug ("note that ipa_traceframe_count's parts changed");
6385 }
6386
6387 /* Get the address of the current TBC object (the IP agent has an
6388 array of 3 such objects). The index is stored in the TBC
6389 token. */
6390 ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
6391 ipa_trace_buffer_ctrl_addr
6392 += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
6393
6394 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
6395 (unsigned char *) &ipa_trace_buffer_ctrl,
6396 sizeof (struct ipa_trace_buffer_control)))
6397 return;
6398
6399 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
6400 &ipa_trace_buffer_lo))
6401 return;
6402 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
6403 &ipa_trace_buffer_hi))
6404 return;
6405
6406 /* Offsets are easier to grok for debugging than raw addresses,
6407 especially for the small trace buffer sizes that are useful for
6408 testing. */
6409 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6410 "endfree=%d wrap=%d hi=%d",
6411 curr_tbctrl_idx,
6412 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6413 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6414 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
6415 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6416 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6417
6418 /* Note that the IPA's buffer is always circular. */
6419
6420#define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6421
6422#define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6423 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6424
6425#define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6426 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6427 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6428 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6429 : 0))
6430
6431 tf = IPA_FIRST_TRACEFRAME ();
6432
6433 while (ipa_traceframe_write_count - ipa_traceframe_read_count)
6434 {
6435 struct tracepoint *tpoint;
6436 struct traceframe *tframe;
6437 unsigned char *block;
6438 struct traceframe ipa_tframe;
6439
6440 if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
6441 offsetof (struct traceframe, data)))
6442 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
6443
6444 if (ipa_tframe.tpnum == 0)
38e08fca
GB
6445 {
6446 internal_error (__FILE__, __LINE__,
6447 "Uploading: No (more) fast traceframes, but"
6448 " ipa_traceframe_count == %u??\n",
6449 ipa_traceframe_write_count
6450 - ipa_traceframe_read_count);
6451 }
fa593d66
PA
6452
6453 /* Note that this will be incorrect for multi-location
6454 tracepoints... */
6455 tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
6456
6457 tframe = add_traceframe (tpoint);
6458 if (tframe == NULL)
6459 {
6460 trace_buffer_is_full = 1;
6461 trace_debug ("Uploading: trace buffer is full");
6462 }
6463 else
6464 {
6465 /* Copy the whole set of blocks in one go for now. FIXME:
6466 split this in smaller blocks. */
5ae4861a
YQ
6467 block = add_traceframe_block (tframe, tpoint,
6468 ipa_tframe.data_size);
fa593d66
PA
6469 if (block != NULL)
6470 {
493e2a69
MS
6471 if (read_inferior_memory (tf
6472 + offsetof (struct traceframe, data),
fa593d66
PA
6473 block, ipa_tframe.data_size))
6474 error ("Uploading: Couldn't read traceframe data at %s\n",
6475 paddress (tf + offsetof (struct traceframe, data)));
6476 }
6477
6478 trace_debug ("Uploading: traceframe didn't fit");
6479 finish_traceframe (tframe);
6480 }
6481
6482 tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
6483
6484 /* If we freed the traceframe that wrapped around, go back
6485 to the non-wrap case. */
6486 if (tf < ipa_trace_buffer_ctrl.start)
6487 {
6488 trace_debug ("Lib: Discarding past the wraparound");
6489 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6490 }
6491 ipa_trace_buffer_ctrl.start = tf;
6492 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
6493 ++ipa_traceframe_read_count;
6494
6495 if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
6496 && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
6497 {
6498 trace_debug ("Lib: buffer is fully empty. "
6499 "Trace buffer [%d] start=%d free=%d endfree=%d",
6500 curr_tbctrl_idx,
6501 (int) (ipa_trace_buffer_ctrl.start
6502 - ipa_trace_buffer_lo),
6503 (int) (ipa_trace_buffer_ctrl.free
6504 - ipa_trace_buffer_lo),
6505 (int) (ipa_trace_buffer_ctrl.end_free
6506 - ipa_trace_buffer_lo));
6507
6508 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
6509 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
6510 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
6511 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6512 }
6513
6514 trace_debug ("Uploaded a traceframe\n"
6515 "Lib: Trace buffer [%d] start=%d free=%d "
6516 "endfree=%d wrap=%d hi=%d",
6517 curr_tbctrl_idx,
6518 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6519 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
493e2a69
MS
6520 (int) (ipa_trace_buffer_ctrl.end_free
6521 - ipa_trace_buffer_lo),
fa593d66
PA
6522 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6523 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6524 }
6525
6526 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr,
6527 (unsigned char *) &ipa_trace_buffer_ctrl,
6528 sizeof (struct ipa_trace_buffer_control)))
6529 return;
6530
6531 write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
6532 ipa_traceframe_read_count);
6533
6534 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
6535
6536 pause_all (1);
fa593d66
PA
6537
6538 delete_breakpoint (about_to_request_buffer_space_bkpt);
6539 about_to_request_buffer_space_bkpt = NULL;
6540
6541 unpause_all (1);
6542
6543 if (trace_buffer_is_full)
6544 stop_tracing ();
6545}
6546#endif
6547
6548#ifdef IN_PROCESS_AGENT
6549
0fb4aa4b 6550IP_AGENT_EXPORT int ust_loaded;
2fa291ac 6551IP_AGENT_EXPORT char cmd_buf[IPA_CMD_BUF_SIZE];
fa593d66 6552
0fb4aa4b 6553#ifdef HAVE_UST
fa593d66 6554
0fb4aa4b
PA
6555/* Static tracepoints. */
6556
6557/* UST puts a "struct tracepoint" in the global namespace, which
6558 conflicts with our tracepoint. Arguably, being a library, it
6559 shouldn't take ownership of such a generic name. We work around it
6560 here. */
6561#define tracepoint ust_tracepoint
6562#include <ust/ust.h>
6563#undef tracepoint
6564
6565extern int serialize_to_text (char *outbuf, int bufsize,
6566 const char *fmt, va_list ap);
6567
6568#define GDB_PROBE_NAME "gdb"
6569
6570/* We dynamically search for the UST symbols instead of linking them
6571 in. This lets the user decide if the application uses static
6572 tracepoints, instead of always pulling libust.so in. This vector
6573 holds pointers to all functions we care about. */
6574
6575static struct
fa593d66 6576{
0fb4aa4b
PA
6577 int (*serialize_to_text) (char *outbuf, int bufsize,
6578 const char *fmt, va_list ap);
6579
6580 int (*ltt_probe_register) (struct ltt_available_probe *pdata);
6581 int (*ltt_probe_unregister) (struct ltt_available_probe *pdata);
6582
6583 int (*ltt_marker_connect) (const char *channel, const char *mname,
6584 const char *pname);
6585 int (*ltt_marker_disconnect) (const char *channel, const char *mname,
6586 const char *pname);
6587
6588 void (*marker_iter_start) (struct marker_iter *iter);
6589 void (*marker_iter_next) (struct marker_iter *iter);
6590 void (*marker_iter_stop) (struct marker_iter *iter);
6591 void (*marker_iter_reset) (struct marker_iter *iter);
6592} ust_ops;
6593
6594#include <dlfcn.h>
6595
6596/* Cast through typeof to catch incompatible API changes. Since UST
6597 only builds with gcc, we can freely use gcc extensions here
6598 too. */
6599#define GET_UST_SYM(SYM) \
6600 do \
6601 { \
6602 if (ust_ops.SYM == NULL) \
6603 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6604 if (ust_ops.SYM == NULL) \
6605 return 0; \
6606 } while (0)
6607
6608#define USTF(SYM) ust_ops.SYM
6609
6610/* Get pointers to all libust.so functions we care about. */
6611
6612static int
6613dlsym_ust (void)
6614{
6615 GET_UST_SYM (serialize_to_text);
6616
6617 GET_UST_SYM (ltt_probe_register);
6618 GET_UST_SYM (ltt_probe_unregister);
6619 GET_UST_SYM (ltt_marker_connect);
6620 GET_UST_SYM (ltt_marker_disconnect);
6621
6622 GET_UST_SYM (marker_iter_start);
6623 GET_UST_SYM (marker_iter_next);
6624 GET_UST_SYM (marker_iter_stop);
6625 GET_UST_SYM (marker_iter_reset);
6626
6627 ust_loaded = 1;
6628 return 1;
fa593d66
PA
6629}
6630
0fb4aa4b
PA
6631/* Given an UST marker, return the matching gdb static tracepoint.
6632 The match is done by address. */
6633
6634static struct tracepoint *
6635ust_marker_to_static_tracepoint (const struct marker *mdata)
6636{
6637 struct tracepoint *tpoint;
6638
6639 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
6640 {
d248b706 6641 if (tpoint->type != static_tracepoint)
0fb4aa4b
PA
6642 continue;
6643
6644 if (tpoint->address == (uintptr_t) mdata->location)
6645 return tpoint;
6646 }
6647
6648 return NULL;
6649}
6650
6651/* The probe function we install on lttng/ust markers. Whenever a
6652 probed ust marker is hit, this function is called. This is similar
6653 to gdb_collect, only for static tracepoints, instead of fast
6654 tracepoints. */
6655
6656static void
6657gdb_probe (const struct marker *mdata, void *probe_private,
6658 struct registers *regs, void *call_private,
6659 const char *fmt, va_list *args)
6660{
6661 struct tracepoint *tpoint;
6662 struct static_tracepoint_ctx ctx;
6663
6664 /* Don't do anything until the trace run is completely set up. */
6665 if (!tracing)
6666 {
6667 trace_debug ("gdb_probe: not tracing\n");
6668 return;
6669 }
6670
6671 ctx.base.type = static_tracepoint;
6672 ctx.regcache_initted = 0;
6673 ctx.regs = regs;
6674 ctx.fmt = fmt;
6675 ctx.args = args;
6676
6677 /* Wrap the regblock in a register cache (in the stack, we don't
6678 want to malloc here). */
3aee8918 6679 ctx.regspace = alloca (ipa_tdesc->registers_size);
0fb4aa4b
PA
6680 if (ctx.regspace == NULL)
6681 {
6682 trace_debug ("Trace buffer block allocation failed, skipping");
6683 return;
6684 }
6685
6686 tpoint = ust_marker_to_static_tracepoint (mdata);
6687 if (tpoint == NULL)
6688 {
6689 trace_debug ("gdb_probe: marker not known: "
6690 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6691 mdata->location, mdata->channel,
6692 mdata->name, mdata->format);
6693 return;
6694 }
6695
d248b706
KY
6696 if (!tpoint->enabled)
6697 {
6698 trace_debug ("gdb_probe: tracepoint disabled");
6699 return;
6700 }
6701
0fb4aa4b
PA
6702 ctx.tpoint = tpoint;
6703
6704 trace_debug ("gdb_probe: collecting marker: "
6705 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6706 mdata->location, mdata->channel,
6707 mdata->name, mdata->format);
6708
6709 /* Test the condition if present, and collect if true. */
6710 if (tpoint->cond == NULL
6711 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6712 tpoint))
6713 {
6714 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6715 tpoint->address, tpoint);
6716
6717 if (stopping_tracepoint
6718 || trace_buffer_is_full
6719 || expr_eval_result != expr_eval_no_error)
6720 stop_tracing ();
6721 }
6722 else
6723 {
6724 /* If there was a condition and it evaluated to false, the only
6725 way we would stop tracing is if there was an error during
6726 condition expression evaluation. */
6727 if (expr_eval_result != expr_eval_no_error)
6728 stop_tracing ();
6729 }
6730}
6731
6732/* Called if the gdb static tracepoint requested collecting "$_sdata",
6733 static tracepoint string data. This is a string passed to the
6734 tracing library by the user, at the time of the tracepoint marker
6735 call. E.g., in the UST marker call:
6736
6737 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6738
6739 the collected data is "str FOOBAZ".
6740*/
6741
6742static void
6743collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
0fb4aa4b
PA
6744 struct traceframe *tframe)
6745{
6746 struct static_tracepoint_ctx *umd = (struct static_tracepoint_ctx *) ctx;
6747 unsigned char *bufspace;
6748 int size;
6749 va_list copy;
6750 unsigned short blocklen;
6751
6752 if (umd == NULL)
6753 {
6754 trace_debug ("Wanted to collect static trace data, "
6755 "but there's no static trace data");
6756 return;
6757 }
6758
6759 va_copy (copy, *umd->args);
6760 size = USTF(serialize_to_text) (NULL, 0, umd->fmt, copy);
6761 va_end (copy);
6762
6763 trace_debug ("Want to collect ust data");
6764
6765 /* 'S' + size + string */
5ae4861a 6766 bufspace = add_traceframe_block (tframe, umd->tpoint,
0fb4aa4b
PA
6767 1 + sizeof (blocklen) + size + 1);
6768 if (bufspace == NULL)
6769 {
6770 trace_debug ("Trace buffer block allocation failed, skipping");
6771 return;
6772 }
6773
6774 /* Identify a static trace data block. */
6775 *bufspace = 'S';
6776
6777 blocklen = size + 1;
6778 memcpy (bufspace + 1, &blocklen, sizeof (blocklen));
6779
6780 va_copy (copy, *umd->args);
6781 USTF(serialize_to_text) ((char *) bufspace + 1 + sizeof (blocklen),
6782 size + 1, umd->fmt, copy);
6783 va_end (copy);
6784
6785 trace_debug ("Storing static tracepoint data in regblock: %s",
6786 bufspace + 1 + sizeof (blocklen));
6787}
6788
6789/* The probe to register with lttng/ust. */
6790static struct ltt_available_probe gdb_ust_probe =
6791 {
6792 GDB_PROBE_NAME,
6793 NULL,
6794 gdb_probe,
6795 };
6796
6797#endif /* HAVE_UST */
6798#endif /* IN_PROCESS_AGENT */
6799
0fb4aa4b
PA
6800#ifndef IN_PROCESS_AGENT
6801
0fb4aa4b
PA
6802/* Ask the in-process agent to run a command. Since we don't want to
6803 have to handle the IPA hitting breakpoints while running the
6804 command, we pause all threads, remove all breakpoints, and then set
6805 the helper thread re-running. We communicate with the helper
6806 thread by means of direct memory xfering, and a socket for
6807 synchronization. */
6808
6809static int
42476b70 6810run_inferior_command (char *cmd, int len)
0fb4aa4b
PA
6811{
6812 int err = -1;
fbd5db48 6813 int pid = ptid_get_pid (current_ptid);
0fb4aa4b
PA
6814
6815 trace_debug ("run_inferior_command: running: %s", cmd);
6816
6817 pause_all (0);
6818 uninsert_all_breakpoints ();
6819
42476b70 6820 err = agent_run_command (pid, (const char *) cmd, len);
0fb4aa4b
PA
6821
6822 reinsert_all_breakpoints ();
6823 unpause_all (0);
6824
6825 return err;
6826}
6827
2fa291ac 6828#else /* !IN_PROCESS_AGENT */
0fb4aa4b 6829
2fa291ac
YQ
6830#include <sys/socket.h>
6831#include <sys/un.h>
0fb4aa4b 6832
2fa291ac
YQ
6833#ifndef UNIX_PATH_MAX
6834#define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6835#endif
0fb4aa4b 6836
2fa291ac
YQ
6837/* Where we put the socked used for synchronization. */
6838#define SOCK_DIR P_tmpdir
0fb4aa4b
PA
6839
6840/* Thread ID of the helper thread. GDBserver reads this to know which
6841 is the help thread. This is an LWP id on Linux. */
6842int helper_thread_id;
6843
0fb4aa4b
PA
6844static int
6845init_named_socket (const char *name)
6846{
6847 int result, fd;
6848 struct sockaddr_un addr;
6849
6850 result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
6851 if (result == -1)
6852 {
6853 warning ("socket creation failed: %s", strerror (errno));
6854 return -1;
6855 }
6856
6857 addr.sun_family = AF_UNIX;
6858
6859 strncpy (addr.sun_path, name, UNIX_PATH_MAX);
6860 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
6861
6862 result = access (name, F_OK);
6863 if (result == 0)
6864 {
6865 /* File exists. */
6866 result = unlink (name);
6867 if (result == -1)
6868 {
6869 warning ("unlink failed: %s", strerror (errno));
6870 close (fd);
6871 return -1;
6872 }
6873 warning ("socket %s already exists; overwriting", name);
6874 }
6875
6876 result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
6877 if (result == -1)
6878 {
6879 warning ("bind failed: %s", strerror (errno));
6880 close (fd);
6881 return -1;
6882 }
6883
6884 result = listen (fd, 1);
6885 if (result == -1)
6886 {
6887 warning ("listen: %s", strerror (errno));
6888 close (fd);
6889 return -1;
6890 }
6891
6892 return fd;
6893}
6894
7255706c
YQ
6895static char agent_socket_name[UNIX_PATH_MAX];
6896
0fb4aa4b 6897static int
2fa291ac 6898gdb_agent_socket_init (void)
0fb4aa4b
PA
6899{
6900 int result, fd;
0fb4aa4b 6901
7255706c 6902 result = xsnprintf (agent_socket_name, UNIX_PATH_MAX, "%s/gdb_ust%d",
6cebaf6e 6903 SOCK_DIR, getpid ());
0fb4aa4b
PA
6904 if (result >= UNIX_PATH_MAX)
6905 {
6906 trace_debug ("string overflow allocating socket name");
6907 return -1;
6908 }
6909
7255706c 6910 fd = init_named_socket (agent_socket_name);
0fb4aa4b
PA
6911 if (fd < 0)
6912 warning ("Error initializing named socket (%s) for communication with the "
6913 "ust helper thread. Check that directory exists and that it "
7255706c 6914 "is writable.", agent_socket_name);
0fb4aa4b
PA
6915
6916 return fd;
6917}
6918
2fa291ac 6919#ifdef HAVE_UST
0fb4aa4b
PA
6920
6921/* The next marker to be returned on a qTsSTM command. */
6922static const struct marker *next_st;
6923
6924/* Returns the first known marker. */
6925
6926struct marker *
6927first_marker (void)
6928{
6929 struct marker_iter iter;
6930
6931 USTF(marker_iter_reset) (&iter);
6932 USTF(marker_iter_start) (&iter);
6933
6934 return iter.marker;
6935}
6936
6937/* Returns the marker following M. */
6938
6939const struct marker *
6940next_marker (const struct marker *m)
6941{
6942 struct marker_iter iter;
6943
6944 USTF(marker_iter_reset) (&iter);
6945 USTF(marker_iter_start) (&iter);
6946
6947 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
6948 {
6949 if (iter.marker == m)
6950 {
6951 USTF(marker_iter_next) (&iter);
6952 return iter.marker;
6953 }
6954 }
6955
6956 return NULL;
6957}
6958
2fa291ac
YQ
6959/* Return an hexstr version of the STR C string, fit for sending to
6960 GDB. */
6961
6962static char *
6963cstr_to_hexstr (const char *str)
6964{
6965 int len = strlen (str);
6966 char *hexstr = xmalloc (len * 2 + 1);
e9371aff 6967 bin2hex ((gdb_byte *) str, hexstr, len);
2fa291ac
YQ
6968 return hexstr;
6969}
6970
0fb4aa4b
PA
6971/* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6972 packets. */
6973
6974static void
6975response_ust_marker (char *packet, const struct marker *st)
6976{
6977 char *strid, *format, *tmp;
6978
6979 next_st = next_marker (st);
6980
6981 tmp = xmalloc (strlen (st->channel) + 1 +
6982 strlen (st->name) + 1);
6983 sprintf (tmp, "%s/%s", st->channel, st->name);
6984
6985 strid = cstr_to_hexstr (tmp);
6986 free (tmp);
6987
6988 format = cstr_to_hexstr (st->format);
6989
6990 sprintf (packet, "m%s:%s:%s",
6991 paddress ((uintptr_t) st->location),
6992 strid,
6993 format);
6994
6995 free (strid);
6996 free (format);
6997}
6998
6999/* Return the first static tracepoint, and initialize the state
7000 machine that will iterate through all the static tracepoints. */
7001
7002static void
7003cmd_qtfstm (char *packet)
7004{
7005 trace_debug ("Returning first trace state variable definition");
7006
7007 if (first_marker ())
7008 response_ust_marker (packet, first_marker ());
7009 else
7010 strcpy (packet, "l");
7011}
7012
7013/* Return additional trace state variable definitions. */
7014
7015static void
7016cmd_qtsstm (char *packet)
7017{
7018 trace_debug ("Returning static tracepoint");
7019
7020 if (next_st)
7021 response_ust_marker (packet, next_st);
7022 else
7023 strcpy (packet, "l");
7024}
7025
7026/* Disconnect the GDB probe from a marker at a given address. */
7027
7028static void
7029unprobe_marker_at (char *packet)
7030{
7031 char *p = packet;
7032 ULONGEST address;
7033 struct marker_iter iter;
7034
7035 p += sizeof ("unprobe_marker_at:") - 1;
7036
7037 p = unpack_varlen_hex (p, &address);
7038
7039 USTF(marker_iter_reset) (&iter);
7040 USTF(marker_iter_start) (&iter);
7041 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
7042 if ((uintptr_t ) iter.marker->location == address)
7043 {
7044 int result;
7045
7046 result = USTF(ltt_marker_disconnect) (iter.marker->channel,
7047 iter.marker->name,
7048 GDB_PROBE_NAME);
7049 if (result < 0)
7050 warning ("could not disable marker %s/%s",
7051 iter.marker->channel, iter.marker->name);
7052 break;
7053 }
7054}
7055
7056/* Connect the GDB probe to a marker at a given address. */
7057
7058static int
7059probe_marker_at (char *packet)
7060{
7061 char *p = packet;
7062 ULONGEST address;
7063 struct marker_iter iter;
7064 struct marker *m;
7065
7066 p += sizeof ("probe_marker_at:") - 1;
7067
7068 p = unpack_varlen_hex (p, &address);
7069
7070 USTF(marker_iter_reset) (&iter);
7071
7072 for (USTF(marker_iter_start) (&iter), m = iter.marker;
7073 m != NULL;
7074 USTF(marker_iter_next) (&iter), m = iter.marker)
7075 if ((uintptr_t ) m->location == address)
7076 {
7077 int result;
7078
7079 trace_debug ("found marker for address. "
7080 "ltt_marker_connect (marker = %s/%s)",
7081 m->channel, m->name);
7082
493e2a69
MS
7083 result = USTF(ltt_marker_connect) (m->channel, m->name,
7084 GDB_PROBE_NAME);
0fb4aa4b
PA
7085 if (result && result != -EEXIST)
7086 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7087 m->channel, m->name, -result);
7088
7089 if (result < 0)
7090 {
7091 sprintf (packet, "E.could not connect marker: channel=%s, name=%s",
7092 m->channel, m->name);
7093 return -1;
7094 }
7095
7096 strcpy (packet, "OK");
7097 return 0;
7098 }
7099
7100 sprintf (packet, "E.no marker found at 0x%s", paddress (address));
7101 return -1;
7102}
7103
7104static int
7105cmd_qtstmat (char *packet)
7106{
7107 char *p = packet;
7108 ULONGEST address;
7109 struct marker_iter iter;
7110 struct marker *m;
7111
7112 p += sizeof ("qTSTMat:") - 1;
7113
7114 p = unpack_varlen_hex (p, &address);
7115
7116 USTF(marker_iter_reset) (&iter);
7117
7118 for (USTF(marker_iter_start) (&iter), m = iter.marker;
7119 m != NULL;
7120 USTF(marker_iter_next) (&iter), m = iter.marker)
7121 if ((uintptr_t ) m->location == address)
7122 {
7123 response_ust_marker (packet, m);
7124 return 0;
7125 }
7126
7127 strcpy (packet, "l");
7128 return -1;
7129}
7130
2fa291ac
YQ
7131static void
7132gdb_ust_init (void)
7133{
7134 if (!dlsym_ust ())
7135 return;
7136
7137 USTF(ltt_probe_register) (&gdb_ust_probe);
7138}
7139
7140#endif /* HAVE_UST */
7141
82067193 7142#include <sys/syscall.h>
7255706c
YQ
7143
7144static void
7145gdb_agent_remove_socket (void)
7146{
7147 unlink (agent_socket_name);
7148}
82067193 7149
2fa291ac
YQ
7150/* Helper thread of agent. */
7151
0fb4aa4b 7152static void *
2fa291ac 7153gdb_agent_helper_thread (void *arg)
0fb4aa4b
PA
7154{
7155 int listen_fd;
7156
7255706c
YQ
7157 atexit (gdb_agent_remove_socket);
7158
0fb4aa4b
PA
7159 while (1)
7160 {
2fa291ac 7161 listen_fd = gdb_agent_socket_init ();
0fb4aa4b 7162
0fb4aa4b
PA
7163 if (helper_thread_id == 0)
7164 helper_thread_id = syscall (SYS_gettid);
0fb4aa4b
PA
7165
7166 if (listen_fd == -1)
7167 {
7168 warning ("could not create sync socket\n");
7169 break;
7170 }
7171
7172 while (1)
7173 {
7174 socklen_t tmp;
7175 struct sockaddr_un sockaddr;
7176 int fd;
7177 char buf[1];
7178 int ret;
7255706c 7179 int stop_loop = 0;
0fb4aa4b
PA
7180
7181 tmp = sizeof (sockaddr);
7182
7183 do
7184 {
7185 fd = accept (listen_fd, &sockaddr, &tmp);
7186 }
7187 /* It seems an ERESTARTSYS can escape out of accept. */
7188 while (fd == -512 || (fd == -1 && errno == EINTR));
7189
7190 if (fd < 0)
7191 {
7192 warning ("Accept returned %d, error: %s\n",
7193 fd, strerror (errno));
7194 break;
7195 }
7196
7197 do
7198 {
7199 ret = read (fd, buf, 1);
7200 } while (ret == -1 && errno == EINTR);
7201
7202 if (ret == -1)
7203 {
7204 warning ("reading socket (fd=%d) failed with %s",
7205 fd, strerror (errno));
7206 close (fd);
7207 break;
7208 }
7209
7210 if (cmd_buf[0])
7211 {
7255706c
YQ
7212 if (strncmp ("close", cmd_buf, 5) == 0)
7213 {
7214 stop_loop = 1;
7215 }
2fa291ac 7216#ifdef HAVE_UST
7255706c 7217 else if (strcmp ("qTfSTM", cmd_buf) == 0)
0fb4aa4b
PA
7218 {
7219 cmd_qtfstm (cmd_buf);
7220 }
7221 else if (strcmp ("qTsSTM", cmd_buf) == 0)
7222 {
7223 cmd_qtsstm (cmd_buf);
7224 }
7225 else if (strncmp ("unprobe_marker_at:",
7226 cmd_buf,
7227 sizeof ("unprobe_marker_at:") - 1) == 0)
7228 {
7229 unprobe_marker_at (cmd_buf);
7230 }
7231 else if (strncmp ("probe_marker_at:",
7232 cmd_buf,
7233 sizeof ("probe_marker_at:") - 1) == 0)
7234 {
7235 probe_marker_at (cmd_buf);
7236 }
7237 else if (strncmp ("qTSTMat:",
7238 cmd_buf,
7239 sizeof ("qTSTMat:") - 1) == 0)
7240 {
7241 cmd_qtstmat (cmd_buf);
7242 }
2fa291ac 7243#endif /* HAVE_UST */
0fb4aa4b
PA
7244 }
7245
712c6575
YQ
7246 /* Fix compiler's warning: ignoring return value of 'write'. */
7247 ret = write (fd, buf, 1);
0fb4aa4b 7248 close (fd);
7255706c
YQ
7249
7250 if (stop_loop)
7251 {
7252 close (listen_fd);
7253 unlink (agent_socket_name);
7254
7255 /* Sleep endlessly to wait the whole inferior stops. This
7256 thread can not exit because GDB or GDBserver may still need
0bfdf32f 7257 'current_thread' (representing this thread) to access
7255706c 7258 inferior memory. Otherwise, this thread exits earlier than
0bfdf32f 7259 other threads, and 'current_thread' is set to NULL. */
7255706c
YQ
7260 while (1)
7261 sleep (10);
7262 }
0fb4aa4b
PA
7263 }
7264 }
7265
7266 return NULL;
7267}
7268
7269#include <signal.h>
2fa291ac 7270#include <pthread.h>
0fb4aa4b 7271
8ffcbaaf
YQ
7272IP_AGENT_EXPORT int gdb_agent_capability = AGENT_CAPA_STATIC_TRACE;
7273
0fb4aa4b 7274static void
2fa291ac 7275gdb_agent_init (void)
0fb4aa4b
PA
7276{
7277 int res;
7278 pthread_t thread;
7279 sigset_t new_mask;
7280 sigset_t orig_mask;
7281
0fb4aa4b
PA
7282 /* We want the helper thread to be as transparent as possible, so
7283 have it inherit an all-signals-blocked mask. */
7284
7285 sigfillset (&new_mask);
7286 res = pthread_sigmask (SIG_SETMASK, &new_mask, &orig_mask);
7287 if (res)
14ce3192 7288 perror_with_name ("pthread_sigmask (1)");
0fb4aa4b
PA
7289
7290 res = pthread_create (&thread,
7291 NULL,
2fa291ac 7292 gdb_agent_helper_thread,
0fb4aa4b
PA
7293 NULL);
7294
7295 res = pthread_sigmask (SIG_SETMASK, &orig_mask, NULL);
7296 if (res)
14ce3192 7297 perror_with_name ("pthread_sigmask (2)");
0fb4aa4b
PA
7298
7299 while (helper_thread_id == 0)
7300 usleep (1);
7301
2fa291ac
YQ
7302#ifdef HAVE_UST
7303 gdb_ust_init ();
7304#endif
0fb4aa4b
PA
7305}
7306
0fb4aa4b
PA
7307#include <sys/mman.h>
7308#include <fcntl.h>
7309
7310IP_AGENT_EXPORT char *gdb_tp_heap_buffer;
7311IP_AGENT_EXPORT char *gdb_jump_pad_buffer;
7312IP_AGENT_EXPORT char *gdb_jump_pad_buffer_end;
405f8e94
SS
7313IP_AGENT_EXPORT char *gdb_trampoline_buffer;
7314IP_AGENT_EXPORT char *gdb_trampoline_buffer_end;
7315IP_AGENT_EXPORT char *gdb_trampoline_buffer_error;
7316
7317/* Record the result of getting buffer space for fast tracepoint
7318 trampolines. Any error message is copied, since caller may not be
7319 using persistent storage. */
7320
7321void
7322set_trampoline_buffer_space (CORE_ADDR begin, CORE_ADDR end, char *errmsg)
7323{
7324 gdb_trampoline_buffer = (char *) (uintptr_t) begin;
7325 gdb_trampoline_buffer_end = (char *) (uintptr_t) end;
7326 if (errmsg)
7327 strncpy (gdb_trampoline_buffer_error, errmsg, 99);
7328 else
7329 strcpy (gdb_trampoline_buffer_error, "no buffer passed");
7330}
0fb4aa4b
PA
7331
7332static void __attribute__ ((constructor))
7333initialize_tracepoint_ftlib (void)
7334{
7335 initialize_tracepoint ();
7336
2fa291ac 7337 gdb_agent_init ();
0fb4aa4b
PA
7338}
7339
7340#endif /* IN_PROCESS_AGENT */
fa593d66 7341
f196051f
SS
7342/* Return a timestamp, expressed as microseconds of the usual Unix
7343 time. (As the result is a 64-bit number, it will not overflow any
7344 time soon.) */
7345
219f2f23 7346static LONGEST
f196051f 7347get_timestamp (void)
219f2f23
PA
7348{
7349 struct timeval tv;
7350
7351 if (gettimeofday (&tv, 0) != 0)
7352 return -1;
7353 else
7354 return (LONGEST) tv.tv_sec * 1000000 + tv.tv_usec;
7355}
7356
7357void
7358initialize_tracepoint (void)
7359{
f6f899bf
HAQ
7360 /* Start with the default size. */
7361 init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE);
219f2f23
PA
7362
7363 /* Wire trace state variable 1 to be the timestamp. This will be
7364 uploaded to GDB upon connection and become one of its trace state
7365 variables. (In case you're wondering, if GDB already has a trace
7366 variable numbered 1, it will be renumbered.) */
fa593d66 7367 create_trace_state_variable (1, 0);
219f2f23 7368 set_trace_state_variable_name (1, "trace_timestamp");
f196051f 7369 set_trace_state_variable_getter (1, get_timestamp);
fa593d66
PA
7370
7371#ifdef IN_PROCESS_AGENT
7372 {
fc1ab1a0 7373 uintptr_t addr;
fa593d66 7374 int pagesize;
fc1ab1a0 7375
fa593d66
PA
7376 pagesize = sysconf (_SC_PAGE_SIZE);
7377 if (pagesize == -1)
14ce3192 7378 perror_with_name ("sysconf");
fa593d66
PA
7379
7380 gdb_tp_heap_buffer = xmalloc (5 * 1024 * 1024);
7381
fc1ab1a0
PA
7382#define SCRATCH_BUFFER_NPAGES 20
7383
7384 /* Allocate scratch buffer aligned on a page boundary, at a low
7385 address (close to the main executable's code). */
7386 for (addr = pagesize; addr != 0; addr += pagesize)
7387 {
7388 gdb_jump_pad_buffer = mmap ((void *) addr, pagesize * SCRATCH_BUFFER_NPAGES,
7389 PROT_READ | PROT_WRITE | PROT_EXEC,
7390 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
7391 -1, 0);
7392 if (gdb_jump_pad_buffer != MAP_FAILED)
7393 break;
7394 }
fa593d66 7395
fc1ab1a0 7396 if (addr == 0)
14ce3192 7397 perror_with_name ("mmap");
fc1ab1a0
PA
7398
7399 gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + pagesize * SCRATCH_BUFFER_NPAGES;
fa593d66
PA
7400 }
7401
405f8e94
SS
7402 gdb_trampoline_buffer = gdb_trampoline_buffer_end = 0;
7403
7404 /* It's not a fatal error for something to go wrong with trampoline
7405 buffer setup, but it can be mysterious, so create a channel to
7406 report back on what went wrong, using a fixed size since we may
7407 not be able to allocate space later when the problem occurs. */
7408 gdb_trampoline_buffer_error = xmalloc (IPA_BUFSIZ);
7409
7410 strcpy (gdb_trampoline_buffer_error, "No errors reported");
7411
fa593d66
PA
7412 initialize_low_tracepoint ();
7413#endif
219f2f23 7414}