Support resources in cross-compiled tests.
[wine/wine-kai.git] / programs / winedbg / hash.c
blobc86ff660c408cac9fbd8259f5a8cc98a16438a5c
1 /*
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
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"
30 #define NR_NAME_HASH 16384
31 #ifndef PATH_MAX
32 #define PATH_MAX MAX_PATH
33 #endif
35 #ifdef __i386__
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)
48 #else
49 static char * reg_name[] = { NULL }; /* FIXME */
50 static unsigned reg_ofs[] = { 0 };
51 #endif
54 struct name_hash
56 struct name_hash * next; /* Used to look up within name hash */
57 char * name;
58 char * sourcefile;
60 int n_locals;
61 int locals_alloc;
62 WineLocals * local_vars;
64 int n_lines;
65 int lines_alloc;
66 WineLineNo * linetab;
68 DBG_VALUE value;
69 unsigned short flags;
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;
86 unsigned int tmp;
87 const char * p;
89 p = name;
91 while (*p)
93 hash = (hash << 4) + *p++;
95 if( (tmp = (hash & 0xf0000000)) )
97 hash ^= tmp >> 24;
99 hash &= ~tmp;
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 )
112 return -1;
115 if( ((*name2)->flags & SYM_INVALID) != 0 )
117 return 1;
120 if( (*name1)->value.addr.seg > (*name2)->value.addr.seg )
122 return 1;
125 if( (*name1)->value.addr.seg < (*name2)->value.addr.seg )
127 return -1;
130 if( (*name1)->value.addr.off > (*name2)->value.addr.off )
132 return 1;
135 if( (*name1)->value.addr.off < (*name2)->value.addr.off )
137 return -1;
140 return 0;
143 /***********************************************************************
144 * DEBUG_ResortSymbols
146 * Rebuild sorted list of symbols.
148 static
149 void
150 DEBUG_ResortSymbols(void)
152 struct name_hash *nh;
153 int nsym = 0;
154 int i;
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 )
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 );
168 sorttab_nsym = nsym;
169 if( nsym == 0 )
171 return;
174 addr_sorttab = (struct name_hash **) DBG_realloc(addr_sorttab,
175 nsym * sizeof(struct name_hash *));
177 nsym = 0;
178 for(i=0; i<NR_NAME_HASH; i++)
180 for (nh = name_hash_table[i]; nh; nh = nh->next)
182 if( (nh->flags & SYM_INVALID) == 0 )
183 addr_sorttab[nsym++] = nh;
187 qsort(addr_sorttab, nsym,
188 sizeof(struct name_hash *), DEBUG_cmp_sym);
189 sortlist_valid = TRUE;
193 /***********************************************************************
194 * DEBUG_AddSymbol
196 * Add a symbol to the table.
198 struct name_hash *
199 DEBUG_AddSymbol( const char * name, const DBG_VALUE *value,
200 const char * source, int flags)
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;
208 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
210 hash = name_hash(name);
211 for (nh = name_hash_table[hash]; nh; nh = nh->next)
213 if (name[0] == nh->name[0] && strcmp(name, nh->name) == 0)
215 int c = memcmp(&nh->value.addr, &value->addr, sizeof(value->addr));
217 if ((nh->flags & SYM_INVALID) != 0)
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,
230 if (nh->value.addr.seg == 0 && nh->value.addr.off == 0 && c != 0)
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;
238 if (nh->value.type == NULL && value->type != NULL)
240 nh->value.type = value->type;
241 nh->value.cookie = value->cookie;
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
248 if ((flags & SYM_INVALID) == 0)
249 nh->flags &= ~SYM_INVALID;
250 return nh;
252 /* don't define a symbol twice */
253 if (c == 0 && (flags & SYM_INVALID) == 0) return nh;
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
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.
267 new = (struct name_hash *) DBG_alloc(sizeof(struct name_hash));
268 new->value = *value;
269 new->name = DBG_strdup(name);
271 if( source != NULL )
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.
278 if( strcmp(source, prev_source) == 0 )
280 new->sourcefile = prev_duped_source;
282 else
284 strcpy(prev_source, source);
285 prev_duped_source = new->sourcefile = DBG_strdup(source);
288 else
290 new->sourcefile = NULL;
293 new->n_lines = 0;
294 new->lines_alloc = 0;
295 new->linetab = NULL;
297 new->n_locals = 0;
298 new->locals_alloc = 0;
299 new->local_vars = NULL;
301 new->flags = flags;
302 new->next = NULL;
304 /* Now insert into the hash table */
305 new->next = name_hash_table[hash];
306 name_hash_table[hash] = new;
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.
315 if ( source != NULL ) {
316 int len = strlen(source);
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;
327 sortlist_valid = FALSE;
328 return new;
331 BOOL DEBUG_Normalize(struct name_hash * nh )
335 * We aren't adding any more locals or linenumbers to this function.
336 * Free any spare memory that we might have allocated.
338 if( nh == NULL )
340 return TRUE;
343 if( nh->n_locals != nh->locals_alloc )
345 nh->locals_alloc = nh->n_locals;
346 nh->local_vars = DBG_realloc(nh->local_vars,
347 nh->locals_alloc * sizeof(WineLocals));
350 if( nh->n_lines != nh->lines_alloc )
352 nh->lines_alloc = nh->n_lines;
353 nh->linetab = DBG_realloc(nh->linetab,
354 nh->lines_alloc * sizeof(WineLineNo));
357 return TRUE;
360 /***********************************************************************
361 * DEBUG_GetSymbolValue
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 occured (likely, many symbols of same name exist,
368 * and user didn't pick one of them)
370 static int DEBUG_GSV_Helper(const char* name, const int lineno,
371 DBG_VALUE* value, int num, int bp_flag)
373 struct name_hash* nh;
374 int i = 0;
375 DBG_ADDR addr;
377 for (nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
379 if ((nh->flags & SYM_INVALID) != 0) continue;
380 if (!strcmp(nh->name, name) && DEBUG_GetLineNumberAddr( nh, lineno, &addr, bp_flag ))
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++;
389 return i;
392 enum get_sym_val DEBUG_GetSymbolValue( const char * name,
393 const int lineno,
394 DBG_VALUE *rtn, int bp_flag )
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;
402 num = DEBUG_GSV_Helper(name, lineno, value, NUMDBGV, bp_flag);
403 if (!num && (name[0] != '_'))
405 char buffer[512];
407 if (strlen(name) < sizeof(buffer) - 2) /* one for '_', one for '\0' */
409 buffer[0] = '_';
410 strcpy(buffer + 1, name);
411 num = DEBUG_GSV_Helper(buffer, lineno, value, NUMDBGV, bp_flag);
413 else DEBUG_Printf(DBG_CHN_WARN, "Way too long symbol (%s)\n", name);
416 /* now get the local symbols if any */
417 if (DEBUG_GetStackSymbolValue(name, &vtmp) && num < NUMDBGV)
419 value[num] = vtmp;
420 local = num;
421 num++;
424 if (num == 0) {
425 return gsv_unknown;
426 } else if (!DEBUG_InteractiveP || num == 1) {
427 i = 0;
428 } else {
429 char buffer[256];
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;
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;
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");
452 do {
453 i = 0;
454 if (DEBUG_ReadLine("=> ", buffer, sizeof(buffer)))
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);
461 } while (i < 1 || i > num);
463 /* The array is 0-based, but the choices are 1..n, so we have to subtract one before returning. */
464 i--;
466 *rtn = value[i];
467 return gsv_found;
470 /***********************************************************************
471 * DEBUG_GetLineNumberAddr
473 * Get the address of a named symbol.
475 BOOL DEBUG_GetLineNumberAddr( const struct name_hash * nh, const int lineno,
476 DBG_ADDR *addr, int bp_flag )
478 int i;
480 if( lineno == -1 )
482 *addr = nh->value.addr;
483 if( bp_flag )
485 addr->off += nh->breakpoint_offset;
488 else
491 * Search for the specific line number. If we don't find it,
492 * then return FALSE.
494 if( nh->linetab == NULL )
496 return FALSE;
499 for(i=0; i < nh->n_lines; i++ )
501 if( nh->linetab[i].line_number == lineno )
503 *addr = nh->linetab[i].pc_offset;
504 return TRUE;
509 * This specific line number not found.
511 return FALSE;
514 return TRUE;
517 /***********************************************************************
518 * DEBUG_FindNearestSymbol
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.
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)
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];
543 if( rtn != NULL )
545 *rtn = NULL;
548 if( source != NULL )
550 source->sourcefile = NULL;
551 source->line = -1;
554 if( sortlist_valid == FALSE )
556 DEBUG_ResortSymbols();
559 if( sortlist_valid == FALSE )
561 return NULL;
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.
571 * Binary search to find closest symbol.
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) )
579 nearest = NULL;
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) )
585 nearest = addr_sorttab[high - 1];
587 else
589 while(1==1)
591 mid = (high + low)/2;
592 if( mid == low )
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.
599 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
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) )
607 mid--;
611 if( (mid < sorttab_nsym - 1)
612 && (addr_sorttab[mid]->linetab == NULL) )
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) )
620 mid++;
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;
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) )
638 low = mid;
640 else
642 high = mid;
647 if (!nearest) return NULL;
649 if( rtn != NULL )
651 *rtn = nearest;
655 * Fill in the relevant bits to the structure so that we can
656 * locate the source and line for this bit of code.
658 if( source != NULL )
660 source->sourcefile = nearest->sourcefile;
661 if( nearest->linetab == NULL )
663 source->line = -1;
665 else
667 source->line = nearest->linetab[0].line_number;
671 lineinfo = "";
672 lineno = -1;
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.
679 memset(arglist, '\0', sizeof(arglist));
680 if( ebp != 0 )
682 for(i=0; i < nearest->n_locals; i++ )
685 * If this is a register (offset == 0) or a local
686 * variable, we don't want to know about it.
688 if( nearest->local_vars[i].offset <= 0 )
690 continue;
693 ptr = (unsigned int *) (ebp + nearest->local_vars[i].offset);
694 if( arglist[0] == '\0' )
696 arglist[0] = '(';
698 else
700 strcat(arglist, ", ");
702 DEBUG_READ_MEM_VERBOSE(ptr, &val, sizeof(val));
703 snprintf(argtmp, sizeof(argtmp), "%s=0x%x", nearest->local_vars[i].name, val);
705 strcat(arglist, argtmp);
707 if( arglist[0] == '(' )
709 strcat(arglist, ")");
713 module = DEBUG_FindModuleByAddr((void*)DEBUG_ToLinear(addr), DMT_UNKNOWN);
714 if (module) {
715 char* ptr = strrchr(module->module_name, '/');
717 if (!ptr++) ptr = module->module_name;
718 snprintf( modbuf, sizeof(modbuf), " in %s", ptr);
720 else
721 modbuf[0] = '\0';
723 if( (nearest->sourcefile != NULL) && (flag == TRUE)
724 && (addr->off - nearest->value.addr.off < 0x100000) )
728 * Try and find the nearest line number to the current offset.
730 if( nearest->linetab != NULL )
732 low = 0;
733 high = nearest->n_lines;
734 while ((high - low) > 1)
736 mid = (high + low) / 2;
737 if (addr->off < nearest->linetab[mid].pc_offset.off)
738 high = mid;
739 else
740 low = mid;
742 lineno = nearest->linetab[low].line_number;
745 if( lineno != -1 )
747 snprintf(linebuff, sizeof(linebuff), ":%d", lineno);
748 lineinfo = linebuff;
749 if( source != NULL )
751 source->line = lineno;
755 /* Remove the path from the file name */
756 sourcefile = strrchr( nearest->sourcefile, '/' );
757 if (!sourcefile) sourcefile = nearest->sourcefile;
758 else sourcefile++;
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 );
768 else
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);
780 return name_buffer;
784 /***********************************************************************
785 * DEBUG_ReadSymbolTable
787 * Read a symbol file into the hash table.
789 void DEBUG_ReadSymbolTable( const char* filename, unsigned long offset )
791 FILE * symbolfile;
792 DBG_VALUE value;
793 char type;
794 char * cpnt;
795 char buffer[256];
796 char name[256];
798 if (!(symbolfile = fopen(filename, "r")))
800 DEBUG_Printf( DBG_CHN_WARN, "Unable to open symbol table %s\n", filename );
801 return;
804 DEBUG_Printf( DBG_CHN_MESG, "Reading symbols from file %s\n", filename );
806 value.type = NULL;
807 value.addr.seg = 0;
808 value.addr.off = 0;
809 value.cookie = DV_TARGET;
811 while (1)
813 fgets( buffer, sizeof(buffer), symbolfile );
814 if (feof(symbolfile)) break;
816 /* Strip any text after a # sign (i.e. comments) */
817 cpnt = buffer;
818 while (*cpnt)
819 if(*cpnt++ == '#') { *cpnt = 0; break; }
821 /* Quietly ignore any lines that have just whitespace */
822 cpnt = buffer;
823 while(*cpnt)
825 if(*cpnt != ' ' && *cpnt != '\t') break;
826 cpnt++;
828 if (!(*cpnt) || *cpnt == '\n') continue;
830 if (sscanf(buffer, "%lx %c %s", &value.addr.off, &type, name) == 3)
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 );
838 fclose(symbolfile);
842 void
843 DEBUG_AddLineNumber( struct name_hash * func, int line_num,
844 unsigned long offset )
846 if( func == NULL )
848 return;
851 if( func->n_lines + 1 >= func->lines_alloc )
853 func->lines_alloc += 64;
854 func->linetab = DBG_realloc(func->linetab,
855 func->lines_alloc * sizeof(WineLineNo));
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++;
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)
872 if( func == NULL )
874 return NULL;
877 if( func->n_locals + 1 >= func->locals_alloc )
879 func->locals_alloc += 32;
880 func->local_vars = DBG_realloc(func->local_vars,
881 func->locals_alloc * sizeof(WineLocals));
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++;
892 return &func->local_vars[func->n_locals - 1];
895 void
896 DEBUG_DumpHashInfo(void)
898 int i;
899 int depth;
900 struct name_hash *nh;
903 * Utility function to dump stats about the hash table.
905 for(i=0; i<NR_NAME_HASH; i++)
907 depth = 0;
908 for (nh = name_hash_table[i]; nh; nh = nh->next)
910 depth++;
912 DEBUG_Printf(DBG_CHN_MESG, "Bucket %d: %d\n", i, depth);
916 /***********************************************************************
917 * DEBUG_CheckLinenoStatus
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.
923 int DEBUG_CheckLinenoStatus( const DBG_ADDR *addr)
925 struct name_hash * nearest = NULL;
926 int mid, high, low;
928 if( sortlist_valid == FALSE )
930 DEBUG_ResortSymbols();
934 * Binary search to find closest symbol.
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) )
942 nearest = NULL;
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) )
948 nearest = addr_sorttab[high - 1];
950 else
952 while(1==1)
954 mid = (high + low)/2;
955 if( mid == low )
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.
962 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
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) )
970 mid--;
974 if( (mid < sorttab_nsym - 1)
975 && (addr_sorttab[mid]->linetab == NULL) )
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) )
983 mid++;
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;
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) )
1001 low = mid;
1003 else
1005 high = mid;
1010 if (!nearest) return FUNC_HAS_NO_LINES;
1012 if( nearest->flags & SYM_STEP_THROUGH )
1015 * This will cause us to keep single stepping until
1016 * we get to the other side somewhere.
1018 return NOT_ON_LINENUMBER;
1021 if( (nearest->flags & SYM_TRAMPOLINE) )
1024 * This will cause us to keep single stepping until
1025 * we get to the other side somewhere.
1027 return FUNC_IS_TRAMPOLINE;
1030 if( nearest->linetab == NULL )
1032 return FUNC_HAS_NO_LINES;
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.
1042 if( nearest->value.addr.off == addr->off && nearest->n_lines > 1 )
1044 return NOT_ON_LINENUMBER;
1047 if( (nearest->sourcefile != NULL)
1048 && (addr->off - nearest->value.addr.off < 0x100000) )
1050 low = 0;
1051 high = nearest->n_lines;
1052 while ((high - low) > 1)
1054 mid = (high + low) / 2;
1055 if (addr->off < nearest->linetab[mid].pc_offset.off) high = mid;
1056 else low = mid;
1058 if (addr->off == nearest->linetab[low].pc_offset.off)
1059 return AT_LINENUMBER;
1060 else
1061 return NOT_ON_LINENUMBER;
1064 return FUNC_HAS_NO_LINES;
1067 /***********************************************************************
1068 * DEBUG_GetFuncInfo
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.
1074 void
1075 DEBUG_GetFuncInfo( struct list_id * ret, const char * filename,
1076 const char * name)
1078 char buffer[256];
1079 char * pnt;
1080 struct name_hash *nh;
1082 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
1084 if( filename != NULL )
1087 if( nh->sourcefile == NULL )
1089 continue;
1092 pnt = strrchr(nh->sourcefile, '/');
1093 if( strcmp(nh->sourcefile, filename) != 0
1094 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1096 continue;
1099 if (!strcmp(nh->name, name)) break;
1102 if (!nh && (name[0] != '_'))
1104 buffer[0] = '_';
1105 strcpy(buffer+1, name);
1106 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
1108 if( filename != NULL )
1110 if( nh->sourcefile == NULL )
1112 continue;
1115 pnt = strrchr(nh->sourcefile, '/');
1116 if( strcmp(nh->sourcefile, filename) != 0
1117 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1119 continue;
1122 if (!strcmp(nh->name, buffer)) break;
1126 if( !nh )
1128 if( filename != NULL )
1130 DEBUG_Printf(DBG_CHN_MESG, "No such function %s in %s\n", name, filename);
1132 else
1134 DEBUG_Printf(DBG_CHN_MESG, "No such function %s\n", name);
1136 ret->sourcefile = NULL;
1137 ret->line = -1;
1138 return;
1141 ret->sourcefile = nh->sourcefile;
1144 * Search for the specific line number. If we don't find it,
1145 * then return FALSE.
1147 if( nh->linetab == NULL )
1149 ret->line = -1;
1151 else
1153 ret->line = nh->linetab[0].line_number;
1157 /***********************************************************************
1158 * DEBUG_GetStackSymbolValue
1160 * Get the address of a named symbol from the current stack frame.
1162 static
1163 BOOL DEBUG_GetStackSymbolValue( const char * name, DBG_VALUE *value )
1165 struct name_hash * curr_func;
1166 unsigned int ebp;
1167 unsigned int eip;
1168 int i;
1170 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1172 return FALSE;
1175 for(i=0; i < curr_func->n_locals; i++ )
1178 * Test the range of validity of the local variable. This
1179 * comes up with RBRAC/LBRAC stabs in particular.
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) )
1185 continue;
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) )
1192 continue;
1195 if( strcmp(name, curr_func->local_vars[i].name) == 0 )
1198 * OK, we found it. Now figure out what to do with this.
1200 if( curr_func->local_vars[i].regno != 0 )
1203 * Register variable. Point to DEBUG_context field.
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;
1210 else
1212 value->addr.off = ebp + curr_func->local_vars[i].offset;
1213 value->cookie = DV_TARGET;
1215 value->addr.seg = 0;
1216 value->type = curr_func->local_vars[i].type;
1218 return TRUE;
1222 return FALSE;
1226 DEBUG_InfoLocals(void)
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;
1235 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1237 return FALSE;
1240 DEBUG_Printf(DBG_CHN_MESG, "%s:\n", curr_func->name);
1242 for(i=0; i < curr_func->n_locals; i++ )
1245 * Test the range of validity of the local variable. This
1246 * comes up with RBRAC/LBRAC stabs in particular.
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) )
1252 continue;
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) )
1259 continue;
1262 DEBUG_PrintTypeCast(curr_func->local_vars[i].type);
1264 if( curr_func->local_vars[i].regno != 0 )
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);
1273 else
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);
1282 return TRUE;
1286 DEBUG_SetSymbolSize(struct name_hash * sym, unsigned int len)
1288 sym->symbol_size = len;
1290 return TRUE;
1294 DEBUG_SetSymbolBPOff(struct name_hash * sym, unsigned int off)
1296 sym->breakpoint_offset = off;
1298 return TRUE;
1302 DEBUG_GetSymbolAddr(struct name_hash * sym, DBG_ADDR * addr)
1305 *addr = sym->value.addr;
1307 return TRUE;
1310 int DEBUG_SetLocalSymbolType(struct wine_locals * sym, struct datatype * type)
1312 sym->type = type;
1314 return TRUE;
1317 #ifdef HAVE_REGEX_H
1319 static int cmp_sym_by_name(const void * p1, const void * p2)
1321 struct name_hash ** name1 = (struct name_hash **) p1;
1322 struct name_hash ** name2 = (struct name_hash **) p2;
1324 return strcmp( (*name1)->name, (*name2)->name );
1327 #include <regex.h>
1329 void DEBUG_InfoSymbols(const char* str)
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;
1340 regcomp(&preg, str, REG_NOSUB);
1342 /* grab all symbols */
1343 for (i = 0; i < NR_NAME_HASH; i++)
1345 for (nh = name_hash_table[i]; nh; nh = nh->next)
1347 if (regexec(&preg, nh->name, 0, NULL, 0) == 0)
1349 if (num_used_array == num_alloc_array)
1351 array = HeapReAlloc(GetProcessHeap(), 0, array, sizeof(*array) * (num_alloc_array += 32));
1352 if (!array) return;
1354 array[num_used_array++] = nh;
1358 regfree(&preg);
1360 /* now sort them by alphabetical order */
1361 qsort(array, num_used_array, sizeof(*array), cmp_sym_by_name);
1363 /* and display them */
1364 for (i = 0; i < num_used_array; i++)
1366 mode = DEBUG_GetSelectorType(array[i]->value.addr.seg);
1367 name = DEBUG_FindNearestSymbol( &array[i]->value.addr, TRUE,
1368 NULL, 0, NULL );
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)
1378 DEBUG_Printf( DBG_CHN_MESG, " (");
1379 DEBUG_PrintTypeCast(array[i]->value.type);
1380 DEBUG_Printf( DBG_CHN_MESG, ")");
1382 if (name) DEBUG_Printf( DBG_CHN_MESG, " %s\n", name );
1384 HeapFree(GetProcessHeap(), 0, array);
1387 #else /* HAVE_REGEX_H */
1389 void DEBUG_InfoSymbols(const char* str)
1391 DEBUG_Printf( DBG_CHN_MESG, "FIXME: needs regex support\n" );
1394 #endif /* HAVE_REGEX_H */