Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing c++ bugs in javascript - feature request #32591

Closed
ghost opened this issue Aug 15, 2017 · 14 comments
Closed

fixing c++ bugs in javascript - feature request #32591

ghost opened this issue Aug 15, 2017 · 14 comments
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code)

Comments

@ghost
Copy link

ghost commented Aug 15, 2017

Emscripten is a very good project to port c/c++ projects to javascript,
there is a feature called Manual print debugging, which prints out the line number of the given c++ code in the ported javascript code.

http://kripken.github.io/emscripten-site/docs/porting/Debugging.html

My feature request sounds crazy, but there is a valid reason.

Because of emscripten you can debug ported javascript code from c++,
what i would like to achieve, to make changes in javascript and automatically converted back to c++.

I think that functionality can empower the free source world.

Especially when you have a badly written aging project like ffmpeg and you would like to find bugs in it,
so you can use emscripten debug it in the browser and convert back the changes into c++.

So any freesource project which misses manpower can get free hands quickly as javascript is a well known and easy to use language for everyone.

There are special cases, but to check and validate an encoder algorithm make it more efficient programatically can be a valid reason to use that technic. If you would be able to read the algorithm in a better way and debug in a better way, you can get an idea to write far better algorithm than the heavily patented mpeg h264 and aac. (I think it's a common problem in every browsers nowadays)

Not to mention c++ projects, which has been ended because lots of resources was needed to fix bugs further. (Osmosis is a good example on open street map - everybody is using it as a google alternative, but the osmisis itself is in 0.55 version, and finished the development after 2016)

@vscodebot vscodebot bot added the debug Debug viewlet, configurations, breakpoints, adapter issues label Aug 15, 2017
@weinand weinand added feature-request Request for new features or functionality help wanted Issues identified as good community contribution opportunities and removed debug Debug viewlet, configurations, breakpoints, adapter issues labels Aug 18, 2017
@weinand weinand removed their assignment Aug 18, 2017
@weinand weinand added the info-needed Issue requires more information from poster label Aug 18, 2017
@weinand
Copy link
Contributor

weinand commented Aug 18, 2017

What exactly is the feature you are asking for?
Your comment from above explains why the Emscripten approach is useful (and I certainly understand that even without explanation).
But I do not understand what is missing from VS Code?

@ghost
Copy link
Author

ghost commented Aug 18, 2017

To be able to use the chrome debugger and profiler in ansi c++ development, and according to debug info produced by Emscripten any changes being made in javascript code Visual studio code applies immediately on the c++ source.

@weinand
Copy link
Contributor

weinand commented Aug 18, 2017

I still don't understand.

Since Emscripten converts C++ into JavaScript, you can just use the JavaScript debuggers "chrome-debug" or "node-debug" to debug the generated JavaScript. If Emscripten generates sourcemaps that map the generated JavaScript back to the original C++ source, the JavaScript debuggers will pick up those SourceMaps and VS Code should show the C++ source.

Have you tried this?

@weinand weinand removed the help wanted Issues identified as good community contribution opportunities label Aug 18, 2017
@ghost
Copy link
Author

ghost commented Aug 18, 2017

There is a manual debugging feature, which puts in javascript comments where the c++ code is. Emscripten would be responsible only the translation to javascript. As my understanding is that code with some flags of the Emscripten compiler, even if it's staticly typed and using asm. js, can be partly human readable code. The Visual Code editor respinsibility would be to find that debug informations in javascript and index with the underlying c++ source. So you have a chrome debugger plugin and javascript to c++ parser. Emscripten is helping the editor on the indexing, but it cannot translate back any code to c++.

As the Emscripten is making a conversion through llvm, there is no way back to translate back c++, but through what debugging information puts to the file, it might be tracked back to the original one, what some programmers doing anyway. The feature request might be something which is a new path. It'a simply not working with new projects, but there are plenty of free source old one.

@weinand
Copy link
Contributor

weinand commented Aug 18, 2017

Before using the clumsy "manual debugging feature" you should try the "sourcemap" approach. From the emscripten doc:

The -g4 option provides the most debug information — it generates source maps that allow you to view and debug the C/C++ source code in your browser’s debugger on Firefox, Chrome or Safari!

These source maps are understood by VS Code's JavaScript debuggers too.

@ghost
Copy link
Author

ghost commented Aug 18, 2017

I'm not sure about your approach. Intersting point, but i have no idea, how to achieve it. I'm not sure that Emscripten generates any source map. I think it is generating line numbers and file names in the javascript comment, and i need to manually traxk back opening the file where is the c++ line exactly, which is tedious.

@ghost
Copy link
Author

ghost commented Aug 18, 2017

I will try that, thanks.

@weinand
Copy link
Contributor

weinand commented Aug 18, 2017

Use of source maps is not "my" approach. It is Emscripten's recommended way to debug C/C++ source code in your browser’s debugger on Firefox, Chrome or Safari!

Just pass -g4 to emcc and look for the source maps either inlined at the end of the generated JavaScript file or as a side car file.

@weinand weinand closed this as completed Aug 18, 2017
@weinand weinand added debug Debug viewlet, configurations, breakpoints, adapter issues *question Issue represents a question, should be posted to StackOverflow (VS Code) and removed feature-request Request for new features or functionality info-needed Issue requires more information from poster labels Aug 18, 2017
@ghost
Copy link
Author

ghost commented Aug 18, 2017

Yes, but what i would have liked, if i would change something in javascript code it would reflect back automatically to c++. So i have confidence that when i build the c++ source, it will work.

@ghost
Copy link
Author

ghost commented Aug 18, 2017

So i'm not talking debugging, i'm talking to change debug profile in javascript, build in c++, as the c++ code automatically changed by vs editor.

@weinand
Copy link
Contributor

weinand commented Aug 18, 2017

That would require a "reverse compiler" that translates JavaScript into C++ (which is very hard if not impossible). But anyway, it is completely outside of VS Code's scope. We are not doing transpilers.

I suggest to create a feature request against Emscripten. They know how to translate C++ to JavaScript, so they are in the best position to do the reverse translation.

@weinand weinand removed the debug Debug viewlet, configurations, breakpoints, adapter issues label Aug 18, 2017
@ghost
Copy link
Author

ghost commented Aug 18, 2017

I accept your answer, but through source map info, there is no way to find back the original c++ code, what the debugger does anyway and from javascript source code to c++ source code make changes on the file? So no compiler.

I thought that when i open a c++ project in Visual studio code, it's indexing, not just formatting c++/javascript files, so it's aware of the structure of class and functions, and can match lines, like an svn editor.

It was just an idea, thanks for your effort.

@weinand
Copy link
Contributor

weinand commented Aug 18, 2017

I just tried to debug an Emscripten translated hello.c with VS Code:

2017-08-18_17-21-59

I compiled with:

./emcc -g4 tests/hello_world.c

The launch config for this looks like this:

{
	// Use IntelliSense to learn about possible Node.js debug attributes.
	// Hover to view descriptions of existing attributes.
	// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
	"version": "0.2.0",
	"configurations": [
		{
			"type": "node",
			"request": "launch",
			"name": "Launch Program",
			"protocol": "inspector",
			"program": "${workspaceRoot}/a.out.js"
		}
	]
}

In order to set breakpoints in *.c files, I had to enable a special VS Code option for the workspace:

2017-08-18_17-32-35

The source map looks like this:

{
	"version": 3,
	"sources": [
		"tests/hello_world.c"
	],
	"names": [

	],
	"mappings": ";;;;;;;;;;;;;AAGA;AAAA;;;;AACA;AAAA;AAAA;AADA;AAAA;AAAA;;AAGA",
	"file": "a.out.js",
	"sourcesContent": [
		"#include <stdio.h>\n\nint main() {\n  for (int i; i < 100; i++) {\n    printf(\"hello, world: %d\\n\", i);\n  }\n  return 0;\n}\n"
	]
}

As you can see the hello_world.c file is inlined in the source map.

@weinand
Copy link
Contributor

weinand commented Aug 18, 2017

You wrote:

I thought that when i open a c++ project in Visual studio code, it's indexing, not just formatting c++/javascript files, so it's aware of the structure of class and functions, and can match lines, like an svn editor.

VS Code has no builtin C/C++ support. You need to install a C/C++ extension before VS Code understands C/C++.

But even a C/C++ extension is of no help if you transpile C/C++ to JavaScript and run it in a node.js runtime. Node.js has no idea that the JavaScript comes from C/C++. It runs it as JavaScript.

However, by using source maps VS Code at least understands how to map JavaScript source lines back to C/C++ source lines. But of course this does not allow allow to transpile modified JavaScript back to C/C++.

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code)
Projects
None yet
Development

No branches or pull requests

1 participant