Skip to content

Refactor, a bit cleaner. #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 18 additions & 23 deletions fbchisellldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,42 @@ def loadCommandsInDirectory(commandsDirectory):
for file in os.listdir(commandsDirectory):
fileName, fileExtension = os.path.splitext(file)
if fileExtension == ".py":
module = imp.load_source(fileName, os.path.join(commandsDirectory, file))
module = imp.load_source(fileName,
os.path.join(commandsDirectory, file))

if hasattr(module, "lldbinit"):
module.lldbinit()

if hasattr(module, "lldbcommands"):
module._loadedFunctions = {}
for command in module.lldbcommands():
loadCommand(
module, command, commandsDirectory, fileName, fileExtension
)
loadCommand(module, command, commandsDirectory, fileName,
fileExtension)


def loadCommand(module, command, directory, filename, extension):
func = makeRunCommand(command, os.path.join(directory, filename + extension))
func = makeRunCommand(command, os.path.join(directory,
filename + extension))
name = command.name()
helpText = (
command.description().strip().splitlines()[0]
) # first line of description
helpText = (command.description().strip().splitlines()[0]
) # first line of description

key = filename + "_" + name

module._loadedFunctions[key] = func

functionName = "__" + key

lldb.debugger.HandleCommand("script " + functionName + " = sys.modules['" +
module.__name__ + "']._loadedFunctions['" +
key + "']")
lldb.debugger.HandleCommand(
"script "
+ functionName
+ " = sys.modules['"
+ module.__name__
+ "']._loadedFunctions['"
+ key
+ "']"
)
lldb.debugger.HandleCommand(
'command script add --help "{help}" --function {function} {name}'.format(
'command script add --help "{help}" --function {function} {name}'.
format(
help=helpText.replace('"', '\\"'), # escape quotes
function=functionName,
name=name,
)
)
))


def makeRunCommand(command, filename):
Expand All @@ -92,7 +86,7 @@ def runCommand(debugger, input, exe_ctx, result, _):
# the initial args form an expression and combine them into a single arg.
if len(args) > len(command.args()):
overhead = len(args) - len(command.args())
head = args[: overhead + 1] # Take N+1 and reduce to 1.
head = args[:overhead + 1] # Take N+1 and reduce to 1.
args = [" ".join(head)] + args[-overhead:]

if validateArgsForCommand(args, command):
Expand All @@ -105,13 +99,14 @@ def runCommand(debugger, input, exe_ctx, result, _):
def validateArgsForCommand(args, command):
if len(args) < len(command.args()):
defaultArgs = [arg.default for arg in command.args()]
defaultArgsToAppend = defaultArgs[len(args) :]
defaultArgsToAppend = defaultArgs[len(args):]

index = len(args)
for defaultArg in defaultArgsToAppend:
if not defaultArg:
arg = command.args()[index]
print("Whoops! You are missing the <" + arg.argName + "> argument.")
print("Whoops! You are missing the <" + arg.argName +
"> argument.")
print("\nUsage: " + usageForCommand(command))
return
index += 1
Expand Down