• Stars
    star
    962
  • Rank 46,408 (Top 1.0 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 1 year ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ” ๐Ÿ Like pstack but for Python!


OS Linux PyPI - Python Version PyPI - Implementation PyPI PyPI - Downloads Tests Code Style

PyStack

Print the stack trace of a running Python process, or of a Python core dump.

PyStack is a tool that uses forbidden magic to let you inspect the stack frames of a running Python process or a Python core dump, helping you quickly and easily learn what it's doing (or what it was doing when it crashed) without having to interpret nasty CPython internals.

What PyStack can do

PyStack has the following amazing features:

  • ๐Ÿ’ป Works with both running processes and core dump files.
  • ๐Ÿงต Shows if each thread currently holds the Python GIL, is waiting to acquire it, or is currently dropping it.
  • ๐Ÿ—‘๏ธ Shows if a thread is running a garbage collection cycle.
  • ๐Ÿ Optionally shows native function calls, as well as Python ones. In this mode, PyStack prints the native stack trace (C/C++/Rust function calls), except that the calls to Python callables are replaced with frames showing the Python code being executed, instead of showing the internal C code the interpreter used to make the call.
  • ๐Ÿ” Automatically demangles symbols shown in the native stack.
  • ๐Ÿ“ˆ Includes calls to inlined functions in the native stack whenever enough debug information is available.
  • ๐Ÿ” Optionally shows the values of local variables and function arguments in Python stack frames.
  • ๐Ÿ”’ Safe to use on running processes. PyStack does not modify any memory or execute any code in a process that is running. It simply attaches just long enough to read some of the process's memory.
  • โšก Optionally, it can perform a Python stack analysis without pausing the process at all. This minimizes impact to the debugged process, at the cost of potentially failing due to data races.
  • ๐Ÿš€ Super fast! It can analyze core files 10x faster than general-purpose tools like GDB.
  • ๐ŸŽฏ Even works with aggressively optimized Python interpreter binaries.
  • ๐Ÿ” Even works with Python interpreters' binaries that do not have symbols or debug information (Python stack only).
  • ๐Ÿ’ฅ Tolerates memory corruption well. Even if the process crashed due to memory corruption, PyStack can usually reconstruct the stack.
  • ๐Ÿ’ผ Self-contained: it does not depend on external tools or programs other than the Python interpreter used to run PyStack itself.

Building from source

If you wish to build PyStack from source, you need the following binary dependencies in your system:

  • libdw
  • libelf

Note that sometimes both libraries are provided together as part of a distribution's elfutils package.

Check your package manager on how to install these dependencies (e.g., apt-get install libdw-dev libelf-dev in Debian-based systems). Note that you may need to tell the compiler where to find the header and library files of the dependencies for the build to succeed. Check your distribution's documentation to determine the location of the header and library files or for more detailed information. When building on Alpine Linux (or any other distribution that doesn't use glibc) you'll need elfutils 0.188 or newer. You may need to build this from source if your distribution's package manager doesn't have it.

Once you have these binary dependencies installed, you can clone the repository and follow the typical build process for Python libraries:

git clone [email protected]:bloomberg/pystack.git pystack
cd pystack
python3 -m venv ../pystack-env/  # just an example, put this wherever you want
source ../pystack-env/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install -e .
python3 -m pip install -r requirements-test.txt -r requirements-extra.txt

This will install PyStack in the virtual environment in development mode (the -e of the last pip install command), and then install the Python libraries needed to test it, lint it, and generate its documentation.

If you plan to contribute back, you should install the pre-commit hooks:

pre-commit install

This will ensure that your contribution passes our linting checks.

Documentation

You can find the full documentation here.

Usage

PyStack uses distinct subcommands for analyzing running processes and core dump files.

usage: pystack [-h] [-v] [--no-color] {remote,core} ...

Get Python stack trace of a remote process

options:
  -h, --help     show this help message and exit
  -v, --verbose
  --no-color     Deactivate colored output

commands:
  {remote,core}  What should be analyzed by PyStack (use <command> --help for a command-specific help section).
    remote       Analyze a remote process given its PID
    core         Analyze a core dump file given its location and the executable

Analyzing running processes

The remote command is used to analyze the status of a running (remote) process. The analysis is always done in a safe and non-intrusive way, as no code is loaded in the memory space of the process under analysis and no memory is modified in the remote process. This makes analysis using PyStack a great option even for those services and applications that are running in environments where the running process must not be impacted in any way (other than being temporarily paused, though --no-block can avoid even that). There are several options available:

usage: pystack remote [-h] [-v] [--no-color] [--no-block] [--native] [--native-all] [--locals] [--exhaustive] [--self] pid

positional arguments:
  pid            The PID of the remote process

options:
  -h, --help     show this help message and exit
  -v, --verbose
  --no-color     Deactivate colored output
  --no-block     do not block the process when inspecting its memory
  --native       Include the native (C) frames in the resulting stack trace
  --native-all   Include native (C) frames from threads not registered with the interpreter (implies --native)
  --locals       Show local variables for each frame in the stack trace
  --exhaustive   Use all possible methods to obtain the Python stack info (may be slow)

To use PyStack, you just need to provide the PID of the process:

$ pystack remote 112
Traceback for thread 112 [] (most recent call last):
    (Python) File "/test.py", line 17, in <module>
        first_func()
    (Python) File "/test.py", line 6, in first_func
        second_func()
    (Python) File "/test.py", line 10, in second_func
        third_func()
    (Python) File "/test.py", line 14, in third_func
        time.sleep(1000)

Analyzing core dumps

The core subcommand is used to analyze the status of a core dump file. Analyzing core files is very similar to analyzing processes but there are some differences, as the core file does not contain the totality of the memory that was valid when the program was live. In most cases, this makes no difference, as PyStack will try to adapt automatically. However, in some cases, you will need to specify extra command line options to help PyStack locate the information it needs. When analyzing cores, there are several options available:

usage: pystack core [-h] [-v] [--no-color] [--native] [--native-all] [--locals] [--exhaustive] [--lib-search-path LIB_SEARCH_PATH | --lib-search-root LIB_SEARCH_ROOT] core [executable]

positional arguments:
  core                  The path to the core file
  executable            (Optional) The path to the executable of the core file

options:
  -h, --help            show this help message and exit
  -v, --verbose
  --no-color            Deactivate colored output
  --native              Include the native (C) frames in the resulting stack trace
  --native-all          Include native (C) frames from threads not registered with the interpreter (implies --native)
  --locals              Show local variables for each frame in the stack trace
  --exhaustive          Use all possible methods to obtain the Python stack info (may be slow)
  --lib-search-path LIB_SEARCH_PATH
                        List of paths to search for shared libraries loaded in the core. Paths must be separated by the ':' character
  --lib-search-root LIB_SEARCH_ROOT
                        Root directory to search recursively for shared libraries loaded into the core.

In most cases, you just need to provide the location of the core to use PyStack with core dump files:

$ pystack core ./the_core_file
Using executable found in the core file: /usr/bin/python3.8

Core file information:
state: t zombie: True niceness: 0
pid: 570 ppid: 1 sid: 1
uid: 0 gid: 0 pgrp: 570
executable: python3.8 arguments: python3.8

The process died due receiving signal SIGSTOP
Traceback for thread 570 [] (most recent call last):
    (Python) File "/test.py", line 19, in <module>
        first_func({1: None}, [1,2,3])
    (Python) File "/test.py", line 7, in first_func
        second_func(x, y)
    (Python) File "/test.py", line 12, in second_func
        third_func(x, y)
    (Python) File "/test.py", line 16, in third_func
        time.sleep(1000)

License

PyStack is Apache-2.0 licensed, as found in the LICENSE file.

Code of Conduct

This project has adopted a Code of Conduct. If you have any concerns about the Code, or behavior that you have experienced in the project, please contact us at [email protected].

Security Policy

If you believe you have identified a security vulnerability in this project, please send an email to the project team at [email protected], detailing the suspected issue and any methods you've found to reproduce it.

Please do NOT open an issue in the GitHub repository, as we'd prefer to keep vulnerability reports private until we've had an opportunity to review and address them.

Contributing

We welcome your contributions to help us improve and extend this project!

Below you will find some basic steps required to be able to contribute to the project. If you have any questions about this process or any other aspect of contributing to a Bloomberg open source project, feel free to send an email to [email protected] and we'll get your questions answered as quickly as we can.

Contribution Licensing

Since this project is distributed under the terms of an open source license, contributions that you make are licensed under the same terms. For us to be able to accept your contributions, we will need explicit confirmation from you that you are able and willing to provide them under these terms, and the mechanism we use to do this is called a Developer's Certificate of Origin (DCO). This is similar to the process used by the Linux kernel, Samba, and many other major open source projects.

To participate under these terms, all that you must do is include a line like the following as the last line of the commit message for each commit in your contribution:

Signed-Off-By: Random J. Developer <[email protected]>

The simplest way to accomplish this is to add -s or --signoff to your git commit command.

You must use your real name (sorry, no pseudonyms, and no anonymous contributions).

Steps

  • Create an Issue, select 'Feature Request', and explain the proposed change.
  • Follow the guidelines in the issue template presented to you.
  • Submit the Issue.
  • Submit a Pull Request and link it to the Issue by including "#" in the Pull Request summary.

More Repositories

1

memray

Memray is a memory profiler for Python
Python
12,679
star
2

blazingmq

A modern high-performance open source message queuing system
C++
2,490
star
3

goldpinger

Debugging tool for Kubernetes which tests and displays connectivity between nodes in the cluster.
JavaScript
2,457
star
4

bde

Basic Development Environment - a set of foundational C++ libraries used at Bloomberg.
C++
1,542
star
5

comdb2

Bloomberg's distributed RDBMS
C
1,311
star
6

xcdiff

A tool which helps you diff xcodeproj files.
Swift
909
star
7

quantum

Powerful multi-threaded coroutine dispatcher and parallel execution engine
C++
567
star
8

ipydatagrid

Fast Datagrid widget for the Jupyter Notebook and JupyterLab
TypeScript
510
star
9

foml

Foundations of Machine Learning
Handlebars
330
star
10

pytest-memray

pytest plugin for easy integration of memray memory profiler
Python
318
star
11

python-github-webhook

A framework for writing webhooks for GitHub, in Python.
Python
276
star
12

chromium.bb

Chromium source code and modifications
267
star
13

koan

A word2vec negative sampling implementation with correct CBOW update.
C++
261
star
14

blpapi-node

Bloomberg Open API module for node.js
C++
243
star
15

chef-bcpc

Bloomberg Clustered Private Cloud distribution
Python
228
star
16

phabricator-tools

Phabricator Tools
Python
221
star
17

scatteract

Project which implements extraction of data from scatter plots
Jupyter Notebook
208
star
18

record-tuple-polyfill

A polyfill for the ECMAScript Record and Tuple proposal.
JavaScript
162
star
19

pasta-sourcemaps

Pretty (and) Accurate Stack Trace Analysis is an extension to the JavaScript source map format that allows for accurate function name decoding.
TypeScript
160
star
20

collectdwin

CollectdWin - a system statistics collection daemon for Windows, inspired by 'collectd'
C#
123
star
21

clangmetatool

A framework for reusing code in Clang tools
C++
119
star
22

kubernetes-cluster-cookbook

Ruby
100
star
23

quant-research

A collection of projects published by Bloomberg's Quantitative Finance Research team.
Jupyter Notebook
98
star
24

blpapi-http

HTTP wrapper for Bloomberg Open API
TypeScript
83
star
25

dataless-model-merging

Code release for Dataless Knowledge Fusion by Merging Weights of Language Models (https://openreview.net/forum?id=FCnohuR6AnM)
Python
74
star
26

amqpprox

An AMQP 0.9.1 proxy server, designed for use in front of an AMQP 0.9.1 compliant message queue broker such as RabbitMQ.
C++
72
star
27

spire-tpm-plugin

Provides agent and server plugins for SPIRE to allow TPM 2-based node attestation.
Go
71
star
28

bde-tools

Tools for developing and building libraries modeled on BDE
Perl
67
star
29

ntf-core

Sockets, timers, resolvers, events, reactors, proactors, and thread pools for asynchronous network programming
C++
67
star
30

repofactor

Tools for refactoring history of git repositories
Perl
63
star
31

chef-bach

Chef recipes for Bloomberg's deployment of Hadoop and related components
Ruby
61
star
32

minilmv2.bb

Our open source implementation of MiniLMv2 (https://aclanthology.org/2021.findings-acl.188)
Python
59
star
33

wsk

A straightforward and maintainable build system from the Bloomberg Graphics team.
JavaScript
58
star
34

git-adventure-game

An adventure game to help people learn Git
Shell
57
star
35

corokafka

C++ Kafka coroutine library using Quantum dispatcher and wrapping CppKafka
C++
50
star
36

attrs-strict

Provides runtime validation of attributes specified in Python 'attr'-based data classes.
Python
50
star
37

cnn-rnf

Convolutional Neural Networks with Recurrent Neural Filters
Python
49
star
38

rmqcpp

A batteries included C++ RabbitMQ Client Library/API.
C++
46
star
39

selekt

A Kotlin and familiar Android SQLite database library that uses encryption.
Kotlin
45
star
40

ppx_string_interpolation

PPX rewriter that enables string interpolation in OCaml
OCaml
44
star
41

bde_verify

Tool used to format, improve and verify code to BDE guidelines
C++
42
star
42

vault-auth-spire

vault-auth-spire is an authentication plugin for Hashicorp Vault which allows logging into Vault using a Spire provided SVID.
Go
41
star
43

spark-flow

Library for organizing batch processing pipelines in Apache Spark
Scala
41
star
44

startup-python-bootcamp

35
star
45

chef-umami

A tool to automatically generate test code for Chef cookbooks and policies.
Ruby
34
star
46

p1160

P1160 Add Test Polymorphic Memory Resource To Standard Library
C++
34
star
47

pycsvw

A tool to read CSV files with CSVW metadata and transform them into other formats.
Python
32
star
48

bde-allocator-benchmarks

A set of benchmarking tools used to quantify the performance of BDE-style polymorphic allocators.
C++
31
star
49

blpapi-hs

Haskell interface to BLPAPI
Haskell
30
star
50

bbit-learning-labs

Learning labs curated by BBIT
Python
28
star
51

rwl-bench

A set of benchmark tools for reader/writer locks.
C++
28
star
52

entsum

Open Source / ENTSUM: A Data Set for Entity-Centric Extractive Summarization
Jupyter Notebook
28
star
53

consul-cluster-cookbook

Wrapper cookbook which installs and configures a Consul cluster.
Ruby
26
star
54

kbir_keybart

Experimental code used in pre-training the KBIR and KeyBART models
Python
26
star
55

presto-accumulo

Presto Accumulo Integration
Java
25
star
56

sgtb

Structured Gradient Tree Boosting
Python
25
star
57

blazingmq-sdk-java

Java SDK for BlazingMQ, a modern high-performance open source message queuing system.
Java
24
star
58

python-comdb2

Python API to Bloomberg's comdb2 database.
Python
23
star
59

jupyterhub-kdcauthenticator

A Kerberos authenticator module for the JupyterHub platform
Python
22
star
60

docket

Tool to make running test suites easier, using docker-compose.
Go
22
star
61

blazingmq-sdk-python

Python SDK for BlazingMQ, a modern high-performance open source message queuing system.
Python
21
star
62

tzcron

A parser of cron-style scheduling expressions.
Python
20
star
63

constant.js

Immutable/Constant Objects for JavaScript
JavaScript
20
star
64

go-testgroup

Helps you organize tests in Go programs into groups.
Go
19
star
65

redis-cookbook

A set of Chef recipes for installing and configuring Redis.
HTML
19
star
66

userchroot

A tool to allow controlled access to 'chroot' functionality by users without root permissions
C
19
star
67

nginx-cookbook

A set of Chef recipes for installing and configuring Nginx.
Ruby
17
star
68

MixCE-acl2023

Implementation of MixCE method described in ACL 2023 paper by Zhang et al.
Python
17
star
69

zookeeper-cookbook

A set of Chef recipes for installing and configuring Apache Zookeeper.
Ruby
17
star
70

mynexttalk

16
star
71

chef-bcs

Bloomberg Cloud Storage Chef application
Ruby
16
star
72

vault-cluster-cookbook

Application cookbook which installs and configures Vault with Consul as a backend.
Ruby
15
star
73

git-adventure-game-builder

A set of tools for building a Git adventure game, to help people learn Git
Shell
15
star
74

emnlp20_depsrl

Research code and scripts used in the paper Semantic Role Labeling as Syntactic Dependency Parsing.
Python
14
star
75

coffeechat

A simple web application for arranging 'chats over coffee'.
TypeScript
12
star
76

k8eraid

A relatively simple, unified method for reporting on Kubernetes resource issues.
Go
12
star
77

hackathon-aws-cluster

HTML
11
star
78

fast-noise-aware-topic-clustering

Research code and scripts used in the Silburt et al. (2021) EMNLP 2021 paper 'FANATIC: FAst Noise-Aware TopIc Clustering'
Python
10
star
79

emnlp21_fewrel

Code to reproduce the results of the paper 'Towards Realistic Few-Shot Relation Extraction' (EMNLP 2021)
Python
10
star
80

mastering-difficult-conversations

Plan It, Say It, Nail It: Mastering Difficult Conversations
10
star
81

wsk-notify

Simple, customizable console notifications.
JavaScript
10
star
82

jenkins-cluster-cookbook

Ruby
9
star
83

decorator-taxonomy

A taxonomy of Python decorator types.
HTML
9
star
84

pytest-pystack

Pytest plugin that runs PyStack on slow or hanging tests.
Python
9
star
85

tdd-labs

Problems and Solutions for Test-Driven-Development training
JavaScript
9
star
86

argument-relation-transformer-acl2022

This repository contains code for our ACL 2022 Findings paper `Efficient Argument Structure Extraction with Transfer Learning and Active Learning`. We implement an argument structure extraction method based on a pre-trained Transformer model.`
Python
9
star
87

sigir2018-kg-contextualization

8
star
88

bloomberg.github.io

Source code for the https://bloomberg.github.io site
HTML
8
star
89

locking_resource-cookbook

Chef cookbook for serializing access to resources
Ruby
7
star
90

datalake-query-ingester

Python
7
star
91

cobbler-cookbook

A Chef cookbook for installing and maintaining Cobbler
Ruby
7
star
92

p2473

Example code for WG21 paper P2473
Perl
6
star
93

collectd-cookbook

Ruby
6
star
94

Catalyst-Authentication-Credential-GSSAPI

A module that provides integration of the Catalyst web application framework with GSSAPI/SPNEGO HTTP authentication.
Perl
6
star
95

bob-bot

Java
5
star
96

.github

Organization-wide community files
5
star
97

jenkins-procguard

Perl
5
star
98

datalake-query-db-consumer

Python
4
star
99

datalake-metrics-db

Python
3
star
100

collectd_plugins-cookbook

Ruby
3
star