5

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.
The_Learner
  • 589
  • 1
  • 6
  • 16

2 Answers2

2

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.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • Thank you very much for the answer. If there is no trace facility, is there a way to log the call stack in visual c++ – The_Learner Jul 18 '19 at 04:45
  • @The_Learner It's a paradigm shift moving from interpreted to fully compiled languages. Inject print/log messages into critical places in your code to help you debug. Also learn how to work with your debugger - there's a step facility to walk through the source code as it executes. – Paul Evans Jul 18 '19 at 10:07
0

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...