status_t Team::Init(team_id teamID, port_id debuggerPort) { // get team info team_info teamInfo; status_t error = get_team_info(teamID, &teamInfo); if (error != B_OK) return error; fID = teamID; fArgs = teamInfo.args; // install ourselves as the team debugger fNubPort = install_team_debugger(teamID, debuggerPort); if (fNubPort < 0) { fprintf(stderr, "%s: Failed to install as debugger for team %ld: " "%s\n", kCommandName, teamID, strerror(fNubPort)); return fNubPort; } // init debug context error = init_debug_context(&fDebugContext, teamID, fNubPort); if (error != B_OK) { fprintf(stderr, "%s: Failed to init debug context for team %ld: " "%s\n", kCommandName, teamID, strerror(error)); return error; } // set team debugging flags int32 teamDebugFlags = B_TEAM_DEBUG_THREADS | B_TEAM_DEBUG_TEAM_CREATION | B_TEAM_DEBUG_IMAGES; set_team_debugging_flags(fNubPort, teamDebugFlags); return B_OK; }
status_t BDebugContext::Init(team_id team, port_id nubPort) { Uninit(); status_t error = init_debug_context(&fContext, team, nubPort); if (error != B_OK) { fContext.team = -1; return error; } return B_OK; }
int main(int argc, char * argv[]) { int parser_ret; void * instance; int arg; FILE * file; char user_state[USER_STATE_SIZE]; int ret; fc_solve_display_information_context_t debug_context; init_debug_context(&debug_context); dc = &debug_context; instance = freecell_solver_user_alloc(); current_instance = instance; { char * error_string; parser_ret = freecell_solver_user_cmd_line_parse_args( instance, argc, (freecell_solver_str_t *)argv, 1, (freecell_solver_str_t *)known_parameters, cmd_line_callback, &debug_context, &error_string, &arg ); if (parser_ret == EXIT_AND_RETURN_0) { freecell_solver_user_free(instance); return 0; } else if ( (parser_ret == FCS_CMD_LINE_PARAM_WITH_NO_ARG) ) { fprintf(stderr, "The command line parameter \"%s\" requires an argument" " and was not supplied with one.\n", argv[arg]); return (-1); } else if ( (parser_ret == FCS_CMD_LINE_ERROR_IN_ARG) ) { if (error_string != NULL) { fprintf(stderr, "%s", error_string); free(error_string); } freecell_solver_user_free(instance); return -1; } } if ((arg == argc) || (!strcmp(argv[arg], "-"))) { file = stdin; if (!getenv("FREECELL_SOLVER_QUIET")) { fprintf(stderr, "%s", "Reading the board from the standard input.\n" "Type \"fc-solve --help\" for more usage information.\n" "To cancel this message set the FREECELL_SOLVER_QUIET environment variable.\n" ); } } else if (argv[arg][0] == '-') { fprintf(stderr, "Unknown option \"%s\". " "Type \"%s --help\" for usage information.\n", argv[arg], argv[0] ); freecell_solver_user_free(instance); return -1; } else { file = fopen(argv[arg], "r"); if (file == NULL) { fprintf(stderr, "Could not open file \"%s\" for input. Exiting.\n", argv[arg] ); freecell_solver_user_free(instance); return -1; } } memset(user_state, '\0', sizeof(user_state)); fread(user_state, sizeof(user_state[0]), USER_STATE_SIZE-1, file); fclose(file); /* Win32 Does not have those signals */ #ifndef WIN32 signal(SIGUSR1, command_signal_handler); signal(SIGUSR2, select_signal_handler); #endif #if 0 { int limit = 500; freecell_solver_user_limit_iterations(instance, limit); ret = freecell_solver_user_solve_board(instance, user_state); while (ret == FCS_STATE_SUSPEND_PROCESS) { limit += 500; freecell_solver_user_limit_iterations(instance, limit); ret = freecell_solver_user_resume_solution(instance); } } #else ret = freecell_solver_user_solve_board(instance, user_state); #endif if (ret == FCS_STATE_INVALID_STATE) { char * error_string; error_string = freecell_solver_user_get_invalid_state_error_string( instance, debug_context.display_10_as_t ); printf("%s\n", error_string); free(error_string); error_string = NULL; } else if (ret == FCS_STATE_FLARES_PLAN_ERROR) { const char * error_string; error_string = freecell_solver_user_get_last_error_string(instance); printf("Flares Plan: %s\n", error_string); } else { FILE * output_fh; if (debug_context.output_filename) { output_fh = fopen(debug_context.output_filename, "wt"); if (! output_fh) { fprintf(stderr, "Could not open output file '%s' for writing!", debug_context.output_filename ); return -1; } } else { output_fh = stdout; } if (ret == FCS_STATE_WAS_SOLVED) { fprintf(output_fh, "-=-=-=-=-=-=-=-=-=-=-=-\n\n"); { fcs_move_t move; FILE * move_dump; char * as_string; int move_num = 0; move_dump = output_fh; if (debug_context.display_states) { as_string = freecell_solver_user_current_state_as_string( instance, debug_context.parseable_output, debug_context.canonized_order_output, debug_context.display_10_as_t ); fprintf(move_dump, "%s\n", as_string); free(as_string); fprintf(move_dump, "%s", "\n====================\n\n"); } while ( freecell_solver_user_get_next_move( instance, &move ) == 0 ) { if (debug_context.display_moves) { as_string = freecell_solver_user_move_to_string_w_state( instance, move, debug_context.standard_notation ); if (debug_context.display_states && debug_context.standard_notation) { fprintf(move_dump, "Move: "); } fprintf( move_dump, (debug_context.standard_notation ? "%s " : "%s\n" ), as_string ); move_num++; if (debug_context.standard_notation) { if ((move_num % 10 == 0) || debug_context.display_states) { fprintf(move_dump, "\n"); } } if (debug_context.display_states) { fprintf(move_dump, "\n"); } fflush(move_dump); free(as_string); } if (debug_context.display_states) { as_string = freecell_solver_user_current_state_as_string( instance, debug_context.parseable_output, debug_context.canonized_order_output, debug_context.display_10_as_t ); fprintf(move_dump, "%s\n", as_string); free(as_string); } if (debug_context.display_states || (!debug_context.standard_notation)) { fprintf(move_dump, "%s", "\n====================\n\n"); } } if (debug_context.standard_notation && (!debug_context.display_states)) { fprintf(move_dump, "\n\n"); } } fprintf(output_fh, "This game is solveable.\n"); } else { fprintf (output_fh, "I could not solve this game.\n"); } fprintf( output_fh, "Total number of states checked is %i.\n", freecell_solver_user_get_num_times(instance) ); #if 1 fprintf( output_fh, "This scan generated %i states.\n", freecell_solver_user_get_num_states_in_collection(instance) ); #endif if (debug_context.output_filename) { fclose(output_fh); output_fh = NULL; } } freecell_solver_user_free(instance); return 0; }
static int cmd_line_callback( void * instance, int argc, const char * argv[], int arg, int * num_to_skip, int * ret, void * context ) { fc_solve_display_information_context_t * dc; *num_to_skip = 0; dc = (fc_solve_display_information_context_t * )context; if (!strcmp(argv[arg], "--version")) { printf( "fc-solve\nlibfreecell-solver version %s\n", freecell_solver_user_get_lib_version(instance) ); *ret = EXIT_AND_RETURN_0; return FCS_CMD_LINE_STOP; } else if ((!strcmp(argv[arg], "-h")) || (!strcmp(argv[arg], "--help"))) { char * help_key; help_key = getenv("FREECELL_SOLVER_DEFAULT_HELP"); if (help_key == NULL) { help_key = "summary"; } print_help_string(help_key); *ret = EXIT_AND_RETURN_0; return FCS_CMD_LINE_STOP; } else if (!strncmp(argv[arg], "--help-", 7)) { print_help_string(argv[arg]+7); *ret = EXIT_AND_RETURN_0; return FCS_CMD_LINE_STOP; } else if ((!strcmp(argv[arg], "-i")) || (!strcmp(argv[arg], "--iter-output"))) { #define set_iter_handler() \ freecell_solver_user_set_iter_handler( \ instance, \ my_iter_handler, \ dc \ ); \ dc->debug_iter_output_on = TRUE; set_iter_handler(); } else if ((!strcmp(argv[arg], "-s")) || (!strcmp(argv[arg], "--state-output"))) { set_iter_handler(); dc->debug_iter_state_output = TRUE; #undef set_iter_handler } else if ((!strcmp(argv[arg], "-p")) || (!strcmp(argv[arg], "--parseable-output"))) { dc->parseable_output = TRUE; } else if ((!strcmp(argv[arg], "-c")) || (!strcmp(argv[arg], "--canonized-order-output"))) { dc->canonized_order_output = TRUE; } else if ((!strcmp(argv[arg], "-o")) || (!strcmp(argv[arg], "--output"))) { arg++; if (arg == argc) { return FCS_CMD_LINE_STOP; } *num_to_skip = 2; dc->output_filename = (const char *)argv[arg]; return FCS_CMD_LINE_SKIP; } else if ((!strcmp(argv[arg], "-t")) || (!strcmp(argv[arg], "--display-10-as-t"))) { dc->display_10_as_t = TRUE; } else if ((!strcmp(argv[arg], "-m")) || (!strcmp(argv[arg], "--display-moves"))) { dc->display_moves = TRUE; dc->display_states = FALSE; } else if ((!strcmp(argv[arg], "-sn")) || (!strcmp(argv[arg], "--standard-notation"))) { dc->standard_notation = STANDARD_NOTATION_REGULAR; } else if ((!strcmp(argv[arg], "-snx")) || (!strcmp(argv[arg], "--standard-notation-extended"))) { dc->standard_notation = STANDARD_NOTATION_EXTENDED; } else if ((!strcmp(argv[arg], "-sam")) || (!strcmp(argv[arg], "--display-states-and-moves"))) { dc->display_moves = TRUE; dc->display_states = TRUE; } else if ((!strcmp(argv[arg], "-pi")) || (!strcmp(argv[arg], "--display-parent-iter"))) { dc->display_parent_iter_num = TRUE; } else if ((!strcmp(argv[arg], "--reset"))) { init_debug_context(dc); freecell_solver_user_set_iter_handler( instance, NULL, NULL ); *num_to_skip = 0; return FCS_CMD_LINE_OK; } else { printf("Unimplemented option - \"%s\"!", argv[arg]); exit(-1); } *num_to_skip = 1; return FCS_CMD_LINE_SKIP; }
status_t Init(team_id team, port_id nubPort) { return init_debug_context(this, team, nubPort); }
int main(int argc, char * argv[]) { int parser_ret; void * instance; int arg; FILE * file; char user_state[USER_STATE_SIZE]; int ret; fc_solve_display_information_context_t debug_context; init_debug_context(&debug_context); dc = &debug_context; instance = freecell_solver_user_alloc(); current_instance = instance; { char * error_string; parser_ret = freecell_solver_user_cmd_line_parse_args( instance, argc, (freecell_solver_str_t *)argv, 1, (freecell_solver_str_t *)known_parameters, cmd_line_callback, &debug_context, &error_string, &arg ); if (parser_ret == EXIT_AND_RETURN_0) { freecell_solver_user_free(instance); return 0; } else if ( (parser_ret == FCS_CMD_LINE_PARAM_WITH_NO_ARG) ) { fprintf(stderr, "The command line parameter \"%s\" requires an argument" " and was not supplied with one.\n", argv[arg]); return (-1); } else if ( (parser_ret == FCS_CMD_LINE_ERROR_IN_ARG) ) { if (error_string != NULL) { fprintf(stderr, "%s", error_string); free(error_string); } freecell_solver_user_free(instance); return -1; } } if ((arg == argc) || (!strcmp(argv[arg], "-"))) { file = stdin; if (!getenv("FREECELL_SOLVER_QUIET")) { fprintf(stderr, "%s", "Reading the board from the standard input.\n" "Type \"fc-solve --help\" for more usage information.\n" "To cancel this message set the FREECELL_SOLVER_QUIET environment variable.\n" ); } } else if (argv[arg][0] == '-') { fprintf(stderr, "Unknown option \"%s\". " "Type \"%s --help\" for usage information.\n", argv[arg], argv[0] ); freecell_solver_user_free(instance); return -1; } else { file = fopen(argv[arg], "r"); if (file == NULL) { fprintf(stderr, "Could not open file \"%s\" for input. Exiting.\n", argv[arg] ); freecell_solver_user_free(instance); return -1; } } memset(user_state, '\0', sizeof(user_state)); fread(user_state, sizeof(user_state[0]), USER_STATE_SIZE-1, file); fclose(file); /* Win32 Does not have those signals */ #ifndef WIN32 signal(SIGUSR1, command_signal_handler); signal(SIGUSR2, select_signal_handler); #endif #if 0 { int limit = 500; freecell_solver_user_limit_iterations(instance, limit); ret = freecell_solver_user_solve_board(instance, user_state); while (ret == FCS_STATE_SUSPEND_PROCESS) { limit += 500; freecell_solver_user_limit_iterations(instance, limit); ret = freecell_solver_user_resume_solution(instance); } } #elif defined(FCS_TRACE_MEM) { #define STEP 100000 int limit = STEP; char stat_fn[1024], foo_str[1024]; fcs_portable_time_t mytime; FILE * stat; long long int rss; unsigned long long unsigned_foo; snprintf(stat_fn, sizeof(stat_fn), "/proc/%ld/stat", (long)(getpid())); freecell_solver_user_limit_iterations(instance, limit); ret = freecell_solver_user_solve_board(instance, user_state); while (ret == FCS_STATE_SUSPEND_PROCESS) { FCS_GET_TIME(mytime); /* This was taken from: * * http://www.brokestream.com/procstat.html * */ stat = fopen(stat_fn, "r"); #define readone(foo) (fscanf(stat, "%lld ", &rss)) #define readstr(foo) (fscanf(stat, "%1000s ", foo_str)) #define readchar(foo) (fscanf(stat, "%c ", foo_str)) #define readunsigned(foo) (fscanf(stat, "%llu ", &unsigned_foo)) readone(&pid); readstr(tcomm); readchar(&state); readone(&ppid); readone(&pgid); readone(&sid); readone(&tty_nr); readone(&tty_pgrp); readone(&flags); readone(&min_flt); readone(&cmin_flt); readone(&maj_flt); readone(&cmaj_flt); readone(&utime); readone(&stimev); readone(&cutime); readone(&cstime); readone(&priority); readone(&nicev); readone(&num_threads); readone(&it_real_value); readunsigned(&start_time); readone(&vsize); readone(&rss); #undef readone #undef readunsigned #undef readchar #undef readstr fclose(stat); printf("Reached:\t%d\t%li.%.6li\t%lld\n", limit, FCS_TIME_GET_SEC(mytime), FCS_TIME_GET_USEC(mytime), rss ); fflush(stdout); limit += STEP; freecell_solver_user_limit_iterations(instance, limit); ret = freecell_solver_user_resume_solution(instance); } } #undef STEP #else ret = freecell_solver_user_solve_board(instance, user_state); #endif if (ret == FCS_STATE_INVALID_STATE) { char * error_string; error_string = freecell_solver_user_get_invalid_state_error_string( instance, debug_context.display_10_as_t ); printf("%s\n", error_string); free(error_string); error_string = NULL; } else if (ret == FCS_STATE_FLARES_PLAN_ERROR) { const char * error_string; error_string = freecell_solver_user_get_last_error_string(instance); printf("Flares Plan: %s\n", error_string); } else { FILE * output_fh; if (debug_context.output_filename) { output_fh = fopen(debug_context.output_filename, "wt"); if (! output_fh) { fprintf(stderr, "Could not open output file '%s' for writing!", debug_context.output_filename ); return -1; } } else { output_fh = stdout; } if (ret == FCS_STATE_WAS_SOLVED) { fprintf(output_fh, "-=-=-=-=-=-=-=-=-=-=-=-\n\n"); { fcs_move_t move; FILE * move_dump; char * as_string; int move_num = 0; move_dump = output_fh; if (debug_context.display_states) { as_string = freecell_solver_user_current_state_as_string( instance, debug_context.parseable_output, debug_context.canonized_order_output, debug_context.display_10_as_t ); fprintf(move_dump, "%s\n", as_string); free(as_string); fprintf(move_dump, "%s", "\n====================\n\n"); } while ( freecell_solver_user_get_next_move( instance, &move ) == 0 ) { if (debug_context.display_moves) { as_string = freecell_solver_user_move_to_string_w_state( instance, move, debug_context.standard_notation ); if (debug_context.display_states && debug_context.standard_notation) { fprintf(move_dump, "Move: "); } fprintf( move_dump, (debug_context.standard_notation ? "%s " : "%s\n" ), as_string ); move_num++; if (debug_context.standard_notation) { if ((move_num % 10 == 0) || debug_context.display_states) { fprintf(move_dump, "\n"); } } if (debug_context.display_states) { fprintf(move_dump, "\n"); } fflush(move_dump); free(as_string); } if (debug_context.display_states) { as_string = freecell_solver_user_current_state_as_string( instance, debug_context.parseable_output, debug_context.canonized_order_output, debug_context.display_10_as_t ); fprintf(move_dump, "%s\n", as_string); free(as_string); } if (debug_context.display_states || (!debug_context.standard_notation)) { fprintf(move_dump, "%s", "\n====================\n\n"); } } if (debug_context.standard_notation && (!debug_context.display_states)) { fprintf(move_dump, "\n\n"); } } fprintf(output_fh, "This game is solveable.\n"); } else { fprintf (output_fh, "I could not solve this game.\n"); } fprintf( output_fh, "Total number of states checked is %i.\n", freecell_solver_user_get_num_times(instance) ); #if 1 fprintf( output_fh, "This scan generated %i states.\n", freecell_solver_user_get_num_states_in_collection(instance) ); #endif if (debug_context.output_filename) { fclose(output_fh); output_fh = NULL; } } freecell_solver_user_free(instance); return 0; }
int main(int argc, char * argv[]) { int parser_ret; int arg; FILE * file; char user_state[USER_STATE_SIZE]; int ret; fc_solve_display_information_context_t debug_context; init_debug_context(&debug_context); dc = &debug_context; void * const instance = freecell_solver_user_alloc(); current_instance = instance; { char * error_string; parser_ret = freecell_solver_user_cmd_line_parse_args( instance, argc, (freecell_solver_str_t *)(void *)argv, 1, (freecell_solver_str_t *)known_parameters, cmd_line_callback, &debug_context, &error_string, &arg ); if (parser_ret == EXIT_AND_RETURN_0) { freecell_solver_user_free(instance); return 0; } else if (parser_ret == FCS_CMD_LINE_PARAM_WITH_NO_ARG) { fprintf(stderr, "The command line parameter \"%s\" requires an argument" " and was not supplied with one.\n", argv[arg]); return (-1); } else if (parser_ret == FCS_CMD_LINE_ERROR_IN_ARG) { if (error_string != NULL) { fprintf(stderr, "%s", error_string); free(error_string); } freecell_solver_user_free(instance); return -1; } } if ((arg == argc) || (!strcmp(argv[arg], "-"))) { file = stdin; if (!getenv("FREECELL_SOLVER_QUIET")) { fprintf(stderr, "%s", "Reading the board from the standard input.\n" "Please refer to the documentation for more usage information:\n" " http://fc-solve.shlomifish.org/docs/\n" "To cancel this message set the FREECELL_SOLVER_QUIET environment variable.\n" ); } } else if (argv[arg][0] == '-') { fprintf(stderr, "Unknown option \"%s\". " "Type \"%s --help\" for usage information.\n", argv[arg], argv[0] ); freecell_solver_user_free(instance); return -1; } else { file = fopen(argv[arg], "r"); if (file == NULL) { fprintf(stderr, "Could not open file \"%s\" for input. Exiting.\n", argv[arg] ); freecell_solver_user_free(instance); return -1; } } memset(user_state, '\0', sizeof(user_state)); fread(user_state, sizeof(user_state[0]), USER_STATE_SIZE-1, file); fclose(file); /* Win32 Does not have those signals */ #ifndef WIN32 signal(SIGUSR1, command_signal_handler); signal(SIGUSR2, select_signal_handler); signal(SIGABRT, abort_signal_handler); #endif #if 0 { fcs_int_limit_t limit = 500; freecell_solver_user_limit_iterations_long(instance, limit); ret = freecell_solver_user_solve_board(instance, user_state); while (ret == FCS_STATE_SUSPEND_PROCESS) { limit += 500; freecell_solver_user_limit_iterations_long(instance, limit); ret = freecell_solver_user_resume_solution(instance); } } #else ret = freecell_solver_user_solve_board(instance, user_state); #endif if (ret == FCS_STATE_INVALID_STATE) { char * error_string; error_string = freecell_solver_user_get_invalid_state_error_string( instance, debug_context.display_10_as_t ); printf("%s\n", error_string); free(error_string); error_string = NULL; } else if (ret == FCS_STATE_FLARES_PLAN_ERROR) { const char * error_string; error_string = freecell_solver_user_get_last_error_string(instance); printf("Flares Plan: %s\n", error_string); } else { FILE * output_fh; if (debug_context.output_filename) { output_fh = fopen(debug_context.output_filename, "wt"); if (! output_fh) { fprintf(stderr, "Could not open output file '%s' for writing!", debug_context.output_filename ); return -1; } } else { output_fh = stdout; } fc_solve_output_result_to_file( output_fh, instance, ret, &debug_context ); if (debug_context.output_filename) { fclose(output_fh); output_fh = NULL; } } freecell_solver_user_free(instance); return 0; }
static void haiku_init_child_debugging (thread_id threadID, bool debugThread) { thread_info threadInfo; status_t result; port_id nubPort; // get a thread info result = get_thread_info(threadID, &threadInfo); if (result != B_OK) error("Thread with ID %ld not found: %s", threadID, strerror(result)); // init our team debug structure sTeamDebugInfo.team = threadInfo.team; sTeamDebugInfo.debugger_port = -1; sTeamDebugInfo.context.nub_port = -1; sTeamDebugInfo.context.reply_port = -1; sTeamDebugInfo.threads = NULL; sTeamDebugInfo.events.head = NULL; sTeamDebugInfo.events.tail = NULL; // create the debugger port sTeamDebugInfo.debugger_port = create_port(10, "gdb debug"); if (sTeamDebugInfo.debugger_port < 0) { error("Failed to create debugger port: %s", strerror(sTeamDebugInfo.debugger_port)); } // install ourselves as the team debugger nubPort = install_team_debugger(sTeamDebugInfo.team, sTeamDebugInfo.debugger_port); if (nubPort < 0) { error("Failed to install ourselves as debugger for team %ld: %s", sTeamDebugInfo.team, strerror(nubPort)); } // get the nub thread { team_info teamInfo; result = get_team_info(sTeamDebugInfo.team, &teamInfo); if (result != B_OK) { error("Failed to get info for team %ld: %s\n", sTeamDebugInfo.team, strerror(result)); } sTeamDebugInfo.nub_thread = teamInfo.debugger_nub_thread; } // init the debug context result = init_debug_context(&sTeamDebugInfo.context, sTeamDebugInfo.team, nubPort); if (result != B_OK) { error("Failed to init debug context for team %ld: %s\n", sTeamDebugInfo.team, strerror(result)); } // start debugging the thread if (debugThread) { result = debug_thread(threadID); if (result != B_OK) { error("Failed to start debugging thread %ld: %s", threadID, strerror(result)); } } // set the team debug flags { debug_nub_set_team_flags message; message.flags = B_TEAM_DEBUG_SIGNALS /*| B_TEAM_DEBUG_PRE_SYSCALL | B_TEAM_DEBUG_POST_SYSCALL*/ | B_TEAM_DEBUG_TEAM_CREATION | B_TEAM_DEBUG_THREADS | B_TEAM_DEBUG_IMAGES; // TODO: We probably don't need all events haiku_send_debugger_message(&sTeamDebugInfo, B_DEBUG_MESSAGE_SET_TEAM_FLAGS, &message, sizeof(message), NULL, 0); } // the fun can start: push the target and init the rest push_target(sHaikuTarget); haiku_init_thread_list(&sTeamDebugInfo); haiku_init_image_list(&sTeamDebugInfo); disable_breakpoints_in_shlibs (1); // child_clear_solibs (); // TODO: Implement? Do we need this? clear_proceed_status (); init_wait_for_inferior (); target_terminal_init (); target_terminal_inferior (); }