BDE 4.14.0 Production release
Loading...
Searching...
No Matches
balst_resolverimpl_elf.h
Go to the documentation of this file.
1/// @file balst_resolverimpl_elf.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// balst_resolverimpl_elf.h -*-C++-*-
8#ifndef INCLUDED_BALST_RESOLVERIMPL_ELF
9#define INCLUDED_BALST_RESOLVERIMPL_ELF
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup balst_resolverimpl_elf balst_resolverimpl_elf
15/// @brief Provide a utility to resolve ELF symbols in a stack trace.
16/// @addtogroup bal
17/// @{
18/// @addtogroup balst
19/// @{
20/// @addtogroup balst_resolverimpl_elf
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#balst_resolverimpl_elf-purpose"> Purpose</a>
25/// * <a href="#balst_resolverimpl_elf-classes"> Classes </a>
26/// * <a href="#balst_resolverimpl_elf-description"> Description </a>
27/// * <a href="#balst_resolverimpl_elf-usage"> Usage </a>
28///
29/// # Purpose {#balst_resolverimpl_elf-purpose}
30/// Provide a utility to resolve ELF symbols in a stack trace.
31///
32/// # Classes {#balst_resolverimpl_elf-classes}
33///
34/// - balst::ResolverImpl<Elf>: symbol resolution for ELF objects
35///
36/// @see balst_resolver_dwarfreader,
37/// balst_resolverimpl_windows,
38/// balst_resolverimpl_xcoff
39///
40/// # Description {#balst_resolverimpl_elf-description}
41/// This component provides a class,
42/// `balst::Resolver<Elf>`, that, given a vector of
43/// `balst::StackTraceFrame`s that have only their `address` fields set,
44/// resolves all other fields in those frames. The Elf object file format is
45/// used on Linux and Solaris platforms. The Elf format is described by
46/// documents at:
47/// * `http://en.wikipedia.org/wiki/Executable_and_Linkable_Format`
48/// * `ftp://ftp.openwatcom.org/pub/devel/docs/elf-64-gen.pdf`
49/// * `http://www.sco.com/developers/gabi/latest/contents.html`
50///
51/// ## Usage {#balst_resolverimpl_elf-usage}
52///
53///
54/// This component is an implementation detail of `balst` and is *not* intended
55/// for direct client use. It is subject to change without notice. As such, a
56/// usage example is not provided.
57/// @}
58/** @} */
59/** @} */
60
61/** @addtogroup bal
62 * @{
63 */
64/** @addtogroup balst
65 * @{
66 */
67/** @addtogroup balst_resolverimpl_elf
68 * @{
69 */
70
71#include <balscm_version.h>
72
74
75#if defined(BALST_OBJECTFILEFORMAT_RESOLVER_ELF)
76#include <balst_stacktrace.h>
79
81
82#include <bdls_filesystemutil.h>
83
84#include <bsls_types.h>
85
86#include <bsl_vector.h>
87
88
89namespace balst {
90
91template <class RESOLVER_POLICY>
92class ResolverImpl;
93
94 // =========================================
95 // class ResolverImpl<ObjectFileFormat::Elf>
96 // =========================================
97
98/// This class provides a public static `resolve` method that, given a
99/// vector of `StackTraceFrame`s that have only their `address` fields set,
100/// resolves as many other fields in those frames as possible. The Elf
101/// object file format is used on Linux and Solaris platforms. On Linux,
102/// some Elf sections contain data in the DWARF format, which makes it
103/// possible to resolve line numbers and file names.
104template <>
105class ResolverImpl<ObjectFileFormat::Elf> {
106
107 // TYPES
108 typedef bsls::Types::UintPtr UintPtr; // 32 bit unsigned on 32 bit, 64
109 // bit unsigned on 64 bit.
110 typedef bsls::Types::IntPtr IntPtr; // 32 bit signed on 32 bit, 64
111 // bit signed on 64 bit.
112
113 typedef bdls::FilesystemUtil::Offset
114 Offset; // Usually used for relative
115 // offsets into a file.
116
117 struct HiddenRec; // 'struct' defined locally in
118 // in the imp file containing
119 // additional information
120
121 // DATA
123 d_hbpAlloc; // heap bypass allocator -- owned
124
125 StackTrace *d_stackTrace_p; // pointer to stack trace object.
126 // The frames contained in this
127 // have their 'address' fields and
128 // nothing else initialized upon
129 // entry to 'resolve', which infers
130 // as many other fields of them as
131 // possible.
132
133 char *d_scratchBufA_p; // scratch buffer A
134
135 char *d_scratchBufB_p; // scratch buffer B
136
137 char *d_scratchBufC_p; // scratch buffer C
138
139 char *d_scratchBufD_p; // scratch buffer D
140
141 char *d_scratchBufE_p; // scratch buffer E
142
143 HiddenRec& d_hidden; // reference to the 'HiddenRec'.
144
145 bool d_demangle; // whether we demangle names
146
147 private:
148 // NOT IMPLEMENTED
149 ResolverImpl(const ResolverImpl&);
150 ResolverImpl& operator=(const ResolverImpl&);
151
152 private:
153 // PRIVATE CREATORS
154
155 /// Create an stack trace reolver that can populate other fields of the
156 /// specified `*stackTrace` object given previously populated `address`
157 /// fields. Specify `demangle`, which indicates whether demangling of
158 /// symbols is to occur.
159 ResolverImpl(StackTrace *stackTrace,
160 bool demanglingPreferredFlag);
161
162 /// Destroy this object.
163 ~ResolverImpl() = default;
164
165 // PRIVATE MANIPULATORS
166
167 /// Read the symbols from the symbol table of the current segment and
168 /// update the `mangledSymbolName`, `symbolName`, `offsetFromSymbol`,
169 /// and sometimes the `SourceFileName` fields of stack frames constain
170 /// addresses within the code section of the current segment, where the
171 /// specified `matched` is the number of addresses in the current
172 /// segment. Return 0 on success and a non-zero value otherwise.
173 int loadSymbols(int matched);
174
175 /// Identify which stack trace frames in `*d_stackTrace_p` are in the
176 /// segment pointed at by the specified `segmentPtr` of the specified
177 /// `segmentSize`, and initialize as many fields of those stack trace
178 /// frames as possible. The segment is defined in the executable file
179 /// or shared library `libraryFileName`. Return 0 on success and a
180 /// non-zero value otherwise.
181 int resolveSegment(void *segmentBaseAddress,
182 void *segmentPtr,
183 UintPtr segmentSize,
184 const char *libraryFileName);
185
186 // PRIVATE ACCESSORS
187
188 /// Set the `symbolName` field of the specified `frame`, which must
189 /// already have the `mangledSymbolName` field set, to the demangled
190 /// version of the `mangledSymbolName` field. Use the specified
191 /// `buffer` of specified length `bufferLen` for temporary storage. If
192 /// `d_demangle` is `false` or we are otherwise unable to demangle, just
193 /// set it to the same as `mangledSymbolName`.
194 void setFrameSymbolName(StackTraceFrame *frame,
195 char *buffer,
196 bsl::size_t bufferLen) const;
197
198 public:
199 // CLASS METHOD
200
201 /// Populate information for the specified `*stackTrace`, which contains
202 /// a sequence of randomly-accessible stack trace frames. Specify
203 /// `demanglingPreferredFlag`, to determine whether demangling is to
204 /// occur. The behavior is undefined unless all the `address` field in
205 /// `*stackTrace` are valid and other fields are invalid.
206 static int resolve(StackTrace *stackTrace,
207 bool demanglingPreferredFlag);
208
209 /// This function is just there to test how code deals with inline
210 /// functions in an include file. It does not provide any otherwise
211 /// useful functionality. Return a line number near the beginning of
212 /// the function in the low-order 14 bits of the result. Other bits of
213 /// the result are to be considered garbage.
214 static int test();
215
216 // MANIPULATOR
217
218 /// Process a loaded image found via the link map, either the main
219 /// program or some shared library. The specified `fileName` is the
220 /// name of the file containing the image. If `fileName == 0`, the file
221 /// is the main program. The specified `programHeaders` is a pointer to
222 /// an array of elf program headers and the specified
223 /// `numProgramHeaders` is its length, it is a `void *` because the type
224 /// `ElfProgramHeader` is local to the implementation file. Specify one
225 /// of `textSegPtr` and `baseAddress`, and the other as 0, this method
226 /// will infer the one specified as 0 from the other. Return 0 on
227 /// success and a non-zero value otherwise. Note that this method is
228 /// not to be called by external users of this component, it is only
229 /// public so a static routine in the implementation file can call it.
230 int processLoadedImage(const char *fileName,
231 const void *programHeaders,
232 int numProgramHeaders,
233 void *textSegPtr,
234 void *baseAddress);
235
236 // ACCESSOR
237
238 /// Return the number of frames in the stack trace that are still
239 /// unmatched.
240 int numUnmatchedFrames() const;
241};
242
243inline
244int ResolverImpl<ObjectFileFormat::Elf>::test()
245{
246
247 StackTrace st;
248
249 int ret = __LINE__;
250 ResolverImpl<ObjectFileFormat::Elf> resolver(&st, true);
251
252 return (resolver.numUnmatchedFrames() << 14) | ret;
253}
254
255} // close package namespace
256
257
258#endif
259#endif
260
261// ----------------------------------------------------------------------------
262// Copyright 2015 Bloomberg Finance L.P.
263//
264// Licensed under the Apache License, Version 2.0 (the "License");
265// you may not use this file except in compliance with the License.
266// You may obtain a copy of the License at
267//
268// http://www.apache.org/licenses/LICENSE-2.0
269//
270// Unless required by applicable law or agreed to in writing, software
271// distributed under the License is distributed on an "AS IS" BASIS,
272// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
273// See the License for the specific language governing permissions and
274// limitations under the License.
275// ----------------------------- END-OF-FILE ----------------------------------
276
277/** @} */
278/** @} */
279/** @} */
Definition bdlma_heapbypassallocator.h:157
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balst_objectfileformat.h:161
std::size_t UintPtr
Definition bsls_types.h:126
std::ptrdiff_t IntPtr
Definition bsls_types.h:130