A real stumper

I’ve run into a puzzling problem that’s kept me stumped for several days. I have a command-line tool that simply can’t be debugged. I’ve built it with optimizations turned off and debugging symbols enabled, but it won’t stop at any breakpoints, even if I put a breakpoint at the first line of main().

Someone suggested adding the command set start-with-shell off to ~/.gdbinit, but that didn’t help. I’ve also tried running gdb from the terminal and turning off ‘start executable after starting debugger’ in XCode’s preferences, but neither one helped. It will load into gdb without starting, but no source will be shown and it will run without stopping as soon as I start it.

I figured out a snippet of code that can identify whether or not it’s running in the debugger, and it properly recognizes that it’s running in the debugger, yet it won’t stop.

For anyone interested, here’s how you can tell if you’re running in a debugger:

int AmIBeingDebugged(void)
{
        int			mib[4];
        struct kinfo_proc	info;
        size_t			size;

        mib[0] = CTL_KERN;
        mib[1] = KERN_PROC;
        mib[2] = KERN_PROC_PID;
        mib[3] = getpid();
        size = sizeof(info);
        info.kp_proc.p_flag = 0;
        sysctl(mib,4,&info,&size,NULL,0);
        return ((info.kp_proc.p_flag & P_TRACED) == P_TRACED);
}

GP2X Development Tools

I finally set up a working GP2X development environment on my Intel Mac. I’ve uploaded the build script & libraries here. The libraries won’t build cleanly on a Mac, so I built them on my Linux box.

Developing GP2X software on an Intel Mac

I spent most of the weekend building Intel native GP2X development tools using ooPo‘s build scripts. Most of it wouldn’t build as is, so I had to do lots of manual tweaking to get it to build. I finally got a working set of tools, and my next step was to hack the script so it will build cleanly with no manual hacking. That’s a lot harder, since a lot of the problems were in scripts generated from automake. I also made the build script work on PowerPC as well as Intel Macs. I’m now able to build the GP2X tools cleanly, but there are still problems building GP2X libraries.

MacIntel hacking

CAUTION: extreme geekiness ahead…

I’ve been attempting to build WINE on my iMac Core Duo. I’m just using the raw wine source code, although someone has Mac patches for it. I ran into difficulty with context_i386.c, which contains functions that obtain the register contents from a process using ptrace. Unfortunately the register structure & PT_GETREGS isn’t defined, so I’ve been looking through the system headers to find some clues.

Reading the Darwine mailing list archive, I found a pointer to this very interesting article which tells how to access the EFI command mode on Intel macs. That article gave some clues about the Mac’s startup process.

[mike: 182]$ file /usr/standalone/i386/boot.efi
/usr/standalone/i386/boot.efi: MS Windows PE 32-bit Intel 80386 executable

hmmmm…. This is the boot image that starts up the OS.

CPU Usage update

The changes I made to MacMegasite seem to have helped the CPU usage situation. It’s reduced the CPU usage by each execution of PHP.CGI by at least 50% as well as reducing the overall CPU usages. Here are the resource usage stats for the last few days, only counting CPU usage by PHP.CGI.

Date Total CPU time # php.cgi Avg CPU
12/05 3780.4000 32005 0.118
12/04 4786.4100 32526 0.147
12/03 3713.8700 31634 0.117
12/02 3720.8000 30562 0.122
12/01 4409.1400 17624 0.250
12/30 5685.9700 21089 0.270
12/29 5281.2200 21208 0.249

Lightweight Drupal home page

I’ve cleaned up my code for MacMegasite’s new page by breaking it up into separate include files for the left & right sidebars. The entire code is less than 50 lines and uses only a single database query and no calls to Drupal code.

Here’s the full code, not including the style sheet and sidebar content:

<html>
<head>
<meta http-equiv=content-type content="text/html; charset=utf-8">
<title>MacMegasite</title>
<style type="text/css" title="text/css">
<!--
@import url(INC/style.css);
-->
</style>
</head>
<body>
<img src="INC/logo.gif" class=logo>
<br>

<div class="left">
<?php include "INC/left.inc" ?>
</div>

<div class="right">
<?php include "INC/right.inc" ?>
</div>

<table class=content>
<?php
$dbname="your_database_name";
$host="your_host_name";
$user="your_user_name";
$pass="your_password";

$dbi = mysql_connect($host,$user,$pass);
mysql_select_db($dbname);

$query = "select nid, title, teaser from node where status=1 and promote=1 order by nid desc limit 10";
$res = mysql_query($query,$dbi);
while (list($nid,$title,$teaser) = mysql_fetch_row($res)) {
   print "<tr><td>";
   print "<h1><a href='http://www.macmegasite.com/drupal/node/".$nid."'>".$title."</a></h1>\n";
   print $teaser;
   print "<hr width=75% align=center><br>\n</td></tr>\n";
}
?>
</table>

</body>
</html>

Random Image Theme

I’m still getting lots of email about my random image theme, so I’ve added a lot more information here explaining how it works and how to modify it. I’ll probably add a link to that page somewhere.

Porting to Intel V

For any networking code, make sure you’re using htonl & htons when building a socket address structure. If you look at the structure you’re passing to connect(), the IP address & port should appear reversed. Dealing with these byte ordering issues can be very confusing. Always be aware of what byte order is expected for any data used externally or passed to system calls.