ios - 如何为在XCode中以 Debug模式运行的iOS应用程序创建整个内存转储

标签 ios xcode memory lldb dump

我有一个在Xcode中以 Debug模式运行的应用程序。我想检查应用程序的整个内存(堆栈和堆)。我知道我可以使用lldb进行转储。我使用以下命令:

(lldb) memory read --outfile filename address

例如。
(lldb) memory read --outfile /tmp/mem-dump.txt --force --count 10000 0x000000010d051000

,但我需要指定起始地址和内存大小。我不知道如何查找我的应用程序进行转储所占用的内存区域。是否有可能找到存储器的地址空间?也许存在其他使转储(不使用lldb)的方法?我不使用越狱设备。

最佳答案

使用lldb python脚本桥接界面。即使应用程序在物理设备而不是模拟器上运行,它也允许访问内存区域信息和内存数据本身。无需更改应用程序。

要访问内存:

  • 将以下python脚本复制并保存到Mac。让我们将其命名为mem.py

  • import lldb
    
    def processAllMemoryRegions():
        process = lldb.debugger.GetSelectedTarget().GetProcess()
        memoryRegionInfoList = process.GetMemoryRegions()
        numberOfMemoryRegions = memoryRegionInfoList.GetSize()
        memoryRegionIndex = 0
        while (memoryRegionIndex < numberOfMemoryRegions):
            memoryRegionInfo = lldb.SBMemoryRegionInfo()
            success = memoryRegionInfoList.GetMemoryRegionAtIndex(memoryRegionIndex, memoryRegionInfo)
            if success:
                print("Processing: "+str(memoryRegionIndex+1)+"/"+str(numberOfMemoryRegions))
                processOneMemoryRegion(process, memoryRegionInfo)
            else:
                print("Could not get memory at index: "+str(memoryRegionIndex))    
            memoryRegionIndex = memoryRegionIndex+1
    
    def processOneMemoryRegion(process, memoryRegionInfo):
        begAddressOfMemoryRegion = memoryRegionInfo.GetRegionBase()
        endAddressOfMemoryRegion = memoryRegionInfo.GetRegionEnd()
        if memoryRegionInfo.IsReadable():
            print("Beg address of a memory region: "+stringifyMemoryAddress(begAddressOfMemoryRegion))
            print("End address of a memory region: "+stringifyMemoryAddress(endAddressOfMemoryRegion))
            error = lldb.SBError()
            regionSize = endAddressOfMemoryRegion-begAddressOfMemoryRegion
            memoryData = process.ReadMemory(begAddressOfMemoryRegion, regionSize, error)
            if error.Success():
                #do something with memoryData (bytearray) eg. save it to file
                pass
            else:
                print("Could not access memory data.")
        else:
            print("Memory region is not readable.")
    
    def stringifyMemoryAddress(memoryAddress):
        return '0x{:016x}'.format(memoryAddress)
    
  • 停止在断点上执行应用程序

  • 在lldb窗口中
  • 输入:脚本
  • 导入python脚本:exec(open('/absolute/path/to/mem.py')。read())
  • 输入:processAllMemoryRegions()

  • enter image description here

    大功告成

    您可以使用process.ReadMemory函数读取内存。检查上面剪下的 sample 。

    您也可以尝试使用以下命令启动脚本执行:
    (lldb) command script import  ~/path/to/script.py
    

    它描述为herehere,...但是在我的情况下,这导致XCode卡住。描述here

    在Xcode 11.3.1上测试过的代码。

    关于ios - 如何为在XCode中以 Debug模式运行的iOS应用程序创建整个内存转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62098874/

    相关文章:

    ios - 如何使用 XCUIKeyboardKey 常量?

    ios - 无法将类型 'UITableViewController' 的值转换为 'UINavigationController'

    ios - UICollectionView 崩溃

    c - 为什么我不能释放那些大内存块

    c++ - 为什么内存访问这么慢?

    ios - ShareKit "SHK.h not found"从 zip 下载

    c++ - MRB_THROW 导致 libc++abi.dylib 出错

    objective-c - 设备旋转后 View 被压扁和扭曲

    ios - 无效的 Swift 支持 - 文件不匹配

    android - 如何安全归零并释放 Android 应用程序使用的所有内存页面?