]> git.siccegge.de Git - frida/frida.git/blob - src/bindings/Guile.cxx
Add freebsd names for llvm-config
[frida/frida.git] / src / bindings / Guile.cxx
1 #include "Guile.hxx"
2 #include "Config.hxx"
3 #include "core/Settings.hxx"
4
5 namespace {
6 SCM handler (void*, SCM tag, SCM throw_args) {
7 scm_handle_by_message_noexit ((void*)"foo", tag, throw_args);
8 return SCM_BOOL_F;
9 }
10 }
11
12 GuileInterpreter::GuileInterpreter()
13 : logger(log4cxx::Logger::getLogger("bindings.Guile")) {
14
15 scm_init_guile();
16 scm_c_use_module("system repl server");
17
18 geiser = new guile::Geiser(this);
19 geiser->start();
20
21 scm_c_load_extension("libguile-frida-binding",
22 "scm_init_frida_module");
23
24 guile_output_port = scm_open_output_string();
25 guile_error_port = scm_open_output_string();
26 scm_set_current_output_port(guile_output_port);
27 scm_set_current_error_port(guile_error_port);
28 LOG4CXX_INFO(logger, "Initializing GUILE finished");
29 }
30
31 GuileInterpreter::~GuileInterpreter() {
32 geiser->terminate();
33 geiser->wait();
34 }
35
36 int GuileInterpreter::evaluateWithErrorHandling(SCM (*fun)(void *),
37 void* data,
38 std::ostream& stdout,
39 std::ostream& stderr,
40 std::string& result) {
41 SCM result_obj = scm_internal_catch(SCM_BOOL_T,
42 fun,
43 data,
44 handler, NULL);
45
46 SCM result_str = scm_object_to_string(result_obj, SCM_UNDEFINED);
47
48 SCM output = scm_get_output_string(guile_output_port);
49 stdout << scm_to_locale_string(output);
50
51 output = scm_get_output_string(guile_error_port);
52 stderr << scm_to_locale_string(output);
53
54 result = scm_to_locale_string(result_str);
55
56 scm_truncate_file(guile_output_port, scm_from_uint16(0));
57 scm_truncate_file(guile_error_port, scm_from_uint16(0));
58 return 0;
59 }
60
61 int GuileInterpreter::evaluate(const std::string& command,
62 std::ostream& stdout,
63 std::ostream& stderr,
64 std::string& result) {
65
66 return evaluateWithErrorHandling((SCM (*)(void *))scm_c_eval_string,
67 (void*)command.c_str(),
68 stdout, stderr, result);
69
70 }
71
72 int GuileInterpreter::loadFile(const std::string& filename,
73 std::ostream& stdout,
74 std::ostream& stderr,
75 std::string& result) {
76 LOG4CXX_DEBUG(logger, "Loading file \"" << filename << "\"");
77 evaluateWithErrorHandling((SCM (*)(void *))scm_c_primitive_load,
78 (void*)filename.c_str(),
79 stdout, stderr, result);
80 LOG4CXX_DEBUG(logger, "Finished file \"" << filename << "\"");
81 return 0;
82 }
83
84 namespace guile {
85 void Geiser::run() {
86 scm_init_guile();
87
88 QString socketpath = Settings::get()->getRuntimeDirectory()->canonicalPath()
89 + "/frida." + QString::number(QCoreApplication::applicationPid(), 16) + ".geiser.sock";
90
91 SCM scm_socketpath = scm_from_locale_string(socketpath.toStdString().c_str());
92 SCM socket = scm_call_2(scm_c_public_ref("system repl server", "make-unix-domain-server-socket"),
93 scm_from_locale_keyword("path"), scm_socketpath);
94 scm_call_1(scm_c_public_ref("system repl server", "run-server"), socket);
95 }
96 }