Rename project to ptrace-inject-cpp

Rename the CMake project and CLI to ptrace-inject-cpp to distinguish this
C++ port from the original Rust project. The library artifact, public
header, and C API keep the name ptrace_inject; the READMEs state this
explicitly and use the new project name and tagline.
This commit is contained in:
xiaozhou233 committed 2026-10-18 21:49:46 +08:00
1 parent 50135e5f87
commit 04d39842ba
5 files changed
+46 -37

No files matched your search

+1 -1
View File
@@ -3,6 +3,6 @@ build-*/
*.o *.o
*.a *.a
*.so *.so
ptrace-inject ptrace-inject-cpp
example_by_pid example_by_pid
example_by_name example_by_name
+2 -2
View File
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(ptrace_inject VERSION 1.0.0 LANGUAGES C CXX) project(ptrace_inject_cpp VERSION 1.0.0 LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
@@ -53,7 +53,7 @@ set_target_properties(ptrace_inject PROPERTIES
if(PTRACE_INJECT_BUILD_CLI) if(PTRACE_INJECT_BUILD_CLI)
add_executable(ptrace_inject_cli src/main.cpp) add_executable(ptrace_inject_cli src/main.cpp)
set_target_properties(ptrace_inject_cli PROPERTIES OUTPUT_NAME ptrace-inject) set_target_properties(ptrace_inject_cli PROPERTIES OUTPUT_NAME ptrace-inject-cpp)
target_link_libraries(ptrace_inject_cli PRIVATE ptrace_inject) target_link_libraries(ptrace_inject_cli PRIVATE ptrace_inject)
endif() endif()
+21 -16
View File
@@ -1,13 +1,18 @@
# ptrace_inject # ptrace-inject-cpp
> English version: [README.md](README.md) > English version: [README.md](README.md)
用 `ptrace` 把 `.so` 注入正在运行的 x86-64 Linux 进程。 一个用 `ptrace` 向 x86-64 Linux 进程注入共享库的 C++ 实现。
本项目是 [Artemis21/ptrace-inject](https://github.com/Artemis21/ptrace-inject)
的 C++ 移植版。
项目名是 `ptrace-inject-cpp`;生成的库、头文件和公开 API 仍然叫 `ptrace_inject`。
提供两样东西: 提供两样东西:
- **一个 C ABI 库** `libptrace_inject`(实现是 C++,头文件可直接给 C/C++ 用); - **一个 C ABI 库** `libptrace_inject`(实现是 C++,头文件可直接给 C/C++ 用);
- **一个薄命令行工具** `ptrace-inject`,以及几个示例程序。 - **一个薄命令行工具** `ptrace-inject-cpp`,以及几个示例程序。
核心流程:attach 目标线程 → 在目标里远程调用 核心流程:attach 目标线程 → 在目标里远程调用
`malloc` / `dlopen` / `free` → 恢复现场 → detach。没有 spawn 模式,也没有 `malloc` / `dlopen` / `free` → 恢复现场 → detach。没有 spawn 模式,也没有
@@ -39,7 +44,7 @@ CMakeLists.txt
## 构建 ## 构建
```bash ```bash
cd /home/xiaozhou/Development/inject cd /path/to/ptrace-inject-cpp
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j cmake --build build -j
@@ -55,7 +60,7 @@ cmake --build build-shared -j
可选开关: 可选开关:
```bash ```bash
-DPTRACE_INJECT_BUILD_CLI=OFF # 不构建 ptrace-inject -DPTRACE_INJECT_BUILD_CLI=OFF # 不构建 ptrace-inject-cpp
-DPTRACE_INJECT_BUILD_EXAMPLES=OFF # 不构建示例 -DPTRACE_INJECT_BUILD_EXAMPLES=OFF # 不构建示例
``` ```
@@ -64,7 +69,7 @@ cmake --build build-shared -j
``` ```
build/libptrace_inject.a # 静态库(默认) build/libptrace_inject.a # 静态库(默认)
build-shared/libptrace_inject.so # 共享库(BUILD_SHARED_LIBS=ON 时的目录) build-shared/libptrace_inject.so # 共享库(BUILD_SHARED_LIBS=ON 时的目录)
build/ptrace-inject # CLI build/ptrace-inject-cpp # CLI
build/example_by_pid # C 示例 build/example_by_pid # C 示例
build/example_by_name # C++ 示例 build/example_by_name # C++ 示例
build/libptrace_inject_sample.so build/libptrace_inject_sample.so
@@ -127,23 +132,23 @@ int main(void) {
C++ 程序: C++ 程序:
```bash ```bash
g++ app.cpp -I/path/to/inject/include \ g++ app.cpp -I/path/to/ptrace-inject-cpp/include \
/path/to/inject/build/libptrace_inject.a -ldl -o app /path/to/ptrace-inject-cpp/build/libptrace_inject.a -ldl -o app
``` ```
C 程序(静态库内部是 C++,用 g++ 链接最省事): C 程序(静态库内部是 C++,用 g++ 链接最省事):
```bash ```bash
gcc -c app.c -I/path/to/inject/include -o app.o gcc -c app.c -I/path/to/ptrace-inject-cpp/include -o app.o
g++ app.o /path/to/inject/build/libptrace_inject.a -ldl -o app g++ app.o /path/to/ptrace-inject-cpp/build/libptrace_inject.a -ldl -o app
``` ```
共享库: 共享库:
```bash ```bash
gcc app.c -I/path/to/inject/include \ gcc app.c -I/path/to/ptrace-inject-cpp/include \
-L/path/to/inject/build-shared -lptrace_inject -ldl -lstdc++ -o app -L/path/to/ptrace-inject-cpp/build-shared -lptrace_inject -ldl -lstdc++ -o app
LD_LIBRARY_PATH=/path/to/inject/build-shared ./app LD_LIBRARY_PATH=/path/to/ptrace-inject-cpp/build-shared ./app
``` ```
如果自己的项目也用 CMake,可以直接 `add_subdirectory`,不需要安装: 如果自己的项目也用 CMake,可以直接 `add_subdirectory`,不需要安装:
@@ -154,7 +159,7 @@ project(consumer LANGUAGES C CXX)
set(PTRACE_INJECT_BUILD_CLI OFF CACHE BOOL "" FORCE) set(PTRACE_INJECT_BUILD_CLI OFF CACHE BOOL "" FORCE)
set(PTRACE_INJECT_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) set(PTRACE_INJECT_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(/path/to/inject ptrace_inject) add_subdirectory(/path/to/ptrace-inject-cpp ptrace_inject)
add_executable(app app.c) add_executable(app app.c)
target_link_libraries(app PRIVATE ptrace_inject) target_link_libraries(app PRIVATE ptrace_inject)
@@ -164,10 +169,10 @@ target_link_libraries(app PRIVATE ptrace_inject)
```bash ```bash
# 按 PID # 按 PID
./build/ptrace-inject -p <PID> /absolute/or/relative/path/to/library.so ./build/ptrace-inject-cpp -p <PID> /absolute/or/relative/path/to/library.so
# 按可执行文件名 # 按可执行文件名
./build/ptrace-inject -n target-process ./library.so ./build/ptrace-inject-cpp -n target-process ./library.so
``` ```
示例输出: 示例输出:
+21 -17
View File
@@ -1,15 +1,19 @@
# ptrace_inject # ptrace-inject-cpp
Inject a shared library (`.so`) into a running x86-64 Linux process with Shared library injection for x86-64 Linux using ptrace, in C++.
`ptrace`.
A C++ port of [Artemis21/ptrace-inject](https://github.com/Artemis21/ptrace-inject).
> Chinese version: [README.ZH.md](README.ZH.md) > Chinese version: [README.ZH.md](README.ZH.md)
The project is named `ptrace-inject-cpp`; the library artifact, header, and
public API keep the name `ptrace_inject`.
This repository provides: This repository provides:
- **A C ABI library** `libptrace_inject` (implemented in C++, but the public - **A C ABI library** `libptrace_inject` (implemented in C++, but the public
header is directly usable from C and C++); header is directly usable from C and C++);
- **A thin command line tool** `ptrace-inject`, plus a few example programs. - **A thin command line tool** `ptrace-inject-cpp`, plus a few example programs.
The core flow is: attach to the target threads → remotely call The core flow is: attach to the target threads → remotely call
`malloc` / `dlopen` / `free` inside the target → restore the original state → `malloc` / `dlopen` / `free` inside the target → restore the original state →
@@ -41,7 +45,7 @@ CMakeLists.txt
## Building ## Building
```bash ```bash
cd /home/xiaozhou/Development/inject cd /path/to/ptrace-inject-cpp
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j cmake --build build -j
@@ -57,7 +61,7 @@ cmake --build build-shared -j
Optional switches: Optional switches:
```bash ```bash
-DPTRACE_INJECT_BUILD_CLI=OFF # do not build ptrace-inject -DPTRACE_INJECT_BUILD_CLI=OFF # do not build ptrace-inject-cpp
-DPTRACE_INJECT_BUILD_EXAMPLES=OFF # do not build the examples -DPTRACE_INJECT_BUILD_EXAMPLES=OFF # do not build the examples
``` ```
@@ -66,7 +70,7 @@ Build artifacts:
``` ```
build/libptrace_inject.a # static library (default) build/libptrace_inject.a # static library (default)
build-shared/libptrace_inject.so # shared library (with BUILD_SHARED_LIBS=ON) build-shared/libptrace_inject.so # shared library (with BUILD_SHARED_LIBS=ON)
build/ptrace-inject # CLI build/ptrace-inject-cpp # CLI
build/example_by_pid # C example build/example_by_pid # C example
build/example_by_name # C++ example build/example_by_name # C++ example
build/libptrace_inject_sample.so build/libptrace_inject_sample.so
@@ -135,24 +139,24 @@ No installation is required; point the compiler at this repository's
C++ program: C++ program:
```bash ```bash
g++ app.cpp -I/path/to/inject/include \ g++ app.cpp -I/path/to/ptrace-inject-cpp/include \
/path/to/inject/build/libptrace_inject.a -ldl -o app /path/to/ptrace-inject-cpp/build/libptrace_inject.a -ldl -o app
``` ```
C program (the static library is C++ internally, so linking with `g++` is the C program (the static library is C++ internally, so linking with `g++` is the
easiest path): easiest path):
```bash ```bash
gcc -c app.c -I/path/to/inject/include -o app.o gcc -c app.c -I/path/to/ptrace-inject-cpp/include -o app.o
g++ app.o /path/to/inject/build/libptrace_inject.a -ldl -o app g++ app.o /path/to/ptrace-inject-cpp/build/libptrace_inject.a -ldl -o app
``` ```
Shared library: Shared library:
```bash ```bash
gcc app.c -I/path/to/inject/include \ gcc app.c -I/path/to/ptrace-inject-cpp/include \
-L/path/to/inject/build-shared -lptrace_inject -ldl -lstdc++ -o app -L/path/to/ptrace-inject-cpp/build-shared -lptrace_inject -ldl -lstdc++ -o app
LD_LIBRARY_PATH=/path/to/inject/build-shared ./app LD_LIBRARY_PATH=/path/to/ptrace-inject-cpp/build-shared ./app
``` ```
If your project also uses CMake, you can use `add_subdirectory`; no install If your project also uses CMake, you can use `add_subdirectory`; no install
@@ -165,7 +169,7 @@ project(consumer LANGUAGES C CXX)
set(PTRACE_INJECT_BUILD_CLI OFF CACHE BOOL "" FORCE) set(PTRACE_INJECT_BUILD_CLI OFF CACHE BOOL "" FORCE)
set(PTRACE_INJECT_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) set(PTRACE_INJECT_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(/path/to/inject ptrace_inject) add_subdirectory(/path/to/ptrace-inject-cpp ptrace_inject)
add_executable(app app.c) add_executable(app app.c)
target_link_libraries(app PRIVATE ptrace_inject) target_link_libraries(app PRIVATE ptrace_inject)
@@ -175,10 +179,10 @@ target_link_libraries(app PRIVATE ptrace_inject)
```bash ```bash
# By PID # By PID
./build/ptrace-inject -p <PID> /absolute/or/relative/path/to/library.so ./build/ptrace-inject-cpp -p <PID> /absolute/or/relative/path/to/library.so
# By executable name # By executable name
./build/ptrace-inject -n target-process ./library.so ./build/ptrace-inject-cpp -n target-process ./library.so
``` ```
Example output: Example output:
+1 -1
View File
@@ -1,7 +1,7 @@
/* /*
* ptrace_inject - C ABI for injecting shared libraries with ptrace. * ptrace_inject - C ABI for injecting shared libraries with ptrace.
* *
* C++ port of the original Rust project: * Part of ptrace-inject-cpp, a C++ port of the original Rust project:
* https://github.com/Artemis21/ptrace-inject * https://github.com/Artemis21/ptrace-inject
*/ */