gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

522 lines
34KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="faulthandler — Dump the Python traceback" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/faulthandler.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="This module contains functions to dump Python tracebacks explicitly, on a fault, after a timeout, or on a user signal. Call faulthandler.enable() to install fault handlers for the SIGSEGV, SIGFPE, ..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="This module contains functions to dump Python tracebacks explicitly, on a fault, after a timeout, or on a user signal. Call faulthandler.enable() to install fault handlers for the SIGSEGV, SIGFPE, ..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>faulthandler — Dump the Python traceback &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="pdb — The Python Debugger" href="pdb.html" />
  33. <link rel="prev" title="bdb — Debugger framework" href="bdb.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/faulthandler.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code> — Dump the Python traceback</a><ul>
  86. <li><a class="reference internal" href="#dumping-the-traceback">Dumping the traceback</a></li>
  87. <li><a class="reference internal" href="#fault-handler-state">Fault handler state</a></li>
  88. <li><a class="reference internal" href="#dumping-the-tracebacks-after-a-timeout">Dumping the tracebacks after a timeout</a></li>
  89. <li><a class="reference internal" href="#dumping-the-traceback-on-a-user-signal">Dumping the traceback on a user signal</a></li>
  90. <li><a class="reference internal" href="#issue-with-file-descriptors">Issue with file descriptors</a></li>
  91. <li><a class="reference internal" href="#example">Example</a></li>
  92. </ul>
  93. </li>
  94. </ul>
  95. </div>
  96. <div>
  97. <h4>Previous topic</h4>
  98. <p class="topless"><a href="bdb.html"
  99. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">bdb</span></code> — Debugger framework</a></p>
  100. </div>
  101. <div>
  102. <h4>Next topic</h4>
  103. <p class="topless"><a href="pdb.html"
  104. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pdb</span></code> — The Python Debugger</a></p>
  105. </div>
  106. <div role="note" aria-label="source link">
  107. <h3>This Page</h3>
  108. <ul class="this-page-menu">
  109. <li><a href="../bugs.html">Report a Bug</a></li>
  110. <li>
  111. <a href="https://github.com/python/cpython/blob/main/Doc/library/faulthandler.rst"
  112. rel="nofollow">Show Source
  113. </a>
  114. </li>
  115. </ul>
  116. </div>
  117. </nav>
  118. </div>
  119. </div>
  120. <div class="related" role="navigation" aria-label="related navigation">
  121. <h3>Navigation</h3>
  122. <ul>
  123. <li class="right" style="margin-right: 10px">
  124. <a href="../genindex.html" title="General Index"
  125. accesskey="I">index</a></li>
  126. <li class="right" >
  127. <a href="../py-modindex.html" title="Python Module Index"
  128. >modules</a> |</li>
  129. <li class="right" >
  130. <a href="pdb.html" title="pdb — The Python Debugger"
  131. accesskey="N">next</a> |</li>
  132. <li class="right" >
  133. <a href="bdb.html" title="bdb — Debugger framework"
  134. accesskey="P">previous</a> |</li>
  135. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  136. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  137. <li class="switchers">
  138. <div class="language_switcher_placeholder"></div>
  139. <div class="version_switcher_placeholder"></div>
  140. </li>
  141. <li>
  142. </li>
  143. <li id="cpython-language-and-version">
  144. <a href="../index.html">3.12.3 Documentation</a> &#187;
  145. </li>
  146. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  147. <li class="nav-item nav-item-2"><a href="debug.html" accesskey="U">Debugging and Profiling</a> &#187;</li>
  148. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code> — Dump the Python traceback</a></li>
  149. <li class="right">
  150. <div class="inline-search" role="search">
  151. <form class="inline-search" action="../search.html" method="get">
  152. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  153. <input type="submit" value="Go" />
  154. </form>
  155. </div>
  156. |
  157. </li>
  158. <li class="right">
  159. <label class="theme-selector-label">
  160. Theme
  161. <select class="theme-selector" oninput="activateTheme(this.value)">
  162. <option value="auto" selected>Auto</option>
  163. <option value="light">Light</option>
  164. <option value="dark">Dark</option>
  165. </select>
  166. </label> |</li>
  167. </ul>
  168. </div>
  169. <div class="document">
  170. <div class="documentwrapper">
  171. <div class="bodywrapper">
  172. <div class="body" role="main">
  173. <section id="module-faulthandler">
  174. <span id="faulthandler-dump-the-python-traceback"></span><h1><a class="reference internal" href="#module-faulthandler" title="faulthandler: Dump the Python traceback."><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code></a> — Dump the Python traceback<a class="headerlink" href="#module-faulthandler" title="Link to this heading">¶</a></h1>
  175. <div class="versionadded">
  176. <p><span class="versionmodified added">New in version 3.3.</span></p>
  177. </div>
  178. <hr class="docutils" />
  179. <p>This module contains functions to dump Python tracebacks explicitly, on a fault,
  180. after a timeout, or on a user signal. Call <a class="reference internal" href="#faulthandler.enable" title="faulthandler.enable"><code class="xref py py-func docutils literal notranslate"><span class="pre">faulthandler.enable()</span></code></a> to
  181. install fault handlers for the <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGSEGV</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGFPE</span></code>,
  182. <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGABRT</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGBUS</span></code>, and <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGILL</span></code> signals. You can also
  183. enable them at startup by setting the <span class="target" id="index-0"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONFAULTHANDLER"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONFAULTHANDLER</span></code></a> environment
  184. variable or by using the <a class="reference internal" href="../using/cmdline.html#cmdoption-X"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> <code class="docutils literal notranslate"><span class="pre">faulthandler</span></code> command line option.</p>
  185. <p>The fault handler is compatible with system fault handlers like Apport or the
  186. Windows fault handler. The module uses an alternative stack for signal handlers
  187. if the <code class="xref c c-func docutils literal notranslate"><span class="pre">sigaltstack()</span></code> function is available. This allows it to dump the
  188. traceback even on a stack overflow.</p>
  189. <p>The fault handler is called on catastrophic cases and therefore can only use
  190. signal-safe functions (e.g. it cannot allocate memory on the heap). Because of
  191. this limitation traceback dumping is minimal compared to normal Python
  192. tracebacks:</p>
  193. <ul class="simple">
  194. <li><p>Only ASCII is supported. The <code class="docutils literal notranslate"><span class="pre">backslashreplace</span></code> error handler is used on
  195. encoding.</p></li>
  196. <li><p>Each string is limited to 500 characters.</p></li>
  197. <li><p>Only the filename, the function name and the line number are
  198. displayed. (no source code)</p></li>
  199. <li><p>It is limited to 100 frames and 100 threads.</p></li>
  200. <li><p>The order is reversed: the most recent call is shown first.</p></li>
  201. </ul>
  202. <p>By default, the Python traceback is written to <a class="reference internal" href="sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a>. To see
  203. tracebacks, applications must be run in the terminal. A log file can
  204. alternatively be passed to <a class="reference internal" href="#faulthandler.enable" title="faulthandler.enable"><code class="xref py py-func docutils literal notranslate"><span class="pre">faulthandler.enable()</span></code></a>.</p>
  205. <p>The module is implemented in C, so tracebacks can be dumped on a crash or when
  206. Python is deadlocked.</p>
  207. <p>The <a class="reference internal" href="devmode.html#devmode"><span class="std std-ref">Python Development Mode</span></a> calls <a class="reference internal" href="#faulthandler.enable" title="faulthandler.enable"><code class="xref py py-func docutils literal notranslate"><span class="pre">faulthandler.enable()</span></code></a>
  208. at Python startup.</p>
  209. <div class="admonition seealso">
  210. <p class="admonition-title">See also</p>
  211. <dl class="simple">
  212. <dt>Module <a class="reference internal" href="pdb.html#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pdb</span></code></a></dt><dd><p>Interactive source code debugger for Python programs.</p>
  213. </dd>
  214. <dt>Module <a class="reference internal" href="traceback.html#module-traceback" title="traceback: Print or retrieve a stack traceback."><code class="xref py py-mod docutils literal notranslate"><span class="pre">traceback</span></code></a></dt><dd><p>Standard interface to extract, format and print stack traces of Python programs.</p>
  215. </dd>
  216. </dl>
  217. </div>
  218. <section id="dumping-the-traceback">
  219. <h2>Dumping the traceback<a class="headerlink" href="#dumping-the-traceback" title="Link to this heading">¶</a></h2>
  220. <dl class="py function">
  221. <dt class="sig sig-object py" id="faulthandler.dump_traceback">
  222. <span class="sig-prename descclassname"><span class="pre">faulthandler.</span></span><span class="sig-name descname"><span class="pre">dump_traceback</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">sys.stderr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">all_threads</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#faulthandler.dump_traceback" title="Link to this definition">¶</a></dt>
  223. <dd><p>Dump the tracebacks of all threads into <em>file</em>. If <em>all_threads</em> is
  224. <code class="docutils literal notranslate"><span class="pre">False</span></code>, dump only the current thread.</p>
  225. <div class="admonition seealso">
  226. <p class="admonition-title">See also</p>
  227. <p><a class="reference internal" href="traceback.html#traceback.print_tb" title="traceback.print_tb"><code class="xref py py-func docutils literal notranslate"><span class="pre">traceback.print_tb()</span></code></a>, which can be used to print a traceback object.</p>
  228. </div>
  229. <div class="versionchanged">
  230. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added support for passing file descriptor to this function.</p>
  231. </div>
  232. </dd></dl>
  233. </section>
  234. <section id="fault-handler-state">
  235. <h2>Fault handler state<a class="headerlink" href="#fault-handler-state" title="Link to this heading">¶</a></h2>
  236. <dl class="py function">
  237. <dt class="sig sig-object py" id="faulthandler.enable">
  238. <span class="sig-prename descclassname"><span class="pre">faulthandler.</span></span><span class="sig-name descname"><span class="pre">enable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">sys.stderr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">all_threads</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#faulthandler.enable" title="Link to this definition">¶</a></dt>
  239. <dd><p>Enable the fault handler: install handlers for the <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGSEGV</span></code>,
  240. <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGFPE</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGABRT</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGBUS</span></code> and <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGILL</span></code>
  241. signals to dump the Python traceback. If <em>all_threads</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>,
  242. produce tracebacks for every running thread. Otherwise, dump only the current
  243. thread.</p>
  244. <p>The <em>file</em> must be kept open until the fault handler is disabled: see
  245. <a class="reference internal" href="#faulthandler-fd"><span class="std std-ref">issue with file descriptors</span></a>.</p>
  246. <div class="versionchanged">
  247. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added support for passing file descriptor to this function.</p>
  248. </div>
  249. <div class="versionchanged">
  250. <p><span class="versionmodified changed">Changed in version 3.6: </span>On Windows, a handler for Windows exception is also installed.</p>
  251. </div>
  252. <div class="versionchanged">
  253. <p><span class="versionmodified changed">Changed in version 3.10: </span>The dump now mentions if a garbage collector collection is running
  254. if <em>all_threads</em> is true.</p>
  255. </div>
  256. </dd></dl>
  257. <dl class="py function">
  258. <dt class="sig sig-object py" id="faulthandler.disable">
  259. <span class="sig-prename descclassname"><span class="pre">faulthandler.</span></span><span class="sig-name descname"><span class="pre">disable</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#faulthandler.disable" title="Link to this definition">¶</a></dt>
  260. <dd><p>Disable the fault handler: uninstall the signal handlers installed by
  261. <a class="reference internal" href="#faulthandler.enable" title="faulthandler.enable"><code class="xref py py-func docutils literal notranslate"><span class="pre">enable()</span></code></a>.</p>
  262. </dd></dl>
  263. <dl class="py function">
  264. <dt class="sig sig-object py" id="faulthandler.is_enabled">
  265. <span class="sig-prename descclassname"><span class="pre">faulthandler.</span></span><span class="sig-name descname"><span class="pre">is_enabled</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#faulthandler.is_enabled" title="Link to this definition">¶</a></dt>
  266. <dd><p>Check if the fault handler is enabled.</p>
  267. </dd></dl>
  268. </section>
  269. <section id="dumping-the-tracebacks-after-a-timeout">
  270. <h2>Dumping the tracebacks after a timeout<a class="headerlink" href="#dumping-the-tracebacks-after-a-timeout" title="Link to this heading">¶</a></h2>
  271. <dl class="py function">
  272. <dt class="sig sig-object py" id="faulthandler.dump_traceback_later">
  273. <span class="sig-prename descclassname"><span class="pre">faulthandler.</span></span><span class="sig-name descname"><span class="pre">dump_traceback_later</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">timeout</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">repeat</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">sys.stderr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exit</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#faulthandler.dump_traceback_later" title="Link to this definition">¶</a></dt>
  274. <dd><p>Dump the tracebacks of all threads, after a timeout of <em>timeout</em> seconds, or
  275. every <em>timeout</em> seconds if <em>repeat</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>. If <em>exit</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, call
  276. <code class="xref c c-func docutils literal notranslate"><span class="pre">_exit()</span></code> with status=1 after dumping the tracebacks. (Note
  277. <code class="xref c c-func docutils literal notranslate"><span class="pre">_exit()</span></code> exits the process immediately, which means it doesn’t do any
  278. cleanup like flushing file buffers.) If the function is called twice, the new
  279. call replaces previous parameters and resets the timeout. The timer has a
  280. sub-second resolution.</p>
  281. <p>The <em>file</em> must be kept open until the traceback is dumped or
  282. <a class="reference internal" href="#faulthandler.cancel_dump_traceback_later" title="faulthandler.cancel_dump_traceback_later"><code class="xref py py-func docutils literal notranslate"><span class="pre">cancel_dump_traceback_later()</span></code></a> is called: see <a class="reference internal" href="#faulthandler-fd"><span class="std std-ref">issue with file
  283. descriptors</span></a>.</p>
  284. <p>This function is implemented using a watchdog thread.</p>
  285. <div class="versionchanged">
  286. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added support for passing file descriptor to this function.</p>
  287. </div>
  288. <div class="versionchanged">
  289. <p><span class="versionmodified changed">Changed in version 3.7: </span>This function is now always available.</p>
  290. </div>
  291. </dd></dl>
  292. <dl class="py function">
  293. <dt class="sig sig-object py" id="faulthandler.cancel_dump_traceback_later">
  294. <span class="sig-prename descclassname"><span class="pre">faulthandler.</span></span><span class="sig-name descname"><span class="pre">cancel_dump_traceback_later</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#faulthandler.cancel_dump_traceback_later" title="Link to this definition">¶</a></dt>
  295. <dd><p>Cancel the last call to <a class="reference internal" href="#faulthandler.dump_traceback_later" title="faulthandler.dump_traceback_later"><code class="xref py py-func docutils literal notranslate"><span class="pre">dump_traceback_later()</span></code></a>.</p>
  296. </dd></dl>
  297. </section>
  298. <section id="dumping-the-traceback-on-a-user-signal">
  299. <h2>Dumping the traceback on a user signal<a class="headerlink" href="#dumping-the-traceback-on-a-user-signal" title="Link to this heading">¶</a></h2>
  300. <dl class="py function">
  301. <dt class="sig sig-object py" id="faulthandler.register">
  302. <span class="sig-prename descclassname"><span class="pre">faulthandler.</span></span><span class="sig-name descname"><span class="pre">register</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">signum</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">sys.stderr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">all_threads</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">chain</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#faulthandler.register" title="Link to this definition">¶</a></dt>
  303. <dd><p>Register a user signal: install a handler for the <em>signum</em> signal to dump
  304. the traceback of all threads, or of the current thread if <em>all_threads</em> is
  305. <code class="docutils literal notranslate"><span class="pre">False</span></code>, into <em>file</em>. Call the previous handler if chain is <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  306. <p>The <em>file</em> must be kept open until the signal is unregistered by
  307. <a class="reference internal" href="#faulthandler.unregister" title="faulthandler.unregister"><code class="xref py py-func docutils literal notranslate"><span class="pre">unregister()</span></code></a>: see <a class="reference internal" href="#faulthandler-fd"><span class="std std-ref">issue with file descriptors</span></a>.</p>
  308. <p>Not available on Windows.</p>
  309. <div class="versionchanged">
  310. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added support for passing file descriptor to this function.</p>
  311. </div>
  312. </dd></dl>
  313. <dl class="py function">
  314. <dt class="sig sig-object py" id="faulthandler.unregister">
  315. <span class="sig-prename descclassname"><span class="pre">faulthandler.</span></span><span class="sig-name descname"><span class="pre">unregister</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">signum</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#faulthandler.unregister" title="Link to this definition">¶</a></dt>
  316. <dd><p>Unregister a user signal: uninstall the handler of the <em>signum</em> signal
  317. installed by <a class="reference internal" href="#faulthandler.register" title="faulthandler.register"><code class="xref py py-func docutils literal notranslate"><span class="pre">register()</span></code></a>. Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the signal was registered,
  318. <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
  319. <p>Not available on Windows.</p>
  320. </dd></dl>
  321. </section>
  322. <section id="issue-with-file-descriptors">
  323. <span id="faulthandler-fd"></span><h2>Issue with file descriptors<a class="headerlink" href="#issue-with-file-descriptors" title="Link to this heading">¶</a></h2>
  324. <p><a class="reference internal" href="#faulthandler.enable" title="faulthandler.enable"><code class="xref py py-func docutils literal notranslate"><span class="pre">enable()</span></code></a>, <a class="reference internal" href="#faulthandler.dump_traceback_later" title="faulthandler.dump_traceback_later"><code class="xref py py-func docutils literal notranslate"><span class="pre">dump_traceback_later()</span></code></a> and <a class="reference internal" href="#faulthandler.register" title="faulthandler.register"><code class="xref py py-func docutils literal notranslate"><span class="pre">register()</span></code></a> keep the
  325. file descriptor of their <em>file</em> argument. If the file is closed and its file
  326. descriptor is reused by a new file, or if <a class="reference internal" href="os.html#os.dup2" title="os.dup2"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.dup2()</span></code></a> is used to replace
  327. the file descriptor, the traceback will be written into a different file. Call
  328. these functions again each time that the file is replaced.</p>
  329. </section>
  330. <section id="example">
  331. <h2>Example<a class="headerlink" href="#example" title="Link to this heading">¶</a></h2>
  332. <p>Example of a segmentation fault on Linux with and without enabling the fault
  333. handler:</p>
  334. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-c<span class="w"> </span><span class="s2">&quot;import ctypes; ctypes.string_at(0)&quot;</span>
  335. <span class="go">Segmentation fault</span>
  336. <span class="gp">$ </span>python<span class="w"> </span>-q<span class="w"> </span>-X<span class="w"> </span>faulthandler
  337. <span class="go">&gt;&gt;&gt; import ctypes</span>
  338. <span class="go">&gt;&gt;&gt; ctypes.string_at(0)</span>
  339. <span class="go">Fatal Python error: Segmentation fault</span>
  340. <span class="go">Current thread 0x00007fb899f39700 (most recent call first):</span>
  341. <span class="go"> File &quot;/home/python/cpython/Lib/ctypes/__init__.py&quot;, line 486 in string_at</span>
  342. <span class="go"> File &quot;&lt;stdin&gt;&quot;, line 1 in &lt;module&gt;</span>
  343. <span class="go">Segmentation fault</span>
  344. </pre></div>
  345. </div>
  346. </section>
  347. </section>
  348. <div class="clearer"></div>
  349. </div>
  350. </div>
  351. </div>
  352. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  353. <div class="sphinxsidebarwrapper">
  354. <div>
  355. <h3><a href="../contents.html">Table of Contents</a></h3>
  356. <ul>
  357. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code> — Dump the Python traceback</a><ul>
  358. <li><a class="reference internal" href="#dumping-the-traceback">Dumping the traceback</a></li>
  359. <li><a class="reference internal" href="#fault-handler-state">Fault handler state</a></li>
  360. <li><a class="reference internal" href="#dumping-the-tracebacks-after-a-timeout">Dumping the tracebacks after a timeout</a></li>
  361. <li><a class="reference internal" href="#dumping-the-traceback-on-a-user-signal">Dumping the traceback on a user signal</a></li>
  362. <li><a class="reference internal" href="#issue-with-file-descriptors">Issue with file descriptors</a></li>
  363. <li><a class="reference internal" href="#example">Example</a></li>
  364. </ul>
  365. </li>
  366. </ul>
  367. </div>
  368. <div>
  369. <h4>Previous topic</h4>
  370. <p class="topless"><a href="bdb.html"
  371. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">bdb</span></code> — Debugger framework</a></p>
  372. </div>
  373. <div>
  374. <h4>Next topic</h4>
  375. <p class="topless"><a href="pdb.html"
  376. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pdb</span></code> — The Python Debugger</a></p>
  377. </div>
  378. <div role="note" aria-label="source link">
  379. <h3>This Page</h3>
  380. <ul class="this-page-menu">
  381. <li><a href="../bugs.html">Report a Bug</a></li>
  382. <li>
  383. <a href="https://github.com/python/cpython/blob/main/Doc/library/faulthandler.rst"
  384. rel="nofollow">Show Source
  385. </a>
  386. </li>
  387. </ul>
  388. </div>
  389. </div>
  390. <div id="sidebarbutton" title="Collapse sidebar">
  391. <span>«</span>
  392. </div>
  393. </div>
  394. <div class="clearer"></div>
  395. </div>
  396. <div class="related" role="navigation" aria-label="related navigation">
  397. <h3>Navigation</h3>
  398. <ul>
  399. <li class="right" style="margin-right: 10px">
  400. <a href="../genindex.html" title="General Index"
  401. >index</a></li>
  402. <li class="right" >
  403. <a href="../py-modindex.html" title="Python Module Index"
  404. >modules</a> |</li>
  405. <li class="right" >
  406. <a href="pdb.html" title="pdb — The Python Debugger"
  407. >next</a> |</li>
  408. <li class="right" >
  409. <a href="bdb.html" title="bdb — Debugger framework"
  410. >previous</a> |</li>
  411. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  412. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  413. <li class="switchers">
  414. <div class="language_switcher_placeholder"></div>
  415. <div class="version_switcher_placeholder"></div>
  416. </li>
  417. <li>
  418. </li>
  419. <li id="cpython-language-and-version">
  420. <a href="../index.html">3.12.3 Documentation</a> &#187;
  421. </li>
  422. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  423. <li class="nav-item nav-item-2"><a href="debug.html" >Debugging and Profiling</a> &#187;</li>
  424. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code> — Dump the Python traceback</a></li>
  425. <li class="right">
  426. <div class="inline-search" role="search">
  427. <form class="inline-search" action="../search.html" method="get">
  428. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  429. <input type="submit" value="Go" />
  430. </form>
  431. </div>
  432. |
  433. </li>
  434. <li class="right">
  435. <label class="theme-selector-label">
  436. Theme
  437. <select class="theme-selector" oninput="activateTheme(this.value)">
  438. <option value="auto" selected>Auto</option>
  439. <option value="light">Light</option>
  440. <option value="dark">Dark</option>
  441. </select>
  442. </label> |</li>
  443. </ul>
  444. </div>
  445. <div class="footer">
  446. &copy;
  447. <a href="../copyright.html">
  448. Copyright
  449. </a>
  450. 2001-2024, Python Software Foundation.
  451. <br />
  452. This page is licensed under the Python Software Foundation License Version 2.
  453. <br />
  454. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  455. <br />
  456. See <a href="/license.html">History and License</a> for more information.<br />
  457. <br />
  458. The Python Software Foundation is a non-profit corporation.
  459. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  460. <br />
  461. <br />
  462. Last updated on Apr 09, 2024 (13:47 UTC).
  463. <a href="/bugs.html">Found a bug</a>?
  464. <br />
  465. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  466. </div>
  467. </body>
  468. </html>
上海开阖软件有限公司 沪ICP备12045867号-1