Search code examples
gdbgetopt

problems debugging getopt function with gdb


I'm building the dnstracer application from source with this script:

#!/bin/bash
#########
# FILES #
#########
PACKAGE_NAME=dnstracer-1.6
PACKAGE_TAR_FILE=${PACKAGE_NAME}.tar.gz    
PACKAGE_FTP_SITE=http://ftp.iij.ad.jp/pub/linux/momonga/1/PKGS/SOURCES/

####################################
# REMOVE OLD STUFF JUST TO BE SURE #
####################################
rm -rf build
rm -rf ${PACKAGE_NAME}
rm -rf ${PACKAGE_TAR_FILE}

#####################################
# Get source code for buggy package #
#####################################
wget ${PACKAGE_FTP_SITE}/${PACKAGE_TAR_FILE}

######################
# Unpack it here ... #
######################
tar xf ${PACKAGE_TAR_FILE}

##########################################
# Prepare an out of tree build directory #
##########################################
mkdir build

##############################
# Get inside build directory #
##############################
cd build

#################
# Configure ... #
#################
../${PACKAGE_NAME}/configure

###############
# Make it !!! #
###############
make -j

And I see it ships with its own getopt, which is good for me because I need to debug it:

$ ls -l ./dnstracer-1.6/getopt.*
./dnstracer-1.6/getopt.c
./dnstracer-1.6/getopt.h

However when I try to step inside getopt from gdb, I realize that it probably has some other getopt (maybe without debug symbols?) and it doesn't let me step inside:

$ cd build
$ gdb --args ./dnstracer -v aaaaaa
$ (gdb) break main
$ (gdb) run
$ (gdb) next
$ 1304 while ((ch=getopt(argc,argv,"coq:r:s:t:v"))!=-1) {
$ step
$ 1305 switch (ch) {

How can I configure the build process to use the shipped getopt version rather than some hidden default? Thanks!


Solution

  • And I see it ships with its own getopt, which is good for me because I need to debug it:

    Since you are on Ubuntu this is not the case for you, see in shipped getopt.h:

    // Only used in the win32-version of dnstracer.
    // Supplied by Mike Black <mblack@csihq.com>
    

    Therefore you are using system getopt which is part of glibc. In order to step into getopt you need glibc debug symbols installed. See https://stackoverflow.com/a/48287761/72178 on how to debug glibc on Ubuntu.