extern
"C"
void
dumpArtMethod(ArtMethod* artmethod) REQUIRES_SHARED(Locks::mutator_lock_) {
char
*dexfilepath=(
char
*)
malloc
(
sizeof
(
char
)*1000);
if
(dexfilepath==nullptr)
{
LOG(ERROR) <<
"ArtMethod::dumpArtMethodinvoked,methodname:"
<<artmethod->PrettyMethod().c_str()<<
"malloc 1000 byte failed"
;
return
;
}
int
result=0;
int
fcmdline =-1;
char
szCmdline[64]= {0};
char
szProcName[256] = {0};
int
procid = getpid();
sprintf
(szCmdline,
"/proc/%d/cmdline"
, procid);
fcmdline = open(szCmdline, O_RDONLY,0644);
if
(fcmdline >0)
{
result=read(fcmdline, szProcName,256);
if
(result<0)
{
LOG(ERROR) <<
"ArtMethod::dumpdexfilebyArtMethod,open cmdline file file error"
;
}
close(fcmdline);
}
if
(szProcName[0])
{
const
DexFile* dex_file = artmethod->GetDexFile();
const
uint8_t* begin_=dex_file->Begin();
size_t
size_=dex_file->Size();
memset
(dexfilepath,0,1000);
int
size_int_=(
int
)size_;
memset
(dexfilepath,0,1000);
sprintf
(dexfilepath,
"%s"
,
"/sdcard/fart"
);
mkdir(dexfilepath,0777);
memset
(dexfilepath,0,1000);
sprintf
(dexfilepath,
"/sdcard/fart/%s"
,szProcName);
mkdir(dexfilepath,0777);
memset
(dexfilepath,0,1000);
sprintf
(dexfilepath,
"/sdcard/fart/%s/%d_dexfile.dex"
,szProcName,size_int_);
int
dexfilefp=open(dexfilepath,O_RDONLY,0666);
if
(dexfilefp>0){
close(dexfilefp);
dexfilefp=0;
}
else
{
int
fp=open(dexfilepath,O_CREAT|O_APPEND|O_RDWR,0666);
if
(fp>0)
{
result=write(fp,(
void
*)begin_,size_);
if
(result<0)
{
LOG(ERROR) <<
"ArtMethod::dumpdexfilebyArtMethod,open dexfilepath file error"
;
}
fsync(fp);
close(fp);
memset
(dexfilepath,0,1000);
sprintf
(dexfilepath,
"/sdcard/fart/%s/%d_classlist.txt"
,szProcName,size_int_);
int
classlistfile=open(dexfilepath,O_CREAT|O_APPEND|O_RDWR,0666);
if
(classlistfile>0)
{
for
(
size_t
ii= 0; ii< dex_file->NumClassDefs(); ++ii)
{
const
DexFile::ClassDef& class_def = dex_file->GetClassDef(ii);
const
char
* descriptor = dex_file->GetClassDescriptor(class_def);
result=write(classlistfile,(
void
*)descriptor,
strlen
(descriptor));
if
(result<0)
{
LOG(ERROR) <<
"ArtMethod::dumpdexfilebyArtMethod,write classlistfile file error"
;
}
const
char
* temp=
"\n"
;
result=write(classlistfile,(
void
*)temp,1);
if
(result<0)
{
LOG(ERROR) <<
"ArtMethod::dumpdexfilebyArtMethod,write classlistfile file error"
;
}
}
fsync(classlistfile);
close(classlistfile);
}
}
}
const
DexFile::CodeItem* code_item = artmethod->GetCodeItem();
if
(LIKELY(code_item != nullptr))
{
int
code_item_len = 0;
uint8_t *item=(uint8_t *) code_item;
if
(code_item->tries_size_>0) {
const
uint8_t *handler_data = (
const
uint8_t *)(DexFile::GetTryItems(*code_item, code_item->tries_size_));
uint8_t * tail = codeitem_end(&handler_data);
code_item_len = (
int
)(tail - item);
}
else
{
code_item_len = 16+code_item->insns_size_in_code_units_*2;
}
memset
(dexfilepath,0,1000);
int
size_int=(
int
)dex_file->Size();
uint32_t method_idx=artmethod->GetDexMethodIndexUnchecked();
sprintf
(dexfilepath,
"/sdcard/fart/%s/%d_ins_%d.bin"
,szProcName,size_int,(
int
)gettidv1());
int
fp2=open(dexfilepath,O_CREAT|O_APPEND|O_RDWR,0666);
if
(fp2>0){
lseek(fp2,0,SEEK_END);
memset
(dexfilepath,0,1000);
int
offset=(
int
)(item - begin_);
sprintf
(dexfilepath,
"{name:%s,method_idx:%d,offset:%d,code_item_len:%d,ins:"
,
artmethod->PrettyMethod().c_str(),method_idx,offset,code_item_len);
int
contentlength=0;
while
(dexfilepath[contentlength]!=0) contentlength++;
result=write(fp2,(
void
*)dexfilepath,contentlength);
if
(result<0)
{
LOG(ERROR) <<
"ArtMethod::dumpdexfilebyArtMethod,write ins file error"
;
}
long
outlen=0;
char
* base64result=base64_encode((
char
*)item,(
long
)code_item_len,&outlen);
result=write(fp2,base64result,outlen);
if
(result<0)
{
LOG(ERROR) <<
"ArtMethod::dumpdexfilebyArtMethod,write ins file error"
;
}
result=write(fp2,
"};"
,2);
if
(result<0)
{
LOG(ERROR) <<
"ArtMethod::dumpdexfilebyArtMethod,write ins file error"
;
}
fsync(fp2);
close(fp2);
if
(base64result!=nullptr){
free
(base64result);
base64result=nullptr;
}
}
}
}
if
(dexfilepath!=nullptr)
{
free
(dexfilepath);
dexfilepath=nullptr;
}
}