source: git/Singular/mmbt.c @ 105efec

spielwiese
Last change on this file since 105efec was 105efec, checked in by Hans Schönemann <hannes@…>, 25 years ago
* hannes: MTRACK fixes git-svn-id: file:///usr/local/Singular/svn/trunk@2962 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 3.4 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: mmbt.c,v 1.7 1999-03-19 16:00:05 Singular 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    p2n[i].name=strdup(n);
104    i++;
105  }
106  fclose(f);
107  unlink("nm.log");
108  p2n[i].name="??";
109  p2n[i].p=~1;
110  mm_p2n_max=i;
111}
112char * mmP2cName(unsigned long p)
113{
114  int a=0;
115  int e=mm_p2n_max;
116  int i;
117  if (mm_p2n_max == -1)
118    mmP2cNameInit();
119  loop
120  {
121    if (a>=e-1) 
122    {
123      if (p2n[a].p<=p) return p2n[a].name;
124      else if (a==0) return("??");
125      else Print("?? sort %x,%d in %d and %d\n",p,p,a,e);
126    }
127    i=(a+e)/2;
128    if (p>=p2n[i].p)
129    {
130      a=i;
131    }
132    else
133    {
134      e=i;
135    }
136  } 
137#if 0
138  for(i=0;i<=mm_p2n_max;i++)
139  {
140    if (p>=p2n[i].p)
141    {
142      if (p<p2n[i+1].p) return p2n[i].name;
143      if (0==p2n[i+1].p) return p2n[i].name;
144    }
145  }
146  return "??";
147#endif
148}
149
150void mmPrintStack(unsigned long *bt_stack) /* print stack */
151{
152  int i=0;
153  PrintS(" ");
154  do
155  {
156    char *s;
157    fprintf( stderr,":%x",bt_stack[i]);
158    s=mmP2cName(bt_stack[i]); 
159    if (s!=NULL)
160      fprintf( stderr,"/%s",s);
161    i++;
162  } while ((i<BT_MAXSTACK) && (bt_stack[i]!=0));
163  fprintf( stderr,"\n");
164}
165#endif /* linux, i386 */
166#endif /* not optimize */
167#endif /* MTRACK */
Note: See TracBrowser for help on using the repository browser.