Changeset 212fc04 in git for omalloc/omTrack.c


Ignore:
Timestamp:
Nov 22, 1999, 7:13:00 PM (24 years ago)
Author:
Olaf Bachmann <obachman@…>
Branches:
(u'spielwiese', '17f1d200f27c5bd38f5dfc6e8a0879242279d1d8')
Children:
6ea5825fc0e0bfabf56883aee9dac9b5bbe415fe
Parents:
e6908a0f43ea328a1aa0c3942e8d309db7de26d5
Message:
* as we go along


git-svn-id: file:///usr/local/Singular/svn/trunk@3878 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • omalloc/omTrack.c

    re6908a0 r212fc04  
    44 *  Author:  obachman (Olaf Bachmann)
    55 *  Created: 11/99
    6  *  Version: $Id: omTrack.c,v 1.1.1.1 1999-11-18 17:45:53 obachman Exp $
     6 *  Version: $Id: omTrack.c,v 1.2 1999-11-22 18:13:00 obachman Exp $
    77 *******************************************************************/
    88#include <limits.h>
     
    1616#endif
    1717
    18 static char* om_this_prog = NULL;
    19 static void* om_this_main_frame_addr = NULL;
    20 static void* om_this_prog_min_return_addr = ((void*) 1023);
    21 static void* om_this_prog_max_return_addr = ((void*) ULONG_MAX -1);
    22 
    23 
    24 void omInitTrack(const char* argv0)
    25 {
    26   char buf[MAXPATHLEN];
    27  
    28   if (argv0 != NULL && omFindExec(argv0, buf))
    29   {
    30     __omTypeAllocChunk(char*, om_this_prog, strlen(buf));
    31     strcpy(om_this_prog, buf);
    32   }
    33 #if defined(OM_FRAME_ADDR_WORKS)
    34   om_this_main_frame_addr = OM_FRAME_ADDR(1);
    35 #endif
    36 #if defined(OM_PROG_NM) && defined(HAVE_POPEN)
    37   if (om_this_prog != NULL)
    38   {
    39     char command[MAXPATHLEN + 30];
    40     FILE *pipe;
    41     sprintf(command, "%s -n %s", OM_PROG_NM, om_this_prog);
    42    
    43     pipe = popen(command, "r");
    44     if (pipe != NULL)
    45     {
    46       /* serach for first address */
    47       int c;
    48       void* nm_addr;
    49       while ( (c=fgetc(pipe)) !=  EOF)
    50       {
    51         if (c == '\n')
    52         {
    53           if (fscanf(pipe, "%p", &nm_addr) &&
    54               (unsigned long) nm_addr >
    55               (unsigned long) om_this_prog_min_return_addr)
    56           {
    57             om_this_prog_min_return_addr = nm_addr;
    58             break;
    59           }
    60         }
    61       }
    62       om_this_prog_max_return_addr = nm_addr;
    63       while ( (c=fgetc(pipe)) !=  EOF)
    64       {
    65         if (c == '\n')
    66         {
    67           if (fscanf(pipe, "%p", &nm_addr) && nm_addr != NULL &&
    68               (unsigned long) nm_addr  >
    69               (unsigned long) om_this_prog_max_return_addr)
    70           {
    71             om_this_prog_max_return_addr = nm_addr;
    72           }
    73         }
    74       }
    75       pclose(pipe);
    76     }
    77   }
    78 #endif /* defined(OM_NM) && defined(HAVE_POPEN) */
    79 }
    80 
    81 #define OM_MAX_TRACK_FRAMES 11
     18/* This is for nice alignment of omPrintBackTrace */
     19#define OM_MAX_PROC_NAME_LENGTH 20
     20/* if you make this larger than 11, extend OM_GET_RETURN_ADDR and
     21   OM_GET_FRAME_ADDR */
     22#define OM_MAX_BT_FRAMES 11
     23
     24#ifdef OM_RETURN_ADDR_RVALUE
     25#define OM_GET_RETURN_ADDR OM_RETURN_ADDR
     26#else
    8227#define OM_GET_RETURN_ADDR(addr, i)             \
    8328switch(i)                                       \
     
    10853      addr = NULL;                              \
    10954}
    110 
     55#endif /* OM_RETURN_ADDR_RVALUE */
     56
     57#ifdef OM_FRAME_ADDR_RVALUE
     58#define OM_GET_FRAME_ADDR OM_FRAME_ADDR
     59#else
    11160#define OM_GET_FRAME_ADDR(addr, i)              \
    11261switch(i)                                       \
     
    13786      addr = NULL;                              \
    13887}
     88#endif /* OM_FRAME_ADDR_RVALUE */
     89
     90static char* om_this_prog = NULL;
     91static void* om_this_main_frame_addr = NULL;
     92static void* om_this_prog_min_return_addr = ((void*) 1023);
     93static void* om_this_prog_max_return_addr = ((void*) ULONG_MAX -1);
     94
     95
     96void omInitTrack(const char* argv0)
     97{
     98  char buf[MAXPATHLEN];
     99 
     100  if (argv0 != NULL && omFindExec(argv0, buf))
     101  {
     102    __omTypeAllocChunk(char*, om_this_prog, strlen(buf));
     103    strcpy(om_this_prog, buf);
     104  }
     105#if defined(OM_FRAME_ADDR_WORKS)
     106  om_this_main_frame_addr = OM_FRAME_ADDR(1);
     107#endif
     108#if defined(OM_PROG_NM) && defined(HAVE_POPEN)
     109  if (om_this_prog != NULL)
     110  {
     111    char command[MAXPATHLEN + 30];
     112    FILE *pipe;
     113    sprintf(command, "%s -n %s", OM_PROG_NM, om_this_prog);
     114   
     115    pipe = popen(command, "r");
     116    if (pipe != NULL)
     117    {
     118      /* serach for first address */
     119      int c;
     120      void* nm_addr;
     121      while ( (c=fgetc(pipe)) !=  EOF)
     122      {
     123        if (c == '\n')
     124        {
     125          if (fscanf(pipe, "%p", &nm_addr) &&
     126              (unsigned long) nm_addr >
     127              (unsigned long) om_this_prog_min_return_addr)
     128          {
     129            om_this_prog_min_return_addr = nm_addr;
     130            break;
     131          }
     132        }
     133      }
     134      om_this_prog_max_return_addr = nm_addr;
     135      while ( (c=fgetc(pipe)) !=  EOF)
     136      {
     137        if (c == '\n')
     138        {
     139          if (fscanf(pipe, "%p", &nm_addr) && nm_addr != NULL &&
     140              (unsigned long) nm_addr  >
     141              (unsigned long) om_this_prog_max_return_addr)
     142          {
     143            om_this_prog_max_return_addr = nm_addr;
     144          }
     145        }
     146      }
     147      pclose(pipe);
     148    }
     149  }
     150#endif /* defined(OM_NM) && defined(HAVE_POPEN) */
     151}
    139152
    140153
    141154int omGetCurrentBackTrace(void** addr, int max_frames)
    142155{
     156  int i = 0;
    143157#if defined(OM_RETURN_ADDR_WORKS)
    144158#if defined(OM_FRAME_ADDR_WORKS)
     
    150164    {
    151165#endif /* ! defined(OM_RETURN_ADDR_WORKS) */
    152       int i;
    153166      void* r_addr;
     167      if (max_frames > OM_MAX_BT_FRAMES) max_frames = OM_MAX_BT_FRAMES;
    154168      for (i=0; i< max_frames; i++)
    155169      {
     
    178192#endif /* OM_FRAME_ADDR_WORKS */
    179193  }
    180   return max_frames;
    181194#endif /* defined(OM_RETURN_ADDR_WORKS) */
    182   return 0;
     195  return i;
    183196}
    184197
     
    186199{
    187200  int i = 0;
     201  if (max_frames > OM_MAX_BT_FRAMES) max_frames = OM_MAX_BT_FRAMES;
    188202#if defined(HAVE_POPEN) && defined(OM_PROG_ADDR2LINE)
    189203  if (om_this_prog != NULL)
    190204  {
    191     char command[2*MAXPATHLEN + 15 + OM_MAX_TRACK_FRAMES*(2*SIZEOF_VOIDP + 4)];
     205    char command[2*MAXPATHLEN + 15 + OM_MAX_BT_FRAMES*(2*SIZEOF_VOIDP + 4)];
    192206    FILE *pipe;
    193207    int l;
    194 
    195208    l = sprintf(command, "%s -s -C -f -e %s",
    196209                     OM_PROG_ADDR2LINE, om_this_prog);
     
    202215    }
    203216   
    204     if (i==0) return 0;
    205     pipe = popen(command, "r");
    206     if (pipe != NULL)
    207     {
    208       int nl = '@';
    209       while ((l=fgetc(pipe)) != EOF)
    210       {
    211         if (l == '\n')
    212         {
    213           l =fgetc(pipe);
    214           if (l == EOF) break;
    215           fputc(nl, fd);
    216           if (nl == '@') nl = '#';
    217           else nl = '@';
    218         }
    219         fputc(l, fd);
    220       }
    221       pclose(pipe);
    222       if (i > 0)
    223       {
    224         fprintf(fd, "%d", i);
    225         fputc('\n', fd);
     217    if (i > 0)
     218    {
     219      pipe = popen(command, "r");
     220      if (pipe != NULL)
     221      {
     222        int nl = 0;
     223        int j = 0;
     224        int k=0;
     225        while ((l=fgetc(pipe)) != EOF)
     226        {
     227          if (nl == 0)
     228          {
     229            fprintf(fd, "  #%d %p in ", j, addr[j]);
     230            nl = 1;
     231            j++;
     232            k=0;
     233          }
     234          if (l == '\n')
     235          {
     236            if (nl == 1)
     237            {
     238              while (k<OM_MAX_PROC_NAME_LENGTH)
     239              {
     240                fprintf(fd, " ");
     241                k++;
     242              }
     243              fprintf(fd, " at ");
     244              nl = 2;
     245            }
     246            else
     247            {
     248              fputc('\n', fd);
     249              nl = 0;
     250            }
     251          }
     252          else
     253          {
     254            k++;
     255            fputc(l, fd);
     256          }
     257        }
     258        pclose(pipe);
    226259        fflush(fd);
    227       }
    228       return i;
    229     }
    230     i=0;
    231   }
    232 #endif
     260        return i;
     261      }
     262      i=0;
     263    }
     264  }
     265#endif
     266#if OM_RETURN_ADDR_WORKS
    233267  while (i<max_frames && addr[i] != NULL)
    234268  {
    235     fprintf(fd, "%p#", addr[i]);
     269    fprintf(fd, "  #%d %p in ??\n", i, addr[i]);
    236270    i++;
    237271  }
    238   if (i > 0)
    239   {
     272#endif
     273  if (i == 0)
     274  {
     275    fprintf(fd, "??");
    240276    fputc('\n', fd);
    241     fflush(fd);
    242   }
     277  }
     278  fflush(fd);
    243279  return i;
    244280}
     
    246282int omPrintCurrentBackTrace(int from_frame, int max_frames, FILE *fd)
    247283{
    248 #if defined(OM_RETURN_ADDR_WORKS)
    249   if (from_frame >= 0)
    250   {
    251     void* addr[OM_MAX_TRACK_FRAMES];
    252     int got_frames = omGetCurrentBackTrace(addr, from_frame + max_frames + 1);
    253     return omPrintBackTrace(&addr[from_frame + 1], got_frames, fd);
    254   }
    255 #endif
    256   return 0;
     284  void* addr[OM_MAX_BT_FRAMES];
     285  int got_frames = omGetCurrentBackTrace(addr, from_frame + max_frames + 1);
     286  return omPrintBackTrace(&addr[from_frame + 1],
     287                          got_frames - from_frame + 1, fd);
    257288}
    258289
Note: See TracChangeset for help on using the changeset viewer.