GNURLOPT_DEBUGFUNCTION(3)
π NAME π
Okay so bestie, this is the manual page for GNURLOPT_DEBUGFUNCTION(3) β the debug callback, and it's lowkey iconic. It's the thing that lets you inject your own callback into the debug flow, and yes, we're gonna go through every single detail together. π«Ά
β¨ SYNOPSIS β¨
#include <gnurl/curl.h>
typedef enum {
CURLINFO_TEXT = 0,
CURLINFO_HEADER_IN, /* 1 */
CURLINFO_HEADER_OUT, /* 2 */
CURLINFO_DATA_IN, /* 3 */
CURLINFO_DATA_OUT, /* 4 */
CURLINFO_SSL_DATA_IN, /* 5 */
CURLINFO_SSL_DATA_OUT, /* 6 */
CURLINFO_END
} curl_infotype;
int debug_callback(CURL *handle,
curl_infotype type,
char *data,
size_t size,
void *userptr);
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_DEBUGFUNCTION,
debug_callback);
π DESCRIPTION π
So you pass a pointer to your callback function, and it should match the prototype shown above. That's the vibe. π
CURLOPT_DEBUGFUNCTION(3) replaces the standard debug function used when CURLOPT_VERBOSE(3) is in effect. This callback receives debug information, as specified in the type argument. This function must return 0. And listen: the data pointed to by the char * passed to this function WILL NOT be null-terminated, but will be exactly of the size as told by the size argument. Not one byte more, not one byte less. Periodt. π
The userptr argument is the pointer set with CURLOPT_DEBUGDATA(3).
Available curl_infotype values:
CURLINFO_TEXTβ the data is informational text. It's giving "just letting you know" energy.CURLINFO_HEADER_INβ the data is header (or header-like) data received from the peer.CURLINFO_HEADER_OUTβ the data is header (or header-like) data sent to the peer.CURLINFO_DATA_INβ the data is protocol data received from the peer.CURLINFO_DATA_OUTβ the data is protocol data sent to the peer.CURLINFO_SSL_DATA_OUTβ the data is SSL/TLS (binary) data sent to the peer.CURLINFO_SSL_DATA_INβ the data is SSL/TLS (binary) data received from the peer.
π DEFAULT π
The default is literally NULL. No callback, no custom debug moment, just the standard situation. π
π¦ PROTOCOLS π¦
All of them. Every protocol. No gatekeeping. π
π₯ EXAMPLE π₯
Here's the whole example, bestie β copied exactly, character for character, no notes:
static
void dump(const char *text,
FILE *stream, unsigned char *ptr, size_t size)
{
size_t i;
size_t c;
unsigned int width=0x10;
fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
text, (long)size, (long)size);
for(i=0; i<size; i+= width) {
fprintf(stream, "%4.4lx: ", (long)i);
/* show hex to the left */
for(c = 0; c < width; c++) {
if(i+c < size)
fprintf(stream, "%02x ", ptr[i+c]);
else
fputs(" ", stream);
}
/* show data on the right */
for(c = 0; (c < width) && (i+c < size); c++) {
char x = (ptr[i+c] >= 0x20 && ptr[i+c] < 0x80) ? ptr[i+c] : '.';
fputc(x, stream);
}
fputc('\n', stream); /* newline */
}
}
static
int my_trace(CURL *handle, curl_infotype type,
char *data, size_t size,
void *userp)
{
const char *text;
(void)handle; /* prevent compiler warning */
(void)userp;
switch (type) {
case CURLINFO_TEXT:
fprintf(stderr, "== Info: %s", data);
default: /* in case a new one is introduced to shock us */
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
break;
case CURLINFO_DATA_OUT:
text = "=> Send data";
break;
case CURLINFO_SSL_DATA_OUT:
text = "=> Send SSL data";
break;
case CURLINFO_HEADER_IN:
text = "<= Recv header";
break;
case CURLINFO_DATA_IN:
text = "<= Recv data";
break;
case CURLINFO_SSL_DATA_IN:
text = "<= Recv SSL data";
break;
}
dump(text, stderr, (unsigned char *)data, size);
return 0;
}
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
/* the DEBUGFUNCTION has no effect until we enable VERBOSE */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
π AVAILABILITY π
Always. It's always available, bestie. β¨
β RETURN VALUE β
Returns CURLE_OK. That's the return value, and that's the whole story. π
π SEE ALSO π
CURLOPT_VERBOSE(3)β the one that makes debugging actually happen.CURLOPT_DEBUGDATA(3),β where youruserptrcomes from, trailing comma and all.
xoxo, your debug-callback bestie π
β this page is from libcurl 7.37.0, dated 17 Jun 2014, and yes, that's the cute sign-off. π
π¦ where this page comes from
- nixos 26.05: libgnurl 7.72.0
- nixpkgs unstable: libgnurl 7.72.0
- Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms