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