2 * File hash.c - generate hash tables for Wine debugger symbols
4 * Copyright (C) 1993, Eric Youngdale.
11 #include <sys/types.h>
14 #include "selectors.h"
19 #define NR_NAME_HASH 128
21 static struct name_hash
* name_hash_table
[NR_NAME_HASH
];
23 static unsigned int name_hash( const char * name
)
25 unsigned int hash
= 0;
30 while (*p
) hash
= (hash
<< 15) + (hash
<< 3) + (hash
>> 3) + *p
++;
31 return hash
% NR_NAME_HASH
;
35 /***********************************************************************
38 * Add a symbol to the table.
41 DEBUG_AddSymbol( const char * name
, const DBG_ADDR
*addr
, const char * source
)
43 struct name_hash
* new;
46 new = (struct name_hash
*) xmalloc(sizeof(struct name_hash
));
48 new->name
= xstrdup(name
);
52 new->sourcefile
= xstrdup(source
);
56 new->sourcefile
= NULL
;
64 new->locals_alloc
= 0;
65 new->local_vars
= NULL
;
68 hash
= name_hash(name
);
70 /* Now insert into the hash table */
71 new->next
= name_hash_table
[hash
];
72 name_hash_table
[hash
] = new;
78 /***********************************************************************
79 * DEBUG_GetSymbolValue
81 * Get the address of a named symbol.
83 BOOL32
DEBUG_GetSymbolValue( const char * name
, const int lineno
,
90 for(nh
= name_hash_table
[name_hash(name
)]; nh
; nh
= nh
->next
)
91 if (!strcmp(nh
->name
, name
)) break;
93 if (!nh
&& (name
[0] != '_'))
96 strcpy(buffer
+1, name
);
97 for(nh
= name_hash_table
[name_hash(buffer
)]; nh
; nh
= nh
->next
)
98 if (!strcmp(nh
->name
, buffer
)) break;
102 * If we don't have anything here, then try and see if this
103 * is a local symbol to the current stack frame. No matter
104 * what, we have nothing more to do, so we let that function
105 * decide what we ultimately return.
107 if (!nh
) return DEBUG_GetStackSymbolValue(name
, addr
);
117 * Search for the specific line number. If we don't find it,
120 if( nh
->linetab
== NULL
)
125 for(i
=0; i
< nh
->n_lines
; i
++ )
127 if( nh
->linetab
[i
].line_number
== lineno
)
129 *addr
= nh
->linetab
[i
].pc_offset
;
135 * This specific line number not found.
144 /***********************************************************************
145 * DEBUG_SetSymbolValue
147 * Set the address of a named symbol.
149 BOOL32
DEBUG_SetSymbolValue( const char * name
, const DBG_ADDR
*addr
)
152 struct name_hash
*nh
;
154 for(nh
= name_hash_table
[name_hash(name
)]; nh
; nh
= nh
->next
)
155 if (!strcmp(nh
->name
, name
)) break;
157 if (!nh
&& (name
[0] != '_'))
160 strcpy(buffer
+1, name
);
161 for(nh
= name_hash_table
[name_hash(buffer
)]; nh
; nh
= nh
->next
)
162 if (!strcmp(nh
->name
, buffer
)) break;
165 if (!nh
) return FALSE
;
167 DBG_FIX_ADDR_SEG( &nh
->addr
, DS_reg(DEBUG_context
) );
172 /***********************************************************************
173 * DEBUG_FindNearestSymbol
175 * Find the symbol nearest to a given address.
176 * If ebp is specified as non-zero, it means we should dump the argument
177 * list into the string we return as well.
179 const char * DEBUG_FindNearestSymbol( const DBG_ADDR
*addr
, int flag
,
180 struct name_hash
** rtn
,
183 static char name_buffer
[MAX_PATH
+ 256];
184 static char arglist
[1024];
185 static char argtmp
[256];
186 struct name_hash
* nearest
= NULL
;
187 struct name_hash
* nh
;
188 unsigned int nearest_address
= 0;
191 char * lineinfo
, *sourcefile
;
197 for(i
=0; i
<NR_NAME_HASH
; i
++)
199 for (nh
= name_hash_table
[i
]; nh
; nh
= nh
->next
)
200 if (nh
->addr
.seg
== addr
->seg
&&
201 nh
->addr
.off
<= addr
->off
&&
202 nh
->addr
.off
>= nearest_address
)
204 nearest_address
= nh
->addr
.off
;
208 if (!nearest
) return NULL
;
214 memset(arglist
, '\0', sizeof(arglist
));
217 for(i
=0; i
< nearest
->n_locals
; i
++ )
220 * If this is a register (offset == 0) or a local
221 * variable, we don't want to know about it.
223 if( nearest
->local_vars
[i
].offset
<= 0 )
228 ptr
= (unsigned int *) (ebp
+ nearest
->local_vars
[i
].offset
);
229 if( arglist
[0] == '\0' )
235 strcat(arglist
, ", ");
238 sprintf(argtmp
, "%s=0x%x", nearest
->local_vars
[i
].name
,
240 strcat(arglist
, argtmp
);
242 if( arglist
[0] == '(' )
244 strcat(arglist
, ")");
248 if( (nearest
->sourcefile
!= NULL
) && (flag
== TRUE
)
249 && (addr
->off
- nearest
->addr
.off
< 0x100000) )
253 * Try and find the nearest line number to the current offset.
255 if( nearest
->linetab
!= NULL
)
258 * FIXME - this is an inefficient linear search. A binary
259 * search would be better if this gets to be a performance
262 for(i
=0; i
< nearest
->n_lines
; i
++)
264 if( addr
->off
< nearest
->linetab
[i
].pc_offset
.off
)
268 lineno
= nearest
->linetab
[i
].line_number
;
274 sprintf(linebuff
, ":%d", lineno
);
278 /* Remove the path from the file name */
279 sourcefile
= strrchr( nearest
->sourcefile
, '/' );
280 if (!sourcefile
) sourcefile
= nearest
->sourcefile
;
283 if (addr
->off
== nearest
->addr
.off
)
284 sprintf( name_buffer
, "%s%s [%s%s]", nearest
->name
,
285 arglist
, sourcefile
, lineinfo
);
287 sprintf( name_buffer
, "%s+0x%lx%s [%s%s]", nearest
->name
,
288 addr
->off
- nearest
->addr
.off
,
289 arglist
, sourcefile
, lineinfo
);
293 if (addr
->off
== nearest
->addr
.off
)
294 sprintf( name_buffer
, "%s%s", nearest
->name
, arglist
);
296 sprintf( name_buffer
, "%s+0x%lx%s", nearest
->name
,
297 addr
->off
- nearest
->addr
.off
, arglist
);
303 /***********************************************************************
304 * DEBUG_ReadSymbolTable
306 * Read a symbol file into the hash table.
308 void DEBUG_ReadSymbolTable( const char * filename
)
311 DBG_ADDR addr
= { 0, 0 };
318 if (!(symbolfile
= fopen(filename
, "r")))
320 fprintf( stderr
, "Unable to open symbol table %s\n", filename
);
324 fprintf( stderr
, "Reading symbols from file %s\n", filename
);
328 fgets( buffer
, sizeof(buffer
), symbolfile
);
329 if (feof(symbolfile
)) break;
331 /* Strip any text after a # sign (i.e. comments) */
334 if(*cpnt
++ == '#') { *cpnt
= 0; break; }
336 /* Quietly ignore any lines that have just whitespace */
340 if(*cpnt
!= ' ' && *cpnt
!= '\t') break;
343 if (!(*cpnt
) || *cpnt
== '\n') continue;
345 nargs
= sscanf(buffer
, "%lx %c %s", &addr
.off
, &type
, name
);
346 DEBUG_AddSymbol( name
, &addr
, NULL
);
352 /***********************************************************************
353 * DEBUG_LoadEntryPoints
355 * Load the entry points of all the modules into the hash table.
357 void DEBUG_LoadEntryPoints(void)
363 unsigned char *cpnt
, *name
;
367 for (ok
= ModuleFirst(&entry
); ok
; ok
= ModuleNext(&entry
))
369 if (!(pModule
= MODULE_GetPtr( entry
.hModule
))) continue;
371 name
= (unsigned char *)pModule
+ pModule
->name_table
;
373 /* First search the resident names */
375 cpnt
= (unsigned char *)pModule
+ pModule
->name_table
;
378 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
379 sprintf( buffer
, "%*.*s.%*.*s", *name
, *name
, name
+ 1,
380 *cpnt
, *cpnt
, cpnt
+ 1 );
381 if ((address
= MODULE_GetEntryPoint( entry
.hModule
,
382 *(WORD
*)(cpnt
+ *cpnt
+ 1) )))
384 addr
.seg
= HIWORD(address
);
385 addr
.off
= LOWORD(address
);
386 DEBUG_AddSymbol( buffer
, &addr
, NULL
);
390 /* Now search the non-resident names table */
392 if (!pModule
->nrname_handle
) continue; /* No non-resident table */
393 cpnt
= (char *)GlobalLock16( pModule
->nrname_handle
);
396 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
397 sprintf( buffer
, "%*.*s.%*.*s", *name
, *name
, name
+ 1,
398 *cpnt
, *cpnt
, cpnt
+ 1 );
399 if ((address
= MODULE_GetEntryPoint( entry
.hModule
,
400 *(WORD
*)(cpnt
+ *cpnt
+ 1) )))
402 addr
.seg
= HIWORD(address
);
403 addr
.off
= LOWORD(address
);
404 DEBUG_AddSymbol( buffer
, &addr
, NULL
);
411 DEBUG_AddLineNumber( struct name_hash
* func
, int line_num
,
412 unsigned long offset
)
419 if( func
->n_lines
+ 1 >= func
->lines_alloc
)
421 func
->lines_alloc
+= 32;
422 func
->linetab
= realloc(func
->linetab
,
423 func
->lines_alloc
* sizeof(WineLineNo
));
426 func
->linetab
[func
->n_lines
].line_number
= line_num
;
427 func
->linetab
[func
->n_lines
].pc_offset
.seg
= func
->addr
.seg
;
428 func
->linetab
[func
->n_lines
].pc_offset
.off
= func
->addr
.off
+ offset
;
434 DEBUG_AddLocal( struct name_hash
* func
, int regno
,
445 if( func
->n_locals
+ 1 >= func
->locals_alloc
)
447 func
->locals_alloc
+= 32;
448 func
->local_vars
= realloc(func
->local_vars
,
449 func
->locals_alloc
* sizeof(WineLocals
));
452 func
->local_vars
[func
->n_locals
].regno
= regno
;
453 func
->local_vars
[func
->n_locals
].offset
= offset
;
454 func
->local_vars
[func
->n_locals
].pc_start
= pc_start
;
455 func
->local_vars
[func
->n_locals
].pc_end
= pc_end
;
456 func
->local_vars
[func
->n_locals
].name
= xstrdup(name
);