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
24 #define unlikely(cond) (cond)
31 * Test of instruction analysis in general and insn_get_length() in
32 * particular. See if insn_get_length() and the disassembler agree
33 * on the length of each instruction in an elf disassembly.
35 * Usage: objdump -d a.out | awk -f distill.awk | ./test_get_len
40 static void usage(void)
42 fprintf(stderr, "Usage: objdump -d a.out | awk -f distill.awk |"
43 " %s [y|n](64bit flag)\n", prog);
47 static void malformed_line(const char *line, int line_nr)
49 fprintf(stderr, "%s: malformed line %d:\n%s", prog, line_nr, line);
55 int main(int argc, char **argv)
58 unsigned char insn_buf[16];
67 if (argc == 2 && argv[1][0] == 'y')
70 while (fgets(line, BUFSIZE, stdin)) {
71 char copy[BUFSIZE], *s, *tab1, *tab2;
76 memset(insn_buf, 0, 16);
78 tab1 = strchr(copy, '\t');
80 malformed_line(line, insns);
83 tab2 = strchr(s, '\t');
85 malformed_line(line, insns);
86 *tab2 = '\0'; /* Characters beyond tab2 aren't examined */
88 if (sscanf(s, "%x", &b) == 1) {
89 insn_buf[nb++] = (unsigned char) b;
94 /* Decode an instruction */
95 insn_init(&insn, insn_buf, x86_64);
96 insn_get_length(&insn);
97 if (insn.length != nb) {
98 fprintf(stderr, "Error: %s", line);
99 fprintf(stderr, "Error: objdump says %d bytes, but "
100 "insn_get_length() says %d (attr:%x)\n", nb,
101 insn.length, insn.attr);
105 fprintf(stderr, "Succeed: decoded and checked %d instructions\n",