@@ -71,6 +71,8 @@ bool option_loader_base_set = false;
7171bool option_loader_phdr_set = false ;
7272bool option_loader_static_set = false ;
7373bool option_mem_rebase_set = false ;
74+ bool option_log = true ;
75+ int option_log_color = COLOR_NONE;
7476
7577/*
7678 * Global statistics.
@@ -142,6 +144,33 @@ void debugImpl(const char *msg, ...)
142144 putc (' \n ' , stderr);
143145}
144146
147+ /*
148+ * Print logging information.
149+ */
150+ void logSetColor (int color)
151+ {
152+ option_log_color = color;
153+ if (!option_is_tty || !option_log)
154+ return ;
155+ switch (color)
156+ {
157+ case COLOR_NONE:
158+ fputs (" \33 [0m" , stdout); break ;
159+ case COLOR_RED:
160+ fputs (" \33 [31m" , stdout); break ;
161+ case COLOR_GREEN:
162+ fputs (" \33 [32m" , stdout); break ;
163+ case COLOR_BLUE:
164+ fputs (" \33 [34m" , stdout); break ;
165+ case COLOR_CYAN:
166+ fputs (" \33 [36m" , stdout); break ;
167+ case COLOR_MAGENTA:
168+ fputs (" \33 [35m" , stdout); break ;
169+ case COLOR_YELLOW:
170+ fputs (" \33 [33m" , stdout); break ;
171+ }
172+ }
173+
145174/*
146175 * Parse an integer from an optarg.
147176 */
@@ -236,8 +265,9 @@ static void usage(FILE *stream, const char *progname)
236265 " \t\t Rewrite the binary in one batch rather than incrementally.\n "
237266 " \t\t Default: false (disabled)\n "
238267 " \n "
239- " \t --debug\n "
240- " \t\t Enable debug log messages.\n "
268+ " \t --debug[=false]\n "
269+ " \t\t Enable [disable] debug log messages.\n "
270+ " \t\t Default: false (disabled)\n "
241271 " \n "
242272 " \t --help, -h\n "
243273 " \t\t Print this help message.\n "
@@ -272,6 +302,10 @@ static void usage(FILE *stream, const char *progname)
272302 " \t\t Only relevant for ELF binaries.\n "
273303 " \t\t Default: false (disabled)\n "
274304 " \n "
305+ " \t --log=[false]\n "
306+ " \t\t Enable [disable] log output.\n "
307+ " \t\t Default: true (enabled)\n "
308+ " \n "
275309 " \t --mem-granularity=SIZE\n "
276310 " \t\t Set SIZE to be the granularity used for the physical page\n "
277311 " \t\t grouping memory optimization. Higher values result in\n "
@@ -347,6 +381,7 @@ enum Option
347381 OPTION_LOADER_BASE,
348382 OPTION_LOADER_PHDR,
349383 OPTION_LOADER_STATIC,
384+ OPTION_LOG,
350385 OPTION_MEM_GRANULARITY,
351386 OPTION_MEM_LB,
352387 OPTION_MEM_MAPPING_SIZE,
@@ -392,12 +427,13 @@ void parseOptions(char * const argv[], bool api)
392427 {" Oprologue-size" , req_arg, nullptr , OPTION_OPROLOGUE_SIZE},
393428 {" Oscratch-stack" , opt_arg, nullptr , OPTION_OSCRATCH_STACK},
394429 {" batch" , opt_arg, nullptr , OPTION_BATCH},
395- {" debug" , no_arg, nullptr , OPTION_DEBUG},
430+ {" debug" , opt_arg, nullptr , OPTION_DEBUG},
396431 {" help" , no_arg, nullptr , OPTION_HELP},
397432 {" input" , req_arg, nullptr , OPTION_INPUT},
398433 {" loader-base" , req_arg, nullptr , OPTION_LOADER_BASE},
399434 {" loader-phdr" , req_arg, nullptr , OPTION_LOADER_PHDR},
400435 {" loader-static" , opt_arg, nullptr , OPTION_LOADER_STATIC},
436+ {" log" , opt_arg, nullptr , OPTION_LOG},
401437 {" mem-granularity" , req_arg, nullptr , OPTION_MEM_GRANULARITY},
402438 {" mem-lb" , req_arg, nullptr , OPTION_MEM_LB},
403439 {" mem-mapping-size" , req_arg, nullptr , OPTION_MEM_MAPPING_SIZE},
@@ -441,7 +477,7 @@ void parseOptions(char * const argv[], bool api)
441477 option_batch = parseBoolOptArg (" --batch" , optarg);
442478 break ;
443479 case OPTION_DEBUG:
444- option_debug = true ;
480+ option_debug = parseBoolOptArg ( " --debug " , optarg) ;
445481 break ;
446482 case ' h' :
447483 case OPTION_HELP:
@@ -547,6 +583,9 @@ void parseOptions(char * const argv[], bool api)
547583 option_loader_static =
548584 parseBoolOptArg (" --loader-static" , optarg);
549585 break ;
586+ case OPTION_LOG:
587+ option_log = parseBoolOptArg (" --log" , optarg);
588+ break ;
550589 case OPTION_MEM_GRANULARITY:
551590 option_mem_granularity = parseIntOptArg (" --mem-granularity" ,
552591 optarg, INTPTR_MIN, INTPTR_MAX);
@@ -619,11 +658,6 @@ extern "C"
619658int realMain (int argc, char **argv)
620659{
621660 option_is_tty = (isatty (STDERR_FILENO) != 0 );
622- if (getenv (" E9PATCH_TTY" ) != nullptr )
623- option_is_tty = true ;
624- if (getenv (" E9PATCH_DEBUG" ) != nullptr )
625- option_debug = true ;
626-
627661 parseOptions (argv);
628662
629663 if (option_input != " -" )
@@ -642,7 +676,7 @@ int realMain(int argc, char **argv)
642676 }
643677 if (isatty (STDIN_FILENO))
644678 warning (" reading JSON-RPC from a terminal (this is probably not "
645- " what you want, please use an E9PATCH frontend instead!)" );
679+ " what you want, please use E9Tool instead!)" );
646680
647681 Binary *B = nullptr ;
648682 Message msg;
@@ -696,9 +730,11 @@ int realMain(int argc, char **argv)
696730 mode_str = " Windows PE dynamic link library" ; break ;
697731 }
698732
699- printf (" \n\n -----------------------------------------------\n " );
733+ log (COLOR_NONE, ' \n ' );
734+ printf (" -----------------------------------------------\n " );
735+ printf (" mode = %s\n " , mode_str);
700736 printf (" input_binary = %s\n " , B->filename );
701- printf (" input_mode = %s\n " , mode_str );
737+ printf (" output_binary = %s\n " , B-> output );
702738 printf (" num_patched = %zu / %zu (%s%s%%)\n " ,
703739 stat_num_patched, stat_num_total, (approx? " ~" : " " ),
704740 percent);
@@ -749,7 +785,7 @@ int realMain(int argc, char **argv)
749785 " \t (1) raising the limit, e.g.:\n "
750786 " \t sudo sysctl -w vm.max_map_count=%zu\n "
751787 " \t (2) rewriting the binary with a larger mapping size\n "
752- " \t (see the `--mem-mapping- size' option)." ,
788+ " \t (see the `--size' option)." ,
753789 stat_num_virtual_mappings,
754790 ((ssize_t )stat_num_virtual_mappings >= MAX_MAPPINGS?
755791 " exceeds" : " may exceed" ),
@@ -838,3 +874,19 @@ asm (
838874 " .Lskip:\n "
839875);
840876
877+ #ifndef NDEBUG
878+ /*
879+ * ASAN options.
880+ */
881+ extern " C"
882+ {
883+ /*
884+ * E9Patch deliberately leaks memory & leaves it to the OS to clean-up.
885+ */
886+ const char *__asan_default_options ()
887+ {
888+ return " detect_leaks=0" ;
889+ }
890+ }
891+ #endif
892+
0 commit comments