2 * File hash.c - generate hash tables for Wine debugger symbols
4 * Copyright (C) 1993, Eric Youngdale.
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.
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.
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
27 #include <sys/types.h>
30 #define NR_NAME_HASH 16384
32 #define PATH_MAX MAX_PATH
36 static char * reg_name[] =
38 "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"
41 static unsigned reg_ofs[] =
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)
49 static char * reg_name[] = { NULL }; /* FIXME */
50 static unsigned reg_ofs[] = { 0 };
56 struct name_hash * next; /* Used to look up within name hash */
62 WineLocals * local_vars;
70 unsigned short breakpoint_offset;
71 unsigned int symbol_size;
75 static BOOL DEBUG_GetStackSymbolValue( const char * name, DBG_VALUE *value );
76 static int sortlist_valid = FALSE;
78 static int sorttab_nsym;
79 static struct name_hash ** addr_sorttab = NULL;
81 static struct name_hash * name_hash_table[NR_NAME_HASH];
83 static unsigned int name_hash( const char * name )
85 unsigned int hash = 0;
93 hash = (hash << 4) + *p++;
95 if( (tmp = (hash & 0xf0000000)) )
101 return hash % NR_NAME_HASH;
105 DEBUG_cmp_sym(const void * p1, const void * p2)
107 struct name_hash ** name1 = (struct name_hash **) p1;
108 struct name_hash ** name2 = (struct name_hash **) p2;
110 if( ((*name1)->flags & SYM_INVALID) != 0 )
115 if( ((*name2)->flags & SYM_INVALID) != 0 )
120 if( (*name1)->value.addr.seg > (*name2)->value.addr.seg )
125 if( (*name1)->value.addr.seg < (*name2)->value.addr.seg )
130 if( (*name1)->value.addr.off > (*name2)->value.addr.off )
135 if( (*name1)->value.addr.off < (*name2)->value.addr.off )
143 /***********************************************************************
144 * DEBUG_ResortSymbols
146 * Rebuild sorted list of symbols.
150 DEBUG_ResortSymbols(void)
152 struct name_hash *nh;
156 for(i=0; i<NR_NAME_HASH; i++)
158 for (nh = name_hash_table[i]; nh; nh = nh->next)
160 if( (nh->flags & SYM_INVALID) == 0 )
163 DEBUG_Printf( DBG_CHN_MESG, "Symbol %s is invalid\n", nh->name );
173 addr_sorttab = (struct name_hash **) DBG_realloc(addr_sorttab,
174 nsym * sizeof(struct name_hash *));
177 for(i=0; i<NR_NAME_HASH; i++)
179 for (nh = name_hash_table[i]; nh; nh = nh->next)
181 if( (nh->flags & SYM_INVALID) == 0 )
182 addr_sorttab[nsym++] = nh;
186 qsort(addr_sorttab, nsym,
187 sizeof(struct name_hash *), DEBUG_cmp_sym);
188 sortlist_valid = TRUE;
192 /***********************************************************************
195 * Add a symbol to the table.
198 DEBUG_AddSymbol( const char * name, const DBG_VALUE *value,
199 const char * source, int flags)
201 struct name_hash * new;
202 struct name_hash *nh;
203 static char prev_source[PATH_MAX] = {'\0', };
204 static char * prev_duped_source = NULL;
207 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
209 hash = name_hash(name);
210 for (nh = name_hash_table[hash]; nh; nh = nh->next)
212 if( ((nh->flags & SYM_INVALID) != 0) && strcmp(name, nh->name) == 0 )
215 DEBUG_Printf(DBG_CHN_MESG, "Changing address for symbol %s (%08lx:%08lx => %08lx:%08lx)\n",
216 name, nh->value.addr.seg, nh->value.addr.off, value->addr.seg, value->addr.off);
218 nh->value.addr = value->addr;
219 if( nh->value.type == NULL && value->type != NULL )
221 nh->value.type = value->type;
222 nh->value.cookie = value->cookie;
224 /* it may happen that the same symbol is defined in several compilation
225 * units, but the linker decides to merge it into a single instance.
226 * in that case, we don't clear the invalid flag for all the compilation
227 * units (N_GSYM), and wait to get the symbol from the symtab
229 if ((flags & SYM_INVALID) == 0)
230 nh->flags &= ~SYM_INVALID;
234 if (nh->value.addr.seg == value->addr.seg &&
235 nh->value.addr.off == value->addr.off &&
236 strcmp(name, nh->name) == 0 )
243 DEBUG_Printf(DBG_CHN_TRACE, "adding symbol (%s) from file '%s' at 0x%04lx:%08lx\n",
244 name, source, value->addr.seg, value->addr.off);
248 * First see if we already have an entry for this symbol. If so
249 * return it, so we don't end up with duplicates.
252 new = (struct name_hash *) DBG_alloc(sizeof(struct name_hash));
254 new->name = DBG_strdup(name);
259 * This is an enhancement to reduce memory consumption. The idea
260 * is that we duplicate a given string only once. This is a big
261 * win if there are lots of symbols defined in a given source file.
263 if( strcmp(source, prev_source) == 0 )
265 new->sourcefile = prev_duped_source;
269 strcpy(prev_source, source);
270 prev_duped_source = new->sourcefile = DBG_strdup(source);
275 new->sourcefile = NULL;
279 new->lines_alloc = 0;
283 new->locals_alloc = 0;
284 new->local_vars = NULL;
289 /* Now insert into the hash table */
290 new->next = name_hash_table[hash];
291 name_hash_table[hash] = new;
294 * Check some heuristics based upon the file name to see whether
295 * we want to step through this guy or not. These are machine generated
296 * assembly files that are used to translate between the MS way of
297 * calling things and the GCC way of calling things. In general we
298 * always want to step through.
300 if ( source != NULL ) {
301 int len = strlen(source);
303 if (len > 2 && source[len-2] == '.' && source[len-1] == 's') {
304 char* c = strrchr(source - 2, '/');
306 if (strcmp(c + 1, "asmrelay.s") == 0)
307 new->flags |= SYM_TRAMPOLINE;
312 sortlist_valid = FALSE;
316 BOOL DEBUG_Normalize(struct name_hash * nh )
320 * We aren't adding any more locals or linenumbers to this function.
321 * Free any spare memory that we might have allocated.
328 if( nh->n_locals != nh->locals_alloc )
330 nh->locals_alloc = nh->n_locals;
331 nh->local_vars = DBG_realloc(nh->local_vars,
332 nh->locals_alloc * sizeof(WineLocals));
335 if( nh->n_lines != nh->lines_alloc )
337 nh->lines_alloc = nh->n_lines;
338 nh->linetab = DBG_realloc(nh->linetab,
339 nh->lines_alloc * sizeof(WineLineNo));
345 /***********************************************************************
346 * DEBUG_GetSymbolValue
348 * Get the address of a named symbol.
350 * gsv_found: if the symbol is found
351 * gsv_unknown: if the symbol isn't found
352 * gsv_aborted: some error occured (likely, many symbols of same name exist,
353 * and user didn't pick one of them)
355 static int DEBUG_GSV_Helper(const char* name, const int lineno,
356 DBG_VALUE* value, int num, int bp_flag)
358 struct name_hash* nh;
362 for (nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
364 if ((nh->flags & SYM_INVALID) != 0) continue;
365 if (!strcmp(nh->name, name) && DEBUG_GetLineNumberAddr( nh, lineno, &addr, bp_flag ))
367 if (i >= num) return num + 1;
368 value[i].addr = addr;
369 value[i].type = nh->value.type;
370 value[i].cookie = nh->value.cookie;
377 enum get_sym_val DEBUG_GetSymbolValue( const char * name,
379 DBG_VALUE *rtn, int bp_flag )
382 /* FIXME: NUMDBGV should be made variable */
383 DBG_VALUE value[NUMDBGV];
385 int num, i, local = -1;
387 num = DEBUG_GSV_Helper(name, lineno, value, NUMDBGV, bp_flag);
388 if (!num && (name[0] != '_'))
392 if (strlen(name) < sizeof(buffer) - 2) /* one for '_', one for '\0' */
395 strcpy(buffer + 1, name);
396 num = DEBUG_GSV_Helper(buffer, lineno, value, NUMDBGV, bp_flag);
398 else DEBUG_Printf(DBG_CHN_WARN, "Way too long symbol (%s)\n", name);
401 /* now get the local symbols if any */
402 if (DEBUG_GetStackSymbolValue(name, &vtmp) && num < NUMDBGV)
411 } else if (!DEBUG_InteractiveP || num == 1) {
416 if (num == NUMDBGV+1) {
417 DEBUG_Printf(DBG_CHN_MESG, "Too many addresses for symbol '%s', limiting the first %d\n", name, NUMDBGV);
420 DEBUG_Printf(DBG_CHN_MESG, "Many symbols with name '%s', choose the one you want (<cr> to abort):\n", name);
421 for (i = 0; i < num; i++) {
422 DEBUG_Printf(DBG_CHN_MESG, "[%d]: ", i + 1);
424 struct name_hash*func;
428 if (DEBUG_GetCurrentFrame(&func, &eip, &ebp))
429 DEBUG_Printf(DBG_CHN_MESG, "local variable of %s in %s\n", func->name, func->sourcefile);
431 DEBUG_Printf(DBG_CHN_MESG, "local variable\n");
433 DEBUG_PrintAddress( &value[i].addr, DEBUG_GetSelectorType(value[i].addr.seg), TRUE);
434 DEBUG_Printf(DBG_CHN_MESG, "\n");
439 if (DEBUG_ReadLine("=> ", buffer, sizeof(buffer)))
441 if (buffer[0] == '\0') return gsv_aborted;
443 if (i < 1 || i > num)
444 DEBUG_Printf(DBG_CHN_MESG, "Invalid choice %d\n", i);
446 } while (i < 1 || i > num);
448 /* The array is 0-based, but the choices are 1..n, so we have to subtract one before returning. */
455 /***********************************************************************
456 * DEBUG_GetLineNumberAddr
458 * Get the address of a named symbol.
460 BOOL DEBUG_GetLineNumberAddr( const struct name_hash * nh, const int lineno,
461 DBG_ADDR *addr, int bp_flag )
467 *addr = nh->value.addr;
470 addr->off += nh->breakpoint_offset;
476 * Search for the specific line number. If we don't find it,
479 if( nh->linetab == NULL )
484 for(i=0; i < nh->n_lines; i++ )
486 if( nh->linetab[i].line_number == lineno )
488 *addr = nh->linetab[i].pc_offset;
494 * This specific line number not found.
503 /***********************************************************************
504 * DEBUG_SetSymbolValue
506 * Set the address of a named symbol.
508 BOOL DEBUG_SetSymbolValue( const char * name, const DBG_VALUE *value )
511 struct name_hash *nh;
513 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
515 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
516 if (!strcmp(nh->name, name)) break;
518 if (!nh && (name[0] != '_'))
521 strcpy(buffer+1, name);
522 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
523 if (!strcmp(nh->name, buffer)) break;
526 if (!nh) return FALSE;
528 nh->flags &= ~SYM_INVALID;
531 DEBUG_FixAddress( &nh->value.addr, DEBUG_context.SegDs );
538 /***********************************************************************
539 * DEBUG_FindNearestSymbol
541 * Find the symbol nearest to a given address.
542 * If ebp is specified as non-zero, it means we should dump the argument
543 * list into the string we return as well.
545 const char * DEBUG_FindNearestSymbol( const DBG_ADDR *addr, int flag,
546 struct name_hash ** rtn,
548 struct list_id * source)
550 static char name_buffer[MAX_PATH + 256];
551 static char arglist[1024];
552 static char argtmp[256];
553 struct name_hash * nearest = NULL;
557 char * lineinfo, *sourcefile;
571 source->sourcefile = NULL;
575 if( sortlist_valid == FALSE )
577 DEBUG_ResortSymbols();
580 if( sortlist_valid == FALSE )
586 * FIXME - use the binary search that we added to
587 * the function DEBUG_CheckLinenoStatus. Better yet, we should
588 * probably keep some notion of the current function so we don't
589 * have to search every time.
592 * Binary search to find closest symbol.
596 if( addr_sorttab[0]->value.addr.seg > addr->seg
597 || ( addr_sorttab[0]->value.addr.seg == addr->seg
598 && addr_sorttab[0]->value.addr.off > addr->off) )
602 else if( addr_sorttab[high - 1]->value.addr.seg < addr->seg
603 || ( addr_sorttab[high - 1]->value.addr.seg == addr->seg
604 && addr_sorttab[high - 1]->value.addr.off < addr->off) )
606 nearest = addr_sorttab[high - 1];
612 mid = (high + low)/2;
616 * See if there are any other entries that might also
617 * have the same address, and would also have a line
620 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
622 if( (addr_sorttab[mid - 1]->value.addr.seg ==
623 addr_sorttab[mid]->value.addr.seg)
624 && (addr_sorttab[mid - 1]->value.addr.off ==
625 addr_sorttab[mid]->value.addr.off)
626 && (addr_sorttab[mid - 1]->linetab != NULL) )
632 if( (mid < sorttab_nsym - 1)
633 && (addr_sorttab[mid]->linetab == NULL) )
635 if( (addr_sorttab[mid + 1]->value.addr.seg ==
636 addr_sorttab[mid]->value.addr.seg)
637 && (addr_sorttab[mid + 1]->value.addr.off ==
638 addr_sorttab[mid]->value.addr.off)
639 && (addr_sorttab[mid + 1]->linetab != NULL) )
644 nearest = addr_sorttab[mid];
646 DEBUG_Printf(DBG_CHN_MESG, "Found %x:%x when looking for %x:%x %x %s\n",
647 addr_sorttab[mid ]->value.addr.seg,
648 addr_sorttab[mid ]->value.addr.off,
649 addr->seg, addr->off,
650 addr_sorttab[mid ]->linetab,
651 addr_sorttab[mid ]->name);
655 if( (addr_sorttab[mid]->value.addr.seg < addr->seg)
656 || ( addr_sorttab[mid]->value.addr.seg == addr->seg
657 && addr_sorttab[mid]->value.addr.off <= addr->off) )
668 if (!nearest) return NULL;
676 * Fill in the relevant bits to the structure so that we can
677 * locate the source and line for this bit of code.
681 source->sourcefile = nearest->sourcefile;
682 if( nearest->linetab == NULL )
688 source->line = nearest->linetab[0].line_number;
696 * Prepare to display the argument list. If ebp is specified, it is
697 * the framepointer for the function in question. If not specified,
698 * we don't want the arglist.
700 memset(arglist, '\0', sizeof(arglist));
703 for(i=0; i < nearest->n_locals; i++ )
706 * If this is a register (offset == 0) or a local
707 * variable, we don't want to know about it.
709 if( nearest->local_vars[i].offset <= 0 )
714 ptr = (unsigned int *) (ebp + nearest->local_vars[i].offset);
715 if( arglist[0] == '\0' )
721 strcat(arglist, ", ");
723 DEBUG_READ_MEM_VERBOSE(ptr, &val, sizeof(val));
724 sprintf(argtmp, "%s=0x%x", nearest->local_vars[i].name, val);
726 strcat(arglist, argtmp);
728 if( arglist[0] == '(' )
730 strcat(arglist, ")");
734 module = DEBUG_FindModuleByAddr((void*)DEBUG_ToLinear(addr), DMT_UNKNOWN);
736 char* ptr = strrchr(module->module_name, '/');
738 if (!ptr++) ptr = module->module_name;
739 sprintf( modbuf, " in %s", ptr);
744 if( (nearest->sourcefile != NULL) && (flag == TRUE)
745 && (addr->off - nearest->value.addr.off < 0x100000) )
749 * Try and find the nearest line number to the current offset.
751 if( nearest->linetab != NULL )
754 high = nearest->n_lines;
755 while ((high - low) > 1)
757 mid = (high + low) / 2;
758 if (addr->off < nearest->linetab[mid].pc_offset.off)
763 lineno = nearest->linetab[low].line_number;
768 sprintf(linebuff, ":%d", lineno);
772 source->line = lineno;
776 /* Remove the path from the file name */
777 sourcefile = strrchr( nearest->sourcefile, '/' );
778 if (!sourcefile) sourcefile = nearest->sourcefile;
781 if (addr->off == nearest->value.addr.off)
782 sprintf( name_buffer, "%s%s [%s%s]%s", nearest->name,
783 arglist, sourcefile, lineinfo, modbuf);
785 sprintf( name_buffer, "%s+0x%lx%s [%s%s]%s", nearest->name,
786 addr->off - nearest->value.addr.off,
787 arglist, sourcefile, lineinfo, modbuf );
791 if (addr->off == nearest->value.addr.off)
792 sprintf( name_buffer, "%s%s%s", nearest->name, arglist, modbuf);
794 if (addr->seg && (nearest->value.addr.seg!=addr->seg))
797 sprintf( name_buffer, "%s+0x%lx%s%s", nearest->name,
798 addr->off - nearest->value.addr.off, arglist, modbuf);
805 /***********************************************************************
806 * DEBUG_ReadSymbolTable
808 * Read a symbol file into the hash table.
810 void DEBUG_ReadSymbolTable( const char* filename, unsigned long offset )
819 if (!(symbolfile = fopen(filename, "r")))
821 DEBUG_Printf( DBG_CHN_WARN, "Unable to open symbol table %s\n", filename );
825 DEBUG_Printf( DBG_CHN_MESG, "Reading symbols from file %s\n", filename );
830 value.cookie = DV_TARGET;
834 fgets( buffer, sizeof(buffer), symbolfile );
835 if (feof(symbolfile)) break;
837 /* Strip any text after a # sign (i.e. comments) */
840 if(*cpnt++ == '#') { *cpnt = 0; break; }
842 /* Quietly ignore any lines that have just whitespace */
846 if(*cpnt != ' ' && *cpnt != '\t') break;
849 if (!(*cpnt) || *cpnt == '\n') continue;
851 if (sscanf(buffer, "%lx %c %s", &value.addr.off, &type, name) == 3)
853 if (value.addr.off + offset < value.addr.off)
854 DEBUG_Printf( DBG_CHN_WARN, "Address wrap around\n");
855 value.addr.off += offset;
856 DEBUG_AddSymbol( name, &value, NULL, SYM_WINE );
864 DEBUG_AddLineNumber( struct name_hash * func, int line_num,
865 unsigned long offset )
872 if( func->n_lines + 1 >= func->lines_alloc )
874 func->lines_alloc += 64;
875 func->linetab = DBG_realloc(func->linetab,
876 func->lines_alloc * sizeof(WineLineNo));
879 func->linetab[func->n_lines].line_number = line_num;
880 func->linetab[func->n_lines].pc_offset.seg = func->value.addr.seg;
881 func->linetab[func->n_lines].pc_offset.off = func->value.addr.off + offset;
887 DEBUG_AddLocal( struct name_hash * func, int regno,
898 if( func->n_locals + 1 >= func->locals_alloc )
900 func->locals_alloc += 32;
901 func->local_vars = DBG_realloc(func->local_vars,
902 func->locals_alloc * sizeof(WineLocals));
905 func->local_vars[func->n_locals].regno = regno;
906 func->local_vars[func->n_locals].offset = offset;
907 func->local_vars[func->n_locals].pc_start = pc_start;
908 func->local_vars[func->n_locals].pc_end = pc_end;
909 func->local_vars[func->n_locals].name = DBG_strdup(name);
910 func->local_vars[func->n_locals].type = NULL;
913 return &func->local_vars[func->n_locals - 1];
917 DEBUG_DumpHashInfo(void)
921 struct name_hash *nh;
924 * Utility function to dump stats about the hash table.
926 for(i=0; i<NR_NAME_HASH; i++)
929 for (nh = name_hash_table[i]; nh; nh = nh->next)
933 DEBUG_Printf(DBG_CHN_MESG, "Bucket %d: %d\n", i, depth);
937 /***********************************************************************
938 * DEBUG_CheckLinenoStatus
940 * Find the symbol nearest to a given address.
941 * If ebp is specified as non-zero, it means we should dump the argument
942 * list into the string we return as well.
944 int DEBUG_CheckLinenoStatus( const DBG_ADDR *addr)
946 struct name_hash * nearest = NULL;
949 if( sortlist_valid == FALSE )
951 DEBUG_ResortSymbols();
955 * Binary search to find closest symbol.
959 if( addr_sorttab[0]->value.addr.seg > addr->seg
960 || ( addr_sorttab[0]->value.addr.seg == addr->seg
961 && addr_sorttab[0]->value.addr.off > addr->off) )
965 else if( addr_sorttab[high - 1]->value.addr.seg < addr->seg
966 || ( addr_sorttab[high - 1]->value.addr.seg == addr->seg
967 && addr_sorttab[high - 1]->value.addr.off < addr->off) )
969 nearest = addr_sorttab[high - 1];
975 mid = (high + low)/2;
979 * See if there are any other entries that might also
980 * have the same address, and would also have a line
983 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
985 if( (addr_sorttab[mid - 1]->value.addr.seg ==
986 addr_sorttab[mid]->value.addr.seg)
987 && (addr_sorttab[mid - 1]->value.addr.off ==
988 addr_sorttab[mid]->value.addr.off)
989 && (addr_sorttab[mid - 1]->linetab != NULL) )
995 if( (mid < sorttab_nsym - 1)
996 && (addr_sorttab[mid]->linetab == NULL) )
998 if( (addr_sorttab[mid + 1]->value.addr.seg ==
999 addr_sorttab[mid]->value.addr.seg)
1000 && (addr_sorttab[mid + 1]->value.addr.off ==
1001 addr_sorttab[mid]->value.addr.off)
1002 && (addr_sorttab[mid + 1]->linetab != NULL) )
1007 nearest = addr_sorttab[mid];
1009 DEBUG_Printf(DBG_CHN_MESG, "Found %x:%x when looking for %x:%x %x %s\n",
1010 addr_sorttab[mid ]->value.addr.seg,
1011 addr_sorttab[mid ]->value.addr.off,
1012 addr->seg, addr->off,
1013 addr_sorttab[mid ]->linetab,
1014 addr_sorttab[mid ]->name);
1018 if( (addr_sorttab[mid]->value.addr.seg < addr->seg)
1019 || ( addr_sorttab[mid]->value.addr.seg == addr->seg
1020 && addr_sorttab[mid]->value.addr.off <= addr->off) )
1031 if (!nearest) return FUNC_HAS_NO_LINES;
1033 if( nearest->flags & SYM_STEP_THROUGH )
1036 * This will cause us to keep single stepping until
1037 * we get to the other side somewhere.
1039 return NOT_ON_LINENUMBER;
1042 if( (nearest->flags & SYM_TRAMPOLINE) )
1045 * This will cause us to keep single stepping until
1046 * we get to the other side somewhere.
1048 return FUNC_IS_TRAMPOLINE;
1051 if( nearest->linetab == NULL )
1053 return FUNC_HAS_NO_LINES;
1058 * We never want to stop on the first instruction of a function
1059 * even if it has it's own linenumber. Let the thing keep running
1060 * until it gets past the function prologue. We only do this if there
1061 * is more than one line number for the function, of course.
1063 if( nearest->value.addr.off == addr->off && nearest->n_lines > 1 )
1065 return NOT_ON_LINENUMBER;
1068 if( (nearest->sourcefile != NULL)
1069 && (addr->off - nearest->value.addr.off < 0x100000) )
1072 high = nearest->n_lines;
1073 while ((high - low) > 1)
1075 mid = (high + low) / 2;
1076 if (addr->off < nearest->linetab[mid].pc_offset.off) high = mid;
1079 if (addr->off == nearest->linetab[low].pc_offset.off)
1080 return AT_LINENUMBER;
1082 return NOT_ON_LINENUMBER;
1085 return FUNC_HAS_NO_LINES;
1088 /***********************************************************************
1091 * Find the symbol nearest to a given address.
1092 * Returns sourcefile name and line number in a format that the listing
1093 * handler can deal with.
1096 DEBUG_GetFuncInfo( struct list_id * ret, const char * filename,
1101 struct name_hash *nh;
1103 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
1105 if( filename != NULL )
1108 if( nh->sourcefile == NULL )
1113 pnt = strrchr(nh->sourcefile, '/');
1114 if( strcmp(nh->sourcefile, filename) != 0
1115 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1120 if (!strcmp(nh->name, name)) break;
1123 if (!nh && (name[0] != '_'))
1126 strcpy(buffer+1, name);
1127 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
1129 if( filename != NULL )
1131 if( nh->sourcefile == NULL )
1136 pnt = strrchr(nh->sourcefile, '/');
1137 if( strcmp(nh->sourcefile, filename) != 0
1138 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1143 if (!strcmp(nh->name, buffer)) break;
1149 if( filename != NULL )
1151 DEBUG_Printf(DBG_CHN_MESG, "No such function %s in %s\n", name, filename);
1155 DEBUG_Printf(DBG_CHN_MESG, "No such function %s\n", name);
1157 ret->sourcefile = NULL;
1162 ret->sourcefile = nh->sourcefile;
1165 * Search for the specific line number. If we don't find it,
1166 * then return FALSE.
1168 if( nh->linetab == NULL )
1174 ret->line = nh->linetab[0].line_number;
1178 /***********************************************************************
1179 * DEBUG_GetStackSymbolValue
1181 * Get the address of a named symbol from the current stack frame.
1184 BOOL DEBUG_GetStackSymbolValue( const char * name, DBG_VALUE *value )
1186 struct name_hash * curr_func;
1191 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1196 for(i=0; i < curr_func->n_locals; i++ )
1199 * Test the range of validity of the local variable. This
1200 * comes up with RBRAC/LBRAC stabs in particular.
1202 if( (curr_func->local_vars[i].pc_start != 0)
1203 && ((eip - curr_func->value.addr.off)
1204 < curr_func->local_vars[i].pc_start) )
1209 if( (curr_func->local_vars[i].pc_end != 0)
1210 && ((eip - curr_func->value.addr.off)
1211 > curr_func->local_vars[i].pc_end) )
1216 if( strcmp(name, curr_func->local_vars[i].name) == 0 )
1219 * OK, we found it. Now figure out what to do with this.
1221 if( curr_func->local_vars[i].regno != 0 )
1224 * Register variable. Point to DEBUG_context field.
1226 assert(curr_func->local_vars[i].regno - 1 < sizeof(reg_ofs)/sizeof(reg_ofs[0]));
1227 value->addr.off = ((DWORD)&DEBUG_context) +
1228 reg_ofs[curr_func->local_vars[i].regno - 1];
1229 value->cookie = DV_HOST;
1233 value->addr.off = ebp + curr_func->local_vars[i].offset;
1234 value->cookie = DV_TARGET;
1236 value->addr.seg = 0;
1237 value->type = curr_func->local_vars[i].type;
1247 DEBUG_InfoLocals(void)
1249 struct name_hash * curr_func;
1256 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1261 DEBUG_Printf(DBG_CHN_MESG, "%s:\n", curr_func->name);
1263 for(i=0; i < curr_func->n_locals; i++ )
1266 * Test the range of validity of the local variable. This
1267 * comes up with RBRAC/LBRAC stabs in particular.
1269 if( (curr_func->local_vars[i].pc_start != 0)
1270 && ((eip - curr_func->value.addr.off)
1271 < curr_func->local_vars[i].pc_start) )
1276 if( (curr_func->local_vars[i].pc_end != 0)
1277 && ((eip - curr_func->value.addr.off)
1278 > curr_func->local_vars[i].pc_end) )
1283 DEBUG_PrintTypeCast(curr_func->local_vars[i].type);
1285 if( curr_func->local_vars[i].regno != 0 )
1287 ptr = (unsigned int *)(((DWORD)&DEBUG_context)
1288 + reg_ofs[curr_func->local_vars[i].regno - 1]);
1289 DEBUG_Printf(DBG_CHN_MESG, " %s (optimized into register $%s) == 0x%8.8x\n",
1290 curr_func->local_vars[i].name,
1291 reg_name[curr_func->local_vars[i].regno - 1],
1296 DEBUG_READ_MEM_VERBOSE((void*)(ebp + curr_func->local_vars[i].offset),
1298 DEBUG_Printf(DBG_CHN_MESG, " %s == 0x%8.8x\n",
1299 curr_func->local_vars[i].name, val);
1307 DEBUG_SetSymbolSize(struct name_hash * sym, unsigned int len)
1309 sym->symbol_size = len;
1315 DEBUG_SetSymbolBPOff(struct name_hash * sym, unsigned int off)
1317 sym->breakpoint_offset = off;
1323 DEBUG_GetSymbolAddr(struct name_hash * sym, DBG_ADDR * addr)
1326 *addr = sym->value.addr;
1331 int DEBUG_SetLocalSymbolType(struct wine_locals * sym, struct datatype * type)
1340 static int cmp_sym_by_name(const void * p1, const void * p2)
1342 struct name_hash ** name1 = (struct name_hash **) p1;
1343 struct name_hash ** name2 = (struct name_hash **) p2;
1345 return strcmp( (*name1)->name, (*name2)->name );
1350 void DEBUG_InfoSymbols(const char* str)
1353 struct name_hash* nh;
1354 struct name_hash** array = NULL;
1355 unsigned num_used_array = 0;
1356 unsigned num_alloc_array = 0;
1361 regcomp(&preg, str, REG_NOSUB);
1363 /* grab all symbols */
1364 for (i = 0; i < NR_NAME_HASH; i++)
1366 for (nh = name_hash_table[i]; nh; nh = nh->next)
1368 if (regexec(&preg, nh->name, 0, NULL, 0) == 0)
1370 if (num_used_array == num_alloc_array)
1372 array = HeapReAlloc(GetProcessHeap(), 0, array, sizeof(*array) * (num_alloc_array += 32));
1375 array[num_used_array++] = nh;
1381 /* now sort them by alphabetical order */
1382 qsort(array, num_used_array, sizeof(*array), cmp_sym_by_name);
1384 /* and display them */
1385 for (i = 0; i < num_used_array; i++)
1387 mode = DEBUG_GetSelectorType(array[i]->value.addr.seg);
1388 name = DEBUG_FindNearestSymbol( &array[i]->value.addr, TRUE,
1391 if (mode != MODE_32)
1392 DEBUG_Printf( DBG_CHN_MESG, "%04lx:%04lx :",
1393 array[i]->value.addr.seg & 0xFFFF,
1394 array[i]->value.addr.off );
1396 DEBUG_Printf( DBG_CHN_MESG, "%08lx :", array[i]->value.addr.off );
1397 if (name) DEBUG_Printf( DBG_CHN_MESG, " %s\n", name );
1399 HeapFree(GetProcessHeap(), 0, array);
1402 #else /* HAVE_REGEX_H */
1404 void DEBUG_InfoSymbols(const char* str)
1406 DEBUG_Printf( DBG_CHN_MESG, "FIXME: needs regex support\n" );
1409 #endif /* HAVE_REGEX_H */