sweep.c 3.99 KB
Newer Older
1 2 3
/* vim:cindent:ts=2:sw=2:expandtab */

#include <stdlib.h>
Julian Kranz's avatar
---  
Julian Kranz committed
4 5
#include <stdint.h>
#include <stdio.h>
Julian Kranz's avatar
---  
Julian Kranz committed
6
#include <gdsl.h>
Julian Kranz's avatar
Julian Kranz committed
7
#include <sys/resource.h>
Julian Kranz's avatar
AVR  
Julian Kranz committed
8 9 10
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
Julian Kranz's avatar
Sweep  
Julian Kranz committed
11
#include <time.h>
Julian Kranz's avatar
---  
Julian Kranz committed
12

Julian Kranz's avatar
AVR  
Julian Kranz committed
13 14
#include <err.h>
#include <fcntl.h>
Julian Kranz's avatar
Julian Kranz committed
15

Julian Kranz's avatar
AVR  
Julian Kranz committed
16 17 18
#include <string.h>
#include <sysexits.h>

Julian Kranz's avatar
Julian Kranz committed
19
#include <gdsl_elf.h>
Julian Kranz's avatar
Sweep  
Julian Kranz committed
20

Julian Kranz's avatar
Julian Kranz committed
21
#define NANOS 1000000000LL
Julian Kranz's avatar
AVR  
Julian Kranz committed
22

Julian Kranz's avatar
Julian Kranz committed
23
int main(int argc, char** argv) {
Julian Kranz's avatar
Julian Kranz committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  const rlim_t kStackSize = 64L * 1024L * 1024L; // min stack size = 64 Mb
  struct rlimit rl;
  int result;

  result = getrlimit(RLIMIT_STACK, &rl);
  if(result == 0) {
    if(rl.rlim_cur < kStackSize) {
      rl.rlim_cur = kStackSize;
      result = setrlimit(RLIMIT_STACK, &rl);
      if(result != 0) {
        fprintf(stderr, "setrlimit returned result = %d\n", result);
      }
    }
  }

  size_t offset;
  size_t length;
  char *file;

  switch(argc) {
    case 2: {
      file = argv[1];

      char e = elf_section_boundary_get(file, &offset, &length);
      if(e) exit(2);
Julian Kranz's avatar
AVR  
Julian Kranz committed
49 50 51 52 53
//			offset = 0;
//
//			struct stat buf;
//			stat(file, &buf);
//			length = buf.st_size;
Julian Kranz's avatar
Julian Kranz committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
      break;
    }
    case 3: {
      file = argv[1];
      sscanf(argv[2], "%zu", &offset);

      struct stat buf;
      stat(file, &buf);
      length = buf.st_size;
      break;
    }
    case 4: {
      file = argv[1];
      sscanf(argv[2], "%zu", &offset);
      sscanf(argv[3], "%zu", &length);
      break;
    }
    default: {
      printf("Usage: sweep file offset length / sweep elf-file\n");
      return 1;
    }
  }

  FILE *f = fopen(file, "r");
  if(!f) {
    printf("Unable to open file.\n");
    return 1;
  }
  fseek(f, offset, SEEK_SET);

  size_t buffer_size = length + 15;
Julian Kranz's avatar
Julian Kranz committed
85
  char *buffer = (char*) malloc(buffer_size);
Julian Kranz's avatar
Julian Kranz committed
86 87
  size_t buffer_length = fread(buffer, 1, buffer_size, f);

Julian Kranz's avatar
Julian Kranz committed
88 89
  fclose(f);

Julian Kranz's avatar
Julian Kranz committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  state_t state = gdsl_init();
  gdsl_set_code(state, buffer, buffer_length, 0);

  struct timespec start;
  struct timespec end;

  size_t memory_dec = 0;
  size_t memory_dec_tran = 0;

  size_t memory_dec_max = 0;
  size_t memory_dec_tran_max = 0;

  size_t instructions = 0;

  clock_gettime(CLOCK_REALTIME, &start);

  size_t last_offset = 0;
  while(last_offset < length) {
Julian Kranz's avatar
...  
Julian Kranz committed
108
//		printf("++++++++++++ DECODING NEXT INSTRUCTION ++++++++++++\n");
Julian Kranz's avatar
---  
Julian Kranz committed
109

Julian Kranz's avatar
Julian Kranz committed
110 111 112 113 114
    if(setjmp(*gdsl_err_tgt(state))) {
      fprintf(stderr, "decode failed: %s\n", gdsl_get_error_message(state));
      break;
    }
    obj_t insn = gdsl_decode(state, gdsl_config_default(state));
Julian Kranz's avatar
---  
Julian Kranz committed
115

Julian Kranz's avatar
Julian Kranz committed
116 117 118 119 120 121 122
    printf("[");
    size_t decoded = gdsl_get_ip_offset(state) - last_offset;
    for(size_t i = 0; i < decoded; ++i) {
      if(i) printf(" ");
      printf("%02x", ((uint8_t*) buffer)[last_offset + i]);
    }
    printf("] ");
Julian Kranz's avatar
---  
Julian Kranz committed
123

Julian Kranz's avatar
Julian Kranz committed
124 125
    string_t fmt = gdsl_merge_rope(state, gdsl_pretty(state, insn));
    puts(fmt);
Julian Kranz's avatar
---  
Julian Kranz committed
126

Julian Kranz's avatar
Julian Kranz committed
127 128 129
    size_t residency = gdsl_heap_residency(state);
    memory_dec += residency;
    if(residency > memory_dec_max) memory_dec_max = residency;
Julian Kranz's avatar
Julian Kranz committed
130

Julian Kranz's avatar
Julian Kranz committed
131
//    printf("---------------------------\n");
Julian Kranz's avatar
---  
Julian Kranz committed
132

Julian Kranz's avatar
Julian Kranz committed
133 134 135 136
    if(setjmp(*gdsl_err_tgt(state))) {
      fprintf(stderr, "translate failed: %s\n", gdsl_get_error_message(state));
      break;
    }
Julian Kranz's avatar
Julian Kranz committed
137
    obj_t rreil = gdsl_translate(state, insn);
Julian Kranz's avatar
---  
Julian Kranz committed
138

Julian Kranz's avatar
Julian Kranz committed
139 140
//    fmt = gdsl_merge_rope(state, gdsl_rreil_pretty(state, rreil));
//    puts(fmt);
Julian Kranz's avatar
---  
Julian Kranz committed
141

Julian Kranz's avatar
Julian Kranz committed
142 143 144
    residency = gdsl_heap_residency(state);
    memory_dec_tran += residency;
    if(residency > memory_dec_tran_max) memory_dec_tran_max = residency;
Julian Kranz's avatar
Julian Kranz committed
145

Julian Kranz's avatar
Julian Kranz committed
146
    gdsl_reset_heap(state);
Julian Kranz's avatar
Sweep  
Julian Kranz committed
147

Julian Kranz's avatar
Julian Kranz committed
148 149 150
    instructions++;
    last_offset = gdsl_get_ip_offset(state);
  }
Julian Kranz's avatar
---  
Julian Kranz committed
151

Julian Kranz's avatar
Julian Kranz committed
152 153
  clock_gettime(CLOCK_REALTIME, &end);
  long time = end.tv_sec * NANOS + end.tv_nsec - start.tv_nsec - start.tv_sec * NANOS;
Julian Kranz's avatar
Sweep  
Julian Kranz committed
154

Julian Kranz's avatar
Julian Kranz committed
155
  gdsl_destroy(state);
Julian Kranz's avatar
Julian Kranz committed
156
  free(buffer);
Julian Kranz's avatar
---  
Julian Kranz committed
157

Julian Kranz's avatar
Julian Kranz committed
158 159 160 161 162
  fprintf(stderr, "---------------------------\n");
  fprintf(stderr, "Statistics\n");
  fprintf(stderr, "Instruction count: %zu\n", instructions);
  fprintf(stderr, "Decoder: Total memoy: %zu, maximal memoy: %zu\n", memory_dec, memory_dec_max);
  fprintf(stderr, "Decoder + Translator: Total memoy: %zu, maximal memoy: %zu\n", memory_dec_tran, memory_dec_tran_max);
Julian Kranz's avatar
Julian Kranz committed
163
  fprintf(stderr, "time: %lf seconds\n", time / (double) (1000000000));
Julian Kranz's avatar
Sweep  
Julian Kranz committed
164

Julian Kranz's avatar
Julian Kranz committed
165
  return 0;
166 167
}