uprobe.php 766 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. if ($argc < 2) {
  3. fwrite(STDERR, "Usage: php script.php <binary_path> <pid>\n");
  4. exit(1);
  5. }
  6. $binary = $argv[1];
  7. $pid = intval($argv[2]);
  8. $bpf_text = <<<EOT
  9. #include <uapi/linux/ptrace.h>
  10. int test(struct pt_regs *ctx)
  11. {
  12. bpf_trace_printk("%d---%d\\n",ctx->di,ctx->si);
  13. return 1;
  14. }
  15. EOT;
  16. $ebpf = new Bpf(["text" => $bpf_text]);
  17. $opt = array("pid"=>$pid);
  18. $ebpf->attach_uprobe($binary,"add","test",$opt);
  19. # header
  20. printf("%-18s %-16s %-6s %s\n", "TIME(s)", "COMM", "PID", "MESSAGE");
  21. # format output
  22. while (true) {
  23. try {
  24. list($task, $pid, $cpu, $flags, $ts, $msg) = $ebpf->trace_fields();
  25. printf("%-18.9f %-16s %-6d %s\n", $ts, $task, $pid, $msg);
  26. flush();
  27. } catch (Exception $e) {
  28. continue;
  29. }
  30. }