tracepoint_test.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. /*
  3. * Copyright 2024 carl.guo <[email protected]>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. $prog = <<<EOT
  18. TRACEPOINT_PROBE(syscalls, sys_enter_openat) {
  19. bpf_trace_printk("openat called\\n");
  20. return 0;
  21. }
  22. EOT;
  23. try {
  24. $bpf = new Bpf(["text" => $prog]);
  25. echo "BPF program loaded successfully\n";
  26. // Start monitoring
  27. echo "Monitoring openat syscalls...\n";
  28. while (true) {
  29. try {
  30. list($task, $pid, $cpu, $flags, $ts, $msg) = $bpf->trace_fields();
  31. printf("%-18.9f %-16s %-6d %s\n", $ts, $task, $pid, $msg);
  32. flush();
  33. } catch (Exception $e) {
  34. continue;
  35. }
  36. }
  37. } catch (Exception $e) {
  38. echo "Error: " . $e->getMessage() . "\n";
  39. exit(1);
  40. }