Skip to content

Commit

Permalink
?cxx/Macros.hxx +SUSUWU_SH_VERBOSE flag
Browse files Browse the repository at this point in the history
	for diagnostic messages.
	?`SUSUWU_SH_USE_FILE`, ?`SUSUEU_SH_USE_LINE`, ?`SUSUWU_NOTICE`, ?`SUSUWU_DEBUG`, ?`SUSUWU_DEBUGEXECUTE`: `%s/!defined(NDEBUG)/SUSUWU_SH_VERBOSE/`
	?`SUSUWU_NOTICE_EXECUTE`, ?`SUSUWU_DEBUG_EXECUTE`: indirect update (since those call `SUSUWU_NOTICE`, `SUSUWU_DEBUG`)
	Is followup to 2bb1eb7
	(SUSUWU_NOTICE_EXECUTE), 5c9e099 (+SUSUWU_DEBUGEXECUTE), aab355b (-`SUSUWU_INFO_DEBUGEXECUTE`: INFO's in `--release`, not in `--debug`)

?`README.md` How to use `-DSUSUWU_SH_VERBOSE`.

?`cxx/ClassSys.cxx`: `execves`: Warning messages:
	-`#ifndef NDEBUG`
	`%s/SUSUWU_PRINT(WARNING, /SUSUWU_NOTICE(/` (thus conditional on `SUSUWU_SH_VERBOSE`).
	Is followup to 5e0b366 (+`classSysColoredParamStr`, `clang-tidy` fixes), 7b2eba9 (?`cxx/ClassSys.hxx`:`execvesFork` ... SUSUWU_PRINT(WARNING, ...) if `wstatus` has errors)

?`cxx/VirusAnalysis.cxx`: Diagnostic messages:
	`%s/SUSUWU_PRINT(NOTICE, /SUSUWU_NOTICE(/` (thus conditional on `SUSUWU_SH_VERBOSE`).
	Indent fix

?`cxx/Macros.hxx`, ?`cxx/VirusAnalysis.cxx`, ?`cxx/CnsAssistant.cxx`:
	`%s/_DEBUGEXECUTE/_VERBOSEEXECUTE/`: since those functions got `%s/!defined(NDEBUG)/SUSUWU_SH_VERBOSE/`
	`%s/_VERBOSEEXECUTE/_EXECUTEVERBOSE/`: so that a possible future `SUSUWU_VERBOSE(message)` (`WARN_LEVEL` == `VERBOSE`) won't confuse. ( 5c9e099 should have introduced the original as `SUSUWU_EXECUTEDEBUG`, for this reason. )

?`posts/VirusAnalysis.md` /* Include all this */

All of this is about functions/macros which will improve once #17 (`SUSUWU_SH_RUNTIME_COLORS`, `classSysConsoleHasAnsiColors()`) closes, but does not assist to close.
  • Loading branch information
SwuduSusuwu committed Nov 6, 2024
1 parent aab355b commit 1d92017
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 48 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ Targets: Windows/Linux/Android/OSX/iOS; all C++ compilers, requires some extensi
Usage: `./build.sh` produces `*.o` static libraries (for distribution to others,) plus `a.out` to do unit tests (test harnesses). Allowed flags; `--debug` (default; includes frame-pointers/debug symbols (`-g`), includes `valgrind`-replacement tools (such as `-fsanitize=address`), `--release` (excludes `--debug`, strips frame-pointers/symbols, optimizes with `-O2`), `--mingw` (if on _Linux_, can use with `--release` or `--debug`, produces _Portable Executable_ for _Windows_. If on _Windows_, the default is to produce _Portable Executable_'s for _Windows_)
Optional flags (`vim build.sh` to use):
`-DSUSUWU_SH_PREFER_STDIO` to replace `std::cXXX << ...` with `fprintf(stdXXX, ...)`, default is `!defined(__cplusplus)`.
`-DSUSUWU_SH_VERBOSE` to print diagnostic messages (`SUSUWU_SH_USE_FILE`, `SUSUEU_SH_USE_LINE`, `SUSUWU_NOTICE`, `SUSUWU_DEBUG`, `SUSUWU_DEBUGEXECUTE`, ?`SUSUWU_NOTICE_EXECUTE`, ?`SUSUWU_DEBUG_EXECUTE`: was all `#if !defined(NDEBUG)`, now is `#if SUSUWU_SH_VERBOSE`), default is `!defined(NDEBUG)`.
`-DSUSUWU_SH_SKIP_BRACKETS = true` sets output format to `WARN_LEVEL: message`, default is `false`.
`-DSUSUWU_SH_FILE = true` sets output format to `[__FILE__: WARN_LEVEL: message]`, default is `!defined(NDEBUG)`.
`-DSUSUWU_SH_LINE = true` sets output format to `[__LINE__: WARN_LEVEL: message]`, default is `!defined(NDEBUG)`.
`-DSUSUWU_SH_FILE = true` sets output format to `[__FILE__: WARN_LEVEL: message]`, default is `SUSUWU_SH_VERBOSE`.
`-DSUSUWU_SH_LINE = true` sets output format to `[__LINE__: WARN_LEVEL: message]`, default is `SUSUWU_SH_VERBOSE`.
`-DSUSUWU_SH_FUNC = true` sets output format to `[__func__: WARN_LEVEL: message]`, default is `false`.
`-DSUSUWU_SH_SKIP_COLORS = true` to omit _VT100_ (_ANSI_) colors, default is `defined(SUSUWU_SH_COLORS_UNSUPPORTED)`).
`-DSUSUWU_SH_SKIP_COLORS = false` to force (even if unsupported) _VT100_ (_ANSI_) color use.
Expand Down
6 changes: 3 additions & 3 deletions cxx/AssistantCns.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "ClassResultList.hxx" /* explodeToList listMaxSize listHasValue ResultList ResultListBytecode resultListDumpTo resultListProduceHashes */
#include "ClassSha2.hxx" /* sha2 */
#include "ClassSys.hxx" /* execvex */
#include "Macros.hxx" /* SUSUWU_NOTICE_DEBUGEXECUTE */
#include "Macros.hxx" /* SUSUWU_NOTICE_EXECUTEVERBOSE */
#include <cassert> /* assert */
#include <iostream> /* std::cin std::cout */
#include <ostream> /* std::ostream */
Expand Down Expand Up @@ -47,8 +47,8 @@ const bool assistantCnsTests() {
assert(responsesOrNull.bytecodes.size() == questionsOrNull.bytecodes.size());
assert(4 == questionsOrNull.hashes.size());
assert(3 == responsesOrNull.hashes.size());
SUSUWU_NOTICE_DEBUGEXECUTE(resultListDumpTo(questionsOrNull, std::cout, true, true, false));
SUSUWU_NOTICE_DEBUGEXECUTE((resultListDumpTo(responsesOrNull, std::cout, false, false, false), std::cout << std::endl));
SUSUWU_NOTICE_EXECUTEVERBOSE(resultListDumpTo(questionsOrNull, std::cout, true, true, false));
SUSUWU_NOTICE_EXECUTEVERBOSE((resultListDumpTo(responsesOrNull, std::cout, false, false, false), std::cout << std::endl));
questionsResponsesFromHosts(questionsOrNull, responsesOrNull);
produceAssistantCns(questionsOrNull, responsesOrNull, assistantCns);
return true;
Expand Down
9 changes: 4 additions & 5 deletions cxx/ClassSys.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,12 @@ const int execves(const std::vector<std::string> &argvS, const std::vector<std::
const pid_t pid = execvesFork(argvS, envpS);
int wstatus = 0;
waitpid(pid, &wstatus, 0);
# ifndef NDEBUG /* NOLINTBEGIN(misc-include-cleaner): `clang-tidy` can't detect `sys/wait.h` definitions of macros */
/* NOLINTBEGIN(misc-include-cleaner): `clang-tidy` can't detect `sys/wait.h` definitions of macros */
if(WIFEXITED(wstatus) && 0 != WEXITSTATUS(wstatus)) {
SUSUWU_PRINT(WARNING, "execves(" + classSysColoredParamStr(argvS) + ", " + classSysColoredParamStr(envpS) + ") {if(WIFEXITED(wstatus) && 0 != WEXITSTATUS(wstatus)) {SUSUWU_DEBUG(...);}}: WEXITSTATUS(wstatus) is " SUSUWU_SH_PURPLE + std::to_string(WEXITSTATUS(wstatus)) + SUSUWU_SH_DEFAULT);
SUSUWU_NOTICE("execves(" + classSysColoredParamStr(argvS) + ", " + classSysColoredParamStr(envpS) + ") {if(WIFEXITED(wstatus) && 0 != WEXITSTATUS(wstatus)) {SUSUWU_NOTICE(... \"WEXITSTATUS(wstatus) is " SUSUWU_SH_PURPLE + std::to_string(WEXITSTATUS(wstatus)) + SUSUWU_SH_DEFAULT "\" ...);}}");
} else if(WIFSIGNALED(wstatus)) {
SUSUWU_PRINT(WARNING, "execves(" + classSysColoredParamStr(argvS) + ", " + classSysColoredParamStr(envpS) + ") {if(WIFSIGNALED(wstatus)) {SUSUWU_PRINT(WARNING, ...);}}: WTERMSIG(wstatus) is " SUSUWU_SH_PURPLE + std::to_string(WTERMSIG(wstatus)) + SUSUWU_SH_DEFAULT);
}
# endif /* ndef NDEBUG */ /* NOLINTEND(misc-include-cleaner): `clang-tidy` on */
SUSUWU_NOTICE("execves(" + classSysColoredParamStr(argvS) + ", " + classSysColoredParamStr(envpS) + ") {if(WIFSIGNALED(wstatus)) {SUSUWU_NOTICE(... \"WTERMSIG(wstatus) is " SUSUWU_SH_PURPLE + std::to_string(WTERMSIG(wstatus)) + SUSUWU_SH_DEFAULT "\" ...);}}");
} /* NOLINTEND(misc-include-cleaner): `clang-tidy` on */
return wstatus;
#else /* ndef _POSIX_VERSION */
throw std::runtime_error(SUSUWU_ERRSTR(ERROR, "execves: {#ifndef _POSIX_VERSION /* TODO: convert to win32 */}"));
Expand Down
28 changes: 17 additions & 11 deletions cxx/Macros.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,22 @@ namespace Susuwu { /* namespaces do not affect macros. Is just standard practice
/* `#pragma S` in macro functions is `_Pragma(S)`, but without this wrap gives `error: _Pragma takes a parenthesized string literal`/`expected string literal in pragma message`. Use as `SUSUWU_PRAGMA(message("Message"))` */
#define SUSUWU_PRAGMA(S) _Pragma(#S)

#if !defined(NDEBUG) && !defined(SUSUWU_SH_VERBOSE)
# define SUSUWU_SH_VERBOSE true /* diagnostic logs to `cerr`/`stderr`; can enable on `--release` with `-DSUSUWU_SH_VERBOSE=true` */
#else
# define SUSUWU_SH_VERBOSE false /* can disable on `--debug` with `-DSUSUWU_SH_VERBOSE=false` */
#endif

#if !defined(SUSUWU_SH_SKIP_BRACKETS) || SUSUWU_SH_SKIP_BRACKETS == false /* overridable with `-DSUSUWU_SH_SKIP_BRACKETS true` (which you can set to mimic `g++`/`clang++` syntax for outputs) */
# define IF_SUSUWU_SH_BRACKETS(TRUE, FALSE) TRUE
#else
# define IF_SUSUWU_SH_BRACKETS(TRUE, FALSE) FALSE
#endif

#if (!defined(SUSUWU_SH_FILE) && !defined(NDEBUG)) || SUSUWU_SH_FILE /* overridable with `-DSUSUWU_SH_FILE true/false` */
#if (!defined(SUSUWU_SH_FILE) && SUSUWU_SH_VERBOSE) || SUSUWU_SH_FILE /* overridable with `-DSUSUWU_SH_FILE true/false` */
# define SUSUWU_SH_USE_FILE /* affix `__FILE__ ":"` to `stderr`/`cerr` printout */
#endif
#if (!defined(SUSUWU_SH_LINE) && !defined(NDEBUG)) || SUSUWU_SH_LINE /* overridable with `-DSUSUWU_SH_LINE true/false` */
#if (!defined(SUSUWU_SH_LINE) && SUSUWU_SH_VERBOSE) || SUSUWU_SH_LINE /* overridable with `-DSUSUWU_SH_LINE true/false` */
# define SUSUWU_SH_USE_LINE /* affix `__LINE__ ":"` to `stderr`/`cerr` printout */
#endif
#if defined(SUSUWU_SH_FUNC) && SUSUWU_SH_FUNC /* overridable with `-DSUSUWU_SH_FUNC true/false` */
Expand Down Expand Up @@ -168,15 +174,15 @@ namespace Susuwu { /* namespaces do not affect macros. Is just standard practice
#define SUSUWU_SUCCESS(x) SUSUWU_PRINT(SUCESS, x)

/* Use this to just print debug/notices to `--debug` builds (+ do conditional execution) */
#ifdef NDEBUG
# define SUSUWU_NOTICE(x) (true)/* skip */
# define SUSUWU_DEBUG(x) (true)/* skip */
# define SUSUWU_DEBUGEXECUTE(x) (true)/*skip*/
#else /* !(defined NDEBUG) */
#if SUSUWU_SH_VERBOSE
# define SUSUWU_NOTICE(x) SUSUWU_PRINT(NOTICE, x)
# define SUSUWU_DEBUG(x) SUSUWU_PRINT(DEBUG, x)
# define SUSUWU_DEBUGEXECUTE(x) x
#endif /* !(defined NDEBUG) */
# define SUSUWU_EXECUTEVERBOSE(x) x /* about side-effects; do not assume that `--debug` was used. `--release -DSUSUWU_SH_VERBOSE=true` will execute this. */
#else /* else SUSUWU_SH_VERBOSE */
# define SUSUWU_NOTICE(x) (true)/* skip */
# define SUSUWU_DEBUG(x) (true)/* skip */
# define SUSUWU_EXECUTEVERBOSE(x) (true)/*skip*/ /* about side-effects; do not assume that just `--release` was used. `--debug -DSUSUWU_SH_VERBOSE=false` will skip. */
#endif /* else SUSUWU_SH_VERBOSE */

/* Use this to reduce print (NOTICE/DEBUG is conditional) + (unconditional) execute into single statement */
#define SUSUWU_ERROR_EXECUTE(x) ((SUSUWU_ERROR(#x)), (x))
Expand All @@ -187,8 +193,8 @@ namespace Susuwu { /* namespaces do not affect macros. Is just standard practice
#define SUSUWU_DEBUG_EXECUTE(x) ((SUSUWU_DEBUG(#x)), (x))

/* Use this to reduce (conditional) print + (conditional) execute into single statement */
#define SUSUWU_NOTICE_DEBUGEXECUTE(x) ((SUSUWU_NOTICE(#x)), SUSUWU_DEBUGEXECUTE(x))
#define SUSUWU_DEBUG_DEBUGEXECUTE(x) ((SUSUWU_DEBUG(#x)), SUSUWU_DEBUGEXECUTE(x))
#define SUSUWU_NOTICE_EXECUTEVERBOSE(x) ((SUSUWU_NOTICE(#x)), SUSUWU_EXECUTEVERBOSE(x))
#define SUSUWU_DEBUG_EXECUTEVERBOSE(x) ((SUSUWU_DEBUG(#x)), SUSUWU_EXECUTEVERBOSE(x))

#ifndef __has_feature
# define __has_feature(X) false /* `gcc` "error: missing binary operator before token \"(\"" fix */
Expand Down
12 changes: 6 additions & 6 deletions cxx/VirusAnalysis.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "ClassResultList.hxx" /* size_t listMaxSize listHasValue listProduceSignature listFindSignatureOfValue ResultList resultListDumpTo resultListProduceHashes */
#include "ClassSha2.hxx" /* sha2 */
#include "ClassSys.hxx" /* classSysArgc classSysArgs classSysHasRoot classSysHexStr classSysSetRoot execvex */
#include "Macros.hxx" /* ERROR NOTICE SUSUWU_NOTICE SUSUWU_NOTICE_DEBUGEXECUTE SUSUWU_ERRSTR */
#include "Macros.hxx" /* ERROR NOTICE SUSUWU_ERRSTR SUSUWU_NOTICE SUSUWU_NOTICE_EXECUTEVERBOSE SUSUWU_SH_VERBOSE */
#include "VirusAnalysis.hxx" /* passList abortList *AnalyisCaches */
#include <algorithm> /* std::sort */
#include <cassert> /* assert */
Expand Down Expand Up @@ -61,8 +61,8 @@ const bool virusAnalysisTests() {
resultListProduceHashes(abortOrNull);
produceAbortListSignatures(passOrNull, abortOrNull);
SUSUWU_NOTICE("resultListDumpTo(.list = passOrNull, .os = std::cout, .index = true, .whitespace = true, .pascalValues = false);");
SUSUWU_DEBUGEXECUTE(resultListDumpTo(passOrNull, std::cout, true, true, false));
SUSUWU_NOTICE_DEBUGEXECUTE((resultListDumpTo(/*.list = */abortOrNull, /*.os = */std::cout, /*.index = */false, /*.whitespace = */false, /*.pascalValues = */false), std::cout << std::endl));
SUSUWU_EXECUTEVERBOSE(resultListDumpTo(passOrNull, std::cout, true, true, false));
SUSUWU_NOTICE_EXECUTEVERBOSE((resultListDumpTo(/*.list = */abortOrNull, /*.os = */std::cout, /*.index = */false, /*.whitespace = */false, /*.pascalValues = */false), std::cout << std::endl));
assert(4 == passOrNull.bytecodes.size());
assert(passOrNull.bytecodes.size() - 1 /* 2 instances of "SW", discount dup */ == passOrNull.hashes.size());
assert(0 == passOrNull.signatures.size()); /* NOLINT(readability-container-size-empty); all `.size()`, intuitive */
Expand Down Expand Up @@ -221,7 +221,7 @@ const VirusAnalysisResult hashAnalysis(const PortableExecutable &file, const Res
if(listHasValue(passList.hashes, fileHash)) {
return hashAnalysisCaches[fileHash] = virusAnalysisPass;
} else if(listHasValue(abortList.hashes, fileHash)) {
SUSUWU_PRINT(NOTICE, "hashAnalysis(/*.file =*/ \"" + file.path + "\", /*.fileHash =*/ 0x" + classSysHexStr(fileHash) + ") {return virusAnalysisAbort;} /* due to hash 0x" + classSysHexStr(fileHash) + " (found in `abortList.hashes`). You should treat this as a virus detection if this was not a test. */");
SUSUWU_NOTICE("hashAnalysis(/*.file =*/ \"" + file.path + "\", /*.fileHash =*/ 0x" + classSysHexStr(fileHash) + ") {return virusAnalysisAbort;} /* due to hash 0x" + classSysHexStr(fileHash) + " (found in `abortList.hashes`). You should treat this as a virus detection if this was not a test. */");
return hashAnalysisCaches[fileHash] = virusAnalysisAbort;
} else {
return hashAnalysisCaches[fileHash] = virusAnalysisContinue; /* continue to next tests */
Expand All @@ -236,8 +236,8 @@ const VirusAnalysisResult signatureAnalysis(const PortableExecutable &file, cons
} catch (...) {
auto match = listFindSignatureOfValue(abortList.signatures, file.bytecode);
if(-1 != match.fileOffset) {
SUSUWU_PRINT(NOTICE, "signatureAnalysis(/*.file =*/ \"" + file.path + "\", /*.fileHash =*/ 0x" + classSysHexStr(fileHash) + ") {return virusAnalysisAbort;} /* due to signature 0x" + classSysHexStr(match.signature) + " found at offset=" + std::to_string(match.fileOffset) + ". You should treat this as a virus detection if this was not a test. */");
return signatureAnalysisCaches[fileHash] = virusAnalysisAbort;
SUSUWU_NOTICE("signatureAnalysis(/*.file =*/ \"" + file.path + "\", /*.fileHash =*/ 0x" + classSysHexStr(fileHash) + ") {return virusAnalysisAbort;} /* due to signature 0x" + classSysHexStr(match.signature) + " found at offset=" + std::to_string(match.fileOffset) + ". You should treat this as a virus detection if this was not a test. */");
return signatureAnalysisCaches[fileHash] = virusAnalysisAbort;
}
return signatureAnalysisCaches[fileHash] = virusAnalysisContinue;
}
Expand Down
Loading

0 comments on commit 1d92017

Please sign in to comment.