---
name: debug
last_updated: 2026-12-04T23:21:36Z
type: library
summary: "This library provides functions useful for debugging and profiling code."
---

# debug

This library provides functions useful for debugging and profiling code.

**Type:** library

## Description

Provides a few basic functions for debugging code in Roblox. Unlike the
[debug](/docs/reference/engine/globals/debug.md) library found in Lua natively, this version has been heavily
sandboxed.

## Functions

### debug.traceback

**Signature:** `debug.traceback(message: string, level?: number): string`

Returns a traceback of the current function call stack as a string; in
other words, a description of the functions that have been called up to
this point. During debugging, this behaves like an error stack trace but
does not stop execution of the script.

The `level` parameter specifies what level of the call stack to consider,
with `1` (the default, and what `0` also gives) being the function that
called [debug.traceback()](/docs/reference/engine/globals/debug.md), `2` being the function that called
that function, and so on. The traceback lists the chosen frame and every
frame above it. See the code sample for an example of sequential function
calls.

Note that this function will often return inaccurate results (compared to
the original source code) and that the format of the returned traceback
may change at any time. You should **not** parse the return value for
specific information such as script names or line numbers.

The code sample includes sequential function calls; `fnB()` is called, and
it calls `fnA()` which then calls [debug.traceback()](/docs/reference/engine/globals/debug.md).

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `message` | `string` |  | The first line of the returned string. |
| `level` | `number` | `1` | The frame to start the traceback at. The traceback lists that frame and every caller above it. |

**Returns:** `string` — Traceback of the current function call stack.

**Debug Traceback**

Demonstrates `debug.traceback`, which describes the current call stack without
stopping execution.

```lua
local function fnA()
	-- The message becomes the first line of the traceback
	local trace = debug.traceback("Specific moment during fnA()")
	print(string.match(trace, "^[^\n]*")) --> Specific moment during fnA()
	print(string.find(trace, "fnA", 1, true) ~= nil) --> true

	-- A higher level starts further up the call stack, skipping fnA itself
	local callerTrace = debug.traceback("From the caller", 2)
	print(string.find(callerTrace, "fnA", 1, true) == nil) --> true
end

local function fnB()
	fnA()
end

-- Call function fnB() to begin traceback
fnB()

-- The format may change at any time, so use debug.info for structured data
```

### debug.traceback

**Signature:** `debug.traceback(thread: thread, message: string, level?: number): string`

Returns a traceback of the current function call stack as a string; in
other words, a description of the functions that have been called up to
this point. During debugging, this behaves like an error stack trace but
does not stop execution of the script.

The `level` parameter specifies what level of the call stack to consider,
with `1` (the default, and what `0` also gives) being the function the
coroutine is suspended in, `2` being the function that called that
function, and so on. The traceback lists the chosen frame and every frame
above it. See the code sample for an example with a coroutine.

Note that this function will often return inaccurate results (compared to
the original source code) and that the format of the returned traceback
may change at any time. You should **not** parse the return value for
specific information such as script names or line numbers.

The code sample inspects a coroutine that is suspended inside a nested
call.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `thread` | `thread` |  | A thread as returned by [coroutine.create()](/docs/reference/engine/globals/coroutine.md). |
| `message` | `string` |  | The first line of the returned string. |
| `level` | `number` | `1` | The frame to start the traceback at. The traceback lists that frame and every caller above it. |

**Returns:** `string` — Traceback of the current function call stack.

**Debug Traceback Of A Thread**

Demonstrates `debug.traceback` called with a coroutine to inspect where it is
suspended.

```lua
local function waitForInput()
	coroutine.yield()
end

local worker = coroutine.create(function()
	waitForInput()
end)
coroutine.resume(worker)

local trace = debug.traceback(worker, "Worker is stuck here")
print(string.match(trace, "^[^\n]*")) --> Worker is stuck here
print(#trace > #"Worker is stuck here") --> true
```

### debug.info

**Signature:** `debug.info(level: number, options: string): Tuple`

Allows programmatic inspection of the call stack. This function differs
from [debug.traceback()](/docs/reference/engine/globals/debug.md) in that it guarantees the format of the
data it returns. This is useful for general logging and filtering purposes
as well as for sending the data to systems expecting structured input,
such as crash aggregation.

Note that this function is similar to
[debug.getinfo](https://www.lua.org/pil/23.1.html), an unavailable part of
the standard Lua library which serves a similar purpose.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `level` | `number` |  | Determines at what level of the call stack the information returned should describe. A value of `1` represents the function which is calling [debug.info()](/docs/reference/engine/globals/debug.md), a value of `2` represents the function that called that function, and so on. A value of `0` represents [debug.info()](/docs/reference/engine/globals/debug.md) itself. |
| `options` | `string` |  | A string that describes what the returned information should represent. It must only contain 0 or 1 instances of the characters `slnaf`, each representing a piece of information:  - `s` ([string](/docs/en-us/luau/strings.md)) — The function source identifier,   equal to the full name of the script the function is defined in. - `l` ([number](/docs/en-us/luau/numbers.md)) — The line number of the function   call represented by `level`. - `n` ([string](/docs/en-us/luau/strings.md)) — The name of the function; may be   `nil` for anonymous functions and C functions without an assigned   debug name. - `a` ([number](/docs/en-us/luau/numbers.md), [boolean](/docs/en-us/luau/booleans.md)) —   Arity of the function, which refers to the parameter count and   whether the function is variadic. - `f` ([function](/docs/en-us/luau/functions.md)) — The function which was   inspected. |

**Returns:** `Tuple` — One value per character in `options`, in the order they appear: source
identifier (`s`), line number (`l`), function name (`n`), parameter
count and whether variadic (`a`), or the function itself (`f`).

**Debug Info By Level**

Demonstrates `debug.info` reading structured details about a call stack level.

```lua
local function fnA()
	-- Level 1 is fnA itself; level 2 is the function that called it
	for _, level in {1, 2} do
		print(level, debug.info(level, "l")) --> 1 9 / 2 15
	end
	print(debug.info(1, "n")) --> fnA
end

local function fnB()
	fnA()
end

fnB()

-- Unlike debug.traceback, the shape of the returned values is guaranteed
local line = debug.info(1, "l")
print(line) --> 21
```

### debug.info

**Signature:** `debug.info(function: function, options: string): Tuple`

Allows programmatic inspection of the call stack. This function differs
from [debug.traceback()](/docs/reference/engine/globals/debug.md) in that it guarantees the format of the
data it returns. This is useful for general logging and filtering purposes
as well as for sending the data to systems expecting structured input,
such as crash aggregation.

Note that this function is similar to
[debug.getinfo](https://www.lua.org/pil/23.1.html), an unavailable part of
the standard Lua library which serves a similar purpose.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `function` | `function` |  | The function of the call stack which the information returned should describe. |
| `options` | `string` |  | A string that describes what the returned information should represent. It must only contain 0 or 1 instances of the characters `slnaf`, each representing a piece of information:  - `s` ([string](/docs/en-us/luau/strings.md)) — The function source identifier,   equal to the full name of the script the function is defined in. - `l` ([number](/docs/en-us/luau/numbers.md)) — The line that `function` is   defined on. - `n` ([string](/docs/en-us/luau/strings.md)) — The name of the function; may be   `nil` for anonymous functions and C functions without an assigned   debug name. - `a` ([number](/docs/en-us/luau/numbers.md), [boolean](/docs/en-us/luau/booleans.md)) —   Arity of the function, which refers to the parameter count and   whether the function is variadic. - `f` ([function](/docs/en-us/luau/functions.md)) — The function which was   inspected. |

**Returns:** `Tuple` — One value per character in `options`, in the order they appear: source
identifier (`s`), line number (`l`), function name (`n`), parameter
count and whether variadic (`a`), or the function itself (`f`).

**Debug Info By Function**

Demonstrates `debug.info` inspecting a function directly for its line, name,
and arity.

```lua
local function fnA()

end

local function fnB(first, second, ...)

end

-- Output line ("l"), name ("n"), and the function itself ("f")
local line, name, fn = debug.info(fnA, "lnf")
print(line, name, fn == fnA) --> 6 fnA true

-- Arity ("a") returns the parameter count and whether the function is variadic
print(debug.info(fnB, "a")) --> 2 true
```

### debug.info

**Signature:** `debug.info(thread: thread, level: number, options: string): Tuple`

Allows programmatic inspection of the call stack. This function differs
from [debug.traceback()](/docs/reference/engine/globals/debug.md) in that it guarantees the format of the
data it returns. This is useful for general logging and filtering purposes
as well as for sending the data to systems expecting structured input,
such as crash aggregation.

Note that this function is similar to
[debug.getinfo](https://www.lua.org/pil/23.1.html), an unavailable part of
the standard Lua library which serves a similar purpose.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `thread` | `thread` |  | A thread as returned by [coroutine.create()](/docs/reference/engine/globals/coroutine.md). |
| `level` | `number` |  | Determines at what level of the call stack the information returned should describe. A value of `1` represents the function which is calling [debug.info()](/docs/reference/engine/globals/debug.md), a value of `2` represents the function that called that function, and so on. |
| `options` | `string` |  | A string that describes what the returned information should represent. It must only contain 0 or 1 instances of the characters `slnaf`, each representing a piece of information:  - `s` ([string](/docs/en-us/luau/strings.md)) — The function source identifier,   equal to the full name of the script the function is defined in. - `l` ([number](/docs/en-us/luau/numbers.md)) — The line number of the function   call represented by `level`. - `n` ([string](/docs/en-us/luau/strings.md)) — The name of the function; may be   `nil` for anonymous functions and C functions without an assigned   debug name. - `a` ([number](/docs/en-us/luau/numbers.md), [boolean](/docs/en-us/luau/booleans.md)) —   Arity of the function, which refers to the parameter count and   whether the function is variadic. - `f` ([function](/docs/en-us/luau/functions.md)) — The function which was   inspected. |

**Returns:** `Tuple` — One value per character in `options`, in the order they appear: source
identifier (`s`), line number (`l`), function name (`n`), parameter
count and whether variadic (`a`), or the function itself (`f`).

**Debug Info By Thread**

Demonstrates `debug.info` inspecting a level of a suspended coroutine's stack.

```lua
local function fetchData()
	coroutine.yield()
end

local worker = coroutine.create(function()
	fetchData()
end)
coroutine.resume(worker)

-- Level 1 is the function the coroutine is suspended in
print(debug.info(worker, 1, "l")) --> 7
```

### debug.profilebegin

**Signature:** `debug.profilebegin(label: string): ()`

Starts profiling for a
[MicroProfiler](/docs/en-us/performance-optimization/microprofiler.md) label.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `label` | `string` |  | The text that this [MicroProfiler](/docs/en-us/performance-optimization/microprofiler.md) label displays. |

**Returns:** `()`

**Debug Profile Begin**

Demonstrates `debug.profilebegin`, which opens a labelled MicroProfiler
section.

```lua
local function buildGrid(size)
	debug.profilebegin("BuildGrid")
	local grid = table.create(size * size, 0)
	debug.profileend()
	return grid
end

print(#buildGrid(32)) --> 1024
```

### debug.profileend

**Signature:** `debug.profileend(): ()`

Stops profiling for the most recent
[MicroProfiler](/docs/en-us/performance-optimization/microprofiler.md) label
that [debug.profilebegin()](/docs/reference/engine/globals/debug.md) opened.

**Returns:** `()`

**Debug Profile End**

Demonstrates `debug.profileend`, which closes the most recent
`debug.profilebegin` label.

```lua
debug.profilebegin("Outer")
debug.profilebegin("Inner")
-- Work done here is timed under both labels
debug.profileend() -- closes "Inner"
debug.profileend() -- closes "Outer"
```

### debug.getmemorycategory

**Signature:** `debug.getmemorycategory(): string`

Returns the name of the current thread's active memory category.

**Returns:** `string` — The current thread's active memory category.

**Debug Get Memory Category**

Demonstrates `debug.getmemorycategory`, which returns the current thread's
memory category name.

```lua
local category = debug.getmemorycategory()
print(type(category)) --> string

-- The category follows the thread, so a new one can differ from this one
local previous = debug.setmemorycategory("Pathfinding")
print(debug.getmemorycategory()) --> Pathfinding
debug.setmemorycategory(previous)
```

### debug.setmemorycategory

**Signature:** `debug.setmemorycategory(tag: string): string`

Assigns a custom tag name to the current thread's memory category in the
[Developer Console](/docs/en-us/studio/developer-console.md). Useful for analyzing
memory usage of multiple threads in the same script which would otherwise
be grouped together under the same tag/name. Returns the name of the
current thread's previous memory category.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `tag` | `string` |  | The custom name to assign to the current thread's memory category in the Developer Console. |

**Returns:** `string` — The current thread's previous memory category.

**Debug Set Memory Category**

Demonstrates `debug.setmemorycategory`, which tags the current thread's memory
in the Developer Console.

```lua
local original = debug.setmemorycategory("MapGeneration")
print(debug.getmemorycategory()) --> MapGeneration

-- Allocations made here are listed under "MapGeneration" in the Developer Console
local tiles = table.create(1000, 0)

-- The return value is the previous category, so it can be restored
print(debug.setmemorycategory(original) == "MapGeneration") --> true
print(#tiles) --> 1000
```

### debug.resetmemorycategory

**Signature:** `debug.resetmemorycategory(): ()`

Resets the tag assigned by [debug.setmemorycategory()](/docs/reference/engine/globals/debug.md) to the
automatically assigned value (typically, the script name).

**Returns:** `()`

**Debug Reset Memory Category**

Demonstrates `debug.resetmemorycategory`, which undoes
`debug.setmemorycategory`.

```lua
local automatic = debug.getmemorycategory()

debug.setmemorycategory("Spawning")
print(debug.getmemorycategory()) --> Spawning

debug.resetmemorycategory()
print(debug.getmemorycategory() == automatic) --> true
```

### debug.dumpcodesize

**Signature:** `debug.dumpcodesize(): ()`

Displays a table of native code size of individual functions and scripts.
This function is only available in the Command Bar in Studio, and raises
an error when called from anywhere else. It prints a "Native Code Size
Analysis" report to the Output window. The report starts with the number
of scripts and functions, the memory used, and the code size limit. It
then has one table for each script that opted into native code generation
with `--!native`, listing each function with its line, its native code
size and that size as a percentage of the limit, largest first. If no
native script has compiled, it prints a message saying so instead. More
details can be found on the
[Native Code Generation](/docs/en-us/luau/native-code-gen.md) page.

**Returns:** `()`

**Debug Dump Code Size**

Demonstrates `debug.dumpcodesize`, which prints native code sizes of functions
and scripts from the Command Bar.

```lua
-- debug.dumpcodesize() only works from the Studio Command Bar, where it prints a
-- report to the Output with one table per native script, largest functions first:
--   # Native Code Size Analysis
--     <n> scripts, <n> functions, <n> KB memory, 256 MB code size limit
--   ## <script name>
--     <n> functions, <n> KB memory
--     | Function | Line | Native Code Size | Native Code Size (%) |
-- Only scripts that start with --!native and have run appear in the report.

-- From a Script it raises an error, which pcall catches
local ok, message = pcall(debug.dumpcodesize)
print(ok) --> false
print(message) --> dumpcodesize can only be called from CommandBar
```