Skip to content

Commit

Permalink
Debug: Merge all runners into efidebug.tool and fix XCODE5 with LLDB
Browse files Browse the repository at this point in the history
  • Loading branch information
vit9696 committed Mar 23, 2020
1 parent d968201 commit a8ebda7
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 76 deletions.
14 changes: 10 additions & 4 deletions Debug/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ when no macOS guest booting is required.
You may additionally pass `-S` flag to QEMU to stop at first instruction
and wait for GDB connection.
#### Debugger Configuration
For simplicitly `efidebug.tool` performs all the necessary GDB or LLDB scripting.
Note, that you need to run `reload-uefi` after any new binary loads.
Check `efidebug.tool` header for environment variables to configure your setup.
For example, you can use `EFI_DEBUGGER` variable to force LLDB (`LLDB`) or GDB (`GDB`).
#### GDB Configuration
It is a good idea to use GDB Multiarch in case different debugging architectures are planned to be
Expand All @@ -112,7 +120,8 @@ used. This can be done in several ways:
- https://macports.org/ — via MacPorts (`sudo port install gdb +multiarch`)
- Your preferred method here
Once GDB is installed the process is as simple as running the following set of commands:
Once GDB is installed you can use `efidebug.tool` for debugging. In case you do not
want to use `efidebug.tool`, the following set of commands can be used as a reference:
```
$ ggdb /opt/UDK/Build/OpenCorePkg/NOOPT_XCODE5/X64/OpenCorePkg/Debug/GdbSyms/GdbSyms/DEBUG/GdbSyms.dll.dSYM/Contents/Resources/DWARF/GdbSyms.dll
Expand All @@ -124,9 +133,6 @@ reload-uefi
b DebugBreak
```
For simplicitly `rungdb.tool` performs them all. Note, that you need to run `reload-uefi`
after any new binary loads.
#### CLANGDWARF
CLANGDWARF toolchain is an LLVM-based toolchain that directly generates
Expand Down
2 changes: 1 addition & 1 deletion Debug/Scripts/lldb_uefi.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def parse_image (self, image, syms):
if sym_name != self.EINVAL:
macho = os.path.isdir(sym_name + '.dSYM')
if macho:
real_sym = sym_name + '.dSYM/Contents/Resources/DWARF/' + os.path.basename(sym_name)
real_sym = sym_name
else:
sym_name_dbg = re.sub(r"\.dll$", ".debug", sym_name)
if sym_name_dbg != sym_name and os.path.exists(sym_name_dbg):
Expand Down
9 changes: 7 additions & 2 deletions Debug/Scripts/x86_64_target_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
# GDB server.
#----------------------------------------------------------------------
from lldb import *
import os
import sys

# Compiler and DWARF register numbers
name_to_gcc_dwarf_regnum = {
Expand Down Expand Up @@ -774,5 +776,8 @@ def get_target_definition(triple):

def get_dynamic_setting(target, setting_name):
if setting_name == 'gdb-server-target-definition':
# FIXME: Make this configurable for e.g. 'x86_64-apple-macosx'
return get_target_definition('x86_64-pc-windows-msvc')
triple = os.getenv('EFI_TRIPLE')
if not triple:
print('Cannot find requested triple!\nHint: set EFI_TRIPLE variable.')
sys.exit(1)
return get_target_definition(triple)
156 changes: 156 additions & 0 deletions Debug/efidebug.tool
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#!/bin/bash

#
# Configuration variables:
# LLDB - path to LLDB debugger
# defaults to finding in PATH
# GDB - path to GDB debugger
# defaults to finding in PATH
# EFI_ARCH - architecture to debug
# defaults to X64
# EFI_PORT - debugger TCP connection port
# defaults to 8864 for X64 and 8832 for IA32
# EFI_HOST - debugger TCP connection host
# defaults to localhost
# EFI_DEBUGGER - debugger to use
# defaults to LLDB on macOS and GDB otherwise, fallbacks to other when missing
# EFI_TOOLCHAIN - toolchain to use
# defaults to XCODE5 on macOS and GCC5 otherwise
# EFI_SYMS - symbols to use
# defaults to files in GdbSyms/Bin
# EFI_SYMS_PDB - optional PDB symbols for LLDB
# defaults to "-s GdbSyms/Bin/${EFI_ARCH}_CLANGPDB/GdbSyms.pdb" for CLANGPDB
# EFI_TRIPLE - optional target triple for LLDB
# defaults to x86_64-apple-macosx for XCODE5, x86_64-pc-windows-msvc for CLANGDWARF/CLANGPDB,
# x86_64-linux-gnu otherwise.
#

RUNDIR=$(dirname "$0")
pushd "${RUNDIR}" >/dev/null
RUNDIR=$(pwd)
popd >/dev/null

cd "$RUNDIR"

find_gdb() {
if [ "${GDB}" = "" ]; then
GDB=$(which ggdb)
fi

if [ "${GDB}" = "" ]; then
GDB=$(which gdb-multiarch)
fi

if [ "${GDB}" = "" ]; then
GDB=$(which gdb)
fi
}

find_lldb() {
if [ "${LLDB}" = "" ]; then
LLDB=$(ls /opt/local/bin/lldb-mp* 2>/dev/null)
fi

if [ "${LLDB}" = "" ]; then
LLDB=$(which lldb)
fi
}

choose_debugger() {
find_gdb
find_lldb

if [ "${EFI_ARCH}" = "" ]; then
EFI_ARCH="X64"
fi

if [ "${EFI_HOST}" = "" ]; then
EFI_HOST="localhost"
fi

if [ "${EFI_PORT}" = "" ]; then
if [ "${EFI_ARCH}" = "IA32" ]; then
EFI_PORT=8832
else
EFI_PORT=8864
fi
fi

if [ "${EFI_DEBUGGER}" = "" ]; then
if [ "${LLDB}" != "" ] && [ "$(uname)" = "Darwin" ]; then
EFI_DEBUGGER="LLDB"
elif [ "${GDB}" != "" ]; then
EFI_DEBUGGER="GDB"
elif [ "${LLDB}" != "" ]; then
EFI_DEBUGGER="LLDB"
else
echo "Cannot find installed GDB or LLDB debugger!"
echo "Hint: set GDB or LLDB variables to debugger path."
exit 1
fi
fi

if [ "${EFI_TOOLCHAIN}" = "" ]; then
if [ "$(uname)" = "Darwin" ]; then
EFI_TOOLCHAIN="XCODE5"
else
EFI_TOOLCHAIN="GCC5"
fi
fi

if [ "${EFI_SYMS}" = "" ]; then
if [ "${EFI_TOOLCHAIN}" = "XCODE5" ]; then
EFI_SYMS="GdbSyms/Bin/${EFI_ARCH}_XCODE5/GdbSyms.dll"
elif [ "${EFI_TOOLCHAIN}" = "CLANGPDB" ]; then
EFI_SYMS_PDB="-s GdbSyms/Bin/${EFI_ARCH}_CLANGPDB/GdbSyms.pdb"
EFI_SYMS="GdbSyms/Bin/${EFI_ARCH}_CLANGPDB/GdbSyms.dll"
else
EFI_SYMS="GdbSyms/Bin/${EFI_ARCH}_${EFI_TOOLCHAIN}/GdbSyms.debug"
fi
fi

if [ ! -f "${EFI_SYMS}" ]; then
echo "Cannot find symbols: ${EFI_SYMS}!"
echo "Hint: compile this file or set EFI_SYMS variable."
exit 1
fi

if [ "${EFI_TRIPLE}" = "" ]; then
if [ "${EFI_ARCH}" = "IA32" ]; then
triple_arch=i386
else
triple_arch=x86_64
fi

if [ "${EFI_TOOLCHAIN}" = "XCODE5" ]; then
export EFI_TRIPLE="${triple_arch}-apple-macosx"
elif [ "${EFI_TOOLCHAIN}" = "CLANGPDB" ] || [ "${EFI_TOOLCHAIN}" = "CLANGDWARF" ]; then
export EFI_TRIPLE="${triple_arch}-pc-windows-msvc"
else
export EFI_TRIPLE="${triple_arch}-linux-gnu"
fi
fi
}

choose_debugger

if [ "${EFI_DEBUGGER}" = "GDB" ]; then
"${GDB}" -ex "target remote ${EFI_HOST}:${EFI_PORT}" \
-ex "source Scripts/gdb_uefi.py" \
-ex "set pagination off" \
-ex "reload-uefi" \
-ex "b DebugBreak" \
"${EFI_SYMS}"
elif [ "${EFI_DEBUGGER}" = "LLDB" ]; then
"$LLDB" -o "settings set plugin.process.gdb-remote.target-definition-file Scripts/x86_64_target_definition.py" \
-o "gdb-remote ${EFI_HOST}:${EFI_PORT}" \
-o "target create ${EFI_SYMS_PDB} ${EFI_SYMS}" \
-o "command script import Scripts/lldb_uefi.py" \
-o "command script add -c lldb_uefi.ReloadUefi reload-uefi" \
-o "reload-uefi" \
-o "b DebugBreak"
else
echo "Unsupported debugger ${EFI_DEBUGGER}!"
echo "Hint: use GDB or LLDB in EFI_DEBUGGER variable."
exit 1
fi
36 changes: 0 additions & 36 deletions Debug/rungdb.tool

This file was deleted.

33 changes: 0 additions & 33 deletions Debug/runlldb.tool

This file was deleted.

0 comments on commit a8ebda7

Please sign in to comment.