int
_vscprintf (const char
*
format
, va_list pargs) {
int
retval;
va_list argcopy;
va_copy(argcopy, pargs);
retval
=
vsnprintf(NULL,
0
,
format
, argcopy);
va_end(argcopy);
return
retval;
}
std::string
format
(const char
*
pszFmt, ...) {
std::string
str
;
va_list args;
va_start(args, pszFmt);
{
int
nLength
=
_vscprintf(pszFmt, args);
nLength
+
=
1
;
/
/
上面返回的长度是包含\
0
,这里加上
std::vector<char> vectorChars(nLength);
snprintf(vectorChars.data(), nLength, pszFmt, args);
str
.assign(vectorChars.data());
}
va_end(args);
return
str
;
}
extern
"C"
JNIEXPORT jstring JNICALL
Java_com_example_helloworld_MainActivity_stringFromJNI(
JNIEnv
*
env,
jobject
/
*
this
*
/
) {
unsigned
int
uint_r0;
unsigned
int
uint_r1;
unsigned
int
uint_r2;
unsigned
int
uint_pc;
unsigned
int
uint_r7;
unsigned
int
uint_sp;
unsigned
int
uint_lr;
/
/
pid_t current_tid
=
gettid();
/
/
LOGD(
"current tid is %d"
, current_tid);
asm volatile(
"mov %[dest], r0"
:[dest]
"=r"
(uint_r0)
);
asm volatile(
"mov %[dest], r1"
:[dest]
"=r"
(uint_r1)
);
asm volatile(
"mov %[dest], r2"
:[dest]
"=r"
(uint_r2)
);
asm volatile(
"mov %[dest], pc"
:[dest]
"=r"
(uint_pc)
);
asm volatile(
"mov %[dest], r7"
:[dest]
"=r"
(uint_r7)
);
asm volatile(
"mov %[dest], sp"
:[dest]
"=r"
(uint_sp)
);
asm volatile(
"mov %[dest], lr"
:[dest]
"=r"
(uint_lr)
);
char buff[
1024
];
sprintf(buff,
"pc is %x \n r0 is %x \n r1 is %x \n r2 is %x \n r7 is %x \n sp is %x \nlr is %x \n"
,uint_pc, uint_r0, uint_r1,uint_r2, uint_r7, uint_sp,uint_lr) ;
return
env
-
>NewStringUTF(buff);
}