Debugging/zh-tw: Difference between revisions

From FreeCAD Documentation
(Updating to match new version of source page)
(Updating to match new version of source page)
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
<languages/>
<languages/>
{{Docnav
|[[Module Creation|Module Creation]]
|[[Testing|Testing]]
}}
{{TOCright}}
== Test First ==
== Test First ==

Before you go through the pain of debugging use the [[Testing|Test framework]] to check if the standard tests work properly. If they do not run complete there is possibly a broken installation.
Before you go through the pain of debugging use the [[Testing|Test framework]] to check if the standard tests work properly. If they do not run complete there is possibly a broken installation.


== Command Line ==
== Command Line ==

The ''debugging'' of FreeCAD is supported by a few internal mechanisms. The command line version of FreeCAD provides some options for debugging support.
The ''debugging'' of FreeCAD is supported by a few internal mechanisms. The command line version of FreeCAD provides some options for debugging support.


Line 20: Line 27:
arbitrary file
arbitrary file
-u [ --user-cfg ] arg User config file to load/save user settings
-u [ --user-cfg ] arg User config file to load/save user settings
-s [ --system-cfg ] arg Systen config file to load/save system settings
-s [ --system-cfg ] arg System config file to load/save system settings
-t [ --run-test ] arg Test level
-t [ --run-test ] arg Test level
-M [ --module-path ] arg Additional module paths
-M [ --module-path ] arg Additional module paths
Line 26: Line 33:


== Generating a Backtrace ==
== Generating a Backtrace ==

If you are running a version of FreeCAD from the bleeding edge of the development curve, it may "crash". You can help solve such problems by providing the developers with a "backtrace". To do this, you need to be running a "debug build" of the software. "Debug build" is a parameter that is set at compile time, so you'll either need to compile FreeCAD yourself, or obtain a pre-compiled "debug" version.
If you are running a version of FreeCAD from the bleeding edge of the development curve, it may "crash". You can help solve such problems by providing the developers with a "backtrace". To do this, you need to be running a "debug build" of the software. "Debug build" is a parameter that is set at compile time, so you'll either need to compile FreeCAD yourself, or obtain a pre-compiled "debug" version.


=== For Linux ===
=== For Linux ===

<!-- START OF COLLAPSIBLE DIV --><div class="toccolours mw-collapsible mw-collapsed" style="width:800px;">
<!-- START OF COLLAPSIBLE DIV --><div class="toccolours mw-collapsible mw-collapsed" style="width:800px;">

Linux Debugging ---->
Linux Debugging →

<div class="mw-collapsible-content">
<div class="mw-collapsible-content">

Prerequisites:
Prerequisites:


* software package gdb installed
* software package gdb installed
* a debug build of FreeCAD (at this time only available by [[CompileOnUnix#For_a_Debug_build|building from source]])
* a debug build of FreeCAD (at this time only available by [[Compile_on_Linux/Unix#For_a_Debug_build|building from source]])
* a FreeCAD model that causes a crash
* a FreeCAD model that causes a crash


Steps:
Steps:
Enter the following in your terminal window:
Enter the following in your terminal window:

<pre>
{{Code|code=
$ cd FreeCAD/bin
$ cd FreeCAD/bin
$ gdb FreeCAD
$ gdb FreeCAD
}}
</pre>

GNUdebugger will output some initializing information. The (gdb) shows GNUDebugger is running in the terminal, now input:
GNUdebugger will output some initializing information. The (gdb) shows GNUDebugger is running in the terminal, now input:

<pre>
{{Code|code=
(gdb) handle SIG33 noprint nostop
(gdb) handle SIG33 noprint nostop
(gdb) run
(gdb) run
}}
</pre>

FreeCAD will now start up. Perform the steps that cause FreeCAD to crash or freeze, then enter in the terminal window:
FreeCAD will now start up. Perform the steps that cause FreeCAD to crash or freeze, then enter in the terminal window:

<pre>
{{Code|code=
(gdb) bt
(gdb) bt
}}
</pre>

This will generate a lengthy listing of exactly what the program was doing when it crashed or froze. Include this with your problem report.
This will generate a lengthy listing of exactly what the program was doing when it crashed or froze. Include this with your problem report.

{{Code|code=
(gdb) bt full
}}

Print the values of the local variables also. This can be combined with a number to limit the number of frames shown.

</div></div> <!-- END OF COLLAPSIBLE DIV -->
</div></div> <!-- END OF COLLAPSIBLE DIV -->


=== For MacOSX ===
=== For MacOSX ===

<!-- START OF COLLAPISBLE DIV --><div class="toccolours mw-collapsible mw-collapsed" style="width:800px;">
<!-- START OF COLLAPISBLE DIV --><div class="toccolours mw-collapsible mw-collapsed" style="width:800px;">

MacOSX Debugging ---->
MacOSX Debugging →

<div class="mw-collapsible-content">
<div class="mw-collapsible-content">

Prerequisites:
Prerequisites:


Line 69: Line 98:
Enter the following in your terminal window:
Enter the following in your terminal window:


{{Code|code=
<pre>
$ cd FreeCAD/bin
$ cd FreeCAD/bin
$ lldb FreeCAD
$ lldb FreeCAD
}}
</pre>

LLDB will output some initializing information. The (lldb) shows the debugger is running in the terminal, now input:
LLDB will output some initializing information. The (lldb) shows the debugger is running in the terminal, now input:

<pre>
{{Code|code=
(lldb) run
(lldb) run
}}
</pre>


FreeCAD will now start up. Perform the steps that cause FreeCAD to crash or freeze, then enter in the terminal window:
FreeCAD will now start up. Perform the steps that cause FreeCAD to crash or freeze, then enter in the terminal window:

<pre>
{{Code|code=
(lldb) bt
(lldb) bt
}}
</pre>

This will generate a lengthy listing of exactly what the program was doing when it crashed or froze. Include this with your problem report.
This will generate a lengthy listing of exactly what the program was doing when it crashed or froze. Include this with your problem report.

</div></div> <!-- END OF COLLAPSIBLE DIV -->
</div></div> <!-- END OF COLLAPSIBLE DIV -->


== Python Debugging ==
== Python Debugging ==

'''For a more modern approach to debugging Python, at least on Windows, see this [https://forum.freecadweb.org/viewtopic.php?f=22&t=28901&hilit=2017| post in the Forum].'''
For a more modern approach to debugging Python, see these posts:
* [https://forum.freecadweb.org/viewtopic.php?f=22&t=28901 Debugging macros with VS 2017]
* [https://forum.freecadweb.org/viewtopic.php?f=10&t=35383 Python workbenches debugging]
* [https://forum.freecadweb.org/viewtopic.php?f=4&t=40251 python3.dll, Qt5Windgets.dll, Qt5Gui.dll and Qt5Core.dll not found]

=== winpdb ===

<!-- START OF COLLAPSIBLE DIV --><div class="toccolours mw-collapsible mw-collapsed" style="width:800px;">

winpdb Debugging →

<div class="mw-collapsible-content">


Here is an example of using ''Winpdb'' inside FreeCAD:
Here is an example of using ''Winpdb'' inside FreeCAD:
Line 99: Line 145:


# Start ''Winpdb''.
# Start ''Winpdb''.
# Set the debugger password to "test": Go to menu ''File''->''Password" and set the password.
# Set the debugger password to "test": Go to menu ''File''''Password" and set the password.


Now we will run a test python script in FreeCAD step by step.
Now we will run a test python script in FreeCAD step by step.
Line 105: Line 151:
# Run winpdb and set the password (e.g. test)
# Run winpdb and set the password (e.g. test)
# Create a Python file with this content
# Create a Python file with this content

<syntaxhighlight>
{{Code|code=
import rpdb2
import rpdb2
rpdb2.start_embedded_debugger("test")
rpdb2.start_embedded_debugger("test")
import FreeCAD
import Part
import FreeCAD
import Draft
import Part
import Draft
print "hello"
print "hello"
print "hello"
print "hello"
import Draft
import Draft
points=[FreeCAD.Vector(-3.0,-1.0,0.0),FreeCAD.Vector(-2.0,0.0,0.0)]
points=[FreeCAD.Vector(-3.0,-1.0,0.0),FreeCAD.Vector(-2.0,0.0,0.0)]
Draft.makeWire(points,closed=False,face=False,support=None)
Draft.makeWire(points,closed=False,face=False,support=None)
</syntaxhighlight>
}}

# Start FreeCAD and load the above file into FreeCAD
# Start FreeCAD and load the above file into FreeCAD
# Press F6 to execute it
# Press F6 to execute it
Line 125: Line 173:
# Now press F7 to step into the Python code of Draft.makeWire
# Now press F7 to step into the Python code of Draft.makeWire


</div></div> <!-- END OF COLLAPSIBLE DIV -->
{{Docnav|Module Creation|Testing}}


=== Visual Studio Code (VS Code) ===
[[Category:Developer Documentation/zh-tw]]

<!-- START OF COLLAPSIBLE DIV --><div class="toccolours mw-collapsible mw-collapsed" style="width:800px;">

VS Code Debugging →

<div class="mw-collapsible-content">

Prerequisites:

* ptvsd package need to be installed

{{Code|code=
pip install ptvsd
}}

[https://pypi.org/project/ptvsd/ pypi page]

[https://code.visualstudio.com/docs/python/debugging#_remote-debugging Visual Studio Code documentation for remote debugging]

Steps:
* Add following code at the beginning of your script

{{Code|code=
import ptvsd
print("Waiting for debugger attach")
# 5678 is the default attach port in the VS Code debug configurations
ptvsd.enable_attach(address=('localhost', 5678), redirect_output=True)
ptvsd.wait_for_attach()
}}

* Add a debug configuration in Visual Studio Code {{MenuCommand|Debug → Add Configurations…}}. It should looks like this :

<!--NOT CHANGE THE <pre> </pre> BALISE CAUSE THE PIPE OR SPACE IN MACRO CODE CUT THE MACRO-->
<pre>
"configurations": [
{
"name": "Python: Attacher",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
},
</pre>

* In VS Code add a breakpoint anywhere you want.
* Launch the script in FreeCAD. FreeCAD freeze waiting for attachment.
* In VS Code start debugging used created configuration. You should see variables in debugger area.

</div></div> <!-- END OF COLLAPSIBLE DIV -->

{{Docnav
|[[Module Creation|Module Creation]]
|[[Testing|Testing]]
}}


{{Userdocnavi{{#translation:}}}}
{{clear}}
[[Category:Developer Documentation{{#translation:}}]]

Revision as of 22:11, 20 February 2020

Test First

Before you go through the pain of debugging use the Test framework to check if the standard tests work properly. If they do not run complete there is possibly a broken installation.

Command Line

The debugging of FreeCAD is supported by a few internal mechanisms. The command line version of FreeCAD provides some options for debugging support.

These are the currently recognized options in FreeCAD 0.15:

Generic options:

 -v [ --version ]      Prints version string
 -h [ --help ]         Prints help message
 -c [ --console ]      Starts in console mode
 --response-file arg   Can be specified with '@name', too

Configuration:

 -l [ --write-log ]       Writes a log file to:
                          $HOME/.FreeCAD/FreeCAD.log
 --log-file arg           Unlike to --write-log this allows to log to an 
                          arbitrary file
 -u [ --user-cfg ] arg    User config file to load/save user settings
 -s [ --system-cfg ] arg  System config file to load/save system settings
 -t [ --run-test ] arg    Test level
 -M [ --module-path ] arg Additional module paths
 -P [ --python-path ] arg Additional python paths

Generating a Backtrace

If you are running a version of FreeCAD from the bleeding edge of the development curve, it may "crash". You can help solve such problems by providing the developers with a "backtrace". To do this, you need to be running a "debug build" of the software. "Debug build" is a parameter that is set at compile time, so you'll either need to compile FreeCAD yourself, or obtain a pre-compiled "debug" version.

For Linux

Linux Debugging →

Prerequisites:

  • software package gdb installed
  • a debug build of FreeCAD (at this time only available by building from source)
  • a FreeCAD model that causes a crash

Steps: Enter the following in your terminal window:

$ cd FreeCAD/bin
$ gdb FreeCAD

GNUdebugger will output some initializing information. The (gdb) shows GNUDebugger is running in the terminal, now input:

(gdb) handle SIG33 noprint nostop
(gdb) run

FreeCAD will now start up. Perform the steps that cause FreeCAD to crash or freeze, then enter in the terminal window:

(gdb) bt

This will generate a lengthy listing of exactly what the program was doing when it crashed or froze. Include this with your problem report.

(gdb) bt full

Print the values of the local variables also. This can be combined with a number to limit the number of frames shown.

For MacOSX

MacOSX Debugging →

Prerequisites:

  • software package lldb installed
  • a debug build of FreeCAD
  • a FreeCAD model that causes a crash

Steps: Enter the following in your terminal window:

$ cd FreeCAD/bin
$ lldb FreeCAD

LLDB will output some initializing information. The (lldb) shows the debugger is running in the terminal, now input:

(lldb) run

FreeCAD will now start up. Perform the steps that cause FreeCAD to crash or freeze, then enter in the terminal window:

(lldb) bt

This will generate a lengthy listing of exactly what the program was doing when it crashed or froze. Include this with your problem report.

Python Debugging

For a more modern approach to debugging Python, see these posts:

winpdb

winpdb Debugging →

Here is an example of using Winpdb inside FreeCAD:

We need the python debugger: Winpdb. If you do not have it installed, on Ubuntu/Debian install it with:

sudo apt-get install winpdb

Now lets setup the debugger.

  1. Start Winpdb.
  2. Set the debugger password to "test": Go to menu FilePassword" and set the password.

Now we will run a test python script in FreeCAD step by step.

  1. Run winpdb and set the password (e.g. test)
  2. Create a Python file with this content
import rpdb2
rpdb2.start_embedded_debugger("test")
import FreeCAD
import Part
import Draft
print "hello"
print "hello"
import Draft
points=[FreeCAD.Vector(-3.0,-1.0,0.0),FreeCAD.Vector(-2.0,0.0,0.0)]
Draft.makeWire(points,closed=False,face=False,support=None)
  1. Start FreeCAD and load the above file into FreeCAD
  2. Press F6 to execute it
  3. Now FreeCAD will become unresponsive because the Python debugger is waiting
  4. Switch to the Windpdb GUI and click on "Attach". After a few seconds an item "<Input>" appears where you have to double-click
  5. Now the currently executed script appears in Winpdb.
  6. Set a break at the last line and press F5
  7. Now press F7 to step into the Python code of Draft.makeWire

Visual Studio Code (VS Code)

VS Code Debugging →

Prerequisites:

  • ptvsd package need to be installed
pip install ptvsd

pypi page

Visual Studio Code documentation for remote debugging

Steps:

  • Add following code at the beginning of your script
import ptvsd
print("Waiting for debugger attach")
# 5678 is the default attach port in the VS Code debug configurations
ptvsd.enable_attach(address=('localhost', 5678), redirect_output=True)
ptvsd.wait_for_attach()
  • Add a debug configuration in Visual Studio Code Debug → Add Configurations…. It should looks like this :
    "configurations": [
        {
            "name": "Python: Attacher",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ]
        },
  • In VS Code add a breakpoint anywhere you want.
  • Launch the script in FreeCAD. FreeCAD freeze waiting for attachment.
  • In VS Code start debugging used created configuration. You should see variables in debugger area.