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