int main (int argc, char *argv[]) { char *main_name __attribute__((unused)) = "main name"; pthread_t ebbr, egll, zzzz; int i = 1234; char undef = '?'; char *some_mem __attribute__((unused)) = malloc(100); VALGRIND_MAKE_MEM_UNDEFINED(&undef, 1); int len = strlen(undefined); breakme(__LINE__); for (i = len-1; i >= 0; i=i-2) undefined[i] = undef; *(char*)&int_und = undef; breakme(__LINE__); if (argc > 1) sleeps = atoi(argv[1]); level(); make_error ("called from main"); pthread_create(&ebbr, NULL, brussels_fn, NULL); pthread_create(&egll, NULL, london_fn, NULL); pthread_create(&zzzz, NULL, petaouchnok_fn, NULL); loopmain = 1; while (! (loopt1 && loopt2 && loopmain)) loopmain++; for (i = 0; i < LOOPS; i++) { loopmain++; if (loopmain == 10000) make_error ("in main loop"); } pthread_join(ebbr, NULL); make_error ("called from main (the end, before joining t3)"); pthread_join(zzzz, NULL); if (argc > 2) { for (i = 0; i < 100; i++) if ((*(&undef + i*4000) == 0) || (*(&undef - i*4000) == 0)) { printf ("there are some null bytes here and there %d\n", i); fflush(stdout); } } exit(0); }
void worker( cyg_addrword_t id ) { for(;;) { thread_count[id]++; thread_pri[id] = cyg_thread_get_priority( cyg_thread_self() ); switch( worker_state ) { case WORKER_STATE_BREAK: if( 0 == (id % 4) ) breakme(); case WORKER_STATE_WAIT: cyg_mutex_lock( &worker_mutex ); workers_asleep++; cyg_cond_wait( &worker_cv ); workers_asleep--; cyg_mutex_unlock( &worker_mutex ); break; case WORKER_STATE_EXIT: cyg_thread_exit(); } } }
static int rfun(int* c, int* f, int n, int target, MyNode** head, int count) { int cv = *c; if (cv > target) { return 0; } int sum = 0; int tdiv = target / cv; int tmod = target % cv; if (!n || !tmod) { breakme(); if (tmod) return 0; MyNode* node = (MyNode*)malloc(sizeof(MyNode)); if (node) { int total = count + tdiv; int* match = (int*)malloc(sizeof(int) * total); if (match) { int* pf; int* pc; node->next = *head; node->cmb = match; node->cmbSize = total; *f = tdiv; int* p = match + total; for (pf = f, pc = c; total > 0; pf--, pc--) { int i = *pf; total -= i; for (; i > 0; i--) { *(--p) = *pc; } } *head = node; #ifdef MY_UNIT_TEST static int ID = 0; printf("CMB %d count %d:", ID++, (total = count + tdiv)); for (int i = 0; i < total; i++) { printf(",%d", node->cmb[i]); } printf("\n"); #endif sum = 1; } else { free(node); } } if (!n || !sum) return sum; tdiv--; } int nt, nc = c[1]; for (int i = tdiv; i >= 0; i--) { *f = i; nt = (target - (i * cv)); if (nc <= nt) { sum += rfun(c + 1, f + 1, n - 1, nt, head, count + i); } } return sum; }
void controller( cyg_addrword_t id ) { cyg_priority_t pri; int i; cyg_mutex_init( &worker_mutex ); cyg_cond_init( &worker_cv, &worker_mutex ); // 1 thread, it is running, it calls BREAKME(); // +++ Thread status returned: // +++ 1 thread, running, is the current one breakme(); // Create N more threads; they are all suspended after creation. Adjust // the priorities of all the threads to lower than the controlling thread. // Make them all be distinct priorities as far as possible. BREAKME(); // +++ 1 thread, running, + N suspended ones of different prios. for( i = 1; i < THREADS; i++ ) { pri = CONTROLLER_PRI_HI + 1 + i % WORKER_PRI_RANGE; cyg_thread_create(pri, worker, (cyg_addrword_t)i, "worker", (void *)(&thread_stack[i]), STACKSIZE, &thread_handle[i], &thread[i]); } breakme(); // Adjust the priorities of all the threads to lower than the controlling // thread. Make them all be THE SAME priority. BREAKME(); // +++ 1 thread, running, + N suspended ones of same prio. for( i = 1; i < THREADS; i++ ) { cyg_thread_set_priority( thread_handle[i], WORKER_PRI ); } breakme(); // Release all the N threads, BREAKME(); // +++ 1 thread, running, + N ready ones of same prio. for( i = 1; i < THREADS; i++ ) { cyg_thread_resume( thread_handle[i] ); } breakme(); // Adjust the priorities of all the threads, lower than the controlling // thread. Make them all be distinct priorities as far as possible. // BREAKME(); // +++ 1 thread, running, + N ready ones of different prios. for( i = 1; i < THREADS; i++ ) { pri = CONTROLLER_PRI_HI + 1 + i % WORKER_PRI_RANGE; cyg_thread_set_priority( thread_handle[i], pri ); } breakme(); // Command all the N threads to sleep; switch my own priority to lower // than theirs so that they all run and sleep, then I get back in and // BREAKME(); // +++ 1 thread, running, + N sleeping ones of different prios. worker_state = WORKER_STATE_WAIT; cyg_thread_set_priority( thread_handle[0], CONTROLLER_PRI_LO ); breakme(); // Make them all be THE SAME priority; BREAKME(); // +++ 1 thread, running, + N sleeping ones of same prio. for( i = 1; i < THREADS; i++ ) { cyg_thread_set_priority( thread_handle[i], WORKER_PRI ); } breakme(); // Wake them all up, they'll loop once and sleep again; I get in and // BREAKME(); // +++ 1 thread, running, + N sleeping ones of same prio. cyg_cond_broadcast( &worker_cv ); breakme(); // Adjust the priorities of all the threads, higher than the controlling // thread. Make them all be distinct priorities as far as possible. // BREAKME(); // +++ 1 thread, running, + N sleeping ones of different prios. for( i = 1; i < THREADS; i++ ) { pri = CONTROLLER_PRI_HI + 1 + i % WORKER_PRI_RANGE; cyg_thread_set_priority( thread_handle[i], pri ); } breakme(); // Wake them all up, they'll loop once and sleep again; I get in and // BREAKME(); // +++ 1 thread, running, + N sleeping ones of different prios. cyg_cond_broadcast( &worker_cv ); breakme(); // Set them all the same prio, set me to the same prio, BREAKME(); // +++ 1 running, + N sleeping, *all* the same prio. for( i = 0; i < THREADS; i++ ) { cyg_thread_set_priority( thread_handle[i], WORKER_PRI ); } breakme(); // Wake them all up, they'll loop once and sleep again; I get in and // BREAKME(); repeatedly until they have all slept again. // +++ 1 running, + some sleeping, some ready, *all* the same prio. cyg_cond_broadcast( &worker_cv ); // cyg_thread_yield(); do { breakme(); } while( workers_asleep != THREADS-1 ); breakme(); // Suspend some of the threads, BREAKME(); // +++ 1 running, + some sleeping, some suspended, *all* the same prio. for( i = 1; i < THREADS; i++ ) { // suspend every 3rd thread if( 0 == (i % 3) ) cyg_thread_suspend( thread_handle[i] ); } breakme(); // Change the prios all different, change my prio to highest , BREAKME(); // +++ 1 running, + some sleeping, some suspended, different prios. cyg_thread_set_priority( thread_handle[0], CONTROLLER_PRI_HI ); for( i = 1; i < THREADS; i++ ) { pri = CONTROLLER_PRI_HI + 1 + i % WORKER_PRI_RANGE; cyg_thread_set_priority( thread_handle[i], pri ); } breakme(); // Wake up all the threads, BREAKME(); // +++ 1 running, + some ready, some suspended/ready, different prios. cyg_cond_broadcast( &worker_cv ); breakme(); // Change my prio to lowest, let all the threads run, BREAKME(). // +++ 1 running + some sleeping, some suspended/ready, different prios. cyg_thread_set_priority( thread_handle[0], CONTROLLER_PRI_LO ); breakme(); // Resume all the threads, BREAKME(); // +++ 1 running, + N ready, different prios. for( i = 1; i < THREADS; i++ ) { cyg_thread_resume( thread_handle[i] ); } breakme(); // Command some of the N threads to call BREAKME(); themselves (then sleep // again). Change my prio to low, so that they all get to run and hit the // breakpoint. // +++ A different one running every time, others in a mixture of // ready and sleeping states. worker_state = WORKER_STATE_BREAK; cyg_cond_broadcast( &worker_cv ); cyg_thread_set_priority( thread_handle[0], CONTROLLER_PRI_LO ); breakme(); // Command all the threads to exit; switch my own priority to lower // than theirs so that they all run and exit, then I get back in and // BREAKME(); // +++ 1 thread, running, + N dormant ones. worker_state = WORKER_STATE_EXIT; cyg_cond_broadcast( &worker_cv ); breakme(); #if 0 // Cannot do this yet... // Destroy some of the N threads to invalidate their IDs. Re-create them // with new IDs, so that we get IDs 1,2,4,6,8,11,12,13,14,15 in use instead // of 1,2,3,4,5,6,7,8,9, if you see what I mean. Do all the above again. // Loop forever, or whatever... #endif breakme(); CYG_TEST_PASS_FINISH("GDB Thread test OK"); }
int main (int argc, char *argv[]) { /* we will test read watchpoint at 0, read/write watchpoints at 4 write watchpoints at 8 */ breakme(__LINE__); //break1 /* We verify read watchpoints are triggered at 0 and 4, not at 8 */ fprintf(stderr, "before reading 0/4/8\n"); if (undefined[0] == 'u') fprintf(stderr, "u: Expected value at 0\n"); else fprintf(stderr, "u: Unexpected value at 0\n"); if (undefined[4] == 'f') fprintf(stderr, "f: Expected value at 4\n"); else fprintf(stderr, "f: Unexpected value at 4\n"); if (undefined[8] == 'd') fprintf(stderr, "d: Expected value at 8\n"); else fprintf(stderr, "d: Unexpected value at 8\n"); /* We verify write watchpoints are triggered at 4 and 8, not at 0 */ fprintf(stderr, "before writing 0\n"); undefined[0] = 'U'; fprintf(stderr, "before writing 4\n"); undefined[4] = 'F'; fprintf(stderr, "before writing 8\n"); undefined[8] = 'D'; fprintf(stderr, "after writing 8\n"); /* after having remove the watchpoints, check we can read and write without break. */ fprintf(stderr, "value %s\n", undefined); fprintf(stderr, "before rewriting 0\n"); undefined[0] = '0'; fprintf(stderr, "before rewriting 4\n"); undefined[4] = '4'; fprintf(stderr, "before rewriting 8\n"); undefined[8] = '8'; fprintf(stderr, "value %s\n", undefined); { char *k50 = malloc(50000); memset (k50, 'p', 50000); fprintf(stderr, "value of k50[1000] %c\n", k50[1000]); free(k50); //break2 } exit(0); }
U32 (*setpatch(U32 maddr, U32 (*code)(struct m_registers *, void *)))(struct m_registers *, void *) { struct seg_info *seg; struct code_info *ci; #ifdef DEBUG printf("Setpatch(0x%08x)\n", maddr); #endif /* DEBUG */ if (maddr & 0xff000001) { fprintf(stderr, "setpatch: Bad address 0x%08x\n", maddr); return(NULL); } if ((!(seg = find_seg(code_tree, maddr))) || (!(ci = find_ci(&seg->codeinfo, maddr)))) { U32 mend; ci = new_codeinfo(maddr); #ifdef DEBUG if (debuglevel & DL_RUNTIME_TRACE) { fprintf(stdout, "setpatch: Compiling 0x%08x...\n", maddr); } #endif /* DEBUG */ mend = compile(ci); if (!seg) { #ifdef DEBUG if (debuglevel & DL_COMPILER_VERBOSE) { fprintf(stdout, "Creating new segment, maddr: %08x, end: %08x\n", maddr, mend); } #endif /* DEBUG */ seg = insert_seg(&code_tree, maddr, mend); if (!seg) { /* BUS ERROR? */ fprintf(stderr, "Out of memory?\n"); abort(); } } else if (mend != seg->mend) { fprintf(stderr, "Strange ending, orig:%08x, new:%08x\n", seg->mend, mend); abort(); } /* Link it first */ ci->next = seg->codeinfo; seg->codeinfo = ci; #ifdef DEBUG if (debuglevel & DL_COMPILER_DISASSEMBLY) { disassemble(maddr, mend); } #endif /* DEBUG */ } /* Don't attempt to patch an already patched function */ if (ci->flags & CIF_PATCHED) { return(NULL); } else { U32 (*retval)(struct m_registers *, void *); /* Don't attempt to free a patched function in the garbage collector */ retval = ci->code; if (retval) { ci->flags |= CIF_LOCKED | CIF_PATCHED; ci->code = code; } else { fprintf(stderr, "Setpatch(0x%08x): retval == NULL\n", maddr); breakme(); } return(retval); } }
//4ms version static int rfun(int* c, int* f, int n, int target, MyNode** head, int count) { int cv = *c; if ((cv > target) || (n < 0)) { breakme(); return 0; } int sum = 0; if (!n || (target == cv)) { breakme(); if (target != cv) return 0; MyNode* node = (MyNode*)malloc(sizeof(MyNode)); if (node) { int total = count + 1; int* match = (int*)malloc(sizeof(int) * total); if (match) { int* pf; int* pc; node->next = *head; node->cmb = match; node->cmbSize = total; *f = 1; int* p = match + total; for (pf = f, pc = c; total > 0; pf--, pc--) { int i = *pf; total -= i; for (; i > 0; i--) { *(--p) = *pc; } } total = node->cmbSize; #ifdef MY_UNIT_TEST static int ID = 0; printf("CMB %d count %d:", ID++, total); for (int i = 0; i < total; i++) { printf(",%d", node->cmb[i]); } printf("\n"); #endif MyNode* prev = *head; DBG("Curr %p cmbSize %d\n", node, total); DBG("Prev %p cmbSize %d\n", prev, prev ? prev->cmbSize : 0); while (prev != NULL) { if ((prev->cmbSize == total) && !memcmp(prev->cmb, match, total * sizeof(int))) { DBG("Duplicate result detected, discarding...\n"); free(match); free(node); return 0; } prev = prev->next; } *head = node; sum = 1; DBG("Good result detected, collecting...\n"); } else { free(node); } } return sum; } { int nt = target - cv; if (cv <= target) { if (c[1] <= nt) { *f = 1; sum = rfun(c + 1, f + 1, n - 1, nt, head, count + 1); } *f = 0; sum += rfun(c + 1, f + 1, n - 1, target, head, count); }/* *f = 1; sum = rfun(c + 1, f + 1, n - 1, nt, head, count + 1); *f = 0; sum += rfun(c + 1, f + 1, n - 1, target, head, count);*/ } return sum; }