libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 2008-2010, 2013 Peter Miller 00004 * Written by Peter Miller <pmiller@opensource.org.au> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as 00008 * published by the Free Software Foundation; either version 3 of the 00009 * License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include <libexplain/ac/dlfcn.h> 00021 00022 #include <libexplain/buffer/pointer.h> 00023 #include <libexplain/option.h> 00024 00025 00026 void 00027 explain_buffer_pointer(explain_string_buffer_t *sb, const void *ptr) 00028 { 00029 /* 00030 * FIXME: use process's symbol table (if available) to find symbol name 00031 * matching this pointer value. This will only get lucky some of the time, 00032 * but it will definitely be more informative. 00033 */ 00034 if (ptr == NULL) 00035 { 00036 explain_string_buffer_puts(sb, "NULL"); 00037 return; 00038 } 00039 00040 /* 00041 * If it is a pointer to a relocation symbol, we may be able to 00042 * locate it using the dynamic library support functions. 00043 */ 00044 #ifdef HAVE_DLADDR 00045 if (explain_option_dialect_specific()) 00046 { 00047 Dl_info dli; 00048 00049 dli.dli_sname = 0; 00050 /* We have to cast const-ness away, because Solaris is stupid. */ 00051 if (dladdr((void *)ptr, &dli) != 0 && dli.dli_sname) 00052 { 00053 explain_string_buffer_printf(sb, "&%s", dli.dli_sname); 00054 if (dli.dli_saddr != ptr) 00055 { 00056 explain_string_buffer_printf 00057 ( 00058 sb, 00059 " + %ld", 00060 (long)((const char *)ptr - (const char *)dli.dli_saddr) 00061 ); 00062 } 00063 return; 00064 } 00065 } 00066 #endif 00067 00068 /* 00069 * Some old systems don't have %p, and some new systems format 00070 * %p differently than glibc. Use consistent formatting, it 00071 * makes the automated tests easier to write. 00072 */ 00073 #if SIZEOF_VOID_P > SIZEOF_LONG 00074 explain_string_buffer_printf(sb, "0x%08llX", (unsigned long long)ptr); 00075 #else 00076 explain_string_buffer_printf(sb, "0x%08lX", (unsigned long)ptr); 00077 #endif 00078 } 00079 00080 00081 /* vim: set ts=8 sw=4 et : */