⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trclib.c

📁 VxWorks BSP框架源代码包含头文件和驱动
💻 C
📖 第 1 页 / 共 3 页
字号:
        doingDefault = TRUE;        nargs = trcDefaultArgs;        (* _func_printErr) ("[");        }    /* print subroutine arguments */    for (i = 0; i < nargs; ++i)        {        if (i != 0)            {            (* _func_printErr) (", ");            }        (* _func_printErr) ("%x", args[i]);        }    if (doingDefault)        {        (* _func_printErr) ("]");        }    (* _func_printErr) (")\n");    }/********************************************************************************* trcFindCall - get address from which function was called** INTERNAL* There is a bit of trouble with this routine.  Given the <returnAdrs>* for some function, we are trying to go to that text address, and then* back up the program counter and look for the CALL instruction that* invoked the function.  The problem is that there are several CALL* instruction formats that the test/filter is NOT looking for.  Thus,* this routine could easilly fail to find the address from which a* function was called.  The test/filter needs to be more sophisticated.** RETURNS: Address from which current subroutine was called, or NULL.** NOMANUAL*/LOCAL const INSTR * trcFindCall    (    const INSTR * returnAdrs  /* return address */    )    {    const INSTR * addr;       /* points to executable instruction address */    /* starting at the word preceding the return adrs, search for CALL */    for (addr = returnAdrs - 1; addr != NULL; --addr)        {        if ((DSM (addr,     CALL_INDIR0,        CALL_INDIR0_MASK) &&            (DSM (addr + 1, CALL_INDIR_REG_EAX, CALL_INDIR_REG_MASK) ||             DSM (addr + 1, CALL_INDIR_REG_EDX, CALL_INDIR_REG_MASK) ||             DSM (addr + 1, CALL_INDIR1,        CALL_INDIR1_MASK))) ||            (DSM (addr,     CALL_DIR,           CALL_DIR_MASK)))            {            return (addr);     /* found it */            }        }    return (NULL);             /* not found */    }/********************************************************************************* trcFindDest - find destination of call instruction** RETURNS:* Address to which call instruction (CALL) will branch or NULL if unknown.** NOMANUAL*/LOCAL const INSTR * trcFindDest    (    const INSTR * callAdrs    )    {    if (DSM (callAdrs, CALL_DIR, CALL_DIR_MASK))        {        /* PC-relative offset */        const int displacement = *(int *)(callAdrs + 1);        /* program counter */        const INSTR * const pc = (INSTR *)((int) callAdrs + 1 + sizeof (int));        return ((const INSTR *) ((int) pc + displacement));        }        return (NULL);    /* don't know destination */    }/********************************************************************************* trcCountArgs - find number of arguments to function** This routine finds the number of arguments passed to the called function* by examining the stack-pop at the return address.  Many compilers offer* optimization that defeats this (e.g. by coalescing stack-pops), so a return* value of 0, may mean "don't know".** INTERNAL* This routine relies on the "caller cleans the stack" convention to* imply how many 4-byte quantities were pushed on the stack for a function* call.  On IA-32, since the stack grows from high to low addresses, the* calling routine cleans the stack by adding some number of bytes to ESP* at the function return address.** RETURNS: The number of arguments passed to a function.** NOMANUAL*/LOCAL int trcCountArgs    (    const INSTR * returnAdrs    /* return address of function call */    )    {    int           nbytes;       /* stores the argument count */    /* if inst is a JMP, use the target of the JMP as the returnAdrs */    const INSTR * const addr = trcFollowJmp (returnAdrs);    if (DSM (addr,   ADDI08_0, ADDI08_0_MASK) &&        DSM (addr+1, ADDI08_1, ADDI08_1_MASK))        {        nbytes = *(char *)(addr + 2);        }    else if (DSM (addr,   ADDI32_0, ADDI32_0_MASK) &&             DSM (addr+1, ADDI32_1, ADDI32_1_MASK))        {        nbytes = *(int *)(addr + 2);        }    else if (DSM (addr,   LEAD08_0, LEAD08_0_MASK) &&             DSM (addr+1, LEAD08_1, LEAD08_1_MASK) &&             DSM (addr+2, LEAD08_2, LEAD08_2_MASK))        {        nbytes = *(char *)(addr + 3);        }    else if (DSM (addr,   LEAD32_0, LEAD32_0_MASK) &&             DSM (addr+1, LEAD32_1, LEAD32_1_MASK) &&             DSM (addr+2, LEAD08_2, LEAD08_2_MASK))        {        nbytes = *(int *)(addr + 3);        }    else        {        nbytes = 0;  /* no args, or unknown */        }    if (nbytes < 0)        nbytes = 0 - nbytes;    return (nbytes >> 2);    }/********************************************************************************* trcFindFuncStart - find the starting address of a function** This routine finds the starting address of a function by one of several ways.** If the given frame pointer points to a legitimate frame pointer, then the* long word following the frame pointer pointed to by the frame pointer should* be the return address of the function call. Then the instruction preceding* the return address would be the function call, and the address can be gotten* from there, provided that the CALL was to an pc-relative address. If it was,* use that address as the function address.  Note that a routine that is* called by other than a call-direct (e.g. indirectly) will not meet these* requirements.* * If the above check fails, we search backward from the given pc until a* PUSH %EBP MOV %ESP %EBP instruction is found.  If the compiler is putting * PUSH %EBP MOV %ESP %EBP instructions as the first instruction of ALL* subroutines, then this will reliably find the start of the routine.* However, some compilers allow routines, especially "leaf" routines that* don't call any other routines, to NOT have stack frames, which will cause* this search to fail.** In either of the above cases, the value is bounded by the nearest* routine in the system symbol table, if there is one.  If neither method* returns a legitimate value, then the value from the symbol table is use.* Note that the routine may not be in the symbol table if it is LOCAL, etc.** Note that the start of a routine that is not called by call-direct and* doesn't start with a PUSH %EBP MOV %ESP %EBP and isn't in the symbol table,* may not be possible to locate.** RETURNS:* The closest function entry-point address found at a memory location* lower than that specified program counter address.** NOMANUAL*/LOCAL const INSTR * trcFindFuncStart    (    const INSTR *  pc             /* address somewhere within the function */    )    {    const INSTR *  minPc = NULL;  /* lower bound on program counter */    int            val   = 0;     /* function address from symbol table */    /* If there is a symbol table, try to find a symbol table value     * that is <= (pc) as the lower bound for the function entry point.     * If we can find a symbol table record for a function entry point     * <= (pc), then that address may, or may not, be the entry point     * for the function (pc) is in.     */    if (sysSymTbl != NULL)        {        char *     pName = NULL;  /* function name from symbol table */        SYM_TYPE   type;          /* function type from symbol table */        if (symByValueFind (sysSymTbl, (int) pc, &pName, &val, &type) == OK)            {            minPc = (const INSTR *)(val);            }        if (pName != NULL)            {            free (pName);  /* new API requires this */            }        }    /* XXX NOTE (fix this) XXX     * Search backward for a recognizable function prologue.  If there is     * no symbol table built into the image, then (minPc) = 0.  In this     * case, the search for function prologue could possibly decrement down     * to address 0 in memory.     */    for (; pc >= minPc; --pc)        {        /* vxTaskEntry is the first code to be executed by every task         * when it comes into existence. Since nothing can come before         * vxTaskEntry, the recursion stops there.         */        if ((int) pc == (int) vxTaskEntry)            return pc;        if ((DSM (pc,     PUSH_EBP, PUSH_EBP_MASK) &&             DSM (pc + 1, MOV_ESP0, MOV_ESP0_MASK) &&             DSM (pc + 2, MOV_ESP1, MOV_ESP1_MASK)) ||            /* this CANNOT distinguish between "func" and "func.aligned" */            (DSM (pc,     PUSH_EBP,    PUSH_EBP_MASK) &&              DSM (pc + 1, PUSH_ESI,    PUSH_ESI_MASK) &&            (DSM (pc + 2, MOV_ESP_ESI, MOV_ESP_ESI_MASK) ||             DSM (pc + 3, MOV_ESP_ESI, MOV_ESP_ESI_MASK))) ||             DSM (pc,     ENTER,       ENTER_MASK)           )            {            return (pc);  /* assume we've found the function entry point */            }        }    return (minPc);       /* return the nearest function entry address */    }/********************************************************************************* trcFollowJmp - resolve any JMP instructions to final destination** This routine returns a pointer to the next non-JMP instruction to be* executed if the pc were at the specified <adrs>.  That is, if the instruction* at <adrs> is not a JMP, then <adrs> is returned.  Otherwise, if the* instruction at <adrs> is a JMP, then the destination of the JMP is* computed, which then becomes the new <adrs> which is tested as before.* Thus we will eventually return the address of the first non-JMP instruction* to be executed.** The need for this arises because compilers may put JMPs to instructions* that we are interested in, instead of the instruction itself.  For example,* optimizers may replace a stack pop with a JMP to a stack pop.  Or in very* UNoptimized code, the first instruction of a subroutine may be a JMP to* a PUSH %EBP MOV %ESP %EBP, instead of a PUSH %EBP MOV %ESP %EBP (compiler* may omit routine "post-amble" at end of parsing the routine!).  We call* this routine anytime we are looking for a specific kind of instruction,* to help handle such cases.** RETURNS: The address that a chain of branches points to.** NOMANUAL*/LOCAL const INSTR * trcFollowJmp    (    const INSTR * addr    )    {    int     displacement;        /* PC relative offset */    int     length;              /* instruction length */    /* while instruction is a JMP, get destination adrs */    while (DSM (addr, JMPD08, JMPD08_MASK) ||           DSM (addr, JMPD32, JMPD32_MASK))        {        if (DSM (addr, JMPD08, JMPD08_MASK))            {            displacement = *(char *)(addr + 1);            length = 2;            addr   = (INSTR *) (addr + length + displacement);            }        else if (DSM (addr, JMPD32, JMPD32_MASK))            {            displacement = *(int *)(addr + 1);            length = 5;            addr   = (INSTR *) (addr + length + displacement);            }        }    return (addr);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -