source: git/Singular/mmbt.c @ 416465

spielwiese
Last change on this file since 416465 was 416465, checked in by Olaf Bachmann <obachman@…>, 24 years ago
* bug-fixes from work with Thomas git-svn-id: file:///usr/local/Singular/svn/trunk@3826 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.8 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: mmbt.c,v 1.21 1999-11-15 17:20:23 obachman Exp $ */
5/*
6* ABSTRACT: backtrace: part of memory subsystem (for linux/elf)
7* needed programs: - mprpc to set the variable MPRPC
8*                  - mprdem: must be in the current directory
9*                  - mprnm: must be in thje current directory
10*                  - nm: to find entry pints if MPRPC is not set
11* files: - feGetResource('S'): the name of the executable
12*        - nm.log: temp. file for the map address -> name
13*/
14
15/* for simplicity: a fixed size array (currently used: 4437 entries (98111016))
16*/
17#define MAX_PROCS_BT 6000
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23#include <signal.h>
24#include <stddef.h>
25#include "mod2.h"
26#include "tok.h"
27#include "mmprivate.h"
28#include "febase.h"
29#include "mmbt.h"
30
31
32#ifdef MTRACK
33// #ifndef __OPTIMIZE__
34/* does only work in debug mode:
35* requires that -fomit-frame-pointer is not given
36*/
37#if defined(linux) && defined(__i386__)
38
39static unsigned long mm_lowpc=0, mm_highpc=0;
40
41extern int etext ();
42
43/*
44 * Number of words between saved pc and saved fp.
45 * All stack frames seen by mmTrack() must conform to
46 * the same value of SLOP.
47 */
48#ifndef SLOP
49#define SLOP 0
50#endif
51
52#define entrypc(pc)        ((pc >= mm_lowpc && pc <= mm_highpc) || pc >= (unsigned long) &etext || (pc < 4096))
53#define getfp(ap)        (ap-2-SLOP)
54#define getpc(fp)        (fp[1+SLOP])
55
56int mmTrackInit ()
57{
58  char *entry = getenv ("MPRPC");
59
60  if ((entry!=NULL) && (strchr (entry, ':')!=NULL))
61  {
62    mm_lowpc = atoi (entry);
63    mm_highpc = atoi (1 + strchr (entry, ':'));
64    return 0;
65  }
66  else if (feRes_works)
67  {
68    char buf[255];
69    sprintf(buf,"nm -n %s",feGetResource('S'));
70    {
71      FILE *nm=popen(buf,"r");
72      if (nm==NULL)
73      {
74        fprintf(stderr,
75          "environment variable MPRPC not found\nand pipe to `%s`failed\n",buf);
76        m2_end(1);
77      }
78      else
79      {
80        while(fgets(buf,sizeof(buf),nm))
81        {
82          if((strstr(buf," t ")!=NULL)
83          ||(strstr(buf," T ")!=NULL))
84          {
85            if(strstr(buf," _start\n")!=NULL)
86            {
87              sscanf(buf,"%lx",&mm_lowpc);
88              fgets(buf,sizeof(buf),nm);
89              sscanf(buf,"%lx",&mm_highpc);
90              mm_highpc--;
91              break;
92            }
93          }
94        }
95        pclose(nm);
96      }
97      return 0;
98    }
99  }
100  return 1;
101}
102
103static mmTrack_sig11_caught = 0;
104typedef void (*si_hdl_typ)(int);
105
106void mmTrack_sig11_handler(int sig)
107{
108  mmTrack_sig11_caught = 1;
109  printf("SIG11 Caught\n");
110  fflush(stdout);
111}
112
113void mmTrack (unsigned long *bt_stack)
114{
115  unsigned long pc, *fp = getfp ((unsigned long *) &bt_stack);
116  int i=0;
117#if 0 /* Geht nicht */
118  si_hdl_typ sig11_handler = signal(SIGSEGV, (si_hdl_typ) mmTrack_sig11_handler);
119  mmTrack_sig11_caught = 0;
120#endif
121
122  if (mm_lowpc==0) mmTrackInit();
123
124  while ((fp!=NULL) && ((unsigned long)fp>4095)
125  && ((unsigned long)fp < ((unsigned long)0xff000000))
126  && *fp && (pc = getpc (fp))
127  && !entrypc (pc) && (i<BT_MAXSTACK))
128  {
129    if ( mmTrack_sig11_caught) break;
130    bt_stack[i]=pc; i++;
131    fp = (unsigned long *) *fp;
132  }
133/*  signal(SIGSEGV, (si_hdl_typ) sig11_handler); */
134  while(i<BT_MAXSTACK)
135  {
136    bt_stack[i]=0; i++;
137  }
138}
139
140struct
141{
142  unsigned long p;
143  char *name;
144} p2n[MAX_PROCS_BT];
145
146static int mm_p2n_max = -1;
147void mmP2cNameInit()
148{
149  FILE *f;
150  int i,j;
151  char n[128];
152  char s[2000];
153  sprintf(s, "%s/mprnm -mprdem %s/mprdem -p %s >nm.log",
154          feGetResource('b'), feGetResource('b'), feGetResource('S'));
155  system(s);
156  f=fopen("nm.log","r");
157  i=0;
158  loop
159  {
160    j=fscanf(f,"%d %s\n",(int *)&p2n[i].p,n);
161    if (j!=2)
162    {
163      break;
164    }
165    if (strcmp(n, "___crt_dummy__") != 0 && strcmp(n, "_start") != 0)
166    {
167      p2n[i].name=strdup(n);
168      i++;
169    }
170  }
171  fclose(f);
172  unlink("nm.log");
173  p2n[i].name="??";
174  p2n[i].p=~1;
175  mm_p2n_max=i;
176}
177
178char * mmP2cName(unsigned long p)
179{
180  int i, e;
181  int a=0;
182  if (mm_p2n_max == -1)
183    mmP2cNameInit();
184  e=mm_p2n_max;
185  loop
186  {
187    if (a>=e-1)
188    {
189      if (p2n[a].p<=p) return p2n[a].name;
190      else if (a==0) return("??");
191      else Print("?? sort %x,%d in %d and %d\n",p,p,a,e);
192    }
193    i=(a+e)/2;
194    if (p>=p2n[i].p)
195    {
196      a=i;
197    }
198    else
199    {
200      e=i;
201    }
202  }
203#if 0
204  for(i=0;i<=mm_p2n_max;i++)
205  {
206    if (p>=p2n[i].p)
207    {
208      if (p<p2n[i+1].p) return p2n[i].name;
209      if (0==p2n[i+1].p) return p2n[i].name;
210    }
211  }
212  return "??";
213#endif
214}
215
216void mmPrintStack(FILE *fd, unsigned long *stack, int all)
217{
218  mmPrintStackFrames(fd, stack, 0, BT_MAXSTACK, all);
219}
220
221void mmDBPrintThisStack(FILE *fd, void* memblock, int all, int free)
222{
223#ifdef MTRACK_FREE
224  if (free)
225    mmPrintStackFrames(fd, ((DBMCB*) memblock)->bt_freed_stack, 0, BT_MAXSTACK, all);
226  else
227#endif
228    mmPrintStackFrames(fd, ((DBMCB*) memblock)->bt_allocated_stack, 0, BT_MAXSTACK, all);
229}
230
231void mmDBPrintStack(FILE *fd, void* memblock, int all)
232{
233  mmPrintStackFrames(fd, ((DBMCB*) memblock)->bt_allocated_stack, 0, BT_MAXSTACK, all);
234}
235
236void mmDBPrintStackFrames(FILE *fd, void* memblock, int start, int end)
237{
238  mmPrintStackFrames(fd, ((DBMCB*) memblock)->bt_allocated_stack, start, end,
239                     MM_PRINT_ALL_STACK);
240}
241
242/* print stack */
243void mmPrintStackFrames(FILE *fd, unsigned long *bt_stack, int start, int end, int mm)
244{
245  int i=start;
246  fprintf( fd," ");
247  do
248  {
249    char *s;
250    s=mmP2cName(bt_stack[i]);
251    if (s!=NULL && strcmp(s, "??"))
252    {
253      if ((mm & MM_PRINT_ALL_STACK) || strncmp(s, "mm", 2) !=0)
254        fprintf( fd,":%s",s);
255      if (strcmp(s, "main") == 0) break;
256    }
257    else
258      fprintf( fd,":%lx",(long)bt_stack[i]);
259    i++;
260  } while ((i<end) && (bt_stack[i]!=0));
261  fprintf( fd,"\n");
262}
263#endif /* linux, i386 */
264// #endif /* not optimize */
265#endif /* MTRACK */
Note: See TracBrowser for help on using the repository browser.