nodejs / nodejs/node

Low cost stacktrace walking

Đang mở
#64,337 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

feature request v8 module
Ngôn ngữ chính
JavaScript
Star
122k
Fork
37.3k
Merge trung bình
4 ngày 2 giờ
Pull request đã merge (30 ngày)
283

Mô tả

What is the problem this feature will solve?

Public APIs that allow for stack collection/walking are quite heavy, doing memory allocation and symbol resolution in the background.
All the necessary features are already available - but exported as local symbols, making theme very hard to use even from native code.

What is the feature you are proposing to solve the problem?

Below is example code, that used to work fine in node 20 and 22, but will not work on 24+ because the necessary symbols are not global anymore (so in a way it is regression) . I do not mind that the private API are not stable and require recompilation specific for v8. I do not need new public API - it would be nice of course, but I can easily maintain multiple version of my code. Having symbols local though, makes the project extremely complex:

struct FrameId {
    uint32_t script_id;
    uint32_t start_position;
    uint32_t end_position;
    uint32_t filler;
    v8::internal::Tagged<v8::internal::SharedFunctionInfo> sfi;
};

size_t get_stack_frame_ids(v8::Isolate* isolate, FrameId* buffer, size_t max_len) {
    v8::internal::JavaScriptStackFrameIterator it(reinterpret_cast<v8::internal::Isolate*>(isolate));
    size_t count = 0;
    while (!it.done() && count < max_len) {
        v8::internal::JavaScriptFrame* frame = it.frame();
        v8::internal::Tagged<v8::internal::JSFunction> function = frame->function();
        v8::internal::Tagged<v8::internal::SharedFunctionInfo> sfi = function->shared();
        v8::internal::Tagged<v8::internal::Script> script = v8::internal::Cast<v8::internal::Script>(sfi->script());

        buffer[count].script_id = script->id();
        buffer[count].start_position = sfi->StartPosition();
        buffer[count].end_position = sfi->EndPosition();
        buffer[count].sfi = sfi;

        count++;
        it.Advance();
    }
    return count;
}

size_t describe_frame(v8::Isolate* isolate, FrameId* frame_id, char* buffer, size_t buffer_size) {
    // Fetch function name and script name
    std::unique_ptr<char[]> name = frame_id->sfi->DebugNameCStr();
    size_t offset = 0;
    size_t i=0;
    while(offset < buffer_size - 3 && name[i] != '\0') {
        buffer[offset++] = name[i++];
    }
    buffer[offset++] = '(';
    v8::internal::Tagged<v8::internal::Script> script = v8::internal::Cast<v8::internal::Script>(frame_id->sfi->script());
    v8::internal::Tagged<v8::internal::Object> raw_name = script->GetNameOrSourceURL();
    if (IsString(raw_name)) {
        name = v8::internal::Cast<v8::internal::String>(raw_name)->ToCString();
        i=0;
        while(offset < buffer_size - 2 && name[i] != '\0') {
            buffer[offset++] = name[i++];
        }
    } else {
        if(offset < buffer_size - 11) {
            memcpy(buffer+offset, "<unknown>", 9);
            offset += 9;
        }
    }
    buffer[offset++] = ':';

    // Format the final description into the buffer
    offset += snprintf(buffer+offset, buffer_size - offset, "%d:%d)", frame_id->start_position, frame_id->end_position);

    // If buffer was too small, null-terminate it safely
    if (offset >= buffer_size) {
        buffer[buffer_size - 1] = '\0'; // Null-terminate the buffer
        offset = buffer_size - 1; // Set offset to the last position
    }

    return offset;
}
What alternatives have you considered?

There do not seem to be alternative for "allocation free" stack walking.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách lần theo các điểm vào nội bộ của V8 được示示 trong ví dụ, đặc biệt là JavaScriptStackFrameIterator, JavaScriptFrame và SharedFunctionInfo, rồi so sánh khả năng hiển thị các symbol của chúng giữa Node 22 và Node 24. Sau đó, xác định cấu hình build hoặc export kiểm soát các symbol này. Hoàn thành khi việc duyệt stack native không cấp phát vẫn có thể sử dụng mà không cần một public API mới.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
cpp, nodejs
Lĩnh vực
backend
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
38/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.