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

generic stack trace overriding mechanism #12922

Merged
merged 3 commits into from
Dec 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
3 changes: 3 additions & 0 deletions compiler/nim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ when defined(profiler) or defined(memProfiler):
{.hint: "Profiling support is turned on!".}
import nimprof

when defined(libbacktrace):
Copy link
Member

@timotheecour timotheecour Dec 18, 2019

Choose a reason for hiding this comment

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

IMO every new defined symbol in nim repository sources should be prefixed with nim => nimlibbacktrace; this prevents clashes with user defined symbols (but what'd be even better is supporting namespace, eg defined(nim.libbacktrace))

import libbacktrace

proc prependCurDir(f: AbsoluteFile): AbsoluteFile =
when defined(unix):
if os.isAbsolute(f.string): result = f
Expand Down
18 changes: 16 additions & 2 deletions lib/system/excpt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,17 @@ proc closureIterSetupExc(e: ref Exception) {.compilerproc, inline.} =
const
nativeStackTraceSupported* = (defined(macosx) or defined(linux)) and
not NimStackTrace
hasSomeStackTrace = NimStackTrace or
defined(nativeStackTrace) and nativeStackTraceSupported
hasSomeStackTrace = NimStackTrace or defined(libbacktrace) or
(defined(nativeStackTrace) and nativeStackTraceSupported)

when defined(libbacktrace):
# This is a Nim procedure, but we can't import the "libbacktrace" module in
# here, so we {.exportc.} it in there and {.importc.} it in here. We rely on
# "libbacktrace" being imported somewhere else, or this will cause a linker error.
proc getBacktrace*(): string {.importc.}
Copy link
Member

Choose a reason for hiding this comment

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

exportc names are global so, again, should be prefixed to prevent potential clashes with user exportc code (including C code in this case!) => nimGetBacktrace()


proc auxWriteStackTraceWithLibbacktrace(s: var string) =
add(s, getBacktrace())

when defined(nativeStacktrace) and nativeStackTraceSupported:
type
Expand Down Expand Up @@ -295,6 +304,9 @@ when hasSomeStackTrace:
else:
add(s, "Traceback (most recent call last)\n")
auxWriteStackTrace(framePtr, s)
elif defined(libbacktrace):
add(s, "Traceback (most recent call last)\n")
auxWriteStackTraceWithLibbacktrace(s)
elif defined(nativeStackTrace) and nativeStackTraceSupported:
add(s, "Traceback from system (most recent call last)\n")
auxWriteStackTraceWithBacktrace(s)
Expand All @@ -313,6 +325,8 @@ when hasSomeStackTrace:
result = false
else:
result = true
elif defined(libbacktrace):
result = true
elif defined(nativeStackTrace) and nativeStackTraceSupported:
result = true
else:
Expand Down