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