BDE 4.14.0 Production release
Loading...
Searching...
No Matches
balst_resolver_filehelper.h
Go to the documentation of this file.
1/// @file balst_resolver_filehelper.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// balst_resolver_filehelper.h -*-C++-*-
8#ifndef INCLUDED_BALST_RESOLVER_FILEHELPER
9#define INCLUDED_BALST_RESOLVER_FILEHELPER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup balst_resolver_filehelper balst_resolver_filehelper
15/// @brief Provide platform-independent file input for stack trace resolvers.
16/// @addtogroup bal
17/// @{
18/// @addtogroup balst
19/// @{
20/// @addtogroup balst_resolver_filehelper
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#balst_resolver_filehelper-purpose"> Purpose</a>
25/// * <a href="#balst_resolver_filehelper-classes"> Classes </a>
26/// * <a href="#balst_resolver_filehelper-description"> Description </a>
27/// * <a href="#balst_resolver_filehelper-usage"> Usage </a>
28/// * <a href="#balst_resolver_filehelper-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#balst_resolver_filehelper-purpose}
31/// Provide platform-independent file input for stack trace resolvers.
32///
33/// # Classes {#balst_resolver_filehelper-classes}
34///
35/// - balst::Resolver_FileHelper: file input for stack trace resolvers
36///
37/// @see balst_resolverimpl_elf, balst_resolverimpl_xcoff
38///
39/// # Description {#balst_resolver_filehelper-description}
40/// The one class in this component opens a file in readonly mode
41/// and then owns the file descriptor, and provides 3 utility functions for
42/// reading from the file: `readBytes`, which attempts to read a number of bytes
43/// into a buffer, and does a partial read if it can't read that many;
44/// `readExact`, which either reads an exact number of bytes or fails, and
45/// `loadString`, which reads a 0 terminated string from the file, copies it to
46/// a buffer it allocates, and returns a pointer to the copy.
47///
48/// ## Usage {#balst_resolver_filehelper-usage}
49///
50///
51/// This section illustrates intended use of this component.
52///
53/// ### Example 1: Basic Usage {#balst_resolver_filehelper-example-1-basic-usage}
54///
55///
56/// First, we prepare the file to be used by this usage example:
57/// @code
58/// bslma::TestAllocator ta;
59///
60/// char fileNameBuffer[100];
61/// sprintf(fileNameBuffer,
62/// "/tmp/balst_Resolver_FileHelper.usage.%d.txt",
63/// getProcessId());
64/// @endcode
65/// Make sure file does not already exist.
66/// @code
67/// bdls::FilesystemUtil::remove(fileNameBuffer);
68/// @endcode
69/// Next, create the file and open a file descriptor to it. The boolean
70/// flags indicate that the file is writable, and not previously existing
71/// (and therefore must be created).
72/// @code
73/// FdType fd = FilesystemUtil::open(fileNameBuffer,
74/// true, // writable
75/// false); // doesn't already exist
76/// assert(FilesystemUtil::k_INVALID_FD != fd);
77/// @endcode
78/// 64 char long string
79/// @code
80/// const char *testString64 =
81/// "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
82/// "0123456789+-";
83/// @endcode
84/// Populate the file with known data, with a zero byte at a known offset.
85/// @code
86/// int rc;
87/// for (int i = 0; i < 20; ++i) {
88/// rc = FilesystemUtil::write(fd, testString64, 64);
89/// assert(64 == rc);
90/// }
91///
92/// enum { OFFSET_OF_ZERO_BYTE = 7 * 64 };
93///
94/// rc = (int) FilesystemUtil::seek(fd,
95/// OFFSET_OF_ZERO_BYTE,
96/// FilesystemUtil::e_SEEK_FROM_BEGINNING);
97/// assert(OFFSET_OF_ZERO_BYTE == rc);
98///
99/// rc = FilesystemUtil::write(fd, "", 1); // write the zero byte
100/// assert(1 == rc);
101///
102/// rc = FilesystemUtil::close(fd);
103/// assert(0 == rc);
104///
105/// {
106/// balst::Resolver_FileHelper helper(fileNameBuffer);
107///
108/// char buf[100];
109/// memset(buf, 0, sizeof(buf));
110/// rc = helper.readExact(buf,
111/// 6, // # chars to read
112/// 128); // offset
113/// assert(0 == rc);
114/// assert(!strcmp(buf, "abcdef"));
115/// @endcode
116/// `readExact` past EOF fails
117/// @code
118/// rc = helper.readExact(buf,
119/// 6, // # chars to read
120/// 64 * 40); // offset
121/// assert(0 != rc);
122/// @endcode
123/// `loadString` will read a zero terminated string at a given offset,
124/// using a buffer passed in, and allocating memory for a new copy of
125/// the string.
126/// @code
127/// memset(buf, 'a', sizeof(buf));
128/// char *result = helper.loadString(OFFSET_OF_ZERO_BYTE - 12,
129/// buf,
130/// sizeof(buf),
131/// &ta);
132///
133/// assert(12 == bsl::strlen(result));
134/// assert(!bsl::strcmp("0123456789+-", result));
135/// @endcode
136/// clean up
137/// @code
138/// ta.deallocate(result);
139/// }
140/// bdls::FilesystemUtil::remove(fileNameBuffer);
141/// @endcode
142/// @}
143/** @} */
144/** @} */
145
146/** @addtogroup bal
147 * @{
148 */
149/** @addtogroup balst
150 * @{
151 */
152/** @addtogroup balst_resolver_filehelper
153 * @{
154 */
155
156#include <balscm_version.h>
157
159
160#if defined(BALST_OBJECTFILEFORMAT_RESOLVER_ELF) || \
161 defined(BALST_OBJECTFILEFORMAT_RESOLVER_XCOFF)
162#include <bdls_filesystemutil.h>
163
164#include <bslma_allocator.h>
165
166#include <bsls_assert.h>
167#include <bsls_review.h>
168#include <bsls_types.h>
169
170
171
172namespace balst {
173 // =========================
174 // class Resolver_FileHelper
175 // =========================
176
177/// This class provides a low-level file utility functions for
178/// `Resolver<Elf>` and `Resolver<Xcoff>`. This class contains the file
179/// descriptor of current object file. Note that the file is opened
180/// readonly, and all reads specify the offset, so the offset of the file
181/// descriptor is not considered part of the state of this object.
182///
183/// See @ref balst_resolver_filehelper
184class Resolver_FileHelper {
185
186 // PRIVATE TYPES
187 typedef bdls::FilesystemUtil FilesystemUtil;
188 typedef FilesystemUtil::Offset Offset;
189 typedef bsls::Types::UintPtr UintPtr;
190 typedef bsls::Types::IntPtr IntPtr;
191
192 // DATA
193 FilesystemUtil::FileDescriptor d_fd; // file descriptor
194
195 private:
196 // NOT IMPLEMENTED
197 Resolver_FileHelper(const Resolver_FileHelper&);
198 Resolver_FileHelper& operator=(
199 const Resolver_FileHelper&);
200 public:
201 // CREATORS
202
203 /// Create a file helper object in an invalid state.
204 explicit
205 Resolver_FileHelper();
206
207 /// Close any file currently opened by this object and destroy it.
208 ~Resolver_FileHelper();
209
210 // MANIPULATOR
211
212 /// Open the file referred to by the specified `fileName` for read-only
213 /// access and set `d_fd` to the file descriptor. Return 0 on success
214 /// and a non-zero value otherwise. If this object already had a file
215 /// descriptor open, close it before opening `fileName`. If the `open`
216 /// call fails, `d_fd` will be set to `FilesystemUtil::k_INVALID_FD`.
217 int initialize(const char *fileName);
218
219 // ACCESSORS
220
221 /// Load into memory newly allocated from the specified `basicAllocator`
222 /// a zero-terminated string from the specified absolute file `offset`,
223 /// using the specified `scratchBuf` of specified length
224 /// `scratchBufLength` as temporary storage before the new string is
225 /// allocated. Return the pointer to the newly allocated string. The
226 /// behavior is undefined unless `scratchBuf != 0` and
227 /// `scratchBufLength >= 1`. Note that if the string is longer than
228 /// `scratchBufLength - 1`, it is truncated.
229 char *loadString(Offset offset,
230 char *scratchBuf,
231 UintPtr scratchBufLength,
232 bslma::Allocator *basicAllocator) const;
233
234 /// Read into the specified `buf` up to the specified `numBytes` of data
235 /// starting at the specified `offset` in the current ELF or XCOFF file.
236 /// Return the number of bytes read, which can be zero. The behavior is
237 /// undefined unless `buf != 0` and `offset >= 0`.
238 UintPtr readBytes(void *buf, UintPtr numBytes, Offset offset) const;
239
240 /// Read into the specified `buf` exactly the specified `numBytes` of
241 /// data starting at the specified `offset` in the current ELF or XCOFF
242 /// file. Return 0 on success, or a negative value otherwise. The
243 /// behavior is undefined unless `buf != 0` and `offset >= 0`.
244 int readExact(void *buf, UintPtr numBytes, Offset offset) const;
245};
246
247// ============================================================================
248// INLINE FUNCTION DEFINITIONS
249// ============================================================================
250
251 // -----------------------------------
252 // Resolver_FileHelper
253 // -----------------------------------
254
255// ACCESSORS
256inline
257int Resolver_FileHelper::readExact(void *buf,
258 UintPtr numBytes,
259 Offset offset) const
260{
261 BSLS_ASSERT(buf);
262 BSLS_ASSERT(offset >= 0);
263
264 UintPtr res = readBytes(buf, numBytes, offset);
265 return res != numBytes ? -1 : 0;
266}
267
268} // close package namespace
269
270
271#endif
272
273#endif
274
275// ----------------------------------------------------------------------------
276// Copyright 2018 Bloomberg Finance L.P.
277//
278// Licensed under the Apache License, Version 2.0 (the "License");
279// you may not use this file except in compliance with the License.
280// You may obtain a copy of the License at
281//
282// http://www.apache.org/licenses/LICENSE-2.0
283//
284// Unless required by applicable law or agreed to in writing, software
285// distributed under the License is distributed on an "AS IS" BASIS,
286// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
287// See the License for the specific language governing permissions and
288// limitations under the License.
289// ----------------------------- END-OF-FILE ----------------------------------
290
291/** @} */
292/** @} */
293/** @} */
Definition bslma_allocator.h:457
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balst_objectfileformat.h:161
Definition bdls_filesystemutil.h:362
std::size_t UintPtr
Definition bsls_types.h:126
std::ptrdiff_t IntPtr
Definition bsls_types.h:130