From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=17.151.62.25; helo=mail-in2.apple.com; envelope-from=afish@apple.com; receiver=edk2-devel@lists.01.org Received: from mail-in2.apple.com (mail-out2.apple.com [17.151.62.25]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 53F95220D4C17 for ; Tue, 14 Nov 2017 06:33:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; d=apple.com; s=mailout2048s; c=relaxed/simple; q=dns/txt; i=@apple.com; t=1510670282; h=From:Sender:Reply-To:Subject:Date:Message-id:To:Cc:MIME-version:Content-type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-reply-to:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=PbxbUo+gqMjCJiTh4wShOkN+eGSHpT1g7buirJlOYRg=; b=Ra/KuvMkiUEsmyAgJdioI81gyDjQuXvybzUqG3yqrMd9HcnizODT7b9Ij8fs0Ody vr/NUPL6b9vWqWDk0MtqRGVUb7vqMvVaG0wRgIbq6s/Td40L/fIPUpJ0u1OGXNJ5 X4pbNMlSRo/V9/VU10PYWEP9NqRvBqfKJdjjB+oR4A5XBbG2PO8eVYfd8hd0ED1O TekAbFPdGKmbnj/Fln3JBOP7gqZo63Q+wIBlLq6bUjbGfc6XewvLqN1sEOGwhPev /c1CoB8TZu04V5LeKABMDnd5vTSXWJBMUHNeSgJui0geEcf/x+EX5hjXIMRsJbPq sP7viUxEbCa6XVhfW5rGXg==; Received: from relay4.apple.com (relay4.apple.com [17.128.113.87]) (using TLS with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mail-in2.apple.com (Apple Secure Mail Relay) with SMTP id 6E.39.22347.ACFFA0A5; Tue, 14 Nov 2017 06:38:02 -0800 (PST) X-AuditID: 11973e11-ccfff7000000574b-16-5a0affcaefbd Received: from nwk-mmpp-sz12.apple.com (nwk-mmpp-sz12.apple.com [17.128.115.204]) by relay4.apple.com (Apple SCV relay) with SMTP id 9B.BD.21277.ACFFA0A5; Tue, 14 Nov 2017 06:38:02 -0800 (PST) MIME-version: 1.0 Received: from [17.234.126.199] by nwk-mmpp-sz12.apple.com (Oracle Communications Messaging Server 8.0.1.3.20170825 64bit (built Aug 25 2017)) with ESMTPSA id <0OZE004GWWND2990@nwk-mmpp-sz12.apple.com>; Tue, 14 Nov 2017 06:38:02 -0800 (PST) Sender: afish@apple.com From: Andrew Fish Message-id: <1AA80444-D1BB-405F-8C2D-E9EF5FE62285@apple.com> Date: Tue, 14 Nov 2017 06:38:02 -0800 In-reply-to: Cc: Paulo Alcantara , "edk2-devel@lists.01.org" , Laszlo Ersek , Eric Dong To: Fan Jeff References: <00e14f85d93a2e81ab008f32020f3048fe4857fb.1510662518.git.pcacjr@zytor.com> X-Mailer: Apple Mail (2.3273) X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrMLMWRmVeSWpSXmKPExsUi2FAYrnvqP1eUwYPz8hZ7Dh1lttj8Ithi 2bEdLBb7Xn9ktDjxeR6bA6vH454zbB6L97xk8uie/Y/F4/2+q2weJ1q+sAawRnHZpKTmZJal FunbJXBl3O++y1rw9gZTxaJnLewNjNMWMXUxcnJICJhI/H0zhRHEFhJYzSTR8z8MJj7z10TW LkYuoPghRompr76ANfAKCEr8mHyPBcRmFgiTeDv7EgtE0TdGiQmtk9hBEsIC4hLvzmxiBrHZ BJQlVsz/wA7RbCOx/+UDFoiaOIlX/48B2RwcLAKqEr1PZEHCnALxEne+bmcDmckssAho8Yqt YNeJCKhJXJrzkA1i2QYmiX0rOxkhTpWVuDX7EjNIQkLgBJvEirs3GCcwCs1Ccu0sJNfOAlrI LKAuMWVKLkRYW+LJuwusELaaxMLfi5iQxRcwsq1iFMpN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U3Lns1X29s3uZ3vM03c3nvL5EiaU4I9FQi7moOBEAqdbj2HoCAAA= X-Content-Filtered-By: Mailman/MimeDel 2.1.22 Subject: Re: [RFC 1/1] UefiCpuPkg/CpuExceptionHandlerLib/X64: Add stack trace support X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Nov 2017 14:33:55 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable > On Nov 14, 2017, at 6:26 AM, Fan Jeff wrote: >=20 > Andrew, > =20 > We could use he EIP offset in Paul=E2=80=99s trace message and work = with the generated map file under debug directory for debug trace. It would also be possible to use gdb.=20 Given 0 0x000000007E510F7F @ 0x000000007E509000+0x7F7E (0x000000007F762CB0) in = PartitionDxe.dll If you load PartitionDxe.dll into gdb you can then do "l *0x7F7E" to = dump the source.=20 I'm mapping lldb behavior to gdb, but it should be close.=20 Thanks, Andrew Fish > =20 > Jeff > =20 > =E5=8F=91=E4=BB=B6=E4=BA=BA: Andrew Fish > =E5=8F=91=E9=80=81=E6=97=B6=E9=97=B4: 2017=E5=B9=B411=E6=9C=8814=E6=97=A5= 22:01 > =E6=94=B6=E4=BB=B6=E4=BA=BA: Paulo Alcantara > =E6=8A=84=E9=80=81: edk2-devel@lists.01.org = ; Laszlo Ersek = ; Eric Dong > =E4=B8=BB=E9=A2=98: Re: [edk2] [RFC 1/1] = UefiCpuPkg/CpuExceptionHandlerLib/X64: Add stack trace support > =20 > Paulo, >=20 > Cool feature. How does this code deal with VC++ that code does not = store the frame pointer and requires symbols to unwind.=20 >=20 > Also on the page fault you can print the fault address since it is in = CR2.=20 >=20 > It should be possible to post process the text file and make a = symbolicated backtrace.=20 >=20 > Thanks, >=20 > Andrew Fish >=20 > > On Nov 14, 2017, at 4:47 AM, Paulo Alcantara = wrote: > >=20 > > This patch adds stack trace support during a X64 CPU exception. > >=20 > > It will dump out back trace, stack contents as well as image module > > names that were part of the call stack. > >=20 > > Contributed-under: TianoCore Contribution Agreement 1.1 > > Cc: Eric Dong > > Cc: Laszlo Ersek > > Signed-off-by: Paulo Alcantara > > --- > > UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c = | 344 +++++++++++++++++++- > > 1 file changed, 342 insertions(+), 2 deletions(-) > >=20 > > diff --git = a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c = b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c > > index 65f0cff680..7048247be3 100644 > > --- = a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c > > +++ = b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c > > @@ -14,6 +14,11 @@ > >=20 > > #include "CpuExceptionCommon.h" > >=20 > > +// > > +// Unknown PDB file name > > +// > > +GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mUnknownPdbFileName =3D = "????"; > > + > > /** > > Return address map of exception handler template so that C code = can generate > > exception tables. > > @@ -243,6 +248,325 @@ DumpCpuContext ( > > } > >=20 > > /** > > + Dump stack contents. > > + > > + @param[in] ImageBase Base address of PE/COFF image. > > + @param[out] PdbAbsoluteFilePath Absolute path of PDB file. > > + @param[out] PdbFileName File name of PDB file. > > +**/ > > +STATIC > > +VOID > > +GetPdbFileName ( > > + IN UINTN ImageBase, > > + OUT CHAR8 **PdbAbsoluteFilePath, > > + OUT CHAR8 **PdbFileName > > + ) > > +{ > > + VOID *PdbPointer; > > + CHAR8 *Str; > > + > > + // > > + // Get PDB file name from PE/COFF image > > + // > > + PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)ImageBase); > > + if (PdbPointer =3D=3D NULL) { > > + // > > + // No PDB file name found. Set it to an unknown file name. > > + // > > + *PdbFileName =3D (CHAR8 *)mUnknownPdbFileName; > > + if (PdbAbsoluteFilePath !=3D NULL) { > > + *PdbAbsoluteFilePath =3D NULL; > > + } > > + } else { > > + // > > + // Get file name portion out of PDB file in PE/COFF image > > + // > > + Str =3D (CHAR8 *)((UINTN)PdbPointer + > > + AsciiStrLen ((CHAR8 *)PdbPointer) - sizeof = *Str); > > + for (; *Str !=3D '/' && *Str !=3D '\\'; Str--) { > > + ; > > + } > > + > > + // > > + // Set PDB file name (also skip trailing path separator: '/' or = '\\') > > + // > > + *PdbFileName =3D Str + 1; > > + > > + if (PdbAbsoluteFilePath !=3D NULL) { > > + // > > + // Set absolute file path of PDB file > > + // > > + *PdbAbsoluteFilePath =3D PdbPointer; > > + } > > + } > > +} > > + > > +/** > > + Dump stack contents. > > + > > + @param[in] CurrentRsp Current stack pointer address. > > + @param[in] UnwondStacksCount Count of unwond stack frames. > > +**/ > > +STATIC > > +VOID > > +DumpStackContents ( > > + IN UINT64 CurrentRsp, > > + IN INTN UnwondStacksCount > > + ) > > +{ > > + if (UnwondStacksCount =3D=3D 0) { > > + return; > > + } > > + > > + // > > + // Dump out stack contents > > + // > > + InternalPrintMessage ("\nStack dump:\n"); > > + while (UnwondStacksCount-- > 0) { > > + InternalPrintMessage ( > > + "0x%016lx: %016lx %016lx\n", > > + CurrentRsp, > > + *(UINT64 *)CurrentRsp, > > + *(UINT64 *)((UINTN)CurrentRsp + 8) > > + ); > > + > > + // > > + // As per Microsoft x64 ABI, the stack pointer must be aligned = on a 16 byte > > + // boundary. > > + // > > + CurrentRsp =3D CurrentRsp + 16; > > + } > > +} > > + > > +/** > > + Dump all image module names from call stack. > > + > > + @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT. > > +**/ > > +STATIC > > +VOID > > +DumpImageModuleNames ( > > + IN EFI_SYSTEM_CONTEXT SystemContext > > + ) > > +{ > > + EFI_STATUS Status; > > + UINT64 Rip; > > + UINTN ImageBase; > > + VOID *EntryPoint; > > + CHAR8 *PdbAbsoluteFilePath; > > + CHAR8 *PdbFileName; > > + UINT64 Rbp; > > + > > + // > > + // Set current RIP address > > + // > > + Rip =3D SystemContext.SystemContextX64->Rip; > > + > > + // > > + // Set current frame pointer address > > + // > > + Rbp =3D SystemContext.SystemContextX64->Rbp; > > + > > + // > > + // Get initial PE/COFF image base address from current RIP > > + // > > + ImageBase =3D PeCoffSearchImageBase (Rip); > > + if (ImageBase =3D=3D 0) { > > + InternalPrintMessage ("!!!! Could not find image module names. = !!!!"); > > + return; > > + } > > + > > + // > > + // Get initial PE/COFF image's entry point > > + // > > + Status =3D PeCoffLoaderGetEntryPoint ((VOID *)ImageBase, = &EntryPoint); > > + if (EFI_ERROR (Status)) { > > + EntryPoint =3D NULL; > > + } > > + > > + // > > + // Get file name and absolute path of initial PDB file > > + // > > + GetPdbFileName (ImageBase, &PdbAbsoluteFilePath, &PdbFileName); > > + > > + // > > + // Print out initial image module name (if any) > > + // > > + if (PdbAbsoluteFilePath !=3D NULL) { > > + InternalPrintMessage ( > > + "\n%a (ImageBase=3D0x%016lx, EntryPoint=3D0x%016lx):\n", > > + PdbFileName, > > + ImageBase, > > + (UINTN)EntryPoint > > + ); > > + InternalPrintMessage ("%a\n", PdbAbsoluteFilePath); > > + } > > + > > + // > > + // Walk through call stack and find next module names > > + // > > + for (;;) { > > + // > > + // Set RIP with return address from current stack frame > > + // > > + Rip =3D *(UINT64 *)((UINTN)Rbp + 8); > > + > > + // > > + // Check if RIP is within another PE/COFF image base address > > + // > > + if (Rip < ImageBase) { > > + // > > + // Search for the respective PE/COFF image based on RIP > > + // > > + ImageBase =3D PeCoffSearchImageBase (Rip); > > + if (ImageBase =3D=3D 0) { > > + // > > + // Stop stack trace > > + // > > + break; > > + } > > + > > + // > > + // Get PE/COFF image's entry point > > + // > > + Status =3D PeCoffLoaderGetEntryPoint ((VOID *)ImageBase, = &EntryPoint); > > + if (EFI_ERROR (Status)) { > > + EntryPoint =3D NULL; > > + } > > + > > + // > > + // Get file name and absolute path of PDB file > > + // > > + GetPdbFileName (ImageBase, &PdbAbsoluteFilePath, = &PdbFileName); > > + > > + // > > + // Print out image module name (if any) > > + // > > + if (PdbAbsoluteFilePath !=3D NULL) { > > + InternalPrintMessage ( > > + "%a (ImageBase=3D0x%016lx, EntryPoint=3D0x%016lx):\n", > > + PdbFileName, > > + ImageBase, > > + (UINTN)EntryPoint > > + ); > > + InternalPrintMessage ("%a\n", PdbAbsoluteFilePath); > > + } > > + } > > + > > + // > > + // Unwind the stack > > + // > > + Rbp =3D *(UINT64 *)(UINTN)Rbp; > > + } > > +} > > + > > +/** > > + Dump stack trace. > > + > > + @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT. > > + @param[out] UnwondStacksCount Count of unwond stack frames. > > +**/ > > +STATIC > > +VOID > > +DumpStackTrace ( > > + IN EFI_SYSTEM_CONTEXT SystemContext, > > + OUT INTN *UnwondStacksCount > > + ) > > +{ > > + UINT64 Rip; > > + UINT64 Rbp; > > + UINTN ImageBase; > > + CHAR8 *PdbFileName; > > + > > + // > > + // Initialize count of unwond stacks > > + // > > + *UnwondStacksCount =3D 0; > > + > > + // > > + // Set current RIP address > > + // > > + Rip =3D SystemContext.SystemContextX64->Rip; > > + > > + // > > + // Set current frame pointer address > > + // > > + Rbp =3D SystemContext.SystemContextX64->Rbp; > > + > > + // > > + // Get initial PE/COFF image base address from current RIP > > + // > > + ImageBase =3D PeCoffSearchImageBase (Rip); > > + if (ImageBase =3D=3D 0) { > > + InternalPrintMessage ("!!!! Could not find backtrace = information. !!!!"); > > + return; > > + } > > + > > + // > > + // Get PDB file name from initial PE/COFF image > > + // > > + GetPdbFileName (ImageBase, NULL, &PdbFileName); > > + > > + // > > + // Print out back trace > > + // > > + InternalPrintMessage ("\nBack trace:\n"); > > + > > + for (;;) { > > + // > > + // Print stack frame in the following format: > > + // > > + // # @ + (RBP) in [ | = ????] > > + // > > + InternalPrintMessage ( > > + "%d 0x%016lx @ 0x%016lx+0x%x (0x%016lx) in %a\n", > > + *UnwondStacksCount, > > + Rip, > > + ImageBase, > > + Rip - ImageBase - 1, > > + Rbp, > > + PdbFileName > > + ); > > + > > + // > > + // Set RIP with return address from current stack frame > > + // > > + Rip =3D *(UINT64 *)((UINTN)Rbp + 8); > > + > > + // > > + // Check if RIP is within another PE/COFF image base address > > + // > > + if (Rip < ImageBase) { > > + // > > + // Search for the respective PE/COFF image based on RIP > > + // > > + ImageBase =3D PeCoffSearchImageBase (Rip); > > + if (ImageBase =3D=3D 0) { > > + // > > + // Stop stack trace > > + // > > + break; > > + } > > + > > + // > > + // Get PDB file name > > + // > > + GetPdbFileName (ImageBase, NULL, &PdbFileName); > > + } > > + > > + // > > + // Unwind the stack > > + // > > + Rbp =3D *(UINT64 *)(UINTN)Rbp; > > + > > + // > > + // Increment count of unwond stacks > > + // > > + (*UnwondStacksCount)++; > > + } > > +} > > + > > +/** > > Display CPU information. > >=20 > > @param ExceptionType Exception type. > > @@ -254,9 +578,25 @@ DumpImageAndCpuContent ( > > IN EFI_SYSTEM_CONTEXT SystemContext > > ) > > { > > + INTN UnwondStacksCount; > > + > > + // > > + // Dump CPU context > > + // > > DumpCpuContext (ExceptionType, SystemContext); > > + > > + // > > + // Dump stack trace > > + // > > + DumpStackTrace (SystemContext, &UnwondStacksCount); > > + > > + // > > + // Dump image module names > > + // > > + DumpImageModuleNames (SystemContext); > > + > > // > > - // Dump module image base and module entry point by RIP > > + // Dump stack contents > > // > > - DumpModuleImageInfo (SystemContext.SystemContextX64->Rip); > > + DumpStackContents (SystemContext.SystemContextX64->Rsp, = UnwondStacksCount); > > } > > --=20 > > 2.11.0 > >=20 > > _______________________________________________ > > edk2-devel mailing list > > edk2-devel@lists.01.org > > https://lists.01.org/mailman/listinfo/edk2-devel = >=20 > _______________________________________________ > edk2-devel mailing list > edk2-devel@lists.01.org > https://lists.01.org/mailman/listinfo/edk2-devel =