Modifying Luau stack trace?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would like to remove and modify certain parts of the stack trace.

  2. What is the issue? Include screenshots / videos if possible!
    Althrough I already used coroutine.wrap, it still shows some parts that I would like to be removed.

  <This is a module>:<Line number>: string:3: <This is a module>:<Line number>: test error
  Stack Begin
  Script '<This is a module>', Line 15 - function <Method A>
  Script 'require(<This is a module>).new(game.ServerScriptService.Script, game.ServerScriptService.Script.Source).<Method A>() warn("DONE")', Line 1
  Stack End

Apologies if the stack trace looks strange, I replaced a bunch of names with placeholder as this is a private module

I would like to entirely remove the stack trace or modify the stack trace message with a spoofed one.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
  • Switching from task.spawn to coroutine.wrap
  • Searching the Roblox Developer Forum and Roblox Developer Hub

Can I ask why you would want to do this?

I don’t really want to say much, but let’s say that I am making a script sandboxing module where I can control the environment and stuff (something like H3x/H6x I guess)

If it’s a sandbox just replace the traceback function with your own function that does what you want in the sandbox’s environment.

1 Like

I mean, the answer is pretty much no.

There’s debug.traceback, which is not the same thing but similar. You could manipulate the string or something.

There’s also a second argument to error that lets you erase lower levels from the resulting trace.

But modifying a stack trace from an error you don’t control is, to my knowledge, impossible.

That’s the best I can give ya with your secrecy :slight_smile:

This doesn’t work since the error function is on the C side and I assume Roblox directly use C functions to get the traceback, same goes to other C functions that can error.

Thanks then, for now I’ll just “hide” the stack trace with coroutine.create + coroutine.resume + task.spawn-ing the error function if script execution halted

Only way I could think of actually having control over the stack is to sandbox the code in your own in-lua VM. Modules that mimic loadstring do this and you will have full control over the “virtualized” stack within the code responsible for running the parsed bytecode. Other than that, as mike said, there is no way to modify the stack trace within Roblox.

I made my module easy to switch between the Roblox-provided loadstring or Yueliang + FiOne

No way I can think of doing it through Roblox’s loadstring but FiOne being a bytecode interpreter would allow you to handle errors differently through modification of the interpreter itself. No idea how FiOne is structured so I cannot give you an exact answer though.

Took a quick look at FiOne,

local function on_lua_error(failed, err)
	local src = failed.source
	local line = failed.lines[failed.pc - 1]

	error(string.format('%s:%i: %s', src, line, err), 0)
end

Editing this should allow you to modify stack trace as a result of errors.

2 Likes

I instead modified the error message from my module before you replied:

task.spawn(function()
	local CThread = coroutine.create(self.CompiledScriptChunk)
	local CodeExecutionCompleted, ErrorMessage = coroutine.resume(CThread)
	if not CodeExecutionCompleted then
		task.spawn(error, string.gsub(string.gsub(ErrorMessage, WrapperFullPath..":168: ", ""), "string", self.LuaSourceContainer:GetFullName()), 0)
	end
	return
end)