Commits

RomanGol committed 52b61b4 Merge

Merge branch 'master' of https://bitbucket.org/RomanGol/diaos

  • Participants
  • Parent commits 8cc875d, 46e7f33

Comments (0)

Files changed (2)

DIA/InterpC-portable.cpp

 #include "diaos.h"
 
 const static uint32_t	ClassFilterSize		= 256;
-const static uint32_t	Buf_size			= 64;
-const static size_t 	Max_line_len		= 256;
+const static uint32_t	BufSize				= 256;
+const static size_t 	MaxLineLen			= 256;
 
+static size_t 			ClassHashFilter[ClassFilterSize]	= {0};
+static size_t 			ActualClassFilterSize				= 0; 
 
-static bool				Init_flag = false;
+static bool				InitFlag = false;
 
 // BKDR Hash Function
 size_t inline BKDRHash( const char * const str )
 
 void record_opcode( LoCCS_opcode &buf, const Thread* self, const Method* curMethod, const u2* pc, const u4* fp )
 {
-	/* record opcode contents */
+	/* record opcode contents */ 
     buf.threadId = self->threadId ;
     buf.pc = (uint32_t) (pc);
 
 	/* must use assignment operation!!! */
-    for (size_t i = 0; i < instNumInLo; i++)
+    for ( size_t i = 0; i < RegMaxNum; ++i )
 		buf.inst[i] = pc[i];
 
-    for (size_t i = 0; i < curMethod->registersSize && i < regNum; ++i )
+    for (size_t i = 0; i < curMethod->registersSize && i < RegMaxNum; ++i )
 		buf.reg[i] = fp[i];
-
-    memcpy( buf.methodDescriptor, curMethod->clazz->descriptor, strlen(curMethod->clazz->descriptor) );
-    memcpy( buf.methodName, curMethod->name, strlen(curMethod->name) );
-
-	//snprintf(buf.logcontent, logContentLen, __VA_ARGS__);
+    strcpy( buf.methodDescriptor, curMethod->clazz->descriptor );
+    strcpy( buf.methodName, curMethod->name );
 }
 
 
     fwrite ( file_end, 1, sizeof(file_end), f ); 
 }
 
-void save_opcode_record( const LoCCS_opcode (&buf)[Buf_size], size_t count, FILE* f )
+void save_opcode_record( LoCCS_opcode (&buf)[BufSize], size_t count, FILE* f )
 {
 	// if no buffer available, write to file and empty buffer
 	if ( count != 0 )
 			ALOG( LOG_VERBOSE, "YWB", "WRITE opcodes.bin ERROR\n" );
 			return;
 		}
+		else
+		{
+			ALOG( LOG_VERBOSE, "YWB", "WRITE %d\n", count );
+			memset ( buf, 0, sizeof(buf) );
+		}
 	}
 }
 
-bool ClassShouldBeTraced ( const Method* const method, const size_t (&filter)[ClassFilterSize], size_t filter_size )
+bool class_should_be_traced ( const Method* const method, const size_t (&filter)[ClassFilterSize], size_t filter_size )
 {
+	if (method->clazz->descriptor[0] == 0) 
+		return false;
+
 	static const char* last_traced_class = 0;
 	if ( last_traced_class == method->clazz->descriptor )
 		return true;
 
+	
 	size_t hash = BKDRHash(method->clazz->descriptor); 
     for ( size_t i = 0; i < filter_size; ++i )
     {
 	return false;
 }
 
-size_t initialClassHashFilter( FILE* f, size_t (&filter)[ClassFilterSize] )
+size_t init_class_hash_filter( FILE* f, size_t (&filter)[ClassFilterSize] )
 {
-	char buf[Max_line_len];
+	char buf[MaxLineLen];
     size_t counter = 0;
 
     while ( !feof(f) )
     {
     	memset( buf, 0, sizeof(buf) );
-        fgets( buf, Max_line_len, f );
-        buf[ strlen(buf) - 1 ] = 0;  // remove newline
+        fgets( buf, MaxLineLen, f );
 
-		filter[counter] = BKDRHash(buf);
-		if ( ++counter == ClassFilterSize )
-			return counter;  
+		size_t read_len = strlen(buf);
+		if ( read_len > 0 && read_len < ClassNameMaxLen )
+		{
+			if ( buf[read_len - 1] == '\n'  )
+        		buf[ read_len - 1 ] = 0;  // remove newline
+			
+			filter[counter] = BKDRHash(buf);
+			if ( ++counter == ClassFilterSize )
+				return counter;
+		} 
     }
 
 	ALOG(LOG_VERBOSE,"YWB","Init class filter\n");
 /*
  * If enabled, log instructions as we execute them.
  */
-# if defined(LOCCS_DIAOS)
-# define ILOGD(...) ILOG(LOG_DEBUG, __VA_ARGS__)
-# define ILOGV(...) ILOG(LOG_VERBOSE, __VA_ARGS__)
-# define ILOG(_level, ...) do {                                                         		\
-        if ( ClassShouldBeTraced ( curMethod, ClassHashFilter, ActualClassFilterSize ) ) {		\
-            record_opcode ( Opcode_buf[Buf_index], self, curMethod, pc, fp );               		\
-            if ( ++Buf_index == Buf_size ) {                                            		\
-                save_opcode_record( Opcode_buf, Buf_index, instFile );                              		\
-                Buf_index = 0;																	\
-        	}                                                                           		\
-        }                                                                               		\
-    } while(false)
-# define DUMP_REGS(_meth, _frame, _inOnly) ((void)0)
-# elif defined(LOG_INSTR)
+# if defined(LOG_INSTR)
 # define ILOGD(...) ILOG(LOG_DEBUG, __VA_ARGS__)
 # define ILOGV(...) ILOG(LOG_VERBOSE, __VA_ARGS__)
 # define ILOG(_level, ...) do {                                             \
  */
 # define H(_op)             &&op_##_op
 # define HANDLE_OPCODE(_op) op_##_op:
+
+#if defined(LOCCS_DIAOS)
+#define FINISH(_offset) {                                                  \
+        if ( class_should_be_traced ( curMethod, ClassHashFilter, ActualClassFilterSize ) ) {	\
+            record_opcode ( opcodeBuf[bufIndex], self, curMethod, pc, fp );               		\
+            if ( ++bufIndex == BufSize ) {                                            			\
+                save_opcode_record( opcodeBuf, bufIndex, instFile );                      		\
+                bufIndex = 0;																	\
+        	}                                                                           		\
+        }                                                                               		\
+        ADJUST_PC(_offset);                                                 \
+        inst = FETCH(0);                                                    \
+        if (self->interpBreak.ctl.subMode) {                                \
+            dvmCheckBefore(pc, fp, self);                                   \
+        }                                                                   \
+        goto *handlerTable[INST_INST(inst)];                                \
+    }
+#else
 # define FINISH(_offset) {                                                  \
         ADJUST_PC(_offset);                                                 \
         inst = FETCH(0);                                                    \
         }                                                                   \
         goto *handlerTable[INST_INST(inst)];                                \
     }
-# define FINISH_BKPT(_opcode) {                                             \
+#endif
+#define FINISH_BKPT(_opcode) {                                             \
         goto *handlerTable[_opcode];                                        \
     }
 
 {
 
 #if defined(LOCCS_DIAOS)
-	static LoCCS_opcode 	Opcode_buf[Buf_size];
-	static size_t	 		Buf_index = 0;
-	static size_t 			ClassHashFilter[ClassFilterSize]	= {0};
-	static size_t 			ActualClassFilterSize				= 0; 
+	static LoCCS_opcode	opcodeBuf[BufSize];
 
-	if ( !Init_flag )
+	if ( !InitFlag )
 	{
-		FILE * class_filter_file = fopen ("/data/diaos/class.dlist", "r");
-		if ( class_filter_file == NULL )
+		FILE * classFilterFile = fopen ("/data/diaos/class.dlist", "r");
+		if ( classFilterFile == NULL )
 		{
 			ALOG(LOG_VERBOSE,"YWB","OPEN TRACE Class NAME CONFIGURE FILE ERROR\n");
 			return;
 		}
-		ActualClassFilterSize = initialClassHashFilter( class_filter_file, ClassHashFilter );
-		fclose(class_filter_file);
-		Init_flag = true;
-	   // memset ( Opcode_buf, 0, sizeof(Opcode_buf) );
+		ActualClassFilterSize = init_class_hash_filter( classFilterFile, ClassHashFilter );
+		fclose(classFilterFile);
+		InitFlag = true;
+		memset ( opcodeBuf, 0, sizeof(opcodeBuf) );
 	}
 
+	size_t bufIndex = 0;
+
 	FILE* instFile = fopen( "/data/diaos/opcodes.bin", "ab" );
 	if ( instFile == NULL )
 	{
             curMethod->name, curMethod->shorty);
 
 #if defined(LOCCS_DIAOS)
-        if ( ClassShouldBeTraced ( curMethod, ClassHashFilter, ActualClassFilterSize ) )
+        if ( class_should_be_traced ( curMethod, ClassHashFilter, ActualClassFilterSize ) )
         {
         	save_func_call( curMethod, func_file );
         }
             curMethod->name, curMethod->shorty);
 
 #if defined(LOCCS_DIAOS)
-        if ( ClassShouldBeTraced ( curMethod, ClassHashFilter, ActualClassFilterSize ) )
+        if ( class_should_be_traced ( curMethod, ClassHashFilter, ActualClassFilterSize ) )
         {
         	save_func_call( curMethod, func_file );
         }
             methodToCall->shorty);
 
 #if defined(LOCCS_DIAOS)
-        if ( ClassShouldBeTraced ( curMethod, ClassHashFilter, ActualClassFilterSize ) )
+        if ( class_should_be_traced ( curMethod, ClassHashFilter, ActualClassFilterSize ) )
         {
         	save_func_call( methodToCall, func_file );
         }
                 curMethod->name, curMethod->shorty);
 
 #if defined(LOCCS_DIAOS)
-            if ( ClassShouldBeTraced ( curMethod, ClassHashFilter, ActualClassFilterSize ) )
+            if ( class_should_be_traced ( curMethod, ClassHashFilter, ActualClassFilterSize ) )
             {
             	save_func_call( curMethod, func_file );
             }
                   methodToCall->name, methodToCall->shorty);
 
 #if defined(LOCCS_DIAOS)
-            if ( ClassShouldBeTraced ( curMethod, ClassHashFilter, ActualClassFilterSize ) )
+            if ( class_should_be_traced ( curMethod, ClassHashFilter, ActualClassFilterSize ) )
             {
             	save_func_call( methodToCall, func_file );
             }
                 curMethod->clazz->descriptor, curMethod->name,
                 curMethod->shorty);
 #if defined(LOCCS_DIAOS)
-		if ( ClassShouldBeTraced ( curMethod, ClassHashFilter, ActualClassFilterSize ) )
+		if ( class_should_be_traced ( curMethod, ClassHashFilter, ActualClassFilterSize ) )
         {
         	save_func_call( curMethod, func_file );
         }
 
     #if defined(LOCCS_DIAOS)
     // write record finally
-    save_opcode_record( Opcode_buf, Buf_index, instFile );
-	Buf_index = 0;
+    save_opcode_record( opcodeBuf, bufIndex, instFile );
+	bufIndex = 0;
 	fclose( instFile );
 	fclose( func_file );
     #endif
 
 #include <stdint.h>
 
-const static uint32_t instNumInLo = 5;	//shujunliang
-const static uint32_t logContentLen = 128;
-const static uint32_t regNum = 16;
-const static uint32_t fileNameLen = 64;
-const static uint32_t methDescLen = 128;
-const static uint32_t methNameLen = 64;
-
+const static uint32_t InstNum = 5;	//shujunliang
+const static uint32_t RegMaxNum = 16;
+const static uint32_t FileNameMaxLen = 64;
+const static uint32_t ClassNameMaxLen = 128;
+const static uint32_t MethodNameMaxLen = 64;
 
 
 struct LoCCS_opcode
 {
 	uint32_t threadId;
 	uint32_t pc;
-	uint16_t inst[instNumInLo];
-    uint32_t reg[regNum];
-	unsigned char methodDescriptor[methDescLen];
-	unsigned char methodName[methNameLen];
-    char logcontent[logContentLen];
+	uint16_t inst[InstNum];
+    uint32_t reg[RegMaxNum];
+	char methodDescriptor[ClassNameMaxLen];
+	char methodName[MethodNameMaxLen];
 };
 
-
-/*
-void get_filename( char filename[] )
-{
-    FILE *f = fopen("/data/fileName","r");
-    if ( f == NULL )
-    {
-        LOG(LOG_VERBOSE,"YWB","open fileName ERROR\n");
-		return;
-    }
-    
-    fgets ( filename, fileNameLen, f );
-    filename[strlen(filename)-1] = '\0';
-	fclose( f );
-}
-
-
-bool ClassShouldBeTraced (const Method* method)
-{
-    FILE *classNameInFile;
-    char clazzDescName[methDescLen], classConfName[methDescLen] ;
-    snprintf(clazzDescName, methDescLen, "%s", method->clazz->descriptor);
-    //LOG(LOG_VERBOSE, "GOSSIP", "%s\n", clazzDescName);
-    if ((classNameInFile = fopen ("/data/traceClassNameList", "r")) == NULL)
-    {
-        LOG(LOG_VERBOSE,"YWB","OPEN TRACE Class NAME CONFIGURE FILE ERROR\n");
-        return false;
-    }
-    int descLen = strlen (clazzDescName);
-    clazzDescName[descLen+1] = clazzDescName[descLen];
-    clazzDescName[descLen] = '\n';
-    int cmpres = 1;
-    while (!feof(classNameInFile))
-    {
-        fgets(classConfName, methDescLen, classNameInFile);
-        cmpres = strcmp (classConfName, clazzDescName);
-        if ( cmpres == 0)
-            break;
-    }
-    fclose(classNameInFile);
-    //LOG(LOG_VERBOSE, "GOSSIP", "compare result = %d\n", cmpres);
-    if (cmpres == 0)
-        return true;
-    else
-        return false;
-
-}
-*/
-
-
 #endif