{"id":19474787,"url":"https://github.com/xigh/x-trace","last_synced_at":"2026-06-09T01:33:55.375Z","repository":{"id":144242085,"uuid":"68538609","full_name":"xigh/x-trace","owner":"xigh","description":"FreeBSD 11+12 kernel hacking example : x-trace, simple tool to trace execution","archived":false,"fork":false,"pushed_at":"2019-01-17T14:00:37.000Z","size":176,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-19T01:02:55.543Z","etag":null,"topics":["driver","freebsd","kernel","syscalls","trace","trace-execution"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xigh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-18T18:12:04.000Z","updated_at":"2019-09-08T04:21:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"c5662374-9820-4699-9575-74d5ee429ea7","html_url":"https://github.com/xigh/x-trace","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/xigh/x-trace","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xigh%2Fx-trace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xigh%2Fx-trace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xigh%2Fx-trace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xigh%2Fx-trace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xigh","download_url":"https://codeload.github.com/xigh/x-trace/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xigh%2Fx-trace/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34088013,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-08T02:00:07.615Z","response_time":111,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["driver","freebsd","kernel","syscalls","trace","trace-execution"],"created_at":"2024-11-10T19:26:59.193Z","updated_at":"2026-06-09T01:33:55.353Z","avatar_url":"https://github.com/xigh.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# x-trace\nFreeBSD 11+12 kernel hacking example : x-trace, simple tool to trace execution\n\nDescription\n--------------\n\nThis is a simple example of FreeBSD 11 kernel hacking. It comes with a driver (sys/), a userland program that communicates with the driver (bin/xtrace) and two tools (bin/xdump, bin/xdot) to dump the content of the binary file created by xtrace.\n\nHow to install\n--------------\n\n\t# git clone https://github.com/xigh/x-trace\n\t# cd x-trace\n\t# make\n\nHow to use it\n--------------\n\nLoad the kernel module:\n\n\t# kldload sys/xtrace.ko\n\nTrace the execution of /bin/ls:\n\n\t# bin/xtrace/xtrace /bin/ls\n\tstarted pid 4467\n\tMakefile\tbin\t\tsys\t\txtrace.out\n\tchild returned with 0\n\nDump the file generated by xtrace:\n\n\t# bin/xdump/xdump xtrace.out \n\t 4466: fork pid=4467\n\t  4467: exec '/bin/ls' argc=1 envc=24\n\t\targv:\n\t\t   0: /bin/ls\n\t\tenvv:\n\t\t   0: e\n\t\t   1: bin/xtSHELL=/bin/csh\n\t\t   2: SSH_CLIENT=2a01:....\n\t\t   3: LOGNAME=xigh\n\t\t   4: PAGER=more\n\t\t   5: MAIL=/var/mail/xigh\n\t\t   6: PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:...\n\t\t   7: EDITOR=vi\n\t\t   8: ENV=/home/xigh/.shrc\n\t\t   9: OLDPWD=/usr/home/xigh/src\n\t\t  10: PWD=/usr/home/xigh/src/xtrace\n\t\t  11: TERM=xterm-256color\n\t\t  12: SSH_TTY=/dev/pts/1\n\t\t  13: HOME=/root\n\t\t  14: USER=xigh\n\t\t  15: SSH_CONNECTION=2a01:....\n\t\t  16: BLOCKSIZE=K\n\t\t  17: HOSTTYPE=FreeBSD\n\t\t  18: VENDOR=amd\n\t\t  19: OSTYPE=FreeBSD\n\t\t  20: MACHTYPE=x86_64\n\t\t  21: SHLVL=1\n\t\t  22: GROUP=wheel\n\t\t  23: HOST=fbsd11\n\t 4467: exit rval=0\n\nOr use the new xexec tool I created:\n\n\t# bin/xexec/xexec xtrace.out\n\t?:/bin/sh\n\t\t?:/usr/bin/make\n\t\t/usr/home/xigh/src/x-trace:/bin/sh \"-c\" \"cc --version || echo 0.0.0\"\n\t\t\t/usr/home/xigh/src/x-trace:/usr/bin/cc \"--version\"\n\t\t/usr/home/xigh/src/x-trace:/bin/sh \"-c\" \"echo \\\"6.0.1 6.0.1)\\\" | awk -F. '{print $1 * 10000 + $2 * 100 + $3;}'\"\n\t\t\t/usr/home/xigh/src/x-trace:/usr/bin/awk \"-F.\" \"{print $1 * 10000 + $2 * 100 + $3;}\"\n\t\t/usr/home/xigh/src/x-trace:/bin/sh \"-c\" \"{ echo \\\"__FreeBSD_cc_version\\\" | cc -E - 2\u003e/dev/null || echo __FreeBSD_cc_version; } | sed -n '$p'\"\n\t\t\t/usr/home/xigh/src/x-trace:/usr/bin/sed \"-n\" \"$p\"\n\t\t\t/usr/home/xigh/src/x-trace:/usr/bin/cc \"-E\" \"-\"\n\t\t\t\t/usr/home/xigh/src/x-trace:/usr/bin/cc \"-cc1\" \"-triple\" \"x86_64-unknown-freebsd12.0\" \"-E\" \"-disable-free\" \"-disable-llvm-verifier\" \"-discard-value-names\" \"-main-file-name\" \"-\" \"-mrelocation-model\" \"static\" \"-mthread-model\" \"posix\" \"-mdisable-fp-elim\" \"-masm-verbose\" \"-mconstructor-aliases\" \"-munwind-tables\" \"-fuse-init-array\" \"-target-cpu\" \"x86-64\" \"-dwarf-column-info\" \"-debugger-tuning=gdb\" \"-resource-dir\" \"/usr/lib/clang/6.0.1\" \"-fdebug-compilation-dir\" \"/usr/home/xigh/src/x-trace\" \"-ferror-limit\" \"19\" \"-fmessage-length\" \"0\" \"-fobjc-runtime=gnustep\" \"-fdiagnostics-show-option\" \"-o\" \"-\" \"-x\" \"c\" \"-\"\n\t\t/usr/home/xigh/src/x-trace:/bin/sh \"-c\" \"(ld --version || echo none) | sed -n 1p\"\n\t\t\t/usr/home/xigh/src/x-trace:/usr/bin/ld \"--version\"\n\t...\n\nCreate a graphviz representation of the trace:\n\n\t# bin/xdot/xdot xtrace.out\n\tdigraph G {\n\t\t\"0:/usr/bin/make\\n0:/usr/bin/make\\n\" -\u003e \"1:/bin/sh\\n0:sh\\n1:-c\\n2:cc --version || echo 0...\\n\"\n\t\t\"1:/bin/sh\\n0:sh\\n1:-c\\n2:cc --version || echo 0...\\n\" -\u003e \"2:/usr/bin/cc\\n0:cc\\n1:--version\\n\"\n\t\t\"0:/usr/bin/make\\n0:/usr/bin/make\\n\" -\u003e \"3:/bin/sh\\n0:sh\\n1:-c\\n2:echo '3.8.0 3.8.0)' | ...\\n\"\n\t\t\"3:/bin/sh\\n0:sh\\n1:-c\\n2:echo '3.8.0 3.8.0)' | ...\\n\" -\u003e \"5:/usr/bin/awk\\n0:awk\\n1:-F.\\n2:{print $1 * 10000 + $2...\\n\"\n\t\t\"0:/usr/bin/make\\n0:/usr/bin/make\\n\" -\u003e \"6:/bin/sh\\n0:sh\\n1:-c\\n2:{ echo '__FreeBSD_cc_v...\\n\"\n\t\t\"6:/bin/sh\\n0:sh\\n1:-c\\n2:{ echo '__FreeBSD_cc_v...\\n\" -\u003e \"8:/usr/bin/sed\\n0:sed\\n1:-n\\n2:$p\\n\"\n\t\t\"10:/usr/bin/cc\\n0:cc\\n1:-E\\n2:-\\n\" -\u003e \"11:/usr/bin/cc\\n0:/usr/bin/cc\\n1:-cc1\\n2:-triple\\n3:x86_64-unknown-freebsd...\\n4:-E\\n5:-disable-free\\n6:-disable-llvm-verifier\\n7:-main-file-name\\n8:-\\n9:-mrelocation-model\\n10:static\\n11:-mthread-model\\n12:posix\\n13:-mdisable-fp-elim\\n14:-masm-verbose\\n15:-mconstructor-aliases\\n16:-munwind-tables\\n17:-target-cpu\\n18:x86-64\\n19:-dwarf-column-info\\n20:-debugger-tuning=gdb\\n21:-resource-dir\\n22:/usr/bin/../lib/clang/...\\n23:-fdebug-compilation-dir\\n24:/usr/home/xigh/src/xtrace\\n25:-ferror-limit\\n26:19\\n27:-fmessage-length\\n28:0\\n29:-fobjc-runtime=gnustep\\n30:-fdiagnostics-show-option\\n31:-o\\n32:-\\n33:-x\\n34:c\\n35:-\\n\"\n\t\t\"0:/usr/bin/make\\n0:/usr/bin/make\\n\" -\u003e \"12:/bin/sh\\n0:/bin/sh\\n1:-e\\n2:-c\\n3:target=all;  for dir i...\\n\"\n\t\t\"12:/bin/sh\\n0:/bin/sh\\n1:-e\\n2:-c\\n3:target=all;  for dir i...\\n\" -\u003e \"13:/usr/bin/make\\n0:/usr/bin/make\\n1:all\\n2:DIRPRFX=sys/\\n\"\n\t\t\"13:/usr/bin/make\\n0:/usr/bin/make\\n1:all\\n2:DIRPRFX=sys/\\n\" -\u003e \"14:/bin/sh\\n0:sh\\n1:-c\\n2:find /usr/src/sys/ -na...\\n\"\n\t\t\"13:/usr/bin/make\\n0:/usr/bin/make\\n1:all\\n2:DIRPRFX=sys/\\n\" -\u003e \"14:/usr/bin/find\\n0:find\\n1:/usr/src/sys/\\n2:-name\\n3:*_if.m\\n\"\n\t\t\"13:/usr/bin/make\\n0:/usr/bin/make\\n1:all\\n2:DIRPRFX=sys/\\n\" -\u003e \"15:/bin/sh\\n0:/bin/sh\\n1:-e\\n2:-c\\n3:echo 'Warning: Object ...\\n\"\n\t\t\"12:/bin/sh\\n0:/bin/sh\\n1:-e\\n2:-c\\n3:target=all;  for dir i...\\n\" -\u003e \"16:/usr/bin/make\\n0:/usr/bin/make\\n1:all\\n2:DIRPRFX=bin/\\n\"\n\t\t\"16:/usr/bin/make\\n0:/usr/bin/make\\n1:all\\n2:DIRPRFX=bin/\\n\" -\u003e \"17:/bin/sh\\n0:/bin/sh\\n1:-e\\n2:-c\\n3:target=all;  for dir i...\\n\"\n\t\t\"17:/bin/sh\\n0:/bin/sh\\n1:-e\\n2:-c\\n3:target=all;  for dir i...\\n\" -\u003e \"18:/usr/bin/make\\n0:/usr/bin/make\\n1:all\\n2:DIRPRFX=bin/xtrace/\\n\"\n\t\t\"18:/usr/bin/make\\n0:/usr/bin/make\\n1:all\\n2:DIRPRFX=bin/xtrace/\\n\" -\u003e \"19:/bin/sh\\n0:/bin/sh\\n1:-e\\n2:-c\\n3:echo 'Warning: Object ...\\n\"\n\t\t\"17:/bin/sh\\n0:/bin/sh\\n1:-e\\n2:-c\\n3:target=all;  for dir i...\\n\" -\u003e \"20:/usr/bin/make\\n0:/usr/bin/make\\n1:all\\n2:DIRPRFX=bin/xdump/\\n\"\n\t\t\"20:/usr/bin/make\\n0:/usr/bin/make\\n1:all\\n2:DIRPRFX=bin/xdump/\\n\" -\u003e \"21:/bin/sh\\n0:/bin/sh\\n1:-e\\n2:-c\\n3:echo 'Warning: Object ...\\n\"\n\t\t\"17:/bin/sh\\n0:/bin/sh\\n1:-e\\n2:-c\\n3:target=all;  for dir i...\\n\" -\u003e \"22:/usr/bin/make\\n0:/usr/bin/make\\n1:all\\n2:DIRPRFX=bin/xdot/\\n\"\n\t\t\"22:/usr/bin/make\\n0:/usr/bin/make\\n1:all\\n2:DIRPRFX=bin/xdot/\\n\" -\u003e \"23:/bin/sh\\n0:/bin/sh\\n1:-e\\n2:-c\\n3:echo 'Warning: Object ...\\n\"\n\t}\n\t\t\n\n![My image](img/example.png)\n\nHow it works\n--------------\n\nWhen the kernel module loads, it install hooks on a few syscalls:\n\n\tcb_fork = sysent[SYS_fork].sy_call;\n\tsysent[SYS_fork].sy_call =  \u0026xtrace_fork;\n\tcb_vfork = sysent[SYS_vfork].sy_call;\n\tsysent[SYS_vfork].sy_call =  \u0026xtrace_vfork;\n\tcb_exit = sysent[SYS_exit].sy_call;\n\tsysent[SYS_exit].sy_call =  \u0026xtrace_exit;\n\tcb_exec = sysent[SYS_execve].sy_call;\n\tsysent[SYS_execve].sy_call =  \u0026xtrace_exec;\n\tcb_chdir = sysent[SYS_chdir].sy_call;\n\tsysent[SYS_chdir].sy_call = \u0026xtrace_chdir;\n\nThen call the original syscall:\n\n\tstatic sy_call_t *cb_exit = 0;\n\tstatic int xtrace_exit(struct thread *td, void *uap) {\n\t       return cb_exit(td, uap);\n\t}\n\nLicence\n--------------\n\n   Copyright (c) 2013-2019 Philippe Anel. All rights reserved.\n  \n   Redistribution and use in source and binary forms, with or without\n   modification, are permitted provided that the following conditions\n   are met:\n   1. Redistributions of source code must retain the above copyright\n      notice, this list of conditions and the following disclaimer.\n   2. Redistributions in binary form must reproduce the above copyright\n      notice, this list of conditions and the following disclaimer in the\n      documentation and/or other materials provided with the distribution.\n   4. Neither the name of the University nor the names of its contributors\n      may be used to endorse or promote products derived from this software\n      without specific prior written permission.\n  \n   THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\n   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n   ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\n   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\n   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n   SUCH DAMAGE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxigh%2Fx-trace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxigh%2Fx-trace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxigh%2Fx-trace/lists"}