<commit_before>/////////////////////////////////////////////////////////////////////////
// $Id$
/////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2001  MandrakeSoft S.A.
//
//    MandrakeSoft S.A.
//    43, rue d'Aboukir
//    75002 Paris - France
//    http://www.linux-mandrake.com/
//    http://www.mandrakesoft.com/
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
/////////////////////////////////////////////////////////////////////////

#include "bochs.h"
#include "cpu/cpu.h"

#if BX_DEBUGGER
#if !((BX_HAVE_HASH_MAP || BX_HAVE_HASH_MAP_H) && (BX_HAVE_SET || BX_HAVE_SET_H))

static char *BX_HAVE_HASH_MAP_ERR = "context not implemented because BX_HAVE_HASH_MAP=0\n";

char* bx_dbg_symbolic_address(Bit32u context, Bit32u eip, Bit32u base)
{
  static bx_bool first = true;
  if (first) {
    dbg_printf(BX_HAVE_HASH_MAP_ERR);
    first = false;
  }
  return "unk. ctxt";
}

char* bx_dbg_symbolic_address_16bit(Bit32u eip, Bit32u cs)
{
  // just prints an error anyway
  return bx_dbg_symbolic_address (0,0,0);
}

void bx_dbg_symbol_command(char* filename, bx_bool global, Bit32u offset)
{
  dbg_printf(BX_HAVE_HASH_MAP_ERR);
}

void bx_dbg_info_symbols_command(char *Symbol)
{
  dbg_printf(BX_HAVE_HASH_MAP_ERR);
}

int bx_dbg_lbreakpoint_symbol_command(char *Symbol)
{
  dbg_printf(BX_HAVE_HASH_MAP_ERR);
  return -1;
}

Bit32u bx_dbg_get_symbol_value(char *Symbol)
{
  return 0;
}

char* bx_dbg_disasm_symbolic_address(Bit32u eip, Bit32u base)
{
  return 0;
}

#else   /* if BX_HAVE_HASH_MAP == 1 */

/* Haven't figured out how to port this code to OSF1 cxx compiler.
   Until a more portable solution is found, at least make it easy
   to disable the template code:  just set BX_HAVE_HASH_MAP=0
   in config.h */
#if BX_HAVE_HASH_MAP
#include <hash_map>
#elif BX_HAVE_HASH_MAP_H
#include <hash_map.h>
#endif

#if BX_HAVE_SET
#include <set>
#elif BX_HAVE_SET_H
#include <set.h>
#endif

using namespace std;

struct symbol_entry_t
{
  symbol_entry_t (Bit32u _start = 0, char* _name = 0)
  {
    start = _start;
    name = _name;
  }

  char* name;
  Bit32u start;
};

struct lt_symbol_entry_t
{
  bool operator()(const symbol_entry_t* s1, const symbol_entry_t* s2) const
  {
    return s1->start < s2->start;
  }
};

struct lt_rsymbol_entry_t
{
  bool operator()(const symbol_entry_t* s1, const symbol_entry_t* s2) const
  {
    return strcoll(s1->name, s2->name) < 0;
  }
};

struct context_t
{
  context_t (Bit32u);
  ~context_t();
  static context_t* get_context(Bit32u);
  symbol_entry_t* get_symbol_entry(Bit32u);
  symbol_entry_t* get_symbol_entry(const char *Symbol) const;
  void add_symbol(symbol_entry_t*);
  const set<symbol_entry_t*,lt_symbol_entry_t>* get_all_symbols() const {return syms;}
  const set<symbol_entry_t*,lt_rsymbol_entry_t>* get_all_rsymbols() const {return rsyms;}
private:
  static hash_map<int,context_t*>* map;
  // Forvard references (find name by address)
  set<symbol_entry_t*,lt_symbol_entry_t>* syms;
  // Reverse references (find address by name)
  set<symbol_entry_t*,lt_rsymbol_entry_t>* rsyms;
  Bit32u id;
};

hash_map<int,context_t*>* context_t::map = new hash_map<int,context_t*>;

context_t::context_t (Bit32u _id)
{
  id = _id;
  syms = new set<symbol_entry_t*, lt_symbol_entry_t>;
  rsyms = new set<symbol_entry_t*, lt_rsymbol_entry_t>;
  (*map)[id] = this;
}

context_t::~context_t()
{
 set<symbol_entry_t*>::iterator iter;
 if(syms) {
  for(iter=syms->begin();iter!=syms->end();++iter)
   if(*iter)
    delete *iter;
 }

 if(rsyms) {
  for(iter=rsyms->begin();iter!=rsyms->end();++iter)
   if(*iter)
    delete *iter;
 }
}

context_t* context_t::get_context(Bit32u i)
{
  return (*map)[i];
}

symbol_entry_t* context_t::get_symbol_entry(Bit32u ip)
{
  symbol_entry_t probe;
  probe.start = ip;
  // find the first symbol whose address is greater than ip.
  if (syms->empty ()) return 0;
  set<symbol_entry_t*>::iterator iter = syms->upper_bound(&probe);

  if (iter == syms->end()) { // No symbol found
    return 0;
  }

  return *(--iter);
}

symbol_entry_t* context_t::get_symbol_entry(const char *Symbol) const
{
  symbol_entry_t probe;
  probe.name=(char *)Symbol;

  if (rsyms->empty ())
   return 0;

  set<symbol_entry_t*>::const_iterator iter;
  iter=rsyms->find(&probe);
  if(iter==rsyms->end()) // No symbol found
   return 0;
  return *iter;
}

void context_t::add_symbol(symbol_entry_t* sym)
{
  syms->insert(sym);
  rsyms->insert(sym);
}

Bit32u bx_dbg_get_symbol_value(char *Symbol)
{
 context_t* cntx = context_t::get_context(0);
 if(!cntx) // Context not found
  return 0;

 if (Symbol[0]=='\"') Symbol++;
 int len = strlen(Symbol);
 if (Symbol[len - 1] == '\"') Symbol[len - 1] = '\0';

 symbol_entry_t* sym=cntx->get_symbol_entry(Symbol);
 if(!sym) // Symbol not found
  return 0;

 return sym->start;
}

char* bx_dbg_symbolic_address(Bit32u context, Bit32u eip, Bit32u base)
{
  static char buf[80];
#if 0
  // bbd: I don't see why we shouldn't allow symbol lookups on
  // segments with a nonzero base.  I need to trace user
  // processes in Linux, which have a base of 0xc0000000.
  if (base != 0) {
    snprintf (buf, 80, "non-zero base");
    return buf;
  }
#endif
  // Look up this context
  context_t* cntx = context_t::get_context(context);
  if (!cntx) {
    // Try global context
    cntx = context_t::get_context(0);
    if (!cntx) {
      snprintf (buf, 80, "unk. ctxt");
      return buf;
    }
  }
  // full linear address not only eip (for nonzero based segments)
  symbol_entry_t* entr = cntx->get_symbol_entry(base+eip);
  if (!entr) {
    snprintf (buf, 80, "no symbol");
    return buf;
  }
  snprintf (buf, 80, "%s+%x", entr->name, (base+eip) - entr->start);
  return buf;
}

char* bx_dbg_disasm_symbolic_address(Bit32u eip, Bit32u base)
{
  static char buf[80];

  // Try global context
  context_t* cntx = context_t::get_context(0);
  if (!cntx) {
    return 0;
  }

  // full linear address not only eip (for nonzero based segments)
  symbol_entry_t* entr = cntx->get_symbol_entry(base+eip);
  if (!entr) {
    return 0;
  }
  snprintf (buf, 80, "%s+%x", entr->name, (base+eip) - entr->start);
  return buf;
}

char* bx_dbg_symbolic_address_16bit(Bit32u eip, Bit32u cs)
{
  // in 16-bit code, the segment selector and offset are combined into a
  // 20-bit linear address = (segment selector<<4) + offset.
  eip &= 0xffff;
  cs &= 0xffff;
  return bx_dbg_symbolic_address (0, eip+(cs<<4), 0);
}

void bx_dbg_symbol_command(char* filename, bx_bool global, Bit32u offset)
{
  if (filename[0] == '"')
    filename++;
  int len = strlen(filename);
  if (filename[len - 1] == '"')
    filename[len - 1] = '\0';

  // Install symbols in correct context (page table)
  // The file format should be
  // address symbol (example '00002afe _StartLoseNT')

  context_t* cntx = (global) ? context_t::get_context(0)
      : context_t::get_context((BX_CPU(dbg_cpu)->cr3) >> 12);

  if (!cntx) {
    cntx = (global) ? new context_t(0)
       : new context_t((BX_CPU(dbg_cpu)->cr3) >> 12);
  }

  FILE* fp = fopen(filename, "rt"); // 't' is need for win32, unixes simply ignore it
  if (!fp) {
    dbg_printf ("Could not open symbol file '%s'\n", filename);
    return;
  }
  char buf[200];
  while (fgets(buf, 200, fp)) {
    // Parse
    char* sym_name = buf;

    for (int i = 0; i < 200 && buf[i]; i++) {
      if (buf[i] == ' ') {
        buf[i] = '\0';
        sym_name = buf + i + 1;
        break;
      }
    }
    if (sym_name == buf) {
      dbg_printf ("Syntax error '%s'\n", buf);
      break;
    }
    Bit32u addr = strtoul(buf, 0, 16);
    if (sym_name[strlen(sym_name)-1] == '\n')
      sym_name[strlen(sym_name)-1] = '\0';
    symbol_entry_t* sym = new symbol_entry_t(addr + offset, strdup(sym_name));
    cntx->add_symbol(sym);
  }
}

// chack if s1 is prefix of s2
static bool bx_dbg_strprefix(const char *s1, const char *s2)
{
  if(!s1 || !s2)
   return false;

  size_t len=strlen(s1);

  if(len>strlen(s2))
   return false;
  return strncmp(s1, s2, len)==0;
}

void bx_dbg_info_symbols_command(char *Symbol)
{
  context_t* cntx = context_t::get_context(0);

  if(!cntx) {
   dbg_printf ("Global context not available\n");
   return;
  }

  if(Symbol) {
   const set<symbol_entry_t*,lt_rsymbol_entry_t>* rsyms;

   rsyms=cntx->get_all_rsymbols();
   if (rsyms->empty ()) {
    dbg_printf ("Symbols not loaded\n");
    return;
   }
   // remove leading and trailing quotas
   if (Symbol[0]=='\"') Symbol++;
   int len = strlen(Symbol);
   if (Symbol[len - 1] == '\"') Symbol[len - 1] = '\0';

   symbol_entry_t probe;
   probe.name=Symbol;
   set<symbol_entry_t*>::const_iterator iter;
   iter=rsyms->lower_bound(&probe);

   if(iter==rsyms->end() || !bx_dbg_strprefix(Symbol, (*iter)->name))
    dbg_printf ("No symbols found\n");
   else
   for(;iter!=rsyms->end() && bx_dbg_strprefix(Symbol, (*iter)->name);++iter) {
    dbg_printf ("%08x: %s\n", (*iter)->start, (*iter)->name);
   }
  }
  else {
   const set<symbol_entry_t*,lt_symbol_entry_t>* syms;

   syms=cntx->get_all_symbols();
   if (syms->empty ()) {
    dbg_printf ("Symbols not loaded\n");
    return;
   }

   set<symbol_entry_t*>::const_iterator iter;
   for(iter = syms->begin();iter!=syms->end();++iter) {
    dbg_printf ("%08x: %s\n", (*iter)->start, (*iter)->name);
   }
  }
}

int bx_dbg_lbreakpoint_symbol_command(char *Symbol)
{
 context_t* cntx = context_t::get_context(0);
 if(!cntx) {
  dbg_printf ("Global context not available\n");
  return -1;
 }
 if (Symbol[0]=='\"') Symbol++;
 int len = strlen(Symbol);
 if (Symbol[len - 1] == '\"') Symbol[len - 1] = '\0';

 const symbol_entry_t* sym=cntx->get_symbol_entry(Symbol);
 if(sym)
  return bx_dbg_lbreakpoint_command(bkRegular, sym->start);
 dbg_printf ("Symbol not found\n");
 return -1;
}
#endif
#endif
<commit_msg>Another #SF patch merge another patch to fix compilation with vc8 (or, more precisely with Dimkunware STL). It corrects the usage of std::set iterators.<commit_after>/////////////////////////////////////////////////////////////////////////
// $Id$
/////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2001  MandrakeSoft S.A.
//
//    MandrakeSoft S.A.
//    43, rue d'Aboukir
//    75002 Paris - France
//    http://www.linux-mandrake.com/
//    http://www.mandrakesoft.com/
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
/////////////////////////////////////////////////////////////////////////

#include "bochs.h"
#include "cpu/cpu.h"

#if BX_DEBUGGER
#if !((BX_HAVE_HASH_MAP || BX_HAVE_HASH_MAP_H) && (BX_HAVE_SET || BX_HAVE_SET_H))

static char *BX_HAVE_HASH_MAP_ERR = "context not implemented because BX_HAVE_HASH_MAP=0\n";

char* bx_dbg_symbolic_address(Bit32u context, Bit32u eip, Bit32u base)
{
  static bx_bool first = true;
  if (first) {
    dbg_printf(BX_HAVE_HASH_MAP_ERR);
    first = false;
  }
  return "unk. ctxt";
}

char* bx_dbg_symbolic_address_16bit(Bit32u eip, Bit32u cs)
{
  // just prints an error anyway
  return bx_dbg_symbolic_address (0,0,0);
}

void bx_dbg_symbol_command(char* filename, bx_bool global, Bit32u offset)
{
  dbg_printf(BX_HAVE_HASH_MAP_ERR);
}

void bx_dbg_info_symbols_command(char *Symbol)
{
  dbg_printf(BX_HAVE_HASH_MAP_ERR);
}

int bx_dbg_lbreakpoint_symbol_command(char *Symbol)
{
  dbg_printf(BX_HAVE_HASH_MAP_ERR);
  return -1;
}

Bit32u bx_dbg_get_symbol_value(char *Symbol)
{
  return 0;
}

char* bx_dbg_disasm_symbolic_address(Bit32u eip, Bit32u base)
{
  return 0;
}

#else   /* if BX_HAVE_HASH_MAP == 1 */

/* Haven't figured out how to port this code to OSF1 cxx compiler.
   Until a more portable solution is found, at least make it easy
   to disable the template code:  just set BX_HAVE_HASH_MAP=0
   in config.h */
#if BX_HAVE_HASH_MAP
#include <hash_map>
#elif BX_HAVE_HASH_MAP_H
#include <hash_map.h>
#endif

#if BX_HAVE_SET
#include <set>
#elif BX_HAVE_SET_H
#include <set.h>
#endif

using namespace std;

struct symbol_entry_t
{
  symbol_entry_t (Bit32u _start = 0, char* _name = 0)
  {
    start = _start;
    name = _name;
  }

  char* name;
  Bit32u start;
};

struct lt_symbol_entry_t
{
  bool operator()(const symbol_entry_t* s1, const symbol_entry_t* s2) const
  {
    return s1->start < s2->start;
  }
};

struct lt_rsymbol_entry_t
{
  bool operator()(const symbol_entry_t* s1, const symbol_entry_t* s2) const
  {
    return strcoll(s1->name, s2->name) < 0;
  }
};

struct context_t
{
  typedef set<symbol_entry_t*,lt_symbol_entry_t>  sym_set_t;
  typedef set<symbol_entry_t*,lt_rsymbol_entry_t> rsym_set_t;

  context_t (Bit32u);
 ~context_t();

  static context_t* get_context(Bit32u);
  symbol_entry_t* get_symbol_entry(Bit32u);
  symbol_entry_t* get_symbol_entry(const char *Symbol) const;
  void add_symbol(symbol_entry_t*);
  const sym_set_t* get_all_symbols() const {return syms;}
  const rsym_set_t* get_all_rsymbols() const {return rsyms;}

private:
  static hash_map<int,context_t*>* map;
  // Forvard references (find name by address)
  sym_set_t* syms;
  // Reverse references (find address by name)
  rsym_set_t* rsyms;
  Bit32u id;
};

hash_map<int,context_t*>* context_t::map = new hash_map<int,context_t*>;

context_t::context_t (Bit32u _id)
{
  id = _id;
  syms = new sym_set_t;
  rsyms = new rsym_set_t;
  (*map)[id] = this;
}

context_t::~context_t()
{
  if(syms) {
    sym_set_t::iterator iter;
    for(iter=syms->begin();iter!=syms->end();++iter)
    if(*iter)
      delete *iter;
  }

  if(rsyms) {
    rsym_set_t::iterator iter;
    for(iter=rsyms->begin();iter!=rsyms->end();++iter)
      if(*iter)
        delete *iter;
  }
}

context_t* context_t::get_context(Bit32u i)
{
  return (*map)[i];
}

symbol_entry_t* context_t::get_symbol_entry(Bit32u ip)
{
  symbol_entry_t probe;
  probe.start = ip;
  // find the first symbol whose address is greater than ip.
  if (syms->empty ()) return 0;
  sym_set_t::iterator iter = syms->upper_bound(&probe);

  if (iter == syms->end()) { // No symbol found
    return 0;
  }

  return *(--iter);
}

symbol_entry_t* context_t::get_symbol_entry(const char *Symbol) const
{
  symbol_entry_t probe;
  probe.name=(char *)Symbol;

  if (rsyms->empty())
    return 0;

  rsym_set_t::const_iterator iter;
  iter=rsyms->find(&probe);
  if(iter==rsyms->end()) // No symbol found
    return 0;
  return *iter;
}

void context_t::add_symbol(symbol_entry_t* sym)
{
  syms->insert(sym);
  rsyms->insert(sym);
}

Bit32u bx_dbg_get_symbol_value(char *Symbol)
{
  context_t* cntx = context_t::get_context(0);
  if(!cntx) // Context not found
    return 0;

  if (Symbol[0]=='\"') Symbol++;
  int len = strlen(Symbol);
  if (Symbol[len - 1] == '\"') Symbol[len - 1] = '\0';

  symbol_entry_t* sym=cntx->get_symbol_entry(Symbol);
  if(!sym) // Symbol not found
    return 0;

  return sym->start;
}

char* bx_dbg_symbolic_address(Bit32u context, Bit32u eip, Bit32u base)
{
  static char buf[80];
#if 0
  // bbd: I don't see why we shouldn't allow symbol lookups on
  // segments with a nonzero base.  I need to trace user
  // processes in Linux, which have a base of 0xc0000000.
  if (base != 0) {
    snprintf (buf, 80, "non-zero base");
    return buf;
  }
#endif
  // Look up this context
  context_t* cntx = context_t::get_context(context);
  if (!cntx) {
    // Try global context
    cntx = context_t::get_context(0);
    if (!cntx) {
      snprintf (buf, 80, "unk. ctxt");
      return buf;
    }
  }
  // full linear address not only eip (for nonzero based segments)
  symbol_entry_t* entr = cntx->get_symbol_entry(base+eip);
  if (!entr) {
    snprintf (buf, 80, "no symbol");
    return buf;
  }
  snprintf (buf, 80, "%s+%x", entr->name, (base+eip) - entr->start);
  return buf;
}

char* bx_dbg_disasm_symbolic_address(Bit32u eip, Bit32u base)
{
  static char buf[80];

  // Try global context
  context_t* cntx = context_t::get_context(0);
  if (!cntx) {
    return 0;
  }

  // full linear address not only eip (for nonzero based segments)
  symbol_entry_t* entr = cntx->get_symbol_entry(base+eip);
  if (!entr) {
    return 0;
  }
  snprintf (buf, 80, "%s+%x", entr->name, (base+eip) - entr->start);
  return buf;
}

char* bx_dbg_symbolic_address_16bit(Bit32u eip, Bit32u cs)
{
  // in 16-bit code, the segment selector and offset are combined into a
  // 20-bit linear address = (segment selector<<4) + offset.
  eip &= 0xffff;
  cs &= 0xffff;
  return bx_dbg_symbolic_address (0, eip+(cs<<4), 0);
}

void bx_dbg_symbol_command(char* filename, bx_bool global, Bit32u offset)
{
  if (filename[0] == '"')
    filename++;
  int len = strlen(filename);
  if (filename[len - 1] == '"')
    filename[len - 1] = '\0';

  // Install symbols in correct context (page table)
  // The file format should be
  // address symbol (example '00002afe _StartLoseNT')

  context_t* cntx = (global) ? context_t::get_context(0)
      : context_t::get_context((BX_CPU(dbg_cpu)->cr3) >> 12);

  if (!cntx) {
    cntx = (global) ? new context_t(0)
       : new context_t((BX_CPU(dbg_cpu)->cr3) >> 12);
  }

  FILE* fp = fopen(filename, "rt"); // 't' is need for win32, unixes simply ignore it
  if (!fp) {
    dbg_printf ("Could not open symbol file '%s'\n", filename);
    return;
  }
  char buf[200];
  while (fgets(buf, 200, fp)) {
    // Parse
    char* sym_name = buf;

    for (int i = 0; i < 200 && buf[i]; i++) {
      if (buf[i] == ' ') {
        buf[i] = '\0';
        sym_name = buf + i + 1;
        break;
      }
    }
    if (sym_name == buf) {
      dbg_printf ("Syntax error '%s'\n", buf);
      break;
    }
    Bit32u addr = strtoul(buf, 0, 16);
    if (sym_name[strlen(sym_name)-1] == '\n')
      sym_name[strlen(sym_name)-1] = '\0';
    symbol_entry_t* sym = new symbol_entry_t(addr + offset, strdup(sym_name));
    cntx->add_symbol(sym);
  }
}

// chack if s1 is prefix of s2
static bool bx_dbg_strprefix(const char *s1, const char *s2)
{
  if(!s1 || !s2)
    return false;

  size_t len=strlen(s1);

  if(len>strlen(s2))
    return false;
  return strncmp(s1, s2, len)==0;
}

void bx_dbg_info_symbols_command(char *Symbol)
{
  context_t* cntx = context_t::get_context(0);

  if(!cntx) {
    dbg_printf ("Global context not available\n");
    return;
  }

  if(Symbol) {
    const context_t::rsym_set_t* rsyms;

    rsyms=cntx->get_all_rsymbols();
    if (rsyms->empty ()) {
      dbg_printf ("Symbols not loaded\n");
      return;
    }
    // remove leading and trailing quotas
    if (Symbol[0]=='\"') Symbol++;
    int len = strlen(Symbol);
    if (Symbol[len - 1] == '\"') Symbol[len - 1] = '\0';

    symbol_entry_t probe;
    probe.name=Symbol;
    context_t::rsym_set_t::const_iterator iter;
    iter=rsyms->lower_bound(&probe);

    if(iter==rsyms->end() || !bx_dbg_strprefix(Symbol, (*iter)->name))
      dbg_printf ("No symbols found\n");
    else {
      for(;iter!=rsyms->end() && bx_dbg_strprefix(Symbol, (*iter)->name);++iter) {
        dbg_printf ("%08x: %s\n", (*iter)->start, (*iter)->name);
      }
    }
  }
  else {
    const context_t::sym_set_t* syms;

    syms=cntx->get_all_symbols();
    if (syms->empty ()) {
      dbg_printf ("Symbols not loaded\n");
      return;
    }

    context_t::sym_set_t::const_iterator iter;
    for(iter = syms->begin();iter!=syms->end();++iter) {
      dbg_printf ("%08x: %s\n", (*iter)->start, (*iter)->name);
    }
  }
}

int bx_dbg_lbreakpoint_symbol_command(char *Symbol)
{
  context_t* cntx = context_t::get_context(0);
  if(!cntx) {
    dbg_printf ("Global context not available\n");
    return -1;
  }
  if (Symbol[0]=='\"') Symbol++;
  int len = strlen(Symbol);
  if (Symbol[len - 1] == '\"') Symbol[len - 1] = '\0';

  const symbol_entry_t* sym=cntx->get_symbol_entry(Symbol);
  if(sym)
    return bx_dbg_lbreakpoint_command(bkRegular, sym->start);
  dbg_printf ("Symbol not found\n");
  return -1;
}

#endif
#endif
<|endoftext|>


<commit_before>#include "Doing.h"
#include <Windows.h>
#include <string>
#include "Win32Util.h"
#include <oleacc.h>
#include <iostream>
#include <atlbase.h>
#include <UIAutomationClient.h>
#include "StringUtil.h"
#include "Activity.h"
#include <ctime>
#include <thread>
#include <chrono>
#include "ActivityKey.h"
#include "SimpleLogger.h"
Doing::Doing(ConcurrentQueue<Activity>& ptr_map) : _job_queue(ptr_map)
{
    ::CoInitialize(NULL);
    _machine_name = Win32Util<std::wstring>::GetMachineName();

}
void Doing::StartReportActivities() const
{
}
void Doing::Sample()
{
    ::CoInitialize(NULL);
    ActivityKey cur_activity_key;
    bool prev_is_active = true;
    while (true)
    {
        ActivityKey next_activity_key;
        std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(
            std::chrono::system_clock::now().time_since_epoch());
        if (_last_time != 0)
        {
            HWND hwnd = GetForegroundWindow();
            DWORD pid = 0;
            if (hwnd)
            {
                if (GetWindowThreadProcessId(hwnd, &pid))
                {
                    if (!(pid == GetCurrentProcessId()))
                    {
                        next_activity_key.proc_name = Win32Util<std::wstring>::GetProcName(pid);;
                        //1. get key of the activity
                        std::wstring url;
                        next_activity_key.window_text = Win32Util<std::wstring>::GetWindowTitleByHandle(hwnd);
                        if (next_activity_key.proc_name.compare(L"chrome.exe") == 0)
                        {
                            next_activity_key.url = ReportUrlByHandle(hwnd);
                        }

                        //creates key internally
                        //as of this step, the internal data members should all be set
                        next_activity_key.GenerateKey();

                        if (cur_activity_key.IsEmpty()) // first time
                        {
                            cur_activity_key = next_activity_key; //copy constructor
                        }
                        else
                        {
                            _acitive_duration += ms.count() - _last_time;//if this is still the same activity, increase the duration
                            bool cur_is_active = prev_is_active;
                            try
                            {
                                cur_is_active = (GetTickCount() - GetLastInputTime()) < 120000;
                                //debug purpose
                                if (cur_is_active != prev_is_active)
                                {
                                    wprintf(L"activity %s activeness changed from %d to %d\n",next_activity_key.GetKey().c_str(), prev_is_active,cur_is_active);
                                    //SimpleLogger::GetInstance().WriteLogMessage(LOGGER_LEVEL::INFO_LVL,);
                                }
                            }
                            catch (const std::exception& e)
                            {
                                std::string s = e.what();
                                //ya lazy ass. handle i18n multibyte to wide char when it breaks :P
                                SimpleLogger::GetInstance().WriteLogMessage(LOGGER_LEVEL::ERROR_LVL, std::wstring(s.begin(), s.end()));
                            }

                            //When Activity itself has changed
                            if (next_activity_key != cur_activity_key)
                            {
                                Activity activity(_acitive_duration,
                                    ms.count(),
                                    _machine_name,
                                    cur_activity_key,
                                    cur_is_active);
                                _job_queue.Push(activity);
                                cur_activity_key = next_activity_key;

                                //previous recording of this activity is done
                                //now reset all bookkeeping local variables
                                _acitive_duration = 0;
                                prev_is_active = true;
                            }
                            else if (cur_is_active != prev_is_active)
                            {
                                Activity activity(_acitive_duration,
                                    ms.count(),
                                    _machine_name,
                                    cur_activity_key,
                                    //cur_is_active actually describes current activity's activeness
                                    //prev_is_active describes the *already queued*, last acitivity
                                    cur_is_active);
                                _job_queue.Push(activity);
                                prev_is_active = cur_is_active;
                                cur_activity_key = next_activity_key;
                                //previous recording of this activity is done
                                //now reset all bookkeeping local variables
                                _acitive_duration = 0;
                            }

                        }
                    }
                }
                else
                {
                    wprintf(L"GetWindowThreadProcessId failed errcode = %d", GetLastError());
                }
            }
        }

        //always bumps this, we are increasing this in a 500 ms
        {
            _last_time = ms.count();
        }

        Sleep(500);
    }
}
void Doing::Monitor()
{
    std::thread t_sampling(&Doing::Sample,this);
    t_sampling.detach();
}
Doing::~Doing()
{
    ::CoUninitialize();
}
std::wstring Doing::ReportUrlByHandle(const HWND hwnd)
{
    CComPtr<IUIAutomation> pAutomation = NULL;
    HRESULT hr = pAutomation.CoCreateInstance(CLSID_CUIAutomation, NULL, CLSCTX_INPROC_SERVER);
    CComPtr<IUIAutomationElement> pElement = NULL;
    std::wstring val_std_str;
    if (SUCCEEDED(hr))
    {
        hr = pAutomation->ElementFromHandle(hwnd, &pElement);
        if (pElement)
        {
            CComPtr<IUIAutomationCondition> pCondition = NULL;
            IUIAutomationElement* pFound = NULL;  // don't use smart pointer because we want non RAII control over this pointer
            BSTR urlControlName = SysAllocString(L"Address and search bar");
            if (urlControlName)
            {
                VARIANT varProp;
                varProp.vt = VT_BSTR;
                varProp.bstrVal = urlControlName;
                pAutomation->CreatePropertyCondition(UIA_NamePropertyId, varProp, &pCondition);
                if (pCondition)
                {
                    pElement->FindFirst(TreeScope_Descendants, pCondition, &pFound);
                    if (pFound)
                    {
                        BSTR val = NULL;
                        BSTR name = NULL;
                        CComPtr<IUIAutomationValuePattern> ptr_val_pattern = NULL;
                        pFound->get_CurrentName(&name);
                        pFound->GetCurrentPatternAs(UIA_ValuePatternId, __uuidof(IUIAutomationValuePattern), (void**)&ptr_val_pattern);
                        ptr_val_pattern->get_CurrentValue(&val);
                        val_std_str = StringUtilBase::BSTR2StdString(val);
                        //std::wstring name_std_str = StringUtilBase<std::wstring>::BSTR2StdString(name);
                        pFound->Release();
                    }
                }

                SysFreeString(urlControlName);
            }
        }
    }
    return val_std_str;
}
/*
desc:
    Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 day
    since last user input

ret:
    last user input time in milis

exception:
    throw std::exception when GetLastInputInfo fails.
    MSDN does not mention what ERROR CODE to expect when failing though.

*/
unsigned long long Doing::GetLastInputTime()
{
#ifdef _WIN32
    LASTINPUTINFO lii;
    //The size of the structure, in bytes. This member must be set to sizeof(LASTINPUTINFO).
    //It's a common way for versioning in Windows API.
    lii.cbSize = sizeof(LASTINPUTINFO);
    if (!GetLastInputInfo(&lii))
    {
        throw std::exception("GetLastInputInfo failed!");
    }
    return lii.dwTime;
#else
    return 0;
#endif
}
//end of the file<commit_msg>time_stamp now is the start time instead of the endtime<commit_after>#include "Doing.h"
#include <Windows.h>
#include <string>
#include "Win32Util.h"
#include <oleacc.h>
#include <iostream>
#include <atlbase.h>
#include <UIAutomationClient.h>
#include "StringUtil.h"
#include "Activity.h"
#include <ctime>
#include <thread>
#include <chrono>
#include "ActivityKey.h"
#include "SimpleLogger.h"
Doing::Doing(ConcurrentQueue<Activity>& ptr_map) : _job_queue(ptr_map)
{
    ::CoInitialize(NULL);
    _machine_name = Win32Util<std::wstring>::GetMachineName();

}
void Doing::StartReportActivities() const
{
}
void Doing::Sample()
{
    ::CoInitialize(NULL);
    ActivityKey cur_activity_key;
    bool prev_is_active = true;
    while (true)
    {
        ActivityKey next_activity_key;
        std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(
            std::chrono::system_clock::now().time_since_epoch());
        unsigned long long start_time = 0; // 0 can be used to verify if start_time has been inited or not
        if (_last_time != 0)
        {
            HWND hwnd = GetForegroundWindow();
            DWORD pid = 0;
            if (hwnd)
            {
                if (GetWindowThreadProcessId(hwnd, &pid))
                {
                    if (!(pid == GetCurrentProcessId()))
                    {
                        next_activity_key.proc_name = Win32Util<std::wstring>::GetProcName(pid);;
                        //1. get key of the activity
                        std::wstring url;
                        next_activity_key.window_text = Win32Util<std::wstring>::GetWindowTitleByHandle(hwnd);
                        if (next_activity_key.proc_name.compare(L"chrome.exe") == 0)
                        {
                            next_activity_key.url = ReportUrlByHandle(hwnd);
                        }

                        //creates key internally
                        //as of this step, the internal data members should all be set
                        next_activity_key.GenerateKey();

                        if (cur_activity_key.IsEmpty()) // first time
                        {
                            cur_activity_key = next_activity_key; //copy constructor
                            start_time = ms.count();//init start time
                        }
                        else
                        {
                            _acitive_duration += ms.count() - _last_time;//if this is still the same activity, increase the duration
                            bool cur_is_active = prev_is_active;
                            try
                            {
                                cur_is_active = (GetTickCount() - GetLastInputTime()) < 120000;
                                //debug purpose
                                if (cur_is_active != prev_is_active)
                                {
                                    wprintf(L"activity %s activeness changed from %d to %d\n",next_activity_key.GetKey().c_str(), prev_is_active,cur_is_active);
                                    //SimpleLogger::GetInstance().WriteLogMessage(LOGGER_LEVEL::INFO_LVL,);
                                }
                            }
                            catch (const std::exception& e)
                            {
                                std::string s = e.what();
                                //ya lazy ass. handle i18n multibyte to wide char when it breaks :P
                                SimpleLogger::GetInstance().WriteLogMessage(LOGGER_LEVEL::ERROR_LVL, std::wstring(s.begin(), s.end()));
                            }

                            //When Activity itself has changed
                            if (next_activity_key != cur_activity_key)
                            {
                                Activity activity(_acitive_duration,
                                    start_time,
                                    _machine_name,
                                    cur_activity_key,
                                    cur_is_active);
                                _job_queue.Push(activity);
                                cur_activity_key = next_activity_key;

                                //previous recording of this activity is done
                                //now reset all bookkeeping local variables
                                _acitive_duration = 0;
                                prev_is_active = true;
                                start_time = ms.count(); //reset start time
                            }
                            else if (cur_is_active != prev_is_active)
                            {
                                Activity activity(_acitive_duration,
                                    start_time,
                                    _machine_name,
                                    cur_activity_key,
                                    //cur_is_active actually describes current activity's activeness
                                    //prev_is_active describes the *already queued*, last acitivity
                                    cur_is_active);
                                _job_queue.Push(activity);
                                prev_is_active = cur_is_active;
                                cur_activity_key = next_activity_key;
                                //previous recording of this activity is done
                                //now reset all bookkeeping local variables
                                _acitive_duration = 0;
                                start_time = ms.count(); //reset start time
                            }

                        }
                    }
                }
                else
                {
                    wprintf(L"GetWindowThreadProcessId failed errcode = %d", GetLastError());
                }
            }
        }

        //always bumps this, we are increasing this in a 500 ms
        {
            _last_time = ms.count();
        }

        Sleep(500);
    }
}
void Doing::Monitor()
{
    std::thread t_sampling(&Doing::Sample,this);
    t_sampling.detach();
}
Doing::~Doing()
{
    ::CoUninitialize();
}
std::wstring Doing::ReportUrlByHandle(const HWND hwnd)
{
    CComPtr<IUIAutomation> pAutomation = NULL;
    HRESULT hr = pAutomation.CoCreateInstance(CLSID_CUIAutomation, NULL, CLSCTX_INPROC_SERVER);
    CComPtr<IUIAutomationElement> pElement = NULL;
    std::wstring val_std_str;
    if (SUCCEEDED(hr))
    {
        hr = pAutomation->ElementFromHandle(hwnd, &pElement);
        if (pElement)
        {
            CComPtr<IUIAutomationCondition> pCondition = NULL;
            IUIAutomationElement* pFound = NULL;  // don't use smart pointer because we want non RAII control over this pointer
            BSTR urlControlName = SysAllocString(L"Address and search bar");
            if (urlControlName)
            {
                VARIANT varProp;
                varProp.vt = VT_BSTR;
                varProp.bstrVal = urlControlName;
                pAutomation->CreatePropertyCondition(UIA_NamePropertyId, varProp, &pCondition);
                if (pCondition)
                {
                    pElement->FindFirst(TreeScope_Descendants, pCondition, &pFound);
                    if (pFound)
                    {
                        BSTR val = NULL;
                        BSTR name = NULL;
                        CComPtr<IUIAutomationValuePattern> ptr_val_pattern = NULL;
                        pFound->get_CurrentName(&name);
                        pFound->GetCurrentPatternAs(UIA_ValuePatternId, __uuidof(IUIAutomationValuePattern), (void**)&ptr_val_pattern);
                        ptr_val_pattern->get_CurrentValue(&val);
                        val_std_str = StringUtilBase::BSTR2StdString(val);
                        //std::wstring name_std_str = StringUtilBase<std::wstring>::BSTR2StdString(name);
                        pFound->Release();
                    }
                }

                SysFreeString(urlControlName);
            }
        }
    }
    return val_std_str;
}
/*
desc:
    Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 day
    since last user input

ret:
    last user input time in milis

exception:
    throw std::exception when GetLastInputInfo fails.
    MSDN does not mention what ERROR CODE to expect when failing though.

*/
unsigned long long Doing::GetLastInputTime()
{
#ifdef _WIN32
    LASTINPUTINFO lii;
    //The size of the structure, in bytes. This member must be set to sizeof(LASTINPUTINFO).
    //It's a common way for versioning in Windows API.
    lii.cbSize = sizeof(LASTINPUTINFO);
    if (!GetLastInputInfo(&lii))
    {
        throw std::exception("GetLastInputInfo failed!");
    }
    return lii.dwTime;
#else
    return 0;
#endif
}
//end of the file<|endoftext|>


<commit_before>/*
 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
 * Contributions: Jon Trulson <jtrulson@ics.com>
 * Contributions: Thomas Ingleby <thomas.c.ingleby@intel.com>
 * Copyright (c) 2014 - 2015 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#pragma once

#include "uart.h"
#include "types.hpp"
#include <stdexcept>
#include <cstring>

namespace mraa
{

/**
 * @brief API to UART (enabling only)
 *
 * This file defines the UART interface for libmraa
 *
 * @snippet Uart-example.cpp Interesting
 */
class Uart
{
  public:
    /**
     * Uart Constructor, takes a pin number which will map directly to the
     * linux uart number, this 'enables' the uart, nothing more
     *
     * @param uart the index of the uart set to use
     */
    Uart(int uart)
    {
        m_uart = mraa_uart_init(uart);

        if (m_uart == NULL) {
            throw std::invalid_argument("Error initialising UART");
        }
    }

    /**
     * Uart Constructor, takes a string to the path of the serial
     * interface that is needed.
     *
     * @param uart the index of the uart set to use
     */
    Uart(std::string path)
    {
        m_uart = mraa_uart_init_raw(path.c_str());

        if (m_uart == NULL) {
            throw std::invalid_argument("Error initialising UART");
        }
    }

    /**
     * Uart destructor
     */
    ~Uart()
    {
        mraa_uart_stop(m_uart);
    }

    /**
     * Get string with tty device path within Linux
     * For example. Could point to "/dev/ttyS0"
     *
     * @return char pointer of device path
     */
    std::string
    getDevicePath()
    {
        std::string ret_val(mraa_uart_get_dev_path(m_uart));
        return ret_val;
    }

    /**
     * Read bytes from the device into char* buffer
     *
     * @param data buffer pointer
     * @param length maximum size of buffer
     * @return numbers of bytes read
     */
    int
    read(char* data, int length)
    {
        return mraa_uart_read(m_uart, data, (size_t) length);
    }

    /**
     * Write bytes in String object to a device
     *
     * @param data buffer pointer
     * @param length maximum size of buffer
     * @return the number of bytes written, or -1 if an error occurred
     */
    int
    write(const char* data, int length)
    {
        return mraa_uart_write(m_uart, data, (size_t) length);
    }

    /**
     * Read bytes from the device into a String object
     *
     * @param length to read
     * @return string of data
     */
    std::string
    readStr(int length)
    {
        char* data = (char*) malloc(sizeof(char) * length);
        int v = mraa_uart_read(m_uart, data, (size_t) length);
        std::string ret(data, v);
        free(data);
        return ret;
    }

    /**
     * Write bytes in String object to a device
     *
     * @param string to write
     * @return the number of bytes written, or -1 if an error occurred
     */
    int
    writeStr(std::string data)
    {
        // this is data.length() not +1 because we want to avoid the '\0' char
        return mraa_uart_write(m_uart, data.c_str(), (data.length()));
    }

    /**
     * Check to see if data is available on the device for reading
     *
     * @param millis number of milliseconds to wait, or 0 to return immediately
     * @return true if there is data available to read, false otherwise
     */
    bool
    dataAvailable(unsigned int millis = 0)
    {
        if (mraa_uart_data_available(m_uart, millis))
            return true;
        else
            return false;
    }

    /**
     * Flush the outbound data.
     * Blocks until complete.
     *
     * @return Result of operation
     */
    Result
    flush()
    {
        return (Result) mraa_uart_flush(m_uart);
    }

    /**
     * Set the baudrate.
     * Takes an int and will attempt to decide what baudrate  is
     * to be used on the UART hardware.
     *
     * @param baud unsigned int of baudrate i.e. 9600
     * @return Result of operation
     */
    Result
    setBaudRate(unsigned int baud)
    {
        return (Result) mraa_uart_set_baudrate(m_uart, baud);
    }

    /**
     * Set the transfer mode
     * For example setting the mode to 8N1 would be
     * "dev.setMode(8,UART_PARITY_NONE , 1)"
     *
     * @param bytesize data bits
     * @param parity Parity bit setting
     * @param stopbits stop bits
     * @return Result of operation
     */
    Result
    setMode(int bytesize, UartParity parity, int stopbits)
    {
        return (Result) mraa_uart_set_mode(m_uart, bytesize, (mraa_uart_parity_t) parity, stopbits);
    }

    /**
     * Set the flowcontrol
     *
     * @param xonxoff XON/XOFF Software flow control.
     * @param rtscts RTS/CTS out of band hardware flow control
     * @return Result of operation
     */
    Result
    setFlowcontrol(bool xonxoff, bool rtscts)
    {
        return (Result) mraa_uart_set_flowcontrol(m_uart, xonxoff, rtscts);
    }

    /**
     * Set the timeout for read and write operations
     * <= 0 will disable that timeout
     *
     * @param read read timeout
     * @param write write timeout
     * @param interchar inbetween char timeout
     * @return Result of operation
     */
    Result
    setTimeout(int read, int write, int interchar)
    {
        return (Result) mraa_uart_set_timeout(m_uart, read, write, interchar);
    }

  private:
    mraa_uart_context m_uart;
};
}
<commit_msg>uart: Added stdlib.h to uart.hpp so malloc() and free() are declared<commit_after>/*
 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
 * Contributions: Jon Trulson <jtrulson@ics.com>
 * Contributions: Thomas Ingleby <thomas.c.ingleby@intel.com>
 * Copyright (c) 2014 - 2015 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#pragma once

#include "uart.h"
#include "types.hpp"
#include <stdlib.h>
#include <stdexcept>
#include <cstring>

namespace mraa
{

/**
 * @brief API to UART (enabling only)
 *
 * This file defines the UART interface for libmraa
 *
 * @snippet Uart-example.cpp Interesting
 */
class Uart
{
  public:
    /**
     * Uart Constructor, takes a pin number which will map directly to the
     * linux uart number, this 'enables' the uart, nothing more
     *
     * @param uart the index of the uart set to use
     */
    Uart(int uart)
    {
        m_uart = mraa_uart_init(uart);

        if (m_uart == NULL) {
            throw std::invalid_argument("Error initialising UART");
        }
    }

    /**
     * Uart Constructor, takes a string to the path of the serial
     * interface that is needed.
     *
     * @param uart the index of the uart set to use
     */
    Uart(std::string path)
    {
        m_uart = mraa_uart_init_raw(path.c_str());

        if (m_uart == NULL) {
            throw std::invalid_argument("Error initialising UART");
        }
    }

    /**
     * Uart destructor
     */
    ~Uart()
    {
        mraa_uart_stop(m_uart);
    }

    /**
     * Get string with tty device path within Linux
     * For example. Could point to "/dev/ttyS0"
     *
     * @return char pointer of device path
     */
    std::string
    getDevicePath()
    {
        std::string ret_val(mraa_uart_get_dev_path(m_uart));
        return ret_val;
    }

    /**
     * Read bytes from the device into char* buffer
     *
     * @param data buffer pointer
     * @param length maximum size of buffer
     * @return numbers of bytes read
     */
    int
    read(char* data, int length)
    {
        return mraa_uart_read(m_uart, data, (size_t) length);
    }

    /**
     * Write bytes in String object to a device
     *
     * @param data buffer pointer
     * @param length maximum size of buffer
     * @return the number of bytes written, or -1 if an error occurred
     */
    int
    write(const char* data, int length)
    {
        return mraa_uart_write(m_uart, data, (size_t) length);
    }

    /**
     * Read bytes from the device into a String object
     *
     * @param length to read
     * @return string of data
     */
    std::string
    readStr(int length)
    {
        char* data = (char*) malloc(sizeof(char) * length);
        int v = mraa_uart_read(m_uart, data, (size_t) length);
        std::string ret(data, v);
        free(data);
        return ret;
    }

    /**
     * Write bytes in String object to a device
     *
     * @param string to write
     * @return the number of bytes written, or -1 if an error occurred
     */
    int
    writeStr(std::string data)
    {
        // this is data.length() not +1 because we want to avoid the '\0' char
        return mraa_uart_write(m_uart, data.c_str(), (data.length()));
    }

    /**
     * Check to see if data is available on the device for reading
     *
     * @param millis number of milliseconds to wait, or 0 to return immediately
     * @return true if there is data available to read, false otherwise
     */
    bool
    dataAvailable(unsigned int millis = 0)
    {
        if (mraa_uart_data_available(m_uart, millis))
            return true;
        else
            return false;
    }

    /**
     * Flush the outbound data.
     * Blocks until complete.
     *
     * @return Result of operation
     */
    Result
    flush()
    {
        return (Result) mraa_uart_flush(m_uart);
    }

    /**
     * Set the baudrate.
     * Takes an int and will attempt to decide what baudrate  is
     * to be used on the UART hardware.
     *
     * @param baud unsigned int of baudrate i.e. 9600
     * @return Result of operation
     */
    Result
    setBaudRate(unsigned int baud)
    {
        return (Result) mraa_uart_set_baudrate(m_uart, baud);
    }

    /**
     * Set the transfer mode
     * For example setting the mode to 8N1 would be
     * "dev.setMode(8,UART_PARITY_NONE , 1)"
     *
     * @param bytesize data bits
     * @param parity Parity bit setting
     * @param stopbits stop bits
     * @return Result of operation
     */
    Result
    setMode(int bytesize, UartParity parity, int stopbits)
    {
        return (Result) mraa_uart_set_mode(m_uart, bytesize, (mraa_uart_parity_t) parity, stopbits);
    }

    /**
     * Set the flowcontrol
     *
     * @param xonxoff XON/XOFF Software flow control.
     * @param rtscts RTS/CTS out of band hardware flow control
     * @return Result of operation
     */
    Result
    setFlowcontrol(bool xonxoff, bool rtscts)
    {
        return (Result) mraa_uart_set_flowcontrol(m_uart, xonxoff, rtscts);
    }

    /**
     * Set the timeout for read and write operations
     * <= 0 will disable that timeout
     *
     * @param read read timeout
     * @param write write timeout
     * @param interchar inbetween char timeout
     * @return Result of operation
     */
    Result
    setTimeout(int read, int write, int interchar)
    {
        return (Result) mraa_uart_set_timeout(m_uart, read, write, interchar);
    }

  private:
    mraa_uart_context m_uart;
};
}
<|endoftext|>


<commit_before>#include <json/json.h>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <algorithm>
#include <regex>

namespace po = boost::program_options;
namespace fs = boost::filesystem;

// append autoids to the given vector
// - recursively parses a json "tree" and stores all values of string type objects
// - strings with leading '%' are stored without '%' as well
void parseJSON(Json::Value& root, std::set<std::string>& autoids)
{
    for ( auto it = root.begin(), e = root.end(); it != e; ++it)
    {
        if ( (*it).isString() )
        {
            std::string id((*it).asString());
            autoids.insert(id);
            // insert it without %% as well
            id.erase(std::remove(id.begin(), id.end(),'%'), id.end());
            autoids.insert(id);
        }
        parseJSON(*it, autoids);
    }
}

// append autoids to the given vector
// - collects string literals from AutoId calls
// - collects string literals from macros that use AutoId
// possible issues:
// - multiplie AutoId expressions in 1 line are not parsed
void parseCpp(std::string& content, std::set<std::string>& autoids)
{
    static std::array<std::string,7> macros = {
        "DEFINE_BUFF_BASE",
        "DEFINE_COMPONENT_BASE",
        "DEFINE_ROOM_PROPERTY",
        "DEFINE_MAP_ELEMENT_BASE",
        "DEFINE_SUB_SYSTEM_BASE",
        "DEFINE_SYSTEM_BASE",
        "DEFINE_MESSAGE_BASE" };
    // the matching group (1) contains the actual string
    static std::regex autoid_regex("AutoId\\(\\s*\"(\\S+)\"\\s*\\)", std::regex_constants::ECMAScript | std::regex_constants::icase);
    auto aid_begin = std::sregex_iterator(content.begin(), content.end(), autoid_regex);
    auto aid_end = std::sregex_iterator();
    for ( auto it = aid_begin; it != aid_end; ++it )
    {
        std::smatch match = *it;
        //std::cout << "full: " << match.str() << ", group: " << match.str(1) << std::endl;
        autoids.insert(match.str(1));
    }
    // todo macros:
    // define_buff_base
    // define_component_base
    // define_room_property_base
    // define_map_element_base
    // define_sub_system_base
    // define_system_base
    // define_message_base
    for ( auto m : macros )
    {
        //std::cout << "matching for macro " << m << "..." << std::endl;
        std::regex macro_regex( m+"\\(\\s*(\\S+)\\s*\\)", std::regex_constants::ECMAScript | std::regex_constants::icase);
        auto m_begin = std::sregex_iterator(content.begin(), content.end(), macro_regex);
        auto m_end = std::sregex_iterator();
        for ( auto it = m_begin; it != m_end; ++it )
        {
            std::smatch match = *it;
            //std::cout << "full: " << match.str() << ", group: " << match.str(1) << std::endl;
            autoids.insert(match.str(1));
        }
    }
}

int main( int argc, char** argv)
{
    std::string path(""), output("");
    std::vector<std::string> ext;
    bool enableCpp(false), enableJSON(false);
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help","prints help")
        ("path",po::value<std::string>(&path),"path to files")
        ("ext", po::value< std::vector<std::string> >(&ext)->multitoken(),"extension of files, use comma separated list for multiple extensions")
        ("enable-cpp", "enable CPP parsing")
        ("enable-json", "enable JSON parsing")
        ("output-file", po::value<std::string>(&output),"name of the output file");
    po::variables_map vm;
    po::store(po::parse_command_line(argc,argv,desc),vm);
    po::notify(vm);
    if (vm.count("help"))
    {
        std::cout << "This program collects autoids from files in a directory recursively." << std::endl
                  << "Files can be interpreted as either source files or content files, so enable-cpp and enable-json are exclusive." << std::endl
                  << desc << std::endl;
        return 1;
    }
    if (vm.count("path") == 0)
    {
        std::cout <<"no path given" << std::endl;
        return 1;
    }
    if (vm.count("output-file") == 0)
    {
        std::cout << "no output file given" << std::endl;
        return 1;
    }
    enableCpp = vm.count("enable-cpp") > 0;
    enableJSON = vm.count("enable-json") > 0;
    if ( enableCpp && enableJSON )
    {
        std::cout << "enable-cpp and enable-json are exclusive, but you enabled both" << std::endl;
        return 1;
    }
    if ( !enableCpp && !enableJSON )
    {
        std::cout << "you must enable either cpp or json parsing" << std::endl;
        return 1;
    }
    if (vm.count("ext") == 0)
    {
        std::cout << "no extension given" << std::endl;
        if (enableCpp)
        {
            std::cout << "defaulting to .cpp,.h" << std::endl;
            ext.push_back(".cpp");
            ext.push_back(".h");
        }
        else
        {
            std::cout << "defaulting to .json" << std::endl;
            ext.push_back(".json");
        }
    }
    //std::cout << "supported extensions:" << std::endl;
    // auto prepend '.' : cpp -> .cpp
    for ( std::string& s : ext )
    {
        if ( s[0] != '.' )
        {
            s = '.'+s;
        }
        //std::cout << s << std::endl;
    }
    /*
    std::string e = ext.front();
    e = std::accumulate(std::next(ext.begin()), ext.end(),e, [](std::string& s, std::string& ss){ return s+","+ss;});
    std::cout << "called with" << std::endl
              << "path:" << path << std::endl
              << "ext:" << e << std::endl
              << "enable-cpp" << enableCpp << std::endl
              << "enable-json" << enableJSON << std::endl
              << "output " << output << std::endl;*/
    if (!fs::exists(path) || !fs::is_directory(path))
    {
        std::cout << path << "does not exist or not a directory" << std::endl;
        return 1;
    }
    std::set<std::string> autoids;
    for ( fs::recursive_directory_iterator it(path), eit; it != eit; ++it )
    {
        if ( fs::is_regular_file(*it) )
        {
            //std::cout << "checking file " << it->path().filename() << " extesion :" << it->path().extension() << std::endl;
            if ( std::find(ext.begin(), ext.end(), it->path().extension()) == ext.end() )
            {
                //std::cout << "not supported extension " << it->path().extension() << std::endl;
                continue;
            }
            if (enableJSON)
            {
                fs::ifstream in(it->path());
                Json::Value root;
                in >> root;
                parseJSON(root, autoids);
            }
            else
            {
                //std::cout << "checking " << it->path() << std::endl;
                fs::ifstream in(it->path());
                std::string content((std::istreambuf_iterator<char>(in)), (std::istreambuf_iterator<char>()));
                //std::cout << "content " << content << std::endl;
                parseCpp(content, autoids);
            }
        }
    }
    fs::ofstream out(output);
    for (auto autoid:autoids)
    {
        out << autoid << std::endl;
    }
    return 0;
}
<commit_msg>remove comment<commit_after>#include <json/json.h>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <algorithm>
#include <regex>

namespace po = boost::program_options;
namespace fs = boost::filesystem;

// append autoids to the given vector
// - recursively parses a json "tree" and stores all values of string type objects
// - strings with leading '%' are stored without '%' as well
void parseJSON(Json::Value& root, std::set<std::string>& autoids)
{
    for ( auto it = root.begin(), e = root.end(); it != e; ++it)
    {
        if ( (*it).isString() )
        {
            std::string id((*it).asString());
            autoids.insert(id);
            // insert it without %% as well
            id.erase(std::remove(id.begin(), id.end(),'%'), id.end());
            autoids.insert(id);
        }
        parseJSON(*it, autoids);
    }
}

// append autoids to the given vector
// - collects string literals from AutoId calls
// - collects string literals from macros that use AutoId
// possible issues:
// - multiplie AutoId expressions in 1 line are not parsed
void parseCpp(std::string& content, std::set<std::string>& autoids)
{
    static std::array<std::string,7> macros = {
        "DEFINE_BUFF_BASE",
        "DEFINE_COMPONENT_BASE",
        "DEFINE_ROOM_PROPERTY",
        "DEFINE_MAP_ELEMENT_BASE",
        "DEFINE_SUB_SYSTEM_BASE",
        "DEFINE_SYSTEM_BASE",
        "DEFINE_MESSAGE_BASE" };
    // the matching group (1) contains the actual string
    static std::regex autoid_regex("AutoId\\(\\s*\"(\\S+)\"\\s*\\)", std::regex_constants::ECMAScript | std::regex_constants::icase);
    auto aid_begin = std::sregex_iterator(content.begin(), content.end(), autoid_regex);
    auto aid_end = std::sregex_iterator();
    for ( auto it = aid_begin; it != aid_end; ++it )
    {
        std::smatch match = *it;
        //std::cout << "full: " << match.str() << ", group: " << match.str(1) << std::endl;
        autoids.insert(match.str(1));
    }
    for ( auto m : macros )
    {
        //std::cout << "matching for macro " << m << "..." << std::endl;
        std::regex macro_regex( m+"\\(\\s*(\\S+)\\s*\\)", std::regex_constants::ECMAScript | std::regex_constants::icase);
        auto m_begin = std::sregex_iterator(content.begin(), content.end(), macro_regex);
        auto m_end = std::sregex_iterator();
        for ( auto it = m_begin; it != m_end; ++it )
        {
            std::smatch match = *it;
            //std::cout << "full: " << match.str() << ", group: " << match.str(1) << std::endl;
            autoids.insert(match.str(1));
        }
    }
}

int main( int argc, char** argv)
{
    std::string path(""), output("");
    std::vector<std::string> ext;
    bool enableCpp(false), enableJSON(false);
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help","prints help")
        ("path",po::value<std::string>(&path),"path to files")
        ("ext", po::value< std::vector<std::string> >(&ext)->multitoken(),"extension of files, use comma separated list for multiple extensions")
        ("enable-cpp", "enable CPP parsing")
        ("enable-json", "enable JSON parsing")
        ("output-file", po::value<std::string>(&output),"name of the output file");
    po::variables_map vm;
    po::store(po::parse_command_line(argc,argv,desc),vm);
    po::notify(vm);
    if (vm.count("help"))
    {
        std::cout << "This program collects autoids from files in a directory recursively." << std::endl
                  << "Files can be interpreted as either source files or content files, so enable-cpp and enable-json are exclusive." << std::endl
                  << desc << std::endl;
        return 1;
    }
    if (vm.count("path") == 0)
    {
        std::cout <<"no path given" << std::endl;
        return 1;
    }
    if (vm.count("output-file") == 0)
    {
        std::cout << "no output file given" << std::endl;
        return 1;
    }
    enableCpp = vm.count("enable-cpp") > 0;
    enableJSON = vm.count("enable-json") > 0;
    if ( enableCpp && enableJSON )
    {
        std::cout << "enable-cpp and enable-json are exclusive, but you enabled both" << std::endl;
        return 1;
    }
    if ( !enableCpp && !enableJSON )
    {
        std::cout << "you must enable either cpp or json parsing" << std::endl;
        return 1;
    }
    if (vm.count("ext") == 0)
    {
        std::cout << "no extension given" << std::endl;
        if (enableCpp)
        {
            std::cout << "defaulting to .cpp,.h" << std::endl;
            ext.push_back(".cpp");
            ext.push_back(".h");
        }
        else
        {
            std::cout << "defaulting to .json" << std::endl;
            ext.push_back(".json");
        }
    }
    //std::cout << "supported extensions:" << std::endl;
    // auto prepend '.' : cpp -> .cpp
    for ( std::string& s : ext )
    {
        if ( s[0] != '.' )
        {
            s = '.'+s;
        }
        //std::cout << s << std::endl;
    }
    /*
    std::string e = ext.front();
    e = std::accumulate(std::next(ext.begin()), ext.end(),e, [](std::string& s, std::string& ss){ return s+","+ss;});
    std::cout << "called with" << std::endl
              << "path:" << path << std::endl
              << "ext:" << e << std::endl
              << "enable-cpp" << enableCpp << std::endl
              << "enable-json" << enableJSON << std::endl
              << "output " << output << std::endl;*/
    if (!fs::exists(path) || !fs::is_directory(path))
    {
        std::cout << path << "does not exist or not a directory" << std::endl;
        return 1;
    }
    std::set<std::string> autoids;
    for ( fs::recursive_directory_iterator it(path), eit; it != eit; ++it )
    {
        if ( fs::is_regular_file(*it) )
        {
            //std::cout << "checking file " << it->path().filename() << " extesion :" << it->path().extension() << std::endl;
            if ( std::find(ext.begin(), ext.end(), it->path().extension()) == ext.end() )
            {
                //std::cout << "not supported extension " << it->path().extension() << std::endl;
                continue;
            }
            if (enableJSON)
            {
                fs::ifstream in(it->path());
                Json::Value root;
                in >> root;
                parseJSON(root, autoids);
            }
            else
            {
                //std::cout << "checking " << it->path() << std::endl;
                fs::ifstream in(it->path());
                std::string content((std::istreambuf_iterator<char>(in)), (std::istreambuf_iterator<char>()));
                //std::cout << "content " << content << std::endl;
                parseCpp(content, autoids);
            }
        }
    }
    fs::ofstream out(output);
    for (auto autoid:autoids)
    {
        out << autoid << std::endl;
    }
    return 0;
}
<|endoftext|>


<commit_before>// Copyright 2013-2015 Eric Schkufza, Rahul Sharma, Berkeley Churchill, Stefan Heule
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <iostream>
#include <sys/time.h>

#include "src/ext/cpputil/include/command_line/command_line.h"
#include "src/ext/cpputil/include/io/column.h"
#include "src/ext/cpputil/include/io/filterstream.h"
#include "src/ext/cpputil/include/signal/debug_handler.h"

#include "src/cfg/cfg_transforms.h"
#include "src/tunit/tunit.h"
#include "src/search/progress_callback.h"
#include "src/search/statistics_callback.h"
#include "src/search/timeout.h"
#include "tools/args/search.h"
#include "tools/args/target.h"
#include "tools/gadgets/cost_function.h"
#include "tools/gadgets/sandbox.h"
#include "tools/gadgets/search.h"
#include "tools/gadgets/search_state.h"
#include "tools/gadgets/seed.h"
#include "tools/gadgets/target.h"
#include "tools/gadgets/testcases.h"
#include "tools/gadgets/transforms.h"
#include "tools/gadgets/verifier.h"
#include "tools/io/timeout.h"
#include "tools/ui/console.h"

using namespace cpputil;
using namespace std;
using namespace stoke;

auto& io = Heading::create("Output Options:");
auto& out = ValueArg<string>::create("out")
            .alternate("o")
            .usage("<path/to/file.s>")
            .description("File to write successful results to")
            .default_val("result.s");

auto& stats = Heading::create("Statistics Options:");
auto& stat_int =
  ValueArg<size_t>::create("statistics_interval")
  .usage("<int>")
  .description("Number of iterations between statistics updates")
  .default_val(1000000);

auto& automation_heading = Heading::create("Automation Options:");
auto& timeout_action_arg =
  ValueArg<Timeout, TimeoutReader, TimeoutWriter>::create("timeout_action")
  .usage("(quit|restart|testcase)")
  .description("Action to take when search times out")
  .default_val(Timeout::RESTART);
auto& timeout_cycles_arg =
  ValueArg<size_t>::create("timeout_cycles")
  .usage("<int>")
  .description("Number of timeout cycles to attempt before giving up")
  .default_val(16);

void sep(ostream& os) {
  for (size_t i = 0; i < 80; ++i) {
    os << "*";
  }
  os << endl << endl;
}

void pcb(const ProgressCallbackData& data, void* arg) {
  ostream& os = *((ostream*)arg);

  CfgTransforms tforms;

  os << "Progress Update: " << endl;
  os << endl;

  ofilterstream<Column> ofs(os);
  ofs.filter().padding(5);

  auto best_yet = data.state.best_yet;
  tforms.remove_unreachable(best_yet);
  tforms.remove_nop(best_yet);

  ofs << "Lowest Cost Discovered (" << data.state.best_yet_cost << ")" << endl;
  ofs << endl;
  ofs << best_yet.get_code();
  ofs.filter().next();

  auto best_correct = data.state.best_correct;
  tforms.remove_unreachable(best_correct);
  tforms.remove_nop(best_correct);

  ofs << "Lowest Known Correct Cost (" << data.state.best_correct_cost << ")" << endl;
  ofs << endl;
  ofs << best_correct.get_code();
  ofs.filter().done();

  os << endl << endl;
  sep(os);
}

struct ScbArg {
  ostream* os;
  uint32_t** cost_stats;
};

void scb(const StatisticsCallbackData& data, void* arg) {
  ScbArg sa = *((ScbArg*)arg);
  ostream& os = *(sa.os);
  uint32_t** cost_stats = sa.cost_stats;

  os << "Statistics Update: " << endl;
  os << endl;
  os << "Iterations:   " << data.iterations << endl;
  os << "Elapsed Time: " << data.elapsed.count() << "s" << endl;
  os << "Iterations/s: " << (data.iterations / data.elapsed.count()) << endl;
  os << endl;

  ofilterstream<Column> ofs(os);
  ofs.filter().padding(5);

  Statistics total;
  for (size_t i = 0; i < 6; ++i) {
    total += data.move_statistics[i];
  }

  ofs << "Move Type" << endl;
  ofs << endl;
  ofs << "Instruction" << endl;
  ofs << "Opcode" << endl;
  ofs << "Operand" << endl;
  ofs << "Resize" << endl;
  ofs << "Local Swap" << endl;
  ofs << "Global Swap" << endl;
  ofs << "Extension" << endl;
  ofs << endl;
  ofs << "Total";
  ofs.filter().next();

  ofs << "Proposed" << endl;
  ofs << endl;
  for (size_t i = 0; i < (size_t) Move::NUM_MOVES; ++i) {
    ofs << 100 * (double)data.move_statistics[i].num_proposed / data.iterations << "%" << endl;
  }
  ofs << endl;
  ofs << 100 * (double)total.num_proposed / data.iterations << "%";
  ofs.filter().next();

  ofs << "Succeeded" << endl;
  ofs << endl;
  for (size_t i = 0; i < (size_t) Move::NUM_MOVES; ++i) {
    ofs << 100 * (double)data.move_statistics[i].num_succeeded / data.iterations << "%" << endl;
  }
  ofs << endl;
  ofs << 100 * (double)total.num_succeeded / data.iterations << "%";
  ofs.filter().next();

  ofs << "Accepted" << endl;
  ofs << endl;
  for (size_t i = 0; i < (size_t) Move::NUM_MOVES; ++i) {
    ofs << 100 * (double)data.move_statistics[i].num_accepted / data.iterations << "%" << endl;
  }
  ofs << endl;
  ofs << 100 * (double)total.num_accepted / data.iterations << "%";
  ofs.filter().done();

  os << endl << endl;
  sep(os);

}

int main(int argc, char** argv) {
  CommandLineConfig::strict_with_convenience(argc, argv);
  DebugHandler::install_sigsegv();
  DebugHandler::install_sigill();

  SeedGadget seed;
  TargetGadget target;

  TrainingSetGadget training_set(seed);
  SandboxGadget training_sb(training_set);

  TransformsGadget transforms(seed);
  SearchGadget search(&transforms, seed);

  TestSetGadget test_set(seed);
  SandboxGadget test_sb(test_set);
  CostFunctionGadget holdout_fxn(target, &test_sb);
  VerifierGadget verifier(holdout_fxn);

  if (!target.is_sound()) {
    Console::error(1) << "Target reads undefined variables, or leaves live_out undefined." << endl;
  }

  ScbArg scb_arg {&Console::msg(), nullptr};
  search.set_progress_callback(pcb, &Console::msg())
  .set_statistics_callback(scb, &scb_arg)
  .set_statistics_interval(stat_int);

  SearchStateGadget state;
  for (size_t i = 0; ; ++i) {
    CostFunctionGadget fxn(target, &training_sb);

    Console::msg() << "Running search:" << endl << endl;
    state = SearchStateGadget();
    search.run(target, fxn, init_arg, state, aux_fxns_arg.value());

    if (state.interrupted) {
      Console::msg() << "Search interrupted!" << endl;
      exit(1);
    }

    const auto verified = verifier.verify(target, state.best_correct);

    if (!state.success) {
      Console::msg() << "Unable to discover a new correct rewrite before timing out... " << endl << endl;
    } else if (!verified) {
      Console::msg() << "Unable to verify new rewrite..." << endl << endl;
    } else {
      if (strategy_arg.value() == Strategy::NONE) {
        Console::msg() << "Search terminated successfully (but no verification was performed)!" << endl;
      } else {
        Console::msg() << "Search terminated successfully with a verified rewrite!" << endl;
      }
      break;
    }

    sep(Console::msg());

    if ((timeout_action_arg == Timeout::RESTART) && (i < timeout_cycles_arg.value())) {
      Console::msg() << "Restarting search:" << endl << endl;
    } else if ((timeout_action_arg == Timeout::TESTCASE) && (i < timeout_cycles_arg.value())
               && verifier.counter_example_available()) {
      Console::msg() << "Restarting search using new testcase:" << endl << endl;
      Console::msg() << verifier.get_counter_example() << endl << endl;
      training_sb.insert_input(verifier.get_counter_example());
    } else {
      Console::error(1) << "Search terminated unsuccessfully; unable to discover a new rewrite!" << endl;
    }
  }

  CfgTransforms tforms;
  tforms.remove_unreachable(state.best_correct);
  tforms.remove_nop(state.best_correct);

  TUnit rewrite;
  rewrite.name = target_arg.value().name;
  rewrite.code = state.best_correct.get_code();

  ofstream ofs(out.value());
  ofs << rewrite;

  return 0;
}
<commit_msg>added verifier errors to stoke search<commit_after>// Copyright 2013-2015 Eric Schkufza, Rahul Sharma, Berkeley Churchill, Stefan Heule
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <iostream>
#include <sys/time.h>

#include "src/ext/cpputil/include/command_line/command_line.h"
#include "src/ext/cpputil/include/io/column.h"
#include "src/ext/cpputil/include/io/filterstream.h"
#include "src/ext/cpputil/include/signal/debug_handler.h"

#include "src/cfg/cfg_transforms.h"
#include "src/tunit/tunit.h"
#include "src/search/progress_callback.h"
#include "src/search/statistics_callback.h"
#include "src/search/timeout.h"
#include "tools/args/search.h"
#include "tools/args/target.h"
#include "tools/gadgets/cost_function.h"
#include "tools/gadgets/sandbox.h"
#include "tools/gadgets/search.h"
#include "tools/gadgets/search_state.h"
#include "tools/gadgets/seed.h"
#include "tools/gadgets/target.h"
#include "tools/gadgets/testcases.h"
#include "tools/gadgets/transforms.h"
#include "tools/gadgets/verifier.h"
#include "tools/io/timeout.h"
#include "tools/ui/console.h"

using namespace cpputil;
using namespace std;
using namespace stoke;

auto& io = Heading::create("Output Options:");
auto& out = ValueArg<string>::create("out")
            .alternate("o")
            .usage("<path/to/file.s>")
            .description("File to write successful results to")
            .default_val("result.s");

auto& stats = Heading::create("Statistics Options:");
auto& stat_int =
  ValueArg<size_t>::create("statistics_interval")
  .usage("<int>")
  .description("Number of iterations between statistics updates")
  .default_val(1000000);

auto& automation_heading = Heading::create("Automation Options:");
auto& timeout_action_arg =
  ValueArg<Timeout, TimeoutReader, TimeoutWriter>::create("timeout_action")
  .usage("(quit|restart|testcase)")
  .description("Action to take when search times out")
  .default_val(Timeout::RESTART);
auto& timeout_cycles_arg =
  ValueArg<size_t>::create("timeout_cycles")
  .usage("<int>")
  .description("Number of timeout cycles to attempt before giving up")
  .default_val(16);

void sep(ostream& os) {
  for (size_t i = 0; i < 80; ++i) {
    os << "*";
  }
  os << endl << endl;
}

void pcb(const ProgressCallbackData& data, void* arg) {
  ostream& os = *((ostream*)arg);

  CfgTransforms tforms;

  os << "Progress Update: " << endl;
  os << endl;

  ofilterstream<Column> ofs(os);
  ofs.filter().padding(5);

  auto best_yet = data.state.best_yet;
  tforms.remove_unreachable(best_yet);
  tforms.remove_nop(best_yet);

  ofs << "Lowest Cost Discovered (" << data.state.best_yet_cost << ")" << endl;
  ofs << endl;
  ofs << best_yet.get_code();
  ofs.filter().next();

  auto best_correct = data.state.best_correct;
  tforms.remove_unreachable(best_correct);
  tforms.remove_nop(best_correct);

  ofs << "Lowest Known Correct Cost (" << data.state.best_correct_cost << ")" << endl;
  ofs << endl;
  ofs << best_correct.get_code();
  ofs.filter().done();

  os << endl << endl;
  sep(os);
}

struct ScbArg {
  ostream* os;
  uint32_t** cost_stats;
};

void scb(const StatisticsCallbackData& data, void* arg) {
  ScbArg sa = *((ScbArg*)arg);
  ostream& os = *(sa.os);
  uint32_t** cost_stats = sa.cost_stats;

  os << "Statistics Update: " << endl;
  os << endl;
  os << "Iterations:   " << data.iterations << endl;
  os << "Elapsed Time: " << data.elapsed.count() << "s" << endl;
  os << "Iterations/s: " << (data.iterations / data.elapsed.count()) << endl;
  os << endl;

  ofilterstream<Column> ofs(os);
  ofs.filter().padding(5);

  Statistics total;
  for (size_t i = 0; i < 6; ++i) {
    total += data.move_statistics[i];
  }

  ofs << "Move Type" << endl;
  ofs << endl;
  ofs << "Instruction" << endl;
  ofs << "Opcode" << endl;
  ofs << "Operand" << endl;
  ofs << "Resize" << endl;
  ofs << "Local Swap" << endl;
  ofs << "Global Swap" << endl;
  ofs << "Extension" << endl;
  ofs << endl;
  ofs << "Total";
  ofs.filter().next();

  ofs << "Proposed" << endl;
  ofs << endl;
  for (size_t i = 0; i < (size_t) Move::NUM_MOVES; ++i) {
    ofs << 100 * (double)data.move_statistics[i].num_proposed / data.iterations << "%" << endl;
  }
  ofs << endl;
  ofs << 100 * (double)total.num_proposed / data.iterations << "%";
  ofs.filter().next();

  ofs << "Succeeded" << endl;
  ofs << endl;
  for (size_t i = 0; i < (size_t) Move::NUM_MOVES; ++i) {
    ofs << 100 * (double)data.move_statistics[i].num_succeeded / data.iterations << "%" << endl;
  }
  ofs << endl;
  ofs << 100 * (double)total.num_succeeded / data.iterations << "%";
  ofs.filter().next();

  ofs << "Accepted" << endl;
  ofs << endl;
  for (size_t i = 0; i < (size_t) Move::NUM_MOVES; ++i) {
    ofs << 100 * (double)data.move_statistics[i].num_accepted / data.iterations << "%" << endl;
  }
  ofs << endl;
  ofs << 100 * (double)total.num_accepted / data.iterations << "%";
  ofs.filter().done();

  os << endl << endl;
  sep(os);

}

int main(int argc, char** argv) {
  CommandLineConfig::strict_with_convenience(argc, argv);
  DebugHandler::install_sigsegv();
  DebugHandler::install_sigill();

  SeedGadget seed;
  TargetGadget target;

  TrainingSetGadget training_set(seed);
  SandboxGadget training_sb(training_set);

  TransformsGadget transforms(seed);
  SearchGadget search(&transforms, seed);

  TestSetGadget test_set(seed);
  SandboxGadget test_sb(test_set);
  CostFunctionGadget holdout_fxn(target, &test_sb);
  VerifierGadget verifier(holdout_fxn);

  if (!target.is_sound()) {
    Console::error(1) << "Target reads undefined variables, or leaves live_out undefined." << endl;
  }

  ScbArg scb_arg {&Console::msg(), nullptr};
  search.set_progress_callback(pcb, &Console::msg())
  .set_statistics_callback(scb, &scb_arg)
  .set_statistics_interval(stat_int);

  SearchStateGadget state;
  for (size_t i = 0; ; ++i) {
    CostFunctionGadget fxn(target, &training_sb);

    Console::msg() << "Running search:" << endl << endl;
    state = SearchStateGadget();
    search.run(target, fxn, init_arg, state, aux_fxns_arg.value());

    if (state.interrupted) {
      Console::msg() << "Search interrupted!" << endl;
      exit(1);
    }

    const auto verified = verifier.verify(target, state.best_correct);

    if(verifier.has_error()) {
      Console::msg() << "The verifier encountered an error:" << endl;
      Console::msg() << verifier.error() << endl;
    }

    if (!state.success) {
      Console::msg() << "Unable to discover a new correct rewrite before timing out... " << endl << endl;
    } else if (!verified) {
      Console::msg() << "Unable to verify new rewrite..." << endl << endl;
    } else {
      if (strategy_arg.value() == Strategy::NONE) {
        Console::msg() << "Search terminated successfully (but no verification was performed)!" << endl;
      } else {
        Console::msg() << "Search terminated successfully with a verified rewrite!" << endl;
      }
      break;
    }

    sep(Console::msg());

    if ((timeout_action_arg == Timeout::RESTART) && (i < timeout_cycles_arg.value())) {
      Console::msg() << "Restarting search:" << endl << endl;
    } else if ((timeout_action_arg == Timeout::TESTCASE) && (i < timeout_cycles_arg.value())
               && verifier.counter_example_available()) {
      Console::msg() << "Restarting search using new testcase:" << endl << endl;
      Console::msg() << verifier.get_counter_example() << endl << endl;
      training_sb.insert_input(verifier.get_counter_example());
    } else {
      Console::error(1) << "Search terminated unsuccessfully; unable to discover a new rewrite!" << endl;
    }
  }

  CfgTransforms tforms;
  tforms.remove_unreachable(state.best_correct);
  tforms.remove_nop(state.best_correct);

  TUnit rewrite;
  rewrite.name = target_arg.value().name;
  rewrite.code = state.best_correct.get_code();

  ofstream ofs(out.value());
  ofs << rewrite;

  return 0;
}
<|endoftext|>


<commit_before>/**
 * @file fractile.cpp
 *
 * @date June, 2016
 * @author Tack
 **/

#include "fractile.h"

#include <iostream>
#include <algorithm>
#include <string>

#include <boost/thread.hpp>

#include "logger_factory.h"
#include "plugin_factory.h"

#include "json_parser.h"
#include "fetcher.h"
#include "ensemble.h"

namespace himan
{

namespace plugin
{

fractile::fractile()
{
    itsClearTextFormula = "%";
    itsCudaEnabledCalculation = false;
    itsLogger = logger_factory::Instance()->GetLog("fractile");
}

fractile::~fractile()
{
}

void fractile::Process(const std::shared_ptr<const plugin_configuration> conf)
{
    Init(conf);

    if(!itsConfiguration->GetValue("param").empty())
    {
        itsParamName = itsConfiguration->GetValue("param");
    }
    else
    {
        throw std::runtime_error("Fractile_plugin: param not specified.");
        exit(1);
    }

    auto r = GET_PLUGIN(radon);

    params calculatedParams;
    std::vector<std::string> fractiles = {"F0-","F10-","F25-","F50-","F75-","F90-","F100-"};

    for (const std::string& fractile : fractiles)
    {
	calculatedParams.push_back(param(fractile + itsParamName));
    }

    SetParams(calculatedParams);

    Start();
}

void fractile::Calculate(std::shared_ptr<info> myTargetInfo, uint16_t threadIndex)
{

    const int numForecasts = 51;
    std::vector<int> fractile = {0,10,25,50,75,90,100};

    const std::string deviceType = "CPU";

    auto threadedLogger = logger_factory::Instance()->GetLog("fractileThread # " + boost::lexical_cast<std::string>(threadIndex));

    forecast_time forecastTime = myTargetInfo->Time();
    level forecastLevel = myTargetInfo->Level();

    threadedLogger->Info("Calculating time " + static_cast<std::string>(forecastTime.ValidDateTime()) +
                         " level " + static_cast<std::string>(forecastLevel));

    ensemble ens(param(itsParamName), numForecasts);

    try
    {
        ens.Fetch(itsConfiguration, forecastTime, forecastLevel);
    }
    catch (const HPExceptionType& e)
    {
	if (e == kFileDataNotFound)
	{
        	throw std::runtime_error(ClassName() + " failed to find ensemble data");
	}
    }

    myTargetInfo->ResetLocation();
    ens.ResetLocation();

    while (myTargetInfo->NextLocation() && ens.NextLocation())
    {
	auto sortedValues = ens.SortedValues();

        size_t targetInfoIndex = 0;
        for (auto i : fractile)
        {
            myTargetInfo->ParamIndex(targetInfoIndex);
            myTargetInfo->Value(sortedValues[i*(numForecasts-1)/100]);
            ++targetInfoIndex;
        }
    }

	threadedLogger->Info("[" + deviceType + "] Missing values: " + boost::lexical_cast<std::string>
			(myTargetInfo->Data().MissingCount()) + "/" + boost::lexical_cast<std::string> (myTargetInfo->Data().Size()));
}

} // plugin

} // namespace
<commit_msg>Remove get plugin call<commit_after>/**
 * @file fractile.cpp
 *
 * @date June, 2016
 * @author Tack
 **/

#include "fractile.h"

#include <iostream>
#include <algorithm>
#include <string>

#include <boost/thread.hpp>

#include "logger_factory.h"
#include "plugin_factory.h"

#include "json_parser.h"
#include "fetcher.h"
#include "ensemble.h"

namespace himan
{

namespace plugin
{

fractile::fractile()
{
    itsClearTextFormula = "%";
    itsCudaEnabledCalculation = false;
    itsLogger = logger_factory::Instance()->GetLog("fractile");
}

fractile::~fractile()
{
}

void fractile::Process(const std::shared_ptr<const plugin_configuration> conf)
{
    Init(conf);

    if(!itsConfiguration->GetValue("param").empty())
    {
        itsParamName = itsConfiguration->GetValue("param");
    }
    else
    {
        throw std::runtime_error("Fractile_plugin: param not specified.");
        exit(1);
    }

    params calculatedParams;
    std::vector<std::string> fractiles = {"F0-","F10-","F25-","F50-","F75-","F90-","F100-"};

    for (const std::string& fractile : fractiles)
    {
	calculatedParams.push_back(param(fractile + itsParamName));
    }

    SetParams(calculatedParams);

    Start();
}

void fractile::Calculate(std::shared_ptr<info> myTargetInfo, uint16_t threadIndex)
{

    const int numForecasts = 51;
    std::vector<int> fractile = {0,10,25,50,75,90,100};

    const std::string deviceType = "CPU";

    auto threadedLogger = logger_factory::Instance()->GetLog("fractileThread # " + boost::lexical_cast<std::string>(threadIndex));

    forecast_time forecastTime = myTargetInfo->Time();
    level forecastLevel = myTargetInfo->Level();

    threadedLogger->Info("Calculating time " + static_cast<std::string>(forecastTime.ValidDateTime()) +
                         " level " + static_cast<std::string>(forecastLevel));

    ensemble ens(param(itsParamName), numForecasts);

    try
    {
        ens.Fetch(itsConfiguration, forecastTime, forecastLevel);
    }
    catch (const HPExceptionType& e)
    {
	if (e == kFileDataNotFound)
	{
        	throw std::runtime_error(ClassName() + " failed to find ensemble data");
	}
    }

    myTargetInfo->ResetLocation();
    ens.ResetLocation();

    while (myTargetInfo->NextLocation() && ens.NextLocation())
    {
	auto sortedValues = ens.SortedValues();

        size_t targetInfoIndex = 0;
        for (auto i : fractile)
        {
            myTargetInfo->ParamIndex(targetInfoIndex);
            myTargetInfo->Value(sortedValues[i*(numForecasts-1)/100]);
            ++targetInfoIndex;
        }
    }

	threadedLogger->Info("[" + deviceType + "] Missing values: " + boost::lexical_cast<std::string>
			(myTargetInfo->Data().MissingCount()) + "/" + boost::lexical_cast<std::string> (myTargetInfo->Data().Size()));
}

} // plugin

} // namespace
<|endoftext|>