How to see printf lines in gdb?

When debugging my own code, I wrote the following lines:

m_sock = socket(m_iAf, m_iType, m_iProtocol); printf("errno = %d, %s\n", errno, strerror(errno)); printf("Hellowrold\n"); 

I created a socket, but when I execute this line, it returns negative. So I have to find a mistake. But neither errno nor Helloworld prints on the console.

How can I see printed lines?

I am new to ndk-gdb, so you need help.

Thanks Riasat

+1
debugging android-ndk gdb
Mar 28 '11 at 12:59
source share
4 answers

Instead of printf, you can use the Android logging tool:

 #include <android/log.h> __android_log_print(ANDROID_LOG_INFO, "MYPROG", "errno = %d, %s", errno, strerror(errno)); __android_log_print(ANDROID_LOG_INFO, "MYPROG", "Hellowrold"); 

There is no need for "\ n", and they will appear in logcat. You also need to link the log library. In the Android.mk file, add the following:

 LOCAL_LDLIBS := -llog 
+11
Mar 28 2018-11-11T00:
source share

Just call strerror directly from inside gdb:

 (gdb) call strerror (errno)
 Unable to call function "strerror" at 0x7fff857ae897: no return type information available.
 To call this function anyway, you can cast the return type explicitly (eg 'print (float) fabs (3.0)')
 (gdb) print (char *) strerror (errno)
 $ 1 = 0x7fff85892565 "Interrupted system call"

(The first call usually works for me, and this is the first time I've seen this error, so I enable it for completeness.)

For the general question of viewing output, it is usually easier to simply separate the program output from gdb output by redirecting it at program startup. For example, one terminal is open with "tail -f output-file", and then do:

 (gdb) run> output-file
+1
Mar 28 2018-11-11T00:
source share

Try this way. So the cpp source for Android cpp writes this way.

 #define LOG_TAG "A_TAG" // the tag to be shown in logcat #include <utils/Log.h> LOGE("Hello world: %s,%d",__FILE__,__LINE__); // somewhat like printf. 

The above code will display an error log with red in logcat.

You can also use

  • LOGW - warning
  • LOGD - debug
  • LOGI - information
  • LOGV - detailed
+1
Apr 20 2018-11-11T00:
source share

Shorter macros are available for logcat login. This example works in kitkat (4.4.2)

 #define LOG_TAG "my_log_tag" #include <cutils/log.h> ALOGD("Format this %d, some_int); 

In Android.mk, add the liblog library to LOCAL_SHARED_LIBRARIES when creating in 'mydroid' (a complete build of the Android system). In the case of the ndk build, LOCAL_LDLIBS: = -L $ (SYSROOT) / usr / lib -llog can be used.

 include $(CLEAR_VARS) LOCAL_MODULE := foo LOCAL_SRC_FILES := foo.c # if mydroid LOCAL_SHARED_LIBRARIES := liblog # in ndk, use LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog instead include $(BUILD_EXECUTABLE) 

There are various other macros defined for all levels of logging. From cutils/log.h :

 #define ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) #define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) ... #define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) 
0
Mar 31 '15 at 1:05
source share



All Articles