2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) IBM Corporation, 2009
29 #define unlikely(cond) (cond)
36 * Test of instruction analysis in general and insn_get_length() in
37 * particular. See if insn_get_length() and the disassembler agree
38 * on the length of each instruction in an elf disassembly.
40 * Usage: objdump -d a.out | awk -f distill.awk | ./test_get_len
45 static void usage(void)
47 fprintf(stderr, "Usage: objdump -d a.out | awk -f distill.awk |"
48 " %s [y|n](64bit flag)\n", prog);
52 static void malformed_line(const char *line, int line_nr)
54 fprintf(stderr, "%s: malformed line %d:\n%s", prog, line_nr, line);
60 int main(int argc, char **argv)
63 unsigned char insn_buf[16];
72 if (argc == 2 && argv[1][0] == 'y')
75 while (fgets(line, BUFSIZE, stdin)) {
76 char copy[BUFSIZE], *s, *tab1, *tab2;
81 memset(insn_buf, 0, 16);
83 tab1 = strchr(copy, '\t');
85 malformed_line(line, insns);
88 tab2 = strchr(s, '\t');
90 malformed_line(line, insns);
91 *tab2 = '\0'; /* Characters beyond tab2 aren't examined */
93 if (sscanf(s, "%x", &b) == 1) {
94 insn_buf[nb++] = (unsigned char) b;
99 /* Decode an instruction */
100 insn_init(&insn, insn_buf, x86_64);
101 insn_get_length(&insn);
102 if (insn.length != nb) {
103 fprintf(stderr, "Error: %s", line);
104 fprintf(stderr, "Error: objdump says %d bytes, but "
105 "insn_get_length() says %d (attr:%x)\n", nb,
106 insn.length, insn.attr);
110 fprintf(stderr, "Succeed: decoded and checked %d instructions\n",