2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: lib/rossym/find.c
5 * PURPOSE: Find symbol info for an address
7 * PROGRAMMERS: Ge van Geldorp (gvg@reactos.com)
10 * Parts of this file based on work Copyright (c) 1990, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <reactos/rossym.h>
40 #include "rossympriv.h"
46 FindEntry(IN PROSSYM_INFO RosSymInfo
, IN ULONG_PTR RelativeAddress
)
49 * Perform a binary search.
51 * The code below is a bit sneaky. After a comparison fails, we
52 * divide the work in half by moving either left or right. If lim
53 * is odd, moving left simply involves halving lim: e.g., when lim
54 * is 5 we look at item 2, so we change lim to 2 so that we will
55 * look at items 0 & 1. If lim is even, the same applies. If lim
56 * is odd, moving right again involes halving lim, this time moving
57 * the base up one item past p: e.g., when lim is 5 we change base
58 * to item 3 and make lim 2 so that we will look at items 3 and 4.
59 * If lim is even, however, we have to shrink it by one before
60 * halving: e.g., when lim is 4, we still looked at item 2, so we
61 * have to make lim 3, then halve, obtaining 1, so that we will only
64 PROSSYM_ENTRY Base
= RosSymInfo
->Symbols
;
66 PROSSYM_ENTRY Mid
, Low
;
68 if (RelativeAddress
< Base
->Address
)
74 for (Lim
= RosSymInfo
->SymbolsCount
; Lim
!= 0; Lim
>>= 1)
76 Mid
= Base
+ (Lim
>> 1);
77 if (RelativeAddress
== Mid
->Address
)
81 if (Mid
->Address
< RelativeAddress
) /* key > mid: move right */
86 } /* else move left */
94 RosSymGetAddressInformation(PROSSYM_INFO RosSymInfo
,
95 ULONG_PTR RelativeAddress
,
100 PROSSYM_ENTRY RosSymEntry
;
102 DPRINT("RelativeAddress = 0x%08x\n", RelativeAddress
);
104 if (RosSymInfo
->Symbols
== NULL
|| RosSymInfo
->SymbolsCount
== 0 ||
105 RosSymInfo
->Strings
== NULL
|| RosSymInfo
->StringsLength
== 0)
107 DPRINT1("Uninitialized RosSymInfo\n");
111 ASSERT(LineNumber
|| FileName
|| FunctionName
);
113 /* find symbol entry for function */
114 RosSymEntry
= FindEntry(RosSymInfo
, RelativeAddress
);
116 if (NULL
== RosSymEntry
)
118 DPRINT("None of the requested information was found!\n");
122 if (LineNumber
!= NULL
)
124 *LineNumber
= RosSymEntry
->SourceLine
;
126 if (FileName
!= NULL
)
129 if (RosSymEntry
->FileOffset
!= 0)
131 Name
= (PCHAR
) RosSymInfo
->Strings
+ RosSymEntry
->FileOffset
;
133 strcpy(FileName
, Name
);
135 if (FunctionName
!= NULL
)
138 if (RosSymEntry
->FunctionOffset
!= 0)
140 Name
= (PCHAR
) RosSymInfo
->Strings
+ RosSymEntry
->FunctionOffset
;
142 strcpy(FunctionName
, Name
);