diff --git a/docs/emcc.txt b/docs/emcc.txt index 520a56f55f9bf..87be29ca9b79d 100644 --- a/docs/emcc.txt +++ b/docs/emcc.txt @@ -49,8 +49,8 @@ Options that are modified or new in *emcc* are listed below: "-O1" [compile+link] Simple optimizations. During the compile step these - include LLVM "-O1" optimizations. During the link step this does - not include various runtime assertions in JS that *-O0* would do. + include LLVM "-O1" optimizations. During the link step this omits + various runtime assertions in JS that *-O0* would include. "-O2" [compile+link] Like "-O1", but enables more optimizations. During @@ -68,15 +68,16 @@ Options that are modified or new in *emcc* are listed below: "-O3" [compile+link] Like "-O2", but with additional optimizations that - may take longer to run. + may take longer to run and may increase code size. Note: This is a good setting for a release build. "-Og" - [compile+link] Like "-O1". In future versions, this option might - disable different optimizations in order to improve debuggability. + [compile+link] Like "-O1", with an additional flag to extend the + liveness of variables for improved debugging. In future versions, + additional optimizations might also be disabled. "-Os" [compile+link] Like "-O3", but focuses more on code size (and may @@ -182,9 +183,10 @@ Options that are modified or new in *emcc* are listed below: with "-c". "-gsource-map[=inline]" - [link] Generate a source map using LLVM debug information (which - must be present in object files, i.e., they should have been - compiled with "-g"). + [compile+link] [same as -g3 if passed at compile time, otherwise + applies at link] Generate a source map using LLVM debug information + (which must be present in object files, i.e., they should have been + compiled with "-g" or "-gsource-map"). When this option is provided, the **.wasm** file is updated to have a "sourceMappingURL" section. The resulting URL will have format: @@ -200,32 +202,34 @@ Options that are modified or new in *emcc* are listed below: a large source map file). "-g" - [compile+link] Controls the level of debuggability. Each level - builds on the previous one: + [compile+link] If used at compile time, adds progressively more + DWARF information to the object file, according to the underlying + behavior of clang. If used at link time, controls the level of + debuggability overall. Each level builds on the previous one: * "-g0": Make no effort to keep code debuggable. - * "-g1": When linking, preserve whitespace in JavaScript. + * "-g1": Preserve whitespace in JavaScript. - * "-g2": When linking, preserve function names in compiled code. + * "-g2": Also preserve function names in compiled code (via the + wasm name section). - * "-g3": When compiling to object files, keep debug info, - including JS whitespace, function names, and LLVM debug info - (DWARF) if any (this is the same as -g). + * "-g3": Also keep LLVM debug info (DWARF) if there is any in + the object files (this is the same as -g). "--profiling" - [same as -g2 if passed at compile time, otherwise applies at link] - Use reasonable defaults when emitting JavaScript to make the build - readable but still useful for profiling. This sets "-g2" (preserve - whitespace and function names) and may also enable optimizations - that affect performance and otherwise might not be performed in - "-g2". + [link] Make the output suitable for profiling. This means including + function names in the wasm and JS output, and preserving whitespace + in the JS output. It does not affect optimizations (to ensure that + performance profiles reflect production builds). Currenly this is + the same as "-g2". "--profiling-funcs" - [link] Preserve function names in profiling, but otherwise minify - whitespace and names as we normally do in optimized builds. This is - useful if you want to look at profiler results based on function - names, but do *not* intend to read the emitted code. + [link] Preserve wasm function names as in "--profiling", but + otherwise minify whitespace and names as we normally do in + optimized builds. This is useful if you want to look at profiler + results based on function names, but do *not* intend to read the + emitted code. "--tracing" [link] Enable the Emscripten Tracing API. @@ -240,7 +244,9 @@ Options that are modified or new in *emcc* are listed below: [link] Save a map file between function indexes in the Wasm and function names. By storing the names on a file on the side, you can avoid shipping the names, and can still reconstruct meaningful - stack traces by translating the indexes back to the names. + stack traces by translating the indexes back to the names. This is + a simpler format than source maps, but less detailed because it + only describes function names and not source locations. Note: diff --git a/site/source/docs/porting/Debugging.rst b/site/source/docs/porting/Debugging.rst index 3a7c3b67bfbe2..8573de6f3af75 100644 --- a/site/source/docs/porting/Debugging.rst +++ b/site/source/docs/porting/Debugging.rst @@ -4,317 +4,438 @@ Debugging ========= -One of the main advantages of debugging cross-platform Emscripten code is that the same cross-platform source code can be debugged on either the native platform or using the web browser's increasingly powerful toolset — including debugger, profiler, and other tools. - -Emscripten provides a lot of functionality and tools to aid debugging: - -- :ref:`Compiler debug information flags ` that allow you to preserve debug information in compiled code and even create source maps so that you can step through the native C++ source when debugging in the browser. -- :ref:`Debug mode `, which emits debug logs and stores intermediate build files for analysis. -- :ref:`Compiler settings ` to enable runtime checking of memory accesses and common allocation errors. -- :ref:`debugging-manual-debugging` of Emscripten-generated code is also supported, which is in some ways even better than on native platforms. -- :ref:`debugging-autodebugger`, which automatically instruments LLVM bitcode to write out each store to memory. - -This article describes the main tools and settings provided by Emscripten for debugging, along with a section explaining how to debug a number of :ref:`debugging-emscripten-specific-issues`. - - -.. _debugging-debug-information-g: - -Debugging in the browser -======================== - -:ref:`Emcc ` can output debug information in two formats, either as -DWARF symbols or as source maps. Both allow you to view and debug the -*C/C++ source code* in a browser's debugger. DWARF offers the most precise and -detailed debugging experience and is supported as an experiment in Chrome 88 -with an `extension `. See -`here ` for a detailed -usage guide. Source maps are more widely supported in Firefox, Chrome, and -Safari, but unlike DWARF they cannot be used to inspect variables, for example. - -:ref:`Emcc ` strips out most of the debug information from -:ref:`optimized builds ` by default. DWARF can be produced with -the *emcc* :ref:`-g flag `, and source maps can be emitted with the -:ref:`-gsource-map ` option. Be aware that optimisation levels -:ref:`-O1 ` and above increasingly remove LLVM debug information, and -also disable runtime :ref:`ASSERTIONS ` checks. Passing a -``-g`` flag also affects the generated JavaScript code and preserves -white-space, function names, and variable names, - -.. tip:: Even for medium-sized projects, DWARF debug information can be of - substantial size and negatively impact the page performance, particularly - compiling and loading of the module. Debug information can also be emitted in - a file on the side instead with the - :ref:`-gseparate-dwarf ` option! The debug information - size also affects the linking time, because the debug information in all - object files needs to be linked as well. Passing the - :ref:`-gsplit-dwarf ` option can help here, which causes - clang to leave debug information scattered across object files. That debug - information needs to be linked into a DWARF package file (``.dwp``) using the - ``emdwp`` tool then, but that could happen in parallel to the linking of - the compiled output! When running it - after linking, it's as simple as ``emdwp -e foo.wasm -o foo.wasm.dwp``, or - ``emdwp -e foo.debug.wasm -o foo.debug.wasm.dwp`` when used together with - ``-gseparate-dwarf`` (the dwp file should have the same file name as the main - symbol file with an extra ``.dwp`` extension). - -The ``-g`` flag can also be specified with an integer levels: -:ref:`-g0 `, :ref:`-g1 `, :ref:`-g2 ` (default with -``-gsource-map``), and :ref:`-g3 ` (default with ``-g``). Each level -builds on the last to provide progressively more debug information in the -compiled output. - -.. note:: Because Binaryen optimization degrades the quality of DWARF info further, ``-O1 -g`` will skip running the Binaryen optimizer (``wasm-opt``) entirely unless required by other options. You can also throw in ``-sERROR_ON_WASM_CHANGES_AFTER_LINK`` option if you want to ensure the debug info is preserved. See `Skipping Binaryen `_ for more details. - -.. note:: Some optimizations may be disabled when used in conjunction with the debug flags both in the Binaryen optimizer (even if it runs) and JavaScript optimizer. For example, if you compile with ``-O3 -g``, the Binaryen optimizer will skip some of the optimization passes that do not produce valid DWARF information, and also some of the normal JavaScript optimization will be disabled in order to better provide the requested debugging information. +One of the main advantages of debugging cross-platform Emscripten code is that +the same cross-platform source code can be debugged on either the native +platform or using the web browser's increasingly powerful toolset — including a +debugger, profiler, and other tools. -.. _debugging-EMCC_DEBUG: +This article describes the main tools and settings provided by Emscripten for +debugging, organized by common developer use cases. + + +Overview: Emitting and Controlling Debug Information +==================================================== +Debugging-related information comes in several forms: in Wasm object and binary +files (as DWARF sections or Wasm name section), side output files (as source +maps, symbol maps, or DWARF sidecar or package files), and even in the code +itself (as assertions or instrumentation, or JS whitespace and comments). For +information on DWARF, see :ref:`below `. In addition to DWARF, +wasm files may contain a `name section +`_ +which includes names for each function; these function names are displayed by +browsers when they generate `stack traces +`_ and in +developer tools. Source maps are also supported by Emscripten and by browser +DevTools (see :ref:`below `). + +This document contains an overview of the flags used to emit and control +debugging behavior, and use-case-based examples. + +Unlike traditional Unix-style C toolchains, flags must be passed at link time to +preserve or generate debug information (these defaults aim to avoid unintended +bloat in production builds). The most common of these are the :ref:`-g flags +`; see the flag documentation or the use cases below for more detail. + +Flags that cause DWARF generation (e.g. ``-g3``, ``-gline-tables-only``) also +generate a name section in the binary and suppress minification of the JS glue +file (since most DWARF use cases are for interactive debugging or where the +binary will be stripped). Other flags (e.g. ``-g2``, ``-gsource-map``) should +affect only a specific behavior or type of debug info, and are generally +composable. + + +.. _debugging-interactive: + +Interactive, Source-Level Debugging +============================================= -Debug mode (EMCC_DEBUG) -======================= +For stepping through C/C++ source code in a browser's debugger, you can use +debug information in either DWARF or source map format. -The ``EMCC_DEBUG`` environment variable can be set to enable Emscripten's debug mode: +DWARF offers the best debugging experience and is supported in Chrome with an +`extension `_. See `here +`_ for a detailed usage +guide. Source maps are more widely supported, but they provide only location +mapping and cannot be used easily to inspect variables. + + +.. _debugging-dwarf: + +DWARF +----- + +In a traditional Unix-style C toolchain, flags such as ``-g`` are passed to the +compiler, placing DWARF sections in the object files. This DWARF info is +combined by the linker and appears in the output, independently of any +optimization settings. In contrast, although :ref:`emcc ` supports many +of the common `clang flags +`_ +to generate DWARF into the object files, final debug output is also controlled +by link-time flags, and is more affected by optimization. For example ``emcc`` +strips out most of the debug information after linking if a debugging-related +flag is not provided at link time, even if the input object files contain DWARF. + +DWARF can be produced at compile time with the *emcc* :ref:`-g flag `. +Optimization levels above :ref:`-O1 ` or :ref:`-Og ` +increasingly degrade LLVM debug information (as with other architectures), and +optimization flags at link time also disable Emscripten's runtime +:ref:`ASSERTIONS ` checks. Passing a ``-g`` flag at link +time also affects the generated JavaScript code (preserving white-space, +function names, and variable names, which makes the JavaScript debuggable). + +The ``-g`` flag can also be specified with integer levels: :ref:`-g0 `, +:ref:`-g1 `, :ref:`-g2 `, and :ref:`-g3 ` (equivalent +to ``-g``). At compile time these flags control the amount of DWARF in the +object files. At link time, each adds sucessively more kinds of information in +the wasm and JS files (DWARF is only retained after linking when using ``-g`` or +``-g3``). + +Example: .. code-block:: bash - # Linux or macOS - EMCC_DEBUG=1 emcc test/hello_world.cpp -o hello.html + emcc source.c -c -o source.o -g # source.o has DWARF sections emcc source.o -o + program.js -g # program.wasm has DWARF and a name section + + +.. tip:: Even for medium-sized projects, DWARF debug information can be large. + Debug information can be emitted in a separate file with the + :ref:`-gseparate-dwarf ` option. To speed up linking, + the :ref:`-gsplit-dwarf ` option can be used at compile + time. See `this article + `_ + for more details on debugging large files, and see :ref:`the next section + ` for more ways to reduce debug info size. + + +.. note:: Because Binaryen optimization degrades the quality of DWARF info + further, higher link-time optimization settings are + not recommended. The ``-O1`` setting will skip running the Binaryen optimizer + (``wasm-opt``) entirely unless required by other options. You can also add the + ``-sERROR_ON_WASM_CHANGES_AFTER_LINK`` option if you want to ensure the debug + info is preserved. See `Skipping Binaryen + `_ + for more details. + + +.. _debugging-symbolization: + +Symbolizing Production Crash Logs +============================================= + +Even when not using an interactive debugger, it's valuable to have source +information for compiled code locations, particularly for stack traces or crash +logs. This is also true for fully-optimized production builds. + +`Source maps `_ are commonly used for +languages that compile to JavaScript (mapping locations in the compiled JS +output to locations in the original source code), but WebAssembly is also +supported. Emscripten can emit source maps with the :ref:`-gsource-map +` link-time flag. Source maps are preserved even with full +post-link optimizations, so they work well for this use case. Source maps are +generated by Emscripten from DWARF information. Therefore the linked object +files must have DWARF. The final linked output will not have DWARF unless `-g` +is also passed at link time. + +DWARF can also be used for this purpose. Typically a binary containing DWARF +would be generated at build time, and then stripped. The stripped copy would be +served to users, and the original would be saved for symbolication purposes. For +this use case, full information about about types and variables from the sources +isn't needed; the `-gline-tables-only +`_ +compile-time flag causes clang to generate only the line table information, +saving DWARF size and compile/linking time. + +Source maps are easier to parse and more widely supported by ecosystem tooling. +And as noted above, preserving DWARF inhibits some Binaryen optimizations. +However DWARF has the advantage that it includes information about inlining, +which can result in more accurate stack traces. + +Examples: - # Windows - set EMCC_DEBUG=1 - emcc test/hello_world.cpp -o hello.html - set EMCC_DEBUG=0 +.. code-block:: bash -With ``EMCC_DEBUG=1`` set, :ref:`emcc ` emits debug output and generates intermediate files for the compiler's various stages. ``EMCC_DEBUG=2`` additionally generates intermediate files for each JavaScript optimizer pass. + emcc source.c -c -o source.o -g # source.o has DWARF sections (-gsource-map also works here) + emcc source.o -o program.js -gsource-map # program.wasm.map contains a source map -The debug logs and intermediate files are output to -**TEMP_DIR/emscripten_temp**, where ``TEMP_DIR`` is the OS default temporary -directory (e.g. **/tmp** on UNIX). + emcc source.o -o program2.js -g # program2.wasm has DWARF + llvm-strip program2.wasm -o program2_stripped.wasm # program2_stripped.wasm has no debug info -The debug logs can be analysed to profile and review the changes that were made in each step. +Emscripten includes a tool called ``emsymbolizer`` that can map wasm code +addresses to sources using several different kinds of debug info, including +DWARF (in wasm object or linked files) and source maps for line/column info, and +symbol maps (see :ref:`emcc-emit-symbol-map`), name sections and object file +symbol tables for function names. -.. note:: The more limited amount of debug information can also be enabled by specifying the :ref:`verbose output ` compiler flag (``emcc -v``). +Fast Edit+Compile with minimal debug information +================================================ -.. _debugging-compilation-settings: +When you want the fastest builds, you generally want to avoid generating large +debug information during compile, because it takes time to link into the final +binary. It is still worthwhile to use the ``-g2`` flag (at link time only) +because browsers understand the name section even when devtools are not in use, +resulting in more useful stack traces at minimal cost. -Compiler settings -================== +Example: -Emscripten has a number of compiler settings that can be useful for debugging. These are set using the :ref:`emcc -s` option, and will override any optimization flags. For example: +.. code-block:: bash + + emcc source.c -c -o source.o # source.o has no debug info + emcc source.o -o program.js -g2 # program.wasm has a name section, program.js is unminified + +Sometimes the use of the ``-O1`` or ``-Og`` flag at compile time can also result +in faster builds, because optimizations early in the pipeline can reduce the +amount of IR that is processed by later phases such as instruction selection and +linking. It also of course reduces test runtime. + +.. _debugging-memory-safety: + +Detecting Memory Errors and Undefined Behavior +============================================== + +The best tools for detecting memory safety and undefined behavior issues are +Clang's sanitizers, such as the Undefined Behavior Sanitizer (UBSan) and the +Address Sanitizer (ASan). For more information, see :ref:`Sanitizers`. + + +Emscripten has several other compiler settings that can be useful for catching +errors at runtime. These are set using the :ref:`emcc -s` +option. For example: .. code-block:: bash - emcc -O1 -sASSERTIONS test/hello_world + emcc -O1 -sASSERTIONS test/hello_world.c Some important settings are: - .. _debugging-ASSERTIONS: - ``ASSERTIONS=1`` is used to enable runtime checks for common memory allocation errors (e.g. writing more memory than was allocated). It also defines how Emscripten should handle errors in program flow. The value can be set to ``ASSERTIONS=2`` in order to run additional tests. - - ``ASSERTIONS=1`` is enabled by default. Assertions are turned off for optimized code (:ref:`-O1 ` and above). + ``ASSERTIONS=1`` is used to enable runtime checks for many types of common + errors. It also defines how Emscripten should handle errors in program flow. + The value can be set to ``ASSERTIONS=2`` in order to run additional tests. + ``ASSERTIONS=1`` is enabled by default at ``-O0`` and disabled at higher + optimization levels, but can be overridden. - .. _debugging-SAFE-HEAP: - ``SAFE_HEAP=1`` adds additional memory access checks, and will give clear errors for problems like dereferencing 0 and memory alignment issues. - - You can also set ``SAFE_HEAP_LOG`` to log ``SAFE_HEAP`` operations. + ``SAFE_HEAP=1`` adds additional memory access checks with a Binaryen pass, + and will give clear errors for problems like dereferencing 0 and memory + alignment issues. You can also set ``SAFE_HEAP_LOG`` to log ``SAFE_HEAP`` + operations. :ref:`ASan` provides most of the functionality + of this pass (plus some extras) and is generally preferred to try first + unless :ref:`alignment issues` are + important for your platform. - .. _debugging-STACK_OVERFLOW_CHECK: - Passing the ``STACK_OVERFLOW_CHECK=1`` linker flag adds a runtime magic - token value at the end of the stack, which is checked in certain locations - to verify that the user code does not accidentally write past the end of the - stack. While overrunning the Emscripten stack is not a security issue for - JavaScript (which is unaffected), writing past the stack causes memory - corruption in global data and dynamically allocated memory sections in the - Emscripten HEAP, which makes the application fail in unexpected ways. The - value ``STACK_OVERFLOW_CHECK=2`` enables slightly more detailed stack guard + ``STACK_OVERFLOW_CHECK=1`` adds a runtime magic token value at the end of + the stack, which is checked in certain locations to verify that the user + code does not accidentally write past the end of the stack. While + overrunning the Emscripten stack is not a security issue for JavaScript + (which is unaffected), writing past the stack causes memory corruption in + global data and dynamically allocated memory sections in the Emscripten + HEAP, which makes the application fail in unexpected ways. The value + ``STACK_OVERFLOW_CHECK=2`` enables slightly more detailed stack guard checks, which can give a more precise callstack at the expense of some performance. Default value is 1 if ``ASSERTIONS=1`` is set, and disabled otherwise. -A number of other useful debug settings are defined in `src/settings.js `_. For more information, search that file for the keywords "check" and "debug". - -.. _debugging-sanitizers: - -Sanitizers -========== - -Emscripten also supports some of Clang's sanitizers, such as :ref:`sanitizer_ubsan` and :ref:`sanitizer_asan`. - -.. _debugging-emcc-v: - -emcc verbose output -=================== - -Compiling with the :ref:`emcc -v ` will cause Emscripten to output -the sub-command that it runs as well as passes ``-v`` to Clang. -.. _debugging-manual-debugging: -Manual print debugging -====================== +A number of other useful debug settings are defined in `src/settings.js +`_. For +more information, search that file for the keywords "check" and "debug". -You can also manually instrument the source code with ``printf()`` statements, then compile and run the code to investigate issues. Note that ``printf()`` is line-buffered, make sure to add ``\n`` to see output in the console. -If you have a good idea of the problem line you can add ``print(new Error().stack)`` to the JavaScript to get a stack trace at that point. - -Debug printouts can even execute arbitrary JavaScript. For example:: +.. _debugging-profiling: - function _addAndPrint($left, $right) { - $left = $left | 0; - $right = $right | 0; - //--- - if ($left < $right) console.log('l` using ``--profiling``, (which is currently the same as +:ref:`-g2 `), and then run the code in the browser's devtools profiler. +You should then be able to see in which functions most of the time is spent. -Chrome devtools support source-level debugging on WebAssembly files with DWARF information. To use that, you need the Wasm debugging extension plugin here: -https://goo.gle/wasm-debugging-extension +Memory +------ -See `Debugging WebAssembly with modern tools -`_ for the details. +The browser's memory profiling tools generally only understand allocations at +the JavaScript level. From that perspective, the entire linear memory that the +emscripten-compiled application uses is a single big allocation (of a +``WebAssembly.Memory``). To get information about usage inside that object, you +need other tools: + +* Emscripten supports the `mallinfo() + `_, API, which gives + you information from ``dlmalloc`` about current allocations. +* Emscripten also has a ``--memoryprofiler`` option that displays memory usage + in a visual manner. Note that you need to emit HTML (e.g. with a command like + ``emcc test/hello_world.c --memoryprofiler -o page.html``) as the memory + profiler output is rendered onto the page. To view it, load ``page.html`` in + your browser (remember to use a :ref:`local webserver `). + The display auto-updates, so you can open the devtools console and run a + command like ``_malloc(1024 * 1024)``. That will allocate 1MB of memory, which + will then show up on the memory profiler display. +.. _debugging-manual-debugging: -Handling C++ Exceptions from JavaScript -======================================= -See :ref:`handling-c-exceptions-from-javascript`. +Manual print debugging +====================== +You can also manually instrument the source code with ``printf()`` statements, +then compile and run the code to investigate issues. The output from the +`stdout` and `stderr` streams is copied to the browser console by default. Note +that ``printf()`` is line-buffered, so make sure to add ``\n`` to see output in +the console. The functions in the :ref:`console.h ` header can also +be used to access the console more directly. .. _debugging-emscripten-specific-issues: -Emscripten-specific issues +Emscripten-Specific Issues ========================== Memory Alignment Issues ----------------------- -The :ref:`Emscripten memory representation ` is compatible with C and C++. However, when undefined behavior is involved you may see differences with native architectures, and also differences between Emscripten's output for asm.js and WebAssembly: - -- In asm.js, loads and stores must be aligned, and performing a normal load or store on an unaligned address can fail silently (access the wrong address). If the compiler knows a load or store is unaligned, it can emulate it in a way that works but is slow. -- In WebAssembly, unaligned loads and stores will work. Each one is annotated with its expected alignment. If the actual alignment does not match, it will still work, but may be slow on some CPU architectures. +The :ref:`Emscripten memory representation ` is +compatible with C and C++. In WebAssembly, unaligned loads and stores will work; +each may be annotated with its expected alignment. However if the actual +alignment does not match, it may be very slow on some systems. .. tip:: :ref:`SAFE_HEAP ` can be used to reveal memory alignment issues. -Generally it is best to avoid unaligned reads and writes — often they occur as the result of undefined behavior, as mentioned above. In some cases, however, they are unavoidable — for example if the code to be ported reads an ``int`` from a packed structure in some pre-existing data format. In that case, to make things work properly in asm.js, and be fast in WebAssembly, you must be sure that the compiler knows the load or store is unaligned. To do so you can: +Generally it is best to avoid unaligned reads and writes. Often they occur as +the result of undefined behavior. In some cases, however, they are unavoidable — +for example if the code to be ported reads an ``int`` from a packed structure in +some pre-existing data format. In that case, to make it as fast as possible in +WebAssembly, you can make sure that the compiler knows the load or store is +unaligned. To do so you can: - Manually read individual bytes and reconstruct the full value -- Use the :c:type:`emscripten_align* ` typedefs, which define unaligned versions of the basic types (``short``, ``int``, ``float``, ``double``). All operations on those types are not fully aligned (use the ``1`` variants in most cases, which mean no alignment whatsoever). - +- Use the :c:type:`emscripten_align* ` typedefs, which + define unaligned versions of the basic types (``short``, ``int``, ``float``, + ``double``). All operations on those types are not fully aligned (use the + ``1`` variants in most cases, which mean no alignment whatsoever). Function Pointer Issues ----------------------- -If you get an ``abort()`` from a function pointer call to ``nullFunc`` or ``b0`` or ``b1`` (possibly with an error message saying "incorrect function pointer"), the problem is that the function pointer was not found in the expected function pointer table when called. +If you get an ``abort()`` from a function pointer call to ``nullFunc`` or ``b0`` +or ``b1`` (possibly with an error message saying "incorrect function pointer"), +the problem is that the function pointer was not found in the expected function +pointer table when called. -.. note:: ``nullFunc`` is the function used to populate empty index entries in the function pointer tables (``b0`` and ``b1`` are shorter names used for ``nullFunc`` in more optimized builds). A function pointer to an invalid index will call this function, which simply calls ``abort()``. +.. note:: ``nullFunc`` is the function used to populate empty index entries in + the function pointer tables (``b0`` and ``b1`` are shorter names used for + ``nullFunc`` in more optimized builds). A function pointer to an invalid + index will call this function, which simply calls ``abort()``. There are several possible causes: -- Your code is calling a function pointer that has been cast from another type (this is undefined behavior but it does happen in real-world code). In optimized Emscripten output, each function pointer type is stored in a separate table based on its original signature, so you *must* call a function pointer with that same signature to get the right behavior (see :ref:`portability-function-pointer-issues` in the code portability section for more information). -- Your code is calling a method on a ``NULL`` pointer or dereferencing 0. This sort of bug can be caused by any sort of coding error, but manifests as a function pointer error because the function can't be found in the expected table at runtime. - -In order to debug these sorts of issues: - -- Compile with ``-Werror``. This turns warnings into errors, which can be useful as some cases of undefined behavior would otherwise show warnings. -- Use ``-sASSERTIONS=2`` to get some useful information about the function pointer being called, and its type. -- Look at the browser stack trace to see where the error occurs and which function should have been called. -- Enable clang warnings on dangerous function pointer casts using ``-Wcast-function-type``. +- Your code is calling a function pointer that has been cast from another type + (this is undefined behavior but it does happen in real-world code). In + optimized Emscripten output, each function pointer type is stored in a + separate table based on its original signature, so you *must* call a function + pointer with that same signature to get the right behavior (see + :ref:`portability-function-pointer-issues` in the code portability section for + more information). +- Your code is calling a method on a ``NULL`` pointer or dereferencing 0. This + sort of bug can be caused by any sort of coding error, but manifests as a + function pointer error because the function can't be found in the expected + table at runtime. + + +To debug these sorts of issues: + +- Compile with ``-Werror`` (or otherwise fix warnings, many of which highlight + undefined behavior). +- Use ``-sASSERTIONS=2`` to get some useful information about the function + pointer being called, and its type. +- Look at the browser stack trace to see where the error occurs and which + function should have been called. +- Enable clang warnings on dangerous function pointer casts using + ``-Wcast-function-type``. - Build with :ref:`SAFE_HEAP=1 `. - :ref:`Sanitizers` can help here, in particular UBSan. -Another function pointer issue is when the wrong function is called. :ref:`SAFE_HEAP=1 ` can help with this as it detects some possible errors with function table accesses. - Infinite loops -------------- -Infinite loops cause your page to hang. After a period the browser will notify the user that the page is stuck and offer to halt or close it. +Infinite loops cause your page to hang. After a period the browser will notify +the user that the page is stuck and offer to halt or close it. If your code hits +an infinite loop, one easy way to find the problem code is to use a *JavaScript +profiler*. In the Firefox profiler, if the code enters an infinite loop you will +see a block of code doing the same thing repeatedly near the end of the profile. -If your code hits an infinite loop, one easy way to find the problem code is to use a *JavaScript profiler*. In the Firefox profiler, if the code enters an infinite loop you will see a block of code doing the same thing repeatedly near the end of the profile. +.. note:: The :ref:`emscripten-runtime-environment-main-loop` may need to be + re-coded if your application uses an infinite main loop. -.. note:: The :ref:`emscripten-runtime-environment-main-loop` may need to be re-coded if your application uses an infinite main loop. +.. _other-debugging-tools: -.. _debugging-profiling: +Debugging Emscripten +==================== -Profiling -========= - -Speed ------ - -To profile your code for speed, build with :ref:`profiling info `, -then run the code in the browser's devtools profiler. You should then be able to -see in which functions is most of the time spent. - -.. _debugging-profiling-memory: - -Memory ------- - -The browser's memory profiling tools generally only understand -allocations at the JavaScript level. From that perspective, the entire linear -memory that the emscripten-compiled application uses is a single big allocation -(of a ``WebAssembly.Memory``). The devtools will not show information about -usage inside that object, so you need other tools for that, which we will now -describe. - -Emscripten supports -`mallinfo() `_, which lets -you get information from ``dlmalloc`` about current allocations. For example -usage, see -`the test `_. - -Emscripten also has a ``--memoryprofiler`` option that displays memory usage -in a visual manner, letting you see how fragmented it is and so forth. To use -it, you can do something like +.. _debugging-EMCC_DEBUG: -.. code-block:: bash +Debugging the compiler driver +----------------------------- - emcc test/hello_world.c --memoryprofiler -o page.html +Compiling with the :ref:`emcc -v ` will cause emcc to output the +sub-commands that it runs as well as passes ``-v`` to Clang. The ``EMCC_DEBUG`` +environment variable can be set to emit even more debug output and generate +intermediate files for the compiler's various stages. -Note that you need to emit HTML as in that example, as the memory profiler -output is rendered onto the page. To view it, load ``page.html`` in your -browser (remember to use a :ref:`local webserver `). The display -auto-updates, so you can open the devtools console and run a command like -``_malloc(1024 * 1024)``. That will allocate 1MB of memory, which will then show -up on the memory profiler display. .. _debugging-autodebugger: AutoDebugger -============ +------------ -The *AutoDebugger* is the 'nuclear option' for debugging Emscripten code. +The *AutoDebugger* is the 'nuclear option' for debugging Emscripten code. It +will rewrite the output so it prints out each store to memory. This is useful +for comparing the output for different compiler settings in order to detect +regressions. To run the *AutoDebugger*, compile with the environment variable +``EMCC_AUTODEBUG=1`` set. .. warning:: This option is primarily intended for Emscripten core developers. -The *AutoDebugger* will rewrite the output so it prints out each store to memory. This is useful because you can compare the output for different compiler settings in order to detect regressions. +The *AutoDebugger* will rewrite the output so it prints out each store to +memory. This is useful because you can compare the output for different compiler +settings in order to detect regressions. -The *AutoDebugger* can potentially find **any** problem in the generated code, so it is strictly more powerful than the ``CHECK_*`` settings and ``SAFE_HEAP``. One use of the *AutoDebugger* is to quickly emit lots of logging output, which can then be reviewed for odd behavior. The *AutoDebugger* is also particularly useful for :ref:`debugging regressions `. +The *AutoDebugger* can potentially find **any** problem in the generated code, +so it is strictly more powerful than the ``CHECK_*`` settings and ``SAFE_HEAP``. +One use of the *AutoDebugger* is to quickly emit lots of logging output, which +can then be reviewed for odd behavior. The *AutoDebugger* is also particularly +useful for :ref:`debugging regressions `. The *AutoDebugger* has some limitations: -- It generates a lot of output. Using *diff* can be very helpful for identifying changes. -- It prints out simple numerical values rather than pointer addresses (because pointer addresses change between runs, and hence can't be compared). This is a limitation because sometimes inspection of addresses can show errors where the pointer address is 0 or impossibly large. It is possible to modify the tool to print out addresses as integers in ``tools/autodebugger.py``. +- It generates a lot of output. Using *diff* can be very helpful for + identifying changes. +- It prints out simple numerical values rather than pointer addresses (because + pointer addresses change between runs, and hence can't be compared). This is + a limitation because sometimes inspection of addresses can show errors where + the pointer address is 0 or impossibly large. It is possible to modify the + tool to print out addresses as integers in ``tools/autodebugger.py``. -To run the *AutoDebugger*, compile with the environment variable ``EMCC_AUTODEBUG=1`` set. For example: +To run the *AutoDebugger*, compile with the environment variable +``EMCC_AUTODEBUG=1`` set. For example: .. code-block:: bash # Linux or macOS EMCC_AUTODEBUG=1 emcc test/hello_world.cpp -o hello.html - # Windows set EMCC_AUTODEBUG=1 emcc test/hello_world.cpp -o hello.html @@ -329,7 +450,9 @@ AutoDebugger Regression Workflow Use the following workflow to find regressions with the *AutoDebugger*: - Compile the working code with ``EMCC_AUTODEBUG=1`` set in the environment. -- Compile the code using ``EMCC_AUTODEBUG=1`` in the environment again, but this time with the settings that cause the regression. Following this step we have one build before the regression and one after. +- Compile the code using ``EMCC_AUTODEBUG=1`` in the environment again, but this + time with the settings that cause the regression. Following this step we have + one build before the regression and one after. - Run both versions of the compiled code and save their output. - Compare the output using a *diff* tool. @@ -340,17 +463,19 @@ Any difference between the outputs is likely to be caused by the bug. and other issues don't cause false positives. + Useful Links ============ -- `Blogpost about reading compiler output `_. -- `GDC 2014: Getting started with asm.js and Emscripten `_ (Debugging slides). - `Links to Wasm debugging-related documents `_ Need help? ========== -The :ref:`Emscripten Test Suite ` contains good examples of almost all functionality offered by Emscripten. If you have a problem, it is a good idea to search the suite to determine whether test code with similar behavior is able to run. +The :ref:`Emscripten Test Suite ` contains good examples +of almost all functionality offered by Emscripten. If you have a problem, it is +a good idea to search the suite to determine whether test code with similar +behavior is able to run. If you've tried the ideas here and you need more help, please :ref:`contact`. diff --git a/site/source/docs/tools_reference/emcc.rst b/site/source/docs/tools_reference/emcc.rst index 266b3d5ed6ba6..68208354eb2be 100644 --- a/site/source/docs/tools_reference/emcc.rst +++ b/site/source/docs/tools_reference/emcc.rst @@ -54,7 +54,7 @@ Options that are modified or new in *emcc* are listed below: ``-O1`` [compile+link] - Simple optimizations. During the compile step these include LLVM ``-O1`` optimizations. During the link step this does not include various runtime assertions in JS that `-O0` would do. + Simple optimizations. During the compile step these include LLVM ``-O1`` optimizations. During the link step this omits various runtime assertions in JS that `-O0` would include. .. _emcc-O2: @@ -68,7 +68,7 @@ Options that are modified or new in *emcc* are listed below: ``-O3`` [compile+link] - Like ``-O2``, but with additional optimizations that may take longer to run. + Like ``-O2``, but with additional optimizations that may take longer to run and may increase code size. .. note:: This is a good setting for a release build. @@ -76,8 +76,8 @@ Options that are modified or new in *emcc* are listed below: ``-Og`` [compile+link] - Like ``-O1``. In future versions, this option might disable different - optimizations in order to improve debuggability. + Like ``-O1``, with an additional flag to extend the liveness of variables for improved debugging. + In future versions, additional optimizations might also be disabled. .. _emcc-Os: @@ -173,9 +173,11 @@ Options that are modified or new in *emcc* are listed below: .. _emcc-gsource-map: ``-gsource-map[=inline]`` - [link] + [compile+link] + [same as -g3 if passed at compile time, otherwise applies at link] Generate a source map using LLVM debug information (which must - be present in object files, i.e., they should have been compiled with ``-g``). + be present in object files, i.e., they should have been compiled with ``-g`` + or ``-gsource-map``). When this option is provided, the **.wasm** file is updated to have a ``sourceMappingURL`` section. The resulting URL will have format: @@ -193,7 +195,9 @@ Options that are modified or new in *emcc* are listed below: ``-g`` [compile+link] - Controls the level of debuggability. Each level builds on the previous one: + If used at compile time, adds progressively more DWARF information to the object file, + according to the underlying behavior of clang. + If used at link time, controls the level of debuggability overall. Each level builds on the previous one: - .. _emcc-g0: @@ -203,29 +207,31 @@ Options that are modified or new in *emcc* are listed below: - .. _emcc-g1: - ``-g1``: When linking, preserve whitespace in JavaScript. + ``-g1``: Preserve whitespace in JavaScript. - .. _emcc-g2: - ``-g2``: When linking, preserve function names in compiled code. + ``-g2``: Also preserve function names in compiled code (via the wasm name section). - .. _emcc-g3: - ``-g3``: When compiling to object files, keep debug info, including JS whitespace, function names, and LLVM debug info (DWARF) if any (this is the same as :ref:`-g `). + ``-g3``: Also keep LLVM debug info (DWARF) if there is any in the object files (this is the same as :ref:`-g `). .. _emcc-profiling: ``--profiling`` - [same as -g2 if passed at compile time, otherwise applies at link] - Use reasonable defaults when emitting JavaScript to make the build readable but still useful for profiling. This sets ``-g2`` (preserve whitespace and function names) and may also enable optimizations that affect performance and otherwise might not be performed in ``-g2``. + [link] + Make the output suitable for profiling. This means including function names in the wasm and JS output, and + preserving whitespace in the JS output. It does not affect optimizations (to ensure that performance profiles + reflect production builds). Currenly this is the same as ``-g2``. .. _emcc-profiling-funcs: ``--profiling-funcs`` [link] - Preserve function names in profiling, but otherwise minify whitespace and names as we normally do in optimized builds. This is useful if you want to look at profiler results based on function names, but do *not* intend to read the emitted code. + Preserve wasm function names as in ``--profiling``, but otherwise minify whitespace and names as we normally do in optimized builds. This is useful if you want to look at profiler results based on function names, but do *not* intend to read the emitted code. ``--tracing`` [link] @@ -244,7 +250,8 @@ Options that are modified or new in *emcc* are listed below: Save a map file between function indexes in the Wasm and function names. By storing the names on a file on the side, you can avoid shipping the names, and can still reconstruct meaningful stack traces by translating the indexes back - to the names. + to the names. This is a simpler format than source maps, but less detailed + because it only describes function names and not source locations. .. note:: When used with ``-sWASM=2``, two symbol files are created. ``[name].js.symbols`` (with WASM symbols) and ``[name].wasm.js.symbols`` (with ASM.js symbols)