forked from cory/tildefriends
Move /trace and /mem to C.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4724 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
@ -5,6 +5,7 @@
|
||||
#include "mem.h"
|
||||
#include "task.h"
|
||||
#include "tlscontext.js.h"
|
||||
#include "trace.h"
|
||||
#include "util.js.h"
|
||||
|
||||
#include "picohttpparser.h"
|
||||
@ -18,6 +19,8 @@
|
||||
#include <alloca.h>
|
||||
#endif
|
||||
|
||||
#define tf_countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
|
||||
|
||||
static JSClassID _httpd_class_id;
|
||||
static JSClassID _httpd_request_class_id;
|
||||
|
||||
@ -365,6 +368,65 @@ void _httpd_request_finalizer(JSRuntime* runtime, JSValue value)
|
||||
tf_http_request_release(request);
|
||||
}
|
||||
|
||||
static void _httpd_endpoint_trace(tf_http_request_t* request)
|
||||
{
|
||||
if (_httpd_redirect(request))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
tf_task_t* task = request->user_data;
|
||||
tf_trace_t* trace = tf_task_get_trace(task);
|
||||
tf_trace_begin(trace, __func__);
|
||||
char* json = tf_trace_export(trace);
|
||||
const char* headers[] =
|
||||
{
|
||||
"Content-Type", "application/json; charset=utf-8",
|
||||
"Access-Control-Allow-Origin", "*",
|
||||
};
|
||||
tf_http_respond(request, 200, headers, tf_countof(headers) / 2, json, json ? strlen(json) : 0);
|
||||
tf_free(json);
|
||||
tf_trace_end(trace);
|
||||
}
|
||||
|
||||
static void _httpd_endpoint_mem(tf_http_request_t* request)
|
||||
{
|
||||
if (_httpd_redirect(request))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
tf_task_t* task = request->user_data;
|
||||
tf_trace_t* trace = tf_task_get_trace(task);
|
||||
tf_trace_begin(trace, __func__);
|
||||
|
||||
char* response = NULL;
|
||||
size_t length = 0;
|
||||
|
||||
int count = 0;
|
||||
tf_mem_allocation_t* alloc = tf_mem_summarize_allocations(&count);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
const char* stack = tf_util_backtrace_to_string(alloc[i].frames, alloc[i].frames_count);
|
||||
int line = snprintf(NULL, 0, "%zd bytes in %d allocations\n%s\n\n", alloc[i].size, alloc[i].count, stack);
|
||||
response = tf_realloc(response, length + line);
|
||||
snprintf(response + length, line, "%zd bytes in %d allocations\n%s\n\n", alloc[i].size, alloc[i].count, stack);
|
||||
length += line - 1;
|
||||
tf_free((void*)stack);
|
||||
}
|
||||
tf_free(alloc);
|
||||
|
||||
const char* headers[] =
|
||||
{
|
||||
"Content-Type", "text/plain; charset=utf-8",
|
||||
"Access-Control-Allow-Origin", "*",
|
||||
};
|
||||
tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, length);
|
||||
tf_free(response);
|
||||
|
||||
tf_trace_end(trace);
|
||||
}
|
||||
|
||||
void tf_httpd_register(JSContext* context)
|
||||
{
|
||||
JS_NewClassID(&_httpd_class_id);
|
||||
@ -395,6 +457,9 @@ void tf_httpd_register(JSContext* context)
|
||||
tf_http_t* http = tf_http_create(loop);
|
||||
JS_SetOpaque(httpd, http);
|
||||
|
||||
tf_http_add_handler(http, "/trace", _httpd_endpoint_trace, task);
|
||||
tf_http_add_handler(http, "/mem", _httpd_endpoint_mem, task);
|
||||
|
||||
JS_SetPropertyStr(context, httpd, "handlers", JS_NewObject(context));
|
||||
JS_SetPropertyStr(context, httpd, "all", JS_NewCFunction(context, _httpd_all, "all", 2));
|
||||
JS_SetPropertyStr(context, httpd, "registerSocketHandler", JS_NewCFunction(context, _httpd_register_socket_handler, "register_socket_handler", 2));
|
||||
|
Reference in New Issue
Block a user