implement output event

This commit is contained in:
lightovernight 2025-08-17 19:01:01 +08:00
parent b5ecbda2c5
commit a4c5ea0d9e

View File

@ -206,9 +206,9 @@ const char* c11_dap_handle_request(const char* message) {
int res = py_dict_getitem_by_str(py_request, "command");
if(res == -1) {
py_printexc();
return NULL;
c11__abort("[DEBUGGER ERROR] an error occurred while parsing request");
} else if(res == 0) {
return "cannot find attribute command";
c11__abort("[DEBUGGER ERROR] no command found in request");
}
py_assign(py_command, py_retval());
const char* command = py_tostr(py_command);
@ -216,14 +216,14 @@ const char* c11_dap_handle_request(const char* message) {
res = py_dict_getitem_by_str(py_request, "arguments");
if(res == -1) {
py_printexc();
return NULL;
c11__abort("[DEBUGGER ERROR] an error occurred while parsing request arguments");
}
py_assign(py_arguments, py_retval());
res = py_dict_getitem_by_str(py_request, "seq");
if(res == -1) {
py_printexc();
return NULL;
c11__abort("[DEBUGGER ERROR] an error occurred while parsing request sequence number");
}
int request_seq = (res == 1) ? py_toint(py_retval()) : 0;
@ -254,25 +254,47 @@ const char* c11_dap_handle_request(const char* message) {
}
void c11_dap_send_event(const char* event_name, const char* body_json) {
char json[256];
int json_len = snprintf(json,
sizeof(json),
"{\"seq\":%d,\"type\":\"event\",\"event\":\"%s\",\"body\":%s}",
server.dap_next_seq++,
event_name,
body_json);
c11_sbuf buffer;
char header[64];
c11_sbuf__ctor(&buffer);
pk_sprintf(&buffer,
"{\"seq\":%d,\"type\":\"event\",\"event\":\"%s\",\"body\":%s}",
server.dap_next_seq++,
event_name,
body_json);
c11_string* json = c11_sbuf__submit(&buffer);
int json_len = json->size;
int header_len = snprintf(header, sizeof(header), "Content-Length: %d\r\n\r\n", json_len);
// printf("[DEBUGGER INFO] send event %s\n", json);
c11_socket_send(server.toclient, header, header_len);
c11_socket_send(server.toclient, json, json_len);
c11_socket_send(server.toclient, json->data, json_len);
c11_string__delete(json);
}
void c11_dap_send_output_event(const char* category, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
c11_sbuf output;
c11_sbuf__ctor(&output);
pk_vsprintf(&output, fmt, args);
va_end(args);
c11_sbuf buffer;
c11_string* output_json = c11_sbuf__submit(&output);
c11_sv sv_output = {.data = output_json->data, .size = output_json->size};
c11_sbuf__ctor(&buffer);
pk_sprintf(&buffer, "{\"category\":\"%s\",\"output\":%Q}", category, sv_output);
c11_string* body_json = c11_sbuf__submit(&buffer);
c11_dap_send_event("output", body_json->data);
c11_string__delete(body_json);
c11_string__delete(output_json);
}
void c11_dap_send_stop_event() {
c11_dap_send_event("stopped", "{\"threadId\":1,\"allThreadsStopped\":true}");
}
void c11_dap_send_exited_event(int exitCode) {
char body[64];
snprintf(body, sizeof(body), "{\"exitCode\":%d}", exitCode);
@ -365,12 +387,12 @@ void c11_dap_init_server(const char* hostname, unsigned short port) {
server.isclientready = false;
c11_socket_bind(server.server, hostname, port);
c11_socket_listen(server.server, 0);
// c11_dap_send_output_event("console", "[DEBUGGER INFO] : listen on %s:%hu\n",hostname,port);
printf("[DEBUGGER INFO] : listen on %s:%hu\n", hostname, port);
}
void c11_dap_waitforclient(const char* hostname, unsigned short port) {
server.toclient = c11_socket_accept(server.server, NULL, NULL);
printf("[DEBUGGER INFO] : connected a client\n");
}
inline static void c11_dap_handle_message() {
@ -404,15 +426,15 @@ void c11_dap_configure_debugger() {
return;
}
}
printf("[DEBUGGER INFO] : configure done\n");
// c11_dap_send_output_event("console", "[DEBUGGER INFO] : client configure done\n");
}
void c11_dap_tracefunc(py_Frame* frame, enum py_TraceEvent event) {
py_sys_settrace(NULL, false);
C11_DEBUGGER_STATUS result = c11_debugger_on_trace(frame, event);
if(result == C11_DEBUGGER_EXIT) {
// c11_dap_send_output_event("console", "[DEBUGGER INFO] : program exit\n");
c11_dap_send_exited_event(0);
printf("[DEBUGGER INFO] : program exit\n");
exit(0);
}
if(result != C11_DEBUGGER_SUCCESS) {
@ -448,7 +470,7 @@ void py_debugger_waitforattach(const char* hostname, unsigned short port) {
c11_dap_configure_debugger();
if(!server.isconfiguredone) {
c11_socket_close(server.toclient);
printf("[DEBUGGER INFO] : An clinet is ready\n");
// c11_dap_send_output_event("console", "[DEBUGGER INFO] : An clinet is ready\n");
}
}
c11_socket_set_block(server.toclient, 0);