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