From 2dfb5da16b97a6c12e01977af5b5188522178a4e Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 20 Apr 2026 21:50:41 +0200 Subject: zen trace analysis support (#945) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Integrates the **tourist** trace analysis library and builds a full `zen trace` command suite for working with Unreal Engine `.utrace` files. ### Trace analysis library (`thirdparty/tourist/`) - Adds the tourist library as a third-party dependency with three modules: **foundation** (platform primitives, memory, scheduling), **trace** (UE Trace protocol decoding), and **analysis** (event dispatching and analyzer framework). - Cross-platform support for Windows, Linux, and macOS. ### `zen trace` CLI commands (`src/zen/cmds/`, `src/zen/trace/`) - **`zen trace analyze`** — Summarize a `.utrace` file: session metadata, thread inventory, command line + build configuration, CPU profiling scopes, timing, event rates, log messages, and (with symbols) memory allocation metrics including live-allocs dumps, callstack-keyed aggregation, and allocation churn. Optional HTML output for memory reports. - **`zen trace inspect`** — Dump the event schema (declared types, fields, sizes) from a trace file. - **`zen trace trim`** — Extract a time-window from a trace into a new `.utrace` file. - **`zen trace serve`** — Launch a local HTTP server hosting an interactive trace viewer; opens in the default browser. ### Symbolication (`src/zen/trace/symbol_resolver.*`, `thirdparty/raw_pdb/`) - Pluggable resolver with multiple backends: `pdb` (in-tree raw_pdb), `dbghelp` (Windows), `llvm-symbolizer` (all platforms), `atos` (macOS). An `auto` backend picks the best available tool per platform. - Microsoft Symbol Server support: downloads PDBs on demand using a redirect-aware HTTP client. - Local PDB cache keyed by image GUID preserves symbols across binary recompilation. - Callstack trimming heuristic strips UE internal noise from reports. - Binary analysis cache (`.ucache_z`) avoids re-resolving the same trace. ### Interactive trace viewer (`src/zen/frontend/html/`, `src/zen/trace/trace_viewer_service.*`) - Timeline: scope-level detail, horizontal zoom/pan, vertical scrolling, viewport-driven loading with pre-computed LOD for responsive navigation of large traces. - Thread grouping (collapsible sidebar sections) synthesized from name suffixes, natural sort order, visual distinction between lane threads and OS threads. - Bookmark and region annotations; region categories with per-category toggles; bookmark marker toggle in the toolbar. - Filterable Logs tab showing captured `UE_LOG` output. - Stats tab with per-scope aggregate statistics. - Memory tab with interactive allocation analysis and an allocation size histogram. - CsvProfiler event parsing and chart UI. ### Other in-branch supporting changes - **Cross-platform browser launcher** (`browser_launcher.{h,cpp}`) used by `trace serve`. - **`ReciprocalU64`** fast 64-bit integer division (zencore/intmath) for trace analyzers. - **`parallelsort`** cross-platform parallel sort helper (zenutil). - Frontend zip build rule so the viewer's HTML assets are bundled into `zen.exe`. - `/Zo` flag for better optimized debug info on Windows release builds. - `trace-tests.cpp` in the `zen-test` harness (harness itself landed on main via #985). --- thirdparty/raw_pdb/src/PDB_IPIStream.cpp | 140 +++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 thirdparty/raw_pdb/src/PDB_IPIStream.cpp (limited to 'thirdparty/raw_pdb/src/PDB_IPIStream.cpp') diff --git a/thirdparty/raw_pdb/src/PDB_IPIStream.cpp b/thirdparty/raw_pdb/src/PDB_IPIStream.cpp new file mode 100644 index 000000000..dfd21bc56 --- /dev/null +++ b/thirdparty/raw_pdb/src/PDB_IPIStream.cpp @@ -0,0 +1,140 @@ +// Copyright 2011-2022, Molecular Matters GmbH +// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause) + +#include "PDB_PCH.h" +#include "PDB_IPIStream.h" +#include "PDB_RawFile.h" +#include "PDB_Util.h" +#include "PDB_DirectMSFStream.h" +#include "PDB_InfoStream.h" +#include "Foundation/PDB_Memory.h" + +namespace +{ + // the IPI stream always resides at index 4 + static constexpr const uint32_t IPIStreamIndex = 4u; +} + + +// ------------------------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------------------------ +PDB::IPIStream::IPIStream(void) PDB_NO_EXCEPT + : m_header() + , m_stream() + , m_records(nullptr) + , m_recordCount(0u) +{ +} + + +// ------------------------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------------------------ +PDB::IPIStream::IPIStream(IPIStream&& other) PDB_NO_EXCEPT + : m_header(PDB_MOVE(other.m_header)) + , m_stream(PDB_MOVE(other.m_stream)) + , m_records(PDB_MOVE(other.m_records)) + , m_recordCount(PDB_MOVE(other.m_recordCount)) +{ + other.m_records = nullptr; + other.m_recordCount = 0u; +} + + +// ------------------------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------------------------ +PDB::IPIStream& PDB::IPIStream::operator=(IPIStream&& other) PDB_NO_EXCEPT +{ + if (this != &other) + { + PDB_DELETE_ARRAY(m_records); + + m_header = PDB_MOVE(other.m_header); + m_stream = PDB_MOVE(other.m_stream); + m_records = PDB_MOVE(other.m_records); + m_recordCount = PDB_MOVE(other.m_recordCount); + + other.m_records = nullptr; + other.m_recordCount = 0u; + } + + return *this; +} + + +// ------------------------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------------------------ +PDB::IPIStream::IPIStream(const RawFile& file, const IPI::StreamHeader& header) PDB_NO_EXCEPT + : m_header(header) + , m_stream(file.CreateMSFStream(IPIStreamIndex)) + , m_records(nullptr) + , m_recordCount(GetLastTypeIndex() - GetFirstTypeIndex()) +{ + // types in the IPI stream are accessed by their index from other streams. + // however, the index is not stored with types in the IPI stream directly, but has to be built while walking the stream. + // similarly, because types are variable-length records, there are no direct offsets to access individual types. + // we therefore walk the IPI stream once, and store pointers to the records for trivial O(N) array lookup by index later. + m_records = PDB_NEW_ARRAY(const CodeView::IPI::Record*, m_recordCount); + + // ignore the stream's header + size_t offset = sizeof(IPI::StreamHeader); + + // parse the CodeView records + uint32_t typeIndex = 0u; + while (offset < m_stream.GetSize()) + { + // https://llvm.org/docs/PDB/CodeViewTypes.html + const CodeView::IPI::Record* record = m_stream.GetDataAtOffset(offset); + const uint32_t recordSize = GetCodeViewRecordSize(record); + m_records[typeIndex] = record; + + // position the stream offset at the next record + offset += sizeof(CodeView::IPI::RecordHeader) + recordSize; + + ++typeIndex; + } +} + + +// ------------------------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------------------------ +PDB::IPIStream::~IPIStream(void) PDB_NO_EXCEPT +{ + PDB_DELETE_ARRAY(m_records); +} + + +// ------------------------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------------------------ +PDB_NO_DISCARD PDB::ErrorCode PDB::HasValidIPIStream(const RawFile& file) PDB_NO_EXCEPT +{ + const PDB::InfoStream infoStream(file); + if (!infoStream.HasIPIStream()) + { + return ErrorCode::InvalidStream; + } + + DirectMSFStream stream = file.CreateMSFStream(IPIStreamIndex); + if (stream.GetSize() < sizeof(IPI::StreamHeader)) + { + return ErrorCode::InvalidStream; + } + + const IPI::StreamHeader header = stream.ReadAtOffset(0u); + if (header.version != IPI::StreamHeader::Version::V80) + { + return ErrorCode::UnknownVersion; + } + + return ErrorCode::Success; +} + + +// ------------------------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------------------------ +PDB_NO_DISCARD PDB::IPIStream PDB::CreateIPIStream(const RawFile& file) PDB_NO_EXCEPT +{ + DirectMSFStream stream = file.CreateMSFStream(IPIStreamIndex); + + const IPI::StreamHeader header = stream.ReadAtOffset(0u); + return IPIStream { file, header }; +} -- cgit v1.2.3