Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable debug mode when debugger is detected #2120

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ extern int sqlite3_shell_main(int argc, char **argv);
// defined in database/sqlite3_rsync.c
extern int sqlite3_rsync_main(int argc, char **argv);

bool dnsmasq_debug = false;
bool debug_mode = false;
bool daemonmode = true, cli_mode = false;
int argc_dnsmasq = 0;
const char** argv_dnsmasq = NULL;
Expand Down Expand Up @@ -611,7 +611,7 @@ void parse_args(int argc, char *argv[])
if(strcmp(argv[i], "lua") == 0 ||
strcmp(argv[i], "--lua") == 0)
{
exit(run_lua_interpreter(argc - i, &argv[i], dnsmasq_debug));
exit(run_lua_interpreter(argc - i, &argv[i], debug_mode));
}

// Expose internal lua compiler
Expand Down Expand Up @@ -729,7 +729,7 @@ void parse_args(int argc, char *argv[])
argv_dnsmasq = calloc(argc_dnsmasq, sizeof(const char*));
argv_dnsmasq[0] = "";

if(dnsmasq_debug)
if(debug_mode)
{
argv_dnsmasq[1] = "-d";
argv_dnsmasq[2] = "--log-debug";
Expand All @@ -740,7 +740,7 @@ void parse_args(int argc, char *argv[])
argv_dnsmasq[2] = "";
}

if(dnsmasq_debug)
if(debug_mode)
{
printf("dnsmasq options: [0]: %s\n", argv_dnsmasq[0]);
printf("dnsmasq options: [1]: %s\n", argv_dnsmasq[1]);
Expand All @@ -751,7 +751,7 @@ void parse_args(int argc, char *argv[])
while(i < argc)
{
argv_dnsmasq[j++] = strdup(argv[i++]);
if(dnsmasq_debug)
if(debug_mode)
printf("dnsmasq options: [%i]: %s\n", j-1, argv_dnsmasq[j-1]);
}

Expand All @@ -764,11 +764,11 @@ void parse_args(int argc, char *argv[])
if(strcmp(argv[i], "d") == 0 ||
strcmp(argv[i], "debug") == 0)
{
dnsmasq_debug = true;
debug_mode = true;
daemonmode = false;
ok = true;

// Replace "-k" by "-d" (dnsmasq_debug mode implies nofork)
// Replace "-k" by "-d" (debug_mode mode implies nofork)
argv_dnsmasq[1] = "-d";
}

Expand Down Expand Up @@ -951,9 +951,9 @@ void parse_args(int argc, char *argv[])
// Enable stdout printing
cli_mode = true;
if(argc == i + 2)
exit(regex_test(dnsmasq_debug, quiet, argv[i + 1], NULL));
exit(regex_test(debug_mode, quiet, argv[i + 1], NULL));
else if(argc == i + 3)
exit(regex_test(dnsmasq_debug, quiet, argv[i + 1], argv[i + 2]));
exit(regex_test(debug_mode, quiet, argv[i + 1], argv[i + 2]));
else
{
printf("pihole-FTL: invalid option -- '%s' need either one or two parameters\nTry '%s --help' for more information\n", argv[i], argv[0]);
Expand Down
2 changes: 1 addition & 1 deletion src/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

void parse_args(int argc, char *argv[]);

extern bool daemonmode, cli_mode, dnsmasq_debug;
extern bool daemonmode, cli_mode;
extern int argc_dnsmasq;
extern const char ** argv_dnsmasq;

Expand Down
5 changes: 5 additions & 0 deletions src/dnsmasq/dnsmasq.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ struct event_desc {
#define option_val(x) ((1u) << ((x) % OPTION_BITS))
#define option_bool(x) (option_var(x) & option_val(x))

/***** Pi-hole modification *****/
#define option_set(x) (option_var(x) |= option_val(x))
#define option_clear(x) (option_var(x) &= ~option_val(x))
/********************************/

/* extra flags for my_syslog, we use facilities since they are known
not to occupy the same bits as priorities, no matter how syslog.h is set up.
MS_DEBUG messages are suppressed unless --log-debug is set. */
Expand Down
53 changes: 51 additions & 2 deletions src/dnsmasq_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
#include "main.h"
// ntp_server_start()
#include "ntp/ntp.h"
// get_process_name()
#include "procps.h"

// Private prototypes
static void print_flags(const unsigned int flags);
Expand Down Expand Up @@ -3386,7 +3388,7 @@ void FTL_forwarding_retried(const struct server *serv, const int oldID, const in
volatile atomic_flag worker_already_terminating = ATOMIC_FLAG_INIT;
void FTL_TCP_worker_terminating(bool finished)
{
if(dnsmasq_debug)
if(get_dnsmasq_debug())
{
// Nothing to be done here, forking does not happen in debug mode
return;
Expand Down Expand Up @@ -3431,7 +3433,7 @@ void FTL_TCP_worker_terminating(bool finished)
// to ending up with a corrupted database.
void FTL_TCP_worker_created(const int confd)
{
if(dnsmasq_debug)
if(get_dnsmasq_debug())
{
// Nothing to be done here, TCP worker forking does not happen
// in debug mode
Expand Down Expand Up @@ -3765,3 +3767,50 @@ void FTL_connection_error(const char *reason, const union mysockaddr *addr)
free(server);
}
}

/**
* @brief Retrieves the debug status of dnsmasq.
*
* @return true if the debug option is enabled, false otherwise.
*/
bool __attribute__ ((pure)) get_dnsmasq_debug(void)
{
return option_bool(OPT_DEBUG);
}

static bool enabled = false;
/**
* @brief Set the dnsmasq debug mode based on the enable flag and process ID.
*
* This function enables or disables the dnsmasq debug mode. When enabling,
* it logs the process ID and name, sets the debug option, and marks the
* debug mode as enabled. When disabling, it logs the detachment and clears
* the debug option. If the debug mode is already enabled, it does nothing.
*
* @param enable A boolean flag indicating whether to enable or disable debug mode.
* @param pid The process ID of the process to attach or detach the debugger.
*/
void set_dnsmasq_debug(const bool enable, const pid_t pid)
{
// Get debugger process' name
char name[PROC_PATH_SIZ] = "???";
get_process_name(pid, name);

// Only enable or disable if the debug mode is not already set
if(enable && !get_dnsmasq_debug())
{
// Enable debug mode
log_info("Debugger attached (%d: %s), entering dnsmasq debug mode",
pid, name);
option_set(OPT_DEBUG);
enabled = true;

return;
}
else if(enabled)
{
// Disable debug mode
log_info("Debugger detached, leaving dnsmasq debug mode");
option_clear(OPT_DEBUG);
}
}
2 changes: 2 additions & 0 deletions src/dnsmasq_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ bool FTL_unlink_DHCP_lease(const char *ipaddr, const char **hint);

void FTL_connection_error(const char *reason, const union mysockaddr *addr);

bool get_dnsmasq_debug(void) __attribute__ ((pure));

// defined in src/dnsmasq/cache.c
extern char *querystr(char *desc, unsigned short type);

Expand Down
14 changes: 14 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ static void recycle(void)

log_debug(DEBUG_GC, "Recycled %u clients, %u domains, and %u cache records (scanned %u queries)",
clients_recycled, domains_recycled, cache_recycled, counters->queries);

dump_strings();
}
}

Expand Down Expand Up @@ -528,8 +530,11 @@ static bool check_files_on_same_device(const char *path1, const char *path2)
return s1.st_dev == s2.st_dev;
}

static bool is_debugged = false;
void *GC_thread(void *val)
{
(void)val; // Mark parameter as unused

// Set thread name
prctl(PR_SET_NAME, thread_names[GC], 0, 0, 0);

Expand Down Expand Up @@ -636,6 +641,15 @@ void *GC_thread(void *val)
if(killed)
break;

// Check if FTL is being debugged and set dnsmasq's debug mode
// accordingly
const pid_t dpid = debugger();
if((dpid > 0) != is_debugged)
{
is_debugged = dpid > 0;
set_dnsmasq_debug(is_debugged, dpid);
}

// Sleep for the remaining time of the interval (if any)
const double time_end = double_time();
const double time_diff = time_end - time_start;
Expand Down
6 changes: 6 additions & 0 deletions src/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
#ifndef GC_H
#define GC_H

#include <stdbool.h>
#include <time.h>

void *GC_thread(void *val);
void runGC(const time_t now, time_t *lastGCrun, const bool flush);
time_t get_rate_limit_turnaround(const unsigned int rate_limit_count);

// Defined in src/dnsmasq_interface.c
void set_dnsmasq_debug(const bool debug, const pid_t pid);

#endif //GC_H
17 changes: 9 additions & 8 deletions src/lookup-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ static inline int cmp_hash(const uint32_t a, const uint32_t b)
* found element or NULL (!) if the element is not found which provides no
* information about the position where the element would need to be inserted.
*/
static bool binsearch(const struct lookup_table *base, const uint32_t hash,
size_t size, const struct lookup_table **try)
static bool binsearch(struct lookup_table *base, const uint32_t hash,
size_t size, struct lookup_table **try)
{
// Initialize the base pointer to the start of the array
*try = base;
Expand Down Expand Up @@ -207,7 +207,7 @@ bool lookup_insert(const enum memory_type type, const unsigned int id, const uin
// Find the correct position in the lookup_table array
// We do not check the return value as we are inserting a new element
// and don't care if elements with the same hash value exist already
const struct lookup_table *try = table;
struct lookup_table *try = table;
binsearch(table, hash, *size, &try);

// Calculate the position where the element would be inserted
Expand All @@ -217,7 +217,7 @@ bool lookup_insert(const enum memory_type type, const unsigned int id, const uin
// one position to the right to make space for the new element
// Don't move anything if the element is added at the end of the array
if(pos < *size)
memmove((void*)(try + 1), try, (*size - pos) * sizeof(struct lookup_table));
memmove(try + 1, try, (*size - pos) * sizeof(struct lookup_table));

// Prepare the new lookup_table element and insert it at the correct
// position
Expand Down Expand Up @@ -253,7 +253,7 @@ bool lookup_remove(const enum memory_type type, const unsigned int id, const uin
return false;

// Find the correct position in the lookup_table array
const struct lookup_table *try = NULL;
struct lookup_table *try = NULL;
if(!binsearch(table, hash, *size, &try))
{
// The element is not in the array
Expand Down Expand Up @@ -283,13 +283,14 @@ bool lookup_remove(const enum memory_type type, const unsigned int id, const uin
// one position to the left to remove the element
// Don't move anything if the element is removed from the end of the array
if(pos < *size - 1)
memmove(&table[pos], &table[pos + 1], (*size - pos - 1) * sizeof(struct lookup_table));
memmove(table + pos, table + pos + 1,
(*size - pos - 1) * sizeof(struct lookup_table));

// Decrease the number of elements in the array
(*size)--;

// Zero out the memory of the removed element
memset(&table[*size], 0, sizeof(struct lookup_table));
memset(table + *size, 0, sizeof(struct lookup_table));

return true;
}
Expand Down Expand Up @@ -332,7 +333,7 @@ bool lookup_find_id(const enum memory_type type, const uint32_t hash, const stru
return false;

// Find the correct position in the lookup_table array
const struct lookup_table *try = NULL;
struct lookup_table *try = NULL;
if(!binsearch(table, hash, *size, &try))
return false;

Expand Down
10 changes: 5 additions & 5 deletions src/lua/ftl_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "daemon.h"


int run_lua_interpreter(const int argc, char **argv, bool dnsmasq_debug)
int run_lua_interpreter(const int argc, char **argv, bool debug)
{
if(argc == 1) // No arguments after this one
printf("Pi-hole FTL %s\n", get_FTL_version());
Expand All @@ -48,7 +48,7 @@ int run_lua_interpreter(const int argc, char **argv, bool dnsmasq_debug)
{
history_file = word.we_wordv[0];
const int ret_r = read_history(history_file);
if(dnsmasq_debug)
if(debug)
{
printf("Reading history ... ");
if(ret_r == 0)
Expand All @@ -60,7 +60,7 @@ int run_lua_interpreter(const int argc, char **argv, bool dnsmasq_debug)
// The history file may not exist, try to create an empty one in this case
if(ret_r == ENOENT)
{
if(dnsmasq_debug)
if(debug)
{
printf("Creating new history file: %s\n", history_file);
}
Expand All @@ -70,15 +70,15 @@ int run_lua_interpreter(const int argc, char **argv, bool dnsmasq_debug)
}
}
#else
if(dnsmasq_debug)
if(debug)
printf("No readline available!\n");
#endif
const int ret = lua_main(argc, argv);
#if defined(LUA_USE_READLINE)
if(history_file != NULL)
{
const int ret_w = write_history(history_file);
if(dnsmasq_debug)
if(debug)
{
printf("Writing history ... ");
if(ret_w == 0)
Expand Down
2 changes: 1 addition & 1 deletion src/lua/ftl_lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#define LUA_HISTORY_FILE "~/.pihole_lua_history"

int run_lua_interpreter(const int argc, char **argv, bool dnsmasq_debug);
int run_lua_interpreter(const int argc, char **argv, bool debug);
int run_luac(const int argc, char **argv);

int lua_main (int argc, char **argv);
Expand Down
3 changes: 1 addition & 2 deletions src/procps.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "config/config.h"

#define PROCESS_NAME "pihole-FTL"
#define PROC_PATH_SIZ 32

// This function tries to obtain the process name of a given PID
// It returns true on success, false otherwise and stores the process name in
Expand All @@ -28,7 +27,7 @@
// to parse /proc/<pid>/comm. The latter is not guaranteed to be correct (e.g.
// processes can easily change it themselves using prctl with PR_SET_NAME), but
// it is better than nothing.
static bool get_process_name(const pid_t pid, char name[PROC_PATH_SIZ])
bool get_process_name(const pid_t pid, char name[PROC_PATH_SIZ])
{
if(pid == 0)
{
Expand Down
7 changes: 7 additions & 0 deletions src/procps.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

#ifndef PROCPS_H
#define PROCPS_H

#include <stdbool.h>
#include <sys/types.h>

#define PROC_PATH_SIZ 32

bool get_process_name(const pid_t pid, char name[PROC_PATH_SIZ]);
bool another_FTL(void);

struct proc_mem {
Expand Down
Loading
Loading