diff options
Diffstat (limited to 'trace.cc')
-rw-r--r-- | trace.cc | 188 |
1 files changed, 85 insertions, 103 deletions
@@ -6,7 +6,8 @@ namespace ledger { -bool trace_mode; +bool trace_alloc_mode; +bool trace_class_mode; void trace(const string& cat, const string& str) { @@ -37,94 +38,99 @@ object_count_map live_count; bool tracing_active = false; -bool trace_ctor(void * ptr, const char * name) +inline void add_to_count_map(object_count_map& the_map, + const char * name, std::size_t size) +{ + object_count_map::iterator k = the_map.find(name); + if (k != the_map.end()) { + (*k).second.first++; + (*k).second.second += size; + } else { + std::pair<object_count_map::iterator, bool> result = + the_map.insert(object_count_pair(name, count_size_pair(1, size))); + assert(result.second); + } +} + +inline void report_count_map(std::ostream& out, object_count_map& the_map) +{ + for (object_count_map::iterator i = the_map.begin(); + i != the_map.end(); + i++) + out << " " << std::right << std::setw(12) << (*i).second.first + << " " << std::right << std::setw(12) << (*i).second.second + << " " << std::left << (*i).first + << std::endl; +} + +bool trace_ctor(void * ptr, const char * cls_name, const char * args, + std::size_t cls_size) { if (! tracing_active) return true; - DEBUG_PRINT("ledger.trace.debug", "trace_ctor " << ptr << " " << name); + if (trace_class_mode && cls_name[0] == '_') + return true; + if (trace_alloc_mode && cls_name[0] != '_') + return true; + + static char name[1024]; + std::strcpy(name, cls_name); + std::strcat(name, "("); + std::strcat(name, args); + std::strcat(name, ")"); - const char * pos = std::strchr(name, '('); - static char cls_name[1024]; - std::strncpy(cls_name, name, pos - name); - cls_name[pos - name] = '\0'; + DEBUG_PRINT("ledger.trace.debug", + "trace_ctor " << ptr << " " << name); live_objects.insert(live_objects_pair(ptr, cls_name)); - object_count_map::iterator i = ctor_count.find(name); - if (i != ctor_count.end()) { - (*i).second++; - } else { - std::pair<object_count_map::iterator, bool> result - = ctor_count.insert(object_count_pair(name, 1)); - if (! result.second) { - tracing_active = false; - return false; - } - } - - object_count_map::iterator j = object_count.find(cls_name); - if (j != object_count.end()) { - (*j).second++; - } else { - std::pair<object_count_map::iterator, bool> result - = object_count.insert(object_count_pair(cls_name, 1)); - if (! result.second) { - tracing_active = false; - return false; - } - } - - object_count_map::iterator k = live_count.find(cls_name); - if (k != live_count.end()) { - (*k).second++; - } else { - std::pair<object_count_map::iterator, bool> result - = live_count.insert(object_count_pair(cls_name, 1)); - if (! result.second) { - tracing_active = false; - return false; - } - } + add_to_count_map(ctor_count, name, cls_size); + add_to_count_map(object_count, cls_name, cls_size); + add_to_count_map(object_count, "__ALL__", cls_size); + add_to_count_map(live_count, cls_name, cls_size); return true; } -bool trace_dtor(void * ptr, const char * name) +bool trace_dtor(void * ptr, const char * cls_name, std::size_t cls_size) { if (! tracing_active) return true; - DEBUG_PRINT("ledger.trace.debug", "trace_dtor " << ptr << " " << name); + if (trace_class_mode && cls_name[0] == '_') + return true; + if (trace_alloc_mode && cls_name[0] != '_') + return true; + + DEBUG_PRINT("ledger.trace.debug", "trace_dtor " << ptr << " " << cls_name); live_objects_map::iterator i = live_objects.find(ptr); if (i == live_objects.end()) { - std::cerr << "Destruction of unknown object " << name << " " << ptr - << std::endl;; - tracing_active = false; + std::cerr << "Destruction of unknown object of type " << cls_name + << " " << ptr << std::endl; + assert(0); return false; } - const char * cls_name = name; - int ptr_count = live_objects.count(ptr); - for (int x = 0; x < ptr_count; x++) { + for (int x = 0; x < ptr_count; x++, i++) { if ((*i).second == cls_name) { live_objects.erase(i); break; - } else { - i++; } } - object_count_map::iterator k = live_count.find(name); + object_count_map::iterator k = live_count.find(cls_name); if (k == live_count.end()) { - std::cerr << "Destruction of unregistered class " << name + std::cerr << "Destruction of unregistered class " << cls_name << std::endl;; - tracing_active = false; + assert(0); return false; } - if (--(*k).second == 0) + + (*k).second.second -= cls_size; + if (--(*k).second.first == 0) live_count.erase(k); return true; @@ -132,91 +138,67 @@ bool trace_dtor(void * ptr, const char * name) void report_memory(std::ostream& out) { - if (live_count.size() > 0) + if (live_count.size() > 0) { out << "Live object counts:" << std::endl; - - for (object_count_map::iterator i = live_count.begin(); - i != live_count.end(); - i++) { - out << " "; - out << std::right; - out.width(7); - out << (*i).second << " " << (*i).first << std::endl; + report_count_map(out, live_count); } - DEBUG_IF("ledger.trace.verbose") { - if (live_objects.size() > 0) - out << "Live objects:" << std::endl; + if (live_objects.size() > 0) { + out << "Live objects:" << std::endl; for (live_objects_map::iterator i = live_objects.begin(); i != live_objects.end(); - i++) { - out << " "; - out << std::right; - out.width(7); - out << (*i).first << " " << (*i).second << std::endl; - } + i++) + out << " " << std::right << std::setw(7) << (*i).first + << " " << std::left << (*i).second + << std::endl; } - if (object_count.size() > 0) + if (object_count.size() > 0) { out << "Object counts:" << std::endl; - - for (object_count_map::iterator i = object_count.begin(); - i != object_count.end(); - i++) { - out << " "; - out << std::right; - out.width(7); - out << (*i).second << " " << (*i).first << std::endl; + report_count_map(out, object_count); } - if (ctor_count.size() > 0) + if (ctor_count.size() > 0) { out << "Constructor counts:" << std::endl; - - for (object_count_map::iterator i = ctor_count.begin(); - i != ctor_count.end(); - i++) { - out << " "; - out << std::right; - out.width(7); - out << (*i).second << " " << (*i).first << std::endl; + report_count_map(out, ctor_count); } } #if DEBUG_LEVEL >= 4 string::string() : std::string() { - TRACE_CTOR("string()"); + TRACE_CTOR(string, ""); } string::string(const string& str) : std::string(str) { - TRACE_CTOR("string(const string&)"); + TRACE_CTOR(string, "const string&"); } string::string(const std::string& str) : std::string(str) { - TRACE_CTOR("string(const std::string&)"); + TRACE_CTOR(string, "const std::string&"); } string::string(const int len, char x) : std::string(len, x) { - TRACE_CTOR("string(const int, char)"); + TRACE_CTOR(string, "const int, char"); } string::string(const char * str) : std::string(str) { - TRACE_CTOR("string(const char *)"); + TRACE_CTOR(string, "const char *"); } string::string(const char * str, const char * end) : std::string(str, end) { - TRACE_CTOR("string(const char *, const char *)"); + TRACE_CTOR(string, "const char *, const char *"); } string::string(const string& str, int x) : std::string(str, x) { - TRACE_CTOR("string(const string&, int)"); + TRACE_CTOR(string, "const string&, int"); } string::string(const string& str, int x, int y) : std::string(str, x, y) { - TRACE_CTOR("string(const string&, int, int)"); + TRACE_CTOR(string, "const string&, int, int"); } string::string(const char * str, int x) : std::string(str, x) { - TRACE_CTOR("string(const char *, int)"); + TRACE_CTOR(string, "const char *, int"); } string::string(const char * str, int x, int y) : std::string(str, x, y) { - TRACE_CTOR("string(const char *, int, int)"); + TRACE_CTOR(string, "const char *, int, int"); } string::~string() { - TRACE_DTOR("string"); + TRACE_DTOR(string); } #endif |