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
40 #include <reactos/rossym.h>
41 #include "rossympriv.h"
47 FindEntry(IN PROSSYM_INFO RosSymInfo
, IN ULONG_PTR RelativeAddress
)
50 * Perform a binary search.
52 * The code below is a bit sneaky. After a comparison fails, we
53 * divide the work in half by moving either left or right. If lim
54 * is odd, moving left simply involves halving lim: e.g., when lim
55 * is 5 we look at item 2, so we change lim to 2 so that we will
56 * look at items 0 & 1. If lim is even, the same applies. If lim
57 * is odd, moving right again involes halving lim, this time moving
58 * the base up one item past p: e.g., when lim is 5 we change base
59 * to item 3 and make lim 2 so that we will look at items 3 and 4.
60 * If lim is even, however, we have to shrink it by one before
61 * halving: e.g., when lim is 4, we still looked at item 2, so we
62 * have to make lim 3, then halve, obtaining 1, so that we will only
65 PROSSYM_ENTRY Base
= RosSymInfo
->Symbols
;
67 PROSSYM_ENTRY Mid
, Low
;
69 if (RelativeAddress
< Base
->Address
)
75 for (Lim
= RosSymInfo
->SymbolsCount
; Lim
!= 0; Lim
>>= 1)
77 Mid
= Base
+ (Lim
>> 1);
78 if (RelativeAddress
== Mid
->Address
)
82 if (Mid
->Address
< RelativeAddress
) /* key > mid: move right */
87 } /* else move left */
95 RosSymGetAddressInformation(PROSSYM_INFO RosSymInfo
,
96 ULONG_PTR RelativeAddress
,
101 PROSSYM_ENTRY RosSymEntry
;
103 DPRINT("RelativeAddress = 0x%08x\n", RelativeAddress
);
105 if (RosSymInfo
->Symbols
== NULL
|| RosSymInfo
->SymbolsCount
== 0 ||
106 RosSymInfo
->Strings
== NULL
|| RosSymInfo
->StringsLength
== 0)
109 DPRINT1("Uninitialized RosSymInfo\n");
113 ASSERT(LineNumber
|| FileName
|| FunctionName
);
115 /* find symbol entry for function */
116 RosSymEntry
= FindEntry(RosSymInfo
, RelativeAddress
);
118 if (NULL
== RosSymEntry
)
120 DPRINT("None of the requested information was found!\n");
124 if (LineNumber
!= NULL
)
126 *LineNumber
= RosSymEntry
->SourceLine
;
128 if (FileName
!= NULL
)
131 if (RosSymEntry
->FileOffset
!= 0)
133 Name
= (PCHAR
) RosSymInfo
->Strings
+ RosSymEntry
->FileOffset
;
135 strcpy(FileName
, Name
);
137 if (FunctionName
!= NULL
)
140 if (RosSymEntry
->FunctionOffset
!= 0)
142 Name
= (PCHAR
) RosSymInfo
->Strings
+ RosSymEntry
->FunctionOffset
;
144 strcpy(FunctionName
, Name
);