Skip to content

Commit

Permalink
migrate to github 2013-04-02
Browse files Browse the repository at this point in the history
  • Loading branch information
bigwhite committed Apr 2, 2013
0 parents commit 77c28f2
Show file tree
Hide file tree
Showing 40 changed files with 1,489 additions and 0 deletions.
17 changes: 17 additions & 0 deletions astyle_scripts/astylerc
@@ -0,0 +1,17 @@
# bigwhite's astyle options file

--indent=spaces=4
--brackets=attach
# --brackets=linux
--indent-switches
--indent-cases
--indent-preprocessor
--indent-col1-comments
--pad-oper
--pad-header
--unpad-paren
--add-brackets
--keep-one-line-statements
--align-pointer=name
--mode=c
--min-conditional-indent=0
3 changes: 3 additions & 0 deletions cdefer/examples/Makefile
@@ -0,0 +1,3 @@
all:
gcc -g main.c -o main -finstrument-functions -I ../lib -L ../lib -lcdefer

27 changes: 27 additions & 0 deletions cdefer/examples/main.c
@@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include "defer.h"

int
bar(int a, char *s)
{
printf("a = [%d], s = [%s]\n", a, s);

}

int
main()
{
FILE *fp = NULL;
fp = fopen("main.c", "r");
if (!fp) return;
defer(fclose, 1, fp);

int *p = malloc(sizeof(*p));
if (!p) return;
defer(free, 1, p);

defer(bar, 2, 13, "hello");
return 0;
}

5 changes: 5 additions & 0 deletions cdefer/lib/Makefile
@@ -0,0 +1,5 @@
# Makefile for cdefer

all:
gcc -g -fPIC -shared -o libcdefer.so funcexit.c defer.c
cp libcdefer.so ../examples
68 changes: 68 additions & 0 deletions cdefer/lib/defer.c
@@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "defer.h"

struct defer_func_ctx ctx_stack[10];
int top_of_stack = 0; /* stack top from 1 to 10 */

void
stack_push(struct defer_func_ctx *ctx)
{
if (top_of_stack >= 10) {
return;
}

ctx_stack[top_of_stack] = *ctx;
top_of_stack++;
}

struct defer_func_ctx*
stack_pop()
{
if (top_of_stack == 0) {
return NULL;
}

top_of_stack--;
return &ctx_stack[top_of_stack];
}

int
stack_top()
{
return top_of_stack;
}

void
defer(defer_func fp, int arg_count, ...)
{
va_list ap;
va_start(ap, arg_count);

struct defer_func_ctx ctx;
memset(&ctx, 0, sizeof(ctx));
ctx.params_count = arg_count;
printf("in defer: params count is [%d]\n", ctx.params_count);

if (arg_count == 0) {
ctx.ctx.zp.df = fp;

} else if (arg_count == 1) {
ctx.ctx.op.df = fp;
ctx.ctx.op.p1 = va_arg(ap, void*);

} else if (arg_count == 2) {
ctx.ctx.tp.df = fp;
ctx.ctx.tp.p1 = va_arg(ap, void*);
ctx.ctx.tp.p2 = va_arg(ap, void*);
ctx.ctx.tp.df(ctx.ctx.tp.p1, ctx.ctx.tp.p2);
}

va_end(ap);
stack_push(&ctx);
printf("defer push function: [%p]\n", fp);
printf("in defer: stack top is: [%d]\n", stack_top());
}

34 changes: 34 additions & 0 deletions cdefer/lib/defer.h
@@ -0,0 +1,34 @@
#ifndef _DEFER_H_
#define _DEFER_H_

typedef void (*defer_func)();

struct zero_params_func_ctx {
defer_func df;
};

struct one_params_func_ctx {
defer_func df;
void *p1;
};

struct two_params_func_ctx {
defer_func df;
void *p1;
void *p2;
};

struct defer_func_ctx {
int params_count;
union {
struct zero_params_func_ctx zp;
struct one_params_func_ctx op;
struct two_params_func_ctx tp;
} ctx;
};

void stack_push(struct defer_func_ctx *ctx);
struct defer_func_ctx* stack_pop();
int stack_top();

#endif
38 changes: 38 additions & 0 deletions cdefer/lib/funcexit.c
@@ -0,0 +1,38 @@
#include <stdio.h>
#include "defer.h"

extern struct defer_func_ctx ctx_stack[10];

__attribute__((no_instrument_function))
void
__cyg_profile_func_enter(void *this_fn, void *call_site)
{
printf("enter func => %p\n", this_fn);
}

__attribute__((no_instrument_function))
void
__cyg_profile_func_exit(void *this_fn, void *call_site)
{
printf("exit func <= %p\n", this_fn);

struct defer_func_ctx *ctx = NULL;
printf("in funcexit: stack top is [%d]\n", stack_top());

while ((ctx = stack_pop()) != NULL) {
printf("params count is [%d]\n", ctx->params_count);
if (ctx->params_count == 0) {
printf("defer pop function: [%p]\n", ctx->ctx.zp.df);
ctx->ctx.zp.df();
} else if (ctx->params_count == 1) {
printf("defer pop function: [%p]\n", ctx->ctx.op.df);
ctx->ctx.op.df(ctx->ctx.op.p1);
} else if (ctx->params_count == 2) {
printf("in exit: df = %p\n", (ctx->ctx).tp.df);
printf("in exit: p1 = %p\n", (ctx->ctx).tp.p1);
printf("in exit: p2 = %p\n", (ctx->ctx).tp.p2);
printf("defer pop function: [%p]\n", ctx->ctx.tp.df);
ctx->ctx.tp.df(ctx->ctx.tp.p1, ctx->ctx.tp.p2);
}
}
}
Binary file added cdefer/lib/libcdefer.so
Binary file not shown.
24 changes: 24 additions & 0 deletions func_trace/Makefile
@@ -0,0 +1,24 @@
#
# Makefile for func_trace share lib
#

OS := $(shell uname -s)

LIBS =

ifeq ($(OS), SunOS)
DEFS = -D_SOLARIS_TRACE
LIBS += -lfunc_trace
else ifeq ($(OS), Linux)
DEFS = -D_LINUX_TRACE
else
$(error $(OS) is not supported!)
endif


all:
gcc $(DEFS) -fPIC -shared -o libfunc_trace.so func_trace.c
gcc -g example.c -o example -finstrument-functions $(LIBS)

clean:
rm -fr example libfunc_trace.so
20 changes: 20 additions & 0 deletions func_trace/example.c
@@ -0,0 +1,20 @@
#include <stdio.h>

static void foo2() {

}

void foo1() {
foo2();
}

void foo(){
chdir("/home/tonybai");
foo1();
}

int main(int argc, const char *argv[])
{
foo();
return 0;
}
72 changes: 72 additions & 0 deletions func_trace/func_trace.c
@@ -0,0 +1,72 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h> /* for PATH_MAX */

static char path[PATH_MAX];

__attribute__((constructor))
static void executable_path_init() {
char buf[PATH_MAX];

memset(buf, 0, sizeof(buf));
memset(path, 0, sizeof(path));

#ifdef _SOLARIS_TRACE
getcwd(buf, PATH_MAX);
sprintf(path, "%s/%s", buf, getexecname());
#elif _LINUX_TRACE
readlink("/proc/self/exe", path, PATH_MAX);
#else
#error "The OS has not been supported!"
#endif
}

__attribute__((no_instrument_function))
void __cyg_profile_func_enter(void *this_fn, void *call_site) {
char buf[PATH_MAX];
char cmd[PATH_MAX];

memset(buf, 0, sizeof(buf));
memset(cmd, 0, sizeof(cmd));

sprintf(cmd, "addr2line %p -e %s -f", this_fn, path);

FILE *ptr = NULL;
memset(buf, 0, sizeof(buf));

if ((ptr = popen(cmd, "r")) != NULL) {
fgets(buf, PATH_MAX, ptr);
buf[strlen(buf) - 1] = '\0';
printf("enter func => %p:%s:", this_fn, buf);
fgets(buf, PATH_MAX, ptr); /* filename and lineno */
printf("%s\n", buf);
}

(void) pclose(ptr);
}

__attribute__((no_instrument_function))
void __cyg_profile_func_exit(void *this_fn, void *call_site) {
char buf[PATH_MAX];
char cmd[PATH_MAX];

memset(buf, 0, sizeof(buf));
memset(cmd, 0, sizeof(cmd));

sprintf(cmd, "addr2line %p -e %s -f", this_fn, path);

FILE *ptr = NULL;
memset(buf, 0, sizeof(buf));

if ((ptr = popen(cmd, "r")) != NULL) {
fgets(buf, PATH_MAX, ptr);
buf[strlen(buf) - 1] = '\0';
printf("exit func <= %p:%s:", this_fn, buf);
fgets(buf, PATH_MAX, ptr); /* filename and lineno */
printf("%s\n", buf);
}

(void) pclose(ptr);
}
4 changes: 4 additions & 0 deletions py-simulators/AUTHORS
@@ -0,0 +1,4 @@
Tony Bai <bigwhite.cn@gmail.com>

Other Contributors:

5 changes: 5 additions & 0 deletions py-simulators/ChangeLog
@@ -0,0 +1,5 @@
#
# ChangeLog for py-simulators
#
2013-01-22 Tony Bai <bigwhite.cn@gmail.com>
* initial import
13 changes: 13 additions & 0 deletions py-simulators/LICENSE
@@ -0,0 +1,13 @@
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


Empty file added py-simulators/README
Empty file.
Empty file added py-simulators/core/__init__.py
Empty file.
Empty file.
27 changes: 27 additions & 0 deletions py-simulators/core/reactor/event.py
@@ -0,0 +1,27 @@

"""event.py
A wrapper of epoll or poll events
"""

import select

try:
from select import epoll
EVENT_IN = select.EPOLLIN
EVENT_PRI = select.EPOLLPRI
EVENT_OUT = select.EPOLLOUT
EVENT_ERR = select.EPOLLERR
EVENT_HUP = select.EPOLLHUP
EVENT_NVAL = 0

except ImportError:

from select import poll
EVENT_IN = select.POLLIN
EVENT_PRI = select.POLLPRI
EVENT_OUT = select.POLLOUT
EVENT_ERR = select.POLLERR
EVENT_HUP = select.POLLHUP
EVENT_NVAL = select.POLLNVAL
23 changes: 23 additions & 0 deletions py-simulators/core/reactor/reactor.py
@@ -0,0 +1,23 @@
#
#
#

"""reactor.py
"""

try:
from select import epoll
EVENT_NOTIFY_FACILITY = 'epoll'
except ImportError:
from select import poll
EVENT_NOTIFY_FACILITY = 'poll'

class Reactor(object):
"""
"""

def __init__(self,


0 comments on commit 77c28f2

Please sign in to comment.