Skip to content

Commit

Permalink
Added optional tracing for some statement types.
Browse files Browse the repository at this point in the history
Not many can be done safely, currently only `index` and `assign` nodes.
  • Loading branch information
Avril112113 committed Jun 4, 2024
1 parent aa69d96 commit 2a6d5b2
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Basic usage example (values represent default)
// generate stack traces with correct file names and line numbers in-game.
// This should be `false` for releases
// as it affects runtime performance and will greatly impact the output size.
// Possible options: false, "simple" and "full"
"tracing": false
}
}
Expand Down
2 changes: 2 additions & 0 deletions tool/project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ end
---@field multiproject SSSWTool.MultiProject
---@field project SSSWTool.Project
---@field parser Parser
---@field config any


---@class SSSWTool.Project.Config
Expand Down Expand Up @@ -220,6 +221,7 @@ function Project:build()
multiproject = self.multiproject,
project = self,
parser = parser,
config = self.config.transformers[transformer_name],
})
if #errors > 0 then
print_error("-- Transformer Errors: " .. #errors .. " --")
Expand Down
10 changes: 7 additions & 3 deletions tool/src/tracing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ function SS_SW_DBG._hook_tbl(tbl, path)
local nindex = SS_SW_DBG._nindex
SS_SW_DBG._info[nindex] = {
["name"] = path .. "." .. i,
["line"] = 1,
["column"] = 1,
["line"] = -1,
["column"] = -1,
["file"] = "{_ENV}",
}
tbl[i] = function(...)
Expand Down Expand Up @@ -83,7 +83,11 @@ function SS_SW_DBG.stacktrace(depth)
prev_file = trace.file
table.insert(lines, (" '%s'"):format(trace.file))
end
table.insert(lines, ("%s %s @ %s:%s"):format(i, trace.name, trace.line, trace.column))
if trace.line == -1 and trace.column == -1 then
table.insert(lines, ("%s %s"):format(i, trace.name))
else
table.insert(lines, ("%s %s @ %s:%s"):format(i, trace.name, trace.line, trace.column))
end
end
return lines
end
Expand Down
93 changes: 93 additions & 0 deletions tool/transform_swaddon_tracing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ function TransformerDefs:_ensure_tracingblock(node)
local block = self._SWAddon_TracingBlock
table.insert(block, #block+1, ASTNodes.LineComment(source, "--", "#region SSSWTool-Tracing-src"))
table.insert(block, #block+1, ast)
table.insert(block, #block+1, ASTNodes.assign(
node, nil,
ASTNodes.varlist(node, ASTNodes.index(
node, nil, ASTNodes.name(node, SPECIAL_NAME),
ASTNodes.index(
node, ".", ASTNodes.name(node, "level")
)
)),
ASTNodes.expressionlist(node, ASTNodes.string(node, self.config == "full" and "full" or "simple"))
))
table.insert(block, #block+1, ASTNodes.LineComment(source, "--", "#endregion"))
table.insert(block, #block+1, ASTNodes.LineComment(source, "--", "#region SSSWTool-Tracing-info"))
table.insert(block, #block+1, ASTNodes.LineComment(source, "--", "#endregion"))
Expand Down Expand Up @@ -170,6 +180,11 @@ end

---@param node ASTNode
function TransformerDefs:funcbody(node)
if node._is_traced then
return node
end
node._is_traced = true

---@type ASTNodeSource
local root_source = assert(self:_get_root_source(node), "root_source ~= nil")

Expand Down Expand Up @@ -261,5 +276,83 @@ function TransformerDefs:funcbody(node)
)
end

local WHITELIST_STMT_TYPES = {
["assign"]=true,
["index"]=true,
}
---@param node ASTNode
function TransformerDefs:block(node)
if self.config ~= "full" then
return node
end
if node._is_traced then
return node
end
node._is_traced = true

if self:_get_root_source(node).block.block == node then
return node
end

---@type ASTNodeSource
local source = assert(self:find_parent_of_type(node, "source"), "parent source node ~= nil")

for i=#node,1,-1 do
local child = node[i]
if not WHITELIST_STMT_TYPES[child.type] then
goto continue
end
local start_line, start_column = source:calcline(child.start)
local local_file_path = "<UNKNOWN>"
if source.file then
if source.file:find("^<SSSWTOOL>[\\/]") then
local_file_path = AVPath.norm(source.file)
else
local_file_path = AVPath.relative(source.file, self.multiproject.project_path)
end
end
self._swdbg_index = self._swdbg_index and self._swdbg_index + 1 or 1
local name = "stmt_"..child.type
if child.type == "index" then
name = emitter:generate(child)
if name:find("\n") then
name = name:match("(.-)\n")
end
elseif child.type == "assign" then
local cpy = Utils.shallowcopy(child)
cpy.values = ASTNodes.expressionlist(child)
name = emitter:generate(cpy) .. " ="
end
self:_add_trace_info(node, ("`%s`"):format(name), start_line, start_column, local_file_path:gsub("<", "{"):gsub(">", "}"))
table.insert(node, i+1, ASTNodes.index(
node, nil, ASTNodes.name(node, SPECIAL_NAME),
ASTNodes.index(
node, ".", ASTNodes.name(node, "_trace_exit"),
ASTNodes.call(
node, ASTNodes.expressionlist(
node,
ASTNodes.numeral(node, tostring(self._swdbg_index))
)
)
)
))
table.insert(node, i, ASTNodes.index(
node, nil, ASTNodes.name(node, SPECIAL_NAME),
ASTNodes.index(
node, ".", ASTNodes.name(node, "_trace_enter"),
ASTNodes.call(
node, ASTNodes.expressionlist(
node,
ASTNodes.numeral(node, tostring(self._swdbg_index))
)
)
)
))
::continue::
end

return node
end


return TransformerDefs

0 comments on commit 2a6d5b2

Please sign in to comment.