gprof : How to generate call graph for functions in shared library that is linked to main program

前端 未结 3 492
一个人的身影
一个人的身影 2020-12-24 12:44

I am working on Linux environment. I have two \'C\' source packages train and test_train.

  1. train package when compiled generates libtrain.so
  2. test_train
3条回答
  •  长发绾君心
    2020-12-24 13:19

    gprof won't work, you need to use sprof instead. I found these links helpful:

    • How to use sprof?
    • http://greg-n-blog.blogspot.com/2010/01/profiling-shared-library-on-linux-using.html

    Summary from the 2nd link:

    1. Compile your shared library (libmylib.so) in debug (-g) mode. No -pg.
    2. export LD_PROFILE_OUTPUT=`pwd`
    3. export LD_PROFILE=libmylib.so
    4. rm -f $LD_PROFILE.profile
    5. execute your program that loads libmylib.so
    6. sprof PATH-TO-LIB/$LD_PROFILE $LD_PROFILE.profile -p >log
    7. See the log.

    I found that in step 2, it needs to be an existing directory -- otherwise you get a helpful warning. And in step 3, you might need to specify the library as libmylib.so.X (maybe even .X.Y, not sure) -- otherwise you get no warning whatsoever.

提交回复
热议问题