Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 77c28f2
Showing
40 changed files
with
1,489 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
all: | ||
gcc -g main.c -o main -finstrument-functions -I ../lib -L ../lib -lcdefer | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Makefile for cdefer | ||
|
||
all: | ||
gcc -g -fPIC -shared -o libcdefer.so funcexit.c defer.c | ||
cp libcdefer.so ../examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Tony Bai <bigwhite.cn@gmail.com> | ||
|
||
Other Contributors: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# | ||
# ChangeLog for py-simulators | ||
# | ||
2013-01-22 Tony Bai <bigwhite.cn@gmail.com> | ||
* initial import |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
|
||
|
Oops, something went wrong.