systemc: move tracefile registration into constructor

The TraceFile object needs to be registered into the scheduler for
triggering its trace function. For now only the TraceFile created by
sc_create_vcd_trace_file is registered automatically. This design is not
good for users to implement their own TraceFile class.

In addition, some libraries, ex Verilator, implement thier own trace file.
To bridge them into gem5, we also need the ability to create customized
TraceFile class.

Change-Id: I38fe510048655c6a2cd848a0a1263a66a1778eee
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/52923
Reviewed-by: Earl Ou <shunhsingou@google.com>
Reviewed-by: Gabe Black <gabe.black@gmail.com>
Maintainer: Gabe Black <gabe.black@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
diff --git a/src/systemc/utils/sc_trace_file.cc b/src/systemc/utils/sc_trace_file.cc
index 38161ba..dddd8eb 100644
--- a/src/systemc/utils/sc_trace_file.cc
+++ b/src/systemc/utils/sc_trace_file.cc
@@ -27,7 +27,6 @@
 
 #include <vector>
 
-#include "systemc/core/scheduler.hh"
 #include "systemc/ext/channel/sc_signal_in_if.hh"
 #include "systemc/ext/core/sc_event.hh"
 #include "systemc/ext/core/sc_time.hh"
@@ -52,16 +51,12 @@
 sc_trace_file *
 sc_create_vcd_trace_file(const char *name)
 {
-    auto tf = new ::sc_gem5::VcdTraceFile(name);
-    ::sc_gem5::scheduler.registerTraceFile(tf);
-    return tf;
+    return new ::sc_gem5::VcdTraceFile(name);
 }
 
 void
 sc_close_vcd_trace_file(sc_trace_file *tf)
 {
-    ::sc_gem5::scheduler.unregisterTraceFile(
-            static_cast<::sc_gem5::TraceFile *>(tf));
     delete tf;
 }
 
diff --git a/src/systemc/utils/tracefile.cc b/src/systemc/utils/tracefile.cc
index a6cf952..0c54052 100644
--- a/src/systemc/utils/tracefile.cc
+++ b/src/systemc/utils/tracefile.cc
@@ -30,6 +30,7 @@
 #include <ctime>
 #include <iomanip>
 
+#include "systemc/core/scheduler.hh"
 #include "systemc/core/time.hh"
 #include "systemc/ext/core/sc_main.hh"
 #include "systemc/ext/core/sc_time.hh"
@@ -41,10 +42,13 @@
 TraceFile::TraceFile(const std::string &name) :
     _os(gem5::simout.create(name, true, true)), timeUnitTicks(0),
     timeUnitValue(0.0), timeUnitUnit(::sc_core::SC_PS), _traceDeltas(false)
-{}
+{
+    ::sc_gem5::scheduler.registerTraceFile(this);
+}
 
 TraceFile::~TraceFile()
 {
+    ::sc_gem5::scheduler.unregisterTraceFile(this);
     gem5::simout.close(_os);
 }