From owner-freebsd-questions@FreeBSD.ORG Tue Dec 13 06:07:59 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 3BAE61065672 for ; Tue, 13 Dec 2011 06:07:59 +0000 (UTC) (envelope-from dan@dan.emsphone.com) Received: from email2.allantgroup.com (email2.emsphone.com [199.67.51.116]) by mx1.freebsd.org (Postfix) with ESMTP id DC5F28FC08 for ; Tue, 13 Dec 2011 06:07:58 +0000 (UTC) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by email2.allantgroup.com (8.14.4/8.14.4) with ESMTP id pBD67tJi077552 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 13 Dec 2011 00:07:55 -0600 (CST) (envelope-from dan@dan.emsphone.com) Received: from dan.emsphone.com (smmsp@localhost [127.0.0.1]) by dan.emsphone.com (8.14.5/8.14.5) with ESMTP id pBD67sur033979 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 13 Dec 2011 00:07:54 -0600 (CST) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.14.5/8.14.5/Submit) id pBD67sPE033978; Tue, 13 Dec 2011 00:07:54 -0600 (CST) (envelope-from dan) Date: Tue, 13 Dec 2011 00:07:54 -0600 From: Dan Nelson To: John Levine Message-ID: <20111213060754.GG53453@dan.emsphone.com> References: <20111213051220.45894.qmail@joyce.lan> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20111213051220.45894.qmail@joyce.lan> X-OS: FreeBSD 8.2-STABLE User-Agent: Mutt/1.5.21 (2010-09-15) X-Virus-Scanned: clamav-milter 0.97.2 at email2.allantgroup.com X-Virus-Status: Clean X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.2.6 (email2.allantgroup.com [199.67.51.78]); Tue, 13 Dec 2011 00:07:55 -0600 (CST) X-Scanned-By: MIMEDefang 2.68 on 199.67.51.78 Cc: freebsd-questions@freebsd.org Subject: Re: What's wrong with this code? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Dec 2011 06:07:59 -0000 In the last episode (Dec 13), John Levine said: > This tiny routine is in a .so loadable module I use. (It's part of the > mailfront SMTP daemon.) > > static const char* date_string(void) > { > static char datebuf[64]; > time_t now = time(0); > struct tm* tm = gmtime(&now); > strftime(datebuf, sizeof datebuf - 1, "%d %b %Y %H:%M:%S -0000", tm); > return datebuf; > } > > I was getting bogus dates. Running it under GDB, time() is returning 1, > -and setting errno to 22, which is EINVAL. Changing the call to > time to time(NULL) or time(&now) made no difference. The manpage says that time() can fail for any of the reasons described in gettimeofday(2), but time() actually calls clock_gettime(CLOCK_SECOND), which technically could return EINVAL if the first argument isn't a valid clock_id. CLOCK_SECOND is valid, though, so in practice it should never fail with EINVAL. You could try adding a printf to sys/kern/kern_time.c:kern_clock_gettime() to see if it's really failing there. > I changed it to a call to gettimeofday(), which works fine. But what > could the problem have been? When I splice this routine into a tiny test > program that calls it and prints out the result, it works fine. > > The obvious problem, since it's in a .so, is that it's linking to > something other than the system library time() function, but I did an nm > on the .so, and it said this, which sure looks like the system time() > function to me: > > U time@@FBSD_1.0 > > Setting a breakpoint in gdb gets a complaint about trying to set a > breakpoint in /lib/libc.so.7. Setting a breakpoint in a llibc should work fine, since time() is a regular function and not a syscall stub. Have you built a libc with debugging symbols? ( easy way: add DEBUG_FLAGS=-g to the top of /usr/src/lib/libc/Makefile, and run "make obj && make clean && make depend && make && make install" in that directory ) > Any ideas what the problem was? -- Dan Nelson dnelson@allantgroup.com