Skip to content

Commit

Permalink
Allow redirection of __dprintf output from console
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Dec 19, 2024
1 parent 69d173e commit 9890f0e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion libc/malloc/amalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ static NPTR allocp; /*search ptr*/
static NPTR alloct; /*arena top*/
static NPTR allocx; /*for benefit of realloc*/

static int debug_level = DEBUG;
#if DEBUG
#define ASSERT(p) if(!(p))malloc_assert_fail(#p,__LINE__);else {}
#define debug(...) do { if (debug_level > 1) __dprintf(__VA_ARGS__); } while (0)
#define debug2(...) do { if (debug_level > 2) __dprintf(__VA_ARGS__); } while (0)
static int debug_level = DEBUG;
static void malloc_assert_fail(char *s, int);
static void malloc_show_heap(void);
static int malloc_check_heap(void);
Expand Down Expand Up @@ -335,6 +335,7 @@ malloc_show_heap(void)
unsigned int size, alloc = 0, free = 0;
static unsigned int maxalloc;

if (!debug_level) return;
debug2("--- heap size ---\n");
malloc_check_heap();
for(p = (NPTR)&allocs[0]; clearbusy(next(p)) > p; p=clearbusy(next(p))) {
Expand Down
8 changes: 6 additions & 2 deletions libc/malloc/dprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ int __dprintf(const char *fmt, ...)
char b[80];
static int fd = -1;

if (fd < 0)
fd = open(_PATH_CONSOLE, O_WRONLY);
if (fd < 0) {
if (!isatty(STDERR_FILENO))
fd = STDERR_FILENO;
else
fd = open(_PATH_CONSOLE, O_WRONLY);
}
va_start(va, fmt);
for (n = 0; *fmt; fmt++) {
if (*fmt == '%') {
Expand Down
3 changes: 2 additions & 1 deletion libc/malloc/v7malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ static NPTR allocp; /*search ptr*/
static NPTR alloct; /*arena top*/
static NPTR allocx; /*for benefit of realloc*/

static int debug_level = DEBUG;
#if DEBUG
#define ASSERT(p) if(!(p))malloc_assert_fail(#p,__LINE__);else {}
#define debug(...) do { if (debug_level > 1) __dprintf(__VA_ARGS__); } while (0)
#define debug2(...) do { if (debug_level > 2) __dprintf(__VA_ARGS__); } while (0)
static int debug_level = DEBUG;
static void malloc_assert_fail(char *s, int);
static void malloc_show_heap(void);
static int malloc_check_heap(void);
Expand Down Expand Up @@ -308,6 +308,7 @@ malloc_show_heap(void)
unsigned int size, alloc = 0, free = 0;
static unsigned int maxalloc;

if (!debug_level) return;
debug2("--- heap size ---\n");
malloc_check_heap();
for(p = (NPTR)&allocs[0]; clearbusy(next(p)) > p; p=clearbusy(next(p))) {
Expand Down

0 comments on commit 9890f0e

Please sign in to comment.