Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
xaxaxa committed Aug 6, 2012
1 parent e182a48 commit 2b74f8b
Show file tree
Hide file tree
Showing 31 changed files with 1,426 additions and 170 deletions.
73 changes: 49 additions & 24 deletions cplib/headers/cplib.hpp
Expand Up @@ -200,8 +200,8 @@ namespace xaxaxa
{}
};

//uncomment to allow hybrid functions to distinguish between a member
//function pointer with a NULL thisptr and a static function.
//uncomment to allow hybrid functions to distinguish between (a member
//function pointer with a NULL thisptr) and (a static function).
//this adds overhead.
//#define XAXAXA_FUNCTION_ALLOW_NULL_THISPTR

Expand Down Expand Up @@ -372,19 +372,19 @@ namespace xaxaxa
if (obj != NULL) obj->RefCount_inc();
return *this;
}
inline T* operator()()
inline T* operator()() const
{
return obj;
}
inline T& operator*()
inline T& operator*() const
{
return *obj;
}
inline T* operator->()
inline T* operator->() const
{
return obj;
}
inline T* get()
inline T* get() const
{
return obj;
}
Expand All @@ -405,19 +405,19 @@ namespace xaxaxa
{
obj->RefCount_dec();
}
inline T* operator()()
inline T* operator()() const
{
return obj;
}
inline T* operator->()
inline T* operator->() const
{
return obj;
}
inline T& operator*()
inline T& operator*() const
{
return *obj;
}
inline T* get()
inline T* get() const
{
return obj;
}
Expand Down Expand Up @@ -2607,7 +2607,7 @@ namespace xaxaxa

}
};
typedef struct ucontext sig_ucontext_t;


class Util_c
{
Expand Down Expand Up @@ -2663,27 +2663,21 @@ namespace xaxaxa
}
}
#if __x86_64__
typedef struct ucontext sig_ucontext_t;
static void crit_err_hdlr(int sig_num, siginfo_t * info, void * ucontext)
{
sig_ucontext_t * uc = (sig_ucontext_t *) ucontext;

void * caller_address = (void *) uc->uc_mcontext.gregs[REG_RIP]; // x86 specific

std::cerr << "signal " << sig_num << " (" << strsignal(sig_num) << "), address is "
<< info->si_addr << " from " << caller_address << std::endl << std::endl;

void * array[50];
int size = backtrace(array, 50);

array[1] = caller_address;

char ** messages = backtrace_symbols(array, size);

// skip first stack frame (points here)
for (int i = 1; i < size && messages != NULL; ++i)
{
char *mangled_name = 0, *offset_begin = 0, *offset_end = 0;

// find parantheses and +address offset surrounding mangled name
for (char *p = messages[i]; *p; ++p)
{
Expand All @@ -2701,14 +2695,12 @@ namespace xaxaxa
break;
}
}

// if the line could be processed, attempt to demangle the symbol
if (mangled_name && offset_begin && offset_end && mangled_name < offset_begin)
{
*mangled_name++ = '\0';
*offset_begin++ = '\0';
*offset_end++ = '\0';

int status;
char * real_name = abi::__cxa_demangle(mangled_name, 0, 0, &status);

Expand All @@ -2717,7 +2709,6 @@ namespace xaxaxa
{
std::cerr << "[bt]: (" << i << ") " << messages[i] << " : " << real_name
<< "+" << offset_begin << offset_end << std::endl;

}
// otherwise, output the mangled function name
else
Expand All @@ -2734,22 +2725,56 @@ namespace xaxaxa
}
}
std::cerr << std::endl;

free(messages);
exit(EXIT_FAILURE);
}
#else
// This structure mirrors the one found in /usr/include/asm/ucontext.h
typedef struct _sig_ucontext {
unsigned long uc_flags;
struct ucontext *uc_link;
stack_t uc_stack;
struct sigcontext uc_mcontext;
sigset_t uc_sigmask;
} sig_ucontext_t;
static void crit_err_hdlr(int sig_num, siginfo_t * info, void * ucontext) {
sig_ucontext_t * uc = (sig_ucontext_t *)ucontext;

// Get the address at the time the signal was raised from the EIP (x86)
void * caller_address = (void *) uc->uc_mcontext.eip;

std::cerr << "signal " << sig_num
<< " (" << strsignal(sig_num) << "), address is "
<< info->si_addr << " from "
<< caller_address << std::endl;
void * array[50];
int size = backtrace(array, 50);
std::cerr << __FUNCTION__ << " backtrace returned "
<< size << " frames\n\n";
// overwrite sigaction with caller's address
array[1] = caller_address;
char ** messages = backtrace_symbols(array, size);

// skip first stack frame (points here)
for (int i = 1; i < size && messages != NULL; ++i) {
std::cerr << "[bt]: (" << i << ") " << messages[i] << std::endl;
}
std::cerr << std::endl;
free(messages);
exit(EXIT_FAILURE);
}

#endif
void SetHandlers()
{
#if __x86_64__
//#if __x86_64__
struct sigaction sigact;
sigact.sa_sigaction = &Util_c::crit_err_hdlr;
sigact.sa_flags = SA_RESTART | SA_SIGINFO;
sigaction(SIGSEGV, &sigact, (struct sigaction *) NULL);
sigaction(SIGABRT, &sigact, (struct sigaction *) NULL);
sigaction(SIGFPE, &sigact, (struct sigaction *) NULL);
#endif
//#endif
}
};
extern Util_c Util;
Expand Down
Binary file added curl_test1
Binary file not shown.
192 changes: 192 additions & 0 deletions curl_test1.C
@@ -0,0 +1,192 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://curl.haxx.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <sys/poll.h>
#include <curl/curl.h>
#include <event.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <functional>

using std::function;
using namespace std;
event_base* eb;
CURLM *m;
event timer_event;
struct taskInfo
{
int socket;
int act; //from libcurl
CURL* c;
event ev; //libevent struct is embedded within
bool isAdded; //whether this is registered with libevent
};
struct curlTaskInfo
{
CURL *c;
function<void(CURL*, CURLcode)> cb;
//void* userdata;
};
void cb_event(int fd, short events, void *userp);
void setTaskInfo(taskInfo* t, int s, CURL* c, int act)
{ //has the side effect of registering or re-registering the event with libevent
int events =
(act&CURL_POLL_IN?EV_READ:0)|(act&CURL_POLL_OUT?EV_WRITE:0)|EV_PERSIST;
if(t->isAdded)
event_del(&t->ev);
t->socket=s;
t->act=act;
t->c=c;
t->isAdded=true;
event_assign(&t->ev,eb,s,events,cb_event,t);
event_add(&t->ev, NULL);
}
void checkQueue()
{
CURLMsg *msg;
int msgs_left;
while ((msg = curl_multi_info_read(m, &msgs_left))) {
if (msg->msg == CURLMSG_DONE) {
CURL *c=msg->easy_handle;
curlTaskInfo* t=NULL;
curl_easy_getinfo(c, CURLINFO_PRIVATE, &t);
//cout << "t = " << t << endl;
t->cb(c,msg->data.result);
curl_multi_remove_handle(m, c);
curl_easy_cleanup(c);
delete t;
}
}
}
void cb_event(int fd, short events, void *userp)
{ // an event has been received from libevent
int act =
(events & EV_READ ? CURL_CSELECT_IN : 0) |
(events & EV_WRITE ? CURL_CSELECT_OUT : 0);
int num_transfers;
curl_multi_socket_action(m, fd, act, &num_transfers);
checkQueue();
}

/* CURLMOPT_SOCKETFUNCTION */
int cb_sock(CURL *c, curl_socket_t s, int what, void *cbp, void *sockp)
{
//cout << "cb_sock()" << endl;
taskInfo* t=(taskInfo*)sockp;
if(what==CURL_POLL_REMOVE && t!=NULL) {
if(t->isAdded)
event_del(&t->ev);
delete t;
} else {
if(t==NULL) { //add
t=new taskInfo();
t->isAdded=false;
setTaskInfo(t,s,c,what);
curl_multi_assign(m,s,t);
} else { //modify events monitored
setTaskInfo(t,s,c,what);
}
}
return 0;
}
void addCurlTask(CURL* c, const function<void(CURL*,CURLcode)>& cb)
{
curlTaskInfo* t=new curlTaskInfo();
t->c=c;
t->cb=cb;
//t->userdata=userdata;
curl_easy_setopt(c, CURLOPT_PRIVATE, t);
curl_multi_add_handle(m, c);
}
struct transferInfo
{
function<bool(const void* data, int len, int state)> cb;

};
size_t cb_data(void *data, size_t size, size_t nmemb, void *userdata)
{
transferInfo* t=(transferInfo*)userdata;
t->cb(data,size*nmemb,3);
return size;
}
void addTransfer(const char* url,
const function<bool(const void* data, int len, int state)>& cb)
/*-1:failed 1:connected 2:sent 3:recving 4:closed*/
{
CURL* c=curl_easy_init();
curl_easy_setopt(c, CURLOPT_URL, url);
transferInfo* t=new transferInfo();
t->cb=cb;
curl_easy_setopt(c, CURLOPT_WRITEDATA, t);
curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, cb_data);
addCurlTask(c,[t](CURL* c,CURLcode res)
{
t->cb(NULL,0,4);
delete t;
});
}
int cb_curl_timer(CURLM *m, long timeout_ms, void* userdata)
{ /* Update the event timer after curl_multi library calls */
struct timeval timeout;

timeout.tv_sec = timeout_ms/1000;
timeout.tv_usec = (timeout_ms%1000)*1000;
//printf("multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms);
evtimer_add(&timer_event, &timeout);
return 0;
}
void cb_timer(int fd, short kind, void *userp)
{
int num_transfers;
curl_multi_socket_action(m,
CURL_SOCKET_TIMEOUT, 0, &num_transfers);
checkQueue();
}
int main(int argc, char **argv)
{
eb=event_base_new();
m=curl_multi_init();

event_assign(&timer_event,eb,-1,0,cb_timer,NULL);
curl_multi_setopt(m, CURLMOPT_SOCKETFUNCTION, cb_sock);
curl_multi_setopt(m, CURLMOPT_TIMERFUNCTION, cb_curl_timer);

addTransfer("https://graph.facebook.com/4",[](const void* data, int len, int state)
{
//cout << len << endl;
if(data!=NULL && len>0)
write(1,data,len);
return true;
});
//sleep(5);
event_base_dispatch(eb);
curl_multi_cleanup(m);
return 0;
}
2 changes: 1 addition & 1 deletion fbdump/MainWindow.cs
Expand Up @@ -18,7 +18,7 @@ public partial class MainWindow: Gtk.Window
{
public const string graph_base = "https://graph.facebook.com/";
public delegate void object_callback (JToken obj,string graph_path,fbdump_params p);

int expand_depth=1;
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
Expand Down
Binary file modified jackfft/.default.jfft
Binary file not shown.

0 comments on commit 2b74f8b

Please sign in to comment.