Introduced DBG_VALUE struct to manipulate debugger/debuggee address space.
[wine.git] / debugger / hash.c
blobcd6cd190014e494c2aeb6d7b3b3172f1ee79d442
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 "neexe.h"
15 #include "module.h"
16 #include "process.h"
17 #include "debugger.h"
18 #include "toolhelp.h"
20 #define NR_NAME_HASH 16384
21 #ifndef PATH_MAX
22 #define PATH_MAX _MAX_PATH
23 #endif
25 #ifdef __i386__
26 static char * reg_name[] =
28 "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"
31 static unsigned reg_ofs[] =
33 FIELD_OFFSET(CONTEXT, Eax), FIELD_OFFSET(CONTEXT, Ecx),
34 FIELD_OFFSET(CONTEXT, Edx), FIELD_OFFSET(CONTEXT, Ebx),
35 FIELD_OFFSET(CONTEXT, Esp), FIELD_OFFSET(CONTEXT, Ebp),
36 FIELD_OFFSET(CONTEXT, Esi), FIELD_OFFSET(CONTEXT, Edi)
38 #else
39 static char * reg_name[] = { NULL }; /* FIXME */
40 static unsigned reg_ofs[] = { 0 };
41 #endif
44 struct name_hash
46 struct name_hash * next; /* Used to look up within name hash */
47 char * name;
48 char * sourcefile;
50 int n_locals;
51 int locals_alloc;
52 WineLocals * local_vars;
54 int n_lines;
55 int lines_alloc;
56 WineLineNo * linetab;
58 DBG_VALUE value;
59 unsigned short flags;
60 unsigned short breakpoint_offset;
61 unsigned int symbol_size;
65 static BOOL DEBUG_GetStackSymbolValue( const char * name, DBG_VALUE *value );
66 static int sortlist_valid = FALSE;
68 static int sorttab_nsym;
69 static struct name_hash ** addr_sorttab = NULL;
71 static struct name_hash * name_hash_table[NR_NAME_HASH];
73 static unsigned int name_hash( const char * name )
75 unsigned int hash = 0;
76 unsigned int tmp;
77 const char * p;
79 p = name;
81 while (*p)
83 hash = (hash << 4) + *p++;
85 if( (tmp = (hash & 0xf0000000)) )
87 hash ^= tmp >> 24;
89 hash &= ~tmp;
91 return hash % NR_NAME_HASH;
94 int
95 DEBUG_cmp_sym(const void * p1, const void * p2)
97 struct name_hash ** name1 = (struct name_hash **) p1;
98 struct name_hash ** name2 = (struct name_hash **) p2;
100 if( ((*name1)->flags & SYM_INVALID) != 0 )
102 return -1;
105 if( ((*name2)->flags & SYM_INVALID) != 0 )
107 return 1;
110 if( (*name1)->value.addr.seg > (*name2)->value.addr.seg )
112 return 1;
115 if( (*name1)->value.addr.seg < (*name2)->value.addr.seg )
117 return -1;
120 if( (*name1)->value.addr.off > (*name2)->value.addr.off )
122 return 1;
125 if( (*name1)->value.addr.off < (*name2)->value.addr.off )
127 return -1;
130 return 0;
133 /***********************************************************************
134 * DEBUG_ResortSymbols
136 * Rebuild sorted list of symbols.
138 static
139 void
140 DEBUG_ResortSymbols(void)
142 struct name_hash *nh;
143 int nsym = 0;
144 int i;
146 for(i=0; i<NR_NAME_HASH; i++)
148 for (nh = name_hash_table[i]; nh; nh = nh->next)
150 if( (nh->flags & SYM_INVALID) == 0 )
151 nsym++;
152 else
153 fprintf( stderr, "Symbol %s is invalid\n", nh->name );
157 sorttab_nsym = nsym;
158 if( nsym == 0 )
160 return;
163 addr_sorttab = (struct name_hash **) DBG_realloc(addr_sorttab,
164 nsym * sizeof(struct name_hash *));
166 nsym = 0;
167 for(i=0; i<NR_NAME_HASH; i++)
169 for (nh = name_hash_table[i]; nh; nh = nh->next)
171 if( (nh->flags & SYM_INVALID) == 0 )
172 addr_sorttab[nsym++] = nh;
176 qsort(addr_sorttab, nsym,
177 sizeof(struct name_hash *), DEBUG_cmp_sym);
178 sortlist_valid = TRUE;
182 /***********************************************************************
183 * DEBUG_AddSymbol
185 * Add a symbol to the table.
187 struct name_hash *
188 DEBUG_AddSymbol( const char * name, const DBG_VALUE *value, const char * source,
189 int flags)
191 struct name_hash * new;
192 struct name_hash *nh;
193 static char prev_source[PATH_MAX] = {'\0', };
194 static char * prev_duped_source = NULL;
195 char * c;
196 int hash;
198 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
200 hash = name_hash(name);
201 for (nh = name_hash_table[hash]; nh; nh = nh->next)
203 if( ((nh->flags & SYM_INVALID) != 0) && strcmp(name, nh->name) == 0 )
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;
231 * First see if we already have an entry for this symbol. If so
232 * return it, so we don't end up with duplicates.
235 new = (struct name_hash *) DBG_alloc(sizeof(struct name_hash));
236 new->value = *value;
237 new->name = DBG_strdup(name);
239 if( source != NULL )
242 * This is an enhancement to reduce memory consumption. The idea
243 * is that we duplicate a given string only once. This is a big
244 * win if there are lots of symbols defined in a given source file.
246 if( strcmp(source, prev_source) == 0 )
248 new->sourcefile = prev_duped_source;
250 else
252 strcpy(prev_source, source);
253 prev_duped_source = new->sourcefile = DBG_strdup(source);
256 else
258 new->sourcefile = NULL;
261 new->n_lines = 0;
262 new->lines_alloc = 0;
263 new->linetab = NULL;
265 new->n_locals = 0;
266 new->locals_alloc = 0;
267 new->local_vars = NULL;
269 new->flags = flags;
270 new->next = NULL;
272 /* Now insert into the hash table */
273 new->next = name_hash_table[hash];
274 name_hash_table[hash] = new;
277 * Check some heuristics based upon the file name to see whether
278 * we want to step through this guy or not. These are machine generated
279 * assembly files that are used to translate between the MS way of
280 * calling things and the GCC way of calling things. In general we
281 * always want to step through.
283 if( source != NULL )
285 c = strrchr(source, '.');
286 if( c != NULL && strcmp(c, ".s") == 0 )
288 c = strrchr(source, '/');
289 if( c != NULL )
291 c++;
292 if( (strcmp(c, "callfrom16.s") == 0)
293 || (strcmp(c, "callto16.s") == 0)
294 || (strcmp(c, "call32.s") == 0) )
296 new->flags |= SYM_TRAMPOLINE;
302 sortlist_valid = FALSE;
303 return new;
306 BOOL DEBUG_Normalize(struct name_hash * nh )
310 * We aren't adding any more locals or linenumbers to this function.
311 * Free any spare memory that we might have allocated.
313 if( nh == NULL )
315 return TRUE;
318 if( nh->n_locals != nh->locals_alloc )
320 nh->locals_alloc = nh->n_locals;
321 nh->local_vars = DBG_realloc(nh->local_vars,
322 nh->locals_alloc * sizeof(WineLocals));
325 if( nh->n_lines != nh->lines_alloc )
327 nh->lines_alloc = nh->n_lines;
328 nh->linetab = DBG_realloc(nh->linetab,
329 nh->lines_alloc * sizeof(WineLineNo));
332 return TRUE;
335 /***********************************************************************
336 * DEBUG_GetSymbolValue
338 * Get the address of a named symbol.
340 BOOL DEBUG_GetSymbolValue( const char * name, const int lineno,
341 DBG_VALUE *value, int bp_flag )
343 char buffer[256];
344 struct name_hash *nh;
346 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
348 if( (nh->flags & SYM_INVALID) != 0 )
350 continue;
353 if (!strcmp(nh->name, name)) break;
356 if (!nh && (name[0] != '_'))
358 buffer[0] = '_';
359 strcpy(buffer+1, name);
360 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
362 if( (nh->flags & SYM_INVALID) != 0 )
364 continue;
366 if (!strcmp(nh->name, buffer)) break;
371 * If we don't have anything here, then try and see if this
372 * is a local symbol to the current stack frame. No matter
373 * what, we have nothing more to do, so we let that function
374 * decide what we ultimately return.
376 if (!nh)
378 return DEBUG_GetStackSymbolValue(name, value);
381 value->type = nh->value.type;
382 value->cookie = nh->value.cookie;
383 return DEBUG_GetLineNumberAddr( nh, lineno, &value->addr, bp_flag );
386 /***********************************************************************
387 * DEBUG_GetLineNumberAddr
389 * Get the address of a named symbol.
391 BOOL DEBUG_GetLineNumberAddr( struct name_hash * nh, const int lineno,
392 DBG_ADDR *addr, int bp_flag )
394 int i;
396 if( lineno == -1 )
398 *addr = nh->value.addr;
399 if( bp_flag )
401 addr->off += nh->breakpoint_offset;
404 else
407 * Search for the specific line number. If we don't find it,
408 * then return FALSE.
410 if( nh->linetab == NULL )
412 return FALSE;
415 for(i=0; i < nh->n_lines; i++ )
417 if( nh->linetab[i].line_number == lineno )
419 *addr = nh->linetab[i].pc_offset;
420 return TRUE;
425 * This specific line number not found.
427 return FALSE;
430 return TRUE;
434 /***********************************************************************
435 * DEBUG_SetSymbolValue
437 * Set the address of a named symbol.
439 BOOL DEBUG_SetSymbolValue( const char * name, const DBG_VALUE *value )
441 char buffer[256];
442 struct name_hash *nh;
444 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
446 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
447 if (!strcmp(nh->name, name)) break;
449 if (!nh && (name[0] != '_'))
451 buffer[0] = '_';
452 strcpy(buffer+1, name);
453 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
454 if (!strcmp(nh->name, buffer)) break;
457 if (!nh) return FALSE;
458 nh->value = *value;
459 nh->flags &= ~SYM_INVALID;
460 DEBUG_FixAddress( &nh->value.addr, DEBUG_context.SegDs );
461 return TRUE;
465 /***********************************************************************
466 * DEBUG_FindNearestSymbol
468 * Find the symbol nearest to a given address.
469 * If ebp is specified as non-zero, it means we should dump the argument
470 * list into the string we return as well.
472 const char * DEBUG_FindNearestSymbol( const DBG_ADDR *addr, int flag,
473 struct name_hash ** rtn,
474 unsigned int ebp,
475 struct list_id * source)
477 static char name_buffer[MAX_PATH + 256];
478 static char arglist[1024];
479 static char argtmp[256];
480 struct name_hash * nearest = NULL;
481 int mid, high, low;
482 unsigned int * ptr;
483 int lineno;
484 char * lineinfo, *sourcefile;
485 int i;
486 char linebuff[16];
487 unsigned val;
489 if( rtn != NULL )
491 *rtn = NULL;
494 if( source != NULL )
496 source->sourcefile = NULL;
497 source->line = -1;
500 if( sortlist_valid == FALSE )
502 DEBUG_ResortSymbols();
505 if( sortlist_valid == FALSE )
507 return NULL;
511 * FIXME - use the binary search that we added to
512 * the function DEBUG_CheckLinenoStatus. Better yet, we should
513 * probably keep some notion of the current function so we don't
514 * have to search every time.
517 * Binary search to find closest symbol.
519 low = 0;
520 high = sorttab_nsym;
521 if( addr_sorttab[0]->value.addr.seg > addr->seg
522 || ( addr_sorttab[0]->value.addr.seg == addr->seg
523 && addr_sorttab[0]->value.addr.off > addr->off) )
525 nearest = NULL;
527 else if( addr_sorttab[high - 1]->value.addr.seg < addr->seg
528 || ( addr_sorttab[high - 1]->value.addr.seg == addr->seg
529 && addr_sorttab[high - 1]->value.addr.off < addr->off) )
531 nearest = addr_sorttab[high - 1];
533 else
535 while(1==1)
537 mid = (high + low)/2;
538 if( mid == low )
541 * See if there are any other entries that might also
542 * have the same address, and would also have a line
543 * number table.
545 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
547 if( (addr_sorttab[mid - 1]->value.addr.seg ==
548 addr_sorttab[mid]->value.addr.seg)
549 && (addr_sorttab[mid - 1]->value.addr.off ==
550 addr_sorttab[mid]->value.addr.off)
551 && (addr_sorttab[mid - 1]->linetab != NULL) )
553 mid--;
557 if( (mid < sorttab_nsym - 1)
558 && (addr_sorttab[mid]->linetab == NULL) )
560 if( (addr_sorttab[mid + 1]->value.addr.seg ==
561 addr_sorttab[mid]->value.addr.seg)
562 && (addr_sorttab[mid + 1]->value.addr.off ==
563 addr_sorttab[mid]->value.addr.off)
564 && (addr_sorttab[mid + 1]->linetab != NULL) )
566 mid++;
569 nearest = addr_sorttab[mid];
570 #if 0
571 fprintf(stderr, "Found %x:%x when looking for %x:%x %x %s\n",
572 addr_sorttab[mid ]->value.addr.seg,
573 addr_sorttab[mid ]->value.addr.off,
574 addr->seg, addr->off,
575 addr_sorttab[mid ]->linetab,
576 addr_sorttab[mid ]->name);
577 #endif
578 break;
580 if( (addr_sorttab[mid]->value.addr.seg < addr->seg)
581 || ( addr_sorttab[mid]->value.addr.seg == addr->seg
582 && addr_sorttab[mid]->value.addr.off <= addr->off) )
584 low = mid;
586 else
588 high = mid;
593 if (!nearest) return NULL;
595 if( rtn != NULL )
597 *rtn = nearest;
601 * Fill in the relevant bits to the structure so that we can
602 * locate the source and line for this bit of code.
604 if( source != NULL )
606 source->sourcefile = nearest->sourcefile;
607 if( nearest->linetab == NULL )
609 source->line = -1;
611 else
613 source->line = nearest->linetab[0].line_number;
617 lineinfo = "";
618 lineno = -1;
621 * Prepare to display the argument list. If ebp is specified, it is
622 * the framepointer for the function in question. If not specified,
623 * we don't want the arglist.
625 memset(arglist, '\0', sizeof(arglist));
626 if( ebp != 0 )
628 for(i=0; i < nearest->n_locals; i++ )
631 * If this is a register (offset == 0) or a local
632 * variable, we don't want to know about it.
634 if( nearest->local_vars[i].offset <= 0 )
636 continue;
639 ptr = (unsigned int *) (ebp + nearest->local_vars[i].offset);
640 if( arglist[0] == '\0' )
642 arglist[0] = '(';
644 else
646 strcat(arglist, ", ");
648 DEBUG_READ_MEM_VERBOSE(ptr, &val, sizeof(val));
649 sprintf(argtmp, "%s=0x%x", nearest->local_vars[i].name, val);
651 strcat(arglist, argtmp);
653 if( arglist[0] == '(' )
655 strcat(arglist, ")");
659 if( (nearest->sourcefile != NULL) && (flag == TRUE)
660 && (addr->off - nearest->value.addr.off < 0x100000) )
664 * Try and find the nearest line number to the current offset.
666 if( nearest->linetab != NULL )
668 low = 0;
669 high = nearest->n_lines;
670 while ((high - low) > 1)
672 mid = (high + low) / 2;
673 if (addr->off < nearest->linetab[mid].pc_offset.off)
674 high = mid;
675 else
676 low = mid;
678 lineno = nearest->linetab[low].line_number;
681 if( lineno != -1 )
683 sprintf(linebuff, ":%d", lineno);
684 lineinfo = linebuff;
685 if( source != NULL )
687 source->line = lineno;
691 /* Remove the path from the file name */
692 sourcefile = strrchr( nearest->sourcefile, '/' );
693 if (!sourcefile) sourcefile = nearest->sourcefile;
694 else sourcefile++;
696 if (addr->off == nearest->value.addr.off)
697 sprintf( name_buffer, "%s%s [%s%s]", nearest->name,
698 arglist, sourcefile, lineinfo);
699 else
700 sprintf( name_buffer, "%s+0x%lx%s [%s%s]", nearest->name,
701 addr->off - nearest->value.addr.off,
702 arglist, sourcefile, lineinfo );
704 else
706 if (addr->off == nearest->value.addr.off)
707 sprintf( name_buffer, "%s%s", nearest->name, arglist);
708 else {
709 if (addr->seg && (nearest->value.addr.seg!=addr->seg))
710 return NULL;
711 else
712 sprintf( name_buffer, "%s+0x%lx%s", nearest->name,
713 addr->off - nearest->value.addr.off, arglist);
716 return name_buffer;
720 /***********************************************************************
721 * DEBUG_ReadSymbolTable
723 * Read a symbol file into the hash table.
725 void DEBUG_ReadSymbolTable( const char * filename )
727 FILE * symbolfile;
728 DBG_VALUE value;
729 int nargs;
730 char type;
731 char * cpnt;
732 char buffer[256];
733 char name[256];
735 if (!(symbolfile = fopen(filename, "r")))
737 fprintf( stderr, "Unable to open symbol table %s\n", filename );
738 return;
741 fprintf( stderr, "Reading symbols from file %s\n", filename );
743 value.type = NULL;
744 value.addr.seg = 0;
745 value.addr.off = 0;
746 value.cookie = DV_TARGET;
748 while (1)
750 fgets( buffer, sizeof(buffer), symbolfile );
751 if (feof(symbolfile)) break;
753 /* Strip any text after a # sign (i.e. comments) */
754 cpnt = buffer;
755 while (*cpnt)
756 if(*cpnt++ == '#') { *cpnt = 0; break; }
758 /* Quietly ignore any lines that have just whitespace */
759 cpnt = buffer;
760 while(*cpnt)
762 if(*cpnt != ' ' && *cpnt != '\t') break;
763 cpnt++;
765 if (!(*cpnt) || *cpnt == '\n') continue;
767 nargs = sscanf(buffer, "%lx %c %s", &value.addr.off, &type, name);
768 DEBUG_AddSymbol( name, &value, NULL, SYM_WINE );
770 fclose(symbolfile);
774 /***********************************************************************
775 * DEBUG_LoadEntryPoints16
777 * Load the entry points of a Win16 module into the hash table.
779 static void DEBUG_LoadEntryPoints16( HMODULE16 hModule, NE_MODULE *pModule,
780 const char *name )
782 DBG_VALUE value;
783 char buffer[256];
784 FARPROC16 address;
785 unsigned char *cpnt = (unsigned char *)pModule + pModule->name_table;
787 value.type = NULL;
788 value.cookie = DV_TARGET;
789 value.addr.seg = 0;
790 value.addr.off = 0;
792 /* First search the resident names */
794 while (*cpnt)
796 cpnt += *cpnt + 1 + sizeof(WORD);
797 sprintf( buffer, "%s.%.*s", name, *cpnt, cpnt + 1 );
798 if ((address = NE_GetEntryPoint(hModule, *(WORD *)(cpnt + *cpnt + 1))))
800 value.addr.seg = HIWORD(address);
801 value.addr.off = LOWORD(address);
802 DEBUG_AddSymbol( buffer, &value, NULL, SYM_WIN32 | SYM_FUNC );
806 /* Now search the non-resident names table */
808 if (!pModule->nrname_handle) return; /* No non-resident table */
809 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
810 while (*cpnt)
812 cpnt += *cpnt + 1 + sizeof(WORD);
813 sprintf( buffer, "%s.%.*s", name, *cpnt, cpnt + 1 );
814 if ((address = NE_GetEntryPoint(hModule, *(WORD *)(cpnt + *cpnt + 1))))
816 value.addr.seg = HIWORD(address);
817 value.addr.off = LOWORD(address);
818 DEBUG_AddSymbol( buffer, &value, NULL, SYM_WIN32 | SYM_FUNC );
824 /***********************************************************************
825 * DEBUG_LoadEntryPoints32
827 * Load the entry points of a Win32 module into the hash table.
829 static void DEBUG_LoadEntryPoints32( HMODULE hModule, const char *name )
831 #define RVA(x) (hModule+(DWORD)(x))
833 DBG_VALUE value;
834 char buffer[256];
835 int i, j;
836 IMAGE_SECTION_HEADER *pe_seg;
837 IMAGE_EXPORT_DIRECTORY *exports;
838 IMAGE_DATA_DIRECTORY *dir;
839 WORD *ordinals;
840 void **functions;
841 const char **names;
843 value.type = NULL;
844 value.cookie = DV_TARGET;
845 value.addr.seg = 0;
846 value.addr.off = 0;
848 /* Add start of DLL */
850 value.addr.off = hModule;
851 DEBUG_AddSymbol( name, &value, NULL, SYM_WIN32 | SYM_FUNC );
853 /* Add entry point */
855 sprintf( buffer, "%s.EntryPoint", name );
856 value.addr.off = (DWORD)RVA_PTR( hModule, OptionalHeader.AddressOfEntryPoint );
857 DEBUG_AddSymbol( buffer, &value, NULL, SYM_WIN32 | SYM_FUNC );
859 /* Add start of sections */
861 pe_seg = PE_SECTIONS(hModule);
862 for (i = 0; i < PE_HEADER(hModule)->FileHeader.NumberOfSections; i++)
864 sprintf( buffer, "%s.%s", name, pe_seg->Name );
865 value.addr.off = RVA(pe_seg->VirtualAddress );
866 DEBUG_AddSymbol( buffer, &value, NULL, SYM_WIN32 | SYM_FUNC );
867 pe_seg++;
870 /* Add exported functions */
872 dir = &PE_HEADER(hModule)->OptionalHeader.
873 DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
874 if (dir->Size)
876 exports = (IMAGE_EXPORT_DIRECTORY *)RVA( dir->VirtualAddress );
877 ordinals = (WORD *)RVA( exports->AddressOfNameOrdinals );
878 names = (const char **)RVA( exports->AddressOfNames );
879 functions = (void **)RVA( exports->AddressOfFunctions );
881 for (i = 0; i < exports->NumberOfNames; i++)
883 if (!names[i]) continue;
884 sprintf( buffer, "%s.%s", name, (char *)RVA(names[i]) );
885 value.addr.off = RVA( functions[ordinals[i]] );
886 DEBUG_AddSymbol( buffer, &value, NULL, SYM_WIN32 | SYM_FUNC );
889 for (i = 0; i < exports->NumberOfFunctions; i++)
891 if (!functions[i]) continue;
892 /* Check if we already added it with a name */
893 for (j = 0; j < exports->NumberOfNames; j++)
894 if ((ordinals[j] == i) && names[j]) break;
895 if (j < exports->NumberOfNames) continue;
896 sprintf( buffer, "%s.%ld", name, i + exports->Base );
897 value.addr.off = (DWORD)RVA( functions[i] );
898 DEBUG_AddSymbol( buffer, &value, NULL, SYM_WIN32 | SYM_FUNC );
901 DEBUG_RegisterDebugInfo(hModule, name);
902 #undef RVA
905 typedef struct tag_lmr{
906 char* module_name;
907 BOOL is16;
908 struct tag_lmr* next;
909 } DBG_LoadedModuleRef;
911 typedef struct {
912 int rowcount;
913 int first;
914 const char* pfx;
915 } DBG_LEPData;
917 static BOOL DEBUG_LEPHelper(const char* mod_name, BOOL is16, DBG_LEPData* lep)
919 static DBG_LoadedModuleRef* lmr = NULL;
920 DBG_LoadedModuleRef* p;
921 DBG_LoadedModuleRef** pp1;
922 DBG_LoadedModuleRef* p2;
923 int len = strlen(mod_name);
924 int cmp;
926 for (p = lmr; p; p = p->next) {
927 cmp = strcasecmp(p->module_name, mod_name);
928 if (cmp == 0 && p->is16 == is16)
929 return FALSE;
930 if (cmp >= 0)
931 break;
934 if (!lep->first) {
935 if (lep->pfx) fprintf( stderr, lep->pfx );
936 fprintf( stderr, " " );
937 lep->first++;
938 lep->rowcount = 3;
941 if ((lep->rowcount + len) > 76)
943 fprintf( stderr, "\n ");
944 lep->rowcount = 3;
946 fprintf( stderr, " %s", mod_name );
947 lep->rowcount += len + 1;
949 p = DBG_alloc(sizeof(*lmr));
950 p->module_name = DBG_strdup(mod_name);
951 p->is16 = is16;
953 p2 = NULL;
954 for (pp1 = &lmr; *pp1; pp1 = &(*pp1)->next) {
955 if (strcasecmp((*pp1)->module_name, mod_name) > 0)
956 break;
957 p2 = *pp1;
959 if (p2 == NULL)
961 p->next = lmr;
962 lmr = p;
964 else if (*pp1 == NULL)
966 p->next = NULL;
967 *pp1 = p;
969 else
971 p->next = *pp1;
972 p2->next = p;
975 return TRUE;
978 /***********************************************************************
979 * DEBUG_LoadEntryPoints
981 * Load the entry points of all the modules into the hash table.
983 int DEBUG_LoadEntryPoints(const char* pfx)
985 MODULEENTRY entry;
986 NE_MODULE* pModule;
987 BOOL ok;
988 WINE_MODREF*wm;
989 DBG_LEPData lep;
990 PDB* current = PROCESS_Current();
992 lep.first = 0;
993 lep.pfx = pfx;
995 /* FIXME: we assume that a module is never removed from memory */
997 for (ok = ModuleFirst16(&entry); ok; ok = ModuleNext16(&entry))
999 if (!(pModule = NE_GetPtr( entry.hModule ))) continue;
1000 if (!(pModule->flags & NE_FFLAGS_WIN32) && /* NE module */
1001 DEBUG_LEPHelper( entry.szModule, TRUE, &lep ))
1002 DEBUG_LoadEntryPoints16( entry.hModule, pModule, entry.szModule );
1005 for (wm = current->modref_list; wm; wm=wm->next)
1007 if ((wm->flags & WINE_MODREF_INTERNAL))
1009 if (DEBUG_LEPHelper( wm->modname, FALSE, &lep ))
1010 DEBUG_LoadEntryPoints32( wm->module, wm->modname );
1013 if (lep.first) fprintf( stderr, " $");
1014 for (wm = current->modref_list; wm; wm=wm->next)
1016 if (!(wm->flags & WINE_MODREF_INTERNAL))
1018 if (DEBUG_LEPHelper( wm->modname, FALSE, &lep ))
1019 DEBUG_LoadEntryPoints32( wm->module, wm->modname );
1022 if (lep.first) fprintf( stderr, "\n" );
1023 return lep.first;
1027 void
1028 DEBUG_AddLineNumber( struct name_hash * func, int line_num,
1029 unsigned long offset )
1031 if( func == NULL )
1033 return;
1036 if( func->n_lines + 1 >= func->lines_alloc )
1038 func->lines_alloc += 64;
1039 func->linetab = DBG_realloc(func->linetab,
1040 func->lines_alloc * sizeof(WineLineNo));
1043 func->linetab[func->n_lines].line_number = line_num;
1044 func->linetab[func->n_lines].pc_offset.seg = func->value.addr.seg;
1045 func->linetab[func->n_lines].pc_offset.off = func->value.addr.off + offset;
1046 func->n_lines++;
1050 struct wine_locals *
1051 DEBUG_AddLocal( struct name_hash * func, int regno,
1052 int offset,
1053 int pc_start,
1054 int pc_end,
1055 char * name)
1057 if( func == NULL )
1059 return NULL;
1062 if( func->n_locals + 1 >= func->locals_alloc )
1064 func->locals_alloc += 32;
1065 func->local_vars = DBG_realloc(func->local_vars,
1066 func->locals_alloc * sizeof(WineLocals));
1069 func->local_vars[func->n_locals].regno = regno;
1070 func->local_vars[func->n_locals].offset = offset;
1071 func->local_vars[func->n_locals].pc_start = pc_start;
1072 func->local_vars[func->n_locals].pc_end = pc_end;
1073 func->local_vars[func->n_locals].name = DBG_strdup(name);
1074 func->local_vars[func->n_locals].type = NULL;
1075 func->n_locals++;
1077 return &func->local_vars[func->n_locals - 1];
1080 void
1081 DEBUG_DumpHashInfo(void)
1083 int i;
1084 int depth;
1085 struct name_hash *nh;
1088 * Utility function to dump stats about the hash table.
1090 for(i=0; i<NR_NAME_HASH; i++)
1092 depth = 0;
1093 for (nh = name_hash_table[i]; nh; nh = nh->next)
1095 depth++;
1097 fprintf(stderr, "Bucket %d: %d\n", i, depth);
1101 /***********************************************************************
1102 * DEBUG_CheckLinenoStatus
1104 * Find the symbol nearest to a given address.
1105 * If ebp is specified as non-zero, it means we should dump the argument
1106 * list into the string we return as well.
1108 int DEBUG_CheckLinenoStatus( const DBG_ADDR *addr)
1110 struct name_hash * nearest = NULL;
1111 int mid, high, low;
1113 if( sortlist_valid == FALSE )
1115 DEBUG_ResortSymbols();
1119 * Binary search to find closest symbol.
1121 low = 0;
1122 high = sorttab_nsym;
1123 if( addr_sorttab[0]->value.addr.seg > addr->seg
1124 || ( addr_sorttab[0]->value.addr.seg == addr->seg
1125 && addr_sorttab[0]->value.addr.off > addr->off) )
1127 nearest = NULL;
1129 else if( addr_sorttab[high - 1]->value.addr.seg < addr->seg
1130 || ( addr_sorttab[high - 1]->value.addr.seg == addr->seg
1131 && addr_sorttab[high - 1]->value.addr.off < addr->off) )
1133 nearest = addr_sorttab[high - 1];
1135 else
1137 while(1==1)
1139 mid = (high + low)/2;
1140 if( mid == low )
1143 * See if there are any other entries that might also
1144 * have the same address, and would also have a line
1145 * number table.
1147 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
1149 if( (addr_sorttab[mid - 1]->value.addr.seg ==
1150 addr_sorttab[mid]->value.addr.seg)
1151 && (addr_sorttab[mid - 1]->value.addr.off ==
1152 addr_sorttab[mid]->value.addr.off)
1153 && (addr_sorttab[mid - 1]->linetab != NULL) )
1155 mid--;
1159 if( (mid < sorttab_nsym - 1)
1160 && (addr_sorttab[mid]->linetab == NULL) )
1162 if( (addr_sorttab[mid + 1]->value.addr.seg ==
1163 addr_sorttab[mid]->value.addr.seg)
1164 && (addr_sorttab[mid + 1]->value.addr.off ==
1165 addr_sorttab[mid]->value.addr.off)
1166 && (addr_sorttab[mid + 1]->linetab != NULL) )
1168 mid++;
1171 nearest = addr_sorttab[mid];
1172 #if 0
1173 fprintf(stderr, "Found %x:%x when looking for %x:%x %x %s\n",
1174 addr_sorttab[mid ]->value.addr.seg,
1175 addr_sorttab[mid ]->value.addr.off,
1176 addr->seg, addr->off,
1177 addr_sorttab[mid ]->linetab,
1178 addr_sorttab[mid ]->name);
1179 #endif
1180 break;
1182 if( (addr_sorttab[mid]->value.addr.seg < addr->seg)
1183 || ( addr_sorttab[mid]->value.addr.seg == addr->seg
1184 && addr_sorttab[mid]->value.addr.off <= addr->off) )
1186 low = mid;
1188 else
1190 high = mid;
1195 if (!nearest) return FUNC_HAS_NO_LINES;
1197 if( nearest->flags & SYM_STEP_THROUGH )
1200 * This will cause us to keep single stepping until
1201 * we get to the other side somewhere.
1203 return NOT_ON_LINENUMBER;
1206 if( (nearest->flags & SYM_TRAMPOLINE) )
1209 * This will cause us to keep single stepping until
1210 * we get to the other side somewhere.
1212 return FUNC_IS_TRAMPOLINE;
1215 if( nearest->linetab == NULL )
1217 return FUNC_HAS_NO_LINES;
1222 * We never want to stop on the first instruction of a function
1223 * even if it has it's own linenumber. Let the thing keep running
1224 * until it gets past the function prologue. We only do this if there
1225 * is more than one line number for the function, of course.
1227 if( nearest->value.addr.off == addr->off && nearest->n_lines > 1 )
1229 return NOT_ON_LINENUMBER;
1232 if( (nearest->sourcefile != NULL)
1233 && (addr->off - nearest->value.addr.off < 0x100000) )
1235 low = 0;
1236 high = nearest->n_lines;
1237 while ((high - low) > 1)
1239 mid = (high + low) / 2;
1240 if (addr->off < nearest->linetab[mid].pc_offset.off) high = mid;
1241 else low = mid;
1243 if (addr->off == nearest->linetab[low].pc_offset.off)
1244 return AT_LINENUMBER;
1245 else
1246 return NOT_ON_LINENUMBER;
1249 return FUNC_HAS_NO_LINES;
1252 /***********************************************************************
1253 * DEBUG_GetFuncInfo
1255 * Find the symbol nearest to a given address.
1256 * Returns sourcefile name and line number in a format that the listing
1257 * handler can deal with.
1259 void
1260 DEBUG_GetFuncInfo( struct list_id * ret, const char * filename,
1261 const char * name)
1263 char buffer[256];
1264 char * pnt;
1265 struct name_hash *nh;
1267 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
1269 if( filename != NULL )
1272 if( nh->sourcefile == NULL )
1274 continue;
1277 pnt = strrchr(nh->sourcefile, '/');
1278 if( strcmp(nh->sourcefile, filename) != 0
1279 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1281 continue;
1284 if (!strcmp(nh->name, name)) break;
1287 if (!nh && (name[0] != '_'))
1289 buffer[0] = '_';
1290 strcpy(buffer+1, name);
1291 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
1293 if( filename != NULL )
1295 if( nh->sourcefile == NULL )
1297 continue;
1300 pnt = strrchr(nh->sourcefile, '/');
1301 if( strcmp(nh->sourcefile, filename) != 0
1302 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1304 continue;
1307 if (!strcmp(nh->name, buffer)) break;
1311 if( !nh )
1313 if( filename != NULL )
1315 fprintf(stderr, "No such function %s in %s\n", name, filename);
1317 else
1319 fprintf(stderr, "No such function %s\n", name);
1321 ret->sourcefile = NULL;
1322 ret->line = -1;
1323 return;
1326 ret->sourcefile = nh->sourcefile;
1329 * Search for the specific line number. If we don't find it,
1330 * then return FALSE.
1332 if( nh->linetab == NULL )
1334 ret->line = -1;
1336 else
1338 ret->line = nh->linetab[0].line_number;
1342 /***********************************************************************
1343 * DEBUG_GetStackSymbolValue
1345 * Get the address of a named symbol from the current stack frame.
1347 static
1348 BOOL DEBUG_GetStackSymbolValue( const char * name, DBG_VALUE *value )
1350 struct name_hash * curr_func;
1351 unsigned int ebp;
1352 unsigned int eip;
1353 int i;
1355 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1357 return FALSE;
1360 for(i=0; i < curr_func->n_locals; i++ )
1363 * Test the range of validity of the local variable. This
1364 * comes up with RBRAC/LBRAC stabs in particular.
1366 if( (curr_func->local_vars[i].pc_start != 0)
1367 && ((eip - curr_func->value.addr.off)
1368 < curr_func->local_vars[i].pc_start) )
1370 continue;
1373 if( (curr_func->local_vars[i].pc_end != 0)
1374 && ((eip - curr_func->value.addr.off)
1375 > curr_func->local_vars[i].pc_end) )
1377 continue;
1380 if( strcmp(name, curr_func->local_vars[i].name) == 0 )
1383 * OK, we found it. Now figure out what to do with this.
1385 if( curr_func->local_vars[i].regno != 0 )
1388 * Register variable. Point to DEBUG_context field.
1390 value->addr.off = ((DWORD)&DEBUG_context) +
1391 reg_ofs[curr_func->local_vars[i].regno - 1];
1392 value->cookie = DV_HOST;
1394 else
1396 value->addr.off = ebp + curr_func->local_vars[i].offset;
1397 value->cookie = DV_TARGET;
1399 value->addr.seg = 0;
1400 value->type = curr_func->local_vars[i].type;
1402 return TRUE;
1406 return FALSE;
1410 DEBUG_InfoLocals(void)
1412 struct name_hash * curr_func;
1413 unsigned int ebp;
1414 unsigned int eip;
1415 int i;
1416 unsigned int * ptr;
1417 unsigned int val;
1419 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1421 return FALSE;
1424 for(i=0; i < curr_func->n_locals; i++ )
1427 * Test the range of validity of the local variable. This
1428 * comes up with RBRAC/LBRAC stabs in particular.
1430 if( (curr_func->local_vars[i].pc_start != 0)
1431 && ((eip - curr_func->value.addr.off)
1432 < curr_func->local_vars[i].pc_start) )
1434 continue;
1437 if( (curr_func->local_vars[i].pc_end != 0)
1438 && ((eip - curr_func->value.addr.off)
1439 > curr_func->local_vars[i].pc_end) )
1441 continue;
1444 if( curr_func->local_vars[i].regno != 0 )
1446 ptr = (unsigned int *)(((DWORD)&DEBUG_context)
1447 + reg_ofs[curr_func->local_vars[i].regno - 1]);
1448 fprintf(stderr, "%s:%s (optimized into register $%s) == 0x%8.8x\n",
1449 curr_func->name, curr_func->local_vars[i].name,
1450 reg_name[curr_func->local_vars[i].regno - 1],
1451 *ptr);
1453 else
1455 DEBUG_READ_MEM_VERBOSE((void*)(ebp + curr_func->local_vars[i].offset),
1456 &val, sizeof(val));
1457 fprintf(stderr, "%s:%s == 0x%8.8x\n",
1458 curr_func->name, curr_func->local_vars[i].name, val);
1462 return TRUE;
1466 DEBUG_SetSymbolSize(struct name_hash * sym, unsigned int len)
1468 sym->symbol_size = len;
1470 return TRUE;
1474 DEBUG_SetSymbolBPOff(struct name_hash * sym, unsigned int off)
1476 sym->breakpoint_offset = off;
1478 return TRUE;
1482 DEBUG_GetSymbolAddr(struct name_hash * sym, DBG_ADDR * addr)
1485 *addr = sym->value.addr;
1487 return TRUE;
1490 int DEBUG_SetLocalSymbolType(struct wine_locals * sym, struct datatype * type)
1492 sym->type = type;
1494 return TRUE;