Insert trace_config as pbtxt in metadata table

Screenshot: https://screenshot.googleplex.com/6bbizGGZzFgkJTs
Bug: 130528569
Change-Id: I4f8eb2490a9754b295e347429fe223133b9b67be
diff --git a/Android.bp b/Android.bp
index 2a2d18b..3fe0336 100644
--- a/Android.bp
+++ b/Android.bp
@@ -2162,6 +2162,21 @@
   ],
 }
 
+// GN: //protos/perfetto/config:descriptor
+genrule {
+  name: "perfetto_protos_perfetto_config_descriptor",
+  srcs: [
+    "protos/perfetto/config/trace_config.proto",
+  ],
+  tools: [
+    "aprotoc",
+  ],
+  cmd: "mkdir -p $(genDir)/external/perfetto/ && $(location aprotoc) --proto_path=external/perfetto --descriptor_set_out=$(out) --include_imports $(in)",
+  out: [
+    "perfetto_protos_perfetto_config_descriptor.bin",
+  ],
+}
+
 // GN: //protos/perfetto/config/ftrace:cpp
 genrule {
   name: "perfetto_protos_perfetto_config_ftrace_cpp_gen",
@@ -6808,6 +6823,21 @@
   ],
 }
 
+// GN: //src/trace_processor/importers:gen_cc_config_descriptor
+genrule {
+  name: "perfetto_src_trace_processor_importers_gen_cc_config_descriptor",
+  srcs: [
+    ":perfetto_protos_perfetto_config_descriptor",
+  ],
+  cmd: "$(location tools/gen_cc_proto_descriptor.py) --gen_dir=$(genDir) --cpp_out=$(out) $(in)",
+  out: [
+    "src/trace_processor/importers/config.descriptor.h",
+  ],
+  tool_files: [
+    "tools/gen_cc_proto_descriptor.py",
+  ],
+}
+
 // GN: //src/trace_processor/importers/memory_tracker:graph_processor
 filegroup {
   name: "perfetto_src_trace_processor_importers_memory_tracker_graph_processor",
@@ -8224,6 +8254,7 @@
     "perfetto_src_protozero_testing_messages_cpp_gen_headers",
     "perfetto_src_protozero_testing_messages_lite_gen_headers",
     "perfetto_src_protozero_testing_messages_zero_gen_headers",
+    "perfetto_src_trace_processor_importers_gen_cc_config_descriptor",
     "perfetto_src_trace_processor_metrics_gen_cc_all_chrome_metrics_descriptor",
     "perfetto_src_trace_processor_metrics_gen_cc_metrics_descriptor",
     "perfetto_src_trace_processor_metrics_gen_merged_sql_metrics",
@@ -8368,6 +8399,7 @@
     "perfetto_protos_perfetto_trace_sys_stats_zero_gen_headers",
     "perfetto_protos_perfetto_trace_system_info_zero_gen_headers",
     "perfetto_protos_perfetto_trace_track_event_zero_gen_headers",
+    "perfetto_src_trace_processor_importers_gen_cc_config_descriptor",
     "perfetto_src_trace_processor_metrics_gen_cc_all_chrome_metrics_descriptor",
     "perfetto_src_trace_processor_metrics_gen_cc_metrics_descriptor",
     "perfetto_src_trace_processor_metrics_gen_merged_sql_metrics",
@@ -8509,6 +8541,7 @@
     "perfetto_protos_perfetto_trace_system_info_zero_gen_headers",
     "perfetto_protos_perfetto_trace_track_event_zero_gen_headers",
     "perfetto_protos_third_party_pprof_zero_gen_headers",
+    "perfetto_src_trace_processor_importers_gen_cc_config_descriptor",
     "perfetto_src_trace_processor_metrics_gen_cc_all_chrome_metrics_descriptor",
     "perfetto_src_trace_processor_metrics_gen_cc_metrics_descriptor",
     "perfetto_src_trace_processor_metrics_gen_merged_sql_metrics",
diff --git a/BUILD b/BUILD
index 3ddec7a..920ce21 100644
--- a/BUILD
+++ b/BUILD
@@ -747,6 +747,16 @@
 )
 
 perfetto_cc_proto_descriptor(
+    name = "src_trace_processor_importers_gen_cc_config_descriptor",
+    deps = [
+        ":protos_perfetto_config_descriptor",
+    ],
+    outs = [
+        "src/trace_processor/importers/config.descriptor.h",
+    ],
+)
+
+perfetto_cc_proto_descriptor(
     name = "src_trace_processor_metrics_gen_cc_all_chrome_metrics_descriptor",
     deps = [
         ":protos_perfetto_metrics_chrome_descriptor",
@@ -1633,6 +1643,17 @@
     ],
 )
 
+# GN target: //protos/perfetto/config:descriptor
+perfetto_proto_descriptor(
+    name = "protos_perfetto_config_descriptor",
+    deps = [
+        ":protos_perfetto_config_protos",
+    ],
+    outs = [
+        "protos_perfetto_config_descriptor.bin",
+    ],
+)
+
 # GN target: //protos/perfetto/config/ftrace:cpp
 perfetto_cc_protocpp_library(
     name = "protos_perfetto_config_ftrace_cpp",
@@ -3049,6 +3070,7 @@
                ":protos_perfetto_trace_sys_stats_zero",
                ":protos_perfetto_trace_system_info_zero",
                ":protos_perfetto_trace_track_event_zero",
+               ":src_trace_processor_importers_gen_cc_config_descriptor",
                ":src_trace_processor_metrics_gen_cc_all_chrome_metrics_descriptor",
                ":src_trace_processor_metrics_gen_cc_metrics_descriptor",
            ] + PERFETTO_CONFIG.deps.jsoncpp +
@@ -3134,6 +3156,7 @@
                ":protos_perfetto_trace_sys_stats_zero",
                ":protos_perfetto_trace_system_info_zero",
                ":protos_perfetto_trace_track_event_zero",
+               ":src_trace_processor_importers_gen_cc_config_descriptor",
                ":src_trace_processor_metrics_gen_cc_all_chrome_metrics_descriptor",
                ":src_trace_processor_metrics_gen_cc_metrics_descriptor",
            ] + PERFETTO_CONFIG.deps.jsoncpp +
@@ -3307,6 +3330,7 @@
                ":protos_perfetto_trace_system_info_zero",
                ":protos_perfetto_trace_track_event_zero",
                ":protos_third_party_pprof_zero",
+               ":src_trace_processor_importers_gen_cc_config_descriptor",
                ":src_trace_processor_metrics_gen_cc_all_chrome_metrics_descriptor",
                ":src_trace_processor_metrics_gen_cc_metrics_descriptor",
            ] + PERFETTO_CONFIG.deps.jsoncpp +
diff --git a/protos/perfetto/config/BUILD.gn b/protos/perfetto/config/BUILD.gn
index b17b961..b765191 100644
--- a/protos/perfetto/config/BUILD.gn
+++ b/protos/perfetto/config/BUILD.gn
@@ -49,3 +49,10 @@
   proto_generators = [ "lite" ]
   sources = [ "perfetto_config.proto" ]
 }
+
+perfetto_proto_library("descriptor") {
+  proto_generators = [ "descriptor" ]
+  generate_descriptor = "config.descriptor"
+  deps = [ ":source_set" ]
+  sources = [ "trace_config.proto" ]
+}
diff --git a/src/trace_processor/importers/BUILD.gn b/src/trace_processor/importers/BUILD.gn
index 8529ee8..0d92555 100644
--- a/src/trace_processor/importers/BUILD.gn
+++ b/src/trace_processor/importers/BUILD.gn
@@ -35,6 +35,10 @@
     "common/track_tracker.cc",
     "common/track_tracker.h",
   ]
+  public_deps = [
+    ":gen_cc_config_descriptor",
+    "../util:protozero_to_text",
+  ]
   deps = [
     "../../../gn:default_deps",
     "../../../protos/perfetto/common:zero",
@@ -66,3 +70,28 @@
     "../types",
   ]
 }
+
+config("gen_config") {
+  include_dirs = [ "${root_gen_dir}/${perfetto_root_path}" ]
+}
+
+action("gen_cc_config_descriptor") {
+  descriptor_target = "../../../../protos/perfetto/config:descriptor"
+  generated_header = "${target_gen_dir}/config.descriptor.h"
+
+  descriptor_file_path =
+      get_label_info(descriptor_target, "target_gen_dir") + "/config.descriptor"
+
+  script = "../../../tools/gen_cc_proto_descriptor.py"
+  deps = [ descriptor_target ]
+  args = [
+    "--gen_dir",
+    rebase_path(root_gen_dir, root_build_dir),
+    "--cpp_out",
+    rebase_path(generated_header, root_build_dir),
+    rebase_path(descriptor_file_path, root_build_dir),
+  ]
+  inputs = [ descriptor_file_path ]
+  outputs = [ generated_header ]
+  public_configs = [ ":gen_config" ]
+}
diff --git a/src/trace_processor/importers/proto/metadata_tracker.cc b/src/trace_processor/importers/proto/metadata_tracker.cc
index 0162392..64f68e8 100644
--- a/src/trace_processor/importers/proto/metadata_tracker.cc
+++ b/src/trace_processor/importers/proto/metadata_tracker.cc
@@ -55,6 +55,17 @@
   return id_and_row.id;
 }
 
+SqlValue MetadataTracker::GetMetadataForTesting(metadata::KeyId key) {
+  // KeyType::kMulti not yet supported by this method:
+  PERFETTO_CHECK(metadata::kKeyTypes[key] == metadata::KeyType::kSingle);
+
+  auto* metadata_table = context_->storage->mutable_metadata_table();
+  uint32_t key_idx = static_cast<uint32_t>(key);
+  uint32_t row =
+      metadata_table->name().IndexOf(metadata::kNames[key_idx]).value();
+  return metadata_table->mutable_str_value()->Get(row);
+}
+
 MetadataId MetadataTracker::AppendMetadata(metadata::KeyId key,
                                            Variadic value) {
   PERFETTO_DCHECK(key < metadata::kNumKeys);
diff --git a/src/trace_processor/importers/proto/metadata_tracker.h b/src/trace_processor/importers/proto/metadata_tracker.h
index fc8bba2..8e8e0ef 100644
--- a/src/trace_processor/importers/proto/metadata_tracker.h
+++ b/src/trace_processor/importers/proto/metadata_tracker.h
@@ -41,6 +41,10 @@
   // Returns the id of the new entry.
   MetadataId AppendMetadata(metadata::KeyId key, Variadic value);
 
+  // Reads back a set metadata value.
+  // For use in tests only.
+  SqlValue GetMetadataForTesting(metadata::KeyId key);
+
  private:
   static constexpr size_t kNumKeys =
       static_cast<size_t>(metadata::KeyId::kNumKeys);
diff --git a/src/trace_processor/importers/proto/proto_trace_parser.cc b/src/trace_processor/importers/proto/proto_trace_parser.cc
index b05846b..e455669 100644
--- a/src/trace_processor/importers/proto/proto_trace_parser.cc
+++ b/src/trace_processor/importers/proto/proto_trace_parser.cc
@@ -36,6 +36,7 @@
 #include "src/trace_processor/importers/common/process_tracker.h"
 #include "src/trace_processor/importers/common/slice_tracker.h"
 #include "src/trace_processor/importers/common/track_tracker.h"
+#include "src/trace_processor/importers/config.descriptor.h"
 #include "src/trace_processor/importers/ftrace/ftrace_module.h"
 #include "src/trace_processor/importers/proto/heap_profile_tracker.h"
 #include "src/trace_processor/importers/proto/metadata_tracker.h"
@@ -48,6 +49,8 @@
 #include "src/trace_processor/timestamped_trace_piece.h"
 #include "src/trace_processor/types/trace_processor_context.h"
 #include "src/trace_processor/types/variadic.h"
+#include "src/trace_processor/util/descriptors.h"
+#include "src/trace_processor/util/protozero_to_text.h"
 
 #include "protos/perfetto/common/builtin_clock.pbzero.h"
 #include "protos/perfetto/common/trace_stats.pbzero.h"
@@ -730,6 +733,17 @@
     context_->metadata_tracker->SetMetadata(metadata::unique_session_name,
                                             Variadic::String(id));
   }
+
+  DescriptorPool pool;
+  pool.AddFromFileDescriptorSet(kConfigDescriptor.data(),
+                                kConfigDescriptor.size());
+
+  std::string text = protozero_to_text::ProtozeroToText(
+      pool, ".perfetto.protos.TraceConfig", blob,
+      protozero_to_text::kIncludeNewLines);
+  StringId id = context_->storage->InternString(base::StringView(text));
+  context_->metadata_tracker->SetMetadata(metadata::trace_config_pbtxt,
+                                          Variadic::String(id));
 }
 
 void ProtoTraceParser::ParseModuleSymbols(ConstBytes blob) {
diff --git a/src/trace_processor/importers/proto/proto_trace_parser_unittest.cc b/src/trace_processor/importers/proto/proto_trace_parser_unittest.cc
index 0270c9f..d72fa70 100644
--- a/src/trace_processor/importers/proto/proto_trace_parser_unittest.cc
+++ b/src/trace_processor/importers/proto/proto_trace_parser_unittest.cc
@@ -40,6 +40,7 @@
 
 #include "protos/perfetto/common/builtin_clock.pbzero.h"
 #include "protos/perfetto/common/sys_stats_counters.pbzero.h"
+#include "protos/perfetto/config/trace_config.pbzero.h"
 #include "protos/perfetto/trace/android/packages_list.pbzero.h"
 #include "protos/perfetto/trace/chrome/chrome_benchmark_metadata.pbzero.h"
 #include "protos/perfetto/trace/chrome/chrome_trace_event.pbzero.h"
@@ -76,6 +77,7 @@
 using ::testing::AtLeast;
 using ::testing::ElementsAreArray;
 using ::testing::Eq;
+using ::testing::HasSubstr;
 using ::testing::InSequence;
 using ::testing::Invoke;
 using ::testing::InvokeArgument;
@@ -2825,6 +2827,29 @@
   EXPECT_EQ(samples.utid()[0], 1u);
 }
 
+TEST_F(ProtoTraceParserTest, ConfigUuid) {
+  auto* config = trace_->add_packet()->set_trace_config();
+  config->set_trace_uuid_lsb(1);
+  config->set_trace_uuid_msb(2);
+
+  ASSERT_TRUE(Tokenize().ok());
+
+  SqlValue value =
+      context_.metadata_tracker->GetMetadataForTesting(metadata::trace_uuid);
+  EXPECT_STREQ(value.string_value, "00000000-0000-0002-0000-000000000001");
+}
+
+TEST_F(ProtoTraceParserTest, ConfigPbtxt) {
+  auto* config = trace_->add_packet()->set_trace_config();
+  config->add_buffers()->set_size_kb(42);
+
+  ASSERT_TRUE(Tokenize().ok());
+
+  SqlValue value = context_.metadata_tracker->GetMetadataForTesting(
+      metadata::trace_config_pbtxt);
+  EXPECT_THAT(value.string_value, HasSubstr("size_kb: 42"));
+}
+
 }  // namespace
 }  // namespace trace_processor
 }  // namespace perfetto
diff --git a/src/trace_processor/storage/metadata.h b/src/trace_processor/storage/metadata.h
index 4796ae4..9351be1 100644
--- a/src/trace_processor/storage/metadata.h
+++ b/src/trace_processor/storage/metadata.h
@@ -49,7 +49,8 @@
   F(trace_size_bytes,                  KeyType::kSingle,  Variadic::kInt),    \
   F(all_data_source_started_ns,        KeyType::kSingle,  Variadic::kInt),    \
   F(tracing_started_ns,                KeyType::kSingle,  Variadic::kInt),    \
-  F(tracing_disabled_ns,               KeyType::kSingle,  Variadic::kInt)
+  F(tracing_disabled_ns,               KeyType::kSingle,  Variadic::kInt),    \
+  F(trace_config_pbtxt,                KeyType::kSingle, Variadic::kString)
 // clang-format on
 
 // Compile time list of metadata items.
diff --git a/test/trace_processor/parsing/config_metadata.out b/test/trace_processor/parsing/config_metadata.out
index edcba50..3a77f45 100644
--- a/test/trace_processor/parsing/config_metadata.out
+++ b/test/trace_processor/parsing/config_metadata.out
@@ -1,3 +1,5 @@
 "name","str_value"
 "android_build_fingerprint","the fingerprint"
+"trace_config_pbtxt","trace_uuid_msb: 1314564453825188563
+trace_uuid_lsb: -6605018796207623390"
 "trace_uuid","123e4567-e89b-12d3-a456-426655443322"
diff --git a/tools/gen_android_bp b/tools/gen_android_bp
index e8a7c9c..d3da87b 100755
--- a/tools/gen_android_bp
+++ b/tools/gen_android_bp
@@ -782,9 +782,10 @@
   elif target.type == 'action':
     if 'gen_merged_sql_metrics' in target.name:
       module = create_merged_sql_metrics_module(blueprint, target)
-    elif 'gen_cc_metrics_descriptor' in target.name or \
-         'gen_cc_all_chrome_metrics_descriptor' in target.name:
+    elif re.match('.*gen_cc_.*_descriptor$', target.name):
       module = create_cc_proto_descriptor_module(blueprint, target)
+    else:
+      raise Error('Unhandled action: {}'.format(target.name))
   else:
     raise Error('Unknown target %s (%s)' % (target.name, target.type))
 
diff --git a/tools/gen_bazel b/tools/gen_bazel
index 65f97d0..08884e6 100755
--- a/tools/gen_bazel
+++ b/tools/gen_bazel
@@ -133,6 +133,8 @@
       gen_cc_metrics_descriptor,
     '//src/trace_processor/metrics:gen_cc_all_chrome_metrics_descriptor':
       gen_cc_metrics_descriptor,
+    '//src/trace_processor/importers:gen_cc_config_descriptor':
+      gen_cc_metrics_descriptor,
 }
 
 # ------------------------------------------------------------------------------
diff --git a/ui/src/assets/trace_info_page.scss b/ui/src/assets/trace_info_page.scss
index baa1263..38c6f9d 100644
--- a/ui/src/assets/trace_info_page.scss
+++ b/ui/src/assets/trace_info_page.scss
@@ -82,6 +82,7 @@
           font-size: 12px;
           padding: 5px;
           word-break: break-all;
+          white-space: pre-wrap;
           &:first-of-type {
             font-weight: 800;
           }