Skip to content
Permalink
Browse files

Add option to redirect context output to other tty or files (#610)

* Add option to redirect context output to other tty or files

The output of context/dashboard can be now be redirected with
 "set context-output /dev/pts/x" to everything wich python can open and
 offers a file like "write".

* Sorted imports correctly (fixing pwndbg CI)
  • Loading branch information...
jerdna-regeiz authored and disconnect3d committed Apr 15, 2019
1 parent f1c0f09 commit 35890f0430878cecc4aee81f18735971a740a02d
Showing with 27 additions and 11 deletions.
  1. +2 −0 FEATURES.md
  2. +23 −8 pwndbg/commands/context.py
  3. +2 −3 pwndbg/commands/peda.py
@@ -17,6 +17,8 @@ All function call sites are annotated with the arguments to those functions. Th

A useful summary of the current execution context is printed every time GDB stops (e.g. breakpoint or single-step), displaying all registers, the stack, call frames, disassembly, and additionally recursively dereferencing all pointers. All memory addresses are color-coded to the type of memory they represent.

The output of the context may be redirected to a file (including other tty) by using `set context-output /path/to/file` while leaving other output in place.

![](caps/context.png)

## Disassembly
@@ -7,8 +7,8 @@

import ast
import codecs
import sys
import ctypes
import sys
from io import open

import gdb
@@ -35,14 +35,15 @@
from pwndbg.color import theme


def clear_screen():
def clear_screen(out=sys.stdout):
"""
Clear the screen by moving the cursor to top-left corner and
clear the content
"""
sys.stdout.write('\x1b[H\x1b[J')
out.write('\x1b[H\x1b[J')

config_clear_screen = pwndbg.config.Parameter('context-clear-screen', False, 'whether to clear the screen before printing the context')
config_output = pwndbg.config.Parameter('context-output', 'stdout', 'where pwndbg should output ("stdout" or file/tty).')
config_context_sections = pwndbg.config.Parameter('context-sections',
'regs disasm code stack backtrace',
'which context sections are displayed (controls order)')
@@ -66,6 +67,19 @@ def validate_context_sections():
config_context_sections.revert_default()
return

class StdOutput(object):
"""A context manager wrapper to give stdout"""
def __enter__(*args,**kwargs):
return sys.stdout
def __exit__(*args, **kwargs):
pass

def output():
"""Creates a context manager corresponding to configured context ouput"""
if not config_output or config_output == "stdout":
return StdOutput()
else:
return open(str( config_output ), "w")

# @pwndbg.events.stop
@pwndbg.commands.Command
@@ -89,12 +103,13 @@ def context(*args):
result.extend(func())
result.extend(context_signal())

if config_clear_screen:
clear_screen()
with output() as out:
if config_clear_screen:
clear_screen(out)

for line in result:
sys.stdout.write(line + '\n')
sys.stdout.flush()
for line in result:
out.write(line + '\n')
out.flush()


def context_regs():
@@ -5,14 +5,13 @@
from __future__ import print_function
from __future__ import unicode_literals

import argparse
import sys

import gdb

import argparse

import pwndbg.color.message as message
import pwndbg.auxv
import pwndbg.color.message as message
import pwndbg.commands
import pwndbg.commands.context
import pwndbg.commands.telescope

0 comments on commit 35890f0

Please sign in to comment.
You can’t perform that action at this time.