int read_reg_value(StackFrame * frame, RegisterDefinition * reg_def, uint64_t * value) { uint8_t buf[8]; if (reg_def == NULL) { set_errno(ERR_INV_CONTEXT, "Invalid register"); return -1; } if (frame == NULL) { set_errno(ERR_INV_CONTEXT, "Invalid stack frame"); return -1; } if (reg_def->size > sizeof(buf)) { errno = ERR_INV_DATA_SIZE; return -1; } if (read_reg_bytes(frame, reg_def, 0, reg_def->size, buf) < 0) return -1; if (value != NULL) { size_t i; uint64_t n = 0; for (i = 0; i < reg_def->size; i++) { n = n << 8; n |= buf[reg_def->big_endian ? i : reg_def->size - i - 1]; } *value = n; } return 0; }
static void command_get_cache_client(void * x) { GetArgs * args = (GetArgs *)x; Channel * c = cache_channel(); Trap trap; bbf_pos = 0; if (set_trap(&trap)) { int frame = 0; Context * ctx = NULL; RegisterDefinition * reg_def = NULL; if (id2register(args->id, &ctx, &frame, ®_def) < 0) exception(errno); if (ctx->exited) exception(ERR_ALREADY_EXITED); if ((ctx->reg_access & REG_ACCESS_RD_STOP) != 0) { check_all_stopped(ctx); } if ((ctx->reg_access & REG_ACCESS_RD_RUNNING) == 0) { if (!ctx->stopped && context_has_state(ctx)) str_exception(ERR_IS_RUNNING, "Cannot read register if not stopped"); } if (reg_def->size > bbf_len) { bbf_len += 0x100 + reg_def->size; bbf = (uint8_t *)loc_realloc(bbf, bbf_len); } bbf_pos = reg_def->size; memset(bbf, 0, reg_def->size); if (frame < 0 || is_top_frame(ctx, frame)) { if (context_read_reg(ctx, reg_def, 0, reg_def->size, bbf) < 0) exception(errno); } else { StackFrame * info = NULL; if (get_frame_info(ctx, frame, &info) < 0) exception(errno); if (read_reg_bytes(info, reg_def, 0, reg_def->size, bbf) < 0) exception(errno); } clear_trap(&trap); } cache_exit(); write_stringz(&c->out, "R"); write_stringz(&c->out, args->token); write_errno(&c->out, trap.error); json_write_binary(&c->out, bbf, bbf_pos); write_stream(&c->out, 0); write_stream(&c->out, MARKER_EOM); }
static void command_getm_cache_client(void * x) { GetmArgs * args = (GetmArgs *)x; Channel * c = cache_channel(); Trap trap; bbf_pos = 0; if (bbf == NULL) bbf = (uint8_t *)loc_alloc(bbf_len = 0x100); if (set_trap(&trap)) { unsigned locs_pos = 0; check_location_list(args->locs, args->locs_cnt, 0); while (locs_pos < args->locs_cnt) { Location * l = args->locs + locs_pos++; if (bbf_pos + l->size > bbf_len) { bbf_len += 0x100 + l->size; bbf = (uint8_t *)loc_realloc(bbf, bbf_len); } memset(bbf + bbf_pos, 0, l->size); if (l->frame_info == NULL) { if (context_read_reg(l->ctx, l->reg_def, l->offs, l->size, bbf + bbf_pos) < 0) exception(errno); } else { if (read_reg_bytes(l->frame_info, l->reg_def, l->offs, l->size, bbf + bbf_pos) < 0) exception(errno); } bbf_pos += l->size; } clear_trap(&trap); } cache_exit(); write_stringz(&c->out, "R"); write_stringz(&c->out, args->token); write_errno(&c->out, trap.error); json_write_binary(&c->out, bbf, bbf_pos); write_stream(&c->out, 0); write_stream(&c->out, MARKER_EOM); loc_free(args->locs); }