Swift 如何像 Python 那样 崩溃了输出 backtrace 或者至少是哪行出错了。。。 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chopin1998519
V2EX    Swift

Swift 如何像 Python 那样 崩溃了输出 backtrace 或者至少是哪行出错了。。。

  •  
  •   chopin1998519 2024-05-13 14:25:04 +08:00 2420 次点击
    这是一个创建于 585 天前的主题,其中的信息可能已经有所发展或是发生改变。

    swift 新手

    写了一个 1000 多行的代码, 没有用 xcode 开发, 直接 vscode 撸的,

    运行时 直接 ./main.swift (代码首行 #!env swift ) 或者 swiftc main.swift -o main.bin , 然后 ./main.bin 运行

    现在程序大体稳定, 但是偶尔会崩溃一下, 崩溃时只是提示 crash 和很少信息, 搞不清楚哪里错误。

    也试过 swiftc -g main.swift -o main.bin 然后有 lldb ./main.bin , 再 run, crash 的时候提示 error: No auto repeat.com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x8)
    (lldb) me #0: 0x0000000192db0154 CoreFoundationCFGetTypeID + 100

    就不会了。。。

    有没有可能像 python 那些,crash 了告诉我是哪行因为什么原因错掉的呢??

    谢谢各位大佬!

    4 条回复    2024-05-13 16:23:34 +08:00
    butanediol2d
        1
    butanediol2d  
       2024-05-13 14:50:39 +08:00 via Android
    Swift 5.9 不是有这个了吗?
    https://www.swift.org/blog/swift-5.9-backtraces/
    chopin1998519
        2
    chopin1998519  
    OP
       2024-05-13 15:01:45 +08:00
    @butanediol2d #1 我看到这个文章, 但是实在没搞懂 怎么启动这个?
    chopin1998519
        3
    chopin1998519  
    OP
       2024-05-13 15:14:20 +08:00
    MsgType = text;
    To = "marco-18149700025";
    } msg: success
    Stack dump:
    0. Program arguments: /Library/Developer/CommandLineTools/usr/bin/swift-frontend -frontend -interpret ./main.swift -Xllvm -aarch64-use-tbi -en
    able-objc-interop -sdk /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -color-diagnostics -external-plugin-path /Library/Developer/CommandLineT
    ools/SDKs/MacOSX.sdk/usr/lib/swift/host/plugins#/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/bin/swift-plugin-server -external-plugin-pa
    th /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/lib/swift/host/plugins#/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/bin
    /swift-plugin-server -external-plugin-path /Library/Developer/Developer/usr/lib/swift/host/plugins#/Library/Developer/Developer/usr/bin/swift-plugi
    n-server -external-plugin-path /Library/Developer/Developer/usr/local/lib/swift/host/plugins#/Library/Developer/Developer/usr/bin/swift-plugin-serv
    er -plugin-path /Library/Developer/CommandLineTools/usr/lib/swift/host/plugins -plugin-path /Library/Developer/CommandLineTools/usr/local/lib/swift
    /host/plugins -target-sdk-version 14.4 -module-name main -- pin
    1. Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
    2. Compiling with the current language version
    3. While running user code "./main.swift"
    Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
    0 swift-frontend 0x00000001060eff3c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
    1 swift-frontend 0x00000001060ef0f8 llvm::sys::RunSignalHandlers() + 112
    2 swift-frontend 0x00000001060f0544 SignalHandler(int) + 360
    3 libsystem_platform.dylib 0x0000000192d5f584 _sigtramp + 56
    4 libsystem_platform.dylib 0xffff80010f473dac _sigtramp + 18446603334014027872
    5 libsystem_platform.dylib 0x000000010f473dac _sigtramp + 18446744071502383200
    6 libsystem_platform.dylib 0x000000010f478f28 _sigtramp + 18446744071502404060
    7 libsystem_platform.dylib 0x000000010f47248c _sigtramp + 18446744071502376768
    8 libsystem_platform.dylib 0x000000010f4794d0 _sigtramp + 18446744071502405508
    9 libsystem_platform.dylib 0x000000010f47c154 _sigtramp + 18446744071502416904
    10 libsystem_platform.dylib 0x000000010f47282c _sigtramp + 18446744071502377696
    11 libsystem_platform.dylib 0x000000010f470210 _sigtramp + 18446744071502367940
    12 libsystem_platform.dylib 0x000000010f470010 _sigtramp + 18446744071502367428
    13 swift-frontend 0x0000000100b17e04 llvm::orc::runAsMain(int (*)(int, char**), llvm::ArrayRef<std::__1::basic_string<char, std::__1::cha
    r_traits<char>, std::__1::allocator<char>>>, llvm::Optional<llvm::StringRef>) + 1344
    14 swift-frontend 0x0000000100a4e01c swift::SwiftJIT::runMain(llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, st
    d::__1::allocator<char>>>) + 168
    15 swift-frontend 0x0000000100a5c19c swift::RunImmediately(swift::CompilerInstance&, std::__1::vector<std::__1::basic_string<char, std::_
    _1::char_traits<char>, std::__1::allocator<char>>, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocato
    r<char>>>> const&, swift::IRGenOptions const&, swift::SILOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SIL
    Module>>&&) + 1028
    16 swift-frontend 0x00000001009f3e90 processCommandLineAndRunImmediately(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule,
    std::__1::default_delete<swift::SILModule>>&&, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::FrontendObserver*, int&) + 624
    17 swift-frontend 0x00000001009ee210 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::
    __1::default_delete<swift::SILModule>>, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift
    ::FrontendObserver*) + 1760
    18 swift-frontend 0x00000001009eda18 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 2100
    19 swift-frontend 0x00000001009f1f0c performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 3616
    20 swift-frontend 0x00000001009ef6d0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) +
    4968
    21 swift-frontend 0x000000010097ee8c swift::mainEntry(int, char const**) + 2612
    22 dyld 0x00000001929a60e0 start + 2360

    比如一次 crash, 结果是这样的。。并没有看到出错行号
    chopin1998519
        4
    chopin1998519  
    OP
       2024-05-13 16:23:34 +08:00
    我终于会了。。。。谢谢!
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1163 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 38ms UTC 17:46 PVG 01:46 LAX 09:46 JFK 12:46
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86