Browse code

bytecode2llvm: convert errs() to cli_{err,warn,dbg} so they are not lost.

Török Edvin authored on 2010/11/10 20:53:18
Showing 1 changed files
... ...
@@ -330,10 +330,8 @@ public:
330 330
     {
331 331
 	if (!cli_debug_flag)
332 332
 	    return;
333
-	errs() << "bytecode JIT: emitted function " << F.getName() << 
334
-	    " of " << Size << " bytes at 0x";
335
-	errs().write_hex((uintptr_t)Code);
336
-	errs() << "\n";
333
+	cli_dbgmsg_internal("[Bytecode JIT]: emitted function %s of %ld bytes at %p\n",
334
+			    F.getNameStr().c_str(), (long)Size, Code);
337 335
     }
338 336
 };
339 337
 
... ...
@@ -781,9 +779,14 @@ private:
781 781
 		isa<PointerType>(Ty))
782 782
 		V = Builder.CreateBitCast(V, Ty);
783 783
 	    if (V->getType() != Ty) {
784
-		errs() << operand << " ";
785
-		V->dump();
786
-		Ty->dump();
784
+		if (cli_debug_flag) {
785
+		    std::string str;
786
+		    raw_string_ostream ostr(str);
787
+		    ostr << operand << " " ;
788
+		    V->print(ostr);
789
+		    Ty->print(ostr);
790
+		    cli_dbgmsg_internal("[Bytecode JIT]: %s\n", ostr.str().c_str());
791
+		}
787 792
 		llvm_report_error("(libclamav) Type mismatch converting operand");
788 793
 	    }
789 794
 	    return V;
... ...
@@ -944,17 +947,25 @@ public:
944 944
     Value* createGEP(Value *Base, const Type *ETy, InputIterator Start, InputIterator End) {
945 945
 	const Type *Ty = GetElementPtrInst::getIndexedType(Base->getType(), Start, End);
946 946
 	if (!Ty || (ETy && (Ty != ETy && (!isa<IntegerType>(Ty) || !isa<IntegerType>(ETy))))) {
947
-	    errs() << MODULE << "Wrong indices for GEP opcode: "
948
-		<< " expected type: " << *ETy;
949
-	    if (Ty)
950
-		errs() << " actual type: " << *Ty;
951
-	    errs() << " base: " << *Base << ";";
952
-	    Base->getType()->dump();
953
-	    errs() << "\n indices: ";
954
-	    for (InputIterator I=Start; I != End; I++) {
955
-		errs() << **I << ", ";
947
+	    if (cli_debug_flag) {
948
+		std::string str;
949
+		raw_string_ostream ostr(str);
950
+
951
+		ostr << "Wrong indices for GEP opcode: "
952
+		    << " expected type: " << *ETy;
953
+		if (Ty)
954
+		    ostr << " actual type: " << *Ty;
955
+		ostr << " base: " << *Base << ";";
956
+		Base->getType()->print(ostr);
957
+		ostr << "\n indices: ";
958
+		for (InputIterator I=Start; I != End; I++) {
959
+		    ostr << **I << ", ";
960
+		}
961
+		ostr << "\n";
962
+		cli_dbgmsg_internal("[Bytecode JIT]: %s\n", ostr.str().c_str());
963
+	    } else {
964
+		cli_warnmsg("[Bytecode JIT]: Wrong indices for GEP opcode\n");
956 965
 	    }
957
-	    errs() << "\n";
958 966
 	    return 0;
959 967
 	}
960 968
 	return Builder.CreateGEP(Base, Start, End);
... ...
@@ -966,7 +977,8 @@ public:
966 966
 	const Type *ETy = cast<PointerType>(cast<PointerType>(Values[dest]->getType())->getElementType())->getElementType();
967 967
 	Value *V = createGEP(Base, ETy, Start, End);
968 968
 	if (!V) {
969
-	    errs() << "@ " << dest << "\n";
969
+	    if (cli_debug_flag)
970
+		cli_dbgmsg_internal("[Bytecode JIT] @%d\n", dest);
970 971
 	    return false;
971 972
 	}
972 973
 	V = Builder.CreateBitCast(V, PointerType::getUnqual(ETy));
... ...
@@ -1178,8 +1190,13 @@ public:
1178 1178
 		    };
1179 1179
 		    globals[i] = createGEP(SpecialGV, 0, C, C+1);
1180 1180
 		    if (!globals[i]) {
1181
-			errs() << i << ":" << g << ":" << bc->globals[i][0] <<"\n";
1182
-			Ty->dump();
1181
+			if (cli_debug_flag) {
1182
+			    std::string str;
1183
+			    raw_string_ostream ostr(str);
1184
+			    ostr << i << ":" << g << ":" << bc->globals[i][0] <<"\n";
1185
+			    Ty->print(ostr);
1186
+			    cli_dbgmsg_internal("[Bytecode JIT]: %s\n", ostr.str().c_str());
1187
+			}
1183 1188
 			llvm_report_error("(libclamav) unable to create fake global");
1184 1189
 		    }
1185 1190
 		    globals[i] = Builder.CreateBitCast(globals[i], Ty);
... ...
@@ -1335,7 +1352,7 @@ public:
1335 1335
 			    BasicBlock *True = BB[inst->u.branch.br_true];
1336 1336
 			    BasicBlock *False = BB[inst->u.branch.br_false];
1337 1337
 			    if (Cond->getType() != Type::getInt1Ty(Context)) {
1338
-				errs() << MODULE << "type mismatch in condition\n";
1338
+				cli_warnmsg("[Bytecode JIT]: type mismatch in condition");
1339 1339
 				return 0;
1340 1340
 			    }
1341 1341
 			    Builder.CreateCondBr(Cond, True, False);
... ...
@@ -1593,17 +1610,22 @@ public:
1593 1593
 				break;
1594 1594
 			    }
1595 1595
 			default:
1596
-			    errs() << MODULE << "JIT doesn't implement opcode " <<
1597
-				inst->opcode << " yet!\n";
1596
+			    cli_warnmsg("[Bytecode JIT]: JIT doesn't implement opcode %d yet!\n",
1597
+					inst->opcode);
1598 1598
 			    return 0;
1599 1599
 		    }
1600 1600
 		}
1601 1601
 	    }
1602 1602
 
1603 1603
 	    if (verifyFunction(*F, PrintMessageAction)) {
1604
-		errs() << MODULE << "Verification failed\n";
1605
-		F->dump();
1606 1604
 		// verification failed
1605
+		cli_warnmsg("[Bytecode JIT]: Verification failed\n");
1606
+		if (cli_debug_flag) {
1607
+		    std::string str;
1608
+		    raw_string_ostream ostr(str);
1609
+		    F->print(ostr);
1610
+		    cli_dbgmsg_internal("[Bytecode JIT]: %s\n", ostr.str().c_str());
1611
+		}
1607 1612
 		return 0;
1608 1613
 	    }
1609 1614
 	    delete [] Values;
... ...
@@ -1637,7 +1659,7 @@ public:
1637 1637
 
1638 1638
 	// If prototype matches, add to callable functions
1639 1639
 	if (Functions[0]->getFunctionType() != Callable) {
1640
-	    errs() << "Wrong prototype for function 0 in bytecode " << bc->id << "\n";
1640
+	    cli_warnmsg("[Bytecode JIT]: Wrong prototype for function 0 in bytecode %d\n",  bc->id);
1641 1641
 	    return 0;
1642 1642
 	}
1643 1643
 	// All functions have the Fast calling convention, however
... ...
@@ -1837,10 +1859,10 @@ int cli_vm_execute_jit(const struct cli_all_bc *bcs, struct cli_bc_ctx *ctx,
1837 1837
     // if needed.
1838 1838
     void *code = bcs->engine->compiledFunctions[func];
1839 1839
     if (!code) {
1840
-	errs() << MODULE << "Unable to find compiled function\n";
1840
+	cli_warnmsg("[Bytecode JIT]: Unable to find compiled function\n");
1841 1841
 	if (func->numArgs)
1842
-	    errs() << MODULE << "Function has "
1843
-		<< (unsigned)func->numArgs << " arguments, it must have 0 to be called as entrypoint\n";
1842
+	    cli_warnmsg("[Bytecode JIT] Function has %d arguments, it must have 0 to be called as entrypoint\n",
1843
+			func->numArgs);
1844 1844
 	return CL_EBYTECODE;
1845 1845
     }
1846 1846
     gettimeofday(&tv0, NULL);
... ...
@@ -1863,9 +1885,8 @@ int cli_vm_execute_jit(const struct cli_all_bc *bcs, struct cli_bc_ctx *ctx,
1863 1863
 	/* only spawn if timeout is set.
1864 1864
 	 * we don't set timeout for selfcheck (see bb #2235) */
1865 1865
 	if ((ret = pthread_create(&thread, NULL, bytecode_watchdog, &w))) {
1866
-	    errs() << "Bytecode: failed to create new thread!";
1867
-	    errs() << cli_strerror(ret, buf, sizeof(buf));
1868
-	    errs() << "\n";
1866
+	    cli_warnmsg("[Bytecode JIT]: Bytecode: failed to create new thread :%s!\n",
1867
+			cli_strerror(ret, buf, sizeof(buf)));
1869 1868
 	    return CL_EBYTECODE;
1870 1869
 	}
1871 1870
     }
... ...
@@ -1880,10 +1901,12 @@ int cli_vm_execute_jit(const struct cli_all_bc *bcs, struct cli_bc_ctx *ctx,
1880 1880
     }
1881 1881
 
1882 1882
     if (cli_debug_flag) {
1883
+	long diff;
1883 1884
 	gettimeofday(&tv1, NULL);
1884 1885
 	tv1.tv_sec -= tv0.tv_sec;
1885 1886
 	tv1.tv_usec -= tv0.tv_usec;
1886
-	errs() << "bytecode finished in " << (tv1.tv_sec*1000000 + tv1.tv_usec) << "us\n";
1887
+	diff = tv1.tv_sec*1000000 + tv1.tv_usec;
1888
+	cli_dbgmsg_internal("bytecode finished in %ld us\n", diff);
1887 1889
     }
1888 1890
     return ctx->timeout ? CL_ETIMEOUT : ret;
1889 1891
 }
... ...
@@ -1937,9 +1960,10 @@ int cli_bytecode_prepare_jit(struct cli_all_bc *bcs)
1937 1937
 	ExecutionEngine *EE = bcs->engine->EE = builder.create();
1938 1938
 	if (!EE) {
1939 1939
 	    if (!ErrorMsg.empty())
1940
-		errs() << MODULE << "error creating execution engine: " << ErrorMsg << "\n";
1940
+		cli_errmsg("[Bytecode JIT]: error creating execution engine: %s\n",
1941
+			   ErrorMsg.c_str());
1941 1942
 	    else
1942
-		errs() << MODULE << "JIT not registered?\n";
1943
+		cli_errmsg("[Bytecode JIT]: JIT not registered?\n");
1943 1944
 	    return CL_EBYTECODE;
1944 1945
 	}
1945 1946
 	bcs->engine->Listener  = new NotifyListener();
... ...
@@ -2044,7 +2068,7 @@ int cli_bytecode_prepare_jit(struct cli_all_bc *bcs)
2044 2044
 				OurFPM, OurFPMUnsigned, apiFuncs, apiMap);
2045 2045
 	    Function *F = Codegen.generate();
2046 2046
 	    if (!F) {
2047
-		errs() << MODULE << "JIT codegen failed\n";
2047
+		cli_errmsg("[Bytecode JIT]: JIT codegen failed\n");
2048 2048
 		return CL_EBYTECODE;
2049 2049
 	    }
2050 2050
 	    Functions[i] = F;
... ...
@@ -2099,10 +2123,11 @@ int cli_bytecode_prepare_jit(struct cli_all_bc *bcs)
2099 2099
     }
2100 2100
     return CL_SUCCESS;
2101 2101
   } catch (std::bad_alloc &badalloc) {
2102
-      errs() << MODULE << badalloc.what() << "\n";
2102
+      cli_errmsg("[Bytecode JIT]: bad_alloc: %s\n",
2103
+		 badalloc.what());
2103 2104
       return CL_EMEM;
2104 2105
   } catch (...) {
2105
-      errs() << MODULE << "Unexpected unknown exception occurred.\n";
2106
+      cli_errmsg("[Bytecode JIT]: Unexpected unknown exception occured\n");
2106 2107
       return CL_EBYTECODE;
2107 2108
   }
2108 2109
 }
... ...
@@ -2111,7 +2136,7 @@ int bytecode_init(void)
2111 2111
 {
2112 2112
     // If already initialized return
2113 2113
     if (llvm_is_multithreaded()) {
2114
-	errs() << "bytecode_init: already initialized";
2114
+	cli_warnmsg("bytecode_init: already initialized");
2115 2115
 	return CL_EARG;
2116 2116
     }
2117 2117
     llvm_install_error_handler(llvm_error_handler);
... ...
@@ -2276,9 +2301,11 @@ void cli_printcxxver()
2276 2276
 namespace ClamBCModule {
2277 2277
 void stop(const char *msg, llvm::Function* F, llvm::Instruction* I)
2278 2278
 {
2279
-    if (F && F->hasName())
2280
-	llvm::errs() << "in function " << F->getName() << ": ";
2281
-    llvm::errs() << msg << "\n";
2279
+    if (F && F->hasName()) {
2280
+	cli_warnmsg("[Bytecode JIT] in function %s: %s", F->getNameStr().c_str(), msg);
2281
+    } else {
2282
+	cli_warnmsg("[Bytecode JIT] %s", msg);
2283
+    }
2282 2284
 }
2283 2285
 }
2284 2286