From owner-p4-projects@FreeBSD.ORG Fri Aug 20 13:58:39 2004 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 7528F16A4D0; Fri, 20 Aug 2004 13:58:39 +0000 (GMT) Delivered-To: perforce@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4F54216A4CE for ; Fri, 20 Aug 2004 13:58:39 +0000 (GMT) Received: from repoman.freebsd.org (repoman.freebsd.org [216.136.204.115]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3226D43D3F for ; Fri, 20 Aug 2004 13:58:39 +0000 (GMT) (envelope-from davidxu@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.12.11/8.12.11) with ESMTP id i7KDwdTR021814 for ; Fri, 20 Aug 2004 13:58:39 GMT (envelope-from davidxu@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.12.11/8.12.11/Submit) id i7KDwcw1021811 for perforce@freebsd.org; Fri, 20 Aug 2004 13:58:38 GMT (envelope-from davidxu@freebsd.org) Date: Fri, 20 Aug 2004 13:58:38 GMT Message-Id: <200408201358.i7KDwcw1021811@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to davidxu@freebsd.org using -f From: David Xu To: Perforce Change Reviews Subject: PERFORCE change 60134 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 20 Aug 2004 13:58:40 -0000 http://perforce.freebsd.org/chv.cgi?CH=60134 Change 60134 by davidxu@davidxu_alona on 2004/08/20 13:57:38 Adjust code to support statically linked thread binary. Affected files ... .. //depot/projects/davidxu_ksedbg/src/gnu/usr.bin/gdb/libgdb/fbsd-threads.c#19 edit Differences ... ==== //depot/projects/davidxu_ksedbg/src/gnu/usr.bin/gdb/libgdb/fbsd-threads.c#19 (text+ko) ==== @@ -46,16 +46,19 @@ pid_t pid; }; +extern int child_suppress_run; + extern struct target_ops child_ops; /* This module's target vector. */ static struct target_ops thread_db_ops; -static struct target_ops base_ops; - /* Pointer to the next function on the objfile event chain. */ static void (*target_new_objfile_chain) (struct objfile *objfile); +/* Non-zero if there is a thread module */ +static int fbsd_thread_present; + /* Non-zero if we're using this module's target vector. */ static int using_thread_db; @@ -78,6 +81,7 @@ static td_err_e (*td_init_p) (void); static td_err_e (*td_ta_new_p) (struct ps_prochandle *ps, td_thragent_t **ta); +static td_err_e (*td_ta_delete_p) (td_thragent_t *); static td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt, td_thrhandle_t *__th); static td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta, lwpid_t lwpid, @@ -119,6 +123,7 @@ /* Prototypes for local functions. */ static void fbsd_thread_find_new_threads (void); static int fbsd_thread_alive (ptid_t ptid); +static void fbsd_thread_activate(void); /* Building process ids. */ @@ -222,13 +227,16 @@ gdb_assert (IS_LWP (ptid)); - err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th); - if (err == TD_OK) + if (using_thread_db) { - err = td_thr_get_info_p (&th, &ti); - if (err != TD_OK) - error ("Cannot get thread info: %s", thread_db_err_str (err)); - return BUILD_THREAD (ti.ti_tid, GET_PID (ptid)); + err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th); + if (err == TD_OK) + { + err = td_thr_get_info_p (&th, &ti); + if (err != TD_OK) + error ("Cannot get thread info: %s", thread_db_err_str (err)); + return BUILD_THREAD (ti.ti_tid, GET_PID (ptid)); + } } /* the LWP is not mapped to user thread */ @@ -269,22 +277,22 @@ /* Don't attempt to use thread_db on targets which can not run (core files). */ - if (objfile == NULL || !target_has_execution) + if (objfile == NULL) { /* All symbols have been discarded. If the thread_db target is active, deactivate it now. */ if (using_thread_db) { gdb_assert (proc_handle.pid == 0); - unpush_target (&thread_db_ops); using_thread_db = 0; } - keep_thread_db = 0; - goto quit; } + if (!child_suppress_run) + goto quit; + if (using_thread_db) /* Nothing to do. The thread library was already detected and the target vector was already activated. */ @@ -305,32 +313,22 @@ case TD_OK: /* The thread library was detected. Activate the thread_db target. */ - base_ops = current_target; - push_target (&thread_db_ops); - using_thread_db = 1; + fbsd_thread_present = 1; - /* If the thread library was detected in the main symbol file - itself, we assume that the program was statically linked - against the thread library and well have to keep this - module's target vector activated until forever... Well, at - least until all symbols have been discarded anyway (see - above). */ - if (objfile == symfile_objfile) - { - gdb_assert (proc_handle.pid == 0); - keep_thread_db = 1; - } - /* We can only poke around if there actually is a child process. If there is no child process alive, postpone the steps below until one has been created. */ if (proc_handle.pid != 0) { - fbsd_thread_find_new_threads (); - get_current_thread (); + push_target(&thread_db_ops); + fbsd_thread_activate(); } else - printf_filtered("%s postpone processing\n", __func__); + { + td_ta_delete_p(thread_agent); + thread_agent = NULL; + printf_filtered("%s postpone processing\n", __func__); + } break; default: @@ -403,9 +401,9 @@ GET_THREAD(inferior_ptid)); #endif - if (proc_handle.pid == 0) + if (!using_thread_db) { - base_ops.to_resume (ptid, step, signo); + child_ops.to_resume (ptid, step, signo); return; } @@ -502,7 +500,7 @@ ret = child_ops.to_wait (ptid, ourstatus); if (GET_PID(ret) >= 0 && ourstatus->kind == TARGET_WAITKIND_STOPPED) { - lwp = get_current_lwp (proc_handle.pid); + lwp = get_current_lwp (GET_PID(ret)); ret = thread_from_lwp (BUILD_LWP (lwp, GET_PID (ret))); if (!in_thread_list (ret)) add_thread (ret); @@ -525,7 +523,7 @@ fbsd_thread_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, struct mem_attrib *attrib, struct target_ops *target) { - return base_ops.to_xfer_memory (memaddr, myaddr, len, write, + return child_ops.to_xfer_memory (memaddr, myaddr, len, write, attrib, target); } @@ -536,7 +534,6 @@ fpregset_t fpregs; lwpid_t lwp; - /* FIXME, use base_ops to fetch lwp registers! */ lwp = GET_LWP (inferior_ptid); if (ptrace (PT_GETREGS, lwp, (caddr_t) &gregs, 0) == -1) @@ -675,14 +672,26 @@ child_ops.to_kill(); } +static int +fbsd_thread_can_run (void) +{ + return child_suppress_run; +} + static void +fbsd_thread_activate (void) +{ + using_thread_db = 1; + init_thread_list(); + fbsd_thread_find_new_threads (); + get_current_thread (); +} + +static void fbsd_thread_create_inferior (char *exec_file, char *allargs, char **env) { - if (!keep_thread_db) - { - unpush_target (&thread_db_ops); - using_thread_db = 0; - } + if (fbsd_thread_present && !using_thread_db) + push_target(&thread_db_ops); child_ops.to_create_inferior (exec_file, allargs, env); } @@ -690,16 +699,15 @@ static void fbsd_thread_post_startup_inferior (ptid_t ptid) { - if (proc_handle.pid == 0) + if (fbsd_thread_present && !using_thread_db) { /* * The child process is now the actual multi-threaded * program. Snatch its process ID... */ proc_handle.pid = GET_PID (ptid); - - fbsd_thread_find_new_threads (); - get_current_thread (); + td_ta_new_p (&proc_handle, &thread_agent); + fbsd_thread_activate(); } } @@ -745,14 +753,17 @@ return 1; } - err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th); + if (using_thread_db) + { + err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th); - /* - * if the lwp was already mapped to user thread, don't use it - * directly, please use user thread id instead. - */ - if (err == TD_OK) - return 0; + /* + * if the lwp was already mapped to user thread, don't use it + * directly, please use user thread id instead. + */ + if (err == TD_OK) + return 0; + } /* check lwp in kernel */ return ptrace (PT_GETREGS, GET_LWP (ptid), (caddr_t)&gregs, 0) == 0; @@ -786,6 +797,9 @@ { td_err_e err; + if (!using_thread_db) + return; + /* Iterate over all user-space threads to discover new threads. */ err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL, TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, @@ -908,7 +922,8 @@ static void fbsd_thread_tsd_cmd (char *exp, int from_tty) { - td_ta_tsd_iter_p (thread_agent, tsd_cb, NULL); + if (using_thread_db) + td_ta_tsd_iter_p (thread_agent, tsd_cb, NULL); } static void @@ -928,11 +943,17 @@ thread_db_ops.to_create_inferior = fbsd_thread_create_inferior; thread_db_ops.to_post_startup_inferior = fbsd_thread_post_startup_inferior; thread_db_ops.to_mourn_inferior = fbsd_thread_mourn_inferior; + thread_db_ops.to_can_run = fbsd_thread_can_run; thread_db_ops.to_thread_alive = fbsd_thread_alive; thread_db_ops.to_find_new_threads = fbsd_thread_find_new_threads; thread_db_ops.to_pid_to_str = fbsd_thread_pid_to_str; thread_db_ops.to_stratum = thread_stratum; thread_db_ops.to_has_thread_control = tc_none; + thread_db_ops.to_has_all_memory = 1; + thread_db_ops.to_has_memory = 1; + thread_db_ops.to_has_stack = 1; + thread_db_ops.to_has_registers = 1; + thread_db_ops.to_has_execution = 1; thread_db_ops.to_insert_breakpoint = memory_insert_breakpoint; thread_db_ops.to_remove_breakpoint = memory_remove_breakpoint; thread_db_ops.to_get_thread_local_address = fbsd_thread_get_local_address; @@ -957,6 +978,10 @@ if (td_ta_new_p == NULL) return 0; + td_ta_delete_p = dlsym (handle, "td_ta_delete"); + if (td_ta_delete_p == NULL) + return 0; + td_ta_map_id2thr_p = dlsym (handle, "td_ta_map_id2thr"); if (td_ta_map_id2thr_p == NULL) return 0; @@ -1045,6 +1070,7 @@ { printf_filtered("%s: can not load %s.\n", __func__, LIBTHREAD_DB_SO); } + child_suppress_run = 1; } /* proc service functions */ @@ -1075,14 +1101,16 @@ ps_err_e ps_pread (struct ps_prochandle *ph, psaddr_t addr, void *buf, size_t len) { - return target_read_memory ((CORE_ADDR) addr, buf, len); + int err = target_read_memory ((CORE_ADDR) addr, buf, len); + return (err == 0 ? PS_OK : PS_ERR); } ps_err_e ps_pwrite (struct ps_prochandle *ph, psaddr_t addr, const void *buf, size_t len) { - return target_write_memory ((CORE_ADDR) addr, (void *)buf, len); + int err = target_write_memory ((CORE_ADDR) addr, (void *)buf, len); + return (err == 0 ? PS_OK : PS_ERR); } ps_err_e