Skip to content

Commit bd93de8

Browse files
[docs] Document tips for debugging assembly & object code.
1 parent e00ef28 commit bd93de8

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

docs/DebuggingTheCompiler.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,97 @@ the pipeline. Here is a quick summary of the various options:
553553
When printing IR for functions for print-[before|after]-all options, Only
554554
print the IR for functions whose name is in this comma separated list.
555555

556+
## Debugging assembly and object code
557+
558+
Understanding layout of compiler-generated metadata
559+
can sometimes involve looking at assembly and object code.
560+
561+
### Working with a single file
562+
563+
Here's how to generate assembly or object code:
564+
565+
```
566+
# Emit assembly in Intel syntax (AT&T syntax is the default)
567+
swiftc tmp.swift -emit-assembly -Xllvm -x86-asm-syntax=intel -o tmp.S
568+
569+
# Emit object code
570+
swiftc tmp.swift -emit-object -o tmp.o
571+
```
572+
573+
Understanding mangled names can be hard though: `swift demangle` to the rescue!
574+
575+
```
576+
swiftc tmp.swift -emit-assembly -Xllvm -x86-asm-syntax=intel -o - \
577+
| swift demangle > tmp-demangled.S
578+
579+
swiftc tmp.swift -emit-object -o tmp.o
580+
581+
# Look at where different symbols are located, sorting by address (-n)
582+
# and displaying section names (-m)
583+
nm -n -m tmp.o | swift demangle > tmp.txt
584+
585+
# Inspect disassembly of an existing dylib (AT&T syntax is the default)
586+
objdump -d -macho --x86-asm-syntax=intel /path/to/libcake.dylib \
587+
| swift demangle > libcake.S
588+
```
589+
590+
### Working with multiple files
591+
592+
Some bugs only manifest in WMO, and may involve complicated Xcode projects.
593+
Moreover, Xcode may be passing arguments via `-filelist`
594+
and expecting outputs via `-output-filelist`, and those file lists
595+
may be in temporary directories.
596+
597+
If you want to inspect assembly or object code for individual files when
598+
compiling under WMO, you can mimic this by doing the following:
599+
600+
```
601+
# Assuming all .swift files from the MyProject/Sources directory
602+
# need to be included
603+
find MyProject/Sources -name '*.swift' -type f > input-files.txt
604+
605+
# In some cases, projects may use multiple files with the same
606+
# name but in different directories (for different schemes),
607+
# which can be a problem. Having a file list makes working around
608+
# this convenient as you can manually manually edit out the files
609+
# that are not of interest at this stage.
610+
611+
mkdir Output
612+
613+
# 1. -output-filelist doesn't recreate a subdirectory structure,
614+
# so first strip out directories
615+
# 2. map .swift files to assembly files
616+
sed -e 's|.*/|Output/|;s|\.swift|.S|' input-files.txt > output-files.txt
617+
618+
# Save command-line arguments from Xcode's 'CompileSwiftSources' phase in
619+
# the build log to a file for convenience, say args.txt.
620+
#
621+
# -sdk /path/to/sdk <... other args ...>
622+
623+
xcrun swift-frontend @args.txt \
624+
-filelist input-files.txt \
625+
-output-filelist output-files.txt \
626+
-O -whole-module-optimization \
627+
-emit-assembly
628+
```
629+
630+
If you are manually calling `swift-frontend` without an Xcode invocation to
631+
use as a template, you will need to at least add
632+
`-sdk "$(xcrun --show-sdk-path macosx)"` (if compiling for macOS),
633+
and `-I /path/to/includedir` to include necessary swift modules and interfaces.
634+
635+
### Working with multi-architecture binaries
636+
637+
In certain cases, one might be looking at multi-architecture binaries,
638+
such as [universal binaries](https://en.wikipedia.org/wiki/Universal_binary).
639+
By default `nm` will show symbols from all architectures, so a universal
640+
binary might look funny due to two copies of everything. Use `nm -arch`
641+
to look at a specific architecture:
642+
643+
```
644+
nm -n -m -arch x86_64 path/to/libcake.dylib | swift demangle
645+
```
646+
556647
## Bisecting Compiler Errors
557648

558649
### Bisecting on SIL optimizer pass counts to identify optimizer bugs

0 commit comments

Comments
 (0)