Implementation of custom dialog messages and notifications.
[wine.git] / debugger / hash.c
blobab1632feb8011e571bb7971a8f11e65f4ce29169
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 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)
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_ADDR addr;
55 unsigned short flags;
56 unsigned short breakpoint_offset;
57 unsigned int symbol_size;
61 static BOOL DEBUG_GetStackSymbolValue( const char * name, DBG_ADDR *addr );
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)->addr.seg > (*name2)->addr.seg )
108 return 1;
111 if( (*name1)->addr.seg < (*name2)->addr.seg )
113 return -1;
116 if( (*name1)->addr.off > (*name2)->addr.off )
118 return 1;
121 if( (*name1)->addr.off < (*name2)->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()
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 fprintf( stderr, "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_ADDR *addr, 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 hash = name_hash(name);
195 for (nh = name_hash_table[hash]; nh; nh = nh->next)
197 if( ((nh->flags & SYM_INVALID) != 0) && strcmp(name, nh->name) == 0 )
199 nh->addr.off = addr->off;
200 nh->addr.seg = addr->seg;
201 if( nh->addr.type == NULL && addr->type != NULL )
203 nh->addr.type = addr->type;
205 /* it may happen that the same symbol is defined in several compilation
206 * units, but the linker decides to merge it into a single instance.
207 * in that case, we don't clear the invalid flag for all the compilation
208 * units (N_GSYM), and wait to get the symbol from the symtab
210 if ((flags & SYM_INVALID) == 0)
211 nh->flags &= ~SYM_INVALID;
212 return nh;
214 if (nh->addr.seg == addr->seg &&
215 nh->addr.off == addr->off &&
216 strcmp(name, nh->name) == 0 )
218 return nh;
223 * First see if we already have an entry for this symbol. If so
224 * return it, so we don't end up with duplicates.
227 new = (struct name_hash *) DBG_alloc(sizeof(struct name_hash));
228 new->addr = *addr;
229 new->name = DBG_strdup(name);
231 if( source != NULL )
234 * This is an enhancement to reduce memory consumption. The idea
235 * is that we duplicate a given string only once. This is a big
236 * win if there are lots of symbols defined in a given source file.
238 if( strcmp(source, prev_source) == 0 )
240 new->sourcefile = prev_duped_source;
242 else
244 strcpy(prev_source, source);
245 prev_duped_source = new->sourcefile = DBG_strdup(source);
248 else
250 new->sourcefile = NULL;
253 new->n_lines = 0;
254 new->lines_alloc = 0;
255 new->linetab = NULL;
257 new->n_locals = 0;
258 new->locals_alloc = 0;
259 new->local_vars = NULL;
261 new->flags = flags;
262 new->next = NULL;
264 /* Now insert into the hash table */
265 new->next = name_hash_table[hash];
266 name_hash_table[hash] = new;
269 * Check some heuristics based upon the file name to see whether
270 * we want to step through this guy or not. These are machine generated
271 * assembly files that are used to translate between the MS way of
272 * calling things and the GCC way of calling things. In general we
273 * always want to step through.
275 if( source != NULL )
277 c = strrchr(source, '.');
278 if( c != NULL && strcmp(c, ".s") == 0 )
280 c = strrchr(source, '/');
281 if( c != NULL )
283 c++;
284 if( (strcmp(c, "callfrom16.s") == 0)
285 || (strcmp(c, "callto16.s") == 0)
286 || (strcmp(c, "call32.s") == 0) )
288 new->flags |= SYM_TRAMPOLINE;
294 sortlist_valid = FALSE;
295 return new;
298 BOOL DEBUG_Normalize(struct name_hash * nh )
302 * We aren't adding any more locals or linenumbers to this function.
303 * Free any spare memory that we might have allocated.
305 if( nh == NULL )
307 return TRUE;
310 if( nh->n_locals != nh->locals_alloc )
312 nh->locals_alloc = nh->n_locals;
313 nh->local_vars = DBG_realloc(nh->local_vars,
314 nh->locals_alloc * sizeof(WineLocals));
317 if( nh->n_lines != nh->lines_alloc )
319 nh->lines_alloc = nh->n_lines;
320 nh->linetab = DBG_realloc(nh->linetab,
321 nh->lines_alloc * sizeof(WineLineNo));
324 return TRUE;
327 /***********************************************************************
328 * DEBUG_GetSymbolValue
330 * Get the address of a named symbol.
332 BOOL DEBUG_GetSymbolValue( const char * name, const int lineno,
333 DBG_ADDR *addr, int bp_flag )
335 char buffer[256];
336 struct name_hash *nh;
338 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
340 if( (nh->flags & SYM_INVALID) != 0 )
342 continue;
345 if (!strcmp(nh->name, name)) break;
348 if (!nh && (name[0] != '_'))
350 buffer[0] = '_';
351 strcpy(buffer+1, name);
352 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
354 if( (nh->flags & SYM_INVALID) != 0 )
356 continue;
358 if (!strcmp(nh->name, buffer)) break;
363 * If we don't have anything here, then try and see if this
364 * is a local symbol to the current stack frame. No matter
365 * what, we have nothing more to do, so we let that function
366 * decide what we ultimately return.
368 if (!nh)
370 return DEBUG_GetStackSymbolValue(name, addr);
373 return DEBUG_GetLineNumberAddr( nh, lineno, addr, bp_flag );
376 /***********************************************************************
377 * DEBUG_GetLineNumberAddr
379 * Get the address of a named symbol.
381 BOOL DEBUG_GetLineNumberAddr( struct name_hash * nh, const int lineno,
382 DBG_ADDR *addr, int bp_flag )
384 int i;
386 if( lineno == -1 )
388 *addr = nh->addr;
389 if( bp_flag )
391 addr->off += nh->breakpoint_offset;
394 else
397 * Search for the specific line number. If we don't find it,
398 * then return FALSE.
400 if( nh->linetab == NULL )
402 return FALSE;
405 for(i=0; i < nh->n_lines; i++ )
407 if( nh->linetab[i].line_number == lineno )
409 *addr = nh->linetab[i].pc_offset;
410 return TRUE;
415 * This specific line number not found.
417 return FALSE;
420 return TRUE;
424 /***********************************************************************
425 * DEBUG_SetSymbolValue
427 * Set the address of a named symbol.
429 BOOL DEBUG_SetSymbolValue( const char * name, const DBG_ADDR *addr )
431 char buffer[256];
432 struct name_hash *nh;
434 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
435 if (!strcmp(nh->name, name)) break;
437 if (!nh && (name[0] != '_'))
439 buffer[0] = '_';
440 strcpy(buffer+1, name);
441 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
442 if (!strcmp(nh->name, buffer)) break;
445 if (!nh) return FALSE;
446 nh->addr = *addr;
447 nh->flags &= SYM_INVALID;
448 DBG_FIX_ADDR_SEG( &nh->addr, DS_reg(&DEBUG_context) );
449 return TRUE;
453 /***********************************************************************
454 * DEBUG_FindNearestSymbol
456 * Find the symbol nearest to a given address.
457 * If ebp is specified as non-zero, it means we should dump the argument
458 * list into the string we return as well.
460 const char * DEBUG_FindNearestSymbol( const DBG_ADDR *addr, int flag,
461 struct name_hash ** rtn,
462 unsigned int ebp,
463 struct list_id * source)
465 static char name_buffer[MAX_PATH + 256];
466 static char arglist[1024];
467 static char argtmp[256];
468 struct name_hash * nearest = NULL;
469 int mid, high, low;
470 unsigned int * ptr;
471 int lineno;
472 char * lineinfo, *sourcefile;
473 int i;
474 char linebuff[16];
476 if( rtn != NULL )
478 *rtn = NULL;
481 if( source != NULL )
483 source->sourcefile = NULL;
484 source->line = -1;
487 if( sortlist_valid == FALSE )
489 DEBUG_ResortSymbols();
492 if( sortlist_valid == FALSE )
494 return NULL;
498 * FIXME - use the binary search that we added to
499 * the function DEBUG_CheckLinenoStatus. Better yet, we should
500 * probably keep some notion of the current function so we don't
501 * have to search every time.
504 * Binary search to find closest symbol.
506 low = 0;
507 high = sorttab_nsym;
508 if( addr_sorttab[0]->addr.seg > addr->seg
509 || ( addr_sorttab[0]->addr.seg == addr->seg
510 && addr_sorttab[0]->addr.off > addr->off) )
512 nearest = NULL;
514 else if( addr_sorttab[high - 1]->addr.seg < addr->seg
515 || ( addr_sorttab[high - 1]->addr.seg == addr->seg
516 && addr_sorttab[high - 1]->addr.off < addr->off) )
518 nearest = addr_sorttab[high - 1];
520 else
522 while(1==1)
524 mid = (high + low)/2;
525 if( mid == low )
528 * See if there are any other entries that might also
529 * have the same address, and would also have a line
530 * number table.
532 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
534 if( (addr_sorttab[mid - 1]->addr.seg ==
535 addr_sorttab[mid]->addr.seg)
536 && (addr_sorttab[mid - 1]->addr.off ==
537 addr_sorttab[mid]->addr.off)
538 && (addr_sorttab[mid - 1]->linetab != NULL) )
540 mid--;
544 if( (mid < sorttab_nsym - 1)
545 && (addr_sorttab[mid]->linetab == NULL) )
547 if( (addr_sorttab[mid + 1]->addr.seg ==
548 addr_sorttab[mid]->addr.seg)
549 && (addr_sorttab[mid + 1]->addr.off ==
550 addr_sorttab[mid]->addr.off)
551 && (addr_sorttab[mid + 1]->linetab != NULL) )
553 mid++;
556 nearest = addr_sorttab[mid];
557 #if 0
558 fprintf(stderr, "Found %x:%x when looking for %x:%x %x %s\n",
559 addr_sorttab[mid ]->addr.seg,
560 addr_sorttab[mid ]->addr.off,
561 addr->seg, addr->off,
562 addr_sorttab[mid ]->linetab,
563 addr_sorttab[mid ]->name);
564 #endif
565 break;
567 if( (addr_sorttab[mid]->addr.seg < addr->seg)
568 || ( addr_sorttab[mid]->addr.seg == addr->seg
569 && addr_sorttab[mid]->addr.off <= addr->off) )
571 low = mid;
573 else
575 high = mid;
580 if (!nearest) return NULL;
582 if( rtn != NULL )
584 *rtn = nearest;
588 * Fill in the relevant bits to the structure so that we can
589 * locate the source and line for this bit of code.
591 if( source != NULL )
593 source->sourcefile = nearest->sourcefile;
594 if( nearest->linetab == NULL )
596 source->line = -1;
598 else
600 source->line = nearest->linetab[0].line_number;
604 lineinfo = "";
605 lineno = -1;
608 * Prepare to display the argument list. If ebp is specified, it is
609 * the framepointer for the function in question. If not specified,
610 * we don't want the arglist.
612 memset(arglist, '\0', sizeof(arglist));
613 if( ebp != 0 )
615 for(i=0; i < nearest->n_locals; i++ )
618 * If this is a register (offset == 0) or a local
619 * variable, we don't want to know about it.
621 if( nearest->local_vars[i].offset <= 0 )
623 continue;
626 ptr = (unsigned int *) (ebp + nearest->local_vars[i].offset);
627 if( arglist[0] == '\0' )
629 arglist[0] = '(';
631 else
633 strcat(arglist, ", ");
636 sprintf(argtmp, "%s=0x%x", nearest->local_vars[i].name,
637 *ptr);
638 strcat(arglist, argtmp);
640 if( arglist[0] == '(' )
642 strcat(arglist, ")");
646 if( (nearest->sourcefile != NULL) && (flag == TRUE)
647 && (addr->off - nearest->addr.off < 0x100000) )
651 * Try and find the nearest line number to the current offset.
653 if( nearest->linetab != NULL )
655 low = 0;
656 high = nearest->n_lines;
657 while ((high - low) > 1)
659 mid = (high + low) / 2;
660 if (addr->off < nearest->linetab[mid].pc_offset.off)
661 high = mid;
662 else
663 low = mid;
665 lineno = nearest->linetab[low].line_number;
668 if( lineno != -1 )
670 sprintf(linebuff, ":%d", lineno);
671 lineinfo = linebuff;
672 if( source != NULL )
674 source->line = lineno;
678 /* Remove the path from the file name */
679 sourcefile = strrchr( nearest->sourcefile, '/' );
680 if (!sourcefile) sourcefile = nearest->sourcefile;
681 else sourcefile++;
683 if (addr->off == nearest->addr.off)
684 sprintf( name_buffer, "%s%s [%s%s]", nearest->name,
685 arglist, sourcefile, lineinfo);
686 else
687 sprintf( name_buffer, "%s+0x%lx%s [%s%s]", nearest->name,
688 addr->off - nearest->addr.off,
689 arglist, sourcefile, lineinfo );
691 else
693 if (addr->off == nearest->addr.off)
694 sprintf( name_buffer, "%s%s", nearest->name, arglist);
695 else {
696 if (addr->seg && (nearest->addr.seg!=addr->seg))
697 return NULL;
698 else
699 sprintf( name_buffer, "%s+0x%lx%s", nearest->name,
700 addr->off - nearest->addr.off, arglist);
703 return name_buffer;
707 /***********************************************************************
708 * DEBUG_ReadSymbolTable
710 * Read a symbol file into the hash table.
712 void DEBUG_ReadSymbolTable( const char * filename )
714 FILE * symbolfile;
715 DBG_ADDR addr = { 0, 0 };
716 int nargs;
717 char type;
718 char * cpnt;
719 char buffer[256];
720 char name[256];
722 if (!(symbolfile = fopen(filename, "r")))
724 fprintf( stderr, "Unable to open symbol table %s\n", filename );
725 return;
728 fprintf( stderr, "Reading symbols from file %s\n", filename );
730 while (1)
732 fgets( buffer, sizeof(buffer), symbolfile );
733 if (feof(symbolfile)) break;
735 /* Strip any text after a # sign (i.e. comments) */
736 cpnt = buffer;
737 while (*cpnt)
738 if(*cpnt++ == '#') { *cpnt = 0; break; }
740 /* Quietly ignore any lines that have just whitespace */
741 cpnt = buffer;
742 while(*cpnt)
744 if(*cpnt != ' ' && *cpnt != '\t') break;
745 cpnt++;
747 if (!(*cpnt) || *cpnt == '\n') continue;
749 nargs = sscanf(buffer, "%lx %c %s", &addr.off, &type, name);
750 DEBUG_AddSymbol( name, &addr, NULL, SYM_WINE );
752 fclose(symbolfile);
756 /***********************************************************************
757 * DEBUG_LoadEntryPoints16
759 * Load the entry points of a Win16 module into the hash table.
761 static void DEBUG_LoadEntryPoints16( HMODULE16 hModule, NE_MODULE *pModule,
762 const char *name )
764 DBG_ADDR addr;
765 char buffer[256];
766 FARPROC16 address;
768 /* First search the resident names */
770 unsigned char *cpnt = (unsigned char *)pModule + pModule->name_table;
771 while (*cpnt)
773 cpnt += *cpnt + 1 + sizeof(WORD);
774 sprintf( buffer, "%s.%.*s", name, *cpnt, cpnt + 1 );
775 if ((address = NE_GetEntryPoint(hModule, *(WORD *)(cpnt + *cpnt + 1))))
777 addr.seg = HIWORD(address);
778 addr.off = LOWORD(address);
779 addr.type = NULL;
780 DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
784 /* Now search the non-resident names table */
786 if (!pModule->nrname_handle) return; /* No non-resident table */
787 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
788 while (*cpnt)
790 cpnt += *cpnt + 1 + sizeof(WORD);
791 sprintf( buffer, "%s.%.*s", name, *cpnt, cpnt + 1 );
792 if ((address = NE_GetEntryPoint(hModule, *(WORD *)(cpnt + *cpnt + 1))))
794 addr.seg = HIWORD(address);
795 addr.off = LOWORD(address);
796 addr.type = NULL;
797 DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
803 /***********************************************************************
804 * DEBUG_LoadEntryPoints32
806 * Load the entry points of a Win32 module into the hash table.
808 static void DEBUG_LoadEntryPoints32( HMODULE hModule, const char *name )
810 #define RVA(x) (hModule+(DWORD)(x))
812 DBG_ADDR addr;
813 char buffer[256];
814 int i, j;
815 IMAGE_SECTION_HEADER *pe_seg;
816 IMAGE_EXPORT_DIRECTORY *exports;
817 IMAGE_DATA_DIRECTORY *dir;
818 WORD *ordinals;
819 void **functions;
820 const char **names;
822 addr.seg = 0;
823 addr.type = NULL;
825 /* Add start of DLL */
827 addr.off = hModule;
828 DEBUG_AddSymbol( name, &addr, NULL, SYM_WIN32 | SYM_FUNC );
830 /* Add entry point */
832 sprintf( buffer, "%s.EntryPoint", name );
833 addr.off = (DWORD)RVA_PTR( hModule, OptionalHeader.AddressOfEntryPoint );
834 DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
836 /* Add start of sections */
838 pe_seg = PE_SECTIONS(hModule);
839 for (i = 0; i < PE_HEADER(hModule)->FileHeader.NumberOfSections; i++)
841 sprintf( buffer, "%s.%s", name, pe_seg->Name );
842 addr.off = RVA(pe_seg->VirtualAddress );
843 DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
844 pe_seg++;
847 /* Add exported functions */
849 dir = &PE_HEADER(hModule)->OptionalHeader.
850 DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
851 if (dir->Size)
853 exports = (IMAGE_EXPORT_DIRECTORY *)RVA( dir->VirtualAddress );
854 ordinals = (WORD *)RVA( exports->AddressOfNameOrdinals );
855 names = (const char **)RVA( exports->AddressOfNames );
856 functions = (void **)RVA( exports->AddressOfFunctions );
858 for (i = 0; i < exports->NumberOfNames; i++)
860 if (!names[i]) continue;
861 sprintf( buffer, "%s.%s", name, (char *)RVA(names[i]) );
862 addr.off = RVA( functions[ordinals[i]] );
863 DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
866 for (i = 0; i < exports->NumberOfFunctions; i++)
868 if (!functions[i]) continue;
869 /* Check if we already added it with a name */
870 for (j = 0; j < exports->NumberOfNames; j++)
871 if ((ordinals[j] == i) && names[j]) break;
872 if (j < exports->NumberOfNames) continue;
873 sprintf( buffer, "%s.%ld", name, i + exports->Base );
874 addr.off = (DWORD)RVA( functions[i] );
875 DEBUG_AddSymbol( buffer, &addr, NULL, SYM_WIN32 | SYM_FUNC );
878 DEBUG_RegisterDebugInfo(hModule, name);
879 #undef RVA
882 typedef struct tag_lmr{
883 char* module_name;
884 BOOL is16;
885 struct tag_lmr* next;
886 } DBG_LoadedModuleRef;
888 typedef struct {
889 int rowcount;
890 int first;
891 const char* pfx;
892 } DBG_LEPData;
894 static BOOL DEBUG_LEPHelper(const char* mod_name, BOOL is16, DBG_LEPData* lep)
896 static DBG_LoadedModuleRef* lmr = NULL;
897 DBG_LoadedModuleRef* p;
898 DBG_LoadedModuleRef** pp1;
899 DBG_LoadedModuleRef* p2;
900 int len = strlen(mod_name);
901 int cmp;
903 for (p = lmr; p; p = p->next) {
904 cmp = strcmp(p->module_name, mod_name);
905 if (cmp < 0)
906 continue;
907 if (cmp == 0) {
908 if (p->is16 == is16)
909 return FALSE;
910 continue;
912 break;
915 if (!lep->first) {
916 if (lep->pfx) fprintf( stderr, lep->pfx );
917 fprintf( stderr, " " );
918 lep->first++;
919 lep->rowcount = 3;
922 if ((lep->rowcount + len) > 76)
924 fprintf( stderr, "\n ");
925 lep->rowcount = 3;
927 fprintf( stderr, " %s", mod_name );
928 lep->rowcount += len + 1;
930 p = DBG_alloc(sizeof(*lmr));
931 p->module_name = DBG_strdup(mod_name);
932 p->is16 = is16;
934 p2 = NULL;
935 for (pp1 = &lmr; *pp1; pp1 = &(*pp1)->next) {
936 if (strcmp((*pp1)->module_name, mod_name) > 0)
937 break;
938 p2 = *pp1;
940 if (p2 == NULL)
942 p->next = lmr;
943 lmr = p;
945 else if (*pp1 == NULL)
947 p->next = NULL;
948 *pp1 = p;
950 else
952 p->next = *pp1;
953 p2->next = p;
956 return TRUE;
959 /***********************************************************************
960 * DEBUG_LoadEntryPoints
962 * Load the entry points of all the modules into the hash table.
964 int DEBUG_LoadEntryPoints(const char* pfx)
966 MODULEENTRY entry;
967 NE_MODULE* pModule;
968 BOOL ok;
969 WINE_MODREF*wm;
970 DBG_LEPData lep;
972 lep.first = 0;
973 lep.pfx = pfx;
975 /* FIXME: we assume that a module is never removed from memory */
977 for (ok = ModuleFirst16(&entry); ok; ok = ModuleNext16(&entry))
979 if (!(pModule = NE_GetPtr( entry.hModule ))) continue;
980 if (!(pModule->flags & NE_FFLAGS_WIN32) && /* NE module */
981 DEBUG_LEPHelper( entry.szModule, TRUE, &lep ))
982 DEBUG_LoadEntryPoints16( entry.hModule, pModule, entry.szModule );
984 for (wm=PROCESS_Current()->modref_list;wm;wm=wm->next)
986 if (DEBUG_LEPHelper( wm->modname, FALSE, &lep ))
987 DEBUG_LoadEntryPoints32( wm->module, wm->modname );
989 if (lep.first) fprintf( stderr, "\n" );
990 return lep.first;
994 void
995 DEBUG_AddLineNumber( struct name_hash * func, int line_num,
996 unsigned long offset )
998 if( func == NULL )
1000 return;
1003 if( func->n_lines + 1 >= func->lines_alloc )
1005 func->lines_alloc += 64;
1006 func->linetab = DBG_realloc(func->linetab,
1007 func->lines_alloc * sizeof(WineLineNo));
1010 func->linetab[func->n_lines].line_number = line_num;
1011 func->linetab[func->n_lines].pc_offset.seg = func->addr.seg;
1012 func->linetab[func->n_lines].pc_offset.off = func->addr.off + offset;
1013 func->linetab[func->n_lines].pc_offset.type = NULL;
1014 func->n_lines++;
1018 struct wine_locals *
1019 DEBUG_AddLocal( struct name_hash * func, int regno,
1020 int offset,
1021 int pc_start,
1022 int pc_end,
1023 char * name)
1025 if( func == NULL )
1027 return NULL;
1030 if( func->n_locals + 1 >= func->locals_alloc )
1032 func->locals_alloc += 32;
1033 func->local_vars = DBG_realloc(func->local_vars,
1034 func->locals_alloc * sizeof(WineLocals));
1037 func->local_vars[func->n_locals].regno = regno;
1038 func->local_vars[func->n_locals].offset = offset;
1039 func->local_vars[func->n_locals].pc_start = pc_start;
1040 func->local_vars[func->n_locals].pc_end = pc_end;
1041 func->local_vars[func->n_locals].name = DBG_strdup(name);
1042 func->local_vars[func->n_locals].type = NULL;
1043 func->n_locals++;
1045 return &func->local_vars[func->n_locals - 1];
1048 void
1049 DEBUG_DumpHashInfo()
1051 int i;
1052 int depth;
1053 struct name_hash *nh;
1056 * Utility function to dump stats about the hash table.
1058 for(i=0; i<NR_NAME_HASH; i++)
1060 depth = 0;
1061 for (nh = name_hash_table[i]; nh; nh = nh->next)
1063 depth++;
1065 fprintf(stderr, "Bucket %d: %d\n", i, depth);
1069 /***********************************************************************
1070 * DEBUG_CheckLinenoStatus
1072 * Find the symbol nearest to a given address.
1073 * If ebp is specified as non-zero, it means we should dump the argument
1074 * list into the string we return as well.
1076 int DEBUG_CheckLinenoStatus( const DBG_ADDR *addr)
1078 struct name_hash * nearest = NULL;
1079 int mid, high, low;
1081 if( sortlist_valid == FALSE )
1083 DEBUG_ResortSymbols();
1087 * Binary search to find closest symbol.
1089 low = 0;
1090 high = sorttab_nsym;
1091 if( addr_sorttab[0]->addr.seg > addr->seg
1092 || ( addr_sorttab[0]->addr.seg == addr->seg
1093 && addr_sorttab[0]->addr.off > addr->off) )
1095 nearest = NULL;
1097 else if( addr_sorttab[high - 1]->addr.seg < addr->seg
1098 || ( addr_sorttab[high - 1]->addr.seg == addr->seg
1099 && addr_sorttab[high - 1]->addr.off < addr->off) )
1101 nearest = addr_sorttab[high - 1];
1103 else
1105 while(1==1)
1107 mid = (high + low)/2;
1108 if( mid == low )
1111 * See if there are any other entries that might also
1112 * have the same address, and would also have a line
1113 * number table.
1115 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
1117 if( (addr_sorttab[mid - 1]->addr.seg ==
1118 addr_sorttab[mid]->addr.seg)
1119 && (addr_sorttab[mid - 1]->addr.off ==
1120 addr_sorttab[mid]->addr.off)
1121 && (addr_sorttab[mid - 1]->linetab != NULL) )
1123 mid--;
1127 if( (mid < sorttab_nsym - 1)
1128 && (addr_sorttab[mid]->linetab == NULL) )
1130 if( (addr_sorttab[mid + 1]->addr.seg ==
1131 addr_sorttab[mid]->addr.seg)
1132 && (addr_sorttab[mid + 1]->addr.off ==
1133 addr_sorttab[mid]->addr.off)
1134 && (addr_sorttab[mid + 1]->linetab != NULL) )
1136 mid++;
1139 nearest = addr_sorttab[mid];
1140 #if 0
1141 fprintf(stderr, "Found %x:%x when looking for %x:%x %x %s\n",
1142 addr_sorttab[mid ]->addr.seg,
1143 addr_sorttab[mid ]->addr.off,
1144 addr->seg, addr->off,
1145 addr_sorttab[mid ]->linetab,
1146 addr_sorttab[mid ]->name);
1147 #endif
1148 break;
1150 if( (addr_sorttab[mid]->addr.seg < addr->seg)
1151 || ( addr_sorttab[mid]->addr.seg == addr->seg
1152 && addr_sorttab[mid]->addr.off <= addr->off) )
1154 low = mid;
1156 else
1158 high = mid;
1163 if (!nearest) return FUNC_HAS_NO_LINES;
1165 if( nearest->flags & SYM_STEP_THROUGH )
1168 * This will cause us to keep single stepping until
1169 * we get to the other side somewhere.
1171 return NOT_ON_LINENUMBER;
1174 if( (nearest->flags & SYM_TRAMPOLINE) )
1177 * This will cause us to keep single stepping until
1178 * we get to the other side somewhere.
1180 return FUNC_IS_TRAMPOLINE;
1183 if( nearest->linetab == NULL )
1185 return FUNC_HAS_NO_LINES;
1190 * We never want to stop on the first instruction of a function
1191 * even if it has it's own linenumber. Let the thing keep running
1192 * until it gets past the function prologue. We only do this if there
1193 * is more than one line number for the function, of course.
1195 if( nearest->addr.off == addr->off && nearest->n_lines > 1 )
1197 return NOT_ON_LINENUMBER;
1200 if( (nearest->sourcefile != NULL)
1201 && (addr->off - nearest->addr.off < 0x100000) )
1203 low = 0;
1204 high = nearest->n_lines;
1205 while ((high - low) > 1)
1207 mid = (high + low) / 2;
1208 if (addr->off < nearest->linetab[mid].pc_offset.off) high = mid;
1209 else low = mid;
1211 if (addr->off == nearest->linetab[low].pc_offset.off)
1212 return AT_LINENUMBER;
1213 else
1214 return NOT_ON_LINENUMBER;
1217 return FUNC_HAS_NO_LINES;
1220 /***********************************************************************
1221 * DEBUG_GetFuncInfo
1223 * Find the symbol nearest to a given address.
1224 * Returns sourcefile name and line number in a format that the listing
1225 * handler can deal with.
1227 void
1228 DEBUG_GetFuncInfo( struct list_id * ret, const char * filename,
1229 const char * name)
1231 char buffer[256];
1232 char * pnt;
1233 struct name_hash *nh;
1235 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
1237 if( filename != NULL )
1240 if( nh->sourcefile == NULL )
1242 continue;
1245 pnt = strrchr(nh->sourcefile, '/');
1246 if( strcmp(nh->sourcefile, filename) != 0
1247 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1249 continue;
1252 if (!strcmp(nh->name, name)) break;
1255 if (!nh && (name[0] != '_'))
1257 buffer[0] = '_';
1258 strcpy(buffer+1, name);
1259 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
1261 if( filename != NULL )
1263 if( nh->sourcefile == NULL )
1265 continue;
1268 pnt = strrchr(nh->sourcefile, '/');
1269 if( strcmp(nh->sourcefile, filename) != 0
1270 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1272 continue;
1275 if (!strcmp(nh->name, buffer)) break;
1279 if( !nh )
1281 if( filename != NULL )
1283 fprintf(stderr, "No such function %s in %s\n", name, filename);
1285 else
1287 fprintf(stderr, "No such function %s\n", name);
1289 ret->sourcefile = NULL;
1290 ret->line = -1;
1291 return;
1294 ret->sourcefile = nh->sourcefile;
1297 * Search for the specific line number. If we don't find it,
1298 * then return FALSE.
1300 if( nh->linetab == NULL )
1302 ret->line = -1;
1304 else
1306 ret->line = nh->linetab[0].line_number;
1310 /***********************************************************************
1311 * DEBUG_GetStackSymbolValue
1313 * Get the address of a named symbol from the current stack frame.
1315 static
1316 BOOL DEBUG_GetStackSymbolValue( const char * name, DBG_ADDR *addr )
1318 struct name_hash * curr_func;
1319 unsigned int ebp;
1320 unsigned int eip;
1321 int i;
1323 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1325 return FALSE;
1328 for(i=0; i < curr_func->n_locals; i++ )
1331 * Test the range of validity of the local variable. This
1332 * comes up with RBRAC/LBRAC stabs in particular.
1334 if( (curr_func->local_vars[i].pc_start != 0)
1335 && ((eip - curr_func->addr.off)
1336 < curr_func->local_vars[i].pc_start) )
1338 continue;
1341 if( (curr_func->local_vars[i].pc_end != 0)
1342 && ((eip - curr_func->addr.off)
1343 > curr_func->local_vars[i].pc_end) )
1345 continue;
1348 if( strcmp(name, curr_func->local_vars[i].name) == 0 )
1351 * OK, we found it. Now figure out what to do with this.
1353 /* FIXME: what if regno == 0 ($eax) */
1354 if( curr_func->local_vars[i].regno != 0 )
1357 * Register variable. Point to DEBUG_context field.
1359 addr->seg = 0;
1360 addr->off = ((DWORD)&DEBUG_context) + reg_ofs[curr_func->local_vars[i].regno];
1361 addr->type = curr_func->local_vars[i].type;
1363 return TRUE;
1366 addr->seg = 0;
1367 addr->off = ebp + curr_func->local_vars[i].offset;
1368 addr->type = curr_func->local_vars[i].type;
1370 return TRUE;
1373 return FALSE;
1377 DEBUG_InfoLocals()
1379 struct name_hash * curr_func;
1380 unsigned int ebp;
1381 unsigned int eip;
1382 int i;
1383 unsigned int * ptr;
1384 int rtn = FALSE;
1386 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1388 return FALSE;
1391 for(i=0; i < curr_func->n_locals; i++ )
1394 * Test the range of validity of the local variable. This
1395 * comes up with RBRAC/LBRAC stabs in particular.
1397 if( (curr_func->local_vars[i].pc_start != 0)
1398 && ((eip - curr_func->addr.off)
1399 < curr_func->local_vars[i].pc_start) )
1401 continue;
1404 if( (curr_func->local_vars[i].pc_end != 0)
1405 && ((eip - curr_func->addr.off)
1406 > curr_func->local_vars[i].pc_end) )
1408 continue;
1411 if( curr_func->local_vars[i].offset == 0 )
1413 ptr = (unsigned int *) (((DWORD)&DEBUG_context)
1414 + reg_ofs[curr_func->local_vars[i].regno]);
1415 fprintf(stderr, "%s:%s (optimized into register $%s) == 0x%8.8x\n",
1416 curr_func->name, curr_func->local_vars[i].name,
1417 reg_name[curr_func->local_vars[i].regno],
1418 *ptr);
1420 else
1422 ptr = (unsigned int *) (ebp + curr_func->local_vars[i].offset);
1423 fprintf(stderr, "%s:%s == 0x%8.8x\n",
1424 curr_func->name, curr_func->local_vars[i].name,
1425 *ptr);
1429 rtn = TRUE;
1431 return (rtn);
1435 DEBUG_SetSymbolSize(struct name_hash * sym, unsigned int len)
1437 sym->symbol_size = len;
1439 return TRUE;
1443 DEBUG_SetSymbolBPOff(struct name_hash * sym, unsigned int off)
1445 sym->breakpoint_offset = off;
1447 return TRUE;
1451 DEBUG_GetSymbolAddr(struct name_hash * sym, DBG_ADDR * addr)
1454 *addr = sym->addr;
1456 return TRUE;
1459 int DEBUG_SetLocalSymbolType(struct wine_locals * sym, struct datatype * type)
1461 sym->type = type;
1463 return TRUE;