Skip to content

Commit 1dc244b

Browse files
authored
Various fixes and enhancements for C++ libtrace (#14)
* Fix string appending in C++ libtrace * Fix TraceContainerReader for version 2 (metaframes) * Replace usage of deprecated std::auto_ptr by std::unique_ptr * Replace custom auto_vec by just std::vector * Remove C++ dynamic exception specifications They are deprecated in C++11 and removed in C++17. See also C++ Core Guidelines E.30.
1 parent 97bad46 commit 1dc244b

File tree

3 files changed

+68
-66
lines changed

3 files changed

+68
-66
lines changed

libtrace/src/readtrace.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ void print_all(const char *f) {
1818

1919
TraceContainerReader t(f);
2020

21+
std::cout << t.get_meta()->DebugString() << std::endl;
22+
2123
while (!t.end_of_trace()) {
2224
ctr++;
2325
print(*(t.get_frame()));

libtrace/src/trace.container.cpp

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
#ifdef _WIN32
1313
typedef uint64_t traceoff_t;
1414
#define SEEKNAME _fseeki64
15-
#define SEEK(f,x) { if (SEEKNAME(f, x, SEEK_SET) != 0) { throw (TraceException("Unable to seek in trace to offset " + x)); } }
15+
#define SEEK(f,x) { if (SEEKNAME(f, x, SEEK_SET) != 0) { throw (TraceException("Unable to seek in trace to offset " + std::to_string(x))); } }
1616
#define TELL(f) _ftelli64(f)
1717
#else
1818
typedef off_t traceoff_t;
1919
#define SEEKNAME fseeko
20-
#define SEEK(f,x) { if (SEEKNAME(f, x, SEEK_SET) != 0) { throw (TraceException("Unable to seek in trace to offset " + x)); } }
20+
#define SEEK(f,x) { if (SEEKNAME(f, x, SEEK_SET) != 0) { throw (TraceException("Unable to seek in trace to offset " + std::to_string(x))); } }
2121
#define TELL(f) ftello(f)
2222
#endif
2323

@@ -46,7 +46,6 @@ namespace SerializedTrace {
4646
frame_architecture arch,
4747
uint64_t machine,
4848
uint64_t frames_per_toc_entry_in)
49-
throw (TraceException)
5049
: num_frames (0)
5150
, frames_per_toc_entry (frames_per_toc_entry_in)
5251
, ofs(open_trace(filename,arch,machine,1LL)) {}
@@ -56,7 +55,6 @@ namespace SerializedTrace {
5655
frame_architecture arch,
5756
uint64_t machine,
5857
uint64_t frames_per_toc_entry_in)
59-
throw (TraceException)
6058
: num_frames (0)
6159
, frames_per_toc_entry (frames_per_toc_entry_in)
6260
, ofs(open_trace(filename,arch,machine,2LL)) {
@@ -72,7 +70,7 @@ namespace SerializedTrace {
7270
}
7371
}
7472

75-
void TraceContainerWriter::add(const frame &f) throw (TraceException) {
73+
void TraceContainerWriter::add(const frame &f) {
7674
if (num_frames > 0 && (num_frames % frames_per_toc_entry) == 0) {
7775
toc.push_back(TELL(ofs));
7876
}
@@ -111,7 +109,7 @@ namespace SerializedTrace {
111109
ofs = NULL;
112110
}
113111

114-
TraceContainerReader::TraceContainerReader(std::string filename) throw (TraceException)
112+
TraceContainerReader::TraceContainerReader(std::string filename)
115113
{
116114
ifs = fopen(filename.c_str(), "rb");
117115
if (!ifs) { throw (TraceException("Unable to open trace for reading")); }
@@ -142,6 +140,18 @@ namespace SerializedTrace {
142140
uint64_t toc_offset;
143141
READ(toc_offset);
144142

143+
uint64_t meta_size;
144+
READ(meta_size);
145+
first_frame_offset = meta_offset + meta_size;
146+
147+
std::vector<uint8_t> meta_buf(meta_size);
148+
if (fread(meta_buf.data(), 1, meta_buf.size(), ifs) != meta_buf.size()) {
149+
throw (TraceException("Unable to read meta frame"));
150+
}
151+
if (!meta.ParseFromArray(meta_buf.data(), meta_buf.size())) {
152+
throw (TraceException("Unable to parse meta frame"));
153+
}
154+
145155
/* Find the toc. */
146156
SEEK(ifs, toc_offset);
147157

@@ -166,31 +176,31 @@ namespace SerializedTrace {
166176
seek(0);
167177
}
168178

169-
TraceContainerReader::~TraceContainerReader(void) throw () {
179+
TraceContainerReader::~TraceContainerReader(void) noexcept {
170180
/* Nothing yet. */
171181
}
172182

173-
uint64_t TraceContainerReader::get_num_frames(void) throw () {
183+
uint64_t TraceContainerReader::get_num_frames(void) noexcept {
174184
return num_frames;
175185
}
176186

177-
uint64_t TraceContainerReader::get_frames_per_toc_entry(void) throw () {
187+
uint64_t TraceContainerReader::get_frames_per_toc_entry(void) noexcept {
178188
return frames_per_toc_entry;
179189
}
180190

181-
frame_architecture TraceContainerReader::get_arch(void) throw () {
191+
frame_architecture TraceContainerReader::get_arch(void) noexcept {
182192
return arch;
183193
}
184194

185-
uint64_t TraceContainerReader::get_machine(void) throw () {
195+
uint64_t TraceContainerReader::get_machine(void) noexcept {
186196
return mach;
187197
}
188198

189-
uint64_t TraceContainerReader::get_trace_version(void) throw () {
199+
uint64_t TraceContainerReader::get_trace_version(void) noexcept {
190200
return trace_version;
191201
}
192202

193-
void TraceContainerReader::seek(uint64_t frame_number) throw (TraceException) {
203+
void TraceContainerReader::seek(uint64_t frame_number) {
194204
/* First, make sure the frame is in range. */
195205
check_end_of_trace_num(frame_number, "seek() to non-existant frame");
196206

@@ -215,68 +225,62 @@ namespace SerializedTrace {
215225
}
216226
}
217227

218-
std::auto_ptr<frame> TraceContainerReader::get_frame(void) throw (TraceException) {
228+
std::unique_ptr<frame> TraceContainerReader::get_frame(void) {
219229
/* Make sure we are in bounds. */
220230
check_end_of_trace("get_frame() on non-existant frame");
221231

222232
uint64_t frame_len;
223233
READ(frame_len);
224234
if (frame_len == 0) {
225-
throw (TraceException("Read zero-length frame at offset " + TELL(ifs)));
235+
throw (TraceException("Read zero-length frame at offset " + std::to_string(TELL(ifs))));
226236
}
227237

228-
/* We really just want a variable sized array, but MS VC++ doesn't support C99 yet.
229-
*
230-
* http://stackoverflow.com/questions/5246900/enabling-vlasvariable-length-arrays-in-ms-visual-c
231-
*/
232-
auto_vec<char> buf ( new char[frame_len] );
238+
std::vector<uint8_t> buf(frame_len);
233239

234240
/* Read the frame into buf. */
235-
if (fread(buf.get(), 1, frame_len, ifs) != frame_len) {
241+
if (fread(buf.data(), 1, frame_len, ifs) != frame_len) {
236242
throw (TraceException("Unable to read frame from trace"));
237243
}
238244

239-
std::string sbuf(buf.get(), frame_len);
240-
241-
std::auto_ptr<frame> f(new frame);
242-
if (!(f->ParseFromString(sbuf))) {
245+
std::unique_ptr<frame> f(new frame);
246+
if (!(f->ParseFromArray(buf.data(), buf.size()))) {
243247
throw (TraceException("Unable to parse from string"));
244248
}
245249
current_frame++;
246250

247251
return f;
248252
}
249253

250-
std::auto_ptr<std::vector<frame> > TraceContainerReader::get_frames(uint64_t requested_frames) throw (TraceException) {
254+
std::unique_ptr<std::vector<frame> > TraceContainerReader::get_frames(uint64_t requested_frames) {
251255
check_end_of_trace("get_frames() on non-existant frame");
252256

253-
std::auto_ptr<std::vector<frame> > frames(new std::vector<frame>);
257+
std::unique_ptr<std::vector<frame> > frames(new std::vector<frame>);
254258
for (uint64_t i = 0; i < requested_frames && current_frame < num_frames; i++) {
255259
frames->push_back(*(get_frame()));
256260
}
257261

258262
return frames;
259263
}
260264

261-
bool TraceContainerReader::end_of_trace(void) throw () {
265+
bool TraceContainerReader::end_of_trace(void) noexcept {
262266
return end_of_trace_num(current_frame);
263267
}
264268

265-
bool TraceContainerReader::end_of_trace_num(uint64_t frame_num) throw () {
269+
bool TraceContainerReader::end_of_trace_num(uint64_t frame_num) noexcept {
266270
if (frame_num + 1 > num_frames) {
267271
return true;
268272
} else {
269273
return false;
270274
}
271275
}
272276

273-
void TraceContainerReader::check_end_of_trace_num(uint64_t frame_num, std::string msg) throw (TraceException) {
277+
void TraceContainerReader::check_end_of_trace_num(uint64_t frame_num, std::string msg) {
274278
if (end_of_trace_num(frame_num)) {
275279
throw (TraceException(msg));
276280
}
277281
}
278282

279-
void TraceContainerReader::check_end_of_trace(std::string msg) throw (TraceException) {
283+
void TraceContainerReader::check_end_of_trace(std::string msg) {
280284
return check_end_of_trace_num(current_frame, msg);
281285
}
282286
};

libtrace/src/trace.container.hpp

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* <uint64_t frame_machine, 0 for unspecified>
2424
* <uint64_t n = number of trace frames>
2525
* <uint64_t offset of field m (below)>
26+
* <uint64_t sizeof(meta frame)>
27+
* <meta frame>
2628
* [ <uint64_t sizeof(trace frame 0)>
2729
* <trace frame 0>
2830
* ..............
@@ -61,9 +63,10 @@ namespace SerializedTrace {
6163
const uint64_t frame_machine_offset = 24LL;
6264
const uint64_t num_trace_frames_offset = 32LL;
6365
const uint64_t toc_offset_offset = 40LL;
64-
const uint64_t first_frame_offset = 48LL;
66+
const uint64_t meta_size_offset = 48LL;
67+
const uint64_t meta_offset = 56LL;
6568

66-
const uint64_t lowest_supported_version = 1LL;
69+
const uint64_t lowest_supported_version = 2LL;
6770
const uint64_t highest_supported_version = 2LL;
6871

6972

@@ -75,9 +78,9 @@ namespace SerializedTrace {
7578
: msg (s)
7679
{ }
7780

78-
~TraceException(void) throw () { }
81+
~TraceException(void) noexcept { }
7982

80-
virtual const char* what() const throw()
83+
virtual const char* what() const noexcept
8184
{
8285
return msg.c_str();
8386
}
@@ -97,19 +100,17 @@ namespace SerializedTrace {
97100
TraceContainerWriter(const std::string& filename,
98101
frame_architecture arch = default_arch,
99102
uint64_t machine = default_machine,
100-
uint64_t frames_per_toc_entry = default_frames_per_toc_entry)
101-
throw (TraceException);
103+
uint64_t frames_per_toc_entry = default_frames_per_toc_entry);
102104

103105
// creates a container for the second version of a protocol.
104106
TraceContainerWriter(const std::string& filename,
105107
const meta_frame& meta,
106108
frame_architecture arch = default_arch,
107109
uint64_t machine = default_machine,
108-
uint64_t frames_per_toc_entry = default_frames_per_toc_entry)
109-
throw (TraceException);
110+
uint64_t frames_per_toc_entry = default_frames_per_toc_entry);
110111

111112
/** Add [frame] to the trace. */
112-
void add(const frame &f) throw (TraceException);
113+
void add(const frame &f);
113114

114115
// closes the trace and underlying file stream. If the stream is
115116
// seekable, the output a table of contents and update the header
@@ -141,44 +142,46 @@ namespace SerializedTrace {
141142
public:
142143

143144
/** Creates a trace container reader that reads from [filename]. */
144-
TraceContainerReader(std::string filename) throw (TraceException);
145+
TraceContainerReader(std::string filename);
145146

146147
/** Destructor. */
147-
~TraceContainerReader(void) throw ();
148+
~TraceContainerReader(void) noexcept;
148149

149150
/** Returns the number of frames in the trace. */
150-
uint64_t get_num_frames(void) throw ();
151+
uint64_t get_num_frames(void) noexcept;
151152

152153
/** Returns the number of frames per toc entry. */
153-
uint64_t get_frames_per_toc_entry(void) throw ();
154+
uint64_t get_frames_per_toc_entry(void) noexcept;
154155

155156
/** Returns the architecture of the trace. */
156-
frame_architecture get_arch(void) throw ();
157+
frame_architecture get_arch(void) noexcept;
157158

158159
/** Returns the machine type (sub-architecture) of the trace. */
159-
uint64_t get_machine(void) throw ();
160+
uint64_t get_machine(void) noexcept;
160161

161162
/** Returns trace version. */
162-
uint64_t get_trace_version(void) throw ();
163+
uint64_t get_trace_version(void) noexcept;
163164

164165
/** Seek to frame number [frame_number]. The frame is numbered
165166
* 0. */
166-
void seek(uint64_t frame_number) throw (TraceException);;
167+
void seek(uint64_t frame_number);;
167168

168169
/** Return the frame pointed to by the frame pointer. Advances the
169170
frame pointer by one after. */
170-
std::auto_ptr<frame> get_frame(void) throw (TraceException);
171+
std::unique_ptr<frame> get_frame(void);
171172

172173
/** Return [num_frames] starting at the frame pointed to by the
173174
frame pointer. If there are not that many frames until the end
174175
of the trace, returns all until the end of the trace. The
175176
frame pointer is set one frame after the last frame returned.
176177
If the last frame returned is the last frame in the trace, the
177178
frame pointer will point to an invalid frame. */
178-
std::auto_ptr<std::vector<frame> > get_frames(uint64_t num_frames) throw (TraceException);
179+
std::unique_ptr<std::vector<frame> > get_frames(uint64_t num_frames);
179180

180181
/** Return true if frame pointer is at the end of the trace. */
181-
bool end_of_trace(void) throw ();
182+
bool end_of_trace(void) noexcept;
183+
184+
const meta_frame *get_meta(void) const { return &meta; }
182185

183186
protected:
184187
/** File to read trace from. */
@@ -196,37 +199,30 @@ namespace SerializedTrace {
196199
/** Number of frames per toc entry. */
197200
uint64_t frames_per_toc_entry;
198201

202+
/** Base address in file where frames begin */
203+
uint64_t first_frame_offset;
204+
199205
/** CPU architecture. */
200206
frame_architecture arch;
201207

202208
/** Machine type. */
203209
uint64_t mach;
204210

211+
meta_frame meta;
212+
205213
/** Current frame number. */
206214
uint64_t current_frame;
207215

208216
/** Return true if [frame_num] is at the end of the trace. */
209-
bool end_of_trace_num(uint64_t frame_num) throw ();
217+
bool end_of_trace_num(uint64_t frame_num) noexcept;
210218

211219
/** Raise exception if [frame_num] is at the end of the trace. */
212-
void check_end_of_trace_num(uint64_t frame_num, std::string msg) throw (TraceException);
220+
void check_end_of_trace_num(uint64_t frame_num, std::string msg);
213221

214222
/** Raise exception if frame pointer is at the end of the trace. */
215-
void check_end_of_trace(std::string msg) throw (TraceException);
223+
void check_end_of_trace(std::string msg);
216224

217225
};
218-
219-
/* A minimal smart pointer class for arrays. */
220-
template< typename T_ >
221-
struct auto_vec{
222-
T_* t_;
223-
auto_vec( T_* t ): t_( t ) {}
224-
~auto_vec() { delete[] t_; }
225-
T_* get() const { return t_; }
226-
T_* operator->() const { return get(); }
227-
T_& operator*() const { return *get(); }
228-
};
229-
230226
};
231227

232228
#endif

0 commit comments

Comments
 (0)