Objective-C - 命令行 (clang) - 打印堆栈跟踪

发布于 2024-12-15 13:48:08 字数 724 浏览 0 评论 0原文

我想打印

我正在使用 clang(自动引用计数)从命令行编译的

Objective-C 程序的堆栈跟踪我想知道以下内容:

  1. 编译时是否必须添加任何参数?
  2. 我是否需要添加任何代码来启动跟踪并打印跟踪?

下面给出的是一个示例程序,我必须为其打印堆栈跟踪:

#import<Foundation/Foundation.h>

@interface Car : NSObject
@property (weak) NSNumber* doors;
@end

@implementation Car 
@synthesize doors;
@end

int main()
{
    system("clear");

    Car *car1 = [[Car alloc] init];

    NSNumber *d1 = [[NSNumber alloc] initWithInteger: 4]; 

    //I want to start printing the stack trace from this point on
    car1.doors = d1;

    printf("---- end\n");

    return(0);
}

用于编译的命令:

clang -fobjc-arc test.m -framework Foundation -o test

I would like to print the stack trace for my Objective-C program

I am compiling from the command line using clang (Automatic reference counting)

I would like to know the following:

  1. Do I have to add any parameters while compiling ?
  2. Do I have to add any code to start the trace and print the trace ?

Given below is a sample program for which I have to print the stack trace:

#import<Foundation/Foundation.h>

@interface Car : NSObject
@property (weak) NSNumber* doors;
@end

@implementation Car 
@synthesize doors;
@end

int main()
{
    system("clear");

    Car *car1 = [[Car alloc] init];

    NSNumber *d1 = [[NSNumber alloc] initWithInteger: 4]; 

    //I want to start printing the stack trace from this point on
    car1.doors = d1;

    printf("---- end\n");

    return(0);
}

Command used to compile:

clang -fobjc-arc test.m -framework Foundation -o test

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-12-22 13:48:08
#include <execinfo.h>

void printStackTrace() {
    void *returnAddresses[500];
    int depth = backtrace(returnAddresses, sizeof returnAddresses / sizeof *returnAddresses);
    printf("stack depth = %d\n", depth);
    char **symbols = backtrace_symbols(returnAddresses, depth);
    for (int i = 0; i < depth; ++i) {
        printf("%s\n", symbols[i]);
    }
    free(symbols);
}
#include <execinfo.h>

void printStackTrace() {
    void *returnAddresses[500];
    int depth = backtrace(returnAddresses, sizeof returnAddresses / sizeof *returnAddresses);
    printf("stack depth = %d\n", depth);
    char **symbols = backtrace_symbols(returnAddresses, depth);
    for (int i = 0; i < depth; ++i) {
        printf("%s\n", symbols[i]);
    }
    free(symbols);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文