Skip to content

Commit

Permalink
Ditch that symbol table hack. It's not portable.
Browse files Browse the repository at this point in the history
In particular, Mac OS and probably BSD don't like it.

Closes gh-8.
  • Loading branch information
kr committed May 15, 2009
1 parent 95ff706 commit 5192ce5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 59 deletions.
52 changes: 1 addition & 51 deletions cut.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,56 +224,6 @@ collect(pid_t *pid, collect_fn fn, void *data)
}
}

static void
exec(void *data)
{
int r;

char **info = (char **) data;
r = execvp(info[0], info);
die(5, "Wrong execvp");
}

/* Read the full symbol table (not just the dynamic symbols) and return the
* value of the specified symbol. If an error occurs, print an error message
* and exit.
*
* We cheat by using "nm" to parse the symbol table of the file on disk.
*/
void *
__cut_debug_addr(const char *sym, const char *file, int line)
{
void *val;
FILE *out;
pid_t pid;
int status, r;
char cmd[BUF_SIZE], s[BUF_SIZE];
char *args[] = { "sh", "-c", cmd, 0 };

sprintf(cmd, "nm %s | grep ' %s$'", program, sym);

out = collect(&pid, exec, args);
if (!out) die(1, " %s:%d: collect", file, line);

pid = waitpid(pid, &status, 0);
if (pid < 1) die(1, " %s:%d: wait", file, line);

rewind(out);
r = fread(s, 1, BUF_SIZE - 1, out);
if (!r) printf(" %s:%d: no symbol: %s\n", file, line, sym), exit(1);

s[r] = 0;

errno = 0;
val = (void *) strtoul(s, 0, 16);
if (errno) die(1, " %s:%d: strtoul on ``%s''", file, line, s);
if (((size_t) val) < 100) {
die(1, " %s:%d: strtoul on ``%s''", file, line, s);
}

return val;
}

static void
run_in_child(void *data)
{
Expand All @@ -294,7 +244,7 @@ void
__cut_run(char *group_name, cut_fn bringup, cut_fn takedown, char *test_name,
cut_fn test, char *filename, int lineno)
{
pid_t pid;
pid_t pid = -1;
int status, r;
FILE *out;
test_output to;
Expand Down
3 changes: 0 additions & 3 deletions cut.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ void cut_exit(void);
__FILE__, \
__LINE__);

#define ADDR(S) (__cut_debug_addr(#S, __FILE__, __LINE__))

#define ASSERT(X,msg) __cut_assert(__FILE__,__LINE__,msg,#X,X)

#define STATIC_ASSERT(X) extern bool __static_ASSERT_at_line_##__LINE__##__[ (0!=(X))*2-1 ];
Expand All @@ -55,7 +53,6 @@ void cut_exit(void);

void __cut_run(char *, cut_fn, cut_fn, char *, cut_fn, char *, int);
void __cut_assert( char *, int, char *, char *, int );
void *__cut_debug_addr(const char *, const char *, int);

#endif

7 changes: 7 additions & 0 deletions job.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ total_jobs()
return next_id - 1;
}

/* for unit tests */
size_t
get_all_jobs_used()
{
return all_jobs_used;
}

void
job_init()
{
Expand Down
3 changes: 3 additions & 0 deletions job.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ void job_insert(job head, job j);

unsigned long long int total_jobs();

/* for unit tests */
size_t get_all_jobs_used();

void job_init();

#endif /*job_h*/
9 changes: 4 additions & 5 deletions tests/test_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,18 @@ void
__CUT__job_test_all_jobs_used()
{
job j, x;
size_t *all_jobs_used_ptr = ADDR(all_jobs_used);

j = make_job(0, 0, 1, 0, default_tube);
ASSERT(*all_jobs_used_ptr == 1, "should match");
ASSERT(get_all_jobs_used() == 1, "should match");

x = allocate_job(10);
ASSERT(*all_jobs_used_ptr == 1, "should match");
ASSERT(get_all_jobs_used() == 1, "should match");

job_free(x);
ASSERT(*all_jobs_used_ptr == 1, "should match");
ASSERT(get_all_jobs_used() == 1, "should match");

job_free(j);
ASSERT(*all_jobs_used_ptr == 0, "should match");
ASSERT(get_all_jobs_used() == 0, "should match");
}

void
Expand Down

0 comments on commit 5192ce5

Please sign in to comment.