2 * File hash.c - generate hash tables for Wine debugger symbols
4 * Copyright (C) 1993, Eric Youngdale.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/types.h>
30 #define NR_NAME_HASH 16384
32 #define PATH_MAX MAX_PATH
36 static char * reg_name
[] =
38 "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"
41 static unsigned reg_ofs
[] =
43 FIELD_OFFSET(CONTEXT
, Eax
), FIELD_OFFSET(CONTEXT
, Ecx
),
44 FIELD_OFFSET(CONTEXT
, Edx
), FIELD_OFFSET(CONTEXT
, Ebx
),
45 FIELD_OFFSET(CONTEXT
, Esp
), FIELD_OFFSET(CONTEXT
, Ebp
),
46 FIELD_OFFSET(CONTEXT
, Esi
), FIELD_OFFSET(CONTEXT
, Edi
)
49 static char * reg_name
[] = { NULL
}; /* FIXME */
50 static unsigned reg_ofs
[] = { 0 };
56 struct name_hash
* next
; /* Used to look up within name hash */
62 WineLocals
* local_vars
;
70 unsigned short breakpoint_offset
;
71 unsigned int symbol_size
;
75 static BOOL
DEBUG_GetStackSymbolValue( const char * name
, DBG_VALUE
*value
);
76 static int sortlist_valid
= FALSE
;
78 static int sorttab_nsym
;
79 static struct name_hash
** addr_sorttab
= NULL
;
81 static struct name_hash
* name_hash_table
[NR_NAME_HASH
];
83 static unsigned int name_hash( const char * name
)
85 unsigned int hash
= 0;
93 hash
= (hash
<< 4) + *p
++;
95 if( (tmp
= (hash
& 0xf0000000)) )
101 return hash
% NR_NAME_HASH
;
105 DEBUG_cmp_sym(const void * p1
, const void * p2
)
107 struct name_hash
** name1
= (struct name_hash
**) p1
;
108 struct name_hash
** name2
= (struct name_hash
**) p2
;
110 if( ((*name1
)->flags
& SYM_INVALID
) != 0 )
115 if( ((*name2
)->flags
& SYM_INVALID
) != 0 )
120 if( (*name1
)->value
.addr
.seg
> (*name2
)->value
.addr
.seg
)
125 if( (*name1
)->value
.addr
.seg
< (*name2
)->value
.addr
.seg
)
130 if( (*name1
)->value
.addr
.off
> (*name2
)->value
.addr
.off
)
135 if( (*name1
)->value
.addr
.off
< (*name2
)->value
.addr
.off
)
143 /***********************************************************************
144 * DEBUG_ResortSymbols
146 * Rebuild sorted list of symbols.
150 DEBUG_ResortSymbols(void)
152 struct name_hash
*nh
;
156 for(i
=0; i
<NR_NAME_HASH
; i
++)
158 for (nh
= name_hash_table
[i
]; nh
; nh
= nh
->next
)
160 if( (nh
->flags
& SYM_INVALID
) == 0 )
163 DEBUG_Printf( DBG_CHN_MESG
, "Symbol %s (%04lx:%08lx) is invalid\n",
164 nh
->name
, nh
->value
.addr
.seg
, nh
->value
.addr
.off
);
174 addr_sorttab
= (struct name_hash
**) DBG_realloc(addr_sorttab
,
175 nsym
* sizeof(struct name_hash
*));
178 for(i
=0; i
<NR_NAME_HASH
; i
++)
180 for (nh
= name_hash_table
[i
]; nh
; nh
= nh
->next
)
182 if( (nh
->flags
& SYM_INVALID
) == 0 )
183 addr_sorttab
[nsym
++] = nh
;
187 qsort(addr_sorttab
, nsym
,
188 sizeof(struct name_hash
*), DEBUG_cmp_sym
);
189 sortlist_valid
= TRUE
;
193 /***********************************************************************
196 * Add a symbol to the table.
199 DEBUG_AddSymbol( const char * name
, const DBG_VALUE
*value
,
200 const char * source
, int flags
)
202 struct name_hash
* new;
203 struct name_hash
*nh
;
204 static char prev_source
[PATH_MAX
] = {'\0', };
205 static char * prev_duped_source
= NULL
;
208 assert(value
->cookie
== DV_TARGET
|| value
->cookie
== DV_HOST
);
210 hash
= name_hash(name
);
211 for (nh
= name_hash_table
[hash
]; nh
; nh
= nh
->next
)
213 if (name
[0] == nh
->name
[0] && strcmp(name
, nh
->name
) == 0)
215 int c
= memcmp(&nh
->value
.addr
, &value
->addr
, sizeof(value
->addr
));
217 if ((nh
->flags
& SYM_INVALID
) != 0)
219 /* this case happens in ELF files, where we don't get the
220 * address of a symbol for the stabs part, but rather from
221 * the ELF sections. in this case, we'll call this function
223 * - a first time while parsing the stabs, with a NULL
225 * - a second time with the correct address
226 * SYM_INVALID is set for the first pass, and cleared in the second
227 * the code below gets most of information for both passes
228 * latest GCC version seem to provide correct address in first pass,
230 if (nh
->value
.addr
.seg
== 0 && nh
->value
.addr
.off
== 0 && c
!= 0)
233 DEBUG_Printf(DBG_CHN_MESG
, "Changing address for symbol %s (%04lx:%08lx => %04lx:%08lx)\n",
234 name
, nh
->value
.addr
.seg
, nh
->value
.addr
.off
, value
->addr
.seg
, value
->addr
.off
);
236 nh
->value
.addr
= value
->addr
;
238 if (nh
->value
.type
== NULL
&& value
->type
!= NULL
)
240 nh
->value
.type
= value
->type
;
241 nh
->value
.cookie
= value
->cookie
;
243 /* it may happen that the same symbol is defined in several compilation
244 * units, but the linker decides to merge it into a single instance.
245 * in that case, we don't clear the invalid flag for all the compilation
246 * units (N_GSYM), and wait to get the symbol from the symtab
248 if ((flags
& SYM_INVALID
) == 0)
249 nh
->flags
&= ~SYM_INVALID
;
252 /* don't define a symbol twice */
253 if (c
== 0 && (flags
& SYM_INVALID
) == 0) return nh
;
258 DEBUG_Printf(DBG_CHN_TRACE
, "adding %s symbol (%s) from file '%s' at %04lx:%08lx\n",
259 (flags
& SYM_INVALID
) ? "invalid" : " valid", name
, source
, value
->addr
.seg
, value
->addr
.off
);
263 * First see if we already have an entry for this symbol. If so
264 * return it, so we don't end up with duplicates.
267 new = (struct name_hash
*) DBG_alloc(sizeof(struct name_hash
));
269 new->name
= DBG_strdup(name
);
274 * This is an enhancement to reduce memory consumption. The idea
275 * is that we duplicate a given string only once. This is a big
276 * win if there are lots of symbols defined in a given source file.
278 if( strcmp(source
, prev_source
) == 0 )
280 new->sourcefile
= prev_duped_source
;
284 strcpy(prev_source
, source
);
285 prev_duped_source
= new->sourcefile
= DBG_strdup(source
);
290 new->sourcefile
= NULL
;
294 new->lines_alloc
= 0;
298 new->locals_alloc
= 0;
299 new->local_vars
= NULL
;
304 /* Now insert into the hash table */
305 new->next
= name_hash_table
[hash
];
306 name_hash_table
[hash
] = new;
309 * Check some heuristics based upon the file name to see whether
310 * we want to step through this guy or not. These are machine generated
311 * assembly files that are used to translate between the MS way of
312 * calling things and the GCC way of calling things. In general we
313 * always want to step through.
315 if ( source
!= NULL
) {
316 int len
= strlen(source
);
318 if (len
> 2 && source
[len
-2] == '.' && source
[len
-1] == 's') {
319 char* c
= strrchr(source
- 2, '/');
321 if (strcmp(c
+ 1, "asmrelay.s") == 0)
322 new->flags
|= SYM_TRAMPOLINE
;
327 sortlist_valid
= FALSE
;
331 BOOL
DEBUG_Normalize(struct name_hash
* nh
)
335 * We aren't adding any more locals or linenumbers to this function.
336 * Free any spare memory that we might have allocated.
343 if( nh
->n_locals
!= nh
->locals_alloc
)
345 nh
->locals_alloc
= nh
->n_locals
;
346 nh
->local_vars
= DBG_realloc(nh
->local_vars
,
347 nh
->locals_alloc
* sizeof(WineLocals
));
350 if( nh
->n_lines
!= nh
->lines_alloc
)
352 nh
->lines_alloc
= nh
->n_lines
;
353 nh
->linetab
= DBG_realloc(nh
->linetab
,
354 nh
->lines_alloc
* sizeof(WineLineNo
));
360 /***********************************************************************
361 * DEBUG_GetSymbolValue
363 * Get the address of a named symbol.
365 * gsv_found: if the symbol is found
366 * gsv_unknown: if the symbol isn't found
367 * gsv_aborted: some error occured (likely, many symbols of same name exist,
368 * and user didn't pick one of them)
370 static int DEBUG_GSV_Helper(const char* name
, const int lineno
,
371 DBG_VALUE
* value
, int num
, int bp_flag
)
373 struct name_hash
* nh
;
377 for (nh
= name_hash_table
[name_hash(name
)]; nh
; nh
= nh
->next
)
379 if ((nh
->flags
& SYM_INVALID
) != 0) continue;
380 if (!strcmp(nh
->name
, name
) && DEBUG_GetLineNumberAddr( nh
, lineno
, &addr
, bp_flag
))
382 if (i
>= num
) return num
+ 1;
383 value
[i
].addr
= addr
;
384 value
[i
].type
= nh
->value
.type
;
385 value
[i
].cookie
= nh
->value
.cookie
;
392 enum get_sym_val
DEBUG_GetSymbolValue( const char * name
,
394 DBG_VALUE
*rtn
, int bp_flag
)
397 /* FIXME: NUMDBGV should be made variable */
398 DBG_VALUE value
[NUMDBGV
];
400 int num
, i
, local
= -1;
402 num
= DEBUG_GSV_Helper(name
, lineno
, value
, NUMDBGV
, bp_flag
);
403 if (!num
&& (name
[0] != '_'))
407 if (strlen(name
) < sizeof(buffer
) - 2) /* one for '_', one for '\0' */
410 strcpy(buffer
+ 1, name
);
411 num
= DEBUG_GSV_Helper(buffer
, lineno
, value
, NUMDBGV
, bp_flag
);
413 else DEBUG_Printf(DBG_CHN_WARN
, "Way too long symbol (%s)\n", name
);
416 /* now get the local symbols if any */
417 if (DEBUG_GetStackSymbolValue(name
, &vtmp
) && num
< NUMDBGV
)
426 } else if (!DEBUG_InteractiveP
|| num
== 1) {
431 if (num
== NUMDBGV
+1) {
432 DEBUG_Printf(DBG_CHN_MESG
, "Too many addresses for symbol '%s', limiting the first %d\n", name
, NUMDBGV
);
435 DEBUG_Printf(DBG_CHN_MESG
, "Many symbols with name '%s', choose the one you want (<cr> to abort):\n", name
);
436 for (i
= 0; i
< num
; i
++) {
437 DEBUG_Printf(DBG_CHN_MESG
, "[%d]: ", i
+ 1);
439 struct name_hash
*func
;
443 if (DEBUG_GetCurrentFrame(&func
, &eip
, &ebp
))
444 DEBUG_Printf(DBG_CHN_MESG
, "local variable of %s in %s\n", func
->name
, func
->sourcefile
);
446 DEBUG_Printf(DBG_CHN_MESG
, "local variable\n");
448 DEBUG_PrintAddress( &value
[i
].addr
, DEBUG_GetSelectorType(value
[i
].addr
.seg
), TRUE
);
449 DEBUG_Printf(DBG_CHN_MESG
, "\n");
454 if (DEBUG_ReadLine("=> ", buffer
, sizeof(buffer
)))
456 if (buffer
[0] == '\0') return gsv_aborted
;
458 if (i
< 1 || i
> num
)
459 DEBUG_Printf(DBG_CHN_MESG
, "Invalid choice %d\n", i
);
461 } while (i
< 1 || i
> num
);
463 /* The array is 0-based, but the choices are 1..n, so we have to subtract one before returning. */
470 /***********************************************************************
471 * DEBUG_GetLineNumberAddr
473 * Get the address of a named symbol.
475 BOOL
DEBUG_GetLineNumberAddr( const struct name_hash
* nh
, const int lineno
,
476 DBG_ADDR
*addr
, int bp_flag
)
482 *addr
= nh
->value
.addr
;
485 addr
->off
+= nh
->breakpoint_offset
;
491 * Search for the specific line number. If we don't find it,
494 if( nh
->linetab
== NULL
)
499 for(i
=0; i
< nh
->n_lines
; i
++ )
501 if( nh
->linetab
[i
].line_number
== lineno
)
503 *addr
= nh
->linetab
[i
].pc_offset
;
509 * This specific line number not found.
517 /***********************************************************************
518 * DEBUG_FindNearestSymbol
520 * Find the symbol nearest to a given address.
521 * If ebp is specified as non-zero, it means we should dump the argument
522 * list into the string we return as well.
524 const char * DEBUG_FindNearestSymbol( const DBG_ADDR
*addr
, int flag
,
525 struct name_hash
** rtn
,
527 struct list_id
* source
)
529 static char name_buffer
[MAX_PATH
+ 256];
530 static char arglist
[1024];
531 static char argtmp
[256];
532 struct name_hash
* nearest
= NULL
;
536 char * lineinfo
, *sourcefile
;
550 source
->sourcefile
= NULL
;
554 if( sortlist_valid
== FALSE
)
556 DEBUG_ResortSymbols();
559 if( sortlist_valid
== FALSE
)
565 * FIXME - use the binary search that we added to
566 * the function DEBUG_CheckLinenoStatus. Better yet, we should
567 * probably keep some notion of the current function so we don't
568 * have to search every time.
571 * Binary search to find closest symbol.
575 if( addr_sorttab
[0]->value
.addr
.seg
> addr
->seg
576 || ( addr_sorttab
[0]->value
.addr
.seg
== addr
->seg
577 && addr_sorttab
[0]->value
.addr
.off
> addr
->off
) )
581 else if( addr_sorttab
[high
- 1]->value
.addr
.seg
< addr
->seg
582 || ( addr_sorttab
[high
- 1]->value
.addr
.seg
== addr
->seg
583 && addr_sorttab
[high
- 1]->value
.addr
.off
< addr
->off
) )
585 nearest
= addr_sorttab
[high
- 1];
591 mid
= (high
+ low
)/2;
595 * See if there are any other entries that might also
596 * have the same address, and would also have a line
599 if( mid
> 0 && addr_sorttab
[mid
]->linetab
== NULL
)
601 if( (addr_sorttab
[mid
- 1]->value
.addr
.seg
==
602 addr_sorttab
[mid
]->value
.addr
.seg
)
603 && (addr_sorttab
[mid
- 1]->value
.addr
.off
==
604 addr_sorttab
[mid
]->value
.addr
.off
)
605 && (addr_sorttab
[mid
- 1]->linetab
!= NULL
) )
611 if( (mid
< sorttab_nsym
- 1)
612 && (addr_sorttab
[mid
]->linetab
== NULL
) )
614 if( (addr_sorttab
[mid
+ 1]->value
.addr
.seg
==
615 addr_sorttab
[mid
]->value
.addr
.seg
)
616 && (addr_sorttab
[mid
+ 1]->value
.addr
.off
==
617 addr_sorttab
[mid
]->value
.addr
.off
)
618 && (addr_sorttab
[mid
+ 1]->linetab
!= NULL
) )
623 nearest
= addr_sorttab
[mid
];
625 DEBUG_Printf(DBG_CHN_MESG
, "Found %x:%x when looking for %x:%x %x %s\n",
626 addr_sorttab
[mid
]->value
.addr
.seg
,
627 addr_sorttab
[mid
]->value
.addr
.off
,
628 addr
->seg
, addr
->off
,
629 addr_sorttab
[mid
]->linetab
,
630 addr_sorttab
[mid
]->name
);
634 if( (addr_sorttab
[mid
]->value
.addr
.seg
< addr
->seg
)
635 || ( addr_sorttab
[mid
]->value
.addr
.seg
== addr
->seg
636 && addr_sorttab
[mid
]->value
.addr
.off
<= addr
->off
) )
647 if (!nearest
) return NULL
;
655 * Fill in the relevant bits to the structure so that we can
656 * locate the source and line for this bit of code.
660 source
->sourcefile
= nearest
->sourcefile
;
661 if( nearest
->linetab
== NULL
)
667 source
->line
= nearest
->linetab
[0].line_number
;
675 * Prepare to display the argument list. If ebp is specified, it is
676 * the framepointer for the function in question. If not specified,
677 * we don't want the arglist.
679 memset(arglist
, '\0', sizeof(arglist
));
682 for(i
=0; i
< nearest
->n_locals
; i
++ )
685 * If this is a register (offset == 0) or a local
686 * variable, we don't want to know about it.
688 if( nearest
->local_vars
[i
].offset
<= 0 )
693 ptr
= (unsigned int *) (ebp
+ nearest
->local_vars
[i
].offset
);
694 if( arglist
[0] == '\0' )
700 strcat(arglist
, ", ");
702 DEBUG_READ_MEM_VERBOSE(ptr
, &val
, sizeof(val
));
703 snprintf(argtmp
, sizeof(argtmp
), "%s=0x%x", nearest
->local_vars
[i
].name
, val
);
705 strcat(arglist
, argtmp
);
707 if( arglist
[0] == '(' )
709 strcat(arglist
, ")");
713 module
= DEBUG_FindModuleByAddr((void*)DEBUG_ToLinear(addr
), DMT_UNKNOWN
);
715 char* ptr
= strrchr(module
->module_name
, '/');
717 if (!ptr
++) ptr
= module
->module_name
;
718 snprintf( modbuf
, sizeof(modbuf
), " in %s", ptr
);
723 if( (nearest
->sourcefile
!= NULL
) && (flag
== TRUE
)
724 && (addr
->off
- nearest
->value
.addr
.off
< 0x100000) )
728 * Try and find the nearest line number to the current offset.
730 if( nearest
->linetab
!= NULL
)
733 high
= nearest
->n_lines
;
734 while ((high
- low
) > 1)
736 mid
= (high
+ low
) / 2;
737 if (addr
->off
< nearest
->linetab
[mid
].pc_offset
.off
)
742 lineno
= nearest
->linetab
[low
].line_number
;
747 snprintf(linebuff
, sizeof(linebuff
), ":%d", lineno
);
751 source
->line
= lineno
;
755 /* Remove the path from the file name */
756 sourcefile
= strrchr( nearest
->sourcefile
, '/' );
757 if (!sourcefile
) sourcefile
= nearest
->sourcefile
;
760 if (addr
->off
== nearest
->value
.addr
.off
)
761 snprintf( name_buffer
, sizeof(name_buffer
), "%s%s [%s%s]%s", nearest
->name
,
762 arglist
, sourcefile
, lineinfo
, modbuf
);
764 snprintf( name_buffer
, sizeof(name_buffer
), "%s+0x%lx%s [%s%s]%s", nearest
->name
,
765 addr
->off
- nearest
->value
.addr
.off
,
766 arglist
, sourcefile
, lineinfo
, modbuf
);
770 if (addr
->off
== nearest
->value
.addr
.off
)
771 snprintf( name_buffer
, sizeof(name_buffer
), "%s%s%s", nearest
->name
, arglist
, modbuf
);
773 if (addr
->seg
&& (nearest
->value
.addr
.seg
!=addr
->seg
))
776 snprintf( name_buffer
, sizeof(name_buffer
), "%s+0x%lx%s%s", nearest
->name
,
777 addr
->off
- nearest
->value
.addr
.off
, arglist
, modbuf
);
784 /***********************************************************************
785 * DEBUG_ReadSymbolTable
787 * Read a symbol file into the hash table.
789 void DEBUG_ReadSymbolTable( const char* filename
, unsigned long offset
)
798 if (!(symbolfile
= fopen(filename
, "r")))
800 DEBUG_Printf( DBG_CHN_WARN
, "Unable to open symbol table %s\n", filename
);
804 DEBUG_Printf( DBG_CHN_MESG
, "Reading symbols from file %s\n", filename
);
809 value
.cookie
= DV_TARGET
;
813 fgets( buffer
, sizeof(buffer
), symbolfile
);
814 if (feof(symbolfile
)) break;
816 /* Strip any text after a # sign (i.e. comments) */
819 if(*cpnt
++ == '#') { *cpnt
= 0; break; }
821 /* Quietly ignore any lines that have just whitespace */
825 if(*cpnt
!= ' ' && *cpnt
!= '\t') break;
828 if (!(*cpnt
) || *cpnt
== '\n') continue;
830 if (sscanf(buffer
, "%lx %c %s", &value
.addr
.off
, &type
, name
) == 3)
832 if (value
.addr
.off
+ offset
< value
.addr
.off
)
833 DEBUG_Printf( DBG_CHN_WARN
, "Address wrap around\n");
834 value
.addr
.off
+= offset
;
835 DEBUG_AddSymbol( name
, &value
, NULL
, SYM_WINE
);
843 DEBUG_AddLineNumber( struct name_hash
* func
, int line_num
,
844 unsigned long offset
)
851 if( func
->n_lines
+ 1 >= func
->lines_alloc
)
853 func
->lines_alloc
+= 64;
854 func
->linetab
= DBG_realloc(func
->linetab
,
855 func
->lines_alloc
* sizeof(WineLineNo
));
858 func
->linetab
[func
->n_lines
].line_number
= line_num
;
859 func
->linetab
[func
->n_lines
].pc_offset
.seg
= func
->value
.addr
.seg
;
860 func
->linetab
[func
->n_lines
].pc_offset
.off
= func
->value
.addr
.off
+ offset
;
866 DEBUG_AddLocal( struct name_hash
* func
, int regno
,
877 if( func
->n_locals
+ 1 >= func
->locals_alloc
)
879 func
->locals_alloc
+= 32;
880 func
->local_vars
= DBG_realloc(func
->local_vars
,
881 func
->locals_alloc
* sizeof(WineLocals
));
884 func
->local_vars
[func
->n_locals
].regno
= regno
;
885 func
->local_vars
[func
->n_locals
].offset
= offset
;
886 func
->local_vars
[func
->n_locals
].pc_start
= pc_start
;
887 func
->local_vars
[func
->n_locals
].pc_end
= pc_end
;
888 func
->local_vars
[func
->n_locals
].name
= DBG_strdup(name
);
889 func
->local_vars
[func
->n_locals
].type
= NULL
;
892 return &func
->local_vars
[func
->n_locals
- 1];
896 DEBUG_DumpHashInfo(void)
900 struct name_hash
*nh
;
903 * Utility function to dump stats about the hash table.
905 for(i
=0; i
<NR_NAME_HASH
; i
++)
908 for (nh
= name_hash_table
[i
]; nh
; nh
= nh
->next
)
912 DEBUG_Printf(DBG_CHN_MESG
, "Bucket %d: %d\n", i
, depth
);
916 /***********************************************************************
917 * DEBUG_CheckLinenoStatus
919 * Find the symbol nearest to a given address.
920 * If ebp is specified as non-zero, it means we should dump the argument
921 * list into the string we return as well.
923 int DEBUG_CheckLinenoStatus( const DBG_ADDR
*addr
)
925 struct name_hash
* nearest
= NULL
;
928 if( sortlist_valid
== FALSE
)
930 DEBUG_ResortSymbols();
934 * Binary search to find closest symbol.
938 if( addr_sorttab
[0]->value
.addr
.seg
> addr
->seg
939 || ( addr_sorttab
[0]->value
.addr
.seg
== addr
->seg
940 && addr_sorttab
[0]->value
.addr
.off
> addr
->off
) )
944 else if( addr_sorttab
[high
- 1]->value
.addr
.seg
< addr
->seg
945 || ( addr_sorttab
[high
- 1]->value
.addr
.seg
== addr
->seg
946 && addr_sorttab
[high
- 1]->value
.addr
.off
< addr
->off
) )
948 nearest
= addr_sorttab
[high
- 1];
954 mid
= (high
+ low
)/2;
958 * See if there are any other entries that might also
959 * have the same address, and would also have a line
962 if( mid
> 0 && addr_sorttab
[mid
]->linetab
== NULL
)
964 if( (addr_sorttab
[mid
- 1]->value
.addr
.seg
==
965 addr_sorttab
[mid
]->value
.addr
.seg
)
966 && (addr_sorttab
[mid
- 1]->value
.addr
.off
==
967 addr_sorttab
[mid
]->value
.addr
.off
)
968 && (addr_sorttab
[mid
- 1]->linetab
!= NULL
) )
974 if( (mid
< sorttab_nsym
- 1)
975 && (addr_sorttab
[mid
]->linetab
== NULL
) )
977 if( (addr_sorttab
[mid
+ 1]->value
.addr
.seg
==
978 addr_sorttab
[mid
]->value
.addr
.seg
)
979 && (addr_sorttab
[mid
+ 1]->value
.addr
.off
==
980 addr_sorttab
[mid
]->value
.addr
.off
)
981 && (addr_sorttab
[mid
+ 1]->linetab
!= NULL
) )
986 nearest
= addr_sorttab
[mid
];
988 DEBUG_Printf(DBG_CHN_MESG
, "Found %x:%x when looking for %x:%x %x %s\n",
989 addr_sorttab
[mid
]->value
.addr
.seg
,
990 addr_sorttab
[mid
]->value
.addr
.off
,
991 addr
->seg
, addr
->off
,
992 addr_sorttab
[mid
]->linetab
,
993 addr_sorttab
[mid
]->name
);
997 if( (addr_sorttab
[mid
]->value
.addr
.seg
< addr
->seg
)
998 || ( addr_sorttab
[mid
]->value
.addr
.seg
== addr
->seg
999 && addr_sorttab
[mid
]->value
.addr
.off
<= addr
->off
) )
1010 if (!nearest
) return FUNC_HAS_NO_LINES
;
1012 if( nearest
->flags
& SYM_STEP_THROUGH
)
1015 * This will cause us to keep single stepping until
1016 * we get to the other side somewhere.
1018 return NOT_ON_LINENUMBER
;
1021 if( (nearest
->flags
& SYM_TRAMPOLINE
) )
1024 * This will cause us to keep single stepping until
1025 * we get to the other side somewhere.
1027 return FUNC_IS_TRAMPOLINE
;
1030 if( nearest
->linetab
== NULL
)
1032 return FUNC_HAS_NO_LINES
;
1037 * We never want to stop on the first instruction of a function
1038 * even if it has it's own linenumber. Let the thing keep running
1039 * until it gets past the function prologue. We only do this if there
1040 * is more than one line number for the function, of course.
1042 if( nearest
->value
.addr
.off
== addr
->off
&& nearest
->n_lines
> 1 )
1044 return NOT_ON_LINENUMBER
;
1047 if( (nearest
->sourcefile
!= NULL
)
1048 && (addr
->off
- nearest
->value
.addr
.off
< 0x100000) )
1051 high
= nearest
->n_lines
;
1052 while ((high
- low
) > 1)
1054 mid
= (high
+ low
) / 2;
1055 if (addr
->off
< nearest
->linetab
[mid
].pc_offset
.off
) high
= mid
;
1058 if (addr
->off
== nearest
->linetab
[low
].pc_offset
.off
)
1059 return AT_LINENUMBER
;
1061 return NOT_ON_LINENUMBER
;
1064 return FUNC_HAS_NO_LINES
;
1067 /***********************************************************************
1070 * Find the symbol nearest to a given address.
1071 * Returns sourcefile name and line number in a format that the listing
1072 * handler can deal with.
1075 DEBUG_GetFuncInfo( struct list_id
* ret
, const char * filename
,
1080 struct name_hash
*nh
;
1082 for(nh
= name_hash_table
[name_hash(name
)]; nh
; nh
= nh
->next
)
1084 if( filename
!= NULL
)
1087 if( nh
->sourcefile
== NULL
)
1092 pnt
= strrchr(nh
->sourcefile
, '/');
1093 if( strcmp(nh
->sourcefile
, filename
) != 0
1094 && (pnt
== NULL
|| strcmp(pnt
+ 1, filename
) != 0) )
1099 if (!strcmp(nh
->name
, name
)) break;
1102 if (!nh
&& (name
[0] != '_'))
1105 strcpy(buffer
+1, name
);
1106 for(nh
= name_hash_table
[name_hash(buffer
)]; nh
; nh
= nh
->next
)
1108 if( filename
!= NULL
)
1110 if( nh
->sourcefile
== NULL
)
1115 pnt
= strrchr(nh
->sourcefile
, '/');
1116 if( strcmp(nh
->sourcefile
, filename
) != 0
1117 && (pnt
== NULL
|| strcmp(pnt
+ 1, filename
) != 0) )
1122 if (!strcmp(nh
->name
, buffer
)) break;
1128 if( filename
!= NULL
)
1130 DEBUG_Printf(DBG_CHN_MESG
, "No such function %s in %s\n", name
, filename
);
1134 DEBUG_Printf(DBG_CHN_MESG
, "No such function %s\n", name
);
1136 ret
->sourcefile
= NULL
;
1141 ret
->sourcefile
= nh
->sourcefile
;
1144 * Search for the specific line number. If we don't find it,
1145 * then return FALSE.
1147 if( nh
->linetab
== NULL
)
1153 ret
->line
= nh
->linetab
[0].line_number
;
1157 /***********************************************************************
1158 * DEBUG_GetStackSymbolValue
1160 * Get the address of a named symbol from the current stack frame.
1163 BOOL
DEBUG_GetStackSymbolValue( const char * name
, DBG_VALUE
*value
)
1165 struct name_hash
* curr_func
;
1170 if( DEBUG_GetCurrentFrame(&curr_func
, &eip
, &ebp
) == FALSE
)
1175 for(i
=0; i
< curr_func
->n_locals
; i
++ )
1178 * Test the range of validity of the local variable. This
1179 * comes up with RBRAC/LBRAC stabs in particular.
1181 if( (curr_func
->local_vars
[i
].pc_start
!= 0)
1182 && ((eip
- curr_func
->value
.addr
.off
)
1183 < curr_func
->local_vars
[i
].pc_start
) )
1188 if( (curr_func
->local_vars
[i
].pc_end
!= 0)
1189 && ((eip
- curr_func
->value
.addr
.off
)
1190 > curr_func
->local_vars
[i
].pc_end
) )
1195 if( strcmp(name
, curr_func
->local_vars
[i
].name
) == 0 )
1198 * OK, we found it. Now figure out what to do with this.
1200 if( curr_func
->local_vars
[i
].regno
!= 0 )
1203 * Register variable. Point to DEBUG_context field.
1205 assert(curr_func
->local_vars
[i
].regno
- 1 < sizeof(reg_ofs
)/sizeof(reg_ofs
[0]));
1206 value
->addr
.off
= ((DWORD
)&DEBUG_context
) +
1207 reg_ofs
[curr_func
->local_vars
[i
].regno
- 1];
1208 value
->cookie
= DV_HOST
;
1212 value
->addr
.off
= ebp
+ curr_func
->local_vars
[i
].offset
;
1213 value
->cookie
= DV_TARGET
;
1215 value
->addr
.seg
= 0;
1216 value
->type
= curr_func
->local_vars
[i
].type
;
1226 DEBUG_InfoLocals(void)
1228 struct name_hash
* curr_func
;
1235 if( DEBUG_GetCurrentFrame(&curr_func
, &eip
, &ebp
) == FALSE
)
1240 DEBUG_Printf(DBG_CHN_MESG
, "%s:\n", curr_func
->name
);
1242 for(i
=0; i
< curr_func
->n_locals
; i
++ )
1245 * Test the range of validity of the local variable. This
1246 * comes up with RBRAC/LBRAC stabs in particular.
1248 if( (curr_func
->local_vars
[i
].pc_start
!= 0)
1249 && ((eip
- curr_func
->value
.addr
.off
)
1250 < curr_func
->local_vars
[i
].pc_start
) )
1255 if( (curr_func
->local_vars
[i
].pc_end
!= 0)
1256 && ((eip
- curr_func
->value
.addr
.off
)
1257 > curr_func
->local_vars
[i
].pc_end
) )
1262 DEBUG_PrintTypeCast(curr_func
->local_vars
[i
].type
);
1264 if( curr_func
->local_vars
[i
].regno
!= 0 )
1266 ptr
= (unsigned int *)(((DWORD
)&DEBUG_context
)
1267 + reg_ofs
[curr_func
->local_vars
[i
].regno
- 1]);
1268 DEBUG_Printf(DBG_CHN_MESG
, " %s (optimized into register $%s) == 0x%8.8x\n",
1269 curr_func
->local_vars
[i
].name
,
1270 reg_name
[curr_func
->local_vars
[i
].regno
- 1],
1275 DEBUG_READ_MEM_VERBOSE((void*)(ebp
+ curr_func
->local_vars
[i
].offset
),
1277 DEBUG_Printf(DBG_CHN_MESG
, " %s == 0x%8.8x\n",
1278 curr_func
->local_vars
[i
].name
, val
);
1286 DEBUG_SetSymbolSize(struct name_hash
* sym
, unsigned int len
)
1288 sym
->symbol_size
= len
;
1294 DEBUG_SetSymbolBPOff(struct name_hash
* sym
, unsigned int off
)
1296 sym
->breakpoint_offset
= off
;
1302 DEBUG_GetSymbolAddr(struct name_hash
* sym
, DBG_ADDR
* addr
)
1305 *addr
= sym
->value
.addr
;
1310 int DEBUG_SetLocalSymbolType(struct wine_locals
* sym
, struct datatype
* type
)
1319 static int cmp_sym_by_name(const void * p1
, const void * p2
)
1321 struct name_hash
** name1
= (struct name_hash
**) p1
;
1322 struct name_hash
** name2
= (struct name_hash
**) p2
;
1324 return strcmp( (*name1
)->name
, (*name2
)->name
);
1329 void DEBUG_InfoSymbols(const char* str
)
1332 struct name_hash
* nh
;
1333 struct name_hash
** array
= NULL
;
1334 unsigned num_used_array
= 0;
1335 unsigned num_alloc_array
= 0;
1340 regcomp(&preg
, str
, REG_NOSUB
);
1342 /* grab all symbols */
1343 for (i
= 0; i
< NR_NAME_HASH
; i
++)
1345 for (nh
= name_hash_table
[i
]; nh
; nh
= nh
->next
)
1347 if (regexec(&preg
, nh
->name
, 0, NULL
, 0) == 0)
1349 if (num_used_array
== num_alloc_array
)
1351 array
= HeapReAlloc(GetProcessHeap(), 0, array
, sizeof(*array
) * (num_alloc_array
+= 32));
1354 array
[num_used_array
++] = nh
;
1360 /* now sort them by alphabetical order */
1361 qsort(array
, num_used_array
, sizeof(*array
), cmp_sym_by_name
);
1363 /* and display them */
1364 for (i
= 0; i
< num_used_array
; i
++)
1366 mode
= DEBUG_GetSelectorType(array
[i
]->value
.addr
.seg
);
1367 name
= DEBUG_FindNearestSymbol( &array
[i
]->value
.addr
, TRUE
,
1370 if (mode
!= MODE_32
)
1371 DEBUG_Printf( DBG_CHN_MESG
, "%04lx:%04lx :",
1372 array
[i
]->value
.addr
.seg
& 0xFFFF,
1373 array
[i
]->value
.addr
.off
);
1375 DEBUG_Printf( DBG_CHN_MESG
, "%08lx :", array
[i
]->value
.addr
.off
);
1376 if (array
[i
]->value
.type
)
1378 DEBUG_Printf( DBG_CHN_MESG
, " (");
1379 DEBUG_PrintTypeCast(array
[i
]->value
.type
);
1380 DEBUG_Printf( DBG_CHN_MESG
, ")");
1382 if (name
) DEBUG_Printf( DBG_CHN_MESG
, " %s\n", name
);
1384 HeapFree(GetProcessHeap(), 0, array
);
1387 #else /* HAVE_REGEX_H */
1389 void DEBUG_InfoSymbols(const char* str
)
1391 DEBUG_Printf( DBG_CHN_MESG
, "FIXME: needs regex support\n" );
1394 #endif /* HAVE_REGEX_H */