Garry's Mod Wiki


The debug library is intended to help you debug your scripts, however it also has several other powerful uses. Some builtin debug function were removed in GMod due to security reasons


Enters an interactive mode with the user, running each string that the user enters. Using simple commands and other debug facilities, the user can inspect global and local variables, change their values, evaluate expressions, and so on. A line containing only the word cont finishes this function, so that the caller continues its execution. Commands for debug.debug are not lexically nested within any function, and so have no direct access to local variables. This only works on the source dedicated server...
table debug.getfenv( table object )
Returns the environment of the passed object. This can be set with debug.setfenv
function, string, number debug.gethook( thread thread = nil )
Returns the current hook settings of the passed thread. The thread argument can be omitted. This is completely different to gamemode hooks. More information on hooks can be found at http://www. lua. org/pil/23. 2. html
table debug.getinfo( function funcOrStackLevel, string fields = "flnSu" )
Returns debug information about a function.
string, any debug.getlocal( thread thread = Current thread, number level, number index )
Gets the name and value of a local variable indexed from the level. When a function has a tailcall return, you cannot access the locals of this function.
table debug.getmetatable( any object )
Returns the metatable of an object. This function ignores the metatable's __metatable field.
table debug.getregistry()
Returns the internal Lua registry table. The Lua registry is used by the engine and binary modules to create references to Lua values. Avoid creating entries into the registry with a number as the key, as they are reserved for the reference system. Improper editing of the registry can result in unintended side effects, including crashing of the game.
string, any debug.getupvalue( function func, number index )
Used for getting variable values in an index from the passed function. This does nothing for C functions.
table debug.setfenv( table object, table env )
Sets the environment of the passed object.
debug.sethook( thread thread, function hook, string mask, number count )
Sets the given function as a Lua hook. This is completely different to gamemode hooks. The thread argument can be completely omitted and calling this function with no arguments will remove the current hook. This is used by default for infinite loop detection. More information on hooks can be found at Hooks are not always ran when code that has been compiled by LuaJIT's JIT compiler is being executed. This means that relying on them for infinite loop protection is unwise.
string debug.setlocal( thread thread = Current Thread, number level, number index, any value = nil )
This function was removed due to security concerns. Sets a local variable's value.
boolean debug.setmetatable( any object, table metatable )
Sets the object's metatable. Unlike setmetatable, this function works regardless of whether the first object passed is a valid table or not; this function even works on primitive datatypes such as numbers, functions, and even nil.
string debug.setupvalue( function func, number index, any val = nil )
This function was removed due to security concerns. Sets the variable indexed from func
Prints out the lua function call stack to the console.
string debug.traceback( thread thread = current thread, string message = "nil", number level = 1 )
Returns a full execution stack trace.
number debug.upvalueid( function func, number index )
This function was removed due to security concerns. Returns an unique identifier for the upvalue indexed from func
debug.upvaluejoin( function f1, number n1, function f2, number n2 )
This function was removed due to security concerns. Make the n1-th upvalue of the Lua closure f1 refer to the n2-th upvalue of the Lua closure f2.