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