Print current settings on crash

This commit is contained in:
Christian Göttsche 2021-05-16 19:55:31 +02:00
parent 204bc710ba
commit 1f5f40c091
4 changed files with 30 additions and 10 deletions

8
CRT.c
View File

@ -80,6 +80,7 @@ bool CRT_utf8 = false;
const char* const* CRT_treeStr = CRT_treeStrAscii; const char* const* CRT_treeStr = CRT_treeStrAscii;
static const Settings* CRT_crashSettings;
static const int* CRT_delay; static const int* CRT_delay;
const char* CRT_degreeSign; const char* CRT_degreeSign;
@ -767,6 +768,7 @@ void CRT_init(const Settings* settings, bool allowUnicode) {
initscr(); initscr();
noecho(); noecho();
CRT_crashSettings = settings;
CRT_delay = &(settings->delay); CRT_delay = &(settings->delay);
CRT_colors = CRT_colorSchemes[settings->colorScheme]; CRT_colors = CRT_colorSchemes[settings->colorScheme];
CRT_colorScheme = settings->colorScheme; CRT_colorScheme = settings->colorScheme;
@ -953,6 +955,12 @@ void CRT_handleSIGSEGV(int signal) {
signal, signal_str signal, signal_str
); );
fprintf(stderr,
"Setting information:\n"
"--------------------\n");
Settings_write(CRT_crashSettings, true);
fprintf(stderr, "\n");
#ifdef HAVE_EXECINFO_H #ifdef HAVE_EXECINFO_H
fprintf(stderr, fprintf(stderr,
"Backtrace information:\n" "Backtrace information:\n"

View File

@ -361,7 +361,7 @@ int CommandLine_run(const char* name, int argc, char** argv) {
CRT_done(); CRT_done();
if (settings->changed) { if (settings->changed) {
int r = Settings_write(settings); int r = Settings_write(settings, false);
if (r < 0) if (r < 0)
fprintf(stderr, "Can not save configuration to %s: %s\n", settings->filename, strerror(-r)); fprintf(stderr, "Can not save configuration to %s: %s\n", settings->filename, strerror(-r));
} }
@ -373,10 +373,12 @@ int CommandLine_run(const char* name, int argc, char** argv) {
MetersPanel_cleanup(); MetersPanel_cleanup();
UsersTable_delete(ut); UsersTable_delete(ut);
Settings_delete(settings);
if (flags.pidMatchList) if (flags.pidMatchList)
Hashtable_delete(flags.pidMatchList); Hashtable_delete(flags.pidMatchList);
/* Delete Settings last, since it can get accessed in the crash handler */
Settings_delete(settings);
return 0; return 0;
} }

View File

@ -281,13 +281,20 @@ static void writeMeterModes(const Settings* this, FILE* fd, int column) {
fprintf(fd, "\n"); fprintf(fd, "\n");
} }
int Settings_write(const Settings* this) { int Settings_write(const Settings* this, bool onCrash) {
FILE* fd = fopen(this->filename, "w"); FILE* fd;
if (onCrash) {
fd = stderr;
} else {
fd = fopen(this->filename, "w");
if (fd == NULL) if (fd == NULL)
return -errno; return -errno;
}
if (!onCrash) {
fprintf(fd, "# Beware! This file is rewritten by htop when settings are changed in the interface.\n"); fprintf(fd, "# Beware! This file is rewritten by htop when settings are changed in the interface.\n");
fprintf(fd, "# The parser is also very primitive, and not human-friendly.\n"); fprintf(fd, "# The parser is also very primitive, and not human-friendly.\n");
}
writeFields(fd, this->fields, "fields"); writeFields(fd, this->fields, "fields");
// This "-1" is for compatibility with the older enum format. // This "-1" is for compatibility with the older enum format.
fprintf(fd, "sort_key=%d\n", (int) this->sortKey - 1); fprintf(fd, "sort_key=%d\n", (int) this->sortKey - 1);
@ -333,6 +340,9 @@ int Settings_write(const Settings* this) {
fprintf(fd, "topology_affinity=%d\n", (int) this->topologyAffinity); fprintf(fd, "topology_affinity=%d\n", (int) this->topologyAffinity);
#endif #endif
if (onCrash)
return 0;
int r = 0; int r = 0;
if (ferror(fd) != 0) if (ferror(fd) != 0)
@ -431,7 +441,7 @@ Settings* Settings_new(unsigned int initialCpuCount) {
ok = Settings_read(this, legacyDotfile, initialCpuCount); ok = Settings_read(this, legacyDotfile, initialCpuCount);
if (ok) { if (ok) {
// Transition to new location and delete old configuration file // Transition to new location and delete old configuration file
if (Settings_write(this) == 0) { if (Settings_write(this, false) == 0) {
unlink(legacyDotfile); unlink(legacyDotfile);
} }
} }

View File

@ -87,7 +87,7 @@ static inline int Settings_getActiveDirection(const Settings* this) {
void Settings_delete(Settings* this); void Settings_delete(Settings* this);
int Settings_write(const Settings* this); int Settings_write(const Settings* this, bool onCrash);
Settings* Settings_new(unsigned int initialCpuCount); Settings* Settings_new(unsigned int initialCpuCount);