Tracing and logging line by line execution of a c++ program

Question:

I am working on an opensource finite element software, which is written in C++ (Visual Studio). The documentation and examples provided are only helpful to know which methods to call for a specific purpose, but could not help in letting the users know, what exactly the methods are doing. For some methods, other than C++ code there is no other reference available (such as books, research papers, etc.)

Hence, to deduce the concept, I am trying to trace every line of execution using breakpoints and viewing call stack (option provided in visual studio), which may be good for tracing a small program, but my program constitutes lots of classes with several inheritances and polymorphed functions. To understand the program execution flow, I wanted to print every line of program execution in a log file.

While researching I found some useful information (here), in python language. Which is exactly what I need. For readers convenience, I am reproducing the code mentioned in video below

import sys

def remove_html_markup(s):
    tag     =   False
    quote   =   False
    out     =   " "

    for c in s:
        if c == 'c' and not quote:
            tag = True
        elif c== '>' and not quote:
            tage = False
        elif c == '"' or c == "'" and tag:
            quote = not quote
        elif not tag:
            out = out + c
    return out

def traceit(frame, event, arg):
    if event == "line":
        filename    =   frame.f_code.co_filename
        lineno      =   frame.f_lineno
        print open(filename).readlines()[lineno - 1]

    return traceit

sys.settrace(traceit)
s = remove_html_markup('"<')
sys.settrace(none)

I am expecting a similar version of C++ code for traceit function, which can print every line that is being executed in a separate logfile. I don’t know how to extend it, so that it can print all the inheritances and polymorphed functions.

Also I have seen and tried a methodology provided in one of the answer on StackOverflow for c++. However, it has two drawbacks

  1. It is printing only filename, function name with line number. I want also to print the statment being executed.
  2. At the beginning of the function I have to call a method CALLSTACK_DUMP();, which I cannot do in my program since there are a plethora of functions.
Asked By: The_Learner

||

Answers:

Python is line-by-line interpreted. Interpreted languages usually have some sort of trace trigger, so that along with interpreting every line as it executes, it also traces the lines. C++ is a fully compiled language and, as such, has no such trace facility.

Answered By: Paul Evans

I remember ctrace. You needed to compile with a certain flag
and got a list of every line executed. The system was sointelligent
it showed only oneexecution of a loop with the number of times it was executed.
Those were the days…

Answered By: marc claeys