debugging C and C structs in javascript

86 views
Skip to first unread message

Mark Hahn

unread,
Mar 4, 2014, 5:53:25 PM3/4/14
to emscripte...@googlegroups.com
I've converted some legacy code to javascript using emscripten.  I'm having a problem with a pointer getting clobbered in the javascript that doesn't happen in the original C code.  I'm not that familiar with the code and I get lost trying to view the simulated memory.

I can put in breakpoints and inspect variables using chrome inspector, but I need to view complex C data structures.  Does anyone know of any tools that let you look at the emscripten simulated memory as C structs with labels?

I have already created a tool that displays memory in hex in the same way that hex editors show binary files.  It takes decimal addresses to make it easy to use from chrome where all the pointers show as decimal.  It also shows the data as text of course.

If nothing exists already then I'm going to write a tool that takes struct definitions from C header files and uses the definitions to display memory in javascript.  Would anyone else be interested in this?

Mark Callow

unread,
Mar 4, 2014, 9:12:01 PM3/4/14
to emscripte...@googlegroups.com

On 2014/03/05 7:53, Mark Hahn wrote:
I can put in breakpoints and inspect variables using chrome inspector, but I need to view complex C data structures.

Can you step through the code? I am unable to in my project. The debugger seems to jump wildly around the JS code when I try. This happens with both Firefox and Chrome built-in debuggers.



Does anyone know of any tools that let you look at the emscripten simulated memory as C structs with labels?

No. I'm having the same difficulty as you with this right now, exacerbated by the stepping problem.


...


If nothing exists already then I'm going to write a tool that takes struct definitions from C header files and uses the definitions to display memory in javascript.  Would anyone else be interested in this?

Yes, absolutely.

Regards

    -Mark

--
注意:この電子メールには、株式会社エイチアイの機密情報が含まれている場合が有ります。正式なメール受信者では無い場合はメール複製、 再配信または情報の使用を固く禁じております。エラー、手違いでこのメールを受け取られましたら削除を行い配信者にご連絡をお願いいたし ます.

NOTE: This electronic mail message may contain confidential and privileged information from HI Corporation. If you are not the intended recipient, any disclosure, photocopying, distribution or use of the contents of the received information is prohibited. If you have received this e-mail in error, please notify the sender immediately and permanently delete this message and all related copies.

Mark Hahn

unread,
Mar 4, 2014, 10:11:48 PM3/4/14
to emscripte...@googlegroups.com

On Tue, Mar 4, 2014 at 6:12 PM, Mark Callow <callo...@artspark.co.jp> wrote:
Can you step through the code?

Can you step through the code?

Yes, I have no problem.  You might be confused by the way the code uses switch (case) statements.  In many functions each C statement is one case and it loops back to the switch over and over.  At the end of each C statement (case) you see something like `label = 5; continue;'.  The good news is that each JS line has the file name and line number of the C source.

Mark Callow

unread,
Mar 4, 2014, 10:23:07 PM3/4/14
to emscripte...@googlegroups.com

Thanks but no I am not. I am aware of the use of switch statements. The wild jumps I see are not following the switch statement and include jumps into other unrelated functions. It looks to me like the debugger having a problem tracking the code execution but since it is happening in 2 different debuggers, an issue with the compiled code seems more likely.

When you say "Chrome Inspector" you are talking about the debugger built into Chrome, right?

Mark Hahn

unread,
Mar 4, 2014, 10:47:53 PM3/4/14
to emscripte...@googlegroups.com
Yes, the chrome inspector (which is similar to the old one in webkit/safari).

 an issue with the compiled code seems more likely.

I don't see how any particular javascript code could cause this problem.  It's either valid javascript or not.  No matter what emscripten generates you can't jump into the middle of a function.

The only time I've seen this kind of problem is when there are source-map files that don't match the javascript.  Emscripten doesn't support source-maps, does it?  That would be awesome.  Make sure the source-maps option is off in chrome.

I have no idea what could cause your problem.



--
You received this message because you are subscribed to a topic in the Google Groups "emscripten-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/emscripten-discuss/5iUdgtkUWaE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Alon Zakai

unread,
Mar 5, 2014, 12:44:30 AM3/5/14
to emscripte...@googlegroups.com
Emscripten does support source maps, see emcc -g4.

- Alon



--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

Jukka Jylänki

unread,
Mar 5, 2014, 4:07:30 AM3/5/14
to emscripte...@googlegroups.com
I'd try handwriting a function that takes in a pointer to a structure, and prints its contents out to console. Then you can invoke that function in the web console just by calling its name with the pointer. That's not quite as convenient as the object property view in the debugger, but might do the job?


Mark Hahn

unread,
Mar 5, 2014, 12:17:35 PM3/5/14
to emscripte...@googlegroups.com
Emscripten does support source maps, see emcc -g4.

Awesome!  I'm going to try it asap.

Mark Hahn

unread,
Mar 5, 2014, 12:42:41 PM3/5/14
to emscripte...@googlegroups.com
I'd try handwriting a function that takes in a pointer to a structure, and prints its contents out to console. 

That is exactly what I started doing yesterday.  Today I got a much better idea.  I'm going to make it a javascript function to convert between data structs in the heap and javascript objects.  Then you can have the inspector show the structs as objects and get the best of all worlds.

I'm going to make it an open source project on github (https://github.com/mark-hahn/emStructObj) so everyone can help.  It should be ready to look at in a few hours,  For now it will just be docs and some incomplete code.

Some features ...

1) Create js objects with keys matching the struct member names.

2) Supports simple variables as well.

3) The definition will be in two parts.  simple type defs and struct defs.  
    
    a) Simple defs: A hash with type names (short, long, LONG, color, etc) as keys and a def spec as the value.  The def will look like 'size, display, array length'.  
        i) The size has to be one of: i8, i16, i32, i64, float, or double.
        ii) Display can be hex, dec, str (ascii string), strw (16-bit unicode str), or one of the struct names.  If a struct name then it is a pointer to that struct def and an object is nested in the object.
        iii) The array length creates an array of these types assuming the vars/structs are contiguous in memory.

    b) Struct defs look like they do in C/C++.   It won't do a real parse, it will just assume one line per member with type and name followed by semicolon.  Usually you will be able to copy a struct definition directly from a c/c++ header. 

4) All defs will be in a separate js file included before the emStructObj code.  

5) The emStructObj code will work in any js environment like node and the browser.





Mark Hahn

unread,
Mar 5, 2014, 1:03:18 PM3/5/14
to Mark Hahn, emscripte...@googlegroups.com
I accidentally sent that before I was finished ...

6) JS functions called from C will be able to convert structs in the heap to objects and vice versa.  It will not only be for debugging.

I've already got the code half written.  I hope others will help add features.

Reply all
Reply to author
Forward
0 new messages