void luaG_errormsg (lua_State *L) { if (L->errfunc != 0) { /* is there an error handling function? */ StkId errfunc = restorestack(L, L->errfunc); if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); setobjs2s(L, L->top, L->top - 1); /* move argument */ setobjs2s(L, L->top - 1, errfunc); /* push function */ incr_top(L); luaD_call(L, L->top - 2, 1); /* call it */ } luaD_throw(L, LUA_ERRRUN); }
static int ktap_lib_len(ktap_State *ks) { int len = kp_objlen(ks, GetArg(ks, 1)); if (len < 0) return -1; setnvalue(ks->top, len); incr_top(ks); return 1; }
static int kplib_len(ktap_state *ks) { int len = kp_obj_len(ks, kp_arg(ks, 1)); if (len < 0) return -1; set_number(ks->top, len); incr_top(ks); return 1; }
static int ktap_lib_gettimeofday_us(ktap_State *ks) { struct timeval tv; do_gettimeofday(&tv); setnvalue(ks->top, tv.tv_sec * USEC_PER_SEC + tv.tv_usec); incr_top(ks); return 1; }
static int kplib_uid(ktap_state *ks) { #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) uid_t uid = from_kuid_munged(current_user_ns(), current_uid()); #else uid_t uid = current_uid(); #endif set_number(ks->top, (int)uid); incr_top(ks); return 1; }
static int kplib_stack(ktap_state_t *ks) { uint16_t skip, depth = 10; depth = kp_arg_checkoptnumber(ks, 1, 10); /* default as 10 */ depth = min_t(uint16_t, depth, KP_MAX_STACK_DEPTH); skip = kp_arg_checkoptnumber(ks, 2, 10); /* default as 10 */ set_kstack(ks->top, depth, skip); incr_top(ks); return 1; }
void luaG_errormsg (lua_State *L) { if (L->errfunc != 0) { /* is there an error handling function? */ StkId errfunc = restorestack(L, L->errfunc); if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); setobjs2s(L, L->top, L->top - 1); /* move argument */ setobjs2s(L, L->top - 1, errfunc); /* push function */ incr_top(L); luaD_call(L, L->top - 2, 1); /* call it */ }else if (L->def_errfunc && lua_checkstack(L, LUA_MINSTACK)) { L->def_errfunc(L); // dunno about locks etc. } luaD_throw(L, LUA_ERRRUN); }
static void collectvalidlines (lua_State *L, Closure *f) { if (f == NULL || f->c.isC) { setnilvalue(L->top); } else { Table *t = luaH_new(L, 0, 0); int *lineinfo = f->l.p->lineinfo; int i; for (i = 0; i < f->l.p->sizelineinfo; i++) setbvalue(luaH_setnum(L, t, lineinfo[i]), 1); sethvalue(L, L->top, t); } incr_top(L); }
/** * Returns a string representation for an IP address */ static int kplib_format_ip_addr(ktap_state *ks) { char ipstr[32]; __be32 ip; kp_arg_check(ks, 1, KTAP_TYPE_NUMBER); ip = (__be32)nvalue(kp_arg(ks, 1)); snprintf(ipstr, 32, "%pI4", &ip); set_string(ks->top, kp_str_new(ks, ipstr)); incr_top(ks); return 1; }
ktap_closure *kp_load(ktap_state *ks, unsigned char *buff) { struct load_state S; ktap_closure *cl; ktap_lclosure *f; int ret, i; S.ks = ks; S.buff = buff; S.pos = 0; ret = load_header(&S); if (ret) return NULL; cl = kp_newlclosure(ks, 1); if (!cl) return cl; /* put closure on the top, prepare to run with this closure */ setcllvalue(ks->top, cl); incr_top(ks); cl->l.p = kp_newproto(ks); if (load_function(&S, cl->l.p)) return NULL; if (cl->l.p->sizeupvalues != 1) { ktap_proto *p = cl->l.p; cl = kp_newlclosure(ks, cl->l.p->sizeupvalues); cl->l.p = p; setcllvalue(ks->top - 1, cl); } f = &cl->l; for (i = 0; i < f->nupvalues; i++) { /* initialize upvalues */ ktap_upval *up = kp_newupval(ks); f->upvals[i] = up; } /* set global table as 1st upvalue of 'f' */ if (f->nupvalues == 1) { ktap_table *reg = hvalue(&G(ks)->registry); const ktap_value *gt = kp_table_getint(reg, KTAP_RIDX_GLOBALS); setobj(f->upvals[0]->v, gt); } verify_code(&S, cl->l.p); return cl; }
static int ktap_lib_user_string(ktap_State *ks) { unsigned long addr = nvalue(GetArg(ks, 1)); char str[256] = {0}; int ret; pagefault_disable(); ret = __copy_from_user_inatomic((void *)str, (const void *)addr, 256); (void) &ret; /* Silence compiler warning. */ pagefault_enable(); str[255] = '\0'; setsvalue(ks->top, kp_tstring_new(ks, str)); incr_top(ks); return 1; }
int luaK_numberK (FuncState *fs, lua_Number r) { int n; lua_State *L = fs->L; TValue o; setnvalue(&o, r); if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */ /* use raw representation as key to avoid numeric problems */ setsvalue(L, L->top, luaS_newlstr(L, (char *)&r, sizeof(r))); incr_top(L); n = addk(fs, L->top - 1, &o); L->top--; } else n = addk(fs, &o, &o); /* regular case */ return n; }
lua_State *luaE_newthread (lua_State *L) { lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State))); luaC_link(L, obj2gco(L1), LUA_TTHREAD); setthvalue(L, L->top, L1); /* put thread on stack */ incr_top(L); preinit_state(L1, G(L)); stack_init(L1, L); /* init stack */ setobj2n(L, gt(L1), gt(L)); /* share table of globals */ L1->hookmask = L->hookmask; L1->basehookcount = L->basehookcount; L1->hook = L->hook; resethookcount(L1); lua_assert(!isdead(G(L), obj2gco(L1))); L->top--; /* remove thread from stack */ return L1; }
static Proto* LoadFunction(LoadState* S) { Proto* f=luaF_newproto(S->L); setptvalue2s(S->L,S->L->top,f); incr_top(S->L); f->linedefined=LoadInt(S); f->lastlinedefined=LoadInt(S); f->numparams=LoadByte(S); f->is_vararg=LoadByte(S); f->maxstacksize=LoadByte(S); LoadCode(S,f); LoadConstants(S,f); LoadUpvalues(S,f); LoadDebug(S,f); S->L->top--; return f; }
int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { //logt("luaG_ordererror"); const char *t1 = luaT_typenames[ttype(p1)]; const char *t2 = luaT_typenames[ttype(p2)]; #if 0 // This does not cause a crash on Symbian. setsvalue2s(L, L->top, luaS_new(L, "dummy message")); incr_top(L); luaD_throw(L, LUA_ERRRUN); #else if (t1[2] == t2[2]) luaG_runerror_m(L, "attempt to compare two %s values", t1); else luaG_runerror_m(L, "attempt to compare %s with %s", t1, t2); #endif return 0; }
static Proto* LoadFunction(LoadState* S, TString* p) { Proto* f=luaF_newproto(S->L); setptvalue2s(S->L,S->L->top,f); incr_top(S->L); f->source=LoadString(S); if (f->source==NULL) f->source=p; f->linedefined=LoadInt(S); f->lastlinedefined=LoadInt(S); f->nups=LoadByte(S); f->numparams=LoadByte(S); f->is_vararg=LoadByte(S); f->maxstacksize=LoadByte(S); LoadCode(S,f); LoadConstants(S,f); LoadDebug(S,f); IF (!luaG_checkcode(f), "bad code"); S->L->top--; return f; }
static int ktap_lib_backtrace(ktap_state *ks) { struct stack_trace trace; ktap_btrace *bt; bt = kp_percpu_data(KTAP_PERCPU_DATA_BTRACE); trace.nr_entries = 0; trace.skip = 10; trace.max_entries = KTAP_STACK_MAX_ENTRIES; trace.entries = &bt->entries[0]; save_stack_trace(&trace); bt->nr_entries = trace.nr_entries; setbtvalue(ks->top, bt); incr_top(ks); return 1; }
Proto *luaY_parser(lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { LexState X; FuncState F; X.buff=buff; luaX_setinput(L,&X,z,luaS_new(L,name)); X.fs=&F; X.fs->h=luaH_new(L,0,0); sethvalue2s(L,L->top,X.fs->h); incr_top(L); if (dump) { printf("0\t<file>\t%s\n",name); dodump(&X); } else dostrip(&X); return luaF_newproto(L); }
static void ktap_call_probe_closure(ktap_state *mainthread, ktap_closure *cl, struct ktap_event *e) { ktap_state *ks; ktap_value *func; ks = kp_newthread(mainthread); setcllvalue(ks->top, cl); func = ks->top; incr_top(ks); ks->current_event = e; kp_call(ks, func, 0); ks->current_event = NULL; kp_exitthread(ks); }
void * luaL_pushcdata(struct lua_State *L, uint32_t ctypeid) { /* * ctypeid is actually has CTypeID type. * CTypeId is defined somewhere inside luajit's internal * headers. */ assert(sizeof(ctypeid) == sizeof(CTypeID)); /* Code below is based on ffi_new() from luajit/src/lib_ffi.c */ /* Get information about ctype */ CTSize size; CTState *cts = ctype_cts(L); CTInfo info = lj_ctype_info(cts, ctypeid, &size); /* Only numbers and pointers are implemented */ assert(ctype_isnum(info) || ctype_isptr(info)); (void) info; assert(size != CTSIZE_INVALID); /* Allocate a new cdata */ GCcdata *cd = lj_cdata_new(cts, ctypeid, size); /* Anchor the uninitialized cdata with the stack. */ TValue *o = L->top; setcdataV(L, o, cd); incr_top(L); /* * lj_cconv_ct_init is omitted because it actually does memset() * Caveats: cdata memory is returned uninitialized */ /* * __gc is omitted because it is only needed for structs. * Caveats: struct are not supported by this function. * Please set finalizer using luaL_setcdatagc() if you need. */ lj_gc_check(L); return cdataptr(cd); }
static int kplib_kernel_string(ktap_state *ks) { unsigned long addr; char str[256] = {0}; char *ret; kp_arg_check(ks, 1, KTAP_TYPE_NUMBER); addr = nvalue(kp_arg(ks, 1)); ret = strncpy((void *)str, (const void *)addr, 256); (void) &ret; /* Silence compiler warning. */ str[255] = '\0'; set_string(ks->top, kp_str_new_local(ks, str)); incr_top(ks); return 1; }
static int kplib_kernel_string(ktap_state_t *ks) { unsigned long addr = kp_arg_checknumber(ks, 1); char str[256] = {0}; ktap_str_t *ts; char *ret; ret = strncpy((void *)str, (const void *)addr, 256); (void) &ret; /* Silence compiler warning. */ str[255] = '\0'; ts = kp_str_newz(ks, str); if (unlikely(!ts)) return -1; set_string(ks->top, ts); incr_top(ks); return 1; }
/* * use gdb to get field offset of struct task_struct, for example: * * gdb vmlinux * (gdb)p &(((struct task_struct *)0).prio) */ static int kplib_curr_taskinfo(ktap_state *ks) { int offset; int fetch_bytes; kp_arg_check(ks, 1, KTAP_TYPE_NUMBER); offset = nvalue(kp_arg(ks, 1)); if (kp_arg_nr(ks) == 1) fetch_bytes = 4; /* default fetch 4 bytes*/ else { kp_arg_check(ks, 2, KTAP_TYPE_NUMBER); fetch_bytes = nvalue(kp_arg(ks, 2)); } if (offset >= sizeof(struct task_struct)) { set_nil(ks->top++); kp_error(ks, "access out of bound value of task_struct\n"); return 1; } #define RET_VALUE ((unsigned long)current + offset) switch (fetch_bytes) { case 4: set_number(ks->top, *(unsigned int *)RET_VALUE); break; case 8: set_number(ks->top, *(unsigned long *)RET_VALUE); break; default: kp_error(ks, "unsupported fetch bytes in curr_task_info\n"); set_nil(ks->top); break; } #undef RET_VALUE incr_top(ks); return 1; }
Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { struct LexState lexstate; struct FuncState funcstate; TString *tname = luaS_new(L, name); setsvalue2s(L, L->top, tname); /* protect name */ incr_top(L); lexstate.buff = buff; luaX_setinput(L, &lexstate, z, tname); open_func(&lexstate, &funcstate); funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ luaX_next(&lexstate); /* read first token */ chunk(&lexstate); check(&lexstate, TK_EOS); close_func(&lexstate); L->top--; /* remove 'name' from stack */ lua_assert(funcstate.prev == NULL); lua_assert(funcstate.f->nups == 0); lua_assert(lexstate.fs == NULL); return funcstate.f; }
static int kplib_ptable(ktap_state *ks) { ktap_ptab *ph; int narr = 0, nrec = 0; if (kp_arg_nr(ks) >= 1) { kp_arg_check(ks, 1, KTAP_TYPE_NUMBER); narr = nvalue(kp_arg(ks, 1)); } if (kp_arg_nr(ks) >= 2) { kp_arg_check(ks, 2, KTAP_TYPE_NUMBER); nrec = nvalue(kp_arg(ks, 2)); } ph = kp_ptab_new(ks, narr, nrec); set_ptable(ks->top, ph); incr_top(ks); return 1; }
static int kplib_user_string(ktap_state *ks) { unsigned long addr; char str[256] = {0}; int ret; kp_arg_check(ks, 1, KTAP_TYPE_NUMBER); addr = nvalue(kp_arg(ks, 1)); pagefault_disable(); ret = __copy_from_user_inatomic((void *)str, (const void *)addr, 256); (void) &ret; /* Silence compiler warning. */ pagefault_enable(); str[255] = '\0'; set_string(ks->top, kp_str_new(ks, str)); incr_top(ks); return 1; }
static Proto* LoadFunction(LoadState* S, TString* p) { Proto* f; if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); f=luaF_newproto(S->L); setptvalue2s(S->L,S->L->top,f); incr_top(S->L); f->source=LoadString(S); if (f->source==nullptr) f->source=p; f->linedefined=LoadInt(S); f->lastlinedefined=LoadInt(S); f->nups=LoadByte(S); f->numparams=LoadByte(S); f->is_vararg=LoadByte(S); f->maxstacksize=LoadByte(S); LoadCode(S,f); LoadConstants(S,f); LoadDebug(S,f); IF (!luaG_checkcode(f), "bad code"); S->L->top--; S->L->nCcalls--; return f; }
static int kplib_user_string(ktap_state_t *ks) { unsigned long addr = kp_arg_checknumber(ks, 1); char str[256] = {0}; ktap_str_t *ts; int ret; pagefault_disable(); ret = __copy_from_user_inatomic((void *)str, (const void *)addr, 256); (void) &ret; /* Silence compiler warning. */ pagefault_enable(); str[255] = '\0'; ts = kp_str_newz(ks, str); if (unlikely(!ts)) return -1; set_string(ks->top, ts); incr_top(ks); return 1; }
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { int status = 1; lua_lock(L); if (*what == '>') { StkId f = L->top - 1; if (!ttisfunction(f)) luaG_runerror(L, "value for `lua_getinfo' is not a function"); status = auxgetinfo(L, what + 1, ar, f, NULL); L->top--; /* pop function */ } else if (ar->i_ci != 0) { /* no tail call? */ CallInfo *ci = L->base_ci + ar->i_ci; lua_assert(ttisfunction(ci->base - 1)); status = auxgetinfo(L, what, ar, ci->base - 1, ci); } else info_tailcall(L, ar); if (strchr(what, 'f')) incr_top(L); lua_unlock(L); return status; }
/** * Return the source IP address for a given sock */ static int kplib_ip_sock_saddr(ktap_state *ks) { struct inet_sock *isk; int family; kp_arg_check(ks, 1, KTAP_TYPE_NUMBER); /* need to validate the address firstly */ isk = (struct inet_sock *)nvalue(kp_arg(ks, 1)); family = isk->sk.__sk_common.skc_family; if (family == AF_INET) { set_number(ks->top, isk->inet_rcv_saddr); } else { kp_error(ks, "ip_sock_saddr only support ipv4 now\n"); set_nil(ks->top); } incr_top(ks); return 1; }