source: git/Singular/mmbt.c @ 6e5833

spielwiese
Last change on this file since 6e5833 was 6e5833, checked in by Olaf Bachmann <obachman@…>, 25 years ago
* some improvements to mmbt git-svn-id: file:///usr/local/Singular/svn/trunk@2963 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 4.0 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: mmbt.c,v 1.8 1999-03-19 17:42:28 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* files: - Singularg: the name of the executable
11*        - nm.log: temp. file for the map address -> name
12*/
13
14/* for simplicity: a fixed size array (currently used: 4437 entries (98111016))
15*/
16#define MAX_PROCS_BT 5000
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22#include "mod2.h"
23#include "tok.h"
24#include "mmprivate.h"
25#include "febase.h"
26#include "mmbt.h"
27
28#ifdef MTRACK
29#ifndef __OPTIMIZE__
30/* does only work in debug mode:
31* requires that -fomit-frame-pointer is not given
32*/
33#if defined(linux) && defined(__i386__)
34
35static unsigned long mm_lowpc=0, mm_highpc=0;
36
37extern int etext ();
38
39/*
40 * Number of words between saved pc and saved fp.
41 * All stack frames seen by mmTrack() must conform to
42 * the same value of SLOP.
43 */
44#ifndef SLOP
45#define SLOP 0
46#endif
47
48#define entrypc(pc)        ((pc >= mm_lowpc && pc <= mm_highpc) || pc >= (unsigned long) &etext || (pc < 4096))
49#define getfp(ap)        (ap-2-SLOP)
50#define getpc(fp)        (fp[1+SLOP])
51
52int mmTrackInit ()
53{
54  char *entry = getenv ("MPRPC");
55
56  if ((entry!=NULL) && (strchr (entry, ':')!=NULL))
57  {
58    mm_lowpc = atoi (entry);
59    mm_highpc = atoi (1 + strchr (entry, ':'));
60    return 0;
61  }
62  return 1;
63}
64
65void mmTrack (unsigned long *bt_stack)
66{
67  unsigned long pc, *fp = getfp ((unsigned long *) &bt_stack);
68  int i=0;
69
70  if (mm_lowpc==0) mmTrackInit();
71
72  while ((fp!=NULL) && ((unsigned long)fp>4095)  && *fp && (pc = getpc (fp)) 
73  && !entrypc (pc) && (i<BT_MAXSTACK))
74  {
75    bt_stack[i]=pc; i++;
76    fp = (unsigned long *) *fp;
77  }
78  while(i<BT_MAXSTACK)
79  {
80    bt_stack[i]=0; i++;
81  }
82}
83
84struct
85{
86  unsigned long p;
87  char *name;
88} p2n[MAX_PROCS_BT];
89
90static int mm_p2n_max = -1;
91void mmP2cNameInit()
92{
93  FILE *f;
94  int i,j;
95  char n[128];
96  system("./mprnm -p Singularg >nm.log");
97  f=fopen("nm.log","r");
98  i=0;
99  loop
100  {
101    j=fscanf(f,"%d %s\n",(int *)&p2n[i].p,n);
102    if (j!=2) break;
103    if (strcmp(n, "___crt_dummy__") != 0 && strcmp(n, "_start") != 0)
104    {
105      p2n[i].name=strdup(n);
106      i++;
107    }
108  }
109  fclose(f);
110  unlink("nm.log");
111  p2n[i].name="??";
112  p2n[i].p=~1;
113  mm_p2n_max=i;
114}
115char * mmP2cName(unsigned long p)
116{
117  int i, e;
118  int a=0;
119  if (mm_p2n_max == -1)
120    mmP2cNameInit();
121  e=mm_p2n_max;
122  loop
123  {
124    if (a>=e-1) 
125    {
126      if (p2n[a].p<=p) return p2n[a].name;
127      else if (a==0) return("??");
128      else Print("?? sort %x,%d in %d and %d\n",p,p,a,e);
129    }
130    i=(a+e)/2;
131    if (p>=p2n[i].p)
132    {
133      a=i;
134    }
135    else
136    {
137      e=i;
138    }
139  } 
140#if 0
141  for(i=0;i<=mm_p2n_max;i++)
142  {
143    if (p>=p2n[i].p)
144    {
145      if (p<p2n[i+1].p) return p2n[i].name;
146      if (0==p2n[i+1].p) return p2n[i].name;
147    }
148  }
149  return "??";
150#endif
151}
152
153void mmPrintStack(unsigned long *stack, int all)
154{
155  mmPrintStackFrames(stack, 0, BT_MAXSTACK, all);
156}
157
158void mmDBPrintStack(void* memblock, int all)
159{
160  mmPrintStackFrames(((DBMCB*) memblock)->bt_stack, 0, BT_MAXSTACK, all);
161}
162
163void mmDBPrintStackFrames(void* memblock, int start, int end)
164{
165  mmPrintStackFrames(((DBMCB*) memblock)->bt_stack, start, end, 
166                     MM_PRINT_ALL_STACK);
167}
168
169/* print stack */
170void mmPrintStackFrames(unsigned long *bt_stack, int start, int end, int mm) 
171{
172  int i=start;
173  PrintS(" ");
174  do
175  {
176    char *s;
177    s=mmP2cName(bt_stack[i]); 
178    if (s!=NULL)
179    {
180      if ((mm & MM_PRINT_ALL_STACK) || strncmp(s, "mm", 2) !=0)
181        fprintf( stderr,":%s",s);
182      if (strcmp(s, "main") == 0) break;
183    }
184    else
185      fprintf( stderr,":%x",bt_stack[i]);
186    i++;
187  } while ((i<end) && (bt_stack[i]!=0));
188  fprintf( stderr,"\n");
189}
190#endif /* linux, i386 */
191#endif /* not optimize */
192#endif /* MTRACK */
Note: See TracBrowser for help on using the repository browser.