Reduce entropy in tools/trace_to_text

- Use protozero instead of libprotobuf for symbolization
  and deobfuscation.
- Remove unnecessary exposure of TracePacket class and just
  pass strings instead.
- Bring sanity to dependencies in BUILD.gn, everything ended
  up in public_deps for no obvious reason.

Bug: 132880619
Change-Id: I69be23a4ee7790eb61eebf14f0b412e5487a5d32
diff --git a/Android.bp b/Android.bp
index 6cf539f..ef50e19 100644
--- a/Android.bp
+++ b/Android.bp
@@ -4067,33 +4067,35 @@
   ],
 }
 
-// GN: //protos/third_party/pprof:lite
+// GN: //protos/third_party/pprof:zero
 genrule {
-  name: "perfetto_protos_third_party_pprof_lite_gen",
+  name: "perfetto_protos_third_party_pprof_zero_gen",
   srcs: [
     "protos/third_party/pprof/profile.proto",
   ],
   tools: [
     "aprotoc",
+    "protozero_plugin",
   ],
-  cmd: "mkdir -p $(genDir)/external/perfetto/ && $(location aprotoc) --proto_path=external/perfetto --cpp_out=$(genDir)/external/perfetto/ $(in)",
+  cmd: "mkdir -p $(genDir)/external/perfetto/ && $(location aprotoc) --proto_path=external/perfetto --plugin=protoc-gen-plugin=$(location protozero_plugin) --plugin_out=wrapper_namespace=pbzero:$(genDir)/external/perfetto/ $(in)",
   out: [
-    "external/perfetto/protos/third_party/pprof/profile.pb.cc",
+    "external/perfetto/protos/third_party/pprof/profile.pbzero.cc",
   ],
 }
 
-// GN: //protos/third_party/pprof:lite
+// GN: //protos/third_party/pprof:zero
 genrule {
-  name: "perfetto_protos_third_party_pprof_lite_gen_headers",
+  name: "perfetto_protos_third_party_pprof_zero_gen_headers",
   srcs: [
     "protos/third_party/pprof/profile.proto",
   ],
   tools: [
     "aprotoc",
+    "protozero_plugin",
   ],
-  cmd: "mkdir -p $(genDir)/external/perfetto/ && $(location aprotoc) --proto_path=external/perfetto --cpp_out=$(genDir)/external/perfetto/ $(in)",
+  cmd: "mkdir -p $(genDir)/external/perfetto/ && $(location aprotoc) --proto_path=external/perfetto --plugin=protoc-gen-plugin=$(location protozero_plugin) --plugin_out=wrapper_namespace=pbzero:$(genDir)/external/perfetto/ $(in)",
   out: [
-    "external/perfetto/protos/third_party/pprof/profile.pb.h",
+    "external/perfetto/protos/third_party/pprof/profile.pbzero.h",
   ],
   export_include_dirs: [
     ".",
@@ -5921,58 +5923,34 @@
     ":perfetto_include_perfetto_trace_processor_basic_types",
     ":perfetto_include_perfetto_trace_processor_storage",
     ":perfetto_include_perfetto_trace_processor_trace_processor",
-    ":perfetto_protos_perfetto_common_lite_gen",
     ":perfetto_protos_perfetto_common_zero_gen",
-    ":perfetto_protos_perfetto_config_android_lite_gen",
     ":perfetto_protos_perfetto_config_android_zero_gen",
-    ":perfetto_protos_perfetto_config_ftrace_lite_gen",
     ":perfetto_protos_perfetto_config_ftrace_zero_gen",
-    ":perfetto_protos_perfetto_config_gpu_lite_gen",
     ":perfetto_protos_perfetto_config_gpu_zero_gen",
-    ":perfetto_protos_perfetto_config_inode_file_lite_gen",
     ":perfetto_protos_perfetto_config_inode_file_zero_gen",
-    ":perfetto_protos_perfetto_config_lite_gen",
-    ":perfetto_protos_perfetto_config_power_lite_gen",
     ":perfetto_protos_perfetto_config_power_zero_gen",
-    ":perfetto_protos_perfetto_config_process_stats_lite_gen",
     ":perfetto_protos_perfetto_config_process_stats_zero_gen",
-    ":perfetto_protos_perfetto_config_profiling_lite_gen",
     ":perfetto_protos_perfetto_config_profiling_zero_gen",
-    ":perfetto_protos_perfetto_config_sys_stats_lite_gen",
     ":perfetto_protos_perfetto_config_sys_stats_zero_gen",
     ":perfetto_protos_perfetto_config_zero_gen",
     ":perfetto_protos_perfetto_metrics_android_zero_gen",
     ":perfetto_protos_perfetto_metrics_zero_gen",
-    ":perfetto_protos_perfetto_trace_android_lite_gen",
     ":perfetto_protos_perfetto_trace_android_zero_gen",
-    ":perfetto_protos_perfetto_trace_chrome_lite_gen",
     ":perfetto_protos_perfetto_trace_chrome_zero_gen",
-    ":perfetto_protos_perfetto_trace_filesystem_lite_gen",
     ":perfetto_protos_perfetto_trace_filesystem_zero_gen",
-    ":perfetto_protos_perfetto_trace_ftrace_lite_gen",
     ":perfetto_protos_perfetto_trace_ftrace_zero_gen",
-    ":perfetto_protos_perfetto_trace_gpu_lite_gen",
     ":perfetto_protos_perfetto_trace_gpu_zero_gen",
-    ":perfetto_protos_perfetto_trace_interned_data_lite_gen",
     ":perfetto_protos_perfetto_trace_interned_data_zero_gen",
-    ":perfetto_protos_perfetto_trace_minimal_lite_gen",
     ":perfetto_protos_perfetto_trace_minimal_zero_gen",
-    ":perfetto_protos_perfetto_trace_non_minimal_lite_gen",
     ":perfetto_protos_perfetto_trace_non_minimal_zero_gen",
-    ":perfetto_protos_perfetto_trace_perfetto_lite_gen",
     ":perfetto_protos_perfetto_trace_perfetto_zero_gen",
-    ":perfetto_protos_perfetto_trace_power_lite_gen",
     ":perfetto_protos_perfetto_trace_power_zero_gen",
     ":perfetto_protos_perfetto_trace_processor_metrics_impl_zero_gen",
-    ":perfetto_protos_perfetto_trace_profiling_lite_gen",
     ":perfetto_protos_perfetto_trace_profiling_zero_gen",
-    ":perfetto_protos_perfetto_trace_ps_lite_gen",
     ":perfetto_protos_perfetto_trace_ps_zero_gen",
-    ":perfetto_protos_perfetto_trace_sys_stats_lite_gen",
     ":perfetto_protos_perfetto_trace_sys_stats_zero_gen",
-    ":perfetto_protos_perfetto_trace_track_event_lite_gen",
     ":perfetto_protos_perfetto_trace_track_event_zero_gen",
-    ":perfetto_protos_third_party_pprof_lite_gen",
+    ":perfetto_protos_third_party_pprof_zero_gen",
     ":perfetto_src_base_base",
     ":perfetto_src_profiling_deobfuscator",
     ":perfetto_src_protozero_protozero",
@@ -5992,7 +5970,6 @@
   ],
   shared_libs: [
     "libprotobuf-cpp-full",
-    "libprotobuf-cpp-lite",
   ],
   static_libs: [
     "libsqlite",
@@ -6000,58 +5977,34 @@
   ],
   generated_headers: [
     "gen_merged_sql_metrics",
-    "perfetto_protos_perfetto_common_lite_gen_headers",
     "perfetto_protos_perfetto_common_zero_gen_headers",
-    "perfetto_protos_perfetto_config_android_lite_gen_headers",
     "perfetto_protos_perfetto_config_android_zero_gen_headers",
-    "perfetto_protos_perfetto_config_ftrace_lite_gen_headers",
     "perfetto_protos_perfetto_config_ftrace_zero_gen_headers",
-    "perfetto_protos_perfetto_config_gpu_lite_gen_headers",
     "perfetto_protos_perfetto_config_gpu_zero_gen_headers",
-    "perfetto_protos_perfetto_config_inode_file_lite_gen_headers",
     "perfetto_protos_perfetto_config_inode_file_zero_gen_headers",
-    "perfetto_protos_perfetto_config_lite_gen_headers",
-    "perfetto_protos_perfetto_config_power_lite_gen_headers",
     "perfetto_protos_perfetto_config_power_zero_gen_headers",
-    "perfetto_protos_perfetto_config_process_stats_lite_gen_headers",
     "perfetto_protos_perfetto_config_process_stats_zero_gen_headers",
-    "perfetto_protos_perfetto_config_profiling_lite_gen_headers",
     "perfetto_protos_perfetto_config_profiling_zero_gen_headers",
-    "perfetto_protos_perfetto_config_sys_stats_lite_gen_headers",
     "perfetto_protos_perfetto_config_sys_stats_zero_gen_headers",
     "perfetto_protos_perfetto_config_zero_gen_headers",
     "perfetto_protos_perfetto_metrics_android_zero_gen_headers",
     "perfetto_protos_perfetto_metrics_zero_gen_headers",
-    "perfetto_protos_perfetto_trace_android_lite_gen_headers",
     "perfetto_protos_perfetto_trace_android_zero_gen_headers",
-    "perfetto_protos_perfetto_trace_chrome_lite_gen_headers",
     "perfetto_protos_perfetto_trace_chrome_zero_gen_headers",
-    "perfetto_protos_perfetto_trace_filesystem_lite_gen_headers",
     "perfetto_protos_perfetto_trace_filesystem_zero_gen_headers",
-    "perfetto_protos_perfetto_trace_ftrace_lite_gen_headers",
     "perfetto_protos_perfetto_trace_ftrace_zero_gen_headers",
-    "perfetto_protos_perfetto_trace_gpu_lite_gen_headers",
     "perfetto_protos_perfetto_trace_gpu_zero_gen_headers",
-    "perfetto_protos_perfetto_trace_interned_data_lite_gen_headers",
     "perfetto_protos_perfetto_trace_interned_data_zero_gen_headers",
-    "perfetto_protos_perfetto_trace_minimal_lite_gen_headers",
     "perfetto_protos_perfetto_trace_minimal_zero_gen_headers",
-    "perfetto_protos_perfetto_trace_non_minimal_lite_gen_headers",
     "perfetto_protos_perfetto_trace_non_minimal_zero_gen_headers",
-    "perfetto_protos_perfetto_trace_perfetto_lite_gen_headers",
     "perfetto_protos_perfetto_trace_perfetto_zero_gen_headers",
-    "perfetto_protos_perfetto_trace_power_lite_gen_headers",
     "perfetto_protos_perfetto_trace_power_zero_gen_headers",
     "perfetto_protos_perfetto_trace_processor_metrics_impl_zero_gen_headers",
-    "perfetto_protos_perfetto_trace_profiling_lite_gen_headers",
     "perfetto_protos_perfetto_trace_profiling_zero_gen_headers",
-    "perfetto_protos_perfetto_trace_ps_lite_gen_headers",
     "perfetto_protos_perfetto_trace_ps_zero_gen_headers",
-    "perfetto_protos_perfetto_trace_sys_stats_lite_gen_headers",
     "perfetto_protos_perfetto_trace_sys_stats_zero_gen_headers",
-    "perfetto_protos_perfetto_trace_track_event_lite_gen_headers",
     "perfetto_protos_perfetto_trace_track_event_zero_gen_headers",
-    "perfetto_protos_third_party_pprof_lite_gen_headers",
+    "perfetto_protos_third_party_pprof_zero_gen_headers",
   ],
   defaults: [
     "perfetto_defaults",
diff --git a/BUILD b/BUILD
index dae81c7..b9f0954 100644
--- a/BUILD
+++ b/BUILD
@@ -2296,15 +2296,7 @@
     ],
 )
 
-# GN target: //protos/third_party/pprof:lite
-perfetto_cc_proto_library(
-    name = "protos_third_party_pprof_lite",
-    deps = [
-        ":protos_third_party_pprof_protos",
-    ],
-)
-
-# GN target: //protos/third_party/pprof:lite
+# GN target: //protos/third_party/pprof:zero
 perfetto_proto_library(
     name = "protos_third_party_pprof_protos",
     srcs = [
@@ -2315,6 +2307,14 @@
     ],
 )
 
+# GN target: //protos/third_party/pprof:zero
+perfetto_cc_protozero_library(
+    name = "protos_third_party_pprof_zero",
+    deps = [
+        ":protos_third_party_pprof_protos",
+    ],
+)
+
 # GN target: //src/perfetto_cmd:protos
 perfetto_cc_proto_library(
     name = "src_perfetto_cmd_protos",
@@ -2672,17 +2672,7 @@
 perfetto_cc_library(
     name = "libpprofbuilder",
     srcs = [
-        ":src_base_base",
         ":src_profiling_deobfuscator",
-        ":src_protozero_protozero",
-        ":src_trace_processor_common",
-        ":src_trace_processor_db_lib",
-        ":src_trace_processor_export_json",
-        ":src_trace_processor_lib",
-        ":src_trace_processor_metrics_lib",
-        ":src_trace_processor_sqlite_sqlite",
-        ":src_trace_processor_storage",
-        ":src_trace_processor_tables_tables",
         ":tools_trace_to_text_pprofbuilder",
         ":tools_trace_to_text_symbolizer",
         ":tools_trace_to_text_utils",
@@ -2690,8 +2680,6 @@
     hdrs = [
         ":include_perfetto_base_base",
         ":include_perfetto_ext_base_base",
-        ":include_perfetto_ext_trace_processor_export_json",
-        ":include_perfetto_ext_traced_sys_stats_counters",
         ":include_perfetto_profiling_deobfuscator",
         ":include_perfetto_profiling_symbolizer",
         ":include_perfetto_protozero_protozero",
@@ -2703,64 +2691,32 @@
         "//visibility:public",
     ],
     deps = [
-               ":protos_perfetto_common_lite",
-               ":protos_perfetto_common_zero",
-               ":protos_perfetto_config_android_lite",
-               ":protos_perfetto_config_android_zero",
-               ":protos_perfetto_config_ftrace_lite",
-               ":protos_perfetto_config_ftrace_zero",
-               ":protos_perfetto_config_gpu_lite",
-               ":protos_perfetto_config_gpu_zero",
-               ":protos_perfetto_config_inode_file_lite",
-               ":protos_perfetto_config_inode_file_zero",
-               ":protos_perfetto_config_lite",
-               ":protos_perfetto_config_power_lite",
-               ":protos_perfetto_config_power_zero",
-               ":protos_perfetto_config_process_stats_lite",
-               ":protos_perfetto_config_process_stats_zero",
-               ":protos_perfetto_config_profiling_lite",
-               ":protos_perfetto_config_profiling_zero",
-               ":protos_perfetto_config_sys_stats_lite",
-               ":protos_perfetto_config_sys_stats_zero",
-               ":protos_perfetto_config_zero",
-               ":protos_perfetto_metrics_android_zero",
-               ":protos_perfetto_metrics_zero",
-               ":protos_perfetto_trace_android_lite",
-               ":protos_perfetto_trace_android_zero",
-               ":protos_perfetto_trace_chrome_lite",
-               ":protos_perfetto_trace_chrome_zero",
-               ":protos_perfetto_trace_filesystem_lite",
-               ":protos_perfetto_trace_filesystem_zero",
-               ":protos_perfetto_trace_ftrace_lite",
-               ":protos_perfetto_trace_ftrace_zero",
-               ":protos_perfetto_trace_gpu_lite",
-               ":protos_perfetto_trace_gpu_zero",
-               ":protos_perfetto_trace_interned_data_lite",
-               ":protos_perfetto_trace_interned_data_zero",
-               ":protos_perfetto_trace_minimal_lite",
-               ":protos_perfetto_trace_minimal_zero",
-               ":protos_perfetto_trace_non_minimal_lite",
-               ":protos_perfetto_trace_non_minimal_zero",
-               ":protos_perfetto_trace_perfetto_lite",
-               ":protos_perfetto_trace_perfetto_zero",
-               ":protos_perfetto_trace_power_lite",
-               ":protos_perfetto_trace_power_zero",
-               ":protos_perfetto_trace_processor_metrics_impl_zero",
-               ":protos_perfetto_trace_profiling_lite",
-               ":protos_perfetto_trace_profiling_zero",
-               ":protos_perfetto_trace_ps_lite",
-               ":protos_perfetto_trace_ps_zero",
-               ":protos_perfetto_trace_sys_stats_lite",
-               ":protos_perfetto_trace_sys_stats_zero",
-               ":protos_perfetto_trace_track_event_lite",
-               ":protos_perfetto_trace_track_event_zero",
-               ":protos_third_party_pprof_lite",
-           ] + PERFETTO_CONFIG.deps.jsoncpp +
-           PERFETTO_CONFIG.deps.sqlite +
-           PERFETTO_CONFIG.deps.sqlite_ext_percentile +
-           PERFETTO_CONFIG.deps.zlib + [
-        ":cc_merged_sql_metrics",
-    ],
+        ":protos_perfetto_common_zero",
+        ":protos_perfetto_config_android_zero",
+        ":protos_perfetto_config_ftrace_zero",
+        ":protos_perfetto_config_gpu_zero",
+        ":protos_perfetto_config_inode_file_zero",
+        ":protos_perfetto_config_power_zero",
+        ":protos_perfetto_config_process_stats_zero",
+        ":protos_perfetto_config_profiling_zero",
+        ":protos_perfetto_config_sys_stats_zero",
+        ":protos_perfetto_config_zero",
+        ":protos_perfetto_trace_android_zero",
+        ":protos_perfetto_trace_chrome_zero",
+        ":protos_perfetto_trace_filesystem_zero",
+        ":protos_perfetto_trace_ftrace_zero",
+        ":protos_perfetto_trace_gpu_zero",
+        ":protos_perfetto_trace_interned_data_zero",
+        ":protos_perfetto_trace_minimal_zero",
+        ":protos_perfetto_trace_non_minimal_zero",
+        ":protos_perfetto_trace_perfetto_zero",
+        ":protos_perfetto_trace_power_zero",
+        ":protos_perfetto_trace_profiling_zero",
+        ":protos_perfetto_trace_ps_zero",
+        ":protos_perfetto_trace_sys_stats_zero",
+        ":protos_perfetto_trace_track_event_zero",
+        ":protos_third_party_pprof_zero",
+    ] + PERFETTO_CONFIG.deps.zlib,
 )
 
 # GN target: //tools/trace_to_text:trace_to_text
@@ -2799,58 +2755,34 @@
         "//visibility:public",
     ],
     deps = [
-               ":protos_perfetto_common_lite",
                ":protos_perfetto_common_zero",
-               ":protos_perfetto_config_android_lite",
                ":protos_perfetto_config_android_zero",
-               ":protos_perfetto_config_ftrace_lite",
                ":protos_perfetto_config_ftrace_zero",
-               ":protos_perfetto_config_gpu_lite",
                ":protos_perfetto_config_gpu_zero",
-               ":protos_perfetto_config_inode_file_lite",
                ":protos_perfetto_config_inode_file_zero",
-               ":protos_perfetto_config_lite",
-               ":protos_perfetto_config_power_lite",
                ":protos_perfetto_config_power_zero",
-               ":protos_perfetto_config_process_stats_lite",
                ":protos_perfetto_config_process_stats_zero",
-               ":protos_perfetto_config_profiling_lite",
                ":protos_perfetto_config_profiling_zero",
-               ":protos_perfetto_config_sys_stats_lite",
                ":protos_perfetto_config_sys_stats_zero",
                ":protos_perfetto_config_zero",
                ":protos_perfetto_metrics_android_zero",
                ":protos_perfetto_metrics_zero",
-               ":protos_perfetto_trace_android_lite",
                ":protos_perfetto_trace_android_zero",
-               ":protos_perfetto_trace_chrome_lite",
                ":protos_perfetto_trace_chrome_zero",
-               ":protos_perfetto_trace_filesystem_lite",
                ":protos_perfetto_trace_filesystem_zero",
-               ":protos_perfetto_trace_ftrace_lite",
                ":protos_perfetto_trace_ftrace_zero",
-               ":protos_perfetto_trace_gpu_lite",
                ":protos_perfetto_trace_gpu_zero",
-               ":protos_perfetto_trace_interned_data_lite",
                ":protos_perfetto_trace_interned_data_zero",
-               ":protos_perfetto_trace_minimal_lite",
                ":protos_perfetto_trace_minimal_zero",
-               ":protos_perfetto_trace_non_minimal_lite",
                ":protos_perfetto_trace_non_minimal_zero",
-               ":protos_perfetto_trace_perfetto_lite",
                ":protos_perfetto_trace_perfetto_zero",
-               ":protos_perfetto_trace_power_lite",
                ":protos_perfetto_trace_power_zero",
                ":protos_perfetto_trace_processor_metrics_impl_zero",
-               ":protos_perfetto_trace_profiling_lite",
                ":protos_perfetto_trace_profiling_zero",
-               ":protos_perfetto_trace_ps_lite",
                ":protos_perfetto_trace_ps_zero",
-               ":protos_perfetto_trace_sys_stats_lite",
                ":protos_perfetto_trace_sys_stats_zero",
-               ":protos_perfetto_trace_track_event_lite",
                ":protos_perfetto_trace_track_event_zero",
-               ":protos_third_party_pprof_lite",
+               ":protos_third_party_pprof_zero",
            ] + PERFETTO_CONFIG.deps.jsoncpp +
            PERFETTO_CONFIG.deps.protobuf_full +
            PERFETTO_CONFIG.deps.sqlite +
diff --git a/include/perfetto/profiling/pprof_builder.h b/include/perfetto/profiling/pprof_builder.h
index cc61b68..4227a8c 100644
--- a/include/perfetto/profiling/pprof_builder.h
+++ b/include/perfetto/profiling/pprof_builder.h
@@ -21,9 +21,12 @@
 #include <string>
 #include <vector>
 
-#include "perfetto/trace_processor/trace_processor.h"
-
 namespace perfetto {
+
+namespace trace_processor {
+class TraceProcessor;
+}
+
 namespace trace_to_text {
 
 class Symbolizer;
diff --git a/protos/third_party/pprof/BUILD.gn b/protos/third_party/pprof/BUILD.gn
index 663ba44..18c24df 100644
--- a/protos/third_party/pprof/BUILD.gn
+++ b/protos/third_party/pprof/BUILD.gn
@@ -14,8 +14,8 @@
 
 import("../../../gn/proto_library.gni")
 
-perfetto_proto_library("lite") {
-  proto_generators = [ "lite" ]
+perfetto_proto_library("@TYPE@") {
+  proto_generators = [ "zero" ]
   sources = [
     "profile.proto",
   ]
diff --git a/tools/protoprofile/BUILD.gn b/tools/protoprofile/BUILD.gn
index fe31432..d23cbf8 100644
--- a/tools/protoprofile/BUILD.gn
+++ b/tools/protoprofile/BUILD.gn
@@ -26,7 +26,7 @@
   testonly = true
   public_deps = [
     "../../gn:default_deps",
-    "../../protos/third_party/pprof:lite",
+    "../../protos/third_party/pprof:zero",
     "../../src/base",
     "../../src/protozero",
   ]
diff --git a/tools/protoprofile/main.cc b/tools/protoprofile/main.cc
index f7bad27..8fe6f24 100644
--- a/tools/protoprofile/main.cc
+++ b/tools/protoprofile/main.cc
@@ -8,23 +8,18 @@
 #include "perfetto/ext/base/file_utils.h"
 #include "perfetto/ext/base/scoped_file.h"
 #include "perfetto/protozero/field.h"
+#include "perfetto/protozero/packed_repeated_fields.h"
 #include "perfetto/protozero/proto_decoder.h"
 #include "perfetto/protozero/proto_utils.h"
+#include "perfetto/protozero/scattered_heap_buffer.h"
 
-#include "protos/third_party/pprof/profile.pb.h"
+#include "protos/third_party/pprof/profile.pbzero.h"
 
 namespace perfetto {
 namespace protoprofile {
 namespace {
 
 using protozero::proto_utils::ProtoWireType;
-using GLine = ::perfetto::third_party::perftools::profiles::Line;
-using GMapping = ::perfetto::third_party::perftools::profiles::Mapping;
-using GLocation = ::perfetto::third_party::perftools::profiles::Location;
-using GProfile = ::perfetto::third_party::perftools::profiles::Profile;
-using GValueType = ::perfetto::third_party::perftools::profiles::ValueType;
-using GFunction = ::perfetto::third_party::perftools::profiles::Function;
-using GSample = ::perfetto::third_party::perftools::profiles::Sample;
 using ::google::protobuf::Descriptor;
 using ::google::protobuf::DynamicMessageFactory;
 using ::google::protobuf::FieldDescriptor;
@@ -68,9 +63,9 @@
 
 class SizeProfileComputer {
  public:
-  GProfile Compute(const uint8_t* ptr,
-                   size_t size,
-                   const Descriptor* descriptor);
+  std::string Compute(const uint8_t* ptr,
+                      size_t size,
+                      const Descriptor* descriptor);
 
  private:
   struct StackInfo {
@@ -168,32 +163,31 @@
   stack_info_[key].samples.push_back(size);
 }
 
-GProfile SizeProfileComputer::Compute(const uint8_t* ptr,
-                                      size_t size,
-                                      const Descriptor* descriptor) {
+std::string SizeProfileComputer::Compute(const uint8_t* ptr,
+                                         size_t size,
+                                         const Descriptor* descriptor) {
   PERFETTO_CHECK(InternString("") == 0);
   ComputeInner(ptr, size, descriptor);
-  GProfile profile;
+  protozero::HeapBuffered<third_party::perftools::profiles::pbzero::Profile>
+      profile;
 
-  GValueType* sample_type;
-
-  sample_type = profile.add_sample_type();
+  auto* sample_type = profile->add_sample_type();
   sample_type->set_type(InternString("protos"));
   sample_type->set_unit(InternString("count"));
 
-  sample_type = profile.add_sample_type();
+  sample_type = profile->add_sample_type();
   sample_type->set_type(InternString("max_size"));
   sample_type->set_unit(InternString("bytes"));
 
-  sample_type = profile.add_sample_type();
+  sample_type = profile->add_sample_type();
   sample_type->set_type(InternString("min_size"));
   sample_type->set_unit(InternString("bytes"));
 
-  sample_type = profile.add_sample_type();
+  sample_type = profile->add_sample_type();
   sample_type->set_type(InternString("median"));
   sample_type->set_unit(InternString("bytes"));
 
-  sample_type = profile.add_sample_type();
+  sample_type = profile->add_sample_type();
   sample_type->set_type(InternString("total_size"));
   sample_type->set_unit(InternString("bytes"));
 
@@ -201,10 +195,12 @@
   for (auto& id_info : stack_info_) {
     StackInfo& info = id_info.second;
 
-    GSample* sample = profile.add_sample();
+    protozero::PackedVarInt location_ids;
+    auto* sample = profile->add_sample();
     for (auto it = info.locations.rbegin(); it != info.locations.rend(); ++it) {
-      sample->add_location_id(static_cast<uint64_t>(*it));
+      location_ids.Append(static_cast<uint64_t>(*it));
     }
+    sample->set_location_id(location_ids);
 
     std::sort(info.samples.begin(), info.samples.end());
     size_t count = info.samples.size();
@@ -215,11 +211,13 @@
     for (size_t i = 0; i < count; ++i)
       total_size += info.samples[i];
     // These have to be in the same order as the sample types above:
-    sample->add_value(static_cast<int64_t>(count));
-    sample->add_value(static_cast<int64_t>(max_size));
-    sample->add_value(static_cast<int64_t>(min_size));
-    sample->add_value(static_cast<int64_t>(median_size));
-    sample->add_value(static_cast<int64_t>(total_size));
+    protozero::PackedVarInt values;
+    values.Append(static_cast<int64_t>(count));
+    values.Append(static_cast<int64_t>(max_size));
+    values.Append(static_cast<int64_t>(min_size));
+    values.Append(static_cast<int64_t>(median_size));
+    values.Append(static_cast<int64_t>(total_size));
+    sample->set_value(values);
   }
 
   // The proto profile has a two step mapping where samples are associated with
@@ -227,14 +225,14 @@
   // distinguish them so we make a 1:1 mapping between the locations and the
   // functions:
   for (const auto& location_id : locations_) {
-    GLocation* location = profile.add_location();
+    auto* location = profile->add_location();
     location->set_id(static_cast<uint64_t>(location_id.second));
-    GLine* line = location->add_line();
+    auto* line = location->add_line();
     line->set_function_id(static_cast<uint64_t>(location_id.second));
   }
 
   for (const auto& location_id : locations_) {
-    GFunction* function = profile.add_function();
+    auto* function = profile->add_function();
     function->set_id(static_cast<uint64_t>(location_id.second));
     function->set_name(InternString(location_id.first));
   }
@@ -242,9 +240,9 @@
   // Finally the string table. We intern more strings above, so this has to be
   // last.
   for (int i = 0; i < static_cast<int>(strings_.size()); i++) {
-    profile.add_string_table(strings_[static_cast<size_t>(i)]);
+    profile->add_string_table(strings_[static_cast<size_t>(i)]);
   }
-  return profile;
+  return profile.SerializeAsString();
 }
 
 void SizeProfileComputer::ComputeInner(const uint8_t* ptr,
@@ -349,9 +347,7 @@
     return 1;
   }
   SizeProfileComputer computer;
-  GProfile profile = computer.Compute(start, size, descriptor);
-  std::string out;
-  profile.SerializeToString(&out);
+  std::string out = computer.Compute(start, size, descriptor);
   base::WriteAll(output_fd.get(), out.data(), out.size());
   base::FlushFile(output_fd.get());
 
diff --git a/tools/trace_to_text/BUILD.gn b/tools/trace_to_text/BUILD.gn
index d7ae08f..4b2044c 100644
--- a/tools/trace_to_text/BUILD.gn
+++ b/tools/trace_to_text/BUILD.gn
@@ -39,20 +39,20 @@
 source_set("utils") {
   deps = [
     ":symbolizer",
+    "../../gn:default_deps",
     "../../include/perfetto/profiling:deobfuscator",
+    "../../include/perfetto/protozero",
+    "../../include/perfetto/trace_processor",
+    "../../protos/perfetto/trace:zero",
+    "../../protos/perfetto/trace/interned_data:zero",
+    "../../protos/perfetto/trace/profiling:zero",
     "../../src/profiling:deobfuscator",
   ]
   public_deps = [
-    "../../gn:default_deps",
     "../../gn:zlib",
-    "../../include/perfetto/base",
     "../../include/perfetto/ext/base",
-    "../../include/perfetto/ext/traced:sys_stats_counters",
-    "../../protos/perfetto/trace:lite",
-    "../../protos/perfetto/trace/ftrace:lite",
-    "../../protos/perfetto/trace/interned_data:lite",
-    "../../protos/perfetto/trace/profiling:lite",
-    "../../src/trace_processor:lib",
+    "../../include/perfetto/profiling:deobfuscator",
+    "../../include/perfetto/profiling:symbolizer",
   ]
   sources = [
     "utils.cc",
@@ -62,16 +62,14 @@
 
 source_set("local_symbolizer") {
   public_deps = [
+    "../../include/perfetto/ext/base",
+    "../../include/perfetto/profiling:symbolizer",
+  ]
+  deps = [
     ":symbolizer",
     ":utils",
     "../../gn:default_deps",
-    "../../include/perfetto/base",
-    "../../include/perfetto/ext/base",
     "../../include/perfetto/protozero",
-    "../../protos/perfetto/trace:lite",
-    "../../protos/perfetto/trace:zero",
-    "../../protos/perfetto/trace/interned_data:lite",
-    "../../protos/perfetto/trace/profiling:lite",
   ]
   sources = [
     "local_symbolizer.cc",
@@ -80,16 +78,9 @@
 }
 
 source_set("symbolizer") {
-  public_deps = [
+  deps = [
     "../../gn:default_deps",
-    "../../include/perfetto/base",
-    "../../include/perfetto/ext/base",
     "../../include/perfetto/profiling:symbolizer",
-    "../../include/perfetto/protozero",
-    "../../protos/perfetto/trace:lite",
-    "../../protos/perfetto/trace:zero",
-    "../../protos/perfetto/trace/interned_data:lite",
-    "../../protos/perfetto/trace/profiling:lite",
   ]
   sources = [
     "symbolizer.cc",
@@ -103,11 +94,11 @@
     "../../gn:default_deps",
     "../../include/perfetto/base",
     "../../include/perfetto/profiling:symbolizer",
-    "../../protos/perfetto/trace:lite",
-    "../../protos/perfetto/trace/profiling:lite",
-    "../../protos/third_party/pprof:lite",
-    "../../src/base",
-    "../../src/trace_processor:lib",
+    "../../include/perfetto/protozero",
+    "../../include/perfetto/trace_processor",
+    "../../protos/perfetto/trace:zero",
+    "../../protos/perfetto/trace/profiling:zero",
+    "../../protos/third_party/pprof:zero",
   ]
   sources = [
     "pprof_builder.cc",
@@ -128,22 +119,13 @@
   deps = [
     ":local_symbolizer",
     ":pprofbuilder",
-    ":symbolizer",
     ":utils",
-    "../../include/perfetto/profiling:deobfuscator",
-    "../../src/profiling:deobfuscator",
-  ]
-  public_deps = [
     "../../gn:default_deps",
     "../../include/perfetto/base",
     "../../include/perfetto/ext/traced:sys_stats_counters",
+    "../../include/perfetto/profiling:deobfuscator",
     "../../include/perfetto/protozero",
-    "../../protos/perfetto/trace:lite",
     "../../protos/perfetto/trace:zero",
-    "../../protos/perfetto/trace/ftrace:lite",
-    "../../protos/perfetto/trace/interned_data:lite",
-    "../../protos/perfetto/trace/profiling:lite",
-    "../../src/base",
     "../../src/trace_processor:lib",
   ]
   sources = [
@@ -170,6 +152,7 @@
   deps = [
     ":common",
     "../../gn:default_deps",
+    "../../include/perfetto/base",
   ]
   sources = [
     "lite_fallbacks.cc",
@@ -185,6 +168,7 @@
     "../../gn:default_deps",
     "../../gn:protobuf_full",
     "../../gn:zlib",
+    "../../protos/perfetto/trace:zero",
   ]
   sources = [
     "proto_full_utils.cc",
diff --git a/tools/trace_to_text/deobfuscate_profile.cc b/tools/trace_to_text/deobfuscate_profile.cc
index 1d1af52..b6e8b8c 100644
--- a/tools/trace_to_text/deobfuscate_profile.cc
+++ b/tools/trace_to_text/deobfuscate_profile.cc
@@ -19,7 +19,7 @@
 #include "perfetto/base/logging.h"
 #include "perfetto/ext/base/scoped_file.h"
 #include "perfetto/profiling/deobfuscator.h"
-#include "protos/perfetto/trace/trace_packet.pb.h"
+#include "perfetto/trace_processor/trace_processor.h"
 #include "tools/trace_to_text/deobfuscate_profile.h"
 #include "tools/trace_to_text/utils.h"
 
@@ -78,8 +78,8 @@
 
   tp->NotifyEndOfFile();
   DeobfuscateDatabase(tp.get(), obfuscation_map,
-                      [output](const perfetto::protos::TracePacket& packet) {
-                        WriteTracePacket(packet.SerializeAsString(), output);
+                      [output](const std::string& packet_proto) {
+                        WriteTracePacket(packet_proto, output);
                       });
   return 0;
 }
diff --git a/tools/trace_to_text/pprof_builder.cc b/tools/trace_to_text/pprof_builder.cc
index 4c61eb3..77dc8615 100644
--- a/tools/trace_to_text/pprof_builder.cc
+++ b/tools/trace_to_text/pprof_builder.cc
@@ -32,13 +32,13 @@
 #include "perfetto/ext/base/string_utils.h"
 #include "perfetto/ext/base/utils.h"
 #include "perfetto/profiling/symbolizer.h"
+#include "perfetto/protozero/packed_repeated_fields.h"
+#include "perfetto/protozero/scattered_heap_buffer.h"
+#include "perfetto/trace_processor/trace_processor.h"
 
-#include "protos/perfetto/trace/profiling/profile_common.pb.h"
-#include "protos/perfetto/trace/profiling/profile_packet.pb.h"
-#include "protos/perfetto/trace/trace.pb.h"
 #include "protos/perfetto/trace/trace.pbzero.h"
 #include "protos/perfetto/trace/trace_packet.pbzero.h"
-#include "protos/third_party/pprof/profile.pb.h"
+#include "protos/third_party/pprof/profile.pbzero.h"
 
 namespace perfetto {
 namespace trace_to_text {
@@ -49,14 +49,6 @@
 using ::protozero::proto_utils::MakeTagLengthDelimited;
 using ::protozero::proto_utils::WriteVarInt;
 
-using GLine = ::perfetto::third_party::perftools::profiles::Line;
-using GMapping = ::perfetto::third_party::perftools::profiles::Mapping;
-using GLocation = ::perfetto::third_party::perftools::profiles::Location;
-using GProfile = ::perfetto::third_party::perftools::profiles::Profile;
-using GValueType = ::perfetto::third_party::perftools::profiles::ValueType;
-using GFunction = ::perfetto::third_party::perftools::profiles::Function;
-using GSample = ::perfetto::third_party::perftools::profiles::Sample;
-
 struct View {
   const char* type;
   const char* unit;
@@ -266,15 +258,18 @@
         break;
       }
 
-      GSample* gsample = result_.add_sample();
+      auto* gsample = result_->add_sample();
+      protozero::PackedVarInt sample_values;
       for (size_t i = 0; i < base::ArraySize(kViews); ++i) {
         int64_t callstack_id = (*view_its)[i].Get(0).long_value;
         if (i == 0) {
           auto frames = FramesForCallstack(callstack_id);
           if (frames.empty())
             return false;
+          protozero::PackedVarInt location_ids;
           for (int64_t frame : frames)
-            gsample->add_location_id(ToPprofId(frame));
+            location_ids.Append(ToPprofId(frame));
+          gsample->set_location_id(location_ids);
           seen_frames->insert(frames.cbegin(), frames.cend());
         } else {
           if (callstack_id != (*view_its)[i].Get(0).long_value) {
@@ -282,8 +277,9 @@
             return false;
           }
         }
-        gsample->add_value((*view_its)[i].Get(1).long_value);
+        sample_values.Append((*view_its)[i].Get(1).long_value);
       }
+      gsample->set_value(sample_values);
     }
     return true;
   }
@@ -299,7 +295,8 @@
       if (seen_mappings.find(id) == seen_mappings.end())
         continue;
       ++mappings_no;
-      GMapping* gmapping = result_.add_mapping();
+      auto interned_filename = Intern(mapping_it.Get(4).string_value);
+      auto* gmapping = result_->add_mapping();
       gmapping->set_id(ToPprofId(id));
       // Do not set the build_id here to avoid downstream services
       // trying to symbolize (e.g. b/141735056)
@@ -309,7 +306,7 @@
           static_cast<uint64_t>(mapping_it.Get(2).long_value));
       gmapping->set_memory_limit(
           static_cast<uint64_t>(mapping_it.Get(3).long_value));
-      gmapping->set_filename(Intern(mapping_it.Get(4).string_value));
+      gmapping->set_filename(interned_filename);
     }
     if (!mapping_it.Status().ok()) {
       PERFETTO_DFATAL_OR_ELOG("Invalid mapping iterator: %s",
@@ -337,11 +334,14 @@
       std::string demangled_name = name;
       MaybeDemangle(&demangled_name);
 
-      GFunction* gfunction = result_.add_function();
+      auto interned_demangled_name = Intern(demangled_name);
+      auto interned_system_name = Intern(name);
+      auto interned_filename = Intern(symbol_it.Get(2).string_value);
+      auto* gfunction = result_->add_function();
       gfunction->set_id(ToPprofId(id));
-      gfunction->set_name(Intern(demangled_name));
-      gfunction->set_system_name(Intern(name));
-      gfunction->set_filename(Intern(symbol_it.Get(2).string_value));
+      gfunction->set_name(interned_demangled_name);
+      gfunction->set_system_name(interned_system_name);
+      gfunction->set_filename(interned_filename);
     }
 
     if (!symbol_it.Status().ok()) {
@@ -376,7 +376,7 @@
       int64_t symbol_set_id = frame_it.Get(4).long_value;
 
       seen_mappings->emplace(mapping_id);
-      GLocation* glocation = result_.add_location();
+      auto* glocation = result_->add_location();
       glocation->set_id(ToPprofId(frame_id));
       glocation->set_mapping_id(ToPprofId(mapping_id));
       // TODO(fmayer): Convert to abspc.
@@ -386,7 +386,7 @@
       if (symbol_set_id) {
         for (const Line& line : LineForSymbolSetId(symbol_set_id)) {
           seen_symbol_ids->emplace(line.symbol_id);
-          GLine* gline = glocation->add_line();
+          auto* gline = glocation->add_line();
           gline->set_line(line.line_number);
           gline->set_function_id(ToPprofId(line.symbol_id));
         }
@@ -395,14 +395,16 @@
         std::string demangled_name = frame_name;
         MaybeDemangle(&demangled_name);
 
-        GFunction* gfunction = result_.add_function();
-        gfunction->set_id(ToPprofId(synthesized_symbol_id));
-        gfunction->set_name(Intern(demangled_name));
-        gfunction->set_system_name(Intern(frame_name));
-
-        GLine* gline = glocation->add_line();
+        auto* gline = glocation->add_line();
         gline->set_line(0);
         gline->set_function_id(ToPprofId(synthesized_symbol_id));
+
+        auto interned_demangled_name = Intern(demangled_name);
+        auto interned_system_name = Intern(frame_name);
+        auto* gfunction = result_->add_function();
+        gfunction->set_id(ToPprofId(synthesized_symbol_id));
+        gfunction->set_name(interned_demangled_name);
+        gfunction->set_system_name(interned_system_name);
       }
     }
 
@@ -425,15 +427,20 @@
 
   void WriteSampleTypes() {
     for (size_t i = 0; i < base::ArraySize(kViews); ++i) {
-      auto* sample_type = result_.add_sample_type();
+      Intern(kViews[i].type);
+      Intern(kViews[i].unit);
+    }
+
+    for (size_t i = 0; i < base::ArraySize(kViews); ++i) {
+      auto* sample_type = result_->add_sample_type();
       sample_type->set_type(Intern(kViews[i].type));
       sample_type->set_unit(Intern(kViews[i].unit));
     }
   }
 
-  GProfile GenerateGProfile(trace_processor::TraceProcessor* tp,
-                            uint64_t upid,
-                            uint64_t ts) {
+  std::string GenerateGProfile(trace_processor::TraceProcessor* tp,
+                               uint64_t upid,
+                               uint64_t ts) {
     std::set<int64_t> seen_frames;
     std::set<int64_t> seen_mappings;
     std::set<int64_t> seen_symbol_ids;
@@ -449,7 +456,7 @@
       return {};
     if (!WriteSymbols(tp, seen_symbol_ids))
       return {};
-    return std::move(result_);
+    return result_.SerializeAsString();
   }
 
   const std::vector<int64_t>& FramesForCallstack(int64_t callstack_id) {
@@ -471,13 +478,14 @@
     if (it == string_table_.end()) {
       std::tie(it, std::ignore) =
           string_table_.emplace(s, string_table_.size());
-      result_.add_string_table(s);
+      result_->add_string_table(s);
     }
     return it->second;
   }
 
  private:
-  GProfile result_;
+  protozero::HeapBuffered<third_party::perftools::profiles::pbzero::Profile>
+      result_;
   std::map<std::string, int64_t> string_table_;
   const std::vector<std::vector<int64_t>>& callsite_to_frames_;
   const std::map<int64_t, std::vector<Line>>& symbol_set_id_to_lines_;
@@ -509,30 +517,22 @@
                   uint64_t pid,
                   const std::vector<uint64_t>& timestamps) {
   if (symbolizer) {
-    SymbolizeDatabase(
-        tp, symbolizer, [&tp](perfetto::protos::TracePacket packet) {
-          size_t size = static_cast<size_t>(packet.ByteSize());
-          std::unique_ptr<uint8_t[]> buf(new uint8_t[size]);
-          packet.SerializeToArray(buf.get(), packet.ByteSize());
-
-          std::unique_ptr<uint8_t[]> preamble(new uint8_t[11]);
-          preamble[0] =
-              MakeTagLengthDelimited(protos::pbzero::Trace::kPacketFieldNumber);
-          uint8_t* end = WriteVarInt(size, &preamble[1]);
-          size_t preamble_size = static_cast<size_t>(end - &preamble[0]);
-          auto status = tp->Parse(std::move(preamble), preamble_size);
-          if (!status.ok()) {
-            PERFETTO_DFATAL_OR_ELOG("Failed to parse: %s",
-                                    status.message().c_str());
-            return;
-          }
-          status = tp->Parse(std::move(buf), size);
-          if (!status.ok()) {
-            PERFETTO_DFATAL_OR_ELOG("Failed to parse: %s",
-                                    status.message().c_str());
-            return;
-          }
-        });
+    SymbolizeDatabase(tp, symbolizer, [&tp](const std::string& packet_proto) {
+      std::unique_ptr<uint8_t[]> buf(new uint8_t[11 + packet_proto.size()]);
+      uint8_t* wptr = &buf[0];
+      *(wptr++) =
+          MakeTagLengthDelimited(protos::pbzero::Trace::kPacketFieldNumber);
+      wptr = WriteVarInt(packet_proto.size(), wptr);
+      memcpy(wptr, packet_proto.data(), packet_proto.size());
+      wptr += packet_proto.size();
+      size_t buf_size = static_cast<size_t>(wptr - &buf[0]);
+      auto status = tp->Parse(std::move(buf), buf_size);
+      if (!status.ok()) {
+        PERFETTO_DFATAL_OR_ELOG("Failed to parse: %s",
+                                status.message().c_str());
+        return;
+      }
+    });
   }
 
   tp->NotifyEndOfFile();
@@ -570,10 +570,9 @@
     Iterator pid_it = tp->ExecuteQuery(pid_query);
     PERFETTO_CHECK(pid_it.Next());
 
-    GProfile profile = builder.GenerateGProfile(tp, upid, ts);
-    output->emplace_back(
-        SerializedProfile{static_cast<uint64_t>(pid_it.Get(0).long_value),
-                          profile.SerializeAsString()});
+    std::string profile_proto = builder.GenerateGProfile(tp, upid, ts);
+    output->emplace_back(SerializedProfile{
+        static_cast<uint64_t>(pid_it.Get(0).long_value), profile_proto});
   }
   if (any_fail) {
     PERFETTO_ELOG(
diff --git a/tools/trace_to_text/symbolize_profile.cc b/tools/trace_to_text/symbolize_profile.cc
index 4ae59fb..b354572 100644
--- a/tools/trace_to_text/symbolize_profile.cc
+++ b/tools/trace_to_text/symbolize_profile.cc
@@ -20,6 +20,7 @@
 
 #include "perfetto/base/logging.h"
 #include "perfetto/profiling/symbolizer.h"
+#include "perfetto/trace_processor/trace_processor.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_LOCAL_SYMBOLIZER)
 #include "tools/trace_to_text/local_symbolizer.h"
@@ -57,8 +58,8 @@
   tp->NotifyEndOfFile();
 
   SymbolizeDatabase(tp.get(), symbolizer.get(),
-                    [output](const perfetto::protos::TracePacket& packet) {
-                      WriteTracePacket(packet.SerializeAsString(), output);
+                    [output](const std::string& packet_proto) {
+                      WriteTracePacket(packet_proto, output);
                     });
   return 0;
 }
diff --git a/tools/trace_to_text/symbolize_profile.h b/tools/trace_to_text/symbolize_profile.h
index 3c787fd..733092e 100644
--- a/tools/trace_to_text/symbolize_profile.h
+++ b/tools/trace_to_text/symbolize_profile.h
@@ -17,10 +17,6 @@
 #ifndef TOOLS_TRACE_TO_TEXT_SYMBOLIZE_PROFILE_H_
 #define TOOLS_TRACE_TO_TEXT_SYMBOLIZE_PROFILE_H_
 
-#include "perfetto/profiling/symbolizer.h"
-#include "perfetto/trace_processor/trace_processor.h"
-#include "protos/perfetto/trace/trace_packet.pb.h"
-
 #include <functional>
 #include <iostream>
 
diff --git a/tools/trace_to_text/trace_to_text.cc b/tools/trace_to_text/trace_to_text.cc
index 6a05a03..3fb86d8 100644
--- a/tools/trace_to_text/trace_to_text.cc
+++ b/tools/trace_to_text/trace_to_text.cc
@@ -29,7 +29,7 @@
 #include "tools/trace_to_text/utils.h"
 
 #include "protos/perfetto/trace/trace.pbzero.h"
-#include "protos/perfetto/trace/trace_packet.pb.h"
+#include "protos/perfetto/trace/trace_packet.pbzero.h"
 
 namespace perfetto {
 namespace trace_to_text {
diff --git a/tools/trace_to_text/utils.cc b/tools/trace_to_text/utils.cc
index a523e2a..7b9b358 100644
--- a/tools/trace_to_text/utils.cc
+++ b/tools/trace_to_text/utils.cc
@@ -26,12 +26,13 @@
 
 #include "perfetto/base/logging.h"
 #include "perfetto/ext/base/string_splitter.h"
-#include "perfetto/ext/traced/sys_stats_counters.h"
-#include "protos/perfetto/trace/ftrace/ftrace_stats.pb.h"
+#include "perfetto/protozero/scattered_heap_buffer.h"
+#include "perfetto/trace_processor/trace_processor.h"
 
-#include "protos/perfetto/trace/trace.pb.h"
+#include "protos/perfetto/trace/profiling/heap_graph.pbzero.h"
+#include "protos/perfetto/trace/profiling/profile_common.pbzero.h"
 #include "protos/perfetto/trace/trace.pbzero.h"
-#include "protos/perfetto/trace/trace_packet.pb.h"
+#include "protos/perfetto/trace/trace_packet.pbzero.h"
 
 namespace perfetto {
 namespace trace_to_text {
@@ -184,22 +185,6 @@
   }
 }
 
-void ForEachPacketInTrace(
-    std::istream* input,
-    const std::function<void(const protos::TracePacket&)>& f) {
-  ForEachPacketBlobInTrace(
-      input, [f](std::unique_ptr<char[]> buf, size_t size) {
-        protos::TracePacket packet;
-        auto res = packet.ParseFromArray(buf.get(), static_cast<int>(size));
-        if (!res) {
-          PERFETTO_ELOG("Skipping invalid packet");
-          return;
-        }
-        f(packet);
-      });
-  fprintf(stderr, "\n");
-}
-
 std::vector<std::string> GetPerfettoBinaryPath() {
   std::vector<std::string> roots;
   const char* root = getenv("PERFETTO_BINARY_PATH");
@@ -257,10 +242,9 @@
   return true;
 }
 
-void SymbolizeDatabase(
-    trace_processor::TraceProcessor* tp,
-    Symbolizer* symbolizer,
-    std::function<void(perfetto::protos::TracePacket)> callback) {
+void SymbolizeDatabase(trace_processor::TraceProcessor* tp,
+                       Symbolizer* symbolizer,
+                       std::function<void(const std::string&)> callback) {
   PERFETTO_CHECK(symbolizer);
   auto unsymbolized = GetUnsymbolizedFrames(tp);
   for (auto it = unsymbolized.cbegin(); it != unsymbolized.cend(); ++it) {
@@ -271,9 +255,8 @@
     if (res.empty())
       continue;
 
-    perfetto::protos::TracePacket packet;
-    perfetto::protos::ModuleSymbols* module_symbols =
-        packet.mutable_module_symbols();
+    protozero::HeapBuffered<perfetto::protos::pbzero::TracePacket> packet;
+    auto* module_symbols = packet->set_module_symbols();
     module_symbols->set_path(name_and_buildid.first);
     module_symbols->set_build_id(name_and_buildid.second);
     PERFETTO_DCHECK(res.size() == rel_pcs.size());
@@ -287,21 +270,20 @@
         line->set_line_number(frame.line);
       }
     }
-    callback(std::move(packet));
+    callback(packet.SerializeAsString());
   }
 }
 
 void DeobfuscateDatabase(
     trace_processor::TraceProcessor* tp,
     const std::map<std::string, profiling::ObfuscatedClass>& mapping,
-    std::function<void(perfetto::protos::TracePacket)> callback) {
+    std::function<void(const std::string&)> callback) {
   std::map<std::string, std::set<std::string>> classes =
       GetHeapGraphClasses(tp);
-  perfetto::protos::TracePacket packet;
+  protozero::HeapBuffered<perfetto::protos::pbzero::TracePacket> packet;
   // TODO(fmayer): Add handling for package name and version code here so we
   // can support multiple dumps in the same trace.
-  perfetto::protos::DeobfuscationMapping* proto_mapping =
-      packet.mutable_deobfuscation_mapping();
+  auto* proto_mapping = packet->set_deobfuscation_mapping();
   for (const auto& p : classes) {
     const std::string& obfuscated_class_name = p.first;
     const std::set<std::string>& obfuscated_field_names = p.second;
@@ -311,15 +293,13 @@
       continue;
     }
     const profiling::ObfuscatedClass& cls = it->second;
-    perfetto::protos::ObfuscatedClass* proto_class =
-        proto_mapping->add_obfuscated_classes();
+    auto* proto_class = proto_mapping->add_obfuscated_classes();
     proto_class->set_obfuscated_name(obfuscated_class_name);
     proto_class->set_deobfuscated_name(cls.deobfuscated_name);
     for (const std::string& obfuscated_field_name : obfuscated_field_names) {
       auto field_it = cls.deobfuscated_fields.find(obfuscated_field_name);
       if (field_it != cls.deobfuscated_fields.end()) {
-        perfetto::protos::ObfuscatedMember* proto_member =
-            proto_class->add_obfuscated_members();
+        auto* proto_member = proto_class->add_obfuscated_members();
         proto_member->set_obfuscated_name(obfuscated_field_name);
         proto_member->set_deobfuscated_name(field_it->second);
       } else {
@@ -328,7 +308,7 @@
       }
     }
   }
-  callback(packet);
+  callback(packet.SerializeAsString());
 }
 
 TraceWriter::TraceWriter(std::ostream* output) : output_(output) {}
diff --git a/tools/trace_to_text/utils.h b/tools/trace_to_text/utils.h
index 9cc7c1f..b2e2c0d 100644
--- a/tools/trace_to_text/utils.h
+++ b/tools/trace_to_text/utils.h
@@ -33,10 +33,13 @@
 #include "perfetto/ext/base/paged_memory.h"
 #include "perfetto/profiling/deobfuscator.h"
 #include "perfetto/profiling/symbolizer.h"
-#include "perfetto/trace_processor/trace_processor.h"
 
 namespace perfetto {
 
+namespace trace_processor {
+class TraceProcessor;
+}
+
 namespace protos {
 class TracePacket;
 }
@@ -55,10 +58,6 @@
     std::istream* input,
     const std::function<void(std::unique_ptr<char[]>, size_t)>&);
 
-void ForEachPacketInTrace(
-    std::istream* input,
-    const std::function<void(const protos::TracePacket&)>&);
-
 std::vector<std::string> GetPerfettoBinaryPath();
 base::Optional<std::string> GetPerfettoProguardMapPath();
 
@@ -67,19 +66,18 @@
 void WriteTracePacket(const std::string& str, std::ostream* output);
 
 // Generate ModuleSymbol protos for all unsymbolized frames in the database.
-// Wrap them in TracePackets and call callback.
-void SymbolizeDatabase(
-    trace_processor::TraceProcessor* tp,
-    Symbolizer* symbolizer,
-    std::function<void(perfetto::protos::TracePacket)> callback);
+// Wrap them in proto-encoded TracePackets messages and call callback.
+void SymbolizeDatabase(trace_processor::TraceProcessor* tp,
+                       Symbolizer* symbolizer,
+                       std::function<void(const std::string&)> callback);
 
 // Generate ObfuscationMapping protos for all obfuscated java names in the
 // database.
-// Wrap them in TracePackets and call callback.
+// Wrap them in proto-encoded TracePackets messages and call callback.
 void DeobfuscateDatabase(
     trace_processor::TraceProcessor* tp,
     const std::map<std::string, profiling::ObfuscatedClass>& mapping,
-    std::function<void(perfetto::protos::TracePacket)> callback);
+    std::function<void(const std::string&)> callback);
 
 class TraceWriter {
  public: