From 0a23358bb20dbbb6e2c47aa294c65108274b6c28 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Wed, 21 May 2014 15:13:56 +0200 Subject: [PATCH] Use vectors everywhere and be pedantic about input data. --- malloctrace.cpp | 2 +- malloctrace_main.cpp | 69 +++++++++++++++++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 26 deletions(-) diff --git a/malloctrace.cpp b/malloctrace.cpp index c31bcec..b63897b 100644 --- a/malloctrace.cpp +++ b/malloctrace.cpp @@ -292,7 +292,7 @@ struct Data } mutex m_mutex; - unsigned int next_module_id = 1; + unsigned int next_module_id = 0; unsigned int next_thread_id = 0; unsigned int next_ipCache_id = 0; unsigned int next_trace_id = 0; diff --git a/malloctrace_main.cpp b/malloctrace_main.cpp index e4468b6..984a53c 100644 --- a/malloctrace_main.cpp +++ b/malloctrace_main.cpp @@ -138,9 +138,8 @@ struct AccumulatedTraceData traces.reserve(16384); } - /// TODO: vectors? - unordered_map> modules; - unordered_map instructions; + vector> modules; + vector instructions; vector traces; }; @@ -166,7 +165,9 @@ int main(int argc, char** argv) string line; line.reserve(1024); stringstream lineIn(ios_base::in); - unsigned int nextTraceId = 0; + size_t nextTraceId = 0; + size_t nextModuleId = 0; + size_t nextIpId = 0; while (in.good()) { getline(in, line); if (line.empty()) { @@ -177,8 +178,13 @@ int main(int argc, char** argv) char mode = 0; lineIn >> mode; if (mode == 'm') { - unsigned int id = 0; + size_t id = 0; lineIn >> id; + if (id != nextModuleId) { + cerr << "inconsistent trace data: " << line << endl + << "expected module with id: " << nextModuleId << endl; + return 1; + } string fileName; lineIn >> fileName; lineIn << hex; @@ -189,48 +195,60 @@ int main(int argc, char** argv) lineIn >> isExe; if (lineIn.bad()) { cerr << "failed to parse line: " << line << endl; - continue; + return 1; } - data.modules[id] = make_shared(fileName, baseAddress, isExe); + data.modules.push_back(make_shared(fileName, baseAddress, isExe)); + ++nextModuleId; } else if (mode == 'i') { InstructionPointer ip; - unsigned int id = 0; + size_t id = 0; lineIn >> id; - unsigned int moduleId = 0; + if (id != nextIpId) { + cerr << "inconsistent trace data: " << line << endl + << "expected instruction with id: " << nextIpId << endl; + return 1; + } + + size_t moduleId = 0; lineIn >> moduleId; + if (moduleId >= data.modules.size()) { + cerr << "inconsistent trace data: " << line << endl + << "failed to find module " << moduleId << ", only got so far: " << data.modules.size() << endl; + return 1; + } + lineIn << hex; lineIn >> ip.offset; lineIn << dec; if (lineIn.bad()) { cerr << "failed to parse line: " << line << endl; - continue; - } - auto module = data.modules.find(moduleId); - if (module != data.modules.end()) { - ip.module = module->second; + return 1; } - data.instructions[id] = ip; + ip.module = data.modules[moduleId]; + data.instructions.push_back(ip); + ++nextIpId; } else if (mode == 't') { Trace trace; unsigned int id = 0; lineIn >> id; if (lineIn.bad()) { cerr << "failed to parse line: " << line << endl; - continue; + return 1; } if (id != nextTraceId) { - cerr << "inconsistent trace data: " << line << endl << "expected trace with id: " << nextTraceId << endl; + cerr << "inconsistent trace data: " << line << endl + << "expected trace with id: " << nextTraceId << endl; return 1; } while (lineIn.good()) { unsigned int ipId = 0; lineIn >> ipId; - auto ip = data.instructions.find(ipId); - if (ip != data.instructions.end()) { - trace.backtrace.push_back(ip->second); - } else { - cerr << "failed to find instruction " << ipId << endl; + if (ipId >= data.instructions.size()) { + cerr << "inconsistent trace data: " << line << endl + << "failed to find instruction " << ipId << endl; + return 1; } + trace.backtrace.push_back(data.instructions[ipId]); } data.traces.push_back(trace); ++nextTraceId; @@ -241,14 +259,15 @@ int main(int argc, char** argv) lineIn >> traceId; if (lineIn.bad()) { cerr << "failed to parse line: " << line << endl; - continue; + return 1; } if (traceId < data.traces.size()) { auto& trace = data.traces[traceId]; trace.leaked += size; - trace.allocations++; + ++trace.allocations; } else { cerr << "failed to find trace of malloc at " << traceId << endl; + return 1; } } else if (mode == '-') { /// TODO @@ -258,7 +277,7 @@ int main(int argc, char** argv) lineIn >> traceId; if (lineIn.bad()) { cerr << "failed to parse line: " << line << endl; - continue; + return 1; } if (traceId < data.traces.size()) { auto& trace = data.traces[traceId]; -- 2.7.4