Release 971101
[wine] / debugger / hash.c
1 /*
2  * File hash.c - generate hash tables for Wine debugger symbols
3  *
4  * Copyright (C) 1993, Eric Youngdale.
5  */
6
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <limits.h>
12 #include <sys/types.h>
13 #include <neexe.h>
14 #include "module.h"
15 #include "process.h"
16 #include "selectors.h"
17 #include "debugger.h"
18 #include "toolhelp.h"
19 #include "xmalloc.h"
20
21 #define NR_NAME_HASH 16384
22 #ifndef PATH_MAX
23 #define PATH_MAX _MAX_PATH
24 #endif
25
26 static char * reg_name[] =
27 {
28   "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"
29 };
30
31
32 struct name_hash
33 {
34     struct name_hash * next;            /* Used to look up within name hash */
35     char *             name;
36     char *             sourcefile;
37
38     int                n_locals;
39     int                locals_alloc;
40     WineLocals       * local_vars;
41   
42     int                n_lines;
43     int                lines_alloc;
44     WineLineNo       * linetab;
45
46     DBG_ADDR           addr;
47     unsigned short     flags;
48     unsigned short     breakpoint_offset;
49     unsigned int       symbol_size;
50 };
51
52
53 static BOOL32 DEBUG_GetStackSymbolValue( const char * name, DBG_ADDR *addr );
54 static int sortlist_valid = FALSE;
55
56 static int sorttab_nsym;
57 static struct name_hash ** addr_sorttab = NULL;
58
59 static struct name_hash * name_hash_table[NR_NAME_HASH];
60
61 static unsigned int name_hash( const char * name )
62 {
63     unsigned int hash = 0;
64     unsigned int tmp;
65     const char * p;
66
67     p = name;
68
69     while (*p) 
70       {
71         hash = (hash << 4) + *p++;
72
73         if( (tmp = (hash & 0xf0000000)) )
74           {
75             hash ^= tmp >> 24;
76           }
77         hash &= ~tmp;
78       }
79     return hash % NR_NAME_HASH;
80 }
81
82 int
83 DEBUG_cmp_sym(const void * p1, const void * p2)
84 {
85   struct name_hash ** name1 = (struct name_hash **) p1;
86   struct name_hash ** name2 = (struct name_hash **) p2;
87
88   if( ((*name1)->flags & SYM_INVALID) != 0 )
89     {
90       return -1;
91     }
92
93   if( ((*name2)->flags & SYM_INVALID) != 0 )
94     {
95       return 1;
96     }
97
98   if( (*name1)->addr.seg > (*name2)->addr.seg )
99     {
100       return 1;
101     }
102
103   if( (*name1)->addr.seg < (*name2)->addr.seg )
104     {
105       return -1;
106     }
107
108   if( (*name1)->addr.off > (*name2)->addr.off )
109     {
110       return 1;
111     }
112
113   if( (*name1)->addr.off < (*name2)->addr.off )
114     {
115       return -1;
116     }
117
118   return 0;
119 }
120
121 /***********************************************************************
122  *           DEBUG_ResortSymbols
123  *
124  * Rebuild sorted list of symbols.
125  */
126 static
127 void
128 DEBUG_ResortSymbols()
129 {
130     struct name_hash *nh;
131     int         nsym = 0;
132     int         i;
133
134     for(i=0; i<NR_NAME_HASH; i++)
135     {
136         for (nh = name_hash_table[i]; nh; nh = nh->next)
137           {
138             nsym++;
139           }
140     }
141
142     sorttab_nsym = nsym;
143     if( nsym == 0 )
144       {
145         return;
146       }
147
148     addr_sorttab = (struct name_hash **) xrealloc(addr_sorttab, 
149                                          nsym * sizeof(struct name_hash *));
150
151     nsym = 0;
152     for(i=0; i<NR_NAME_HASH; i++)
153     {
154         for (nh = name_hash_table[i]; nh; nh = nh->next)
155           {
156             addr_sorttab[nsym++] = nh;
157           }
158     }
159
160     qsort(addr_sorttab, nsym,
161           sizeof(struct name_hash *), DEBUG_cmp_sym);
162     sortlist_valid = TRUE;
163
164 }
165
166 /***********************************************************************
167  *           DEBUG_AddSymbol
168  *
169  * Add a symbol to the table.
170  */
171 struct name_hash *
172 DEBUG_AddSymbol( const char * name, const DBG_ADDR *addr, const char * source,
173                  int flags)
174 {
175     struct name_hash  * new;
176     struct name_hash *nh;
177     static char  prev_source[PATH_MAX] = {'\0', };
178     static char * prev_duped_source = NULL;
179     char * c;
180     int hash;
181
182     hash = name_hash(name);
183     for (nh = name_hash_table[hash]; nh; nh = nh->next)
184     {
185         if( ((nh->flags & SYM_INVALID) != 0) && strcmp(name, nh->name) == 0 )
186         {
187             nh->addr.off = addr->off;
188             nh->addr.seg = addr->seg;
189             if( nh->addr.type == NULL && addr->type != NULL )
190             {
191                 nh->addr.type = addr->type;
192             }
193             nh->flags &= ~SYM_INVALID;
194             return nh;
195         }
196         if (nh->addr.seg == addr->seg &&
197             nh->addr.off == addr->off &&
198             strcmp(name, nh->name) == 0 )
199         {
200             return nh;
201         }
202     }
203
204     /*
205      * First see if we already have an entry for this symbol.  If so
206      * return it, so we don't end up with duplicates.
207      */
208     
209     new = (struct name_hash *) xmalloc(sizeof(struct name_hash));
210     new->addr = *addr;
211     new->name = xstrdup(name);
212
213     if( source != NULL )
214       {
215         /*
216          * This is an enhancement to reduce memory consumption.  The idea
217          * is that we duplicate a given string only once.  This is a big
218          * win if there are lots of symbols defined in a given source file.
219          */
220         if( strcmp(source, prev_source) == 0 )
221           {
222             new->sourcefile = prev_duped_source;
223           }
224         else
225           {
226             strcpy(prev_source, source);
227             prev_duped_source = new->sourcefile = xstrdup(source);
228           }
229       }
230     else
231       {
232         new->sourcefile = NULL;
233       }
234
235     new->n_lines        = 0;
236     new->lines_alloc    = 0;
237     new->linetab        = NULL;
238
239     new->n_locals       = 0;
240     new->locals_alloc   = 0;
241     new->local_vars     = NULL;
242
243     new->flags          = flags;
244     new->next           = NULL;
245
246     /* Now insert into the hash table */
247     new->next = name_hash_table[hash];
248     name_hash_table[hash] = new;
249
250     /*
251      * Check some heuristics based upon the file name to see whether
252      * we want to step through this guy or not.  These are machine generated
253      * assembly files that are used to translate between the MS way of
254      * calling things and the GCC way of calling things.  In general we
255      * always want to step through.
256      */
257     if( source != NULL )
258       {
259         c = strrchr(source, '.');
260         if( c != NULL && strcmp(c, ".s") == 0 )
261           {
262             c = strrchr(source, '/');
263             if( c != NULL )
264               {
265                 c++;
266                 if(    (strcmp(c, "callfrom16.s") == 0)
267                     || (strcmp(c, "callto16.s") == 0)
268                     || (strcmp(c, "call32.s") == 0) )
269                   {
270                     new->flags |= SYM_TRAMPOLINE;
271                   }
272               }
273           }
274       }
275
276     sortlist_valid = FALSE;
277     return new;
278 }
279
280 BOOL32 DEBUG_Normalize(struct name_hash * nh )
281 {
282
283   /*
284    * We aren't adding any more locals or linenumbers to this function.
285    * Free any spare memory that we might have allocated.
286    */
287   if( nh == NULL )
288     {
289       return TRUE;
290     }
291
292   if( nh->n_locals != nh->locals_alloc )
293     {
294       nh->locals_alloc = nh->n_locals;
295       nh->local_vars = xrealloc(nh->local_vars,
296                                   nh->locals_alloc * sizeof(WineLocals));
297     }
298
299   if( nh->n_lines != nh->lines_alloc )
300     {
301       nh->lines_alloc = nh->n_lines;
302       nh->linetab = xrealloc(nh->linetab,
303                               nh->lines_alloc * sizeof(WineLineNo));
304     }
305
306   return TRUE;
307 }
308
309 /***********************************************************************
310  *           DEBUG_GetSymbolValue
311  *
312  * Get the address of a named symbol.
313  */
314 BOOL32 DEBUG_GetSymbolValue( const char * name, const int lineno, 
315                              DBG_ADDR *addr, int bp_flag )
316 {
317     char buffer[256];
318     struct name_hash *nh;
319
320     for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
321       {
322         if( (nh->flags & SYM_INVALID) != 0 )
323           {
324             continue;
325           }
326
327         if (!strcmp(nh->name, name)) break;
328       }
329
330     if (!nh && (name[0] != '_'))
331     {
332         buffer[0] = '_';
333         strcpy(buffer+1, name);
334         for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
335           {
336             if( (nh->flags & SYM_INVALID) != 0 )
337               {
338                 continue;
339               }
340             if (!strcmp(nh->name, buffer)) break;
341           }
342     }
343
344     /*
345      * If we don't have anything here, then try and see if this
346      * is a local symbol to the current stack frame.  No matter
347      * what, we have nothing more to do, so we let that function
348      * decide what we ultimately return.
349      */
350     if (!nh) 
351       {
352         return DEBUG_GetStackSymbolValue(name, addr);
353       }
354
355     return DEBUG_GetLineNumberAddr( nh, lineno, addr, bp_flag );
356 }
357
358 /***********************************************************************
359  *           DEBUG_GetLineNumberAddr
360  *
361  * Get the address of a named symbol.
362  */
363 BOOL32 DEBUG_GetLineNumberAddr( struct name_hash * nh, const int lineno, 
364                                 DBG_ADDR *addr, int bp_flag )
365 {
366     int i;
367
368     if( lineno == -1 )
369       {
370         *addr = nh->addr;
371         if( bp_flag )
372           {
373             addr->off += nh->breakpoint_offset;
374           }
375       }
376     else
377       {
378         /*
379          * Search for the specific line number.  If we don't find it,
380          * then return FALSE.
381          */
382         if( nh->linetab == NULL )
383           {
384             return FALSE;
385           }
386
387         for(i=0; i < nh->n_lines; i++ )
388           {
389             if( nh->linetab[i].line_number == lineno )
390               {
391                 *addr = nh->linetab[i].pc_offset;
392                 return TRUE;
393               }
394           }
395
396         /*
397          * This specific line number not found.
398          */
399         return FALSE;
400       }
401
402     return TRUE;
403 }
404
405
406 /***********************************************************************
407  *           DEBUG_SetSymbolValue
408  *
409  * Set the address of a named symbol.
410  */
411 BOOL32 DEBUG_SetSymbolValue( const char * name, const DBG_ADDR *addr )
412 {
413     char buffer[256];
414     struct name_hash *nh;
415
416     for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
417         if (!strcmp(nh->name, name)) break;
418
419     if (!nh && (name[0] != '_'))
420     {
421         buffer[0] = '_';
422         strcpy(buffer+1, name);
423         for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
424             if (!strcmp(nh->name, buffer)) break;
425     }
426
427     if (!nh) return FALSE;
428     nh->addr = *addr;
429     nh->flags &= SYM_INVALID;
430     DBG_FIX_ADDR_SEG( &nh->addr, DS_reg(&DEBUG_context) );
431     return TRUE;
432 }
433
434
435 /***********************************************************************
436  *           DEBUG_FindNearestSymbol
437  *
438  * Find the symbol nearest to a given address.
439  * If ebp is specified as non-zero, it means we should dump the argument
440  * list into the string we return as well.
441  */
442 const char * DEBUG_FindNearestSymbol( const DBG_ADDR *addr, int flag,
443                                       struct name_hash ** rtn,
444                                       unsigned int ebp,
445                                       struct list_id * source)
446 {
447     static char name_buffer[MAX_PATH + 256];
448     static char arglist[1024];
449     static char argtmp[256];
450     struct name_hash * nearest = NULL;
451     int mid, high, low;
452     unsigned    int * ptr;
453     int lineno;
454     char * lineinfo, *sourcefile;
455     int i;
456     char linebuff[16];
457
458     if( rtn != NULL )
459       {
460         *rtn = NULL;
461       }
462
463     if( source != NULL )
464       {
465         source->sourcefile = NULL;
466         source->line = -1;
467       }
468
469     if( sortlist_valid == FALSE )
470       {
471         DEBUG_ResortSymbols();
472       }
473
474     if( sortlist_valid == FALSE )
475       {
476         return NULL;
477       }
478
479     /*
480      * FIXME  - use the binary search that we added to
481      * the function DEBUG_CheckLinenoStatus.  Better yet, we should
482      * probably keep some notion of the current function so we don't
483      * have to search every time.
484      */
485     /*
486      * Binary search to find closest symbol.
487      */
488     low = 0;
489     high = sorttab_nsym;
490     if( addr_sorttab[0]->addr.seg > addr->seg
491         || (   addr_sorttab[0]->addr.seg == addr->seg 
492             && addr_sorttab[0]->addr.off > addr->off) )
493       {
494         nearest = NULL;
495       }
496     else if( addr_sorttab[high - 1]->addr.seg < addr->seg
497         || (   addr_sorttab[high - 1]->addr.seg == addr->seg 
498             && addr_sorttab[high - 1]->addr.off < addr->off) )
499       {
500         nearest = addr_sorttab[high - 1];
501       }
502     else
503       {
504         while(1==1)
505           {
506             mid = (high + low)/2;
507             if( mid == low )
508               {
509                 /* 
510                  * See if there are any other entries that might also
511                  * have the same address, and would also have a line
512                  * number table.  
513                  */
514                 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
515                   {
516                     if(    (addr_sorttab[mid - 1]->addr.seg ==
517                             addr_sorttab[mid]->addr.seg)
518                         && (addr_sorttab[mid - 1]->addr.off ==
519                             addr_sorttab[mid]->addr.off)
520                         && (addr_sorttab[mid - 1]->linetab != NULL) )
521                       {
522                         mid--;
523                       }
524                   }
525
526                 if(    (mid < sorttab_nsym - 1)
527                     && (addr_sorttab[mid]->linetab == NULL) )
528                   {
529                     if(    (addr_sorttab[mid + 1]->addr.seg ==
530                             addr_sorttab[mid]->addr.seg)
531                         && (addr_sorttab[mid + 1]->addr.off ==
532                             addr_sorttab[mid]->addr.off)
533                         && (addr_sorttab[mid + 1]->linetab != NULL) )
534                       {
535                         mid++;
536                       }
537                   }
538                 nearest = addr_sorttab[mid];
539 #if 0
540                 fprintf(stderr, "Found %x:%x when looking for %x:%x %x %s\n",
541                         addr_sorttab[mid ]->addr.seg,
542                         addr_sorttab[mid ]->addr.off,
543                         addr->seg, addr->off,
544                         addr_sorttab[mid ]->linetab,
545                         addr_sorttab[mid ]->name);
546 #endif
547                 break;
548               }
549             if(    (addr_sorttab[mid]->addr.seg < addr->seg)
550                 || (   addr_sorttab[mid]->addr.seg == addr->seg 
551                     && addr_sorttab[mid]->addr.off <= addr->off) )
552               {
553                 low = mid;
554               }
555             else
556               {
557                 high = mid;
558               }
559           }
560       }
561
562     if (!nearest) return NULL;
563
564     if( rtn != NULL )
565       {
566         *rtn = nearest;
567       }
568
569     /*
570      * Fill in the relevant bits to the structure so that we can
571      * locate the source and line for this bit of code.
572      */
573     if( source != NULL )
574       {
575         source->sourcefile = nearest->sourcefile;
576         if( nearest->linetab == NULL )
577           {
578             source->line = -1;
579           }
580         else
581           {
582             source->line = nearest->linetab[0].line_number;
583           }
584       }
585
586     lineinfo = "";
587     lineno = -1;
588
589     /*
590      * Prepare to display the argument list.  If ebp is specified, it is
591      * the framepointer for the function in question.  If not specified,
592      * we don't want the arglist.
593      */
594     memset(arglist, '\0', sizeof(arglist));
595     if( ebp != 0 )
596       {
597         for(i=0; i < nearest->n_locals; i++ )
598           {
599             /*
600              * If this is a register (offset == 0) or a local
601              * variable, we don't want to know about it.
602              */
603             if( nearest->local_vars[i].offset <= 0 )
604               {
605                 continue;
606               }
607
608             ptr = (unsigned int *) (ebp + nearest->local_vars[i].offset);
609             if( arglist[0] == '\0' )
610               {
611                 arglist[0] = '(';
612               }
613             else
614               {
615                 strcat(arglist, ", ");
616               }
617
618             sprintf(argtmp, "%s=0x%x", nearest->local_vars[i].name,
619                     *ptr);
620             strcat(arglist, argtmp);
621           }
622         if( arglist[0] == '(' )
623           {
624             strcat(arglist, ")");
625           }
626       }
627
628     if( (nearest->sourcefile != NULL) && (flag == TRUE)
629         && (addr->off - nearest->addr.off < 0x100000) )
630       {
631
632         /*
633          * Try and find the nearest line number to the current offset.
634          */
635         if( nearest->linetab != NULL )
636           {
637             /*
638              * FIXME - this is an inefficient linear search.  A binary
639              * search would be better if this gets to be a performance
640              * bottleneck.
641              */
642             for(i=0; i < nearest->n_lines; i++)
643               {
644                 if( addr->off < nearest->linetab[i].pc_offset.off )
645                 {
646                   break;
647                 }
648                 lineno = nearest->linetab[i].line_number;
649               }
650           }
651
652         if( lineno != -1 )
653           {
654             sprintf(linebuff, ":%d", lineno);
655             lineinfo = linebuff;
656             if( source != NULL )
657               {
658                 source->line = lineno;
659               }
660           }
661
662         /* Remove the path from the file name */
663         sourcefile = strrchr( nearest->sourcefile, '/' );
664         if (!sourcefile) sourcefile = nearest->sourcefile;
665         else sourcefile++;
666
667         if (addr->off == nearest->addr.off)
668           sprintf( name_buffer, "%s%s [%s%s]", nearest->name, 
669                    arglist, sourcefile, lineinfo);
670         else
671           sprintf( name_buffer, "%s+0x%lx%s [%s%s]", nearest->name,
672                    addr->off - nearest->addr.off, 
673                    arglist, sourcefile, lineinfo );
674       }
675     else
676       {
677         if (addr->off == nearest->addr.off)
678           sprintf( name_buffer, "%s%s", nearest->name, arglist);
679         else {
680           if (addr->seg && (nearest->addr.seg!=addr->seg))
681               return NULL;
682           else
683               sprintf( name_buffer, "%s+0x%lx%s", nearest->name,
684                        addr->off - nearest->addr.off, arglist);
685         }
686       }
687     return name_buffer;
688 }
689
690
691 /***********************************************************************
692  *           DEBUG_ReadSymbolTable
693  *
694  * Read a symbol file into the hash table.
695  */
696 void DEBUG_ReadSymbolTable( const char * filename )
697 {
698     FILE * symbolfile;
699     DBG_ADDR addr = { 0, 0 };
700     int nargs;
701     char type;
702     char * cpnt;
703     char buffer[256];
704     char name[256];
705
706     if (!(symbolfile = fopen(filename, "r")))
707     {
708         fprintf( stderr, "Unable to open symbol table %s\n", filename );
709         return;
710     }
711
712     fprintf( stderr, "Reading symbols from file %s\n", filename );
713
714     while (1)
715     {
716         fgets( buffer, sizeof(buffer), symbolfile );
717         if (feof(symbolfile)) break;
718                 
719         /* Strip any text after a # sign (i.e. comments) */
720         cpnt = buffer;
721         while (*cpnt)
722             if(*cpnt++ == '#') { *cpnt = 0; break; }
723                 
724         /* Quietly ignore any lines that have just whitespace */
725         cpnt = buffer;
726         while(*cpnt)
727         {
728             if(*cpnt != ' ' && *cpnt != '\t') break;
729             cpnt++;
730         }
731         if (!(*cpnt) || *cpnt == '\n') continue;
732                 
733         nargs = sscanf(buffer, "%lx %c %s", &addr.off, &type, name);
734         DEBUG_AddSymbol( name, &addr, NULL, SYM_WINE );
735     }
736     fclose(symbolfile);
737 }
738
739
740 /***********************************************************************
741  *           DEBUG_LoadEntryPoints16
742  *
743  * Load the entry points of a Win16 module into the hash table.
744  */
745 static void DEBUG_LoadEntryPoints16( HMODULE16 hModule, NE_MODULE *pModule,
746                                      const char *name )
747 {
748     DBG_ADDR addr;
749     char buffer[256];
750     FARPROC16 address;
751
752     /* First search the resident names */
753
754     unsigned char *cpnt = (unsigned char *)pModule + pModule->name_table;
755     while (*cpnt)
756     {
757         cpnt += *cpnt + 1 + sizeof(WORD);
758         sprintf( buffer, "%s.%.*s", name, *cpnt, cpnt + 1 );
759         if ((address = MODULE_GetEntryPoint( hModule,
760                                              *(WORD *)(cpnt + *cpnt + 1) )))
761         {
762             addr.seg = HIWORD(address);
763             addr.off = LOWORD(address);
764             addr.type = NULL;
765             DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
766         }
767     }
768
769     /* Now search the non-resident names table */
770
771     if (!pModule->nrname_handle) return;  /* No non-resident table */
772     cpnt = (char *)GlobalLock16( pModule->nrname_handle );
773     while (*cpnt)
774     {
775         cpnt += *cpnt + 1 + sizeof(WORD);
776         sprintf( buffer, "%s.%.*s", name, *cpnt, cpnt + 1 );
777         if ((address = MODULE_GetEntryPoint( hModule,
778                                              *(WORD *)(cpnt + *cpnt + 1) )))
779         {
780             addr.seg = HIWORD(address);
781             addr.off = LOWORD(address);
782             addr.type = NULL;
783             DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
784         }
785     }
786 }
787
788
789 /***********************************************************************
790  *           DEBUG_LoadEntryPoints32
791  *
792  * Load the entry points of a Win32 module into the hash table.
793  */
794 static void DEBUG_LoadEntryPoints32( PE_MODULE *pe, const char *name )
795 {
796 #define RVA(x) (load_addr+(DWORD)(x))
797
798     DBG_ADDR addr;
799     char buffer[256];
800     int i, j;
801     IMAGE_EXPORT_DIRECTORY *exports;
802     DWORD load_addr;
803     WORD *ordinals;
804     void **functions;
805     const char **names;
806
807     PE_MODREF *pem = pCurrentProcess->modref_list;
808     while (pem && (pem->pe_module != pe)) pem = pem->next;
809     if (!pem) return;
810     load_addr = pem->load_addr;
811     exports = pem->pe_export;
812
813     addr.seg = 0;
814     addr.type = NULL;
815
816     /* Add start of DLL */
817
818     addr.off = load_addr;
819     DEBUG_AddSymbol( name, &addr, NULL, SYM_WIN32 | SYM_FUNC );
820
821     /* Add entry point */
822
823     sprintf( buffer, "%s.EntryPoint", name );
824     addr.off = RVA( pe->pe_header->OptionalHeader.AddressOfEntryPoint );
825     DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
826
827     /* Add start of sections */
828
829     for (i = 0; i < pe->pe_header->FileHeader.NumberOfSections; i++)
830     {
831         sprintf( buffer, "%s.%s", name, pe->pe_seg[i].Name );
832         addr.off = RVA( pe->pe_seg[i].VirtualAddress );
833         DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
834     }
835
836     /* Add exported functions */
837
838     if (!exports) return;
839     ordinals  = (WORD *)RVA( exports->AddressOfNameOrdinals );
840     names     = (const char **)RVA( exports->AddressOfNames );
841     functions = (void **)RVA( exports->AddressOfFunctions );
842
843     for (i = 0; i < exports->NumberOfNames; i++)
844     {
845         if (!names[i]) continue;
846         sprintf( buffer, "%s.%s", name, (char *)RVA(names[i]) );
847         addr.off = RVA( functions[ordinals[i]] );
848         DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
849     }
850
851     for (i = 0; i < exports->NumberOfFunctions; i++)
852     {
853         if (!functions[i]) continue;
854         /* Check if we already added it with a name */
855         for (j = 0; j < exports->NumberOfNames; j++)
856             if ((ordinals[j] == i) && names[j]) break;
857         if (j < exports->NumberOfNames) continue;
858         sprintf( buffer, "%s.%ld", name, i + exports->Base );
859         addr.off = (DWORD)RVA( functions[i] );
860         DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
861     }
862 #undef RVA
863 }
864
865
866 /***********************************************************************
867  *           DEBUG_LoadEntryPoints
868  *
869  * Load the entry points of all the modules into the hash table.
870  */
871 void DEBUG_LoadEntryPoints(void)
872 {
873     MODULEENTRY entry;
874     NE_MODULE *pModule;
875     BOOL32 ok;
876
877     for (ok = ModuleFirst(&entry); ok; ok = ModuleNext(&entry))
878     {
879         if (!(pModule = MODULE_GetPtr( entry.hModule ))) continue;
880         fprintf( stderr, " %s", entry.szModule );
881
882         if (pModule->flags & NE_FFLAGS_WIN32)  /* PE module */
883         {
884             if (pModule->flags & NE_FFLAGS_BUILTIN) continue;
885             DEBUG_LoadEntryPoints32( pModule->pe_module, entry.szModule );
886         }
887         else  /* NE module */
888             DEBUG_LoadEntryPoints16( entry.hModule, pModule, entry.szModule );
889     }
890 }
891
892
893 void
894 DEBUG_AddLineNumber( struct name_hash * func, int line_num, 
895                      unsigned long offset )
896 {
897   if( func == NULL )
898     {
899       return;
900     }
901
902   if( func->n_lines + 1 >= func->lines_alloc )
903     {
904       func->lines_alloc += 64;
905       func->linetab = xrealloc(func->linetab,
906                               func->lines_alloc * sizeof(WineLineNo));
907     }
908
909   func->linetab[func->n_lines].line_number = line_num;
910   func->linetab[func->n_lines].pc_offset.seg = func->addr.seg;
911   func->linetab[func->n_lines].pc_offset.off = func->addr.off + offset;
912   func->linetab[func->n_lines].pc_offset.type = NULL;
913   func->n_lines++;
914 }
915
916
917 struct wine_locals *
918 DEBUG_AddLocal( struct name_hash * func, int regno, 
919                 int offset,
920                 int pc_start,
921                 int pc_end,
922                 char * name)
923 {
924   if( func == NULL )
925     {
926       return NULL;
927     }
928
929   if( func->n_locals + 1 >= func->locals_alloc )
930     {
931       func->locals_alloc += 32;
932       func->local_vars = xrealloc(func->local_vars,
933                               func->locals_alloc * sizeof(WineLocals));
934     }
935
936   func->local_vars[func->n_locals].regno = regno;
937   func->local_vars[func->n_locals].offset = offset;
938   func->local_vars[func->n_locals].pc_start = pc_start;
939   func->local_vars[func->n_locals].pc_end = pc_end;
940   func->local_vars[func->n_locals].name = xstrdup(name);
941   func->local_vars[func->n_locals].type = NULL;
942   func->n_locals++;
943
944   return &func->local_vars[func->n_locals - 1];
945 }
946
947 void
948 DEBUG_DumpHashInfo()
949 {
950   int i;
951   int depth;
952   struct name_hash *nh;
953
954   /*
955    * Utility function to dump stats about the hash table.
956    */
957     for(i=0; i<NR_NAME_HASH; i++)
958     {
959       depth = 0;
960       for (nh = name_hash_table[i]; nh; nh = nh->next)
961         {
962           depth++;
963         }
964       fprintf(stderr, "Bucket %d: %d\n", i, depth);
965     }
966 }
967
968 /***********************************************************************
969  *           DEBUG_CheckLinenoStatus
970  *
971  * Find the symbol nearest to a given address.
972  * If ebp is specified as non-zero, it means we should dump the argument
973  * list into the string we return as well.
974  */
975 int DEBUG_CheckLinenoStatus( const DBG_ADDR *addr)
976 {
977     struct name_hash * nearest = NULL;
978     int mid, high, low;
979     int i;
980
981     if( sortlist_valid == FALSE )
982       {
983         DEBUG_ResortSymbols();
984       }
985
986     /*
987      * Binary search to find closest symbol.
988      */
989     low = 0;
990     high = sorttab_nsym;
991     if( addr_sorttab[0]->addr.seg > addr->seg
992         || (   addr_sorttab[0]->addr.seg == addr->seg 
993             && addr_sorttab[0]->addr.off > addr->off) )
994       {
995         nearest = NULL;
996       }
997     else if( addr_sorttab[high - 1]->addr.seg < addr->seg
998         || (   addr_sorttab[high - 1]->addr.seg == addr->seg 
999             && addr_sorttab[high - 1]->addr.off < addr->off) )
1000       {
1001         nearest = addr_sorttab[high - 1];
1002       }
1003     else
1004       {
1005         while(1==1)
1006           {
1007             mid = (high + low)/2;
1008             if( mid == low )
1009               {
1010                 /* 
1011                  * See if there are any other entries that might also
1012                  * have the same address, and would also have a line
1013                  * number table.  
1014                  */
1015                 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
1016                   {
1017                     if(    (addr_sorttab[mid - 1]->addr.seg ==
1018                             addr_sorttab[mid]->addr.seg)
1019                         && (addr_sorttab[mid - 1]->addr.off ==
1020                             addr_sorttab[mid]->addr.off)
1021                         && (addr_sorttab[mid - 1]->linetab != NULL) )
1022                       {
1023                         mid--;
1024                       }
1025                   }
1026
1027                 if(    (mid < sorttab_nsym - 1)
1028                     && (addr_sorttab[mid]->linetab == NULL) )
1029                   {
1030                     if(    (addr_sorttab[mid + 1]->addr.seg ==
1031                             addr_sorttab[mid]->addr.seg)
1032                         && (addr_sorttab[mid + 1]->addr.off ==
1033                             addr_sorttab[mid]->addr.off)
1034                         && (addr_sorttab[mid + 1]->linetab != NULL) )
1035                       {
1036                         mid++;
1037                       }
1038                   }
1039                 nearest = addr_sorttab[mid];
1040 #if 0
1041                 fprintf(stderr, "Found %x:%x when looking for %x:%x %x %s\n",
1042                         addr_sorttab[mid ]->addr.seg,
1043                         addr_sorttab[mid ]->addr.off,
1044                         addr->seg, addr->off,
1045                         addr_sorttab[mid ]->linetab,
1046                         addr_sorttab[mid ]->name);
1047 #endif
1048                 break;
1049               }
1050             if(    (addr_sorttab[mid]->addr.seg < addr->seg)
1051                 || (   addr_sorttab[mid]->addr.seg == addr->seg 
1052                     && addr_sorttab[mid]->addr.off <= addr->off) )
1053               {
1054                 low = mid;
1055               }
1056             else
1057               {
1058                 high = mid;
1059               }
1060           }
1061       }
1062
1063     if (!nearest) return FUNC_HAS_NO_LINES;
1064
1065     if( nearest->flags & SYM_STEP_THROUGH )
1066       {
1067         /*
1068          * This will cause us to keep single stepping until
1069          * we get to the other side somewhere.
1070          */
1071         return NOT_ON_LINENUMBER;
1072       }
1073
1074     if( (nearest->flags & SYM_TRAMPOLINE) )
1075       {
1076         /*
1077          * This will cause us to keep single stepping until
1078          * we get to the other side somewhere.
1079          */
1080         return FUNC_IS_TRAMPOLINE;
1081       }
1082
1083     if( nearest->linetab == NULL )
1084       {
1085         return FUNC_HAS_NO_LINES;
1086       }
1087
1088
1089     /*
1090      * We never want to stop on the first instruction of a function
1091      * even if it has it's own linenumber.  Let the thing keep running
1092      * until it gets past the function prologue.  We only do this if there
1093      * is more than one line number for the function, of course.
1094      */
1095     if( nearest->addr.off == addr->off && nearest->n_lines > 1 )
1096       {
1097         return NOT_ON_LINENUMBER;
1098       }
1099
1100     if( (nearest->sourcefile != NULL)
1101         && (addr->off - nearest->addr.off < 0x100000) )
1102       {
1103         /*
1104          * FIXME - this is an inefficient linear search.  A binary
1105          * search would be better if this gets to be a performance
1106          * bottleneck.
1107          */
1108         for(i=0; i < nearest->n_lines; i++)
1109           {
1110             if( addr->off == nearest->linetab[i].pc_offset.off )
1111               {
1112                 return AT_LINENUMBER;
1113               }
1114             if( addr->off < nearest->linetab[i].pc_offset.off )
1115               {
1116                 break;
1117               }
1118           }
1119
1120         return NOT_ON_LINENUMBER;
1121       }
1122
1123     return FUNC_HAS_NO_LINES;
1124 }
1125
1126 /***********************************************************************
1127  *           DEBUG_GetFuncInfo
1128  *
1129  * Find the symbol nearest to a given address.
1130  * Returns sourcefile name and line number in a format that the listing
1131  * handler can deal with.
1132  */
1133 void
1134 DEBUG_GetFuncInfo( struct list_id * ret, const char * filename, 
1135                    const char * name)
1136 {
1137     char buffer[256];
1138     char * pnt;
1139     struct name_hash *nh;
1140
1141     for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
1142       {
1143         if( filename != NULL )
1144           {
1145
1146             if( nh->sourcefile == NULL )
1147               {
1148                 continue;
1149               }
1150
1151             pnt = strrchr(nh->sourcefile, '/');
1152             if( strcmp(nh->sourcefile, filename) != 0
1153                 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1154               {
1155                 continue;
1156               }
1157           }
1158         if (!strcmp(nh->name, name)) break;
1159       }
1160
1161     if (!nh && (name[0] != '_'))
1162     {
1163         buffer[0] = '_';
1164         strcpy(buffer+1, name);
1165         for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
1166           {
1167             if( filename != NULL )
1168               {
1169                 if( nh->sourcefile == NULL )
1170                   {
1171                     continue;
1172                   }
1173
1174                 pnt = strrchr(nh->sourcefile, '/');
1175                 if( strcmp(nh->sourcefile, filename) != 0
1176                     && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1177                   {
1178                     continue;
1179                   }
1180               }
1181             if (!strcmp(nh->name, buffer)) break;
1182           }
1183     }
1184
1185     if( !nh )
1186       {
1187         if( filename != NULL )
1188           {
1189             fprintf(stderr, "No such function %s in %s\n", name, filename);
1190           }
1191         else
1192           {
1193             fprintf(stderr, "No such function %s\n", name);
1194           }
1195         ret->sourcefile = NULL;
1196         ret->line = -1;
1197         return;
1198       }
1199
1200     ret->sourcefile = nh->sourcefile;
1201
1202     /*
1203      * Search for the specific line number.  If we don't find it,
1204      * then return FALSE.
1205      */
1206     if( nh->linetab == NULL )
1207       {
1208         ret->line = -1;
1209       }
1210     else
1211       {
1212         ret->line = nh->linetab[0].line_number;
1213       }
1214 }
1215
1216 /***********************************************************************
1217  *           DEBUG_GetStackSymbolValue
1218  *
1219  * Get the address of a named symbol from the current stack frame.
1220  */
1221 static
1222 BOOL32 DEBUG_GetStackSymbolValue( const char * name, DBG_ADDR *addr )
1223 {
1224   struct name_hash * curr_func;
1225   unsigned int       ebp;
1226   unsigned int       eip;
1227   int                i;
1228
1229   if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1230     {
1231       return FALSE;
1232     }
1233
1234   for(i=0; i < curr_func->n_locals; i++ )
1235     {
1236       /*
1237        * Test the range of validity of the local variable.  This
1238        * comes up with RBRAC/LBRAC stabs in particular.
1239        */
1240       if(    (curr_func->local_vars[i].pc_start != 0)
1241           && ((eip - curr_func->addr.off) 
1242               < curr_func->local_vars[i].pc_start) )
1243         {
1244           continue;
1245         }
1246
1247       if(    (curr_func->local_vars[i].pc_end != 0)
1248           && ((eip - curr_func->addr.off) 
1249               > curr_func->local_vars[i].pc_end) )
1250         {
1251           continue;
1252         }
1253
1254       if( strcmp(name, curr_func->local_vars[i].name) == 0 )
1255         {
1256           /*
1257            * OK, we found it.  Now figure out what to do with this.
1258            */
1259           if( curr_func->local_vars[i].regno != 0 )
1260             {
1261               /*
1262                * Register variable.  We don't know how to treat
1263                * this yet.
1264                */
1265               return FALSE;
1266             }
1267
1268           addr->seg = 0;
1269           addr->off = ebp + curr_func->local_vars[i].offset;
1270           addr->type = curr_func->local_vars[i].type;
1271
1272           return TRUE;
1273         }
1274     }
1275   return FALSE;
1276 }
1277
1278 int
1279 DEBUG_InfoLocals()
1280 {
1281   struct name_hash  * curr_func;
1282   unsigned int        ebp;
1283   unsigned int        eip;
1284   int                 i;
1285   unsigned int      * ptr;
1286   int                 rtn = FALSE;
1287
1288   if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1289     {
1290       return FALSE;
1291     }
1292
1293   for(i=0; i < curr_func->n_locals; i++ )
1294     {
1295       /*
1296        * Test the range of validity of the local variable.  This
1297        * comes up with RBRAC/LBRAC stabs in particular.
1298        */
1299       if(    (curr_func->local_vars[i].pc_start != 0)
1300           && ((eip - curr_func->addr.off) 
1301               < curr_func->local_vars[i].pc_start) )
1302         {
1303           continue;
1304         }
1305
1306       if(    (curr_func->local_vars[i].pc_end != 0)
1307           && ((eip - curr_func->addr.off) 
1308               > curr_func->local_vars[i].pc_end) )
1309         {
1310           continue;
1311         }
1312       
1313       if( curr_func->local_vars[i].offset == 0 )
1314         {
1315           fprintf(stderr, "%s:%s optimized into register $%s \n",
1316                   curr_func->name, curr_func->local_vars[i].name,
1317                   reg_name[curr_func->local_vars[i].regno]);
1318         }
1319       else
1320         {
1321           ptr = (unsigned int *) (ebp + curr_func->local_vars[i].offset);
1322           fprintf(stderr, "%s:%s == 0x%8.8x\n",
1323                   curr_func->name, curr_func->local_vars[i].name,
1324                   *ptr);
1325         }
1326     }
1327
1328   rtn = TRUE;
1329
1330   return (rtn);
1331 }
1332
1333 int
1334 DEBUG_SetSymbolSize(struct name_hash * sym, unsigned int len)
1335 {
1336   sym->symbol_size = len;
1337
1338   return TRUE;
1339 }
1340
1341 int
1342 DEBUG_SetSymbolBPOff(struct name_hash * sym, unsigned int off)
1343 {
1344   sym->breakpoint_offset = off;
1345
1346   return TRUE;
1347 }
1348
1349 int
1350 DEBUG_GetSymbolAddr(struct name_hash * sym, DBG_ADDR * addr)
1351 {
1352
1353   *addr = sym->addr;
1354
1355   return TRUE;
1356 }
1357
1358 int DEBUG_SetLocalSymbolType(struct wine_locals * sym, struct datatype * type)
1359 {
1360   sym->type = type;
1361
1362   return TRUE;
1363 }