libdwarf編程舉例

1. 本文件基於libdwarf的例子simplereader.c修改,用於從具有dwarf格式的調試信息的二進制文件(gcc使用-g編譯)中讀取各符號(包括函數符號和數據符號)的地址、大小等信息。

/*
  Copyright (c) 2009-2010 David Anderson.  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
  * Neither the name of the example nor the
    names of its contributors may be used to endorse or promote products
    derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY David Anderson ''AS IS'' AND ANY
  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL David Anderson BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
*/
/*  simplereader.c
    This is an example of code reading dwarf .debug_info.
    It is kept as simple as possible to expose essential features.
    It does not do all possible error reporting or error handling.


    The --names 
    option adds some extra printing.


    To use, try
        make
        ./simplereader simplereader
*/
#include <sys/types.h> /* For open() */
#include <sys/stat.h>  /* For open() */
#include <fcntl.h>     /* For open() */
#include <stdlib.h>     /* For exit() */
#include <unistd.h>     /* For close() */
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <fstream>
#include "dwarf.h"
#include "libdwarf.h"


#include "tool.h"
//using namespace qali;
using namespace std;


uint g_nNumOfGlobal = 0;
uint g_nNumOfLocal = 0;
uint g_nNumOfFile = 0;
std::map<uint, std::list<CData *> > g_DataMap;
std::set<string> g_UsedData;
std::vector<CData *> g_DataVec;
NameMangle g_NameMangle;
CTool g_Tool;


struct srcfilesdata {
    char ** srcfiles;
    Dwarf_Signed srcfilescount;
    int srcfilesres;
};


static void read_cu_list(Dwarf_Debug dbg);
static void print_die_data(Dwarf_Debug dbg, Dwarf_Die print_me,int level,
   struct srcfilesdata *sf);
static void get_die_and_siblings(Dwarf_Debug dbg, Dwarf_Die in_die,int in_level,
   struct srcfilesdata *sf);
static void resetsrcfiles(Dwarf_Debug dbg,struct srcfilesdata *sf);


static int namesoptionon = 0;


int main(int argc, char **argv)
{


    Dwarf_Debug dbg = 0;
    int fd = -1;
    const char *filepath = "<stdin>";
    int res = DW_DLV_ERROR;
    Dwarf_Error error;
    Dwarf_Handler errhand = 0;
    Dwarf_Ptr errarg = 0;


    if(argc < 2) {
        fd = 0; /* stdin */
    } else {
        int i = 0;
        for(i = 1; i < (argc-1) ; ++i) {
            if(strcmp(argv[i],"--names") == 0) {
                namesoptionon=1;
            } else {
                printf("Unknown argument \"%s\" ignored\n",argv[i]);
            }
        }
        filepath = argv[i];
        fd = open(filepath,O_RDONLY);  // Open the object file before dwarf_init
    }
    if(argc > 2) {
    }
    if(fd < 0) {
        printf("Failure attempting to open \"%s\"\n",filepath);
    }
    res = dwarf_init(fd,DW_DLC_READ,errhand,errarg, &dbg,&error); // Obtain the debug info as stored int dbg
    if(res != DW_DLV_OK) {
        printf("Giving up, cannot do DWARF processing\n");
        exit(1);
    }


    read_cu_list(dbg);      // read and print all compiler units
    res = dwarf_finish(dbg,&error);
    if(res != DW_DLV_OK) {
        printf("dwarf_finish failed!\n");
    }
    close(fd);      // Close the object file after dwarf_finish
    ofstream fout;
    fout.open("data.log");
    CTool::DumpData(fout);
    fout.close();
    return 0;
}


static void 
read_cu_list(Dwarf_Debug dbg)
{
    Dwarf_Unsigned cu_header_length = 0;
    Dwarf_Half version_stamp = 0;
    Dwarf_Unsigned abbrev_offset = 0;
    Dwarf_Half address_size = 0;
    Dwarf_Unsigned next_cu_header = 0;
    Dwarf_Error error;
    int cu_number = 0;


    for(;;++cu_number) {       
        Dwarf_Die no_die = 0;
        Dwarf_Die cu_die = 0;
        int res = DW_DLV_ERROR;
        res = dwarf_next_cu_header(dbg,&cu_header_length,
            &version_stamp, &abbrev_offset, &address_size,
            &next_cu_header, &error);            // get the offset of the next CU header
        if(res == DW_DLV_ERROR) {
            printf("Error in dwarf_next_cu_header\n");
            exit(1);
        }
        if(res == DW_DLV_NO_ENTRY) {
            /* Done. */
            return;
        }
        /* The CU will have a single sibling, a cu_die. */
        res = dwarf_siblingof(dbg,no_die,&cu_die,&error);  // get the first die (cu, pu, or tu) of dbg
        if(res == DW_DLV_ERROR) {
            printf("Error in dwarf_siblingof on CU die \n");
            exit(1);
        }
        if(res == DW_DLV_NO_ENTRY) {
            /* Impossible case. */
            printf("no entry! in dwarf_siblingof on CU die \n");
            exit(1);
        }
    // timon
    g_NameMangle.SetFile(cu_die);
    
        get_die_and_siblings(dbg,cu_die,0,NULL);      // travel the tree-grouped dies in pre-order
        dwarf_dealloc(dbg,cu_die,DW_DLA_DIE);
    
    // timon
    g_NameMangle.ResetFile();
    
    }
}


static void
get_die_and_siblings(Dwarf_Debug dbg, Dwarf_Die in_die,int in_level,
   struct srcfilesdata *sf)
{
    int res = DW_DLV_ERROR;
    Dwarf_Die cur_die=in_die;
    Dwarf_Die child = 0;
    Dwarf_Error error;
    
    print_die_data(dbg,in_die,in_level,sf);     // print out the die element first (root node first)
    


    for(;;) {
        Dwarf_Die sib_die = 0;
        res = dwarf_child(cur_die,&child,&error);
        if(res == DW_DLV_ERROR) {
            printf("Error in dwarf_child , level %d \n",in_level);
            exit(1);
        }
    
    // timon
    Dwarf_Error error1 = 0;
    Dwarf_Half tag1 = 0;
    int res1 = dwarf_tag(cur_die,&tag1,&error1);    // get the tag number
    if(res1 != DW_DLV_OK) {
      printf("Error in dwarf_tag \n");
      exit(1);
    }
    if( tag1 == DW_TAG_subprogram )
      g_NameMangle.SetFunc(cur_die);       
    // before child
        if(res == DW_DLV_OK) {       // foreach child-node, call the same function recursively (left first)      
            get_die_and_siblings(dbg,child,in_level+1,sf);
        }
    // after child
    // timon
    if( tag1 == DW_TAG_subprogram )
      g_NameMangle.ResetFunc();    
      
        /* res == DW_DLV_NO_ENTRY */
        res = dwarf_siblingof(dbg,cur_die,&sib_die,&error); // deal with the siblings last
        if(res == DW_DLV_ERROR) {
            printf("Error in dwarf_siblingof , level %d \n",in_level);
            exit(1);
        }
        if(res == DW_DLV_NO_ENTRY) {
            /* Done at this level. */
            break;
        }
        /* res == DW_DLV_OK */
        if(cur_die != in_die) {
            dwarf_dealloc(dbg,cur_die,DW_DLA_DIE);
        }
        cur_die = sib_die;
        print_die_data(dbg,cur_die,in_level,sf);
    }
    return;
}


// print out the die element
static void
print_die_data(Dwarf_Debug dbg, Dwarf_Die print_me,int level, struct srcfilesdata *sf)
{
    char *name = 0;
    Dwarf_Error error = 0;
    Dwarf_Half tag = 0;
    const char *tagname = 0;
    int localname = 0;
    char digits[8];


    int res = dwarf_tag(print_me,&tag,&error);    // get the tag number
    if(res != DW_DLV_OK) {
        printf("Error in dwarf_tag , level %d \n",level);
        exit(1);
    }
    sprintf(digits, "%d", tag);
    if( tag != DW_TAG_variable )          // only deal with variables
    return;
    
    // 0.1 if external?    
    Dwarf_Attribute attr;
    Dwarf_Bool bRet;
    bool bExternal = false;
    attr = CTool::GetAttribute(print_me,DW_AT_external,string("external"),false);    
    if( attr == NULL)
    bExternal = false;
    else
    {
    res = dwarf_formflag(attr, &bRet, &error );
    if(res != DW_DLV_OK ) {
      printf("Error in getting external value\n");
      exit(1);
    }    
    bExternal = bRet;
    }
    
    // 0.2 if declaration?
    bool bDeclared = false;
    attr = CTool::GetAttribute(print_me,DW_AT_declaration,string("declaration"),false);    
    if( attr == NULL)
    bDeclared = false;
    else
    {
    res = dwarf_formflag(attr, &bRet, &error );
    if(res != DW_DLV_OK ) {
      printf("Error in getting declaration value\n");
      exit(1);
    }    
    bDeclared = bRet;
    }
    // 1. get name    
    std::string szName = g_NameMangle.GetName(print_me, bExternal);
    if(szName.find("NoName") != string::npos)     // some variable has several variants, and not considered
    return;
    
    // 2. get address
    if( bDeclared )       // declared variables are not considered
    return;
    uint addr = 0;
    attr = CTool::GetAttribute(print_me, DW_AT_location, string("location"),false);
    if( attr == NULL)      // in optimized code, some symbols are removed
    addr = CTool::NON_ADDRESS;
    else
    {
    Dwarf_Block *pBlock;
    res = dwarf_formblock(attr, &pBlock, &error);
    if(res != DW_DLV_OK)
    {
      printf("Warning:\tno location value of %s; perhaps for the dynamic address-list\n", szName.c_str());  // error for getting address value
      addr = CTool::NON_STATIC_ADDRESS;
    }    
    else
    {    
      if( *(char *)(pBlock->bl_data) != 0X03 )    // if not static address, mark it by zero
       addr = CTool::NON_STATIC_ADDRESS;
      else 
       addr = *(int *)(pBlock->bl_data+1);
    }
    }
    // 3. get size
    attr = CTool::GetAttribute(print_me, DW_AT_type, string("type")+szName, true );
    Dwarf_Off offset;
    res = dwarf_global_formref(attr, &offset, &error);
    if( res != DW_DLV_OK) 
    {
    printf("Error in getting type value for %s\n", szName.c_str()); 
    exit(1);    
    }    
    
    Dwarf_Die typeDie = 0;
    res = dwarf_offdie(dbg, offset, &typeDie, &error);
    if( res != DW_DLV_OK)
    {
    printf("Error in getting die of type for %s\n", szName.c_str()); 
    exit(1);
    }
    uint size = CTool::GetTypeSize(dbg, typeDie, szName+digits);
    
    // 4. build a data record
    //string oriName = g_NameMangle.GetDieName(print_me);
    if( g_UsedData.find(szName) == g_UsedData.end() )
    {
    if( szName.find("%%") != string::npos)
      ++g_nNumOfFile;
    else if( szName.find("##") != string::npos)
      ++ g_nNumOfLocal;
    else
      ++g_nNumOfGlobal;
    CData *pData = new CData(szName, addr, size, level ); 
    pData->SetFile(g_NameMangle.GetDieName(g_NameMangle.m_fileDie));
    g_DataMap[addr].push_back(pData);
    g_UsedData.insert(szName);
    }
    printf("tag_level<%d>,\tname<\"%s\">,\taddress<%x>,\tsize<%d>\n", level,szName.c_str(),addr,size);   
}
2. 本文件爲輔助文件,幫助實現上個文件的功能。
/*  tool.cpp/tool.h
    This is a file implementing some basic functions for helping the functions of the file above.
    
For each data symbol, the following information are collected:         name, type, address, size
*/

#ifndef _TOOL_H
#define _TOOL_H

#include <sys/types.h> /* For open() */
#include <sys/stat.h>  /* For open() */
#include <fcntl.h>     /* For open() */
#include <stdlib.h>     /* For exit() */
#include <unistd.h>     /* For close() */
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "dwarf.h"
#include "libdwarf.h"

#include <ostream>
#include <string>
#include <assert.h>


//namespace qali/
//{
typedef unsigned int uint;

class NameMangle
{
	public:
	
	NameMangle() {ResetFile(); ResetFunc();};
	
	void SetFile(Dwarf_Die die ) { m_fileDie = die;}
	void SetFunc(Dwarf_Die die) { m_funcDie = die;}
	void ResetFile() { m_fileDie = 0;};
	void ResetFunc() { m_funcDie = 0;};
	std::string GetGlobal(Dwarf_Die);
	std::string GetFile(Dwarf_Die);
	std::string GetLocal(Dwarf_Die);
	std::string GetName(Dwarf_Die, bool bExternal = true);

public:
	
	Dwarf_Die	m_fileDie;
	Dwarf_Die	m_funcDie;
	

	std::string GetDieName(Dwarf_Die die);
	
};

class CData
{
	public:
	CData(){};
	CData(std::string &szName, uint nAddr, uint nSize = 0, uint nLevel=1) 
	{
		m_szName = szName;
		m_nAddr = nAddr;
		m_nSize = nSize;
		m_nLevel = nLevel;
	};
	
	void SetFile(std::string szFile) { m_szFile = szFile; }
	
	public:
	std::string m_szName;
	uint m_nAddr;
	uint m_nSize;
	uint m_nLevel;
	
	std::string m_szFile;
};

class CTool
{
	public:
	enum ADDR
	{
		NON_ADDRESS = -1,
		NON_STATIC_ADDRESS =0
	};
	public:
	
	static Dwarf_Attribute GetAttribute(Dwarf_Die die, int nAttribute, std::string szInfo, bool bNeedAttri);
	static uint GetTypeSize(Dwarf_Debug dbg, Dwarf_Die die, std::string szInfo);
	static uint GetArrayLength(Dwarf_Debug, Dwarf_Die die, uint extLen);
	
	static Dwarf_Die GetAttrDie(Dwarf_Debug dbg, Dwarf_Die die, std::string szInfo);
	
	// Query function: from address to symbol
	static string QueryAddress(uint nAddr);
	
	static int DumpData(std::ostream &os);
	static std::string DupChar(uint num, char c);
};
#endif
#include "tool.h"
#include <map>
#include <list>


extern std::map<uint, std::list<CData *> > g_DataMap;
extern uint g_nNumOfGlobal;
extern uint g_nNumOfLocal;
extern uint g_nNumOfFile;
//using namespace qali;


using namespace std;


string NameMangle::GetGlobal(Dwarf_Die die)
{
    assert( m_fileDie != 0 );
    string szName = GetDieName(die);
    string szFile = GetDieName(m_fileDie);
    
    return szName =/* szFile+ ": " +*/ szName;
    return GetDieName(die);
}


string NameMangle::GetName(Dwarf_Die die, bool bExt)
{
    if( m_funcDie != 0)
    {    
    return GetLocal(die);    
    }
    if( !bExt)
    {    
    return GetFile(die);
    }
    return GetGlobal(die);
}


string NameMangle::GetFile(Dwarf_Die die)
{
    assert( m_fileDie != 0 );
    string szName = GetDieName(die);
    string szFile = GetDieName(m_fileDie);
    
    return szName = /*szFile+ ": " + */szFile + "%%" + szName;
}


string NameMangle::GetLocal(Dwarf_Die die)
{
    assert( m_fileDie != 0 && m_funcDie != 0);
    string szName = GetDieName(die);
    string szFunc = GetDieName(m_funcDie);
    string szFile = GetDieName(m_fileDie);
    //string szFile = GetDieName(m_fileDie);
    
    return szName =  /*szFile+ ": " +*/ szFunc + "##" + szName;
}


string NameMangle::GetDieName(Dwarf_Die die)
{
    Dwarf_Error error = 0;   
    string szName;
    char *name;    
     Dwarf_Half tag = 0;
    int res = dwarf_diename(die,&name,&error);    


    if(res == DW_DLV_ERROR) {
        printf("Error in dwarf_diename\n");
        exit(1);
    }
    if(res == DW_DLV_NO_ENTRY) {
        name = "NoName";
    res = dwarf_tag(die,&tag,&error);    // get the tag number
    if(res != DW_DLV_OK) {
      printf("Error in dwarf_tag\n");
      exit(1);
    }
      
    printf("Waring:\t no name for tag %d\n", tag);
    }
    szName = name;   
    return szName;
}


Dwarf_Attribute CTool::GetAttribute(Dwarf_Die die, int nAttri, string szInfo, bool bNeedAttri=false)
{
    Dwarf_Error error = 0;
    int res;
    Dwarf_Bool bRet;
    res = dwarf_hasattr(die,nAttri, &bRet, &error);  // no attribute
    if( res != DW_DLV_OK || !bRet) 
    {
    if(bNeedAttri)
    {
      printf("Error in no %s attribute \n", szInfo.c_str()); 
      exit(1);
    }
    return NULL;
    }    
    
    Dwarf_Attribute attr;
    res = dwarf_attr(die, nAttri, &attr, &error );  // error for getting location
    if(res != DW_DLV_OK ) {
        printf("Error in getting %s attribute\n", szInfo.c_str());
    exit(1);
    }
    return attr;
}


uint CTool::GetTypeSize(Dwarf_Debug dbg, Dwarf_Die die, string szInfo)
{
    Dwarf_Error error = 0;
    Dwarf_Half tag = 0;
    uint size = 1;
    
    int res = dwarf_tag(die, &tag, &error);
    if( res != DW_DLV_OK )
    {
    printf("Error in tag\n");
    exit(1);
    }    
    
    Dwarf_Attribute attr;
    if( tag == DW_TAG_array_type )
    {
    // get original type of array-type
    Dwarf_Die typeDie = CTool::GetAttrDie(dbg, die, string("type of array-type for ")+szInfo);    
    uint oriSize = GetTypeSize(dbg, typeDie, string("size of type of array type for ") + szInfo );
    
    // the child(ren) of an array-type is a subrange-type
    Dwarf_Die child;
    res = dwarf_child(die,&child,&error);    
    if(res != DW_DLV_OK) 
    {
      printf("Error in getting a child die of array-type\n");
      exit(1);
    }
    res = dwarf_tag(child, &tag, &error);
    if( res != DW_DLV_OK || tag != DW_TAG_subrange_type )
    {
      printf("Error a non-subgrange child of a subrange_type\n");
      exit(1);
    }    
    return oriSize * CTool::GetArrayLength(dbg, child, 1);
    }
    else if( tag == DW_TAG_typedef)
    {
    Dwarf_Die typeDie = CTool::GetAttrDie(dbg, die, string("type of typedef")+szInfo);
    
    return GetTypeSize(dbg, typeDie, string("size of type of typedef") + szInfo);
    }
    else if ( tag == DW_TAG_const_type )
    {
    Dwarf_Die typeDie = CTool::GetAttrDie(dbg, die, string("type of const")+szInfo);
    
    return GetTypeSize(dbg, typeDie, string("size of type of const") + szInfo);
    }
    else
    {
    //assert(tag == DW_TAG_base_type || tag == DW_TAG_structure_type);
    attr = GetAttribute(die, DW_AT_byte_size, string("byte_size for ") + szInfo, true );
    Dwarf_Unsigned size = 0;
    res = dwarf_formudata(attr, &size, &error);
    if( res != DW_DLV_OK) 
    {
      printf("Error in getting size of %s \n", szInfo.c_str());  
      exit(0);
    }
    return size;
    }
}


// getting the (type) attribute as a die
Dwarf_Die CTool::GetAttrDie(Dwarf_Debug dbg, Dwarf_Die die, string szInfo)
{
    Dwarf_Error error = 0;
    Dwarf_Half tag = 0;
    Dwarf_Attribute attr;
    attr = GetAttribute(die, DW_AT_type, string("type for ")+szInfo, true );
    Dwarf_Off offset;
    int res = dwarf_global_formref(attr, &offset, &error);
    if( res != DW_DLV_OK) 
    {
    printf("Error in getting type value of type for %s\n", szInfo.c_str()); 
    exit(1);    
    }    
    
    // the type die of this type attribute
    Dwarf_Die typeDie = 0;
    res = dwarf_offdie(dbg, offset, &typeDie, &error);
    if( res != DW_DLV_OK)
    {
    printf("Error in getting die of type of type-type for %s\n", szInfo.c_str()); 
    exit(1);
    }
    return typeDie;
}


uint CTool::GetArrayLength(Dwarf_Debug dbg, Dwarf_Die die, uint extLen)
{
    Dwarf_Error error = 0;
    Dwarf_Half tag = 0;
    Dwarf_Die sible;
    uint len = 0;
    
    // get bound
    Dwarf_Attribute attr;
    attr = GetAttribute(die, DW_AT_upper_bound, string("upper bound"), true);
    Dwarf_Unsigned bound = 0;
    int res = dwarf_formudata(attr, &bound, &error);
    if( res != DW_DLV_OK )
    {    
    printf("Error in getting upper bounoned value of subrange_type\n");
    exit(1);    
    }
    len = bound + 1;
    
    // test siblings
    while( dwarf_siblingof(dbg,die,&sible,&error) != DW_DLV_NO_ENTRY)
    {
    // exist?    
    if(res == DW_DLV_ERROR) 
    {
      printf("Error in getting a child die \n");
      exit(1);
    }
    
    // subrange-type?
    res = dwarf_tag(sible, &tag, &error);
    if( res != DW_DLV_OK || tag != DW_TAG_subrange_type )
    {
      printf("Error a non-subgrange child of a subrange_type\n");
      exit(1);
    }    
    
    // uppper bound?
    attr = GetAttribute(sible, DW_AT_upper_bound, string("upper bound"), true);
    int res = dwarf_formudata(attr, &bound, &error);
    if( res != DW_DLV_OK )
    {    
      printf("Error in getting upper bound value of subrange_type\n");
      exit(1);    
    }
    len *= bound + 1;
    die = sible;
    }
    return len;
}


string CTool::QueryAddress(uint nAddr)
{
    string symbol = "";
    map<uint, list<CData *> >::iterator u2d_p = g_DataMap.begin(), u2d_e = g_DataMap.end();
    
    if( nAddr < u2d_p->first )
    return symbol;
    
    bool flag = false;
    for(; u2d_p != u2d_e; ++ u2d_p)
    {
    next_p = u2d_p;
    ++ next_p;
    if( nAddr == u2d_p->first)
    {
      symbol = u2d_p->second.front()->m_szName;
      flag = true;
      break;
    }
    else if( nAddr > u2d_p->first)
    {      
      CData *pData = u2d_p->second.front();
      if( nAddr <= pData->m_nAddr + pData->m_nSize - 1)
      {
       symbol = u2d_p->second.front()->m_szName;
       flag = true;
       break;
      }      
    }
    else
    {
      flag = false;
      break;
    }
    }
    return symbol;
}


int CTool::DumpData(std::ostream &os)
{
    uint nTotal = g_nNumOfGlobal + g_nNumOfFile + g_nNumOfLocal;
    os << "Number of global:\t" << g_nNumOfGlobal << "(" << g_nNumOfGlobal*100/nTotal<< "%)\n";
    os << "Number of file-static:\t" << g_nNumOfFile << "(" << g_nNumOfFile*100/nTotal<< "%)\n";
    os << "Number of Local:\t" << g_nNumOfLocal << "(" << g_nNumOfLocal*100/nTotal<< "%)\n";
    
    std::map<uint, list<CData *> >::iterator u2d_p = g_DataMap.begin(), u2d_e = g_DataMap.end();
    for(; u2d_p != u2d_e; ++ u2d_p)
    {
    std::list<CData *>::iterator d_p = u2d_p->second.begin(), d_e = u2d_p->second.end();
    for( ; d_p != d_e; ++ d_p)
    {
      CData *pData = *d_p;
      uint nlen = pData->m_szFile.size() + pData->m_szName.size() + 3;
      os << pData->m_szFile << ": " << pData->m_szName << ":" << DupChar(6-nlen/8, '\t');
      if(pData->m_nAddr == CTool::NON_STATIC_ADDRESS)
       os << hex << "0x???????" << "------0x???????(" << dec << pData->m_nSize << ")" << endl;
      else if (pData->m_nAddr == CTool::NON_ADDRESS)
       os << hex << "0x#######" << "------0x#######(" << dec << pData->m_nSize << ")" << endl;
      else
       os << hex << "0x" << pData->m_nAddr << "------0x" << pData->m_nAddr + pData->m_nSize - 1 <<  dec << "(" << pData->m_nSize << ")" << endl;
    }
    }
    return 0;
}


string CTool::DupChar(uint num, char c)
{
    string str = "";
    for(int i = 0; i < num ; ++i)
    str += c;
    return str;
}





發佈了41 篇原創文章 · 獲贊 8 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章