exception.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "exception.h"
  19. #include "hdfs/hdfs.h"
  20. #include "jni_helper.h"
  21. #include "platform.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #define EXCEPTION_INFO_LEN (sizeof(gExceptionInfo)/sizeof(gExceptionInfo[0]))
  26. struct ExceptionInfo {
  27. const char * const name;
  28. int noPrintFlag;
  29. int excErrno;
  30. };
  31. static const struct ExceptionInfo gExceptionInfo[] = {
  32. {
  33. "java.io.FileNotFoundException",
  34. NOPRINT_EXC_FILE_NOT_FOUND,
  35. ENOENT,
  36. },
  37. {
  38. "org.apache.hadoop.security.AccessControlException",
  39. NOPRINT_EXC_ACCESS_CONTROL,
  40. EACCES,
  41. },
  42. {
  43. "org.apache.hadoop.fs.UnresolvedLinkException",
  44. NOPRINT_EXC_UNRESOLVED_LINK,
  45. ENOLINK,
  46. },
  47. {
  48. "org.apache.hadoop.fs.ParentNotDirectoryException",
  49. NOPRINT_EXC_PARENT_NOT_DIRECTORY,
  50. ENOTDIR,
  51. },
  52. {
  53. "java.lang.IllegalArgumentException",
  54. NOPRINT_EXC_ILLEGAL_ARGUMENT,
  55. EINVAL,
  56. },
  57. {
  58. "java.lang.OutOfMemoryError",
  59. 0,
  60. ENOMEM,
  61. },
  62. {
  63. "org.apache.hadoop.hdfs.server.namenode.SafeModeException",
  64. 0,
  65. EROFS,
  66. },
  67. {
  68. "org.apache.hadoop.fs.FileAlreadyExistsException",
  69. 0,
  70. EEXIST,
  71. },
  72. {
  73. "org.apache.hadoop.hdfs.protocol.QuotaExceededException",
  74. 0,
  75. EDQUOT,
  76. },
  77. {
  78. "java.lang.UnsupportedOperationException",
  79. 0,
  80. ENOTSUP,
  81. },
  82. {
  83. "org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException",
  84. 0,
  85. ESTALE,
  86. },
  87. };
  88. void getExceptionInfo(const char *excName, int noPrintFlags,
  89. int *excErrno, int *shouldPrint)
  90. {
  91. int i;
  92. for (i = 0; i < EXCEPTION_INFO_LEN; i++) {
  93. if (strstr(gExceptionInfo[i].name, excName)) {
  94. break;
  95. }
  96. }
  97. if (i < EXCEPTION_INFO_LEN) {
  98. *shouldPrint = !(gExceptionInfo[i].noPrintFlag & noPrintFlags);
  99. *excErrno = gExceptionInfo[i].excErrno;
  100. } else {
  101. *shouldPrint = 1;
  102. *excErrno = EINTERNAL;
  103. }
  104. }
  105. /**
  106. * getExceptionUtilString: A helper function that calls 'methodName' in
  107. * ExceptionUtils. The function 'methodName' should have a return type of a
  108. * java String.
  109. *
  110. * @param env The JNI environment.
  111. * @param exc The exception to get information for.
  112. * @param methodName The method of ExceptionUtils to call that has a String
  113. * return type.
  114. *
  115. * @return A C-type string containing the string returned by
  116. * ExceptionUtils.'methodName', or NULL on failure.
  117. */
  118. static char* getExceptionUtilString(JNIEnv *env, jthrowable exc, char *methodName)
  119. {
  120. jthrowable jthr;
  121. jvalue jVal;
  122. jstring jStr = NULL;
  123. char *excString = NULL;
  124. jthr = invokeMethod(env, &jVal, STATIC, NULL,
  125. "org/apache/commons/lang/exception/ExceptionUtils",
  126. methodName, "(Ljava/lang/Throwable;)Ljava/lang/String;", exc);
  127. if (jthr) {
  128. destroyLocalReference(env, jthr);
  129. return NULL;
  130. }
  131. jStr = jVal.l;
  132. jthr = newCStr(env, jStr, &excString);
  133. if (jthr) {
  134. destroyLocalReference(env, jthr);
  135. return NULL;
  136. }
  137. destroyLocalReference(env, jStr);
  138. return excString;
  139. }
  140. int printExceptionAndFreeV(JNIEnv *env, jthrowable exc, int noPrintFlags,
  141. const char *fmt, va_list ap)
  142. {
  143. int i, noPrint, excErrno;
  144. char *className = NULL;
  145. jthrowable jthr;
  146. const char *stackTrace;
  147. const char *rootCause;
  148. jthr = classNameOfObject(exc, env, &className);
  149. if (jthr) {
  150. fprintf(stderr, "PrintExceptionAndFree: error determining class name "
  151. "of exception.\n");
  152. className = strdup("(unknown)");
  153. destroyLocalReference(env, jthr);
  154. }
  155. for (i = 0; i < EXCEPTION_INFO_LEN; i++) {
  156. if (!strcmp(gExceptionInfo[i].name, className)) {
  157. break;
  158. }
  159. }
  160. if (i < EXCEPTION_INFO_LEN) {
  161. noPrint = (gExceptionInfo[i].noPrintFlag & noPrintFlags);
  162. excErrno = gExceptionInfo[i].excErrno;
  163. } else {
  164. noPrint = 0;
  165. excErrno = EINTERNAL;
  166. }
  167. // We don't want to use ExceptionDescribe here, because that requires a
  168. // pending exception. Instead, use ExceptionUtils.
  169. rootCause = getExceptionUtilString(env, exc, "getRootCauseMessage");
  170. stackTrace = getExceptionUtilString(env, exc, "getStackTrace");
  171. // Save the exception details in the thread-local state.
  172. setTLSExceptionStrings(rootCause, stackTrace);
  173. if (!noPrint) {
  174. vfprintf(stderr, fmt, ap);
  175. fprintf(stderr, " error:\n");
  176. if (!rootCause) {
  177. fprintf(stderr, "(unable to get root cause for %s)\n", className);
  178. } else {
  179. fprintf(stderr, "%s", rootCause);
  180. }
  181. if (!stackTrace) {
  182. fprintf(stderr, "(unable to get stack trace for %s)\n", className);
  183. } else {
  184. fprintf(stderr, "%s", stackTrace);
  185. }
  186. }
  187. destroyLocalReference(env, exc);
  188. free(className);
  189. return excErrno;
  190. }
  191. int printExceptionAndFree(JNIEnv *env, jthrowable exc, int noPrintFlags,
  192. const char *fmt, ...)
  193. {
  194. va_list ap;
  195. int ret;
  196. va_start(ap, fmt);
  197. ret = printExceptionAndFreeV(env, exc, noPrintFlags, fmt, ap);
  198. va_end(ap);
  199. return ret;
  200. }
  201. int printPendingExceptionAndFree(JNIEnv *env, int noPrintFlags,
  202. const char *fmt, ...)
  203. {
  204. va_list ap;
  205. int ret;
  206. jthrowable exc;
  207. exc = (*env)->ExceptionOccurred(env);
  208. if (!exc) {
  209. va_start(ap, fmt);
  210. vfprintf(stderr, fmt, ap);
  211. va_end(ap);
  212. fprintf(stderr, " error: (no exception)");
  213. ret = 0;
  214. } else {
  215. (*env)->ExceptionClear(env);
  216. va_start(ap, fmt);
  217. ret = printExceptionAndFreeV(env, exc, noPrintFlags, fmt, ap);
  218. va_end(ap);
  219. }
  220. return ret;
  221. }
  222. jthrowable getPendingExceptionAndClear(JNIEnv *env)
  223. {
  224. jthrowable jthr = (*env)->ExceptionOccurred(env);
  225. if (!jthr)
  226. return NULL;
  227. (*env)->ExceptionClear(env);
  228. return jthr;
  229. }
  230. jthrowable newRuntimeError(JNIEnv *env, const char *fmt, ...)
  231. {
  232. char buf[512];
  233. jobject out, exc;
  234. jstring jstr;
  235. va_list ap;
  236. va_start(ap, fmt);
  237. vsnprintf(buf, sizeof(buf), fmt, ap);
  238. va_end(ap);
  239. jstr = (*env)->NewStringUTF(env, buf);
  240. if (!jstr) {
  241. // We got an out of memory exception rather than a RuntimeException.
  242. // Too bad...
  243. return getPendingExceptionAndClear(env);
  244. }
  245. exc = constructNewObjectOfClass(env, &out, "RuntimeException",
  246. "(java/lang/String;)V", jstr);
  247. (*env)->DeleteLocalRef(env, jstr);
  248. // Again, we'll either get an out of memory exception or the
  249. // RuntimeException we wanted.
  250. return (exc) ? exc : out;
  251. }