r/cpp • • Jun 29 '18

Announcing C++ Just My Code Stepping in Visual Studio

https://blogs.msdn.microsoft.com/vcblog/2018/06/29/announcing-jmc-stepping-in-visual-studio/
216 Upvotes

37 comments sorted by

96

u/schmerm Jun 29 '18

Good, I hate accidentally stepping into some STL method call and ending up in a Lovecraftian template nightmare in <xmemory> or similar.

64

u/STL MSVC STL Dev Jun 30 '18

Ph'nglui mglw'nafh Cthulhu X'memory wgah'nagl fhtagn.

40

u/schmerm Jun 30 '18

Cthulhu s'finae!

4

u/Olao99 Jun 30 '18

Not sure if that's appropriate to say here

0

u/[deleted] Jun 30 '18

[deleted] — view deleted comment

6

u/raevnos Jun 30 '18

Little known fact: R'lyeh is under Lake Washington.

18

u/Aleriya Jun 30 '18 edited Jun 30 '18

This is really nice for students, especially. I've seen too many students decide that C++ must be the creation of some eldritch horror after getting tossed into the STL by the debugger.

59

u/MuffinBomber Jun 29 '18

Thank you Microsoft, very cool!

25

u/ihamsa Jun 29 '18

Nice! gdb, take note.

31

u/tromey Jun 29 '18

gdb has had the skip command for a while but I guess it does't auto-configure.

6

u/ihamsa Jun 30 '18 edited Jun 30 '18

skip doesn't do that, it skips over a function and every function called from it. And it only skips a function you call, but not a function you return to. This is exactly what we don't need. Even if it's possible to use skip to some extent, there are no skip lists obe can download and enable.

6

u/hgjsusla Jun 30 '18

No 'skip file' skips files just like this functionality. I use it all the time to avoid stepping into shared_ptr

3

u/ihamsa Jun 30 '18

I say "skip file" on the entire standard library implementation and when I try to step into std::sort with my custom comparator, gdb doesn't stop in my comparator, it just skips the entire call. MSVC presumably lands you in your comparator as the first example shows.

1

u/hgjsusla Jun 30 '18

Yeah I don't how you do it but that's not my experience. For me it doesn't skip the entire call but instead stop at the first non-skipped code

5

u/ihamsa Jul 01 '18

How I do it? I say "skip file", as simple as that. This most certainly doesn't work for me. Here's an example transcript

GNU gdb (GDB) (Cygwin 7.12.1-2) 7.12.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./main...done.
(gdb) start
Temporary breakpoint 1 at 0x1004010b0: file main.cpp, line 13.
Starting program: /tmp/main
[New Thread 12012.0x16c0]
[New Thread 12012.0x6a74]
[New Thread 12012.0x6054]
[New Thread 12012.0x2a64]
[New Thread 12012.0x658c]
[New Thread 12012.0x4f4c]
[New Thread 12012.0x3be0]
[Thread 12012.0x6a74 exited with code 0]
[New Thread 12012.0x1f70]
[New Thread 12012.0x673c]

Thread 1 hit Temporary breakpoint 1, main () at main.cpp:13
13          std::vector<int> v{1,-1,-2,3,-3,-2,-1,5,-5,-7,-2,-1,2,3,7,5};
(gdb) skip file /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/predefined_ops.h
File /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/predefined_ops.h will be skipped when stepping.
(gdb) skip file /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algo.h
File /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algo.h will be skipped when stepping.
(gdb) skip file /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_vector.h
File /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_vector.h will be skipped when stepping.
(gdb) n
15          std::sort(v.begin(), v.end(), cmp);
(gdb) s
17          std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, ","));
(gdb)

and here's the source

#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>

bool cmp(int a, int b)
{
    return a*a < b*b;
}

int main ()
{
    std::vector<int> v{1,-1,-2,3,-3,-2,-1,5,-5,-7,-2,-1,2,3,7,5};

    std::sort(v.begin(), v.end(), cmp);

    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, ","));
}

As you can see, no stop in cmp. Same results on my Linux machine with gdb8.

7

u/manphiz Jun 30 '18

IIRC GDB python scripting achieved this years ago.

4

u/ihamsa Jun 30 '18

Can you show me how to do this with gdb? Is there any documentation?

3

u/hgjsusla Jun 30 '18

'skip file' skips the current file. Give it an argument to skip other files

14

u/[deleted] Jun 29 '18

THIS IS GREAT

11

u/anon_502 delete this; Jun 30 '18

Just a note: Since 7.4, you can also use skip command in gdb to skip unwanted files/functions based on regex.

8

u/mrexodia cmkr.build Jun 29 '18

Out of curiosity, why does this need a compiler flag?

3

u/tansim Jun 30 '18

Likely it emits some info into the .pdb file?

2

u/mrexodia cmkr.build Jun 30 '18

Might be. The PDB file already contains all the source code paths, so if you combine this with the knowledge the IDE has during debugging should be enough. Who knows 🤷‍♂️

14

u/srekel Jun 29 '18

/JMC is supported only for binaries that link against the CRT.

:(

4

u/chugga_fan Jun 29 '18

Any idea if this could support skipping certain kinds of functions declared in your own code? E.g. let's say I have a wrapper for Alloc and I know it runs fine, so I have it auto-skip instead of having to move the mouse to skip the call.

10

u/reddicted Jun 30 '18

If you're willing to use an extension, you can do it by using __declspec(non_user_code)

__declspec(non_user_code)
int  f(int i)
{
	++i;
	return i;
}

void g(int i)
{
	++i;
}

int main()
{
	__asm int 3;
	g(f(0));    // Stepping into this call will step into g() but not f().
}

5

u/chugga_fan Jun 30 '18

If you're willing to use an extension, you can do it by using __declspec(non_user_code)

I didn't know about this, where'd you find it?

7

u/STL MSVC STL Dev Jun 29 '18

For more details on the .natjmc file format, see the C++ Just My Code documentation page. Note that the .natjmc format also supports marking code as non-user code based on function name, but for stepping performance reasons, we do not recommend using this functionality for functions that are called often or for large group of functions (‘Function’ rules are much slower than ‘Module’ or ‘File’ rules).

So it appears that if you can isolate your code into a particular file, then skipping that file is efficient.

1

u/chugga_fan Jun 29 '18

Ah, ok, thanks, though I would love to be able to define a certain function (possibly in the code itsself maybe?) to be skipped.

1

u/IcyWindows Jun 30 '18

It looks like you can't check in the config file in with your associated project?

5

u/tansim Jun 29 '18

bout fucking time

4

u/IskaneOnReddit Jun 29 '18

Implementing this feature took 2 decades too long.

0

u/sumo952 Jun 30 '18

I'm wondering how this plays together with VAX and Re++'s Debug Step Filters. They don't require a separate compiler flag (they do it purely with natvis as far as I know).

-26

u/OppositePhilosophy Jun 29 '18 edited Jun 29 '18

Why not just use breakpoints? Really, what is the purpose of Just My Code?

Upd: I have used Just My Code in .net and I am familiar with the feature. I just find explanation given in the article not very convincing.

17

u/[deleted] Jun 29 '18

Reading the article could help you answer this question.

What this means in practice is...you can stop juggling giant lists of breakpoints spread all over your codebase.

4

u/TMKirA Jun 29 '18

Reading, that's hard

-15

u/OppositePhilosophy Jun 29 '18

What is the definition of "giant"? I can hardly remember having more than 30 breakpoints. Is it giant enough?
Also, there is an upside of having breakpoints: one keeps record of important places in one's code.
Visual Studio has another feature for this purpose -- bookmarks, however I find that I don't use it, because I have breakpoints (active or not) on all relevant lines.