OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [gdb-7.2/] [gdb-7.2-or32-1.0rc1/] [gdb/] [gdbserver/] [tracepoint.c] - Blame information for rev 341

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.