# Debug

## debug.isvalidlevel

```lua
<boolean> debug.isvalidlevel(<number> level)
```

Returns whether `level` is a valid level or not.

***

## debug.getregistry

```lua
<table<function | thread>> debug.getregistry()
```

Returns the lua registry.

***

## debug.getconstant

```lua
<any> debug.getconstant(<function | number> func, <number> index)
```

Returns the constant at `index` in the constant table of the function or level `func`. Throws an error if the constant does not exist.

***

## debug.getconstants

```lua
<table<any>> debug.getconstants(<function | number> func)
```

Returns the constant table of the function or level `func`.

***

## debug.getinfo

```lua
<DebugInfo> debug.getinfo(<function | number> func)
```

Returns debugger information about a function or stack level.

#### DebugInfo

| Field         | Type     | Description                                                                        |
| ------------- | -------- | ---------------------------------------------------------------------------------- |
| `source`      | string   | The name of the chunk that created the function.                                   |
| `short_src`   | string   | A "printable" version of `source` to be used in error messages.                    |
| `func`        | function | The function itself.                                                               |
| `what`        | string   | The string "Lua" if the function is a Luau function, or "C" if it is a C function. |
| `currentline` | number   | The current line where the given function is executing.                            |
| `name`        | string   | The name of the function.                                                          |
| `nups`        | number   | The number of upvalues in the function.                                            |
| `numparams`   | number   | The number of parameters in the function.                                          |
| `is_vararg`   | number   | Whether the function has a variadic argument.                                      |

***

## debug.getproto

```lua
<function | table<function>> debug.getproto(<function | number> func, <number> index, <boolean?> active)
```

Returns the proto at `index` in the function or level `func` if `active` is false.

If `active` is true, then every active function of the proto is returned.

***

## debug.getprotos

```lua
<table<function>> debug.getprotos(<function | number> func)
```

Returns a list of protos of the function or level `func`.

***

## debug.getstack

```lua
<any | table<any>> debug.getstack(<function | number> func, <number?> index)
```

Returns the value at `index` in the stack frame `level`. Throws an error if no value could be found.

If `index` is not specified, then the entire stack frame is returned.

***

## debug.getupvalue

```lua
<any> debug.getupvalue(<function | number> func, <number> index)
```

Returns the upvalue at `index` in the function or level `func`. Throws an error if the upvalue does not exist.

An upvalue is a local variable used by an inner function, and is also called an *external local variable*.

Read more on [Lua visibility rules](http://www.lua.org/manual/5.1/manual.html#2.6).

***

## debug.getupvalues

```lua
<table<any>> debug.getupvalues(<function | number> func)
```

Returns a list of upvalues of the function or level `func`.

***

## debug.setconstant

```lua
<nil> debug.setconstant(<function | number> func, <number> index, <any> value)
```

Sets the constant at `index` in the function or level `func` to `value`.

***

## debug.setstack

```lua
<nil> debug.setstack(<function | number> func, <number> index, <any> value)
```

Sets the register at `index` in the stack frame `level` to `value`.

***

## debug.setupvalue

```lua
<nil> debug.setupvalue(<function | number> func, <number> index, <any> value)
```

Sets the upvalue at `index` in the function or level `func` to `value`.

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://potassium.gitbook.io/api/environment/debug.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
