int main(int argc, char **argv) { pid_t cpid; child_t *child; int i, status, event, lclock; char *PEGASUS_HOME; char kickstart[BUFSIZ]; struct user_regs_struct regs; /* check for kickstart in local dir */ sprintf(kickstart, "./kickstart"); if (access(kickstart, X_OK) < 0) { /* check for PEGASUS_HOME env var */ PEGASUS_HOME = getenv("PEGASUS_HOME"); if (PEGASUS_HOME == NULL) { fprintf(stderr, "Please set PEGASUS_HOME\n"); exit(1); } /* check for kickstart in $PEGASUS_HOME/bin */ sprintf(kickstart, "%s/bin/kickstart", PEGASUS_HOME); if (access(kickstart, X_OK) < 0) { fprintf(stderr, "cannot execute kickstart: %s\n", kickstart); exit(1); } } /* Get transformation name if possible */ for (i=0; i<argc; i++) { if (strcmp(argv[i], "-n") == 0) { strcpy(XFORM, argv[i+1]); break; } } /* Fork kickstart */ cpid = fork(); if (cpid < 0) { perror("fork"); exit(1); } else if(cpid == 0) { if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) < 0) { perror("PTRACE_TRACEME"); exit(1); } dup2(1, 2); /* redirect stderr to stdout */ argv[0] = "kickstart"; execv(kickstart, argv); _exit(0); } else { /* initialize logical clock */ lclock = 0; print_header(); while (1) { /* __WALL is needed so that we can wait on threads too */ cpid = waitpid(0, &status, __WALL); /* find the child */ child = find_child(cpid); /* if not found, then it is new, so add it */ if (child == NULL) { child = add_child(cpid); child->tstart = get_time(); child->lstart = lclock++; if (ptrace(PTRACE_SETOPTIONS, cpid, NULL, PTRACE_O_TRACESYSGOOD|PTRACE_O_TRACEEXIT| PTRACE_O_TRACEFORK|PTRACE_O_TRACEVFORK| PTRACE_O_TRACECLONE)) { perror("PTRACE_SETOPTIONS"); exit(1); } } /* child exited */ if (WIFEXITED(status)) { remove_child(cpid); if (no_children()) break; } /* child was stopped */ if (WIFSTOPPED(status)) { /* Because of a special event we wanted to see */ if(WSTOPSIG(status) == SIGTRAP) { event = status >> 16; if (event == PTRACE_EVENT_EXIT) { child->tstop = get_time(); child->lstop = lclock++; /* fill in exe name */ if (read_exeinfo(child) < 0) { perror("read_exeinfo"); exit(1); } /* fill in memory info */ if (read_meminfo(child) < 0) { perror("read_meminfo"); exit(1); } /* fill in stat info */ if (read_statinfo(child) < 0) { perror("read_statinfo"); exit(1); } /* print stats */ print_report(child); } if (ptrace(PTRACE_SYSCALL, cpid, NULL, NULL)) { perror("PTRACE_SYSCALL event"); exit(1); } } /* Because of a system call */ else if(WSTOPSIG(status) == (SIGTRAP|0x80)) { if (ptrace(PTRACE_GETREGS, cpid, NULL, ®s)) { perror("PTRACE_GETREGS"); exit(1); } if (child->insyscall) { child->sc_rval = SC_RVAL(regs); int (*handler)(child_t *c) = syscalls[child->sc_nr].handler; if (handler) handler(child); child->insyscall = 0; } else { child->sc_nr = SC_NR(regs); child->sc_args[0] = SC_ARG0(regs); child->sc_args[1] = SC_ARG1(regs); child->sc_args[2] = SC_ARG2(regs); child->sc_args[3] = SC_ARG3(regs); child->sc_args[4] = SC_ARG4(regs); child->sc_args[5] = SC_ARG5(regs); child->insyscall = 1; } if (ptrace(PTRACE_SYSCALL, cpid, NULL, NULL)) { perror("PTRACE_SYSCALL syscall"); exit(1); } } /* Because it got a signal */ else { /* pass the signal on to the child */ if (ptrace(PTRACE_SYSCALL, cpid, 0, WSTOPSIG(status))) { perror("PTRACE_SYSCALL signal"); exit(1); } } } }
int main(void) { void *ctx = tal_strdup(NULL, "toplevel"); char *a, *b; /* If it accesses this, it will crash. */ char **invalid = (char **)1L; plan_tests(41); /* Simple matching. */ ok1(tal_strreg(ctx, "hello world!", "hello") == true); ok1(tal_strreg(ctx, "hello world!", "hi") == false); /* No parentheses means we don't use any extra args. */ ok1(tal_strreg(ctx, "hello world!", "hello", invalid) == true); ok1(tal_strreg(ctx, "hello world!", "hi", invalid) == false); ok1(tal_strreg(ctx, "hello world!", "[a-z]+", invalid) == true); ok1(tal_strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true); /* Found string */ ok1(streq(a, "hello")); /* Allocated off ctx */ ok1(find_parent(a, ctx)); tal_free(a); ok1(tal_strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)", &a, &b, invalid) == true); ok1(streq(a, "hello")); ok1(streq(b, "world")); ok1(find_parent(a, ctx)); ok1(find_parent(b, ctx)); tal_free(a); tal_free(b); /* * after parentheses returns last match. */ ok1(tal_strreg(ctx, "hello world!", "([a-z])* ([a-z]+)", &a, &b, invalid) == true); ok1(streq(a, "o")); ok1(streq(b, "world")); tal_free(a); tal_free(b); /* Nested parentheses are ordered by open brace. */ ok1(tal_strreg(ctx, "hello world!", "(([a-z]*) world)", &a, &b, invalid) == true); ok1(streq(a, "hello world")); ok1(streq(b, "hello")); tal_free(a); tal_free(b); /* Nested parentheses are ordered by open brace. */ ok1(tal_strreg(ctx, "hello world!", "(([a-z]*) world)", &a, &b, invalid) == true); ok1(streq(a, "hello world")); ok1(streq(b, "hello")); tal_free(a); tal_free(b); /* NULL means we're not interested. */ ok1(tal_strreg(ctx, "hello world!", "((hello|goodbye) world)", &a, NULL, invalid) == true); ok1(streq(a, "hello world")); tal_free(a); /* No leaks! */ ok1(no_children(ctx)); /* NULL arg with take means always fail. */ ok1(tal_strreg(ctx, take(NULL), "((hello|goodbye) world)", &b, NULL, invalid) == false); /* Take string. */ a = tal_strdup(ctx, "hello world!"); ok1(tal_strreg(ctx, take(a), "([a-z]+)", &b, invalid) == true); ok1(streq(b, "hello")); ok1(tal_parent(b) == ctx); tal_free(b); ok1(no_children(ctx)); /* Take regex. */ a = tal_strdup(ctx, "([a-z]+)"); ok1(tal_strreg(ctx, "hello world!", take(a), &b, invalid) == true); ok1(streq(b, "hello")); ok1(tal_parent(b) == ctx); tal_free(b); ok1(no_children(ctx)); /* Take both. */ a = tal_strdup(ctx, "([a-z]+)"); ok1(tal_strreg(ctx, take(tal_strdup(ctx, "hello world!")), take(a), &b, invalid) == true); ok1(streq(b, "hello")); ok1(tal_parent(b) == ctx); tal_free(b); ok1(no_children(ctx)); /* ... even if we fail to match. */ a = tal_strdup(ctx, "([a-z]+)"); ok1(tal_strreg(ctx, take(tal_strdup(ctx, "HELLO WORLD!")), take(a), &b, invalid) == false); ok1(no_children(ctx)); tal_free(ctx); /* Don't get fooled by \(! */ ok1(tal_strreg(ctx, "(hello) (world)!", "\\([a-z]*\\) \\([a-z]+\\)", invalid) == true); return exit_status(); }
/// Number of siblings of a node /// /// Siblings are nodes sharing the same parent. /// /// \param nd [in] spatial dimension of the node /// /// Formula: \f$ 2^{nd} \f$ /// static constexpr uint_t no_siblings(uint_t nd) noexcept { return no_children(nd); }
int main(void) { char *parent, *c; plan_tests(43); parent = tal(NULL, char); ok1(parent); c = tal_strdup(parent, "hello"); ok1(strcmp(c, "hello") == 0); ok1(tal_parent(c) == parent); ok1(tal_count(c) == strlen(c) + 1); tal_free(c); c = tal_strndup(parent, "hello", 3); ok1(strcmp(c, "hel") == 0); ok1(tal_parent(c) == parent); ok1(tal_count(c) == strlen(c) + 1); tal_free(c); #ifdef TAL_USE_TALLOC c = tal_talloc_typechk_(parent, char *); #else c = tal_typechk_(parent, char *); #endif c = tal_dup_arr(parent, char, "hello", 6, 0); ok1(strcmp(c, "hello") == 0); ok1(strcmp(tal_name(c), "char[]") == 0); ok1(tal_count(c) == 6); ok1(tal_parent(c) == parent); tal_free(c); /* Now with an extra byte. */ c = tal_dup_arr(parent, char, "hello", 6, 1); ok1(strcmp(c, "hello") == 0); ok1(strcmp(tal_name(c), "char[]") == 0); ok1(tal_count(c) == 7); ok1(tal_parent(c) == parent); strcat(c, "x"); tal_free(c); c = tal_fmt(parent, "hello %s", "there"); ok1(strcmp(c, "hello there") == 0); ok1(tal_count(c) == strlen(c) + 1); ok1(tal_parent(c) == parent); tal_free(c); c = tal_strcat(parent, "hello ", "there"); ok1(strcmp(c, "hello there") == 0); ok1(tal_count(c) == strlen(c) + 1); ok1(tal_parent(c) == parent); /* Make sure take works correctly. */ c = tal_strcat(parent, take(c), " again"); ok1(strcmp(c, "hello there again") == 0); ok1(tal_count(c) == strlen(c) + 1); ok1(tal_parent(c) == parent); ok1(single_child(parent, c)); c = tal_strcat(parent, "And ", take(c)); ok1(tal_count(c) == strlen(c) + 1); ok1(strcmp(c, "And hello there again") == 0); ok1(tal_parent(c) == parent); ok1(single_child(parent, c)); /* NULL pass through works... */ c = tal_strcat(parent, take(NULL), take(c)); ok1(!c); ok1(no_children(parent)); c = tal_strcat(parent, take(tal_strdup(parent, "hi")), take(NULL)); ok1(!c); ok1(no_children(parent)); c = tal_strcat(parent, take(NULL), take(NULL)); ok1(!c); ok1(no_children(parent)); /* Appending formatted strings. */ c = tal_strdup(parent, "hi"); ok1(tal_count(c) == strlen(c) + 1); ok1(tal_append_fmt(&c, "%s %s", "there", "world")); ok1(strcmp(c, "hithere world") == 0); ok1(tal_count(c) == strlen(c) + 1); ok1(tal_parent(c) == parent); ok1(!tal_append_fmt(&c, take(NULL), "there", "world")); ok1(strcmp(c, "hithere world") == 0); ok1(tal_count(c) == strlen(c) + 1); tal_free(parent); return exit_status(); }