Add debugging framework

This patch adds a user configurable debug level. For testing
it includes connection info and reporting errors.
This commit is contained in:
Peter Lieven
2012-10-18 10:36:26 +02:00
parent c4592550d6
commit 4cb845477d
9 changed files with 53 additions and 5 deletions
+2
View File
@@ -65,6 +65,7 @@ struct iscsi_context {
const char *initiator_name; const char *initiator_name;
const char *target_name; const char *target_name;
const char *target_address; /* If a redirect */ const char *target_address; /* If a redirect */
const char *connected_portal;
const char *alias; const char *alias;
const char *user; const char *user;
@@ -120,6 +121,7 @@ struct iscsi_context {
const char *portal; const char *portal;
int no_auto_reconnect; int no_auto_reconnect;
int reconnect_deferred; int reconnect_deferred;
int debug;
}; };
#define ISCSI_PDU_IMMEDIATE 0x40 #define ISCSI_PDU_IMMEDIATE 0x40
+8
View File
@@ -958,6 +958,14 @@ iscsi_scsi_task_cancel(struct iscsi_context *iscsi,
EXTERN void EXTERN void
iscsi_scsi_cancel_all_tasks(struct iscsi_context *iscsi); iscsi_scsi_cancel_all_tasks(struct iscsi_context *iscsi);
#define DPRINTF(iscsi,level,fmt,args...) do { if ((iscsi)->debug >= level) {fprintf(stderr,"libiscsi: ");fprintf(stderr, (fmt), ##args); fprintf(stderr,"\n");} } while (0);
/*
* This function is to set the debugging level (0=disabled).
*/
EXTERN void
iscsi_set_debug(struct iscsi_context *iscsi, int level);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
+4
View File
@@ -158,6 +158,8 @@ int iscsi_reconnect(struct iscsi_context *old_iscsi)
{ {
struct iscsi_context *iscsi = old_iscsi; struct iscsi_context *iscsi = old_iscsi;
DPRINTF(iscsi,2,"reconnect initiated");
/* This is mainly for tests, where we do not want to automatically /* This is mainly for tests, where we do not want to automatically
reconnect but rather want the commands to fail with an error reconnect but rather want the commands to fail with an error
if the target drops the session. if the target drops the session.
@@ -213,6 +215,8 @@ try_again:
iscsi->lun = old_iscsi->lun; iscsi->lun = old_iscsi->lun;
iscsi->portal = strdup(old_iscsi->portal); iscsi->portal = strdup(old_iscsi->portal);
iscsi->debug = old_iscsi->debug;
if (iscsi_full_connect_sync(iscsi, iscsi->portal, iscsi->lun) != 0) { if (iscsi_full_connect_sync(iscsi, iscsi->portal, iscsi->lun) != 0) {
iscsi_destroy_context(iscsi); iscsi_destroy_context(iscsi);
+14 -2
View File
@@ -244,6 +244,11 @@ iscsi_destroy_context(struct iscsi_context *iscsi)
free(discard_const(iscsi->chap_c)); free(discard_const(iscsi->chap_c));
iscsi->chap_c = NULL; iscsi->chap_c = NULL;
if (iscsi->connected_portal != NULL) {
free(discard_const(iscsi->connected_portal));
iscsi->connected_portal = NULL;
}
iscsi->connect_data = NULL; iscsi->connect_data = NULL;
free(iscsi); free(iscsi);
@@ -251,8 +256,6 @@ iscsi_destroy_context(struct iscsi_context *iscsi)
return 0; return 0;
} }
void void
iscsi_set_error(struct iscsi_context *iscsi, const char *error_string, ...) iscsi_set_error(struct iscsi_context *iscsi, const char *error_string, ...)
{ {
@@ -270,9 +273,18 @@ iscsi_set_error(struct iscsi_context *iscsi, const char *error_string, ...)
free(iscsi->error_string); free(iscsi->error_string);
iscsi->error_string = str; iscsi->error_string = str;
va_end(ap); va_end(ap);
DPRINTF(iscsi,1,str);
} }
void
iscsi_set_debug(struct iscsi_context *iscsi, int level)
{
iscsi->debug = level;
DPRINTF(iscsi,2,"set debug level to %d",level);
}
const char * const char *
iscsi_get_error(struct iscsi_context *iscsi) iscsi_get_error(struct iscsi_context *iscsi)
+1
View File
@@ -62,6 +62,7 @@ iscsi_scsi_command_sync
iscsi_scsi_task_cancel iscsi_scsi_task_cancel
iscsi_service iscsi_service
iscsi_set_alias iscsi_set_alias
iscsi_set_debug
iscsi_set_header_digest iscsi_set_header_digest
iscsi_set_initiator_username_pwd iscsi_set_initiator_username_pwd
iscsi_set_isid_en iscsi_set_isid_en
+1
View File
@@ -60,6 +60,7 @@ iscsi_scsi_command_sync
iscsi_scsi_task_cancel iscsi_scsi_task_cancel
iscsi_service iscsi_service
iscsi_set_alias iscsi_set_alias
iscsi_set_debug
iscsi_set_header_digest iscsi_set_header_digest
iscsi_set_initiator_username_pwd iscsi_set_initiator_username_pwd
iscsi_set_isid_en iscsi_set_isid_en
+9
View File
@@ -68,6 +68,8 @@ iscsi_connect_async(struct iscsi_context *iscsi, const char *portal,
struct addrinfo *ai = NULL; struct addrinfo *ai = NULL;
int socksize; int socksize;
DPRINTF(iscsi,2,"connecting to portal %s",portal);
if (iscsi->fd != -1) { if (iscsi->fd != -1) {
iscsi_set_error(iscsi, iscsi_set_error(iscsi,
"Trying to connect but already connected."); "Trying to connect but already connected.");
@@ -170,6 +172,10 @@ iscsi_connect_async(struct iscsi_context *iscsi, const char *portal,
} }
freeaddrinfo(ai); freeaddrinfo(ai);
if (iscsi->connected_portal) free(iscsi->connected_portal);
iscsi->connected_portal=strdup(portal);
return 0; return 0;
} }
@@ -183,6 +189,9 @@ iscsi_disconnect(struct iscsi_context *iscsi)
} }
close(iscsi->fd); close(iscsi->fd);
if (iscsi->connected_portal)
DPRINTF(iscsi,2,"disconnected from portal %s",iscsi->connected_portal);
iscsi->fd = -1; iscsi->fd = -1;
iscsi->is_connected = 0; iscsi->is_connected = 0;
+7 -1
View File
@@ -196,6 +196,7 @@ void print_help(void)
fprintf(stderr, " -i, --initiator-name=iqn-name Initiatorname to use\n"); fprintf(stderr, " -i, --initiator-name=iqn-name Initiatorname to use\n");
fprintf(stderr, " -e, --evpd=integer evpd\n"); fprintf(stderr, " -e, --evpd=integer evpd\n");
fprintf(stderr, " -c, --pagecode=integer page code\n"); fprintf(stderr, " -c, --pagecode=integer page code\n");
fprintf(stderr, " -d, --debug=integer debug level (0=disabled)\n");
fprintf(stderr, "\n"); fprintf(stderr, "\n");
fprintf(stderr, "Help options:\n"); fprintf(stderr, "Help options:\n");
fprintf(stderr, " -?, --help Show this help message\n"); fprintf(stderr, " -?, --help Show this help message\n");
@@ -218,7 +219,7 @@ int main(int argc, const char *argv[])
const char *url = NULL; const char *url = NULL;
struct iscsi_url *iscsi_url = NULL; struct iscsi_url *iscsi_url = NULL;
int evpd = 0, pagecode = 0; int evpd = 0, pagecode = 0;
int show_help = 0, show_usage = 0; int show_help = 0, show_usage = 0, debug = 0;
int res; int res;
struct poptOption popt_options[] = { struct poptOption popt_options[] = {
@@ -227,6 +228,7 @@ int main(int argc, const char *argv[])
{ "initiator-name", 'i', POPT_ARG_STRING, &initiator, 0, "Initiatorname to use", "iqn-name" }, { "initiator-name", 'i', POPT_ARG_STRING, &initiator, 0, "Initiatorname to use", "iqn-name" },
{ "evpd", 'e', POPT_ARG_INT, &evpd, 0, "evpd", "integer" }, { "evpd", 'e', POPT_ARG_INT, &evpd, 0, "evpd", "integer" },
{ "pagecode", 'c', POPT_ARG_INT, &pagecode, 0, "page code", "integer" }, { "pagecode", 'c', POPT_ARG_INT, &pagecode, 0, "page code", "integer" },
{ "debug", 'd', POPT_ARG_INT, &debug, 0, "Debugging level", "integer" },
POPT_TABLEEND POPT_TABLEEND
}; };
@@ -263,6 +265,10 @@ int main(int argc, const char *argv[])
exit(10); exit(10);
} }
if (debug > 0) {
iscsi_set_debug(iscsi, debug);
}
if (url == NULL) { if (url == NULL) {
fprintf(stderr, "You must specify the URL\n"); fprintf(stderr, "You must specify the URL\n");
print_usage(); print_usage();
+7 -2
View File
@@ -291,6 +291,7 @@ void print_help(void)
fprintf(stderr, "Usage: iscsi-ls [OPTION...] <iscsi-url>\n"); fprintf(stderr, "Usage: iscsi-ls [OPTION...] <iscsi-url>\n");
fprintf(stderr, " -i, --initiator-name=iqn-name Initiatorname to use\n"); fprintf(stderr, " -i, --initiator-name=iqn-name Initiatorname to use\n");
fprintf(stderr, " -s, --show-luns Show the luns for each target\n"); fprintf(stderr, " -s, --show-luns Show the luns for each target\n");
fprintf(stderr, " -d, --debug=integer debug level (0=disabled)\n");
fprintf(stderr, "\n"); fprintf(stderr, "\n");
fprintf(stderr, "Help options:\n"); fprintf(stderr, "Help options:\n");
fprintf(stderr, " -?, --help Show this help message\n"); fprintf(stderr, " -?, --help Show this help message\n");
@@ -314,13 +315,14 @@ int main(int argc, const char *argv[])
const char *url = NULL; const char *url = NULL;
poptContext pc; poptContext pc;
int res; int res;
int show_help = 0, show_usage = 0; int show_help = 0, show_usage = 0, debug = 0;
struct poptOption popt_options[] = { struct poptOption popt_options[] = {
{ "help", '?', POPT_ARG_NONE, &show_help, 0, "Show this help message", NULL }, { "help", '?', POPT_ARG_NONE, &show_help, 0, "Show this help message", NULL },
{ "usage", 0, POPT_ARG_NONE, &show_usage, 0, "Display brief usage message", NULL }, { "usage", 0, POPT_ARG_NONE, &show_usage, 0, "Display brief usage message", NULL },
{ "initiator-name", 'i', POPT_ARG_STRING, &initiator, 0, "Initiatorname to use", "iqn-name" }, { "initiator-name", 'i', POPT_ARG_STRING, &initiator, 0, "Initiatorname to use", "iqn-name" },
{ "show-luns", 's', POPT_ARG_NONE, &showluns, 0, "Show the luns for each target", NULL }, { "show-luns", 's', POPT_ARG_NONE, &showluns, 0, "Show the luns for each target", NULL },
{ "debug", 'd', POPT_ARG_INT, &debug, 0, "Debugging level", "integer" },
POPT_TABLEEND POPT_TABLEEND
}; };
@@ -353,7 +355,6 @@ int main(int argc, const char *argv[])
poptFreeContext(pc); poptFreeContext(pc);
if (url == NULL) { if (url == NULL) {
fprintf(stderr, "You must specify iscsi target portal.\n"); fprintf(stderr, "You must specify iscsi target portal.\n");
print_usage(); print_usage();
@@ -366,6 +367,10 @@ int main(int argc, const char *argv[])
exit(10); exit(10);
} }
if (debug > 0) {
iscsi_set_debug(iscsi, debug);
}
iscsi_url = iscsi_parse_portal_url(iscsi, url); iscsi_url = iscsi_parse_portal_url(iscsi, url);
if (iscsi_url == NULL) { if (iscsi_url == NULL) {
fprintf(stderr, "Failed to parse URL: %s\n", fprintf(stderr, "Failed to parse URL: %s\n",