xref: /aosp_15_r20/tools/dexter/slicer/common.cc (revision f0dffb02cdb5c647d21204e89a92a1ffae2dad87)
1*f0dffb02SXin Li /*
2*f0dffb02SXin Li  * Copyright (C) 2017 The Android Open Source Project
3*f0dffb02SXin Li  *
4*f0dffb02SXin Li  * Licensed under the Apache License, Version 2.0 (the "License");
5*f0dffb02SXin Li  * you may not use this file except in compliance with the License.
6*f0dffb02SXin Li  * You may obtain a copy of the License at
7*f0dffb02SXin Li  *
8*f0dffb02SXin Li  *      http://www.apache.org/licenses/LICENSE-2.0
9*f0dffb02SXin Li  *
10*f0dffb02SXin Li  * Unless required by applicable law or agreed to in writing, software
11*f0dffb02SXin Li  * distributed under the License is distributed on an "AS IS" BASIS,
12*f0dffb02SXin Li  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*f0dffb02SXin Li  * See the License for the specific language governing permissions and
14*f0dffb02SXin Li  * limitations under the License.
15*f0dffb02SXin Li  */
16*f0dffb02SXin Li 
17*f0dffb02SXin Li #include "slicer/common.h"
18*f0dffb02SXin Li 
19*f0dffb02SXin Li #include <stdio.h>
20*f0dffb02SXin Li #include <stdlib.h>
21*f0dffb02SXin Li 
22*f0dffb02SXin Li #include <iostream>
23*f0dffb02SXin Li #include <sstream>
24*f0dffb02SXin Li #include <set>
25*f0dffb02SXin Li #include <utility>
26*f0dffb02SXin Li 
27*f0dffb02SXin Li namespace slicer {
28*f0dffb02SXin Li 
log_(const std::string & msg)29*f0dffb02SXin Li static void log_(const std::string& msg) {
30*f0dffb02SXin Li   printf("%s", msg.c_str());
31*f0dffb02SXin Li   fflush(stdout);
32*f0dffb02SXin Li }
33*f0dffb02SXin Li 
34*f0dffb02SXin Li static logger_type log = log_;
35*f0dffb02SXin Li 
set_logger(logger_type new_logger)36*f0dffb02SXin Li void set_logger(logger_type new_logger) {
37*f0dffb02SXin Li   log = new_logger;
38*f0dffb02SXin Li }
39*f0dffb02SXin Li 
40*f0dffb02SXin Li // Helper for the default SLICER_CHECK() policy
_checkFailed(const char * expr,int line,const char * file)41*f0dffb02SXin Li void _checkFailed(const char* expr, int line, const char* file) {
42*f0dffb02SXin Li   std::stringstream ss;
43*f0dffb02SXin Li   ss << std::endl << "SLICER_CHECK failed [";
44*f0dffb02SXin Li   ss << expr << "] at " << file << ":" << line;
45*f0dffb02SXin Li   ss << std::endl << std::endl;
46*f0dffb02SXin Li   log(ss.str());
47*f0dffb02SXin Li   abort();
48*f0dffb02SXin Li }
49*f0dffb02SXin Li 
_checkFailedOp(const void * lhs,const void * rhs,const char * op,const char * suffix,int line,const char * file)50*f0dffb02SXin Li void _checkFailedOp(const void* lhs, const void* rhs, const char* op, const char* suffix, int line,
51*f0dffb02SXin Li                     const char* file) {
52*f0dffb02SXin Li   std::stringstream ss;
53*f0dffb02SXin Li   ss << std::endl << "SLICER_CHECK_" << suffix << " failed [";
54*f0dffb02SXin Li   ss << lhs << " " << op << " " << rhs;
55*f0dffb02SXin Li   ss << "] at " << file << ":" << line;
56*f0dffb02SXin Li   log(ss.str());
57*f0dffb02SXin Li   abort();
58*f0dffb02SXin Li }
59*f0dffb02SXin Li 
_checkFailedOp(uint32_t lhs,uint32_t rhs,const char * op,const char * suffix,int line,const char * file)60*f0dffb02SXin Li void _checkFailedOp(uint32_t lhs, uint32_t rhs, const char* op, const char* suffix, int line,
61*f0dffb02SXin Li                     const char* file) {
62*f0dffb02SXin Li   std::stringstream ss;
63*f0dffb02SXin Li   ss << std::endl << "SLICER_CHECK_" << suffix << " failed [";
64*f0dffb02SXin Li   ss << lhs << " " << op << " " << rhs;
65*f0dffb02SXin Li   ss << "] at " << file << ":" << line;
66*f0dffb02SXin Li   log(ss.str());
67*f0dffb02SXin Li   abort();
68*f0dffb02SXin Li }
69*f0dffb02SXin Li 
70*f0dffb02SXin Li // keep track of the failures we already saw to avoid spamming with duplicates
71*f0dffb02SXin Li thread_local std::set<std::pair<int, const char*>> weak_failures;
72*f0dffb02SXin Li 
73*f0dffb02SXin Li // Helper for the default SLICER_WEAK_CHECK() policy
74*f0dffb02SXin Li //
75*f0dffb02SXin Li // TODO: implement a modal switch (abort/continue)
76*f0dffb02SXin Li //
_weakCheckFailed(const char * expr,int line,const char * file)77*f0dffb02SXin Li void _weakCheckFailed(const char* expr, int line, const char* file) {
78*f0dffb02SXin Li   auto failure_id = std::make_pair(line, file);
79*f0dffb02SXin Li   if (weak_failures.find(failure_id) == weak_failures.end()) {
80*f0dffb02SXin Li     std::stringstream ss;
81*f0dffb02SXin Li     ss << std::endl << "SLICER_WEAK_CHECK failed [";
82*f0dffb02SXin Li     ss << expr << "] at " << file << ":";
83*f0dffb02SXin Li     ss << line << std::endl << std::endl;
84*f0dffb02SXin Li     log(ss.str());
85*f0dffb02SXin Li     weak_failures.insert(failure_id);
86*f0dffb02SXin Li   }
87*f0dffb02SXin Li }
88*f0dffb02SXin Li 
89*f0dffb02SXin Li // Prints a formatted message and aborts
_fatal(const std::string & msg)90*f0dffb02SXin Li void _fatal(const std::string& msg) {
91*f0dffb02SXin Li   log("SLICER_FATAL: " + msg);
92*f0dffb02SXin Li   abort();
93*f0dffb02SXin Li }
94*f0dffb02SXin Li 
95*f0dffb02SXin Li } // namespace slicer
96*f0dffb02SXin Li 
97