[关闭]
@weixin 2015-04-17T22:41:07.000000Z 字数 2567 阅读 1317

compiler study 4 - crowbar debug and code analysis

compile

debug

on mac, the gdb has been replaced with lldb, here is some debug tips.

set alias for frequently used command

I wrote a file for command aliases, here is the script:

  1. command alias bfl breakpoint set -f %1 -l %2
  2. bfl main.c 21

then start program :

  1. lldb --source br_aliases crowbar test2.crb

--source would specify which file would be used.

static analysis

There is a very good tool for c code static analysis - doxygen
here is the webiste : doxygen, just follow the instruction, compile and install it. the path would be /usr/local/bin/doxygen.

how to use

  1. CALL_GRAPH = YES
  2. CALLER_GRAPH = YES
  3. RECURSIVE = YES
  4. EXTRACT_ALL = YES
  5. EXTRACT_STATIC = YES
  6. OUTPUT_DIRECTORY= ./call_graph

I make a directory named call_graph for holding all the doucments doxygen generated. be aware that you need dot, ie , graphviz as prerequsite.

dynamic analysis

using instruments

for linux user, there are some choices, such as gprof, codeviz and ncc, egypt, etc. but most of them are gnu based. on my mac, it is clang compiler so those tool could not be used. I tried to install egypt but failed, don't want to spend much time on it. so I choose use the osx instruments directly.

this is also a good tool for analysis the code.

using graphviz and pvtrace

  1. install graphviz
  2. install pvtrace
  3. add instrument.c into the project
  4. link instrument.o ,

    util.o\
    native.o\
    instrument.o\
    error.o\
  5. change compile flag

    #CFLAGS = -c -g -Wall -Wswitch-enum -ansi -pedantic -DDEBUG
    CFLAGS = -c -g -finstrument-functions -Wall -Wswitch-enum -ansi -pedantic -DDEBUG

    add -finstrument-functions flag
  6. rebuild the project
  7. run ./crowbar test/test.crb, then a file trace.txt would be generated, in this file, all the function enter and leave address is recorded.
  8. pvtrace crowbar, in fact, pvtrace call addr2line to find the symbol line # . unfortunately, mac doesn't have addr2line, the alternative atos doens't work. so I have to use vmware to start an opensuse box. a file, graph.dot would be generated.
  9. dot -Tpng graph.dot -o graph.png to generate a png file. it looks like this :
    call graph
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注