Raise an exception when a critical section wait failed.
[wine/multimedia.git] / debugger / hash.c
blob8bc7cdb360e67aa7d9d07d1f2676c52f9ac7a276
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 "selectors.h"
18 #include "debugger.h"
19 #include "toolhelp.h"
21 #define NR_NAME_HASH 16384
22 #ifndef PATH_MAX
23 #define PATH_MAX _MAX_PATH
24 #endif
26 #ifdef __i386__
27 static char * reg_name[] =
29 "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"
32 static unsigned reg_ofs[] =
34 FIELD_OFFSET(CONTEXT, Eax), FIELD_OFFSET(CONTEXT, Ecx),
35 FIELD_OFFSET(CONTEXT, Edx), FIELD_OFFSET(CONTEXT, Ebx),
36 FIELD_OFFSET(CONTEXT, Esp), FIELD_OFFSET(CONTEXT, Ebp),
37 FIELD_OFFSET(CONTEXT, Esi), FIELD_OFFSET(CONTEXT, Edi)
39 #else
40 static char * reg_name[] = { NULL }; /* FIXME */
41 static unsigned reg_ofs[] = { 0 };
42 #endif
45 struct name_hash
47 struct name_hash * next; /* Used to look up within name hash */
48 char * name;
49 char * sourcefile;
51 int n_locals;
52 int locals_alloc;
53 WineLocals * local_vars;
55 int n_lines;
56 int lines_alloc;
57 WineLineNo * linetab;
59 DBG_ADDR addr;
60 unsigned short flags;
61 unsigned short breakpoint_offset;
62 unsigned int symbol_size;
66 static BOOL DEBUG_GetStackSymbolValue( const char * name, DBG_ADDR *addr );
67 static int sortlist_valid = FALSE;
69 static int sorttab_nsym;
70 static struct name_hash ** addr_sorttab = NULL;
72 static struct name_hash * name_hash_table[NR_NAME_HASH];
74 static unsigned int name_hash( const char * name )
76 unsigned int hash = 0;
77 unsigned int tmp;
78 const char * p;
80 p = name;
82 while (*p)
84 hash = (hash << 4) + *p++;
86 if( (tmp = (hash & 0xf0000000)) )
88 hash ^= tmp >> 24;
90 hash &= ~tmp;
92 return hash % NR_NAME_HASH;
95 int
96 DEBUG_cmp_sym(const void * p1, const void * p2)
98 struct name_hash ** name1 = (struct name_hash **) p1;
99 struct name_hash ** name2 = (struct name_hash **) p2;
101 if( ((*name1)->flags & SYM_INVALID) != 0 )
103 return -1;
106 if( ((*name2)->flags & SYM_INVALID) != 0 )
108 return 1;
111 if( (*name1)->addr.seg > (*name2)->addr.seg )
113 return 1;
116 if( (*name1)->addr.seg < (*name2)->addr.seg )
118 return -1;
121 if( (*name1)->addr.off > (*name2)->addr.off )
123 return 1;
126 if( (*name1)->addr.off < (*name2)->addr.off )
128 return -1;
131 return 0;
134 /***********************************************************************
135 * DEBUG_ResortSymbols
137 * Rebuild sorted list of symbols.
139 static
140 void
141 DEBUG_ResortSymbols()
143 struct name_hash *nh;
144 int nsym = 0;
145 int i;
147 for(i=0; i<NR_NAME_HASH; i++)
149 for (nh = name_hash_table[i]; nh; nh = nh->next)
151 if( (nh->flags & SYM_INVALID) == 0 )
152 nsym++;
153 else
154 fprintf( stderr, "Symbol %s is invalid\n", nh->name );
158 sorttab_nsym = nsym;
159 if( nsym == 0 )
161 return;
164 addr_sorttab = (struct name_hash **) DBG_realloc(addr_sorttab,
165 nsym * sizeof(struct name_hash *));
167 nsym = 0;
168 for(i=0; i<NR_NAME_HASH; i++)
170 for (nh = name_hash_table[i]; nh; nh = nh->next)
172 if( (nh->flags & SYM_INVALID) == 0 )
173 addr_sorttab[nsym++] = nh;
177 qsort(addr_sorttab, nsym,
178 sizeof(struct name_hash *), DEBUG_cmp_sym);
179 sortlist_valid = TRUE;
183 /***********************************************************************
184 * DEBUG_AddSymbol
186 * Add a symbol to the table.
188 struct name_hash *
189 DEBUG_AddSymbol( const char * name, const DBG_ADDR *addr, const char * source,
190 int flags)
192 struct name_hash * new;
193 struct name_hash *nh;
194 static char prev_source[PATH_MAX] = {'\0', };
195 static char * prev_duped_source = NULL;
196 char * c;
197 int hash;
199 hash = name_hash(name);
200 for (nh = name_hash_table[hash]; nh; nh = nh->next)
202 if( ((nh->flags & SYM_INVALID) != 0) && strcmp(name, nh->name) == 0 )
204 nh->addr.off = addr->off;
205 nh->addr.seg = addr->seg;
206 if( nh->addr.type == NULL && addr->type != NULL )
208 nh->addr.type = addr->type;
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;
217 return nh;
219 if (nh->addr.seg == addr->seg &&
220 nh->addr.off == addr->off &&
221 strcmp(name, nh->name) == 0 )
223 return nh;
228 * First see if we already have an entry for this symbol. If so
229 * return it, so we don't end up with duplicates.
232 new = (struct name_hash *) DBG_alloc(sizeof(struct name_hash));
233 new->addr = *addr;
234 new->name = DBG_strdup(name);
236 if( source != NULL )
239 * This is an enhancement to reduce memory consumption. The idea
240 * is that we duplicate a given string only once. This is a big
241 * win if there are lots of symbols defined in a given source file.
243 if( strcmp(source, prev_source) == 0 )
245 new->sourcefile = prev_duped_source;
247 else
249 strcpy(prev_source, source);
250 prev_duped_source = new->sourcefile = DBG_strdup(source);
253 else
255 new->sourcefile = NULL;
258 new->n_lines = 0;
259 new->lines_alloc = 0;
260 new->linetab = NULL;
262 new->n_locals = 0;
263 new->locals_alloc = 0;
264 new->local_vars = NULL;
266 new->flags = flags;
267 new->next = NULL;
269 /* Now insert into the hash table */
270 new->next = name_hash_table[hash];
271 name_hash_table[hash] = new;
274 * Check some heuristics based upon the file name to see whether
275 * we want to step through this guy or not. These are machine generated
276 * assembly files that are used to translate between the MS way of
277 * calling things and the GCC way of calling things. In general we
278 * always want to step through.
280 if( source != NULL )
282 c = strrchr(source, '.');
283 if( c != NULL && strcmp(c, ".s") == 0 )
285 c = strrchr(source, '/');
286 if( c != NULL )
288 c++;
289 if( (strcmp(c, "callfrom16.s") == 0)
290 || (strcmp(c, "callto16.s") == 0)
291 || (strcmp(c, "call32.s") == 0) )
293 new->flags |= SYM_TRAMPOLINE;
299 sortlist_valid = FALSE;
300 return new;
303 BOOL DEBUG_Normalize(struct name_hash * nh )
307 * We aren't adding any more locals or linenumbers to this function.
308 * Free any spare memory that we might have allocated.
310 if( nh == NULL )
312 return TRUE;
315 if( nh->n_locals != nh->locals_alloc )
317 nh->locals_alloc = nh->n_locals;
318 nh->local_vars = DBG_realloc(nh->local_vars,
319 nh->locals_alloc * sizeof(WineLocals));
322 if( nh->n_lines != nh->lines_alloc )
324 nh->lines_alloc = nh->n_lines;
325 nh->linetab = DBG_realloc(nh->linetab,
326 nh->lines_alloc * sizeof(WineLineNo));
329 return TRUE;
332 /***********************************************************************
333 * DEBUG_GetSymbolValue
335 * Get the address of a named symbol.
337 BOOL DEBUG_GetSymbolValue( const char * name, const int lineno,
338 DBG_ADDR *addr, int bp_flag )
340 char buffer[256];
341 struct name_hash *nh;
343 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
345 if( (nh->flags & SYM_INVALID) != 0 )
347 continue;
350 if (!strcmp(nh->name, name)) break;
353 if (!nh && (name[0] != '_'))
355 buffer[0] = '_';
356 strcpy(buffer+1, name);
357 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
359 if( (nh->flags & SYM_INVALID) != 0 )
361 continue;
363 if (!strcmp(nh->name, buffer)) break;
368 * If we don't have anything here, then try and see if this
369 * is a local symbol to the current stack frame. No matter
370 * what, we have nothing more to do, so we let that function
371 * decide what we ultimately return.
373 if (!nh)
375 return DEBUG_GetStackSymbolValue(name, addr);
378 return DEBUG_GetLineNumberAddr( nh, lineno, addr, bp_flag );
381 /***********************************************************************
382 * DEBUG_GetLineNumberAddr
384 * Get the address of a named symbol.
386 BOOL DEBUG_GetLineNumberAddr( struct name_hash * nh, const int lineno,
387 DBG_ADDR *addr, int bp_flag )
389 int i;
391 if( lineno == -1 )
393 *addr = nh->addr;
394 if( bp_flag )
396 addr->off += nh->breakpoint_offset;
399 else
402 * Search for the specific line number. If we don't find it,
403 * then return FALSE.
405 if( nh->linetab == NULL )
407 return FALSE;
410 for(i=0; i < nh->n_lines; i++ )
412 if( nh->linetab[i].line_number == lineno )
414 *addr = nh->linetab[i].pc_offset;
415 return TRUE;
420 * This specific line number not found.
422 return FALSE;
425 return TRUE;
429 /***********************************************************************
430 * DEBUG_SetSymbolValue
432 * Set the address of a named symbol.
434 BOOL DEBUG_SetSymbolValue( const char * name, const DBG_ADDR *addr )
436 char buffer[256];
437 struct name_hash *nh;
439 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
440 if (!strcmp(nh->name, name)) break;
442 if (!nh && (name[0] != '_'))
444 buffer[0] = '_';
445 strcpy(buffer+1, name);
446 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
447 if (!strcmp(nh->name, buffer)) break;
450 if (!nh) return FALSE;
451 nh->addr = *addr;
452 nh->flags &= SYM_INVALID;
453 DBG_FIX_ADDR_SEG( &nh->addr, DS_reg(&DEBUG_context) );
454 return TRUE;
458 /***********************************************************************
459 * DEBUG_FindNearestSymbol
461 * Find the symbol nearest to a given address.
462 * If ebp is specified as non-zero, it means we should dump the argument
463 * list into the string we return as well.
465 const char * DEBUG_FindNearestSymbol( const DBG_ADDR *addr, int flag,
466 struct name_hash ** rtn,
467 unsigned int ebp,
468 struct list_id * source)
470 static char name_buffer[MAX_PATH + 256];
471 static char arglist[1024];
472 static char argtmp[256];
473 struct name_hash * nearest = NULL;
474 int mid, high, low;
475 unsigned int * ptr;
476 int lineno;
477 char * lineinfo, *sourcefile;
478 int i;
479 char linebuff[16];
481 if( rtn != NULL )
483 *rtn = NULL;
486 if( source != NULL )
488 source->sourcefile = NULL;
489 source->line = -1;
492 if( sortlist_valid == FALSE )
494 DEBUG_ResortSymbols();
497 if( sortlist_valid == FALSE )
499 return NULL;
503 * FIXME - use the binary search that we added to
504 * the function DEBUG_CheckLinenoStatus. Better yet, we should
505 * probably keep some notion of the current function so we don't
506 * have to search every time.
509 * Binary search to find closest symbol.
511 low = 0;
512 high = sorttab_nsym;
513 if( addr_sorttab[0]->addr.seg > addr->seg
514 || ( addr_sorttab[0]->addr.seg == addr->seg
515 && addr_sorttab[0]->addr.off > addr->off) )
517 nearest = NULL;
519 else if( addr_sorttab[high - 1]->addr.seg < addr->seg
520 || ( addr_sorttab[high - 1]->addr.seg == addr->seg
521 && addr_sorttab[high - 1]->addr.off < addr->off) )
523 nearest = addr_sorttab[high - 1];
525 else
527 while(1==1)
529 mid = (high + low)/2;
530 if( mid == low )
533 * See if there are any other entries that might also
534 * have the same address, and would also have a line
535 * number table.
537 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
539 if( (addr_sorttab[mid - 1]->addr.seg ==
540 addr_sorttab[mid]->addr.seg)
541 && (addr_sorttab[mid - 1]->addr.off ==
542 addr_sorttab[mid]->addr.off)
543 && (addr_sorttab[mid - 1]->linetab != NULL) )
545 mid--;
549 if( (mid < sorttab_nsym - 1)
550 && (addr_sorttab[mid]->linetab == NULL) )
552 if( (addr_sorttab[mid + 1]->addr.seg ==
553 addr_sorttab[mid]->addr.seg)
554 && (addr_sorttab[mid + 1]->addr.off ==
555 addr_sorttab[mid]->addr.off)
556 && (addr_sorttab[mid + 1]->linetab != NULL) )
558 mid++;
561 nearest = addr_sorttab[mid];
562 #if 0
563 fprintf(stderr, "Found %x:%x when looking for %x:%x %x %s\n",
564 addr_sorttab[mid ]->addr.seg,
565 addr_sorttab[mid ]->addr.off,
566 addr->seg, addr->off,
567 addr_sorttab[mid ]->linetab,
568 addr_sorttab[mid ]->name);
569 #endif
570 break;
572 if( (addr_sorttab[mid]->addr.seg < addr->seg)
573 || ( addr_sorttab[mid]->addr.seg == addr->seg
574 && addr_sorttab[mid]->addr.off <= addr->off) )
576 low = mid;
578 else
580 high = mid;
585 if (!nearest) return NULL;
587 if( rtn != NULL )
589 *rtn = nearest;
593 * Fill in the relevant bits to the structure so that we can
594 * locate the source and line for this bit of code.
596 if( source != NULL )
598 source->sourcefile = nearest->sourcefile;
599 if( nearest->linetab == NULL )
601 source->line = -1;
603 else
605 source->line = nearest->linetab[0].line_number;
609 lineinfo = "";
610 lineno = -1;
613 * Prepare to display the argument list. If ebp is specified, it is
614 * the framepointer for the function in question. If not specified,
615 * we don't want the arglist.
617 memset(arglist, '\0', sizeof(arglist));
618 if( ebp != 0 )
620 for(i=0; i < nearest->n_locals; i++ )
623 * If this is a register (offset == 0) or a local
624 * variable, we don't want to know about it.
626 if( nearest->local_vars[i].offset <= 0 )
628 continue;
631 ptr = (unsigned int *) (ebp + nearest->local_vars[i].offset);
632 if( arglist[0] == '\0' )
634 arglist[0] = '(';
636 else
638 strcat(arglist, ", ");
641 sprintf(argtmp, "%s=0x%x", nearest->local_vars[i].name,
642 *ptr);
643 strcat(arglist, argtmp);
645 if( arglist[0] == '(' )
647 strcat(arglist, ")");
651 if( (nearest->sourcefile != NULL) && (flag == TRUE)
652 && (addr->off - nearest->addr.off < 0x100000) )
656 * Try and find the nearest line number to the current offset.
658 if( nearest->linetab != NULL )
660 low = 0;
661 high = nearest->n_lines;
662 while ((high - low) > 1)
664 mid = (high + low) / 2;
665 if (addr->off < nearest->linetab[mid].pc_offset.off)
666 high = mid;
667 else
668 low = mid;
670 lineno = nearest->linetab[low].line_number;
673 if( lineno != -1 )
675 sprintf(linebuff, ":%d", lineno);
676 lineinfo = linebuff;
677 if( source != NULL )
679 source->line = lineno;
683 /* Remove the path from the file name */
684 sourcefile = strrchr( nearest->sourcefile, '/' );
685 if (!sourcefile) sourcefile = nearest->sourcefile;
686 else sourcefile++;
688 if (addr->off == nearest->addr.off)
689 sprintf( name_buffer, "%s%s [%s%s]", nearest->name,
690 arglist, sourcefile, lineinfo);
691 else
692 sprintf( name_buffer, "%s+0x%lx%s [%s%s]", nearest->name,
693 addr->off - nearest->addr.off,
694 arglist, sourcefile, lineinfo );
696 else
698 if (addr->off == nearest->addr.off)
699 sprintf( name_buffer, "%s%s", nearest->name, arglist);
700 else {
701 if (addr->seg && (nearest->addr.seg!=addr->seg))
702 return NULL;
703 else
704 sprintf( name_buffer, "%s+0x%lx%s", nearest->name,
705 addr->off - nearest->addr.off, arglist);
708 return name_buffer;
712 /***********************************************************************
713 * DEBUG_ReadSymbolTable
715 * Read a symbol file into the hash table.
717 void DEBUG_ReadSymbolTable( const char * filename )
719 FILE * symbolfile;
720 DBG_ADDR addr = { 0, 0 };
721 int nargs;
722 char type;
723 char * cpnt;
724 char buffer[256];
725 char name[256];
727 if (!(symbolfile = fopen(filename, "r")))
729 fprintf( stderr, "Unable to open symbol table %s\n", filename );
730 return;
733 fprintf( stderr, "Reading symbols from file %s\n", filename );
735 while (1)
737 fgets( buffer, sizeof(buffer), symbolfile );
738 if (feof(symbolfile)) break;
740 /* Strip any text after a # sign (i.e. comments) */
741 cpnt = buffer;
742 while (*cpnt)
743 if(*cpnt++ == '#') { *cpnt = 0; break; }
745 /* Quietly ignore any lines that have just whitespace */
746 cpnt = buffer;
747 while(*cpnt)
749 if(*cpnt != ' ' && *cpnt != '\t') break;
750 cpnt++;
752 if (!(*cpnt) || *cpnt == '\n') continue;
754 nargs = sscanf(buffer, "%lx %c %s", &addr.off, &type, name);
755 DEBUG_AddSymbol( name, &addr, NULL, SYM_WINE );
757 fclose(symbolfile);
761 /***********************************************************************
762 * DEBUG_LoadEntryPoints16
764 * Load the entry points of a Win16 module into the hash table.
766 static void DEBUG_LoadEntryPoints16( HMODULE16 hModule, NE_MODULE *pModule,
767 const char *name )
769 DBG_ADDR addr;
770 char buffer[256];
771 FARPROC16 address;
773 /* First search the resident names */
775 unsigned char *cpnt = (unsigned char *)pModule + pModule->name_table;
776 while (*cpnt)
778 cpnt += *cpnt + 1 + sizeof(WORD);
779 sprintf( buffer, "%s.%.*s", name, *cpnt, cpnt + 1 );
780 if ((address = NE_GetEntryPoint(hModule, *(WORD *)(cpnt + *cpnt + 1))))
782 addr.seg = HIWORD(address);
783 addr.off = LOWORD(address);
784 addr.type = NULL;
785 DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
789 /* Now search the non-resident names table */
791 if (!pModule->nrname_handle) return; /* No non-resident table */
792 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
793 while (*cpnt)
795 cpnt += *cpnt + 1 + sizeof(WORD);
796 sprintf( buffer, "%s.%.*s", name, *cpnt, cpnt + 1 );
797 if ((address = NE_GetEntryPoint(hModule, *(WORD *)(cpnt + *cpnt + 1))))
799 addr.seg = HIWORD(address);
800 addr.off = LOWORD(address);
801 addr.type = NULL;
802 DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
808 /***********************************************************************
809 * DEBUG_LoadEntryPoints32
811 * Load the entry points of a Win32 module into the hash table.
813 static void DEBUG_LoadEntryPoints32( HMODULE hModule, const char *name )
815 #define RVA(x) (hModule+(DWORD)(x))
817 DBG_ADDR addr;
818 char buffer[256];
819 int i, j;
820 IMAGE_SECTION_HEADER *pe_seg;
821 IMAGE_EXPORT_DIRECTORY *exports;
822 IMAGE_DATA_DIRECTORY *dir;
823 WORD *ordinals;
824 void **functions;
825 const char **names;
827 addr.seg = 0;
828 addr.type = NULL;
830 /* Add start of DLL */
832 addr.off = hModule;
833 DEBUG_AddSymbol( name, &addr, NULL, SYM_WIN32 | SYM_FUNC );
835 /* Add entry point */
837 sprintf( buffer, "%s.EntryPoint", name );
838 addr.off = (DWORD)RVA_PTR( hModule, OptionalHeader.AddressOfEntryPoint );
839 DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
841 /* Add start of sections */
843 pe_seg = PE_SECTIONS(hModule);
844 for (i = 0; i < PE_HEADER(hModule)->FileHeader.NumberOfSections; i++)
846 sprintf( buffer, "%s.%s", name, pe_seg->Name );
847 addr.off = RVA(pe_seg->VirtualAddress );
848 DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
849 pe_seg++;
852 /* Add exported functions */
854 dir = &PE_HEADER(hModule)->OptionalHeader.
855 DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
856 if (dir->Size)
858 exports = (IMAGE_EXPORT_DIRECTORY *)RVA( dir->VirtualAddress );
859 ordinals = (WORD *)RVA( exports->AddressOfNameOrdinals );
860 names = (const char **)RVA( exports->AddressOfNames );
861 functions = (void **)RVA( exports->AddressOfFunctions );
863 for (i = 0; i < exports->NumberOfNames; i++)
865 if (!names[i]) continue;
866 sprintf( buffer, "%s.%s", name, (char *)RVA(names[i]) );
867 addr.off = RVA( functions[ordinals[i]] );
868 DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
871 for (i = 0; i < exports->NumberOfFunctions; i++)
873 if (!functions[i]) continue;
874 /* Check if we already added it with a name */
875 for (j = 0; j < exports->NumberOfNames; j++)
876 if ((ordinals[j] == i) && names[j]) break;
877 if (j < exports->NumberOfNames) continue;
878 sprintf( buffer, "%s.%ld", name, i + exports->Base );
879 addr.off = (DWORD)RVA( functions[i] );
880 DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
883 DEBUG_RegisterDebugInfo(hModule, name);
884 #undef RVA
887 typedef struct tag_lmr{
888 char* module_name;
889 BOOL is16;
890 struct tag_lmr* next;
891 } DBG_LoadedModuleRef;
893 typedef struct {
894 int rowcount;
895 int first;
896 const char* pfx;
897 } DBG_LEPData;
899 static BOOL DEBUG_LEPHelper(const char* mod_name, BOOL is16, DBG_LEPData* lep)
901 static DBG_LoadedModuleRef* lmr = NULL;
902 DBG_LoadedModuleRef* p;
903 DBG_LoadedModuleRef** pp1;
904 DBG_LoadedModuleRef* p2;
905 int len = strlen(mod_name);
906 int cmp;
908 for (p = lmr; p; p = p->next) {
909 cmp = strcmp(p->module_name, mod_name);
910 if (cmp < 0)
911 continue;
912 if (cmp == 0) {
913 if (p->is16 == is16)
914 return FALSE;
915 continue;
917 break;
920 if (!lep->first) {
921 if (lep->pfx) fprintf( stderr, lep->pfx );
922 fprintf( stderr, " " );
923 lep->first++;
924 lep->rowcount = 3;
927 if ((lep->rowcount + len) > 76)
929 fprintf( stderr, "\n ");
930 lep->rowcount = 3;
932 fprintf( stderr, " %s", mod_name );
933 lep->rowcount += len + 1;
935 p = DBG_alloc(sizeof(*lmr));
936 p->module_name = DBG_strdup(mod_name);
937 p->is16 = is16;
939 p2 = NULL;
940 for (pp1 = &lmr; *pp1; pp1 = &(*pp1)->next) {
941 if (strcmp((*pp1)->module_name, mod_name) > 0)
942 break;
943 p2 = *pp1;
945 if (p2 == NULL)
947 p->next = lmr;
948 lmr = p;
950 else if (*pp1 == NULL)
952 p->next = NULL;
953 *pp1 = p;
955 else
957 p->next = *pp1;
958 p2->next = p;
961 return TRUE;
964 /***********************************************************************
965 * DEBUG_LoadEntryPoints
967 * Load the entry points of all the modules into the hash table.
969 int DEBUG_LoadEntryPoints(const char* pfx)
971 MODULEENTRY entry;
972 NE_MODULE* pModule;
973 BOOL ok;
974 WINE_MODREF*wm;
975 DBG_LEPData lep;
977 lep.first = 0;
978 lep.pfx = pfx;
980 /* FIXME: we assume that a module is never removed from memory */
982 for (ok = ModuleFirst16(&entry); ok; ok = ModuleNext16(&entry))
984 if (!(pModule = NE_GetPtr( entry.hModule ))) continue;
985 if (!(pModule->flags & NE_FFLAGS_WIN32) && /* NE module */
986 DEBUG_LEPHelper( entry.szModule, TRUE, &lep ))
987 DEBUG_LoadEntryPoints16( entry.hModule, pModule, entry.szModule );
989 for (wm=PROCESS_Current()->modref_list;wm;wm=wm->next)
991 if (DEBUG_LEPHelper( wm->modname, FALSE, &lep ))
992 DEBUG_LoadEntryPoints32( wm->module, wm->modname );
994 if (lep.first) fprintf( stderr, "\n" );
995 return lep.first;
999 void
1000 DEBUG_AddLineNumber( struct name_hash * func, int line_num,
1001 unsigned long offset )
1003 if( func == NULL )
1005 return;
1008 if( func->n_lines + 1 >= func->lines_alloc )
1010 func->lines_alloc += 64;
1011 func->linetab = DBG_realloc(func->linetab,
1012 func->lines_alloc * sizeof(WineLineNo));
1015 func->linetab[func->n_lines].line_number = line_num;
1016 func->linetab[func->n_lines].pc_offset.seg = func->addr.seg;
1017 func->linetab[func->n_lines].pc_offset.off = func->addr.off + offset;
1018 func->linetab[func->n_lines].pc_offset.type = NULL;
1019 func->n_lines++;
1023 struct wine_locals *
1024 DEBUG_AddLocal( struct name_hash * func, int regno,
1025 int offset,
1026 int pc_start,
1027 int pc_end,
1028 char * name)
1030 if( func == NULL )
1032 return NULL;
1035 if( func->n_locals + 1 >= func->locals_alloc )
1037 func->locals_alloc += 32;
1038 func->local_vars = DBG_realloc(func->local_vars,
1039 func->locals_alloc * sizeof(WineLocals));
1042 func->local_vars[func->n_locals].regno = regno;
1043 func->local_vars[func->n_locals].offset = offset;
1044 func->local_vars[func->n_locals].pc_start = pc_start;
1045 func->local_vars[func->n_locals].pc_end = pc_end;
1046 func->local_vars[func->n_locals].name = DBG_strdup(name);
1047 func->local_vars[func->n_locals].type = NULL;
1048 func->n_locals++;
1050 return &func->local_vars[func->n_locals - 1];
1053 void
1054 DEBUG_DumpHashInfo()
1056 int i;
1057 int depth;
1058 struct name_hash *nh;
1061 * Utility function to dump stats about the hash table.
1063 for(i=0; i<NR_NAME_HASH; i++)
1065 depth = 0;
1066 for (nh = name_hash_table[i]; nh; nh = nh->next)
1068 depth++;
1070 fprintf(stderr, "Bucket %d: %d\n", i, depth);
1074 /***********************************************************************
1075 * DEBUG_CheckLinenoStatus
1077 * Find the symbol nearest to a given address.
1078 * If ebp is specified as non-zero, it means we should dump the argument
1079 * list into the string we return as well.
1081 int DEBUG_CheckLinenoStatus( const DBG_ADDR *addr)
1083 struct name_hash * nearest = NULL;
1084 int mid, high, low;
1086 if( sortlist_valid == FALSE )
1088 DEBUG_ResortSymbols();
1092 * Binary search to find closest symbol.
1094 low = 0;
1095 high = sorttab_nsym;
1096 if( addr_sorttab[0]->addr.seg > addr->seg
1097 || ( addr_sorttab[0]->addr.seg == addr->seg
1098 && addr_sorttab[0]->addr.off > addr->off) )
1100 nearest = NULL;
1102 else if( addr_sorttab[high - 1]->addr.seg < addr->seg
1103 || ( addr_sorttab[high - 1]->addr.seg == addr->seg
1104 && addr_sorttab[high - 1]->addr.off < addr->off) )
1106 nearest = addr_sorttab[high - 1];
1108 else
1110 while(1==1)
1112 mid = (high + low)/2;
1113 if( mid == low )
1116 * See if there are any other entries that might also
1117 * have the same address, and would also have a line
1118 * number table.
1120 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
1122 if( (addr_sorttab[mid - 1]->addr.seg ==
1123 addr_sorttab[mid]->addr.seg)
1124 && (addr_sorttab[mid - 1]->addr.off ==
1125 addr_sorttab[mid]->addr.off)
1126 && (addr_sorttab[mid - 1]->linetab != NULL) )
1128 mid--;
1132 if( (mid < sorttab_nsym - 1)
1133 && (addr_sorttab[mid]->linetab == NULL) )
1135 if( (addr_sorttab[mid + 1]->addr.seg ==
1136 addr_sorttab[mid]->addr.seg)
1137 && (addr_sorttab[mid + 1]->addr.off ==
1138 addr_sorttab[mid]->addr.off)
1139 && (addr_sorttab[mid + 1]->linetab != NULL) )
1141 mid++;
1144 nearest = addr_sorttab[mid];
1145 #if 0
1146 fprintf(stderr, "Found %x:%x when looking for %x:%x %x %s\n",
1147 addr_sorttab[mid ]->addr.seg,
1148 addr_sorttab[mid ]->addr.off,
1149 addr->seg, addr->off,
1150 addr_sorttab[mid ]->linetab,
1151 addr_sorttab[mid ]->name);
1152 #endif
1153 break;
1155 if( (addr_sorttab[mid]->addr.seg < addr->seg)
1156 || ( addr_sorttab[mid]->addr.seg == addr->seg
1157 && addr_sorttab[mid]->addr.off <= addr->off) )
1159 low = mid;
1161 else
1163 high = mid;
1168 if (!nearest) return FUNC_HAS_NO_LINES;
1170 if( nearest->flags & SYM_STEP_THROUGH )
1173 * This will cause us to keep single stepping until
1174 * we get to the other side somewhere.
1176 return NOT_ON_LINENUMBER;
1179 if( (nearest->flags & SYM_TRAMPOLINE) )
1182 * This will cause us to keep single stepping until
1183 * we get to the other side somewhere.
1185 return FUNC_IS_TRAMPOLINE;
1188 if( nearest->linetab == NULL )
1190 return FUNC_HAS_NO_LINES;
1195 * We never want to stop on the first instruction of a function
1196 * even if it has it's own linenumber. Let the thing keep running
1197 * until it gets past the function prologue. We only do this if there
1198 * is more than one line number for the function, of course.
1200 if( nearest->addr.off == addr->off && nearest->n_lines > 1 )
1202 return NOT_ON_LINENUMBER;
1205 if( (nearest->sourcefile != NULL)
1206 && (addr->off - nearest->addr.off < 0x100000) )
1208 low = 0;
1209 high = nearest->n_lines;
1210 while ((high - low) > 1)
1212 mid = (high + low) / 2;
1213 if (addr->off < nearest->linetab[mid].pc_offset.off) high = mid;
1214 else low = mid;
1216 if (addr->off == nearest->linetab[low].pc_offset.off)
1217 return AT_LINENUMBER;
1218 else
1219 return NOT_ON_LINENUMBER;
1222 return FUNC_HAS_NO_LINES;
1225 /***********************************************************************
1226 * DEBUG_GetFuncInfo
1228 * Find the symbol nearest to a given address.
1229 * Returns sourcefile name and line number in a format that the listing
1230 * handler can deal with.
1232 void
1233 DEBUG_GetFuncInfo( struct list_id * ret, const char * filename,
1234 const char * name)
1236 char buffer[256];
1237 char * pnt;
1238 struct name_hash *nh;
1240 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
1242 if( filename != NULL )
1245 if( nh->sourcefile == NULL )
1247 continue;
1250 pnt = strrchr(nh->sourcefile, '/');
1251 if( strcmp(nh->sourcefile, filename) != 0
1252 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1254 continue;
1257 if (!strcmp(nh->name, name)) break;
1260 if (!nh && (name[0] != '_'))
1262 buffer[0] = '_';
1263 strcpy(buffer+1, name);
1264 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
1266 if( filename != NULL )
1268 if( nh->sourcefile == NULL )
1270 continue;
1273 pnt = strrchr(nh->sourcefile, '/');
1274 if( strcmp(nh->sourcefile, filename) != 0
1275 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1277 continue;
1280 if (!strcmp(nh->name, buffer)) break;
1284 if( !nh )
1286 if( filename != NULL )
1288 fprintf(stderr, "No such function %s in %s\n", name, filename);
1290 else
1292 fprintf(stderr, "No such function %s\n", name);
1294 ret->sourcefile = NULL;
1295 ret->line = -1;
1296 return;
1299 ret->sourcefile = nh->sourcefile;
1302 * Search for the specific line number. If we don't find it,
1303 * then return FALSE.
1305 if( nh->linetab == NULL )
1307 ret->line = -1;
1309 else
1311 ret->line = nh->linetab[0].line_number;
1315 /***********************************************************************
1316 * DEBUG_GetStackSymbolValue
1318 * Get the address of a named symbol from the current stack frame.
1320 static
1321 BOOL DEBUG_GetStackSymbolValue( const char * name, DBG_ADDR *addr )
1323 struct name_hash * curr_func;
1324 unsigned int ebp;
1325 unsigned int eip;
1326 int i;
1328 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1330 return FALSE;
1333 for(i=0; i < curr_func->n_locals; i++ )
1336 * Test the range of validity of the local variable. This
1337 * comes up with RBRAC/LBRAC stabs in particular.
1339 if( (curr_func->local_vars[i].pc_start != 0)
1340 && ((eip - curr_func->addr.off)
1341 < curr_func->local_vars[i].pc_start) )
1343 continue;
1346 if( (curr_func->local_vars[i].pc_end != 0)
1347 && ((eip - curr_func->addr.off)
1348 > curr_func->local_vars[i].pc_end) )
1350 continue;
1353 if( strcmp(name, curr_func->local_vars[i].name) == 0 )
1356 * OK, we found it. Now figure out what to do with this.
1358 /* FIXME: what if regno == 0 ($eax) */
1359 if( curr_func->local_vars[i].regno != 0 )
1362 * Register variable. Point to DEBUG_context field.
1364 addr->seg = 0;
1365 addr->off = ((DWORD)&DEBUG_context) + reg_ofs[curr_func->local_vars[i].regno];
1366 addr->type = curr_func->local_vars[i].type;
1368 return TRUE;
1371 addr->seg = 0;
1372 addr->off = ebp + curr_func->local_vars[i].offset;
1373 addr->type = curr_func->local_vars[i].type;
1375 return TRUE;
1378 return FALSE;
1382 DEBUG_InfoLocals()
1384 struct name_hash * curr_func;
1385 unsigned int ebp;
1386 unsigned int eip;
1387 int i;
1388 unsigned int * ptr;
1389 int rtn = FALSE;
1391 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1393 return FALSE;
1396 for(i=0; i < curr_func->n_locals; i++ )
1399 * Test the range of validity of the local variable. This
1400 * comes up with RBRAC/LBRAC stabs in particular.
1402 if( (curr_func->local_vars[i].pc_start != 0)
1403 && ((eip - curr_func->addr.off)
1404 < curr_func->local_vars[i].pc_start) )
1406 continue;
1409 if( (curr_func->local_vars[i].pc_end != 0)
1410 && ((eip - curr_func->addr.off)
1411 > curr_func->local_vars[i].pc_end) )
1413 continue;
1416 if( curr_func->local_vars[i].offset == 0 )
1418 ptr = (unsigned int *) (((DWORD)&DEBUG_context)
1419 + reg_ofs[curr_func->local_vars[i].regno]);
1420 fprintf(stderr, "%s:%s (optimized into register $%s) == 0x%8.8x\n",
1421 curr_func->name, curr_func->local_vars[i].name,
1422 reg_name[curr_func->local_vars[i].regno],
1423 *ptr);
1425 else
1427 ptr = (unsigned int *) (ebp + curr_func->local_vars[i].offset);
1428 fprintf(stderr, "%s:%s == 0x%8.8x\n",
1429 curr_func->name, curr_func->local_vars[i].name,
1430 *ptr);
1434 rtn = TRUE;
1436 return (rtn);
1440 DEBUG_SetSymbolSize(struct name_hash * sym, unsigned int len)
1442 sym->symbol_size = len;
1444 return TRUE;
1448 DEBUG_SetSymbolBPOff(struct name_hash * sym, unsigned int off)
1450 sym->breakpoint_offset = off;
1452 return TRUE;
1456 DEBUG_GetSymbolAddr(struct name_hash * sym, DBG_ADDR * addr)
1459 *addr = sym->addr;
1461 return TRUE;
1464 int DEBUG_SetLocalSymbolType(struct wine_locals * sym, struct datatype * type)
1466 sym->type = type;
1468 return TRUE;