Revived the GEN_C_SRCS variable to support wrc lex/yacc sources.
[wine.git] / debugger / hash.c
blob2d921aae99d24c218a8c30fccb41c6ad808308e1
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, const char * source,
185 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 char * c;
192 int hash;
194 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
196 hash = name_hash(name);
197 for (nh = name_hash_table[hash]; nh; nh = nh->next)
199 if( ((nh->flags & SYM_INVALID) != 0) && strcmp(name, nh->name) == 0 )
201 #if 0
202 DEBUG_Printf(DBG_CHN_MESG, "Changing address for symbol %s (%08lx:%08lx => %08lx:%08lx)\n",
203 name, nh->value.addr.seg, nh->value.addr.off, value->addr.seg, value->addr.off);
204 #endif
205 nh->value.addr = value->addr;
207 if( nh->value.type == NULL && value->type != NULL )
209 nh->value.type = value->type;
210 nh->value.cookie = value->cookie;
212 /* it may happen that the same symbol is defined in several compilation
213 * units, but the linker decides to merge it into a single instance.
214 * in that case, we don't clear the invalid flag for all the compilation
215 * units (N_GSYM), and wait to get the symbol from the symtab
217 if ((flags & SYM_INVALID) == 0)
218 nh->flags &= ~SYM_INVALID;
220 return nh;
222 if (nh->value.addr.seg == value->addr.seg &&
223 nh->value.addr.off == value->addr.off &&
224 strcmp(name, nh->name) == 0 )
226 return nh;
230 #if 0
231 DEBUG_Printf(DBG_CHN_TRACE, "adding symbol (%s) from file '%s' at 0x%04lx:%08lx\n",
232 name, source, value->addr.seg, value->addr.off);
233 #endif
236 * First see if we already have an entry for this symbol. If so
237 * return it, so we don't end up with duplicates.
240 new = (struct name_hash *) DBG_alloc(sizeof(struct name_hash));
241 new->value = *value;
242 new->name = DBG_strdup(name);
244 if( source != NULL )
247 * This is an enhancement to reduce memory consumption. The idea
248 * is that we duplicate a given string only once. This is a big
249 * win if there are lots of symbols defined in a given source file.
251 if( strcmp(source, prev_source) == 0 )
253 new->sourcefile = prev_duped_source;
255 else
257 strcpy(prev_source, source);
258 prev_duped_source = new->sourcefile = DBG_strdup(source);
261 else
263 new->sourcefile = NULL;
266 new->n_lines = 0;
267 new->lines_alloc = 0;
268 new->linetab = NULL;
270 new->n_locals = 0;
271 new->locals_alloc = 0;
272 new->local_vars = NULL;
274 new->flags = flags;
275 new->next = NULL;
277 /* Now insert into the hash table */
278 new->next = name_hash_table[hash];
279 name_hash_table[hash] = new;
282 * Check some heuristics based upon the file name to see whether
283 * we want to step through this guy or not. These are machine generated
284 * assembly files that are used to translate between the MS way of
285 * calling things and the GCC way of calling things. In general we
286 * always want to step through.
288 if( source != NULL )
290 c = strrchr(source, '.');
291 if( c != NULL && strcmp(c, ".s") == 0 )
293 c = strrchr(source, '/');
294 if( c != NULL )
296 c++;
297 if( (strcmp(c, "callfrom16.s") == 0)
298 || (strcmp(c, "callto16.s") == 0)
299 || (strcmp(c, "call32.s") == 0) )
301 new->flags |= SYM_TRAMPOLINE;
307 sortlist_valid = FALSE;
308 return new;
311 BOOL DEBUG_Normalize(struct name_hash * nh )
315 * We aren't adding any more locals or linenumbers to this function.
316 * Free any spare memory that we might have allocated.
318 if( nh == NULL )
320 return TRUE;
323 if( nh->n_locals != nh->locals_alloc )
325 nh->locals_alloc = nh->n_locals;
326 nh->local_vars = DBG_realloc(nh->local_vars,
327 nh->locals_alloc * sizeof(WineLocals));
330 if( nh->n_lines != nh->lines_alloc )
332 nh->lines_alloc = nh->n_lines;
333 nh->linetab = DBG_realloc(nh->linetab,
334 nh->lines_alloc * sizeof(WineLineNo));
337 return TRUE;
340 /***********************************************************************
341 * DEBUG_GetSymbolValue
343 * Get the address of a named symbol.
345 BOOL DEBUG_GetSymbolValue( const char * name, const int lineno,
346 DBG_VALUE *value, int bp_flag )
348 char buffer[256];
349 struct name_hash *nh;
351 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
353 if( (nh->flags & SYM_INVALID) != 0 )
355 continue;
358 if (!strcmp(nh->name, name)) break;
361 if (!nh && (name[0] != '_'))
363 buffer[0] = '_';
364 strcpy(buffer+1, name);
365 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
367 if( (nh->flags & SYM_INVALID) != 0 )
369 continue;
371 if (!strcmp(nh->name, buffer)) break;
376 * If we don't have anything here, then try and see if this
377 * is a local symbol to the current stack frame. No matter
378 * what, we have nothing more to do, so we let that function
379 * decide what we ultimately return.
381 if (!nh)
383 return DEBUG_GetStackSymbolValue(name, value);
386 value->type = nh->value.type;
387 value->cookie = nh->value.cookie;
388 return DEBUG_GetLineNumberAddr( nh, lineno, &value->addr, bp_flag );
391 /***********************************************************************
392 * DEBUG_GetLineNumberAddr
394 * Get the address of a named symbol.
396 BOOL DEBUG_GetLineNumberAddr( struct name_hash * nh, const int lineno,
397 DBG_ADDR *addr, int bp_flag )
399 int i;
401 if( lineno == -1 )
403 *addr = nh->value.addr;
404 if( bp_flag )
406 addr->off += nh->breakpoint_offset;
409 else
412 * Search for the specific line number. If we don't find it,
413 * then return FALSE.
415 if( nh->linetab == NULL )
417 return FALSE;
420 for(i=0; i < nh->n_lines; i++ )
422 if( nh->linetab[i].line_number == lineno )
424 *addr = nh->linetab[i].pc_offset;
425 return TRUE;
430 * This specific line number not found.
432 return FALSE;
435 return TRUE;
439 /***********************************************************************
440 * DEBUG_SetSymbolValue
442 * Set the address of a named symbol.
444 BOOL DEBUG_SetSymbolValue( const char * name, const DBG_VALUE *value )
446 char buffer[256];
447 struct name_hash *nh;
449 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
451 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
452 if (!strcmp(nh->name, name)) break;
454 if (!nh && (name[0] != '_'))
456 buffer[0] = '_';
457 strcpy(buffer+1, name);
458 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
459 if (!strcmp(nh->name, buffer)) break;
462 if (!nh) return FALSE;
463 nh->value = *value;
464 nh->flags &= ~SYM_INVALID;
465 DEBUG_FixAddress( &nh->value.addr, DEBUG_context.SegDs );
466 return TRUE;
470 /***********************************************************************
471 * DEBUG_FindNearestSymbol
473 * Find the symbol nearest to a given address.
474 * If ebp is specified as non-zero, it means we should dump the argument
475 * list into the string we return as well.
477 const char * DEBUG_FindNearestSymbol( const DBG_ADDR *addr, int flag,
478 struct name_hash ** rtn,
479 unsigned int ebp,
480 struct list_id * source)
482 static char name_buffer[MAX_PATH + 256];
483 static char arglist[1024];
484 static char argtmp[256];
485 struct name_hash * nearest = NULL;
486 int mid, high, low;
487 unsigned int * ptr;
488 int lineno;
489 char * lineinfo, *sourcefile;
490 int i;
491 char linebuff[16];
492 unsigned val;
494 if( rtn != NULL )
496 *rtn = NULL;
499 if( source != NULL )
501 source->sourcefile = NULL;
502 source->line = -1;
505 if( sortlist_valid == FALSE )
507 DEBUG_ResortSymbols();
510 if( sortlist_valid == FALSE )
512 return NULL;
516 * FIXME - use the binary search that we added to
517 * the function DEBUG_CheckLinenoStatus. Better yet, we should
518 * probably keep some notion of the current function so we don't
519 * have to search every time.
522 * Binary search to find closest symbol.
524 low = 0;
525 high = sorttab_nsym;
526 if( addr_sorttab[0]->value.addr.seg > addr->seg
527 || ( addr_sorttab[0]->value.addr.seg == addr->seg
528 && addr_sorttab[0]->value.addr.off > addr->off) )
530 nearest = NULL;
532 else if( addr_sorttab[high - 1]->value.addr.seg < addr->seg
533 || ( addr_sorttab[high - 1]->value.addr.seg == addr->seg
534 && addr_sorttab[high - 1]->value.addr.off < addr->off) )
536 nearest = addr_sorttab[high - 1];
538 else
540 while(1==1)
542 mid = (high + low)/2;
543 if( mid == low )
546 * See if there are any other entries that might also
547 * have the same address, and would also have a line
548 * number table.
550 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
552 if( (addr_sorttab[mid - 1]->value.addr.seg ==
553 addr_sorttab[mid]->value.addr.seg)
554 && (addr_sorttab[mid - 1]->value.addr.off ==
555 addr_sorttab[mid]->value.addr.off)
556 && (addr_sorttab[mid - 1]->linetab != NULL) )
558 mid--;
562 if( (mid < sorttab_nsym - 1)
563 && (addr_sorttab[mid]->linetab == NULL) )
565 if( (addr_sorttab[mid + 1]->value.addr.seg ==
566 addr_sorttab[mid]->value.addr.seg)
567 && (addr_sorttab[mid + 1]->value.addr.off ==
568 addr_sorttab[mid]->value.addr.off)
569 && (addr_sorttab[mid + 1]->linetab != NULL) )
571 mid++;
574 nearest = addr_sorttab[mid];
575 #if 0
576 DEBUG_Printf(DBG_CHN_MESG, "Found %x:%x when looking for %x:%x %x %s\n",
577 addr_sorttab[mid ]->value.addr.seg,
578 addr_sorttab[mid ]->value.addr.off,
579 addr->seg, addr->off,
580 addr_sorttab[mid ]->linetab,
581 addr_sorttab[mid ]->name);
582 #endif
583 break;
585 if( (addr_sorttab[mid]->value.addr.seg < addr->seg)
586 || ( addr_sorttab[mid]->value.addr.seg == addr->seg
587 && addr_sorttab[mid]->value.addr.off <= addr->off) )
589 low = mid;
591 else
593 high = mid;
598 if (!nearest) return NULL;
600 if( rtn != NULL )
602 *rtn = nearest;
606 * Fill in the relevant bits to the structure so that we can
607 * locate the source and line for this bit of code.
609 if( source != NULL )
611 source->sourcefile = nearest->sourcefile;
612 if( nearest->linetab == NULL )
614 source->line = -1;
616 else
618 source->line = nearest->linetab[0].line_number;
622 lineinfo = "";
623 lineno = -1;
626 * Prepare to display the argument list. If ebp is specified, it is
627 * the framepointer for the function in question. If not specified,
628 * we don't want the arglist.
630 memset(arglist, '\0', sizeof(arglist));
631 if( ebp != 0 )
633 for(i=0; i < nearest->n_locals; i++ )
636 * If this is a register (offset == 0) or a local
637 * variable, we don't want to know about it.
639 if( nearest->local_vars[i].offset <= 0 )
641 continue;
644 ptr = (unsigned int *) (ebp + nearest->local_vars[i].offset);
645 if( arglist[0] == '\0' )
647 arglist[0] = '(';
649 else
651 strcat(arglist, ", ");
653 DEBUG_READ_MEM_VERBOSE(ptr, &val, sizeof(val));
654 sprintf(argtmp, "%s=0x%x", nearest->local_vars[i].name, val);
656 strcat(arglist, argtmp);
658 if( arglist[0] == '(' )
660 strcat(arglist, ")");
664 if( (nearest->sourcefile != NULL) && (flag == TRUE)
665 && (addr->off - nearest->value.addr.off < 0x100000) )
669 * Try and find the nearest line number to the current offset.
671 if( nearest->linetab != NULL )
673 low = 0;
674 high = nearest->n_lines;
675 while ((high - low) > 1)
677 mid = (high + low) / 2;
678 if (addr->off < nearest->linetab[mid].pc_offset.off)
679 high = mid;
680 else
681 low = mid;
683 lineno = nearest->linetab[low].line_number;
686 if( lineno != -1 )
688 sprintf(linebuff, ":%d", lineno);
689 lineinfo = linebuff;
690 if( source != NULL )
692 source->line = lineno;
696 /* Remove the path from the file name */
697 sourcefile = strrchr( nearest->sourcefile, '/' );
698 if (!sourcefile) sourcefile = nearest->sourcefile;
699 else sourcefile++;
701 if (addr->off == nearest->value.addr.off)
702 sprintf( name_buffer, "%s%s [%s%s]", nearest->name,
703 arglist, sourcefile, lineinfo);
704 else
705 sprintf( name_buffer, "%s+0x%lx%s [%s%s]", nearest->name,
706 addr->off - nearest->value.addr.off,
707 arglist, sourcefile, lineinfo );
709 else
711 if (addr->off == nearest->value.addr.off)
712 sprintf( name_buffer, "%s%s", nearest->name, arglist);
713 else {
714 if (addr->seg && (nearest->value.addr.seg!=addr->seg))
715 return NULL;
716 else
717 sprintf( name_buffer, "%s+0x%lx%s", nearest->name,
718 addr->off - nearest->value.addr.off, arglist);
721 return name_buffer;
725 /***********************************************************************
726 * DEBUG_ReadSymbolTable
728 * Read a symbol file into the hash table.
730 void DEBUG_ReadSymbolTable( const char * filename )
732 FILE * symbolfile;
733 DBG_VALUE value;
734 char type;
735 char * cpnt;
736 char buffer[256];
737 char name[256];
739 if (!(symbolfile = fopen(filename, "r")))
741 DEBUG_Printf( DBG_CHN_WARN, "Unable to open symbol table %s\n", filename );
742 return;
745 DEBUG_Printf( DBG_CHN_MESG, "Reading symbols from file %s\n", filename );
747 value.type = NULL;
748 value.addr.seg = 0;
749 value.addr.off = 0;
750 value.cookie = DV_TARGET;
752 while (1)
754 fgets( buffer, sizeof(buffer), symbolfile );
755 if (feof(symbolfile)) break;
757 /* Strip any text after a # sign (i.e. comments) */
758 cpnt = buffer;
759 while (*cpnt)
760 if(*cpnt++ == '#') { *cpnt = 0; break; }
762 /* Quietly ignore any lines that have just whitespace */
763 cpnt = buffer;
764 while(*cpnt)
766 if(*cpnt != ' ' && *cpnt != '\t') break;
767 cpnt++;
769 if (!(*cpnt) || *cpnt == '\n') continue;
771 if (sscanf(buffer, "%lx %c %s", &value.addr.off, &type, name) == 3)
772 DEBUG_AddSymbol( name, &value, NULL, SYM_WINE );
774 fclose(symbolfile);
778 void
779 DEBUG_AddLineNumber( struct name_hash * func, int line_num,
780 unsigned long offset )
782 if( func == NULL )
784 return;
787 if( func->n_lines + 1 >= func->lines_alloc )
789 func->lines_alloc += 64;
790 func->linetab = DBG_realloc(func->linetab,
791 func->lines_alloc * sizeof(WineLineNo));
794 func->linetab[func->n_lines].line_number = line_num;
795 func->linetab[func->n_lines].pc_offset.seg = func->value.addr.seg;
796 func->linetab[func->n_lines].pc_offset.off = func->value.addr.off + offset;
797 func->n_lines++;
801 struct wine_locals *
802 DEBUG_AddLocal( struct name_hash * func, int regno,
803 int offset,
804 int pc_start,
805 int pc_end,
806 char * name)
808 if( func == NULL )
810 return NULL;
813 if( func->n_locals + 1 >= func->locals_alloc )
815 func->locals_alloc += 32;
816 func->local_vars = DBG_realloc(func->local_vars,
817 func->locals_alloc * sizeof(WineLocals));
820 func->local_vars[func->n_locals].regno = regno;
821 func->local_vars[func->n_locals].offset = offset;
822 func->local_vars[func->n_locals].pc_start = pc_start;
823 func->local_vars[func->n_locals].pc_end = pc_end;
824 func->local_vars[func->n_locals].name = DBG_strdup(name);
825 func->local_vars[func->n_locals].type = NULL;
826 func->n_locals++;
828 return &func->local_vars[func->n_locals - 1];
831 void
832 DEBUG_DumpHashInfo(void)
834 int i;
835 int depth;
836 struct name_hash *nh;
839 * Utility function to dump stats about the hash table.
841 for(i=0; i<NR_NAME_HASH; i++)
843 depth = 0;
844 for (nh = name_hash_table[i]; nh; nh = nh->next)
846 depth++;
848 DEBUG_Printf(DBG_CHN_MESG, "Bucket %d: %d\n", i, depth);
852 /***********************************************************************
853 * DEBUG_CheckLinenoStatus
855 * Find the symbol nearest to a given address.
856 * If ebp is specified as non-zero, it means we should dump the argument
857 * list into the string we return as well.
859 int DEBUG_CheckLinenoStatus( const DBG_ADDR *addr)
861 struct name_hash * nearest = NULL;
862 int mid, high, low;
864 if( sortlist_valid == FALSE )
866 DEBUG_ResortSymbols();
870 * Binary search to find closest symbol.
872 low = 0;
873 high = sorttab_nsym;
874 if( addr_sorttab[0]->value.addr.seg > addr->seg
875 || ( addr_sorttab[0]->value.addr.seg == addr->seg
876 && addr_sorttab[0]->value.addr.off > addr->off) )
878 nearest = NULL;
880 else if( addr_sorttab[high - 1]->value.addr.seg < addr->seg
881 || ( addr_sorttab[high - 1]->value.addr.seg == addr->seg
882 && addr_sorttab[high - 1]->value.addr.off < addr->off) )
884 nearest = addr_sorttab[high - 1];
886 else
888 while(1==1)
890 mid = (high + low)/2;
891 if( mid == low )
894 * See if there are any other entries that might also
895 * have the same address, and would also have a line
896 * number table.
898 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
900 if( (addr_sorttab[mid - 1]->value.addr.seg ==
901 addr_sorttab[mid]->value.addr.seg)
902 && (addr_sorttab[mid - 1]->value.addr.off ==
903 addr_sorttab[mid]->value.addr.off)
904 && (addr_sorttab[mid - 1]->linetab != NULL) )
906 mid--;
910 if( (mid < sorttab_nsym - 1)
911 && (addr_sorttab[mid]->linetab == NULL) )
913 if( (addr_sorttab[mid + 1]->value.addr.seg ==
914 addr_sorttab[mid]->value.addr.seg)
915 && (addr_sorttab[mid + 1]->value.addr.off ==
916 addr_sorttab[mid]->value.addr.off)
917 && (addr_sorttab[mid + 1]->linetab != NULL) )
919 mid++;
922 nearest = addr_sorttab[mid];
923 #if 0
924 DEBUG_Printf(DBG_CHN_MESG, "Found %x:%x when looking for %x:%x %x %s\n",
925 addr_sorttab[mid ]->value.addr.seg,
926 addr_sorttab[mid ]->value.addr.off,
927 addr->seg, addr->off,
928 addr_sorttab[mid ]->linetab,
929 addr_sorttab[mid ]->name);
930 #endif
931 break;
933 if( (addr_sorttab[mid]->value.addr.seg < addr->seg)
934 || ( addr_sorttab[mid]->value.addr.seg == addr->seg
935 && addr_sorttab[mid]->value.addr.off <= addr->off) )
937 low = mid;
939 else
941 high = mid;
946 if (!nearest) return FUNC_HAS_NO_LINES;
948 if( nearest->flags & SYM_STEP_THROUGH )
951 * This will cause us to keep single stepping until
952 * we get to the other side somewhere.
954 return NOT_ON_LINENUMBER;
957 if( (nearest->flags & SYM_TRAMPOLINE) )
960 * This will cause us to keep single stepping until
961 * we get to the other side somewhere.
963 return FUNC_IS_TRAMPOLINE;
966 if( nearest->linetab == NULL )
968 return FUNC_HAS_NO_LINES;
973 * We never want to stop on the first instruction of a function
974 * even if it has it's own linenumber. Let the thing keep running
975 * until it gets past the function prologue. We only do this if there
976 * is more than one line number for the function, of course.
978 if( nearest->value.addr.off == addr->off && nearest->n_lines > 1 )
980 return NOT_ON_LINENUMBER;
983 if( (nearest->sourcefile != NULL)
984 && (addr->off - nearest->value.addr.off < 0x100000) )
986 low = 0;
987 high = nearest->n_lines;
988 while ((high - low) > 1)
990 mid = (high + low) / 2;
991 if (addr->off < nearest->linetab[mid].pc_offset.off) high = mid;
992 else low = mid;
994 if (addr->off == nearest->linetab[low].pc_offset.off)
995 return AT_LINENUMBER;
996 else
997 return NOT_ON_LINENUMBER;
1000 return FUNC_HAS_NO_LINES;
1003 /***********************************************************************
1004 * DEBUG_GetFuncInfo
1006 * Find the symbol nearest to a given address.
1007 * Returns sourcefile name and line number in a format that the listing
1008 * handler can deal with.
1010 void
1011 DEBUG_GetFuncInfo( struct list_id * ret, const char * filename,
1012 const char * name)
1014 char buffer[256];
1015 char * pnt;
1016 struct name_hash *nh;
1018 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
1020 if( filename != NULL )
1023 if( nh->sourcefile == NULL )
1025 continue;
1028 pnt = strrchr(nh->sourcefile, '/');
1029 if( strcmp(nh->sourcefile, filename) != 0
1030 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1032 continue;
1035 if (!strcmp(nh->name, name)) break;
1038 if (!nh && (name[0] != '_'))
1040 buffer[0] = '_';
1041 strcpy(buffer+1, name);
1042 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
1044 if( filename != NULL )
1046 if( nh->sourcefile == NULL )
1048 continue;
1051 pnt = strrchr(nh->sourcefile, '/');
1052 if( strcmp(nh->sourcefile, filename) != 0
1053 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1055 continue;
1058 if (!strcmp(nh->name, buffer)) break;
1062 if( !nh )
1064 if( filename != NULL )
1066 DEBUG_Printf(DBG_CHN_MESG, "No such function %s in %s\n", name, filename);
1068 else
1070 DEBUG_Printf(DBG_CHN_MESG, "No such function %s\n", name);
1072 ret->sourcefile = NULL;
1073 ret->line = -1;
1074 return;
1077 ret->sourcefile = nh->sourcefile;
1080 * Search for the specific line number. If we don't find it,
1081 * then return FALSE.
1083 if( nh->linetab == NULL )
1085 ret->line = -1;
1087 else
1089 ret->line = nh->linetab[0].line_number;
1093 /***********************************************************************
1094 * DEBUG_GetStackSymbolValue
1096 * Get the address of a named symbol from the current stack frame.
1098 static
1099 BOOL DEBUG_GetStackSymbolValue( const char * name, DBG_VALUE *value )
1101 struct name_hash * curr_func;
1102 unsigned int ebp;
1103 unsigned int eip;
1104 int i;
1106 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1108 return FALSE;
1111 for(i=0; i < curr_func->n_locals; i++ )
1114 * Test the range of validity of the local variable. This
1115 * comes up with RBRAC/LBRAC stabs in particular.
1117 if( (curr_func->local_vars[i].pc_start != 0)
1118 && ((eip - curr_func->value.addr.off)
1119 < curr_func->local_vars[i].pc_start) )
1121 continue;
1124 if( (curr_func->local_vars[i].pc_end != 0)
1125 && ((eip - curr_func->value.addr.off)
1126 > curr_func->local_vars[i].pc_end) )
1128 continue;
1131 if( strcmp(name, curr_func->local_vars[i].name) == 0 )
1134 * OK, we found it. Now figure out what to do with this.
1136 if( curr_func->local_vars[i].regno != 0 )
1139 * Register variable. Point to DEBUG_context field.
1141 assert(curr_func->local_vars[i].regno - 1 < sizeof(reg_ofs)/sizeof(reg_ofs[0]));
1142 value->addr.off = ((DWORD)&DEBUG_context) +
1143 reg_ofs[curr_func->local_vars[i].regno - 1];
1144 value->cookie = DV_HOST;
1146 else
1148 value->addr.off = ebp + curr_func->local_vars[i].offset;
1149 value->cookie = DV_TARGET;
1151 value->addr.seg = 0;
1152 value->type = curr_func->local_vars[i].type;
1154 return TRUE;
1158 return FALSE;
1162 DEBUG_InfoLocals(void)
1164 struct name_hash * curr_func;
1165 unsigned int ebp;
1166 unsigned int eip;
1167 int i;
1168 unsigned int * ptr;
1169 unsigned int val;
1171 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1173 return FALSE;
1176 for(i=0; i < curr_func->n_locals; i++ )
1179 * Test the range of validity of the local variable. This
1180 * comes up with RBRAC/LBRAC stabs in particular.
1182 if( (curr_func->local_vars[i].pc_start != 0)
1183 && ((eip - curr_func->value.addr.off)
1184 < curr_func->local_vars[i].pc_start) )
1186 continue;
1189 if( (curr_func->local_vars[i].pc_end != 0)
1190 && ((eip - curr_func->value.addr.off)
1191 > curr_func->local_vars[i].pc_end) )
1193 continue;
1196 if( curr_func->local_vars[i].regno != 0 )
1198 ptr = (unsigned int *)(((DWORD)&DEBUG_context)
1199 + reg_ofs[curr_func->local_vars[i].regno - 1]);
1200 DEBUG_Printf(DBG_CHN_MESG, "%s:%s (optimized into register $%s) == 0x%8.8x\n",
1201 curr_func->name, curr_func->local_vars[i].name,
1202 reg_name[curr_func->local_vars[i].regno - 1],
1203 *ptr);
1205 else
1207 DEBUG_READ_MEM_VERBOSE((void*)(ebp + curr_func->local_vars[i].offset),
1208 &val, sizeof(val));
1209 DEBUG_Printf(DBG_CHN_MESG, "%s:%s == 0x%8.8x\n",
1210 curr_func->name, curr_func->local_vars[i].name, val);
1214 return TRUE;
1218 DEBUG_SetSymbolSize(struct name_hash * sym, unsigned int len)
1220 sym->symbol_size = len;
1222 return TRUE;
1226 DEBUG_SetSymbolBPOff(struct name_hash * sym, unsigned int off)
1228 sym->breakpoint_offset = off;
1230 return TRUE;
1234 DEBUG_GetSymbolAddr(struct name_hash * sym, DBG_ADDR * addr)
1237 *addr = sym->value.addr;
1239 return TRUE;
1242 int DEBUG_SetLocalSymbolType(struct wine_locals * sym, struct datatype * type)
1244 sym->type = type;
1246 return TRUE;