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:
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)
}ms run words.ms -- 212For your own application, use its real entry point and representative data. ms run --profile runs main, not test or benchmark blocks.
Capture a profile#
ms run --profile words.ms --out-dir capture --top 10 -- 1000000The 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:
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:
| File | Use |
|---|---|
program | Rerun the exact profiled executable. |
top.txt | Read the ranked function report. |
profile.folded | Load folded stacks into a compatible flamegraph viewer. |
profile.pb | Open the profile in a pprof-compatible viewer. |
manifest.json | Find 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:
ms run --profile words.ms --out-dir capture --allocsThe 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:
ms build words.ms --build-type Debug -o words
./words 2Built words
12Launch the native executable:
lldb ./wordsAt 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:
breakpoint set --file words.ms --line 4
breakpoint list
run 2Once stopped, inspect the call stack and source, then move back to the caller:
thread backtrace
source list
thread step-out
thread step-over
breakpoint disable
continueThe 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, soinfo args,info localsandframe variableshow them. Each callee the build inlined is a frame of its own with its own parameters and locals, so a backtrace andupstill 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
printcalls 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.
Print a value as Manuscript shows it#
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:
source tools/debugger/manuscript_gdb.py
print *(struct Shape*)$rsp$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):
command script import tools/debugger/manuscript_lldb.py
expression -- *(struct Shape*)$spOn 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 .