This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Tracepoint compile error fixes


Thanks to Hui and Jan for catching these! Tested with a manually-corrupted trace file, and committed to trunk.

Stan

2010-01-16 Stan Shebs <stan@codesourcery.com>

       * tracepoint.h (struct trace_status): Use unsigned long long
       instead of size_t.
       * tracepoint.c (trace_status_command): Fix printf directive.
       (trace_save_command): Check fwrite returns, fix printf directive.
       (trace_filename): New global.
       (tfile_open): Set it, check read returns.
       (tfile_close): Free trace_filename.
       (tfile_get_traceframe_address): Check read returns.
       (tfile_trace_find): Ditto.
       (tfile_fetch_registers): Ditto.
       (tfile_xfer_partial): Ditto.
       (tfile_get_trace_state_variable_value): Ditto.

Index: tracepoint.c
===================================================================
RCS file: /cvs/src/src/gdb/tracepoint.c,v
retrieving revision 1.139
diff -p -r1.139 tracepoint.c
*** tracepoint.c	15 Jan 2010 22:37:17 -0000	1.139
--- tracepoint.c	16 Jan 2010 23:07:41 -0000
*************** trace_status_command (char *args, int fr
*** 1647,1653 ****
  
    if (ts->buffer_free)
      {
!       printf_filtered (_("Trace buffer has %d bytes free.\n"),
  		       ts->buffer_free);
      }
  
--- 1647,1653 ----
  
    if (ts->buffer_free)
      {
!       printf_filtered (_("Trace buffer has %llu bytes free.\n"),
  		       ts->buffer_free);
      }
  
*************** trace_save_command (char *args, int from
*** 2342,2347 ****
--- 2342,2348 ----
    ULONGEST offset = 0;
  #define MAX_TRACE_UPLOAD 2000
    gdb_byte buf[MAX_TRACE_UPLOAD];
+   int written;
  
    if (args == NULL)
      error_no_arg (_("file in which to save trace data"));
*************** trace_save_command (char *args, int from
*** 2389,2395 ****
    /* Write a file header, with a high-bit-set char to indicate a
       binary file, plus a hint as what this file is, and a version
       number in case of future needs.  */
!   fwrite ("\x7fTRACE0\n", 8, 1, fp);
  
    /* Write descriptive info.  */
  
--- 2390,2398 ----
    /* Write a file header, with a high-bit-set char to indicate a
       binary file, plus a hint as what this file is, and a version
       number in case of future needs.  */
!   written = fwrite ("\x7fTRACE0\n", 8, 1, fp);
!   if (written < 8)
!     perror_with_name (pathname);
  
    /* Write descriptive info.  */
  
*************** trace_save_command (char *args, int from
*** 2397,2406 ****
    fprintf (fp, "R %x\n", trace_regblock_size);
  
    /* Write out status of the tracing run (aka "tstatus" info).  */
!   fprintf (fp, "status %c;%s:%x;tframes:%x;tfree:%x\n",
  	   (ts->running ? '1' : '0'),
  	   stop_reason_names[ts->stop_reason], ts->stopping_tracepoint,
! 	   ts->traceframe_count, (unsigned int) ts->buffer_free);
  
    /* Note that we want to upload tracepoints and save those, rather
       than simply writing out the local ones, because the user may have
--- 2400,2409 ----
    fprintf (fp, "R %x\n", trace_regblock_size);
  
    /* Write out status of the tracing run (aka "tstatus" info).  */
!   fprintf (fp, "status %c;%s:%x;tframes:%x;tfree:%llx\n",
  	   (ts->running ? '1' : '0'),
  	   stop_reason_names[ts->stop_reason], ts->stopping_tracepoint,
! 	   ts->traceframe_count, ts->buffer_free);
  
    /* Note that we want to upload tracepoints and save those, rather
       than simply writing out the local ones, because the user may have
*************** trace_save_command (char *args, int from
*** 2473,2484 ****
        /* No more data is forthcoming, we're done.  */
        if (gotten == 0)
  	break;
!       fwrite (buf, gotten, 1, fp);
        offset += gotten;
      }
  
    /* Mark the end of trace data.  */
!   fwrite (&gotten, 4, 1, fp);
  
    do_cleanups (cleanup);
    if (from_tty)
--- 2476,2491 ----
        /* No more data is forthcoming, we're done.  */
        if (gotten == 0)
  	break;
!       written = fwrite (buf, gotten, 1, fp);
!       if (written < gotten)
! 	perror_with_name (pathname);
        offset += gotten;
      }
  
    /* Mark the end of trace data.  */
!   written = fwrite (&gotten, 4, 1, fp);
!   if (written < 4)
!     perror_with_name (pathname);
  
    do_cleanups (cleanup);
    if (from_tty)
*************** struct target_ops tfile_ops;
*** 2766,2771 ****
--- 2773,2779 ----
  
  #define TRACE_HEADER_SIZE 8
  
+ char *trace_filename;
  int trace_fd = -1;
  off_t trace_frames_offset;
  off_t cur_offset;
*************** tfile_open (char *filename, int from_tty
*** 2786,2792 ****
    char header[TRACE_HEADER_SIZE];
    char linebuf[1000]; /* should be max remote packet size or so */
    char byte;
!   int bytes, i;
    struct trace_status *ts;
    struct uploaded_tp *uploaded_tps = NULL;
    struct uploaded_tsv *uploaded_tsvs = NULL;
--- 2794,2800 ----
    char header[TRACE_HEADER_SIZE];
    char linebuf[1000]; /* should be max remote packet size or so */
    char byte;
!   int bytes, i, gotten;
    struct trace_status *ts;
    struct uploaded_tp *uploaded_tps = NULL;
    struct uploaded_tsv *uploaded_tsvs = NULL;
*************** tfile_open (char *filename, int from_tty
*** 2819,2829 ****
    push_target (&tfile_ops);
    discard_cleanups (old_chain);
  
    trace_fd = scratch_chan;
  
    bytes = 0;
    /* Read the file header and test for validity.  */
!   read (trace_fd, &header, TRACE_HEADER_SIZE);
    bytes += TRACE_HEADER_SIZE;
    if (!(header[0] == 0x7f
  	&& (strncmp (header + 1, "TRACE0\n", 7) == 0)))
--- 2827,2843 ----
    push_target (&tfile_ops);
    discard_cleanups (old_chain);
  
+   trace_filename = xstrdup (filename);
    trace_fd = scratch_chan;
  
    bytes = 0;
    /* Read the file header and test for validity.  */
!   gotten = read (trace_fd, &header, TRACE_HEADER_SIZE);
!   if (gotten < 0)
!     perror_with_name (trace_filename);
!   else if (gotten < TRACE_HEADER_SIZE)
!     error (_("Premature end of file while reading trace file"));
! 
    bytes += TRACE_HEADER_SIZE;
    if (!(header[0] == 0x7f
  	&& (strncmp (header + 1, "TRACE0\n", 7) == 0)))
*************** tfile_open (char *filename, int from_tty
*** 2844,2850 ****
    i = 0;
    while (1)
      {
!       read (trace_fd, &byte, 1);
        ++bytes;
        if (byte == '\n')
  	{
--- 2858,2869 ----
    i = 0;
    while (1)
      {
!       gotten = read (trace_fd, &byte, 1);
!       if (gotten < 0)
! 	perror_with_name (trace_filename);
!       else if (gotten < 1)
! 	error (_("Premature end of file while reading trace file"));
! 
        ++bytes;
        if (byte == '\n')
  	{
*************** tfile_close (int quitting)
*** 3112,3117 ****
--- 3131,3138 ----
  
    close (trace_fd);
    trace_fd = -1;
+   if (trace_filename)
+     xfree (trace_filename);
  }
  
  static void
*************** tfile_get_traceframe_address (off_t tfra
*** 3144,3155 ****
    short tpnum;
    struct breakpoint *tp;
    off_t saved_offset = cur_offset;
  
    /* FIXME dig pc out of collected registers */
  
    /* Fall back to using tracepoint address.  */
    lseek (trace_fd, tframe_offset, SEEK_SET);
!   read (trace_fd, &tpnum, 2);
    tp = get_tracepoint_by_number_on_target (tpnum);
    if (tp && tp->loc)
      addr = tp->loc->address;
--- 3165,3182 ----
    short tpnum;
    struct breakpoint *tp;
    off_t saved_offset = cur_offset;
+   int gotten;
  
    /* FIXME dig pc out of collected registers */
  
    /* Fall back to using tracepoint address.  */
    lseek (trace_fd, tframe_offset, SEEK_SET);
!   gotten = read (trace_fd, &tpnum, 2);
!   if (gotten < 0)
!     perror_with_name (trace_filename);
!   else if (gotten < 2)
!     error (_("Premature end of file while reading trace file"));
! 
    tp = get_tracepoint_by_number_on_target (tpnum);
    if (tp && tp->loc)
      addr = tp->loc->address;
*************** tfile_trace_find (enum trace_find_type t
*** 3170,3176 ****
  		  ULONGEST addr1, ULONGEST addr2, int *tpp)
  {
    short tpnum;
!   int tfnum = 0, found = 0;
    int data_size;
    struct breakpoint *tp;
    off_t offset, tframe_offset;
--- 3197,3203 ----
  		  ULONGEST addr1, ULONGEST addr2, int *tpp)
  {
    short tpnum;
!   int tfnum = 0, found = 0, gotten;
    int data_size;
    struct breakpoint *tp;
    off_t offset, tframe_offset;
*************** tfile_trace_find (enum trace_find_type t
*** 3181,3191 ****
    while (1)
      {
        tframe_offset = offset;
!       read (trace_fd, &tpnum, 2);
        offset += 2;
        if (tpnum == 0)
  	break;
!       read (trace_fd, &data_size, 4);	
        offset += 4;
        switch (type)
  	{
--- 3208,3226 ----
    while (1)
      {
        tframe_offset = offset;
!       gotten = read (trace_fd, &tpnum, 2);
!       if (gotten < 0)
! 	perror_with_name (trace_filename);
!       else if (gotten < 2)
! 	error (_("Premature end of file while reading trace file"));
        offset += 2;
        if (tpnum == 0)
  	break;
!       gotten = read (trace_fd, &data_size, 4);	
!       if (gotten < 0)
! 	perror_with_name (trace_filename);
!       else if (gotten < 4)
! 	error (_("Premature end of file while reading trace file"));
        offset += 4;
        switch (type)
  	{
*************** tfile_fetch_registers (struct target_ops
*** 3246,3252 ****
  {
    struct gdbarch *gdbarch = get_regcache_arch (regcache);
    char block_type;
!   int i, pos, offset, regn, regsize;
    unsigned short mlen;
    char *regs;
  
--- 3281,3287 ----
  {
    struct gdbarch *gdbarch = get_regcache_arch (regcache);
    char block_type;
!   int i, pos, offset, regn, regsize, gotten;
    unsigned short mlen;
    char *regs;
  
*************** tfile_fetch_registers (struct target_ops
*** 3261,3272 ****
    pos = 0;
    while (pos < cur_data_size)
      {
!       read (trace_fd, &block_type, 1);
        ++pos;
        switch (block_type)
  	{
  	case 'R':
! 	  read (trace_fd, regs, trace_regblock_size);
  	  /* Assume the block is laid out in GDB register number order,
  	     each register with the size that it has in GDB.  */
  	  offset = 0;
--- 3296,3317 ----
    pos = 0;
    while (pos < cur_data_size)
      {
!       gotten = read (trace_fd, &block_type, 1);
!       if (gotten < 0)
! 	perror_with_name (trace_filename);
!       else if (gotten < 1)
! 	error (_("Premature end of file while reading trace file"));
! 
        ++pos;
        switch (block_type)
  	{
  	case 'R':
! 	  gotten = read (trace_fd, regs, trace_regblock_size);
! 	  if (gotten < 0)
! 	    perror_with_name (trace_filename);
! 	  else if (gotten < trace_regblock_size)
! 	    error (_("Premature end of file while reading trace file"));
! 
  	  /* Assume the block is laid out in GDB register number order,
  	     each register with the size that it has in GDB.  */
  	  offset = 0;
*************** tfile_fetch_registers (struct target_ops
*** 3293,3299 ****
  	  return;
  	case 'M':
  	  lseek (trace_fd, 8, SEEK_CUR);
! 	  read (trace_fd, &mlen, 2);
  	  lseek (trace_fd, mlen, SEEK_CUR);
  	  pos += (8 + 2 + mlen);
  	  break;
--- 3338,3348 ----
  	  return;
  	case 'M':
  	  lseek (trace_fd, 8, SEEK_CUR);
! 	  gotten = read (trace_fd, &mlen, 2);
! 	  if (gotten < 0)
! 	    perror_with_name (trace_filename);
! 	  else if (gotten < 2)
! 	    error (_("Premature end of file while reading trace file"));
  	  lseek (trace_fd, mlen, SEEK_CUR);
  	  pos += (8 + 2 + mlen);
  	  break;
*************** tfile_xfer_partial (struct target_ops *o
*** 3315,3321 ****
  		    const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
  {
    char block_type;
!   int pos;
    ULONGEST maddr;
    unsigned short mlen;
  
--- 3364,3370 ----
  		    const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
  {
    char block_type;
!   int pos, gotten;
    ULONGEST maddr;
    unsigned short mlen;
  
*************** tfile_xfer_partial (struct target_ops *o
*** 3330,3336 ****
    pos = 0;
    while (pos < cur_data_size)
      {
!       read (trace_fd, &block_type, 1);
        ++pos;
        switch (block_type)
  	{
--- 3379,3389 ----
    pos = 0;
    while (pos < cur_data_size)
      {
!       gotten = read (trace_fd, &block_type, 1);
!       if (gotten < 0)
! 	perror_with_name (trace_filename);
!       else if (gotten < 1)
! 	error (_("Premature end of file while reading trace file"));
        ++pos;
        switch (block_type)
  	{
*************** tfile_xfer_partial (struct target_ops *o
*** 3339,3349 ****
  	  pos += trace_regblock_size;
  	  break;
  	case 'M':
! 	  read (trace_fd, &maddr, 8);
! 	  read (trace_fd, &mlen, 2);
  	  if (maddr <= offset && (offset + len) <= (maddr + mlen))
  	    {
! 	      read (trace_fd, readbuf, mlen);
  	      return mlen;
  	    }
  	  lseek (trace_fd, mlen, SEEK_CUR);
--- 3392,3416 ----
  	  pos += trace_regblock_size;
  	  break;
  	case 'M':
! 	  gotten = read (trace_fd, &maddr, 8);
! 	  if (gotten < 0)
! 	    perror_with_name (trace_filename);
! 	  else if (gotten < 8)
! 	    error (_("Premature end of file while reading trace file"));
! 
! 	  gotten = read (trace_fd, &mlen, 2);
! 	  if (gotten < 0)
! 	    perror_with_name (trace_filename);
! 	  else if (gotten < 2)
! 	    error (_("Premature end of file while reading trace file"));
  	  if (maddr <= offset && (offset + len) <= (maddr + mlen))
  	    {
! 	      gotten = read (trace_fd, readbuf, mlen);
! 	      if (gotten < 0)
! 		perror_with_name (trace_filename);
! 	      else if (gotten < mlen)
! 		error (_("Premature end of file qwhile reading trace file"));
! 
  	      return mlen;
  	    }
  	  lseek (trace_fd, mlen, SEEK_CUR);
*************** static int
*** 3370,3383 ****
  tfile_get_trace_state_variable_value (int tsvnum, LONGEST *val)
  {
    char block_type;
!   int pos, vnum;
    unsigned short mlen;
  
    lseek (trace_fd, cur_offset, SEEK_SET);
    pos = 0;
    while (pos < cur_data_size)
      {
!       read (trace_fd, &block_type, 1);
        ++pos;
        switch (block_type)
  	{
--- 3437,3454 ----
  tfile_get_trace_state_variable_value (int tsvnum, LONGEST *val)
  {
    char block_type;
!   int pos, vnum, gotten;
    unsigned short mlen;
  
    lseek (trace_fd, cur_offset, SEEK_SET);
    pos = 0;
    while (pos < cur_data_size)
      {
!       gotten = read (trace_fd, &block_type, 1);
!       if (gotten < 0)
! 	perror_with_name (trace_filename);
!       else if (gotten < 1)
! 	error (_("Premature end of file while reading trace file"));
        ++pos;
        switch (block_type)
  	{
*************** tfile_get_trace_state_variable_value (in
*** 3387,3401 ****
  	  break;
  	case 'M':
  	  lseek (trace_fd, 8, SEEK_CUR);
! 	  read (trace_fd, &mlen, 2);
  	  lseek (trace_fd, mlen, SEEK_CUR);
  	  pos += (8 + 2 + mlen);
  	  break;
  	case 'V':
! 	  read (trace_fd, &vnum, 4);
  	  if (tsvnum == vnum)
  	    {
! 	      read (trace_fd, val, 8);
  	      return 1;
  	    }
  	  lseek (trace_fd, 8, SEEK_CUR);
--- 3458,3484 ----
  	  break;
  	case 'M':
  	  lseek (trace_fd, 8, SEEK_CUR);
! 	  gotten = read (trace_fd, &mlen, 2);
! 	  if (gotten < 0)
! 	    perror_with_name (trace_filename);
! 	  else if (gotten < 2)
! 	    error (_("Premature end of file while reading trace file"));
  	  lseek (trace_fd, mlen, SEEK_CUR);
  	  pos += (8 + 2 + mlen);
  	  break;
  	case 'V':
! 	  gotten = read (trace_fd, &vnum, 4);
! 	  if (gotten < 0)
! 	    perror_with_name (trace_filename);
! 	  else if (gotten < 4)
! 	    error (_("Premature end of file while reading trace file"));
  	  if (tsvnum == vnum)
  	    {
! 	      gotten = read (trace_fd, val, 8);
! 	      if (gotten < 0)
! 		perror_with_name (trace_filename);
! 	      else if (gotten < 8)
! 		error (_("Premature end of file while reading trace file"));
  	      return 1;
  	    }
  	  lseek (trace_fd, 8, SEEK_CUR);
Index: tracepoint.h
===================================================================
RCS file: /cvs/src/src/gdb/tracepoint.h,v
retrieving revision 1.20
diff -p -r1.20 tracepoint.h
*** tracepoint.h	15 Jan 2010 22:37:17 -0000	1.20
--- tracepoint.h	16 Jan 2010 23:07:41 -0000
*************** struct trace_status
*** 97,105 ****
  
    int traceframe_count;
  
!   size_t buffer_size;
  
!   size_t buffer_free;
  };
  
  struct trace_status *current_trace_status (void);
--- 97,105 ----
  
    int traceframe_count;
  
!   unsigned long long buffer_size;
  
!   unsigned long long buffer_free;
  };
  
  struct trace_status *current_trace_status (void);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]