# Copyright 2002 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  

# This file was written by Jason Molenda (jmolenda@apple.com)


# These were written when we were having a problem with gcc not handling
# instance variables for an object whose debug info resides in a shared
# library.  These tests never did reproduce the problem, but they do some
# useful shlib handling things, so they remain.
# There are two versions of the executable - ivars-static and ivars-dynamic.

# ivars-static links against the shared library at link-time (ld), so gdb
# can viably get the dynamic info before it starts execution.  Despite the
# name, nothing is "statically linked" as you'd know it on an ELF system;
# it refers to the static link edtor, aka ld.  The shlib is named on the
# final gcc command line, so ld knows about it and records information
# about it in the executable.

# ivars-dynamic links against the shared library at run-time, so gdb has
# to notice the shared library load message from the dynamic run-time linker
# and insert the breakpoint correctly.  The shared library was unmentioned
# at static link time (ld), so there's no information about it in the
# executable.


if $tracelevel then {
	strace $tracelevel
}

set timeout 30
set prms_id 0
set bug_id 0

# APPLE LOCAL jmolenda
# r. 3094426 gdb future-break on selector does not work with hand-loaded shlib
set r3094426 "(r. 3094426)"

set libfile "libivars"
set libbinfile ${objdir}/${subdir}/${libfile}.dylib

set staticfile "ivars-static"
set staticsrcfile ${staticfile}.m
set staticbinfile ${objdir}/${subdir}/${staticfile}

set dynamicfile "ivars-dynamic"
set dynamicsrcfile ${dynamicfile}.m
set dynamicbinfile ${objdir}/${subdir}/${dynamicfile}

proc get_libsystem_load_addr { } {
   global decimal
   global hex
   global gdb_prompt
   global libsystem_load_addr

   set libsystem_load_addr -1

   gdb_exit
   gdb_start
   gdb_load "/bin/echo"
   send_gdb "info sharedlibrary\n"
   gdb_expect {
     -re ".*$decimal libSystem...dylib - - *init Y Y \"\[^\"\]+libSystem...dylib\" at ($hex).*$gdb_prompt $" {
       set libsystem_load_addr $expect_out(1,string)
     }
     -re "$gdb_prompt $" { }
     timeout { }
   }
   return 0
}


proc build_library { args } {
   global srcdir
   global subdir
   global libfile
   global libbinfile

   set more_opts [lindex $args 0]
   set additional_flags "additional_flags=-dynamiclib $more_opts -framework Foundation"
if     { [gdb_compile "${srcdir}/${subdir}/${libfile}.m" "${libbinfile}" executable [list debug $additional_flags]] != "" } {
        gdb_suppress_entire_file "Testcase library compile failed, so all tests in this file will automatically fail."
   }

   return 0
}

proc test_run { args } {
   # Start with a fresh gdb
   global srcdir
   global subdir
   global staticbinfile
   global dynamicbinfile
   global gdb_prompt
 
   set run_name [lindex $args 0]

   gdb_exit
   gdb_start
   gdb_reinitialize_dir $srcdir/$subdir
   gdb_load ${staticbinfile}
   gdb_test "show lang" "The current source language is \"auto; currently objective-c\"." "obj-c lang detection ($run_name) (static prog)"
   gdb_test "info sharedlibrary" ".*libivars.dylib.*init Y Y.*libivars.dylib.*" "info sharedlibrary pre-start ($run_name) (static prog)"
   gdb_test "future-break randomFunc" ".*Breakpoint 1.*" "Set object breakpoint ($run_name) (static prog)"
   send_gdb "run\n"
   set stopped "no"
   gdb_expect {
     -re ".*Breakpoint 1, -\\\[MyClass randomFunc\\\].*libivars.m.*$gdb_prompt $" {
       pass "continue to obj bp ($run_name) (static prog)"
       set stopped "yes"
     }
     -re ".*Program exited.*" {
       fail "continue to obj bp: Program exited ($run_name) (static prog)"
       set stopped "no"
     }
     -re ".*internal-error: assertion failure in function.*Quit this debugging session. .y or n. $" {
       fail "continue to obj bp: assertion error! ($run_name) (static prog)"
       set stopped "no"
     }
     timeout {
       fail "(timeout) continue to obj bp ($run_name) (static prog)"
       set stopped "yes"
     }
   }
   if {$stopped == "yes"} {
     gdb_test "show lang" "The current source language is \"auto; currently objective-c\"." "obj-c lang detection at breakpoint (static prog)"
     gdb_test "po object" "hi there" "print ivar object #1 ($run_name) (static prog)"
     gdb_test "po _object2" "hi there" "print ivar object #2 ($run_name) (static prog)"
     gdb_test "po self->object" "hi there" "print ivar object #3 ($run_name) (static prog)"
     gdb_test "po self->_object2" "hi there" "print ivar object #4 ($run_name) (static prog)"
     gdb_test "info sharedlibrary" ".*Foundation.*" "quick post-run info shlib check to help debugging ($run_name) (static prog)"
   }

   gdb_exit


   gdb_start
   gdb_reinitialize_dir $srcdir/$subdir
   gdb_load ${dynamicbinfile}
   gdb_test "show lang" "The current source language is \"auto; currently objective-c\"." "obj-c lang detection pre-run (dynamic prog)"
   gdb_test "info sharedlibrary" ".*"
   gdb_test "future-break randomFunc" ".*Breakpoint 1.*" "Set object breakpoint ($run_name) (dynamic prog)"
   send_gdb "run\n"
   set stopped "no"
   gdb_expect {
     -re ".*Breakpoint 1, -\\\[MyClass randomFunc\\\].*libivars.m.*$gdb_prompt $" {
       pass "continue to obj bp ($run_name) (dynamic prog)"
       set stopped "yes"
     }
     -re ".*Program exited.*" {
       fail "continue to obj bp: Program exited ($run_name) (dynamic prog) (r. 3094426)"
       set stopped "no"
     }
     -re ".*internal-error: assertion failure in function.*Quit this debugging session. .y or n. $" {
       fail "continue to obj bp: assertion error! ($run_name) (dynamic prog)"
       set stopped "no"
     }
     timeout {
       fail "(timeout) continue to obj bp ($run_name) (dynamic prog)"
       set stopped "yes"
     }
   }
   if {$stopped == "yes"} {
     gdb_test "show lang" "The current source language is \"auto; currently objective-c\"." "obj-c lang detection at breakpoint (dynamic prog)"
     gdb_test "po object" "hi there" "print ivar object #1 ($run_name) (dynamic prog)"
     gdb_test "po _object2" "hi there" "print ivar object #2 ($run_name) (dynamic prog)"
     gdb_test "po self->object" "hi there" "print ivar object #3 ($run_name) (dynamic prog)"
     gdb_test "po self->_object2" "hi there" "print ivar object #4 ($run_name) (dynamic prog)"
     gdb_test "info sharedlibrary" ".*Foundation.*" "quick post-run info shlib check to help debugging ($run_name) (dynamic_prog)"
   }
   
   gdb_exit

   return 0
}

build_library ""

# Build ivars-static

set additional_flags "additional_flags=-framework Foundation -L$objdir/$subdir -livars"
if  { [gdb_compile "${srcdir}/${subdir}/$staticsrcfile" "${staticbinfile}" executable [list debug $additional_flags]] != "" } {
     gdb_suppress_entire_file "Testcase executable compile failed, so all tests in this file will automatically fail."
}

# build ivars-dynamic

set additional_flags "additional_flags=-framework Foundation -DLIBNAME=\"${libbinfile}\""
if  { [gdb_compile "${srcdir}/${subdir}/$dynamicsrcfile" "${dynamicbinfile}" executable [list debug $additional_flags]] != "" } {
     gdb_suppress_entire_file "Testcase executable compile failed, so all tests in this file will automatically fail."
}

test_run "dylib no addr specified"

build_library "-seg1addr 0x0"
test_run "dylib slide from 0x0"

get_libsystem_load_addr
build_library "-seg1addr $libsystem_load_addr"
test_run "dylib slide from $libsystem_load_addr"

build_library "-seg1addr  0x500000"
test_run "dylib maybe without a slide"

return 0
