int cp_ctype_free() { int i; csymbol *cs; if (cts.stack) free(cts.stack); if (cs_arr) { for (i = 0; i < cs_nr; i++) { cs = &cs_arr[i]; if (csym_type(cs) == FFI_FUNC) { if (csym_func(cs)->arg_ids) free(csym_func(cs)->arg_ids); } else if (csym_type(cs) == FFI_STRUCT) { if (csym_struct(cs)->members) free(csym_struct(cs)->members); } } free(cs_arr); } if (cte_arr) { free(cte_arr); } return 0; }
void kp_cdata_record_get(ktap_state_t *ks, ktap_cdata_t *cd, ktap_val_t *key, ktap_val_t *val) { const char *mb_name; csymbol *cs, *mb_cs; csymbol_struct *csst; struct_member *mb; char *addr; csymbol_id mb_cs_id; if (!is_shrstring(key)) { kp_error(ks, "struct member name should be string\n"); return; } mb_name = svalue(key); cs = cd_csym(ks, cd); csst = csym_struct(cs); mb = csymst_mb_by_name(ks, csst, mb_name); if (mb == NULL) { kp_error(ks, "struct member %s doesn't exist\n", mb_name); return; } mb_cs_id = mb->id; mb_cs = id_to_csym(ks, mb_cs_id); addr = cd_record(cd); addr += csym_record_mb_offset_by_name(ks, cs, mb_name); kp_cdata_init(ks, val, addr, mb->len, mb_cs_id); if (mb->len < 0) kp_cdata_pack(ks, val, addr, mb_cs); }
void kp_cdata_record_set(ktap_state *ks, ktap_cdata *cd, ktap_value *key, ktap_value *val) { const char *mb_name; csymbol *cs, *mb_cs; csymbol_struct *csst; struct_member *mb; char *addr; if (!is_shrstring(key)) { kp_error(ks, "struct member name should be string\n"); return; } mb_name = svalue(key); cs = cd_csym(ks, cd); csst = csym_struct(cs); mb = csymst_mb_by_name(ks, csst, mb_name); if (mb == NULL) { kp_error(ks, "struct member %s doesn't exist\n", mb_name); return; } mb_cs = id_to_csym(ks, mb->id); if (kp_cdata_type_match(ks, mb_cs, val)) { kp_error(ks, "struct member should be %s type\n", csym_name(mb_cs)); return; } addr = cd_struct(cd); addr += csym_record_mb_offset_by_name(ks, cs, mb_name); kp_cdata_unpack(ks, addr, mb_cs, val); }
static void DumpCSymbolStruct(csymbol *cs, DumpState *D) { csymbol_struct *csst = csym_struct(cs); DumpBlock(cs, sizeof(csymbol), D); /* dump csymbol index for argument types */ DumpBlock(csst->members, csst->memb_nr*sizeof(struct_member), D); }
void __cp_symbol_dump_struct(csymbol *cs) { int i; csymbol *ncs; csymbol_struct *stcs = csym_struct(cs); printf("=== [%s] definition ==================\n", csym_name(cs)); for (i = 0; i < stcs->memb_nr; i++) { printf("\t(%d) ", i); printf("csym_id: %d, ", stcs->members[i].id); ncs = &cs_arr[stcs->members[i].id]; printf("name: %s, ffi_ctype: %d, %s\n", stcs->members[i].name, ncs->type, csym_name(ncs)); } }
/* this is a debug function used for check csymbol array */ void ktapc_dump_csymbols() { int i, j, cs_nr; cp_csymbol_state *cs_state; csymbol *cs, *cs_arr; csymbol_func *csf; csymbol_struct *csst; cs_state = ctype_get_csym_state(); cs_arr = cs_state->cs_arr; cs_nr = cs_state->cs_nr; printf("\n----------------------------------------------------\n"); printf("Number of csymbols: %d\n", cs_nr); for (i = 0; i < cs_nr; i++) { cs = &cs_arr[i]; printf("%dth symbol", i); ktapc_dump_csymbol_id("", i); switch (cs->type) { case FFI_PTR: ktapc_dump_csymbol_id("\tDeref", csym_ptr_deref_id(cs)); break; case FFI_FUNC: csf = csym_func(cs); printf("\tAddress: 0x%p\n", csf->addr); ktapc_dump_csymbol_id("\tReturn", csf->ret_id); printf("\tArg number: %d\n", csf->arg_nr); for (j = 0; j < csf->arg_nr; j++) ktapc_dump_csymbol_id("\t\tArg", csf->arg_ids[j]); printf("\tHas variable arg: %d\n", csf->has_var_arg); break; case FFI_STRUCT: case FFI_UNION: csst = csym_struct(cs); printf("\tMember number: %d\n", csst->memb_nr); for (j = 0; j < csst->memb_nr; j++) { printf("\t\tMember %s", csst->members[j].name); if (csst->members[j].len >= 0) printf("(len %d)", csst->members[j].len); ktapc_dump_csymbol_id("", csst->members[j].id); } break; default: break; } } }
int cp_symbol_build_struct(const char *stname) { int i, id, memb_size; cp_ctype_entry *cte; csymbol nst; struct_member *st_membs; csymbol_struct *stcs; if (cts.top <= 0 || !stname) { cp_error("invalid struct definition.\n"); } memb_size = cts.top; st_membs = malloc(memb_size*sizeof(struct_member)); if (!st_membs) cp_error("failed to allocate memory for struct members.\n"); memset(st_membs, 0, memb_size*sizeof(struct_member)); nst.type = FFI_STRUCT; strcpy(nst.name, stname); stcs = csym_struct(&nst); stcs->memb_nr = memb_size; stcs->members = st_membs; for (i = 0; i < memb_size; i++) { assert(i < cts.top); cte = ct_stack(i); if (cte->name) strcpy(st_membs[i].name, cte->name); st_membs[i].id = ct_stack_ct(i)->ffi_cs_id; } id = cp_ctype_reg_csymbol(&nst); ctype_stack_reset(); return id; }