Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to print all source code what was running?

How to print all source code what was running?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
33 Posts 7 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U User 13049321

    Thanks,but I sure "

    A line by line code execution logger does make sense for me

    I have very large storage capacity, and I can wait for long time to output.

    S Offline
    S Offline
    Stefan_Lang
    wrote on last edited by
    #23

    No it doesn't. You don't understand what you're asking for. Even if it would kind of work out for the very simple program you are looking at right now, it won't help you beyond that: the next time you're trying to understand some code, it won't be good enough, and you will eventually realize that you wasted a lot of time with a tool that, in the end, doesn't really help. Please take the advice from people who have spent decades on programming and using tools. The debugger is the second tool I learned to use after the editor, and it is still the most helpful tool I use today. Without it, large scale applications wouldn't be possible, and smaller ones would cost orders of magnitude more money and time to develop. With a debugger you can do the inspection you're asking for at runtime, and a lot more. You won't need to write one yourself, nyou won't have to wait for it's output to be written, and you won't waste ludicrous amouunts of storage space. It's better in any way you can think of.

    GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

    1 Reply Last reply
    0
    • U User 13049321

      Thanks, but I want to find a way that don't add any code like DEBUG_LOG(), to print source code. because the source code too many. I wouldn't be do this otherwise.

      S Offline
      S Offline
      Stefan_Lang
      wrote on last edited by
      #24

      This response shows that you have really no understanding of what a debugger does, and how to use it. A debugger does not require you to add anything to your code at all. There's no need for a debugger to print code, nor to add any code to your program. You really should read this up before continuing to refuse the only sensible advice any experienced programmer can offer you on this problem: Debugger - Wikipedia[^]

      GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

      1 Reply Last reply
      0
      • L Lost User

        Hi, You could use the [Debug Interface Access SDK](https://docs.microsoft.com/en-us/visualstudio/debugger/debug-interface-access/debug-interface-access-sdk?view=vs-2017) to get the exact line of 'source code' associated with any instruction offset in the executable. This is how WinDbg and Visual Studio knows where the line of source code is located. Using your sample:

        main()
        {
        int iTemp = 0; __asm int 3;
        fun(); __asm int 3;
        iTemp = 1; __asm int 3;
        }

        Then add an exception hander for STATUS_BREAKPOINT (0x80000003) and call into DIA and pass the exception offset to get the source line. It's actually quite simple. However if you are looking to perform instrumentation at the instruction level then have a look at [DynamoRIO](https://www.dynamorio.org/) and specifically the [instrace_x86 sample](https://github.com/DynamoRIO/dynamorio/blob/master/api/samples/instrace\_x86.c). Best Wishes, -David Delaune

        S Offline
        S Offline
        Stefan_Lang
        wrote on last edited by
        #25

        Why use the Visual Studio Debugger SDK, when you can just use the much more intuitive visual debugger that comes with the VS IDE? :confused:

        GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

        L 1 Reply Last reply
        0
        • V Victor Nijegorodov

          Member 13081369 wrote:

          ... the actual code of mine, are complex than above are. Very very complex, run one FunA(),need a lot of times. (many lines code)

          And how will you analyze such a "many lines code" in your "line output"? How much time would you need to just to look in this output?

          U Offline
          U Offline
          User 13049321
          wrote on last edited by
          #26

          If the code like below: if(iVal < 10) { FunA(); } else { FunB(); } When first run, iVal=1. So the first.txt Should be: if(iVal < 10) FunA(); And second run, iVal=20. The second.txt Should be: if(iVal < 10) FunB(); I could compared two file(first.txt and second.txt) to find code execution difference. I need it, because my codes very very larged. when iVal differeced every times, the differece appeard at after hundreds lines between first.txt and second.txt.

          V 1 Reply Last reply
          0
          • U User 13049321

            When breaked at debugging, I can press F10, line by line to view the current execution of the source code. Suppose my program has 1000 lines of code, I don't want to press F10 1000 times to watch all the 1000 line in the order of execution. I wish output whole 1000 lines of code in execution order. surce code like below:

            id fun(int* piVal)
            {
            *piVal = 0;
            }

            main()
            {
            int iTemp = 0;
            fun();
            iTemp = 1;
            }

            I need a text file after running the program. The contents of the file are like below:

            int iTemp = 0;
            *piVal = 0;
            iTemp = 1;

            text file has 3 line, It exact save the three lines of execution.

            S Offline
            S Offline
            Stefan_Lang
            wrote on last edited by
            #27

            If you are using Visual Studio, you can use its integrated debugger. Here is a guide to get started with it - it's really easy: Get started with Visual Studio 2017 - Debugging[^] If you are not using Visual Studio, you should tell us what you use, so someone can suggest a suitable debugger to use for that, and/or a quick guide to learn how to use it. The important things to know about debugging: 1. if you don't want to, you do not need to add any specialized code to your program to enable debugging. 2. You can tell your debugger that you want your program to stop if something specific happens. What you told us is quite easy to specify with Visual Studio: you can make the program run up to exactly the point where your iVal changes (see Data Breakpoints – Visual Studio 2017 15.8 Update   | Visual C++ Team Blog[^] ) 3. When using a debugger, you can not only make your program run up to a point, you can also inspect all variables, the call stack, and other aspects of the current state of your program. There are many more things that a debugger can help you with, e. g. change a variable at runtime, make the computer skip or repeat some code, etc.. All without changing the program code. But you have to learn the basics.

            GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

            1 Reply Last reply
            0
            • U User 13049321

              If the code like below: if(iVal < 10) { FunA(); } else { FunB(); } When first run, iVal=1. So the first.txt Should be: if(iVal < 10) FunA(); And second run, iVal=20. The second.txt Should be: if(iVal < 10) FunB(); I could compared two file(first.txt and second.txt) to find code execution difference. I need it, because my codes very very larged. when iVal differeced every times, the differece appeard at after hundreds lines between first.txt and second.txt.

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #28

              Member 13081369 wrote:

              I need it, because my codes very very larged. when iVal differeced every times, the differece appeard at after hundreds lines between first.txt and second.txt.

              1. What do you mean by "codes very very larged"? How many lines does it contain? 1000? 10000? 100000? One million? 2. Did you try to estimate how long would it take you to compare these "first.txt and second.txt"?

              1 Reply Last reply
              0
              • U User 13049321

                Thanks, I known how to "using run to cursor",but the problem is I need to pressed F10 button hundreds, for execution whole program. I need to view "execution whole program line by line" exacts

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #29

                Neither 'Run to cursor' nor 'step over' (F10) are suitable functions for your problems. Either set a breakpoint within the code of the right if/else branch, or set a data breakpoint that stops execution the moment iVal changes its value: How to: Set a Data Breakpoint (Native Only) | Microsoft Docs[^]

                GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                1 Reply Last reply
                0
                • S Stefan_Lang

                  Why use the Visual Studio Debugger SDK, when you can just use the much more intuitive visual debugger that comes with the VS IDE? :confused:

                  GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #30

                  Hi Stefan, The poster is asking "How do I print each source line into a text file as my program is being executed?" Rather than belittle him I simply answered the question. In fact I just thought of a much easier way to do this using Powershell with the [Windbg COM interfaces](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/client-com-interfaces) and [IDiaDataSource](https://docs.microsoft.com/en-us/visualstudio/debugger/debug-interface-access/idiadatasource?view=vs-2017) that doesn't require writing any code. He could just write a powershell script to automate it. Best Wishes, -David Delaune

                  S 1 Reply Last reply
                  0
                  • U User 13049321

                    First, I sure. I run DIA SDK\Samples\DIA2Dump\Dia2Dump.exe myProgram.pdb I can view values and functions list when Dia2Dump.exe is run. But, How to "set the hardware breakpoints at DR0-DR7 and automate every single-step through your entire program"? Is modify dia2dump.cpp -> main(), start my program by dia2dump.exe? and WaitForDebugEvent(&dbgEvent, INFINITE); in dia2dump.exe? Can you give me a sample?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #31

                    Member 13081369 wrote:

                    But, How to "set the hardware breakpoints at DR0-DR7 and automate every single-step through your entire program"?

                    With the [SetThreadContext function](https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-setthreadcontext). It would be a great learning exercise for understanding both debugging and exception handling.

                    Member 13081369 wrote:

                    Can you give me a sample?

                    No, But I thought of another way to do this automated that would not require modifying your source code. [Dan Thompson](https://github.com/jazzdelightsme) has authored [DbgShell](https://github.com/Microsoft/DbgShell) which can be used from Powershell. With a simple powershell script you should be able to attach to your executable and single-step automated and then instantiate an [IDiaDataSource object](https://docs.microsoft.com/en-us/visualstudio/debugger/debug-interface-access/idiadatasource?view=vs-2017). Again, like the other members responding to you... I can't imagine why you would want to print each line of source code. Best Wishes, -David Delaune

                    U 1 Reply Last reply
                    0
                    • L Lost User

                      Hi Stefan, The poster is asking "How do I print each source line into a text file as my program is being executed?" Rather than belittle him I simply answered the question. In fact I just thought of a much easier way to do this using Powershell with the [Windbg COM interfaces](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/client-com-interfaces) and [IDiaDataSource](https://docs.microsoft.com/en-us/visualstudio/debugger/debug-interface-access/idiadatasource?view=vs-2017) that doesn't require writing any code. He could just write a powershell script to automate it. Best Wishes, -David Delaune

                      S Offline
                      S Offline
                      Stefan_Lang
                      wrote on last edited by
                      #32

                      Fair enough. I shouldn't have been so harsh. That said, my experience tells me that people rarely want, literally, what they're asking for. If they're asking for something that requires even a modest amount of expertise, they likely have no good understanding of what is possible, or sensible, in the context of their problem. Therefore the first thing I usually do is question their real needs, or make an educated guess about it. Often I'm right, sometimes I'm wrong. But either way, I typically get a better understanding of what is really required after a response or two. In this case, the OP is more adamant about what he needs than usual, but at the same time I'm convinced that answering his request, literally, won't really help him: His statement about not wanting to 'press F10 10000 times' implies two things: 1. that he does use a debugger 2. that he doesn't know how to use it for his particular problem 3. that he doesn't even expect that it can help him (Ok, that's three, but the third is implied other than the quoted statements) Unless I'm for some reason totally wrong with my interpretation, I feel that learning to use the debugger more efficiently is going to help a lot more than actually printing an exection log.

                      GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                      1 Reply Last reply
                      0
                      • L Lost User

                        Member 13081369 wrote:

                        But, How to "set the hardware breakpoints at DR0-DR7 and automate every single-step through your entire program"?

                        With the [SetThreadContext function](https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-setthreadcontext). It would be a great learning exercise for understanding both debugging and exception handling.

                        Member 13081369 wrote:

                        Can you give me a sample?

                        No, But I thought of another way to do this automated that would not require modifying your source code. [Dan Thompson](https://github.com/jazzdelightsme) has authored [DbgShell](https://github.com/Microsoft/DbgShell) which can be used from Powershell. With a simple powershell script you should be able to attach to your executable and single-step automated and then instantiate an [IDiaDataSource object](https://docs.microsoft.com/en-us/visualstudio/debugger/debug-interface-access/idiadatasource?view=vs-2017). Again, like the other members responding to you... I can't imagine why you would want to print each line of source code. Best Wishes, -David Delaune

                        U Offline
                        U Offline
                        User 13049321
                        wrote on last edited by
                        #33

                        Thank you!

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups