struct sr_python_frame * sr_python_frame_from_json(struct sr_json_value *root, char **error_message) { if (!JSON_CHECK_TYPE(root, SR_JSON_OBJECT, "frame")) return NULL; struct sr_python_frame *result = sr_python_frame_new(); struct sr_json_value *val; /* Source file name / special file */ if ((val = json_element(root, "file_name"))) { if (!JSON_CHECK_TYPE(val, SR_JSON_STRING, "file_name")) goto fail; result->special_file = false; result->file_name = sr_strdup(val->u.string.ptr); } else if ((val = json_element(root, "special_file"))) { if (!JSON_CHECK_TYPE(val, SR_JSON_STRING, "special_file")) goto fail; result->special_file = true; result->file_name = sr_strdup(val->u.string.ptr); } /* Function name / special function. */ if ((val = json_element(root, "function_name"))) { if (!JSON_CHECK_TYPE(val, SR_JSON_STRING, "function_name")) goto fail; result->special_function = false; result->function_name = sr_strdup(val->u.string.ptr); } else if ((val = json_element(root, "special_function"))) { if (!JSON_CHECK_TYPE(val, SR_JSON_STRING, "special_function")) goto fail; result->special_function = true; result->function_name = sr_strdup(val->u.string.ptr); } bool success = JSON_READ_STRING(root, "line_contents", &result->line_contents) && JSON_READ_UINT32(root, "file_line", &result->file_line); if (!success) goto fail; return result; fail: sr_python_frame_free(result); return NULL; }
struct sr_core_stacktrace * sr_core_stacktrace_from_json(struct sr_json_value *root, char **error_message) { if (!JSON_CHECK_TYPE(root, SR_JSON_OBJECT, "stacktrace")) return NULL; struct sr_core_stacktrace *result = sr_core_stacktrace_new(); bool success = JSON_READ_UINT16(root, "signal", &result->signal) && JSON_READ_STRING(root, "executable", &result->executable) && JSON_READ_BOOL(root, "only_crash_thread", &result->only_crash_thread); if (!success) goto fail; /* Read threads. */ struct sr_json_value *stacktrace = json_element(root, "stacktrace"); if (stacktrace) { if (!JSON_CHECK_TYPE(stacktrace, SR_JSON_ARRAY, "stacktrace")) goto fail; struct sr_json_value *json_thread; FOR_JSON_ARRAY(stacktrace, json_thread) { struct sr_core_thread *thread = sr_core_thread_from_json(json_thread, error_message); if (!thread) goto fail; struct sr_json_value *crash_thread = json_element(json_thread, "crash_thread"); if (crash_thread) { if (!JSON_CHECK_TYPE(crash_thread, SR_JSON_BOOLEAN, "crash_thread")) { sr_core_thread_free(thread); goto fail; } if (crash_thread->u.boolean) result->crash_thread = thread; } result->threads = sr_core_thread_append(result->threads, thread); } } return result; fail: sr_core_stacktrace_free(result); return NULL; }
static int json_object(JOQE_YYSTYPE *yylval, joqe_build *b, joqe_node *n) { n->type |= joqe_type_none_object; int token = joqe_yylex(yylval, b); if(token == '}') return 0; goto first; do { token = joqe_yylex(yylval, b); first: if(STRING != token) { //expected string or '}', not a string so check '}' //this allows {"a":"b",} break; } const char *key = yylval->string; if(':' != joqe_yylex(yylval, b)) { joqe_yyerror(b, "expected ':'"); return -1; } joqe_nodels l = {{}, {joqe_type_string_none, .k = {.key = key}}}, *ls; token = joqe_yylex(yylval, b); if((token = json_element(token, yylval, b, &l.n))) return token; *(ls = calloc(1, sizeof(*ls))) = l; joqe_list_append((joqe_list**)&n->u.ls, &ls->ll); } while((token = joqe_yylex(yylval, b)) == ',');
/** * Supplies blocks of data to server_current_tcp_out. */ static void server_tcp_out(struct tcp_pcb *pcb, struct server_tcp_state *ss) { if (ss->connected) { /* If we're connected */ if (server_current_tcp_out(pcb, ss)) { /* If the current block is empty */ /* Clear our position */ ss->current_pos = 0; ss->current_len = 0; /* Select a new data block to output */ switch(ss->output_phase) { case OP_HTTP_HEADER: /* Send a header that specifies how many records we are going to send */ ss->current_len = http_header(ss->output_buffer, ss->auth, ss->records_count); ss->output_phase++; break; case OP_JSON_HEADER: /* Start the JSON object */ ss->current_len = json_header(ss->output_buffer); ss->output_phase++; break; case OP_JSON_DATA: if (ss->records_index++ < ss->records_count) { /* If there are more records to be output */ /* Read from memory, and encode as a JSON element */ ss->current_len = json_element(ss->output_buffer, get_sample(ss->mem_output_index++), /* Read in the data from memory */ (ss->records_index < ss->records_count)); /* If this isn't the last, we need a comma */ } else { ss->output_phase++; } break; case OP_JSON_FOOTER: /* End the JSON object */ ss->current_len = json_footer(ss->output_buffer); ss->output_phase++; break; case OP_WAIT_BEFORE_CLOSE: /* Wait before closing the connection */ if (ss->wait_before_close_counter++ > WAIT_BEFORE_CLOSE) { ss->output_phase++; } break; case OP_CLOSE: tcp_close(ss->pcb); /* Close the connection */ ss->output_phase++; break; } /* And output that */ server_current_tcp_out(pcb, ss); } } }
struct sr_python_stacktrace * sr_python_stacktrace_from_json(struct sr_json_value *root, char **error_message) { if (!JSON_CHECK_TYPE(root, SR_JSON_OBJECT, "stacktrace")) return NULL; struct sr_python_stacktrace *result = sr_python_stacktrace_new(); /* Exception name. */ if (!JSON_READ_STRING(root, "exception_name", &result->exception_name)) goto fail; /* Frames. */ struct sr_json_value *stacktrace = json_element(root, "stacktrace"); if (stacktrace) { if (!JSON_CHECK_TYPE(stacktrace, SR_JSON_ARRAY, "stacktrace")) goto fail; struct sr_json_value *frame_json; FOR_JSON_ARRAY(stacktrace, frame_json) { struct sr_python_frame *frame = sr_python_frame_from_json(frame_json, error_message); if (!frame) goto fail; result->frames = sr_python_frame_append(result->frames, frame); } } return result; fail: sr_python_stacktrace_free(result); return NULL; }
struct sr_koops_stacktrace * sr_koops_stacktrace_from_json(struct sr_json_value *root, char **error_message) { if (!JSON_CHECK_TYPE(root, SR_JSON_OBJECT, "stacktrace")) return NULL; struct sr_koops_stacktrace *result = sr_koops_stacktrace_new(); /* Kernel version. */ if (!JSON_READ_STRING(root, "version", &result->version)) goto fail; /* Raw oops text. */ if (!JSON_READ_STRING(root, "raw_oops", &result->raw_oops)) goto fail; /* Kernel taint flags. */ struct sr_json_value *taint_flags = json_element(root, "taint_flags"); if (taint_flags) { if (!JSON_CHECK_TYPE(taint_flags, SR_JSON_ARRAY, "taint_flags")) goto fail; struct sr_json_value *flag_json; FOR_JSON_ARRAY(taint_flags, flag_json) { if (!JSON_CHECK_TYPE(flag_json, SR_JSON_STRING, "taint flag")) goto fail; for (struct sr_taint_flag *f = sr_flags; f->name; f++) { if (0 == strcmp(f->name, flag_json->u.string.ptr)) { *(bool *)((void *)result + f->member_offset) = true; break; } } /* XXX should we do something if nothing is set? */ } } /* Modules. */ struct sr_json_value *modules = json_element(root, "modules"); if (modules) { if (!JSON_CHECK_TYPE(modules, SR_JSON_ARRAY, "modules")) goto fail; unsigned i = 0; size_t allocated = 128; result->modules = sr_malloc_array(allocated, sizeof(char*)); struct sr_json_value *mod_json; FOR_JSON_ARRAY(modules, mod_json) { if (!JSON_CHECK_TYPE(mod_json, SR_JSON_STRING, "module")) goto fail; /* need to keep the last element for NULL terminator */ if (i+1 == allocated) { allocated *= 2; result->modules = sr_realloc(result->modules, allocated); } result->modules[i] = sr_strdup(mod_json->u.string.ptr); i++; } result->modules[i] = NULL; } /* Frames. */ struct sr_json_value *frames = json_element(root, "frames"); if (frames) { if (!JSON_CHECK_TYPE(frames, SR_JSON_ARRAY, "frames")) goto fail; struct sr_json_value *frame_json; FOR_JSON_ARRAY(frames, frame_json) { struct sr_koops_frame *frame = sr_koops_frame_from_json(frame_json, error_message); if (!frame) goto fail; result->frames = sr_koops_frame_append(result->frames, frame); } } return result; fail: sr_koops_stacktrace_free(result); return NULL; }