lldb: Linux: delete Ptrace.h pollyfill
Project: llvm/llvm-project | Language: C++ | Concepts: refactoring | Difficulty: mid
Link: https://github.com/llvm/llvm-project/pull/218045
The problem
The codebase included a local header file, lldb/Host/linux/Ptrace.h, as a workaround for including <sys/ptrace.h> in multiple places. This header was used to avoid macro collisions that can occur when system headers are included before certain framework headers. However, this workaround had become unnecessary because the required system includes were already being handled locally where needed, making the pollyfill redundant.
Why it's non-trivial
A naive fix might simply delete the header and its includes, but this could break builds if any files still depended on the pollyfill for definitions or if the order of includes reintroduced macro conflicts. The change must ensure that all necessary system includes are present in each source file, and that the problematic macros are still handled safely.
The approach
1. Delete the pollyfill header:
The PR removes the now-unnecessary header file entirely.
diff --git a/lldb/include/lldb/Host/linux/Ptrace.h b/lldb/include/lldb/Host/linux/Ptrace.h
deleted file mode 100644
index 8b3719113d3c6..0000000000000
--- a/lldb/include/lldb/Host/linux/Ptrace.h
+++ /dev/null
@@ -1,18 +0,0 @@
-//===-- Ptrace.h ------------------------------------------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// This file defines ptrace functions & structures
-
-#ifndef liblldb_Host_linux_Ptrace_h_
-#define liblldb_Host_linux_Ptrace_h_
-
-#include <sys/ptrace.h>
-
-// Support ptrace extensions even when compiled without required kernel support
-
-#endif // liblldb_Host_linux_Ptrace_h_
This removes the pollyfill header, ensuring it cannot be included elsewhere.
2. Replace pollyfill includes with direct system includes:
In every source file that previously included the pollyfill, the PR:
- Removes
#include "lldb/Host/linux/Ptrace.h" - Adds
#include <sys/ptrace.h>after framework includes, with a comment explaining the order.
Example from NativeProcessLinux.cpp:
-#include "lldb/Host/linux/Ptrace.h"
...
-#include <linux/unistd.h>
+// System includes - They have to be included after framework includes because
+// they define some macros which collide with variable names in other modules.
+#include <linux/unistd.h>
+#include <sys/ptrace.h>
This ensures <sys/ptrace.h> is included directly and in the correct order to avoid macro conflicts.
Similar changes are made in all relevant source files, always guarded by the explanatory comment.
3. Consistent comment about include order:
Every new direct include of <sys/ptrace.h> is preceded by:
// System includes - They have to be included after framework includes because
// they define some macros which collide with variable names in other modules.
This documents the rationale and helps prevent future include-order bugs.
From the review thread
No notable review discussion on this PR.
Takeaways
- Remove redundant abstraction layers once their purpose is obsolete. If a workaround or compatibility shim is no longer needed, deleting it reduces maintenance burden and confusion.
- Be explicit about include order when dealing with system headers that define problematic macros. Use comments to document why certain headers must be included after others.
- Refactoring should ensure all dependencies are handled locally. When removing a shared header, update all affected files to include what they need directly, preserving build correctness.