import re
 from subprocess import *
 import argparse
+import platform
 
-from perf_trace_context import perf_set_itrace_options, \
-       perf_sample_insn, perf_sample_srccode
+from perf_trace_context import perf_sample_srccode, perf_config_get
 
 # Below are some example commands for using this script.
+# Note a --kcore recording is required for accurate decode
+# due to the alternatives patching mechanism. However this
+# script only supports reading vmlinux for disassembly dump,
+# meaning that any patched instructions will appear
+# as unpatched, but the instruction ranges themselves will
+# be correct. In addition to this, source line info comes
+# from Perf, and when using kcore there is no debug info. The
+# following lists the supported features in each mode:
+#
+# +-----------+-----------------+------------------+------------------+
+# | Recording | Accurate decode | Source line dump | Disassembly dump |
+# +-----------+-----------------+------------------+------------------+
+# | --kcore   | yes             | no               | yes              |
+# | normal    | no              | yes              | yes              |
+# +-----------+-----------------+------------------+------------------+
+#
+# Output disassembly with objdump and auto detect vmlinux
+# (when running on same machine.)
+#  perf script -s scripts/python/arm-cs-trace-disasm.py -d
 #
-# Output disassembly with objdump:
-#  perf script -s scripts/python/arm-cs-trace-disasm.py \
-#              -- -d objdump -k path/to/vmlinux
 # Output disassembly with llvm-objdump:
 #  perf script -s scripts/python/arm-cs-trace-disasm.py \
 #              -- -d llvm-objdump-11 -k path/to/vmlinux
+#
 # Output only source line and symbols:
 #  perf script -s scripts/python/arm-cs-trace-disasm.py
 
+def default_objdump():
+       config = perf_config_get("annotate.objdump")
+       return config if config else "objdump"
+
 # Command line parsing.
 args = argparse.ArgumentParser()
-args.add_argument("-k", "--vmlinux", help="Set path to vmlinux file")
-args.add_argument("-d", "--objdump", help="Set path to objdump executable file"),
+args.add_argument("-k", "--vmlinux",
+                 help="Set path to vmlinux file. Omit to autodetect if running on same machine")
+args.add_argument("-d", "--objdump", nargs="?", const=default_objdump(),
+                 help="Show disassembly. Can also be used to change the objdump path"),
 args.add_argument("-v", "--verbose", action="store_true", help="Enable debugging log")
 options = args.parse_args()
 
 glb_line_number                = None
 glb_dso                        = None
 
+kver = platform.release()
+vmlinux_paths = [
+       f"/usr/lib/debug/boot/vmlinux-{kver}.debug",
+       f"/usr/lib/debug/lib/modules/{kver}/vmlinux",
+       f"/lib/modules/{kver}/build/vmlinux",
+       f"/usr/lib/debug/boot/vmlinux-{kver}",
+       f"/boot/vmlinux-{kver}",
+       f"/boot/vmlinux",
+       f"vmlinux"
+]
+
 def get_optional(perf_dict, field):
        if field in perf_dict:
                return perf_dict[field]
                return "+%#x" % perf_dict[field]
        return ""
 
+def find_vmlinux():
+       if hasattr(find_vmlinux, "path"):
+               return find_vmlinux.path
+
+       for v in vmlinux_paths:
+               if os.access(v, os.R_OK):
+                       find_vmlinux.path = v
+                       break
+       else:
+               find_vmlinux.path = None
+
+       return find_vmlinux.path
+
 def get_dso_file_path(dso_name, dso_build_id):
        if (dso_name == "[kernel.kallsyms]" or dso_name == "vmlinux"):
                if (options.vmlinux):
                        return options.vmlinux;
                else:
-                       return dso_name
+                       return find_vmlinux() if find_vmlinux() else dso_name
 
        if (dso_name == "[vdso]") :
                append = "/vdso"