Introspection functions

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/functions/introspection.md)

You can use functions described in this chapter to introspect [ELF](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) and [DWARF](https://en.wikipedia.org/wiki/DWARF) for query profiling.

Note

These functions are slow and may impose security considerations.

For proper operation of introspection functions:

- Install the `clickhouse-common-static-dbg` package.
- Set the [allow\_introspection\_functions](/docs/operations/settings/settings#allow_introspection_functions) setting to 1.

  For security reasons introspection functions are disabled by default.

ClickHouse saves profiler reports to the [trace\_log](/docs/operations/system-tables/trace_log) system table. Make sure the table and profiler are configured properly.

## addressToLine[​](#addressToLine "Direct link to addressToLine")

Introduced in: v20.1.0

Converts a virtual memory address inside the ClickHouse server process to a filename and line number in ClickHouse's source code.

Note

This function is slow and may impose security considerations.

To enable this introspection function:

- Install the `clickhouse-common-static-dbg` package.
- Set setting [`allow_introspection_functions`](/docs/operations/settings/settings#allow_introspection_functions) to `1`.

**Syntax**

```
addressToLine(address_of_binary_instruction)
```

**Arguments**

- `address_of_binary_instruction` — Address of instruction in a running process. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns a source code filename and line number delimited by a colon, for example, `/build/obj-x86_64-linux-gnu/../src/Common/ThreadPool.cpp:199`. Returns the name of a binary, if no debug information could be found, otherwise an empty string, if the address is not valid. [`String`](/docs/sql-reference/data-types/string)

**Examples**

**Selecting the first string from the `trace_log` system table**

```
SET allow_introspection_functions=1;
SELECT * FROM system.trace_log LIMIT 1 \G;
```

```
-- The `trace` field contains the stack trace at the moment of sampling.
Row 1:
──────
event_date:              2019-11-19
event_time:              2019-11-19 18:57:23
revision:                54429
timer_type:              Real
thread_number:           48
query_id:                421b6855-1858-45a5-8f37-f383409d6d72
trace:                   [140658411141617,94784174532828,94784076370703,94784076372094,94784076361020,94784175007680,140658411116251,140658403895439]
```

**Getting the source code filename and the line number for a single address**

```
SET allow_introspection_functions=1;
SELECT addressToLine(94784076370703) \G;
```

```
Row 1:
──────
addressToLine(94784076370703): /build/obj-x86_64-linux-gnu/../src/Common/ThreadPool.cpp:199
```

**Applying the function to the whole stack trace**

```
-- The arrayMap function in this example processing each individual element of the trace array by the addressToLine function.
-- The result of this processing is seen in the trace_source_code_lines column of output.

SELECT
    arrayStringConcat(arrayMap(x -> addressToLine(x), trace), '\n') AS trace_source_code_lines
FROM system.trace_log
LIMIT 1
\G
```

```
Row 1:
──────
trace_source_code_lines: /lib/x86_64-linux-gnu/libpthread-2.27.so
/usr/lib/debug/usr/bin/clickhouse
/build/obj-x86_64-linux-gnu/../src/Common/ThreadPool.cpp:199
/build/obj-x86_64-linux-gnu/../src/Common/ThreadPool.h:155
/usr/include/c++/9/bits/atomic_base.h:551
/usr/lib/debug/usr/bin/clickhouse
/lib/x86_64-linux-gnu/libpthread-2.27.so
/build/glibc-OTsEL5/glibc-2.27/misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:97
```

## addressToLineWithInlines[​](#addressToLineWithInlines "Direct link to addressToLineWithInlines")

Introduced in: v22.2.0

Similar to `addressToLine`, but returns an Array with all inline functions.
As a result of this, it is slower than `addressToLine`.

To enable this introspection function:

- Install the `clickhouse-common-static-dbg` package.
- Set setting [`allow_introspection_functions`](/docs/operations/settings/settings#allow_introspection_functions) to `1`.

**Syntax**

```
addressToLineWithInlines(address_of_binary_instruction)
```

**Arguments**

- `address_of_binary_instruction` — The address of an instruction in a running process. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns an array whose first element is the source code filename and line number delimited by a colon. The second, third, etc. element list inline functions' source code filenames, line numbers and function names. If no debug information could be found, then an array with a single element equal to the name of the binary is returned, otherwise an empty array is returned if the address is not valid. [`Array(String)`](/docs/sql-reference/data-types/array)

**Examples**

**Applying the function to an address**

```
SET allow_introspection_functions=1;
SELECT addressToLineWithInlines(531055181::UInt64);
```

```
┌─addressToLineWithInlines(CAST('531055181', 'UInt64'))────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ ['./src/Functions/addressToLineWithInlines.cpp:98','./build_normal_debug/./src/Functions/addressToLineWithInlines.cpp:176:DB::(anonymous namespace)::FunctionAddressToLineWithInlines::implCached(unsigned long) const'] │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

**Applying the function to the whole stack trace**

```
SET allow_introspection_functions=1;

-- The arrayJoin function will split array to rows

SELECT
    ta, addressToLineWithInlines(arrayJoin(trace) AS ta)
FROM system.trace_log
WHERE
    query_id = '5e173544-2020-45de-b645-5deebe2aae54';
```