/
/
ptrace(PTRACE_ATTACH)附加到app
/
/
执行mmap获取返回值, 写入dlopen需要的参数
/
/
执行dlopen函数加载自己的so
/
/
调用函数的方法如下
int
ptraceCall(
int
nPid, uint32_t unAddr,
long
arrParams[],
int
nParamNum, pt_regs
*
pRegs)
{
/
/
写入参数到堆栈
pRegs
-
>esp
-
=
(
long
) (nParamNum
*
sizeof(
long
));
ptraceWriteData(nPid, (uint8_t
*
) pRegs
-
>esp, (uint8_t
*
) arrParams,
(
int
) (nParamNum
*
sizeof(
long
)));
/
/
不知道为什么写入这个
long
unTmp
=
0x00
;
pRegs
-
>esp
-
=
sizeof(
long
);
ptraceWriteData(nPid, (uint8_t
*
) pRegs
-
>esp, (uint8_t
*
) &unTmp, sizeof(unTmp));
pRegs
-
>eip
=
(
long
) unAddr;
/
/
写入参数
for
(
int
i
=
0
; i < nParamNum;
+
+
i)
{
/
/
写入参数到堆栈
if
(i >
=
4
)
{
pRegs
-
>ARM_sp
-
=
(
long
)((nParamNum
-
i)
*
sizeof(
long
));
break
;
}
/
/
写入参数到寄存器
pRegs
-
>uregs[i]
=
arrParams[i];
}
pRegs
-
>ARM_pc
=
(
long
)unAddr;
if
(pRegs
-
>ARM_pc &
1
)
{
pRegs
-
>ARM_pc &
=
(
long
)(~
1u
);
pRegs
-
>ARM_cpsr |
=
CPSR_T_MASK;
}
else
{
pRegs
-
>ARM_cpsr &
=
~CPSR_T_MASK;
}
/
/
设置参数并运行
if
(ptraceSetRegs(nPid, pRegs)
=
=
-
1
|| ptraceContinue(nPid)
=
=
-
1
)
{
perror(
"设置寄存器出错或继续运行出错"
);
return
-
1
;
}
int
nStat
=
0
;
waitpid(nPid, &nStat, WUNTRACED);
while
(nStat !
=
0xB7F
)
{
if
(ptraceContinue(nPid)
=
=
-
1
)
{
perror(
"继续运行出错"
);
return
-
1
;
}
waitpid(nPid, &nStat, WUNTRACED);
}
return
0
;
}