Search code examples
linuxllvmlldb

LLDB pass bash command output as arg to application


I am trying to debug a program using lldb and I want to start the program with different arguments. Is it possible to pass the output of a bash command to a program when you start it in lldb?

As an example from bash I can do the following:

lldb program $(cat file.txt)

And the program will start with the contents of file.txt passed as the first argument.

When I am inside lldb I can run platform shell cat file.txt to get the contents of file and I can use either run -- args or process launch -- args to launch a process with a set of arguments.

However when trying to do this using run or process launch combined with platform shell command I get the command passed as the argument.

So as an example if I ran the command run -- platform shell cat file.txt and then settings show target.run-args I would get the following output:

[0]: "--"
  [1]: "platform"
  [2]: "shell"
  [3]: "cat"
  [4]: "file.txt"

I have tried all of the following with no success:

run -- platform shell cat payload.txt
run platform shell cat payload.txt
process launch -- platform shell cat payload.txt
process launch platform shell cat payload.txt
process launch -- platform shell $(cat payload.txt)
A whole bunch more combinations like this.

I have also searched for similar questions and found a few however none of them are getting me the results I want.

I feel like I am close to getting this right but not quite getting the syntax correct. Does anyone have any examples of how to do this correctly?


Solution

  • The correct syntax to get shell expansion is:

    (lldb) process launch -X 1 -- arg1 arg2

    But it looks like when handling an arg of the form $(aaa bbb) lldb inserts one too many levels of backslash in trying to form the arguments to pass to the process so this construct doesn't get substituted. It works for simple things like "$HOME" but not the "$(...)" construct.