Path: blob/main/sys/tools/syscalls/scripts/systrace_args.lua
39530 views
#!/usr/libexec/flua1--2-- SPDX-License-Identifier: BSD-2-Clause3--4-- Copyright (c) 2024 Tyler Baxter <[email protected]>5-- Copyright (c) 2023 Warner Losh <[email protected]>6-- Copyright (c) 2019 Kyle Evans <[email protected]>7--89-- Setup to be a module, or ran as its own script.10local systrace_args = {}11local script = not pcall(debug.getlocal, 4, 1) -- TRUE if script.12if script then13-- Add library root to the package path.14local path = arg[0]:gsub("/[^/]+.lua$", "")15package.path = package.path .. ";" .. path .. "/../?.lua"16end1718local FreeBSDSyscall = require("core.freebsd-syscall")19local util = require("tools.util")20local generator = require("tools.generator")2122-- File has not been decided yet; config will decide file. Default defined as23-- /dev/null.24systrace_args.file = "/dev/null"2526function systrace_args.generate(tbl, config, fh)27-- Grab the master system calls table.28local s = tbl.syscalls2930-- Bind the generator to the parameter file.31local gen = generator:new({}, fh)32gen.storage_levels = {} -- make sure storage is clear3334-- 64-bit padding preprocessor directive.35gen:pad64(config.abiChanges("pair_64bit"))3637-- Write the generated preamble.38gen:preamble(39"System call argument to DTrace register array conversion.\n" ..40"\n" ..41"This file is part of the DTrace syscall provider.")4243gen:write(string.format([[44static void45systrace_args(int sysnum, void *params, uint64_t *uarg, int *n_args)46{47int64_t *iarg = (int64_t *)uarg;48int a = 0;49switch (sysnum) {50]]))5152gen:store(string.format([[53static void54systrace_entry_setargdesc(int sysnum, int ndx, char *desc, size_t descsz)55{56const char *p = NULL;57switch (sysnum) {58]]), 1)5960gen:store(string.format([[61static void62systrace_return_setargdesc(int sysnum, int ndx, char *desc, size_t descsz)63{64const char *p = NULL;65switch (sysnum) {66]]), 2)6768for _, v in pairs(s) do6970gen:write(v.prolog);71gen:store(v.prolog, 1);72gen:store(v.prolog, 2);7374-- Handle non compat:75if v:native() then76gen:write(string.format([[77/* %s */78case %d: {79]], v.name, v.num))8081gen:store(string.format([[82/* %s */83case %d:84]], v.name, v.num), 1)8586gen:store(string.format([[87/* %s */88case %d:89]], v.name, v.num), 2)9091local n_args = #v.args92if v.type.SYSMUX then93n_args = 094end9596if #v.args > 0 and not v.type.SYSMUX then97local padding = ""9899gen:write(string.format([[100struct %s *p = params;101]],102v.arg_alias))103104gen:store([[105switch (ndx) {106]],1071)108109for idx, arg in ipairs(v.args) do110local argtype = util.trim(111arg.type:gsub(112"__restrict$", ""), nil)113if argtype == "int" and114arg.name == "_pad" and115config.abiChanges("pair_64bit") then116gen:store(117"#ifdef PAD64_REQUIRED\n",1181)119end120121-- Pointer arg?122local desc123if argtype:find("*") then124desc = "userland " .. argtype125else126desc = argtype;127end128129gen:store(string.format([[130case %d%s:131p = "%s";132break;133]],134idx - 1, padding, desc), 1)135136if argtype == "int" and137arg.name == "_pad" and138config.abiChanges("pair_64bit") then139padding = " - _P_"140gen:store([[141#define _P_ 0142#else143#define _P_ 1144#endif145]],1461)147end148149if util.isPtrType(argtype,150config.abi_intptr_t) then151gen:write(string.format([[152uarg[a++] = (%s)p->%s; /* %s */153]],154config.ptr_intptr_t_cast,155arg.name, argtype))156elseif argtype == "union l_semun" then157gen:write(string.format([[158uarg[a++] = p->%s.buf; /* %s */159]],160arg.name, argtype))161elseif argtype:sub(1,1) == "u" or162argtype == "size_t" then163gen:write(string.format([[164uarg[a++] = p->%s; /* %s */165]],166arg.name, argtype))167else168if argtype == "int" and169arg.name == "_pad" and170config.abiChanges(171"pair_64bit") then172gen:write([[173#ifdef PAD64_REQUIRED174]])175end176177gen:write(string.format([[178iarg[a++] = p->%s; /* %s */179]],180arg.name, argtype))181182if argtype == "int" and183arg.name == "_pad" and184config.abiChanges(185"pair_64bit") then186gen:write("#endif\n")187end188end189end190191gen:store([[192default:193break;194};195]],1961)197198if padding ~= "" then199gen:store("#undef _P_\n\n", 1)200end201202gen:store(string.format([[203if (ndx == 0 || ndx == 1)204p = "%s";205break;206]], v.ret), 2)207end208209gen:write(string.format("\t\t*n_args = %d;\n\t\tbreak;\n\t}\n",210n_args))211gen:store("\t\tbreak;\n", 1)212213-- Handle compat (everything >= FREEBSD3):214-- Do nothing, only for native.215end216end217218gen:write(tbl.epilog)219gen:write([[220default:221*n_args = 0;222break;223};224}225]])226227gen:store(tbl.epilog, 1)228gen:store([[229default:230break;231};232if (p != NULL)233strlcpy(desc, p, descsz);234}235]], 1)236237gen:store(tbl.epilog, 2)238gen:store([[239default:240break;241};242if (p != NULL)243strlcpy(desc, p, descsz);244}245]], 2)246247-- Write all stored lines.248if gen.storage_levels ~= nil then249gen:writeStorage()250end251252end253254-- Entry of script:255if script then256local config = require("config")257258if #arg < 1 or #arg > 2 then259error("usage: " .. arg[0] .. " syscall.master")260end261262local sysfile, configfile = arg[1], arg[2]263264config.merge(configfile)265config.mergeCompat()266267-- The parsed system call table.268local tbl = FreeBSDSyscall:new{sysfile = sysfile, config = config}269270systrace_args.file = config.systrace -- change file here271systrace_args.generate(tbl, config, systrace_args.file)272end273274-- Return the module.275return systrace_args276277278