Commit Graph

465 Commits

Author SHA1 Message Date
Bartosz Taudul
c9d3ec0088 Harden external thread name lookups against dead threads.
A thread exiting between fopen and read leaves the comm and status
buffers under-filled or uninitialized: zero-initialize them, treat a
short or empty read as unknown, and never scan the status when the read
returned nothing.
2026-08-31 01:12:39 +02:00
Bartosz Taudul
e5d20e1377 Free the tid buffer when thread enumeration yields nothing.
A task directory yielding no numeric entries (the target exiting
mid-enumeration) returned 0 with the freshly allocated array still
handed back through *out, and the caller's failure path returned without
freeing it.
2026-08-31 01:12:39 +02:00
Bartosz Taudul
da0d2328f5 Require per-target events for external sampling success.
SysTraceStart only failed when no event of any kind opened, but in
external mode the global sched/vsync tracepoints (pid -1) succeed
independently of the target: a target exiting after thread enumeration
left every per-target open failing while the startup still reported
success. s_ctxBufferIdx is the per-target ring count right after the
per-target setup, so in external mode require it to be non-zero as well.
2026-08-31 01:12:39 +02:00
Bartosz Taudul
8db1a298c4 Open external sampling as CPU-gated per-CPU events.
The previous external shape - one per-task (cpu = -1) inherit event per
existing thread - cannot be mmap'd at all: perf_mmap() in
kernel/events/core.c refuses inherited per-task counters (-EINVAL, all
children would write the same ring), so no sample ring was ever created.
A per-CPU event filtered on the target's tgid (the self-profiling shape)
only covers the group leader; the kernel does not retro-inherit onto
pre-existing sibling threads.

Open one CPU-gated event per ring instead - perf_event_open(attr, pid,
cpu), one open per CPU and per target thread, the same shape as perf's
open loop (tools/perf/util/evsel.c:3031). The thread enumeration picks
the fan-out: at launch, per-CPU events on the target pid, inherited by
every later-spawned thread; on attach, per-thread per-CPU events for
every existing tid. Failing opens degrade gracefully: the affected
thread is simply not sampled.
2026-08-31 01:12:39 +02:00
Bartosz Taudul
37410933db Always deliver the sample IP.
Add PERF_SAMPLE_IP to the callstack sample: for code compiled without
frame pointers the kernel delivers no user stack, and such samples were
dropped entirely. When the callchain count is zero, synthesize a
one-frame trace holding just the leaf IP.
2026-08-31 01:12:39 +02:00
Bartosz Taudul
afe1ec5faf Factor the sample event open into OpenSampleEvent.
The seven sampling event setups repeated the same open/mmap/retry
sequence. On a refused open the retry now stays user-space only
(exclude_kernel and exclude_callchain_kernel both still require
perf_allow_kernel), and a failed open no longer breaks out of the whole
event loop.
2026-08-31 01:12:39 +02:00
Bartosz Taudul
d440f5e055 Report whether the data port is listening. 2026-08-31 01:12:39 +02:00
Bartosz Taudul
ae70255b4e Adopt a pre-bound listen socket. 2026-08-31 01:12:39 +02:00
Bartosz Taudul
b887def929 Record and expose system tracing startup failure. 2026-08-31 01:12:38 +02:00
Bartosz Taudul
7aee673792 Use the target executable time for external captures.
The worker must key source and symbol transfers on the target's
executable, not the monitor's own binary; the exe mtime was read
straight from /proc/<pid>/exe by InitExternalTarget (the readlink'd path
is namespace-dependent and carries a " (deleted)" suffix). For an
external target the time may be unavailable, so drop the assert on a
nonzero exectime.
2026-08-31 01:12:38 +02:00
Bartosz Taudul
81f3c71ef3 Transfer machine code for external targets.
For an external target the symbol code query addresses are VMAs in the
target's address space, not pointers in the monitor's, so read them out
with ReadExternalTargetMemory. Kernel code (the top bit marks it) is
shared by the monitor and its target alike, so the kernel path applies
in both modes.
2026-08-31 01:12:38 +02:00
Bartosz Taudul
d8a011630d Add an API to read external target memory.
ReadExternalTargetMemory reads bytes out of the target's address space:
process_vm_readv for live bytes (gated like ptrace access), falling back
to the target's on-disk image located by scanning /proc/<pid>/maps
directly - the caller runs while the symbol thread may be rebuilding the
shared image cache on refresh.
2026-08-31 01:12:38 +02:00
Bartosz Taudul
1f1d3ab856 Resolve external callstacks to the full inline chain.
The resolver previously captured only the first pcinfo result, so inline
frames were lost, and reported ELF virtual addresses the server could
never re-map to the target. Mirror the in-process pcinfo+syminfo flow,
and report symAddr as the target VMA (loadBias + ELF vaddr) so the
server's re-queries can resolve it.
2026-08-31 01:12:38 +02:00
Bartosz Taudul
2e566513dd Create external backtrace states from openable paths only.
backtrace_create_state_for_file opens the file lazily on first use and,
on a failed open, silently falls back to /proc/self/exe - so a state
created from a path the monitor cannot open would symbolize the target's
addresses against the monitor's own binary. Probe the target's root path
(and, for unlinked images, the mapping's map_files entry), and only
create a state for a path that actually opens.
2026-08-31 01:12:38 +02:00
Bartosz Taudul
2a40f6f990 Converge the external image list on refresh.
The list only ever added entries, so a mapping the target re-mapped or
unloaded stayed in the list and could shadow the new mapping or keep
serving an already-unloaded library. Rebuild it from the target's
current maps on each refresh, carrying over the cached backtrace state
for unchanged mappings and dropping entries the current maps no longer
confirm. The dropped entries' paths are abandoned rather than freed:
queued callstack items still carry the path pointer, which the worker
thread reads at its own pace. Strip the kernel's " (deleted)" suffix so
unlinked images converge to their canonical path.
2026-08-31 01:12:38 +02:00
Bartosz Taudul
0e7040d73b Derive the external load base from the mapping's own segment.
The minVaddr heuristic assumes the mapping's file offset is measured
against the lowest PT_LOAD vaddr of the image. The kernel maps each
PT_LOAD independently, at load_bias + ELF_PAGESTART(p_vaddr) from
p_offset - ELF_PAGEOFFSET(p_vaddr) (fs/binfmt_elf.c, elf_map), so the
assumption only holds for the segment carrying the minimum vaddr. Match
the mapping to its PT_LOAD through the exact offset relation the kernel
used, and take the base from that segment's own vaddr; the minVaddr
heuristic remains as the fallback. The header must be ELF64
(e_ident[4]): the fixed-size elf_ehdr/elf_phdr structs misparse other
classes, and a false segment match on misparsed headers would return a
silently wrong base.
2026-08-31 01:12:38 +02:00
Bartosz Taudul
bd1d783017 Resolve external images through the target mount namespace.
The image paths from /proc/<pid>/maps are only valid in the target's
mount namespace, so resolve them through /proc/<pid>/root (proc(5)). For
unlinked images the kernel keeps the bytes alive in the mapping's
map_files entry, which is opened directly instead.
2026-08-31 01:12:38 +02:00
Bartosz Taudul
d352907e5d Drop the magic monitor globals. 2026-08-31 01:12:37 +02:00
Bartosz Taudul
4e520cfc18 Add the external target API. 2026-08-31 01:12:34 +02:00
Bartosz Taudul
6b58f7d8d0 Report system tracing setup failures.
SysTraceStart previously reported success even when no perf events
could be opened, leaving a worker thread running over no buffers
and an empty CPU section without explanation; it now fails in that
case. Also log when /proc/kallsyms cannot be read, which otherwise
silently results in ??? kernel stack frames.
2026-08-19 22:18:59 +02:00
Bartosz Taudul
05f0508684 Fix system tracing on kernels older than 4.1.
perf_event_attr.use_clockid only exists since Linux 4.1 and
sample_max_stack since Linux 4.8; older kernels (e.g. the 3.18
kernels of 32-bit Android devices) reject the attributes with
EINVAL/E2BIG, which silently disabled all of system tracing.
Classify the running kernel from uname(2) and only send the fields
its perf_event_open ABI supports.
2026-08-19 22:18:57 +02:00
Bartosz Taudul
275fef576a Bring Lua on-demand connection matching logic up-to-date with C++ side. 2026-08-19 20:30:40 +02:00
Bartosz Taudul
2a3792aaa3 Handle source code queries in no-callstack builds.
The symbol worker thread, which answers source code queries, only exists
when call stack support is compiled in. Handle the query directly in the
main thread so it is always answered and the server can terminate.
2026-08-19 17:54:44 +02:00
Bartosz Taudul
fee9285d68 Use the compiler builtin for the ARM thread id.
The mrc to c13/c0/3 (thread id register) is undefined on ARMv5 (e.g.
ARM926EJ-S) and SIGILL'd during init. __builtin_thread_pointer lowers
to the same mrc on targets with a thread pointer register and to
__aeabi_read_tp on soft-TLS targets.
2026-08-18 22:35:44 +02:00
Bartosz Taudul
16d9c15663 Include TracyMangle.hpp in TracyDebug.hpp for consistent delayed init state. 2026-08-18 00:30:28 +02:00
Bartosz Taudul
4e9121eb2f Typos. 2026-08-18 00:28:32 +02:00
Bartosz Taudul
7f7bd69195 Remove TRACY_DELAYED_INIT option.
Delayed init was introduced 2019-02-19 in ef5e30056 and was lazy init at
that time, matching the name.

This changed 2020-05-19 in 4eb78f5c8, when auto-init was added, making
delayed init not delayed anymore.

While the option is still needed to make manual lifetime work, and it has
to be enabled on apple, there's no reason anymore to expose it to users.
2026-08-18 00:26:24 +02:00
Bartosz Taudul
2a7542ccd8 Mangle the GetToken accessor based on config.
The macro fast paths only reference GetProfiler from -O0 code, direct
calls, and ON_DEMAND builds: the single GetProfiler() call on the macro
path sits behind the constexpr-dead callstack guard and is folded away
at -O1+, so Release builds carried no config fingerprint and mismatched
clients linked silently.

GetToken is the hot-path binding of every queueing macro and is
referenced at all optimization levels; mangle it the same way so
mismatches fail at link time in Release builds too.
2026-08-17 22:21:57 +02:00
Bartosz Taudul
453d649c1a Add TRACY_ASSERT for custom assert implementations. 2026-08-16 13:32:44 +02:00
Bartosz Taudul
8fdfd08af0 Refactor GPU context id handling.
The gpu backends each used a bare 255 as the sentinel for a
not-yet-initialized context id. Define InvalidGpuContextId (-1) in
TracyQueue.hpp and replace the scattered 255s and their asserts
with it.

Id exhaustion is handled by an error message and an assert in the
new NextGpuContextId() getter. Non-assert builds continue execution,
at which point they are no longer valid. Handling this code path
is out of scope here. Various attempts at handling the exhaustion
problem have been otherwise purged from the API implementations.
2026-08-15 21:46:29 +02:00
Bartosz Taudul
756411d802 Allow disabling wait stacks capture. 2026-08-14 01:28:22 +02:00
Bartosz Taudul
d579ed3cce Fix VSync on 32-bit win32 targets.
The struct sizes are kernel sizes. A 64-bit kernel is required for
tracing anyway.
2026-08-13 18:34:58 +02:00
Bartosz Taudul
edc09a4e97 Fix 16-bit string length desync on offset recovery
The decoder recovered offset-encoded 16-bit string lengths into a
uint16_t, so sz += ProtocolOffset8Bit truncated lengths in [65536,
65791] back into [0, 255] in release builds, desynchronizing the
stream. Read the wire value into uint16_t sz16 and recover into a
uint32_t sz, relying on automatic promotion for the addition.

Align the client asserts with the encoder's actual capacity
(ProtocolOffset8Bit + uint16 max).
2026-08-13 18:18:58 +02:00
Bartosz Taudul
821d43c6b5 Send string for SectionSetup during on-demand deferred messages replay. 2026-08-13 18:15:20 +02:00
Bartosz Taudul
5d2c353335 Fix TRACY_NO_CODE_TRANSFER source code transfer. 2026-08-07 20:44:51 +02:00
Bartosz Taudul
d91769b72e Check if there's a parent directory to go to on .. in NormalizePath. 2026-08-03 18:26:49 +02:00
Bartosz Taudul
df29943d78 Add TRACY_ON_DEMAND guards to C API GPU functions. 2026-08-02 20:44:46 +02:00
Bartosz Taudul
9209bcdafd Add connectionId protection to C API zone functions. 2026-08-02 19:57:13 +02:00
nyanpasu64
bfdf647114 Rename TracyFormat.hpp to .h since it's used in the C API 2026-08-01 18:19:01 -07:00
nyanpasu64
0558301e49 Address LLM review comments 2026-08-01 18:15:27 -07:00
nyanpasu64
71098f707c TracyCZoneTextF/TracyCZoneNameF 2026-08-01 16:27:55 -07:00
Bartosz Taudul
3448832b72 Replace impactful cases of strcpy with strzcpy. 2026-08-01 00:37:35 +02:00
Bartosz Taudul
c136232c97 Use strzcpy in client library. 2026-08-01 00:06:39 +02:00
Bartosz Taudul
bc85c4faa5 Fix include guard. 2026-07-31 18:12:13 +02:00
Bartosz Taudul
3baa2f0a09 Fix typo. 2026-07-29 18:57:03 +02:00
Bartosz Taudul
46b21c0c75 Update ARM CPU identifiers. 2026-07-29 18:54:10 +02:00
Bartosz Taudul
a02513c1a2 Update Tim Apple devices list. 2026-07-29 15:18:46 +02:00
Bartosz Taudul
3e1de56e15 Add section category to section enter message. 2026-07-14 19:12:59 +02:00
Bartosz Taudul
859709a98a Add client-side section category setup. 2026-07-14 19:12:58 +02:00
Bartosz Taudul
59013f2a8c Add GetNextSectionId() for getting section id. 2026-07-10 16:05:37 +02:00