linux/arch/x86/tools/distill.awk
<<
>>
Prefs
   1#!/bin/awk -f
   2# Usage: objdump -d a.out | awk -f distill.awk | ./test_get_len
   3# Distills the disassembly as follows:
   4# - Removes all lines except the disassembled instructions.
   5# - For instructions that exceed 1 line (7 bytes), crams all the hex bytes
   6# into a single line.
   7# - Remove bad(or prefix only) instructions
   8
   9BEGIN {
  10        prev_addr = ""
  11        prev_hex = ""
  12        prev_mnemonic = ""
  13        bad_expr = "(\\(bad\\)|^rex|^.byte|^rep(z|nz)$|^lock$|^es$|^cs$|^ss$|^ds$|^fs$|^gs$|^data(16|32)$|^addr(16|32|64))"
  14        fwait_expr = "^9b "
  15        fwait_str="9b\tfwait"
  16}
  17
  18/^ *[0-9a-f]+ <[^>]*>:/ {
  19        # Symbol entry
  20        printf("%s%s\n", $2, $1)
  21}
  22
  23/^ *[0-9a-f]+:/ {
  24        if (split($0, field, "\t") < 3) {
  25                # This is a continuation of the same insn.
  26                prev_hex = prev_hex field[2]
  27        } else {
  28                # Skip bad instructions
  29                if (match(prev_mnemonic, bad_expr))
  30                        prev_addr = ""
  31                # Split fwait from other f* instructions
  32                if (match(prev_hex, fwait_expr) && prev_mnemonic != "fwait") {
  33                        printf "%s\t%s\n", prev_addr, fwait_str
  34                        sub(fwait_expr, "", prev_hex)
  35                }
  36                if (prev_addr != "")
  37                        printf "%s\t%s\t%s\n", prev_addr, prev_hex, prev_mnemonic
  38                prev_addr = field[1]
  39                prev_hex = field[2]
  40                prev_mnemonic = field[3]
  41        }
  42}
  43
  44END {
  45        if (prev_addr != "")
  46                printf "%s\t%s\t%s\n", prev_addr, prev_hex, prev_mnemonic
  47}
  48