python-uncompyle6/uncompyle6/bin/pydisassemble.py

108 lines
3.1 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python
#
2024-02-05 21:26:59 +00:00
# Copyright (c) 2015-2016, 2018, 2020, 2022-2024
# by Rocky Bernstein <rb@dustyfeet.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
2023-05-29 15:00:44 +00:00
import getopt
import os
import sys
from uncompyle6.code_fns import disassemble_file
2020-10-31 15:25:06 +00:00
from uncompyle6.version import __version__
program, ext = os.path.splitext(os.path.basename(__file__))
__doc__ = """
Usage:
2018-02-15 12:30:01 +00:00
{0} [OPTIONS]... FILE
{0} [--help | -h | -V | --version]
2022-04-26 22:09:16 +00:00
Disassemble/Tokenize FILE with in the way that is done to
2018-02-15 12:30:01 +00:00
assist uncompyle6 in parsing the instruction stream. For example
instructions with variable-length arguments like CALL_FUNCTION and
2020-01-28 00:41:55 +00:00
BUILD_LIST have argument counts appended to the instruction name, and
2023-12-17 15:52:32 +00:00
COME_FROM pseudo instructions are inserted into the instruction stream.
2022-04-26 22:09:16 +00:00
Bit flag values encoded in an operand are expanding, EXTENDED_ARG
value are folded into the following instruction operand.
Like the parser, you may find this more high-level and or helpful.
However if you want a true disassembler see the Standard built-in
Python library module "dis", or pydisasm from the cross-version
2022-04-26 22:09:16 +00:00
Python bytecode package "xdis".
Examples:
{0} foo.pyc
{0} foo.py # same thing as above but find the file
{0} foo.pyc bar.pyc # disassemble foo.pyc and bar.pyc
2018-02-15 12:30:01 +00:00
See also `pydisasm' from the `xdis' package.
Options:
-V | --version show version and stop
-h | --help show this message
2023-05-29 15:00:44 +00:00
""".format(
program
)
PATTERNS = ("*.pyc", "*.pyo")
def main():
2024-02-05 21:20:15 +00:00
usage_short = (
2024-02-05 21:26:59 +00:00
f"""usage: {program} FILE...
2023-05-29 15:00:44 +00:00
Type -h for for full help."""
)
if len(sys.argv) == 1:
print("No file(s) given", file=sys.stderr)
2024-02-05 21:20:15 +00:00
print(usage_short, file=sys.stderr)
sys.exit(1)
try:
2023-05-29 15:00:44 +00:00
opts, files = getopt.getopt(
sys.argv[1:], "hVU", ["help", "version", "uncompyle6"]
)
except getopt.GetoptError as e:
2024-02-05 21:26:59 +00:00
print(f"{os.path.basename(sys.argv[0])}: {e}", file=sys.stderr)
sys.exit(-1)
for opt, val in opts:
2023-05-29 15:00:44 +00:00
if opt in ("-h", "--help"):
print(__doc__)
sys.exit(1)
2023-05-29 15:00:44 +00:00
elif opt in ("-V", "--version"):
2024-02-05 21:26:59 +00:00
print(f"{program} {__version__}")
sys.exit(0)
else:
print(opt)
2024-02-05 21:20:15 +00:00
print(usage_short, file=sys.stderr)
sys.exit(1)
for file in files:
if os.path.exists(files[0]):
2018-02-15 12:30:01 +00:00
disassemble_file(file, sys.stdout)
else:
2024-02-05 21:26:59 +00:00
print(f"Can't read {files[0]} - skipping", file=sys.stderr)
pass
pass
return
2023-05-29 15:00:44 +00:00
if __name__ == "__main__":
main()