gdb/tracefile.c - gdb

Functions defined

Macros defined

Source code

  1. /* Trace file support in GDB.

  2.    Copyright (C) 1997-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "tracefile.h"
  16. #include "ctf.h"
  17. #include "exec.h"
  18. #include "regcache.h"

  19. /* Helper macros.  */

  20. #define TRACE_WRITE_R_BLOCK(writer, buf, size)        \
  21.   writer->ops->frame_ops->write_r_block ((writer), (buf), (size))
  22. #define TRACE_WRITE_M_BLOCK_HEADER(writer, addr, size)                  \
  23.   writer->ops->frame_ops->write_m_block_header ((writer), (addr), \
  24.                                                 (size))
  25. #define TRACE_WRITE_M_BLOCK_MEMORY(writer, buf, size)          \
  26.   writer->ops->frame_ops->write_m_block_memory ((writer), (buf), \
  27.                                                 (size))
  28. #define TRACE_WRITE_V_BLOCK(writer, num, val)        \
  29.   writer->ops->frame_ops->write_v_block ((writer), (num), (val))

  30. /* Free trace file writer.  */

  31. static void
  32. trace_file_writer_xfree (void *arg)
  33. {
  34.   struct trace_file_writer *writer = arg;

  35.   writer->ops->dtor (writer);
  36.   xfree (writer);
  37. }

  38. /* Save tracepoint data to file named FILENAME through WRITER.  WRITER
  39.    determines the trace file format.  If TARGET_DOES_SAVE is non-zero,
  40.    the save is performed on the target, otherwise GDB obtains all trace
  41.    data and saves it locally.  */

  42. static void
  43. trace_save (const char *filename, struct trace_file_writer *writer,
  44.             int target_does_save)
  45. {
  46.   struct trace_status *ts = current_trace_status ();
  47.   int status;
  48.   struct uploaded_tp *uploaded_tps = NULL, *utp;
  49.   struct uploaded_tsv *uploaded_tsvs = NULL, *utsv;

  50.   ULONGEST offset = 0;
  51. #define MAX_TRACE_UPLOAD 2000
  52.   gdb_byte buf[MAX_TRACE_UPLOAD];
  53.   int written;
  54.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());

  55.   /* If the target is to save the data to a file on its own, then just
  56.      send the command and be done with it.  */
  57.   if (target_does_save)
  58.     {
  59.       if (!writer->ops->target_save (writer, filename))
  60.         error (_("Target failed to save trace data to '%s'."),
  61.                filename);
  62.       return;
  63.     }

  64.   /* Get the trace status first before opening the file, so if the
  65.      target is losing, we can get out without touching files.  */
  66.   status = target_get_trace_status (ts);

  67.   writer->ops->start (writer, filename);

  68.   writer->ops->write_header (writer);

  69.   /* Write descriptive info.  */

  70.   /* Write out the size of a register block.  */
  71.   writer->ops->write_regblock_type (writer, trace_regblock_size);

  72.   /* Write out status of the tracing run (aka "tstatus" info).  */
  73.   writer->ops->write_status (writer, ts);

  74.   /* Note that we want to upload tracepoints and save those, rather
  75.      than simply writing out the local ones, because the user may have
  76.      changed tracepoints in GDB in preparation for a future tracing
  77.      run, or maybe just mass-deleted all types of breakpoints as part
  78.      of cleaning up.  So as not to contaminate the session, leave the
  79.      data in its uploaded form, don't make into real tracepoints.  */

  80.   /* Get trace state variables first, they may be checked when parsing
  81.      uploaded commands.  */

  82.   target_upload_trace_state_variables (&uploaded_tsvs);

  83.   for (utsv = uploaded_tsvs; utsv; utsv = utsv->next)
  84.     writer->ops->write_uploaded_tsv (writer, utsv);

  85.   free_uploaded_tsvs (&uploaded_tsvs);

  86.   target_upload_tracepoints (&uploaded_tps);

  87.   for (utp = uploaded_tps; utp; utp = utp->next)
  88.     target_get_tracepoint_status (NULL, utp);

  89.   for (utp = uploaded_tps; utp; utp = utp->next)
  90.     writer->ops->write_uploaded_tp (writer, utp);

  91.   free_uploaded_tps (&uploaded_tps);

  92.   /* Mark the end of the definition section.  */
  93.   writer->ops->write_definition_end (writer);

  94.   /* Get and write the trace data proper.  */
  95.   while (1)
  96.     {
  97.       LONGEST gotten = 0;

  98.       /* The writer supports writing the contents of trace buffer
  99.           directly to trace file.  Don't parse the contents of trace
  100.           buffer.  */
  101.       if (writer->ops->write_trace_buffer != NULL)
  102.         {
  103.           /* We ask for big blocks, in the hopes of efficiency, but
  104.              will take less if the target has packet size limitations
  105.              or some such.  */
  106.           gotten = target_get_raw_trace_data (buf, offset,
  107.                                               MAX_TRACE_UPLOAD);
  108.           if (gotten < 0)
  109.             error (_("Failure to get requested trace buffer data"));
  110.           /* No more data is forthcoming, we're done.  */
  111.           if (gotten == 0)
  112.             break;

  113.           writer->ops->write_trace_buffer (writer, buf, gotten);

  114.           offset += gotten;
  115.         }
  116.       else
  117.         {
  118.           uint16_t tp_num;
  119.           uint32_t tf_size;
  120.           /* Parse the trace buffers according to how data are stored
  121.              in trace buffer in GDBserver.  */

  122.           gotten = target_get_raw_trace_data (buf, offset, 6);

  123.           if (gotten == 0)
  124.             break;

  125.           /* Read the first six bytes in, which is the tracepoint
  126.              number and trace frame size.  */
  127.           tp_num = (uint16_t)
  128.             extract_unsigned_integer (&buf[0], 2, byte_order);

  129.           tf_size = (uint32_t)
  130.             extract_unsigned_integer (&buf[2], 4, byte_order);

  131.           writer->ops->frame_ops->start (writer, tp_num);
  132.           gotten = 6;

  133.           if (tf_size > 0)
  134.             {
  135.               unsigned int block;

  136.               offset += 6;

  137.               for (block = 0; block < tf_size; )
  138.                 {
  139.                   gdb_byte block_type;

  140.                   /* We'll fetch one block each time, in order to
  141.                      handle the extremely large 'M' block.  We first
  142.                      fetch one byte to get the type of the block.  */
  143.                   gotten = target_get_raw_trace_data (buf, offset, 1);
  144.                   if (gotten < 1)
  145.                     error (_("Failure to get requested trace buffer data"));

  146.                   gotten = 1;
  147.                   block += 1;
  148.                   offset += 1;

  149.                   block_type = buf[0];
  150.                   switch (block_type)
  151.                     {
  152.                     case 'R':
  153.                       gotten
  154.                         = target_get_raw_trace_data (buf, offset,
  155.                                                      trace_regblock_size);
  156.                       if (gotten < trace_regblock_size)
  157.                         error (_("Failure to get requested trace"
  158.                                  " buffer data"));

  159.                       TRACE_WRITE_R_BLOCK (writer, buf,
  160.                                            trace_regblock_size);
  161.                       break;
  162.                     case 'M':
  163.                       {
  164.                         unsigned short mlen;
  165.                         ULONGEST addr;
  166.                         LONGEST t;
  167.                         int j;

  168.                         t = target_get_raw_trace_data (buf,offset, 10);
  169.                         if (t < 10)
  170.                           error (_("Failure to get requested trace"
  171.                                    " buffer data"));

  172.                         offset += 10;
  173.                         block += 10;

  174.                         gotten = 0;
  175.                         addr = (ULONGEST)
  176.                           extract_unsigned_integer (buf, 8,
  177.                                                     byte_order);
  178.                         mlen = (unsigned short)
  179.                           extract_unsigned_integer (&buf[8], 2,
  180.                                                     byte_order);

  181.                         TRACE_WRITE_M_BLOCK_HEADER (writer, addr,
  182.                                                     mlen);

  183.                         /* The memory contents in 'M' block may be
  184.                            very large.  Fetch the data from the target
  185.                            and write them into file one by one.  */
  186.                         for (j = 0; j < mlen; )
  187.                           {
  188.                             unsigned int read_length;

  189.                             if (mlen - j > MAX_TRACE_UPLOAD)
  190.                               read_length = MAX_TRACE_UPLOAD;
  191.                             else
  192.                               read_length = mlen - j;

  193.                             t = target_get_raw_trace_data (buf,
  194.                                                            offset + j,
  195.                                                            read_length);
  196.                             if (t < read_length)
  197.                               error (_("Failure to get requested"
  198.                                        " trace buffer data"));

  199.                             TRACE_WRITE_M_BLOCK_MEMORY (writer, buf,
  200.                                                         read_length);

  201.                             j += read_length;
  202.                             gotten += read_length;
  203.                           }

  204.                         break;
  205.                       }
  206.                     case 'V':
  207.                       {
  208.                         int vnum;
  209.                         LONGEST val;

  210.                         gotten
  211.                           = target_get_raw_trace_data (buf, offset,
  212.                                                        12);
  213.                         if (gotten < 12)
  214.                           error (_("Failure to get requested"
  215.                                    " trace buffer data"));

  216.                         vnum  = (int) extract_signed_integer (buf,
  217.                                                               4,
  218.                                                               byte_order);
  219.                         val
  220.                           = extract_signed_integer (&buf[4], 8,
  221.                                                     byte_order);

  222.                         TRACE_WRITE_V_BLOCK (writer, vnum, val);
  223.                       }
  224.                       break;
  225.                     default:
  226.                       error (_("Unknown block type '%c' (0x%x) in"
  227.                                " trace frame"),
  228.                              block_type, block_type);
  229.                     }

  230.                   block += gotten;
  231.                   offset += gotten;
  232.                 }
  233.             }
  234.           else
  235.             offset += gotten;

  236.           writer->ops->frame_ops->end (writer);
  237.         }
  238.     }

  239.   writer->ops->end (writer);
  240. }

  241. static void
  242. trace_save_command (char *args, int from_tty)
  243. {
  244.   int target_does_save = 0;
  245.   char **argv;
  246.   char *filename = NULL;
  247.   struct cleanup *back_to;
  248.   int generate_ctf = 0;
  249.   struct trace_file_writer *writer = NULL;

  250.   if (args == NULL)
  251.     error_no_arg (_("file in which to save trace data"));

  252.   argv = gdb_buildargv (args);
  253.   back_to = make_cleanup_freeargv (argv);

  254.   for (; *argv; ++argv)
  255.     {
  256.       if (strcmp (*argv, "-r") == 0)
  257.         target_does_save = 1;
  258.       if (strcmp (*argv, "-ctf") == 0)
  259.         generate_ctf = 1;
  260.       else if (**argv == '-')
  261.         error (_("unknown option `%s'"), *argv);
  262.       else
  263.         filename = *argv;
  264.     }

  265.   if (!filename)
  266.     error_no_arg (_("file in which to save trace data"));

  267.   if (generate_ctf)
  268.     writer = ctf_trace_file_writer_new ();
  269.   else
  270.     writer = tfile_trace_file_writer_new ();

  271.   make_cleanup (trace_file_writer_xfree, writer);

  272.   trace_save (filename, writer, target_does_save);

  273.   if (from_tty)
  274.     printf_filtered (_("Trace data saved to %s '%s'.\n"),
  275.                      generate_ctf ? "directory" : "file", filename);

  276.   do_cleanups (back_to);
  277. }

  278. /* Save the trace data to file FILENAME of tfile format.  */

  279. void
  280. trace_save_tfile (const char *filename, int target_does_save)
  281. {
  282.   struct trace_file_writer *writer;
  283.   struct cleanup *back_to;

  284.   writer = tfile_trace_file_writer_new ();
  285.   back_to = make_cleanup (trace_file_writer_xfree, writer);
  286.   trace_save (filename, writer, target_does_save);
  287.   do_cleanups (back_to);
  288. }

  289. /* Save the trace data to dir DIRNAME of ctf format.  */

  290. void
  291. trace_save_ctf (const char *dirname, int target_does_save)
  292. {
  293.   struct trace_file_writer *writer;
  294.   struct cleanup *back_to;

  295.   writer = ctf_trace_file_writer_new ();
  296.   back_to = make_cleanup (trace_file_writer_xfree, writer);

  297.   trace_save (dirname, writer, target_does_save);
  298.   do_cleanups (back_to);
  299. }

  300. /* Fetch register data from tracefile, shared for both tfile and
  301.    ctf.  */

  302. void
  303. tracefile_fetch_registers (struct regcache *regcache, int regno)
  304. {
  305.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  306.   int regn, pc_regno;

  307.   /* We get here if no register data has been found.  Mark registers
  308.      as unavailable.  */
  309.   for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
  310.     regcache_raw_supply (regcache, regn, NULL);

  311.   /* We can often usefully guess that the PC is going to be the same
  312.      as the address of the tracepoint.  */
  313.   pc_regno = gdbarch_pc_regnum (gdbarch);

  314.   /* XXX This guessing code below only works if the PC register isn't
  315.      a pseudo-register.  The value of a pseudo-register isn't stored
  316.      in the (non-readonly) regcache -- instead it's recomputed
  317.      (probably from some other cached raw register) whenever the
  318.      register is read.  This guesswork should probably move to some
  319.      higher layer.  */
  320.   if (pc_regno < 0 || pc_regno >= gdbarch_num_regs (gdbarch))
  321.     return;

  322.   if (regno == -1 || regno == pc_regno)
  323.     {
  324.       struct tracepoint *tp = get_tracepoint (get_tracepoint_number ());
  325.       gdb_byte *regs;

  326.       if (tp && tp->base.loc)
  327.         {
  328.           /* But don't try to guess if tracepoint is multi-location...  */
  329.           if (tp->base.loc->next)
  330.             {
  331.               warning (_("Tracepoint %d has multiple "
  332.                          "locations, cannot infer $pc"),
  333.                        tp->base.number);
  334.               return;
  335.             }
  336.           /* ... or does while-stepping.  */
  337.           if (tp->step_count > 0)
  338.             {
  339.               warning (_("Tracepoint %d does while-stepping, "
  340.                          "cannot infer $pc"),
  341.                        tp->base.number);
  342.               return;
  343.             }

  344.           regs = alloca (register_size (gdbarch, pc_regno));
  345.           store_unsigned_integer (regs, register_size (gdbarch, pc_regno),
  346.                                   gdbarch_byte_order (gdbarch),
  347.                                   tp->base.loc->address);
  348.           regcache_raw_supply (regcache, pc_regno, regs);
  349.         }
  350.     }
  351. }

  352. /* This is the implementation of target_ops method to_has_all_memory.  */

  353. static int
  354. tracefile_has_all_memory (struct target_ops *ops)
  355. {
  356.   return 1;
  357. }

  358. /* This is the implementation of target_ops method to_has_memory.  */

  359. static int
  360. tracefile_has_memory (struct target_ops *ops)
  361. {
  362.   return 1;
  363. }

  364. /* This is the implementation of target_ops method to_has_stack.
  365.    The target has a stack when GDB has already selected one trace
  366.    frame.  */

  367. static int
  368. tracefile_has_stack (struct target_ops *ops)
  369. {
  370.   return get_traceframe_number () != -1;
  371. }

  372. /* This is the implementation of target_ops method to_has_registers.
  373.    The target has registers when GDB has already selected one trace
  374.    frame.  */

  375. static int
  376. tracefile_has_registers (struct target_ops *ops)
  377. {
  378.   return get_traceframe_number () != -1;
  379. }

  380. /* This is the implementation of target_ops method to_thread_alive.
  381.    tracefile has one thread faked by GDB.  */

  382. static int
  383. tracefile_thread_alive (struct target_ops *ops, ptid_t ptid)
  384. {
  385.   return 1;
  386. }

  387. /* This is the implementation of target_ops method to_get_trace_status.
  388.    The trace status for a file is that tracing can never be run.  */

  389. static int
  390. tracefile_get_trace_status (struct target_ops *self, struct trace_status *ts)
  391. {
  392.   /* Other bits of trace status were collected as part of opening the
  393.      trace files, so nothing to do here.  */

  394.   return -1;
  395. }

  396. /* Initialize OPS for tracefile related targets.  */

  397. void
  398. init_tracefile_ops (struct target_ops *ops)
  399. {
  400.   ops->to_stratum = process_stratum;
  401.   ops->to_get_trace_status = tracefile_get_trace_status;
  402.   ops->to_has_all_memory = tracefile_has_all_memory;
  403.   ops->to_has_memory = tracefile_has_memory;
  404.   ops->to_has_stack = tracefile_has_stack;
  405.   ops->to_has_registers = tracefile_has_registers;
  406.   ops->to_thread_alive = tracefile_thread_alive;
  407.   ops->to_magic = OPS_MAGIC;
  408. }

  409. extern initialize_file_ftype _initialize_tracefile;

  410. void
  411. _initialize_tracefile (void)
  412. {
  413.   add_com ("tsave", class_trace, trace_save_command, _("\
  414. Save the trace data to a file.\n\
  415. Use the '-ctf' option to save the data to CTF format.\n\
  416. Use the '-r' option to direct the target to save directly to the file,\n\
  417. using its own filesystem."));
  418. }