Merge "tp: fix tracking when process reuse happens before ftrace starts" into main
diff --git a/src/trace_processor/importers/common/process_tracker.cc b/src/trace_processor/importers/common/process_tracker.cc
index 3fc95fc..e292801 100644
--- a/src/trace_processor/importers/common/process_tracker.cc
+++ b/src/trace_processor/importers/common/process_tracker.cc
@@ -356,6 +356,17 @@
   UniquePid upid = GetOrCreateProcess(pid);
   auto* process_table = context_->storage->mutable_process_table();
 
+  // If we both know the previous and current parent pid and the two are not
+  // matching, we must have died and restarted: create a new process.
+  if (pupid) {
+    std::optional<UniquePid> prev_parent_upid =
+        process_table->parent_upid()[upid];
+    if (prev_parent_upid && prev_parent_upid != pupid) {
+      upid = StartNewProcess(std::nullopt, ppid, pid, kNullStringId,
+                             ThreadNamePriority::kOther);
+    }
+  }
+
   StringId proc_name_id = context_->storage->InternString(name);
   process_table->mutable_name()->Set(upid, proc_name_id);
   process_table->mutable_cmdline()->Set(
diff --git a/test/trace_processor/diff_tests/parser/process_tracking/process_tracking_exec.py b/test/trace_processor/diff_tests/parser/process_tracking/process_tracking_exec.py
index a706d7e..83e1034 100644
--- a/test/trace_processor/diff_tests/parser/process_tracking/process_tracking_exec.py
+++ b/test/trace_processor/diff_tests/parser/process_tracking/process_tracking_exec.py
@@ -22,24 +22,25 @@
 
 trace = synth_common.create_trace()
 
-# Create a parent process which  will be forked below.
+# Create a parent process which will be forked below.
 trace.add_packet(ts=1)
 trace.add_process(10, 0, "parent")
 
-# Fork off the new process and then kill it 5ns later.
+# Fork the process into a child.
 trace.add_ftrace_packet(0)
 trace.add_newtask(ts=15, tid=10, new_tid=11, new_comm='child', flags=0)
 trace.add_sched(ts=16, prev_pid=10, next_pid=11, next_comm='child')
 
-# Create a parent process which  will be forked below.
+# Scrape event for the forked process.
 trace.add_packet(ts=20)
-trace.add_process(11, 0, "child_process")
+trace.add_process(11, 10, "child_process")
 
+# Rename of the main thread of forked process.
 trace.add_ftrace_packet(0)
 trace.add_rename(
     ts=25, tid=11, old_comm='child', new_comm='true_name', oom_score_adj=1000)
 
-# Create a parent process which  will be forked below.
+# Scrape of the forked process.
 trace.add_packet(ts=30)
 trace.add_process(11, 10, "true_process_name")
 
diff --git a/test/trace_processor/diff_tests/parser/process_tracking/synth_process_tracking.py b/test/trace_processor/diff_tests/parser/process_tracking/synth_process_tracking.py
index 68641ad..19bf834 100644
--- a/test/trace_processor/diff_tests/parser/process_tracking/synth_process_tracking.py
+++ b/test/trace_processor/diff_tests/parser/process_tracking/synth_process_tracking.py
@@ -90,7 +90,7 @@
     ts=28, prev_pid=32, next_pid=40, prev_comm='p3-t2', next_comm='p4-t0')
 
 trace.add_packet(ts=29)
-trace.add_process(40, 0, "process_4")
+trace.add_process(40, 30, "process_4")
 
 # And now, this new process starts a new thread that recycles TID=31 (previously
 # used as p3-t1, now becomes p4-t1).