Skip to content

Commit

Permalink
Resolve modes better
Browse files Browse the repository at this point in the history
  • Loading branch information
wmww committed Sep 1, 2022
1 parent 0d0a245 commit c0f5fc9
Showing 1 changed file with 52 additions and 19 deletions.
71 changes: 52 additions & 19 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import logging
from typing import Callable, List
from enum import Enum

from interfaces import UIState, ConnectionIDSink, CommandSink
from core import matcher, ConnectionManager
Expand Down Expand Up @@ -46,6 +47,41 @@ def file_input_main(
ui.run_until_stopped()
logger.info('Done with file')

class Mode(str, Enum):
NONE = 'none'
RUN = 'run'
GDB_RUNNER = 'gdb-runner'
GDB_PLUGIN = 'gdb-plugin'
LOAD_FROM_FILE = 'load-from-file'
PIPE = 'pipe'

def select_mode(parsed_command: ParsedCommand, args, output) -> Mode:
modes = []
if parsed_command.command == '':
pass
elif parsed_command.command == 'g':
modes.append(Mode.GDB_RUNNER)
elif parsed_command.command == 'r':
modes.append(Mode.RUN)
else:
assert False, 'invalid command ' + parsed_command.command

if check_gdb():
modes.append(Mode.GDB_PLUGIN)
if args.path is not None:
modes.append(Mode.LOAD_FROM_FILE)
if args.pipe:
modes.append(Mode.PIPE)

if len(modes) == 0:
output.error('No mode specified')
return Mode.NONE
elif len(modes) > 1:
output.error(', '.join(modes[:-1]) + ' and ' + modes[-1] + ' modes conflict, please specify a single mode')
return Mode.NONE
else:
return modes[0]

def main(out_stream: stream.Base, err_stream: stream.Base, argv: List[str], input_func: Callable[[str], str]) -> None:
'''
Parse arguments and run wayland-debug
Expand All @@ -57,6 +93,7 @@ def main(out_stream: stream.Base, err_stream: stream.Base, argv: List[str], inpu

parsed_command = parse_command(argv, [
['-g', '--gdb'],
['-r', '--run'],
])

# If we want to run inside GDB, the rest of main does not get called in this instance of the script
Expand All @@ -72,21 +109,20 @@ def main(out_stream: stream.Base, err_stream: stream.Base, argv: List[str], inpu
import argparse
parser = argparse.ArgumentParser(description='Debug Wayland protocol messages, see https://github.com/wmww/wayland-debug for additional info')
parser.add_argument('--matcher-help', action='store_true', help='show how to write matchers and exit')
parser.add_argument('-r', '--run', action='store_true', help='run the following program and parse it\'s libwayland debugging messages. All subsequent command line arguments are sent to the program')
parser.add_argument('-g', '--gdb', action='store_true', help='run inside gdb. All subsequent arguments are sent to gdb. When inside gdb start commands with \'wl\'')
parser.add_argument('-v', '--verbose', action='store_true', help='verbose output, mostly used for debugging this program')
parser.add_argument('-g', '--gdb', action='store_true', help='run inside gdb, all subsequent arguments are sent to gdb, when inside gdb start commands with \'wl\'')
parser.add_argument('-l', '--load', dest='path', type=str, help='load WAYLAND_DEBUG=1 messages from a file')
parser.add_argument('-p', '--pipe', action='store_true', help='receive WAYLAND_DEBUG=1 messages from stdin (note: messages are printed to stderr so you may want to redirect using 2>&1 before piping)')
parser.add_argument('-s', '--supress', action='store_true', help='supress non-wayland output of the program')
parser.add_argument('-c', '--color', action='store_true', help='force color output (default for interactive sessions)')
parser.add_argument('-C', '--no-color', action='store_true', help='disable color output (default for non-interactive sessions)')
parser.add_argument('-f', '--filter', dest='f', type=str, help='only show these objects/messages (see --matcher-help for syntax)')
parser.add_argument('-b', '--break', dest='b', type=str, help='stop on these objects/messages (see --matcher-help for syntax)')
parser.add_argument('--libwayland', type=str, help='path to directory that contains libwayland-client.so and libwayland-server.so. Only applies to GDB mode. Must come before --gdb argument')
# NOTE: -g/--gdb and --libwayland are here only for the help text, they are processed without argparse
parser.add_argument('--libwayland', type=str, help='path to directory that contains libwayland-client.so and libwayland-server.so. Only applies to GDB and run mode. Must come before --gdb/--run argument')
# NOTE: -g/--gdb, -r/--run and --libwayland are here only for the help text, they are processed without argparse

args = parser.parse_args(args=argv[1:]) # chop off the first argument (program name)

assert not args.gdb, 'GDB argument should have been intercepted by gdb_plugin.runner.parse_args()'
args = parser.parse_args(args=parsed_command.wayland_debug_args[1:]) # chop off the first argument (program name)

if args.no_color:
set_color_output(False)
Expand Down Expand Up @@ -140,28 +176,25 @@ def main(out_stream: stream.Base, err_stream: stream.Base, argv: List[str], inpu
connection_list = ConnectionManager()
ui_controller = Controller(output, connection_list, filter_matcher, stop_matcher)

file_path = args.path
input_from_pipe = args.pipe

if check_gdb():
mode = select_mode(parsed_command, args, output)
if mode is None:
parser.print_help()
elif mode == Mode.GDB_PLUGIN:
try:
if file_path is not None or input_from_pipe:
output.warn('Ignoring load/pipe argument because we\'re inside GDB')
gdb_plugin.plugin.Plugin(output, connection_list, ui_controller, ui_controller)
except:
import traceback
traceback.print_exc()
elif file_path is not None:
if input_from_pipe:
output.warn('Ignoring piped input because load file is specified')
file_input_main(file_path, output, connection_list, ui_controller, ui_controller, input_func)
elif input_from_pipe:
elif mode == Mode.LOAD_FROM_FILE:
file_input_main(args.path, output, connection_list, ui_controller, ui_controller, input_func)
elif mode == Mode.PIPE:
if args.b:
output.warn('Ignoring stop matcher when stdin is used for messages')
piped_input_main(output, connection_list)
elif mode == Mode.RUN:
output.error('Run mode not supported yet')
else:
output.warn('No action specified')
parser.print_help()
assert False, 'invalid mode ' + repr(mode)

def premain() -> None:
# If isatty() is false, we might be redirecting to a file (or in another non-interactive context)
Expand Down

0 comments on commit c0f5fc9

Please sign in to comment.