Skip to content

Commit

Permalink
core/debug: add levels to debug output [DLOGx()]
Browse files Browse the repository at this point in the history
  • Loading branch information
nil0x42 committed Sep 4, 2020
1 parent ad127b5 commit d58379f
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 83 deletions.
31 changes: 13 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
TARGET = duplicut

SHELL = /bin/sh

# debug level
level = 1

CFLAGS = -Iinclude -Wall -Wextra \
-Wdisabled-optimization -Winline \
-Wdouble-promotion -Wunknown-pragmas \
-Wno-unknown-warning-option \
-mtune=native -ffast-math
LDFLAGS = -lm -pthread
RELEASEFLAGS = -O2 -D NDEBUG
DEBUGFLAGS = -O0 -D DEBUG -std=gnu99 -g3
DEBUGFLAGS = -O0 -D DEBUG=$(level) -std=gnu99 -g3

SOURCES = main.c thpool.c file.c chunk.c line.c tag_duplicates.c \
optparse.c config.c error.c memstate.c meminfo.c bytesize.c \
Expand All @@ -24,19 +25,21 @@ BINDIR = $(PREFIX)/bin


all: $(TARGET)
release: $(TARGET)
re: $(TARGET)

$(TARGET): CFLAGS += $(DEBUGFLAGS)
$(TARGET): $(OBJECTS) $(COMMON)
debug: CFLAGS += $(DEBUGFLAGS)
debug: distclean $(OBJECTS) $(COMMON)
-ctags -R .
$(CC) $(FLAGS) $(CFLAGS) $(DEBUGFLAGS) -o $(TARGET) $(OBJECTS) $(LDFLAGS)

release: CFLAGS += $(RELEASEFLAGS)
release: fclean $(OBJECTS) $(COMMON)
$(TARGET): CFLAGS += $(RELEASEFLAGS)
$(TARGET): distclean $(OBJECTS) $(COMMON)
$(CC) $(FLAGS) $(CFLAGS) $(RELEASEFLAGS) -o $(TARGET) $(OBJECTS) $(LDFLAGS)
strip -s $(TARGET)

profile: CFLAGS += -pg
profile: fclean $(TARGET)
profile: distclean $(TARGET)

objects/%.o: src/%.c
mkdir -p `dirname $@`
Expand All @@ -45,9 +48,6 @@ objects/%.o: src/%.c
install: release
install -D $(TARGET) $(BINDIR)/$(TARGET)

install-strip: release
install -D -s $(TARGET) $(BINDIR)/$(TARGET)

uninstall:
rm $(BINDIR)/$(TARGET)

Expand All @@ -62,11 +62,6 @@ clean:
distclean: clean
-rm -f $(TARGET)

fclean: distclean

re: fclean all

.PHONY: all release profile \
clean distclean \
install install-strip uninstall \
test
.PHONY: all release profile clean distclean \
install uninstall test
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ OOM** even on huge wordlists

#### Quick start:
```sh
make release
git clone https://github.com/nil0x42/duplicut
cd duplicut/ && make
./duplicut <WORDLIST_WITH_DUPLICATES> -o <NEW_CLEAN_WORDLIST>
```

Expand Down Expand Up @@ -78,11 +79,23 @@ Example: duplicut wordlist.txt -o new-wordlist.txt
- Any line longer than 255 chars is ignored.
- Heavily tested on Linux x64, mostly untested on other platforms.

---------------------------------------------------------------------
#### Throubleshotting ####

If you find some bug, or something doesn't work as expected,
compile duplicut in debug mode and post an [issue] with debug output.
```
# debug level can be 1 to 4
make debug level=1
./duplicut [OPTIONS] 2>&1 | tee /tmp/duplicut-debug/log
```


[img-1-comparison]: data/img/1-comparison.png
[img-2-line-struct]: data/img/2-line-struct.png
[img-3-chunked-processing]: data/img/3-chunked-processing.png

[issue]: https://github.com/nil0x42/duplicut/issues
[tagged-pointer]: https://en.wikipedia.org/wiki/Tagged_pointer

[latex-n]: http://www.sciweavers.org/tex2img.php?fs=15&eq=n
Expand Down
30 changes: 25 additions & 5 deletions include/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# define DEBUG_H

/*
* DLOG("debug message")
* Define DLOG macros
*/
# ifdef DEBUG
# if DEBUG >= 1
# include <unistd.h>
# include <stdio.h>
# include <stdarg.h>
Expand All @@ -13,7 +13,7 @@
static void _dlog(int level, const char *file, int line, const char *fmt, ...)
{
char out[1024] = {0};
int i;
int i = 0;

time_t now;
struct tm* tm_info;
Expand Down Expand Up @@ -44,10 +44,30 @@ static void _dlog(int level, const char *file, int line, const char *fmt, ...)

write(STDERR_FILENO, out, i);
}
# endif

# if DEBUG >= 1
# define DLOG1(...) (_dlog(1, __FILE__, __LINE__, __VA_ARGS__))
# else
# define DLOG1(...)
# endif

# if DEBUG >= 2
# define DLOG2(...) (_dlog(2, __FILE__, __LINE__, __VA_ARGS__))
# else
# define DLOG2(...)
# endif

# if DEBUG >= 3
# define DLOG3(...) (_dlog(3, __FILE__, __LINE__, __VA_ARGS__))
# else
# define DLOG3(...)
# endif

# define DLOG(...) (_dlog(1, __FILE__, __LINE__, __VA_ARGS__))
# if DEBUG >= 4
# define DLOG4(...) (_dlog(4, __FILE__, __LINE__, __VA_ARGS__))
# else
# define DLOG(...)
# define DLOG4(...)
# endif

#endif
24 changes: 12 additions & 12 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,16 @@ void config(void)
config_hmap_size(g_file, &memstate);
config_chunk_size(g_file);

DLOG("");
DLOG("---------- g_conf ------------");
DLOG("g_conf.infile_name: %s", g_conf.infile_name);
DLOG("g_conf.outfile_name: %s", g_conf.outfile_name);
DLOG("g_conf.threads: %u", g_conf.threads);
DLOG("g_conf.line_max_size: %u", g_conf.line_max_size);
DLOG("g_conf.hmap_size: %ld", g_conf.hmap_size);
DLOG("g_conf.chunk_size: %ld", g_conf.chunk_size);
DLOG("g_conf.filter_printable: %d", g_conf.filter_printable);
DLOG("g_conf.memlimit: %ld", g_conf.memlimit);
DLOG("------------------------------");
DLOG("");
DLOG1("");
DLOG1("---------- g_conf ------------");
DLOG1("g_conf.infile_name: %s", g_conf.infile_name);
DLOG1("g_conf.outfile_name: %s", g_conf.outfile_name);
DLOG1("g_conf.threads: %u", g_conf.threads);
DLOG1("g_conf.line_max_size: %u", g_conf.line_max_size);
DLOG1("g_conf.hmap_size: %ld", g_conf.hmap_size);
DLOG1("g_conf.chunk_size: %ld", g_conf.chunk_size);
DLOG1("g_conf.filter_printable: %d", g_conf.filter_printable);
DLOG1("g_conf.memlimit: %ld", g_conf.memlimit);
DLOG1("------------------------------");
DLOG1("");
}
16 changes: 8 additions & 8 deletions src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ void init_file(const char *infile_name, const char *outfile_name)
file->orig_size = file->info.st_size;
g_file = file;

DLOG("");
DLOG("---------- g_file ------------");
DLOG("g_file->fd: %d", g_file->fd);
DLOG("g_file->name: %s", g_file->name);
DLOG("g_file->addr: %p", g_file->addr);
DLOG("g_file->info.st_size: %ld", g_file->info.st_size);
DLOG("------------------------------");
DLOG("");
DLOG1("");
DLOG1("---------- g_file ------------");
DLOG1("g_file->fd: %d", g_file->fd);
DLOG1("g_file->name: %s", g_file->name);
DLOG1("g_file->addr: %p", g_file->addr);
DLOG1("g_file->info.st_size: %ld", g_file->info.st_size);
DLOG1("------------------------------");
DLOG1("");
}


Expand Down
31 changes: 18 additions & 13 deletions src/hmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

void init_hmap(size_t size)
{
DLOG("init_hmap()");
DLOG2("CALL init_hmap()");
t_line *area;

if (g_hmap.ptr != NULL)
Expand All @@ -28,6 +28,7 @@ void init_hmap(size_t size)

void destroy_hmap(void)
{
DLOG2("CALL destroy_hmap()");
if (g_hmap.ptr != NULL)
{
free(g_hmap.ptr);
Expand All @@ -43,7 +44,7 @@ void destroy_hmap(void)
*/
void populate_hmap(t_chunk *chunk)
{
DLOG("populate_hmap()");
DLOG2("CALL populate_hmap()");
t_line line;
long slot;
size_t has_slots;
Expand All @@ -53,10 +54,12 @@ void populate_hmap(t_chunk *chunk)
i = 0;
base_ptr = chunk->ptr;

#ifdef DEBUG
#if DEBUG >= 2
size_t filled = 0;
#endif
#if DEBUG >= 3
int last_percent_filled = 0;
int tmp = 0;
size_t filled = 0;
#endif

memset(g_hmap.ptr, 0, g_hmap.size * sizeof(t_line));
Expand All @@ -71,14 +74,16 @@ void populate_hmap(t_chunk *chunk)
{
/* use first free slot */
g_hmap.ptr[slot] = line;
#ifdef DEBUG
#if DEBUG >= 2
filled++;
/* tmp = (int)((double)filled / (double)g_hmap.size * 100.0); */
/* if (tmp > last_percent_filled) { */
/* last_percent_filled = tmp; */
/* DLOG("populate_hmap(): used %ld/%ld slots (%d%%) ...", */
/* filled, g_hmap.size, tmp); */
/* } */
#endif
#if DEBUG >= 3
tmp = (int)((double)filled / (double)g_hmap.size * 100.0);
if (tmp > last_percent_filled) {
last_percent_filled = tmp;
DLOG3("populate_hmap(): used %ld/%ld slots (%d%%) ...",
filled, g_hmap.size, tmp);
}
#endif
break;
}
Expand All @@ -100,8 +105,8 @@ void populate_hmap(t_chunk *chunk)
}
}
set_status(TAGDUP_BYTES, (size_t)(chunk->ptr - base_ptr));
#ifdef DEBUG
DLOG("populate_hmap(): used %ld/%ld slots (%.2f%%)",
#if DEBUG >= 2
DLOG2("populate_hmap(): used %ld/%ld slots (%.2f%%)",
filled, g_hmap.size, (double)filled / (double)g_hmap.size * 100.0);
#endif
}
4 changes: 2 additions & 2 deletions src/optparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ void optparse(int argc, char **argv)
/* STDIN can be used as infile (priority to --infile arg if exists) */
if (optind == argc - 1)
{
DLOG("using %s as input file", argv[argc - 1]);
DLOG1("using %s as input file", argv[argc - 1]);
setopt_infile(argv[argc - 1]);
}
else if (optind == argc && !isatty(STDIN_FILENO))
{
DLOG("using STDIN as input file");
DLOG1("using STDIN as input file");
setopt_infile("/dev/stdin");
}
else
Expand Down
20 changes: 10 additions & 10 deletions src/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,30 @@ void update_status(enum e_status_update action)
{
switch (action) {
case FCOPY_START:
DLOG("update_status(FCOPY_START) called");
DLOG1("CALL update_status(FCOPY_START)");
g_status.fcopy_date = time(NULL);
break ;
case TAGDUP_START:
DLOG("update_status(TAGDUP_START) called");
DLOG1("CALL update_status(TAGDUP_START)");
g_status.ctasks_date = time(NULL);
g_status.total_chunks = count_chunks();
g_status.total_ctasks = TRIANGULAR(g_status.total_chunks);
break ;
case CHUNK_DONE:
DLOG("update_status(CHUNK_DONE) called");
DLOG1("CALL update_status(CHUNK_DONE)");
pthread_mutex_lock(&g_mutex);
++g_status.done_chunks;
pthread_mutex_unlock(&g_mutex);
break ;
case CTASK_DONE:
DLOG("update_status(CTASK_DONE) called");
DLOG1("CALL update_status(CTASK_DONE)");
pthread_mutex_lock(&g_mutex);
++g_status.done_ctasks;
g_status.last_ctask_date = time(NULL);
pthread_mutex_unlock(&g_mutex);
break ;
case FCLEAN_START:
DLOG("update_status(FCLEAN_START) called");
DLOG1("CALL update_status(FCLEAN_START)");
g_status.fclean_date = time(NULL);
break ;
}
Expand All @@ -100,25 +100,25 @@ void set_status(enum e_status_set var, size_t val)

switch (var) {
case FILE_SIZE:
DLOG("set_status(FILE_SIZE, %lu) called", val);
DLOG1("CALL set_status(FILE_SIZE, %lu)", val);
g_status.file_size = val;
break ;
case FCOPY_BYTES:
DLOG("set_status(FCOPY_BYTES, %lu) called", val);
DLOG2("CALL set_status(FCOPY_BYTES, %lu)", val);
g_status.fcopy_bytes += val;
break ;
case CHUNK_SIZE:
DLOG("set_status(CHUNK_SIZE, %lu) called", val);
DLOG1("CALL set_status(CHUNK_SIZE, %lu)", val);
g_status.chunk_size = val;
break ;
case TAGDUP_BYTES:
DLOG("set_status(TAGDUP_BYTES, %lu) called", val);
DLOG2("CALL set_status(TAGDUP_BYTES, %lu)", val);
pthread_mutex_lock(&g_mutex);
g_status.tagdup_bytes += val;
pthread_mutex_unlock(&g_mutex);
break ;
case FCLEAN_BYTES:
DLOG("set_status(FCLEAN_BYTES, %lu) called", val);
DLOG2("CALL set_status(FCLEAN_BYTES, %lu)", val);
g_status.fclean_bytes += val;
break ;
}
Expand Down
6 changes: 3 additions & 3 deletions test/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set -ve
CC="$CC"
FLAGS="$FLAGS"

make fclean
make distclean
make debug
make distclean
make
make fclean
make release

0 comments on commit d58379f

Please sign in to comment.