Beispiel #1
0
static void
event_thread_init(void *drcontext)
{
    per_thread_t *data;

    /* allocate thread private data */
    data = dr_thread_alloc(drcontext, sizeof(per_thread_t));
    drmgr_set_tls_field(drcontext, tls_index, data);
    data->buf_base = dr_thread_alloc(drcontext, MEM_BUF_SIZE);
    data->buf_ptr  = data->buf_base;
    /* set buf_end to be negative of address of buffer end for the lea later */
    data->buf_end  = -(ptr_int_t)(data->buf_base + MEM_BUF_SIZE);
    data->num_refs = 0;

    /* We're going to dump our data to a per-thread file.
     * On Windows we need an absolute path so we place it in
     * the same directory as our library. We could also pass
     * in a path as a client argument.
     */
    data->log = log_file_open(client_id, drcontext, NULL /* using client lib path */,
                              "instrace",
#ifndef WINDOWS
                              DR_FILE_CLOSE_ON_FORK |
#endif
                              DR_FILE_ALLOW_LARGE);
#ifdef OUTPUT_TEXT
    data->logf = log_stream_from_file(data->log);
    fprintf(data->logf, "Format: <instr address>,<opcode>\n");
#endif
}
Beispiel #2
0
static void
event_thread_init(void *drcontext)
{
    per_thread_t *data = dr_thread_alloc(drcontext, sizeof(per_thread_t));
    DR_ASSERT(data != NULL);
    drmgr_set_tls_field(drcontext, tls_idx, data);

    /* Keep seg_base in a per-thread data structure so we can get the TLS
     * slot and find where the pointer points to in the buffer.
     */
    data->seg_base = dr_get_dr_segment_base(tls_seg);
    data->buf_base =
        dr_raw_mem_alloc(MEM_BUF_SIZE, DR_MEMPROT_READ | DR_MEMPROT_WRITE, NULL);
    DR_ASSERT(data->seg_base != NULL && data->buf_base != NULL);
    /* put buf_base to TLS as starting buf_ptr */
    BUF_PTR(data->seg_base) = data->buf_base;

    data->num_refs = 0;

    /* We're going to dump our data to a per-thread file.
     * On Windows we need an absolute path so we place it in
     * the same directory as our library. We could also pass
     * in a path as a client argument.
     */
    data->log =
        log_file_open(client_id, drcontext, NULL /* using client lib path */, "memtrace",
#ifndef WINDOWS
                      DR_FILE_CLOSE_ON_FORK |
#endif
                          DR_FILE_ALLOW_LARGE);
    data->logf = log_stream_from_file(data->log);
    fprintf(data->logf, "Format: <data address>: <data size>, <(r)ead/(w)rite/opcode>\n");
}
Beispiel #3
0
static void
event_exit(void)
{
    file_t f;
    /* Display the results! */
    char msg[512];
    int len;
    len = dr_snprintf(msg, sizeof(msg)/sizeof(msg[0]),
                      "Instrumentation results:\n"
                      "  saw %" STAT_FORMAT_CODE " flops\n", stats->num_flops);
    DR_ASSERT(len > 0);
    msg[sizeof(msg)/sizeof(msg[0])-1] = '\0';
#ifdef SHOW_RESULTS
    DISPLAY_STRING(msg);
#endif /* SHOW_RESULTS */

    /* On Windows we need an absolute path so we place it in
     * the same directory as our library.
     */
    f = log_file_open(my_id, NULL, NULL /* client lib path */, "stats", 0);
    DR_ASSERT(f != INVALID_FILE);
    dr_fprintf(f, "%s\n", msg);
    dr_close_file(f);

    shared_memory_exit();

    drx_exit();
    if (!drmgr_unregister_bb_instrumentation_event(event_analyze_bb))
        DR_ASSERT(false);
    drmgr_exit();
}
Beispiel #4
0
static void
event_thread_init(void *drcontext)
{
    file_t log;
    log = log_file_open(client_id, drcontext, NULL /* using client lib path */,
                        "cbrtrace",
#ifndef WINDOWS
                        DR_FILE_CLOSE_ON_FORK |
#endif
                        DR_FILE_ALLOW_LARGE);
    DR_ASSERT(log != INVALID_FILE);
    dr_set_tls_field(drcontext, (void *)(ptr_uint_t)log);
}
void log_message(LOG_LEVEL log_evel, int line, const char *funcname, const char *fmt, ...)
{
    struct timespec now;
    struct tm *p;
    char timep[128] = {0};
    char errorinfo[128] = {0};
    char str[1024] = {0};
    char msg[1024] = {0};
    va_list vargs;


    if(log_evel < log_filter)
    {
        return ;
    }
    
    pthread_mutex_lock(&log_thread_mutex);

    va_start(vargs, fmt); 

    vsnprintf(str, sizeof(str), fmt, vargs);
    /*get time string*/
    clock_gettime(CLOCK_REALTIME, &now);
    p = localtime(&now.tv_sec);
    snprintf(timep, sizeof(timep), "%04d-%02d-%02d %02d:%02d:%02d:%03d", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, (int)(now.tv_nsec/1000000));

    snprintf(msg, sizeof(msg), "%s: %s : %s:%d : %s\n", timep,  log_level_string(log_evel), funcname, line, str);
    if (NULL == msg)
        goto cleanup;

    log_file_open();

    if (gLogFd > 0)
    {
        if (safewrite(gLogFd, msg, strlen(msg)) < 0)
        {
            print_log_to_syslog(msg);  
        }
        close(gLogFd);
    }
    else
    {
       print_log_to_syslog(msg); 
    } 
cleanup:
    va_end(vargs);
    pthread_mutex_unlock(&log_thread_mutex);
}
Beispiel #6
0
static int set_log_file_name(const char *val, void *param)
{
    if (locked) {
        return 0;
    }

    if (util_string_set(&log_file_name, val) < 0) {
        return 0;
    }

    if (log_file) {
        fclose(log_file);
        log_file_open();
    }

    return 0;
}
Beispiel #7
0
int log_init(void)
{
#if 0
    /*
     * The current calling order in main.c (log_init() after processing
     * resources) makes this break if anything in the resource set_*
     * functions does a log_open().  On platforms that have no regular
     * stdout (e.g win32) no logging will be seen.  On win32 startup will
     * also be preceeded by a modal error requester.  / tlr
     */
    if (logs != NULL)
        return -1;
#endif

    log_file_open();

    return log_file == NULL ? -1 : 0;
}
Beispiel #8
0
DR_EXPORT void
dr_client_main(client_id_t id, int argc, const char *argv[])
{
    /* We need no drreg slots ourselves, but we initialize drreg as we call
     * drreg_restore_app_values(), required since drx_insert_counter_update()
     * uses drreg when drmgr is used.
     */
    drreg_options_t ops = {sizeof(ops)};
    dr_set_client_name("DynamoRIO Sample Client 'modxfer'",
                       "http://dynamorio.org/issues");
    if (!drmgr_init() || drreg_init(&ops) != DRREG_SUCCESS)
        DR_ASSERT(false);
    drx_init();
    /* register events */
    dr_register_exit_event(event_exit);
    if (!drmgr_register_bb_instrumentation_event(event_analyze_bb,
                                                 event_insert_instrumentation, NULL))
        DR_ASSERT(false);
    drmgr_register_module_load_event(event_module_load);
    drmgr_register_module_unload_event(event_module_unload);

    mod_lock = dr_mutex_create();

    logfile = log_file_open(id, NULL /* drcontext */,
                            NULL/* path */, "modxfer",
#ifndef WINDOWS
                            DR_FILE_CLOSE_ON_FORK |
#endif
                            DR_FILE_ALLOW_LARGE);

    DR_ASSERT(logfile != INVALID_FILE);
    /* make it easy to tell, by looking at log file, which client executed */
    dr_log(NULL, LOG_ALL, 1, "Client 'modxfer' initializing\n");
#ifdef SHOW_RESULTS
    /* also give notification to stderr */
    if (dr_is_notify_on()) {
# ifdef WINDOWS
        /* ask for best-effort printing to cmd window.  must be called at init. */
        dr_enable_console_printing();
# endif
        dr_fprintf(STDERR, "Client modxfer is running\n");
    }
#endif
}
Beispiel #9
0
static void
event_thread_init(void *drcontext)
{
    file_t f;
    /* We're going to dump our data to a per-thread file.
     * On Windows we need an absolute path so we place it in
     * the same directory as our library. We could also pass
     * in a path and retrieve with dr_get_options().
     */
    f = log_file_open(my_id, drcontext, NULL /* client lib path */,
                      "instrcalls",
#ifndef WINDOWS
                      DR_FILE_CLOSE_ON_FORK |
#endif
                      DR_FILE_ALLOW_LARGE);
    DR_ASSERT(f != INVALID_FILE);

    /* store it in the slot provided in the drcontext */
    dr_set_tls_field(drcontext, (void *)(ptr_uint_t)f);
}
Beispiel #10
0
int DEFAULT_CC
log_start(struct log_config* l_cfg)
{
  if (0 == l_cfg)
  {
    return LOG_ERROR_MALLOC;
  }

  /* if logfile is NULL, we use a default logfile */
  if (0 == l_cfg->log_file)
  {
    l_cfg->log_file = g_strdup("./myprogram.log");
  }

  /* if progname is NULL, we use a default name */
  if (0 == l_cfg->program_name)
  {
    l_cfg->program_name = g_strdup("myprogram");
  }

  /* open file */
  l_cfg->fd = log_file_open(l_cfg->log_file);

  if (-1 == l_cfg->fd)
  {
    return LOG_ERROR_FILE_OPEN;
  }

  /* if syslog is enabled, open it */
  if (l_cfg->enable_syslog)
  {
    openlog(l_cfg->program_name, LOG_CONS | LOG_PID, LOG_DAEMON);
  }

#ifdef LOG_ENABLE_THREAD
  pthread_mutexattr_init(&(l_cfg->log_lock_attr));
  pthread_mutex_init(&(l_cfg->log_lock), &(l_cfg->log_lock_attr));
#endif

  return LOG_STARTUP_OK;
}
Beispiel #11
0
static void
event_thread_init(void *drcontext)
{
    per_thread_t *data = dr_thread_alloc(drcontext, sizeof(per_thread_t));
    DR_ASSERT(data != NULL);
    data->reg_addr = DR_REG_NULL;
    drmgr_set_tls_field(drcontext, tls_idx, data);

    /* We're going to dump our data to a per-thread file.
     * On Windows we need an absolute path so we place it in
     * the same directory as our library. We could also pass
     * in a path as a client argument.
     */
    data->log =
        log_file_open(client_id, drcontext, NULL /* using client lib path */, "memval",
#ifndef WINDOWS
                      DR_FILE_CLOSE_ON_FORK |
#endif
                          DR_FILE_ALLOW_LARGE);
    data->logf = log_stream_from_file(data->log);
}
Beispiel #12
0
main
( int argc,
  char **argv
)
{ 
  int lindex, hindex, index_mod;

  log_file logf;
  log_gobbled logg;

  char *values;

  int first, pass;

  mc_temp_sched *sch;
  mc_temp_state *ts;
  mc_iter *it;

  char **ap;

  double ww;
  char *v;
  int i;

  /* Look at arguments other than log files and ranges. */

  if (argc<3) usage();

  values = argv[1];

  /* Set records sizes required. */

  logg.req_size['i'] = sizeof (mc_iter);
  logg.req_size['o'] = sizeof (mc_ops);
  logg.req_size['t'] = sizeof (mc_traj);
  logg.req_size['b'] = sizeof (mc_temp_state);
  logg.req_size['m'] = sizeof (mc_temp_sched);

  /* Go twice through all the log files and ranges specified.  Compute
     means the first time, variances the second. */

  for (pass = 1; pass<=2; pass++)
  {
    first = 1;

    ap = argv+2;  

    while (*ap)
    {
      /* Look at arguments giving log file and range. */
  
      logf.file_name = *ap++;
  
      if (*ap!=0 && strchr(":%0123456789",**ap)!=0)
      { parse_range(*ap,&lindex,&hindex,&index_mod);
        if (index_mod<0)
        { fprintf(stderr,"Bad range specification: %s\n",*ap);
          exit(1);
        }
        ap += 1;
      }
      else
      { lindex = 1;
        hindex = -1;
        index_mod = 1;
      }
  
      if (hindex<0) hindex = 1000000000;
  
      /* Open log file and set up for gobbling. */
    
      log_file_open(&logf,0);
  
      log_gobble_init(&logg,!first);
      
      /* Gobble up records with negative indexes. */
  
      while (logf.header.index<0)
      { log_gobble(&logf,&logg);
      }
  
      /* Skip to start of range, gobble up records at start. */
  
      while (!logf.at_end 
         && (logf.header.index<lindex || logf.header.index%index_mod!=0))
      { log_file_forward(&logf);
      }
  
      if (logf.at_end) continue;
  
      log_gobble(&logf,&logg);
  
      /* Go through all the records in the indicated range. */
  
      for (;;)
      {
        /* See what's here. */
  
        sch = logg.data['m'];
  
        if (sch==0)
        { fprintf(stderr,"No tempering schedule present\n");
          exit(1);
        }
  
        ts  = logg.data['b']!=0 && logg.index['b']==logg.last_index
               ? logg.data['b'] : 0;
        it  = logg.data['i']!=0 && logg.index['i']==logg.last_index
               ? logg.data['i'] : 0;
  
        /* Look at data for this index, if anything is happening. */
  
        if (ts!=0 || it!=0)
        {
          if (ts==0)
          { fprintf(stderr,"No tempering state present (%s %d)\n",
               logf.file_name, logg.last_index);
            exit(1);
          }
  
          if (it==0)
          { fprintf(stderr,"No record describing iteration present (%s %d)\n",
               logf.file_name, logg.last_index);
            exit(1);
          }
  
          i = mc_temp_index(sch,ts->inv_temp);
  
          /* Look at data for pass 1. */

          if (pass==1)
          {
            if (n_points[i]==0) 
            { w_mean[i] = it->log_weight;
              logw_mean[i] = it->log_weight;
            }
            else
            { w_mean[i] = addlogs (w_mean[i], it->log_weight);
              logw_mean[i] = logw_mean[i] + it->log_weight;
            }
  
            n_points[i] += 1;
          }

          /* Look at data for pass 2. */

          if (pass==2)
          { 
            ww = exp(it->log_weight-w_mean[i]);
            ww_var[i] += (ww-1)*(ww-1);
            logw_var[i] += (it->log_weight - logw_mean[i])
                             * (it->log_weight - logw_mean[i]);
          }
        }
  
        /* Skip to next desired index, or to end of range. */
    
        while (!logf.at_end && logf.header.index<=hindex 
                 && logf.header.index%index_mod!=0)
        { log_file_forward(&logf);
        }
  
        if (logf.at_end || logf.header.index>hindex)
        { break;
        }
  
        /* Gobble up records for next index. */
  
        log_gobble(&logf,&logg);
      }
  
      log_file_close(&logf);
      first = 0;
    }

    /* Finish up pass 1. */

    if (pass==1)
    { for (i = 0; i<Max_temps; i++)
      { if (n_points[i]>0)
        { w_mean[i] -= log((double)n_points[i]);
          logw_mean[i] /= n_points[i];
        }
        if (sch->sched[i].inv_temp==1) break;
      }
    }

    /* Finish up pass 2. */

    if (pass==2)
    { for (i = 0; i<Max_temps; i++)
      { if (n_points[i]>1)
        { ww_var[i] /= (n_points[i]-1);
          logw_var[i] /= (n_points[i]-1);
        }
        if (sch->sched[i].inv_temp==1) break;
      }
    }
  }
  
  /* Print values for each temperature. */

  for (i = 0; i<Max_temps; i++)
  {
    if (n_points[i]>0)
    { 
      for (v = values; *v; v++)
      { 
        switch (*v)
        { 
          case 'I': 
          { printf(" %4d",i);
            break;
          }
  
          case 'i':
          { printf(" %.6f",sch->sched[i].inv_temp);
            break;
          }
   
          case 'T':
          { printf(" %9.4f",1/sch->sched[i].inv_temp);
            break;
          }
  
          case 'm':
          { printf(" %.6e",
              n_points[i]==0 ? 1 : exp(w_mean[i]));
            break;
          }
  
          case 'M':
          { printf(" %.6e",
              n_points[i]==0 ? 0 : w_mean[i]);
            break;
          }
  
          case 'F':
          { printf(" %.6e",
              n_points[i]==0 ? 0 : -w_mean[i]);
            break;
          }

          case 'v':
          { printf(" %8.3f", n_points[i]>1 ? ww_var[i] : 0);
            break;
          }

          case 'V':
          { printf(" %8.3f", n_points[i]>1 ? logw_var[i] : 0);
            break;
          }

          case 'a':
          { printf(" %9.3f", n_points[i]>1 ? n_points[i]/(1+ww_var[i]) : 0);
            break;
          }

          case 'W':
          { printf(" %9.3f", n_points[i]>1 ? log (1+ww_var[i]) : 0);
            break;
          }
  
          default:
          { fprintf(stderr,"Invalid value specifier: %c\n",*v);
            exit(1);
          }       
        }
      }
  
      printf("\n");
    }

    if (sch->sched[i].inv_temp==1) break;
  }
 
  exit(0);
}
Beispiel #13
0
int main(int argc, char** argv){
    printf("hello world\n");
    
    behaviors_init();
   
    log_file_open("log.txt");

    if (SDL_Init(SDL_INIT_EVERYTHING) == -1){
		printf("%s\n", SDL_GetError());
		return 1;
	}
    printf("sdl init success\n");

    SDL_Renderer *renderer = NULL;
    SDL_Window *window = NULL;
    
	//Setup our window and renderer
	window = SDL_CreateWindow("Stick Fighter", SDL_WINDOWPOS_CENTERED, 
                                               SDL_WINDOWPOS_CENTERED, 
                                               SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
	if (window == NULL){
		printf("%s\n", SDL_GetError());
		return 2;
	}
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	if (renderer == NULL){
		printf("%s\n", SDL_GetError());
		return 3;
	}
    
    SDL_Texture *background1 = NULL;
    SDL_Texture *background2 = NULL;
    SDL_Texture *image = NULL;

    //background = sprite_load_image(renderer, "res/background_rcr.png");
    background1 = sprite_load_image(renderer, "res/background_rcr.png");
    background2 = sprite_load_image(renderer, "res/background_new.png");

    SDL_Texture* backgrounds[2];
    backgrounds[0] = background1;
    backgrounds[1] = background2;
    
    SDL_Rect bg;
    //Query the texture to get its width and height to use
    SDL_QueryTexture(background1, NULL, NULL, &bg.w, &bg.h);
    float bg_width = bg.w;
    float bg_height = bg.h;
    float world_width = bg_width / 10;
    float world_height = bg_height / 10;

    printf("bg_width: %f bg_height: %f world_width: %f world_height: %f\n", bg_width, bg_height, world_width, world_height);

    //printf("bg 1: [%p]\n", backgrounds[0]);
    //printf("bg 2: [%p]\n", backgrounds[1]);
	
    image = sprite_load_image(renderer, "res/output.png");
    //printf("after sprite load image\n");
        
    //load the meta info
    char* filename = "res/animation_meta_info.txt";
    DArray meta_info;
    darray_init(&meta_info);
    sprite_load_meta_file(filename, &meta_info);
    darray_print(&meta_info);

    float interval = 0.0f;
    float start = 0.0f;
    int quit = 0;
    
    float delay = 1000.0f / FPS;
    Sprite* player = sprite_create(PLAYER, WALK, 200, 300, delay, image);
    player->x_speed = 0;
    player->y_speed = 0;
    
    Sprite* enemy = sprite_create(ENEMY, WALK, 0, 300, delay, image);
    enemy->x_speed = 2;
    enemy->y_speed = 2;
    enemy->advance_frame = 1;
    //set white background
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
    
    pixel_to_world(bg_width, bg_height, world_width, world_height,
                   enemy->x, enemy->y, &enemy->location->coords->x, &enemy->location->coords->y);
    
    int dest_x = 0;
    int dest_y = 0;
    
    //TODO:
    //animations for punch and basic kick
    //animation for getting hit
    //animations for walk left and run left
    //2 more ai behaviors
    //health bars above units and health stats
    //dying animation
    //dying logic
    //generate enemies off screen and have them walk on to screen
    //redo tornado kick and spinning back kick so stick figure is same size
    //figure out how to color stick figure without having to make new sprites on sheet...need to make figures white to set modulation
    //add rolling punch move
    //add game state which keeps track of game state
    //add buy moves screen in between levels
    //add a generator which generates guys for 100 levels

    int i = 0;
    while (!quit){
        printf("iteration: %i\n", i);
		
        start = SDL_GetTicks();
        
        quit = player_input(player, &meta_info);
        //printf("quit: %i\n", quit);  
       
        //log_msg(LOG_DEBUG, "after player input: current frame: %i\n", player->current_frame[HIT]);
        
        //pixel location to world location
        pixel_to_world(bg_width, bg_height, world_width, world_height, 
                       player->x, player->y, &player->location->coords->x, &player->location->coords->y);

        //world location to pixel location
        world_to_pixel(bg_width, bg_height, world_width, world_height,
                       enemy->location->coords->x, enemy->location->coords->y, &dest_x, &dest_y);
        
        //boundry check
        boundry_check(SCREEN_WIDTH, SCREEN_HEIGHT, enemy, &meta_info, &dest_x, &dest_y);

        //printf("2nd before moveto = enemy x:%i enemy y:%i dest_x:%i dest_y:%i\n", enemy->x, enemy->y, dest_x, dest_y);
        
        //update enemy sprite by sprite's speed
        moveto_coordinates(enemy, dest_x, dest_y);
        
        //are we at the original world location in pixel coordinates?
        int arrived = within_coordinates(enemy, dest_x, dest_y);
        
        printf("arrived: %i\n", arrived);
        if(arrived == 1) {
            //wander(enemy); 
            // we reached last behavior's destination so do new AI behavior
            wander_stall_attack(player, enemy, &meta_info, 3);
        }

        sprite_update_bounding_box(player, &meta_info);
        sprite_update_bounding_box(enemy, &meta_info);

        //check collision
        int collision = sprite_check_collision(player, enemy);
        
        //printf("collision: %i\n", collision);
        
        if(collision) {
            sprite_handle_collision(player, enemy, &meta_info);
        }
        
        //handle collision
        //if animation is an attack then check frame range that triggers a hit
        //check if attack animation triggers a hi or low hit
        //if sequence of attacks is within delta time then 4th hit triggers a knockdown
        //if attack would make health below 0 then it triggers a knockdown
        
        //handle opposite attack from opposing sprite
        //if is in attack animation then check frame range for a hit

        //update animation frame
        sprite_update(player, &meta_info, renderer);
        sprite_update(enemy, &meta_info, renderer);
        
        //Rendering
		SDL_RenderClear(renderer);
       
        //printf("player->x: %i\n", player->x);
        //printf("moving right: %i\n", player->moving_right);
        //printf("moving left: %i\n", player->moving_left);
        int current_background = 0;
        sprite_draw_background(renderer, backgrounds, 2, &current_background, player->x, player->moving_right, player->moving_left, &player->scroll);
        
        sprite_draw_health_bar(renderer, player, &meta_info, 100); 
        sprite_draw_health_bar(renderer, enemy, &meta_info, 100); 
        
        //draw sprite
        sprite_render_frame(SCREEN_WIDTH, SCREEN_HEIGHT, player, &meta_info, renderer, 0);
        sprite_render_frame(bg_width, bg_height, enemy, &meta_info, renderer, 0);

		//Update the screen
		SDL_RenderPresent(renderer);
         
        interval = SDL_GetTicks() - start;
        
        if(interval > 0) {
            //float fps = 1.0f / (interval / 1000.0f);
            //printf("%f fps\n", fps);
        }
        i++;
	}
    
    //SDL_Delay(4000);

	//Destroy the various items
    sprite_destroy(player);
    sprite_destroy(enemy);
    darray_destroy(&meta_info);
	SDL_DestroyTexture(backgrounds[0]);
	SDL_DestroyTexture(backgrounds[1]);
	SDL_DestroyTexture(image);
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
    
    printf("after destory text rend win\n");

	SDL_Quit();
    
    log_file_close();

    return 0;
}