1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
/*
(c) Copyright W. Schelter 1988, All rights reserved.
Copyright (c) 2024 Camm Maguire
*/
#include <unistd.h>
#include "include.h"
#include "page.h"
#ifdef HAVE_LIBBFD
#ifdef NEED_CONST
#define CONST const
#endif
#define IN_GCC
#include <bfd.h>
#include <bfdlink.h>
#endif
#define FAT_STRING
enum type what_to_collect;
/* start fasdump stuff */
#include "fasdump.c"
object sSAprofile_arrayA;
#ifdef NO_PROFILE
#ifdef DARWIN/*FIXME macosx10.8 has a prototype (which must match here) but unlinkable function in 64bit*/
int profil(char *buf, size_t bufsiz, unsigned long offset, unsigned int scale){return 0;}
#else
void profil(void){;}
#endif
#endif
#ifndef NO_PROFILE
DEFUN("PROFILE",object,fSprofile,SI
,2,2,NONE,OO,OO,OO,OO,(object start_address,object scale),
"Sets up profiling with START-ADDRESS and SCALE where scale is \
between 0 and 256")
{ /* 2 args */
object ar=sSAprofile_arrayA->s.s_dbind;
void *x;
fixnum a,s;
if (!stringp(ar))
FEerror("si:*Profile-array* not a string",0);
if( type_of(start_address)!=t_fixnum || type_of(scale)!=t_fixnum)
FEerror("Needs start address and scale as args",0);
massert((a=fix(start_address))>=0);
massert((s=fix(scale))>=0);
x=a&&s ? (void *) (ar->ust.ust_self) : NULL;
profil(x, (ar->ust.ust_dim),fix(start_address),fix(scale) << 8);
RETURN1(start_address);
}
#endif
DEFUN("FUNCTION-START",object,fSfunction_start,SI
,1,1,NONE,OO,OO,OO,OO,(object funobj),"")
{/* 1 args */
if(/* type_of(funobj)!=t_cfun */
/* && */type_of(funobj)!=t_function)
FEerror("not compiled function",0);
funobj=make_fixnum((long) (funobj->fun.fun_self));
RETURN1(funobj);
}
/* begin fasl stuff*/
/* this is for windows to not include all of windows.h for this..*/
#include "ptable.h"
#ifdef AIX3
#include <sys/ldr.h>
char *data_load_addr =0;
#endif
#define CFUN_LIM 10000
int maxpage;
object sScdefn;
#define CF_FLAG ((unsigned long)1 << (sizeof(long)*CHAR_SIZE-1))
static void
cfuns_to_combined_table(unsigned int n) /* non zero n will ensure new table length */
{int ii=0;
STATIC int j;
STATIC object x;
STATIC char *p,*cf_addr;
STATIC struct typemanager *tm;
if (! (n || combined_table.ptable)) n=CFUN_LIM;
if (n && combined_table.alloc_length < n)
{
(combined_table.ptable)=NULL;
(combined_table.ptable)= (struct node *)malloc(n* sizeof(struct node));
if(!combined_table.ptable)
FEerror("unable to allocate",0);
combined_table.alloc_length=n;}
{
struct pageinfo *v;
for (v=cell_list_head;v;v=v->next) {
enum type tp=v->type;
if (tp!=tm_table[(short)t_function].tm_type)
continue;
tm = tm_of(tp);
p = pagetochar(page(v));
for (j = tm->tm_nppage; j > 0; --j, p += tm->tm_size) {
x = (object)p;
if (type_of(x)!=t_function)
continue;
if (is_free(x) || x->fun.fun_self == NULL)
continue;
/* the cdefn things are the proclaimed call types. */
cf_addr=(char * ) ((unsigned long)(x->fun.fun_self));
SYM_ADDRESS(combined_table,ii)=(unsigned long)cf_addr;
SYM_STRING(combined_table,ii)= (char *)(CF_FLAG | (unsigned long)x) ;
/* (x->cf.cf_name ? x->cf.cf_name->s.st_self : NULL) ; */
combined_table.length = ++ii;
if (ii >= combined_table.alloc_length)
FEerror("Need a larger combined_table",0);
}
}
}
}
static int
address_node_compare(const void *node1, const void *node2)
{unsigned int a1,a2;
a1=((struct node *)node1)->address;
a2=((struct node *)node2)->address;
if (a1> a2) return 1;
if (a1< a2) return -1;
return 0;
}
#if defined(HAVE_LIBBFD) && ! defined(SPECIAL_RSYM)
static int bfd_update;
static MY_BFD_BOOLEAN
bfd_combined_table_update(struct bfd_link_hash_entry *h,PTR ct) {
if (ct!=&combined_table)
return MY_BFD_FALSE;
if (h->type!=bfd_link_hash_defined)
return MY_BFD_TRUE;
if (!h->u.def.section) {
FEerror("Symbol without section",0);
return MY_BFD_FALSE;
}
if (bfd_update) {
if (combined_table.length>=combined_table.alloc_length)
FEerror("combined table overflow", 0);
SYM_ADDRESS(combined_table,combined_table.length)=h->u.def.value+h->u.def.section->vma;
SYM_STRING(combined_table,combined_table.length)=(char *)h->root.string;
}
combined_table.length++;
return MY_BFD_TRUE;
}
#endif
DEFUN("SET-UP-COMBINED",object,fSset_up_combined,SI
,0,1,NONE,OO,OO,OO,OO,(object first,...),"") {
unsigned int n;
object siz,l=Cnil,f=OBJNULL;
fixnum nargs=INIT_NARGS(0);
va_list ap;
va_start(ap,first);
siz=NEXT_ARG(nargs,ap,l,f,make_fixnum(0));
n = (unsigned int) fix(siz);
cfuns_to_combined_table(n);
#if !defined(HAVE_LIBBFD) && !defined(SPECIAL_RSYM)
#error Need either BFD or SPECIAL_RSYM
#endif
#if defined(SPECIAL_RSYM)
if (c_table.ptable) {
int j,k;
if((k=combined_table.length)+c_table.length >= combined_table.alloc_length)
cfuns_to_combined_table(combined_table.length+c_table.length+20);
for(j = 0; j < c_table.length;) {
SYM_ADDRESS(combined_table,k) =SYM_ADDRESS(c_table,j);
SYM_STRING(combined_table,k) =SYM_STRING(c_table,j);
k++;
j++;
}
combined_table.length += c_table.length ;
}
#else
#if defined(HAVE_LIBBFD)
if (link_info.hash) {
bfd_update=0;
bfd_link_hash_traverse(link_info.hash,
bfd_combined_table_update,&combined_table);
if (combined_table.length >=combined_table.alloc_length)
cfuns_to_combined_table(combined_table.length);
bfd_update=1;
bfd_link_hash_traverse(link_info.hash,
bfd_combined_table_update,&combined_table);
bfd_update=0;
}
#endif
#endif
qsort(combined_table.ptable,combined_table.length,sizeof(*combined_table.ptable),address_node_compare);
RETURN1(siz);
}
static int prof_start;
static int
prof_ind(unsigned int address, int scale)
{address = address - prof_start ;
if (address > 0) return ((address * scale) >> 8) ;
return 0;
}
/* sum entries AAR up to DIM entries */
static int
string_sum(register unsigned char *aar, unsigned int dim)
{register unsigned char *endar;
register unsigned int count = 0;
endar=aar+dim;
for ( ; aar< endar; aar++)
count += *aar;
return count;
}
DEFUN("DISPLAY-PROFILE",object,fSdisplay_profile,SI
,2,2,NONE,OO,OO,OO,OO,(object start_addr,object scal),"") {
if (!combined_table.ptable)
FEerror("must symbols first",0);
/* 2 args */
{
unsigned int prev,next,upto,dim,total;
int j,scale,count;
unsigned char *ar;
object obj_ar;
obj_ar=sSAprofile_arrayA->s.s_dbind;
if (!stringp(obj_ar))
FEerror("si:*Profile-array* not a string",0);
ar=obj_ar->ust.ust_self;
scale=fix(scal);
prof_start=fix(start_addr);
vs_top=vs_base;
dim= (obj_ar->ust.ust_dim);
total=string_sum(ar,dim);
j=0;
{
int i, finish = combined_table.length-1;
for(i =0,prev=SYM_ADDRESS(combined_table,i); i< finish;prev=next) {
++i;
next=SYM_ADDRESS(combined_table,i);
if (prev<prof_start)
continue;
upto=prof_ind(next,scale);
if (upto >= dim)
upto=dim;
{
const char *name; unsigned long uname;
count=0;
for(;j<upto;j++)
count += ar[j];
if (count > 0) {
name=SYM_STRING(combined_table,i-1);
uname = (unsigned long) name;
printf("\n%6.2f%% (%5d): ",(100.0*count)/total, count);
fflush(stdout);
if (CF_FLAG & uname)
;/*{ if (~CF_FLAG & uname) prin1( ((object) (~CF_FLAG & uname))->cf.cf_name,Cnil);} *//*FIXME*/
else if (name ) printf("%s",name);};
if (upto==dim) goto TOTALS ;
}
}
}
TOTALS:
printf("\nTotal ticks %d",total);fflush(stdout);
}
RETURN1(start_addr);
}
/* end fasl stuff*/
/* These are some low level hacks to allow determining the address
of an array body, and to allow jumping to inside the body
of the array */
DEFUN("ARRAY-ADRESS",object,fSarray_adress,SI
,1,1,NONE,OO,OO,OO,OO,(object array),"")
{/* 1 args */
array=make_fixnum((long) (&(array->st.st_self[0])));
RETURN1(array);
}
/* This is some very low level code for hacking invokation of
m68k instructions in a lisp array. The index used should be
a byte index. So invoke(ar,3) jmps to byte ar+3.
*/
#ifdef CLI
invoke(ar)
char *ar;
{asm("movel a6@(8),a0");
asm("jmp a0@");
}
/* save regs (2 3 4 5 6 7 10 11 12 13 14) and invoke restoring them */
save_regs_invoke(ar)
char *ar;
{asm("moveml #0x3f3e,sp@-");
invoke(ar);
asm("moveml a6@(-44),#0x7cfc");
}
/* DEFUNO_NEW("SAVE-REGS-INVOKE",object,fSsave_regs_invoke,SI
,2,2,NONE,OO,OO,OO,OO,void,siLsave_regs_invoke,"",(x0,x1))
object x0,x1;
{int x;
check_type_integer(&x1);
x=save_regs_invoke((x0->st.st_self)+fix(x1));
x0=make_fixnum(x);
RETURN1(x0);
}
*/
#endif
DEFVAR("*PROFILE-ARRAY*",sSAprofile_arrayA,SI,Cnil,"");
void
gcl_init_fat_string(void)
{
make_si_constant("*ASH->>*",(-1==(((int)-1) >> 20))? Ct :Cnil);
/* #ifdef SFASL */
/* make_si_function("BUILD-SYMBOL-TABLE",build_symbol_table); */
/* #endif */
init_fasdump();
}
|