Added "yosys -X"
authorClifford Wolf <clifford@clifford.at>
Mon, 29 Dec 2014 12:33:33 +0000 (13:33 +0100)
committerClifford Wolf <clifford@clifford.at>
Mon, 29 Dec 2014 12:33:33 +0000 (13:33 +0100)
kernel/driver.cc
kernel/log.cc
kernel/log.h
kernel/rtlil.cc
kernel/rtlil.h
kernel/yosys.cc
kernel/yosys.h

index 9a81d8a4879bb8d2f2bf0804af58d9dd0a3d361e..cdf78460251aa830a11433bdd7ae95dac2d6dc13 100644 (file)
@@ -151,6 +151,9 @@ int main(int argc, char **argv)
                printf("    -m module_file\n");
                printf("        load the specified module (aka plugin)\n");
                printf("\n");
+               printf("    -X\n");
+               printf("        enable tracing of core data structure changes. for debugging\n");
+               printf("\n");
                printf("    -M\n");
                printf("        will slightly randomize allocated pointer addresses. for debugging\n");
                printf("\n");
@@ -177,13 +180,16 @@ int main(int argc, char **argv)
        }
 
        int opt;
-       while ((opt = getopt(argc, argv, "MAQTVSm:f:Hh:b:o:p:l:qv:tds:c:")) != -1)
+       while ((opt = getopt(argc, argv, "MXAQTVSm:f:Hh:b:o:p:l:qv:tds:c:")) != -1)
        {
                switch (opt)
                {
                case 'M':
                        memhasher_on();
                        break;
+               case 'X':
+                       yosys_xtrace++;
+                       break;
                case 'A':
                        call_abort = true;
                        break;
index 5b18e3d6c3705fdae1bd085905b7ad909ba16c7f..b984f0112e4183220730c3f31b70637218b555ee 100644 (file)
 #  include <sys/time.h>
 #endif
 
+#ifdef __linux__
+#  include <dlfcn.h>
+#endif
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -243,6 +247,97 @@ void log_pop()
        log_flush();
 }
 
+void log_backtrace(const char *prefix, int levels)
+{
+#ifdef __linux__
+       if (levels <= 0) return;
+
+       Dl_info dli;
+       void *p;
+
+       if ((p = __builtin_extract_return_addr(__builtin_return_address(0))) && dladdr(p, &dli)) {
+               log("%sframe #1: %p %s(%p) %s(%p)\n", prefix, p, dli.dli_fname, dli.dli_fbase, dli.dli_sname, dli.dli_saddr);
+       } else {
+               log("%sframe #1: ---\n", prefix);
+               return;
+       }
+
+       if (levels <= 1) return;
+
+       if ((p = __builtin_extract_return_addr(__builtin_return_address(1))) && dladdr(p, &dli)) {
+               log("%sframe #2: %p %s(%p) %s(%p)\n", prefix, p, dli.dli_fname, dli.dli_fbase, dli.dli_sname, dli.dli_saddr);
+       } else {
+               log("%sframe #2: ---\n", prefix);
+               return;
+       }
+
+       if (levels <= 2) return;
+
+       if ((p = __builtin_extract_return_addr(__builtin_return_address(2))) && dladdr(p, &dli)) {
+               log("%sframe #3: %p %s(%p) %s(%p)\n", prefix, p, dli.dli_fname, dli.dli_fbase, dli.dli_sname, dli.dli_saddr);
+       } else {
+               log("%sframe #3: ---\n", prefix);
+               return;
+       }
+
+       if (levels <= 3) return;
+
+       if ((p = __builtin_extract_return_addr(__builtin_return_address(3))) && dladdr(p, &dli)) {
+               log("%sframe #4: %p %s(%p) %s(%p)\n", prefix, p, dli.dli_fname, dli.dli_fbase, dli.dli_sname, dli.dli_saddr);
+       } else {
+               log("%sframe #4: ---\n", prefix);
+               return;
+       }
+
+       if (levels <= 4) return;
+
+       if ((p = __builtin_extract_return_addr(__builtin_return_address(4))) && dladdr(p, &dli)) {
+               log("%sframe #5: %p %s(%p) %s(%p)\n", prefix, p, dli.dli_fname, dli.dli_fbase, dli.dli_sname, dli.dli_saddr);
+       } else {
+               log("%sframe #5: ---\n", prefix);
+               return;
+       }
+
+       if (levels <= 5) return;
+
+       if ((p = __builtin_extract_return_addr(__builtin_return_address(5))) && dladdr(p, &dli)) {
+               log("%sframe #6: %p %s(%p) %s(%p)\n", prefix, p, dli.dli_fname, dli.dli_fbase, dli.dli_sname, dli.dli_saddr);
+       } else {
+               log("%sframe #6: ---\n", prefix);
+               return;
+       }
+
+       if (levels <= 6) return;
+
+       if ((p = __builtin_extract_return_addr(__builtin_return_address(6))) && dladdr(p, &dli)) {
+               log("%sframe #7: %p %s(%p) %s(%p)\n", prefix, p, dli.dli_fname, dli.dli_fbase, dli.dli_sname, dli.dli_saddr);
+       } else {
+               log("%sframe #7: ---\n", prefix);
+               return;
+       }
+
+       if (levels <= 7) return;
+
+       if ((p = __builtin_extract_return_addr(__builtin_return_address(7))) && dladdr(p, &dli)) {
+               log("%sframe #8: %p %s(%p) %s(%p)\n", prefix, p, dli.dli_fname, dli.dli_fbase, dli.dli_sname, dli.dli_saddr);
+       } else {
+               log("%sframe #8: ---\n", prefix);
+               return;
+       }
+
+       if (levels <= 8) return;
+
+       if ((p = __builtin_extract_return_addr(__builtin_return_address(8))) && dladdr(p, &dli)) {
+               log("%sframe #9: %p %s(%p) %s(%p)\n", prefix, p, dli.dli_fname, dli.dli_fbase, dli.dli_sname, dli.dli_saddr);
+       } else {
+               log("%sframe #9: ---\n", prefix);
+               return;
+       }
+
+       if (levels <= 9) return;
+#endif
+}
+
 void log_reset_stack()
 {
        while (header_count.size() > 1)
index 9bb2b3621159ca9aad64a6476418bf2bba835953..8096758f679a5962d45fabdb8646fe26c87ee855 100644 (file)
@@ -65,6 +65,7 @@ void log_spacer();
 void log_push();
 void log_pop();
 
+void log_backtrace(const char *prefix, int levels);
 void log_reset_stack();
 void log_flush();
 
index f25df175d99de6a5ed202a299c464eb4f1d0c0c1..912df790357f8dba54a8bb2ba913260a7ad2045d 100644 (file)
@@ -269,6 +269,11 @@ void RTLIL::Design::add(RTLIL::Module *module)
 
        for (auto mon : monitors)
                mon->notify_module_add(module);
+
+       if (yosys_xtrace) {
+               log("#X# New Module: %s\n", log_id(module));
+               log_backtrace("-X- ", yosys_xtrace-1);
+       }
 }
 
 RTLIL::Module *RTLIL::Design::addModule(RTLIL::IdString name)
@@ -284,6 +289,11 @@ RTLIL::Module *RTLIL::Design::addModule(RTLIL::IdString name)
        for (auto mon : monitors)
                mon->notify_module_add(module);
 
+       if (yosys_xtrace) {
+               log("#X# New Module: %s\n", log_id(module));
+               log_backtrace("-X- ", yosys_xtrace-1);
+       }
+
        return module;
 }
 
@@ -353,6 +363,11 @@ void RTLIL::Design::remove(RTLIL::Module *module)
        for (auto mon : monitors)
                mon->notify_module_del(module);
 
+       if (yosys_xtrace) {
+               log("#X# Remove Module: %s\n", log_id(module));
+               log_backtrace("-X- ", yosys_xtrace-1);
+       }
+
        log_assert(modules_.at(module->name) == module);
        modules_.erase(module->name);
        delete module;
@@ -1283,6 +1298,11 @@ void RTLIL::Module::connect(const RTLIL::SigSig &conn)
                for (auto mon : design->monitors)
                        mon->notify_connect(this, conn);
 
+       if (yosys_xtrace) {
+               log("#X# Connect (SigSig) in %s: %s = %s (%d bits)\n", log_id(this), log_signal(conn.first), log_signal(conn.second), GetSize(conn.first));
+               log_backtrace("-X- ", yosys_xtrace-1);
+       }
+
        connections_.push_back(conn);
 }
 
@@ -1300,6 +1320,13 @@ void RTLIL::Module::new_connections(const std::vector<RTLIL::SigSig> &new_conn)
                for (auto mon : design->monitors)
                        mon->notify_connect(this, new_conn);
 
+       if (yosys_xtrace) {
+               log("#X# New connections vector in %s:\n", log_id(this));
+               for (auto &conn: new_conn)
+                       log("#X#    %s = %s (%d bits)\n", log_signal(conn.first), log_signal(conn.second), GetSize(conn.first));
+               log_backtrace("-X- ", yosys_xtrace-1);
+       }
+
        connections_ = new_conn;
 }
 
@@ -1795,6 +1822,11 @@ void RTLIL::Cell::unsetPort(RTLIL::IdString portname)
                        for (auto mon : module->design->monitors)
                                mon->notify_connect(this, conn_it->first, conn_it->second, signal);
 
+               if (yosys_xtrace) {
+                       log("#X# Unconnect %s.%s.%s\n", log_id(this->module), log_id(this), log_id(portname));
+                       log_backtrace("-X- ", yosys_xtrace-1);
+               }
+
                connections_.erase(conn_it);
        }
 }
@@ -1816,6 +1848,11 @@ void RTLIL::Cell::setPort(RTLIL::IdString portname, RTLIL::SigSpec signal)
                for (auto mon : module->design->monitors)
                        mon->notify_connect(this, conn_it->first, conn_it->second, signal);
 
+       if (yosys_xtrace) {
+               log("#X# Connect %s.%s.%s = %s (%d)\n", log_id(this->module), log_id(this), log_id(portname), log_signal(signal), GetSize(signal));
+               log_backtrace("-X- ", yosys_xtrace-1);
+       }
+
        conn_it->second = signal;
 }
 
index 25477d02e876769e88265515a2616eb5c58d23dd..bb9e85d93edbd0bf8719e68b8a1703138cd40b3f 100644 (file)
@@ -120,6 +120,12 @@ namespace RTLIL
                        global_id_storage_.at(idx) = strdup(p);
                        global_id_index_[global_id_storage_.at(idx)] = idx;
                        global_refcount_storage_.at(idx)++;
+
+                       if (yosys_xtrace) {
+                               log("#X# New IdString '%s' with index %d.\n", p, idx);
+                               log_backtrace("-X- ", yosys_xtrace-1);
+                       }
+
                        return idx;
                }
 
@@ -135,6 +141,11 @@ namespace RTLIL
                        if (--global_refcount_storage_.at(idx) != 0)
                                return;
 
+                       if (yosys_xtrace) {
+                               log("#X# Removed IdString '%s' with index %d.\n", global_id_storage_.at(idx), idx);
+                               log_backtrace("-X- ", yosys_xtrace-1);
+                       }
+
                        global_id_index_.erase(global_id_storage_.at(idx));
                        free(global_id_storage_.at(idx));
                        global_id_storage_.at(idx) = nullptr;
index 0faca8d51ef5761a2aa282bce08200b06a53e7f9..6bc8dd0bb2dc97711e7ff89220ecf17c69263f5a 100644 (file)
@@ -49,6 +49,7 @@
 YOSYS_NAMESPACE_BEGIN
 
 int autoidx = 1;
+int yosys_xtrace = 0;
 RTLIL::Design *yosys_design = NULL;
 
 #ifdef YOSYS_ENABLE_TCL
index c0004abcec5567e82e892cd4297f16e99f70b181..a4836f35a9b0454116ff09117b2c7abd237187a5 100644 (file)
@@ -210,6 +210,7 @@ template<typename T> int GetSize(const T &obj) { return obj.size(); }
 int GetSize(RTLIL::Wire *wire);
 
 extern int autoidx;
+extern int yosys_xtrace;
 
 YOSYS_NAMESPACE_END