llvm-project/lldb/examples/ptrace_example.c
David Spickett 3398744a61
[lldb][Docs] Additions to debuging LLDB page (#65635)
Adds the following:
* A note that you can use attaching to debug the right lldb-server
process, though there are drawbacks.
* A section on debugging the remote protocol.
* Reducing bugs, including reducing ptrace bugs to remove the need for
LLDB.

I've added a standlone ptrace program to the examples folder because:
* There's no better place to put it.
* Adding it to the page seems like wasting space, and would be harder to
update.
* I link to Eli Bendersky's classic blog on the subject, but we are
safer with our own example as well.
* Eli's example is for 32 bit Intel, AArch64 is more common these days.
* It's easier to show the software breakpoint steps in code than explain
it (though I still do that in the text).
* It was living on my laptop not helping anyone so I think it's good to
have it upstream for others, including future me.
2023-09-08 10:05:16 +01:00

107 lines
2.5 KiB
C

//===-- ptrace_example.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
//
//===----------------------------------------------------------------------===//
#include <asm/ptrace.h>
#include <linux/elf.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/prctl.h>
#include <sys/ptrace.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <unistd.h>
// The demo program shows how to do basic ptrace operations without lldb
// or lldb-server. For the purposes of experimentation or reporting bugs
// in kernels.
//
// It is AArch64 Linux specific, adapt as needed.
//
// Expected output:
// Before breakpoint
// After breakpoint
void inferior() {
if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
perror("ptrace");
return;
}
printf("Before breakpoint\n");
// Go into debugger. Instruction replaced with nop later.
// We write 2 instuctions because POKETEXT works with
// 64 bit values and we don't want to overwrite the
// call to printf accidentally.
asm volatile("BRK #0 \n nop");
printf("After breakpoint\n");
}
void debugger(pid_t child) {
int wait_status;
// Wait until it hits the breakpoint.
wait(&wait_status);
while (WIFSTOPPED(wait_status)) {
if (WIFEXITED(wait_status)) {
printf("inferior exited normally\n");
return;
}
// Read general purpose registers to find the PC value.
struct user_pt_regs regs;
struct iovec io;
io.iov_base = &regs;
io.iov_len = sizeof(regs);
if (ptrace(PTRACE_GETREGSET, child, NT_PRSTATUS, &io) < 0) {
printf("getregset failed\n");
return;
}
// Replace brk #0 / nop with nop / nop by writing to memory
// at the current PC.
uint64_t replace = 0xd503201fd503201f;
if (ptrace(PTRACE_POKETEXT, child, regs.pc, replace) < 0) {
printf("replacing bkpt failed\n");
return;
}
// Single step over where the brk was.
if (ptrace(PTRACE_SINGLESTEP, child, 0, 0) < 0) {
perror("ptrace");
return;
}
// Wait for single step to be done.
wait(&wait_status);
// Run to completion.
if (ptrace(PTRACE_CONT, child, 0, 0) < 0) {
perror("ptrace");
return;
}
// Wait to see that the inferior exited.
wait(&wait_status);
}
}
int main() {
pid_t child = fork();
if (child == 0)
inferior();
else if (child > 0)
debugger(child);
else
return -1;
return 0;
}