Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only show stack trace in debug mode #1860

Merged
merged 43 commits into from
Dec 8, 2023
Merged
Changes from 32 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
1bb0afa
Only show stack trace in dev mode
aaronatp Nov 18, 2023
1680d2f
Rewrote the decision logic in `last_resort_exception_handler` to depe…
aaronatp Nov 19, 2023
29da6f3
Replacement for default sys.excepthook
aaronatp Dec 4, 2023
8fd1e12
Merge branch 'master' into stack-trace
aaronatp Dec 4, 2023
2c2cce4
Removed unused imports
aaronatp Dec 4, 2023
ea5ca21
Fix formatting errors raised by lint with black
aaronatp Dec 4, 2023
1d65eea
Fix formatting error raised by lint with black
aaronatp Dec 4, 2023
05f3114
Fix indentation error raised by lint with black
aaronatp Dec 4, 2023
ad660c5
Fixed implicit concatenation error for flake8
aaronatp Dec 4, 2023
8006ee6
Updated spelling error in sys.excepthook module name
aaronatp Dec 4, 2023
a3e09ae
Improve docstring and syntax
aaronatp Dec 4, 2023
579da77
Update type annotations
aaronatp Dec 4, 2023
a7969d5
Update imports and create return constant
aaronatp Dec 4, 2023
e1a73b8
Update presentation of function's parameters
aaronatp Dec 4, 2023
b1846cc
Update imports and function statement
aaronatp Dec 4, 2023
f81a8c9
Reorder imports from "typing" module
aaronatp Dec 4, 2023
cb2fa8c
Move function parameters to multiple lines
aaronatp Dec 4, 2023
385a298
Add comma after function parameter
aaronatp Dec 4, 2023
345b2db
Reorder imports from "typing" module and improve function statement
aaronatp Dec 4, 2023
4f89441
Remove typing.Union import
aaronatp Dec 4, 2023
edba966
Making type annotations of function parameters compatible with sys.ex…
aaronatp Dec 4, 2023
325b9fc
Update parameter annotation to use typing.Type
aaronatp Dec 5, 2023
8cd428c
Fix function error in how function is invoked
aaronatp Dec 5, 2023
3a91963
Reorder imports from "typing" module
aaronatp Dec 5, 2023
26304f5
Update args of function invocation
aaronatp Dec 5, 2023
f6ad042
Delete nonlocal variable section from function, implement functools.p…
aaronatp Dec 5, 2023
01cb484
Fix imports, fix functools.partial syntax
aaronatp Dec 5, 2023
4263069
Place functools.partial import in different place
aaronatp Dec 5, 2023
a7ee43a
Restructure and rename custom exception handler
aaronatp Dec 8, 2023
32720be
Update exctype type annotation
aaronatp Dec 8, 2023
02eacba
Update typing module imports order
aaronatp Dec 8, 2023
7ba8db1
Update function definition format
aaronatp Dec 8, 2023
f0c29b4
Update function definition and docstring
aaronatp Dec 8, 2023
2e358d9
Disable mypy warning
aaronatp Dec 8, 2023
34b0ec6
Change line where we disable mypy warning
aaronatp Dec 8, 2023
525908d
Edited method for disabling mypy warning
aaronatp Dec 8, 2023
a0e1763
Update mypy error disable information
aaronatp Dec 8, 2023
d12bcd3
Update type ignore for mypy error disabling
aaronatp Dec 8, 2023
cb866d6
Improve mypy type ignore
aaronatp Dec 8, 2023
32a7360
Update statement to disable mypy error
aaronatp Dec 8, 2023
172eb0d
Update statement for disabling mypy error
aaronatp Dec 8, 2023
7ac646b
Update custom exception handler to handle KeyboardInterrupts
aaronatp Dec 8, 2023
495f1e4
Make function compliant with black test
aaronatp Dec 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion capa/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import datetime
import textwrap
import contextlib
from typing import Any, Set, Dict, List, Callable, Optional
from types import TracebackType
from typing import Any, Set, Dict, List, Type, Union, Callable, Optional
from pathlib import Path

import halo
Expand Down Expand Up @@ -112,6 +113,7 @@
E_MISSING_CAPE_STATIC_ANALYSIS = 21
E_MISSING_CAPE_DYNAMIC_ANALYSIS = 22
E_EMPTY_REPORT = 23
E_UNHANDLED = 24
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
E_UNHANDLED = 24


logger = logging.getLogger("capa")

Expand Down Expand Up @@ -978,6 +980,26 @@ def handle_common_args(args):
args.signatures = sigs_path


def simple_message_exception_handler(
exctype: Union[type[BaseException], Type[BaseException]],
value: BaseException,
traceback: TracebackType,
):
"""
if not in debug mode, prints helpful message on unhandled exceptions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not in debug mode, prints helpful message on unhandled exceptions
prints friendly message on unhandled exceptions to regular users, debug mode shows regular stack trace


args:
# TODO(aaronatp): Once capa drops support for Python 3.8, move this to the type annotation in the function parameters
exctype (type[BaseException]): exception class
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this still accurate or can it be removed?

"""

print(
f"Unexpected exception raised: {exctype}. Please run capa in debug mode (-d/--debug) "
+ "to see the stack trace. Please also report your issue on the capa GitHub page so we "
+ "can improve the code! (https://github.com/mandiant/capa/issues)"
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(
f"Unexpected exception raised: {exctype}. Please run capa in debug mode (-d/--debug) "
+ "to see the stack trace. Please also report your issue on the capa GitHub page so we "
+ "can improve the code! (https://github.com/mandiant/capa/issues)"
)
if exctype is KeyboardInterrupt:
print("KeyboardInterrupt detected, program terminated")
else:
print(
f"Unexpected exception raised: {exctype}. Please run capa in debug mode (-d/--debug) "
+ "to see the stack trace. Please also report your issue on the capa GitHub page so we "
+ "can improve the code: https://github.com/mandiant/capa/issues"
)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think of this to also handle CTRL-C (and not say unhandled exception there)



def main(argv: Optional[List[str]] = None):
if sys.version_info < (3, 8):
raise UnsupportedRuntimeError("This version of capa can only be used with Python 3.8+")
Expand Down Expand Up @@ -1020,6 +1042,8 @@ def main(argv: Optional[List[str]] = None):
install_common_args(parser, {"sample", "format", "backend", "os", "signatures", "rules", "tag"})
parser.add_argument("-j", "--json", action="store_true", help="emit JSON instead of text")
args = parser.parse_args(args=argv)
if not args.debug:
sys.excepthook = simple_message_exception_handler
ret = handle_common_args(args)
if ret is not None and ret != 0:
return ret
Expand Down
Loading