mescc: Refactor debug and stub printing.

* lib/libc.c (__mes_debug): New function.  Use it for debug printing
  and in stubs throughout.
This commit is contained in:
Jan Nieuwenhuizen 2018-06-10 14:07:19 +02:00
parent 18194b1ae4
commit c2c3554884
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273
8 changed files with 186 additions and 106 deletions

View file

@ -33,7 +33,10 @@ abs (int x)
int int
chown (char const *file_name, uid_t owner, gid_t group) chown (char const *file_name, uid_t owner, gid_t group)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("chown stub\n"); eputs ("chown stub\n");
stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }
@ -41,9 +44,10 @@ chown (char const *file_name, uid_t owner, gid_t group)
int int
ctime (int x) ctime (int x)
{ {
#if NOISY_TIMES static int stub = 0;
if (__mes_debug () && !stub)
eputs ("ctime stub\n"); eputs ("ctime stub\n");
#endif stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }
@ -84,7 +88,10 @@ fgets (char *s, int count, FILE *stream)
int int
frexp (int x) frexp (int x)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("frexp stub\n"); eputs ("frexp stub\n");
stub = 1;
return 0; return 0;
} }
@ -97,7 +104,10 @@ perror (char const *message)
int int
sigsetmask (int x) sigsetmask (int x)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("sigsetmask stub\n"); eputs ("sigsetmask stub\n");
stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }
@ -153,7 +163,10 @@ strspn (char const *string, char const *skipset)
int int
sys_siglist (int x) sys_siglist (int x)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("sys_siglist stub\n"); eputs ("sys_siglist stub\n");
stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }
@ -161,7 +174,10 @@ sys_siglist (int x)
int int
umask (int x) umask (int x)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("umask stub\n"); eputs ("umask stub\n");
stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }
@ -169,9 +185,9 @@ umask (int x)
int int
utime (int x) utime (int x)
{ {
#if NOISY_TIMES static int stub = 0;
if (__mes_debug () && !stub)
eputs ("utime stub\n"); eputs ("utime stub\n");
#endif
errno = 0; errno = 0;
return 0; return 0;
} }
@ -180,7 +196,10 @@ utime (int x)
int int
fscanf (FILE *stream, char const *template, ...) fscanf (FILE *stream, char const *template, ...)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("fscan stub\n"); eputs ("fscan stub\n");
stub = 1;
return 0; return 0;
} }
@ -197,16 +216,20 @@ bsearch (void const *key, void const *array, size_t count, size_t size, void (*c
bsearch (void const *key, void const *array, size_t count, size_t size, comparison_fn_t compare) bsearch (void const *key, void const *array, size_t count, size_t size, comparison_fn_t compare)
#endif #endif
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("bsearch stub\n"); eputs ("bsearch stub\n");
stub = 1;
return 0; return 0;
} }
struct tm * struct tm *
gmtime (time_t const *time) gmtime (time_t const *time)
{ {
#if NOISY_TIMES static int stub = 0;
if (__mes_debug () && !stub)
eputs ("gmtime stub\n"); eputs ("gmtime stub\n");
#endif stub = 1;
errno = 0; errno = 0;
return localtime (time); return localtime (time);
} }
@ -245,7 +268,10 @@ size_t
strftime (char *s, size_t size, char const *template, strftime (char *s, size_t size, char const *template,
struct tm const *brokentime) struct tm const *brokentime)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("strftime stub\n"); eputs ("strftime stub\n");
stub = 1;
return template; return template;
} }
@ -256,7 +282,10 @@ size_t
mbstowcs (wchar_t *wstring, char const *string, mbstowcs (wchar_t *wstring, char const *string,
size_t size) size_t size)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("mbstowcs stub\n"); eputs ("mbstowcs stub\n");
stub = 1;
strcpy (wstring, string); strcpy (wstring, string);
return strlen (string); return strlen (string);
} }

View file

@ -34,9 +34,10 @@ freopen (char const *file_name, char const *opentype, FILE *stream)
clock_t clock_t
times (struct tms *buffer) times (struct tms *buffer)
{ {
#if NOISY_TIMES static int stub = 0;
if (__mes_debug () && !stub)
eputs ("times stub\n"); eputs ("times stub\n");
#endif stub = 1;
return 0; return 0;
} }
@ -54,14 +55,20 @@ sleep (unsigned int seconds)
double double
__divdi3 (double a, double b) __divdi3 (double a, double b)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("__divdi3 stub\n"); eputs ("__divdi3 stub\n");
stub = 1;
return ((int)a / (int)b); return ((int)a / (int)b);
} }
double double
__moddi3 (double a, double b) __moddi3 (double a, double b)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("__moddi3 stub\n"); eputs ("__moddi3 stub\n");
stub = 1;
return ((int) a %(int)b); return ((int) a %(int)b);
} }
@ -112,7 +119,10 @@ alarm (unsigned int seconds)
struct passwd * struct passwd *
getpwnam (const char *NAME) getpwnam (const char *NAME)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("getpwnam stub\n"); eputs ("getpwnam stub\n");
stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }

View file

@ -21,7 +21,10 @@
int int
__cleanup () __cleanup ()
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("__cleanup stub\n"); eputs ("__cleanup stub\n");
stub = 1;
return 0; return 0;
} }

View file

@ -112,44 +112,49 @@ __ashrdi3 (long long a, int b)
long double long double
__floatundixf (unsigned long long a) __floatundixf (unsigned long long a)
{ {
#if NOISY_FLOATS static int stub = 0;
if (__mes_debug () && !stub)
eputs ("__floatundix stub\n"); eputs ("__floatundix stub\n");
#endif stub = 1;
return 0; return 0;
} }
unsigned long long unsigned long long
__fixunsxfdi (double a1) __fixunsxfdi (double a1)
{ {
#if NOISY_FLOATS static int stub = 0;
if (__mes_debug () && !stub)
eputs ("__fixunsxfdi stub\n"); eputs ("__fixunsxfdi stub\n");
#endif stub = 1;
return 0; return 0;
} }
unsigned long long unsigned long long
__fixdfdi (double a1) __fixdfdi (double a1)
{ {
#if NOISY_FLOATS static int stub = 0;
if (__mes_debug () && !stub)
eputs ("__fixdfdi stub\n"); eputs ("__fixdfdi stub\n");
#endif stub = 1;
return 0; return 0;
} }
unsigned long long unsigned long long
__fixxfdi (double a1) __fixxfdi (double a1)
{ {
#if NOISY_FLOATS static int stub = 0;
if (__mes_debug () && !stub)
eputs ("__fixxfdi stub\n"); eputs ("__fixxfdi stub\n");
#endif stub = 1;
return 0; return 0;
} }
unsigned long long unsigned long long
__fixsfdi (double a1) __fixsfdi (double a1)
{ {
#if NOISY_FLOATS static int stub = 0;
if (__mes_debug () && !stub)
eputs ("__fixsfdi stub\n"); eputs ("__fixsfdi stub\n");
#endif stub = 1;
return 0; return 0;
} }

View file

@ -64,7 +64,7 @@ search_path (char const *file_name)
{ {
static char buf[256]; static char buf[256];
char *path = getenv ("PATH"); char *path = getenv ("PATH");
if (getenv ("MESC_DEBUG")) if (__mes_debug ())
{ {
eputs ("\n search-path: "); eputs (file_name); eputs ("\n"); eputs ("\n search-path: "); eputs (file_name); eputs ("\n");
} }
@ -75,7 +75,7 @@ search_path (char const *file_name)
end = strchr (path, '\0'); end = strchr (path, '\0');
strncpy (buf, path, end - path); strncpy (buf, path, end - path);
buf[end - path] = 0; buf[end - path] = 0;
if (getenv ("MESC_DEBUG")) if (__mes_debug ())
{ {
eputs (" dir: "); eputs (buf); eputs ("\n"); eputs (" dir: "); eputs (buf); eputs ("\n");
} }
@ -84,7 +84,7 @@ search_path (char const *file_name)
strcat (buf, file_name); strcat (buf, file_name);
if (!access (buf, X_OK)) if (!access (buf, X_OK))
{ {
if (getenv ("MESC_DEBUG")) if (__mes_debug ())
{ {
eputs (" found: "); eputs (buf); eputs ("\n"); eputs (" found: "); eputs (buf); eputs ("\n");
} }
@ -105,7 +105,7 @@ execvp (char const *file_name, char *const argv[])
errno = ENOENT; errno = ENOENT;
return -1; return -1;
} }
if (getenv ("MESC_DEBUG")) if (__mes_debug ())
{ {
eputs (" EXEC: "); eputs (file_name); eputs ("\n"); eputs (" EXEC: "); eputs (file_name); eputs ("\n");
int i = 0; int i = 0;
@ -170,7 +170,7 @@ fread (void *data, size_t size, size_t count, FILE *stream)
size_t size_t
fwrite (void const *data, size_t size, size_t count, FILE *stream) fwrite (void const *data, size_t size, size_t count, FILE *stream)
{ {
if (getenv ("MESC_DEBUG")) if (__mes_debug ())
{ {
eputs ("fwrite "); eputs (itoa ((int)stream)); eputs ("fwrite "); eputs (itoa ((int)stream));
eputs (" "); eputs (itoa (size)); eputs ("\n"); eputs (" "); eputs (itoa (size)); eputs ("\n");
@ -180,7 +180,7 @@ fwrite (void const *data, size_t size, size_t count, FILE *stream)
return 0; return 0;
int bytes = write ((int)stream, data, size * count); int bytes = write ((int)stream, data, size * count);
if (getenv ("MESC_DEBUG")) if (__mes_debug ())
{ {
eputs (" => "); eputs (itoa (bytes)); eputs ("\n"); eputs (" => "); eputs (itoa (bytes)); eputs ("\n");
} }
@ -199,7 +199,7 @@ ftell (FILE *stream)
FILE* FILE*
fopen (char const *file_name, char const *opentype) fopen (char const *file_name, char const *opentype)
{ {
if (getenv ("MESC_DEBUG")) if (__mes_debug ())
{ {
eputs ("fopen "); eputs (file_name); eputs ("fopen "); eputs (file_name);
eputs (" "); eputs (opentype); eputs ("\n"); eputs (" "); eputs (opentype); eputs ("\n");
@ -224,7 +224,7 @@ fopen (char const *file_name, char const *opentype)
else else
fd = open (file_name, 0, 0); fd = open (file_name, 0, 0);
if (getenv ("MESC_DEBUG")) if (__mes_debug ())
{ {
eputs (" => fd="); eputs (itoa (fd)); eputs ("\n"); eputs (" => fd="); eputs (itoa (fd)); eputs ("\n");
} }
@ -244,9 +244,10 @@ fseek (FILE *stream, long offset, int whence)
int int
gettimeofday (struct timeval *tv, struct timezone *tz) gettimeofday (struct timeval *tv, struct timezone *tz)
{ {
#if NOISY_TIMES static int stub = 0;
if (__mes_debug () && !stub)
eputs ("gettimeofday stub\n"); eputs ("gettimeofday stub\n");
#endif stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }
@ -254,16 +255,20 @@ gettimeofday (struct timeval *tv, struct timezone *tz)
double double
ldexp (double x, int exp) ldexp (double x, int exp)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("ldexp stub\n"); eputs ("ldexp stub\n");
stub = 1;
return 0; return 0;
} }
struct tm * struct tm *
localtime (time_t const *timep) localtime (time_t const *timep)
{ {
#if NOISY_TIMES static int stub = 0;
if (__mes_debug () && !stub)
eputs ("localtime stub\n"); eputs ("localtime stub\n");
#endif stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }
@ -485,9 +490,10 @@ strstr (char const *haystack, char const *needle)
double double
strtod (char const *string, char **tailptr) strtod (char const *string, char **tailptr)
{ {
#if NOISY_FLOATS static int stub = 0;
if (__mes_debug () && !stub)
eputs ("strtod stub\n"); eputs ("strtod stub\n");
#endif stub = 1;
return 0; return 0;
} }
@ -520,8 +526,6 @@ strtol (char const *string, char **tailptr, int base)
return abtoi (p, base); return abtoi (p, base);
} }
#if 1
long long int long long int
strtoll (char const *string, char **tailptr, int base) strtoll (char const *string, char **tailptr, int base)
{ {
@ -540,39 +544,13 @@ strtoull (char const *string, char **tailptr, int base)
return strtol (string, tailptr, base); return strtol (string, tailptr, base);
} }
#else
long long int
strtoll (char const *string, char **tailptr, int base)
{
eputs ("strtoll stub\n");
return 0;
}
unsigned long
strtoul (char const *string, char **tailptr, int base)
{
eputs ("strtoul stub\n");
return 0;
}
unsigned long long
strtoull (char const *string, char **tailptr, int base)
{
// *endptr = p;
// return abtoi (endptr, base);
eputs ("strtoull stub\n");
return 0;
}
#endif
time_t time_t
time (time_t *tloc) time (time_t *tloc)
{ {
#if NOISY_TIMES static int stub = 0;
if (__mes_debug () && !stub)
eputs ("time stub\n"); eputs ("time stub\n");
#endif stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }

View file

@ -49,6 +49,20 @@ int g_stdin = 0;
void _env (); void _env ();
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
int
__mes_debug ()
{
static int __mes_debug = -1;
if (__mes_debug == -1)
{
char *p = getenv ("MES_DEBUG");
__mes_debug = p ? MAX (itoa (p), 1) : 0;
}
return __mes_debug;
}
int int
getchar () getchar ()
{ {

View file

@ -45,27 +45,39 @@
int int
__cleanup () __cleanup ()
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("__cleanup stub\n"); eputs ("__cleanup stub\n");
stub = 1;
return 0; return 0;
} }
int int
__libc_subinit () __libc_subinit ()
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("__libc_subinit stub\n"); eputs ("__libc_subinit stub\n");
stub = 1;
return 0; return 0;
} }
int int
__syscall_error () __syscall_error ()
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("__syscall_error stub\n"); eputs ("__syscall_error stub\n");
stub = 1;
return 0; return 0;
} }
int int
__fpu_control () __fpu_control ()
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("__fpu_control stub\n"); eputs ("__fpu_control stub\n");
stub = 1;
return 0; return 0;
} }

View file

@ -21,14 +21,20 @@
int int
abort () abort ()
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("abort stub\n"); eputs ("abort stub\n");
stub = 1;
return 0; return 0;
} }
int int
atof (int x) atof (int x)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("atof stub\n"); eputs ("atof stub\n");
stub = 1;
return 0; return 0;
} }
@ -65,7 +71,10 @@ fileno (FILE *stream)
int int
fpurge (FILE *stream) fpurge (FILE *stream)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("fpurge stub\n"); eputs ("fpurge stub\n");
stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }
@ -73,7 +82,10 @@ fpurge (FILE *stream)
size_t size_t
freadahead (FILE *fp) freadahead (FILE *fp)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("freadahead stub\n"); eputs ("freadahead stub\n");
stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }
@ -131,7 +143,10 @@ mktemp (char *template)
int int
pclose (int x) pclose (int x)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("pclose stub\n"); eputs ("pclose stub\n");
stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }
@ -139,7 +154,10 @@ pclose (int x)
int int
popen (int x) popen (int x)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("popen stub\n"); eputs ("popen stub\n");
stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }
@ -153,7 +171,10 @@ rindex (char const *s, int c)
int int
rewind (int x) rewind (int x)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("rewind stub\n"); eputs ("rewind stub\n");
stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }
@ -161,7 +182,10 @@ rewind (int x)
int int
setbuf (int x) setbuf (int x)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("setbuf stub\n"); eputs ("setbuf stub\n");
stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }
@ -169,12 +193,14 @@ setbuf (int x)
int int
system (int x) system (int x)
{ {
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("system stub\n"); eputs ("system stub\n");
stub = 1;
errno = 0; errno = 0;
return 0; return 0;
} }
//char const *const sys_errlist[40] = {
char *sys_errlist[] = { char *sys_errlist[] = {
"error 00", "error 00",
"error 01", "error 01",
@ -222,8 +248,11 @@ int sys_nerr = 39;
char * char *
strerror (int errnum) strerror (int errnum)
{
if (__mes_debug ())
{ {
eputs ("strerror errnum="); eputs (itoa (errnum)); eputs ("\n"); eputs ("strerror errnum="); eputs (itoa (errnum)); eputs ("\n");
}
if (errnum > 0 && errnum <= sys_nerr) if (errnum > 0 && errnum <= sys_nerr)
return sys_errlist[errnum]; return sys_errlist[errnum];
return "sterror: unknown error"; return "sterror: unknown error";