11 #include "../clib/logfacility.h" 12 #include "../clib/rawtypes.h" 13 #include "../clib/strutil.h" 41 fp = fopen( fname,
"rb" );
43 throw std::runtime_error( std::string(
"Unable to open " ) + fname +
" for reading." );
46 if ( fread( &hdr,
sizeof hdr, 1, fp ) != 1 )
48 ERROR_PRINT <<
"Error loading script " << fname <<
": error reading header\n";
54 ERROR_PRINT <<
"Error loading script " << fname <<
": bad magic value '" << hdr.
magic2[0]
62 ERROR_PRINT <<
"Error loading script " << fname <<
": Recompile required. Bad version number " 70 while ( fread( &sechdr,
sizeof sechdr, 1, fp ) == 1 )
72 switch ( sechdr.
type )
77 ERROR_PRINT <<
"Error loading script " << fname <<
": error reading progdef section\n";
86 ERROR_PRINT <<
"Error loading script " << fname <<
": error reading module section\n";
101 <<
": error reading global variable name section\n";
110 <<
": error reading exported functions section\n";
116 ERROR_PRINT <<
"Error loading script " << fname <<
": unknown section type " << sechdr.
type 126 catch ( std::exception& ex )
128 ERROR_PRINT <<
"Exception caught while loading script " << fname <<
": " << ex.what() <<
"\n";
136 ERROR_PRINT <<
"Exception caught while loading script " << fname <<
"\n";
146 instr.resize( nLines );
148 for (
int i = 0; i < nLines; i++ )
166 if ( fread( &hdr,
sizeof hdr, 1, fp ) != 1 )
180 if ( fread( &hdr,
sizeof hdr, 1, fp ) != 1 )
183 for (
unsigned i = 0; i < hdr.
nfuncs; i++ )
186 if ( fread( &func,
sizeof func, 1, fp ) != 1 )
205 token.
id = static_cast<BTokenId>( st.
id );
218 throw std::runtime_error(
219 "Symbol offset of " +
Clib::decint( st.
offset ) +
" exceeds symbol store length of " +
227 throw std::runtime_error(
228 "Symbol offset of " +
Clib::decint( st.
offset ) +
" exceeds symbol store length of " +
236 throw std::runtime_error(
237 "Symbol offset of " +
Clib::decint( st.
offset ) +
" exceeds symbol store length of " +
248 throw std::runtime_error(
249 "Symbol offset of " +
Clib::decint( st.
offset ) +
" exceeds symbol store length of " +
254 token.
lval = dt->offset;
258 throw std::runtime_error(
"Symbol offset of " +
Clib::decint( dt->strOffset ) +
259 " exceeds symbol store length of " +
308 throw std::runtime_error(
309 "Symbol offset of " +
Clib::decint( st.
offset ) +
" exceeds symbol store length of " +
331 throw std::runtime_error(
332 "Symbol offset of " +
Clib::decint( st.
offset ) +
" exceeds symbol store length of " +
344 if ( fread( &hdr,
sizeof hdr, 1, fp ) != 1 )
347 unsigned bufalloc = 20;
348 auto buffer =
new char[bufalloc];
349 for (
unsigned idx = 0; idx < hdr.
nGlobalVars; ++idx )
352 if ( fread( &ghdr,
sizeof ghdr, 1, fp ) != 1 )
357 if ( ghdr.
namelen >= bufalloc )
361 buffer =
new char[bufalloc];
380 unsigned nexports = hdr->
length /
sizeof bef;
383 if ( fread( &bef,
sizeof bef, 1, fp ) != 1 )
401 std::string mname =
name;
402 mname.replace( mname.size() - 3, 3,
"dbg" );
403 FILE* fp = fopen( mname.c_str(),
"rb" );
411 size_t fread_res = fread( &dbgversion,
sizeof dbgversion, 1, fp );
412 if ( fread_res != 1 || ( dbgversion != 2 && dbgversion != 3 ) )
414 ERROR_PRINT <<
"Recompile required. Bad version " << dbgversion <<
" in " << mname
415 <<
", expected version 2\n";
420 size_t bufalloc = 20;
421 auto buffer = std::unique_ptr<char[]>(
new char[bufalloc] );
425 fread_res = fread( &count,
sizeof count, 1, fp );
426 if ( fread_res != 1 )
434 fread_res = fread( &count,
sizeof count, 1, fp );
435 if ( fread_res != 1 )
440 if ( count >= bufalloc )
442 bufalloc = count * 2;
443 buffer.reset(
new char[bufalloc] );
446 if ( fread_res != 1 )
454 fread_res = fread( &count,
sizeof count, 1, fp );
455 if ( fread_res != 1 )
463 fread_res = fread( &count,
sizeof count, 1, fp );
464 if ( fread_res != 1 )
469 if ( count >= bufalloc )
471 bufalloc = count * 2;
472 buffer.reset(
new char[bufalloc] );
475 if ( fread_res != 1 )
483 fread_res = fread( &count,
sizeof count, 1, fp );
484 if ( fread_res != 1 )
496 fread_res = fread( &ins,
sizeof ins, 1, fp );
497 if ( fread_res != 1 )
507 fread_res = fread( &count,
sizeof count, 1, fp );
508 if ( fread_res != 1 )
514 for (
auto& block :
blocks )
518 fread_res = fread( &tmp,
sizeof tmp, 1, fp );
519 if ( fread_res != 1 )
524 block.parentblockidx = tmp;
526 fread_res = fread( &tmp,
sizeof tmp, 1, fp );
527 if ( fread_res != 1 )
532 block.parentvariables = tmp;
534 fread_res = fread( &tmp,
sizeof tmp, 1, fp );
535 if ( fread_res != 1 )
540 block.localvarnames.resize( tmp );
542 for (
auto& elem : block.localvarnames )
544 fread_res = fread( &count,
sizeof count, 1, fp );
545 if ( fread_res != 1 )
550 if ( count >= bufalloc )
552 bufalloc = count * 2;
553 buffer.reset(
new char[bufalloc] );
556 if ( fread_res != 1 )
564 if ( dbgversion >= 3 )
566 fread_res = fread( &count,
sizeof count, 1, fp );
567 if ( fread_res != 1 )
576 fread_res = fread( &tmp,
sizeof tmp, 1, fp );
577 if ( fread_res != 1 )
582 if ( tmp >= bufalloc )
585 buffer.reset(
new char[bufalloc] );
587 fread_res = fread(
buffer.get(), tmp, 1, fp );
588 if ( fread_res != 1 )
595 fread_res = fread( &tmp,
sizeof tmp, 1, fp );
596 if ( fread_res != 1 )
602 fread_res = fread( &tmp,
sizeof tmp, 1, fp );
603 if ( fread_res != 1 )
std::vector< unsigned > dbg_ins_blocks
int read_module(FILE *fp)
void atGet1(unsigned position, StoredToken &token) const
int create_instructions()
#define ESCRIPT_FILE_VER_CURRENT
int read_progdef_hdr(FILE *fp)
std::vector< bool > dbg_ins_statementbegin
#define BSCRIPT_FILE_MAGIC0
std::string decint(unsigned short v)
std::vector< EPExportedFunction > exported_functions
boost_utils::script_name_flystring name
int _readToken(Token &token, unsigned position) const
ObjMethod * getKnownObjMethod(const char *token)
virtual void read(FILE *fp) POL_OVERRIDE
std::vector< EPDbgFunction > dbg_functions
boost_utils::function_name_flystring name
int read_globalvarnames(FILE *fp)
std::vector< std::string > globalvarnames
#define BSCRIPT_FILE_MAGIC1
std::vector< std::string > dbg_filenames
unsigned count(void) const
int read_exported_functions(FILE *fp, BSCRIPT_SECTION_HDR *hdr)
std::vector< Instruction > instr
unsigned length(void) const
std::vector< ModuleFunction * > functions
std::vector< EPDbgBlock > blocks
unsigned int count() const
const unsigned char * dataptr
StoredTokenContainer tokens
std::vector< unsigned > dbg_linenum
std::vector< unsigned > dbg_filenum
int read(const char *fname)
virtual void read(FILE *fp)
const char * array(void) const
void setStr(const char *s)
std::vector< FunctionalityModule * > modules
static ExecInstrFunc GetInstrFunc(const Token &token)