kdb_main.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939
  1. /*
  2. * Kernel Debugger Architecture Independent Main Code
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
  9. * Copyright (C) 2000 Stephane Eranian <[email protected]>
  10. * Xscale (R) modifications copyright (C) 2003 Intel Corporation.
  11. * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
  12. */
  13. #include <linux/ctype.h>
  14. #include <linux/types.h>
  15. #include <linux/string.h>
  16. #include <linux/kernel.h>
  17. #include <linux/kmsg_dump.h>
  18. #include <linux/reboot.h>
  19. #include <linux/sched.h>
  20. #include <linux/sched/loadavg.h>
  21. #include <linux/sched/stat.h>
  22. #include <linux/sched/debug.h>
  23. #include <linux/sysrq.h>
  24. #include <linux/smp.h>
  25. #include <linux/utsname.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/atomic.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/mm.h>
  30. #include <linux/init.h>
  31. #include <linux/kallsyms.h>
  32. #include <linux/kgdb.h>
  33. #include <linux/kdb.h>
  34. #include <linux/notifier.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/delay.h>
  37. #include <linux/nmi.h>
  38. #include <linux/time.h>
  39. #include <linux/ptrace.h>
  40. #include <linux/sysctl.h>
  41. #include <linux/cpu.h>
  42. #include <linux/kdebug.h>
  43. #include <linux/proc_fs.h>
  44. #include <linux/uaccess.h>
  45. #include <linux/slab.h>
  46. #include <linux/security.h>
  47. #include "kdb_private.h"
  48. #undef MODULE_PARAM_PREFIX
  49. #define MODULE_PARAM_PREFIX "kdb."
  50. static int kdb_cmd_enabled = CONFIG_KDB_DEFAULT_ENABLE;
  51. module_param_named(cmd_enable, kdb_cmd_enabled, int, 0600);
  52. char kdb_grep_string[KDB_GREP_STRLEN];
  53. int kdb_grepping_flag;
  54. EXPORT_SYMBOL(kdb_grepping_flag);
  55. int kdb_grep_leading;
  56. int kdb_grep_trailing;
  57. /*
  58. * Kernel debugger state flags
  59. */
  60. unsigned int kdb_flags;
  61. /*
  62. * kdb_lock protects updates to kdb_initial_cpu. Used to
  63. * single thread processors through the kernel debugger.
  64. */
  65. int kdb_initial_cpu = -1; /* cpu number that owns kdb */
  66. int kdb_nextline = 1;
  67. int kdb_state; /* General KDB state */
  68. struct task_struct *kdb_current_task;
  69. struct pt_regs *kdb_current_regs;
  70. const char *kdb_diemsg;
  71. static int kdb_go_count;
  72. #ifdef CONFIG_KDB_CONTINUE_CATASTROPHIC
  73. static unsigned int kdb_continue_catastrophic =
  74. CONFIG_KDB_CONTINUE_CATASTROPHIC;
  75. #else
  76. static unsigned int kdb_continue_catastrophic;
  77. #endif
  78. /* kdb_cmds_head describes the available commands. */
  79. static LIST_HEAD(kdb_cmds_head);
  80. typedef struct _kdbmsg {
  81. int km_diag; /* kdb diagnostic */
  82. char *km_msg; /* Corresponding message text */
  83. } kdbmsg_t;
  84. #define KDBMSG(msgnum, text) \
  85. { KDB_##msgnum, text }
  86. static kdbmsg_t kdbmsgs[] = {
  87. KDBMSG(NOTFOUND, "Command Not Found"),
  88. KDBMSG(ARGCOUNT, "Improper argument count, see usage."),
  89. KDBMSG(BADWIDTH, "Illegal value for BYTESPERWORD use 1, 2, 4 or 8, "
  90. "8 is only allowed on 64 bit systems"),
  91. KDBMSG(BADRADIX, "Illegal value for RADIX use 8, 10 or 16"),
  92. KDBMSG(NOTENV, "Cannot find environment variable"),
  93. KDBMSG(NOENVVALUE, "Environment variable should have value"),
  94. KDBMSG(NOTIMP, "Command not implemented"),
  95. KDBMSG(ENVFULL, "Environment full"),
  96. KDBMSG(ENVBUFFULL, "Environment buffer full"),
  97. KDBMSG(TOOMANYBPT, "Too many breakpoints defined"),
  98. #ifdef CONFIG_CPU_XSCALE
  99. KDBMSG(TOOMANYDBREGS, "More breakpoints than ibcr registers defined"),
  100. #else
  101. KDBMSG(TOOMANYDBREGS, "More breakpoints than db registers defined"),
  102. #endif
  103. KDBMSG(DUPBPT, "Duplicate breakpoint address"),
  104. KDBMSG(BPTNOTFOUND, "Breakpoint not found"),
  105. KDBMSG(BADMODE, "Invalid IDMODE"),
  106. KDBMSG(BADINT, "Illegal numeric value"),
  107. KDBMSG(INVADDRFMT, "Invalid symbolic address format"),
  108. KDBMSG(BADREG, "Invalid register name"),
  109. KDBMSG(BADCPUNUM, "Invalid cpu number"),
  110. KDBMSG(BADLENGTH, "Invalid length field"),
  111. KDBMSG(NOBP, "No Breakpoint exists"),
  112. KDBMSG(BADADDR, "Invalid address"),
  113. KDBMSG(NOPERM, "Permission denied"),
  114. };
  115. #undef KDBMSG
  116. static const int __nkdb_err = ARRAY_SIZE(kdbmsgs);
  117. /*
  118. * Initial environment. This is all kept static and local to
  119. * this file. We don't want to rely on the memory allocation
  120. * mechanisms in the kernel, so we use a very limited allocate-only
  121. * heap for new and altered environment variables. The entire
  122. * environment is limited to a fixed number of entries (add more
  123. * to __env[] if required) and a fixed amount of heap (add more to
  124. * KDB_ENVBUFSIZE if required).
  125. */
  126. static char *__env[31] = {
  127. #if defined(CONFIG_SMP)
  128. "PROMPT=[%d]kdb> ",
  129. #else
  130. "PROMPT=kdb> ",
  131. #endif
  132. "MOREPROMPT=more> ",
  133. "RADIX=16",
  134. "MDCOUNT=8", /* lines of md output */
  135. KDB_PLATFORM_ENV,
  136. "DTABCOUNT=30",
  137. "NOSECT=1",
  138. };
  139. static const int __nenv = ARRAY_SIZE(__env);
  140. struct task_struct *kdb_curr_task(int cpu)
  141. {
  142. struct task_struct *p = curr_task(cpu);
  143. #ifdef _TIF_MCA_INIT
  144. if ((task_thread_info(p)->flags & _TIF_MCA_INIT) && KDB_TSK(cpu))
  145. p = krp->p;
  146. #endif
  147. return p;
  148. }
  149. /*
  150. * Update the permissions flags (kdb_cmd_enabled) to match the
  151. * current lockdown state.
  152. *
  153. * Within this function the calls to security_locked_down() are "lazy". We
  154. * avoid calling them if the current value of kdb_cmd_enabled already excludes
  155. * flags that might be subject to lockdown. Additionally we deliberately check
  156. * the lockdown flags independently (even though read lockdown implies write
  157. * lockdown) since that results in both simpler code and clearer messages to
  158. * the user on first-time debugger entry.
  159. *
  160. * The permission masks during a read+write lockdown permits the following
  161. * flags: INSPECT, SIGNAL, REBOOT (and ALWAYS_SAFE).
  162. *
  163. * The INSPECT commands are not blocked during lockdown because they are
  164. * not arbitrary memory reads. INSPECT covers the backtrace family (sometimes
  165. * forcing them to have no arguments) and lsmod. These commands do expose
  166. * some kernel state but do not allow the developer seated at the console to
  167. * choose what state is reported. SIGNAL and REBOOT should not be controversial,
  168. * given these are allowed for root during lockdown already.
  169. */
  170. static void kdb_check_for_lockdown(void)
  171. {
  172. const int write_flags = KDB_ENABLE_MEM_WRITE |
  173. KDB_ENABLE_REG_WRITE |
  174. KDB_ENABLE_FLOW_CTRL;
  175. const int read_flags = KDB_ENABLE_MEM_READ |
  176. KDB_ENABLE_REG_READ;
  177. bool need_to_lockdown_write = false;
  178. bool need_to_lockdown_read = false;
  179. if (kdb_cmd_enabled & (KDB_ENABLE_ALL | write_flags))
  180. need_to_lockdown_write =
  181. security_locked_down(LOCKDOWN_DBG_WRITE_KERNEL);
  182. if (kdb_cmd_enabled & (KDB_ENABLE_ALL | read_flags))
  183. need_to_lockdown_read =
  184. security_locked_down(LOCKDOWN_DBG_READ_KERNEL);
  185. /* De-compose KDB_ENABLE_ALL if required */
  186. if (need_to_lockdown_write || need_to_lockdown_read)
  187. if (kdb_cmd_enabled & KDB_ENABLE_ALL)
  188. kdb_cmd_enabled = KDB_ENABLE_MASK & ~KDB_ENABLE_ALL;
  189. if (need_to_lockdown_write)
  190. kdb_cmd_enabled &= ~write_flags;
  191. if (need_to_lockdown_read)
  192. kdb_cmd_enabled &= ~read_flags;
  193. }
  194. /*
  195. * Check whether the flags of the current command, the permissions of the kdb
  196. * console and the lockdown state allow a command to be run.
  197. */
  198. static bool kdb_check_flags(kdb_cmdflags_t flags, int permissions,
  199. bool no_args)
  200. {
  201. /* permissions comes from userspace so needs massaging slightly */
  202. permissions &= KDB_ENABLE_MASK;
  203. permissions |= KDB_ENABLE_ALWAYS_SAFE;
  204. /* some commands change group when launched with no arguments */
  205. if (no_args)
  206. permissions |= permissions << KDB_ENABLE_NO_ARGS_SHIFT;
  207. flags |= KDB_ENABLE_ALL;
  208. return permissions & flags;
  209. }
  210. /*
  211. * kdbgetenv - This function will return the character string value of
  212. * an environment variable.
  213. * Parameters:
  214. * match A character string representing an environment variable.
  215. * Returns:
  216. * NULL No environment variable matches 'match'
  217. * char* Pointer to string value of environment variable.
  218. */
  219. char *kdbgetenv(const char *match)
  220. {
  221. char **ep = __env;
  222. int matchlen = strlen(match);
  223. int i;
  224. for (i = 0; i < __nenv; i++) {
  225. char *e = *ep++;
  226. if (!e)
  227. continue;
  228. if ((strncmp(match, e, matchlen) == 0)
  229. && ((e[matchlen] == '\0')
  230. || (e[matchlen] == '='))) {
  231. char *cp = strchr(e, '=');
  232. return cp ? ++cp : "";
  233. }
  234. }
  235. return NULL;
  236. }
  237. /*
  238. * kdballocenv - This function is used to allocate bytes for
  239. * environment entries.
  240. * Parameters:
  241. * match A character string representing a numeric value
  242. * Outputs:
  243. * *value the unsigned long representation of the env variable 'match'
  244. * Returns:
  245. * Zero on success, a kdb diagnostic on failure.
  246. * Remarks:
  247. * We use a static environment buffer (envbuffer) to hold the values
  248. * of dynamically generated environment variables (see kdb_set). Buffer
  249. * space once allocated is never free'd, so over time, the amount of space
  250. * (currently 512 bytes) will be exhausted if env variables are changed
  251. * frequently.
  252. */
  253. static char *kdballocenv(size_t bytes)
  254. {
  255. #define KDB_ENVBUFSIZE 512
  256. static char envbuffer[KDB_ENVBUFSIZE];
  257. static int envbufsize;
  258. char *ep = NULL;
  259. if ((KDB_ENVBUFSIZE - envbufsize) >= bytes) {
  260. ep = &envbuffer[envbufsize];
  261. envbufsize += bytes;
  262. }
  263. return ep;
  264. }
  265. /*
  266. * kdbgetulenv - This function will return the value of an unsigned
  267. * long-valued environment variable.
  268. * Parameters:
  269. * match A character string representing a numeric value
  270. * Outputs:
  271. * *value the unsigned long representation of the env variable 'match'
  272. * Returns:
  273. * Zero on success, a kdb diagnostic on failure.
  274. */
  275. static int kdbgetulenv(const char *match, unsigned long *value)
  276. {
  277. char *ep;
  278. ep = kdbgetenv(match);
  279. if (!ep)
  280. return KDB_NOTENV;
  281. if (strlen(ep) == 0)
  282. return KDB_NOENVVALUE;
  283. *value = simple_strtoul(ep, NULL, 0);
  284. return 0;
  285. }
  286. /*
  287. * kdbgetintenv - This function will return the value of an
  288. * integer-valued environment variable.
  289. * Parameters:
  290. * match A character string representing an integer-valued env variable
  291. * Outputs:
  292. * *value the integer representation of the environment variable 'match'
  293. * Returns:
  294. * Zero on success, a kdb diagnostic on failure.
  295. */
  296. int kdbgetintenv(const char *match, int *value)
  297. {
  298. unsigned long val;
  299. int diag;
  300. diag = kdbgetulenv(match, &val);
  301. if (!diag)
  302. *value = (int) val;
  303. return diag;
  304. }
  305. /*
  306. * kdb_setenv() - Alter an existing environment variable or create a new one.
  307. * @var: Name of the variable
  308. * @val: Value of the variable
  309. *
  310. * Return: Zero on success, a kdb diagnostic on failure.
  311. */
  312. static int kdb_setenv(const char *var, const char *val)
  313. {
  314. int i;
  315. char *ep;
  316. size_t varlen, vallen;
  317. varlen = strlen(var);
  318. vallen = strlen(val);
  319. ep = kdballocenv(varlen + vallen + 2);
  320. if (ep == (char *)0)
  321. return KDB_ENVBUFFULL;
  322. sprintf(ep, "%s=%s", var, val);
  323. for (i = 0; i < __nenv; i++) {
  324. if (__env[i]
  325. && ((strncmp(__env[i], var, varlen) == 0)
  326. && ((__env[i][varlen] == '\0')
  327. || (__env[i][varlen] == '=')))) {
  328. __env[i] = ep;
  329. return 0;
  330. }
  331. }
  332. /*
  333. * Wasn't existing variable. Fit into slot.
  334. */
  335. for (i = 0; i < __nenv-1; i++) {
  336. if (__env[i] == (char *)0) {
  337. __env[i] = ep;
  338. return 0;
  339. }
  340. }
  341. return KDB_ENVFULL;
  342. }
  343. /*
  344. * kdb_printenv() - Display the current environment variables.
  345. */
  346. static void kdb_printenv(void)
  347. {
  348. int i;
  349. for (i = 0; i < __nenv; i++) {
  350. if (__env[i])
  351. kdb_printf("%s\n", __env[i]);
  352. }
  353. }
  354. /*
  355. * kdbgetularg - This function will convert a numeric string into an
  356. * unsigned long value.
  357. * Parameters:
  358. * arg A character string representing a numeric value
  359. * Outputs:
  360. * *value the unsigned long representation of arg.
  361. * Returns:
  362. * Zero on success, a kdb diagnostic on failure.
  363. */
  364. int kdbgetularg(const char *arg, unsigned long *value)
  365. {
  366. char *endp;
  367. unsigned long val;
  368. val = simple_strtoul(arg, &endp, 0);
  369. if (endp == arg) {
  370. /*
  371. * Also try base 16, for us folks too lazy to type the
  372. * leading 0x...
  373. */
  374. val = simple_strtoul(arg, &endp, 16);
  375. if (endp == arg)
  376. return KDB_BADINT;
  377. }
  378. *value = val;
  379. return 0;
  380. }
  381. int kdbgetu64arg(const char *arg, u64 *value)
  382. {
  383. char *endp;
  384. u64 val;
  385. val = simple_strtoull(arg, &endp, 0);
  386. if (endp == arg) {
  387. val = simple_strtoull(arg, &endp, 16);
  388. if (endp == arg)
  389. return KDB_BADINT;
  390. }
  391. *value = val;
  392. return 0;
  393. }
  394. /*
  395. * kdb_set - This function implements the 'set' command. Alter an
  396. * existing environment variable or create a new one.
  397. */
  398. int kdb_set(int argc, const char **argv)
  399. {
  400. /*
  401. * we can be invoked two ways:
  402. * set var=value argv[1]="var", argv[2]="value"
  403. * set var = value argv[1]="var", argv[2]="=", argv[3]="value"
  404. * - if the latter, shift 'em down.
  405. */
  406. if (argc == 3) {
  407. argv[2] = argv[3];
  408. argc--;
  409. }
  410. if (argc != 2)
  411. return KDB_ARGCOUNT;
  412. /*
  413. * Censor sensitive variables
  414. */
  415. if (strcmp(argv[1], "PROMPT") == 0 &&
  416. !kdb_check_flags(KDB_ENABLE_MEM_READ, kdb_cmd_enabled, false))
  417. return KDB_NOPERM;
  418. /*
  419. * Check for internal variables
  420. */
  421. if (strcmp(argv[1], "KDBDEBUG") == 0) {
  422. unsigned int debugflags;
  423. char *cp;
  424. debugflags = simple_strtoul(argv[2], &cp, 0);
  425. if (cp == argv[2] || debugflags & ~KDB_DEBUG_FLAG_MASK) {
  426. kdb_printf("kdb: illegal debug flags '%s'\n",
  427. argv[2]);
  428. return 0;
  429. }
  430. kdb_flags = (kdb_flags & ~KDB_DEBUG(MASK))
  431. | (debugflags << KDB_DEBUG_FLAG_SHIFT);
  432. return 0;
  433. }
  434. /*
  435. * Tokenizer squashed the '=' sign. argv[1] is variable
  436. * name, argv[2] = value.
  437. */
  438. return kdb_setenv(argv[1], argv[2]);
  439. }
  440. static int kdb_check_regs(void)
  441. {
  442. if (!kdb_current_regs) {
  443. kdb_printf("No current kdb registers."
  444. " You may need to select another task\n");
  445. return KDB_BADREG;
  446. }
  447. return 0;
  448. }
  449. /*
  450. * kdbgetaddrarg - This function is responsible for parsing an
  451. * address-expression and returning the value of the expression,
  452. * symbol name, and offset to the caller.
  453. *
  454. * The argument may consist of a numeric value (decimal or
  455. * hexadecimal), a symbol name, a register name (preceded by the
  456. * percent sign), an environment variable with a numeric value
  457. * (preceded by a dollar sign) or a simple arithmetic expression
  458. * consisting of a symbol name, +/-, and a numeric constant value
  459. * (offset).
  460. * Parameters:
  461. * argc - count of arguments in argv
  462. * argv - argument vector
  463. * *nextarg - index to next unparsed argument in argv[]
  464. * regs - Register state at time of KDB entry
  465. * Outputs:
  466. * *value - receives the value of the address-expression
  467. * *offset - receives the offset specified, if any
  468. * *name - receives the symbol name, if any
  469. * *nextarg - index to next unparsed argument in argv[]
  470. * Returns:
  471. * zero is returned on success, a kdb diagnostic code is
  472. * returned on error.
  473. */
  474. int kdbgetaddrarg(int argc, const char **argv, int *nextarg,
  475. unsigned long *value, long *offset,
  476. char **name)
  477. {
  478. unsigned long addr;
  479. unsigned long off = 0;
  480. int positive;
  481. int diag;
  482. int found = 0;
  483. char *symname;
  484. char symbol = '\0';
  485. char *cp;
  486. kdb_symtab_t symtab;
  487. /*
  488. * If the enable flags prohibit both arbitrary memory access
  489. * and flow control then there are no reasonable grounds to
  490. * provide symbol lookup.
  491. */
  492. if (!kdb_check_flags(KDB_ENABLE_MEM_READ | KDB_ENABLE_FLOW_CTRL,
  493. kdb_cmd_enabled, false))
  494. return KDB_NOPERM;
  495. /*
  496. * Process arguments which follow the following syntax:
  497. *
  498. * symbol | numeric-address [+/- numeric-offset]
  499. * %register
  500. * $environment-variable
  501. */
  502. if (*nextarg > argc)
  503. return KDB_ARGCOUNT;
  504. symname = (char *)argv[*nextarg];
  505. /*
  506. * If there is no whitespace between the symbol
  507. * or address and the '+' or '-' symbols, we
  508. * remember the character and replace it with a
  509. * null so the symbol/value can be properly parsed
  510. */
  511. cp = strpbrk(symname, "+-");
  512. if (cp != NULL) {
  513. symbol = *cp;
  514. *cp++ = '\0';
  515. }
  516. if (symname[0] == '$') {
  517. diag = kdbgetulenv(&symname[1], &addr);
  518. if (diag)
  519. return diag;
  520. } else if (symname[0] == '%') {
  521. diag = kdb_check_regs();
  522. if (diag)
  523. return diag;
  524. /* Implement register values with % at a later time as it is
  525. * arch optional.
  526. */
  527. return KDB_NOTIMP;
  528. } else {
  529. found = kdbgetsymval(symname, &symtab);
  530. if (found) {
  531. addr = symtab.sym_start;
  532. } else {
  533. diag = kdbgetularg(argv[*nextarg], &addr);
  534. if (diag)
  535. return diag;
  536. }
  537. }
  538. if (!found)
  539. found = kdbnearsym(addr, &symtab);
  540. (*nextarg)++;
  541. if (name)
  542. *name = symname;
  543. if (value)
  544. *value = addr;
  545. if (offset && name && *name)
  546. *offset = addr - symtab.sym_start;
  547. if ((*nextarg > argc)
  548. && (symbol == '\0'))
  549. return 0;
  550. /*
  551. * check for +/- and offset
  552. */
  553. if (symbol == '\0') {
  554. if ((argv[*nextarg][0] != '+')
  555. && (argv[*nextarg][0] != '-')) {
  556. /*
  557. * Not our argument. Return.
  558. */
  559. return 0;
  560. } else {
  561. positive = (argv[*nextarg][0] == '+');
  562. (*nextarg)++;
  563. }
  564. } else
  565. positive = (symbol == '+');
  566. /*
  567. * Now there must be an offset!
  568. */
  569. if ((*nextarg > argc)
  570. && (symbol == '\0')) {
  571. return KDB_INVADDRFMT;
  572. }
  573. if (!symbol) {
  574. cp = (char *)argv[*nextarg];
  575. (*nextarg)++;
  576. }
  577. diag = kdbgetularg(cp, &off);
  578. if (diag)
  579. return diag;
  580. if (!positive)
  581. off = -off;
  582. if (offset)
  583. *offset += off;
  584. if (value)
  585. *value += off;
  586. return 0;
  587. }
  588. static void kdb_cmderror(int diag)
  589. {
  590. int i;
  591. if (diag >= 0) {
  592. kdb_printf("no error detected (diagnostic is %d)\n", diag);
  593. return;
  594. }
  595. for (i = 0; i < __nkdb_err; i++) {
  596. if (kdbmsgs[i].km_diag == diag) {
  597. kdb_printf("diag: %d: %s\n", diag, kdbmsgs[i].km_msg);
  598. return;
  599. }
  600. }
  601. kdb_printf("Unknown diag %d\n", -diag);
  602. }
  603. /*
  604. * kdb_defcmd, kdb_defcmd2 - This function implements the 'defcmd'
  605. * command which defines one command as a set of other commands,
  606. * terminated by endefcmd. kdb_defcmd processes the initial
  607. * 'defcmd' command, kdb_defcmd2 is invoked from kdb_parse for
  608. * the following commands until 'endefcmd'.
  609. * Inputs:
  610. * argc argument count
  611. * argv argument vector
  612. * Returns:
  613. * zero for success, a kdb diagnostic if error
  614. */
  615. struct kdb_macro {
  616. kdbtab_t cmd; /* Macro command */
  617. struct list_head statements; /* Associated statement list */
  618. };
  619. struct kdb_macro_statement {
  620. char *statement; /* Statement text */
  621. struct list_head list_node; /* Statement list node */
  622. };
  623. static struct kdb_macro *kdb_macro;
  624. static bool defcmd_in_progress;
  625. /* Forward references */
  626. static int kdb_exec_defcmd(int argc, const char **argv);
  627. static int kdb_defcmd2(const char *cmdstr, const char *argv0)
  628. {
  629. struct kdb_macro_statement *kms;
  630. if (!kdb_macro)
  631. return KDB_NOTIMP;
  632. if (strcmp(argv0, "endefcmd") == 0) {
  633. defcmd_in_progress = false;
  634. if (!list_empty(&kdb_macro->statements))
  635. kdb_register(&kdb_macro->cmd);
  636. return 0;
  637. }
  638. kms = kmalloc(sizeof(*kms), GFP_KDB);
  639. if (!kms) {
  640. kdb_printf("Could not allocate new kdb macro command: %s\n",
  641. cmdstr);
  642. return KDB_NOTIMP;
  643. }
  644. kms->statement = kdb_strdup(cmdstr, GFP_KDB);
  645. list_add_tail(&kms->list_node, &kdb_macro->statements);
  646. return 0;
  647. }
  648. static int kdb_defcmd(int argc, const char **argv)
  649. {
  650. kdbtab_t *mp;
  651. if (defcmd_in_progress) {
  652. kdb_printf("kdb: nested defcmd detected, assuming missing "
  653. "endefcmd\n");
  654. kdb_defcmd2("endefcmd", "endefcmd");
  655. }
  656. if (argc == 0) {
  657. kdbtab_t *kp;
  658. struct kdb_macro *kmp;
  659. struct kdb_macro_statement *kms;
  660. list_for_each_entry(kp, &kdb_cmds_head, list_node) {
  661. if (kp->func == kdb_exec_defcmd) {
  662. kdb_printf("defcmd %s \"%s\" \"%s\"\n",
  663. kp->name, kp->usage, kp->help);
  664. kmp = container_of(kp, struct kdb_macro, cmd);
  665. list_for_each_entry(kms, &kmp->statements,
  666. list_node)
  667. kdb_printf("%s", kms->statement);
  668. kdb_printf("endefcmd\n");
  669. }
  670. }
  671. return 0;
  672. }
  673. if (argc != 3)
  674. return KDB_ARGCOUNT;
  675. if (in_dbg_master()) {
  676. kdb_printf("Command only available during kdb_init()\n");
  677. return KDB_NOTIMP;
  678. }
  679. kdb_macro = kzalloc(sizeof(*kdb_macro), GFP_KDB);
  680. if (!kdb_macro)
  681. goto fail_defcmd;
  682. mp = &kdb_macro->cmd;
  683. mp->func = kdb_exec_defcmd;
  684. mp->minlen = 0;
  685. mp->flags = KDB_ENABLE_ALWAYS_SAFE;
  686. mp->name = kdb_strdup(argv[1], GFP_KDB);
  687. if (!mp->name)
  688. goto fail_name;
  689. mp->usage = kdb_strdup(argv[2], GFP_KDB);
  690. if (!mp->usage)
  691. goto fail_usage;
  692. mp->help = kdb_strdup(argv[3], GFP_KDB);
  693. if (!mp->help)
  694. goto fail_help;
  695. if (mp->usage[0] == '"') {
  696. strcpy(mp->usage, argv[2]+1);
  697. mp->usage[strlen(mp->usage)-1] = '\0';
  698. }
  699. if (mp->help[0] == '"') {
  700. strcpy(mp->help, argv[3]+1);
  701. mp->help[strlen(mp->help)-1] = '\0';
  702. }
  703. INIT_LIST_HEAD(&kdb_macro->statements);
  704. defcmd_in_progress = true;
  705. return 0;
  706. fail_help:
  707. kfree(mp->usage);
  708. fail_usage:
  709. kfree(mp->name);
  710. fail_name:
  711. kfree(kdb_macro);
  712. fail_defcmd:
  713. kdb_printf("Could not allocate new kdb_macro entry for %s\n", argv[1]);
  714. return KDB_NOTIMP;
  715. }
  716. /*
  717. * kdb_exec_defcmd - Execute the set of commands associated with this
  718. * defcmd name.
  719. * Inputs:
  720. * argc argument count
  721. * argv argument vector
  722. * Returns:
  723. * zero for success, a kdb diagnostic if error
  724. */
  725. static int kdb_exec_defcmd(int argc, const char **argv)
  726. {
  727. int ret;
  728. kdbtab_t *kp;
  729. struct kdb_macro *kmp;
  730. struct kdb_macro_statement *kms;
  731. if (argc != 0)
  732. return KDB_ARGCOUNT;
  733. list_for_each_entry(kp, &kdb_cmds_head, list_node) {
  734. if (strcmp(kp->name, argv[0]) == 0)
  735. break;
  736. }
  737. if (list_entry_is_head(kp, &kdb_cmds_head, list_node)) {
  738. kdb_printf("kdb_exec_defcmd: could not find commands for %s\n",
  739. argv[0]);
  740. return KDB_NOTIMP;
  741. }
  742. kmp = container_of(kp, struct kdb_macro, cmd);
  743. list_for_each_entry(kms, &kmp->statements, list_node) {
  744. /*
  745. * Recursive use of kdb_parse, do not use argv after this point.
  746. */
  747. argv = NULL;
  748. kdb_printf("[%s]kdb> %s\n", kmp->cmd.name, kms->statement);
  749. ret = kdb_parse(kms->statement);
  750. if (ret)
  751. return ret;
  752. }
  753. return 0;
  754. }
  755. /* Command history */
  756. #define KDB_CMD_HISTORY_COUNT 32
  757. #define CMD_BUFLEN 200 /* kdb_printf: max printline
  758. * size == 256 */
  759. static unsigned int cmd_head, cmd_tail;
  760. static unsigned int cmdptr;
  761. static char cmd_hist[KDB_CMD_HISTORY_COUNT][CMD_BUFLEN];
  762. static char cmd_cur[CMD_BUFLEN];
  763. /*
  764. * The "str" argument may point to something like | grep xyz
  765. */
  766. static void parse_grep(const char *str)
  767. {
  768. int len;
  769. char *cp = (char *)str, *cp2;
  770. /* sanity check: we should have been called with the \ first */
  771. if (*cp != '|')
  772. return;
  773. cp++;
  774. while (isspace(*cp))
  775. cp++;
  776. if (!str_has_prefix(cp, "grep ")) {
  777. kdb_printf("invalid 'pipe', see grephelp\n");
  778. return;
  779. }
  780. cp += 5;
  781. while (isspace(*cp))
  782. cp++;
  783. cp2 = strchr(cp, '\n');
  784. if (cp2)
  785. *cp2 = '\0'; /* remove the trailing newline */
  786. len = strlen(cp);
  787. if (len == 0) {
  788. kdb_printf("invalid 'pipe', see grephelp\n");
  789. return;
  790. }
  791. /* now cp points to a nonzero length search string */
  792. if (*cp == '"') {
  793. /* allow it be "x y z" by removing the "'s - there must
  794. be two of them */
  795. cp++;
  796. cp2 = strchr(cp, '"');
  797. if (!cp2) {
  798. kdb_printf("invalid quoted string, see grephelp\n");
  799. return;
  800. }
  801. *cp2 = '\0'; /* end the string where the 2nd " was */
  802. }
  803. kdb_grep_leading = 0;
  804. if (*cp == '^') {
  805. kdb_grep_leading = 1;
  806. cp++;
  807. }
  808. len = strlen(cp);
  809. kdb_grep_trailing = 0;
  810. if (*(cp+len-1) == '$') {
  811. kdb_grep_trailing = 1;
  812. *(cp+len-1) = '\0';
  813. }
  814. len = strlen(cp);
  815. if (!len)
  816. return;
  817. if (len >= KDB_GREP_STRLEN) {
  818. kdb_printf("search string too long\n");
  819. return;
  820. }
  821. strcpy(kdb_grep_string, cp);
  822. kdb_grepping_flag++;
  823. return;
  824. }
  825. /*
  826. * kdb_parse - Parse the command line, search the command table for a
  827. * matching command and invoke the command function. This
  828. * function may be called recursively, if it is, the second call
  829. * will overwrite argv and cbuf. It is the caller's
  830. * responsibility to save their argv if they recursively call
  831. * kdb_parse().
  832. * Parameters:
  833. * cmdstr The input command line to be parsed.
  834. * regs The registers at the time kdb was entered.
  835. * Returns:
  836. * Zero for success, a kdb diagnostic if failure.
  837. * Remarks:
  838. * Limited to 20 tokens.
  839. *
  840. * Real rudimentary tokenization. Basically only whitespace
  841. * is considered a token delimiter (but special consideration
  842. * is taken of the '=' sign as used by the 'set' command).
  843. *
  844. * The algorithm used to tokenize the input string relies on
  845. * there being at least one whitespace (or otherwise useless)
  846. * character between tokens as the character immediately following
  847. * the token is altered in-place to a null-byte to terminate the
  848. * token string.
  849. */
  850. #define MAXARGC 20
  851. int kdb_parse(const char *cmdstr)
  852. {
  853. static char *argv[MAXARGC];
  854. static int argc;
  855. static char cbuf[CMD_BUFLEN+2];
  856. char *cp;
  857. char *cpp, quoted;
  858. kdbtab_t *tp;
  859. int escaped, ignore_errors = 0, check_grep = 0;
  860. /*
  861. * First tokenize the command string.
  862. */
  863. cp = (char *)cmdstr;
  864. if (KDB_FLAG(CMD_INTERRUPT)) {
  865. /* Previous command was interrupted, newline must not
  866. * repeat the command */
  867. KDB_FLAG_CLEAR(CMD_INTERRUPT);
  868. KDB_STATE_SET(PAGER);
  869. argc = 0; /* no repeat */
  870. }
  871. if (*cp != '\n' && *cp != '\0') {
  872. argc = 0;
  873. cpp = cbuf;
  874. while (*cp) {
  875. /* skip whitespace */
  876. while (isspace(*cp))
  877. cp++;
  878. if ((*cp == '\0') || (*cp == '\n') ||
  879. (*cp == '#' && !defcmd_in_progress))
  880. break;
  881. /* special case: check for | grep pattern */
  882. if (*cp == '|') {
  883. check_grep++;
  884. break;
  885. }
  886. if (cpp >= cbuf + CMD_BUFLEN) {
  887. kdb_printf("kdb_parse: command buffer "
  888. "overflow, command ignored\n%s\n",
  889. cmdstr);
  890. return KDB_NOTFOUND;
  891. }
  892. if (argc >= MAXARGC - 1) {
  893. kdb_printf("kdb_parse: too many arguments, "
  894. "command ignored\n%s\n", cmdstr);
  895. return KDB_NOTFOUND;
  896. }
  897. argv[argc++] = cpp;
  898. escaped = 0;
  899. quoted = '\0';
  900. /* Copy to next unquoted and unescaped
  901. * whitespace or '=' */
  902. while (*cp && *cp != '\n' &&
  903. (escaped || quoted || !isspace(*cp))) {
  904. if (cpp >= cbuf + CMD_BUFLEN)
  905. break;
  906. if (escaped) {
  907. escaped = 0;
  908. *cpp++ = *cp++;
  909. continue;
  910. }
  911. if (*cp == '\\') {
  912. escaped = 1;
  913. ++cp;
  914. continue;
  915. }
  916. if (*cp == quoted)
  917. quoted = '\0';
  918. else if (*cp == '\'' || *cp == '"')
  919. quoted = *cp;
  920. *cpp = *cp++;
  921. if (*cpp == '=' && !quoted)
  922. break;
  923. ++cpp;
  924. }
  925. *cpp++ = '\0'; /* Squash a ws or '=' character */
  926. }
  927. }
  928. if (!argc)
  929. return 0;
  930. if (check_grep)
  931. parse_grep(cp);
  932. if (defcmd_in_progress) {
  933. int result = kdb_defcmd2(cmdstr, argv[0]);
  934. if (!defcmd_in_progress) {
  935. argc = 0; /* avoid repeat on endefcmd */
  936. *(argv[0]) = '\0';
  937. }
  938. return result;
  939. }
  940. if (argv[0][0] == '-' && argv[0][1] &&
  941. (argv[0][1] < '0' || argv[0][1] > '9')) {
  942. ignore_errors = 1;
  943. ++argv[0];
  944. }
  945. list_for_each_entry(tp, &kdb_cmds_head, list_node) {
  946. /*
  947. * If this command is allowed to be abbreviated,
  948. * check to see if this is it.
  949. */
  950. if (tp->minlen && (strlen(argv[0]) <= tp->minlen) &&
  951. (strncmp(argv[0], tp->name, tp->minlen) == 0))
  952. break;
  953. if (strcmp(argv[0], tp->name) == 0)
  954. break;
  955. }
  956. /*
  957. * If we don't find a command by this name, see if the first
  958. * few characters of this match any of the known commands.
  959. * e.g., md1c20 should match md.
  960. */
  961. if (list_entry_is_head(tp, &kdb_cmds_head, list_node)) {
  962. list_for_each_entry(tp, &kdb_cmds_head, list_node) {
  963. if (strncmp(argv[0], tp->name, strlen(tp->name)) == 0)
  964. break;
  965. }
  966. }
  967. if (!list_entry_is_head(tp, &kdb_cmds_head, list_node)) {
  968. int result;
  969. if (!kdb_check_flags(tp->flags, kdb_cmd_enabled, argc <= 1))
  970. return KDB_NOPERM;
  971. KDB_STATE_SET(CMD);
  972. result = (*tp->func)(argc-1, (const char **)argv);
  973. if (result && ignore_errors && result > KDB_CMD_GO)
  974. result = 0;
  975. KDB_STATE_CLEAR(CMD);
  976. if (tp->flags & KDB_REPEAT_WITH_ARGS)
  977. return result;
  978. argc = tp->flags & KDB_REPEAT_NO_ARGS ? 1 : 0;
  979. if (argv[argc])
  980. *(argv[argc]) = '\0';
  981. return result;
  982. }
  983. /*
  984. * If the input with which we were presented does not
  985. * map to an existing command, attempt to parse it as an
  986. * address argument and display the result. Useful for
  987. * obtaining the address of a variable, or the nearest symbol
  988. * to an address contained in a register.
  989. */
  990. {
  991. unsigned long value;
  992. char *name = NULL;
  993. long offset;
  994. int nextarg = 0;
  995. if (kdbgetaddrarg(0, (const char **)argv, &nextarg,
  996. &value, &offset, &name)) {
  997. return KDB_NOTFOUND;
  998. }
  999. kdb_printf("%s = ", argv[0]);
  1000. kdb_symbol_print(value, NULL, KDB_SP_DEFAULT);
  1001. kdb_printf("\n");
  1002. return 0;
  1003. }
  1004. }
  1005. static int handle_ctrl_cmd(char *cmd)
  1006. {
  1007. #define CTRL_P 16
  1008. #define CTRL_N 14
  1009. /* initial situation */
  1010. if (cmd_head == cmd_tail)
  1011. return 0;
  1012. switch (*cmd) {
  1013. case CTRL_P:
  1014. if (cmdptr != cmd_tail)
  1015. cmdptr = (cmdptr + KDB_CMD_HISTORY_COUNT - 1) %
  1016. KDB_CMD_HISTORY_COUNT;
  1017. strscpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
  1018. return 1;
  1019. case CTRL_N:
  1020. if (cmdptr != cmd_head)
  1021. cmdptr = (cmdptr+1) % KDB_CMD_HISTORY_COUNT;
  1022. strscpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
  1023. return 1;
  1024. }
  1025. return 0;
  1026. }
  1027. /*
  1028. * kdb_reboot - This function implements the 'reboot' command. Reboot
  1029. * the system immediately, or loop for ever on failure.
  1030. */
  1031. static int kdb_reboot(int argc, const char **argv)
  1032. {
  1033. emergency_restart();
  1034. kdb_printf("Hmm, kdb_reboot did not reboot, spinning here\n");
  1035. while (1)
  1036. cpu_relax();
  1037. /* NOTREACHED */
  1038. return 0;
  1039. }
  1040. static void kdb_dumpregs(struct pt_regs *regs)
  1041. {
  1042. int old_lvl = console_loglevel;
  1043. console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
  1044. kdb_trap_printk++;
  1045. show_regs(regs);
  1046. kdb_trap_printk--;
  1047. kdb_printf("\n");
  1048. console_loglevel = old_lvl;
  1049. }
  1050. static void kdb_set_current_task(struct task_struct *p)
  1051. {
  1052. kdb_current_task = p;
  1053. if (kdb_task_has_cpu(p)) {
  1054. kdb_current_regs = KDB_TSKREGS(kdb_process_cpu(p));
  1055. return;
  1056. }
  1057. kdb_current_regs = NULL;
  1058. }
  1059. static void drop_newline(char *buf)
  1060. {
  1061. size_t len = strlen(buf);
  1062. if (len == 0)
  1063. return;
  1064. if (*(buf + len - 1) == '\n')
  1065. *(buf + len - 1) = '\0';
  1066. }
  1067. /*
  1068. * kdb_local - The main code for kdb. This routine is invoked on a
  1069. * specific processor, it is not global. The main kdb() routine
  1070. * ensures that only one processor at a time is in this routine.
  1071. * This code is called with the real reason code on the first
  1072. * entry to a kdb session, thereafter it is called with reason
  1073. * SWITCH, even if the user goes back to the original cpu.
  1074. * Inputs:
  1075. * reason The reason KDB was invoked
  1076. * error The hardware-defined error code
  1077. * regs The exception frame at time of fault/breakpoint.
  1078. * db_result Result code from the break or debug point.
  1079. * Returns:
  1080. * 0 KDB was invoked for an event which it wasn't responsible
  1081. * 1 KDB handled the event for which it was invoked.
  1082. * KDB_CMD_GO User typed 'go'.
  1083. * KDB_CMD_CPU User switched to another cpu.
  1084. * KDB_CMD_SS Single step.
  1085. */
  1086. static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
  1087. kdb_dbtrap_t db_result)
  1088. {
  1089. char *cmdbuf;
  1090. int diag;
  1091. struct task_struct *kdb_current =
  1092. kdb_curr_task(raw_smp_processor_id());
  1093. KDB_DEBUG_STATE("kdb_local 1", reason);
  1094. kdb_check_for_lockdown();
  1095. kdb_go_count = 0;
  1096. if (reason == KDB_REASON_DEBUG) {
  1097. /* special case below */
  1098. } else {
  1099. kdb_printf("\nEntering kdb (current=0x%px, pid %d) ",
  1100. kdb_current, kdb_current ? kdb_current->pid : 0);
  1101. #if defined(CONFIG_SMP)
  1102. kdb_printf("on processor %d ", raw_smp_processor_id());
  1103. #endif
  1104. }
  1105. switch (reason) {
  1106. case KDB_REASON_DEBUG:
  1107. {
  1108. /*
  1109. * If re-entering kdb after a single step
  1110. * command, don't print the message.
  1111. */
  1112. switch (db_result) {
  1113. case KDB_DB_BPT:
  1114. kdb_printf("\nEntering kdb (0x%px, pid %d) ",
  1115. kdb_current, kdb_current->pid);
  1116. #if defined(CONFIG_SMP)
  1117. kdb_printf("on processor %d ", raw_smp_processor_id());
  1118. #endif
  1119. kdb_printf("due to Debug @ " kdb_machreg_fmt "\n",
  1120. instruction_pointer(regs));
  1121. break;
  1122. case KDB_DB_SS:
  1123. break;
  1124. case KDB_DB_SSBPT:
  1125. KDB_DEBUG_STATE("kdb_local 4", reason);
  1126. return 1; /* kdba_db_trap did the work */
  1127. default:
  1128. kdb_printf("kdb: Bad result from kdba_db_trap: %d\n",
  1129. db_result);
  1130. break;
  1131. }
  1132. }
  1133. break;
  1134. case KDB_REASON_ENTER:
  1135. if (KDB_STATE(KEYBOARD))
  1136. kdb_printf("due to Keyboard Entry\n");
  1137. else
  1138. kdb_printf("due to KDB_ENTER()\n");
  1139. break;
  1140. case KDB_REASON_KEYBOARD:
  1141. KDB_STATE_SET(KEYBOARD);
  1142. kdb_printf("due to Keyboard Entry\n");
  1143. break;
  1144. case KDB_REASON_ENTER_SLAVE:
  1145. /* drop through, slaves only get released via cpu switch */
  1146. case KDB_REASON_SWITCH:
  1147. kdb_printf("due to cpu switch\n");
  1148. break;
  1149. case KDB_REASON_OOPS:
  1150. kdb_printf("Oops: %s\n", kdb_diemsg);
  1151. kdb_printf("due to oops @ " kdb_machreg_fmt "\n",
  1152. instruction_pointer(regs));
  1153. kdb_dumpregs(regs);
  1154. break;
  1155. case KDB_REASON_SYSTEM_NMI:
  1156. kdb_printf("due to System NonMaskable Interrupt\n");
  1157. break;
  1158. case KDB_REASON_NMI:
  1159. kdb_printf("due to NonMaskable Interrupt @ "
  1160. kdb_machreg_fmt "\n",
  1161. instruction_pointer(regs));
  1162. break;
  1163. case KDB_REASON_SSTEP:
  1164. case KDB_REASON_BREAK:
  1165. kdb_printf("due to %s @ " kdb_machreg_fmt "\n",
  1166. reason == KDB_REASON_BREAK ?
  1167. "Breakpoint" : "SS trap", instruction_pointer(regs));
  1168. /*
  1169. * Determine if this breakpoint is one that we
  1170. * are interested in.
  1171. */
  1172. if (db_result != KDB_DB_BPT) {
  1173. kdb_printf("kdb: error return from kdba_bp_trap: %d\n",
  1174. db_result);
  1175. KDB_DEBUG_STATE("kdb_local 6", reason);
  1176. return 0; /* Not for us, dismiss it */
  1177. }
  1178. break;
  1179. case KDB_REASON_RECURSE:
  1180. kdb_printf("due to Recursion @ " kdb_machreg_fmt "\n",
  1181. instruction_pointer(regs));
  1182. break;
  1183. default:
  1184. kdb_printf("kdb: unexpected reason code: %d\n", reason);
  1185. KDB_DEBUG_STATE("kdb_local 8", reason);
  1186. return 0; /* Not for us, dismiss it */
  1187. }
  1188. while (1) {
  1189. /*
  1190. * Initialize pager context.
  1191. */
  1192. kdb_nextline = 1;
  1193. KDB_STATE_CLEAR(SUPPRESS);
  1194. kdb_grepping_flag = 0;
  1195. /* ensure the old search does not leak into '/' commands */
  1196. kdb_grep_string[0] = '\0';
  1197. cmdbuf = cmd_cur;
  1198. *cmdbuf = '\0';
  1199. *(cmd_hist[cmd_head]) = '\0';
  1200. do_full_getstr:
  1201. /* PROMPT can only be set if we have MEM_READ permission. */
  1202. snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
  1203. raw_smp_processor_id());
  1204. if (defcmd_in_progress)
  1205. strncat(kdb_prompt_str, "[defcmd]", CMD_BUFLEN);
  1206. /*
  1207. * Fetch command from keyboard
  1208. */
  1209. cmdbuf = kdb_getstr(cmdbuf, CMD_BUFLEN, kdb_prompt_str);
  1210. if (*cmdbuf != '\n') {
  1211. if (*cmdbuf < 32) {
  1212. if (cmdptr == cmd_head) {
  1213. strscpy(cmd_hist[cmd_head], cmd_cur,
  1214. CMD_BUFLEN);
  1215. *(cmd_hist[cmd_head] +
  1216. strlen(cmd_hist[cmd_head])-1) = '\0';
  1217. }
  1218. if (!handle_ctrl_cmd(cmdbuf))
  1219. *(cmd_cur+strlen(cmd_cur)-1) = '\0';
  1220. cmdbuf = cmd_cur;
  1221. goto do_full_getstr;
  1222. } else {
  1223. strscpy(cmd_hist[cmd_head], cmd_cur,
  1224. CMD_BUFLEN);
  1225. }
  1226. cmd_head = (cmd_head+1) % KDB_CMD_HISTORY_COUNT;
  1227. if (cmd_head == cmd_tail)
  1228. cmd_tail = (cmd_tail+1) % KDB_CMD_HISTORY_COUNT;
  1229. }
  1230. cmdptr = cmd_head;
  1231. diag = kdb_parse(cmdbuf);
  1232. if (diag == KDB_NOTFOUND) {
  1233. drop_newline(cmdbuf);
  1234. kdb_printf("Unknown kdb command: '%s'\n", cmdbuf);
  1235. diag = 0;
  1236. }
  1237. if (diag == KDB_CMD_GO
  1238. || diag == KDB_CMD_CPU
  1239. || diag == KDB_CMD_SS
  1240. || diag == KDB_CMD_KGDB)
  1241. break;
  1242. if (diag)
  1243. kdb_cmderror(diag);
  1244. }
  1245. KDB_DEBUG_STATE("kdb_local 9", diag);
  1246. return diag;
  1247. }
  1248. /*
  1249. * kdb_print_state - Print the state data for the current processor
  1250. * for debugging.
  1251. * Inputs:
  1252. * text Identifies the debug point
  1253. * value Any integer value to be printed, e.g. reason code.
  1254. */
  1255. void kdb_print_state(const char *text, int value)
  1256. {
  1257. kdb_printf("state: %s cpu %d value %d initial %d state %x\n",
  1258. text, raw_smp_processor_id(), value, kdb_initial_cpu,
  1259. kdb_state);
  1260. }
  1261. /*
  1262. * kdb_main_loop - After initial setup and assignment of the
  1263. * controlling cpu, all cpus are in this loop. One cpu is in
  1264. * control and will issue the kdb prompt, the others will spin
  1265. * until 'go' or cpu switch.
  1266. *
  1267. * To get a consistent view of the kernel stacks for all
  1268. * processes, this routine is invoked from the main kdb code via
  1269. * an architecture specific routine. kdba_main_loop is
  1270. * responsible for making the kernel stacks consistent for all
  1271. * processes, there should be no difference between a blocked
  1272. * process and a running process as far as kdb is concerned.
  1273. * Inputs:
  1274. * reason The reason KDB was invoked
  1275. * error The hardware-defined error code
  1276. * reason2 kdb's current reason code.
  1277. * Initially error but can change
  1278. * according to kdb state.
  1279. * db_result Result code from break or debug point.
  1280. * regs The exception frame at time of fault/breakpoint.
  1281. * should always be valid.
  1282. * Returns:
  1283. * 0 KDB was invoked for an event which it wasn't responsible
  1284. * 1 KDB handled the event for which it was invoked.
  1285. */
  1286. int kdb_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error,
  1287. kdb_dbtrap_t db_result, struct pt_regs *regs)
  1288. {
  1289. int result = 1;
  1290. /* Stay in kdb() until 'go', 'ss[b]' or an error */
  1291. while (1) {
  1292. /*
  1293. * All processors except the one that is in control
  1294. * will spin here.
  1295. */
  1296. KDB_DEBUG_STATE("kdb_main_loop 1", reason);
  1297. while (KDB_STATE(HOLD_CPU)) {
  1298. /* state KDB is turned off by kdb_cpu to see if the
  1299. * other cpus are still live, each cpu in this loop
  1300. * turns it back on.
  1301. */
  1302. if (!KDB_STATE(KDB))
  1303. KDB_STATE_SET(KDB);
  1304. }
  1305. KDB_STATE_CLEAR(SUPPRESS);
  1306. KDB_DEBUG_STATE("kdb_main_loop 2", reason);
  1307. if (KDB_STATE(LEAVING))
  1308. break; /* Another cpu said 'go' */
  1309. /* Still using kdb, this processor is in control */
  1310. result = kdb_local(reason2, error, regs, db_result);
  1311. KDB_DEBUG_STATE("kdb_main_loop 3", result);
  1312. if (result == KDB_CMD_CPU)
  1313. break;
  1314. if (result == KDB_CMD_SS) {
  1315. KDB_STATE_SET(DOING_SS);
  1316. break;
  1317. }
  1318. if (result == KDB_CMD_KGDB) {
  1319. if (!KDB_STATE(DOING_KGDB))
  1320. kdb_printf("Entering please attach debugger "
  1321. "or use $D#44+ or $3#33\n");
  1322. break;
  1323. }
  1324. if (result && result != 1 && result != KDB_CMD_GO)
  1325. kdb_printf("\nUnexpected kdb_local return code %d\n",
  1326. result);
  1327. KDB_DEBUG_STATE("kdb_main_loop 4", reason);
  1328. break;
  1329. }
  1330. if (KDB_STATE(DOING_SS))
  1331. KDB_STATE_CLEAR(SSBPT);
  1332. /* Clean up any keyboard devices before leaving */
  1333. kdb_kbd_cleanup_state();
  1334. return result;
  1335. }
  1336. /*
  1337. * kdb_mdr - This function implements the guts of the 'mdr', memory
  1338. * read command.
  1339. * mdr <addr arg>,<byte count>
  1340. * Inputs:
  1341. * addr Start address
  1342. * count Number of bytes
  1343. * Returns:
  1344. * Always 0. Any errors are detected and printed by kdb_getarea.
  1345. */
  1346. static int kdb_mdr(unsigned long addr, unsigned int count)
  1347. {
  1348. unsigned char c;
  1349. while (count--) {
  1350. if (kdb_getarea(c, addr))
  1351. return 0;
  1352. kdb_printf("%02x", c);
  1353. addr++;
  1354. }
  1355. kdb_printf("\n");
  1356. return 0;
  1357. }
  1358. /*
  1359. * kdb_md - This function implements the 'md', 'md1', 'md2', 'md4',
  1360. * 'md8' 'mdr' and 'mds' commands.
  1361. *
  1362. * md|mds [<addr arg> [<line count> [<radix>]]]
  1363. * mdWcN [<addr arg> [<line count> [<radix>]]]
  1364. * where W = is the width (1, 2, 4 or 8) and N is the count.
  1365. * for eg., md1c20 reads 20 bytes, 1 at a time.
  1366. * mdr <addr arg>,<byte count>
  1367. */
  1368. static void kdb_md_line(const char *fmtstr, unsigned long addr,
  1369. int symbolic, int nosect, int bytesperword,
  1370. int num, int repeat, int phys)
  1371. {
  1372. /* print just one line of data */
  1373. kdb_symtab_t symtab;
  1374. char cbuf[32];
  1375. char *c = cbuf;
  1376. int i;
  1377. int j;
  1378. unsigned long word;
  1379. memset(cbuf, '\0', sizeof(cbuf));
  1380. if (phys)
  1381. kdb_printf("phys " kdb_machreg_fmt0 " ", addr);
  1382. else
  1383. kdb_printf(kdb_machreg_fmt0 " ", addr);
  1384. for (i = 0; i < num && repeat--; i++) {
  1385. if (phys) {
  1386. if (kdb_getphysword(&word, addr, bytesperword))
  1387. break;
  1388. } else if (kdb_getword(&word, addr, bytesperword))
  1389. break;
  1390. kdb_printf(fmtstr, word);
  1391. if (symbolic)
  1392. kdbnearsym(word, &symtab);
  1393. else
  1394. memset(&symtab, 0, sizeof(symtab));
  1395. if (symtab.sym_name) {
  1396. kdb_symbol_print(word, &symtab, 0);
  1397. if (!nosect) {
  1398. kdb_printf("\n");
  1399. kdb_printf(" %s %s "
  1400. kdb_machreg_fmt " "
  1401. kdb_machreg_fmt " "
  1402. kdb_machreg_fmt, symtab.mod_name,
  1403. symtab.sec_name, symtab.sec_start,
  1404. symtab.sym_start, symtab.sym_end);
  1405. }
  1406. addr += bytesperword;
  1407. } else {
  1408. union {
  1409. u64 word;
  1410. unsigned char c[8];
  1411. } wc;
  1412. unsigned char *cp;
  1413. #ifdef __BIG_ENDIAN
  1414. cp = wc.c + 8 - bytesperword;
  1415. #else
  1416. cp = wc.c;
  1417. #endif
  1418. wc.word = word;
  1419. #define printable_char(c) \
  1420. ({unsigned char __c = c; isascii(__c) && isprint(__c) ? __c : '.'; })
  1421. for (j = 0; j < bytesperword; j++)
  1422. *c++ = printable_char(*cp++);
  1423. addr += bytesperword;
  1424. #undef printable_char
  1425. }
  1426. }
  1427. kdb_printf("%*s %s\n", (int)((num-i)*(2*bytesperword + 1)+1),
  1428. " ", cbuf);
  1429. }
  1430. static int kdb_md(int argc, const char **argv)
  1431. {
  1432. static unsigned long last_addr;
  1433. static int last_radix, last_bytesperword, last_repeat;
  1434. int radix = 16, mdcount = 8, bytesperword = KDB_WORD_SIZE, repeat;
  1435. int nosect = 0;
  1436. char fmtchar, fmtstr[64];
  1437. unsigned long addr;
  1438. unsigned long word;
  1439. long offset = 0;
  1440. int symbolic = 0;
  1441. int valid = 0;
  1442. int phys = 0;
  1443. int raw = 0;
  1444. kdbgetintenv("MDCOUNT", &mdcount);
  1445. kdbgetintenv("RADIX", &radix);
  1446. kdbgetintenv("BYTESPERWORD", &bytesperword);
  1447. /* Assume 'md <addr>' and start with environment values */
  1448. repeat = mdcount * 16 / bytesperword;
  1449. if (strcmp(argv[0], "mdr") == 0) {
  1450. if (argc == 2 || (argc == 0 && last_addr != 0))
  1451. valid = raw = 1;
  1452. else
  1453. return KDB_ARGCOUNT;
  1454. } else if (isdigit(argv[0][2])) {
  1455. bytesperword = (int)(argv[0][2] - '0');
  1456. if (bytesperword == 0) {
  1457. bytesperword = last_bytesperword;
  1458. if (bytesperword == 0)
  1459. bytesperword = 4;
  1460. }
  1461. last_bytesperword = bytesperword;
  1462. repeat = mdcount * 16 / bytesperword;
  1463. if (!argv[0][3])
  1464. valid = 1;
  1465. else if (argv[0][3] == 'c' && argv[0][4]) {
  1466. char *p;
  1467. repeat = simple_strtoul(argv[0] + 4, &p, 10);
  1468. mdcount = ((repeat * bytesperword) + 15) / 16;
  1469. valid = !*p;
  1470. }
  1471. last_repeat = repeat;
  1472. } else if (strcmp(argv[0], "md") == 0)
  1473. valid = 1;
  1474. else if (strcmp(argv[0], "mds") == 0)
  1475. valid = 1;
  1476. else if (strcmp(argv[0], "mdp") == 0) {
  1477. phys = valid = 1;
  1478. }
  1479. if (!valid)
  1480. return KDB_NOTFOUND;
  1481. if (argc == 0) {
  1482. if (last_addr == 0)
  1483. return KDB_ARGCOUNT;
  1484. addr = last_addr;
  1485. radix = last_radix;
  1486. bytesperword = last_bytesperword;
  1487. repeat = last_repeat;
  1488. if (raw)
  1489. mdcount = repeat;
  1490. else
  1491. mdcount = ((repeat * bytesperword) + 15) / 16;
  1492. }
  1493. if (argc) {
  1494. unsigned long val;
  1495. int diag, nextarg = 1;
  1496. diag = kdbgetaddrarg(argc, argv, &nextarg, &addr,
  1497. &offset, NULL);
  1498. if (diag)
  1499. return diag;
  1500. if (argc > nextarg+2)
  1501. return KDB_ARGCOUNT;
  1502. if (argc >= nextarg) {
  1503. diag = kdbgetularg(argv[nextarg], &val);
  1504. if (!diag) {
  1505. mdcount = (int) val;
  1506. if (raw)
  1507. repeat = mdcount;
  1508. else
  1509. repeat = mdcount * 16 / bytesperword;
  1510. }
  1511. }
  1512. if (argc >= nextarg+1) {
  1513. diag = kdbgetularg(argv[nextarg+1], &val);
  1514. if (!diag)
  1515. radix = (int) val;
  1516. }
  1517. }
  1518. if (strcmp(argv[0], "mdr") == 0) {
  1519. int ret;
  1520. last_addr = addr;
  1521. ret = kdb_mdr(addr, mdcount);
  1522. last_addr += mdcount;
  1523. last_repeat = mdcount;
  1524. last_bytesperword = bytesperword; // to make REPEAT happy
  1525. return ret;
  1526. }
  1527. switch (radix) {
  1528. case 10:
  1529. fmtchar = 'd';
  1530. break;
  1531. case 16:
  1532. fmtchar = 'x';
  1533. break;
  1534. case 8:
  1535. fmtchar = 'o';
  1536. break;
  1537. default:
  1538. return KDB_BADRADIX;
  1539. }
  1540. last_radix = radix;
  1541. if (bytesperword > KDB_WORD_SIZE)
  1542. return KDB_BADWIDTH;
  1543. switch (bytesperword) {
  1544. case 8:
  1545. sprintf(fmtstr, "%%16.16l%c ", fmtchar);
  1546. break;
  1547. case 4:
  1548. sprintf(fmtstr, "%%8.8l%c ", fmtchar);
  1549. break;
  1550. case 2:
  1551. sprintf(fmtstr, "%%4.4l%c ", fmtchar);
  1552. break;
  1553. case 1:
  1554. sprintf(fmtstr, "%%2.2l%c ", fmtchar);
  1555. break;
  1556. default:
  1557. return KDB_BADWIDTH;
  1558. }
  1559. last_repeat = repeat;
  1560. last_bytesperword = bytesperword;
  1561. if (strcmp(argv[0], "mds") == 0) {
  1562. symbolic = 1;
  1563. /* Do not save these changes as last_*, they are temporary mds
  1564. * overrides.
  1565. */
  1566. bytesperword = KDB_WORD_SIZE;
  1567. repeat = mdcount;
  1568. kdbgetintenv("NOSECT", &nosect);
  1569. }
  1570. /* Round address down modulo BYTESPERWORD */
  1571. addr &= ~(bytesperword-1);
  1572. while (repeat > 0) {
  1573. unsigned long a;
  1574. int n, z, num = (symbolic ? 1 : (16 / bytesperword));
  1575. if (KDB_FLAG(CMD_INTERRUPT))
  1576. return 0;
  1577. for (a = addr, z = 0; z < repeat; a += bytesperword, ++z) {
  1578. if (phys) {
  1579. if (kdb_getphysword(&word, a, bytesperword)
  1580. || word)
  1581. break;
  1582. } else if (kdb_getword(&word, a, bytesperword) || word)
  1583. break;
  1584. }
  1585. n = min(num, repeat);
  1586. kdb_md_line(fmtstr, addr, symbolic, nosect, bytesperword,
  1587. num, repeat, phys);
  1588. addr += bytesperword * n;
  1589. repeat -= n;
  1590. z = (z + num - 1) / num;
  1591. if (z > 2) {
  1592. int s = num * (z-2);
  1593. kdb_printf(kdb_machreg_fmt0 "-" kdb_machreg_fmt0
  1594. " zero suppressed\n",
  1595. addr, addr + bytesperword * s - 1);
  1596. addr += bytesperword * s;
  1597. repeat -= s;
  1598. }
  1599. }
  1600. last_addr = addr;
  1601. return 0;
  1602. }
  1603. /*
  1604. * kdb_mm - This function implements the 'mm' command.
  1605. * mm address-expression new-value
  1606. * Remarks:
  1607. * mm works on machine words, mmW works on bytes.
  1608. */
  1609. static int kdb_mm(int argc, const char **argv)
  1610. {
  1611. int diag;
  1612. unsigned long addr;
  1613. long offset = 0;
  1614. unsigned long contents;
  1615. int nextarg;
  1616. int width;
  1617. if (argv[0][2] && !isdigit(argv[0][2]))
  1618. return KDB_NOTFOUND;
  1619. if (argc < 2)
  1620. return KDB_ARGCOUNT;
  1621. nextarg = 1;
  1622. diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
  1623. if (diag)
  1624. return diag;
  1625. if (nextarg > argc)
  1626. return KDB_ARGCOUNT;
  1627. diag = kdbgetaddrarg(argc, argv, &nextarg, &contents, NULL, NULL);
  1628. if (diag)
  1629. return diag;
  1630. if (nextarg != argc + 1)
  1631. return KDB_ARGCOUNT;
  1632. width = argv[0][2] ? (argv[0][2] - '0') : (KDB_WORD_SIZE);
  1633. diag = kdb_putword(addr, contents, width);
  1634. if (diag)
  1635. return diag;
  1636. kdb_printf(kdb_machreg_fmt " = " kdb_machreg_fmt "\n", addr, contents);
  1637. return 0;
  1638. }
  1639. /*
  1640. * kdb_go - This function implements the 'go' command.
  1641. * go [address-expression]
  1642. */
  1643. static int kdb_go(int argc, const char **argv)
  1644. {
  1645. unsigned long addr;
  1646. int diag;
  1647. int nextarg;
  1648. long offset;
  1649. if (raw_smp_processor_id() != kdb_initial_cpu) {
  1650. kdb_printf("go must execute on the entry cpu, "
  1651. "please use \"cpu %d\" and then execute go\n",
  1652. kdb_initial_cpu);
  1653. return KDB_BADCPUNUM;
  1654. }
  1655. if (argc == 1) {
  1656. nextarg = 1;
  1657. diag = kdbgetaddrarg(argc, argv, &nextarg,
  1658. &addr, &offset, NULL);
  1659. if (diag)
  1660. return diag;
  1661. } else if (argc) {
  1662. return KDB_ARGCOUNT;
  1663. }
  1664. diag = KDB_CMD_GO;
  1665. if (KDB_FLAG(CATASTROPHIC)) {
  1666. kdb_printf("Catastrophic error detected\n");
  1667. kdb_printf("kdb_continue_catastrophic=%d, ",
  1668. kdb_continue_catastrophic);
  1669. if (kdb_continue_catastrophic == 0 && kdb_go_count++ == 0) {
  1670. kdb_printf("type go a second time if you really want "
  1671. "to continue\n");
  1672. return 0;
  1673. }
  1674. if (kdb_continue_catastrophic == 2) {
  1675. kdb_printf("forcing reboot\n");
  1676. kdb_reboot(0, NULL);
  1677. }
  1678. kdb_printf("attempting to continue\n");
  1679. }
  1680. return diag;
  1681. }
  1682. /*
  1683. * kdb_rd - This function implements the 'rd' command.
  1684. */
  1685. static int kdb_rd(int argc, const char **argv)
  1686. {
  1687. int len = kdb_check_regs();
  1688. #if DBG_MAX_REG_NUM > 0
  1689. int i;
  1690. char *rname;
  1691. int rsize;
  1692. u64 reg64;
  1693. u32 reg32;
  1694. u16 reg16;
  1695. u8 reg8;
  1696. if (len)
  1697. return len;
  1698. for (i = 0; i < DBG_MAX_REG_NUM; i++) {
  1699. rsize = dbg_reg_def[i].size * 2;
  1700. if (rsize > 16)
  1701. rsize = 2;
  1702. if (len + strlen(dbg_reg_def[i].name) + 4 + rsize > 80) {
  1703. len = 0;
  1704. kdb_printf("\n");
  1705. }
  1706. if (len)
  1707. len += kdb_printf(" ");
  1708. switch(dbg_reg_def[i].size * 8) {
  1709. case 8:
  1710. rname = dbg_get_reg(i, &reg8, kdb_current_regs);
  1711. if (!rname)
  1712. break;
  1713. len += kdb_printf("%s: %02x", rname, reg8);
  1714. break;
  1715. case 16:
  1716. rname = dbg_get_reg(i, &reg16, kdb_current_regs);
  1717. if (!rname)
  1718. break;
  1719. len += kdb_printf("%s: %04x", rname, reg16);
  1720. break;
  1721. case 32:
  1722. rname = dbg_get_reg(i, &reg32, kdb_current_regs);
  1723. if (!rname)
  1724. break;
  1725. len += kdb_printf("%s: %08x", rname, reg32);
  1726. break;
  1727. case 64:
  1728. rname = dbg_get_reg(i, &reg64, kdb_current_regs);
  1729. if (!rname)
  1730. break;
  1731. len += kdb_printf("%s: %016llx", rname, reg64);
  1732. break;
  1733. default:
  1734. len += kdb_printf("%s: ??", dbg_reg_def[i].name);
  1735. }
  1736. }
  1737. kdb_printf("\n");
  1738. #else
  1739. if (len)
  1740. return len;
  1741. kdb_dumpregs(kdb_current_regs);
  1742. #endif
  1743. return 0;
  1744. }
  1745. /*
  1746. * kdb_rm - This function implements the 'rm' (register modify) command.
  1747. * rm register-name new-contents
  1748. * Remarks:
  1749. * Allows register modification with the same restrictions as gdb
  1750. */
  1751. static int kdb_rm(int argc, const char **argv)
  1752. {
  1753. #if DBG_MAX_REG_NUM > 0
  1754. int diag;
  1755. const char *rname;
  1756. int i;
  1757. u64 reg64;
  1758. u32 reg32;
  1759. u16 reg16;
  1760. u8 reg8;
  1761. if (argc != 2)
  1762. return KDB_ARGCOUNT;
  1763. /*
  1764. * Allow presence or absence of leading '%' symbol.
  1765. */
  1766. rname = argv[1];
  1767. if (*rname == '%')
  1768. rname++;
  1769. diag = kdbgetu64arg(argv[2], &reg64);
  1770. if (diag)
  1771. return diag;
  1772. diag = kdb_check_regs();
  1773. if (diag)
  1774. return diag;
  1775. diag = KDB_BADREG;
  1776. for (i = 0; i < DBG_MAX_REG_NUM; i++) {
  1777. if (strcmp(rname, dbg_reg_def[i].name) == 0) {
  1778. diag = 0;
  1779. break;
  1780. }
  1781. }
  1782. if (!diag) {
  1783. switch(dbg_reg_def[i].size * 8) {
  1784. case 8:
  1785. reg8 = reg64;
  1786. dbg_set_reg(i, &reg8, kdb_current_regs);
  1787. break;
  1788. case 16:
  1789. reg16 = reg64;
  1790. dbg_set_reg(i, &reg16, kdb_current_regs);
  1791. break;
  1792. case 32:
  1793. reg32 = reg64;
  1794. dbg_set_reg(i, &reg32, kdb_current_regs);
  1795. break;
  1796. case 64:
  1797. dbg_set_reg(i, &reg64, kdb_current_regs);
  1798. break;
  1799. }
  1800. }
  1801. return diag;
  1802. #else
  1803. kdb_printf("ERROR: Register set currently not implemented\n");
  1804. return 0;
  1805. #endif
  1806. }
  1807. #if defined(CONFIG_MAGIC_SYSRQ)
  1808. /*
  1809. * kdb_sr - This function implements the 'sr' (SYSRQ key) command
  1810. * which interfaces to the soi-disant MAGIC SYSRQ functionality.
  1811. * sr <magic-sysrq-code>
  1812. */
  1813. static int kdb_sr(int argc, const char **argv)
  1814. {
  1815. bool check_mask =
  1816. !kdb_check_flags(KDB_ENABLE_ALL, kdb_cmd_enabled, false);
  1817. if (argc != 1)
  1818. return KDB_ARGCOUNT;
  1819. kdb_trap_printk++;
  1820. __handle_sysrq(*argv[1], check_mask);
  1821. kdb_trap_printk--;
  1822. return 0;
  1823. }
  1824. #endif /* CONFIG_MAGIC_SYSRQ */
  1825. /*
  1826. * kdb_ef - This function implements the 'regs' (display exception
  1827. * frame) command. This command takes an address and expects to
  1828. * find an exception frame at that address, formats and prints
  1829. * it.
  1830. * regs address-expression
  1831. * Remarks:
  1832. * Not done yet.
  1833. */
  1834. static int kdb_ef(int argc, const char **argv)
  1835. {
  1836. int diag;
  1837. unsigned long addr;
  1838. long offset;
  1839. int nextarg;
  1840. if (argc != 1)
  1841. return KDB_ARGCOUNT;
  1842. nextarg = 1;
  1843. diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
  1844. if (diag)
  1845. return diag;
  1846. show_regs((struct pt_regs *)addr);
  1847. return 0;
  1848. }
  1849. /*
  1850. * kdb_env - This function implements the 'env' command. Display the
  1851. * current environment variables.
  1852. */
  1853. static int kdb_env(int argc, const char **argv)
  1854. {
  1855. kdb_printenv();
  1856. if (KDB_DEBUG(MASK))
  1857. kdb_printf("KDBDEBUG=0x%x\n",
  1858. (kdb_flags & KDB_DEBUG(MASK)) >> KDB_DEBUG_FLAG_SHIFT);
  1859. return 0;
  1860. }
  1861. #ifdef CONFIG_PRINTK
  1862. /*
  1863. * kdb_dmesg - This function implements the 'dmesg' command to display
  1864. * the contents of the syslog buffer.
  1865. * dmesg [lines] [adjust]
  1866. */
  1867. static int kdb_dmesg(int argc, const char **argv)
  1868. {
  1869. int diag;
  1870. int logging;
  1871. int lines = 0;
  1872. int adjust = 0;
  1873. int n = 0;
  1874. int skip = 0;
  1875. struct kmsg_dump_iter iter;
  1876. size_t len;
  1877. char buf[201];
  1878. if (argc > 2)
  1879. return KDB_ARGCOUNT;
  1880. if (argc) {
  1881. char *cp;
  1882. lines = simple_strtol(argv[1], &cp, 0);
  1883. if (*cp)
  1884. lines = 0;
  1885. if (argc > 1) {
  1886. adjust = simple_strtoul(argv[2], &cp, 0);
  1887. if (*cp || adjust < 0)
  1888. adjust = 0;
  1889. }
  1890. }
  1891. /* disable LOGGING if set */
  1892. diag = kdbgetintenv("LOGGING", &logging);
  1893. if (!diag && logging) {
  1894. const char *setargs[] = { "set", "LOGGING", "0" };
  1895. kdb_set(2, setargs);
  1896. }
  1897. kmsg_dump_rewind(&iter);
  1898. while (kmsg_dump_get_line(&iter, 1, NULL, 0, NULL))
  1899. n++;
  1900. if (lines < 0) {
  1901. if (adjust >= n)
  1902. kdb_printf("buffer only contains %d lines, nothing "
  1903. "printed\n", n);
  1904. else if (adjust - lines >= n)
  1905. kdb_printf("buffer only contains %d lines, last %d "
  1906. "lines printed\n", n, n - adjust);
  1907. skip = adjust;
  1908. lines = abs(lines);
  1909. } else if (lines > 0) {
  1910. skip = n - lines - adjust;
  1911. lines = abs(lines);
  1912. if (adjust >= n) {
  1913. kdb_printf("buffer only contains %d lines, "
  1914. "nothing printed\n", n);
  1915. skip = n;
  1916. } else if (skip < 0) {
  1917. lines += skip;
  1918. skip = 0;
  1919. kdb_printf("buffer only contains %d lines, first "
  1920. "%d lines printed\n", n, lines);
  1921. }
  1922. } else {
  1923. lines = n;
  1924. }
  1925. if (skip >= n || skip < 0)
  1926. return 0;
  1927. kmsg_dump_rewind(&iter);
  1928. while (kmsg_dump_get_line(&iter, 1, buf, sizeof(buf), &len)) {
  1929. if (skip) {
  1930. skip--;
  1931. continue;
  1932. }
  1933. if (!lines--)
  1934. break;
  1935. if (KDB_FLAG(CMD_INTERRUPT))
  1936. return 0;
  1937. kdb_printf("%.*s\n", (int)len - 1, buf);
  1938. }
  1939. return 0;
  1940. }
  1941. #endif /* CONFIG_PRINTK */
  1942. /* Make sure we balance enable/disable calls, must disable first. */
  1943. static atomic_t kdb_nmi_disabled;
  1944. static int kdb_disable_nmi(int argc, const char *argv[])
  1945. {
  1946. if (atomic_read(&kdb_nmi_disabled))
  1947. return 0;
  1948. atomic_set(&kdb_nmi_disabled, 1);
  1949. arch_kgdb_ops.enable_nmi(0);
  1950. return 0;
  1951. }
  1952. static int kdb_param_enable_nmi(const char *val, const struct kernel_param *kp)
  1953. {
  1954. if (!atomic_add_unless(&kdb_nmi_disabled, -1, 0))
  1955. return -EINVAL;
  1956. arch_kgdb_ops.enable_nmi(1);
  1957. return 0;
  1958. }
  1959. static const struct kernel_param_ops kdb_param_ops_enable_nmi = {
  1960. .set = kdb_param_enable_nmi,
  1961. };
  1962. module_param_cb(enable_nmi, &kdb_param_ops_enable_nmi, NULL, 0600);
  1963. /*
  1964. * kdb_cpu - This function implements the 'cpu' command.
  1965. * cpu [<cpunum>]
  1966. * Returns:
  1967. * KDB_CMD_CPU for success, a kdb diagnostic if error
  1968. */
  1969. static void kdb_cpu_status(void)
  1970. {
  1971. int i, start_cpu, first_print = 1;
  1972. char state, prev_state = '?';
  1973. kdb_printf("Currently on cpu %d\n", raw_smp_processor_id());
  1974. kdb_printf("Available cpus: ");
  1975. for (start_cpu = -1, i = 0; i < NR_CPUS; i++) {
  1976. if (!cpu_online(i)) {
  1977. state = 'F'; /* cpu is offline */
  1978. } else if (!kgdb_info[i].enter_kgdb) {
  1979. state = 'D'; /* cpu is online but unresponsive */
  1980. } else {
  1981. state = ' '; /* cpu is responding to kdb */
  1982. if (kdb_task_state_char(KDB_TSK(i)) == '-')
  1983. state = '-'; /* idle task */
  1984. }
  1985. if (state != prev_state) {
  1986. if (prev_state != '?') {
  1987. if (!first_print)
  1988. kdb_printf(", ");
  1989. first_print = 0;
  1990. kdb_printf("%d", start_cpu);
  1991. if (start_cpu < i-1)
  1992. kdb_printf("-%d", i-1);
  1993. if (prev_state != ' ')
  1994. kdb_printf("(%c)", prev_state);
  1995. }
  1996. prev_state = state;
  1997. start_cpu = i;
  1998. }
  1999. }
  2000. /* print the trailing cpus, ignoring them if they are all offline */
  2001. if (prev_state != 'F') {
  2002. if (!first_print)
  2003. kdb_printf(", ");
  2004. kdb_printf("%d", start_cpu);
  2005. if (start_cpu < i-1)
  2006. kdb_printf("-%d", i-1);
  2007. if (prev_state != ' ')
  2008. kdb_printf("(%c)", prev_state);
  2009. }
  2010. kdb_printf("\n");
  2011. }
  2012. static int kdb_cpu(int argc, const char **argv)
  2013. {
  2014. unsigned long cpunum;
  2015. int diag;
  2016. if (argc == 0) {
  2017. kdb_cpu_status();
  2018. return 0;
  2019. }
  2020. if (argc != 1)
  2021. return KDB_ARGCOUNT;
  2022. diag = kdbgetularg(argv[1], &cpunum);
  2023. if (diag)
  2024. return diag;
  2025. /*
  2026. * Validate cpunum
  2027. */
  2028. if ((cpunum >= CONFIG_NR_CPUS) || !kgdb_info[cpunum].enter_kgdb)
  2029. return KDB_BADCPUNUM;
  2030. dbg_switch_cpu = cpunum;
  2031. /*
  2032. * Switch to other cpu
  2033. */
  2034. return KDB_CMD_CPU;
  2035. }
  2036. /* The user may not realize that ps/bta with no parameters does not print idle
  2037. * or sleeping system daemon processes, so tell them how many were suppressed.
  2038. */
  2039. void kdb_ps_suppressed(void)
  2040. {
  2041. int idle = 0, daemon = 0;
  2042. unsigned long cpu;
  2043. const struct task_struct *p, *g;
  2044. for_each_online_cpu(cpu) {
  2045. p = kdb_curr_task(cpu);
  2046. if (kdb_task_state(p, "-"))
  2047. ++idle;
  2048. }
  2049. for_each_process_thread(g, p) {
  2050. if (kdb_task_state(p, "ims"))
  2051. ++daemon;
  2052. }
  2053. if (idle || daemon) {
  2054. if (idle)
  2055. kdb_printf("%d idle process%s (state -)%s\n",
  2056. idle, idle == 1 ? "" : "es",
  2057. daemon ? " and " : "");
  2058. if (daemon)
  2059. kdb_printf("%d sleeping system daemon (state [ims]) "
  2060. "process%s", daemon,
  2061. daemon == 1 ? "" : "es");
  2062. kdb_printf(" suppressed,\nuse 'ps A' to see all.\n");
  2063. }
  2064. }
  2065. void kdb_ps1(const struct task_struct *p)
  2066. {
  2067. int cpu;
  2068. unsigned long tmp;
  2069. if (!p ||
  2070. copy_from_kernel_nofault(&tmp, (char *)p, sizeof(unsigned long)))
  2071. return;
  2072. cpu = kdb_process_cpu(p);
  2073. kdb_printf("0x%px %8d %8d %d %4d %c 0x%px %c%s\n",
  2074. (void *)p, p->pid, p->parent->pid,
  2075. kdb_task_has_cpu(p), kdb_process_cpu(p),
  2076. kdb_task_state_char(p),
  2077. (void *)(&p->thread),
  2078. p == kdb_curr_task(raw_smp_processor_id()) ? '*' : ' ',
  2079. p->comm);
  2080. if (kdb_task_has_cpu(p)) {
  2081. if (!KDB_TSK(cpu)) {
  2082. kdb_printf(" Error: no saved data for this cpu\n");
  2083. } else {
  2084. if (KDB_TSK(cpu) != p)
  2085. kdb_printf(" Error: does not match running "
  2086. "process table (0x%px)\n", KDB_TSK(cpu));
  2087. }
  2088. }
  2089. }
  2090. /*
  2091. * kdb_ps - This function implements the 'ps' command which shows a
  2092. * list of the active processes.
  2093. *
  2094. * ps [<state_chars>] Show processes, optionally selecting only those whose
  2095. * state character is found in <state_chars>.
  2096. */
  2097. static int kdb_ps(int argc, const char **argv)
  2098. {
  2099. struct task_struct *g, *p;
  2100. const char *mask;
  2101. unsigned long cpu;
  2102. if (argc == 0)
  2103. kdb_ps_suppressed();
  2104. kdb_printf("%-*s Pid Parent [*] cpu State %-*s Command\n",
  2105. (int)(2*sizeof(void *))+2, "Task Addr",
  2106. (int)(2*sizeof(void *))+2, "Thread");
  2107. mask = argc ? argv[1] : kdbgetenv("PS");
  2108. /* Run the active tasks first */
  2109. for_each_online_cpu(cpu) {
  2110. if (KDB_FLAG(CMD_INTERRUPT))
  2111. return 0;
  2112. p = kdb_curr_task(cpu);
  2113. if (kdb_task_state(p, mask))
  2114. kdb_ps1(p);
  2115. }
  2116. kdb_printf("\n");
  2117. /* Now the real tasks */
  2118. for_each_process_thread(g, p) {
  2119. if (KDB_FLAG(CMD_INTERRUPT))
  2120. return 0;
  2121. if (kdb_task_state(p, mask))
  2122. kdb_ps1(p);
  2123. }
  2124. return 0;
  2125. }
  2126. /*
  2127. * kdb_pid - This function implements the 'pid' command which switches
  2128. * the currently active process.
  2129. * pid [<pid> | R]
  2130. */
  2131. static int kdb_pid(int argc, const char **argv)
  2132. {
  2133. struct task_struct *p;
  2134. unsigned long val;
  2135. int diag;
  2136. if (argc > 1)
  2137. return KDB_ARGCOUNT;
  2138. if (argc) {
  2139. if (strcmp(argv[1], "R") == 0) {
  2140. p = KDB_TSK(kdb_initial_cpu);
  2141. } else {
  2142. diag = kdbgetularg(argv[1], &val);
  2143. if (diag)
  2144. return KDB_BADINT;
  2145. p = find_task_by_pid_ns((pid_t)val, &init_pid_ns);
  2146. if (!p) {
  2147. kdb_printf("No task with pid=%d\n", (pid_t)val);
  2148. return 0;
  2149. }
  2150. }
  2151. kdb_set_current_task(p);
  2152. }
  2153. kdb_printf("KDB current process is %s(pid=%d)\n",
  2154. kdb_current_task->comm,
  2155. kdb_current_task->pid);
  2156. return 0;
  2157. }
  2158. static int kdb_kgdb(int argc, const char **argv)
  2159. {
  2160. return KDB_CMD_KGDB;
  2161. }
  2162. /*
  2163. * kdb_help - This function implements the 'help' and '?' commands.
  2164. */
  2165. static int kdb_help(int argc, const char **argv)
  2166. {
  2167. kdbtab_t *kt;
  2168. kdb_printf("%-15.15s %-20.20s %s\n", "Command", "Usage", "Description");
  2169. kdb_printf("-----------------------------"
  2170. "-----------------------------\n");
  2171. list_for_each_entry(kt, &kdb_cmds_head, list_node) {
  2172. char *space = "";
  2173. if (KDB_FLAG(CMD_INTERRUPT))
  2174. return 0;
  2175. if (!kdb_check_flags(kt->flags, kdb_cmd_enabled, true))
  2176. continue;
  2177. if (strlen(kt->usage) > 20)
  2178. space = "\n ";
  2179. kdb_printf("%-15.15s %-20s%s%s\n", kt->name,
  2180. kt->usage, space, kt->help);
  2181. }
  2182. return 0;
  2183. }
  2184. /*
  2185. * kdb_kill - This function implements the 'kill' commands.
  2186. */
  2187. static int kdb_kill(int argc, const char **argv)
  2188. {
  2189. long sig, pid;
  2190. char *endp;
  2191. struct task_struct *p;
  2192. if (argc != 2)
  2193. return KDB_ARGCOUNT;
  2194. sig = simple_strtol(argv[1], &endp, 0);
  2195. if (*endp)
  2196. return KDB_BADINT;
  2197. if ((sig >= 0) || !valid_signal(-sig)) {
  2198. kdb_printf("Invalid signal parameter.<-signal>\n");
  2199. return 0;
  2200. }
  2201. sig = -sig;
  2202. pid = simple_strtol(argv[2], &endp, 0);
  2203. if (*endp)
  2204. return KDB_BADINT;
  2205. if (pid <= 0) {
  2206. kdb_printf("Process ID must be large than 0.\n");
  2207. return 0;
  2208. }
  2209. /* Find the process. */
  2210. p = find_task_by_pid_ns(pid, &init_pid_ns);
  2211. if (!p) {
  2212. kdb_printf("The specified process isn't found.\n");
  2213. return 0;
  2214. }
  2215. p = p->group_leader;
  2216. kdb_send_sig(p, sig);
  2217. return 0;
  2218. }
  2219. /*
  2220. * Most of this code has been lifted from kernel/timer.c::sys_sysinfo().
  2221. * I cannot call that code directly from kdb, it has an unconditional
  2222. * cli()/sti() and calls routines that take locks which can stop the debugger.
  2223. */
  2224. static void kdb_sysinfo(struct sysinfo *val)
  2225. {
  2226. u64 uptime = ktime_get_mono_fast_ns();
  2227. memset(val, 0, sizeof(*val));
  2228. val->uptime = div_u64(uptime, NSEC_PER_SEC);
  2229. val->loads[0] = avenrun[0];
  2230. val->loads[1] = avenrun[1];
  2231. val->loads[2] = avenrun[2];
  2232. val->procs = nr_threads-1;
  2233. si_meminfo(val);
  2234. return;
  2235. }
  2236. /*
  2237. * kdb_summary - This function implements the 'summary' command.
  2238. */
  2239. static int kdb_summary(int argc, const char **argv)
  2240. {
  2241. time64_t now;
  2242. struct sysinfo val;
  2243. if (argc)
  2244. return KDB_ARGCOUNT;
  2245. kdb_printf("sysname %s\n", init_uts_ns.name.sysname);
  2246. kdb_printf("release %s\n", init_uts_ns.name.release);
  2247. kdb_printf("version %s\n", init_uts_ns.name.version);
  2248. kdb_printf("machine %s\n", init_uts_ns.name.machine);
  2249. kdb_printf("nodename %s\n", init_uts_ns.name.nodename);
  2250. kdb_printf("domainname %s\n", init_uts_ns.name.domainname);
  2251. now = __ktime_get_real_seconds();
  2252. kdb_printf("date %ptTs tz_minuteswest %d\n", &now, sys_tz.tz_minuteswest);
  2253. kdb_sysinfo(&val);
  2254. kdb_printf("uptime ");
  2255. if (val.uptime > (24*60*60)) {
  2256. int days = val.uptime / (24*60*60);
  2257. val.uptime %= (24*60*60);
  2258. kdb_printf("%d day%s ", days, days == 1 ? "" : "s");
  2259. }
  2260. kdb_printf("%02ld:%02ld\n", val.uptime/(60*60), (val.uptime/60)%60);
  2261. kdb_printf("load avg %ld.%02ld %ld.%02ld %ld.%02ld\n",
  2262. LOAD_INT(val.loads[0]), LOAD_FRAC(val.loads[0]),
  2263. LOAD_INT(val.loads[1]), LOAD_FRAC(val.loads[1]),
  2264. LOAD_INT(val.loads[2]), LOAD_FRAC(val.loads[2]));
  2265. /* Display in kilobytes */
  2266. #define K(x) ((x) << (PAGE_SHIFT - 10))
  2267. kdb_printf("\nMemTotal: %8lu kB\nMemFree: %8lu kB\n"
  2268. "Buffers: %8lu kB\n",
  2269. K(val.totalram), K(val.freeram), K(val.bufferram));
  2270. return 0;
  2271. }
  2272. /*
  2273. * kdb_per_cpu - This function implements the 'per_cpu' command.
  2274. */
  2275. static int kdb_per_cpu(int argc, const char **argv)
  2276. {
  2277. char fmtstr[64];
  2278. int cpu, diag, nextarg = 1;
  2279. unsigned long addr, symaddr, val, bytesperword = 0, whichcpu = ~0UL;
  2280. if (argc < 1 || argc > 3)
  2281. return KDB_ARGCOUNT;
  2282. diag = kdbgetaddrarg(argc, argv, &nextarg, &symaddr, NULL, NULL);
  2283. if (diag)
  2284. return diag;
  2285. if (argc >= 2) {
  2286. diag = kdbgetularg(argv[2], &bytesperword);
  2287. if (diag)
  2288. return diag;
  2289. }
  2290. if (!bytesperword)
  2291. bytesperword = KDB_WORD_SIZE;
  2292. else if (bytesperword > KDB_WORD_SIZE)
  2293. return KDB_BADWIDTH;
  2294. sprintf(fmtstr, "%%0%dlx ", (int)(2*bytesperword));
  2295. if (argc >= 3) {
  2296. diag = kdbgetularg(argv[3], &whichcpu);
  2297. if (diag)
  2298. return diag;
  2299. if (whichcpu >= nr_cpu_ids || !cpu_online(whichcpu)) {
  2300. kdb_printf("cpu %ld is not online\n", whichcpu);
  2301. return KDB_BADCPUNUM;
  2302. }
  2303. }
  2304. /* Most architectures use __per_cpu_offset[cpu], some use
  2305. * __per_cpu_offset(cpu), smp has no __per_cpu_offset.
  2306. */
  2307. #ifdef __per_cpu_offset
  2308. #define KDB_PCU(cpu) __per_cpu_offset(cpu)
  2309. #else
  2310. #ifdef CONFIG_SMP
  2311. #define KDB_PCU(cpu) __per_cpu_offset[cpu]
  2312. #else
  2313. #define KDB_PCU(cpu) 0
  2314. #endif
  2315. #endif
  2316. for_each_online_cpu(cpu) {
  2317. if (KDB_FLAG(CMD_INTERRUPT))
  2318. return 0;
  2319. if (whichcpu != ~0UL && whichcpu != cpu)
  2320. continue;
  2321. addr = symaddr + KDB_PCU(cpu);
  2322. diag = kdb_getword(&val, addr, bytesperword);
  2323. if (diag) {
  2324. kdb_printf("%5d " kdb_bfd_vma_fmt0 " - unable to "
  2325. "read, diag=%d\n", cpu, addr, diag);
  2326. continue;
  2327. }
  2328. kdb_printf("%5d ", cpu);
  2329. kdb_md_line(fmtstr, addr,
  2330. bytesperword == KDB_WORD_SIZE,
  2331. 1, bytesperword, 1, 1, 0);
  2332. }
  2333. #undef KDB_PCU
  2334. return 0;
  2335. }
  2336. /*
  2337. * display help for the use of cmd | grep pattern
  2338. */
  2339. static int kdb_grep_help(int argc, const char **argv)
  2340. {
  2341. kdb_printf("Usage of cmd args | grep pattern:\n");
  2342. kdb_printf(" Any command's output may be filtered through an ");
  2343. kdb_printf("emulated 'pipe'.\n");
  2344. kdb_printf(" 'grep' is just a key word.\n");
  2345. kdb_printf(" The pattern may include a very limited set of "
  2346. "metacharacters:\n");
  2347. kdb_printf(" pattern or ^pattern or pattern$ or ^pattern$\n");
  2348. kdb_printf(" And if there are spaces in the pattern, you may "
  2349. "quote it:\n");
  2350. kdb_printf(" \"pat tern\" or \"^pat tern\" or \"pat tern$\""
  2351. " or \"^pat tern$\"\n");
  2352. return 0;
  2353. }
  2354. /**
  2355. * kdb_register() - This function is used to register a kernel debugger
  2356. * command.
  2357. * @cmd: pointer to kdb command
  2358. *
  2359. * Note that it's the job of the caller to keep the memory for the cmd
  2360. * allocated until unregister is called.
  2361. */
  2362. int kdb_register(kdbtab_t *cmd)
  2363. {
  2364. kdbtab_t *kp;
  2365. list_for_each_entry(kp, &kdb_cmds_head, list_node) {
  2366. if (strcmp(kp->name, cmd->name) == 0) {
  2367. kdb_printf("Duplicate kdb cmd: %s, func %p help %s\n",
  2368. cmd->name, cmd->func, cmd->help);
  2369. return 1;
  2370. }
  2371. }
  2372. list_add_tail(&cmd->list_node, &kdb_cmds_head);
  2373. return 0;
  2374. }
  2375. EXPORT_SYMBOL_GPL(kdb_register);
  2376. /**
  2377. * kdb_register_table() - This function is used to register a kdb command
  2378. * table.
  2379. * @kp: pointer to kdb command table
  2380. * @len: length of kdb command table
  2381. */
  2382. void kdb_register_table(kdbtab_t *kp, size_t len)
  2383. {
  2384. while (len--) {
  2385. list_add_tail(&kp->list_node, &kdb_cmds_head);
  2386. kp++;
  2387. }
  2388. }
  2389. /**
  2390. * kdb_unregister() - This function is used to unregister a kernel debugger
  2391. * command. It is generally called when a module which
  2392. * implements kdb command is unloaded.
  2393. * @cmd: pointer to kdb command
  2394. */
  2395. void kdb_unregister(kdbtab_t *cmd)
  2396. {
  2397. list_del(&cmd->list_node);
  2398. }
  2399. EXPORT_SYMBOL_GPL(kdb_unregister);
  2400. static kdbtab_t maintab[] = {
  2401. { .name = "md",
  2402. .func = kdb_md,
  2403. .usage = "<vaddr>",
  2404. .help = "Display Memory Contents, also mdWcN, e.g. md8c1",
  2405. .minlen = 1,
  2406. .flags = KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS,
  2407. },
  2408. { .name = "mdr",
  2409. .func = kdb_md,
  2410. .usage = "<vaddr> <bytes>",
  2411. .help = "Display Raw Memory",
  2412. .flags = KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS,
  2413. },
  2414. { .name = "mdp",
  2415. .func = kdb_md,
  2416. .usage = "<paddr> <bytes>",
  2417. .help = "Display Physical Memory",
  2418. .flags = KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS,
  2419. },
  2420. { .name = "mds",
  2421. .func = kdb_md,
  2422. .usage = "<vaddr>",
  2423. .help = "Display Memory Symbolically",
  2424. .flags = KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS,
  2425. },
  2426. { .name = "mm",
  2427. .func = kdb_mm,
  2428. .usage = "<vaddr> <contents>",
  2429. .help = "Modify Memory Contents",
  2430. .flags = KDB_ENABLE_MEM_WRITE | KDB_REPEAT_NO_ARGS,
  2431. },
  2432. { .name = "go",
  2433. .func = kdb_go,
  2434. .usage = "[<vaddr>]",
  2435. .help = "Continue Execution",
  2436. .minlen = 1,
  2437. .flags = KDB_ENABLE_REG_WRITE |
  2438. KDB_ENABLE_ALWAYS_SAFE_NO_ARGS,
  2439. },
  2440. { .name = "rd",
  2441. .func = kdb_rd,
  2442. .usage = "",
  2443. .help = "Display Registers",
  2444. .flags = KDB_ENABLE_REG_READ,
  2445. },
  2446. { .name = "rm",
  2447. .func = kdb_rm,
  2448. .usage = "<reg> <contents>",
  2449. .help = "Modify Registers",
  2450. .flags = KDB_ENABLE_REG_WRITE,
  2451. },
  2452. { .name = "ef",
  2453. .func = kdb_ef,
  2454. .usage = "<vaddr>",
  2455. .help = "Display exception frame",
  2456. .flags = KDB_ENABLE_MEM_READ,
  2457. },
  2458. { .name = "bt",
  2459. .func = kdb_bt,
  2460. .usage = "[<vaddr>]",
  2461. .help = "Stack traceback",
  2462. .minlen = 1,
  2463. .flags = KDB_ENABLE_MEM_READ | KDB_ENABLE_INSPECT_NO_ARGS,
  2464. },
  2465. { .name = "btp",
  2466. .func = kdb_bt,
  2467. .usage = "<pid>",
  2468. .help = "Display stack for process <pid>",
  2469. .flags = KDB_ENABLE_INSPECT,
  2470. },
  2471. { .name = "bta",
  2472. .func = kdb_bt,
  2473. .usage = "[<state_chars>|A]",
  2474. .help = "Backtrace all processes whose state matches",
  2475. .flags = KDB_ENABLE_INSPECT,
  2476. },
  2477. { .name = "btc",
  2478. .func = kdb_bt,
  2479. .usage = "",
  2480. .help = "Backtrace current process on each cpu",
  2481. .flags = KDB_ENABLE_INSPECT,
  2482. },
  2483. { .name = "btt",
  2484. .func = kdb_bt,
  2485. .usage = "<vaddr>",
  2486. .help = "Backtrace process given its struct task address",
  2487. .flags = KDB_ENABLE_MEM_READ | KDB_ENABLE_INSPECT_NO_ARGS,
  2488. },
  2489. { .name = "env",
  2490. .func = kdb_env,
  2491. .usage = "",
  2492. .help = "Show environment variables",
  2493. .flags = KDB_ENABLE_ALWAYS_SAFE,
  2494. },
  2495. { .name = "set",
  2496. .func = kdb_set,
  2497. .usage = "",
  2498. .help = "Set environment variables",
  2499. .flags = KDB_ENABLE_ALWAYS_SAFE,
  2500. },
  2501. { .name = "help",
  2502. .func = kdb_help,
  2503. .usage = "",
  2504. .help = "Display Help Message",
  2505. .minlen = 1,
  2506. .flags = KDB_ENABLE_ALWAYS_SAFE,
  2507. },
  2508. { .name = "?",
  2509. .func = kdb_help,
  2510. .usage = "",
  2511. .help = "Display Help Message",
  2512. .flags = KDB_ENABLE_ALWAYS_SAFE,
  2513. },
  2514. { .name = "cpu",
  2515. .func = kdb_cpu,
  2516. .usage = "<cpunum>",
  2517. .help = "Switch to new cpu",
  2518. .flags = KDB_ENABLE_ALWAYS_SAFE_NO_ARGS,
  2519. },
  2520. { .name = "kgdb",
  2521. .func = kdb_kgdb,
  2522. .usage = "",
  2523. .help = "Enter kgdb mode",
  2524. .flags = 0,
  2525. },
  2526. { .name = "ps",
  2527. .func = kdb_ps,
  2528. .usage = "[<state_chars>|A]",
  2529. .help = "Display active task list",
  2530. .flags = KDB_ENABLE_INSPECT,
  2531. },
  2532. { .name = "pid",
  2533. .func = kdb_pid,
  2534. .usage = "<pidnum>",
  2535. .help = "Switch to another task",
  2536. .flags = KDB_ENABLE_INSPECT,
  2537. },
  2538. { .name = "reboot",
  2539. .func = kdb_reboot,
  2540. .usage = "",
  2541. .help = "Reboot the machine immediately",
  2542. .flags = KDB_ENABLE_REBOOT,
  2543. },
  2544. #if defined(CONFIG_MODULES)
  2545. { .name = "lsmod",
  2546. .func = kdb_lsmod,
  2547. .usage = "",
  2548. .help = "List loaded kernel modules",
  2549. .flags = KDB_ENABLE_INSPECT,
  2550. },
  2551. #endif
  2552. #if defined(CONFIG_MAGIC_SYSRQ)
  2553. { .name = "sr",
  2554. .func = kdb_sr,
  2555. .usage = "<key>",
  2556. .help = "Magic SysRq key",
  2557. .flags = KDB_ENABLE_ALWAYS_SAFE,
  2558. },
  2559. #endif
  2560. #if defined(CONFIG_PRINTK)
  2561. { .name = "dmesg",
  2562. .func = kdb_dmesg,
  2563. .usage = "[lines]",
  2564. .help = "Display syslog buffer",
  2565. .flags = KDB_ENABLE_ALWAYS_SAFE,
  2566. },
  2567. #endif
  2568. { .name = "defcmd",
  2569. .func = kdb_defcmd,
  2570. .usage = "name \"usage\" \"help\"",
  2571. .help = "Define a set of commands, down to endefcmd",
  2572. /*
  2573. * Macros are always safe because when executed each
  2574. * internal command re-enters kdb_parse() and is safety
  2575. * checked individually.
  2576. */
  2577. .flags = KDB_ENABLE_ALWAYS_SAFE,
  2578. },
  2579. { .name = "kill",
  2580. .func = kdb_kill,
  2581. .usage = "<-signal> <pid>",
  2582. .help = "Send a signal to a process",
  2583. .flags = KDB_ENABLE_SIGNAL,
  2584. },
  2585. { .name = "summary",
  2586. .func = kdb_summary,
  2587. .usage = "",
  2588. .help = "Summarize the system",
  2589. .minlen = 4,
  2590. .flags = KDB_ENABLE_ALWAYS_SAFE,
  2591. },
  2592. { .name = "per_cpu",
  2593. .func = kdb_per_cpu,
  2594. .usage = "<sym> [<bytes>] [<cpu>]",
  2595. .help = "Display per_cpu variables",
  2596. .minlen = 3,
  2597. .flags = KDB_ENABLE_MEM_READ,
  2598. },
  2599. { .name = "grephelp",
  2600. .func = kdb_grep_help,
  2601. .usage = "",
  2602. .help = "Display help on | grep",
  2603. .flags = KDB_ENABLE_ALWAYS_SAFE,
  2604. },
  2605. };
  2606. static kdbtab_t nmicmd = {
  2607. .name = "disable_nmi",
  2608. .func = kdb_disable_nmi,
  2609. .usage = "",
  2610. .help = "Disable NMI entry to KDB",
  2611. .flags = KDB_ENABLE_ALWAYS_SAFE,
  2612. };
  2613. /* Initialize the kdb command table. */
  2614. static void __init kdb_inittab(void)
  2615. {
  2616. kdb_register_table(maintab, ARRAY_SIZE(maintab));
  2617. if (arch_kgdb_ops.enable_nmi)
  2618. kdb_register_table(&nmicmd, 1);
  2619. }
  2620. /* Execute any commands defined in kdb_cmds. */
  2621. static void __init kdb_cmd_init(void)
  2622. {
  2623. int i, diag;
  2624. for (i = 0; kdb_cmds[i]; ++i) {
  2625. diag = kdb_parse(kdb_cmds[i]);
  2626. if (diag)
  2627. kdb_printf("kdb command %s failed, kdb diag %d\n",
  2628. kdb_cmds[i], diag);
  2629. }
  2630. if (defcmd_in_progress) {
  2631. kdb_printf("Incomplete 'defcmd' set, forcing endefcmd\n");
  2632. kdb_parse("endefcmd");
  2633. }
  2634. }
  2635. /* Initialize kdb_printf, breakpoint tables and kdb state */
  2636. void __init kdb_init(int lvl)
  2637. {
  2638. static int kdb_init_lvl = KDB_NOT_INITIALIZED;
  2639. int i;
  2640. if (kdb_init_lvl == KDB_INIT_FULL || lvl <= kdb_init_lvl)
  2641. return;
  2642. for (i = kdb_init_lvl; i < lvl; i++) {
  2643. switch (i) {
  2644. case KDB_NOT_INITIALIZED:
  2645. kdb_inittab(); /* Initialize Command Table */
  2646. kdb_initbptab(); /* Initialize Breakpoints */
  2647. break;
  2648. case KDB_INIT_EARLY:
  2649. kdb_cmd_init(); /* Build kdb_cmds tables */
  2650. break;
  2651. }
  2652. }
  2653. kdb_init_lvl = lvl;
  2654. }