test_get_len.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  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.
  6. *
  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.
  11. *
  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.
  15. *
  16. * Copyright (C) IBM Corporation, 2009
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <assert.h>
  22. #define unlikely(cond) (cond)
  23. #include <asm/insn.h>
  24. #include <inat.c>
  25. #include <insn.c>
  26. /*
  27. * Test of instruction analysis in general and insn_get_length() in
  28. * particular. See if insn_get_length() and the disassembler agree
  29. * on the length of each instruction in an elf disassembly.
  30. *
  31. * Usage: objdump -d a.out | awk -f distill.awk | ./test_get_len
  32. */
  33. const char *prog;
  34. static void usage(void)
  35. {
  36. fprintf(stderr, "Usage: objdump -d a.out | awk -f distill.awk |"
  37. " %s [y|n](64bit flag)\n", prog);
  38. exit(1);
  39. }
  40. static void malformed_line(const char *line, int line_nr)
  41. {
  42. fprintf(stderr, "%s: malformed line %d:\n%s", prog, line_nr, line);
  43. exit(3);
  44. }
  45. #define BUFSIZE 256
  46. int main(int argc, char **argv)
  47. {
  48. char line[BUFSIZE];
  49. unsigned char insn_buf[16];
  50. struct insn insn;
  51. int insns = 0;
  52. int x86_64 = 0;
  53. prog = argv[0];
  54. if (argc > 2)
  55. usage();
  56. if (argc == 2 && argv[1][0] == 'y')
  57. x86_64 = 1;
  58. while (fgets(line, BUFSIZE, stdin)) {
  59. char copy[BUFSIZE], *s, *tab1, *tab2;
  60. int nb = 0;
  61. unsigned int b;
  62. insns++;
  63. memset(insn_buf, 0, 16);
  64. strcpy(copy, line);
  65. tab1 = strchr(copy, '\t');
  66. if (!tab1)
  67. malformed_line(line, insns);
  68. s = tab1 + 1;
  69. s += strspn(s, " ");
  70. tab2 = strchr(s, '\t');
  71. if (!tab2)
  72. malformed_line(line, insns);
  73. *tab2 = '\0'; /* Characters beyond tab2 aren't examined */
  74. while (s < tab2) {
  75. if (sscanf(s, "%x", &b) == 1) {
  76. insn_buf[nb++] = (unsigned char) b;
  77. s += 3;
  78. } else
  79. break;
  80. }
  81. /* Decode an instruction */
  82. insn_init(&insn, insn_buf, x86_64);
  83. insn_get_length(&insn);
  84. if (insn.length != nb) {
  85. fprintf(stderr, "Error: %s", line);
  86. fprintf(stderr, "Error: objdump says %d bytes, but "
  87. "insn_get_length() says %d (attr:%x)\n", nb,
  88. insn.length, insn.attr);
  89. exit(2);
  90. }
  91. }
  92. fprintf(stderr, "Succeed: decoded and checked %d instructions\n",
  93. insns);
  94. return 0;
  95. }