Android Hprof 协议

概述

    欲立则先破,想要弄清楚 hprof 是如何记录我们的类,对象所占内存大小,以及引用链等,则先要破解出 hprof 的文件协议及内容。即 hprof 到底写入了什么?

    同其他类型文件,如Bitmap,Hprof 也有自己的文件的协议。其包括了 hprof head 和 hprof body 两部分。head 部分主要包括版本,索引等,而 body 则存储了类,对象,stack trace 等。貌似又是一个套路。

HProf Header

fixed header

主要写入了文件的版本描述及版本号,当前为固定的以'/0'结尾的字串,ID标识的size,写入时间戳。

1948
"JAVA PROFILE 1.0.3/0"indentify sizetime stamp

string table

写入了当前所用到所有的字符串,包括类名,常量等。这里的TAG,以及其他数据的 TAG 很重要。解析 Hprof 文件协议时,就是用 TAG 来区分的

string 的 TAG 定义为 0x01

1444strlen(string content)
tagtime stamplengthidstring content
tagtime stamplengthidstring content
tagtime stamplengthidstring content
.............................

class table

class 的 TAG 定义为 0x02

1444444
tagtime stamplengthclass serial numberclass object idstack trace serial number

class name string id

.......................................

stack trace

stack trace 的 TAG 定义为 0x05。实际取出来是空,即 num frames 为 0

144444
tagtime stamplengthserial numberthread serial numbernum frames

HProf Body

Body 部分,以一行为划分,每一行包括所有的 heap dump类型,以及该 heap dump下的子类型

Heap Dump,Heap 级别

144
tag(0x0C,HEAP_DUMP)time stamplength
tag(0x1C,HEAP_DUMP_SEGMENT)time stamplength

Sub Heap Dump

下面的 string id 对应的 string table 中的 string id

14 
0xff(ROOT_UNKNOWN)string id[RootType.UNKNOW]
144 
0x01(ROOT_JNI_GLOBAL)string idjni global id[RootType.NATIVE_STATIC]
1444
0x02(ROOT_JNI_LOCAL)string idthread serial numberstack frame number
1444
0x03(ROOT_JAVA_FRAME)string idthread serial numberstack frame number
144
0x04(ROOT_NATIVE_STACK)string idthread serial number
14 
0x05(ROOT_STICKY_CLASS)string id[RootType.SYSTEM_CLASS]
144
0x06(ROOT_THREAD_BLOCK)string idthread serial number
14 
0x07(ROOT_MONITOR_USED)string id[RootType.BUSY_MONITOR]
1444
0x08(ROOT_THREAD_OBJECT)string idthread serial numberstack frame number
14 
0x20(ROOT_CLASS_DUMP)string id有点复杂,在下面单独列出来
14444
0x21(ROOT_INSTANCE_DUMP)string idstack idclass idremaining(skip)
14444 
0x22(ROOT_OBJECT_ARRAY_DUMP)string idstack idnum elementsclass id4 * num elements(skip)
14441 
0x23(ROOT_PRIMITIVE_ARRAY_DUMP)string idstack idnum elementsprimitive type4 * num elements(skip)
1 
0xc3(ROOT_PRIMITIVE_ARRAY_NODATA)暂时表示错误类型
144
0xfe(ROOT_HEAP_DUMP_INFO)heap idheap name id(string id)
14 
0x89(ROOT_INTERNED_STRING)string id[RootType.INTERNED_STRING]
14 
0x8a(ROOT_FINALIZING)string id[RootType.FINALIZING]
14 
0x8b(ROOT_DEBUGGER)string id[RootType.DEBUGGER]
14 
0x8c(ROOT_REFERENCE_CLEANUP)string id[RootType.REFERENCE_CLEANUP]
14 
0x8d(ROOT_VM_INTERNAL)string id[RootType.VM_INTERNAL]
1444
0x8e(ROOT_JNI_MONITOR)string idthread serial numberstack frame number
14 
0x90(ROOT_UNREACHABLE)string id[RootType.UNREACHABLE]

0x20(ROOT_CLASS_DUMP)

0x20(ROOT_CLASS_DUMP)1
id4
stack serial number4
super class id4
class loader id4
signeres id4
protection domain id4
reserved4
reserved4
instance size4
const pool num entries2
2 * num entries 
static fields num entries2
static fieldsstatic fields num entries * (static fields),下面会再单独列出来
instance fields num entries2
instance fieldsinstance fields num entries * (instance fields)

0x20(ROOT_CLASS_DUMP).Static Fields

414
static fields idstatic fields typetype size

0x20(ROOT_CLASS_DUMP).Instance Fields

41
instance idinstance type

转载于:https://my.oschina.net/u/217380/blog/1507542

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值