00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 static char *Version = "$Source: /home/bos/bk/kpro64-pending/common/util/SCCS/s.mstack.c $ $Revision: 1.5 $";
00045 #include <stdio.h>
00046 #include <stdlib.h>
00047 #include <string.h>
00048 #include <sys/types.h>
00049 #include <unistd.h>
00050
00051 #include "mstack.h"
00052
00053 static int getsp ( int a );
00054 static int fra ( int a );
00055 static char *savestr ( char *str );
00056 #ifndef MONGOOSE_BE
00057 static void make_ftab ( void );
00058 #endif
00059 static struct frec *search_in_ftab ( int adr );
00060
00061 #if 0 // WAS: #if mips
00062 static int ftab_problems = 0;
00063
00064
00065
00066
00067 static int getsp(int a)
00068 {
00069 return (int) &a;
00070 }
00071
00072
00073
00074
00075
00076 static int fra(int a)
00077 {
00078 int *p = &a;
00079 return p[5];
00080 }
00081
00082 static int getra(void)
00083 {
00084 return fra(3);
00085 }
00086
00087 struct frec {
00088 char *name, *file;
00089 int addr, fsize, raofst;
00090 };
00091
00092 #define STRSP_SIZE 512
00093 static char *strsp;
00094 static int strsp_left = 0;
00095
00096 static char *savestr(char *str)
00097 {
00098 int l;
00099 char *x;
00100
00101 l = strlen(str);
00102 if (strsp_left < (l+1)) {
00103 strsp = (char *) malloc(STRSP_SIZE);
00104 strsp_left = STRSP_SIZE;
00105 }
00106 x = strsp;
00107 strcpy(x, str);
00108 strsp += (l+1);
00109 strsp_left -= (l+1);
00110 return x;
00111 }
00112
00113 static struct frec *ftab;
00114 static struct frec *main_fr;
00115 static struct frec *trst_fr;
00116 static char *tmpname = " ";
00117
00118 #ifndef MONGOOSE_BE
00119 static void make_ftab()
00120 {
00121 FILE *fp;
00122 char *curfile = "", buf1[200], buf[200];
00123 int adr, j1, j2, j3, j4, j5, ro, fs, l, i, lc, ch;
00124 struct frec *cf;
00125 extern char **__Argv;
00126
00127 sprintf(tmpname, "/tmp/TPF%1d", getpid());
00128 sprintf(buf, "odump -P %s > %s", __Argv[0], tmpname);
00129 system(buf);
00130 fp = fopen(tmpname, "r");
00131 unlink(tmpname);
00132 lc = 0;
00133 while ((ch = getc(fp)) != EOF) {
00134 if (ch == '\n')
00135 lc++;
00136 }
00137
00138 if (lc < 100) {
00139 ftab_problems++;
00140 fclose(fp);
00141 }
00142 fseek(fp, 0, 0);
00143 ftab = (struct frec *) malloc(lc * sizeof(struct frec));
00144 cf = ftab;
00145 for (i=0; i<8; i++)
00146 fgets(buf, 200, fp);
00147 while (fgets(buf, 200, fp)) {
00148 l = strlen(buf);
00149 if (buf[l-2] == ']') {
00150 sscanf(buf, " %s", buf1);
00151 curfile = savestr(buf1);
00152 } else if (buf[l-2] != ':') {
00153 sscanf(buf, "%s 0x%x %d %d %d 0x%x %d %d %d", buf1, &adr, &j1, &j2,
00154 &j3, &j4, &ro, &fs, &j5);
00155
00156 cf->file = curfile;
00157 cf->name = savestr(buf1);
00158 cf->addr = adr;
00159 cf->raofst = ro;
00160 cf->fsize = fs;
00161 if (strcmp(buf1, "main") == 0) main_fr = cf;
00162 if (strcmp(buf1, "trace_stack") == 0) trst_fr = cf;
00163 cf++;
00164 fgets(buf, 200, fp);
00165 }
00166 }
00167 fclose(fp);
00168 cf->addr = 0x7fffffff;
00169 }
00170 #endif
00171
00172
00173
00174 static struct frec *search_in_ftab(int adr)
00175 {
00176 struct frec *f;
00177
00178
00179 if (ftab_problems) {
00180 return NULL;
00181 }
00182 f = ftab;
00183 while (f->addr <= adr)
00184 f++;
00185 return f-1;
00186 }
00187
00188 static struct frec *this_func;
00189
00190 #ifndef MONGOOSE_BE
00191 int trace_stack(prfunc, prfile)
00192 int prfunc, prfile;
00193 {
00194 int sp, ra, fc;
00195 struct frec *cf;
00196
00197 #ifndef BACK_END
00198 return ( 1 );
00199 #endif
00200
00201 #define SPOFST -36
00202
00203 fc = 0;
00204 sp = getsp(0);
00205 if (ftab_problems == 0 && ftab == NULL)
00206 make_ftab();
00207
00208 cf = search_in_ftab(getra());
00209
00210
00211
00212
00213
00214 if (cf == NULL) return fc;
00215 while (1) {
00216 if (prfunc) {
00217 if (prfile)
00218 printf("%s:", cf->file);
00219 printf("%s\n", cf->name);
00220 }
00221 if (cf == main_fr)
00222 break;
00223 ra = * ((int *)(sp + cf->fsize + cf->raofst));
00224 sp += cf->fsize;
00225 cf = search_in_ftab(ra);
00226 fc++;
00227 }
00228 return fc;
00229 }
00230
00231 #endif
00232
00233 #else
00234
00235 #if A_UX
00236
00237 struct x {
00238 struct x *next;
00239 };
00240
00241 stack_lev(b)
00242 int b;
00243 {
00244 struct x *l = (struct x *) (((int)(&b)) - 8);
00245 int a = 0;
00246 while (l) {
00247 a++;
00248 l = l->next;
00249 }
00250 return a;
00251 }
00252
00253 trace_stack(a, b)
00254 {
00255 return stack_lev()-1;
00256 }
00257
00258 #else
00259
00260 char **__Argv;
00261
00262
00263 int trace_stack(a, b)
00264 int a;
00265 int b;
00266 {
00267 return 1;
00268 }
00269
00270 #endif
00271 #endif
00272