>From 9db497b4b5bbdb90f9980fc5364e8f3e4c63cffc Mon Sep 17 00:00:00 2001
From: HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com>
Date: Thu, 31 Mar 2011 23:10:28 +0900
Subject: [PATCH 2/4] Move all common error processings in get_arch_regs_from_elf_notes()

Move all common error processings in get_arch_regs_from_elf_notes() to unify
them. This change keeps unique error processing on PPC64 and so doesn't bring
about any logical change.
---
 netdump.c |  157 +++++++++++++++++++++++++++----------------------------------
 1 files changed, 70 insertions(+), 87 deletions(-)

diff --git a/netdump.c b/netdump.c
index 8b64e7c..12930d2 100644
--- a/netdump.c
+++ b/netdump.c
@@ -2791,6 +2791,18 @@ get_regs_from_elf_notes(struct task_context *tc)
 		      "support for ELF machine type %d not available\n",
 		      e_machine);
 
+	if ((tc->task != tt->panic_task) &&
+	    (!is_task_active(tc->task) || (nd->num_prstatus_notes <= 1)))
+		error(FATAL,
+		      "cannot determine register set for task \"%s\"\n",
+		      tc->comm);
+
+	if (e_machine == EM_PPC64 && nd->num_prstatus_notes > 1 &&
+	    tc->processor >= nd->num_prstatus_notes)
+		error(FATAL, "cannot determine NT_PRSTATUS ELF note "
+		      "for %s task: %lx\n", (tc->task == tt->panic_task) ?
+		      "panic" : "active", tc->task);
+
 	switch(e_machine)
 	{
 	case EM_386:
@@ -2818,31 +2830,25 @@ get_x86_regs_from_elf_notes(struct task_context *tc)
 	len = 0;
 	pt_regs = NULL;
 
-	if ((tc->task == tt->panic_task) || 
-	    (is_task_active(tc->task) && (nd->num_prstatus_notes > 1))) {
-		if (nd->num_prstatus_notes > 1)
-			note = (void *)
-				nd->nt_prstatus_percpu[tc->processor];
-		else
-			note = (void *)nd->nt_prstatus;
-		if (nd->elf32) {
-			note_32 = (Elf32_Nhdr *)note;
-			len = sizeof(Elf32_Nhdr);
-			len = roundup(len + note_32->n_namesz, 4);
-		} else if (nd->elf64) {
-			note_64 = (Elf64_Nhdr *)note;
-			len = sizeof(Elf64_Nhdr);
-			len = roundup(len + note_64->n_namesz, 4);
-		}
-		
-		pt_regs = (void *)((char *)note + len + 
-			MEMBER_OFFSET("elf_prstatus", "pr_reg"));
-		/* NEED TO BE FIXED: Hack to get the proper alignment */
-		pt_regs +=4;
-	} else
-		error(FATAL, 
-		    "cannot determine register set for task \"%s\"\n",
-			tc->comm);
+	if (nd->num_prstatus_notes > 1)
+		note = (void *)nd->nt_prstatus_percpu[tc->processor];
+	else
+		note = (void *)nd->nt_prstatus;
+	if (nd->elf32) {
+		note_32 = (Elf32_Nhdr *)note;
+		len = sizeof(Elf32_Nhdr);
+		len = roundup(len + note_32->n_namesz, 4);
+	} else if (nd->elf64) {
+		note_64 = (Elf64_Nhdr *)note;
+		len = sizeof(Elf64_Nhdr);
+		len = roundup(len + note_64->n_namesz, 4);
+	}
+
+	pt_regs = (void *)((char *)note + len +
+			   MEMBER_OFFSET("elf_prstatus", "pr_reg"));
+	/* NEED TO BE FIXED: Hack to get the proper alignment */
+	pt_regs +=4;
+
 	return pt_regs;
 
 }
@@ -2856,22 +2862,16 @@ get_x86_64_regs_from_elf_notes(struct task_context *tc)
 
 	pt_regs = NULL;
 
-	if ((tc->task == tt->panic_task) || 
-	    (is_task_active(tc->task) && (nd->num_prstatus_notes > 1))) {
-		if (nd->num_prstatus_notes > 1)
-			note = (Elf64_Nhdr *)
-				nd->nt_prstatus_percpu[tc->processor];
-		else
-			note = (Elf64_Nhdr *)nd->nt_prstatus;
+	if (nd->num_prstatus_notes > 1)
+		note = (Elf64_Nhdr *)nd->nt_prstatus_percpu[tc->processor];
+	else
+		note = (Elf64_Nhdr *)nd->nt_prstatus;
+
+	len = sizeof(Elf64_Nhdr);
+	len = roundup(len + note->n_namesz, 4);
+	pt_regs = (void *)((char *)note + len +
+			   MEMBER_OFFSET("elf_prstatus", "pr_reg"));
 
-		len = sizeof(Elf64_Nhdr);
-		len = roundup(len + note->n_namesz, 4);
-		pt_regs = (void *)((char *)note + len + 
-			MEMBER_OFFSET("elf_prstatus", "pr_reg"));
-	} else
-		error(FATAL, 
-		    "cannot determine register set for task \"%s\"\n",
-			tc->comm);
 	return pt_regs;
 }
 
@@ -2885,32 +2885,21 @@ get_ppc64_regs_from_elf_notes(struct task_context *tc)
 
 	pt_regs = NULL;
 
-	if ((tc->task == tt->panic_task) ||
-	    (is_task_active(tc->task) && (nd->num_prstatus_notes > 1))) {
-		/*	
-		 * Registers are always saved during the dump process for the 
-		 * panic task.  Kdump also captures registers for all CPUs if
-		 * they responded to an IPI.
-		 */
-		if (nd->num_prstatus_notes > 1) {
-			if (tc->processor >= nd->num_prstatus_notes)
-				error(FATAL, "cannot determine NT_PRSTATUS ELF note "
-				    "for %s task: %lx\n", (tc->task == tt->panic_task) ?
-				    "panic" : "active", tc->task);	
-			note = (Elf64_Nhdr *)
-				nd->nt_prstatus_percpu[tc->processor];
-		} else
-			note = (Elf64_Nhdr *)nd->nt_prstatus;
-
-		len = sizeof(Elf64_Nhdr);
-		len = roundup(len + note->n_namesz, 4);
-		pt_regs = (void *)((char *)note + len + 
-			MEMBER_OFFSET("elf_prstatus", "pr_reg"));
+	/*
+	 * Registers are always saved during the dump process for the
+	 * panic task.  Kdump also captures registers for all CPUs if
+	 * they responded to an IPI.
+	 */
+	if (nd->num_prstatus_notes > 1) {
+		note = (Elf64_Nhdr *)nd->nt_prstatus_percpu[tc->processor];
 	} else
-		error(FATAL, 
-		    "cannot determine register set for task \"%s\"\n",
-			tc->comm);
-	
+		note = (Elf64_Nhdr *)nd->nt_prstatus;
+
+	len = sizeof(Elf64_Nhdr);
+	len = roundup(len + note->n_namesz, 4);
+	pt_regs = (void *)((char *)note + len +
+			   MEMBER_OFFSET("elf_prstatus", "pr_reg"));
+
 	return pt_regs;
 }
 
@@ -2953,29 +2942,23 @@ get_arm_regs_from_elf_notes(struct task_context *tc)
 	len = 0;
 	pt_regs = NULL;
 
-	if ((tc->task == tt->panic_task) ||
-	    (is_task_active(tc->task) && (nd->num_prstatus_notes > 1))) {
-		if (nd->num_prstatus_notes > 1)
-			note = (void *)
-				nd->nt_prstatus_percpu[tc->processor];
-		else
-			note = (void *)nd->nt_prstatus;
-		if (nd->elf32) {
-			note_32 = (Elf32_Nhdr *)note;
-			len = sizeof(Elf32_Nhdr);
-			len = roundup(len + note_32->n_namesz, 4);
-		} else if (nd->elf64) {
-			note_64 = (Elf64_Nhdr *)note;
-			len = sizeof(Elf64_Nhdr);
-			len = roundup(len + note_64->n_namesz, 4);
-		}
+	if (nd->num_prstatus_notes > 1)
+		note = (void *)nd->nt_prstatus_percpu[tc->processor];
+	else
+		note = (void *)nd->nt_prstatus;
+	if (nd->elf32) {
+		note_32 = (Elf32_Nhdr *)note;
+		len = sizeof(Elf32_Nhdr);
+		len = roundup(len + note_32->n_namesz, 4);
+	} else if (nd->elf64) {
+		note_64 = (Elf64_Nhdr *)note;
+		len = sizeof(Elf64_Nhdr);
+		len = roundup(len + note_64->n_namesz, 4);
+	}
+
+	pt_regs = (void *)((char *)note + len +
+			   MEMBER_OFFSET("elf_prstatus", "pr_reg"));
 
-		pt_regs = (void *)((char *)note + len +
-			MEMBER_OFFSET("elf_prstatus", "pr_reg"));
-	} else
-		error(FATAL,
-		    "cannot determine arm register set for task \"%s\"\n",
-			tc->comm);
 	return pt_regs;
 }
 
-- 
1.7.4

