Release 961222
[wine/multimedia.git] / debugger / hash.c
blobbfbdafa3dd4533a66584398e902266524beb5d40
1 /*
2 * File hash.c - generate hash tables for Wine debugger symbols
4 * Copyright (C) 1993, Eric Youngdale.
5 */
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <neexe.h>
13 #include "module.h"
14 #include "selectors.h"
15 #include "debugger.h"
16 #include "toolhelp.h"
17 #include "xmalloc.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;
26 const char * p;
28 p = name;
30 while (*p) hash = (hash << 15) + (hash << 3) + (hash >> 3) + *p++;
31 return hash % NR_NAME_HASH;
35 /***********************************************************************
36 * DEBUG_AddSymbol
38 * Add a symbol to the table.
40 struct name_hash *
41 DEBUG_AddSymbol( const char * name, const DBG_ADDR *addr, const char * source )
43 struct name_hash * new;
44 int hash;
46 new = (struct name_hash *) xmalloc(sizeof(struct name_hash));
47 new->addr = *addr;
48 new->name = xstrdup(name);
50 if( source != NULL )
52 new->sourcefile = xstrdup(source);
54 else
56 new->sourcefile = NULL;
59 new->n_lines = 0;
60 new->lines_alloc = 0;
61 new->linetab = NULL;
63 new->n_locals = 0;
64 new->locals_alloc = 0;
65 new->local_vars = NULL;
67 new->next = 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;
74 return new;
78 /***********************************************************************
79 * DEBUG_GetSymbolValue
81 * Get the address of a named symbol.
83 BOOL32 DEBUG_GetSymbolValue( const char * name, const int lineno,
84 DBG_ADDR *addr )
86 char buffer[256];
87 int i;
88 struct name_hash *nh;
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] != '_'))
95 buffer[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);
110 if( lineno == -1 )
112 *addr = nh->addr;
114 else
117 * Search for the specific line number. If we don't find it,
118 * then return FALSE.
120 if( nh->linetab == NULL )
122 return FALSE;
125 for(i=0; i < nh->n_lines; i++ )
127 if( nh->linetab[i].line_number == lineno )
129 *addr = nh->linetab[i].pc_offset;
130 return TRUE;
135 * This specific line number not found.
137 return FALSE;
140 return TRUE;
144 /***********************************************************************
145 * DEBUG_SetSymbolValue
147 * Set the address of a named symbol.
149 BOOL32 DEBUG_SetSymbolValue( const char * name, const DBG_ADDR *addr )
151 char buffer[256];
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] != '_'))
159 buffer[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;
166 nh->addr = *addr;
167 DBG_FIX_ADDR_SEG( &nh->addr, DS_reg(DEBUG_context) );
168 return TRUE;
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,
181 unsigned int ebp)
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;
189 unsigned int * ptr;
190 int lineno;
191 char * lineinfo, *sourcefile;
192 int i;
193 char linebuff[16];
195 *rtn = NULL;
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;
205 nearest = nh;
208 if (!nearest) return NULL;
210 *rtn = nearest;
211 lineinfo = "";
212 lineno = -1;
214 memset(arglist, '\0', sizeof(arglist));
215 if( ebp != 0 )
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 )
225 continue;
228 ptr = (unsigned int *) (ebp + nearest->local_vars[i].offset);
229 if( arglist[0] == '\0' )
231 arglist[0] = '(';
233 else
235 strcat(arglist, ", ");
238 sprintf(argtmp, "%s=0x%x", nearest->local_vars[i].name,
239 *ptr);
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
260 * bottleneck.
262 for(i=0; i < nearest->n_lines; i++)
264 if( addr->off < nearest->linetab[i].pc_offset.off )
266 break;
268 lineno = nearest->linetab[i].line_number;
272 if( lineno != -1 )
274 sprintf(linebuff, ":%d", lineno);
275 lineinfo = linebuff;
278 /* Remove the path from the file name */
279 sourcefile = strrchr( nearest->sourcefile, '/' );
280 if (!sourcefile) sourcefile = nearest->sourcefile;
281 else sourcefile++;
283 if (addr->off == nearest->addr.off)
284 sprintf( name_buffer, "%s%s [%s%s]", nearest->name,
285 arglist, sourcefile, lineinfo);
286 else
287 sprintf( name_buffer, "%s+0x%lx%s [%s%s]", nearest->name,
288 addr->off - nearest->addr.off,
289 arglist, sourcefile, lineinfo );
291 else
293 if (addr->off == nearest->addr.off)
294 sprintf( name_buffer, "%s%s", nearest->name, arglist);
295 else
296 sprintf( name_buffer, "%s+0x%lx%s", nearest->name,
297 addr->off - nearest->addr.off, arglist);
299 return name_buffer;
303 /***********************************************************************
304 * DEBUG_ReadSymbolTable
306 * Read a symbol file into the hash table.
308 void DEBUG_ReadSymbolTable( const char * filename )
310 FILE * symbolfile;
311 DBG_ADDR addr = { 0, 0 };
312 int nargs;
313 char type;
314 char * cpnt;
315 char buffer[256];
316 char name[256];
318 if (!(symbolfile = fopen(filename, "r")))
320 fprintf( stderr, "Unable to open symbol table %s\n", filename );
321 return;
324 fprintf( stderr, "Reading symbols from file %s\n", filename );
326 while (1)
328 fgets( buffer, sizeof(buffer), symbolfile );
329 if (feof(symbolfile)) break;
331 /* Strip any text after a # sign (i.e. comments) */
332 cpnt = buffer;
333 while (*cpnt)
334 if(*cpnt++ == '#') { *cpnt = 0; break; }
336 /* Quietly ignore any lines that have just whitespace */
337 cpnt = buffer;
338 while(*cpnt)
340 if(*cpnt != ' ' && *cpnt != '\t') break;
341 cpnt++;
343 if (!(*cpnt) || *cpnt == '\n') continue;
345 nargs = sscanf(buffer, "%lx %c %s", &addr.off, &type, name);
346 DEBUG_AddSymbol( name, &addr, NULL );
348 fclose(symbolfile);
352 /***********************************************************************
353 * DEBUG_LoadEntryPoints
355 * Load the entry points of all the modules into the hash table.
357 void DEBUG_LoadEntryPoints(void)
359 MODULEENTRY entry;
360 NE_MODULE *pModule;
361 DBG_ADDR addr;
362 char buffer[256];
363 unsigned char *cpnt, *name;
364 FARPROC16 address;
365 BOOL32 ok;
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;
376 while (*cpnt)
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 );
394 while (*cpnt)
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);
410 void
411 DEBUG_AddLineNumber( struct name_hash * func, int line_num,
412 unsigned long offset )
414 if( func == NULL )
416 return;
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;
429 func->n_lines++;
433 void
434 DEBUG_AddLocal( struct name_hash * func, int regno,
435 int offset,
436 int pc_start,
437 int pc_end,
438 char * name)
440 if( func == NULL )
442 return;
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);
457 func->n_locals++;