# Protocol: debug, part 2 of 2

The `debug` commands a program sends to a running game, as the engine describes them: what each takes, what it answers and what it does. Back to [all commands](../protocol-commands.md).

## debug.state

`debug.state {} -> {attached: value, named: value, process: integer, state: text, step: integer, reason: text, file: text, line: integer, column: integer, nil: record, last_entity: text, last_entity_name: text, error: text, stops: integer, errors: text, note: text, pending_reload: [text]}` · read, read-only

Says whether the game is running or a script is stopped, and where and why. The server sends nothing by itself: ask again to follow the game.

| Result | Type | Meaning |
| --- | --- | --- |
| `attached` | value | true when the debugger is attached, false when not. |
| `named` | value | true when every loaded script is compiled with the names of its locals, so debug.scope can show them: it was loaded, reloaded or reset while the debugger was attached. false otherwise, and when no debugger is attached. |
| `process` | integer | The engine's own process id on this machine: where the game's scripts run, for a debugger of the machine to attach to. `frost dev` prints the same number on the line after the one that says where it listens, as `process PID`. |
| `state` | text | "running", or "paused" while a script is stopped part way through a step. |
| `step` | integer | How many fixed steps have run. |
| `reason` | text | Why it stopped: "breakpoint", "step", "pause" or "error". null while running. |
| `file` | text | The script or library it is stopped in, as the project names it. null while running. |
| `line` | integer | The line it stands on, which has not run yet; for an error, the line that raised it. null while running. |
| `column` | integer | The column on that line, from 1 in UTF-16 code units: the line's first code, as Luau stops at lines; for an error, where on the line it failed (the name that was nil, the call to error or assert). null while running. |
| `nil` | record | For an error that indexed or called a nil: {name, how, key, also, since}, the expression that was nil as written ("state.boss", "t.go", "t:go"), "index" or "call", the key it was indexed with or null, the other expressions on the line it may have been, and since (null here: a failed game.step says it). null otherwise. |
| `last_entity` | text | The id of the entity the script last read, wrote or removed in this step, like "e3.0": one of the many a script may touch, and most often the one an error is about. null when it touched none, and while running. |
| `last_entity_name` | text | That entity's scene name, or null. |
| `error` | text | The error's message when the reason is "error", else null. |
| `stops` | integer | How many times a script has stopped since this server started: it counts up at every stop, so a client that polls can tell a new stop from the one it knew, though file and line be the same. |
| `errors` | text | "stop": a script stops where it raises an error. "pass": the error ends the step as with no debugger. null when no debugger is attached. |
| `note` | text | null, or a sentence to show the person: the scripts were loaded before the debugger attached, so their locals have no names, and what to do about it. |
| `pending_reload` | [text] | The scripts whose new code waits for the stopped step to end: saved, or sent with script.reload, while a script was stopped. Empty otherwise. |

## debug.stack

`debug.stack {} -> {frames: [record], note: text}` · read, read-only

Lists the functions the stopped script is in, the innermost first.

| Result | Type | Meaning |
| --- | --- | --- |
| `frames` | [record] | Up to 64 {frame, file, line, column, function}: frame is the number debug.scope and debug.evaluate take, from 0; column is where on its line, from 1 in UTF-16 code units: the stop's for the innermost (debug.state's column), the line's first code for the others (Luau keeps no column of a call); function is the function's name, or null for one without a name. The engine's own functions are left out. |
| `note` | text | null, or a sentence to show the person: the scripts were loaded before the debugger attached, so their locals have no names, and what to do about it. |

## debug.scope

`debug.scope {frame?: integer} -> {frame: integer, locals: [record], upvalues: [record], note: text}` · read, read-only

Lists the local variables in scope in one function of the stopped script, and the variables of enclosing functions it uses.

| Parameters | Type | Meaning |
| --- | --- | --- |
| `frame?` | integer | The function, as debug.stack numbers them. Left out: 0, the innermost. |

| Result | Type | Meaning |
| --- | --- | --- |
| `frame` | integer | The function. |
| `locals` | [record] | One {name, type, value, ref, entity, entity_name} per local in scope, those of the innermost block first. entity is null, or for a number that is the handle of a living entity, its id like "e3.0", with its scene name or null in entity_name: a script holds an entity as a number, so a plain number that happens to equal a handle shows one too. type is the Luau type; value is the value as text, a string quoted and cut at 200 characters; ref is a number for debug.expand when the value is a table, else null. |
| `upvalues` | [record] | The same for the variables of enclosing functions that this function uses. |
| `note` | text | null, or a sentence to show the person: the scripts were loaded before the debugger attached, so their locals have no names, and what to do about it. |

## debug.expand

`debug.expand {ref: integer, from?: integer} -> {ref: integer, from: integer, entries: [record], more: value}` · read, read-only

Lists the entries of a table of the stopped script, by the reference it was handed out under.

| Parameters | Type | Meaning |
| --- | --- | --- |
| `ref` | integer | The ref of a table, from debug.scope, debug.expand or debug.evaluate since the script last stopped. |
| `from?` | integer | How many entries to pass over first: the `from` of the last answer plus the entries it held, to read on where it ended. Left out: 0. |

| Result | Type | Meaning |
| --- | --- | --- |
| `ref` | integer | The reference. |
| `from` | integer | How many entries were passed over. |
| `entries` | [record] | Up to 100 entries as {name, type, value, ref, entity, entity_name}, in the order Luau walks the table: name is the key, a key that is not a string in square brackets. |
| `more` | value | true when the table has entries after these, else false. |

## debug.evaluate

`debug.evaluate {text: text, frame?: integer} -> {type: text, value: text, ref: integer, entity: text, entity_name: text, step: integer, note: text}` · trusted-project-only

Runs Luau where the stopped script stands and returns what it gives. The text sees the function's locals and upvalues and everything the script sees, and may call the script API and change the game; assigning to a local changes a copy, not the script's variable. It runs under a script's limits of time and memory.

| Parameters | Type | Meaning |
| --- | --- | --- |
| `text` | text | An expression, or statements (their `return` gives the value). |
| `frame?` | integer | The function whose variables it sees, as debug.stack numbers them. Left out: 0. |

| Result | Type | Meaning |
| --- | --- | --- |
| `type` | text | The Luau type of the first value it gave; "nil" when it gave none. |
| `value` | text | The value as text. |
| `ref` | integer | A number for debug.expand when the value is a table, else null. |
| `entity` | text | null, or the id of the living entity the value is the handle of, as debug.scope says it. |
| `entity_name` | text | That entity's scene name, or null. |
| `step` | integer | How many fixed steps have run. |
| `note` | text | null, or a sentence to show the person: the scripts were loaded before the debugger attached, so a local named in the text read as nil. Text that fails then fails with E0444, which says the same. |
