Fix off by one error in TOOLBAR_AddStringW.
[wine/wine-kai.git] / debugger / hash.c
blob6ea1c4799dd8d7bd2e7d81f61d652e42239e801a
1 /*
2 * File hash.c - generate hash tables for Wine debugger symbols
4 * Copyright (C) 1993, Eric Youngdale.
5 */
8 #include "config.h"
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <limits.h>
13 #include <sys/types.h>
14 #include "debugger.h"
16 #define NR_NAME_HASH 16384
17 #ifndef PATH_MAX
18 #define PATH_MAX _MAX_PATH
19 #endif
21 #ifdef __i386__
22 static char * reg_name[] =
24 "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"
27 static unsigned reg_ofs[] =
29 FIELD_OFFSET(CONTEXT, Eax), FIELD_OFFSET(CONTEXT, Ecx),
30 FIELD_OFFSET(CONTEXT, Edx), FIELD_OFFSET(CONTEXT, Ebx),
31 FIELD_OFFSET(CONTEXT, Esp), FIELD_OFFSET(CONTEXT, Ebp),
32 FIELD_OFFSET(CONTEXT, Esi), FIELD_OFFSET(CONTEXT, Edi)
34 #else
35 static char * reg_name[] = { NULL }; /* FIXME */
36 static unsigned reg_ofs[] = { 0 };
37 #endif
40 struct name_hash
42 struct name_hash * next; /* Used to look up within name hash */
43 char * name;
44 char * sourcefile;
46 int n_locals;
47 int locals_alloc;
48 WineLocals * local_vars;
50 int n_lines;
51 int lines_alloc;
52 WineLineNo * linetab;
54 DBG_VALUE value;
55 unsigned short flags;
56 unsigned short breakpoint_offset;
57 unsigned int symbol_size;
61 static BOOL DEBUG_GetStackSymbolValue( const char * name, DBG_VALUE *value );
62 static int sortlist_valid = FALSE;
64 static int sorttab_nsym;
65 static struct name_hash ** addr_sorttab = NULL;
67 static struct name_hash * name_hash_table[NR_NAME_HASH];
69 static unsigned int name_hash( const char * name )
71 unsigned int hash = 0;
72 unsigned int tmp;
73 const char * p;
75 p = name;
77 while (*p)
79 hash = (hash << 4) + *p++;
81 if( (tmp = (hash & 0xf0000000)) )
83 hash ^= tmp >> 24;
85 hash &= ~tmp;
87 return hash % NR_NAME_HASH;
90 int
91 DEBUG_cmp_sym(const void * p1, const void * p2)
93 struct name_hash ** name1 = (struct name_hash **) p1;
94 struct name_hash ** name2 = (struct name_hash **) p2;
96 if( ((*name1)->flags & SYM_INVALID) != 0 )
98 return -1;
101 if( ((*name2)->flags & SYM_INVALID) != 0 )
103 return 1;
106 if( (*name1)->value.addr.seg > (*name2)->value.addr.seg )
108 return 1;
111 if( (*name1)->value.addr.seg < (*name2)->value.addr.seg )
113 return -1;
116 if( (*name1)->value.addr.off > (*name2)->value.addr.off )
118 return 1;
121 if( (*name1)->value.addr.off < (*name2)->value.addr.off )
123 return -1;
126 return 0;
129 /***********************************************************************
130 * DEBUG_ResortSymbols
132 * Rebuild sorted list of symbols.
134 static
135 void
136 DEBUG_ResortSymbols(void)
138 struct name_hash *nh;
139 int nsym = 0;
140 int i;
142 for(i=0; i<NR_NAME_HASH; i++)
144 for (nh = name_hash_table[i]; nh; nh = nh->next)
146 if( (nh->flags & SYM_INVALID) == 0 )
147 nsym++;
148 else
149 DEBUG_Printf( DBG_CHN_MESG, "Symbol %s is invalid\n", nh->name );
153 sorttab_nsym = nsym;
154 if( nsym == 0 )
156 return;
159 addr_sorttab = (struct name_hash **) DBG_realloc(addr_sorttab,
160 nsym * sizeof(struct name_hash *));
162 nsym = 0;
163 for(i=0; i<NR_NAME_HASH; i++)
165 for (nh = name_hash_table[i]; nh; nh = nh->next)
167 if( (nh->flags & SYM_INVALID) == 0 )
168 addr_sorttab[nsym++] = nh;
172 qsort(addr_sorttab, nsym,
173 sizeof(struct name_hash *), DEBUG_cmp_sym);
174 sortlist_valid = TRUE;
178 /***********************************************************************
179 * DEBUG_AddSymbol
181 * Add a symbol to the table.
183 struct name_hash *
184 DEBUG_AddSymbol( const char * name, const DBG_VALUE *value,
185 const char * source, int flags)
187 struct name_hash * new;
188 struct name_hash *nh;
189 static char prev_source[PATH_MAX] = {'\0', };
190 static char * prev_duped_source = NULL;
191 int hash;
193 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
195 hash = name_hash(name);
196 for (nh = name_hash_table[hash]; nh; nh = nh->next)
198 if( ((nh->flags & SYM_INVALID) != 0) && strcmp(name, nh->name) == 0 )
200 #if 0
201 DEBUG_Printf(DBG_CHN_MESG, "Changing address for symbol %s (%08lx:%08lx => %08lx:%08lx)\n",
202 name, nh->value.addr.seg, nh->value.addr.off, value->addr.seg, value->addr.off);
203 #endif
204 nh->value.addr = value->addr;
205 if( nh->value.type == NULL && value->type != NULL )
207 nh->value.type = value->type;
208 nh->value.cookie = value->cookie;
210 /* it may happen that the same symbol is defined in several compilation
211 * units, but the linker decides to merge it into a single instance.
212 * in that case, we don't clear the invalid flag for all the compilation
213 * units (N_GSYM), and wait to get the symbol from the symtab
215 if ((flags & SYM_INVALID) == 0)
216 nh->flags &= ~SYM_INVALID;
218 return nh;
220 if (nh->value.addr.seg == value->addr.seg &&
221 nh->value.addr.off == value->addr.off &&
222 strcmp(name, nh->name) == 0 )
224 return nh;
228 #if 0
229 DEBUG_Printf(DBG_CHN_TRACE, "adding symbol (%s) from file '%s' at 0x%04lx:%08lx\n",
230 name, source, value->addr.seg, value->addr.off);
231 #endif
234 * First see if we already have an entry for this symbol. If so
235 * return it, so we don't end up with duplicates.
238 new = (struct name_hash *) DBG_alloc(sizeof(struct name_hash));
239 new->value = *value;
240 new->name = DBG_strdup(name);
242 if( source != NULL )
245 * This is an enhancement to reduce memory consumption. The idea
246 * is that we duplicate a given string only once. This is a big
247 * win if there are lots of symbols defined in a given source file.
249 if( strcmp(source, prev_source) == 0 )
251 new->sourcefile = prev_duped_source;
253 else
255 strcpy(prev_source, source);
256 prev_duped_source = new->sourcefile = DBG_strdup(source);
259 else
261 new->sourcefile = NULL;
264 new->n_lines = 0;
265 new->lines_alloc = 0;
266 new->linetab = NULL;
268 new->n_locals = 0;
269 new->locals_alloc = 0;
270 new->local_vars = NULL;
272 new->flags = flags;
273 new->next = NULL;
275 /* Now insert into the hash table */
276 new->next = name_hash_table[hash];
277 name_hash_table[hash] = new;
280 * Check some heuristics based upon the file name to see whether
281 * we want to step through this guy or not. These are machine generated
282 * assembly files that are used to translate between the MS way of
283 * calling things and the GCC way of calling things. In general we
284 * always want to step through.
286 if ( source != NULL ) {
287 int len = strlen(source);
289 if (len > 2 && source[len-2] == '.' && source[len-1] == 's') {
290 char* c = strrchr(source - 2, '/');
291 if (c != NULL) {
292 if (strcmp(c + 1, "asmrelay.s") == 0)
293 new->flags |= SYM_TRAMPOLINE;
298 sortlist_valid = FALSE;
299 return new;
302 BOOL DEBUG_Normalize(struct name_hash * nh )
306 * We aren't adding any more locals or linenumbers to this function.
307 * Free any spare memory that we might have allocated.
309 if( nh == NULL )
311 return TRUE;
314 if( nh->n_locals != nh->locals_alloc )
316 nh->locals_alloc = nh->n_locals;
317 nh->local_vars = DBG_realloc(nh->local_vars,
318 nh->locals_alloc * sizeof(WineLocals));
321 if( nh->n_lines != nh->lines_alloc )
323 nh->lines_alloc = nh->n_lines;
324 nh->linetab = DBG_realloc(nh->linetab,
325 nh->lines_alloc * sizeof(WineLineNo));
328 return TRUE;
331 /***********************************************************************
332 * DEBUG_GetSymbolValue
334 * Get the address of a named symbol.
336 static int DEBUG_GSV_Helper(const char* name, const int lineno,
337 DBG_VALUE* value, int num, int bp_flag)
339 struct name_hash* nh;
340 int i = 0;
341 DBG_ADDR addr;
343 for (nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
345 if ((nh->flags & SYM_INVALID) != 0) continue;
346 if (!strcmp(nh->name, name) && DEBUG_GetLineNumberAddr( nh, lineno, &addr, bp_flag ))
348 if (i >= num) return num + 1;
349 value[i].addr = addr;
350 value[i].type = nh->value.type;
351 value[i].cookie = nh->value.cookie;
352 i++;
355 return i;
358 BOOL DEBUG_GetSymbolValue( const char * name, const int lineno,
359 DBG_VALUE *rtn, int bp_flag )
361 #define NUMDBGV 10
362 /* FIXME: NUMDBGV should be made variable */
363 DBG_VALUE value[NUMDBGV];
364 DBG_VALUE vtmp;
365 int num, i;
367 num = DEBUG_GSV_Helper(name, lineno, value, NUMDBGV, bp_flag);
368 if (!num && (name[0] != '_'))
370 char buffer[512];
372 assert(strlen(name) < sizeof(buffer) - 2); /* one for '_', one for '\0' */
373 buffer[0] = '_';
374 strcpy(buffer + 1, name);
375 num = DEBUG_GSV_Helper(buffer, lineno, value, NUMDBGV, bp_flag);
378 /* now get the local symbols if any */
379 if (DEBUG_GetStackSymbolValue(name, &vtmp) && num < NUMDBGV)
381 value[num] = vtmp;
382 num++;
385 if (num == 0) {
386 return FALSE;
387 } else if (!DEBUG_interactiveP || num == 1) {
388 i = 0;
389 } else {
390 char buffer[256];
392 if (num == NUMDBGV+1) {
393 DEBUG_Printf(DBG_CHN_MESG, "Too many addresses for symbol '%s', limiting the first %d\n", name, NUMDBGV);
394 num = NUMDBGV;
396 DEBUG_Printf(DBG_CHN_MESG, "Many symbols with name '%s', choose the one you want (<cr> to abort):\n", name);
397 for (i = 0; i < num; i++) {
398 DEBUG_Printf(DBG_CHN_MESG, "[%d]: ", i + 1);
399 DEBUG_PrintAddress( &value[i].addr, DEBUG_GetSelectorType(value[i].addr.seg), TRUE);
400 DEBUG_Printf(DBG_CHN_MESG, "\n");
402 do {
403 i = 0;
404 if (DEBUG_ReadLine("=> ", buffer, sizeof(buffer), FALSE))
406 i = atoi(buffer);
407 if (i < 1 || i > num)
408 DEBUG_Printf(DBG_CHN_MESG, "Invalid choice %d\n", i);
410 } while (i < 1 || i > num);
412 /* The array is 0-based, but the choices are 1..n, so we have to subtract one before returning. */
413 i--;
415 *rtn = value[i];
416 return TRUE;
419 /***********************************************************************
420 * DEBUG_GetLineNumberAddr
422 * Get the address of a named symbol.
424 BOOL DEBUG_GetLineNumberAddr( const struct name_hash * nh, const int lineno,
425 DBG_ADDR *addr, int bp_flag )
427 int i;
429 if( lineno == -1 )
431 *addr = nh->value.addr;
432 if( bp_flag )
434 addr->off += nh->breakpoint_offset;
437 else
440 * Search for the specific line number. If we don't find it,
441 * then return FALSE.
443 if( nh->linetab == NULL )
445 return FALSE;
448 for(i=0; i < nh->n_lines; i++ )
450 if( nh->linetab[i].line_number == lineno )
452 *addr = nh->linetab[i].pc_offset;
453 return TRUE;
458 * This specific line number not found.
460 return FALSE;
463 return TRUE;
467 /***********************************************************************
468 * DEBUG_SetSymbolValue
470 * Set the address of a named symbol.
472 BOOL DEBUG_SetSymbolValue( const char * name, const DBG_VALUE *value )
474 char buffer[256];
475 struct name_hash *nh;
477 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
479 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
480 if (!strcmp(nh->name, name)) break;
482 if (!nh && (name[0] != '_'))
484 buffer[0] = '_';
485 strcpy(buffer+1, name);
486 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
487 if (!strcmp(nh->name, buffer)) break;
490 if (!nh) return FALSE;
491 nh->value = *value;
492 nh->flags &= ~SYM_INVALID;
494 #ifdef __i386__
495 DEBUG_FixAddress( &nh->value.addr, DEBUG_context.SegDs );
496 #endif
498 return TRUE;
502 /***********************************************************************
503 * DEBUG_FindNearestSymbol
505 * Find the symbol nearest to a given address.
506 * If ebp is specified as non-zero, it means we should dump the argument
507 * list into the string we return as well.
509 const char * DEBUG_FindNearestSymbol( const DBG_ADDR *addr, int flag,
510 struct name_hash ** rtn,
511 unsigned int ebp,
512 struct list_id * source)
514 static char name_buffer[MAX_PATH + 256];
515 static char arglist[1024];
516 static char argtmp[256];
517 struct name_hash * nearest = NULL;
518 int mid, high, low;
519 unsigned int * ptr;
520 int lineno;
521 char * lineinfo, *sourcefile;
522 int i;
523 char linebuff[16];
524 unsigned val;
525 DBG_MODULE* module;
526 char modbuf[256];
528 if( rtn != NULL )
530 *rtn = NULL;
533 if( source != NULL )
535 source->sourcefile = NULL;
536 source->line = -1;
539 if( sortlist_valid == FALSE )
541 DEBUG_ResortSymbols();
544 if( sortlist_valid == FALSE )
546 return NULL;
550 * FIXME - use the binary search that we added to
551 * the function DEBUG_CheckLinenoStatus. Better yet, we should
552 * probably keep some notion of the current function so we don't
553 * have to search every time.
556 * Binary search to find closest symbol.
558 low = 0;
559 high = sorttab_nsym;
560 if( addr_sorttab[0]->value.addr.seg > addr->seg
561 || ( addr_sorttab[0]->value.addr.seg == addr->seg
562 && addr_sorttab[0]->value.addr.off > addr->off) )
564 nearest = NULL;
566 else if( addr_sorttab[high - 1]->value.addr.seg < addr->seg
567 || ( addr_sorttab[high - 1]->value.addr.seg == addr->seg
568 && addr_sorttab[high - 1]->value.addr.off < addr->off) )
570 nearest = addr_sorttab[high - 1];
572 else
574 while(1==1)
576 mid = (high + low)/2;
577 if( mid == low )
580 * See if there are any other entries that might also
581 * have the same address, and would also have a line
582 * number table.
584 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
586 if( (addr_sorttab[mid - 1]->value.addr.seg ==
587 addr_sorttab[mid]->value.addr.seg)
588 && (addr_sorttab[mid - 1]->value.addr.off ==
589 addr_sorttab[mid]->value.addr.off)
590 && (addr_sorttab[mid - 1]->linetab != NULL) )
592 mid--;
596 if( (mid < sorttab_nsym - 1)
597 && (addr_sorttab[mid]->linetab == NULL) )
599 if( (addr_sorttab[mid + 1]->value.addr.seg ==
600 addr_sorttab[mid]->value.addr.seg)
601 && (addr_sorttab[mid + 1]->value.addr.off ==
602 addr_sorttab[mid]->value.addr.off)
603 && (addr_sorttab[mid + 1]->linetab != NULL) )
605 mid++;
608 nearest = addr_sorttab[mid];
609 #if 0
610 DEBUG_Printf(DBG_CHN_MESG, "Found %x:%x when looking for %x:%x %x %s\n",
611 addr_sorttab[mid ]->value.addr.seg,
612 addr_sorttab[mid ]->value.addr.off,
613 addr->seg, addr->off,
614 addr_sorttab[mid ]->linetab,
615 addr_sorttab[mid ]->name);
616 #endif
617 break;
619 if( (addr_sorttab[mid]->value.addr.seg < addr->seg)
620 || ( addr_sorttab[mid]->value.addr.seg == addr->seg
621 && addr_sorttab[mid]->value.addr.off <= addr->off) )
623 low = mid;
625 else
627 high = mid;
632 if (!nearest) return NULL;
634 if( rtn != NULL )
636 *rtn = nearest;
640 * Fill in the relevant bits to the structure so that we can
641 * locate the source and line for this bit of code.
643 if( source != NULL )
645 source->sourcefile = nearest->sourcefile;
646 if( nearest->linetab == NULL )
648 source->line = -1;
650 else
652 source->line = nearest->linetab[0].line_number;
656 lineinfo = "";
657 lineno = -1;
660 * Prepare to display the argument list. If ebp is specified, it is
661 * the framepointer for the function in question. If not specified,
662 * we don't want the arglist.
664 memset(arglist, '\0', sizeof(arglist));
665 if( ebp != 0 )
667 for(i=0; i < nearest->n_locals; i++ )
670 * If this is a register (offset == 0) or a local
671 * variable, we don't want to know about it.
673 if( nearest->local_vars[i].offset <= 0 )
675 continue;
678 ptr = (unsigned int *) (ebp + nearest->local_vars[i].offset);
679 if( arglist[0] == '\0' )
681 arglist[0] = '(';
683 else
685 strcat(arglist, ", ");
687 DEBUG_READ_MEM_VERBOSE(ptr, &val, sizeof(val));
688 sprintf(argtmp, "%s=0x%x", nearest->local_vars[i].name, val);
690 strcat(arglist, argtmp);
692 if( arglist[0] == '(' )
694 strcat(arglist, ")");
698 module = DEBUG_FindModuleByAddr((void*)DEBUG_ToLinear(addr), DMT_UNKNOWN);
699 if (module) {
700 char* ptr = strrchr(module->module_name, '/');
702 if (!ptr++) ptr = module->module_name;
703 sprintf( modbuf, " in %s", ptr);
705 else
706 modbuf[0] = '\0';
708 if( (nearest->sourcefile != NULL) && (flag == TRUE)
709 && (addr->off - nearest->value.addr.off < 0x100000) )
713 * Try and find the nearest line number to the current offset.
715 if( nearest->linetab != NULL )
717 low = 0;
718 high = nearest->n_lines;
719 while ((high - low) > 1)
721 mid = (high + low) / 2;
722 if (addr->off < nearest->linetab[mid].pc_offset.off)
723 high = mid;
724 else
725 low = mid;
727 lineno = nearest->linetab[low].line_number;
730 if( lineno != -1 )
732 sprintf(linebuff, ":%d", lineno);
733 lineinfo = linebuff;
734 if( source != NULL )
736 source->line = lineno;
740 /* Remove the path from the file name */
741 sourcefile = strrchr( nearest->sourcefile, '/' );
742 if (!sourcefile) sourcefile = nearest->sourcefile;
743 else sourcefile++;
745 if (addr->off == nearest->value.addr.off)
746 sprintf( name_buffer, "%s%s [%s%s]%s", nearest->name,
747 arglist, sourcefile, lineinfo, modbuf);
748 else
749 sprintf( name_buffer, "%s+0x%lx%s [%s%s]%s", nearest->name,
750 addr->off - nearest->value.addr.off,
751 arglist, sourcefile, lineinfo, modbuf );
753 else
755 if (addr->off == nearest->value.addr.off)
756 sprintf( name_buffer, "%s%s%s", nearest->name, arglist, modbuf);
757 else {
758 if (addr->seg && (nearest->value.addr.seg!=addr->seg))
759 return NULL;
760 else
761 sprintf( name_buffer, "%s+0x%lx%s%s", nearest->name,
762 addr->off - nearest->value.addr.off, arglist, modbuf);
765 return name_buffer;
769 /***********************************************************************
770 * DEBUG_ReadSymbolTable
772 * Read a symbol file into the hash table.
774 void DEBUG_ReadSymbolTable( const char* filename )
776 FILE * symbolfile;
777 DBG_VALUE value;
778 char type;
779 char * cpnt;
780 char buffer[256];
781 char name[256];
783 if (!(symbolfile = fopen(filename, "r")))
785 DEBUG_Printf( DBG_CHN_WARN, "Unable to open symbol table %s\n", filename );
786 return;
789 DEBUG_Printf( DBG_CHN_MESG, "Reading symbols from file %s\n", filename );
791 value.type = NULL;
792 value.addr.seg = 0;
793 value.addr.off = 0;
794 value.cookie = DV_TARGET;
796 while (1)
798 fgets( buffer, sizeof(buffer), symbolfile );
799 if (feof(symbolfile)) break;
801 /* Strip any text after a # sign (i.e. comments) */
802 cpnt = buffer;
803 while (*cpnt)
804 if(*cpnt++ == '#') { *cpnt = 0; break; }
806 /* Quietly ignore any lines that have just whitespace */
807 cpnt = buffer;
808 while(*cpnt)
810 if(*cpnt != ' ' && *cpnt != '\t') break;
811 cpnt++;
813 if (!(*cpnt) || *cpnt == '\n') continue;
815 if (sscanf(buffer, "%lx %c %s", &value.addr.off, &type, name) == 3)
816 DEBUG_AddSymbol( name, &value, NULL, SYM_WINE );
818 fclose(symbolfile);
822 void
823 DEBUG_AddLineNumber( struct name_hash * func, int line_num,
824 unsigned long offset )
826 if( func == NULL )
828 return;
831 if( func->n_lines + 1 >= func->lines_alloc )
833 func->lines_alloc += 64;
834 func->linetab = DBG_realloc(func->linetab,
835 func->lines_alloc * sizeof(WineLineNo));
838 func->linetab[func->n_lines].line_number = line_num;
839 func->linetab[func->n_lines].pc_offset.seg = func->value.addr.seg;
840 func->linetab[func->n_lines].pc_offset.off = func->value.addr.off + offset;
841 func->n_lines++;
845 struct wine_locals *
846 DEBUG_AddLocal( struct name_hash * func, int regno,
847 int offset,
848 int pc_start,
849 int pc_end,
850 char * name)
852 if( func == NULL )
854 return NULL;
857 if( func->n_locals + 1 >= func->locals_alloc )
859 func->locals_alloc += 32;
860 func->local_vars = DBG_realloc(func->local_vars,
861 func->locals_alloc * sizeof(WineLocals));
864 func->local_vars[func->n_locals].regno = regno;
865 func->local_vars[func->n_locals].offset = offset;
866 func->local_vars[func->n_locals].pc_start = pc_start;
867 func->local_vars[func->n_locals].pc_end = pc_end;
868 func->local_vars[func->n_locals].name = DBG_strdup(name);
869 func->local_vars[func->n_locals].type = NULL;
870 func->n_locals++;
872 return &func->local_vars[func->n_locals - 1];
875 void
876 DEBUG_DumpHashInfo(void)
878 int i;
879 int depth;
880 struct name_hash *nh;
883 * Utility function to dump stats about the hash table.
885 for(i=0; i<NR_NAME_HASH; i++)
887 depth = 0;
888 for (nh = name_hash_table[i]; nh; nh = nh->next)
890 depth++;
892 DEBUG_Printf(DBG_CHN_MESG, "Bucket %d: %d\n", i, depth);
896 /***********************************************************************
897 * DEBUG_CheckLinenoStatus
899 * Find the symbol nearest to a given address.
900 * If ebp is specified as non-zero, it means we should dump the argument
901 * list into the string we return as well.
903 int DEBUG_CheckLinenoStatus( const DBG_ADDR *addr)
905 struct name_hash * nearest = NULL;
906 int mid, high, low;
908 if( sortlist_valid == FALSE )
910 DEBUG_ResortSymbols();
914 * Binary search to find closest symbol.
916 low = 0;
917 high = sorttab_nsym;
918 if( addr_sorttab[0]->value.addr.seg > addr->seg
919 || ( addr_sorttab[0]->value.addr.seg == addr->seg
920 && addr_sorttab[0]->value.addr.off > addr->off) )
922 nearest = NULL;
924 else if( addr_sorttab[high - 1]->value.addr.seg < addr->seg
925 || ( addr_sorttab[high - 1]->value.addr.seg == addr->seg
926 && addr_sorttab[high - 1]->value.addr.off < addr->off) )
928 nearest = addr_sorttab[high - 1];
930 else
932 while(1==1)
934 mid = (high + low)/2;
935 if( mid == low )
938 * See if there are any other entries that might also
939 * have the same address, and would also have a line
940 * number table.
942 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
944 if( (addr_sorttab[mid - 1]->value.addr.seg ==
945 addr_sorttab[mid]->value.addr.seg)
946 && (addr_sorttab[mid - 1]->value.addr.off ==
947 addr_sorttab[mid]->value.addr.off)
948 && (addr_sorttab[mid - 1]->linetab != NULL) )
950 mid--;
954 if( (mid < sorttab_nsym - 1)
955 && (addr_sorttab[mid]->linetab == NULL) )
957 if( (addr_sorttab[mid + 1]->value.addr.seg ==
958 addr_sorttab[mid]->value.addr.seg)
959 && (addr_sorttab[mid + 1]->value.addr.off ==
960 addr_sorttab[mid]->value.addr.off)
961 && (addr_sorttab[mid + 1]->linetab != NULL) )
963 mid++;
966 nearest = addr_sorttab[mid];
967 #if 0
968 DEBUG_Printf(DBG_CHN_MESG, "Found %x:%x when looking for %x:%x %x %s\n",
969 addr_sorttab[mid ]->value.addr.seg,
970 addr_sorttab[mid ]->value.addr.off,
971 addr->seg, addr->off,
972 addr_sorttab[mid ]->linetab,
973 addr_sorttab[mid ]->name);
974 #endif
975 break;
977 if( (addr_sorttab[mid]->value.addr.seg < addr->seg)
978 || ( addr_sorttab[mid]->value.addr.seg == addr->seg
979 && addr_sorttab[mid]->value.addr.off <= addr->off) )
981 low = mid;
983 else
985 high = mid;
990 if (!nearest) return FUNC_HAS_NO_LINES;
992 if( nearest->flags & SYM_STEP_THROUGH )
995 * This will cause us to keep single stepping until
996 * we get to the other side somewhere.
998 return NOT_ON_LINENUMBER;
1001 if( (nearest->flags & SYM_TRAMPOLINE) )
1004 * This will cause us to keep single stepping until
1005 * we get to the other side somewhere.
1007 return FUNC_IS_TRAMPOLINE;
1010 if( nearest->linetab == NULL )
1012 return FUNC_HAS_NO_LINES;
1017 * We never want to stop on the first instruction of a function
1018 * even if it has it's own linenumber. Let the thing keep running
1019 * until it gets past the function prologue. We only do this if there
1020 * is more than one line number for the function, of course.
1022 if( nearest->value.addr.off == addr->off && nearest->n_lines > 1 )
1024 return NOT_ON_LINENUMBER;
1027 if( (nearest->sourcefile != NULL)
1028 && (addr->off - nearest->value.addr.off < 0x100000) )
1030 low = 0;
1031 high = nearest->n_lines;
1032 while ((high - low) > 1)
1034 mid = (high + low) / 2;
1035 if (addr->off < nearest->linetab[mid].pc_offset.off) high = mid;
1036 else low = mid;
1038 if (addr->off == nearest->linetab[low].pc_offset.off)
1039 return AT_LINENUMBER;
1040 else
1041 return NOT_ON_LINENUMBER;
1044 return FUNC_HAS_NO_LINES;
1047 /***********************************************************************
1048 * DEBUG_GetFuncInfo
1050 * Find the symbol nearest to a given address.
1051 * Returns sourcefile name and line number in a format that the listing
1052 * handler can deal with.
1054 void
1055 DEBUG_GetFuncInfo( struct list_id * ret, const char * filename,
1056 const char * name)
1058 char buffer[256];
1059 char * pnt;
1060 struct name_hash *nh;
1062 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
1064 if( filename != NULL )
1067 if( nh->sourcefile == NULL )
1069 continue;
1072 pnt = strrchr(nh->sourcefile, '/');
1073 if( strcmp(nh->sourcefile, filename) != 0
1074 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1076 continue;
1079 if (!strcmp(nh->name, name)) break;
1082 if (!nh && (name[0] != '_'))
1084 buffer[0] = '_';
1085 strcpy(buffer+1, name);
1086 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
1088 if( filename != NULL )
1090 if( nh->sourcefile == NULL )
1092 continue;
1095 pnt = strrchr(nh->sourcefile, '/');
1096 if( strcmp(nh->sourcefile, filename) != 0
1097 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1099 continue;
1102 if (!strcmp(nh->name, buffer)) break;
1106 if( !nh )
1108 if( filename != NULL )
1110 DEBUG_Printf(DBG_CHN_MESG, "No such function %s in %s\n", name, filename);
1112 else
1114 DEBUG_Printf(DBG_CHN_MESG, "No such function %s\n", name);
1116 ret->sourcefile = NULL;
1117 ret->line = -1;
1118 return;
1121 ret->sourcefile = nh->sourcefile;
1124 * Search for the specific line number. If we don't find it,
1125 * then return FALSE.
1127 if( nh->linetab == NULL )
1129 ret->line = -1;
1131 else
1133 ret->line = nh->linetab[0].line_number;
1137 /***********************************************************************
1138 * DEBUG_GetStackSymbolValue
1140 * Get the address of a named symbol from the current stack frame.
1142 static
1143 BOOL DEBUG_GetStackSymbolValue( const char * name, DBG_VALUE *value )
1145 struct name_hash * curr_func;
1146 unsigned int ebp;
1147 unsigned int eip;
1148 int i;
1150 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1152 return FALSE;
1155 for(i=0; i < curr_func->n_locals; i++ )
1158 * Test the range of validity of the local variable. This
1159 * comes up with RBRAC/LBRAC stabs in particular.
1161 if( (curr_func->local_vars[i].pc_start != 0)
1162 && ((eip - curr_func->value.addr.off)
1163 < curr_func->local_vars[i].pc_start) )
1165 continue;
1168 if( (curr_func->local_vars[i].pc_end != 0)
1169 && ((eip - curr_func->value.addr.off)
1170 > curr_func->local_vars[i].pc_end) )
1172 continue;
1175 if( strcmp(name, curr_func->local_vars[i].name) == 0 )
1178 * OK, we found it. Now figure out what to do with this.
1180 if( curr_func->local_vars[i].regno != 0 )
1183 * Register variable. Point to DEBUG_context field.
1185 assert(curr_func->local_vars[i].regno - 1 < sizeof(reg_ofs)/sizeof(reg_ofs[0]));
1186 value->addr.off = ((DWORD)&DEBUG_context) +
1187 reg_ofs[curr_func->local_vars[i].regno - 1];
1188 value->cookie = DV_HOST;
1190 else
1192 value->addr.off = ebp + curr_func->local_vars[i].offset;
1193 value->cookie = DV_TARGET;
1195 value->addr.seg = 0;
1196 value->type = curr_func->local_vars[i].type;
1198 return TRUE;
1202 return FALSE;
1206 DEBUG_InfoLocals(void)
1208 struct name_hash * curr_func;
1209 unsigned int ebp;
1210 unsigned int eip;
1211 int i;
1212 unsigned int * ptr;
1213 unsigned int val;
1215 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1217 return FALSE;
1220 DEBUG_Printf(DBG_CHN_MESG, "%s:\n", curr_func->name);
1222 for(i=0; i < curr_func->n_locals; i++ )
1225 * Test the range of validity of the local variable. This
1226 * comes up with RBRAC/LBRAC stabs in particular.
1228 if( (curr_func->local_vars[i].pc_start != 0)
1229 && ((eip - curr_func->value.addr.off)
1230 < curr_func->local_vars[i].pc_start) )
1232 continue;
1235 if( (curr_func->local_vars[i].pc_end != 0)
1236 && ((eip - curr_func->value.addr.off)
1237 > curr_func->local_vars[i].pc_end) )
1239 continue;
1242 DEBUG_PrintTypeCast(curr_func->local_vars[i].type);
1244 if( curr_func->local_vars[i].regno != 0 )
1246 ptr = (unsigned int *)(((DWORD)&DEBUG_context)
1247 + reg_ofs[curr_func->local_vars[i].regno - 1]);
1248 DEBUG_Printf(DBG_CHN_MESG, " %s (optimized into register $%s) == 0x%8.8x\n",
1249 curr_func->local_vars[i].name,
1250 reg_name[curr_func->local_vars[i].regno - 1],
1251 *ptr);
1253 else
1255 DEBUG_READ_MEM_VERBOSE((void*)(ebp + curr_func->local_vars[i].offset),
1256 &val, sizeof(val));
1257 DEBUG_Printf(DBG_CHN_MESG, " %s == 0x%8.8x\n",
1258 curr_func->local_vars[i].name, val);
1262 return TRUE;
1266 DEBUG_SetSymbolSize(struct name_hash * sym, unsigned int len)
1268 sym->symbol_size = len;
1270 return TRUE;
1274 DEBUG_SetSymbolBPOff(struct name_hash * sym, unsigned int off)
1276 sym->breakpoint_offset = off;
1278 return TRUE;
1282 DEBUG_GetSymbolAddr(struct name_hash * sym, DBG_ADDR * addr)
1285 *addr = sym->value.addr;
1287 return TRUE;
1290 int DEBUG_SetLocalSymbolType(struct wine_locals * sym, struct datatype * type)
1292 sym->type = type;
1294 return TRUE;