Pull segfault-handling into a dedicated function and ifdef.

Some stringing tweaks.
This commit is contained in:
Sage Vaillancourt 2022-04-05 16:41:23 -04:00 committed by Sage Vaillancourt
parent b419826b8e
commit 61b9f79c36
3 changed files with 53 additions and 39 deletions

View File

@ -129,25 +129,26 @@ Object* nf_addToList(Object* dest, const Object src)
} }
#ifndef SIMPLE_ERRORS #ifndef SIMPLE_ERRORS
static const char* errorText[] = {"MISMATCHED_PARENS", static const char* errorText[] = {
"NULL_ENV", "MISMATCHED_PARENS",
"EMPTY_ENV", "NULL_ENV",
"NULL_PARSE", "EMPTY_ENV",
"NULL_LAMBDA_LIST", "NULL_PARSE",
"NULL_MAP_ARGS", "NULL_LAMBDA_LIST",
"LAMBDA_ARGS_NOT_LIST", "NULL_MAP_ARGS",
"DID_NOT_FIND_SYMBOL", "LAMBDA_ARGS_NOT_LIST",
"BAD_TYPE", "DID_NOT_FIND_SYMBOL",
"BAD_PARAMS_ON", "BAD_TYPE",
"BAD_NUMBER", "BAD_PARAMS_ON",
"UNSUPPORTED_NUMBER_TYPE", "BAD_NUMBER",
"NOT_ENOUGH_ARGUMENTS", "UNSUPPORTED_NUMBER_TYPE",
"NOT_A_LIST", "NOT_ENOUGH_ARGUMENTS",
"SCRIPT_NOT_FOUND", "NOT_A_LIST",
"NO_CLONE_SPECIFIED", "SCRIPT_NOT_FOUND",
"CAN_ONLY_EVAL_STRINGS", "NO_CLONE_SPECIFIED",
"UNEXPECTED_EOF", "CAN_ONLY_EVAL_STRINGS",
"INDEX_PAST_END"}; "UNEXPECTED_EOF",
"INDEX_PAST_END"};
#endif #endif
size_t inflate(struct string* s, size_t additional) size_t inflate(struct string* s, size_t additional)
@ -246,20 +247,20 @@ int stringNObj(struct string* s, const Object* obj)
case TYPE_BOOL: case TYPE_BOOL:
s->cursor += sprintf(s->cursor, "%s", obj->number ? "T" : "F"); s->cursor += sprintf(s->cursor, "%s", obj->number ? "T" : "F");
break; break;
case TYPE_SYMBOL:
case TYPE_STRING: { case TYPE_STRING: {
size_t stringLen = strlen(obj->string); size_t stringLen = strlen(obj->string);
inflate(s, stringLen); inflate(s, stringLen);
s->cursor += sprintf(s->cursor, "%s", obj->string); const char* format = obj->type == TYPE_STRING ? "%s" : "`%s`";
s->cursor += sprintf(s->cursor, format, obj->string);
break; break;
} }
case TYPE_SYMBOL:
s->cursor += sprintf(s->cursor, "`%s`", obj->string);
break;
case TYPE_STRUCT: case TYPE_STRUCT:
stringStruct(s, obj); stringStruct(s, obj);
break; break;
case TYPE_LIST:
case TYPE_SLIST: case TYPE_SLIST:
s->cursor += sprintf(s->cursor, "'");
case TYPE_LIST:
stringList(s, obj); stringList(s, obj);
break; break;
case TYPE_ERROR: { case TYPE_ERROR: {

View File

@ -13,6 +13,7 @@
#define printd(...) printf(__VA_ARGS__) #define printd(...) printf(__VA_ARGS__)
#else #else
#define printd(...) do { } while (0) #define printd(...) do { } while (0)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#endif #endif
#endif #endif

View File

@ -15,9 +15,6 @@
#include <readline/readline.h> #include <readline/readline.h>
#include <readline/history.h> #include <readline/history.h>
#include <signal.h>
#include <ucontext.h>
#include "web.h" #include "web.h"
#endif #endif
@ -677,6 +674,10 @@ void loadArgsIntoEnv(int argc, const char* argv[], struct Environment* env)
addToEnv(env, "args", args); addToEnv(env, "args", args);
} }
#ifdef __x86_64__
#include <signal.h>
#include <ucontext.h>
int nestedSegfault = 0; int nestedSegfault = 0;
void handler(int nSignum, siginfo_t* si, void* vcontext) void handler(int nSignum, siginfo_t* si, void* vcontext)
@ -697,10 +698,29 @@ void handler(int nSignum, siginfo_t* si, void* vcontext)
context->uc_mcontext.gregs[REG_RIP]++; context->uc_mcontext.gregs[REG_RIP]++;
exit(139); exit(139);
} }
void setupSegfaultHandler()
{
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_flags = SA_SIGINFO;
action.sa_sigaction = handler;
sigaction(SIGSEGV, &action, NULL);
// TODO: Track crashes in ~/.plcrash or some such }
#else
void setupSegfaultHandler()
{
}
#endif
// TODO: add --no-lib and --no-config and/or --config=
int main(int argc, const char* argv[]) int main(int argc, const char* argv[])
{ {
setupSegfaultHandler();
const char* const home = getenv("HOME");
char config[strlen(home) + 15];
struct Environment env = defaultEnv(); struct Environment env = defaultEnv();
setGlobal(&env); setGlobal(&env);
@ -714,12 +734,6 @@ int main(int argc, const char* argv[])
} }
} }
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_flags = SA_SIGINFO;
action.sa_sigaction = handler;
sigaction(SIGSEGV, &action, NULL);
readFile(SCRIPTDIR "/lib.pbl", &env); readFile(SCRIPTDIR "/lib.pbl", &env);
Object o = parseEval("(def prompt \"pebblisp::> \")", &env); Object o = parseEval("(def prompt \"pebblisp::> \")", &env);
@ -727,8 +741,6 @@ int main(int argc, const char* argv[])
o = parseEval("(def preprocess (fn (text) (text)))", &env); o = parseEval("(def preprocess (fn (text) (text)))", &env);
cleanObject(&o); cleanObject(&o);
const char* const home = getenv("HOME");
char config[strlen(home) + 15];
sprintf(config, "%s/.pebblisp.pbl", home); sprintf(config, "%s/.pebblisp.pbl", home);
readFile(config, &env); readFile(config, &env);
@ -753,9 +765,9 @@ int main(int argc, const char* argv[])
} }
deleteEnv(&env); deleteEnv(&env);
shredDictionary(); shredDictionary();
// fprintf(stderr, "totalSearchDepth: %d of %d searches\n", getTotalSearchDepth(), getTotalSearches()); // eprintf("totalSearchDepth: %d of %d searches\n", getTotalSearchDepth(), getTotalSearches());
// fprintf(stderr, "\nHEAP-ALLOCATED OBJECTS: %d\n", getAllocations()); // eprintf("\nHEAP-ALLOCATED OBJECTS: %d\n", getAllocations());
// fprintf(stderr, "TOTAL OBJECT.C ALLOC: %zu\n", getBytes()); // eprintf("TOTAL OBJECT.C ALLOC: %zu\n", getBytes());
} }
#endif #endif