From c5ffb6e1cc9c5ff880d037c53b8ebc8562be0008 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 25 May 2015 22:27:19 -0700 Subject: 1459 --- html/003trace.cc.html | 302 ++++++++++++++++---------------------------------- 1 file changed, 98 insertions(+), 204 deletions(-) (limited to 'html/003trace.cc.html') diff --git a/html/003trace.cc.html b/html/003trace.cc.html index 9e98eba5..6a8adeb0 100644 --- a/html/003trace.cc.html +++ b/html/003trace.cc.html @@ -2,7 +2,7 @@ -~/Desktop/s/mu/003trace.cc +003trace.cc @@ -80,11 +80,6 @@ body { font-family: monospace; color: #d0d0d0; background-color: #000000; } //: where changes can cause breakages in faraway subsystems, and picking the //: right test to debug can be an important skill to pick up. //: -//: A final wrinkle is for recursive functions; it's often useful to segment -//: calls of different depth in the trace: -//: +eval/1: => 34 # the topmost call to eval should have logged this line -//: (look at new_trace_frame below) -//: //: To build robust tests, trace facts about your domain rather than details of //: how you computed them. //: @@ -125,64 +120,66 @@ bool Hide_warnings = false //? cerr << "AAA setup\n"; //? 2 Hide_warnings = false; +:(before "End Types") +struct trace_line { + int depth; // optional field just to help browse traces later + string label; + string contents; + trace_line(string l, string c) :depth(0), label(l), contents(c) {} + trace_line(int d, string l, string c) :depth(d), label(l), contents(c) {} +}; + :(before "End Tracing") struct trace_stream { - vector<pair<string, pair<int, string> > > past_lines; // [(layer label, frame, line)] - map<string, int> frame; + vector<trace_line> past_lines; // accumulator for current line ostringstream* curr_stream; string curr_layer; + int curr_depth; string dump_layer; - trace_stream() :curr_stream(NULL) {} + trace_stream() :curr_stream(NULL), curr_depth(0) {} ~trace_stream() { if (curr_stream) delete curr_stream; } ostringstream& stream(string layer) { + return stream(0, layer); + } + + ostringstream& stream(int depth, string layer) { newline(); curr_stream = new ostringstream; curr_layer = layer; + curr_depth = depth; return *curr_stream; } - // be sure to call this before messing with curr_stream or curr_layer or frame + // be sure to call this before messing with curr_stream or curr_layer void newline() { if (!curr_stream) return; string curr_contents = curr_stream->str(); - curr_contents.erase(curr_contents.find_last_not_of("\r\n")+1); - past_lines.push_back(pair<string, pair<int, string> >(curr_layer, pair<int, string>(frame[curr_layer], curr_contents))); + past_lines.push_back(trace_line(curr_depth, trim(curr_layer), curr_contents)); // preserve indent in contents if (curr_layer == dump_layer || curr_layer == "dump" || dump_layer == "all" || (!Hide_warnings && curr_layer == "warn")) //? if (dump_layer == "all" && (Current_routine->id == 3 || curr_layer == "schedule")) //? 1 - cerr << curr_layer << '/' << frame[curr_layer] << ": " << curr_contents << '\n'; + cerr << curr_layer << ": " << curr_contents << '\n'; delete curr_stream; curr_stream = NULL; + curr_layer.clear(); + curr_depth = 0; } // Useful for debugging. - string readable_contents(string layer) { // missing layer = everything, frame, hierarchical layers + string readable_contents(string layer) { // missing layer = everything newline(); ostringstream output; - string real_layer, frame; - parse_layer_and_frame(layer, &real_layer, &frame); - for (vector<pair<string, pair<int, string> > >::iterator p = past_lines.begin(); p != past_lines.end(); ++p) - if (layer.empty() || prefix_match(real_layer, p->first)) - output << p->first << "/" << p->second.first << ": " << p->second.second << '\n'; + layer = trim(layer); + for (vector<trace_line>::iterator p = past_lines.begin(); p != past_lines.end(); ++p) + if (layer.empty() || layer == p->label) { + if (p->depth) + output << std::setw(4) << p->depth << ' '; + output << p->label << ": " << p->contents << '\n'; + } return output.str(); } - - // Useful for a newcomer to visualize the program at work. - void dump_browseable_contents(string layer) { - ofstream dump("dump"); - dump << "<div class='frame' frame_index='1'>start</div>\n"; - for (vector<pair<string, pair<int, string> > >::iterator p = past_lines.begin(); p != past_lines.end(); ++p) { - if (p->first != layer) continue; - dump << "<div class='frame"; - if (p->second.first > 1) dump << " hidden"; - dump << "' frame_index='" << p->second.first << "'>"; - dump << p->second.second; - dump << "</div>\n"; - } - dump.close(); - } }; ^L @@ -190,7 +187,7 @@ struct trace_stream { trace_stream* Trace_stream = NULL; // Top-level helper. IMPORTANT: can't nest. -#define trace(layer) !Trace_stream ? cerr /*print nothing*/ : Trace_stream->stream(layer) +#define trace(...) !Trace_stream ? cerr /*print nothing*/ : Trace_stream->stream(__VA_ARGS__) // Warnings should go straight to cerr by default since calls to trace() have // some unfriendly constraints (they delay printing, they can't nest) #define raise ((!Trace_stream || !Hide_warnings) ? cerr /*do print*/ : Trace_stream->stream("warn")) @@ -241,106 +238,58 @@ struct lease_tracer { START_TRACING_UNTIL_END_OF_SCOPE //? Trace_stream->dump_layer = "all"; //? 1 -:(before "End Tracing") -void trace_all(const string& label, const list<string>& in) { - for (list<string>::const_iterator p = in.begin(); p != in.end(); ++p) - trace(label) << *p; -} +#define CHECK_TRACE_CONTENTS(...) check_trace_contents(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__) -bool check_trace_contents(string FUNCTION, string FILE, int LINE, string expected) { // missing layer == anywhere, frame, hierarchical layers +:(before "End Tracing") +bool check_trace_contents(string FUNCTION, string FILE, int LINE, string expected) { // missing layer == anywhere vector<string> expected_lines = split(expected, "^D"); - index_t curr_expected_line = 0; - while (curr_expected_line < expected_lines.size() && expected_lines.at(curr_expected_line).empty()) + long long int curr_expected_line = 0; + while (curr_expected_line < SIZE(expected_lines) && expected_lines.at(curr_expected_line).empty()) ++curr_expected_line; - if (curr_expected_line == expected_lines.size()) return true; + if (curr_expected_line == SIZE(expected_lines)) return true; Trace_stream->newline(); - string layer, frame, contents; - parse_layer_frame_contents(expected_lines.at(curr_expected_line), &layer, &frame, &contents); - for (vector<pair<string, pair<int, string> > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { - if (!layer.empty() && !prefix_match(layer, p->first)) - continue; - - if (!frame.empty() && strtol(frame.c_str(), NULL, 0) != p->second.first) + string layer, contents; + split_layer_contents(expected_lines.at(curr_expected_line), &layer, &contents); + for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { +//? cerr << "AAA " << layer << ' ' << p->label << '\n'; //? 1 + if (layer != p->label) continue; - if (contents != p->second.second) +//? cerr << "BBB ^" << contents << "$ ^" << p->contents << "$\n"; //? 1 + if (contents != trim(p->contents)) continue; +//? cerr << "CCC\n"; //? 1 ++curr_expected_line; - while (curr_expected_line < expected_lines.size() && expected_lines.at(curr_expected_line).empty()) + while (curr_expected_line < SIZE(expected_lines) && expected_lines.at(curr_expected_line).empty()) ++curr_expected_line; - if (curr_expected_line == expected_lines.size()) return true; - parse_layer_frame_contents(expected_lines.at(curr_expected_line), &layer, &frame, &contents); + if (curr_expected_line == SIZE(expected_lines)) return true; + split_layer_contents(expected_lines.at(curr_expected_line), &layer, &contents); } ++Num_failures; cerr << "\nF - " << FUNCTION << "(" << FILE << ":" << LINE << "): missing [" << contents << "] in trace:\n"; DUMP(layer); +//? exit(0); //? 1 Passed = false; return false; } -void parse_layer_frame_contents(const string& orig, string* layer, string* frame, string* contents) { - string layer_and_frame; - parse_contents(orig, ": ", &layer_and_frame, contents); - parse_layer_and_frame(layer_and_frame, layer, frame); -} - -void parse_contents(const string& s, const string& delim, string* prefix, string* contents) { - index_t pos = s.find(delim); - if (pos == NOT_FOUND) { - *prefix = ""; - *contents = s; - } - else { - *prefix = s.substr(0, pos); - *contents = s.substr(pos+delim.size()); - } -} - -void parse_layer_and_frame(const string& orig, string* layer, string* frame) { - index_t last_slash = orig.rfind('/'); - if (last_slash == NOT_FOUND - || orig.find_last_not_of("0123456789") != last_slash) { - *layer = orig; - *frame = ""; +void split_layer_contents(const string& s, string* layer, string* contents) { + static const string delim(": "); + size_t pos = s.find(delim); + if (pos == string::npos) { + *layer = ""; + *contents = trim(s); } else { - *layer = orig.substr(0, last_slash); - *frame = orig.substr(last_slash+1); + *layer = trim(s.substr(0, pos)); + *contents = trim(s.substr(pos+SIZE(delim))); } } ^L -bool check_trace_contents(string FUNCTION, string FILE, int LINE, string layer, string expected) { // empty layer == everything, multiple layers, hierarchical layers - vector<string> expected_lines = split(expected, "^D"); - index_t curr_expected_line = 0; - while (curr_expected_line < expected_lines.size() && expected_lines.at(curr_expected_line).empty()) - ++curr_expected_line; - if (curr_expected_line == expected_lines.size()) return true; - Trace_stream->newline(); - vector<string> layers = split(layer, ","); - for (vector<pair<string, pair<int, string> > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { - if (!layer.empty() && !any_prefix_match(layers, p->first)) - continue; - if (p->second.second != expected_lines.at(curr_expected_line)) - continue; - ++curr_expected_line; - while (curr_expected_line < expected_lines.size() && expected_lines.at(curr_expected_line).empty()) - ++curr_expected_line; - if (curr_expected_line == expected_lines.size()) return true; - } - - ++Num_failures; - cerr << "\nF - " << FUNCTION << "(" << FILE << ":" << LINE << "): missing [" << expected_lines.at(curr_expected_line) << "] in trace:\n"; - DUMP(layer); - Passed = false; - return false; -} - -#define CHECK_TRACE_CONTENTS(...) check_trace_contents(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__) - int trace_count(string layer) { return trace_count(layer, ""); } @@ -348,22 +297,9 @@ int trace_count(string layer(string layer, string line) { Trace_stream->newline(); long result = 0; - vector<string> layers = split(layer, ","); - for (vector<pair<string, pair<int, string> > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { - if (any_prefix_match(layers, p->first)) - if (line == "" || p->second.second == line) - ++result; - } - return result; -} - -int trace_count(string layer, int frame, string line) { - Trace_stream->newline(); - long result = 0; - vector<string> layers = split(layer, ","); - for (vector<pair<string, pair<int, string> > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { - if (any_prefix_match(layers, p->first) && p->second.first == frame) - if (line == "" || p->second.second == line) + for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { + if (layer == p->label) + if (line == "" || line == p->contents) ++result; } return result; @@ -388,98 +324,36 @@ bool trace_doesnt_contain(string expectedreturn trace_doesnt_contain(tmp.at(0), tmp.at(1)); } -bool trace_doesnt_contain(string layer, int frame, string line) { - return trace_count(layer, frame, line) == 0; -} - #define CHECK_TRACE_DOESNT_CONTAIN(...) CHECK(trace_doesnt_contain(__VA_ARGS__)) ^L -// manage layer counts in Trace_stream using RAII -struct lease_trace_frame { - string layer; - lease_trace_frame(string l) :layer(l) { - if (!Trace_stream) return; - Trace_stream->newline(); - ++Trace_stream->frame[layer]; - } - ~lease_trace_frame() { - if (!Trace_stream) return; - Trace_stream->newline(); - --Trace_stream->frame[layer]; - } -}; -#define new_trace_frame(layer) lease_trace_frame leased_frame(layer); - -bool check_trace_contents(string FUNCTION, string FILE, int LINE, string layer, int frame, string expected) { // multiple layers, hierarchical layers - vector<string> expected_lines = split(expected, "^D"); // hack: doesn't handle newlines in embedded in lines - index_t curr_expected_line = 0; - while (curr_expected_line < expected_lines.size() && expected_lines.at(curr_expected_line).empty()) - ++curr_expected_line; - if (curr_expected_line == expected_lines.size()) return true; - Trace_stream->newline(); - vector<string> layers = split(layer, ","); - for (vector<pair<string, pair<int, string> > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { - if (!layer.empty() && !any_prefix_match(layers, p->first)) - continue; - if (p->second.first != frame) - continue; - if (p->second.second != expected_lines.at(curr_expected_line)) - continue; - ++curr_expected_line; - while (curr_expected_line < expected_lines.size() && expected_lines.at(curr_expected_line).empty()) - ++curr_expected_line; - if (curr_expected_line == expected_lines.size()) return true; - } - - ++Num_failures; - cerr << "\nF - " << FUNCTION << "(" << FILE << ":" << LINE << "): missing [" << expected_lines.at(curr_expected_line) << "] in trace/" << frame << ":\n"; - DUMP(layer); - Passed = false; - return false; -} - -#define CHECK_TRACE_TOP(layer, expected) CHECK_TRACE_CONTENTS(layer, 1, expected) - -^L - vector<string> split(string s, string delim) { vector<string> result; - index_t begin=0, end=s.find(delim); + size_t begin=0, end=s.find(delim); while (true) { - if (end == NOT_FOUND) { - result.push_back(string(s, begin, NOT_FOUND)); + if (end == string::npos) { + result.push_back(string(s, begin, string::npos)); break; } result.push_back(string(s, begin, end-begin)); - begin = end+delim.size(); + begin = SIZE(end+delim); end = s.find(delim, begin); } return result; } -bool any_prefix_match(const vector<string>& pats, const string& needle) { - if (pats.empty()) return false; - if (*pats.at(0).rbegin() != '/') - // prefix match not requested - return find(pats.begin(), pats.end(), needle) != pats.end(); - // first pat ends in a '/'; assume all pats do. - for (vector<string>::const_iterator p = pats.begin(); p != pats.end(); ++p) - if (headmatch(needle, *p)) return true; - return false; -} - -bool prefix_match(const string& pat, const string& needle) { - if (*pat.rbegin() != '/') - // prefix match not requested - return pat == needle; - return headmatch(needle, pat); -} - -bool headmatch(const string& s, const string& pat) { - if (pat.size() > s.size()) return false; - return std::mismatch(pat.begin(), pat.end(), s.begin()).first == pat.end(); +string trim(const string& s) { + string::const_iterator first = s.begin(); + while (first != s.end() && isspace(*first)) + ++first; + if (first == s.end()) return ""; + + string::const_iterator last = --s.end(); + while (last != s.begin() && isspace(*last)) + --last; + ++last; + return string(first, last); } :(before "End Includes") @@ -487,8 +361,6 @@ bool headmatch(const string& s; #include<list> using std::list; -#include<utility> -using std::pair; #include<map> using std::map; #include<set> @@ -501,6 +373,7 @@ using std::ostream; using std::cin; using std::cout; using std::cerr; +#include<iomanip> #include<sstream> using std::istringstream; @@ -511,6 +384,27 @@ using std::ifstream; using std::ofstream; #define unused __attribute__((unused)) + +:(before "End Globals") +//: In future layers we'll use the depth field as follows: +//: +//: Mu 'applications' will be able to use depths 1-99 as they like. +//: Depth 100 will be for scheduling (more on that later). +const int Scheduling_depth = 100; +//: Primitive statements will occupy 101-9998 +const int Initial_callstack_depth = 101; +const int Max_callstack_depth = 9998; +//: (ignore this until the call layer) +:(before "End Globals") +int Callstack_depth = 0; +:(before "End Setup") +Callstack_depth = 0; +//: Finally, details of primitive mu statements will occupy depth 9999 (more on that later as well) +:(before "End Globals") +const int Primitive_recipe_depth = 9999; +//: +//: This framework should help us hide some details at each level, mixing +//: static ideas like layers with the dynamic notion of call-stack depth. -- cgit 1.4.1-2-gfad0