Click here to Skip to main content
Click here to Skip to main content

A Simple Profiler using the Visual Studio C/C++ Compiler and DIA SDK

, 15 Dec 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Easy to use profiler for time and impact analysis of C/C++ code which uses the Visual Studio C/C++ compiler (/Gh and /GH flags) and the DIA SDK to gather profiling data.


As a C++ programmer, I always end up writing code for products which are either add-on applications or third-party DLLs to the main application. So as a developer, it's important for me to ensure that my code does not degrade the performance of the main application or product. Generally, I use commercial products like IBM Rational Product Suite or Bounds Checker. But at times, these commercial tools are not useful as the code (DLL) which I write relies on the main application as the main application is not at all modifiable for profiling. The main motive behind this is I should get the time taken by each function in my code and the number of times that function is called. In short, I want to do an impact analysis of my code so that I can get input for performance improvements. Thus, I can ensure that the overall performance of the main application or product is maintained.


I happen to discuss this problem with one of my friends who is also an experienced C/C++ programmer. He suggested me that the Visual Studio C++ compiler has some flags which can be used for writing function specific code. Those compiler flags are /Gh and /GH. The /Gh flag causes a call to the _penter function at the start of every method or function, and the /GH flag causes a call to the _pexit function at the end of every method or function. So, if I write some code in these methods to find out the caller function, then I would gather the stack trace information. Also, if I write the code to start the timer in the _penter function and stop the corresponding timer in the _pexit function, then I would roughly measure the time taken by the method or function to execute. But, it's very important to understand the _penter and _pexit functions before writing any code. MSDN states that the _penter and _pexit functions are not part of any library, and it is up to the developer to provide a definition for _penter and _pexit. So, I decided to write my own DLL in which I would provide the definition of _penter and _pexit, and at the same time, these two functions would be exported from the DLL. The prototype is as follows:

void __declspec(naked) _cdecl _penter( void);
void __declspec(naked) _cdecl _pexit( void);

These methods are defined as __declspec(naked) and _cdecl, which means the implementation should push the content of all the registers on entry, and pop the unchanged content on exit. Also, objects can not be instantiated inside the function body, and only global or static variables can be used inside the function body. We can only call global or static methods from inside the function body. Keeping all this in mind, I created a singleton Profiler class which will have the necessary methods to collect the time profiling data. I used the C++ inline assembler feature to implement _penter and _pexit. The sample implementation is as follows:

extern "C" void __declspec(naked) _cdecl _penter( void ) 
        //Prolog instructions
        //calculate the pointer to the return address by adding 4*8 bytes 
        //(8 register values are pushed onto stack which must be removed)
        mov  eax, esp
        add  eax, 32
        // retrieve return address from stack
        mov  eax, dword ptr[eax]
        // subtract 5 bytes as instruction for call _penter
        // is 5 bytes long on 32-bit machines, e.g. E8 <00 00 00 00>
        sub  eax, 5
        // provide return address to recordFunctionCall
        push eax
        call enterFunc
        pop eax

        //Epilog instructions

The interesting part in the implementation is how a non naked global function, i.e., enterFunc is called with the caller function virtual address as an argument. After writing the Prolog instruction, travel the stack by adding 32 bytes from the current stack pointer. Then, get the return address from that virtual address by a pointer operation. Now, subtract 5 bytes from that address, which will get the virtual address from the caller function body. This virtual address is passed as an argument to the global function which does further processing. The time keeping part is solved with this arrangement. But, what about the function name? How should I get the function name from a virtual address from any function body?

I solved this name problem by using the DIA (Debug Interface Access) SDK. The DIA SDK has a unified model to access or query any symbol and its properties from PDB files. So, to use this profiler, it's important, rather mandatory, that the DLL or EXE should have Debugging Information (the PDB file).

I implemented getFunc in the Profiler class which finds out the function name from the given virtual address. The process is as follows:

  • Get the current process handle using the GetCurrentProcess function
  • Get all the loaded modules of the current process using the EnumProcessModules function
  • Check if the given virtual address belongs to any modules using its address space size and load address
  • Get the module file path from the module handle if the given virtual address belongs to that module, using GetModuleFileNameEx
  • Load the PDB file from the module file path using the loadDataForExe method of IDiaDataSource
  • Use the openSession method of IDiaDataSource to get IDiaSession and use the put_loadAddress method of IDiaSession to setup the symbol database for the query
  • Now, query the IDiaSession object using the findSymbolByVA method which would return IDiaSymbol, i.e., the function having the given virtual address in its body
  • Get the function name from the IDiaSymbol object using the get_name method

Using the Code

The Profiler described in this article uses the DIA SDK, so if you want to use this profiler, then it's mandatory that the project (LIB/DLL/EXE) to be profiled should generate debugging information.

To generate debugging information for a project, go to the respective Project's General property page which is under the C/C++ tab and set the Debug Information Format to Program Database(/Zi)

Also, for the Debugging property page which is under the Linker tab, set the Generate Debug Info to Yes(/Debug). These two settings will ensure that the PDB file is created for the project to be profiled.

After setting the project to generate a PDB file, set the /Gh and /GH flags in Additional options of the Command Line property page under the C/C++ tab, as shown below.

Add profiler.lib (export library from the profiler project) in Additional Dependencies of the Input property page under the Linker tab. This is an important setting for the profiler to work as profiler.lib/dll has the _penter and _pexit implementations.

Provide the path to profiler.lib in Additional Library Directories of the Linker property page. This setting would be developer dependent.

If the user wants to view the profiling result in the form of a CSV file, then set the PROFILER_LOG environment variable with the CSV file path. After the complete run of the main application, the profiling data is saved to the specified CSV file.

As depicted earlier, the profiler CSV file contains the executed function/method name, how many times it is called, the total time taken by the function and its child functions in milliseconds, the time taken by the function, i.e., self time, and the time taken by child functions.


The /Gh and /GH compiler flags are supported only on the Win32 platform, so the current profiler will not be useful for native 64 bit applications.

Scope for Improvement

The Simple Profiler discussed in this article is complete in itself, but it can be further improved. I can think of the following ways to improve it or make it more developer friendly:

  • Can use a multimedia timer instead of the default clock to get precise time
  • A memory profiler can be added
  • A Visual Studio add-in or macro can be created for quick and all time profiling for a complete solution


Here is the list of help which I took while coding this article:


  • 15 Dec. 2009 - Article first posted to The Code Project.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nikhil Soman
Technical Lead Geometric Ltd
India India
C/C++ practitioner with more than 5 years of experience in 3D Visualization.

Comments and Discussions

Questionsomep problem with _pexit PinmemberYin laiyier23-Oct-14 0:14 
QuestionFilter library functions Pinmembersyed_babu7-Jul-14 4:09 
QuestionLogs in Output Window Pinmembersyed_babu13-Feb-13 18:48 
AnswerRe: Logs in Output Window PinmemberNikhil Soman13-Feb-13 23:32 
Questionfunction parameter values Pinmembercolumn.column20-Nov-12 0:36 
AnswerRe: function parameter values PinmemberNikhil Soman27-Nov-12 19:40 
QuestionCant see CSV file :( Pinmemberparikshitnangre14-Oct-12 20:50 
Hi Nikhil,
This is parikshit, ex 3dplm employee Smile | :)
I downloaded all 3 Download demo - 914 KB, Download demo project - 20.6 KB, Download source - 9.05 KB things.
As per my understanding to use it, we need to first compile the Simple_Profiler_Src so that we can have a profiler.lib which need to be put in Simple_Profiler_Demo_Project for build purpose.
To use it i opened Simple_Profiler_Src in Visual studio 2010 abd build it. I got the following error -
error C1083: Cannot open include file: 'dia2.h': No such file or directory profiler\simple_profiler_src\profiler\includes\includes.h
To get rid of this error i serached for dia2.h in Program Files , i found it in C:\Program Files (x86)\Microsoft Visual Studio 10.0\DIA SDK\include folder.
So i changed the include statement #include <dia2.h> to
#include <C:\Program Files (x86)\Microsoft Visual Studio 10.0\DIA SDK\include\dia2.h>
then i got the linking error as follows -
error LNK1104: cannot open file 'diaguids.lib' C:\Users\sinpgr\Desktop\Profiler\Simple_Profiler_Src\profiler\mak\LINK
to remove this, i changed settings - Project--> Profiler Properties-->Linker-->Input-->Additional Dependencies : diaguids.lib to
C:\Program Files (x86)\Microsoft Visual Studio 10.0\DIA SDK\lib\diaguids.lib
and build it, it builds successfully !!
and created following files in C:\Users\sinpgr\Desktop\Profiler\Simple_Profiler_Src\profiler\lib\Debug folder -
profiler.dll, profiler.exp, profiler.ilk, profiler.lib & profiler.pdb.
i copied all these 5 files and pasted them in Simple_Profiler_Demo_Project\profiler\lib\Debug folder. Then i set the env variable PROFILER_LOG = "C:\Temp\1.CSV" as you mentioned then i build the project and executed it.
But i cant see I the CSV file created in c\temp.To try i also set the same variable in Simple_Profiler_Src. But it didn't work.
Whats is going wrong. Please reply soon.
Thanks in advance,
AnswerRe: Cant see CSV file :( PinmemberNikhil Soman14-Oct-12 20:55 
GeneralRe: Cant see CSV file :( Pinmemberparikshitnangre16-Oct-12 3:07 
GeneralRe: Cant see CSV file :( Pinmemberparikshitnangre18-Oct-12 21:32 
QuestionPossibility to exclude some functions from profiling? PinmemberMi-La13-Mar-12 0:03 
GeneralLimit for time to run a function Pinmemberanupw7-Dec-10 20:11 
GeneralThis source is not compiled PinmemberBatiat22-Aug-10 9:42 
GeneralRe: This source is not compiled PinmemberNikhil Soman22-Aug-10 19:42 
GeneralRe: This source is not compiled Pinmemberleebs01177-Aug-11 17:04 
QuestionSo how do you use it? Pinmemberndwork2-Apr-10 22:17 
GeneralAnother interesting project around profiling. Pinmemberpapaathome5-Jan-10 5:28 
GeneralSome confusion on recursion. Pinmemberpapaathome4-Jan-10 10:13 
GeneralRe: Some confusion on recursion. Pinmembernikhil soman4-Jan-10 22:04 
GeneralRe: Some confusion on recursion. Pinmemberpapaathome5-Jan-10 0:56 
Generalno function names in csv output Pinmemberpapaathome2-Jan-10 14:01 
GeneralRe: no function names in csv output Pinmembernikhil soman4-Jan-10 2:50 
GeneralRe: no function names in csv output Pinmemberpapaathome4-Jan-10 8:53 
GeneralRe: no function names in csv output Pinmembernikhil soman4-Jan-10 18:59 
GeneralRe: no function names in csv output Pinmemberpapaathome5-Jan-10 0:32 
QuestionDoes it support multi-thread? PinmemberMember 436432125-Dec-09 2:23 
AnswerRe: Does it support multi-thread? Pinmembernikhil soman25-Dec-09 5:31 
GeneralRe: Does it support multi-thread? Pinmember_kummer22-Jan-10 3:34 
GeneralIntroduction to DIA (Debug Interface Access) SDK [modified] Pinmembermarc ochsenmeier21-Dec-09 21:14 
Generaldeque iterator not dereferencable Pinmembersalciccia18-Dec-09 1:57 
GeneralRe: deque iterator not dereferencable Pinmembernikhil soman18-Dec-09 2:17 
GeneralRe: deque iterator not dereferencable Pinmembersalciccia18-Dec-09 5:48 
GeneralRe: [found] deque iterator not dereferencable Pinmembersalciccia18-Dec-09 6:08 
GeneralRe: [found] deque iterator not dereferencable Pinmembernikhil soman18-Dec-09 20:34 
GeneralRe: [found] deque iterator not dereferencable Pinmembersalciccia18-Dec-09 22:34 
QuestionRe: [found] deque iterator not dereferencable PinmemberTrowers6-Feb-10 11:03 
QuestionWhere is the output from the demo? PinmemberSeikilos16-Dec-09 22:42 
AnswerRe: Where is the output from the demo? Pinmembernikhil soman16-Dec-09 22:47 
GeneralRe: Where is the output from the demo? PinmemberSeikilos16-Dec-09 23:25 
GeneralRe: Where is the output from the demo? Pinmembernikhil soman17-Dec-09 0:02 
QuestionRe: Where is the output from the demo? PinmemberCpp guy11-Jan-10 20:43 
AnswerRe: Where is the output from the demo? Pinmembernikhil soman11-Jan-10 22:47 
GeneralRe: Where is the output from the demo? PinmemberMark W. Park28-Jan-10 10:50 
GeneralRe: Where is the output from the demo? Pinmembernikhil soman28-Jan-10 18:51 
AnswerRe: Where is the output from the demo? PinmemberMark W. Park29-Jan-10 6:19 
GeneralRe: Where is the output from the demo? Pinmemberpuneet130121-May-11 3:45 
GeneralBroken links Pinmemberr.guerzoni15-Dec-09 1:05 
GeneralRe: Broken links Pinmembernikhil soman15-Dec-09 1:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 15 Dec 2009
Article Copyright 2009 by Nikhil Soman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid