Skip to content
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

fix: split current iterm window during gdb.debug process #2341

Merged
merged 5 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ The table below shows which release corresponds to each branch, and what date th
- [#2323][2323] Retry failed lookups after one week in libcdb
- [#2325][2325] Match against local system libc first in libcdb
- [#2336][2336] Add `ELF.stripped` and `ELF.debuginfo` properties
- [#2338][2338] Fix: follow symlink for libs on ssh connection
- [#2341][2341] Launch GDB correctly in iTerm on Mac

[2242]: https://github.com/Gallopsled/pwntools/pull/2242
[2277]: https://github.com/Gallopsled/pwntools/pull/2277
Expand All @@ -105,6 +107,8 @@ The table below shows which release corresponds to each branch, and what date th
[2323]: https://github.com/Gallopsled/pwntools/pull/2323
[2325]: https://github.com/Gallopsled/pwntools/pull/2325
[2336]: https://github.com/Gallopsled/pwntools/pull/2336
[2338]: https://github.com/Gallopsled/pwntools/pull/2338
[2341]: https://github.com/Gallopsled/pwntools/pull/2341

## 4.12.0 (`beta`)

Expand Down
25 changes: 24 additions & 1 deletion pwnlib/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr
elif 'STY' in os.environ and which('screen'):
terminal = 'screen'
args = ['-t','pwntools-gdb','bash','-c']
elif 'TERM_PROGRAM' in os.environ and os.environ['TERM_PROGRAM'] == "iTerm.app" and which('osascript'):
# if we're on a mac, and using iTerm
terminal = "osascript"
args = []
elif 'TERM_PROGRAM' in os.environ and which(os.environ['TERM_PROGRAM']):
terminal = os.environ['TERM_PROGRAM']
args = []
Expand Down Expand Up @@ -366,7 +370,6 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr
args.extend(['wsl.exe', '-d', distro_name, 'bash', '-c'])
else:
args.extend(['bash.exe', '-c'])


if not terminal:
log.error('Could not find a terminal binary to use. Set context.terminal to your terminal.')
Expand Down Expand Up @@ -410,6 +413,26 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr
argv += [tmp.name]


# if we're on a Mac and use iTerm, we use `osascript` to split the current window
# `command` was sanitized on the previous step. It is now either a string, or was written to a tmp file
# we run the command, which is now `argv[-1]`
if terminal == 'osascript':
osa_script = f"""
tell application "iTerm"
tell current session of current window
set newSession to (split horizontally with default profile)
end tell
tell newSession
write text "{argv[-1]}"
end tell
end tell
"""
with tempfile.NamedTemporaryFile(delete=False, mode='wt+') as tmp:
tmp.write(osa_script.lstrip())
tmp.flush()
os.chmod(tmp.name, 0o700)
argv = [which(terminal), tmp.name]

log.debug("Launching a new terminal: %r" % argv)

stdin = stdout = stderr = open(os.devnull, 'r+b')
Expand Down
Loading