Manuscript Documentation
v2026.12.0

Profiling and native debugging#

Use ms run --profile to find where a program spends CPU time. Build an unoptimized executable when you want to stop at a source line and follow execution with a native debugger. Start with the same input that reproduces the behavior you are investigating.

Prepare a repeatable workload#

Save this as words.ms. It splits a short line repeatedly and prints the number of words processed. The argument controls how much work to do:

Manuscript
import arguments from "@std/os/env.ms"

fn count_words(text String) Int {
    return text.split(" ").length()
}

fn main() Void {
    repeats := arguments().get_or(0, "1000000").parse_int() or 1000000
    var total = 0
    for index in 0..repeats {
        total += count_words("small programs are easy to review")
    }
    print(total)
}
sh
ms run words.ms -- 2
text
12

For your own application, use its real entry point and representative data. ms run --profile runs main, not test or benchmark blocks.

Capture a profile#

sh
ms run --profile words.ms --out-dir capture --top 10 -- 1000000

The program prints 6000000, followed by a summary of sampled functions and the bundle location. capture must be a new directory; choose another name for the next run. The default build is optimized Release, so you profile the code you would ship. Arguments after -- reach the program, and the command returns the program’s exit status.

Open capture/top.txt to read the same report again. Its columns are:

text
    flat   flat%      cum    cum%  function
  • flat counts samples where this function was the innermost frame: start here to find work done directly in a function.
  • cum counts samples whose stack contains the function, including its callees: use this to find callers responsible for expensive work.
  • The percentages use the total sample count. Counts and rankings vary between runs; they are not exact call counts or elapsed wall-clock measurements.

This example spends most of its work splitting and allocating strings, so [runtime] may lead the flat report while main has the cumulative samples. Optimization can inline the small count_words helper. A very short run may collect no samples; increase the workload before drawing conclusions. Measure normal execution separately, because profiling adds overhead.

The bundle keeps the executable and data for further investigation:

FileUse
programRerun the exact profiled executable.
top.txtRead the ranked function report.
profile.foldedLoad folded stacks into a compatible flamegraph viewer.
profile.pbOpen the profile in a pprof-compatible viewer.
manifest.jsonFind artifact names, sampling frequency and sample count.

Count allocations#

CPU samples show where time goes, not who asks for memory. Add --allocs to also sample allocations:

sh
ms run --profile words.ms --out-dir capture --allocs

The report then lists the functions that allocate most, in allocations and bytes, and the bundle adds allocs.txt, allocs.folded (stacks weighted by allocation count) and allocs.folded.bytes (weighted by bytes). A sample is a stack walk on every 128th allocation (set MS_PROFILE_ALLOC_PERIOD to change it; 1 counts every allocation), so counts are scaled estimates. Only the program’s main thread is sampled.

--allocs has the program’s runtime count each allocation while it runs. Without --allocs the runtime records nothing and pays no cost.

Use --json when another tool needs the versioned result instead of terminal prose. On Linux, the command also prints a perf record invocation for the executable; using it requires an installed perf and permission to sample processes.

Stop at a source line with LLDB#

Use an installed LLDB and keep words.ms beside your working directory. Build without optimization so source stepping follows the program more closely:

sh
ms build words.ms --build-type Debug -o words
./words 2
text
Built words
12

Launch the native executable:

sh
lldb ./words

At the LLDB prompt, set a file/line breakpoint on the return statement inside count_words (line 4 in the file above), confirm that it has a resolved location, then run a small input:

text
breakpoint set --file words.ms --line 4
breakpoint list
run 2

Once stopped, inspect the call stack and source, then move back to the caller:

text
thread backtrace
source list
thread step-out
thread step-over
breakpoint disable
continue

The final continue lets the program finish and print 12. File/line breakpoints select your code directly; a breakpoint named main can instead select the native startup wrapper. Generated symbol names may have suffixes, so do not guess them from Manuscript declarations. If a breakpoint has no locations, check the filename and line against the source used for that build and rebuild after edits.

With GDB , start gdb ./words, then use break words.ms:4, run 2, backtrace, list, finish, next, disable breakpoints, and continue for the same workflow.

What source information is available#

Native builds keep frame pointers and DWARF file and line mappings. These support source breakpoints, stack attribution and stepping. Optimized code can reorder or remove operations. Both backends also describe every Manuscript type as a structure (String, List[Int], Int?, records, enums), with member names and offsets.

  • Debug builds (--build-type debug) name and locate a frame’s parameters and the locals it declares, so info args, info locals and frame variable show them. Each callee the build inlined is a frame of its own with its own parameters and locals, so a backtrace and up still name it. A part with no location at the current instruction reads <optimized out>. A local that is assigned again after its declaration is not described, because it holds several values and naming the first would show a stale one.
  • Release builds (LLVM) describe the types but not variables, because variable debug info costs release compile time (about 4%) and size. Use source-level print calls or a focused test, and read a value you can address (a pointer from a register, a member, a stack slot) through its type, as below.

In GDB, ptype struct Point lists a record’s fields, and print *(struct Shape*)$rsp reads the bytes at an address as that type. Load the display helper from the repository to print strings as text, lists with their elements, optionals as some or null, and enums as the case with its fields:

text
source tools/debugger/manuscript_gdb.py
print *(struct Shape*)$rsp
text
$1 = Rect = {w = 2, h = 3}

A string is two words (an address, or the bytes themselves when short, and a tagged length); a list is an address, a length and a capacity; an enum is a case number followed by a payload shared by every case; an optional is a present flag followed by the value. The structure names (struct "List[String]") are those of the types in your source. In LLDB, load the matching helper after creating the target (it finds enums and optionals by their shape, and manuscript-refresh repeats that for programs loaded later):

text
command script import tools/debugger/manuscript_lldb.py
expression -- *(struct Shape*)$sp

On Windows, GDB and LLDB read the DWARF in the .exe and use the same helpers. Debuggers that need a PDB (Visual Studio, WinDbg) cannot read the type information, because the compiler does not emit a PDB. The decoding rules live in tools/debugger/manuscript_values.py, which tools/debugger/test_values.py tests without a debugger. In LLDB, name a list type with the script API (frame variable, expression), not a cast, since the name contains brackets.

On macOS, keep the adjacent words.dSYM bundle with the executable. ms build creates it when dsymutil is available; without it the executable runs but source line lookup is unavailable. On Windows, name the output words.exe and use a debugger that can read DWARF. Linux keeps the line information in the executable itself.

To investigate which functions can call each other before running, open an interactive code map . To compare an optimization with repeatable measurements, use benchmarks .