Fix off by one error in WINSPOOL_GetOpenedPrinterA.
[wine/multimedia.git] / debugger / hash.c
blobb0ad4070ca71e9a4a73ba4960693a6b62cbddf4a
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
883 /***********************************************************************
884 * DEBUG_LoadEntryPoints
886 * Load the entry points of all the modules into the hash table.
888 void DEBUG_LoadEntryPoints(void)
890 MODULEENTRY entry;
891 NE_MODULE *pModule;
892 BOOL ok;
893 WINE_MODREF *wm;
894 int rowcount = 3;
896 fprintf( stderr, " " );
897 for (ok = ModuleFirst16(&entry); ok; ok = ModuleNext16(&entry))
899 if (!(pModule = NE_GetPtr( entry.hModule ))) continue;
900 if (!(pModule->flags & NE_FFLAGS_WIN32)) /* NE module */
902 if ((rowcount + strlen(entry.szModule)) > 76)
904 fprintf( stderr,"\n ");
905 rowcount = 3;
907 fprintf( stderr, " %s", entry.szModule );
908 rowcount += strlen(entry.szModule) + 1;
910 DEBUG_LoadEntryPoints16( entry.hModule, pModule, entry.szModule );
913 for (wm=PROCESS_Current()->modref_list;wm;wm=wm->next)
915 if ((rowcount + strlen(wm->modname)) > 76)
917 fprintf( stderr,"\n ");
918 rowcount = 3;
920 fprintf( stderr, " %s", wm->modname );
921 rowcount += strlen(wm->modname) + 1;
922 DEBUG_LoadEntryPoints32( wm->module, wm->modname );
924 fprintf( stderr, "\n" );
928 void
929 DEBUG_AddLineNumber( struct name_hash * func, int line_num,
930 unsigned long offset )
932 if( func == NULL )
934 return;
937 if( func->n_lines + 1 >= func->lines_alloc )
939 func->lines_alloc += 64;
940 func->linetab = DBG_realloc(func->linetab,
941 func->lines_alloc * sizeof(WineLineNo));
944 func->linetab[func->n_lines].line_number = line_num;
945 func->linetab[func->n_lines].pc_offset.seg = func->addr.seg;
946 func->linetab[func->n_lines].pc_offset.off = func->addr.off + offset;
947 func->linetab[func->n_lines].pc_offset.type = NULL;
948 func->n_lines++;
952 struct wine_locals *
953 DEBUG_AddLocal( struct name_hash * func, int regno,
954 int offset,
955 int pc_start,
956 int pc_end,
957 char * name)
959 if( func == NULL )
961 return NULL;
964 if( func->n_locals + 1 >= func->locals_alloc )
966 func->locals_alloc += 32;
967 func->local_vars = DBG_realloc(func->local_vars,
968 func->locals_alloc * sizeof(WineLocals));
971 func->local_vars[func->n_locals].regno = regno;
972 func->local_vars[func->n_locals].offset = offset;
973 func->local_vars[func->n_locals].pc_start = pc_start;
974 func->local_vars[func->n_locals].pc_end = pc_end;
975 func->local_vars[func->n_locals].name = DBG_strdup(name);
976 func->local_vars[func->n_locals].type = NULL;
977 func->n_locals++;
979 return &func->local_vars[func->n_locals - 1];
982 void
983 DEBUG_DumpHashInfo()
985 int i;
986 int depth;
987 struct name_hash *nh;
990 * Utility function to dump stats about the hash table.
992 for(i=0; i<NR_NAME_HASH; i++)
994 depth = 0;
995 for (nh = name_hash_table[i]; nh; nh = nh->next)
997 depth++;
999 fprintf(stderr, "Bucket %d: %d\n", i, depth);
1003 /***********************************************************************
1004 * DEBUG_CheckLinenoStatus
1006 * Find the symbol nearest to a given address.
1007 * If ebp is specified as non-zero, it means we should dump the argument
1008 * list into the string we return as well.
1010 int DEBUG_CheckLinenoStatus( const DBG_ADDR *addr)
1012 struct name_hash * nearest = NULL;
1013 int mid, high, low;
1015 if( sortlist_valid == FALSE )
1017 DEBUG_ResortSymbols();
1021 * Binary search to find closest symbol.
1023 low = 0;
1024 high = sorttab_nsym;
1025 if( addr_sorttab[0]->addr.seg > addr->seg
1026 || ( addr_sorttab[0]->addr.seg == addr->seg
1027 && addr_sorttab[0]->addr.off > addr->off) )
1029 nearest = NULL;
1031 else if( addr_sorttab[high - 1]->addr.seg < addr->seg
1032 || ( addr_sorttab[high - 1]->addr.seg == addr->seg
1033 && addr_sorttab[high - 1]->addr.off < addr->off) )
1035 nearest = addr_sorttab[high - 1];
1037 else
1039 while(1==1)
1041 mid = (high + low)/2;
1042 if( mid == low )
1045 * See if there are any other entries that might also
1046 * have the same address, and would also have a line
1047 * number table.
1049 if( mid > 0 && addr_sorttab[mid]->linetab == NULL )
1051 if( (addr_sorttab[mid - 1]->addr.seg ==
1052 addr_sorttab[mid]->addr.seg)
1053 && (addr_sorttab[mid - 1]->addr.off ==
1054 addr_sorttab[mid]->addr.off)
1055 && (addr_sorttab[mid - 1]->linetab != NULL) )
1057 mid--;
1061 if( (mid < sorttab_nsym - 1)
1062 && (addr_sorttab[mid]->linetab == NULL) )
1064 if( (addr_sorttab[mid + 1]->addr.seg ==
1065 addr_sorttab[mid]->addr.seg)
1066 && (addr_sorttab[mid + 1]->addr.off ==
1067 addr_sorttab[mid]->addr.off)
1068 && (addr_sorttab[mid + 1]->linetab != NULL) )
1070 mid++;
1073 nearest = addr_sorttab[mid];
1074 #if 0
1075 fprintf(stderr, "Found %x:%x when looking for %x:%x %x %s\n",
1076 addr_sorttab[mid ]->addr.seg,
1077 addr_sorttab[mid ]->addr.off,
1078 addr->seg, addr->off,
1079 addr_sorttab[mid ]->linetab,
1080 addr_sorttab[mid ]->name);
1081 #endif
1082 break;
1084 if( (addr_sorttab[mid]->addr.seg < addr->seg)
1085 || ( addr_sorttab[mid]->addr.seg == addr->seg
1086 && addr_sorttab[mid]->addr.off <= addr->off) )
1088 low = mid;
1090 else
1092 high = mid;
1097 if (!nearest) return FUNC_HAS_NO_LINES;
1099 if( nearest->flags & SYM_STEP_THROUGH )
1102 * This will cause us to keep single stepping until
1103 * we get to the other side somewhere.
1105 return NOT_ON_LINENUMBER;
1108 if( (nearest->flags & SYM_TRAMPOLINE) )
1111 * This will cause us to keep single stepping until
1112 * we get to the other side somewhere.
1114 return FUNC_IS_TRAMPOLINE;
1117 if( nearest->linetab == NULL )
1119 return FUNC_HAS_NO_LINES;
1124 * We never want to stop on the first instruction of a function
1125 * even if it has it's own linenumber. Let the thing keep running
1126 * until it gets past the function prologue. We only do this if there
1127 * is more than one line number for the function, of course.
1129 if( nearest->addr.off == addr->off && nearest->n_lines > 1 )
1131 return NOT_ON_LINENUMBER;
1134 if( (nearest->sourcefile != NULL)
1135 && (addr->off - nearest->addr.off < 0x100000) )
1137 low = 0;
1138 high = nearest->n_lines;
1139 while ((high - low) > 1)
1141 mid = (high + low) / 2;
1142 if (addr->off < nearest->linetab[mid].pc_offset.off) high = mid;
1143 else low = mid;
1145 if (addr->off == nearest->linetab[low].pc_offset.off)
1146 return AT_LINENUMBER;
1147 else
1148 return NOT_ON_LINENUMBER;
1151 return FUNC_HAS_NO_LINES;
1154 /***********************************************************************
1155 * DEBUG_GetFuncInfo
1157 * Find the symbol nearest to a given address.
1158 * Returns sourcefile name and line number in a format that the listing
1159 * handler can deal with.
1161 void
1162 DEBUG_GetFuncInfo( struct list_id * ret, const char * filename,
1163 const char * name)
1165 char buffer[256];
1166 char * pnt;
1167 struct name_hash *nh;
1169 for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
1171 if( filename != NULL )
1174 if( nh->sourcefile == NULL )
1176 continue;
1179 pnt = strrchr(nh->sourcefile, '/');
1180 if( strcmp(nh->sourcefile, filename) != 0
1181 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1183 continue;
1186 if (!strcmp(nh->name, name)) break;
1189 if (!nh && (name[0] != '_'))
1191 buffer[0] = '_';
1192 strcpy(buffer+1, name);
1193 for(nh = name_hash_table[name_hash(buffer)]; nh; nh = nh->next)
1195 if( filename != NULL )
1197 if( nh->sourcefile == NULL )
1199 continue;
1202 pnt = strrchr(nh->sourcefile, '/');
1203 if( strcmp(nh->sourcefile, filename) != 0
1204 && (pnt == NULL || strcmp(pnt + 1, filename) != 0) )
1206 continue;
1209 if (!strcmp(nh->name, buffer)) break;
1213 if( !nh )
1215 if( filename != NULL )
1217 fprintf(stderr, "No such function %s in %s\n", name, filename);
1219 else
1221 fprintf(stderr, "No such function %s\n", name);
1223 ret->sourcefile = NULL;
1224 ret->line = -1;
1225 return;
1228 ret->sourcefile = nh->sourcefile;
1231 * Search for the specific line number. If we don't find it,
1232 * then return FALSE.
1234 if( nh->linetab == NULL )
1236 ret->line = -1;
1238 else
1240 ret->line = nh->linetab[0].line_number;
1244 /***********************************************************************
1245 * DEBUG_GetStackSymbolValue
1247 * Get the address of a named symbol from the current stack frame.
1249 static
1250 BOOL DEBUG_GetStackSymbolValue( const char * name, DBG_ADDR *addr )
1252 struct name_hash * curr_func;
1253 unsigned int ebp;
1254 unsigned int eip;
1255 int i;
1257 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1259 return FALSE;
1262 for(i=0; i < curr_func->n_locals; i++ )
1265 * Test the range of validity of the local variable. This
1266 * comes up with RBRAC/LBRAC stabs in particular.
1268 if( (curr_func->local_vars[i].pc_start != 0)
1269 && ((eip - curr_func->addr.off)
1270 < curr_func->local_vars[i].pc_start) )
1272 continue;
1275 if( (curr_func->local_vars[i].pc_end != 0)
1276 && ((eip - curr_func->addr.off)
1277 > curr_func->local_vars[i].pc_end) )
1279 continue;
1282 if( strcmp(name, curr_func->local_vars[i].name) == 0 )
1285 * OK, we found it. Now figure out what to do with this.
1287 /* FIXME: what if regno == 0 ($eax) */
1288 if( curr_func->local_vars[i].regno != 0 )
1291 * Register variable. Point to DEBUG_context field.
1293 addr->seg = 0;
1294 addr->off = ((DWORD)&DEBUG_context) + reg_ofs[curr_func->local_vars[i].regno];
1295 addr->type = curr_func->local_vars[i].type;
1297 return TRUE;
1300 addr->seg = 0;
1301 addr->off = ebp + curr_func->local_vars[i].offset;
1302 addr->type = curr_func->local_vars[i].type;
1304 return TRUE;
1307 return FALSE;
1311 DEBUG_InfoLocals()
1313 struct name_hash * curr_func;
1314 unsigned int ebp;
1315 unsigned int eip;
1316 int i;
1317 unsigned int * ptr;
1318 int rtn = FALSE;
1320 if( DEBUG_GetCurrentFrame(&curr_func, &eip, &ebp) == FALSE )
1322 return FALSE;
1325 for(i=0; i < curr_func->n_locals; i++ )
1328 * Test the range of validity of the local variable. This
1329 * comes up with RBRAC/LBRAC stabs in particular.
1331 if( (curr_func->local_vars[i].pc_start != 0)
1332 && ((eip - curr_func->addr.off)
1333 < curr_func->local_vars[i].pc_start) )
1335 continue;
1338 if( (curr_func->local_vars[i].pc_end != 0)
1339 && ((eip - curr_func->addr.off)
1340 > curr_func->local_vars[i].pc_end) )
1342 continue;
1345 if( curr_func->local_vars[i].offset == 0 )
1347 ptr = (unsigned int *) (((DWORD)&DEBUG_context)
1348 + reg_ofs[curr_func->local_vars[i].regno]);
1349 fprintf(stderr, "%s:%s (optimized into register $%s) == 0x%8.8x\n",
1350 curr_func->name, curr_func->local_vars[i].name,
1351 reg_name[curr_func->local_vars[i].regno],
1352 *ptr);
1354 else
1356 ptr = (unsigned int *) (ebp + curr_func->local_vars[i].offset);
1357 fprintf(stderr, "%s:%s == 0x%8.8x\n",
1358 curr_func->name, curr_func->local_vars[i].name,
1359 *ptr);
1363 rtn = TRUE;
1365 return (rtn);
1369 DEBUG_SetSymbolSize(struct name_hash * sym, unsigned int len)
1371 sym->symbol_size = len;
1373 return TRUE;
1377 DEBUG_SetSymbolBPOff(struct name_hash * sym, unsigned int off)
1379 sym->breakpoint_offset = off;
1381 return TRUE;
1385 DEBUG_GetSymbolAddr(struct name_hash * sym, DBG_ADDR * addr)
1388 *addr = sym->addr;
1390 return TRUE;
1393 int DEBUG_SetLocalSymbolType(struct wine_locals * sym, struct datatype * type)
1395 sym->type = type;
1397 return TRUE;