Build and debug LLVM in Visual Studio - shijunjing/edk2 GitHub Wiki

Build LLVM9.0 Clang and LLD in Visual Studio 2019

C:\steven\LLVM>git clone https://github.com/llvm/llvm-project.git
C:\steven\LLVM>cd llvm-project
C:\steven\LLVM\llvm-project>git checkout main
C:\steven\LLVM\llvm-project>mkdir _build_debug
C:\steven\LLVM\llvm-project>mkdir _install_debug
C:\steven\LLVM\llvm-project>cd _build_debug
C:\steven\LLVM\llvm-project\_build_debug>cmake ..\llvm -G "Visual Studio 16 2019" -A x64 -DLLVM_TARGETS_TO_BUILD="X86" -DCMAKE_BUILD_TYPE="RelWithDebInfo" -Thost=x64 -DLLVM_ENABLE_PROJECTS="clang;lld;compiler-rt;clang-tools-extra" -DCLANG_BUILD_EXAMPLES=ON -DCMAKE_INSTALL_PREFIX=C:\steven\LLVM\llvm-project\_install_debug 

click the C:\steven\LLVM\llvm-project>_build_debug\LLVM.sln to launch the Visual Studio 2019

Build LLVM9.0 Clang and LLD in Visual Studio 2017

C:\steven\LLVM>git clone https://github.com/llvm/llvm-project.git
C:\steven\LLVM>cd llvm-project
C:\steven\LLVM\llvm-project>git checkout llvmorg-9.0.0
C:\steven\LLVM\llvm-project>mkdir _build_debug
C:\steven\LLVM\llvm-project>mkdir _install_debug
C:\steven\LLVM\llvm-project>cd _build_debug
C:\steven\LLVM\llvm-project\_build_debug>cmake ..\llvm -G "Visual Studio 15 2017" -A x64 -DLLVM_TARGETS_TO_BUILD="X86" -DCMAKE_VERBOSE_MAKEFILE=ON  -DCMAKE_BUILD_TYPE="Debug" -DLLVM_ENABLE_ASSERTIONS=ON -Thost=x64 -DLLVM_ENABLE_PROJECTS="clang;lld"  -DCMAKE_INSTALL_PREFIX=C:\steven\LLVM\llvm-project\_install_debug
click the C:\steven\LLVM\llvm-project>_build_debug\LLVM.sln to launch the Visual Studio 2017

C:\steven\LLVM\llvm-project\_build_release>cmake ..\llvm -G "Visual Studio 15 2017" -A x64 -DLLVM_TARGETS_TO_BUILD="X86" -DCMAKE_VERBOSE_MAKEFILE=ON  -DCMAKE_BUILD_TYPE="MinSizeRel" -Thost=x64 -DLLVM_ENABLE_PROJECTS="clang;lld"  -DCMAKE_INSTALL_PREFIX=C:\steven\LLVM\llvm-project\_install_release
  • Note: if you set LLVM_ENABLE_PROJECTS to multiple projects with a semicolon-separated list, please don't forget to add quotation “” on the list string, e.g. "clang;lld". Otherwise, the string is empty.

  • If need build "Debug" binary, Click the C:\steven\LLVM\llvm-project2_build_debug\LLVM.sln to launch the Visual Studio 2017, then select project ALL_BUILD and right click to build:

  • If need build "MinSizeRel" binary, Click the C:\steven\LLVM\llvm-project>_build_release\LLVM.sln to launch the Visual Studio 2017, then select the "MinSizeRel" in the Solution Configuration menu as below picture, next select project ALL_BUILD and right click to build as above picture.

Debug Clang in Visual Studio 2017

Firstly, install the Microsoft Child Process Debugging Power Tool, which is a plugin of VS to automatically debug child processes (new processes spawned from a process that you are already debugging). The clang driver (main.cpp) creates child process (cc1_main.cpp) in the compilation. This VS plugin can let breakpoint works in the child process.

Secondly, right click Clang project and configure the Command,Command Arguments,Working Directory in the clang Property Pages as needed:

Thirdly, set breakpoints in the main entries of main.cpp and cc1_main.cpp as below:

Fourthly, set clang project as "Set as Startup Project" and then start new debugging instance as below:

To avoid rebuild projects before debug every time, right click the Solution 'LLVM' to select property and deselect all projects in the Configuration Build column as below:

Build edk2 OVMF via CLANGPDB toolchain with LLVM9.0:

Pls refer to https://github.com/lgao4/edk2/tree/CLANG9

C:\steven>git clone https://github.com/tianocore/edk2.git
C:\steven>cd edk2
C:\steven\edk2>clang -v
clang version 9.0.0 (tags/RELEASE_900/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
C:\steven\edk2>lld-link --version
LLD 9.0.0

C:\steven\edk2>edksetup.bat ForceRebuild
C:\steven\edk2>set CLANG_HOST_BIN=n
C:\steven\edk2>set IASL_PREFIX=C:\Asl\
C:\steven\edk2>build -a IA32 -a X64 -p OvmfPkg\OvmfPkgIa32X64.dsc -t CLANGPDB > build.log 2>&1

Build Simics BoardX58Ich10 via CLANGPDB toolchain with LLVM9.0:

C:\steven>rmdir Build /s /q
C:\steven>rmdir Conf /s /q
C:\steven\edk2>cd C:\steven\edk2-platforms\Platform\Intel
C:\steven\edk2-platforms\Platform\Intel>set CLANG_HOST_BIN=n
C:\steven\edk2-platforms\Platform\Intel>set IASL_PREFIX=C:\Asl\
C:\steven\edk2-platforms\Platform\Intel>set CLANG_BIN=C:\Program Files\LLVM\bin\
C:\steven\edk2-platforms\Platform\Intel>python build_bios.py -p BoardX58Ich10 -t CLANGPDB

Build EmulatorPkg via CLANGPDB toolchain with LLVM9.0:

cd /D C:\steven\edk2
call edksetup.bat
set CLANG_HOST_BIN=n
set CLANG_BIN=C:\Program Files\LLVM\bin\
build -p EmulatorPkg\EmulatorPkg.dsc -a X64 -DWIN_HOST_BUILD=TRUE -t CLANGPDB

The PDB File Format

How to read the PDB file

Use microsoft cvdump.exe to read the PDB file

c:\steven>git clone https://github.com/microsoft/microsoft-pdb.git
C:\steven\LLVM\wrongcode\lld-link5>C:\steven\microsoft-pdb\cvdump\cvdump.exe main.pdb
...
*** LINES

** Module: "assembly.obj" from "C:\steven\LLVM\wrongcode\lld-link5\assembly.lib"

Mod::GetEnumLines failed

** Module: "C:\steven\LLVM\wrongcode\lld-link5\lto.tmp"

  C:\steven\LLVM\wrongcode\lld-link5\main.c (MD5: 7C0C6D8EEF4BCA5B3DB38473E587E625), 0001:00000004-0000001E, line/addr pairs = 5

      5 00000004      0 00000008      8 0000000D      0 00000012
     12 00000017

** Module: "* Linker *" from ""

Mod::GetEnumLines failed
...

Use llvm-pdbutil.exe to read the PDB file

C:\steven\LLVM\wrongcode\lld-link5>C:\steven\LLVM\llvm-project\_build_debug\Debug\bin\llvm-pdbutil.exe dump -all main.pdb
... ...
                           Lines
============================================================
Mod 0000 | `assembly.obj`:
Mod 0001 | `C:\steven\LLVM\wrongcode\lld-link5\lto.tmp`:
C:\steven\LLVM\wrongcode\lld-link5\main.c (MD5: 7C0C6D8EEF4BCA5B3DB38473E587E625)
  0001:00000004-0000001E, line/addr entries = 5
     5 00000004 !    0 00000008 !    8 0000000D !    0 00000012 !   12 00000017 !

Mod 0002 | `* Linker *`:
... ...

Use DBH tool (dbh.exe) to see the PDB file info

C:\Program Files (x86)\Windows Kits\10\Debuggers\x64>dbh

dbh: load C:\steven\edk2\Build\Ovmf3264\DEBUG_CLANGPDB\IA32\MdeModulePkg\Core\Pei\PeiMain\OUTPUT\Peicore.pdb


Peicore [1000000]: o

c:/steven/edk2/Build/Ovmf3264/DEBUG_CLANGPDB/IA32/MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicSev/OUTPUT/Ia32/IoFifoSev.obj
c:/steven/edk2/Build/Ovmf3264/DEBUG_CLANGPDB/IA32/MdePkg/Library/BaseLib/BaseLib/OUTPUT/Ia32/CpuId.obj
c:/steven/edk2/Build/Ovmf3264/DEBUG_CLANGPDB/IA32/MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr/OUTPUT/Ia32/SetMem.obj
c:/steven/edk2/Build/Ovmf3264/DEBUG_CLANGPDB/IA32/MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr/OUTPUT/Ia32/CopyMem.obj
c:/steven/edk2/Build/Ovmf3264/DEBUG_CLANGPDB/IA32/MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr/OUTPUT/Ia32/ZeroMem.obj
c:/steven/edk2/Build/Ovmf3264/DEBUG_CLANGPDB/IA32/MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr/OUTPUT/Ia32/SetMem32.obj
c:/steven/edk2/Build/Ovmf3264/DEBUG_CLANGPDB/IA32/MdePkg/Library/BaseLib/BaseLib/OUTPUT/Ia32/LShiftU64.obj
c:/steven/edk2/Build/Ovmf3264/DEBUG_CLANGPDB/IA32/MdePkg/Library/BaseLib/BaseLib/OUTPUT/Ia32/RShiftU64.obj
c:/steven/edk2/Build/Ovmf3264/DEBUG_CLANGPDB/IA32/MdePkg/Library/BaseLib/BaseLib/OUTPUT/Ia32/InternalSwitchStack.obj
c:/steven/edk2/Build/Ovmf3264/DEBUG_CLANGPDB/IA32/MdePkg/Library/BaseLib/BaseLib/OUTPUT/Ia32/DivU64x32Remainder.obj
c:/steven/edk2/Build/Ovmf3264/DEBUG_CLANGPDB/IA32/MdePkg/Library/BaseLib/BaseLib/OUTPUT/Ia32/DivU64x32.obj
c:/steven/edk2/Build/Ovmf3264/DEBUG_CLANGPDB/IA32/MdePkg/Library/BaseLib/BaseLib/OUTPUT/Ia32/MultU64x32.obj
c:\steven\edk2\Build\Ovmf3264\DEBUG_CLANGPDB\IA32\MdeModulePkg\Core\Pei\PeiMain\lto.tmp
* Linker *

Peicore [1000000]: elines


OBJ:c:\steven\edk2\Build\Ovmf3264\DEBUG_CLANGPDB\IA32\MdeModulePkg\Core\Pei\PeiMain\lto.tmp
   c:\steven\edk2\OvmfPkg\Library\PlatformDebugLibIoPort\DebugLib.c
      54 57 58 60 204 211 217 218 227 229
   c:\steven\edk2\MdePkg\Library\BaseLib\CpuDeadLoop.c
      28 31 32
   c:\steven\edk2\OvmfPkg\Library\PlatformDebugLibIoPort\DebugLibDetect.c
      60 64 61 61 62 64
   c:\steven\edk2\MdeModulePkg\Core\Pei\PeiMain\PeiMain.c
      167 189 200 204 205 206 0 206 207 208 209 211 212 214 215 217
      218 220 221 223 0 224 225 226 228 229 224 232 235 236 237 239
      240 242 243 245 246 248 249 251 0 252 253 254 256 257 252 260
      0 267 277 0 278 0 283 284 285 286 291 296 302 307 321 322
      324 324 330 335 336 193 194 195 344 347 349 350 352 354 355 361
      366 376 398 399 400 189 405 409 417 418 424 430 434 441 446 454
      456 460 460 0 471 477 477 477 477 483 487 493 494 502 502 502
      502 503
   c:\steven\edk2\MdePkg\Library\PeiServicesTablePointerLibIdt\PeiServicesTablePointer.c
      36 40 41 42 42 42 43
   c:\steven\edk2\MdePkg\Library\BasePrintLib\PrintLib.c
      516 517 613 617 618 620
   c:\steven\edk2\MdePkg\Library\BaseMemoryLibRepStr\SetMemWrapper.c
      43 44 48 48 50 51
   c:\steven\edk2\MdeModulePkg\Core\Pei\Ppi\Ppi.c
      57 60 65 0 65 0 65 77 65 77 0 77 88 0 88 99
      100 104 0 104 0 104 99 112 130 137 142 147 148 164 170 171
      170 181 182 181 192 193 192 199 225 232 236 239 248 254 0 260
      264 267 267 267 268 273 274 277 278 280 0 280 282 287 0 248
      303 255 256 313 733 0 740 0 747 0 749 750 756 760 764 749
      740 772 336 337 364 369 373 377 0 383 384 383 395 396 401 411
      438 445 0 450 451 452 459 463 476 450 481 465 466 469 470 506
      516 520 522 523 527 526 540 547 568 584 548 564 572 575 575 575
      576 581 582 584 585 552 555 555 555 556 561 562 564 565 0 589
      0 591 596 536 612 541 542 543 622 644 645 659 671 0 671 692
      694 702 0 692 673 681 705 710 787 793 797 798 798 798 798 803
      804 804 804 804 0 804 0 804 807 814 807 823 824 825 826 827
      828 828 828 828 831
   c:\steven\edk2\MdeModulePkg\Core\Pei\BootMode\BootMode.c
      31 36 40 42 44 48 67 72 74 76 79
   c:\steven\edk2\MdeModulePkg\Core\Pei\Hob\Hob.c
      29 37 36 42 44 47 72 79 80 89 0 92 94 97 98 99
      100 104 105 106 107 109 110 115 112 114 116 119 136 145 146 147
      150 150 150 156 156 0 161 161 162 166 166 166 168 171 172 173
      174 178 179 181 182 187 184 186 188 191
   c:\steven\edk2\MdeModulePkg\Core\Pei\FwVol\FwVol.c
      550 564 566 570 576 577 581 592 592 598 604 608 609 0 0 617
      618 617 695 697 0 610 628 648 619 620 621 623 697 632 635 635
      635 636 641 642 648 649 650 651 652 653 661 666 668 674 675 681
      682 690 691 693 701 1381 1401 1402 0 1403 1410 1414 1413 0 1423 1424
      1426 1439 1442 1449 1453 1454 1463 1463 1467 1468 1472 1483 1484 1485 1486 1490
      1501 1490 1494 1495 0 1503 1507 1508 1508 1508 1508 1510 1511 1511 1511 1511
      1518 1527 1538 1547 1557 1566 1407 1569 0 1577 2318 2333 2337 2345 2346 0
      0 2355 0 2355 2356 0 2356 2357 2347 0 2347 2367 2387 2371 2374 2374
      2374 2375 2380 2381 2387 2388 2389 2390 2391 2392 2400 2405 2407 2413 2414 2420
      2421 2429 2430 2432 2337 2434 1611 1614 1614 1614 1616 1624 1636 1668 1669 272
      292 294 295 0 306 318 325 307 311 312 319 320 322 323 323 323
      331 330 314 316 0 334 0 0 337 341 342 346 360 360 366 377
      378 425 432 348 367 368 368 368 369 0 370 382 383 0 389 306
      395 396 0 0 400 0 414 0 426 427 429 430 430 430 434 371
      0 401 0 361 390 0 0 447 147 0 155 0 155 159 1705 1710
      1714 1715 1716 1717 1725 1726 1730 1731 1732 1726 1733 1741 1769 1776 1783 1784
      1788 1790 0 1799 1801 1810 1819 1811 1811 1812 1816 0 1822 1823 1824 1824
      1825 1826 1813 1829 210 216 217 0 224 225 226 228 0 228 231 224
      238 239 0 243 1902 1906 1915 1920 1924 1925 1926 1927 1928 1930 1931 1932
      1936 1965 1967 2003 2013 2017 2022 2023 0 2029 2039 2044 2030 2030 2031 2036
      2035 2036 2042 2043 2044 2054 2058 2032 2061 782 799 800 804 805 806 808
      809 809 0 810 825 829 0 830 0 841 842 850 851 851 851 856
      0 861 871 861 862 864 865 866 870 871 881 861 811 812 817 818
      818 818 892 974 975 983 984 984 984 0 818 0 818 0 894 895
      896 928 929 930 0 903 904 939 911 916 917 919 920 923 0 940
      944 945 947 948 949 950 951 955 954 955 965 0 990 0 834 0
      1857 1861 1868 1869 0 1873 1874 1875 1879 460 469 474 476 481 487 487
      487 487 492 498 498 498 498 500 501 501 507 502 507 508 509 510
      511 519 525 526 526 526 526 528 1014 1017 1018 1022 1023 1049 1052 1053
      1057 1059 1066 1092 1095 1097 1101 1102 2075 2079 0 2080 2081 2080 2082 0
      2087 1125 1129 1133 1135 1136 1141 1137 1144 1167 1170 1174 1175 1179 1180 1199
      1202 1209 1210 1214 1215 1234 1237 1244 1245 1249 1251 1258 1283 1286 1290 1292
      1296 1297 2125 2135 2141 2141 2141 2141 2147 2148 2148 2148 2148 2153 2154 2155
      2153 2163 2169 2169 2169 2169 2175 2176 2176 2176 2176 2181 2182 2183 2181 2186
   c:\steven\edk2\MdePkg\Library\BaseLib\Unaligned.c
      185 186 186 186 188
   c:\steven\edk2\MdePkg\Library\PeiMemoryAllocationLib\MemoryAllocationLib.c
      401 405 406 409 407 409 501 504 505 506 508 614 617 617 617 618
      618 620 621 622 624
   c:\steven\edk2\MdePkg\Library\PeiServicesLib\PeiServicesLib.c
      39 42 43 67 70 71 95 98 99 122 125 126 211 214 215 321
      324 325 374 377 378 399 402 403 499 500 632 642 646 647 647 653
      654 654 654 655 647 662 659 660 662 672 672 674 675 676 677 678
      678 678 679 681 682 683 683 683 684 687 688 688 688 690 691 692
      693 694 694 696 694 694 696
   c:\steven\edk2\MdePkg\Library\PeiHobLib\HobLib.c
      39 43 44 44 44 44 45 45 45 47 73 76 76 76 82 83
      86 89 109 112 113 233 237 238 244 239 244 245 485 497 501 505
      508 512 524
   c:\steven\edk2\MdePkg\Library\BaseLib\CheckSum.c
      36 40 40 40 41 41 43 44 43 47
   c:\steven\edk2\MdeModulePkg\Core\Pei\Memory\MemoryServices.c
      29 31 37 39 40 42 51 54 80 83 84 91 92 93 97 98
      99 101 104 119 123 125 131 133 133 134 135 136 136 140 0 140
      0 146 0 156 160 161 157 158 164 166 167 180 186 193 197 0
      198 200 0 203 0 210 211 213 231 239 0 240 241 246 247 268
      254 0 274 296 297 302 309 313 323 324 325 326 500 510 524 525
      527 546 554 555 546 551 552 0 562 563 564 0 568 584 589 593
      597 598 604 609 614 593 622 708 716 718 720 0 720 724 727 735
      0 736 737 746 747 751 752 757 782 795 799 804 804 809 804 804
      807 813
   c:\steven\edk2\MdePkg\Library\BaseMemoryLibRepStr\CopyMemWrapper.c
      46 47 50 50 51 51 53 56 57
   c:\steven\edk2\MdeModulePkg\Core\Pei\StatusCode\StatusCode.c
      37 44 51 52 65
   c:\steven\edk2\MdeModulePkg\Core\Pei\Reset\Reset.c
      28 36 41 47 48 54 63 86 93 99 100 107 111
   c:\steven\edk2\MdeModulePkg\Core\Pei\Dispatcher\Dispatcher.c
      643 660 688 0 697 0 697 700 703 704 705 706 709 712 715 717
      715 746 747 748 749 750 750 756 757 758 765 758 766 771 772 777
      778 779 780 781 782 787 793 847 852 859 860 0 860 864 866 870
      871 867 868 0 868 874 879 880 880 881 886 860 891 895 0 906
      907 908 910 911 912 914 915 916 922 923 924 925 926 928 929 931
      932 922 940 951 953 798 802 803 799 800 0 800 806 758 811 825
      837 842 969 986 988 989 991 997 996 998 0 1000 1001 1002 1003 1004
      1005 1006 1007 1014 1018 1022 1025 1033 1001 1000 1037 1038 1039 1054 1058 1055
      1061 1062 1063 1063 1063 1068 1072 1074 1080 1086 1089 1090 1092 1093 1093 1094
      1093 1096 1097 1097 1097 1097 1098 0 1098 1102 1103 1115 1121 1128 1135 1142
      1143 1160 1107 1108 1147 1151 1152 1153 1163 1174 1180 1187 1189 1211 1211 1211
      1212 1218 1224 1088 1086 1234 1235 1236 1061 1243 1253 1255 1276 1277 1278 1279
      1365 1367 1369 1376 1383 1386
   c:\steven\edk2\MdePkg\Library\BaseMemoryLibRepStr\MemLibGuid.c
      43 44 48 52 77 83 84 85 88
   c:\steven\edk2\MdeModulePkg\Core\Pei\Image\Image.c
      660 661 661 44 49 50 51 54 755 771 777 778 808 786 790 791
      792 801 801 802 0 812 792 830 842 835 836
   c:\steven\edk2\MdePkg\Library\BasePeCoffLib\BasePeCoff.c
      555 571 577 580 581 588 602 603 604 590 594 599 610 615 616 617
      588 630 632 0 739 0 744 0 744 748 750 756 764 766 775 776
      0 792 793 796 630 638 0 639 799 0 800 804 806 812 820 800
      756 757 758 653 655 664 0 669 673 675 681 689 696 669 681 821
      692 699 700 0 700 704 706 712 725 700 0 0 829 580 726 727
      728
   c:\steven\edk2\MdePkg\Library\BasePeCoffGetEntryPointLib\PeCoffGetEntryPoint.c
      97 101 101 101 104 108 0 116 117 118 119 123
   c:\steven\edk2\MdePkg\Library\BaseMemoryLibRepStr\ZeroMemWrapper.c
      42 43 47 47 47 48 48 49 50
   c:\steven\edk2\MdeModulePkg\Core\Pei\Security\Security.c
      57 63 68 69 71 32 35 33 93 98 111 119 103 102 123
   c:\steven\edk2\MdeModulePkg\Library\PeiReportStatusCodeLib\ReportStatusCodeLib.c
      54 58 61 62 297 386 387 387 387 389 387
   c:\steven\edk2\MdeModulePkg\Core\Pei\CpuIo\CpuIo.c
      77 197 266 288 110 142 174 220 243 309 330 351 374 398 422 446
      469 491 513 535
   c:\steven\edk2\MdeModulePkg\Core\Pei\PciCfg2\PciCfg2.c
      55 86 121
   c:\steven\edk2\MdePkg\Library\BasePrintLib\PrintLibInternal.c
      97 100 101 100 108 529 572 572 580 580 611 611 611 637 0 655
      656 662 675 668 672 680 681 682 683 732 735 733 735 732 0 683
      701 738 704 705 707 691 697 688 685 720 694 713 0 1075 1076 1053
      1054 0 0 1083 0 0 0 1090 0 1097 1104 1090 1104 0 1104 1110
      1111 1109 1104 1098 765 0 920 0 926 0 765 1047 0 0 1117 1124
      1125 656 1126 1127 1131 1147 1131 1132 1133 1134 1135 1143 1144 1145 1139 1138
      1139 1140 1159 1166 0 1166 0 1172 1169 1173 0 1175 1176 0 1177 1178
      1179 1181 1182 1183 0 1184 1185 1195 1196 1197 0 1197 1198 1205 0 1147
      1148 1144 1149 1150 897 0 902 907 914 0 0 930 935 938 939 940
      941 770 778 783 788 789 790 796 806 812 820 826 823 828 832 833
      835 796 850 0 836 0 865 865 866 871 872 875 0 875 0 875
      876 878 884 0 0 987 993 1003 0 963 968 971 1035 1036 1013 1014
      0 997 998 999 0 1007 1008 0 572 580 0 572 1224 1213 1217 1217
      1217 1221 1223 0 1255 1259 1260
   c:\steven\edk2\MdeModulePkg\Core\Pei\Dependency\Dependency.c
      35 43 52 57 0 44 69 96 0 0 105 107 195 0 202 206
      207 208 119 0 128 130 131 137 139 0 139 0 139 147 161 167
      0 0 167 0 168 169 170 213 215 0 215 0 215 221 230 0
      0 235 236 0 173 174 175 240 181 182 187 0 120 0 240 0
      240 247 191 192
   c:\steven\edk2\MdePkg\Library\BaseLib\LShiftU64.c
      34
   c:\steven\edk2\MdePkg\Library\BaseLib\SwitchStack.c
      49 53 53 53 58 58 60 62 69 70
   c:\steven\edk2\MdePkg\Library\BaseLib\DivU64x32Remainder.c
      40 41 41 41 42
   c:\steven\edk2\MdePkg\Library\BaseLib\SafeString.c
      1706 1712 1723 1724 1727 1730

Debug the CLANGPDB toolchain debug issue

Please note to keep the debug version lld same version (llvm 9.0) as the release one which use to build edk2. The different version LLVM tools (e.g. clang and lld) cannot be mixed to use and debug.

work notes: 20200109

There is many 0 lines in above llvm-pdbutil.exe, cvdump.exe and dbh.exe elines command output, which is not correct. These 0 lines might be caused by NASM assembly code inline optimization. If there is assembly function inline optimization, the PDB file will has lots of 0 in elines command output. If add the -O0 in CC flag to disable the compiler optimization, the PDB file content is fine.

work notes: 20200110

Asked LLVM developers about the PDB issue of Lines zero number in DEBUG_S_LINES subsection. I've gotten some advice from LLVM guys as below links but the suggested "-mllvm -use-unknown-locations=Disable" option only work for Dwarf debug info and not work for Codeview.

work notes: 20200112

I checked the LLVM code and confirm that the -use-unknown-locations option and its related logic is only supported in DwarfDebug.

C:\steven\LLVM\llvm-project2\llvm\lib\CodeGen\AsmPrinter\DwarfDebug.cpp
static cl::opt<DefaultOnOff> UnknownLocations(
    "use-unknown-locations", cl::Hidden,
    cl::desc("Make an absence of debug location information explicit."),
    cl::values(clEnumVal(Default, "At top of block or after label"),
               clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")),
    cl::init(Default));
...
    // If user said Don't Do That, don't do that.
    if (UnknownLocations == Disable)
      return;

Need to ask the LLVM developers help to port the -use-unknown-locations option and its related logic into C:\steven\LLVM\llvm-project2\llvm\lib\CodeGen\AsmPrinter\CodeViewDebug.cpp as well. Have submitted a BZ for this option support missing issue:

work notes: 20200115

The LLVM developers offer a fix for the zero line issue in the clang PDB file and now the master branch version LLVM/Clang can generate the correct PDB file without zero line.

Current problem is the only 32bits bios part (e.g. PEI) can fully be functional for the Simics debugger, but the 64bits part (e.g. DXE) still not correctly show the source location and call stack info. Will continue to debug the 64bits clang build options.

work notes: 20200116

There is only one frame source location info shown in current Call Stack windows which is just current execution address. There is no any other caller function' info. And this issue only happen in 64bits (x64) code. Have tried that the Frame Pointer Omission is not the reason and this issue still exist even with -fno-omit-frame-pointer and -mno-omit-leaf-frame-pointer build options. Suspect that there is still somthing wrong in PDB file or __chkstk implementation.

work notes: 20200126

After remove the -fno-asynchronous-unwind-tables option in CLANGPDB_ALL_CC_FLAGS, the .dll file can contain the stack unwind information, but the GenFW will deliberately discard them in the .efi file. So the unwind RuntimeFunction missing in .efi is not the root cause of the only-single-callstack-frame issue.

C:\steven\edk2\Build\EmulatorX64\DEBUG_CLANGPDB\X64\MdeModulePkg\Core\Pei\PeiMain\DEBUG>C:\steven\LLVM\llvm-project\_install_release\bin\llvm-readobj.exe --unwind PeiCore.dll

File: PeiCore.dll
Format: COFF-x86-64
Arch: x86_64
AddressSize: 64bit
UnwindInformation [
  RuntimeFunction {
    StartAddress: (0x11040)
    EndAddress: (0x1105E)
    UnwindInfoAddress: (0x24E78)
    UnwindInfo {
      Version: 1
      Flags [ (0x0)
      ]
      PrologSize: 4
      FrameRegister: -
      FrameOffset: -
      UnwindCodeCount: 1
      UnwindCodes [
        0x04: ALLOC_SMALL size=56
      ]
    }
  }
  RuntimeFunction {
    StartAddress: (0x11060)
    EndAddress: (0x11072)
    UnwindInfoAddress: (0x24E80)
    UnwindInfo {
      Version: 1
      Flags [ (0x0)
      ]
      PrologSize: 4
      FrameRegister: -
      FrameOffset: -
      UnwindCodeCount: 1
      UnwindCodes [
        0x04: ALLOC_SMALL size=16
      ]
    }
  }
... ...

C:\steven\edk2\Build\EmulatorX64\DEBUG_CLANGPDB\X64\MdeModulePkg\Core\Pei\PeiMain\DEBUG>C:\steven\LLVM\llvm-project\_install_release\bin\llvm-readobj.exe --unwind PeiCore.efi

File: PeiCore.efi
Format: COFF-x86-64
Arch: x86_64
AddressSize: 64bit
UnwindInformation [
]

work notes: 20200127

why the pdata and xdata sections are important to unwind the call stack: https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=vs-2019#unwindability

work notes: 20200128

Done. Figure out a patch as below to fix this issue.

From c22dc0008ba0758ef0c4b41b8e47547887cf7746 Mon Sep 17 00:00:00 2001
From: Steven <[email protected]>
Date: Tue, 28 Jan 2020 14:45:56 +0800
Subject: [PATCH] BaseTools: Enhance call stack unwindability for CLANGPDB x64
 binary

BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2487

The call stack unwindability of the COFF X64 binary requires
the binary to remain the pdata and xdata sections.
Details see the MSVC X64 calling convertion doc in below link:
https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention

Current build options discard or zero the data in pdata and xdata
sections which cause the debugger cannot correctly unwind the
X64 binary call stack in the runtime.
Enhance the build options to force emit the unwind tables and
keep the data of pdata and xdata sections correct in the binary.

Signed-off-by: Steven Shi <[email protected]>
Cc: Liming Gao <[email protected]>
Cc: Bob Feng <[email protected]>
---
 BaseTools/Conf/build_rule.template |  2 +-
 BaseTools/Conf/tools_def.template  | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/BaseTools/Conf/build_rule.template b/BaseTools/Conf/build_rule.template
index 51748bc065..5d08217a0c 100755
--- a/BaseTools/Conf/build_rule.template
+++ b/BaseTools/Conf/build_rule.template
@@ -367,7 +367,7 @@
         $(OUTPUT_DIR)(+)$(MODULE_NAME).efi
 
     <Command.MSFT, Command.INTEL, Command.RVCT, Command.CLANGPDB>
-        "$(GENFW)" -e $(MODULE_TYPE) -o ${dst} ${src} $(GENFW_FLAGS)
+        "$(GENFW)" -e $(MODULE_TYPE) -o ${dst} ${src} $(GENFW_FLAGS) --keepexceptiontable
         $(CP) ${dst} $(DEBUG_DIR)
         $(CP) ${dst} $(BIN_DIR)(+)$(MODULE_NAME_GUID).efi
         -$(CP) $(DEBUG_DIR)(+)*.map $(OUTPUT_DIR)
diff --git a/BaseTools/Conf/tools_def.template b/BaseTools/Conf/tools_def.template
index feee2bbf16..55988a2da1 100755
--- a/BaseTools/Conf/tools_def.template
+++ b/BaseTools/Conf/tools_def.template
@@ -2759,7 +2759,7 @@ DEFINE CLANGPDB_IA32_TARGET          = -target i686-unknown-windows
 DEFINE CLANGPDB_X64_TARGET           = -target x86_64-unknown-windows
 
 DEFINE CLANGPDB_WARNING_OVERRIDES    = -Wno-parentheses-equality -Wno-tautological-compare -Wno-tautological-constant-out-of-range-compare -Wno-empty-body -Wno-unused-const-variable -Wno-varargs -Wno-unknown-warning-option -Wno-microsoft-enum-forward-reference
-DEFINE CLANGPDB_ALL_CC_FLAGS         = DEF(GCC48_ALL_CC_FLAGS) DEF(CLANGPDB_WARNING_OVERRIDES) -fno-stack-protector -mms-bitfields -Wno-address -Wno-shift-negative-value -Wno-unknown-pragmas -Wno-incompatible-library-redeclaration -fno-asynchronous-unwind-tables -mno-implicit-float  -ftrap-function=undefined_behavior_has_been_optimized_away_by_clang -funsigned-char -fno-ms-extensions -Wno-null-dereference -fms-compatibility -mno-stack-arg-probe
+DEFINE CLANGPDB_ALL_CC_FLAGS         = DEF(GCC48_ALL_CC_FLAGS) DEF(CLANGPDB_WARNING_OVERRIDES) -fno-stack-protector -mms-bitfields -Wno-address -Wno-shift-negative-value -Wno-unknown-pragmas -Wno-incompatible-library-redeclaration -mno-implicit-float  -ftrap-function=undefined_behavior_has_been_optimized_away_by_clang -funsigned-char -fno-ms-extensions -Wno-null-dereference -fms-compatibility -mno-stack-arg-probe
 
 ###########################
 # CLANGPDB IA32 definitions
@@ -2817,15 +2817,15 @@ NOOPT_CLANGPDB_IA32_DLINK2_FLAGS     =
 *_CLANGPDB_X64_ASLPP_FLAGS          = DEF(GCC_ASLPP_FLAGS) DEF(CLANGPDB_X64_TARGET)
 *_CLANGPDB_X64_VFRPP_FLAGS          = DEF(GCC_VFRPP_FLAGS) DEF(CLANGPDB_X64_TARGET)
 
-DEBUG_CLANGPDB_X64_CC_FLAGS         = DEF(CLANGPDB_ALL_CC_FLAGS) -m64 "-DEFIAPI=__attribute__((ms_abi))" -mno-red-zone -mcmodel=small -Oz -flto DEF(CLANGPDB_X64_TARGET) -gcodeview
-DEBUG_CLANGPDB_X64_DLINK_FLAGS      = /NOLOGO /NODEFAULTLIB /IGNORE:4001 /OPT:REF /OPT:ICF=10 /ALIGN:32 /FILEALIGN:32 /SECTION:.xdata,D /SECTION:.pdata,D /Machine:X64 /DLL /ENTRY:$(IMAGE_ENTRY_POINT) /SUBSYSTEM:EFI_BOOT_SERVICE_DRIVER /SAFESEH:NO /BASE:0 /DEBUG:GHASH  /lldmap
+DEBUG_CLANGPDB_X64_CC_FLAGS         = DEF(CLANGPDB_ALL_CC_FLAGS) -m64 "-DEFIAPI=__attribute__((ms_abi))" -mno-red-zone -mcmodel=small -Oz -flto DEF(CLANGPDB_X64_TARGET) -gcodeview -funwind-tables
+DEBUG_CLANGPDB_X64_DLINK_FLAGS      = /NOLOGO /NODEFAULTLIB /IGNORE:4001 /OPT:REF /OPT:ICF=10 /ALIGN:32 /FILEALIGN:32 /Machine:X64 /DLL /ENTRY:$(IMAGE_ENTRY_POINT) /SUBSYSTEM:EFI_BOOT_SERVICE_DRIVER /SAFESEH:NO /BASE:0 /DEBUG:GHASH  /lldmap
 DEBUG_CLANGPDB_X64_DLINK2_FLAGS     =
 
-RELEASE_CLANGPDB_X64_CC_FLAGS       = DEF(CLANGPDB_ALL_CC_FLAGS) -m64 "-DEFIAPI=__attribute__((ms_abi))" -mno-red-zone -mcmodel=small -Oz -flto DEF(CLANGPDB_X64_TARGET)
+RELEASE_CLANGPDB_X64_CC_FLAGS       = DEF(CLANGPDB_ALL_CC_FLAGS) -m64 "-DEFIAPI=__attribute__((ms_abi))" -mno-red-zone -mcmodel=small -Oz -flto DEF(CLANGPDB_X64_TARGET) -fno-unwind-tables
 RELEASE_CLANGPDB_X64_DLINK_FLAGS    = /NOLOGO /NODEFAULTLIB /IGNORE:4001 /IGNORE:4254 /OPT:REF /OPT:ICF=10 /ALIGN:32 /FILEALIGN:32 /SECTION:.xdata,D /SECTION:.pdata,D /Machine:X64 /DLL /ENTRY:$(IMAGE_ENTRY_POINT) /SUBSYSTEM:EFI_BOOT_SERVICE_DRIVER /SAFESEH:NO /BASE:0 /MERGE:.rdata=.data  /lldmap
 RELEASE_CLANGPDB_X64_DLINK2_FLAGS   =
 
-NOOPT_CLANGPDB_X64_CC_FLAGS         = DEF(CLANGPDB_ALL_CC_FLAGS) -m64 "-DEFIAPI=__attribute__((ms_abi))" -mno-red-zone -mcmodel=small -O0 DEF(CLANGPDB_X64_TARGET) -gcodeview
+NOOPT_CLANGPDB_X64_CC_FLAGS         = DEF(CLANGPDB_ALL_CC_FLAGS) -m64 "-DEFIAPI=__attribute__((ms_abi))" -mno-red-zone -mcmodel=small -O0 DEF(CLANGPDB_X64_TARGET) -gcodeview -funwind-tables
 NOOPT_CLANGPDB_X64_DLINK_FLAGS      = /NOLOGO /NODEFAULTLIB /IGNORE:4001 /OPT:REF /OPT:ICF=10 /ALIGN:32 /FILEALIGN:32 /SECTION:.xdata,D /SECTION:.pdata,D /Machine:X64 /DLL /ENTRY:$(IMAGE_ENTRY_POINT) /SUBSYSTEM:EFI_BOOT_SERVICE_DRIVER /SAFESEH:NO /BASE:0 /DEBUG:GHASH  /lldmap
 NOOPT_CLANGPDB_X64_DLINK2_FLAGS     =
 
-- 
2.16.1.windows.4
  • Before apply above patch, only 32bits bios part (e.g. PEI) can fully be functional for the Simics debugger, but the 64bits part (e.g. DXE) still not correctly unwind and show the call stack frames. There is only one frame source location info shown in the Call Stack windows which is just current IP execution address:
  • After apply above patch: The break point at the GetNextGuidHob() in the DxeMain() function body of C:\steven\edk2\MdeModulePkg\Core\Dxe\DxeMain\DxeMain.c. It is in 64bit DXE code and the debugger can show all stack frames source location in the call stack view. If I step into the above GetNextGuidHob() in the DxeMain() function body, the debugger can correctly show all callers IP address source location in the call stack view.