20000-05-19 H.J. Lu (hjl@gnu.org)
[binutils-gdb.git] / gprof / gmon_io.c
index cec19736f0600c80f812745bb648ebf1d5e07511..7e0a1a0a81beaa3f69f433ed87cebffc23733505 100644 (file)
@@ -4,7 +4,7 @@
 #include "cg_arcs.h"
 #include "basic_blocks.h"
 #include "bfd.h"
-#include "core.h"
+#include "corefile.h"
 #include "call_graph.h"
 #include "gmon_io.h"
 #include "gmon_out.h"
@@ -23,18 +23,18 @@ int gmon_file_version = 0;  /* 0 == old (non-versioned) file format */
 bfd_vma
 DEFUN (get_vma, (abfd, addr), bfd * abfd AND bfd_byte * addr)
 {
-  switch (sizeof (bfd_vma))
+  switch (sizeof (char*))
     {
     case 4:
       return bfd_get_32 (abfd, addr);
     case 8:
       return bfd_get_64 (abfd, addr);
     default:
-      fprintf (stderr, "%s: bfd_vma has unexpected size of %ld bytes\n",
-              whoami, (long) sizeof (bfd_vma));
+      fprintf (stderr, _("%s: bfd_vma has unexpected size of %ld bytes\n"),
+              whoami, (long) sizeof (char*));
       done (1);
-    }                          /* switch */
-}                              /* get_vma */
+    }
+}
 
 
 /*
@@ -43,7 +43,7 @@ DEFUN (get_vma, (abfd, addr), bfd * abfd AND bfd_byte * addr)
 void
 DEFUN (put_vma, (abfd, val, addr), bfd * abfd AND bfd_vma val AND bfd_byte * addr)
 {
-  switch (sizeof (bfd_vma))
+  switch (sizeof (char*))
     {
     case 4:
       bfd_put_32 (abfd, val, addr);
@@ -52,11 +52,11 @@ DEFUN (put_vma, (abfd, val, addr), bfd * abfd AND bfd_vma val AND bfd_byte * add
       bfd_put_64 (abfd, val, addr);
       break;
     default:
-      fprintf (stderr, "%s: bfd_vma has unexpected size of %ld bytes\n",
-              whoami, (long) sizeof (bfd_vma));
+      fprintf (stderr, _("%s: bfd_vma has unexpected size of %ld bytes\n"),
+              whoami, (long) sizeof (char*));
       done (1);
-    }                          /* switch */
-}                              /* put_vma */
+    }
+}
 
 
 void
@@ -80,24 +80,24 @@ DEFUN (gmon_out_read, (filename), const char *filename)
        {
          perror (filename);
          done (1);
-       }                       /* if */
-    }                          /* if */
+       }
+    }
   if (fread (&ghdr, sizeof (struct gmon_hdr), 1, ifp) != 1)
     {
-      fprintf (stderr, "%s: file too short to be a gmon file\n",
+      fprintf (stderr, _("%s: file too short to be a gmon file\n"),
               filename);
       done (1);
-    }                          /* if */
+    }
 
   if ((file_format == FF_MAGIC) ||
       (file_format == FF_AUTO && !strncmp (&ghdr.cookie[0], GMON_MAGIC, 4)))
     {
       if (file_format == FF_MAGIC && strncmp (&ghdr.cookie[0], GMON_MAGIC, 4))
        {
-         fprintf (stderr, "%s: file `%s' has bad magic cookie\n",
+         fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
                   whoami, filename);
          done (1);
-       }                       /* if */
+       }
 
       /* right magic, so it's probably really a new gmon.out file */
 
@@ -105,10 +105,10 @@ DEFUN (gmon_out_read, (filename), const char *filename)
       if (gmon_file_version != GMON_VERSION && gmon_file_version != 0)
        {
          fprintf (stderr,
-                  "%s: file `%s' has unsupported version %d\n",
+                  _("%s: file `%s' has unsupported version %d\n"),
                   whoami, filename, gmon_file_version);
          done (1);
-       }                       /* if */
+       }
 
       /* read in all the records: */
       while (fread (&tag, sizeof (tag), 1, ifp) == 1)
@@ -135,13 +135,15 @@ DEFUN (gmon_out_read, (filename), const char *filename)
 
            default:
              fprintf (stderr,
-                      "%s: %s: found bad tag %d (file corrupted?)\n",
+                      _("%s: %s: found bad tag %d (file corrupted?)\n"),
                       whoami, filename, tag);
              done (1);
-           }                   /* switch */
-       }                       /* while */
+           }
+       }
     }
-  else if (file_format == FF_AUTO || file_format == FF_BSD)
+  else if (file_format == FF_AUTO
+          || file_format == FF_BSD
+          || file_format == FF_BSD44)
     {
       struct hdr
       {
@@ -149,7 +151,8 @@ DEFUN (gmon_out_read, (filename), const char *filename)
        bfd_vma high_pc;
        int ncnt;
       };
-      int i, samp_bytes, count;
+      int i, samp_bytes, header_size;
+      unsigned long count;
       bfd_vma from_pc, self_pc;
       struct raw_arc raw_arc;
       struct raw_phdr raw;
@@ -172,64 +175,114 @@ DEFUN (gmon_out_read, (filename), const char *filename)
        {
          perror (filename);
          done (1);
-       }                       /* if */
+       }
       if (fread (&raw, 1, sizeof (struct raw_phdr), ifp)
          != sizeof (struct raw_phdr))
        {
-         fprintf (stderr, "%s: file too short to be a gmon file\n",
+         fprintf (stderr, _("%s: file too short to be a gmon file\n"),
                   filename);
          done (1);
-       }                       /* if */
-      tmp.low_pc = get_vma (core_bfd, (bfd_byte *) & raw.low_pc[0]);
-      tmp.high_pc = get_vma (core_bfd, (bfd_byte *) & raw.high_pc[0]);
-      tmp.ncnt = bfd_get_32 (core_bfd, (bfd_byte *) & raw.ncnt[0]);
+       }
+      tmp.low_pc = get_vma (core_bfd, (bfd_byte *) &raw.low_pc[0]);
+      tmp.high_pc = get_vma (core_bfd, (bfd_byte *) &raw.high_pc[0]);
+      tmp.ncnt = bfd_get_32 (core_bfd, (bfd_byte *) &raw.ncnt[0]);
+
+      if (bfd_get_32 (core_bfd, (bfd_byte *) &raw.version[0])
+         == GMONVERSION)
+       {
+         int profrate;
+
+         /* 4.4BSD format header.  */
+
+         profrate = bfd_get_32 (core_bfd, (bfd_byte *) &raw.profrate[0]);
+         if (!s_highpc)
+           hz = profrate;
+         else if (hz != profrate)
+           {
+             fprintf (stderr,
+                      _("%s: profiling rate incompatible with first gmon file\n"),
+                      filename);
+             done (1);
+           }
+
+         header_size = sizeof (struct raw_phdr);
+       }
+      else
+       {
+         /* old style BSD format.  */
+         if (file_format == FF_BSD44)
+           {
+             fprintf (stderr, _("%s: file `%s' has bad magic cookie\n"),
+                      whoami, filename);
+             done (1);
+           }
+
+         if (fseek (ifp, sizeof (struct old_raw_phdr), SEEK_SET) < 0)
+           {
+             perror (filename);
+             done (1);
+           }
+
+         header_size = sizeof (struct old_raw_phdr);
+       }
+
       if (s_highpc && (tmp.low_pc != h.low_pc ||
                       tmp.high_pc != h.high_pc || tmp.ncnt != h.ncnt))
        {
-         fprintf (stderr, "%s: incompatible with first gmon file\n",
+         fprintf (stderr, _("%s: incompatible with first gmon file\n"),
                   filename);
          done (1);
-       }                       /* if */
+       }
       h = tmp;
       s_lowpc = (bfd_vma) h.low_pc;
       s_highpc = (bfd_vma) h.high_pc;
       lowpc = (bfd_vma) h.low_pc / sizeof (UNIT);
       highpc = (bfd_vma) h.high_pc / sizeof (UNIT);
-      samp_bytes = h.ncnt - sizeof (struct raw_phdr);
+      samp_bytes = h.ncnt - header_size;
       hist_num_bins = samp_bytes / sizeof (UNIT);
       DBG (SAMPLEDEBUG,
           printf ("[gmon_out_read] lowpc 0x%lx highpc 0x%lx ncnt %d\n",
-                  h.low_pc, h.high_pc, h.ncnt);
+                  (unsigned long) h.low_pc, (unsigned long) h.high_pc,
+                  h.ncnt);
           printf ("[gmon_out_read]   s_lowpc 0x%lx   s_highpc 0x%lx\n",
-                  s_lowpc, s_highpc);
+                  (unsigned long) s_lowpc, (unsigned long) s_highpc);
           printf ("[gmon_out_read]     lowpc 0x%lx     highpc 0x%lx\n",
-                  lowpc, highpc);
+                  (unsigned long) lowpc, (unsigned long) highpc);
           printf ("[gmon_out_read] samp_bytes %d hist_num_bins %d\n",
                   samp_bytes, hist_num_bins));
 
+      /* Make sure that we have sensible values.  */
+      if (samp_bytes < 0 || lowpc > highpc)
+        {
+          fprintf (stderr, 
+           _("%s: file '%s' does not appear to be in gmon.out format\n"),
+           whoami, filename);
+          done (1);
+        }
+
       if (hist_num_bins)
        {
          ++nhist;
-       }                       /* if */
+       }
 
       if (!hist_sample)
        {
          hist_sample =
            (int *) xmalloc (hist_num_bins * sizeof (hist_sample[0]));
          memset (hist_sample, 0, hist_num_bins * sizeof (hist_sample[0]));
-       }                       /* if */
+       }
 
       for (i = 0; i < hist_num_bins; ++i)
        {
          if (fread (raw_bin_count, sizeof (raw_bin_count), 1, ifp) != 1)
            {
              fprintf (stderr,
-                      "%s: unexpected EOF after reading %d/%d bins\n",
+                      _("%s: unexpected EOF after reading %d/%d bins\n"),
                       whoami, --i, hist_num_bins);
              done (1);
-           }                   /* if */
+           }
          hist_sample[i] += bfd_get_16 (core_bfd, (bfd_byte *) raw_bin_count);
-       }                       /* for */
+       }
 
       /*
        * The rest of the file consists of a bunch of <from,self,count>
@@ -242,11 +295,11 @@ DEFUN (gmon_out_read, (filename), const char *filename)
          self_pc = get_vma (core_bfd, (bfd_byte *) raw_arc.self_pc);
          count = bfd_get_32 (core_bfd, (bfd_byte *) raw_arc.count);
          DBG (SAMPLEDEBUG,
-            printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %d\n",
-                    from_pc, self_pc, count));
+            printf ("[gmon_out_read] frompc 0x%lx selfpc 0x%lx count %lu\n",
+                    (unsigned long) from_pc, (unsigned long) self_pc, count));
          /* add this arc: */
          cg_tally (from_pc, self_pc, count);
-       }                       /* while */
+       }
       fclose (ifp);
 
       if (hz == HZ_WRONG)
@@ -259,30 +312,30 @@ DEFUN (gmon_out_read, (filename), const char *filename)
          if (hz == HZ_WRONG)
            {
              hz = 1;
-             fprintf (stderr, "time is in ticks, not seconds\n");
-           }                   /* if */
-       }                       /* if */
+             fprintf (stderr, _("time is in ticks, not seconds\n"));
+           }
+       }
     }
   else
     {
-      fprintf (stderr, "%s: don't know how to deal with file format %d\n",
+      fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
               whoami, file_format);
       done (1);
-    }                          /* if */
+    }
 
   if (output_style & STYLE_GMON_INFO)
     {
-      printf ("File `%s' (version %d) contains:\n",
+      printf (_("File `%s' (version %d) contains:\n"),
              filename, gmon_file_version);
-      printf ("\t%d histogram record%s\n",
+      printf (_("\t%d histogram record%s\n"),
              nhist, nhist == 1 ? "" : "s");
-      printf ("\t%d call-graph record%s\n",
+      printf (_("\t%d call-graph record%s\n"),
              narcs, narcs == 1 ? "" : "s");
-      printf ("\t%d basic-block count record%s\n",
+      printf (_("\t%d basic-block count record%s\n"),
              nbbs, nbbs == 1 ? "" : "s");
       first_output = FALSE;
-    }                          /* if */
-}                              /* gmon_out_read */
+    }
+}
 
 
 void
@@ -296,7 +349,7 @@ DEFUN (gmon_out_write, (filename), const char *filename)
     {
       perror (filename);
       done (1);
-    }                          /* if */
+    }
 
   if (file_format == FF_AUTO || file_format == FF_MAGIC)
     {
@@ -308,49 +361,64 @@ DEFUN (gmon_out_write, (filename), const char *filename)
        {
          perror (filename);
          done (1);
-       }                       /* if */
+       }
 
       /* write execution time histogram if we have one: */
       if (gmon_input & INPUT_HISTOGRAM)
        {
          hist_write_hist (ofp, filename);
-       }                       /* if */
+       }
 
       /* write call graph arcs if we have any: */
       if (gmon_input & INPUT_CALL_GRAPH)
        {
          cg_write_arcs (ofp, filename);
-       }                       /* if */
+       }
 
       /* write basic-block info if we have it: */
       if (gmon_input & INPUT_BB_COUNTS)
        {
          bb_write_blocks (ofp, filename);
-       }                       /* if */
+       }
     }
-  else if (file_format == FF_BSD)
+  else if (file_format == FF_BSD || file_format == FF_BSD44)
     {
       struct raw_arc raw_arc;
       UNIT raw_bin_count;
-      bfd_vma lpc, hpc;
-      int i, ncnt;
+      struct raw_phdr h;
+      int i;
       Arc *arc;
       Sym *sym;
 
-      put_vma (core_bfd, s_lowpc, (bfd_byte *) & lpc);
-      put_vma (core_bfd, s_highpc, (bfd_byte *) & hpc);
+      memset (&h, 0, sizeof h);
+      put_vma (core_bfd, s_lowpc, (bfd_byte *) &h.low_pc);
+      put_vma (core_bfd, s_highpc, (bfd_byte *) &h.high_pc);
       bfd_put_32 (core_bfd,
                  hist_num_bins * sizeof (UNIT) + sizeof (struct raw_phdr),
-                   (bfd_byte *) & ncnt);
+                 (bfd_byte *) &h.ncnt);
 
-      /* write header: */
-      if (fwrite (&lpc, sizeof (lpc), 1, ofp) != 1
-         || fwrite (&hpc, sizeof (hpc), 1, ofp) != 1
-         || fwrite (&ncnt, sizeof (ncnt), 1, ofp) != 1)
+      /* Write header.  Use new style BSD format is explicitly
+         specified, or if the profiling rate is non-standard;
+         otherwise, use the old BSD format.  */
+      if (file_format == FF_BSD44
+         || hz != hertz ())
        {
-         perror (filename);
-         done (1);
-       }                       /* if */
+         bfd_put_32 (core_bfd, GMONVERSION, (bfd_byte *) &h.version);
+         bfd_put_32 (core_bfd, hz, (bfd_byte *) &h.profrate);
+         if (fwrite (&h, sizeof (struct raw_phdr), 1, ofp) != 1)
+           {
+             perror (filename);
+             done (1);
+           }
+       }
+      else
+       {
+         if (fwrite (&h, sizeof (struct old_raw_phdr), 1, ofp) != 1)
+           {
+             perror (filename);
+             done (1);
+           }
+       }
 
       /* dump the samples: */
 
@@ -361,8 +429,8 @@ DEFUN (gmon_out_write, (filename), const char *filename)
            {
              perror (filename);
              done (1);
-           }                   /* if */
-       }                       /* for */
+           }
+       }
 
       /* dump the normalized raw arc information: */
 
@@ -379,20 +447,19 @@ DEFUN (gmon_out_write, (filename), const char *filename)
                {
                  perror (filename);
                  done (1);
-               }               /* if */
+               }
              DBG (SAMPLEDEBUG,
-                  printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %d\n",
-                          arc->parent->addr, arc->child->addr, arc->count));
-           }                   /* for */
-       }                       /* for */
+                  printf ("[dumpsum] frompc 0x%lx selfpc 0x%lx count %lu\n",
+                          (unsigned long) arc->parent->addr,
+                          (unsigned long) arc->child->addr, arc->count));
+           }
+       }
       fclose (ofp);
     }
   else
     {
-      fprintf (stderr, "%s: don't know how to deal with file format %d\n",
+      fprintf (stderr, _("%s: don't know how to deal with file format %d\n"),
               whoami, file_format);
       done (1);
-    }                          /* if */
-}                              /* gmon_out_write */
-
-/*** gmon_out.c ***/
+    }
+}