11# lensm
22
3- A tool for viewing assembly and source.
3+ A tool for viewing assembly and source side by side .
44
55![ Screenshot] ( ./screenshot.webp )
66
7- Install with the usual Go commands:
7+ I wrote a [ blog post] ( https://www.storj.io/blog/lensm ) on why and how the
8+ core of it works.
9+
10+ ## Install
811
912```
1013go install loov.dev/lensm@main
1114```
1215
13- _ For Linux you may need to add some [ additional dependencies] ( https://gioui.org/doc/install/linux ) . You can use ` go install --tags nowayland loov.dev/lensm@main ` or ` go install --tags nox11 loov.dev/lensm@main ` respectively to skip building Wayland or X11 version._
16+ On Linux you may need some [ additional
17+ dependencies] ( https://gioui.org/doc/install/linux ) . Add ` -tags nowayland `
18+ or ` -tags nox11 ` to skip building the Wayland or X11 backend.
1419
15- Build this checkout locally :
20+ To build this checkout:
1621
1722```
1823go build -o lensm .
1924```
2025
21- GitHub Actions also builds installable macOS disk images for Apple Silicon
22- and Intel. Download the DMG artifact from a successful CI run, open it, and
23- drag ` Lensm.app ` to Applications. CI builds use an ad-hoc signature, so macOS
24- may require the first launch through ** Control-click → Open** .
25-
26- To build the DMG locally on macOS:
26+ GitHub Actions builds macOS disk images for Apple Silicon and Intel.
27+ Download the DMG artifact from a CI run, open it and drag ` Lensm.app ` to
28+ Applications. The builds have an ad-hoc signature, so the first launch
29+ may need Control-click, Open. To build the DMG yourself:
2730
2831```
2932./scripts/build-macos-dmg.sh 0.0.0 1 "$(go env GOARCH)"
3033```
3134
32- On Linux, use the same Gio tags if you want to skip a windowing backend:
33-
34- ```
35- go build -tags nowayland -o lensm .
36- go build -tags nox11 -o lensm .
37- ```
35+ ## Usage
3836
39- Run the program with an executable path, or start it without arguments
40- and load an executable from the top bar. On macOS, use Choose... to select
41- the executable with the native Finder file dialog. The function filter is
42- available inside the UI. ` -watch ` allows to automatically reload the
43- executable and information when it changes.
37+ Pass the binary on the command line, or start lensm without arguments
38+ and open one from the toolbar.
4439
4540```
4641lensm -watch lensm
4742```
4843
49- Flags:
44+ The binary has to be built on your machine, otherwise lensm can't find
45+ the source files.
5046
5147| Flag | Meaning |
5248| ------| ---------|
@@ -59,142 +55,108 @@ Flags:
5955| ` -profile path ` | show a pprof profile of the binary next to the code |
6056| ` -cpuprofile path ` | write a CPU profile of lensm itself |
6157
62- Keys:
63-
64- - ` Cmd/Ctrl+F ` in the code finds text in the shown columns of the open
65- function; ` Enter ` or ` Cmd/Ctrl+G ` steps to the next match, with ` Shift `
66- to the previous, and ` Escape ` closes the find bar;
67- - ` Cmd/Ctrl+F ` elsewhere focuses the function filter; ` Down ` or ` Enter ` moves into the
68- list, and ` Enter ` there keeps the previewed function open in a tab;
69- - ` Cmd/Ctrl+P ` goes to a function by name, or by an address such as
70- ` 0x4a2f10 ` from a stack trace or profile, selecting its instruction;
71- - ` ? ` lists the keyboard shortcuts;
72- - ` Cmd/Ctrl+O ` opens a binary and ` Cmd/Ctrl+, ` opens settings;
73- - ` Ctrl+Tab ` and ` Ctrl+Shift+Tab ` switch tabs, ` Cmd/Ctrl+W ` closes one;
74- - follow call targets and use ` Alt+Left/Right ` (or ` Cmd/Ctrl+[ ` and
75- ` Cmd/Ctrl+] ` ) to navigate between functions;
76- - in the code, ` Up ` /` Down ` , ` Page Up ` /` Page Down ` and ` Home ` /` End ` move
77- between instructions or source lines, with ` Shift ` to extend a
78- selection, and ` Tab ` moves to the counterpart in the other column. In
79- the assembly ` Right ` follows a jump or call and ` Left ` returns from a
80- followed jump;
81- - hover an assembly instruction to see its reference and a simplified
82- explanation when Lensm has a matching rule;
83- - drag across Go assembly, native assembly, or source lines to select a block,
84- then use ` Cmd/Ctrl+C ` to copy it. ` Shift ` extends a selection and
85- ` Escape ` clears it;
86- - right-click an instruction to scroll its source line beside it, or a
87- source line to scroll its instructions beside it; when the line
88- compiled into several runs of instructions, each right-click steps to
89- the next run;
90- - the Callers panel in the sidebar lists the functions that call the
91- open one; click one to open it. The Summary panel lists every
92- function by size, spills, bounds checks, allocations or write
93- barriers, largest first;
94- - open a pprof profile of the binary with Open Profile… in the menu, or
95- ` -profile ` , to see each instruction's and source line's share of the
96- samples in a lane beside them (click the profile in the status bar to
97- pick which of its values to show, such as allocated bytes instead of
98- in-use ones; each binary remembers its profile, and with Watch on a
99- rewritten profile reloads), and to rank functions by samples in
100- the Summary panel. A call's row shows the time spent below it, marked
101- ↳, and a source line counts the time in its calls too, as pprof's
102- list does. With a profile the Callers panel puts the callers that
103- spend the most through the open function first, and the Call graph
104- panel draws the hottest callers and callees around it; click a
105- function in the graph to open it. The first button on the right edge,
106- or Profile Graph in the menu, draws the whole profile's graph in a
107- resizable pane right of the code, as pprof's web view does, with a node's text growing with
108- its own time. Drag or scroll to pan, and hold Command or Ctrl and
109- scroll to zoom. Above it, find functions by name (Enter steps
110- through them), focus on the stacks through the open function, hide
111- the runtime so its time counts in the code calling it, and pick the
112- least share a function needs to be drawn. The second button on the
113- right edge draws a flame graph of the same samples instead, in a
114- resizable pane across the bottom of the window, callers on top.
115- Settings picks whether each graph goes right of or below the code,
116- and whether the flame graph's root is at the top or the bottom. Click a frame to zoom to
117- it, and double-click it or hold Command or Ctrl while clicking to
118- open its function. Click a frame above, or press Escape, to zoom out
119- a step, and Home to see it all again. Enter in the search zooms to
120- the next frame found, widest first. Hold Command or Ctrl and scroll
121- to zoom about the pointer, and drag or scroll to pan.
122- Compare Profile With… in
123- the menu loads a profile to compare with, from this build or another,
124- and the graphs then show the change from it by function name, as
125- pprof's -diff_base does: red where time grew, green where it shrank. The button at the end lets
126- it fill the window. The profile's addresses are matched
127- to the binary by function, so a profile of a position independent
128- binary works;
129- - Compare With… in the menu loads another build of the program and
130- shows the open function from both side by side, with the instructions
131- aligned and the removed and added ones marked. Addresses and branch
132- distances, which shift whenever code moves, don't count as changes;
133- - turn on "Sync scroll" in the status bar to scroll the assembly and the
134- source together;
135- - double-click a line, or press ` Enter ` on a selected one, to comment on it;
136- ` Escape ` leaves the comment.
58+ ## Keys
59+
60+ ` Cmd ` is ` Ctrl ` outside macOS. ` ? ` in the app lists all of them.
61+
62+ | Key | Action |
63+ | -----| --------|
64+ | ` Cmd+P ` | go to a function by name, or by an address such as ` 0x4a2f10 ` |
65+ | ` Cmd+F ` | in the code, find in the open function; elsewhere, filter the function list |
66+ | ` Down ` , ` Enter ` | from the filter, move into the function list; ` Enter ` there keeps the previewed function open |
67+ | ` Enter ` , ` Cmd+G ` | next match, with ` Shift ` the previous |
68+ | ` Cmd+O ` | open a binary |
69+ | ` Cmd+, ` | settings |
70+ | ` Cmd+B ` | show or hide the sidebar |
71+ | ` Ctrl+Tab ` , ` Ctrl+Shift+Tab ` | next and previous tab |
72+ | ` Cmd+W ` | close the tab |
73+ | ` Alt+Left ` , ` Alt+Right ` | back and forward between functions, also ` Cmd+[ ` and ` Cmd+] ` |
74+ | ` Up ` , ` Down ` , ` Page Up ` , ` Page Down ` , ` Home ` , ` End ` | move between instructions or source lines, with ` Shift ` to select |
75+ | ` Tab ` | move to the matching line in the other column |
76+ | ` Right ` , ` Left ` | follow a jump or call, and come back |
77+ | ` Cmd+C ` | copy the selection |
78+ | ` Enter ` | comment on the selected line |
79+ | ` Escape ` | close the find bar, clear the selection or leave a comment |
80+
81+ Double-click a line to comment on it. Right-click an instruction to
82+ bring its source line beside it, or a source line to bring its
83+ instructions; when a line compiled into several runs, each right-click
84+ steps to the next.
85+
86+ ## Profiles
87+
88+ Open a pprof profile with Open Profile in the menu or ` -profile ` , and
89+ each instruction and source line shows its share of the samples. The
90+ buttons on the right edge show the whole profile as a call graph or a
91+ flame graph. Compare Profile With loads a second profile and shows the
92+ change, red where time grew and green where it shrank.
93+
94+ In the graphs, drag or scroll to pan and hold ` Cmd ` and scroll to zoom.
95+ In the flame graph, click a frame to zoom to it, double-click or
96+ ` Cmd ` -click to open its function, and press ` Escape ` to zoom out.
97+
98+ ## Comparing builds
99+
100+ Compare With in the menu loads another build of the program and shows
101+ the open function from both side by side. The instructions are aligned
102+ and the removed and added ones marked. Addresses and branch distances
103+ don't count as changes, since they shift whenever code moves.
104+
105+ ## MCP
137106
138107Run lensm as an MCP server over stdio:
139108
140109```
141110lensm mcp [-comments ./lensm.lensm-comments.json] ./lensm
142111```
143112
144- Flags go before the path: ` lensm mcp ./lensm -comments x ` is a usage error.
145-
146- The MCP server exposes tools for listing functions, reading a function's
147- source with its assembly in Go and native syntax, and reading or writing
148- comments. ` get_function ` returns 500 instructions at a time; ` views ` ,
149- ` offset ` and ` limit ` select what comes back. By default comments are stored
150- in a sidecar file named ` <executable>.lensm-comments.json ` .
151-
152- The GUI can also serve MCP over HTTP: Settings starts a server at
153- ` http://127.0.0.1:7077/mcp ` (or another free port if that one is taken) for
154- the binary that is open, and it follows whatever the window loads. It only
155- listens on loopback and refuses requests from web pages, but it has no
156- authentication, so any program running as any user on the machine can read
157- the code and write comments while it runs.
158-
159- When the GUI and a separate ` lensm mcp ` process write the same sidecar,
160- saves merge per comment: each process's additions, edits, and deletions
161- survive, and only conflicting edits to the same comment resolve to the
162- last writer. The merge is read-then-write without a file lock, so two
163- saves landing in the same instant can still lose one side's changes.
164- Comments are keyed by function name, not by binary: point ` -comments ` at
165- one shared file only for builds of the same program.
166-
167- Binaries from other compilers work too. Functions come from the symbol
168- table and source from DWARF, so a C or C++ program built with ` -g ` shows
169- its source alongside the assembly; on macOS the debug info lives in the
170- ` .dSYM ` bundle next to the binary, which is read automatically. C++ and
171- Rust symbols are demangled — for C++ the signature is kept, so overloads
172- stay apart. A function is listed under the file it was written in, which
173- for heavily inlined code is not the file its first instruction belongs to.
174-
175- Note: The program requires a binary that is built on your computer, otherwise the source code for the functions cannot be loaded.
176-
177- WebAssembly modules (` GOOS=wasip1 GOARCH=wasm ` , TinyGo, or any ` .wasm ` ) are
178- recognised by their magic bytes and shown as WAT, one instruction per line
179- with calls resolved to function names. Source comes from whichever line
180- table the module carries. Go embeds a pclntab in its data segments, which
181- records a position per resume point rather than per instruction, so a run
182- of instructions between two resume points shares one line. TinyGo and
183- clang emit DWARF instead, which is per statement. All of TinyGo's wasm
184- targets work. A ` wasip2 ` build is a component rather than a plain module,
185- so its functions come from the core modules nested inside it and are
186- listed under the module they belong to, as in ` main/main.sumInts ` .
187-
188- Microcontroller builds work too. TinyGo's Cortex-M targets (` pico ` , ` pico2 ` ,
189- ` feather-m4 ` , ` teensy40 ` , ...) produce Thumb code, which is decoded by a
190- disassembler generated from ARM's architecture reference XML — Thumb-1,
191- Thumb-2 and the floating-point extension, IT blocks included, with branch
192- targets resolved to function names and the literal pools inside functions
193- shown as ` .word ` data. 32-bit RISC-V targets such as the ESP32-C3 are
194- decoded with the RISC-V decoder; AVR (` arduino ` , ` attiny ` , ...) and Xtensa
195- (` esp32-* ` , ` nodemcu ` , ` d1mini ` ) by decoders of their own, in the syntax
196- of the GNU objdump for each.
197-
198- ## Why?
199-
200- I wrote a blog post at https://www.storj.io/blog/lensm on why and how the core functionality works.
113+ Flags go before the path.
114+
115+ The server has tools to list functions, read a function's source with
116+ its Go and native assembly, and read and write comments. ` get_function `
117+ returns 500 instructions at a time, and ` views ` , ` offset ` and ` limit `
118+ pick what comes back.
119+
120+ The app can serve MCP over HTTP too. Settings starts a server at
121+ ` http://127.0.0.1:7077/mcp ` , or another port if that one is taken, for
122+ the binary that is open. It listens only on loopback and refuses
123+ requests from web pages, but it has no authentication: any program on
124+ the machine can read the code and write comments while it runs.
125+
126+ Comments are saved next to the binary in
127+ ` <executable>.lensm-comments.json ` , keyed by function name. When the app
128+ and ` lensm mcp ` save the same file, each keeps the other's changes, and
129+ only two edits of the same comment go to the last writer. The merge takes
130+ no lock, so two saves at the same instant can still lose one side.
131+ Share one comments file only between builds of the same program.
132+
133+ ## Other compilers
134+
135+ Functions come from the symbol table and source from DWARF, so a C, C++
136+ or Rust program built with ` -g ` shows its source too. On macOS lensm
137+ reads the debug info from the ` .dSYM ` bundle next to the binary. C++ and
138+ Rust names are demangled, and C++ keeps the signature so overloads stay
139+ apart. A function is listed under the file it was written in, which for
140+ inlined code isn't always the file of its first instruction.
141+
142+ ### WebAssembly
143+
144+ Modules from ` GOOS=wasip1 GOARCH=wasm ` , TinyGo or anything else are shown
145+ as WAT, with calls resolved to function names.
146+
147+ - Go records a source position per resume point, not per instruction, so
148+ the instructions between two resume points share one line.
149+ - TinyGo and clang write DWARF, which is per statement. All of TinyGo's
150+ wasm targets work.
151+ - A ` wasip2 ` build is a component; its functions are listed under the
152+ core module they belong to, as in ` main/main.sumInts ` .
153+
154+ ### Microcontrollers
155+
156+ - TinyGo's Cortex-M targets (` pico ` , ` pico2 ` , ` feather-m4 ` , ` teensy40 ` ,
157+ ...) are Thumb code. The decoder is generated from ARM's architecture
158+ reference and covers Thumb-1, Thumb-2, IT blocks and floating point.
159+ Literal pools show as ` .word ` data.
160+ - 32-bit RISC-V targets, such as the ESP32-C3, use the RISC-V decoder.
161+ - AVR (` arduino ` , ` attiny ` , ...) and Xtensa (` esp32-* ` , ` nodemcu ` ,
162+ ` d1mini ` ) have decoders of their own, in GNU objdump's syntax.
0 commit comments