]> git.saurik.com Git - cycript.git/commitdiff
Finally got process attach stable.
authorJay Freeman (saurik) <saurik@saurik.com>
Thu, 15 Oct 2009 06:12:21 +0000 (06:12 +0000)
committerJay Freeman (saurik) <saurik@saurik.com>
Thu, 15 Oct 2009 06:12:21 +0000 (06:12 +0000)
Application.mm
Library.mm
cycript.hpp
makefile

index e910f66f6e221beea067f8d1722a6eec1fa434ce..145501200649d9a7dd6a6f9ebba6fb72f457cb96 100644 (file)
 
 #include "Cycript.tab.hh"
 
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <sys/un.h>
+
 static jmp_buf ctrlc_;
 
 static void sigint(int) {
     longjmp(ctrlc_, 1);
 }
 
-static JSStringRef Result_;
-
-void Run(const char *code, FILE *fout) { _pooled
-    JSStringRef script(JSStringCreateWithUTF8CString(code));
-
-    JSContextRef context(CYGetJSContext());
+void Run(int socket, std::string &code, FILE *fout) {
+    CYPool pool;
 
-    JSValueRef exception(NULL);
-    JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception));
-    JSStringRelease(script);
-
-    if (exception != NULL) { error:
-        result = exception;
-        exception = NULL;
+    const char *json;
+    if (socket == -1)
+        json = CYExecute(pool, code.c_str());
+    else {
+        const char *data(code.c_str());
+        size_t size(code.size());
+        CYSendAll(socket, &size, sizeof(size));
+        CYSendAll(socket, data, size);
+        CYRecvAll(socket, &size, sizeof(size));
+        if (size == _not(size_t))
+            json = NULL;
+        else {
+            char *temp(new(pool) char[size + 1]);
+            CYRecvAll(socket, temp, size);
+            temp[size] = '\0';
+            json = temp;
+        }
     }
 
-    if (!JSValueIsUndefined(context, result)) {
-        CYPool pool;
-        const char *json;
-
-        json = CYPoolCYONString(pool, context, result, &exception);
-        if (exception != NULL)
-            goto error;
-
-        CYSetProperty(context, CYGetGlobalObject(context), Result_, result);
-
-        if (fout != NULL) {
-            fputs(json, fout);
-            fputs("\n", fout);
-            fflush(fout);
-        }
+    if (json != NULL && fout != NULL) {
+        fputs(json, fout);
+        fputs("\n", fout);
+        fflush(fout);
     }
 }
 
-static void Console() {
+static void Console(int socket) {
     bool bypass(false);
     bool debug(false);
 
@@ -186,9 +186,13 @@ static void Console() {
             if (driver.source_ == NULL)
                 goto restart;
 
-            std::ostringstream str;
-            driver.source_->Show(str);
-            code = str.str();
+            if (socket != -1)
+                code = command;
+            else {
+                std::ostringstream str;
+                driver.source_->Show(str);
+                code = str.str();
+            }
         }
 
         add_history(command.c_str());
@@ -196,7 +200,7 @@ static void Console() {
         if (debug)
             std::cout << code << std::endl;
 
-        Run(code.c_str(), fout);
+        Run(socket, code, fout);
     }
 
     fputs("\n", fout);
@@ -220,20 +224,61 @@ static void *Map(const char *path, size_t *psize) {
     return base;
 }
 
-int main(int argc, const char *argv[]) {
+int main(int argc, char *argv[]) {
+    pid_t pid(_not(pid_t));
+
+    for (;;) switch (getopt(argc, argv, "p:")) {
+        case -1:
+            goto getopt;
+        case '?':
+            fprintf(stderr, "usage: cycript [-p <pid>] [<script>]\n");
+            return 1;
+
+        case 'p': {
+            size_t size(strlen(optarg));
+            char *end;
+            pid = strtoul(optarg, &end, 0);
+            if (optarg + size != end) {
+                fprintf(stderr, "invalid pid for -p\n");
+                return 1;
+            }
+        } break;
+    } getopt:;
+
     const char *script;
 
-    if (argc == 1)
+    if (optind == argc)
         script = NULL;
     else {
-        CYSetArgs(argc - 1, argv + 1);
-        script = argv[1];
+        // XXX: const_cast?! wtf gcc :(
+        CYSetArgs(argc - optind - 1, const_cast<const char **>(argv + optind + 1));
+        script = argv[optind];
+        if (strcmp(script, "-") == 0)
+            script = NULL;
     }
 
-    Result_ = CYCopyJSString("_");
+    if (script != NULL && pid != _not(pid_t)) {
+        fprintf(stderr, "-p or <script>: choose one\n");
+        return 1;
+    }
+
+    int socket;
+
+    if (pid == _not(pid_t))
+        socket = -1;
+    else {
+        socket = _syscall(::socket(PF_UNIX, SOCK_STREAM, 0));
+
+        struct sockaddr_un address;
+        memset(&address, 0, sizeof(address));
+        address.sun_family = AF_UNIX;
+        sprintf(address.sun_path, "/tmp/.s.cy.%u", pid);
+
+        _syscall(connect(socket, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
+    }
 
-    if (script == NULL || strcmp(script, "-") == 0)
-        Console();
+    if (script == NULL)
+        Console(socket);
     else {
         CYDriver driver(script);
         cy::parser parser(driver);
@@ -262,7 +307,7 @@ int main(int argc, const char *argv[]) {
             driver.source_->Show(str);
             std::string code(str.str());
             std::cout << code << std::endl;
-            Run(code.c_str(), stdout);
+            Run(socket, code, stdout);
         }
     }
 
index a0ab2adab22d1d8255c5c9ea37df06e328467b10..c4bc8e9269f91a3dfe311e853234780db27e94bf 100644 (file)
@@ -58,6 +58,8 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <sys/un.h>
+
 #include <sys/mman.h>
 
 #include <iostream>
 #include <set>
 #include <map>
 
+#include <sstream>
 #include <cmath>
 
 #include "Parser.hpp"
 #include "Cycript.tab.hh"
 
+#include <fcntl.h>
+
+#include <apr-1/apr_thread_proc.h>
+
 #undef _assert
 #undef _trace
 
@@ -111,6 +118,8 @@ static JSClassRef Struct_;
 static JSObjectRef Array_;
 static JSObjectRef Function_;
 
+static JSStringRef Result_;
+
 static JSStringRef length_;
 static JSStringRef message_;
 static JSStringRef name_;
@@ -895,10 +904,6 @@ CYRange DigitRange_    (0x3ff000000000000LLU, 0x000000000000000LLU); // 0-9
 CYRange WordStartRange_(0x000001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$
 CYRange WordEndRange_  (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$0-9
 
-JSGlobalContextRef CYGetJSContext() {
-    return Context_;
-}
-
 #define CYTry \
     @try
 #define CYCatch \
@@ -1861,12 +1866,11 @@ JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char
     // XXX: in case of exceptions this will leak
     ffoData *data(new ffoData(type));
 
-    ffi_closure *closure;
-    _syscall(closure = (ffi_closure *) mmap(
+    ffi_closure *closure((ffi_closure *) _syscall(mmap(
         NULL, sizeof(ffi_closure),
         PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
         -1, 0
-    ));
+    )));
 
     ffi_status status(ffi_prep_closure(closure, &data->cif_, &Closure_, data));
     _assert(status == FFI_OK);
@@ -2244,97 +2248,275 @@ JSObjectRef CYGetGlobalObject(JSContextRef context) {
     return JSContextGetGlobalObject(context);
 }
 
+const char *CYExecute(apr_pool_t *pool, const char *code) { _pooled
+    JSStringRef script(JSStringCreateWithUTF8CString(code));
+
+    JSContextRef context(CYGetJSContext());
+
+    JSValueRef exception(NULL);
+    JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception));
+    JSStringRelease(script);
+
+    if (exception != NULL) { error:
+        result = exception;
+        exception = NULL;
+    }
+
+    if (JSValueIsUndefined(context, result))
+        return NULL;
+
+    const char *json(CYPoolCYONString(pool, context, result, &exception));
+    if (exception != NULL)
+        goto error;
+
+    CYSetProperty(context, CYGetGlobalObject(context), Result_, result);
+    return json;
+}
+
+bool CYRecvAll_(int socket, uint8_t *data, size_t size) {
+    while (size != 0) if (size_t writ = _syscall(recv(socket, data, size, 0))) {
+        data += writ;
+        size -= writ;
+    } else
+        return false;
+    return true;
+}
+
+bool CYSendAll_(int socket, const uint8_t *data, size_t size) {
+    while (size != 0) if (size_t writ = _syscall(send(socket, data, size, 0))) {
+        data += writ;
+        size -= writ;
+    } else
+        return false;
+    return true;
+}
+
+static int Socket_;
+apr_pool_t *Pool_;
+
+struct CYExecute_ {
+    apr_pool_t *pool_;
+    const char * volatile data_;
+};
+
+// XXX: this is "tre lame"
+@interface CYClient_ : NSObject {
+}
+
+- (void) execute:(NSValue *)value;
+
+@end
+
+@implementation CYClient_
+
+- (void) execute:(NSValue *)value {
+    CYExecute_ *execute(reinterpret_cast<CYExecute_ *>([value pointerValue]));
+    NSLog(@"b:%p", execute->data_);
+    NSLog(@"s:%s", execute->data_);
+    execute->data_ = CYExecute(execute->pool_, execute->data_);
+    NSLog(@"a:%p", execute->data_);
+}
+
+@end
+
+struct CYClient :
+    CYData
+{
+    int socket_;
+    apr_thread_t *thread_;
+
+    CYClient(int socket) :
+        socket_(socket)
+    {
+    }
+
+    ~CYClient() {
+        _syscall(close(socket_));
+    }
+
+    void Handle() { _pooled
+        CYClient_ *client = [[[CYClient_ alloc] init] autorelease];
+
+        for (;;) {
+            size_t size;
+            if (!CYRecvAll(socket_, &size, sizeof(size)))
+                return;
+
+            CYPool pool;
+            char *data(new(pool) char[size + 1]);
+            if (!CYRecvAll(socket_, data, size))
+                return;
+            data[size] = '\0';
+
+            CYDriver driver("");
+            cy::parser parser(driver);
+
+            driver.data_ = data;
+            driver.size_ = size;
+
+            const char *json;
+            if (parser.parse() != 0 || !driver.errors_.empty()) {
+                json = NULL;
+                size = _not(size_t);
+            } else {
+                std::ostringstream str;
+                driver.source_->Show(str);
+                std::string code(str.str());
+                CYExecute_ execute = {pool, code.c_str()};
+                [client performSelectorOnMainThread:@selector(execute:) withObject:[NSValue valueWithPointer:&execute] waitUntilDone:YES];
+                json = execute.data_;
+                size = json == NULL ? _not(size_t) : strlen(json);
+            }
+
+            if (!CYSendAll(socket_, &size, sizeof(size)))
+                return;
+            if (json != NULL)
+                if (!CYSendAll(socket_, json, size))
+                    return;
+        }
+    }
+};
+
+static void * APR_THREAD_FUNC OnClient(apr_thread_t *thread, void *data) {
+    CYClient *client(reinterpret_cast<CYClient *>(data));
+    client->Handle();
+    delete client;
+    return NULL;
+}
+
+static void * APR_THREAD_FUNC Cyrver(apr_thread_t *thread, void *data) {
+    for (;;) {
+        int socket(_syscall(accept(Socket_, NULL, NULL)));
+        CYClient *client(new CYClient(socket));
+        apr_threadattr_t *attr;
+        _aprcall(apr_threadattr_create(&attr, Pool_));
+        _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_));
+    }
+
+    return NULL;
+}
+
 MSInitialize { _pooled
-    apr_initialize();
+    _aprcall(apr_initialize());
+    _aprcall(apr_pool_create(&Pool_, NULL));
 
     Bridge_ = [[NSMutableArray arrayWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain];
-
     NSCFBoolean_ = objc_getClass("NSCFBoolean");
 
-    JSClassDefinition definition;
-
-    definition = kJSClassDefinitionEmpty;
-    definition.className = "Pointer";
-    definition.staticFunctions = Pointer_staticFunctions;
-    definition.getProperty = &Pointer_getProperty;
-    definition.setProperty = &Pointer_setProperty;
-    definition.finalize = &CYData::Finalize;
-    Pointer_ = JSClassCreate(&definition);
-
-    definition = kJSClassDefinitionEmpty;
-    definition.className = "Functor";
-    definition.staticFunctions = Functor_staticFunctions;
-    definition.callAsFunction = &Functor_callAsFunction;
-    definition.finalize = &CYData::Finalize;
-    Functor_ = JSClassCreate(&definition);
-
-    definition = kJSClassDefinitionEmpty;
-    definition.className = "Struct";
-    definition.getProperty = &Struct_getProperty;
-    definition.setProperty = &Struct_setProperty;
-    definition.getPropertyNames = &Struct_getPropertyNames;
-    definition.finalize = &CYData::Finalize;
-    Struct_ = JSClassCreate(&definition);
-
-    definition = kJSClassDefinitionEmpty;
-    definition.className = "Selector";
-    definition.staticValues = CYValue_staticValues;
-    //definition.staticValues = Selector_staticValues;
-    definition.staticFunctions = Selector_staticFunctions;
-    definition.callAsFunction = &Selector_callAsFunction;
-    definition.finalize = &CYData::Finalize;
-    Selector_ = JSClassCreate(&definition);
-
-    definition = kJSClassDefinitionEmpty;
-    definition.className = "Instance";
-    definition.staticValues = CYValue_staticValues;
-    definition.staticFunctions = Instance_staticFunctions;
-    definition.getProperty = &Instance_getProperty;
-    definition.setProperty = &Instance_setProperty;
-    definition.deleteProperty = &Instance_deleteProperty;
-    definition.callAsConstructor = &Instance_callAsConstructor;
-    definition.finalize = &CYData::Finalize;
-    Instance_ = JSClassCreate(&definition);
-
-    definition = kJSClassDefinitionEmpty;
-    definition.className = "Runtime";
-    definition.getProperty = &Runtime_getProperty;
-    Runtime_ = JSClassCreate(&definition);
-
-    definition = kJSClassDefinitionEmpty;
-    //definition.getProperty = &Global_getProperty;
-    JSClassRef Global(JSClassCreate(&definition));
-
-    JSGlobalContextRef context(JSGlobalContextCreate(Global));
-    Context_ = context;
-
-    JSObjectRef global(CYGetGlobalObject(context));
-
-    JSObjectSetPrototype(context, global, JSObjectMake(context, Runtime_, NULL));
-    CYSetProperty(context, global, CYJSString("ObjectiveC"), JSObjectMake(context, Runtime_, NULL));
-
-    CYSetProperty(context, global, CYJSString("Functor"), JSObjectMakeConstructor(context, Functor_, &Functor_new));
-    CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new));
-    CYSetProperty(context, global, CYJSString("Selector"), JSObjectMakeConstructor(context, Selector_, &Selector_new));
-
-    MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair));
-
-    CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_));
-    CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend));
-
-    System_ = JSObjectMake(context, NULL, NULL);
-    CYSetProperty(context, global, CYJSString("system"), System_);
-    CYSetProperty(context, System_, CYJSString("args"), CYJSNull(context));
-    //CYSetProperty(context, System_, CYJSString("global"), global);
-
-    CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print));
-
-    length_ = JSStringCreateWithUTF8CString("length");
-    message_ = JSStringCreateWithUTF8CString("message");
-    name_ = JSStringCreateWithUTF8CString("name");
-    toCYON_ = JSStringCreateWithUTF8CString("toCYON");
-    toJSON_ = JSStringCreateWithUTF8CString("toJSON");
-
-    Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array")));
-    Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function")));
+    Socket_ = _syscall(socket(PF_UNIX, SOCK_STREAM, 0));
+
+    struct sockaddr_un address;
+    memset(&address, 0, sizeof(address));
+    address.sun_family = AF_UNIX;
+
+    pid_t pid(getpid());
+    sprintf(address.sun_path, "/tmp/.s.cy.%u", pid);
+
+    try {
+        _syscall(bind(Socket_, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
+        _syscall(listen(Socket_, 0));
+
+        apr_threadattr_t *attr;
+        _aprcall(apr_threadattr_create(&attr, Pool_));
+
+        apr_thread_t *thread;
+        _aprcall(apr_thread_create(&thread, attr, &Cyrver, NULL, Pool_));
+    } catch (...) {
+        NSLog(@"failed to setup Cyrver");
+    }
+}
+
+JSGlobalContextRef CYGetJSContext() {
+    if (Context_ == NULL) {
+        JSClassDefinition definition;
+
+        definition = kJSClassDefinitionEmpty;
+        definition.className = "Pointer";
+        definition.staticFunctions = Pointer_staticFunctions;
+        definition.getProperty = &Pointer_getProperty;
+        definition.setProperty = &Pointer_setProperty;
+        definition.finalize = &CYData::Finalize;
+        Pointer_ = JSClassCreate(&definition);
+
+        definition = kJSClassDefinitionEmpty;
+        definition.className = "Functor";
+        definition.staticFunctions = Functor_staticFunctions;
+        definition.callAsFunction = &Functor_callAsFunction;
+        definition.finalize = &CYData::Finalize;
+        Functor_ = JSClassCreate(&definition);
+
+        definition = kJSClassDefinitionEmpty;
+        definition.className = "Struct";
+        definition.getProperty = &Struct_getProperty;
+        definition.setProperty = &Struct_setProperty;
+        definition.getPropertyNames = &Struct_getPropertyNames;
+        definition.finalize = &CYData::Finalize;
+        Struct_ = JSClassCreate(&definition);
+
+        definition = kJSClassDefinitionEmpty;
+        definition.className = "Selector";
+        definition.staticValues = CYValue_staticValues;
+        //definition.staticValues = Selector_staticValues;
+        definition.staticFunctions = Selector_staticFunctions;
+        definition.callAsFunction = &Selector_callAsFunction;
+        definition.finalize = &CYData::Finalize;
+        Selector_ = JSClassCreate(&definition);
+
+        definition = kJSClassDefinitionEmpty;
+        definition.className = "Instance";
+        definition.staticValues = CYValue_staticValues;
+        definition.staticFunctions = Instance_staticFunctions;
+        definition.getProperty = &Instance_getProperty;
+        definition.setProperty = &Instance_setProperty;
+        definition.deleteProperty = &Instance_deleteProperty;
+        definition.callAsConstructor = &Instance_callAsConstructor;
+        definition.finalize = &CYData::Finalize;
+        Instance_ = JSClassCreate(&definition);
+
+        definition = kJSClassDefinitionEmpty;
+        definition.className = "Runtime";
+        definition.getProperty = &Runtime_getProperty;
+        Runtime_ = JSClassCreate(&definition);
+
+        definition = kJSClassDefinitionEmpty;
+        //definition.getProperty = &Global_getProperty;
+        JSClassRef Global(JSClassCreate(&definition));
+
+        JSGlobalContextRef context(JSGlobalContextCreate(Global));
+        Context_ = context;
+
+        JSObjectRef global(CYGetGlobalObject(context));
+
+        JSObjectSetPrototype(context, global, JSObjectMake(context, Runtime_, NULL));
+        CYSetProperty(context, global, CYJSString("ObjectiveC"), JSObjectMake(context, Runtime_, NULL));
+
+        CYSetProperty(context, global, CYJSString("Functor"), JSObjectMakeConstructor(context, Functor_, &Functor_new));
+        CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new));
+        CYSetProperty(context, global, CYJSString("Selector"), JSObjectMakeConstructor(context, Selector_, &Selector_new));
+
+        MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair));
+
+        CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_));
+        CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend));
+
+        System_ = JSObjectMake(context, NULL, NULL);
+        CYSetProperty(context, global, CYJSString("system"), System_);
+        CYSetProperty(context, System_, CYJSString("args"), CYJSNull(context));
+        //CYSetProperty(context, System_, CYJSString("global"), global);
+
+        CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print));
+
+        Result_ = JSStringCreateWithUTF8CString("_");
+
+        length_ = JSStringCreateWithUTF8CString("length");
+        message_ = JSStringCreateWithUTF8CString("message");
+        name_ = JSStringCreateWithUTF8CString("name");
+        toCYON_ = JSStringCreateWithUTF8CString("toCYON");
+        toJSON_ = JSStringCreateWithUTF8CString("toJSON");
+
+        Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array")));
+        Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function")));
+    }
+
+    return Context_;
 }
index 0da87f9d6258a2e65f1d20b9cfa31af837791566..0658f5df6cd21618dce657d1decbaa6d2b69a3ef 100644 (file)
 
 #include <sig/types.hpp>
 
+bool CYRecvAll_(int socket, uint8_t *data, size_t size);
+bool CYSendAll_(int socket, const uint8_t *data, size_t size);
+
+template <typename Type_>
+bool CYRecvAll(int socket, Type_ *data, size_t size) {
+    return CYRecvAll_(socket, reinterpret_cast<uint8_t *>(data), size);
+}
+
+template <typename Type_>
+bool CYSendAll(int socket, const Type_ *data, size_t size) {
+    return CYSendAll_(socket, reinterpret_cast<const uint8_t *>(data), size);
+}
+
 JSGlobalContextRef CYGetJSContext();
 JSObjectRef CYGetGlobalObject(JSContextRef context);
+const char *CYExecute(apr_pool_t *pool, const char *code);
 
 void CYSetArgs(int argc, const char *argv[]);
 
index 70b88a987a180f23962bb2f95498782b6514532f..f75fd3a58c5ef269da78022499aa2e03cadd37c3 100644 (file)
--- a/makefile
+++ b/makefile
@@ -88,7 +88,7 @@ package: all
        #cp -a Cycript.dylib package/Library/MobileSubstrate/DynamicLibraries
        mkdir -p package/usr/{bin,lib}
        cp -a libcycript.dylib package/usr/lib
-       #ln -s /usr/lib/libcycript.dylib package/Library/MobileSubstrate/DynamicLibraries/Cycript.dylib
+       ln -s /usr/lib/libcycript.dylib package/Library/MobileSubstrate/DynamicLibraries/Cycript.dylib
        cp -a cycript package/usr/bin
        cp -a libcycript.plist package/usr/lib
        dpkg-deb -b package $(deb)