public inbox for insight@sourceware.org
 help / color / mirror / Atom feed
* (patch) g77 helper
@ 1999-08-26 22:39 Mumit Khan
  1999-08-30 11:07 ` James Ingham
  0 siblings, 1 reply; 3+ messages in thread
From: Mumit Khan @ 1999-08-26 22:39 UTC (permalink / raw)
  To: insight

When you start debugging a g77 generated program, you'll have the 
somewhat disconcerting experience of jumping into main, and stare
at what is typically a bunch of startup-style assembly code.

This trivial patch checks for MAIN__ and MAIN___ (depends on the target's
underscoring habits, but only MAIN__ may suffice) before main. Tested
on Linux and Cygwin. Could someone check on HPUX (no underscoring case)
to see what g77 emits there?

Against ss-1999-08-23.

Fri Aug 27 00:07:10 1999  Mumit Khan  <khan@xraylith.wisc.edu>

	* srcwin.itb (SrcWin::_build_win): Check for g77 MAIN before
	checking for main.
	(SrcWin::location): Likewise.
	(SrcWin::point_to_main): Likewise.
	* interface.tcl (run_executable): Likewise.

--- srcwin.itb.~1	Thu Aug 26 23:48:34 1999
+++ srcwin.itb	Fri Aug 27 00:09:38 1999
@@ -144,7 +144,9 @@ body SrcWin::_build_win {} {
   if {$gdb_running} {
     update
   } else {
-    if {![catch {gdb_loc main} linespec]} {
+    if {![catch {gdb_loc MAIN__} linespec] \
+        || ![catch {gdb_loc MAIN___} linespec] \
+        || ![catch {gdb_loc main} linespec]} {
       location BROWSE_TAG $linespec
     }
   }
@@ -420,7 +422,9 @@ body SrcWin::location {tag linespec} {
     set tag BROWSE_TAG
     debug "not running: name=$name funcname=$funcname line=$line"
     if {$name == ""} {
-      if {[catch {gdb_loc main} linespec]} {
+      if {[catch {gdb_loc MAIN__} linespec] \
+          && [catch {gdb_loc MAIN___} linespec] \
+          && [catch {gdb_loc main} linespec]} {
 	# no "main" function found
 	return 
       }
@@ -765,7 +769,9 @@ body SrcWin::point_to_main {} {
   # We need to force this to some default location. Assume main and
   # if that fails, let the source window guess (via gdb_loc using stop_pc).
   set src [lindex [ManagedWin::find SrcWin] 0]
-  if {[catch {gdb_loc main} loc]} {
+  if {[catch {gdb_loc MAIN__} loc] \
+      && [catch {gdb_loc MAIN___} loc] \
+      && [catch {gdb_loc main} loc]} {
     gdbtk_update
     debug "could not find main"
   } else {
--- interface.tcl.~1	Fri Aug 27 00:23:36 1999
+++ interface.tcl	Fri Aug 27 00:28:51 1999
@@ -964,8 +964,16 @@ proc run_executable { {auto_start 1} } {
       
     if {[pref get gdb/load/main]} {
       debug "Setting new BP at main"
+      catch {gdb_cmd "clear MAIN__"}
+      catch {gdb_cmd "clear MAIN___"}
       catch {gdb_cmd "clear main"}
-      catch {gdb_cmd "break main"}
+      # For fortran, we try MAIN__ and MAIN__ first.
+      if {![catch {gdb_loc MAIN__}] || ![catch {gdb_loc MAIN__}]} {
+	catch {gdb_cmd "break MAIN__"}
+	catch {gdb_cmd "break MAIN___"}
+      } else {
+	catch {gdb_cmd "break main"}
+      }
     }
 
     # set BP at user-specified function

Regards,
Mumit

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: (patch) g77 helper
  1999-08-26 22:39 (patch) g77 helper Mumit Khan
@ 1999-08-30 11:07 ` James Ingham
  1999-08-30 13:47   ` Mumit Khan
  0 siblings, 1 reply; 3+ messages in thread
From: James Ingham @ 1999-08-30 11:07 UTC (permalink / raw)
  To: Mumit Khan; +Cc: insight

Mumit,

We have a similar problem with Java, where the real "main" is the main 
method of the class that you told gcj was the main class of your
application.  There is a C main that gcj cooks up, and which Java
programmers definitely didn't want to know about...

The real solution is to have the compiler be able to emit some debug
information that would tell gdb what the user visible main is.  I am
kind of leery of starting to hard-code heuristics for all the various cases
of "real" mains that might crop up. 

However, given that we don't have a good "entry point" symbol right
now, it is okay to guess in gdbtk.  However, if we are going to do
this, then it would probably be better to abstract it a little bit,
and have a list of potential main's in order, and read it out of the
preferences file, rather than hard coding a set of choices.  So
something like putting:

pref define gdb/main_names  [list MAIN__ MAIN_ main]

in prefs.tcl

and then getting this list with "pref get", and doing a foreach in
your first srcwin.itb modification.  Also, it is probably better to do 

gdb_search functions -static 1 MAIN__

and then do gdb_loc on what you found.  This is faster, and more
straight-forward.

Using the preferences will make it clearer, and more adaptable, and if
someone has some bizarre case, they can go edit the prefs file and add
their case.  We could even stick this in the preferences dialog
somewhere.

You should also remember what main you found, probably in a global
variable, though it might be good to put a little wrapper fn. in
interface.tcl to get and set it.  That is better than again just
blindly setting and unsetting the list...

What do you think?

Jim

 > When you start debugging a g77 generated program, you'll have the 
 > somewhat disconcerting experience of jumping into main, and stare
 > at what is typically a bunch of startup-style assembly code.
 > 
 > This trivial patch checks for MAIN__ and MAIN___ (depends on the target's
 > underscoring habits, but only MAIN__ may suffice) before main. Tested
 > on Linux and Cygwin. Could someone check on HPUX (no underscoring case)
 > to see what g77 emits there?
 > 
 > Against ss-1999-08-23.
 > 
 > Fri Aug 27 00:07:10 1999  Mumit Khan  <khan@xraylith.wisc.edu>
 > 
 > 	* srcwin.itb (SrcWin::_build_win): Check for g77 MAIN before
 > 	checking for main.
 > 	(SrcWin::location): Likewise.
 > 	(SrcWin::point_to_main): Likewise.
 > 	* interface.tcl (run_executable): Likewise.
 > 
 > --- srcwin.itb.~1	Thu Aug 26 23:48:34 1999
 > +++ srcwin.itb	Fri Aug 27 00:09:38 1999
 > @@ -144,7 +144,9 @@ body SrcWin::_build_win {} {
 >    if {$gdb_running} {
 >      update
 >    } else {
 > -    if {![catch {gdb_loc main} linespec]} {
 > +    if {![catch {gdb_loc MAIN__} linespec] \
 > +        || ![catch {gdb_loc MAIN___} linespec] \
 > +        || ![catch {gdb_loc main} linespec]} {
 >        location BROWSE_TAG $linespec
 >      }
 >    }
 > @@ -420,7 +422,9 @@ body SrcWin::location {tag linespec} {
 >      set tag BROWSE_TAG
 >      debug "not running: name=$name funcname=$funcname line=$line"
 >      if {$name == ""} {
 > -      if {[catch {gdb_loc main} linespec]} {
 > +      if {[catch {gdb_loc MAIN__} linespec] \
 > +          && [catch {gdb_loc MAIN___} linespec] \
 > +          && [catch {gdb_loc main} linespec]} {
 >  	# no "main" function found
 >  	return 
 >        }
 > @@ -765,7 +769,9 @@ body SrcWin::point_to_main {} {
 >    # We need to force this to some default location. Assume main and
 >    # if that fails, let the source window guess (via gdb_loc using stop_pc).
 >    set src [lindex [ManagedWin::find SrcWin] 0]
 > -  if {[catch {gdb_loc main} loc]} {
 > +  if {[catch {gdb_loc MAIN__} loc] \
 > +      && [catch {gdb_loc MAIN___} loc] \
 > +      && [catch {gdb_loc main} loc]} {
 >      gdbtk_update
 >      debug "could not find main"
 >    } else {
 > --- interface.tcl.~1	Fri Aug 27 00:23:36 1999
 > +++ interface.tcl	Fri Aug 27 00:28:51 1999
 > @@ -964,8 +964,16 @@ proc run_executable { {auto_start 1} } {
 >        
 >      if {[pref get gdb/load/main]} {
 >        debug "Setting new BP at main"
 > +      catch {gdb_cmd "clear MAIN__"}
 > +      catch {gdb_cmd "clear MAIN___"}
 >        catch {gdb_cmd "clear main"}
 > -      catch {gdb_cmd "break main"}
 > +      # For fortran, we try MAIN__ and MAIN__ first.
 > +      if {![catch {gdb_loc MAIN__}] || ![catch {gdb_loc MAIN__}]} {
 > +	catch {gdb_cmd "break MAIN__"}
 > +	catch {gdb_cmd "break MAIN___"}
 > +      } else {
 > +	catch {gdb_cmd "break main"}
 > +      }
 >      }
 >  
 >      # set BP at user-specified function
 > 
 > Regards,
 > Mumit
 > 
 > 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: (patch) g77 helper
  1999-08-30 11:07 ` James Ingham
@ 1999-08-30 13:47   ` Mumit Khan
  0 siblings, 0 replies; 3+ messages in thread
From: Mumit Khan @ 1999-08-30 13:47 UTC (permalink / raw)
  To: James Ingham; +Cc: insight

James Ingham <jingham@cygnus.com> writes:
> 
> Using the preferences will make it clearer, and more adaptable, and if
> someone has some bizarre case, they can go edit the prefs file and add
> their case.  We could even stick this in the preferences dialog
> somewhere.
> 
> You should also remember what main you found, probably in a global
> variable, though it might be good to put a little wrapper fn. in
> interface.tcl to get and set it.  That is better than again just
> blindly setting and unsetting the list...
> 

Jim, Thanks for the feedback. How about the following patch? 

I'm not setting a global since I haven't looked at the code enough to 
see what needs to be reinitialized when a new file is loaded etc. In 
theory, caching the name of the main and then re-using it should speed
up parts of the code, but I need to study the code a bit to see where
it needs to be reset correctly. Since this change isn't that much
slower than the existing code, I'll punt on this until I get some free
time. There are only so many weekends in a week ;-)

Mon Aug 30 15:21:02 1999  Mumit Khan  <khan@xraylith.wisc.edu>

	* prefs.tcl (pref_set_defaults): Add main_names preference.
	* interface.tcl (gdbtk_locate_main): New proc.
	(run_executable): Use.
	* srcwin.itb (SrcWin::_build_win): Use.
	(SrcWin::location): Likewise.
	(SrcWin::point_to_main): Likewise.


--- prefs.tcl.~1	Mon Aug 30 14:50:23 1999
+++ prefs.tcl	Mon Aug 30 14:52:13 1999
@@ -322,6 +322,9 @@ proc pref_set_defaults {} {
 
   # Kernel Objects (kod)
   pref define gdb/kod/show_icon           0
+
+  # Various possible "main" functions. What's for Java?
+  pref define gdb/main_names              [list MAIN___ MAIN__ main]
 }
 
 # This traces the global/fixed font and forces src-font to
--- interface.tcl.~1	Mon Aug 30 15:26:33 1999
+++ interface.tcl	Mon Aug 30 15:40:08 1999
@@ -647,6 +647,35 @@ proc gdbtk_tcl_exec_file_display {filena
   SrcWin::point_to_main
 }
 
+# ------------------------------------------------------------------
+#  PROCEDURE: gdbtk_locate_main 
+#         This proc tries to locate a suitable main function from
+#         a list of names defined in the gdb/main_names preference; 
+#         returns the linespec (see below) if found, or raises error.
+#
+#  The return linespec looks like this:
+#  0: basename of the file
+#  1: function name
+#  2: full filename
+#  3: source line number
+#  4: address
+#  5: current PC - which will often be the same as address, but not when
+#  6: shared library name if the pc is in a shared lib
+#  we are browsing, or walking the stack.
+#
+# ------------------------------------------------------------------
+proc gdbtk_locate_main {} {
+  set main_names [pref get gdb/main_names]
+  debug "gdbtk_locate_main: Searching $main_names"
+  foreach main $main_names {
+    if {![catch {gdb_search functions $main -static 1}] \
+        && ![catch {gdb_loc $main} linespec]} {
+      return $linespec
+    }
+  }
+  error "cannot locate a suitable main"
+}
+
 ##############################################
 #  The rest of this file is an assortment of Tcl wrappers
 #  for various bits of gdb functionality.
@@ -968,9 +997,13 @@ proc run_executable { {auto_start 1} } {
     }
       
     if {[pref get gdb/load/main]} {
-      debug "Setting new BP at main"
-      catch {gdb_cmd "clear main"}
-      catch {gdb_cmd "break main"}
+      set main "main"
+      if {![catch {gdbtk_locate_main} linespec]} {
+        set main [lindex $linespec 1]
+      }
+      debug "Setting new BP at $main"
+      catch {gdb_cmd "clear $main"}
+      catch {gdb_cmd "break $main"}
     }
 
     # set BP at user-specified function
@@ -1420,3 +1453,4 @@ proc initialize_gdbtk {} {
   # to be displayed on the first "run"
   set gdb_target_changed 1
 }
+
--- srcwin.itb.~1	Mon Aug 30 14:52:49 1999
+++ srcwin.itb	Mon Aug 30 15:32:10 1999
@@ -144,7 +144,7 @@ body SrcWin::_build_win {} {
   if {$gdb_running} {
     update
   } else {
-    if {![catch {gdb_loc main} linespec]} {
+    if {![catch {gdbtk_locate_main} linespec]} {
       location BROWSE_TAG $linespec
     }
   }
@@ -420,7 +420,7 @@ body SrcWin::location {tag linespec} {
     set tag BROWSE_TAG
     debug "not running: name=$name funcname=$funcname line=$line"
     if {$name == ""} {
-      if {[catch {gdb_loc main} linespec]} {
+      if {[catch {gdbtk_locate_main} linespec]} {
 	# no "main" function found
 	return 
       }
@@ -765,7 +765,7 @@ body SrcWin::point_to_main {} {
   # We need to force this to some default location. Assume main and
   # if that fails, let the source window guess (via gdb_loc using stop_pc).
   set src [lindex [ManagedWin::find SrcWin] 0]
-  if {[catch {gdb_loc main} loc]} {
+  if {[catch {gdbtk_locate_main} loc]} {
     gdbtk_update
     debug "could not find main"
   } else {

Regards,
Mumit

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1999-08-30 13:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-08-26 22:39 (patch) g77 helper Mumit Khan
1999-08-30 11:07 ` James Ingham
1999-08-30 13:47   ` Mumit Khan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).