tp: get table name from SQLite rather than storing ourselves

We were accidentally using the wrong table name for tables created by
running CREATE VIRTUAL TABLE.

Change-Id: Ia8603fab01ff95179e41ce4a6808b431f9d1e737
diff --git a/src/trace_processor/sqlite/sqlite_table.h b/src/trace_processor/sqlite/sqlite_table.h
index 4b6b765..2d9ed50 100644
--- a/src/trace_processor/sqlite/sqlite_table.h
+++ b/src/trace_processor/sqlite/sqlite_table.h
@@ -189,7 +189,6 @@
   struct TableDescriptor {
     SqliteTable::Factory<Context> factory;
     Context context;
-    std::string name;
     sqlite3_module module = {};
   };
 
@@ -204,7 +203,7 @@
   template <typename TTable, typename Context = const TraceStorage*>
   static void Register(sqlite3* db,
                        Context ctx,
-                       const std::string& table_name,
+                       const std::string& module_name,
                        bool read_write = false,
                        bool requires_args = false) {
     using TCursor = typename TTable::Cursor;
@@ -213,7 +212,6 @@
         new TableDescriptor<Context>());
     desc->context = std::move(ctx);
     desc->factory = GetFactory<TTable, Context>();
-    desc->name = table_name;
     sqlite3_module* module = &desc->module;
     memset(module, 0, sizeof(*module));
 
@@ -222,7 +220,16 @@
                         char** pzErr) {
       auto* xdesc = static_cast<TableDescriptor<Context>*>(arg);
       auto table = xdesc->factory(xdb, std::move(xdesc->context));
-      table->name_ = xdesc->name;
+
+      // SQLite guarantees that argv[0] will be the "module" name: this is the
+      // same as |table_name| passed to the Register function.
+      table->module_name_ = argv[0];
+
+      // SQLite guarantees that argv[2] contains the name of the table: for
+      // non-arg taking tables, this will be the same as |table_name| but for
+      // arg-taking tables, this will be the table name as defined by the user
+      // in the CREATE VIRTUAL TABLE call.
+      table->name_ = argv[2];
 
       Schema schema;
       base::Status status = table->Init(argc, argv, &schema);
@@ -298,7 +305,7 @@
     }
 
     int res = sqlite3_create_module_v2(
-        db, table_name.c_str(), module, desc.release(),
+        db, module_name.c_str(), module, desc.release(),
         [](void* arg) { delete static_cast<TableDescriptor<Context>*>(arg); });
     PERFETTO_CHECK(res == SQLITE_OK);
 
@@ -307,8 +314,9 @@
     // that virtual tables requiring arguments aren't registered because they
     // can't be automatically instantiated for exporting.
     if (!requires_args) {
-      char* insert_sql = sqlite3_mprintf(
-          "INSERT INTO perfetto_tables(name) VALUES('%q')", table_name.c_str());
+      char* insert_sql =
+          sqlite3_mprintf("INSERT INTO perfetto_tables(name) VALUES('%q')",
+                          module_name.c_str());
       char* error = nullptr;
       sqlite3_exec(db, insert_sql, nullptr, nullptr, &error);
       sqlite3_free(insert_sql);
@@ -338,6 +346,7 @@
   }
 
   const Schema& schema() const { return schema_; }
+  const std::string& module_name() const { return module_name_; }
   const std::string& name() const { return name_; }
 
  private:
@@ -357,7 +366,17 @@
   SqliteTable(const SqliteTable&) = delete;
   SqliteTable& operator=(const SqliteTable&) = delete;
 
+  // This name of the table. For tables created using CREATE VIRTUAL TABLE, this
+  // will be the name of the table specified by the query. For automatically
+  // created tables, this will be the same as the module name passed to
+  // RegisterTable.
   std::string name_;
+
+  // The module name is the name passed to RegisterTable. This is differs from
+  // the table name (|name_|) where the table was created using CREATE VIRTUAL
+  // TABLE.
+  std::string module_name_;
+
   Schema schema_;
 
   QueryConstraints qc_cache_;