2 * File hash.c - generate hash tables for Wine debugger symbols
4 * Copyright (C) 1993, Eric Youngdale.
13 #include <sys/types.h>
17 #include "selectors.h"
22 #define NR_NAME_HASH 16384
24 #define PATH_MAX _MAX_PATH
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
)
43 struct name_hash
* next
; /* Used to look up within name hash */
49 WineLocals
* local_vars
;
57 unsigned short breakpoint_offset
;
58 unsigned int symbol_size
;
62 static BOOL
DEBUG_GetStackSymbolValue( const char * name
, DBG_ADDR
*addr
);
63 static int sortlist_valid
= FALSE
;
65 static int sorttab_nsym
;
66 static struct name_hash
** addr_sorttab
= NULL
;
68 static struct name_hash
* name_hash_table
[NR_NAME_HASH
];
70 static unsigned int name_hash( const char * name
)
72 unsigned int hash
= 0;
80 hash
= (hash
<< 4) + *p
++;
82 if( (tmp
= (hash
& 0xf0000000)) )
88 return hash
% NR_NAME_HASH
;
92 DEBUG_cmp_sym(const void * p1
, const void * p2
)
94 struct name_hash
** name1
= (struct name_hash
**) p1
;
95 struct name_hash
** name2
= (struct name_hash
**) p2
;
97 if( ((*name1
)->flags
& SYM_INVALID
) != 0 )
102 if( ((*name2
)->flags
& SYM_INVALID
) != 0 )
107 if( (*name1
)->addr
.seg
> (*name2
)->addr
.seg
)
112 if( (*name1
)->addr
.seg
< (*name2
)->addr
.seg
)
117 if( (*name1
)->addr
.off
> (*name2
)->addr
.off
)
122 if( (*name1
)->addr
.off
< (*name2
)->addr
.off
)
130 /***********************************************************************
131 * DEBUG_ResortSymbols
133 * Rebuild sorted list of symbols.
137 DEBUG_ResortSymbols()
139 struct name_hash
*nh
;
143 for(i
=0; i
<NR_NAME_HASH
; i
++)
145 for (nh
= name_hash_table
[i
]; nh
; nh
= nh
->next
)
157 addr_sorttab
= (struct name_hash
**) xrealloc(addr_sorttab
,
158 nsym
* sizeof(struct name_hash
*));
161 for(i
=0; i
<NR_NAME_HASH
; i
++)
163 for (nh
= name_hash_table
[i
]; nh
; nh
= nh
->next
)
165 addr_sorttab
[nsym
++] = nh
;
169 qsort(addr_sorttab
, nsym
,
170 sizeof(struct name_hash
*), DEBUG_cmp_sym
);
171 sortlist_valid
= TRUE
;
175 /***********************************************************************
178 * Add a symbol to the table.
181 DEBUG_AddSymbol( const char * name
, const DBG_ADDR
*addr
, const char * source
,
184 struct name_hash
* new;
185 struct name_hash
*nh
;
186 static char prev_source
[PATH_MAX
] = {'\0', };
187 static char * prev_duped_source
= NULL
;
191 hash
= name_hash(name
);
192 for (nh
= name_hash_table
[hash
]; nh
; nh
= nh
->next
)
194 if( ((nh
->flags
& SYM_INVALID
) != 0) && strcmp(name
, nh
->name
) == 0 )
196 nh
->addr
.off
= addr
->off
;
197 nh
->addr
.seg
= addr
->seg
;
198 if( nh
->addr
.type
== NULL
&& addr
->type
!= NULL
)
200 nh
->addr
.type
= addr
->type
;
202 nh
->flags
&= ~SYM_INVALID
;
205 if (nh
->addr
.seg
== addr
->seg
&&
206 nh
->addr
.off
== addr
->off
&&
207 strcmp(name
, nh
->name
) == 0 )
214 * First see if we already have an entry for this symbol. If so
215 * return it, so we don't end up with duplicates.
218 new = (struct name_hash
*) xmalloc(sizeof(struct name_hash
));
220 new->name
= xstrdup(name
);
225 * This is an enhancement to reduce memory consumption. The idea
226 * is that we duplicate a given string only once. This is a big
227 * win if there are lots of symbols defined in a given source file.
229 if( strcmp(source
, prev_source
) == 0 )
231 new->sourcefile
= prev_duped_source
;
235 strcpy(prev_source
, source
);
236 prev_duped_source
= new->sourcefile
= xstrdup(source
);
241 new->sourcefile
= NULL
;
245 new->lines_alloc
= 0;
249 new->locals_alloc
= 0;
250 new->local_vars
= NULL
;
255 /* Now insert into the hash table */
256 new->next
= name_hash_table
[hash
];
257 name_hash_table
[hash
] = new;
260 * Check some heuristics based upon the file name to see whether
261 * we want to step through this guy or not. These are machine generated
262 * assembly files that are used to translate between the MS way of
263 * calling things and the GCC way of calling things. In general we
264 * always want to step through.
268 c
= strrchr(source
, '.');
269 if( c
!= NULL
&& strcmp(c
, ".s") == 0 )
271 c
= strrchr(source
, '/');
275 if( (strcmp(c
, "callfrom16.s") == 0)
276 || (strcmp(c
, "callto16.s") == 0)
277 || (strcmp(c
, "call32.s") == 0) )
279 new->flags
|= SYM_TRAMPOLINE
;
285 sortlist_valid
= FALSE
;
289 BOOL
DEBUG_Normalize(struct name_hash
* nh
)
293 * We aren't adding any more locals or linenumbers to this function.
294 * Free any spare memory that we might have allocated.
301 if( nh
->n_locals
!= nh
->locals_alloc
)
303 nh
->locals_alloc
= nh
->n_locals
;
304 nh
->local_vars
= xrealloc(nh
->local_vars
,
305 nh
->locals_alloc
* sizeof(WineLocals
));
308 if( nh
->n_lines
!= nh
->lines_alloc
)
310 nh
->lines_alloc
= nh
->n_lines
;
311 nh
->linetab
= xrealloc(nh
->linetab
,
312 nh
->lines_alloc
* sizeof(WineLineNo
));
318 /***********************************************************************
319 * DEBUG_GetSymbolValue
321 * Get the address of a named symbol.
323 BOOL
DEBUG_GetSymbolValue( const char * name
, const int lineno
,
324 DBG_ADDR
*addr
, int bp_flag
)
327 struct name_hash
*nh
;
329 for(nh
= name_hash_table
[name_hash(name
)]; nh
; nh
= nh
->next
)
331 if( (nh
->flags
& SYM_INVALID
) != 0 )
336 if (!strcmp(nh
->name
, name
)) break;
339 if (!nh
&& (name
[0] != '_'))
342 strcpy(buffer
+1, name
);
343 for(nh
= name_hash_table
[name_hash(buffer
)]; nh
; nh
= nh
->next
)
345 if( (nh
->flags
& SYM_INVALID
) != 0 )
349 if (!strcmp(nh
->name
, buffer
)) break;
354 * If we don't have anything here, then try and see if this
355 * is a local symbol to the current stack frame. No matter
356 * what, we have nothing more to do, so we let that function
357 * decide what we ultimately return.
361 return DEBUG_GetStackSymbolValue(name
, addr
);
364 return DEBUG_GetLineNumberAddr( nh
, lineno
, addr
, bp_flag
);
367 /***********************************************************************
368 * DEBUG_GetLineNumberAddr
370 * Get the address of a named symbol.
372 BOOL
DEBUG_GetLineNumberAddr( struct name_hash
* nh
, const int lineno
,
373 DBG_ADDR
*addr
, int bp_flag
)
382 addr
->off
+= nh
->breakpoint_offset
;
388 * Search for the specific line number. If we don't find it,
391 if( nh
->linetab
== NULL
)
396 for(i
=0; i
< nh
->n_lines
; i
++ )
398 if( nh
->linetab
[i
].line_number
== lineno
)
400 *addr
= nh
->linetab
[i
].pc_offset
;
406 * This specific line number not found.
415 /***********************************************************************
416 * DEBUG_SetSymbolValue
418 * Set the address of a named symbol.
420 BOOL
DEBUG_SetSymbolValue( const char * name
, const DBG_ADDR
*addr
)
423 struct name_hash
*nh
;
425 for(nh
= name_hash_table
[name_hash(name
)]; nh
; nh
= nh
->next
)
426 if (!strcmp(nh
->name
, name
)) break;
428 if (!nh
&& (name
[0] != '_'))
431 strcpy(buffer
+1, name
);
432 for(nh
= name_hash_table
[name_hash(buffer
)]; nh
; nh
= nh
->next
)
433 if (!strcmp(nh
->name
, buffer
)) break;
436 if (!nh
) return FALSE
;
438 nh
->flags
&= SYM_INVALID
;
439 DBG_FIX_ADDR_SEG( &nh
->addr
, DS_reg(&DEBUG_context
) );
444 /***********************************************************************
445 * DEBUG_FindNearestSymbol
447 * Find the symbol nearest to a given address.
448 * If ebp is specified as non-zero, it means we should dump the argument
449 * list into the string we return as well.
451 const char * DEBUG_FindNearestSymbol( const DBG_ADDR
*addr
, int flag
,
452 struct name_hash
** rtn
,
454 struct list_id
* source
)
456 static char name_buffer
[MAX_PATH
+ 256];
457 static char arglist
[1024];
458 static char argtmp
[256];
459 struct name_hash
* nearest
= NULL
;
463 char * lineinfo
, *sourcefile
;
474 source
->sourcefile
= NULL
;
478 if( sortlist_valid
== FALSE
)
480 DEBUG_ResortSymbols();
483 if( sortlist_valid
== FALSE
)
489 * FIXME - use the binary search that we added to
490 * the function DEBUG_CheckLinenoStatus. Better yet, we should
491 * probably keep some notion of the current function so we don't
492 * have to search every time.
495 * Binary search to find closest symbol.
499 if( addr_sorttab
[0]->addr
.seg
> addr
->seg
500 || ( addr_sorttab
[0]->addr
.seg
== addr
->seg
501 && addr_sorttab
[0]->addr
.off
> addr
->off
) )
505 else if( addr_sorttab
[high
- 1]->addr
.seg
< addr
->seg
506 || ( addr_sorttab
[high
- 1]->addr
.seg
== addr
->seg
507 && addr_sorttab
[high
- 1]->addr
.off
< addr
->off
) )
509 nearest
= addr_sorttab
[high
- 1];
515 mid
= (high
+ low
)/2;
519 * See if there are any other entries that might also
520 * have the same address, and would also have a line
523 if( mid
> 0 && addr_sorttab
[mid
]->linetab
== NULL
)
525 if( (addr_sorttab
[mid
- 1]->addr
.seg
==
526 addr_sorttab
[mid
]->addr
.seg
)
527 && (addr_sorttab
[mid
- 1]->addr
.off
==
528 addr_sorttab
[mid
]->addr
.off
)
529 && (addr_sorttab
[mid
- 1]->linetab
!= NULL
) )
535 if( (mid
< sorttab_nsym
- 1)
536 && (addr_sorttab
[mid
]->linetab
== NULL
) )
538 if( (addr_sorttab
[mid
+ 1]->addr
.seg
==
539 addr_sorttab
[mid
]->addr
.seg
)
540 && (addr_sorttab
[mid
+ 1]->addr
.off
==
541 addr_sorttab
[mid
]->addr
.off
)
542 && (addr_sorttab
[mid
+ 1]->linetab
!= NULL
) )
547 nearest
= addr_sorttab
[mid
];
549 fprintf(stderr
, "Found %x:%x when looking for %x:%x %x %s\n",
550 addr_sorttab
[mid
]->addr
.seg
,
551 addr_sorttab
[mid
]->addr
.off
,
552 addr
->seg
, addr
->off
,
553 addr_sorttab
[mid
]->linetab
,
554 addr_sorttab
[mid
]->name
);
558 if( (addr_sorttab
[mid
]->addr
.seg
< addr
->seg
)
559 || ( addr_sorttab
[mid
]->addr
.seg
== addr
->seg
560 && addr_sorttab
[mid
]->addr
.off
<= addr
->off
) )
571 if (!nearest
) return NULL
;
579 * Fill in the relevant bits to the structure so that we can
580 * locate the source and line for this bit of code.
584 source
->sourcefile
= nearest
->sourcefile
;
585 if( nearest
->linetab
== NULL
)
591 source
->line
= nearest
->linetab
[0].line_number
;
599 * Prepare to display the argument list. If ebp is specified, it is
600 * the framepointer for the function in question. If not specified,
601 * we don't want the arglist.
603 memset(arglist
, '\0', sizeof(arglist
));
606 for(i
=0; i
< nearest
->n_locals
; i
++ )
609 * If this is a register (offset == 0) or a local
610 * variable, we don't want to know about it.
612 if( nearest
->local_vars
[i
].offset
<= 0 )
617 ptr
= (unsigned int *) (ebp
+ nearest
->local_vars
[i
].offset
);
618 if( arglist
[0] == '\0' )
624 strcat(arglist
, ", ");
627 sprintf(argtmp
, "%s=0x%x", nearest
->local_vars
[i
].name
,
629 strcat(arglist
, argtmp
);
631 if( arglist
[0] == '(' )
633 strcat(arglist
, ")");
637 if( (nearest
->sourcefile
!= NULL
) && (flag
== TRUE
)
638 && (addr
->off
- nearest
->addr
.off
< 0x100000) )
642 * Try and find the nearest line number to the current offset.
644 if( nearest
->linetab
!= NULL
)
647 high
= nearest
->n_lines
;
648 while ((high
- low
) > 1)
650 mid
= (high
+ low
) / 2;
651 if (addr
->off
< nearest
->linetab
[mid
].pc_offset
.off
)
656 lineno
= nearest
->linetab
[low
].line_number
;
661 sprintf(linebuff
, ":%d", lineno
);
665 source
->line
= lineno
;
669 /* Remove the path from the file name */
670 sourcefile
= strrchr( nearest
->sourcefile
, '/' );
671 if (!sourcefile
) sourcefile
= nearest
->sourcefile
;
674 if (addr
->off
== nearest
->addr
.off
)
675 sprintf( name_buffer
, "%s%s [%s%s]", nearest
->name
,
676 arglist
, sourcefile
, lineinfo
);
678 sprintf( name_buffer
, "%s+0x%lx%s [%s%s]", nearest
->name
,
679 addr
->off
- nearest
->addr
.off
,
680 arglist
, sourcefile
, lineinfo
);
684 if (addr
->off
== nearest
->addr
.off
)
685 sprintf( name_buffer
, "%s%s", nearest
->name
, arglist
);
687 if (addr
->seg
&& (nearest
->addr
.seg
!=addr
->seg
))
690 sprintf( name_buffer
, "%s+0x%lx%s", nearest
->name
,
691 addr
->off
- nearest
->addr
.off
, arglist
);
698 /***********************************************************************
699 * DEBUG_ReadSymbolTable
701 * Read a symbol file into the hash table.
703 void DEBUG_ReadSymbolTable( const char * filename
)
706 DBG_ADDR addr
= { 0, 0 };
713 if (!(symbolfile
= fopen(filename
, "r")))
715 fprintf( stderr
, "Unable to open symbol table %s\n", filename
);
719 fprintf( stderr
, "Reading symbols from file %s\n", filename
);
723 fgets( buffer
, sizeof(buffer
), symbolfile
);
724 if (feof(symbolfile
)) break;
726 /* Strip any text after a # sign (i.e. comments) */
729 if(*cpnt
++ == '#') { *cpnt
= 0; break; }
731 /* Quietly ignore any lines that have just whitespace */
735 if(*cpnt
!= ' ' && *cpnt
!= '\t') break;
738 if (!(*cpnt
) || *cpnt
== '\n') continue;
740 nargs
= sscanf(buffer
, "%lx %c %s", &addr
.off
, &type
, name
);
741 DEBUG_AddSymbol( name
, &addr
, NULL
, SYM_WINE
);
747 /***********************************************************************
748 * DEBUG_LoadEntryPoints16
750 * Load the entry points of a Win16 module into the hash table.
752 static void DEBUG_LoadEntryPoints16( HMODULE16 hModule
, NE_MODULE
*pModule
,
759 /* First search the resident names */
761 unsigned char *cpnt
= (unsigned char *)pModule
+ pModule
->name_table
;
764 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
765 sprintf( buffer
, "%s.%.*s", name
, *cpnt
, cpnt
+ 1 );
766 if ((address
= NE_GetEntryPoint(hModule
, *(WORD
*)(cpnt
+ *cpnt
+ 1))))
768 addr
.seg
= HIWORD(address
);
769 addr
.off
= LOWORD(address
);
771 DEBUG_AddSymbol( buffer
, &addr
, NULL
, SYM_WIN32
| SYM_FUNC
);
775 /* Now search the non-resident names table */
777 if (!pModule
->nrname_handle
) return; /* No non-resident table */
778 cpnt
= (char *)GlobalLock16( pModule
->nrname_handle
);
781 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
782 sprintf( buffer
, "%s.%.*s", name
, *cpnt
, cpnt
+ 1 );
783 if ((address
= NE_GetEntryPoint(hModule
, *(WORD
*)(cpnt
+ *cpnt
+ 1))))
785 addr
.seg
= HIWORD(address
);
786 addr
.off
= LOWORD(address
);
788 DEBUG_AddSymbol( buffer
, &addr
, NULL
, SYM_WIN32
| SYM_FUNC
);
794 /***********************************************************************
795 * DEBUG_LoadEntryPoints32
797 * Load the entry points of a Win32 module into the hash table.
799 static void DEBUG_LoadEntryPoints32( HMODULE hModule
, const char *name
)
801 #define RVA(x) (hModule+(DWORD)(x))
806 IMAGE_SECTION_HEADER
*pe_seg
;
807 IMAGE_EXPORT_DIRECTORY
*exports
;
808 IMAGE_DATA_DIRECTORY
*dir
;
816 /* Add start of DLL */
819 DEBUG_AddSymbol( name
, &addr
, NULL
, SYM_WIN32
| SYM_FUNC
);
821 /* Add entry point */
823 sprintf( buffer
, "%s.EntryPoint", name
);
824 addr
.off
= (DWORD
)RVA_PTR( hModule
, OptionalHeader
.AddressOfEntryPoint
);
825 DEBUG_AddSymbol( buffer
, &addr
, NULL
, SYM_WIN32
| SYM_FUNC
);
827 /* Add start of sections */
829 pe_seg
= PE_SECTIONS(hModule
);
830 for (i
= 0; i
< PE_HEADER(hModule
)->FileHeader
.NumberOfSections
; i
++)
832 sprintf( buffer
, "%s.%s", name
, pe_seg
->Name
);
833 addr
.off
= RVA(pe_seg
->VirtualAddress
);
834 DEBUG_AddSymbol( buffer
, &addr
, NULL
, SYM_WIN32
| SYM_FUNC
);
838 /* Add exported functions */
840 dir
= &PE_HEADER(hModule
)->OptionalHeader
.
841 DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
];
844 exports
= (IMAGE_EXPORT_DIRECTORY
*)RVA( dir
->VirtualAddress
);
845 ordinals
= (WORD
*)RVA( exports
->AddressOfNameOrdinals
);
846 names
= (const char **)RVA( exports
->AddressOfNames
);
847 functions
= (void **)RVA( exports
->AddressOfFunctions
);
849 for (i
= 0; i
< exports
->NumberOfNames
; i
++)
851 if (!names
[i
]) continue;
852 sprintf( buffer
, "%s.%s", name
, (char *)RVA(names
[i
]) );
853 addr
.off
= RVA( functions
[ordinals
[i
]] );
854 DEBUG_AddSymbol( buffer
, &addr
, NULL
, SYM_WIN32
| SYM_FUNC
);
857 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++)
859 if (!functions
[i
]) continue;
860 /* Check if we already added it with a name */
861 for (j
= 0; j
< exports
->NumberOfNames
; j
++)
862 if ((ordinals
[j
] == i
) && names
[j
]) break;
863 if (j
< exports
->NumberOfNames
) continue;
864 sprintf( buffer
, "%s.%ld", name
, i
+ exports
->Base
);
865 addr
.off
= (DWORD
)RVA( functions
[i
] );
866 DEBUG_AddSymbol( buffer
, &addr
, NULL
, SYM_WIN32
| SYM_FUNC
);
869 DEBUG_RegisterDebugInfo(hModule
, name
);
874 /***********************************************************************
875 * DEBUG_LoadEntryPoints
877 * Load the entry points of all the modules into the hash table.
879 void DEBUG_LoadEntryPoints(void)
887 fprintf( stderr
, " " );
888 for (ok
= ModuleFirst16(&entry
); ok
; ok
= ModuleNext16(&entry
))
890 if (!(pModule
= NE_GetPtr( entry
.hModule
))) continue;
891 if (!(pModule
->flags
& NE_FFLAGS_WIN32
)) /* NE module */
893 if ((rowcount
+ strlen(entry
.szModule
)) > 76)
895 fprintf( stderr
,"\n ");
898 fprintf( stderr
, " %s", entry
.szModule
);
899 rowcount
+= strlen(entry
.szModule
) + 1;
901 DEBUG_LoadEntryPoints16( entry
.hModule
, pModule
, entry
.szModule
);
904 for (wm
=PROCESS_Current()->modref_list
;wm
;wm
=wm
->next
)
906 if ((rowcount
+ strlen(wm
->modname
)) > 76)
908 fprintf( stderr
,"\n ");
911 fprintf( stderr
, " %s", wm
->modname
);
912 rowcount
+= strlen(wm
->modname
) + 1;
913 DEBUG_LoadEntryPoints32( wm
->module
, wm
->modname
);
915 fprintf( stderr
, "\n" );
920 DEBUG_AddLineNumber( struct name_hash
* func
, int line_num
,
921 unsigned long offset
)
928 if( func
->n_lines
+ 1 >= func
->lines_alloc
)
930 func
->lines_alloc
+= 64;
931 func
->linetab
= xrealloc(func
->linetab
,
932 func
->lines_alloc
* sizeof(WineLineNo
));
935 func
->linetab
[func
->n_lines
].line_number
= line_num
;
936 func
->linetab
[func
->n_lines
].pc_offset
.seg
= func
->addr
.seg
;
937 func
->linetab
[func
->n_lines
].pc_offset
.off
= func
->addr
.off
+ offset
;
938 func
->linetab
[func
->n_lines
].pc_offset
.type
= NULL
;
944 DEBUG_AddLocal( struct name_hash
* func
, int regno
,
955 if( func
->n_locals
+ 1 >= func
->locals_alloc
)
957 func
->locals_alloc
+= 32;
958 func
->local_vars
= xrealloc(func
->local_vars
,
959 func
->locals_alloc
* sizeof(WineLocals
));
962 func
->local_vars
[func
->n_locals
].regno
= regno
;
963 func
->local_vars
[func
->n_locals
].offset
= offset
;
964 func
->local_vars
[func
->n_locals
].pc_start
= pc_start
;
965 func
->local_vars
[func
->n_locals
].pc_end
= pc_end
;
966 func
->local_vars
[func
->n_locals
].name
= xstrdup(name
);
967 func
->local_vars
[func
->n_locals
].type
= NULL
;
970 return &func
->local_vars
[func
->n_locals
- 1];
978 struct name_hash
*nh
;
981 * Utility function to dump stats about the hash table.
983 for(i
=0; i
<NR_NAME_HASH
; i
++)
986 for (nh
= name_hash_table
[i
]; nh
; nh
= nh
->next
)
990 fprintf(stderr
, "Bucket %d: %d\n", i
, depth
);
994 /***********************************************************************
995 * DEBUG_CheckLinenoStatus
997 * Find the symbol nearest to a given address.
998 * If ebp is specified as non-zero, it means we should dump the argument
999 * list into the string we return as well.
1001 int DEBUG_CheckLinenoStatus( const DBG_ADDR
*addr
)
1003 struct name_hash
* nearest
= NULL
;
1006 if( sortlist_valid
== FALSE
)
1008 DEBUG_ResortSymbols();
1012 * Binary search to find closest symbol.
1015 high
= sorttab_nsym
;
1016 if( addr_sorttab
[0]->addr
.seg
> addr
->seg
1017 || ( addr_sorttab
[0]->addr
.seg
== addr
->seg
1018 && addr_sorttab
[0]->addr
.off
> addr
->off
) )
1022 else if( addr_sorttab
[high
- 1]->addr
.seg
< addr
->seg
1023 || ( addr_sorttab
[high
- 1]->addr
.seg
== addr
->seg
1024 && addr_sorttab
[high
- 1]->addr
.off
< addr
->off
) )
1026 nearest
= addr_sorttab
[high
- 1];
1032 mid
= (high
+ low
)/2;
1036 * See if there are any other entries that might also
1037 * have the same address, and would also have a line
1040 if( mid
> 0 && addr_sorttab
[mid
]->linetab
== NULL
)
1042 if( (addr_sorttab
[mid
- 1]->addr
.seg
==
1043 addr_sorttab
[mid
]->addr
.seg
)
1044 && (addr_sorttab
[mid
- 1]->addr
.off
==
1045 addr_sorttab
[mid
]->addr
.off
)
1046 && (addr_sorttab
[mid
- 1]->linetab
!= NULL
) )
1052 if( (mid
< sorttab_nsym
- 1)
1053 && (addr_sorttab
[mid
]->linetab
== NULL
) )
1055 if( (addr_sorttab
[mid
+ 1]->addr
.seg
==
1056 addr_sorttab
[mid
]->addr
.seg
)
1057 && (addr_sorttab
[mid
+ 1]->addr
.off
==
1058 addr_sorttab
[mid
]->addr
.off
)
1059 && (addr_sorttab
[mid
+ 1]->linetab
!= NULL
) )
1064 nearest
= addr_sorttab
[mid
];
1066 fprintf(stderr
, "Found %x:%x when looking for %x:%x %x %s\n",
1067 addr_sorttab
[mid
]->addr
.seg
,
1068 addr_sorttab
[mid
]->addr
.off
,
1069 addr
->seg
, addr
->off
,
1070 addr_sorttab
[mid
]->linetab
,
1071 addr_sorttab
[mid
]->name
);
1075 if( (addr_sorttab
[mid
]->addr
.seg
< addr
->seg
)
1076 || ( addr_sorttab
[mid
]->addr
.seg
== addr
->seg
1077 && addr_sorttab
[mid
]->addr
.off
<= addr
->off
) )
1088 if (!nearest
) return FUNC_HAS_NO_LINES
;
1090 if( nearest
->flags
& SYM_STEP_THROUGH
)
1093 * This will cause us to keep single stepping until
1094 * we get to the other side somewhere.
1096 return NOT_ON_LINENUMBER
;
1099 if( (nearest
->flags
& SYM_TRAMPOLINE
) )
1102 * This will cause us to keep single stepping until
1103 * we get to the other side somewhere.
1105 return FUNC_IS_TRAMPOLINE
;
1108 if( nearest
->linetab
== NULL
)
1110 return FUNC_HAS_NO_LINES
;
1115 * We never want to stop on the first instruction of a function
1116 * even if it has it's own linenumber. Let the thing keep running
1117 * until it gets past the function prologue. We only do this if there
1118 * is more than one line number for the function, of course.
1120 if( nearest
->addr
.off
== addr
->off
&& nearest
->n_lines
> 1 )
1122 return NOT_ON_LINENUMBER
;
1125 if( (nearest
->sourcefile
!= NULL
)
1126 && (addr
->off
- nearest
->addr
.off
< 0x100000) )
1129 high
= nearest
->n_lines
;
1130 while ((high
- low
) > 1)
1132 mid
= (high
+ low
) / 2;
1133 if (addr
->off
< nearest
->linetab
[mid
].pc_offset
.off
) high
= mid
;
1136 if (addr
->off
== nearest
->linetab
[low
].pc_offset
.off
)
1137 return AT_LINENUMBER
;
1139 return NOT_ON_LINENUMBER
;
1142 return FUNC_HAS_NO_LINES
;
1145 /***********************************************************************
1148 * Find the symbol nearest to a given address.
1149 * Returns sourcefile name and line number in a format that the listing
1150 * handler can deal with.
1153 DEBUG_GetFuncInfo( struct list_id
* ret
, const char * filename
,
1158 struct name_hash
*nh
;
1160 for(nh
= name_hash_table
[name_hash(name
)]; nh
; nh
= nh
->next
)
1162 if( filename
!= NULL
)
1165 if( nh
->sourcefile
== NULL
)
1170 pnt
= strrchr(nh
->sourcefile
, '/');
1171 if( strcmp(nh
->sourcefile
, filename
) != 0
1172 && (pnt
== NULL
|| strcmp(pnt
+ 1, filename
) != 0) )
1177 if (!strcmp(nh
->name
, name
)) break;
1180 if (!nh
&& (name
[0] != '_'))
1183 strcpy(buffer
+1, name
);
1184 for(nh
= name_hash_table
[name_hash(buffer
)]; nh
; nh
= nh
->next
)
1186 if( filename
!= NULL
)
1188 if( nh
->sourcefile
== NULL
)
1193 pnt
= strrchr(nh
->sourcefile
, '/');
1194 if( strcmp(nh
->sourcefile
, filename
) != 0
1195 && (pnt
== NULL
|| strcmp(pnt
+ 1, filename
) != 0) )
1200 if (!strcmp(nh
->name
, buffer
)) break;
1206 if( filename
!= NULL
)
1208 fprintf(stderr
, "No such function %s in %s\n", name
, filename
);
1212 fprintf(stderr
, "No such function %s\n", name
);
1214 ret
->sourcefile
= NULL
;
1219 ret
->sourcefile
= nh
->sourcefile
;
1222 * Search for the specific line number. If we don't find it,
1223 * then return FALSE.
1225 if( nh
->linetab
== NULL
)
1231 ret
->line
= nh
->linetab
[0].line_number
;
1235 /***********************************************************************
1236 * DEBUG_GetStackSymbolValue
1238 * Get the address of a named symbol from the current stack frame.
1241 BOOL
DEBUG_GetStackSymbolValue( const char * name
, DBG_ADDR
*addr
)
1243 struct name_hash
* curr_func
;
1248 if( DEBUG_GetCurrentFrame(&curr_func
, &eip
, &ebp
) == FALSE
)
1253 for(i
=0; i
< curr_func
->n_locals
; i
++ )
1256 * Test the range of validity of the local variable. This
1257 * comes up with RBRAC/LBRAC stabs in particular.
1259 if( (curr_func
->local_vars
[i
].pc_start
!= 0)
1260 && ((eip
- curr_func
->addr
.off
)
1261 < curr_func
->local_vars
[i
].pc_start
) )
1266 if( (curr_func
->local_vars
[i
].pc_end
!= 0)
1267 && ((eip
- curr_func
->addr
.off
)
1268 > curr_func
->local_vars
[i
].pc_end
) )
1273 if( strcmp(name
, curr_func
->local_vars
[i
].name
) == 0 )
1276 * OK, we found it. Now figure out what to do with this.
1278 /* FIXME: what if regno == 0 ($eax) */
1279 if( curr_func
->local_vars
[i
].regno
!= 0 )
1282 * Register variable. Point to DEBUG_context field.
1285 addr
->off
= ((DWORD
)&DEBUG_context
) + reg_ofs
[curr_func
->local_vars
[i
].regno
];
1286 addr
->type
= curr_func
->local_vars
[i
].type
;
1292 addr
->off
= ebp
+ curr_func
->local_vars
[i
].offset
;
1293 addr
->type
= curr_func
->local_vars
[i
].type
;
1304 struct name_hash
* curr_func
;
1311 if( DEBUG_GetCurrentFrame(&curr_func
, &eip
, &ebp
) == FALSE
)
1316 for(i
=0; i
< curr_func
->n_locals
; i
++ )
1319 * Test the range of validity of the local variable. This
1320 * comes up with RBRAC/LBRAC stabs in particular.
1322 if( (curr_func
->local_vars
[i
].pc_start
!= 0)
1323 && ((eip
- curr_func
->addr
.off
)
1324 < curr_func
->local_vars
[i
].pc_start
) )
1329 if( (curr_func
->local_vars
[i
].pc_end
!= 0)
1330 && ((eip
- curr_func
->addr
.off
)
1331 > curr_func
->local_vars
[i
].pc_end
) )
1336 if( curr_func
->local_vars
[i
].offset
== 0 )
1338 ptr
= (unsigned int *) (((DWORD
)&DEBUG_context
)
1339 + reg_ofs
[curr_func
->local_vars
[i
].regno
]);
1340 fprintf(stderr
, "%s:%s (optimized into register $%s) == 0x%8.8x\n",
1341 curr_func
->name
, curr_func
->local_vars
[i
].name
,
1342 reg_name
[curr_func
->local_vars
[i
].regno
],
1347 ptr
= (unsigned int *) (ebp
+ curr_func
->local_vars
[i
].offset
);
1348 fprintf(stderr
, "%s:%s == 0x%8.8x\n",
1349 curr_func
->name
, curr_func
->local_vars
[i
].name
,
1360 DEBUG_SetSymbolSize(struct name_hash
* sym
, unsigned int len
)
1362 sym
->symbol_size
= len
;
1368 DEBUG_SetSymbolBPOff(struct name_hash
* sym
, unsigned int off
)
1370 sym
->breakpoint_offset
= off
;
1376 DEBUG_GetSymbolAddr(struct name_hash
* sym
, DBG_ADDR
* addr
)
1384 int DEBUG_SetLocalSymbolType(struct wine_locals
* sym
, struct datatype
* type
)