ReactOS  0.4.15-dev-1184-g23e04ae
find.c
Go to the documentation of this file.
1 /*
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
6  *
7  * PROGRAMMERS: Ge van Geldorp (gvg@reactos.com)
8  */
9 /*
10  * Parts of this file based on work Copyright (c) 1990, 1993
11  * The Regents of the University of California. All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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.
24  *
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
35  * SUCH DAMAGE.
36  */
37 
38 #include <ntdef.h>
39 #include <reactos/rossym.h>
40 #include "rossympriv.h"
41 
42 #define NDEBUG
43 #include <debug.h>
44 
45 static PROSSYM_ENTRY
46 FindEntry(IN PROSSYM_INFO RosSymInfo, IN ULONG_PTR RelativeAddress)
47 {
48  /*
49  * Perform a binary search.
50  *
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
62  * look at item 3.
63  */
64  PROSSYM_ENTRY Base = RosSymInfo->Symbols;
65  ULONG Lim;
66  PROSSYM_ENTRY Mid, Low;
67 
68  if (RelativeAddress < Base->Address)
69  {
70  return NULL;
71  }
72 
73  Low = Base;
74  for (Lim = RosSymInfo->SymbolsCount; Lim != 0; Lim >>= 1)
75  {
76  Mid = Base + (Lim >> 1);
77  if (RelativeAddress == Mid->Address)
78  {
79  return Mid;
80  }
81  if (Mid->Address < RelativeAddress) /* key > mid: move right */
82  {
83  Low = Mid;
84  Base = Mid + 1;
85  Lim--;
86  } /* else move left */
87  }
88 
89  return Low;
90 }
91 
92 
93 BOOLEAN
95  ULONG_PTR RelativeAddress,
97  char *FileName,
98  char *FunctionName)
99 {
100  PROSSYM_ENTRY RosSymEntry;
101 
102  DPRINT("RelativeAddress = 0x%08x\n", RelativeAddress);
103 
104  if (RosSymInfo->Symbols == NULL || RosSymInfo->SymbolsCount == 0 ||
105  RosSymInfo->Strings == NULL || RosSymInfo->StringsLength == 0)
106  {
107  DPRINT1("Uninitialized RosSymInfo\n");
108  return FALSE;
109  }
110 
112 
113  /* find symbol entry for function */
114  RosSymEntry = FindEntry(RosSymInfo, RelativeAddress);
115 
116  if (NULL == RosSymEntry)
117  {
118  DPRINT("None of the requested information was found!\n");
119  return FALSE;
120  }
121 
122  if (LineNumber != NULL)
123  {
124  *LineNumber = RosSymEntry->SourceLine;
125  }
126  if (FileName != NULL)
127  {
128  PCSTR Name = "";
129  if (RosSymEntry->FileOffset != 0)
130  {
131  Name = (PCHAR) RosSymInfo->Strings + RosSymEntry->FileOffset;
132  }
133  strcpy(FileName, Name);
134  }
135  if (FunctionName != NULL)
136  {
137  PCSTR Name = "";
138  if (RosSymEntry->FunctionOffset != 0)
139  {
140  Name = (PCHAR) RosSymInfo->Strings + RosSymEntry->FunctionOffset;
141  }
143  }
144 
145  return TRUE;
146 }
147 
148 /* EOF */
#define IN
Definition: typedefs.h:39
ULONG SourceLine
Definition: rossym.h:30
#define TRUE
Definition: types.h:120
_In_opt_ ULONG Base
Definition: rtlfuncs.h:2373
Definition: strmini.h:380
Definition: rossym.h:26
uint32_t ULONG_PTR
Definition: typedefs.h:65
ULONG FileOffset
Definition: rossym.h:29
#define FALSE
Definition: types.h:117
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
void DPRINT(...)
Definition: polytest.cpp:61
PCHAR Strings
Definition: rossympriv.h:15
#define PCHAR
Definition: match.c:90
static PROSSYM_ENTRY FindEntry(IN PROSSYM_INFO RosSymInfo, IN ULONG_PTR RelativeAddress)
Definition: find.c:46
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
ULONG StringsLength
Definition: rossympriv.h:16
ACPI_BUFFER *RetBuffer ACPI_BUFFER *RetBuffer char ACPI_WALK_RESOURCE_CALLBACK void *Context ACPI_BUFFER *RetBuffer UINT16 ACPI_RESOURCE **ResourcePtr ACPI_GENERIC_ADDRESS *Reg UINT32 *ReturnValue UINT8 UINT8 *Slp_TypB ACPI_PHYSICAL_ADDRESS PhysicalAddress64 UINT32 UINT32 *TimeElapsed UINT32 LineNumber
Definition: acpixf.h:1216
BOOLEAN RosSymGetAddressInformation(PROSSYM_INFO RosSymInfo, ULONG_PTR RelativeAddress, ULONG *LineNumber, char *FileName, char *FunctionName)
Definition: find.c:94
#define DPRINT1
Definition: precomp.h:8
ACPI_BUFFER *RetBuffer ACPI_BUFFER *RetBuffer char ACPI_WALK_RESOURCE_CALLBACK void *Context ACPI_BUFFER *RetBuffer UINT16 ACPI_RESOURCE **ResourcePtr ACPI_GENERIC_ADDRESS *Reg UINT32 *ReturnValue UINT8 UINT8 *Slp_TypB ACPI_PHYSICAL_ADDRESS PhysicalAddress64 UINT32 UINT32 *TimeElapsed UINT32 ACPI_STATUS const char UINT32 ACPI_STATUS const char UINT32 const char * FunctionName
Definition: acpixf.h:1274
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
unsigned int ULONG
Definition: retypes.h:1
ULONG SymbolsCount
Definition: rossympriv.h:14
const char * PCSTR
Definition: typedefs.h:52
ULONG FunctionOffset
Definition: rossym.h:28
PROSSYM_ENTRY Symbols
Definition: rossympriv.h:13
ULONG_PTR Address
Definition: rossym.h:27